Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workspace file stat #98

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,11 @@ export class GPTScript {
return out.trim()
}

async deleteWorkspace(workspaceID?: string): Promise<void> {
async deleteWorkspace(workspaceID: string): Promise<void> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
return Promise.reject("workspace ID cannot be empty")
}

await this.runBasicCommand("workspaces/delete", {
id: workspaceID,
workspaceTool: this.opts.WorkspaceTool,
Expand Down Expand Up @@ -547,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 @@ -589,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