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

chore: update prompt types for more granular configuration #106

Merged
merged 1 commit into from
Feb 4, 2025
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
30 changes: 20 additions & 10 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,11 @@ export class GPTScript {
return JSON.parse(result) as Array<DatasetMeta>
}

async addDatasetElements(elements: Array<DatasetElement>, opts: {name?: string, description?: string, datasetID?: string}): Promise<string> {
async addDatasetElements(elements: Array<DatasetElement>, opts: {
name?: string,
description?: string,
datasetID?: string
}): Promise<string> {
const serializableElements = elements.map(e => {
return {
name: e.name,
Expand Down Expand Up @@ -1136,11 +1140,17 @@ export interface PromptFrame {
type: RunEventType.Prompt
time: string
message: string
fields: string[]
fields: Field[]
sensitive: boolean
metadata: Record<string, string>
}

export interface Field {
name: string
description?: string
sensitive?: boolean
}

export type Frame = RunFrame | CallFrame | PromptFrame

export interface AuthResponse {
Expand Down Expand Up @@ -1322,19 +1332,19 @@ export function createServer(listener: http.RequestListener<typeof http.Incoming
const gptscriptCertB64 = process.env.GPTSCRIPT_CERT

if (!certB64) {
console.log('Missing CERT env var')
console.log("Missing CERT env var")
process.exit(1)
} else if (!privateKeyB64) {
console.log('Missing PRIVATE_KEY env var')
console.log("Missing PRIVATE_KEY env var")
process.exit(1)
} else if (!gptscriptCertB64) {
console.log('Missing GPTSCRIPT_CERT env var')
console.log("Missing GPTSCRIPT_CERT env var")
process.exit(1)
}

const cert = Buffer.from(certB64, 'base64').toString('utf-8')
const privateKey = Buffer.from(privateKeyB64, 'base64').toString('utf-8')
const gptscriptCert = Buffer.from(gptscriptCertB64, 'base64').toString('utf-8')
const cert = Buffer.from(certB64, "base64").toString("utf-8")
const privateKey = Buffer.from(privateKeyB64, "base64").toString("utf-8")
const gptscriptCert = Buffer.from(gptscriptCertB64, "base64").toString("utf-8")

const options = {
key: privateKey,
Expand All @@ -1350,11 +1360,11 @@ export function createServer(listener: http.RequestListener<typeof http.Incoming
export function startServer(server: https.Server) {
const port = process.env.PORT
if (!port) {
console.log('Missing PORT env var')
console.log("Missing PORT env var")
process.exit(1)
}

server.listen(parseInt(port, 10), '127.0.0.1', () => {
server.listen(parseInt(port, 10), "127.0.0.1", () => {
console.log(`Server listening on port ${port}`)
})
}
33 changes: 17 additions & 16 deletions tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("gptscript module", () => {

expect(run).toBeDefined()
expect(await run.text()).toContain("Understood.")
})
}, 10000)

test("evaluate executes and streams a prompt correctly", async () => {
let out = ""
Expand All @@ -129,7 +129,7 @@ describe("gptscript module", () => {
}

const run = await g.evaluate(t, opts)
run.on(gptscript.RunEventType.CallProgress, (data: gptscript.CallFrame) => {
run.on(gptscript.RunEventType.CallFinish, data => {
for (let output of data.output) out += `system: ${output.content}`
})

Expand Down Expand Up @@ -210,10 +210,11 @@ describe("gptscript module", () => {
}

const run = await g.run(testGptPath, opts)
run.on(gptscript.RunEventType.CallProgress, data => {
run.on(gptscript.RunEventType.CallFinish, data => {
for (let output of data.output) out += `system: ${output.content}`
})
await run.text()

expect(await run.text()).toContain("Calvin Coolidge")
err = run.err

for (let c in run.calls) {
Expand All @@ -231,23 +232,21 @@ describe("gptscript module", () => {

test("run executes and streams a file with global tools correctly", async () => {
let out = ""
let err = undefined
const testGptPath = path.join(__dirname, "fixtures", "global-tools.gpt")
const opts = {
disableCache: true,
credentialOverrides: ["github.com/gptscript-ai/gateway:OPENAI_API_KEY"]
}

const run = await g.run(testGptPath, opts)
run.on(gptscript.RunEventType.CallProgress, data => {
run.on(gptscript.RunEventType.CallFinish, data => {
for (let output of data.output) out += `system: ${output.content}`
})
await run.text()
err = run.err

expect(await run.text()).toContain("Hello!")
expect(run.err).toEqual("")
expect(out).toContain("Hello!")
expect(err).toEqual("")
}, 30000)
}, 60000)

test("aborting a run is reported correctly", async () => {
let errMessage = ""
Expand Down Expand Up @@ -627,7 +626,7 @@ describe("gptscript module", () => {
expect(await run.text()).toContain("Lake Huron")
expect(run.err).toEqual("")
expect(run.state).toEqual(gptscript.RunState.Continue)
}, 10000)
}, 15000)

test("nextChat on tool providing chat state", async () => {
const t = {
Expand All @@ -651,7 +650,7 @@ describe("gptscript module", () => {
expect(await run.text()).toContain("Austin")
expect(run.err).toEqual("")
expect(run.state).toEqual(gptscript.RunState.Continue)
}, 10000)
}, 15000)

test("confirm", async () => {
const t = {
Expand Down Expand Up @@ -702,11 +701,11 @@ describe("gptscript module", () => {
run.on(gptscript.RunEventType.Prompt, async (data: gptscript.PromptFrame) => {
expect(data.message).toContain("first name")
expect(data.fields.length).toEqual(1)
expect(data.fields[0]).toEqual("first name")
expect(data.fields[0].name).toEqual("first name")
expect(data.sensitive).toBeFalsy()

promptFound = true
await g.promptResponse({id: data.id, responses: {[data.fields[0]]: "Clicky"}})
await g.promptResponse({id: data.id, responses: {[data.fields[0].name]: "Clicky"}})
})

expect(await run.text()).toContain("Clicky")
Expand All @@ -722,12 +721,12 @@ describe("gptscript module", () => {
})
run.on(gptscript.RunEventType.Prompt, async (data: gptscript.PromptFrame) => {
expect(data.fields.length).toEqual(1)
expect(data.fields[0]).toEqual("first name")
expect(data.fields[0].name).toEqual("first name")
expect(data.metadata).toEqual({key: "value"})
expect(data.sensitive).toBeFalsy()

promptFound = true
await g.promptResponse({id: data.id, responses: {[data.fields[0]]: "Clicky"}})
await g.promptResponse({id: data.id, responses: {[data.fields[0].name]: "Clicky"}})
})

expect(await run.text()).toContain("Clicky")
Expand Down Expand Up @@ -968,6 +967,8 @@ describe("gptscript module", () => {
} catch (e) {
throw new Error("failed to list datasets: " + e)
}

client.close()
}, 60000)

test("create and delete workspace", async () => {
Expand Down