Skip to content

Commit 7cf2b83

Browse files
authored
Stream JSON events for invoke cmd (#455)
* record thoughts * added json outputs for testing * verbose
1 parent 113618c commit 7cf2b83

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

.github/data/agent-framework/run-challenge.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ log "Trying to fix the kagent broken environment using Kagent..."
6363
touch "${scenario_dir}/$NAME.thought.log"
6464
mkdir -p "${scenario_dir}/results"
6565

66-
timeout --signal=INT 3m bash -c 'echo "$1" | kagent invoke --agent "k8s-agent" --task -' -- "$USER_PROMPT" > "${scenario_dir}/$NAME.thought.log" 2>&1
66+
timeout --signal=INT 3m bash -c 'echo "$1" | kagent invoke -v --agent "k8s-agent" -S --task -' -- "$USER_PROMPT" > "${scenario_dir}/results/$NAME.thought.log" 2>&1
6767

6868
TIMEOUT_STATUS=$?
6969
if [ $TIMEOUT_STATUS -eq 124 ]; then

go/cli/internal/cli/utils.go

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,34 @@ func StreamEvents(ch <-chan *autogen_client.SseEvent, usage *autogen_client.Mode
200200
if typed.Source == "user" || typed.Source == "system" {
201201
continue
202202
}
203-
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldYellow("Event Type"), "TextMessage")
204-
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldGreen("Source"), typed.Source)
205-
fmt.Fprintln(os.Stdout)
206-
fmt.Fprintln(os.Stdout, typed.Content)
207-
fmt.Fprintln(os.Stdout, "----------------------------------")
208-
fmt.Fprintln(os.Stdout)
203+
if verbose {
204+
enc := json.NewEncoder(os.Stdout)
205+
enc.SetIndent("", " ")
206+
if err := enc.Encode(typed); err != nil {
207+
fmt.Fprintf(os.Stderr, "Error encoding event: %v\n", err)
208+
continue
209+
}
210+
} else {
211+
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldYellow("Event Type"), "TextMessage")
212+
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldGreen("Source"), typed.Source)
213+
fmt.Fprintln(os.Stdout)
214+
fmt.Fprintln(os.Stdout, typed.Content)
215+
fmt.Fprintln(os.Stdout, "----------------------------------")
216+
fmt.Fprintln(os.Stdout)
217+
}
209218
case *ModelClientStreamingChunkEvent:
210219
usage.Add(typed.ModelsUsage)
211220
streaming[typed.Source] = true
212-
fmt.Fprintf(os.Stdout, "%s", typed.Content)
221+
if verbose {
222+
enc := json.NewEncoder(os.Stdout)
223+
enc.SetIndent("", " ")
224+
if err := enc.Encode(typed); err != nil {
225+
fmt.Fprintf(os.Stderr, "Error encoding event: %v\n", err)
226+
continue
227+
}
228+
} else {
229+
fmt.Fprintf(os.Stdout, "%s", typed.Content)
230+
}
213231
case *ToolCallRequestEvent:
214232
bufferedToolCallRequest = typed
215233
case *ToolCallExecutionEvent:
@@ -218,26 +236,20 @@ func StreamEvents(ch <-chan *autogen_client.SseEvent, usage *autogen_client.Mode
218236
continue
219237
}
220238
usage.Add(typed.ModelsUsage)
221-
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldYellow("Event Type"), "ToolCall(s)")
222-
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldGreen("Source"), typed.Source)
223239
if verbose {
224-
// For each function execution, find the corresponding tool call request and print them together
225-
for i, functionExecution := range typed.Content {
226-
for _, functionRequest := range bufferedToolCallRequest.Content {
227-
if functionExecution.CallID == functionRequest.ID {
228-
fmt.Fprintln(os.Stdout)
229-
fmt.Fprintln(os.Stdout, "++++++++")
230-
fmt.Fprintf(os.Stdout, "Tool Call %d: (id: %s)\n", i, functionRequest.ID)
231-
fmt.Fprintln(os.Stdout)
232-
fmt.Fprintf(os.Stdout, "%s(%s)\n", functionRequest.Name, functionRequest.Arguments)
233-
fmt.Fprintln(os.Stdout)
234-
fmt.Fprintln(os.Stdout, functionExecution.Content)
235-
fmt.Fprintln(os.Stdout, "++++++++")
236-
fmt.Fprintln(os.Stdout)
237-
}
238-
}
240+
enc := json.NewEncoder(os.Stdout)
241+
out := map[string]interface{}{
242+
"request": bufferedToolCallRequest,
243+
"execution": typed,
244+
}
245+
enc.SetIndent("", " ")
246+
if err := enc.Encode(out); err != nil {
247+
fmt.Fprintf(os.Stderr, "Error encoding event: %v\n", err)
248+
continue
239249
}
240250
} else {
251+
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldYellow("Event Type"), "ToolCall(s)")
252+
fmt.Fprintf(os.Stdout, "%s: %s\n", config.BoldGreen("Source"), typed.Source)
241253
tw := table.NewWriter()
242254
tw.AppendHeader(table.Row{"#", "Name", "Arguments"})
243255
for idx, functionRequest := range bufferedToolCallRequest.Content {
@@ -246,8 +258,11 @@ func StreamEvents(ch <-chan *autogen_client.SseEvent, usage *autogen_client.Mode
246258
fmt.Fprintln(os.Stdout, tw.Render())
247259
}
248260

249-
fmt.Fprintln(os.Stdout, "----------------------------------")
250-
fmt.Fprintln(os.Stdout)
261+
if !verbose {
262+
fmt.Fprintln(os.Stdout, "----------------------------------")
263+
fmt.Fprintln(os.Stdout)
264+
}
265+
251266
bufferedToolCallRequest = nil
252267
}
253268
}

0 commit comments

Comments
 (0)