Skip to content

Commit 3bbcf31

Browse files
committed
changes from thedadams
Signed-off-by: Donnie Adams <[email protected]>
1 parent 42541fd commit 3bbcf31

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "v0.5.0",
44
"description": "Run gptscript in node.js",
55
"main": "dist/gptscript.js",
6+
"type": "module",
67
"repository": {
78
"type": "git",
89
"url": "git+https://github.com/gptscript-ai/node-gptscript.git"

src/gptscript.ts

+55-46
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Run {
4747
public readonly opts: RunOpts
4848
public state: RunState = RunState.Creating
4949
public calls: Call[] = []
50-
public err = ''
50+
public err = ""
5151
public readonly path: string
5252

5353
private promise?: Promise<string>
@@ -157,7 +157,7 @@ export class Run {
157157
this.err = "Run has been aborted"
158158
} else if (code !== 0) {
159159
this.state = RunState.Error
160-
this.err = this.stderr || ''
160+
this.err = this.stderr || ""
161161
} else {
162162
this.state = RunState.Finished
163163
}
@@ -226,14 +226,14 @@ export class Run {
226226

227227
res.on("error", (error: Error) => {
228228
this.state = RunState.Error
229-
this.err = error.message || ''
229+
this.err = error.message || ""
230230
reject(this.err)
231231
})
232232
})
233233

234234
this.req.on("error", (error: Error) => {
235235
this.state = RunState.Error
236-
this.err = error.message || ''
236+
this.err = error.message || ""
237237
reject(this.err)
238238
})
239239

@@ -287,7 +287,7 @@ export class Run {
287287
} else if (f.type === RunEventType.RunFinish) {
288288
if (f.err) {
289289
this.state = RunState.Error
290-
this.err = f.err || ''
290+
this.err = f.err || ""
291291
} else {
292292
this.state = RunState.Finished
293293
this.stdout = f.output || ""
@@ -343,12 +343,6 @@ export class Run {
343343
return ""
344344
}
345345

346-
private emit(event: RunEventType, data: any){
347-
for ( const cb of this.callbacks[event] || [] ){
348-
cb(data)
349-
}
350-
}
351-
352346
public on(event: RunEventType.RunStart, listener: (data: RunStartFrame) => void): this;
353347
public on(event: RunEventType.RunFinish, listener: (data: RunFinishFrame) => void): this;
354348
public on(event: RunEventType.CallStart, listener: (data: CallStartFrame) => void): this;
@@ -358,8 +352,8 @@ export class Run {
358352
public on(event: RunEventType.CallFinish, listener: (data: CallFinishFrame) => void): this;
359353
public on(event: RunEventType.Event, listener: (data: Frame) => void): this;
360354
public on(event: RunEventType, listener: (data: any) => void): this {
361-
if(!this.callbacks[event]) {
362-
this.callbacks[event] = [];
355+
if (!this.callbacks[event]) {
356+
this.callbacks[event] = []
363357
}
364358

365359
this.callbacks[event].push(listener)
@@ -398,6 +392,12 @@ export class Run {
398392

399393
throw new Error("Run not started")
400394
}
395+
396+
private emit(event: RunEventType, data: any) {
397+
for (const cb of this.callbacks[event] || []) {
398+
cb(data)
399+
}
400+
}
401401
}
402402

403403
export type Arguments = string | Record<string, string>
@@ -408,25 +408,6 @@ export interface ArgumentSchema {
408408
required?: string[]
409409
}
410410

411-
export type EnvVars = string[]
412-
413-
export interface Parameters {
414-
name: string
415-
description: string
416-
maxTokens: number
417-
modelName: string
418-
modelProvider: boolean
419-
jsonResponse: boolean
420-
temperature: number
421-
cache?: boolean
422-
internalPrompt: boolean
423-
arguments: ArgumentSchema
424-
tools: string[]
425-
globalTools: string[]
426-
export: string[]
427-
blocking: boolean
428-
}
429-
430411
export interface Program {
431412
name: string
432413
blocks: Block[]
@@ -446,7 +427,21 @@ export interface Repo {
446427
revision: string
447428
}
448429

449-
export interface ToolDef extends Parameters {
430+
export interface ToolDef {
431+
name: string
432+
description: string
433+
maxTokens: number
434+
modelName: string
435+
modelProvider: boolean
436+
jsonResponse: boolean
437+
temperature: number
438+
cache?: boolean
439+
internalPrompt: boolean
440+
arguments: ArgumentSchema
441+
tools: string[]
442+
globalTools: string[]
443+
export: string[]
444+
blocking: boolean
450445
instructions: string
451446
}
452447

@@ -493,12 +488,17 @@ export interface Call {
493488
showSystemMessages?: boolean
494489
}
495490

496-
export interface BaseFrame {
491+
interface BaseFrame {
497492
type: RunEventType
498493
time: string
499494
runID: string
500495
}
501496

497+
interface CallFrame extends BaseFrame {
498+
callContext: Call
499+
input: Arguments
500+
}
501+
502502
export interface RunStartFrame extends BaseFrame {
503503
type: RunEventType.RunStart
504504
program: Program
@@ -512,11 +512,6 @@ export interface RunFinishFrame extends BaseFrame {
512512
output?: string
513513
}
514514

515-
export interface CallFrame extends BaseFrame {
516-
callContext: Call
517-
input: Arguments
518-
}
519-
520515
export interface CallStartFrame extends CallFrame {
521516
type: RunEventType.CallStart
522517
content: string
@@ -649,7 +644,14 @@ function runBasicCommand(cmd: string, gptscriptURL?: string): Promise<string> {
649644
return r.text()
650645
}
651646

652-
export function run(toolName: string, opts: RunOpts): Run {
647+
/**
648+
* Runs a tool with the specified name and options.
649+
*
650+
* @param {string} toolName - The name of the tool to run. Can be a file path, URL, or GitHub URL.
651+
* @param {RunOpts} [opts={}] - The options for running the tool.
652+
* @return {Run} The Run object representing the running tool.
653+
*/
654+
export function run(toolName: string, opts: RunOpts = {}): Run {
653655
const r: Run = new Run(toolName, opts)
654656

655657
if (opts.gptscriptURL) {
@@ -661,7 +663,14 @@ export function run(toolName: string, opts: RunOpts): Run {
661663
return r
662664
}
663665

664-
export function evaluate(tool: ToolDef | ToolDef[] | string, opts: RunOpts): Run {
666+
/**
667+
* Evaluates the given tool and returns a Run object.
668+
*
669+
* @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.
670+
* @param {RunOpts} [opts={}] - Optional options for the evaluation.
671+
* @return {Run} The Run object representing the evaluation.
672+
*/
673+
export function evaluate(tool: ToolDef | ToolDef[] | string, opts: RunOpts = {}): Run {
665674
let toolString: string = ""
666675

667676
if (Array.isArray(tool)) {
@@ -695,12 +704,12 @@ export async function parse(fileName: string, gptscriptURL?: string): Promise<Bl
695704
return parseBlocksFromNodes((await r.json()).nodes)
696705
}
697706

698-
export async function parseTool(tool: string, gptscriptURL?: string): Promise<Block[]> {
707+
export async function parseTool(toolContent: string, gptscriptURL?: string): Promise<Block[]> {
699708
const r: Run = new Run("", {gptscriptURL: gptscriptURL})
700709
if (gptscriptURL) {
701-
r.request("parse", {input: tool})
710+
r.request("parse", {input: toolContent})
702711
} else {
703-
r.exec(getCmdPath(), ["parse", "-"], tool)
712+
r.exec(getCmdPath(), ["parse", "-"], toolContent)
704713
}
705714
return parseBlocksFromNodes((await r.json()).nodes)
706715
}
@@ -744,7 +753,7 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
744753
blocks.push({
745754
type: "tool",
746755
...node.toolNode.tool,
747-
})
756+
} as Tool)
748757
}
749758
if (node.textNode) {
750759
const format = node.textNode.text.substring(1, node.textNode.text.indexOf("\n")).trim() || "text"
@@ -753,7 +762,7 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
753762
type: "text",
754763
format: format,
755764
content: node.textNode.text.substring(node.textNode.text.indexOf("\n") + 1).trim(),
756-
})
765+
} as Text)
757766
}
758767
}
759768
return blocks

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
/* Language and Environment */
4-
"target": "ES2020",
4+
"target": "esnext",
55
/* Specify what module code is generated. */
66
"module": "esnext",
77
// Specify the root folder within your source files.

0 commit comments

Comments
 (0)