Skip to content

Commit ea42fed

Browse files
authored
chore: add addDatasetElements function (#99)
Signed-off-by: Grant Linville <[email protected]>
1 parent 306861b commit ea42fed

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/gptscript.ts

+13
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,19 @@ export class GPTScript {
434434
return JSON.parse(result) as DatasetElementMeta
435435
}
436436

437+
async addDatasetElements(workspaceID: string, datasetID: string, elements: Array<DatasetElement>) {
438+
if (workspaceID === "") {
439+
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
440+
}
441+
442+
return await this.runBasicCommand("datasets/add-elements", {
443+
input: JSON.stringify({datasetID, elements}),
444+
workspaceID: workspaceID,
445+
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
446+
env: this.opts.Env,
447+
})
448+
}
449+
437450
async listDatasetElements(workspaceID: string, datasetID: string): Promise<Array<DatasetElementMeta>> {
438451
if (workspaceID == "") {
439452
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""

tests/gptscript.test.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,28 @@ describe("gptscript module", () => {
929929
throw new Error("failed to add elements: " + e)
930930
}
931931

932+
// Add two elements at once.
933+
try {
934+
await g.addDatasetElements(
935+
workspaceID,
936+
datasetID,
937+
[
938+
{
939+
name: "element3",
940+
description: "a description",
941+
contents: "this is element 3 contents"
942+
},
943+
{
944+
name: "element4",
945+
description: "a description",
946+
contents: "this is element 4 contents"
947+
}
948+
]
949+
)
950+
} catch (e) {
951+
throw new Error("failed to add elements: " + e)
952+
}
953+
932954
// Get elements
933955
try {
934956
const e1 = await g.getDatasetElement(workspaceID, datasetID, "element1")
@@ -940,16 +962,28 @@ describe("gptscript module", () => {
940962
expect(e2.name).toEqual("element2")
941963
expect(e2.description).toEqual("a description")
942964
expect(e2.contents).toEqual("this is element 2 contents")
965+
966+
const e3 = await g.getDatasetElement(workspaceID, datasetID, "element3")
967+
expect(e3.name).toEqual("element3")
968+
expect(e3.description).toEqual("a description")
969+
expect(e3.contents).toEqual("this is element 3 contents")
970+
971+
const e4 = await g.getDatasetElement(workspaceID, datasetID, "element4")
972+
expect(e4.name).toEqual("element4")
973+
expect(e4.description).toEqual("a description")
974+
expect(e4.contents).toEqual("this is element 4 contents")
943975
} catch (e) {
944976
throw new Error("failed to get elements: " + e)
945977
}
946978

947979
// List the elements in the dataset
948980
try {
949981
const elements = await g.listDatasetElements(workspaceID, datasetID)
950-
expect(elements.length).toEqual(2)
982+
expect(elements.length).toEqual(4)
951983
expect(elements.map(e => e.name)).toContain("element1")
952984
expect(elements.map(e => e.name)).toContain("element2")
985+
expect(elements.map(e => e.name)).toContain("element3")
986+
expect(elements.map(e => e.name)).toContain("element4")
953987
} catch (e) {
954988
throw new Error("failed to list elements: " + e)
955989
}

0 commit comments

Comments
 (0)