@@ -43,8 +43,10 @@ export class GPTScript {
4343 constructor ( ) {
4444 this . ready = false
4545 GPTScript . instanceCount ++
46+ if ( ! GPTScript . serverURL ) {
47+ GPTScript . serverURL = "http://" + ( process . env . GPTSCRIPT_URL || "127.0.0.1:0" )
48+ }
4649 if ( GPTScript . instanceCount === 1 && process . env . GPTSCRIPT_DISABLE_SERVER !== "true" ) {
47- GPTScript . serverURL = process . env . GPTSCRIPT_URL || "http://127.0.0.1:0"
4850 const u = new URL ( GPTScript . serverURL )
4951 if ( u . port === "0" ) {
5052 const srv = net . createServer ( )
@@ -90,7 +92,7 @@ export class GPTScript {
9092 if ( ! this . ready ) {
9193 this . ready = await this . testGPTScriptURL ( 20 )
9294 }
93- const r = new RunSubcommand ( cmd , "" , "" , { } , GPTScript . serverURL )
95+ const r = new RunSubcommand ( cmd , "" , { } , GPTScript . serverURL )
9496 r . requestNoStream ( null )
9597 return r . text ( )
9698 }
@@ -106,38 +108,29 @@ export class GPTScript {
106108 if ( ! this . ready ) {
107109 this . ready = await this . testGPTScriptURL ( 20 )
108110 }
109- return ( new Run ( "run" , toolName , "" , opts , GPTScript . serverURL ) ) . nextChat ( opts . input )
111+ return ( new Run ( "run" , toolName , opts , GPTScript . serverURL ) ) . nextChat ( opts . input )
110112 }
111113
112114 /**
113115 * Evaluates the given tool and returns a Run object.
114116 *
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 .
117+ * @param {ToolDef | ToolDef[] } tool - The tool to be evaluated. Can be a single ToolDef object or an array of ToolDef objects.
116118 * @param {RunOpts } [opts={}] - Optional options for the evaluation.
117119 * @return {Run } The Run object representing the evaluation.
118120 */
119- async evaluate ( tool : ToolDef | ToolDef [ ] | string , opts : RunOpts = { } ) : Promise < Run > {
121+ async evaluate ( tool : ToolDef | ToolDef [ ] , opts : RunOpts = { } ) : Promise < Run > {
120122 if ( ! this . ready ) {
121123 this . ready = await this . testGPTScriptURL ( 20 )
122124 }
123- let toolString : string = ""
124-
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- }
132125
133- return ( new Run ( "evaluate" , "" , toolString , opts , GPTScript . serverURL ) ) . nextChat ( opts . input )
126+ return ( new Run ( "evaluate" , tool , opts , GPTScript . serverURL ) ) . nextChat ( opts . input )
134127 }
135128
136129 async parse ( fileName : string ) : Promise < Block [ ] > {
137130 if ( ! this . ready ) {
138131 this . ready = await this . testGPTScriptURL ( 20 )
139132 }
140- const r : Run = new RunSubcommand ( "parse" , fileName , "" , { } , GPTScript . serverURL )
133+ const r : Run = new RunSubcommand ( "parse" , fileName , { } , GPTScript . serverURL )
141134 r . request ( { file : fileName } )
142135 return parseBlocksFromNodes ( ( await r . json ( ) ) . nodes )
143136 }
@@ -146,7 +139,7 @@ export class GPTScript {
146139 if ( ! this . ready ) {
147140 this . ready = await this . testGPTScriptURL ( 20 )
148141 }
149- const r : Run = new RunSubcommand ( "parse" , "" , toolContent , { } , GPTScript . serverURL )
142+ const r : Run = new RunSubcommand ( "parse" , "" , { } , GPTScript . serverURL )
150143 r . request ( { content : toolContent } )
151144 return parseBlocksFromNodes ( ( await r . json ( ) ) . nodes )
152145 }
@@ -173,7 +166,7 @@ export class GPTScript {
173166 }
174167 }
175168
176- const r : Run = new RunSubcommand ( "fmt" , "" , JSON . stringify ( { nodes : nodes } ) , { } , GPTScript . serverURL )
169+ const r : Run = new RunSubcommand ( "fmt" , "" , { } , GPTScript . serverURL )
177170 r . request ( { nodes : nodes } )
178171 return r . text ( )
179172 }
@@ -223,8 +216,7 @@ export class GPTScript {
223216export class Run {
224217 public readonly id : string
225218 public readonly opts : RunOpts
226- public readonly filePath : string
227- public readonly content : string
219+ public readonly tools ?: ToolDef | ToolDef [ ] | string
228220 public state : RunState = RunState . Creating
229221 public calls : Record < string , CallFrame > = { }
230222 public err : string = ""
@@ -238,17 +230,15 @@ export class Run {
238230 private stderr ?: string
239231 private callbacks : Record < string , ( ( f : Frame ) => void ) [ ] > = { }
240232 private chatState ?: string
241- private callIdsByParentIds : Record < string , string [ ] > = { }
242233 private parentCallId : string = ""
243234 private prg ?: Program
244235 private respondingToolId ?: string
245236
246- constructor ( subCommand : string , path : string , content : string , opts : RunOpts , gptscriptURL ?: string ) {
237+ constructor ( subCommand : string , tools : ToolDef | ToolDef [ ] | string , opts : RunOpts , gptscriptURL ?: string ) {
247238 this . id = randomId ( "run-" )
248239 this . requestPath = subCommand
249240 this . opts = opts
250- this . filePath = path
251- this . content = content
241+ this . tools = tools
252242
253243 this . gptscriptURL = gptscriptURL
254244 }
@@ -260,7 +250,7 @@ export class Run {
260250
261251 let run = this
262252 if ( run . state !== RunState . Creating ) {
263- run = new ( this . constructor as any ) ( this . requestPath , this . filePath , this . content , this . opts , this . gptscriptURL )
253+ run = new ( this . constructor as any ) ( this . requestPath , this . tools , this . opts , this . gptscriptURL )
264254 }
265255
266256 if ( this . chatState && this . state === RunState . Continue ) {
@@ -269,10 +259,13 @@ export class Run {
269259 this . opts . chatState = this . chatState
270260 }
271261 run . opts . input = input
272- if ( run . content !== "" ) {
273- run . request ( { content : this . content , ...this . opts } )
262+ if ( Array . isArray ( this . tools ) ) {
263+ run . request ( { toolDefs : this . tools , ...this . opts } )
264+ } else if ( typeof this . tools === "string" ) {
265+ run . request ( { file : this . tools , ...this . opts } )
274266 } else {
275- run . request ( { file : this . filePath , ...this . opts } )
267+ // In this last case, this.tools is a single ToolDef.
268+ run . request ( { toolDefs : [ this . tools ] , ...this . opts } )
276269 }
277270
278271 return run
@@ -463,8 +456,12 @@ export class Run {
463456 return this . chatState
464457 }
465458
466- public firstCallId ( ) : string {
467- return this . parentCallId
459+ public parentCallFrame ( ) : CallFrame | undefined {
460+ if ( this . parentCallId ) {
461+ return this . calls [ this . parentCallId ]
462+ }
463+
464+ return undefined
468465 }
469466
470467 public program ( ) : Program | undefined {
@@ -551,8 +548,8 @@ export class Run {
551548}
552549
553550class RunSubcommand extends Run {
554- constructor ( subCommand : string , path : string , content : string , opts : RunOpts , gptscriptURL ?: string ) {
555- super ( subCommand , path , content , opts , gptscriptURL )
551+ constructor ( subCommand : string , tool : ToolDef | ToolDef [ ] | string , opts : RunOpts , gptscriptURL ?: string ) {
552+ super ( subCommand , tool , opts , gptscriptURL )
556553 }
557554
558555 processStdout ( data : string | object ) : string {
@@ -608,16 +605,19 @@ export interface ToolDef {
608605 modelName : string
609606 modelProvider : boolean
610607 jsonResponse : boolean
611- temperature : number
608+ temperature ? : number
612609 cache ?: boolean
613610 chat : boolean
614- internalPrompt : boolean
611+ internalPrompt ? : boolean
615612 arguments : ArgumentSchema
616613 tools : string [ ]
617614 globalTools : string [ ]
615+ globalModelName : string
618616 context : string [ ]
617+ exportContext : string [ ]
619618 export : string [ ]
620- blocking : boolean
619+ agents : string [ ]
620+ credentials : string [ ]
621621 instructions : string
622622}
623623
@@ -697,6 +697,7 @@ export interface Usage {
697697export interface CallFrame {
698698 id : string
699699 tool ?: Tool
700+ agentGroup ?: ToolReference [ ]
700701 displayText ?: string
701702 inputContext : InputContext [ ]
702703 toolCategory ?: string
@@ -768,67 +769,6 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
768769 return blocks
769770}
770771
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-
832772function randomId ( prefix : string ) : string {
833773 return prefix + Math . random ( ) . toString ( 36 ) . substring ( 2 , 12 )
834774}
0 commit comments