Skip to content

Commit

Permalink
feat: add ability to stat files in workspace API
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Oct 28, 2024
1 parent 8ef0412 commit 306861b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,20 @@ export class GPTScript {
return Buffer.from(out.trim(), "base64")
}

async statFileInWorkspace(filePath: string, workspaceID?: string): Promise<FileInfo> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}
const out = await this.runBasicCommand("workspaces/stat-file", {
id: workspaceID,
filePath: filePath,
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})

return JSON.parse(out)
}

/**
* Helper method to handle the common logic for loading.
*
Expand Down Expand Up @@ -590,6 +604,13 @@ export class GPTScript {
}
}

export interface FileInfo {
workspaceID: string
name: string
size: number
modTime: string
}

export class Run {
public readonly id: string
public readonly opts: RunOpts
Expand Down
16 changes: 16 additions & 0 deletions tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,14 @@ describe("gptscript module", () => {
await g.writeFileInWorkspace("test.txt", Buffer.from("test"), workspaceID)
const content = await g.readFileInWorkspace("test.txt", workspaceID)
expect(content.toString()).toEqual("test")

const fileInfo = await g.statFileInWorkspace("test.txt", workspaceID)
expect(fileInfo.size).toEqual(4)
expect(fileInfo.name).toEqual("test.txt")
expect(fileInfo.workspaceID).toEqual(workspaceID)
expect(fileInfo.modTime).toBeDefined()

await g.deleteFileInWorkspace("test.txt", workspaceID)
await g.deleteWorkspace(workspaceID)
}, 60000)

Expand Down Expand Up @@ -1034,6 +1042,14 @@ describe("gptscript module", () => {
await g.writeFileInWorkspace("test.txt", Buffer.from("test"), workspaceID)
const content = await g.readFileInWorkspace("test.txt", workspaceID)
expect(content.toString()).toEqual("test")

const fileInfo = await g.statFileInWorkspace("test.txt", workspaceID)
expect(fileInfo.size).toEqual(4)
expect(fileInfo.name).toEqual("test.txt")
expect(fileInfo.workspaceID).toEqual(workspaceID)
expect(fileInfo.modTime).toBeDefined()

await g.deleteFileInWorkspace("test.txt", workspaceID)
await g.deleteWorkspace(workspaceID)
}, 60000)

Expand Down

0 comments on commit 306861b

Please sign in to comment.