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