feat(model): Top5#1 结构化流事件 + WS 事件体系(对齐 OpenClaw AssistantMessageEvent)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- 新增 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:
Aska committed 2026-09-06 16:31:28 +08:00
1 parent 54f0b75646
commit 24d5924cee
23 files changed
+425 -90

No files matched your search

+34
View File
@@ -803,6 +803,40 @@ func TestHubStreamChat(t *testing.T) {
}
}
func TestHubBroadcastStream(t *testing.T) {
t.Parallel()
hb := newTestHub(nil, nil)
c := newFakeConn(hb, 32)
hb.register(c)
// A text_delta StreamEvent on the OnStream→bus→relay path becomes a typed
// message.delta frame carrying the delta text and a monotonically increasing
// seq, leaving the legacy stream_chunk shape out of the way.
hb.broadcastStream(model.StreamEvent{Kind: model.StreamKindTextDelta, Delta: "Hel", Index: 0})
ev := readFakeFrame(t, c)
if ev.Type != "message.delta" {
t.Fatalf("frame type = %q, want message.delta (frame=%+v)", ev.Type, ev)
}
data, ok := ev.Data.(map[string]any)
if !ok || data["delta"] != "Hel" {
t.Fatalf("frame data = %+v, want delta=Hel", ev.Data)
}
if ev.Seq != 1 {
t.Fatalf("frame seq = %d, want 1", ev.Seq)
}
// A tool_call event maps to tool_call.*; a non-StreamEvent payload is dropped.
hb.broadcastStream(model.StreamEvent{Kind: model.StreamKindToolCallEnd, ID: "toolu_1", Index: 0})
ev = readFakeFrame(t, c)
if ev.Type != "tool_call.end" {
t.Fatalf("second frame type = %q, want tool_call.end", ev.Type)
}
hb.broadcastStream("not-a-stream-event")
if err := connHasNoFrame(c); err != nil {
t.Fatalf("unexpected frame from non-StreamEvent payload: %v", err)
}
}
func TestHubStreamChatIdle(t *testing.T) {
t.Parallel()
hb := newTestHub(nil, nil) // nil runner
+33
View File
@@ -4,6 +4,7 @@ import (
"strings"
"git.yeij.top/AskaEth/Cyrene_v2/internal/bus"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/orchestration"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/orchestration/comm"
)
@@ -79,6 +80,38 @@ func subagentProgressEvent(e bus.Event) (wsEvent, bool) {
}}, true
}
// streamEventData maps a streamed model.StreamKind to its downstream WS event
// type and builds a typed data payload. It is what turns the loop's structured
// events into the message.*/thinking.*/tool_call.*/tool_execution.* frames the
// frontend renders. Unknown kinds fall back to the legacy stream_chunk shape so
// an unrecognized event still reaches the client instead of being dropped.
func streamEventData(ev model.StreamEvent) (string, map[string]any) {
switch ev.Kind {
case model.StreamKindTextStart:
return "message.start", map[string]any{"index": ev.Index}
case model.StreamKindTextDelta:
return "message.delta", map[string]any{"delta": ev.Delta, "index": ev.Index}
case model.StreamKindTextEnd:
return "message.end", map[string]any{"index": ev.Index}
case model.StreamKindThinkingStart:
return "thinking.start", map[string]any{"index": ev.Index}
case model.StreamKindThinkingDelta:
return "thinking.delta", map[string]any{"delta": ev.Delta, "index": ev.Index}
case model.StreamKindThinkingEnd:
return "thinking.end", map[string]any{"index": ev.Index}
case model.StreamKindToolCallStart:
return "tool_call.start", map[string]any{"id": ev.ID, "index": ev.Index}
case model.StreamKindToolCallDelta:
return "tool_call.delta", map[string]any{"id": ev.ID, "index": ev.Index, "delta": ev.Delta}
case model.StreamKindToolCallEnd:
return "tool_call.end", map[string]any{"id": ev.ID, "index": ev.Index}
case model.StreamKindToolExecution:
return "tool_execution.update", map[string]any{"id": ev.ID, "index": ev.Index, "delta": ev.Delta}
default:
return "stream_chunk", map[string]any{"delta": ev.Delta}
}
}
// isBroadcastAgentEvent reports whether subject has the exact 4-token broadcast
// shape cyrene.agent.<from>.event, which is how the scheduler publishes progress.
// It deliberately does not match addressed events (cyrene.agent.<from>.<to>.event)
+61 -14
View File
@@ -2,11 +2,14 @@ package gateway
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"log/slog"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@@ -23,6 +26,7 @@ const (
wsEventsPattern = "ws.>"
wsPrefix = "ws."
wsChatSubject = "ws.chat"
wsStreamSubject = "ws.stream"
wsVoiceInput = "ws.voice_input"
wsReadLimit = 4096
wsWriteWait = 10 * time.Second
@@ -31,10 +35,14 @@ const (
wsSendBuffer = 32
)
// wsEvent is the outbound (server→client) frame.
// wsEvent is the outbound (server→client) frame. TurnID/Seq are optional and
// used by the streaming path so a client can correlate a burst of streamed
// events to one assistant turn and keep them in order.
type wsEvent struct {
Type string `json:"type"`
Data any `json:"data,omitempty"`
Type string `json:"type"`
TurnID string `json:"turnId,omitempty"`
Seq int `json:"seq,omitempty"`
Data any `json:"data,omitempty"`
}
// wsInbound is the client→server frame. Data carries the per-type payload.
@@ -70,6 +78,7 @@ type hub struct {
origins map[string]bool
allowAll bool
cancel context.CancelFunc
streamSeq atomic.Uint64
}
// newHub builds a hub bound to the server's dependencies.
@@ -178,6 +187,12 @@ func (h *hub) relay(ctx context.Context, e bus.Event) error {
go h.streamChat(ctx, e.Payload)
return nil
}
if e.Subject == wsStreamSubject {
// OnStream→bus→relay path: the loop already published a structured
// model.StreamEvent; map it to the typed downstream event.
h.broadcastStream(e.Payload)
return nil
}
ev, ok := normalizeEvent(e)
if !ok {
return nil
@@ -186,16 +201,20 @@ func (h *hub) relay(ctx context.Context, e bus.Event) error {
return nil
}
// streamChat runs one agent turn for a chat trigger and broadcasts the result as
// stream_chunk/stream_end. It runs on its own goroutine so a slow model call does
// not stall the bus subscription worker.
// streamChat runs one agent turn for a chat trigger and broadcasts the turn
// terminus (stream_end) with a fresh turnId; on the non-streaming fallback path
// it also emits a single stream_chunk. When the loop is streaming, the per-event
// deltas arrive via the OnStream→bus→relay path as structured message.*/
// thinking.*/tool_call.* events. It runs on its own goroutine so a slow model
// call does not stall the bus subscription worker.
func (h *hub) streamChat(ctx context.Context, payload any) {
var req chatStreamRequest
if b, err := json.Marshal(payload); err == nil {
_ = json.Unmarshal(b, &req)
}
turnID := newTurnID()
if h.runner == nil || strings.TrimSpace(req.Text) == "" {
h.broadcast(wsEvent{Type: "stream_end", Data: map[string]any{"stopReason": "idle"}})
h.broadcast(wsEvent{Type: "stream_end", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), Data: map[string]any{"stopReason": "idle"}})
return
}
if req.SessionID == "" {
@@ -204,17 +223,33 @@ func (h *hub) streamChat(ctx context.Context, payload any) {
res, err := h.runner.Run(ctx, req.SessionID, []model.Message{model.TextMessage(model.RoleUser, req.Text)})
if err != nil {
h.log.Error("ws chat run", "err", err, "session", req.SessionID)
h.broadcast(wsEvent{Type: "stream_end", Data: map[string]any{"stopReason": "error", "error": err.Error()}})
h.broadcast(wsEvent{Type: "stream_end", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), Data: map[string]any{"stopReason": "error", "error": err.Error()}})
return
}
// When the loop is streaming (OnStream wired), the deltas already fan out as
// ws.stream_chunk through the OnStream→bus→relay path; a second whole-text
// chunk would duplicate the tail. The non-streaming path (or a stub runner)
// still emits one chunk so the stream contract holds in tests.
// When the loop is streaming (OnStream wired), the structured deltas already
// fan out as message.*/thinking.*/tool_call.* events through the
// OnStream→bus→relay path; a second whole-text chunk would duplicate the
// tail. The non-streaming path (or a stub runner) still emits one chunk so
// the stream contract holds in tests.
if !h.streaming {
h.broadcast(wsEvent{Type: "stream_chunk", Data: map[string]any{"delta": res.Text}})
h.broadcast(wsEvent{Type: "stream_chunk", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), Data: map[string]any{"delta": res.Text}})
}
h.broadcast(wsEvent{Type: "stream_end", Data: map[string]any{"stopReason": res.StopReason, "steps": res.Steps}})
h.broadcast(wsEvent{Type: "stream_end", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), Data: map[string]any{"stopReason": res.StopReason, "steps": res.Steps}})
}
// broadcastStream maps a streamed model.StreamEvent to its downstream WS event
// type and fans it out to every client. It runs on the bus subscription worker;
// seq is assigned atomically so a burst stays ordered even if the loop emits
// events from more than one goroutine. TurnId is left empty here because the
// OnStream bus payload does not carry the turn identity (future work would
// thread a turn id through loop → streamChunk → payload).
func (h *hub) broadcastStream(payload any) {
ev, ok := payload.(model.StreamEvent)
if !ok {
return
}
typ, data := streamEventData(ev)
h.broadcast(wsEvent{Type: typ, Seq: int(h.streamSeq.Add(1)), Data: data})
}
// chatStreamRequest is the decoded ws.chat payload.
@@ -372,6 +407,18 @@ func eventType(subject string) string {
return strings.TrimPrefix(subject, wsPrefix)
}
// newTurnID returns a fresh random hex identifier used to correlate a burst of
// streamed WS events to one assistant turn. It burns the crypto/rand source and
// falls back to a nanosecond timestamp if the source is unavailable, so a id is
// always produced (a blank turnId would break client correlation silently).
func newTurnID() string {
var b [8]byte
if _, err := rand.Read(b[:]); err == nil {
return "turn-" + hex.EncodeToString(b[:])
}
return "turn-" + time.Now().UTC().Format("20060102150405.000000000")
}
// rawData converts a possibly-empty raw payload into a value safe to publish.
func rawData(d json.RawMessage) any {
if len(d) == 0 {