Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d0e4219

Browse files
committedJun 5, 2024··
chore: remove "to string" functions
The source of truth for converting a tool to a string should be the SDK. This change removes the "to string" functions to make the maintenance of the SDK simpler.
1 parent 6ae6961 commit d0e4219

File tree

1 file changed

+33
-95
lines changed

1 file changed

+33
-95
lines changed
 

‎src/gptscript.ts

+33-95
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class GPTScript {
9090
if (!this.ready) {
9191
this.ready = await this.testGPTScriptURL(20)
9292
}
93-
const r = new RunSubcommand(cmd, "", "", {}, GPTScript.serverURL)
93+
const r = new RunSubcommand(cmd, "", {}, GPTScript.serverURL)
9494
r.requestNoStream(null)
9595
return r.text()
9696
}
@@ -106,38 +106,29 @@ export class GPTScript {
106106
if (!this.ready) {
107107
this.ready = await this.testGPTScriptURL(20)
108108
}
109-
return (new Run("run", toolName, "", opts, GPTScript.serverURL)).nextChat(opts.input)
109+
return (new Run("run", toolName, opts, GPTScript.serverURL)).nextChat(opts.input)
110110
}
111111

112112
/**
113113
* Evaluates the given tool and returns a Run object.
114114
*
115-
* @param {ToolDef | ToolDef[] | string} tool - The tool to be evaluated. Can be a single ToolDef object, an array of ToolDef objects, or a string representing the tool contents.
115+
* @param {ToolDef | ToolDef[]} tool - The tool to be evaluated. Can be a single ToolDef object or an array of ToolDef objects.
116116
* @param {RunOpts} [opts={}] - Optional options for the evaluation.
117117
* @return {Run} The Run object representing the evaluation.
118118
*/
119-
async evaluate(tool: ToolDef | ToolDef[] | string, opts: RunOpts = {}): Promise<Run> {
119+
async evaluate(tool: ToolDef | ToolDef[], opts: RunOpts = {}): Promise<Run> {
120120
if (!this.ready) {
121121
this.ready = await this.testGPTScriptURL(20)
122122
}
123-
let toolString: string = ""
124123

125-
if (Array.isArray(tool)) {
126-
toolString = toolArrayToContents(tool)
127-
} else if (typeof tool === "string") {
128-
toolString = tool
129-
} else {
130-
toolString = toolDefToString(tool)
131-
}
132-
133-
return (new Run("evaluate", "", toolString, opts, GPTScript.serverURL)).nextChat(opts.input)
124+
return (new Run("evaluate", tool, opts, GPTScript.serverURL)).nextChat(opts.input)
134125
}
135126

136127
async parse(fileName: string): Promise<Block[]> {
137128
if (!this.ready) {
138129
this.ready = await this.testGPTScriptURL(20)
139130
}
140-
const r: Run = new RunSubcommand("parse", fileName, "", {}, GPTScript.serverURL)
131+
const r: Run = new RunSubcommand("parse", fileName, {}, GPTScript.serverURL)
141132
r.request({file: fileName})
142133
return parseBlocksFromNodes((await r.json()).nodes)
143134
}
@@ -146,7 +137,7 @@ export class GPTScript {
146137
if (!this.ready) {
147138
this.ready = await this.testGPTScriptURL(20)
148139
}
149-
const r: Run = new RunSubcommand("parse", "", toolContent, {}, GPTScript.serverURL)
140+
const r: Run = new RunSubcommand("parse", "", {}, GPTScript.serverURL)
150141
r.request({content: toolContent})
151142
return parseBlocksFromNodes((await r.json()).nodes)
152143
}
@@ -173,7 +164,7 @@ export class GPTScript {
173164
}
174165
}
175166

