Skip to content

Commit

Permalink
Change output to be log based
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 4, 2024
1 parent ce79ae0 commit f82253a
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 402 deletions.
11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/acorn-io/cmd v0.0.0-20240203032901-e9e631185ddb
github.com/adrg/xdg v0.4.0
github.com/hexops/autogold/v2 v2.1.0
github.com/pterm/pterm v0.12.76
github.com/sashabaranov/go-openai v1.18.3
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
Expand All @@ -23,33 +22,23 @@ require (
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/bombsimon/logrusr/v4 v4.0.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/hexops/valast v1.4.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/nightlyone/lockfile v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/samber/slog-logrus v1.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.16.0 // indirect
Expand Down
74 changes: 0 additions & 74 deletions go.sum

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type GPTScript struct {
runner.Options
DisplayOptions
Debug bool `usage:"Enable debug logging"`
Quiet bool `usage:"No output logging" short:"q"`
Output string `usage:"Save output to a file" short:"o"`
Input string `usage:"Read input from a file (\"-\" for stdin)" short:"f"`
SubTool string `usage:"Use tool of this name, not the first tool in file"`
Expand Down Expand Up @@ -65,6 +66,11 @@ func (r *GPTScript) listModels(ctx context.Context) error {
func (r *GPTScript) Pre(cmd *cobra.Command, args []string) error {
if r.Debug {
mvl.SetDebug()
} else {
mvl.SetSimpleFormat()
}
if r.Quiet {
mvl.SetError()
}
return nil
}
Expand Down Expand Up @@ -128,6 +134,13 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
return err
}
} else {
if !r.Quiet {
if toolInput != "" {
_, _ = fmt.Fprintln(os.Stderr, "\nINPUT:\n")
_, _ = fmt.Fprintln(os.Stderr, toolInput)
}
_, _ = fmt.Fprintln(os.Stderr, "\nOUTPUT:\n")
}
fmt.Print(s)
if !strings.HasSuffix(s, "\n") {
fmt.Println()
Expand Down
47 changes: 27 additions & 20 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"
"os/exec"
"sort"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -77,6 +76,13 @@ type Context struct {
Tool types.Tool
}

func (c *Context) ParentID() string {
if c.Parent == nil {
return ""
}
return c.Parent.ID
}

func (c *Context) UnmarshalJSON(data []byte) error {
panic("this data struct is circular by design and can not be read from json")
}
Expand Down Expand Up @@ -327,12 +333,10 @@ func (e *Engine) Continue(ctx context.Context, state *State, results ...CallResu
}

var (
added bool
pendingToolCalls []types.CompletionToolCall
added bool
)

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

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

for _, pending := range pendingToolCalls {
pending := pending
if result, ok := state.Results[pending.ID]; ok {
added = true
state.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeTool,
Content: types.Text(result.Result),
ToolCall: &pending,
})
pending, ok := state.Pending[content.ToolCall.ID]
if !ok {
return nil, fmt.Errorf("missing tool call pennding for id %s, most likely a %s BUG",
content.ToolCall.ID, version.ProgramName)
}

added = true
state.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeTool,
Content: types.Text(result.Result),
ToolCall: &pending,
})
}

if !added {
Expand Down
Loading

0 comments on commit f82253a

Please sign in to comment.