Files
sam ff8615b090 feat(gateway): stream model reasoning as reasoning chat events (#1907)
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.
2026-11-13 00:21:23 +11:00

193 lines
5.5 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"time"
"unicode/utf8"
"github.com/samcharles93/ai-sdk/core"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/tools"
)
// SummarizeToolParameters returns a bounded JSON shape of tool input.
func SummarizeToolParameters(input string) string {
return messaging.SummarizeToolParameters(input)
}
// ReasoningStream is the optional TurnStream extension for the model's
// thinking. It is separate from TurnStream so streams that cannot render
// reasoning keep satisfying TurnStream unchanged; producers type-assert.
type ReasoningStream interface {
// Reasoning appends the next fragment of the model's reasoning.
Reasoning(text string)
}
type toolCallRecorder struct {
next TurnStream
events []ToolCallEvent
}
func (r *toolCallRecorder) Delta(text string) {
if r.next != nil {
r.next.Delta(text)
}
}
func (r *toolCallRecorder) Reasoning(text string) {
if rs, ok := r.next.(ReasoningStream); ok {
rs.Reasoning(text)
}
}
func (r *toolCallRecorder) ToolCall(event ToolCallEvent) {
r.events = append(r.events, event)
if r.next != nil {
r.next.ToolCall(event)
}
}
func (r *toolCallRecorder) Media(event MediaEvent) {
if r.next != nil {
r.next.Media(event)
}
}
// pendingToolCall is a tool invocation awaiting its result.
type pendingToolCall struct {
name string
parameters string
started time.Time
offset int
}
// took is the call's running time so far, zero for a result with no
// recorded invocation.
func (c pendingToolCall) took() int64 {
if c.started.IsZero() {
return 0
}
return time.Since(c.started).Milliseconds()
}
// DrainStreamParts consumes an ai-sdk FullStream to close, reporting the
// model's reasoning, assistant text and each completed tool call to turn,
// and returns the assembled reply.
//
// The parts channel is authoritative and its writes are synchronous, so it
// must be drained to close even when turn is nil -- an unread stream stalls
// the generating goroutine.
//
// A tool is reported on its result, never on its call: the call part carries
// no outcome yet, so reporting both would show every tool twice, once with
// nothing to say.
func DrainStreamParts(parts <-chan core.StreamPart, turn TurnStream, icons map[string]string) string {
var sb strings.Builder
pending := make(map[string]pendingToolCall)
runes := 0
for part := range parts {
switch part.Type {
case core.StreamPartReasoningDelta:
reportReasoning(turn, part.ReasoningDelta)
case core.StreamPartTextDelta:
if part.TextDelta == "" {
continue
}
sb.WriteString(part.TextDelta)
runes += utf8.RuneCountInString(part.TextDelta)
if turn != nil {
turn.Delta(part.TextDelta)
}
case core.StreamPartToolCall:
if part.ToolCall == nil || part.ToolCall.ToolCallID == "" {
continue
}
pending[part.ToolCall.ToolCallID] = pendingToolCall{
name: part.ToolCall.ToolName,
parameters: SummarizeToolParameters(part.ToolCall.Input),
started: time.Now(),
offset: runes,
}
case core.StreamPartToolResult:
reportToolResult(turn, part.ToolResult, pending, icons)
}
}
return sb.String()
}
// reportReasoning forwards one reasoning fragment to a stream that can
// render thinking. Reasoning is not part of the reply text, so it travels on
// its own stream method; a stream without that method simply gets none.
func reportReasoning(turn TurnStream, delta string) {
if delta == "" {
return
}
if rs, ok := turn.(ReasoningStream); ok {
rs.Reasoning(delta)
}
}
// reportToolResult reports one finished tool invocation, plus any media
// attachment its output carries, to the turn.
func reportToolResult(turn TurnStream, result *core.ToolResult, pending map[string]pendingToolCall, icons map[string]string) {
if result == nil || turn == nil {
return
}
call := pending[result.ToolCallID]
delete(pending, result.ToolCallID)
name := result.ToolName
if name == "" {
name = call.name
}
turn.ToolCall(ToolCallEvent{
ID: result.ToolCallID,
Name: name,
Emoji: icons[name],
Parameters: call.parameters,
Output: result.Output,
Err: result.Error,
DurationMS: call.took(),
TextOffset: call.offset,
})
for _, ref := range multimodalMediaRefs(result.Output) {
// Path and URL are alternatives, not a pair: the channel uploads one
// and fetches the other. A ref carrying neither names nothing
// deliverable, so it is dropped rather than sent as an empty
// attachment.
if ref.URL == "" && ref.Path == "" {
continue
}
turn.Media(MediaEvent{
ToolName: name,
Attachment: MediaAttachment{
Type: ref.Type,
URL: ref.URL,
Path: ref.Path,
FileName: ref.FileName,
},
})
}
}
// multimodalMediaRefs extracts the URLs a tool result carries when its
// output is a tools.MultimodalResult, so DrainStreamParts can report each as
// a MediaEvent alongside the ordinary ToolCall event.
//
// output is decoded with json.Unmarshal directly rather than checked
// against a schema first: most tool output is plain text or unrelated
// JSON, and unmarshalling into MultimodalResult simply leaves IsMultimodal
// false for anything that doesn't match, which is exactly "not multimodal"
// -- no separate detection step earns its keep here.
func multimodalMediaRefs(output string) []tools.MediaRef {
if output == "" {
return nil
}
var result tools.MultimodalResult
if err := json.Unmarshal([]byte(output), &result); err != nil || !result.IsMultimodal {
return nil
}
return result.URLs
}