Skip to content

Commit 4b587e2

Browse files
committed
feat: collect calls as they come for easy processing
Signed-off-by: Donnie Adams <[email protected]>
1 parent 8452bda commit 4b587e2

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

run.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"log/slog"
11+
"maps"
1112
"net/http"
1213
"os/exec"
1314
"strconv"
@@ -26,10 +27,13 @@ type Run struct {
2627
wait func()
2728
basicCommand bool
2829

29-
rawOutput map[string]any
30-
output, errput string
31-
events chan Frame
32-
lock sync.Mutex
30+
callsLock sync.RWMutex
31+
calls map[string]CallFrame
32+
parentCallFrameID string
33+
rawOutput map[string]any
34+
output, errput string
35+
events chan Frame
36+
lock sync.Mutex
3337
}
3438

3539
// Text returns the text output of the gptscript. It blocks until the output is ready.
@@ -59,6 +63,25 @@ func (r *Run) Err() error {
5963
return r.err
6064
}
6165

66+
// Calls will return a flattened array of the calls for this run.
67+
func (r *Run) Calls() map[string]CallFrame {
68+
r.callsLock.RLock()
69+
defer r.callsLock.RUnlock()
70+
return maps.Clone(r.calls)
71+
}
72+
73+
// ParentCallFrame returns the CallFrame for the top-level or "parent" call. The boolean indicates whether there is a parent CallFrame.
74+
func (r *Run) ParentCallFrame() (CallFrame, bool) {
75+
r.callsLock.RLock()
76+
defer r.callsLock.RUnlock()
77+
78+
if r.parentCallFrameID == "" {
79+
return CallFrame{}, false
80+
}
81+
82+
return r.calls[r.parentCallFrameID], true
83+
}
84+
6285
// ErrorOutput returns the stderr output of the gptscript.
6386
// Should only be called after Bytes or Text has returned an error.
6487
func (r *Run) ErrorOutput() string {
@@ -287,6 +310,15 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
287310
return
288311
}
289312

313+
if event.Call != nil {
314+
r.callsLock.Lock()
315+
r.calls[event.Call.ID] = *event.Call
316+
if r.parentCallFrameID == "" && event.Call.ParentID == "" {
317+
r.parentCallFrameID = event.Call.ID
318+
}
319+
r.callsLock.Unlock()
320+
}
321+
290322
if r.opts.IncludeEvents {
291323
r.events <- event
292324
}

0 commit comments

Comments
 (0)