streaming: handle tool streaming via empty-content start signal

This commit is contained in:
Levi Neely 2026-07-19 21:36:55 +02:00
parent 4b24b33377
commit 208db7ba68
1 changed files with 13 additions and 13 deletions

View File

@ -147,17 +147,15 @@ func (sess *Session) startEventLog() {
sess.AppendLog(FormatEvent(ev)) sess.AppendLog(FormatEvent(ev))
case "tool": case "tool":
// Tool events can stream (multiple chunks with same name) or // Tool events: empty content = header-only (stream start),
// arrive as a single final result. Write header on first chunk, // non-empty while streaming = chunk, non-empty without streaming = full result.
// then append content. Duplicate final result is suppressed.
if streamingRole == "tool" { if streamingRole == "tool" {
// Continuation of streaming tool output. if ev.Content == "" {
if ev.Content != "" { // End of stream.
sess.AppendLog([]byte(ev.Content))
} else {
// Empty content = final result marker; streaming is done.
sess.AppendLog([]byte("\n")) sess.AppendLog([]byte("\n"))
streamingRole = "" streamingRole = ""
} else {
sess.AppendLog([]byte(ev.Content))
} }
return return
} }
@ -165,12 +163,14 @@ func (sess *Session) startEventLog() {
sess.AppendLog([]byte("\n")) sess.AppendLog([]byte("\n"))
streamingRole = "" streamingRole = ""
} }
// First tool event — write header and start streaming. if ev.Content == "" {
sess.AppendLog([]byte("[tool:" + ev.Name + "]\n")) // Stream start: write header, enter streaming mode.
if ev.Content != "" { sess.AppendLog([]byte("[tool:" + ev.Name + "]\n"))
sess.AppendLog([]byte(ev.Content)) streamingRole = "tool"
} else {
// Non-streamed tool result (fast tool, no streaming happened).
sess.AppendLog(FormatEvent(ev))
} }
streamingRole = "tool"
case "call": case "call":
if streamingRole != "" { if streamingRole != "" {