Skip to content

Commit b629bad

Browse files
committed
fix: better error reporting
This change also includes change to Text nodes when parsing files for parity with other SDKs. Signed-off-by: Donnie Adams <[email protected]>
1 parent 8111c2b commit b629bad

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

gptscript.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
4949
defer lock.Unlock()
5050
gptscriptCount++
5151

52-
disableServer := os.Getenv("GPT_SCRIPT_DISABLE_SERVER") == "true"
52+
disableServer := os.Getenv("GPTSCRIPT_DISABLE_SERVER") == "true"
5353

5454
if serverURL == "" && disableServer {
5555
serverURL = os.Getenv("GPTSCRIPT_URL")
@@ -165,6 +165,10 @@ func (g *gptscript) Parse(ctx context.Context, fileName string) ([]Node, error)
165165
return nil, err
166166
}
167167

168+
for _, node := range doc.Nodes {
169+
node.TextNode.process()
170+
}
171+
168172
return doc.Nodes, nil
169173
}
170174

@@ -180,11 +184,19 @@ func (g *gptscript) ParseTool(ctx context.Context, toolDef string) ([]Node, erro
180184
return nil, err
181185
}
182186

187+
for _, node := range doc.Nodes {
188+
node.TextNode.process()
189+
}
190+
183191
return doc.Nodes, nil
184192
}
185193

186194
// Fmt will format the given nodes into a string.
187195
func (g *gptscript) Fmt(ctx context.Context, nodes []Node) (string, error) {
196+
for _, node := range nodes {
197+
node.TextNode.combine()
198+
}
199+
188200
out, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
189201
if err != nil {
190202
return "", err

gptscript_test.go

+24-28
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestEvaluateWithContext(t *testing.T) {
151151
},
152152
}
153153

154-
run, err := g.Evaluate(context.Background(), Options{DisableCache: true, IncludeEvents: true}, tool)
154+
run, err := g.Evaluate(context.Background(), Options{}, tool)
155155
if err != nil {
156156
t.Errorf("Error executing tool: %v", err)
157157
}
@@ -214,7 +214,7 @@ func TestEvaluateWithToolList(t *testing.T) {
214214
Tools: []string{"sys.exec"},
215215
Description: "Echoes the input",
216216
Arguments: ObjectSchema("input", "The string input to echo"),
217-
Instructions: shebang + "\n echo ${input}",
217+
Instructions: shebang + "\necho ${input}",
218218
},
219219
}
220220

@@ -410,9 +410,12 @@ func TestParseToolWithTextNode(t *testing.T) {
410410
t.Fatalf("No text node found")
411411
}
412412

413-
if tools[1].TextNode.Text != "!markdown\nhello\n" {
413+
if tools[1].TextNode.Text != "hello\n" {
414414
t.Errorf("Unexpected text: %s", tools[1].TextNode.Text)
415415
}
416+
if tools[1].TextNode.Fmt != "markdown" {
417+
t.Errorf("Unexpected fmt: %s", tools[1].TextNode.Fmt)
418+
}
416419
}
417420

