Skip to content

Commit

Permalink
feat: Implement no-cache option and refactor display logic
Browse files Browse the repository at this point in the history
Introducing the ability to disable caching and overhauled the display logic for better status tracking during tool execution. Added a display module to better manage state and output during runs, and implemented a no-cache option to allow runs without utilizing the cache layer. Additionally, refactored context management within the execution engine to handle unique execution IDs. The go.mod changes include updating dependencies and adding the `golang.org/x/term` package for better terminal interaction.
  • Loading branch information
ibuildthecloud committed Jan 31, 2024
1 parent 2b2c3a7 commit 6887aeb
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 113 deletions.
17 changes: 17 additions & 0 deletions examples/git-commit.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tools: gitstatus

Create well formed git commit message based of off the currently stated file
contents. The message should convey why something was changed and not what
changed. Use the well known format that has the prefix chore, fix, etc.

Only include changed to *.go files and any change to the go.mod file. Exclude
the go.sum file.

Do not use markdown format for the output.

---
name: gitstatus

#!/bin/sh

git diff --staged
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ require (
github.com/spf13/cobra v1.8.0
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
golang.org/x/sync v0.5.0
golang.org/x/term v0.16.0
)

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.3 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
Expand All @@ -37,7 +38,6 @@ require (
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/bombsimon/logrusr/v4 v4.0.0 h1:Pm0InGphX0wMhPqC02t31onlq9OVyJ98eP/Vh63t1Oo=
github.com/bombsimon/logrusr/v4 v4.0.0/go.mod h1:pjfHC5e59CvjTBIU3V3sGhFWFAnsnhOR03TRc6im0l8=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -159,6 +160,7 @@ golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
13 changes: 10 additions & 3 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ import (
)

type Client struct {
dir string
dir string
noop bool
}

func NoCache() *Client {
return &Client{
noop: true,
}

}
func New() (*Client, error) {
dir := filepath.Join(xdg.CacheHome, version.ProgramName)
if err := os.MkdirAll(dir, 0755); err != nil {
Expand All @@ -26,14 +33,14 @@ func New() (*Client, error) {
}

func (c *Client) Store(ctx context.Context, key string, content []byte) error {
if c == nil {
if c == nil || c.noop {
return nil
}
return os.WriteFile(filepath.Join(c.dir, key), content, 0644)
}

func (c *Client) Get(ctx context.Context, key string) ([]byte, bool, error) {
if c == nil {
if c == nil || c.noop {
return nil, false, nil
}
data, err := os.ReadFile(filepath.Join(c.dir, key))
Expand Down
10 changes: 9 additions & 1 deletion pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"github.com/acorn-io/gptscript/pkg/runner"
"github.com/acorn-io/gptscript/pkg/version"
"github.com/spf13/cobra"
"golang.org/x/term"
)

type Root struct {
runner.Options
}

func New() *cobra.Command {
Expand All @@ -37,7 +39,13 @@ func (r *Root) Run(cmd *cobra.Command, args []string) error {
return err
}

runner, err := runner.New()
if !r.Quiet {
if !term.IsTerminal(int(os.Stdout.Fd())) {
r.Quiet = true
}
}

runner, err := runner.New(r.Options)
if err != nil {
return err
}
Expand Down
23 changes: 19 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"

"github.com/acorn-io/gptscript/pkg/openai"
"github.com/acorn-io/gptscript/pkg/types"
Expand Down Expand Up @@ -54,10 +55,24 @@ type CallResult struct {
}

type Context struct {
Ctx context.Context
Parent *Context
Tool types.Tool
Tools []types.Tool
ID string `json:"id,omitempty"`
Ctx context.Context `json:"-"`
Parent *Context `json:"parent,omitempty"`
Tool types.Tool `json:"tool,omitempty"`
Tools map[string]types.Tool `json:"tools,omitempty"`
}

var execID int32

func NewContext(ctx context.Context, parent *Context, tool types.Tool, tools map[string]types.Tool) Context {
callCtx := Context{
ID: fmt.Sprint(atomic.AddInt32(&execID, 1)),
Ctx: ctx,
Parent: parent,
Tool: tool,
Tools: tools,
}
return callCtx
}

func (c *Context) getTool(name string) (types.Tool, error) {
Expand Down
25 changes: 17 additions & 8 deletions pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,28 +311,37 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
cacheKey := c.cacheKey(request)
request.Stream = true

msg := ""
if len(request.Messages) > 0 {
msg = request.Messages[len(request.Messages)-1].Content
}

partial <- types.CompletionMessage{
Role: types.CompletionMessageRoleTypeAssistant,
Content: types.Text("Waiting for model response...\n" + msg),
}

slog.Debug("calling openai", "message", request.Messages)
stream, err := c.c.CreateChatCompletionStream(ctx, request)
if err != nil {
return nil, err
}
defer stream.Close()

var partialMessage types.CompletionMessage
for {
response, err := stream.Recv()
if err == io.EOF {
return responses, c.store(ctx, cacheKey, responses)
} else if err != nil {
return nil, err
}
if len(response.Choices) > 0 {
slog.Debug("stream", "content", response.Choices[0].Delta.Content)
if partial != nil {
partial <- types.CompletionMessage{
Role: types.CompletionMessageRoleType(response.Choices[0].Delta.Role),
Content: types.Text(response.Choices[0].Delta.Content),
}
}
slog.Debug("stream", "content", response.Choices[0].Delta.Content)
if partial != nil {
partialMessage = appendMessage(partialMessage, response)
//d, _ := json.MarshalIndent(partialMessage, "", " ")
//fmt.Println(string(d))
partial <- partialMessage
}
responses = append(responses, response)
}
Expand Down
Loading

0 comments on commit 6887aeb

Please sign in to comment.