The ai-sdk FullStream drain moves from the archiegateway composition into
internal/gateway (DrainStreamParts), next to the TurnStream it feeds, and
gains a StreamPartReasoningDelta case: each reasoning delta is forwarded on
the new optional ReasoningStream method, which localChatStream turns into a
ChatEvent{Kind: "reasoning", Text: delta} on the same stream. done/error
semantics are unchanged, and the FullStream is still drained to close when no
stream is attached.
125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/ai-sdk/core"
|
|
)
|
|
|
|
// A fake ai-sdk FullStream drives the drain: every part must reach the turn
|
|
// stream in order, and a reasoning delta must surface as a Kind "reasoning"
|
|
// ChatEvent carrying its text. Text deltas and tool results keep working.
|
|
func TestDrainStreamPartsForwardsStreamParts(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
parts []core.StreamPart
|
|
want []ChatEvent
|
|
wantText string
|
|
}{
|
|
{
|
|
name: "reasoning deltas become reasoning events",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartReasoningDelta, ReasoningDelta: "considering "},
|
|
{Type: core.StreamPartReasoningDelta, ReasoningDelta: "the request"},
|
|
},
|
|
want: []ChatEvent{
|
|
{Kind: "reasoning", Text: "considering ", SessionID: "s1"},
|
|
{Kind: "reasoning", Text: "the request", SessionID: "s1"},
|
|
},
|
|
},
|
|
{
|
|
name: "text deltas still stream and assemble the reply",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartTextDelta, TextDelta: "Hello, "},
|
|
{Type: core.StreamPartTextDelta, TextDelta: "world"},
|
|
},
|
|
want: []ChatEvent{
|
|
{Kind: "delta", Text: "Hello, ", SessionID: "s1"},
|
|
{Kind: "delta", Text: "world", SessionID: "s1"},
|
|
},
|
|
wantText: "Hello, world",
|
|
},
|
|
{
|
|
name: "reasoning ahead of the answer text",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartReasoningDelta, ReasoningDelta: "hmm"},
|
|
{Type: core.StreamPartTextDelta, TextDelta: "answer"},
|
|
},
|
|
want: []ChatEvent{
|
|
{Kind: "reasoning", Text: "hmm", SessionID: "s1"},
|
|
{Kind: "delta", Text: "answer", SessionID: "s1"},
|
|
},
|
|
wantText: "answer",
|
|
},
|
|
{
|
|
name: "an empty reasoning delta is dropped",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartReasoningDelta, ReasoningDelta: ""},
|
|
{Type: core.StreamPartTextDelta, TextDelta: "hi"},
|
|
},
|
|
want: []ChatEvent{{Kind: "delta", Text: "hi", SessionID: "s1"}},
|
|
wantText: "hi",
|
|
},
|
|
{
|
|
name: "a completed tool call still reports its tool event",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "t1", ToolName: "search", Input: `{"q":"archie"}`}},
|
|
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolCallID: "t1", ToolName: "search", Output: "42"}},
|
|
},
|
|
want: []ChatEvent{{
|
|
Kind: "tool",
|
|
SessionID: "s1",
|
|
Tool: ToolCallEvent{
|
|
ID: "t1",
|
|
Name: "search",
|
|
Emoji: "🔍",
|
|
Parameters: SummarizeToolParameters(`{"q":"archie"}`),
|
|
Output: "42",
|
|
},
|
|
}},
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
parts := make(chan core.StreamPart, len(tc.parts))
|
|
for _, part := range tc.parts {
|
|
parts <- part
|
|
}
|
|
close(parts)
|
|
|
|
events := make(chan ChatEvent, len(tc.parts)+1)
|
|
stream := localChatStream{done: make(chan struct{}), events: events, sessionID: "s1"}
|
|
text := DrainStreamParts(parts, stream, map[string]string{"search": "🔍"})
|
|
|
|
var got []ChatEvent
|
|
for {
|
|
select {
|
|
case event := <-events:
|
|
got = append(got, event)
|
|
default:
|
|
if !reflect.DeepEqual(got, tc.want) {
|
|
t.Fatalf("events = %+v, want %+v", got, tc.want)
|
|
}
|
|
if text != tc.wantText {
|
|
t.Fatalf("reply text = %q, want %q", text, tc.wantText)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A stream nobody renders still has to be drained to close: an unread
|
|
// FullStream stalls the generating goroutine, reasoning included.
|
|
func TestDrainStreamPartsWithoutStreamStillDrains(t *testing.T) {
|
|
parts := make(chan core.StreamPart, 3)
|
|
parts <- core.StreamPart{Type: core.StreamPartReasoningDelta, ReasoningDelta: "thinking"}
|
|
parts <- core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "reply"}
|
|
close(parts)
|
|
if text := DrainStreamParts(parts, nil, nil); text != "reply" {
|
|
t.Fatalf("reply text = %q, want %q", text, "reply")
|
|
}
|
|
}
|