Skip to content

Commit bceea92

Browse files
authored
Merge pull request #38 from thedadams/clicky-serves-updates
fix: add compatibility for the new clicky-serves implementation
2 parents 9dcfa68 + c753d94 commit bceea92

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ To ensure it is working properly, you can run the following command:
2727
npm exec -c "gptscript https://get.gptscript.ai/echo.gpt --input 'Hello, World!'"
2828
```
2929

30-
you will see "Hello, World!" in the output of the command.
30+
You will see "Hello, World!" in the output of the command.
3131

3232
## Client
3333

src/gptscript.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class Client {
8080
* @return {Run} The Run object representing the running tool.
8181
*/
8282
run(toolName: string, opts: RunOpts = {}): Run {
83-
return (new Run("run-file-stream-with-events", toolName, "", opts, this.gptscriptBin, this.gptscriptURL)).nextChat(opts.input)
83+
return (new Run("run", toolName, "", opts, this.gptscriptBin, this.gptscriptURL)).nextChat(opts.input)
8484
}
8585

8686
/**
@@ -101,7 +101,7 @@ export class Client {
101101
toolString = toolDefToString(tool)
102102
}
103103

104-
return (new Run("run-tool-stream-with-event", "", toolString, opts, this.gptscriptBin, this.gptscriptURL)).nextChat(opts.input)
104+
return (new Run("evaluate", "", toolString, opts, this.gptscriptBin, this.gptscriptURL)).nextChat(opts.input)
105105
}
106106

107107
async parse(fileName: string): Promise<Block[]> {
@@ -117,7 +117,7 @@ export class Client {
117117
async parseTool(toolContent: string): Promise<Block[]> {
118118
const r: Run = new RunSubcommand("parse", "", toolContent, {}, this.gptscriptBin, this.gptscriptURL)
119119
if (this.gptscriptURL) {
120-
r.request({input: toolContent})
120+
r.request({content: toolContent})
121121
} else {
122122
await r.exec(["parse"])
123123
}
@@ -196,29 +196,25 @@ export class Run {
196196

197197
let run = this
198198
if (run.state !== RunState.Creating) {
199-
run = new (this.constructor as any)(run.requestPath, run.filePath, run.content, run.opts, run.gptscriptURL)
199+
run = new (this.constructor as any)(this.requestPath, this.filePath, this.content, this.opts, this.bin, this.gptscriptURL)
200200
}
201201

202202
run.chatState = this.chatState
203203
run.opts.input = input
204204
if (run.gptscriptURL) {
205205
if (run.content !== "") {
206-
run.request({content: this.content})
206+
run.request({content: this.content, chatState: JSON.stringify(run.chatState)})
207207
} else {
208-
run.request({file: this.filePath})
208+
run.request({file: this.filePath, chatState: JSON.stringify(run.chatState)})
209209
}
210210
} else {
211-
run.exec().catch((e) => {
212-
run.err = e.toString()
213-
run.state = RunState.Error
214-
}
215-
)
211+
run.exec()
216212
}
217213

218214
return run
219215
}
220216

221-
async exec(extraArgs: string[] = [], env: NodeJS.Dict<string> = process.env) {
217+
exec(extraArgs: string[] = [], env: NodeJS.Dict<string> = process.env) {
222218
extraArgs.push(...toArgs(this.opts))
223219
extraArgs.push("--chat-state=" + (this.chatState ? JSON.stringify(this.chatState) : "null"))
224220
this.chatState = undefined
@@ -406,9 +402,16 @@ export class Run {
406402
})
407403

408404
this.sse.addEventListener("close", () => {
409-
if (this.state === RunState.Running || this.state === RunState.Finished) {
410-
this.state = RunState.Finished
411-
resolve(this.stdout || "")
405+
if (this.state === RunState.Running || this.state === RunState.Finished || this.state === RunState.Continue) {
406+
if (this.stdout) {
407+
if (this.state !== RunState.Continue) {
408+
this.state = RunState.Finished
409+
}
410+
resolve(this.stdout)
411+
} else {
412+
this.state = RunState.Error
413+
reject(this.stderr)
414+
}
412415
} else if (this.state === RunState.Error) {
413416
reject(this.err)
414417
}
@@ -458,9 +461,16 @@ export class Run {
458461
})
459462

460463
res.on("end", () => {
461-
if (this.state === RunState.Running || this.state === RunState.Finished) {
462-
this.state = RunState.Finished
463-
resolve(this.stdout || "")
464+
if (this.state === RunState.Running || this.state === RunState.Finished || this.state === RunState.Continue) {
465+
if (this.stdout) {
466+
if (this.state !== RunState.Continue) {
467+
this.state = RunState.Finished
468+
}
469+
resolve(this.stdout)
470+
} else {
471+
this.state = RunState.Error
472+
reject(this.stderr)
473+
}
464474
} else if (this.state === RunState.Error) {
465475
reject(this.err)
466476
}

tests/fixtures/global-tools.gpt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
!title
2+
3+
Runbook 3
4+
5+
---
6+
Name: tool_1
7+
Global Tools: sys.workspace.ls, sys.workspace.read, sys.workspace.write, github.com/gptscript-ai/knowledge, github.com/drpebcak/duckdb, github.com/gptscript-ai/browser, github.com/gptscript-ai/browser-search/google, github.com/gptscript-ai/browser-search/google-question-answerer
8+
9+
Say "Hello!"
10+
11+
---
12+
Name: tool_2
13+
14+
What time is it?
15+
16+
---
17+
Name: tool_3
18+
19+
Give me a paragraph of lorem ipsum

tests/gptscript.test.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ describe("gptscript module", () => {
115115
expect(err).toEqual("")
116116
})
117117

118+
test("run executes and streams a file with global tools correctly", async () => {
119+
let out = ""
120+
let err = undefined
121+
const testGptPath = path.join(__dirname, "fixtures", "global-tools.gpt")
122+
const opts = {
123+
disableCache: true,
124+
}
125+
126+
try {
127+
const run = client.run(testGptPath, opts)
128+
run.on(gptscript.RunEventType.CallProgress, data => {
129+
out += `system: ${(data as any).content}`
130+
})
131+
await run.text()
132+
err = run.err
133+
} catch (e) {
134+
console.error(e)
135+
}
136+
137+
expect(out).toContain("Hello!")
138+
expect(err).toEqual("")
139+
}, 15000)
140+
118141
test("aborting a run is reported correctly", async () => {
119142
let errMessage = ""
120143
let err = undefined
@@ -338,15 +361,4 @@ describe("gptscript module", () => {
338361
expect(run.state).toEqual(gptscript.RunState.Finished)
339362
expect(err).toEqual("")
340363
}, 60000)
341-
342-
test("with workspace", async () => {
343-
const t0 = {
344-
tools: ["sys.workspace.ls", "sys.workspace.write"],
345-
instructions: "Write a file named 'test.txt' in the workspace with contents 'Hello!' and then list the files in the workspace.",
346-
} as any
347-
348-
const response = await client.evaluate(t0, {workspace: "./workspace"}).text()
349-
expect(response).toBeDefined()
350-
expect(response).toContain("test.txt")
351-
}, 30000)
352364
})

0 commit comments

Comments
 (0)