Skip to content

Commit 46809ca

Browse files
chore: event cleanup, send first start before context/credential
1 parent 0127cca commit 46809ca

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

pkg/engine/engine.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ type CallContext struct {
6969

7070
type Context struct {
7171
commonContext
72-
Ctx context.Context
73-
Parent *Context
74-
Program *types.Program
75-
ToolCategory ToolCategory
72+
Ctx context.Context
73+
Parent *Context
74+
Program *types.Program
7675
}
7776

7877
type ToolCategory string

pkg/monitor/display.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (c *Console) Start(_ context.Context, prg *types.Program, _ []string, input
5151
mon.dump.Program = prg
5252
mon.dump.Input = input
5353

54-
log.Fields("runID", mon.dump.ID, "input", input, "program", prg).Debugf("Run started")
54+
log.Fields("runID", mon.dump.ID, "input", input, "program", prg, "type", "runStart").Debugf("Run started")
5555
return mon, nil
5656
}
5757

@@ -210,6 +210,7 @@ func (d *display) Event(event runner.Event) {
210210
"id", currentCall.ID,
211211
"parentID", currentCall.ParentID,
212212
"toolID", currentCall.ToolID,
213+
"type", event.Type,
213214
)
214215

215216
_, ok := d.callIDMap[currentCall.ID]
@@ -287,7 +288,7 @@ func (d *display) Stop(output string, err error) {
287288
d.callLock.Lock()
288289
defer d.callLock.Unlock()
289290

290-
log.Fields("runID", d.dump.ID, "output", output, "err", err).Debugf("Run stopped")
291+
log.Fields("runID", d.dump.ID, "output", output, "err", err, "type", "runFinish").Debugf("Run stopped")
291292
if d.usage.TotalTokens > 0 {
292293
log.Fields("runID", d.dump.ID, "total", d.usage.TotalTokens, "prompt", d.usage.PromptTokens, "completion", d.usage.CompletionTokens).Infof("usage ")
293294
}

pkg/runner/runner.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
367367
progress, progressClose := streamProgress(&callCtx, monitor)
368368
defer progressClose()
369369

370+
monitor.Event(Event{
371+
Time: time.Now(),
372+
CallContext: callCtx.GetCallContext(),
373+
Type: EventTypeCallStart,
374+
Content: input,
375+
})
376+
370377
if len(callCtx.Tool.Credentials) > 0 {
371378
var err error
372379
env, err = r.handleCredentials(callCtx, monitor, env)
@@ -396,13 +403,6 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
396403
Ports: &r.ports,
397404
}
398405

399-
monitor.Event(Event{
400-
Time: time.Now(),
401-
CallContext: callCtx.GetCallContext(),
402-
Type: EventTypeCallStart,
403-
Content: input,
404-
})
405-
406406
callCtx.Ctx = context2.AddPauseFuncToCtx(callCtx.Ctx, monitor.Pause)
407407

408408
ret, err := e.Start(callCtx, input)
@@ -719,7 +719,7 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
719719
for _, id := range ids {
720720
call := state.Continuation.Calls[id]
721721
d.Run(func(ctx context.Context) error {
722-
result, err := r.subCall(ctx, callCtx, monitor, env, call.ToolID, call.Input, id, "")
722+
result, err := r.subCall(ctx, callCtx, monitor, env, call.ToolID, call.Input, id, toolCategory)
723723
if err != nil {
724724
return err
725725
}

pkg/server/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/acorn-io/broadcaster"
1919
"github.com/gptscript-ai/gptscript/pkg/builtin"
2020
"github.com/gptscript-ai/gptscript/pkg/cache"
21+
"github.com/gptscript-ai/gptscript/pkg/counter"
2122
"github.com/gptscript-ai/gptscript/pkg/gptscript"
2223
"github.com/gptscript-ai/gptscript/pkg/loader"
2324
"github.com/gptscript-ai/gptscript/pkg/runner"
@@ -89,7 +90,7 @@ var (
8990
type execKey struct{}
9091

9192
func ContextWithNewID(ctx context.Context) context.Context {
92-
return context.WithValue(ctx, execKey{}, fmt.Sprint(atomic.AddInt64(&execID, 1)))
93+
return context.WithValue(ctx, execKey{}, counter.Next())
9394
}
9495

9596
func IDFromContext(ctx context.Context) string {

0 commit comments

Comments
 (0)