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

feat: add workspace API #97

Merged
merged 3 commits into from
Oct 24, 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
209 changes: 145 additions & 64 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GlobalOpts {
DefaultModel?: string
DefaultModelProvider?: string
DatasetToolRepo?: string
WorkspaceTool?: string
Env?: string[]
}

Expand Down Expand Up @@ -140,9 +141,12 @@ export class GPTScript {
if (!this.opts.URL) {
this.opts.URL = GPTScript.serverURL
}
if (this.opts.URL !== "" && !this.opts.URL.startsWith("http://") && !this.opts.URL.startsWith("https://")) {
this.opts.URL = "http://" + this.opts.URL
}

if (!this.opts.Env) {
this.opts.Env = []
this.opts.Env = Object.entries(process.env).map(([k, v]) => `${k}=${v}`)
}
if (this.opts.URL) {
this.opts.Env.push(`GPTSCRIPT_URL=${this.opts.URL}`)
Expand Down Expand Up @@ -362,113 +366,187 @@ export class GPTScript {
}

async createCredential(credential: Credential): Promise<void> {
if (!this.opts.URL) {
await this.testGPTScriptURL(20)
}

const r: Run = new RunSubcommand("credentials/create", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({content: credentialToJSON(credential)})
await r.text()
await this.runBasicCommand("credentials/create", {
content: credentialToJSON(credential)
})
}

async revealCredential(context: Array<string>, name: string): Promise<Credential> {
if (!this.opts.URL) {
await this.testGPTScriptURL(20)
}

const r: Run = new RunSubcommand("credentials/reveal", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({context, name})
return jsonToCredential(await r.text())
const resp = await this.runBasicCommand("credentials/reveal", {
context,
name
})
return jsonToCredential(resp)
}

async deleteCredential(context: string, name: string): Promise<void> {
if (!this.opts.URL) {
await this.testGPTScriptURL(20)
}

const r: Run = new RunSubcommand("credentials/delete", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({context: [context], name})
await r.text()
await this.runBasicCommand("credentials/delete", {
context: [context],
name
})
}

// Dataset methods

async listDatasets(workspace: string): Promise<Array<DatasetMeta>> {
if (workspace == "") {
workspace = process.env.GPTSCRIPT_WORKSPACE_DIR ?? ""
async listDatasets(workspaceID: string): Promise<Array<DatasetMeta>> {
if (workspaceID == "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}

const r: Run = new RunSubcommand("datasets", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({input: "{}", workspace: workspace, datasetToolRepo: this.opts.DatasetToolRepo ?? ""})
const result = await r.text()
const result = await this.runBasicCommand("datasets", {
workspaceID: workspaceID,
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
env: this.opts.Env
})
return JSON.parse(result) as Array<DatasetMeta>
}

async createDataset(workspace: string, name: string, description: string): Promise<Dataset> {
if (workspace == "") {
workspace = process.env.GPTSCRIPT_WORKSPACE_DIR ?? ""
async createDataset(workspaceID: string, name: string, description: string): Promise<Dataset> {
if (workspaceID == "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}

const r: Run = new RunSubcommand("datasets/create", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({
const result = await this.runBasicCommand("datasets/create", {
input: JSON.stringify({datasetName: name, datasetDescription: description}),
workspace: workspace,
datasetToolRepo: this.opts.DatasetToolRepo ?? ""
workspaceID: workspaceID,
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
env: this.opts.Env
})
const result = await r.text()
return JSON.parse(result) as Dataset
}

async addDatasetElement(workspace: string, datasetID: string, elementName: string, elementDescription: string, elementContent: string): Promise<DatasetElementMeta> {
if (workspace == "") {
workspace = process.env.GPTSCRIPT_WORKSPACE_DIR ?? ""
async addDatasetElement(workspaceID: string, datasetID: string, elementName: string, elementDescription: string, elementContent: string): Promise<DatasetElementMeta> {
if (workspaceID == "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}

const r: Run = new RunSubcommand("datasets/add-element", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({
const result = await this.runBasicCommand("datasets/add-element", {
input: JSON.stringify({
datasetID,
elementName,
elementDescription,
elementContent
elementName: elementName,
elementDescription: elementDescription,
elementContent: elementContent
}),
workspace: workspace,
datasetToolRepo: this.opts.DatasetToolRepo ?? ""
workspaceID: workspaceID,
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
env: this.opts.Env
})
const result = await r.text()
return JSON.parse(result) as DatasetElementMeta
}

async listDatasetElements(workspace: string, datasetID: string): Promise<Array<DatasetElementMeta>> {
if (workspace == "") {
workspace = process.env.GPTSCRIPT_WORKSPACE_DIR ?? ""
async listDatasetElements(workspaceID: string, datasetID: string): Promise<Array<DatasetElementMeta>> {
if (workspaceID == "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}

const r: Run = new RunSubcommand("datasets/list-elements", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({

const result = await this.runBasicCommand("datasets/list-elements", {
input: JSON.stringify({datasetID}),
workspace: workspace,
datasetToolRepo: this.opts.DatasetToolRepo ?? ""
workspaceID: workspaceID,
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
env: this.opts.Env
})
const result = await r.text()
return JSON.parse(result) as Array<DatasetElementMeta>
}

async getDatasetElement(workspace: string, datasetID: string, elementName: string): Promise<DatasetElement> {
if (workspace == "") {
workspace = process.env.GPTSCRIPT_WORKSPACE_DIR ?? ""
async getDatasetElement(workspaceID: string, datasetID: string, elementName: string): Promise<DatasetElement> {
if (workspaceID == "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}

const r: Run = new RunSubcommand("datasets/get-element", "", {URL: this.opts.URL, Token: this.opts.Token})
r.request({
const result = await this.runBasicCommand("datasets/get-element", {
input: JSON.stringify({datasetID, element: elementName}),
workspace: workspace,
datasetToolRepo: this.opts.DatasetToolRepo ?? ""
workspaceID: workspaceID,
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
env: this.opts.Env
})
const result = await r.text()
return JSON.parse(result) as DatasetElement
}

async createWorkspace(providerType: string, ...fromWorkspaces: string[]): Promise<string> {
const out = await this.runBasicCommand("workspaces/create", {
providerType: providerType,
fromWorkspaceIDs: fromWorkspaces,
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})
return out.trim()
}

async deleteWorkspace(workspaceID?: string): Promise<void> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}
await this.runBasicCommand("workspaces/delete", {
id: workspaceID,
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})
}

async listFilesInWorkspace(prefix?: string, workspaceID?: string): Promise<Array<string>> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}
const out = await this.runBasicCommand("workspaces/list", {
id: workspaceID,
prefix: prefix,
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})
return JSON.parse(out)
}

async removeAll(withPrefix?: string, workspaceID?: string): Promise<void> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}
await this.runBasicCommand("workspaces/remove-all-with-prefix", {
id: workspaceID,
prefix: withPrefix,
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})
}

async writeFileInWorkspace(filePath: string, content: ArrayBuffer, workspaceID?: string): Promise<void> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}
await this.runBasicCommand("workspaces/write-file", {
id: workspaceID,
filePath: filePath,
contents: Buffer.from(content).toString("base64"),
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})
}

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

async readFileInWorkspace(filePath: string, workspaceID?: string): Promise<ArrayBuffer> {
if (!workspaceID) {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}
const out = await this.runBasicCommand("workspaces/read-file", {
id: workspaceID,
filePath: filePath,
workspaceTool: this.opts.WorkspaceTool,
env: this.opts.Env,
})
return Buffer.from(out.trim(), "base64")
}

/**
* Helper method to handle the common logic for loading.
*
Expand Down Expand Up @@ -694,7 +772,10 @@ export class Run {
fetch(req).then(resp => {
return resp.json()
}).then(res => {
resolve(res.stdout)
if (typeof res.stdout === "string") {
resolve(res.stdout)
}
resolve(JSON.stringify(res.stdout))
}).catch(e => {
reject(new Error(e))
})
Expand Down
Loading