Skip to content

fix: ensure the parent call frame is of category none #82

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
Nov 2, 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
11 changes: 11 additions & 0 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ type RunFrame struct {
Type EventType `json:"type"`
}

type CallFrames map[string]CallFrame

func (c CallFrames) ParentCallFrame() CallFrame {
for _, call := range c {
if call.ParentID == "" && call.ToolCategory == NoCategory {
return call
}
}
return CallFrame{}
}

type CallFrame struct {
CallContext `json:",inline"`

Expand Down
28 changes: 10 additions & 18 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ type Run struct {
wait func()
basicCommand bool

program *Program
callsLock sync.RWMutex
calls map[string]CallFrame
parentCallFrameID string
rawOutput map[string]any
output, errput string
events chan Frame
lock sync.Mutex
responseCode int
program *Program
callsLock sync.RWMutex
calls CallFrames
rawOutput map[string]any
output, errput string
events chan Frame
lock sync.Mutex
responseCode int
}

// Text returns the text output of the gptscript. It blocks until the output is ready.
Expand Down Expand Up @@ -104,7 +103,7 @@ func (r *Run) RespondingTool() Tool {
}

// Calls will return a flattened array of the calls for this run.
func (r *Run) Calls() map[string]CallFrame {
func (r *Run) Calls() CallFrames {
r.callsLock.RLock()
defer r.callsLock.RUnlock()
return maps.Clone(r.calls)
Expand All @@ -115,11 +114,7 @@ func (r *Run) ParentCallFrame() (CallFrame, bool) {
r.callsLock.RLock()
defer r.callsLock.RUnlock()

if r.parentCallFrameID == "" {
return CallFrame{}, false
}

return r.calls[r.parentCallFrameID], true
return r.calls.ParentCallFrame(), true
}

// ErrorOutput returns the stderr output of the gptscript.
Expand Down Expand Up @@ -394,9 +389,6 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
if event.Call != nil {
r.callsLock.Lock()
r.calls[event.Call.ID] = *event.Call
if r.parentCallFrameID == "" && event.Call.ParentID == "" {
r.parentCallFrameID = event.Call.ID
}
r.callsLock.Unlock()
} else if event.Run != nil {
if event.Run.Type == EventTypeRunStart {
Expand Down