Skip to content

chore: event cleanup, send first start before context/credential #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ type CallContext struct {

type Context struct {
commonContext
Ctx context.Context
Parent *Context
Program *types.Program
ToolCategory ToolCategory
Ctx context.Context
Parent *Context
Program *types.Program
}

type ToolCategory string
Expand Down
5 changes: 3 additions & 2 deletions pkg/monitor/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Console) Start(_ context.Context, prg *types.Program, _ []string, input
mon.dump.Program = prg
mon.dump.Input = input

log.Fields("runID", mon.dump.ID, "input", input, "program", prg).Debugf("Run started")
log.Fields("runID", mon.dump.ID, "input", input, "program", prg, "type", "runStart").Debugf("Run started")
return mon, nil
}

Expand Down Expand Up @@ -210,6 +210,7 @@ func (d *display) Event(event runner.Event) {
"id", currentCall.ID,
"parentID", currentCall.ParentID,
"toolID", currentCall.ToolID,
"type", event.Type,
)

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

log.Fields("runID", d.dump.ID, "output", output, "err", err).Debugf("Run stopped")
log.Fields("runID", d.dump.ID, "output", output, "err", err, "type", "runFinish").Debugf("Run stopped")
if d.usage.TotalTokens > 0 {
log.Fields("runID", d.dump.ID, "total", d.usage.TotalTokens, "prompt", d.usage.PromptTokens, "completion", d.usage.CompletionTokens).Infof("usage ")
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
progress, progressClose := streamProgress(&callCtx, monitor)
defer progressClose()

monitor.Event(Event{
Time: time.Now(),
CallContext: callCtx.GetCallContext(),
Type: EventTypeCallStart,
Content: input,
})

if len(callCtx.Tool.Credentials) > 0 {
var err error
env, err = r.handleCredentials(callCtx, monitor, env)
Expand Down Expand Up @@ -396,13 +403,6 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
Ports: &r.ports,
}

monitor.Event(Event{
Time: time.Now(),
CallContext: callCtx.GetCallContext(),
Type: EventTypeCallStart,
Content: input,
})

callCtx.Ctx = context2.AddPauseFuncToCtx(callCtx.Ctx, monitor.Pause)

ret, err := e.Start(callCtx, input)
Expand Down Expand Up @@ -719,7 +719,7 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
for _, id := range ids {
call := state.Continuation.Calls[id]
d.Run(func(ctx context.Context) error {
result, err := r.subCall(ctx, callCtx, monitor, env, call.ToolID, call.Input, id, "")
result, err := r.subCall(ctx, callCtx, monitor, env, call.ToolID, call.Input, id, toolCategory)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/acorn-io/broadcaster"
"github.com/gptscript-ai/gptscript/pkg/builtin"
"github.com/gptscript-ai/gptscript/pkg/cache"
"github.com/gptscript-ai/gptscript/pkg/counter"
"github.com/gptscript-ai/gptscript/pkg/gptscript"
"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/runner"
Expand Down Expand Up @@ -89,7 +90,7 @@ var (
type execKey struct{}

func ContextWithNewID(ctx context.Context) context.Context {
return context.WithValue(ctx, execKey{}, fmt.Sprint(atomic.AddInt64(&execID, 1)))
return context.WithValue(ctx, execKey{}, counter.Next())
}

func IDFromContext(ctx context.Context) string {
Expand Down