Skip to content

Commit f82253a

Browse files
Change output to be log based
1 parent ce79ae0 commit f82253a

File tree

10 files changed

+345
-402
lines changed

10 files changed

+345
-402
lines changed

go.mod

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/acorn-io/cmd v0.0.0-20240203032901-e9e631185ddb
77
github.com/adrg/xdg v0.4.0
88
github.com/hexops/autogold/v2 v2.1.0
9-
github.com/pterm/pterm v0.12.76
109
github.com/sashabaranov/go-openai v1.18.3
1110
github.com/sirupsen/logrus v1.9.3
1211
github.com/spf13/cobra v1.8.0
@@ -23,33 +22,23 @@ require (
2322
)
2423

2524
require (
26-
atomicgo.dev/cursor v0.2.0 // indirect
27-
atomicgo.dev/keyboard v0.2.9 // indirect
28-
atomicgo.dev/schedule v0.1.0 // indirect
2925
github.com/bombsimon/logrusr/v4 v4.0.0 // indirect
30-
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
3126
github.com/davecgh/go-spew v1.1.1 // indirect
3227
github.com/fatih/color v1.15.0 // indirect
3328
github.com/go-logr/logr v1.4.1 // indirect
3429
github.com/google/go-cmp v0.6.0 // indirect
3530
github.com/google/go-containerregistry v0.16.1 // indirect
36-
github.com/gookit/color v1.5.4 // indirect
3731
github.com/hexops/gotextdiff v1.0.3 // indirect
3832
github.com/hexops/valast v1.4.3 // indirect
3933
github.com/inconshreveable/mousetrap v1.1.0 // indirect
40-
github.com/lithammer/fuzzysearch v1.1.8 // indirect
4134
github.com/mattn/go-colorable v0.1.13 // indirect
4235
github.com/mattn/go-isatty v0.0.17 // indirect
43-
github.com/mattn/go-runewidth v0.0.15 // indirect
4436
github.com/nightlyone/lockfile v1.0.0 // indirect
4537
github.com/pmezard/go-difflib v1.0.0 // indirect
46-
github.com/rivo/uniseg v0.4.4 // indirect
4738
github.com/rogpeppe/go-internal v1.11.0 // indirect
4839
github.com/samber/lo v1.38.1 // indirect
4940
github.com/samber/slog-logrus v1.0.0 // indirect
50-
github.com/sergi/go-diff v1.3.1 // indirect
5141
github.com/spf13/pflag v1.0.5 // indirect
52-
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
5342
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
5443
golang.org/x/mod v0.14.0 // indirect
5544
golang.org/x/sys v0.16.0 // indirect

go.sum

Lines changed: 0 additions & 74 deletions
Large diffs are not rendered by default.

pkg/cli/gptscript.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type GPTScript struct {
2828
runner.Options
2929
DisplayOptions
3030
Debug bool `usage:"Enable debug logging"`
31+
Quiet bool `usage:"No output logging" short:"q"`
3132
Output string `usage:"Save output to a file" short:"o"`
3233
Input string `usage:"Read input from a file (\"-\" for stdin)" short:"f"`
3334
SubTool string `usage:"Use tool of this name, not the first tool in file"`
@@ -65,6 +66,11 @@ func (r *GPTScript) listModels(ctx context.Context) error {
6566
func (r *GPTScript) Pre(cmd *cobra.Command, args []string) error {
6667
if r.Debug {
6768
mvl.SetDebug()
69+
} else {
70+
mvl.SetSimpleFormat()
71+
}
72+
if r.Quiet {
73+
mvl.SetError()
6874
}
6975
return nil
7076
}
@@ -128,6 +134,13 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
128134
return err
129135
}
130136
} else {
137+
if !r.Quiet {
138+
if toolInput != "" {
139+
_, _ = fmt.Fprintln(os.Stderr, "\nINPUT:\n")
140+
_, _ = fmt.Fprintln(os.Stderr, toolInput)
141+
}
142+
_, _ = fmt.Fprintln(os.Stderr, "\nOUTPUT:\n")
143+
}
131144
fmt.Print(s)
132145
if !strings.HasSuffix(s, "\n") {
133146
fmt.Println()

pkg/engine/engine.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"os"
99
"os/exec"
10-
"sort"
1110
"strings"
1211
"sync"
1312
"sync/atomic"
@@ -77,6 +76,13 @@ type Context struct {
7776
Tool types.Tool
7877
}
7978

79+
func (c *Context) ParentID() string {
80+
if c.Parent == nil {
81+
return ""
82+
}
83+
return c.Parent.ID
84+
}
85+
8086
func (c *Context) UnmarshalJSON(data []byte) error {
8187
panic("this data struct is circular by design and can not be read from json")
8288
}
@@ -327,12 +333,10 @@ func (e *Engine) Continue(ctx context.Context, state *State, results ...CallResu
327333
}
328334

329335
var (
330-
added bool
331-
pendingToolCalls []types.CompletionToolCall
336+
added bool
332337
)
333338

334339
for id, pending := range state.Pending {
335-
pendingToolCalls = append(pendingToolCalls, pending)
336340
if _, ok := state.Results[id]; !ok {
337341
ret.Calls[id] = Call{
338342
ToolName: pending.Function.Name,
@@ -345,25 +349,28 @@ func (e *Engine) Continue(ctx context.Context, state *State, results ...CallResu
345349
return &ret, nil
346350
}
347351

348-
sort.Slice(pendingToolCalls, func(i, j int) bool {
349-
left := pendingToolCalls[i].Function.Name + pendingToolCalls[i].Function.Arguments
350-
right := pendingToolCalls[j].Function.Name + pendingToolCalls[j].Function.Arguments
351-
if left == right {
352-
return pendingToolCalls[i].ID < pendingToolCalls[j].ID
352+
for _, content := range state.Completion.Messages[len(state.Completion.Messages)-1].Content {
353+
if content.ToolCall == nil {
354+
continue
355+
}
356+
result, ok := state.Results[content.ToolCall.ID]
357+
if !ok {
358+
return nil, fmt.Errorf("missing tool call result for id %s, most likely a %s BUG",
359+
content.ToolCall.ID, version.ProgramName)
353360
}
354-
return left < right
355-
})
356361

357-
for _, pending := range pendingToolCalls {
358-
pending := pending
359-
if result, ok := state.Results[pending.ID]; ok {
360-
added = true
361-
state.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
362-
Role: types.CompletionMessageRoleTypeTool,
363-
Content: types.Text(result.Result),
364-
ToolCall: &pending,
365-
})
362+
pending, ok := state.Pending[content.ToolCall.ID]
363+
if !ok {
364+
return nil, fmt.Errorf("missing tool call pennding for id %s, most likely a %s BUG",
365+
content.ToolCall.ID, version.ProgramName)
366366
}
367+
368+
added = true
369+
state.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
370+
Role: types.CompletionMessageRoleTypeTool,
371+
Content: types.Text(result.Result),
372+
ToolCall: &pending,
373+
})
367374
}
368375

369376
if !added {

0 commit comments

Comments
 (0)