feat(model): Top5#1 结构化流事件 + WS 事件体系(对齐 OpenClaw AssistantMessageEvent)
- 新增 model/stream.go:StreamKind(text/thinking/tool_call 各 start/delta/end + tool_execution)
+ StreamEvent{Kind,Delta,Index,ID}。
- ChatStream 签名 onChunk func(delta string)→onEvent func(StreamEvent);loop OnStream 同改;
streamStep 转发结构化事件。
- openai:text_delta + text_start/text_end(正文三段式);anthropic:文本块 text_*/tool_call_*
按块 Index(content_block_stop 分支)真分段;fallback:有事件即交付。
- ws_hub:wsEvent 壳补 turnId/seq;新增 broadcastStream;streamChat 每帧打 turnId+seq;
stream.go 把事件 publish 到 bus ws.stream。
- ws_bridge:streamEventData 映射 text_*→message.start/delta/end、thinking_*→thinking.*、
tool_call_*→tool_call.*、tool_execution_update→tool_execution.update,未知回退 stream_chunk。
- 前端 ws.ts:解析 message.start/delta/end、thinking.start/delta/end(pendingThinking)、
tool_call.start/end→addTool、tool_execution.update;保留旧 stream_chunk/thinking/tool_used 兼容;
types.ts 补 turnId/seq。Docs/api-client.md 增 §3.1 v0.4。
已知限制(注释/文档标注):turnId 在 OnStream→bus→relay 路径暂为空(StreamEvent 无 turnId 字段,
bus ws.stream 载荷不带 turn);OpenAI adapter 未解析 tool_call 段;thinking 段两 adapter 暂不发;
tool_execution.update 是保留项(随工具循环钩子后置)。go build/vet/test + 前端 tsc 全绿。
This commit is contained in:
1 parent
54f0b75646
commit
24d5924cee
23 files changed
+425
-90
No files matched your search
@@ -32,7 +32,7 @@ func (m *mockAdapter) Chat(_ context.Context, req model.Request, _ model.Options
|
||||
return m.resp, m.err
|
||||
}
|
||||
|
||||
func (m *mockAdapter) ChatStream(_ context.Context, _ model.Request, _ func(string)) (model.Response, error) {
|
||||
func (m *mockAdapter) ChatStream(_ context.Context, _ model.Request, _ func(model.StreamEvent)) (model.Response, error) {
|
||||
return m.resp, m.err
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/guardrail"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/persist"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/tool"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
|
||||
@@ -79,7 +80,7 @@ type Loop struct {
|
||||
guards *guardrail.Pipeline
|
||||
personaID string
|
||||
maxRounds int
|
||||
onStream func(delta string)
|
||||
onStream func(model.StreamEvent)
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
|
||||
@@ -281,7 +281,7 @@ func TestRun_StreamDeltas(t *testing.T) {
|
||||
var deltas []string
|
||||
l := New(Options{
|
||||
Agent: a,
|
||||
OnStream: func(d string) { mu.Lock(); deltas = append(deltas, d); mu.Unlock() },
|
||||
OnStream: func(ev model.StreamEvent) { mu.Lock(); deltas = append(deltas, ev.Delta); mu.Unlock() },
|
||||
MaxRounds: 8,
|
||||
})
|
||||
|
||||
@@ -306,7 +306,7 @@ func TestRun_StreamGuardrailBlocks(t *testing.T) {
|
||||
pipe := guardrail.New(guardrail.WithGuard(g))
|
||||
ad := &mockAdapter{}
|
||||
a := newTestAgent(t, ad, nil)
|
||||
l := New(Options{Agent: a, Guards: pipe, OnStream: func(string) {}, MaxRounds: 8})
|
||||
l := New(Options{Agent: a, Guards: pipe, OnStream: func(model.StreamEvent) {}, MaxRounds: 8})
|
||||
|
||||
res, err := l.Run(context.Background(), "sess", []model.Message{model.TextMessage(model.RoleUser, "hi")})
|
||||
if err != nil {
|
||||
|
||||
@@ -59,14 +59,14 @@ func (m *mockAdapter) Chat(_ context.Context, req model.Request, _ model.Options
|
||||
return r.resp, r.err
|
||||
}
|
||||
|
||||
func (m *mockAdapter) ChatStream(_ context.Context, req model.Request, on func(string)) (model.Response, error) {
|
||||
func (m *mockAdapter) ChatStream(_ context.Context, req model.Request, on func(model.StreamEvent)) (model.Response, error) {
|
||||
m.streamReqs = append(m.streamReqs, req)
|
||||
i := m.streamCalls
|
||||
m.streamCalls++
|
||||
if i < len(m.chunks) {
|
||||
for _, c := range m.chunks[i] {
|
||||
if on != nil {
|
||||
on(c)
|
||||
on(model.StreamEvent{Kind: model.StreamKindTextDelta, Delta: c})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/guardrail"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/persist"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/tool"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
|
||||
@@ -47,9 +48,10 @@ type Options struct {
|
||||
// DefaultMaxRounds (8).
|
||||
MaxRounds int
|
||||
// OnStream, when non-nil, switches the loop to the streaming path: the model
|
||||
// adapter's ChatStream is driven directly and each token delta is forwarded
|
||||
// to this callback.
|
||||
OnStream func(delta string)
|
||||
// adapter's ChatStream is driven directly and each structured stream event is
|
||||
// forwarded to this callback (model.StreamEvent), so the frontend can render
|
||||
// body text, tool calls and (eventually) reasoning segments independently.
|
||||
OnStream func(model.StreamEvent)
|
||||
// Log is the base logger. Nil is replaced with a discard logger tagged with
|
||||
// source "runtime/loop".
|
||||
Log *slog.Logger
|
||||
|
||||
@@ -31,9 +31,9 @@ func (l *Loop) streamStep(ctx context.Context, messages []model.Message) (*stepO
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := adapter.ChatStream(ctx, req, func(delta string) {
|
||||
resp, err := adapter.ChatStream(ctx, req, func(ev model.StreamEvent) {
|
||||
if l.onStream != nil {
|
||||
l.onStream(delta)
|
||||
l.onStream(ev)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -25,7 +25,7 @@ func (m *mockAdapter) Chat(ctx context.Context, req model.Request, opts model.Op
|
||||
return model.Response{Content: "[]"}, nil
|
||||
}
|
||||
|
||||
func (m *mockAdapter) ChatStream(ctx context.Context, req model.Request, onChunk func(delta string)) (model.Response, error) {
|
||||
func (m *mockAdapter) ChatStream(ctx context.Context, req model.Request, onEvent func(model.StreamEvent)) (model.Response, error) {
|
||||
return model.Response{}, model.ErrStream
|
||||
}
|
||||
|
||||
|
||||
@@ -34,8 +34,11 @@ type Adapter interface {
|
||||
Name() string
|
||||
// Chat performs a non-streaming completion and returns the full response.
|
||||
Chat(ctx context.Context, req Request, opts Options) (Response, error)
|
||||
// ChatStream streams token-level deltas via onChunk and returns the
|
||||
// aggregated response (with usage when the provider reports it). onChunk may
|
||||
// be nil to discard deltas while still aggregating.
|
||||
ChatStream(ctx context.Context, req Request, onChunk func(delta string)) (Response, error)
|
||||
// ChatStream streams structured events via onEvent and returns the aggregated
|
||||
// response (with usage when the provider reports it). onEvent may be nil to
|
||||
// discard events while still aggregating. Every provider-supported segment is
|
||||
// bracketed (text_start/delta/end, tool_call_start/delta/end), so the consumer
|
||||
// can render each independently; segments adapters cannot yet distinguish are
|
||||
// delivered as text_delta events.
|
||||
ChatStream(ctx context.Context, req Request, onEvent func(StreamEvent)) (Response, error)
|
||||
}
|
||||
@@ -169,9 +169,12 @@ func (a *AnthropicChatAdapter) Chat(ctx context.Context, req Request, opts Optio
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ChatStream streams content-block deltas via onChunk and returns the aggregated
|
||||
// response with usage when the provider reports it.
|
||||
func (a *AnthropicChatAdapter) ChatStream(ctx context.Context, req Request, onChunk func(delta string)) (Response, error) {
|
||||
// ChatStream streams structured events via onEvent and returns the aggregated
|
||||
// response with usage when the provider reports it. Text blocks are delivered as
|
||||
// text_start/text_delta/text_end events; tool_use blocks are delivered as
|
||||
// tool_call_start/tool_call_delta/tool_call_end so the frontend can render each
|
||||
// segment independently.
|
||||
func (a *AnthropicChatAdapter) ChatStream(ctx context.Context, req Request, onEvent func(StreamEvent)) (Response, error) {
|
||||
start := time.Now()
|
||||
if a.baseURL == "" {
|
||||
return Response{}, ErrUpstream
|
||||
@@ -197,7 +200,7 @@ func (a *AnthropicChatAdapter) ChatStream(ctx context.Context, req Request, onCh
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return Response{}, a.upstreamError(resp)
|
||||
}
|
||||
out, err := scanAnthropicSSE(resp.Body, onChunk)
|
||||
out, err := scanAnthropicSSE(resp.Body, onEvent)
|
||||
if err != nil {
|
||||
return Response{}, err
|
||||
}
|
||||
|
||||
@@ -52,11 +52,13 @@ type streamBlock struct {
|
||||
}
|
||||
|
||||
// scanAnthropicSSE incrementally decodes Anthropic's content-block stream. Text
|
||||
// deltas are forwarded to onChunk; tool_use input_json_delta fragments are
|
||||
// accumulated per block and re-assembled into a structured Args map when the block
|
||||
// stops. The stream must terminate with a message_stop event, otherwise ErrStream
|
||||
// is returned.
|
||||
func scanAnthropicSSE(r io.Reader, onChunk func(string)) (Response, error) {
|
||||
// deltas are forwarded as text_delta events (segment bracketed by
|
||||
// text_start/text_end); tool_use input_json_delta fragments are forwarded as
|
||||
// tool_call_delta events (segment bracketed by tool_call_start/tool_call_end)
|
||||
// and accumulated per block so they re-assemble into a structured Args map when
|
||||
// the block stops. The stream must terminate with a message_stop event,
|
||||
// otherwise ErrStream is returned.
|
||||
func scanAnthropicSSE(r io.Reader, onEvent func(StreamEvent)) (Response, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Buffer(make([]byte, 0, sseBufInit), maxSSELineBytes)
|
||||
agg := Response{}
|
||||
@@ -78,7 +80,7 @@ func scanAnthropicSSE(r io.Reader, onChunk func(string)) (Response, error) {
|
||||
if err := json.Unmarshal([]byte(data), &evt); err != nil {
|
||||
return Response{}, fmt.Errorf("model: %w: decode stream chunk: %v", ErrUpstream, err)
|
||||
}
|
||||
if err := applyAnthropicEvent(&agg, blocks, &order, &text, evt, onChunk); err != nil {
|
||||
if err := applyAnthropicEvent(&agg, blocks, &order, &text, evt, onEvent); err != nil {
|
||||
return Response{}, err
|
||||
}
|
||||
if evt.Type == "message_stop" {
|
||||
@@ -102,7 +104,7 @@ func applyAnthropicEvent(
|
||||
order *[]int,
|
||||
text *strings.Builder,
|
||||
evt anthropicStreamEvent,
|
||||
onChunk func(string),
|
||||
onEvent func(StreamEvent),
|
||||
) error {
|
||||
switch evt.Type {
|
||||
case "message_start":
|
||||
@@ -123,11 +125,21 @@ func applyAnthropicEvent(
|
||||
name: evt.Content.Name,
|
||||
initialArgs: evt.Content.Input,
|
||||
}
|
||||
if evt.Content.Type == "text" && evt.Content.Text != "" {
|
||||
b.text.WriteString(evt.Content.Text)
|
||||
text.WriteString(evt.Content.Text)
|
||||
if onChunk != nil {
|
||||
onChunk(evt.Content.Text)
|
||||
switch evt.Content.Type {
|
||||
case "text":
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindTextStart, Index: *evt.Index})
|
||||
}
|
||||
if evt.Content.Text != "" {
|
||||
b.text.WriteString(evt.Content.Text)
|
||||
text.WriteString(evt.Content.Text)
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindTextDelta, Delta: evt.Content.Text, Index: *evt.Index})
|
||||
}
|
||||
}
|
||||
case "tool_use":
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindToolCallStart, Index: *evt.Index, ID: evt.Content.ID})
|
||||
}
|
||||
}
|
||||
if _, ok := blocks[*evt.Index]; !ok {
|
||||
@@ -146,11 +158,31 @@ func applyAnthropicEvent(
|
||||
case "text_delta":
|
||||
b.text.WriteString(evt.Delta.Text)
|
||||
text.WriteString(evt.Delta.Text)
|
||||
if onChunk != nil {
|
||||
onChunk(evt.Delta.Text)
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindTextDelta, Delta: evt.Delta.Text, Index: *evt.Index})
|
||||
}
|
||||
case "input_json_delta":
|
||||
b.input.WriteString(evt.Delta.PartialJSON)
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindToolCallDelta, Delta: evt.Delta.PartialJSON, Index: *evt.Index, ID: b.id})
|
||||
}
|
||||
}
|
||||
case "content_block_stop":
|
||||
if evt.Index == nil {
|
||||
return nil
|
||||
}
|
||||
b := blocks[*evt.Index]
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
if b.typ == "text" {
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindTextEnd, Index: *evt.Index})
|
||||
}
|
||||
} else if b.typ == "tool_use" {
|
||||
if onEvent != nil {
|
||||
onEvent(StreamEvent{Kind: StreamKindToolCallEnd, Index: *evt.Index, ID: b.id})
|
||||
}
|
||||
}
|
||||
case "message_delta":
|
||||
if evt.Usage != nil {
|
||||
|
||||
@@ -323,10 +323,10 @@ func TestAnthropicChat_ChatStream_Text(t *testing.T) {
|
||||
ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
||||
WithAnthropicRecorder(func(cr CallRecord) { recs = append(recs, cr) }))
|
||||
|
||||
var deltas []string
|
||||
var events []StreamEvent
|
||||
resp, err := ad.ChatStream(context.Background(),
|
||||
Request{Messages: []Message{TextMessage(RoleUser, "hi")}, Model: "claude-test", Purpose: "chat"},
|
||||
func(d string) { deltas = append(deltas, d) })
|
||||
func(ev StreamEvent) { events = append(events, ev) })
|
||||
if err != nil {
|
||||
t.Fatalf("ChatStream error: %v", err)
|
||||
}
|
||||
@@ -348,8 +348,14 @@ func TestAnthropicChat_ChatStream_Text(t *testing.T) {
|
||||
if resp.TotalTokens != 7 {
|
||||
t.Errorf("resp.TotalTokens = %d, want 7", resp.TotalTokens)
|
||||
}
|
||||
if !reflect.DeepEqual(deltas, []string{"Hel", "lo"}) {
|
||||
t.Errorf("deltas = %v, want [Hel lo]", deltas)
|
||||
wantEvents := []StreamEvent{
|
||||
{Kind: StreamKindTextStart, Index: 0},
|
||||
{Kind: StreamKindTextDelta, Index: 0, Delta: "Hel"},
|
||||
{Kind: StreamKindTextDelta, Index: 0, Delta: "lo"},
|
||||
{Kind: StreamKindTextEnd, Index: 0},
|
||||
}
|
||||
if !reflect.DeepEqual(events, wantEvents) {
|
||||
t.Errorf("events = %+v, want %+v", events, wantEvents)
|
||||
}
|
||||
if len(recs) != 1 || recs[0].Tokens != 7 || recs[0].Purpose != "chat" {
|
||||
t.Errorf("records = %+v, want 1 record tokens=7 purpose=chat", recs)
|
||||
@@ -377,10 +383,10 @@ func TestAnthropicChat_ChatStream_ToolUseReassembly(t *testing.T) {
|
||||
srv := newAnthropicServer(t, http.StatusOK, "text/event-stream", sse, nil)
|
||||
ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"})
|
||||
|
||||
var deltas []string
|
||||
var events []StreamEvent
|
||||
resp, err := ad.ChatStream(context.Background(),
|
||||
Request{Messages: []Message{TextMessage(RoleUser, "hi")}, Model: "claude-test"},
|
||||
func(d string) { deltas = append(deltas, d) })
|
||||
func(ev StreamEvent) { events = append(events, ev) })
|
||||
if err != nil {
|
||||
t.Fatalf("ChatStream error: %v", err)
|
||||
}
|
||||
@@ -401,8 +407,14 @@ func TestAnthropicChat_ChatStream_ToolUseReassembly(t *testing.T) {
|
||||
if resp.TotalTokens != 18 {
|
||||
t.Errorf("resp.TotalTokens = %d, want 18", resp.TotalTokens)
|
||||
}
|
||||
if len(deltas) != 0 {
|
||||
t.Errorf("deltas = %v, want none (only tool_use)", deltas)
|
||||
wantEvents := []StreamEvent{
|
||||
{Kind: StreamKindToolCallStart, Index: 0, ID: "toolu_1"},
|
||||
{Kind: StreamKindToolCallDelta, Index: 0, ID: "toolu_1", Delta: `{"location":`},
|
||||
{Kind: StreamKindToolCallDelta, Index: 0, ID: "toolu_1", Delta: `"Paris"}`},
|
||||
{Kind: StreamKindToolCallEnd, Index: 0, ID: "toolu_1"},
|
||||
}
|
||||
if !reflect.DeepEqual(events, wantEvents) {
|
||||
t.Errorf("events = %+v, want %+v", events, wantEvents)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,10 +60,10 @@ func (c *Chain) Chat(ctx context.Context, req Request, opts Options) (Response,
|
||||
}
|
||||
|
||||
// ChatStream tries each step in order. If a step fails before delivering any
|
||||
// delta, the chain moves to the next step; once a step has delivered output it
|
||||
// event, the chain moves to the next step; once a step has delivered output it
|
||||
// is committed, so a mid-stream failure surfaces as an error rather than mixing
|
||||
// content across providers.
|
||||
func (c *Chain) ChatStream(ctx context.Context, req Request, onChunk func(delta string)) (Response, error) {
|
||||
func (c *Chain) ChatStream(ctx context.Context, req Request, onEvent func(StreamEvent)) (Response, error) {
|
||||
if len(c.steps) == 0 {
|
||||
return Response{}, ErrNoSteps
|
||||
}
|
||||
@@ -74,10 +74,10 @@ func (c *Chain) ChatStream(ctx context.Context, req Request, onChunk func(delta
|
||||
stepReq.Model = s.Model
|
||||
}
|
||||
delivered := false
|
||||
wrapped := func(delta string) {
|
||||
wrapped := func(ev StreamEvent) {
|
||||
delivered = true
|
||||
if onChunk != nil {
|
||||
onChunk(delta)
|
||||
if onEvent != nil {
|
||||
onEvent(ev)
|
||||
}
|
||||
}
|
||||
resp, err := s.Adapter.ChatStream(ctx, stepReq, wrapped)
|
||||
|
||||
@@ -194,9 +194,9 @@ func (a *OpenAIChatAdapter) Chat(ctx context.Context, req Request, opts Options)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ChatStream streams token-level deltas via onChunk and returns the aggregated
|
||||
// ChatStream streams structured events via onEvent and returns the aggregated
|
||||
// response with usage when the provider reports it in the final chunk.
|
||||
func (a *OpenAIChatAdapter) ChatStream(ctx context.Context, req Request, onChunk func(delta string)) (Response, error) {
|
||||
func (a *OpenAIChatAdapter) ChatStream(ctx context.Context, req Request, onEvent func(StreamEvent)) (Response, error) {
|
||||
start := time.Now()
|
||||
if a.baseURL == "" {
|
||||
return Response{}, ErrUpstream
|
||||
@@ -222,7 +222,7 @@ func (a *OpenAIChatAdapter) ChatStream(ctx context.Context, req Request, onChunk
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return Response{}, a.upstreamError(resp)
|
||||
}
|
||||
out, err := scanSSE(resp.Body, onChunk)
|
||||
out, err := scanSSE(resp.Body, onEvent)
|
||||
if err != nil {
|
||||
return Response{}, err
|
||||
}
|
||||
@@ -380,12 +380,14 @@ func responseFromWire(wire *openAIChatResponse) Response {
|
||||
|
||||
// scanSSE incrementally decodes stream chunks, accumulates the full content
|
||||
// with a strings.Builder (never reading the whole body at once) and captures
|
||||
// usage from the final chunk.
|
||||
func scanSSE(r io.Reader, onChunk func(string)) (Response, error) {
|
||||
// usage from the final chunk. Each text delta is delivered as a text_delta
|
||||
// StreamEvent with the body-text segment bracketed by text_start/text_end.
|
||||
func scanSSE(r io.Reader, onEvent func(StreamEvent)) (Response, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Buffer(make([]byte, 0, sseBufInit), maxSSELineBytes)
|
||||
agg := Response{}
|
||||
var b strings.Builder
|
||||
started := false
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || !strings.HasPrefix(line, sseDataPrefix) {
|
||||
@@ -412,13 +414,20 @@ func scanSSE(r io.Reader, onChunk func(string)) (Response, error) {
|
||||
continue
|
||||
}
|
||||
b.WriteString(delta)
|
||||
if onChunk != nil {
|
||||
onChunk(delta)
|
||||
if onEvent != nil {
|
||||
if !started {
|
||||
started = true
|
||||
onEvent(StreamEvent{Kind: StreamKindTextStart})
|
||||
}
|
||||
onEvent(StreamEvent{Kind: StreamKindTextDelta, Delta: delta})
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return Response{}, fmt.Errorf("model: %w: read stream: %v", ErrUpstream, err)
|
||||
}
|
||||
if onEvent != nil && started {
|
||||
onEvent(StreamEvent{Kind: StreamKindTextEnd})
|
||||
}
|
||||
agg.Content = b.String()
|
||||
agg.Message = Message{Role: RoleAssistant, Content: []Content{{Type: ContentTypeText, Text: agg.Content}}}
|
||||
return agg, nil
|
||||
|
||||
@@ -200,10 +200,10 @@ func TestOpenAIChat_ChatStream_SSE(t *testing.T) {
|
||||
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-test"},
|
||||
WithRecorder(func(cr CallRecord) { recs = append(recs, cr) }))
|
||||
|
||||
var deltas []string
|
||||
var events []StreamEvent
|
||||
resp, err := ad.ChatStream(context.Background(),
|
||||
Request{Messages: []Message{TextMessage(RoleUser, "hi")}, Model: "gpt-test", Purpose: "chat"},
|
||||
func(d string) { deltas = append(deltas, d) })
|
||||
func(ev StreamEvent) { events = append(events, ev) })
|
||||
if err != nil {
|
||||
t.Fatalf("ChatStream error: %v", err)
|
||||
}
|
||||
@@ -216,8 +216,14 @@ func TestOpenAIChat_ChatStream_SSE(t *testing.T) {
|
||||
if resp.TotalTokens != 7 {
|
||||
t.Errorf("resp.TotalTokens = %d, want 7", resp.TotalTokens)
|
||||
}
|
||||
if !reflect.DeepEqual(deltas, []string{"Hel", "lo"}) {
|
||||
t.Errorf("deltas = %v, want [Hel lo]", deltas)
|
||||
wantEvents := []StreamEvent{
|
||||
{Kind: StreamKindTextStart},
|
||||
{Kind: StreamKindTextDelta, Delta: "Hel"},
|
||||
{Kind: StreamKindTextDelta, Delta: "lo"},
|
||||
{Kind: StreamKindTextEnd},
|
||||
}
|
||||
if !reflect.DeepEqual(events, wantEvents) {
|
||||
t.Errorf("events = %+v, want %+v", events, wantEvents)
|
||||
}
|
||||
if len(recs) != 1 || recs[0].Tokens != 7 || recs[0].Purpose != "chat" {
|
||||
t.Errorf("records = %+v, want 1 record tokens=7 purpose=chat", recs)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package model
|
||||
|
||||
// StreamKind enumerates the kinds of structured events produced while a
|
||||
// completion streams. It mirrors OpenClaw's AssistantMessageEvent segments
|
||||
// (text / thinking / tool_call) plus a tool-execution progress event, so the
|
||||
// frontend can render each segment into its own surface (body, collapsed
|
||||
// thinking block, tool card) rather than accumulating a single opaque token
|
||||
// stream.
|
||||
type StreamKind string
|
||||
|
||||
const (
|
||||
// StreamKindTextStart opens the body-text segment. The single text segment
|
||||
// of a response is bracketed by TextStart/TextEnd; deltas fill it.
|
||||
StreamKindTextStart StreamKind = "text_start"
|
||||
// StreamKindTextDelta is one body-text token fragment.
|
||||
StreamKindTextDelta StreamKind = "text_delta"
|
||||
// StreamKindTextEnd closes the body-text segment.
|
||||
StreamKindTextEnd StreamKind = "text_end"
|
||||
|
||||
// StreamKindThinkingStart opens a chain-of-thought segment (frontend
|
||||
// collapsed block). Reserved: adapters that report reasoning emit it.
|
||||
StreamKindThinkingStart StreamKind = "thinking_start"
|
||||
// StreamKindThinkingDelta is one reasoning token fragment.
|
||||
StreamKindThinkingDelta StreamKind = "thinking_delta"
|
||||
// StreamKindThinkingEnd closes a reasoning segment.
|
||||
StreamKindThinkingEnd StreamKind = "thinking_end"
|
||||
|
||||
// StreamKindToolCallStart opens a tool invocation's streaming form.
|
||||
StreamKindToolCallStart StreamKind = "tool_call_start"
|
||||
// StreamKindToolCallDelta is a fragment of the tool arguments (e.g. the
|
||||
// incremental input_json of an Anthropic tool_use block).
|
||||
StreamKindToolCallDelta StreamKind = "tool_call_delta"
|
||||
// StreamKindToolCallEnd closes a tool invocation block.
|
||||
StreamKindToolCallEnd StreamKind = "tool_call_end"
|
||||
|
||||
// StreamKindToolExecution reports intermediate progress while the loop runs
|
||||
// a tool that the model requested (design/04 §1.1 tool_execution_update). It
|
||||
// is emitted by the tool loop, not the model adapter; reserved for a later
|
||||
// milestone, but the wire mapping is defined so the frontend can react.
|
||||
StreamKindToolExecution StreamKind = "tool_execution_update"
|
||||
)
|
||||
|
||||
// StreamEvent is a single structured event in a streamed completion. It replaces
|
||||
// the former plain text delta as the unit delivered by Adapter.ChatStream, so a
|
||||
// consumer can distinguish body text from tool calls (and, in time, reasoning
|
||||
// segments) and track each by Index/ID.
|
||||
type StreamEvent struct {
|
||||
// Kind classifies the event (see StreamKind).
|
||||
Kind StreamKind
|
||||
// Delta carries the incremental payload: body text for TextDelta, reasoning
|
||||
// text for ThinkingDelta, or a partial_argument fragment for ToolCallDelta.
|
||||
Delta string
|
||||
// Index is the zero-based content-block index the event belongs to, when the
|
||||
// provider reports one. The frontend uses it to correlate a segment's
|
||||
// start/delta/end events.
|
||||
Index int
|
||||
// ID is the provider block/index identifier for a tool call (e.g. Anthropic's
|
||||
// tool_use block id). Empty for text segments.
|
||||
ID string
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func (m *mockAdapter) Chat(context.Context, model.Request, model.Options) (model
|
||||
return m.resp, m.err
|
||||
}
|
||||
|
||||
func (m *mockAdapter) ChatStream(context.Context, model.Request, func(string)) (model.Response, error) {
|
||||
func (m *mockAdapter) ChatStream(context.Context, model.Request, func(model.StreamEvent)) (model.Response, error) {
|
||||
return m.resp, m.err
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user