Skip to content

Commit 6887aeb

Browse files
feat: Implement no-cache option and refactor display logic
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.
1 parent 2b2c3a7 commit 6887aeb

File tree

10 files changed

+382
-113
lines changed

10 files changed

+382
-113
lines changed

examples/git-commit.gpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tools: gitstatus
2+
3+
Create well formed git commit message based of off the currently stated file
4+
contents. The message should convey why something was changed and not what
5+
changed. Use the well known format that has the prefix chore, fix, etc.
6+
7+
Only include changed to *.go files and any change to the go.mod file. Exclude
8+
the go.sum file.
9+
10+
Do not use markdown format for the output.
11+
12+
---
13+
name: gitstatus
14+
15+
#!/bin/sh
16+
17+
git diff --staged

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ require (
1111
github.com/spf13/cobra v1.8.0
1212
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
1313
golang.org/x/sync v0.5.0
14+
golang.org/x/term v0.16.0
1415
)
1516

1617
require (
1718
atomicgo.dev/cursor v0.2.0 // indirect
1819
atomicgo.dev/keyboard v0.2.9 // indirect
1920
atomicgo.dev/schedule v0.1.0 // indirect
2021
github.com/bombsimon/logrusr/v4 v4.0.0 // indirect
21-
github.com/containerd/console v1.0.3 // indirect
22+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
2223
github.com/fsnotify/fsnotify v1.7.0 // indirect
2324
github.com/go-logr/logr v1.4.1 // indirect
2425
github.com/google/go-containerregistry v0.16.1 // indirect
@@ -37,7 +38,6 @@ require (
3738
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
3839
golang.org/x/mod v0.14.0 // indirect
3940
golang.org/x/sys v0.16.0 // indirect
40-
golang.org/x/term v0.16.0 // indirect
4141
golang.org/x/text v0.14.0 // indirect
4242
golang.org/x/tools v0.16.0 // indirect
4343
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E
2424
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
2525
github.com/bombsimon/logrusr/v4 v4.0.0 h1:Pm0InGphX0wMhPqC02t31onlq9OVyJ98eP/Vh63t1Oo=
2626
github.com/bombsimon/logrusr/v4 v4.0.0/go.mod h1:pjfHC5e59CvjTBIU3V3sGhFWFAnsnhOR03TRc6im0l8=
27-
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
2827
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
28+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
29+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
2930
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3031
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3132
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -159,6 +160,7 @@ golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBc
159160
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
160161
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
161162
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
163+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
162164
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
163165
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
164166
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

pkg/cache/cache.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ import (
1212
)
1313

1414
type Client struct {
15-
dir string
15+
dir string
16+
noop bool
1617
}
1718

19+
func NoCache() *Client {
20+
return &Client{
21+
noop: true,
22+
}
23+
24+
}
1825
func New() (*Client, error) {
1926
dir := filepath.Join(xdg.CacheHome, version.ProgramName)
2027
if err := os.MkdirAll(dir, 0755); err != nil {
@@ -26,14 +33,14 @@ func New() (*Client, error) {
2633
}
2734

2835
func (c *Client) Store(ctx context.Context, key string, content []byte) error {
29-
if c == nil {
36+
if c == nil || c.noop {
3037
return nil
3138
}
3239
return os.WriteFile(filepath.Join(c.dir, key), content, 0644)
3340
}
3441

3542
func (c *Client) Get(ctx context.Context, key string) ([]byte, bool, error) {
36-
if c == nil {
43+
if c == nil || c.noop {
3744
return nil, false, nil
3845
}
3946
data, err := os.ReadFile(filepath.Join(c.dir, key))

pkg/cli/root.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"github.com/acorn-io/gptscript/pkg/runner"
1111
"github.com/acorn-io/gptscript/pkg/version"
1212
"github.com/spf13/cobra"
13+
"golang.org/x/term"
1314
)
1415

1516
type Root struct {
17+
runner.Options
1618
}
1719

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

40-
runner, err := runner.New()
42+
if !r.Quiet {
43+
if !term.IsTerminal(int(os.Stdout.Fd())) {
44+
r.Quiet = true
45+
}
46+
}
47+
48+
runner, err := runner.New(r.Options)
4149
if err != nil {
4250
return err
4351
}

pkg/engine/engine.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sort"
1111
"strings"
1212
"sync"
13+
"sync/atomic"
1314

1415
"github.com/acorn-io/gptscript/pkg/openai"
1516
"github.com/acorn-io/gptscript/pkg/types"
@@ -54,10 +55,24 @@ type CallResult struct {
5455
}
5556

5657
type Context struct {
57-
Ctx context.Context
58-
Parent *Context
59-
Tool types.Tool
60-
Tools []types.Tool
58+
ID string `json:"id,omitempty"`
59+
Ctx context.Context `json:"-"`
60+
Parent *Context `json:"parent,omitempty"`
61+
Tool types.Tool `json:"tool,omitempty"`
62+
Tools map[string]types.Tool `json:"tools,omitempty"`
63+
}
64+
65+
var execID int32
66+
67+
func NewContext(ctx context.Context, parent *Context, tool types.Tool, tools map[string]types.Tool) Context {
68+
callCtx := Context{
69+
ID: fmt.Sprint(atomic.AddInt32(&execID, 1)),
70+
Ctx: ctx,
71+
Parent: parent,
72+
Tool: tool,
73+
Tools: tools,
74+
}
75+
return callCtx
6176
}
6277

6378
func (c *Context) getTool(name string) (types.Tool, error) {

pkg/openai/client.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,28 +311,37 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
311311
cacheKey := c.cacheKey(request)
312312
request.Stream = true
313313

314+
msg := ""
315+
if len(request.Messages) > 0 {
316+
msg = request.Messages[len(request.Messages)-1].Content
317+
}
318+
319+
partial <- types.CompletionMessage{
320+
Role: types.CompletionMessageRoleTypeAssistant,
321+
Content: types.Text("Waiting for model response...\n" + msg),
322+
}
323+
314324
slog.Debug("calling openai", "message", request.Messages)
315325
stream, err := c.c.CreateChatCompletionStream(ctx, request)
316326
if err != nil {
317327
return nil, err
318328
}
319329
defer stream.Close()
320330

331+
var partialMessage types.CompletionMessage
321332
for {
322333
response, err := stream.Recv()
323334
if err == io.EOF {
324335
return responses, c.store(ctx, cacheKey, responses)
325336
} else if err != nil {
326337
return nil, err
327338
}
328-
if len(response.Choices) > 0 {
329-
slog.Debug("stream", "content", response.Choices[0].Delta.Content)
330-
if partial != nil {
331-
partial <- types.CompletionMessage{
332-
Role: types.CompletionMessageRoleType(response.Choices[0].Delta.Role),
333-
Content: types.Text(response.Choices[0].Delta.Content),
334-
}
335-
}
339+
slog.Debug("stream", "content", response.Choices[0].Delta.Content)
340+
if partial != nil {
341+
partialMessage = appendMessage(partialMessage, response)
342+
//d, _ := json.MarshalIndent(partialMessage, "", " ")
343+
//fmt.Println(string(d))
344+
partial <- partialMessage
336345
}
337346
responses = append(responses, response)
338347
}

0 commit comments

Comments
 (0)