Skip to content

Commit

Permalink
chore: update datasets API and use runBasicCommand
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Oct 24, 2024
1 parent 4deabc3 commit b42172c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 67 deletions.
81 changes: 40 additions & 41 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,79 +388,78 @@ export class GPTScript {

// 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
}

Expand Down
51 changes: 25 additions & 26 deletions tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import path from "path"
import {fileURLToPath} from "url"
import * as fs from "node:fs"
import {randomBytes} from "node:crypto"
import {tmpdir} from "node:os"

let gFirst: gptscript.GPTScript
let g: gptscript.GPTScript
Expand Down Expand Up @@ -660,7 +659,7 @@ describe("gptscript module", () => {
tools: ["sys.exec"]
}

const commands = [`"ls"`, `"dir"`]
const commands = [`ls`, `dir`]
let confirmCallCount = 0
const run = await g.evaluate(t, {confirm: true})
run.on(gptscript.RunEventType.CallConfirm, async (data: gptscript.CallFrame) => {
Expand All @@ -683,7 +682,7 @@ describe("gptscript module", () => {
}
const run = await g.evaluate(t, {confirm: true})
run.on(gptscript.RunEventType.CallConfirm, async (data: gptscript.CallFrame) => {
expect(data.input).toContain(`"ls"`)
expect(data.input).toContain(`ls`)
confirmFound = true
await g.confirm({id: data.id, accept: false, message: "I will not allow it!"})
})
Expand Down Expand Up @@ -889,12 +888,12 @@ describe("gptscript module", () => {

test("dataset operations", async () => {
const datasetName = "test-" + randomBytes(10).toString("hex")
const workspace = fs.mkdtempSync(path.join(tmpdir(), "node-gptscript-"))
const workspaceID = await g.createWorkspace("directory")
let datasetID: string

// Create
try {
const dataset = await g.createDataset(workspace, datasetName, "a test dataset")
const dataset = await g.createDataset(workspaceID, datasetName, "a test dataset")
expect(dataset).toBeDefined()
expect(dataset.name).toEqual(datasetName)
expect(dataset.description).toEqual("a test dataset")
Expand All @@ -908,7 +907,7 @@ describe("gptscript module", () => {
// Add elements
try {
const e1 = await g.addDatasetElement(
workspace,
workspaceID,
datasetID,
"element1",
"",
Expand All @@ -918,7 +917,7 @@ describe("gptscript module", () => {
expect(e1.description).toEqual("")

const e2 = await g.addDatasetElement(
workspace,
workspaceID,
datasetID,
"element2",
"a description",
Expand All @@ -932,12 +931,12 @@ describe("gptscript module", () => {

// Get elements
try {
const e1 = await g.getDatasetElement(workspace, datasetID, "element1")
const e1 = await g.getDatasetElement(workspaceID, datasetID, "element1")
expect(e1.name).toEqual("element1")
expect(e1.description).toBeUndefined()
expect(e1.contents).toEqual("this is element 1 contents")

const e2 = await g.getDatasetElement(workspace, datasetID, "element2")
const e2 = await g.getDatasetElement(workspaceID, datasetID, "element2")
expect(e2.name).toEqual("element2")
expect(e2.description).toEqual("a description")
expect(e2.contents).toEqual("this is element 2 contents")
Expand All @@ -947,7 +946,7 @@ describe("gptscript module", () => {

// List the elements in the dataset
try {
const elements = await g.listDatasetElements(workspace, datasetID)
const elements = await g.listDatasetElements(workspaceID, datasetID)
expect(elements.length).toEqual(2)
expect(elements.map(e => e.name)).toContain("element1")
expect(elements.map(e => e.name)).toContain("element2")
Expand All @@ -957,7 +956,7 @@ describe("gptscript module", () => {

// List datasets
try {
const datasets = await g.listDatasets(workspace)
const datasets = await g.listDatasets(workspaceID)
expect(datasets.length).toBeGreaterThan(0)
expect(datasets.map(d => d.name)).toContain(datasetName)
} catch (e) {
Expand All @@ -966,22 +965,12 @@ describe("gptscript module", () => {
}, 60000)

test("create and delete workspace", async () => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
console.log("AWS credentials not set, skipping test")
return
}

const workspaceID = await g.createWorkspace("directory")
expect(workspaceID).toBeDefined()
await g.deleteWorkspace(workspaceID)
}, 60000)

test("write, read, and delete file", async () => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
console.log("AWS credentials not set, skipping test")
return
}

const workspaceID = await g.createWorkspace("directory")
expect(workspaceID).toBeDefined()

Expand All @@ -992,11 +981,6 @@ describe("gptscript module", () => {
}, 60000)

test("test complex ls", async () => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
console.log("AWS credentials not set, skipping test")
return
}

const workspaceID = await g.createWorkspace("directory")

// Write files in the workspace
Expand Down Expand Up @@ -1028,12 +1012,22 @@ describe("gptscript module", () => {
}, 60000)

test("create and delete workspace in s3", async () => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
console.log("AWS credentials not set, skipping test")
return
}

const workspaceID = await g.createWorkspace("s3")
expect(workspaceID).toBeDefined()
await g.deleteWorkspace(workspaceID)
}, 60000)

test("write, read, and delete file in s3", async () => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
console.log("AWS credentials not set, skipping test")
return
}

const workspaceID = await g.createWorkspace("s3")
expect(workspaceID).toBeDefined()

Expand All @@ -1044,6 +1038,11 @@ describe("gptscript module", () => {
}, 60000)

test("test complex ls in s3", async () => {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
console.log("AWS credentials not set, skipping test")
return
}

const workspaceID = await g.createWorkspace("s3")

// Write files in the workspace
Expand Down

0 comments on commit b42172c

Please sign in to comment.