Skip to content

Commit 306861b

Browse files
committed
feat: add ability to stat files in workspace API
Signed-off-by: Donnie Adams <[email protected]>
1 parent 8ef0412 commit 306861b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/gptscript.ts

+21
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,20 @@ export class GPTScript {
548548
return Buffer.from(out.trim(), "base64")
549549
}
550550

551+
async statFileInWorkspace(filePath: string, workspaceID?: string): Promise<FileInfo> {
552+
if (!workspaceID) {
553+
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
554+
}
555+
const out = await this.runBasicCommand("workspaces/stat-file", {
556+
id: workspaceID,
557+
filePath: filePath,
558+
workspaceTool: this.opts.WorkspaceTool,
559+
env: this.opts.Env,
560+
})
561+
562+
return JSON.parse(out)
563+
}
564+
551565
/**
552566
* Helper method to handle the common logic for loading.
553567
*
@@ -590,6 +604,13 @@ export class GPTScript {
590604
}
591605
}
592606

607+
export interface FileInfo {
608+
workspaceID: string
609+
name: string
610+
size: number
611+
modTime: string
612+
}
613+
593614
export class Run {
594615
public readonly id: string
595616
public readonly opts: RunOpts

tests/gptscript.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,14 @@ describe("gptscript module", () => {
977977
await g.writeFileInWorkspace("test.txt", Buffer.from("test"), workspaceID)
978978
const content = await g.readFileInWorkspace("test.txt", workspaceID)
979979
expect(content.toString()).toEqual("test")
980+
981+
const fileInfo = await g.statFileInWorkspace("test.txt", workspaceID)
982+
expect(fileInfo.size).toEqual(4)
983+
expect(fileInfo.name).toEqual("test.txt")
984+
expect(fileInfo.workspaceID).toEqual(workspaceID)
985+
expect(fileInfo.modTime).toBeDefined()
986+
987+
await g.deleteFileInWorkspace("test.txt", workspaceID)
980988
await g.deleteWorkspace(workspaceID)
981989
}, 60000)
982990

@@ -1034,6 +1042,14 @@ describe("gptscript module", () => {
10341042
await g.writeFileInWorkspace("test.txt", Buffer.from("test"), workspaceID)
10351043
const content = await g.readFileInWorkspace("test.txt", workspaceID)
10361044
expect(content.toString()).toEqual("test")
1045+
1046+
const fileInfo = await g.statFileInWorkspace("test.txt", workspaceID)
1047+
expect(fileInfo.size).toEqual(4)
1048+
expect(fileInfo.name).toEqual("test.txt")
1049+
expect(fileInfo.workspaceID).toEqual(workspaceID)
1050+
expect(fileInfo.modTime).toBeDefined()
1051+
1052+
await g.deleteFileInWorkspace("test.txt", workspaceID)
10371053
await g.deleteWorkspace(workspaceID)
10381054
}, 60000)
10391055

0 commit comments

Comments
 (0)