Skip to content

Commit b17c321

Browse files
chore: make context counters start with time.Now(), not 0
1 parent 313bd10 commit b17c321

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

pkg/counter/counter.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package counter
2+
3+
import (
4+
"fmt"
5+
"sync/atomic"
6+
"time"
7+
)
8+
9+
var counter = int32(time.Now().Unix())
10+
11+
func Reset(i int32) {
12+
atomic.StoreInt32(&counter, i)
13+
}
14+
15+
func Next() string {
16+
return fmt.Sprint(atomic.AddInt32(&counter, 1))
17+
}

pkg/engine/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import (
1212
"runtime"
1313
"sort"
1414
"strings"
15-
"sync/atomic"
1615

1716
"github.com/google/shlex"
1817
context2 "github.com/gptscript-ai/gptscript/pkg/context"
18+
"github.com/gptscript-ai/gptscript/pkg/counter"
1919
"github.com/gptscript-ai/gptscript/pkg/env"
2020
"github.com/gptscript-ai/gptscript/pkg/types"
2121
"github.com/gptscript-ai/gptscript/pkg/version"
2222
)
2323

2424
func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string, toolCategory ToolCategory) (cmdOut string, cmdErr error) {
25-
id := fmt.Sprint(atomic.AddInt64(&completionID, 1))
25+
id := counter.Next()
2626

2727
defer func() {
2828
e.Progress <- types.CompletionStatus{

pkg/engine/engine.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"fmt"
77
"strings"
88
"sync"
9-
"sync/atomic"
109

10+
"github.com/gptscript-ai/gptscript/pkg/counter"
1111
"github.com/gptscript-ai/gptscript/pkg/system"
1212
"github.com/gptscript-ai/gptscript/pkg/types"
1313
"github.com/gptscript-ai/gptscript/pkg/version"
1414
)
1515

16-
var completionID int64
17-
1816
type Model interface {
1917
Call(ctx context.Context, messageRequest types.CompletionRequest, status chan<- types.CompletionStatus) (*types.CompletionMessage, error)
2018
}
@@ -123,12 +121,10 @@ func (c *Context) MarshalJSON() ([]byte, error) {
123121
return json.Marshal(c.GetCallContext())
124122
}
125123

126-
var execID int32
127-
128124
func NewContext(ctx context.Context, prg *types.Program) Context {
129125
callCtx := Context{
130126
commonContext: commonContext{
131-
ID: fmt.Sprint(atomic.AddInt32(&execID, 1)),
127+
ID: counter.Next(),
132128
Tool: prg.ToolSet[prg.EntryToolID],
133129
},
134130
Ctx: ctx,
@@ -144,7 +140,7 @@ func (c *Context) SubCall(ctx context.Context, toolID, callID string, toolCatego
144140
}
145141

146142
if callID == "" {
147-
callID = fmt.Sprint(atomic.AddInt32(&execID, 1))
143+
callID = counter.Next()
148144
}
149145

150146
return Context{

pkg/engine/print.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package engine
22

33
import (
4-
"fmt"
54
"strings"
6-
"sync/atomic"
75

6+
"github.com/gptscript-ai/gptscript/pkg/counter"
87
"github.com/gptscript-ai/gptscript/pkg/types"
98
)
109

1110
func (e *Engine) runEcho(tool types.Tool) (cmdOut *Return, cmdErr error) {
12-
id := fmt.Sprint(atomic.AddInt64(&completionID, 1))
11+
id := counter.Next()
1312
out := strings.TrimPrefix(tool.Instructions, types.EchoPrefix+"\n")
1413

1514
e.Progress <- types.CompletionStatus{

pkg/monitor/display.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/fatih/color"
16+
"github.com/gptscript-ai/gptscript/pkg/counter"
1617
"github.com/gptscript-ai/gptscript/pkg/engine"
1718
"github.com/gptscript-ai/gptscript/pkg/runner"
1819
"github.com/gptscript-ai/gptscript/pkg/types"
@@ -40,12 +41,11 @@ type Console struct {
4041
}
4142

4243
var (
43-
runID int64
4444
prettyIDCounter int64
4545
)
4646

4747
func (c *Console) Start(_ context.Context, prg *types.Program, _ []string, input string) (runner.Monitor, error) {
48-
id := atomic.AddInt64(&runID, 1)
48+
id := counter.Next()
4949
mon := newDisplay(c.dumpState, c.displayProgress, c.printMessages)
5050
mon.dump.ID = fmt.Sprint(id)
5151
mon.dump.Program = prg

pkg/openai/client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"slices"
1010
"sort"
1111
"strings"
12-
"sync/atomic"
1312

1413
"github.com/getkin/kin-openapi/openapi3"
1514
openai "github.com/gptscript-ai/chat-completion-client"
1615
"github.com/gptscript-ai/gptscript/pkg/cache"
16+
"github.com/gptscript-ai/gptscript/pkg/counter"
1717
"github.com/gptscript-ai/gptscript/pkg/hash"
1818
"github.com/gptscript-ai/gptscript/pkg/system"
1919
"github.com/gptscript-ai/gptscript/pkg/types"
@@ -24,10 +24,9 @@ const (
2424
)
2525

2626
var (
27-
key = os.Getenv("OPENAI_API_KEY")
28-
url = os.Getenv("OPENAI_URL")
29-
azureModel = os.Getenv("OPENAI_AZURE_DEPLOYMENT")
30-
completionID int64
27+
key = os.Getenv("OPENAI_API_KEY")
28+
url = os.Getenv("OPENAI_URL")
29+
azureModel = os.Getenv("OPENAI_AZURE_DEPLOYMENT")
3130
)
3231

3332
type Client struct {
@@ -332,7 +331,7 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
332331
})
333332
}
334333

335-
id := fmt.Sprint(atomic.AddInt64(&completionID, 1))
334+
id := counter.Next()
336335
status <- types.CompletionStatus{
337336
CompletionID: id,
338337
Request: request,

0 commit comments

Comments
 (0)