176-
const r: Run = new RunSubcommand("fmt", "", JSON.stringify({nodes: nodes}), {}, GPTScript.serverURL)
167+
const r: Run = new RunSubcommand("fmt", "", {}, GPTScript.serverURL)
177168
r.request({nodes: nodes})
178169
return r.text()
179170
}
@@ -223,8 +214,7 @@ export class GPTScript {
223214
export class Run {
224215
public readonly id: string
225216
public readonly opts: RunOpts
226-
public readonly filePath: string
227-
public readonly content: string
217+
public readonly tools?: ToolDef | ToolDef[] | string
228218
public state: RunState = RunState.Creating
229219
public calls: Record<string, CallFrame> = {}
230220
public err: string = ""
@@ -238,17 +228,15 @@ export class Run {
238228
private stderr?: string
239229
private callbacks: Record<string, ((f: Frame) => void)[]> = {}
240230
private chatState?: string
241-
private callIdsByParentIds: Record<string, string[]> = {}
242231
private parentCallId: string = ""
243232
private prg?: Program
244233
private respondingToolId?: string
245234

246-
constructor(subCommand: string, path: string, content: string, opts: RunOpts, gptscriptURL?: string) {
235+
constructor(subCommand: string, tools: ToolDef | ToolDef[] | string, opts: RunOpts, gptscriptURL?: string) {
247236
this.id = randomId("run-")
248237
this.requestPath = subCommand
249238
this.opts = opts
250-
this.filePath = path
251-
this.content = content
239+
this.tools = tools
252240

253241
this.gptscriptURL = gptscriptURL
254242
}
@@ -260,7 +248,7 @@ export class Run {
260248

261249
let run = this
262250
if (run.state !== RunState.Creating) {
263-
run = new (this.constructor as any)(this.requestPath, this.filePath, this.content, this.opts, this.gptscriptURL)
251+
run = new (this.constructor as any)(this.requestPath, this.tools, this.opts, this.gptscriptURL)
264252
}
265253

266254
if (this.chatState && this.state === RunState.Continue) {
@@ -269,10 +257,13 @@ export class Run {
269257
this.opts.chatState = this.chatState
270258
}
271259
run.opts.input = input
272-
if (run.content !== "") {
273-
run.request({content: this.content, ...this.opts})
260+
if (Array.isArray(this.tools)) {
261+
run.request({toolDefs: this.tools, ...this.opts})
262+
} else if (typeof this.tools === "string") {
263+
run.request({file: this.tools, ...this.opts})
274264
} else {
275-
run.request({file: this.filePath, ...this.opts})
265+
// In this last case, this.tools is a single ToolDef.
266+
run.request({toolDefs: [this.tools], ...this.opts})
276267
}
277268

278269
return run
@@ -463,8 +454,12 @@ export class Run {
463454
return this.chatState
464455
}
465456

466-
public firstCallId(): string {
467-
return this.parentCallId
457+
public parentCallFrame(): CallFrame | undefined {
458+
if (this.parentCallId) {
459+
return this.calls[this.parentCallId]
460+
}
461+
462+
return undefined
468463
}
469464

470465
public program(): Program | undefined {
@@ -551,8 +546,8 @@ export class Run {
551546
}
552547

553548
class RunSubcommand extends Run {
554-
constructor(subCommand: string, path: string, content: string, opts: RunOpts, gptscriptURL?: string) {
555-
super(subCommand, path, content, opts, gptscriptURL)
549+
constructor(subCommand: string, tool: ToolDef | ToolDef[] | string, opts: RunOpts, gptscriptURL?: string) {
550+
super(subCommand, tool, opts, gptscriptURL)
556551
}
557552

558553
processStdout(data: string | object): string {
@@ -608,16 +603,19 @@ export interface ToolDef {
608603
modelName: string
609604
modelProvider: boolean
610605
jsonResponse: boolean
611-
temperature: number
606+
temperature?: number
612607
cache?: boolean
613608
chat: boolean
614-
internalPrompt: boolean
609+
internalPrompt?: boolean
615610
arguments: ArgumentSchema
616611
tools: string[]
617612
globalTools: string[]
613+
globalModelName: string
618614
context: string[]
615+
exportContext: string[]
619616
export: string[]
620-
blocking: boolean
617+
agents: string[]
618+
credentials: string[]
621619
instructions: string
622620
}
623621

@@ -697,6 +695,7 @@ export interface Usage {
697695
export interface CallFrame {
698696
id: string
699697
tool?: Tool
698+
agentGroup?: ToolReference[]
700699
displayText?: string
701700
inputContext: InputContext[]
702701
toolCategory?: string
@@ -768,67 +767,6 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
768767
return blocks
769768
}
770769

771-
function toolArrayToContents(toolArray: ToolDef[]) {
772-
return toolArray.map(singleTool => {
773-
return toolDefToString(singleTool)
774-
}).join("\n---\n")
775-
}
776-
777-
function toolDefToString(tool: ToolDef) {
778-
let toolInfo: string[] = []
779-
if (tool.name) {
780-
toolInfo.push(`Name: ${tool.name}`)
781-
}
782-
if (tool.description) {
783-
toolInfo.push(`Description: ${tool.description}`)
784-
}
785-
if (tool.globalTools?.length) {
786-
toolInfo.push(`Global Tools: ${tool.globalTools.join(", ")}`)
787-
}
788-
if (tool.tools?.length > 0) {
789-
toolInfo.push(`Tools: ${tool.tools.join(", ")}`)
790-
}
791-
if (tool.context?.length > 0) {
792-
toolInfo.push(`Context: ${tool.context.join(", ")}`)
793-
}
794-
if (tool.export?.length > 0) {
795-
toolInfo.push(`Export: ${tool.export.join(", ")}`)
796-
}
797-
if (tool.maxTokens !== undefined) {
798-
toolInfo.push(`Max Tokens: ${tool.maxTokens}`)
799-
}
800-
if (tool.modelName) {
801-
toolInfo.push(`Model: ${tool.modelName}`)
802-
}
803-
if (tool.cache !== undefined && !tool.cache) {
804-
toolInfo.push("Cache: false")
805-
}
806-
if (tool.temperature !== undefined) {
807-
toolInfo.push(`Temperature: ${tool.temperature}`)
808-
}
809-
if (tool.jsonResponse) {
810-
toolInfo.push("JSON Response: true")
811-
}
812-
if (tool.arguments && tool.arguments.properties) {
813-
for (const [arg, desc] of Object.entries(tool.arguments.properties)) {
814-
toolInfo.push(`Args: ${arg}: ${desc.description}`)
815-
}
816-
}
817-
if (tool.internalPrompt) {
818-
toolInfo.push(`Internal Prompt: ${tool.internalPrompt}`)
819-
}
820-
if (tool.chat) {
821-
toolInfo.push("Chat: true")
822-
}
823-
824-
if (tool.instructions) {
825-
toolInfo.push("")
826-
toolInfo.push(tool.instructions)
827-
}
828-
829-
return toolInfo.join("\n")
830-
}
831-
832770
function randomId(prefix: string): string {
833771
return prefix + Math.random().toString(36).substring(2, 12)
834772
}

0 commit comments

Comments
 (0)
Please sign in to comment.