Skip to content

fix: send only one run start/finish event from sdkserver #551

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
Jun 25, 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: 6 additions & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,13 @@ func WithToolCategory(ctx context.Context, toolCategory ToolCategory) context.Co
return context.WithValue(ctx, toolCategoryKey{}, toolCategory)
}

func NewContext(ctx context.Context, prg *types.Program, input string) (Context, error) {
func ToolCategoryFromContext(ctx context.Context) ToolCategory {
category, _ := ctx.Value(toolCategoryKey{}).(ToolCategory)
return category
}

func NewContext(ctx context.Context, prg *types.Program, input string) (Context, error) {
category := ToolCategoryFromContext(ctx)

callCtx := Context{
commonContext: commonContext{
Expand Down
6 changes: 2 additions & 4 deletions pkg/monitor/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ type Console struct {
callLock sync.Mutex
}

var (
prettyIDCounter int64
)
var prettyIDCounter int64

func (c *Console) Start(_ context.Context, prg *types.Program, _ []string, input string) (runner.Monitor, error) {
id := counter.Next()
Expand Down Expand Up @@ -290,7 +288,7 @@ func (d *display) Event(event runner.Event) {
d.dump.Calls[currentIndex] = currentCall
}

func (d *display) Stop(output string, err error) {
func (d *display) Stop(_ context.Context, output string, err error) {
d.callLock.Lock()
defer d.callLock.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion pkg/monitor/fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (f *fd) event(event Event) {
}
}

func (f *fd) Stop(output string, err error) {
func (f *fd) Stop(_ context.Context, output string, err error) {
e := Event{
Event: runner.Event{
Time: time.Now(),
Expand Down
8 changes: 3 additions & 5 deletions pkg/runner/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"github.com/gptscript-ai/gptscript/pkg/types"
)

type noopFactory struct {
}
type noopFactory struct{}

func (n noopFactory) Start(context.Context, *types.Program, []string, string) (Monitor, error) {
return noopMonitor{}, nil
Expand All @@ -17,13 +16,12 @@ func (n noopFactory) Pause() func() {
return func() {}
}

type noopMonitor struct {
}
type noopMonitor struct{}

func (n noopMonitor) Event(Event) {
}

func (n noopMonitor) Stop(string, error) {}
func (n noopMonitor) Stop(context.Context, string, error) {}

func (n noopMonitor) Pause() func() {
return func() {}
Expand Down
16 changes: 5 additions & 11 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type MonitorFactory interface {
type Monitor interface {
Event(event Event)
Pause() func()
Stop(output string, err error)
Stop(ctx context.Context, output string, err error)
}

type Options struct {
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *Runner) Chat(ctx context.Context, prevState ChatState, prg types.Progra
return resp, err
}
defer func() {
monitor.Stop(resp.Content, err)
monitor.Stop(ctx, resp.Content, err)
}()

callCtx, err := engine.NewContext(ctx, &prg, input)
Expand Down Expand Up @@ -425,9 +425,7 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
}
}

var (
newState *State
)
var newState *State
callCtx.InputContext, newState, err = r.getContext(callCtx, state, monitor, env, input)
if err != nil {
return nil, err
Expand Down Expand Up @@ -632,9 +630,7 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
Env: env,
}

var (
contentInput string
)
var contentInput string

if state.Continuation != nil && state.Continuation.State != nil {
contentInput = state.Continuation.State.Input
Expand Down Expand Up @@ -745,9 +741,7 @@ func (r *Runner) newDispatcher(ctx context.Context) dispatcher {
}

func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string, state *State, toolCategory engine.ToolCategory) (_ *State, callResults []SubCallResult, _ error) {
var (
resultLock sync.Mutex
)
var resultLock sync.Mutex

if state.Continuation != nil {
callCtx.LastReturn = state.Continuation
Expand Down
28 changes: 19 additions & 9 deletions pkg/sdkserver/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gptscript-ai/broadcaster"
"github.com/gptscript-ai/gptscript/pkg/engine"
"github.com/gptscript-ai/gptscript/pkg/runner"
gserver "github.com/gptscript-ai/gptscript/pkg/server"
"github.com/gptscript-ai/gptscript/pkg/types"
Expand All @@ -23,16 +24,19 @@ func NewSessionFactory(events *broadcaster.Broadcaster[event]) *SessionFactory {

func (s SessionFactory) Start(ctx context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
id := gserver.RunIDFromContext(ctx)
category := engine.ToolCategoryFromContext(ctx)

s.events.C <- event{
Event: gserver.Event{
Event: runner.Event{
Time: time.Now(),
Type: runner.EventTypeRunStart,
if category == engine.NoCategory {
s.events.C <- event{
Event: gserver.Event{
Event: runner.Event{
Time: time.Now(),
Type: runner.EventTypeRunStart,
},
RunID: id,
Program: prg,
},
RunID: id,
Program: prg,
},
}
}

return &Session{
Expand Down Expand Up @@ -69,7 +73,13 @@ func (s *Session) Event(e runner.Event) {
}
}

func (s *Session) Stop(output string, err error) {
func (s *Session) Stop(ctx context.Context, output string, err error) {
category := engine.ToolCategoryFromContext(ctx)

if category != engine.NoCategory {
return
}

e := event{
Event: gserver.Event{
Event: runner.Event{
Expand Down
Loading