418421
func TestFmt(t *testing.T) {
@@ -484,7 +487,8 @@ func TestFmtWithTextNode(t *testing.T) {
484487
},
485488
{
486489
TextNode: &TextNode{
487-
Text: "!markdown\nWe now echo hello there\n",
490+
Fmt: "markdown",
491+
Text: "We now echo hello there\n",
488492
},
489493
},
490494
{
@@ -686,14 +690,12 @@ func TestToolWithGlobalTools(t *testing.T) {
686690

687691
func TestConfirm(t *testing.T) {
688692
var eventContent string
689-
tools := []ToolDef{
690-
{
691-
Instructions: "List the files in the current directory",
692-
Tools: []string{"sys.exec"},
693-
},
693+
tools := ToolDef{
694+
Instructions: "List the files in the current directory",
695+
Tools: []string{"sys.exec"},
694696
}
695697

696-
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true, Confirm: true}, tools...)
698+
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true, Confirm: true}, tools)
697699
if err != nil {
698700
t.Errorf("Error executing tool: %v", err)
699701
}
@@ -771,14 +773,12 @@ func TestConfirm(t *testing.T) {
771773

772774
func TestConfirmDeny(t *testing.T) {
773775
var eventContent string
774-
tools := []ToolDef{
775-
{
776-
Instructions: "List the files in the current directory",
777-
Tools: []string{"sys.exec"},
778-
},
776+
tools := ToolDef{
777+
Instructions: "List the files in the current directory",
778+
Tools: []string{"sys.exec"},
779779
}
780780

781-
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true, Confirm: true}, tools...)
781+
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true, Confirm: true}, tools)
782782
if err != nil {
783783
t.Errorf("Error executing tool: %v", err)
784784
}
@@ -843,14 +843,12 @@ func TestConfirmDeny(t *testing.T) {
843843

844844
func TestPrompt(t *testing.T) {
845845
var eventContent string
846-
tools := []ToolDef{
847-
{
848-
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
849-
Tools: []string{"sys.prompt"},
850-
},
846+
tools := ToolDef{
847+
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
848+
Tools: []string{"sys.prompt"},
851849
}
852850

853-
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true, Prompt: true}, tools...)
851+
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true, Prompt: true}, tools)
854852
if err != nil {
855853
t.Errorf("Error executing tool: %v", err)
856854
}
@@ -926,14 +924,12 @@ func TestPrompt(t *testing.T) {
926924
}
927925

928926
func TestPromptWithoutPromptAllowed(t *testing.T) {
929-
tools := []ToolDef{
930-
{
931-
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
932-
Tools: []string{"sys.prompt"},
933-
},
927+
tools := ToolDef{
928+
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
929+
Tools: []string{"sys.prompt"},
934930
}
935931

936-
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true}, tools...)
932+
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true}, tools)
937933
if err != nil {
938934
t.Errorf("Error executing tool: %v", err)
939935
}

run.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (r *Run) Text() (string, error) {
4343
r.lock.Lock()
4444
defer r.lock.Unlock()
4545
if r.err != nil {
46-
return "", r.err
46+
return "", fmt.Errorf("run encounterd an error: %w with error output: %s", r.err, r.errput)
4747
}
4848

4949
return r.output, nil
@@ -237,11 +237,11 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
237237

238238
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
239239
r.state = Error
240-
r.err = fmt.Errorf("unexpected response status: %s", resp.Status)
241-
return r.err
240+
r.err = fmt.Errorf("run encountered an error")
241+
} else {
242+
r.state = Running
242243
}
243244

244-
r.state = Running
245245
r.events = make(chan Frame, 100)
246246
r.lock.Lock()
247247
go func() {

tool.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gptscript
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/getkin/kin-openapi/openapi3"
58
)
69

@@ -56,11 +59,26 @@ type Node struct {
5659
}
5760

5861
type TextNode struct {
62+
Fmt string `json:"fmt,omitempty"`
5963
Text string `json:"text,omitempty"`
6064
}
6165

66+
func (n *TextNode) combine() {
67+
if n != nil && n.Fmt != "" {
68+
n.Text = fmt.Sprintf("!%s\n%s", n.Fmt, n.Text)
69+
n.Fmt = ""
70+
}
71+
}
72+
73+
func (n *TextNode) process() {
74+
if n != nil && strings.HasPrefix(n.Text, "!") {
75+
n.Fmt, n.Text, _ = strings.Cut(strings.TrimPrefix(n.Text, "!"), "\n")
76+
}
77+
}
78+
6279
type ToolNode struct {
63-
Tool Tool `json:"tool,omitempty"`
80+
Fmt string `json:"fmt,omitempty"`
81+
Tool Tool `json:"tool,omitempty"`
6482
}
6583

6684
type Tool struct {

0 commit comments

Comments
 (0)