按 DevDocs/design/22 批次 1 后端(§2/§3/§4.1-4.5/§4.7/§5 验收 1-6)。
- 迁移 0011:chat_sessions.persona_id + chat_messages.persona_id(均 DEFAULT '',无回填 → 升级零风险);同步包内幂等 DDL;**真实库升级验证**(拷贝 data/cyrene.db 到临时目录跑 MigrateUp:既有 1 会话/109 消息 persona 仍为 '',原库未被触碰)
- history:SessionMeta/Message 加 PersonaID + SetSessionPersona;ListSessions/List/scan/Append 带上
- runtime:SessionEnv.PersonaID + loop.personaFor(ctx) 替换固定 l.personaID(memory/request/turn)+ **agent 池**(Options.Agents,按角色取 instructions,未命中回落)+ **提取器不再把 persona 烤死在构造期**(多角色下记忆不再永远记在 cyrene 名下)+ **thinking 旋钮改为按会话角色解析**(否则角色 Y 会用角色 X 的预算/展示)
- gateway:会话 DTO/POST/PATCH 支持 personaId;ws/rest/voice 的 OwnerEnv 硬编改为**按会话解析角色**(actor 元数据语义不变);admin memories 搜索去掉硬编 cyrene
- Docs/api-client v0.42;**两处有意的行为收紧**已写明:POST /sessions body 非法 JSON → 400(原先完全忽略 body;空/null/{} 仍 201);POST /admin/memories/search 省略 personaId 且无默认角色 → 400(原先默认查 cyrene)
- 测试:design/22 §5 验收 1-6(记忆隔离/提示词首条 system/情绪按角色/提取 persona 归属/迁移兼容)+ 进程内 e2e(断言 adapter 收到的 model.Request.Messages[0] 即该角色的 systemPrompt)
- **已知未做(留批次 2 / 待确认)**:① IM 与渠道级未做 → **§7-3「owner 私聊镜像随主会话角色」尚未生效**;② 请求级覆盖未实现(本批只做会话级);③ **§7-5 的「上下文发送角色标记」只完成存储+DTO 半边**——消息 persona 已落库且 DTO 已带,但模型上下文不从 chat_messages 重建,「第 N 条是谁说的」尚未进上下文(需确认是否属批次 2);④「切角色重建摘要」当前是空操作(压缩未接线);⑤ thinking 旋钮仅在流式路径生效(非流式 agent.RunStep 不带,A 批次既有性质)
- 验收:go build/vet/test 全绿 + linux/amd64 与 darwin/arm64 交叉编译(既有 flake 本轮未触发;同批 session_orchestrator.go 一度 501 行 → 抽出 session_tail.go)
180 lines
7.9 KiB
Go
180 lines
7.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/bus"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/loop"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
|
)
|
|
|
|
// relay is the bus handler for the ws.> pattern and the producer bridge
|
|
// patterns. The ws.chat subject is a streaming trigger (run the loop and emit
|
|
// chunks); a ws.* subject becomes an outbound event of its design type; a
|
|
// bridged producer subject (device_update, task output/done, automation result,
|
|
// reminder, subagent progress) is normalized into the matching ws.* event type.
|
|
// Events with no WS-visible mapping are dropped so internal agent-to-agent
|
|
// traffic never leaks to clients.
|
|
func (h *hub) relay(ctx context.Context, e bus.Event) error {
|
|
if e.Subject == wsChatSubject {
|
|
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
|
|
}
|
|
h.broadcast(ev)
|
|
return nil
|
|
}
|
|
|
|
// streamChat is the ws.chat trigger entry point. It decodes the request, and
|
|
// routes the turn through the session orchestrator when one is wired (the
|
|
// non-blocking busy/pending/resume path of design/15); otherwise it falls back to
|
|
// the legacy direct run so a hub built without an orchestrator keeps the exact
|
|
// stream_chunk/stream_end contract. 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)
|
|
}
|
|
if h.runner == nil || (strings.TrimSpace(req.Text) == "" && strings.TrimSpace(req.Image) == "") {
|
|
h.broadcast(wsEvent{Type: "stream_end", TurnID: newTurnID(), Seq: int(h.streamSeq.Add(1)), SessionID: req.SessionID, Data: map[string]any{"stopReason": "idle"}})
|
|
return
|
|
}
|
|
if req.SessionID == "" {
|
|
req.SessionID = "web"
|
|
}
|
|
// The Web main session is the owner's private conversation (JWT = owner), so
|
|
// it binds the full-access env: memory read+write, unchanged behavior. The role
|
|
// comes from the session's binding (design/22 §4.5): a session switched to role Y
|
|
// answers as Y on the next turn, while an unbound/existing session keeps
|
|
// resolving to the global default.
|
|
msgs := userChatMessages(req.Text, req.Image)
|
|
// Thread the client's deviceID and behavior mode into the orchestrator via ctx
|
|
// so both the persisted user turn and the assistant reply are tagged
|
|
// (design/16 §4.5, §5.1).
|
|
ctx = withMode(withDeviceID(ctx, req.DeviceID), req.Mode)
|
|
env := h.envResolver.owner(ctx, req.SessionID)
|
|
if h.orchestrator != nil {
|
|
_ = h.orchestrator.Submit(ctx, req.SessionID, msgs, env)
|
|
return
|
|
}
|
|
_, _ = h.runTurn(ctx, req.SessionID, msgs, env)
|
|
}
|
|
|
|
// runTurn executes one agent turn and broadcasts its output stream. It is the
|
|
// unit both the orchestrator (via its injected TurnRunner) and the legacy
|
|
// fallback path share, so a turn always emits the same stream_chunk/stream_end
|
|
// events regardless of who drives it. 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, so only the non-streaming path
|
|
// emits a single whole-text chunk to hold the stream contract.
|
|
func (h *hub) runTurn(ctx context.Context, sessionID string, msgs []model.Message, env loop.SessionEnv) (*loop.Result, error) {
|
|
turnID := newTurnID()
|
|
// Thread the turn identity into the run context so the loop stamps it on every
|
|
// streamed event it emits (message.*/thinking.*/tool_call.*); the hub then puts
|
|
// it on the outbound frame. Every turn takes a fresh id — a session has at most
|
|
// one in-flight turn — so a client can correlate one burst of deltas to one
|
|
// assistant turn (design/16 §4.2).
|
|
ctx = loop.WithTurnID(ctx, turnID)
|
|
if h.runner == nil || len(msgs) == 0 {
|
|
h.broadcast(wsEvent{Type: "stream_end", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), SessionID: sessionID, Data: map[string]any{"stopReason": "idle"}})
|
|
return &loop.Result{StopReason: "idle"}, nil
|
|
}
|
|
res, err := h.runner.Run(loop.WithSessionEnv(ctx, env), sessionID, msgs)
|
|
if err != nil {
|
|
h.log.Error("chat run", "err", err, "session", sessionID)
|
|
h.broadcast(wsEvent{Type: "stream_end", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), SessionID: sessionID, Data: map[string]any{"stopReason": "error", "error": err.Error()}})
|
|
return nil, err
|
|
}
|
|
if !h.streaming {
|
|
h.broadcast(wsEvent{Type: "stream_chunk", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), SessionID: sessionID, Data: map[string]any{"delta": res.Text}})
|
|
}
|
|
h.broadcast(wsEvent{Type: "stream_end", TurnID: turnID, Seq: int(h.streamSeq.Add(1)), SessionID: sessionID, Data: map[string]any{"stopReason": res.StopReason, "steps": res.Steps}})
|
|
return res, nil
|
|
}
|
|
|
|
// handleInterrupt routes a client "interrupt" frame (design/15 §3) to the
|
|
// orchestrator, canceling the in-flight turn and clearing the pending queue for
|
|
// the requested session. It is a no-op when no orchestrator is wired.
|
|
func (h *hub) handleInterrupt(payload json.RawMessage) {
|
|
if h.orchestrator == nil {
|
|
return
|
|
}
|
|
var req struct {
|
|
SessionID string `json:"sessionId"`
|
|
}
|
|
_ = json.Unmarshal(payload, &req)
|
|
if req.SessionID == "" {
|
|
return
|
|
}
|
|
h.orchestrator.Interrupt(req.SessionID)
|
|
}
|
|
|
|
// 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. SessionID/TurnID ride on the event itself
|
|
// (the loop stamps them at the turn boundary from the run context), so the frame
|
|
// carries the scope a client needs to route it to the active session and to one
|
|
// assistant turn.
|
|
func (h *hub) broadcastStream(payload any) {
|
|
ev, ok := payload.(model.StreamEvent)
|
|
if !ok {
|
|
return
|
|
}
|
|
typ, data := streamEventData(ev)
|
|
h.broadcast(wsEvent{Type: typ, SessionID: ev.SessionID, TurnID: ev.TurnID, Seq: int(h.streamSeq.Add(1)), Data: data})
|
|
}
|
|
|
|
// chatStreamRequest is the decoded ws.chat payload.
|
|
type chatStreamRequest struct {
|
|
SessionID string `json:"sessionId"`
|
|
Text string `json:"text"`
|
|
Image string `json:"image,omitempty"`
|
|
// DeviceID is the client-produced identifier of the sending browser/device
|
|
// (design/16 §4.5), e.g. "web:chrome:a1b2c3"; it is optional and persisted
|
|
// alongside the message so the agent knows which end the owner is chatting from.
|
|
DeviceID string `json:"deviceId,omitempty"`
|
|
// Mode is the behavior mode the client sent the turn in (design/01 §3,
|
|
// design/16 §5.1); it is optional and persisted per message, falling back to
|
|
// "chat" when omitted.
|
|
Mode string `json:"mode,omitempty"`
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
// publishChatStream publishes a ws.chat streaming trigger (REST stream mode).
|
|
// deviceID is the sending browser/device id and mode the behavior mode the client
|
|
// sent the turn in, both carried on the payload so the ws.chat consumer can tag
|
|
// the persisted turn (design/16 §4.5, §5.1).
|
|
func (s *Server) publishChatStream(ctx context.Context, sessionID, text, image, deviceID, mode string) {
|
|
if s.bus == nil {
|
|
return
|
|
}
|
|
_ = s.bus.Publish(ctx, wsChatSubject, map[string]any{"sessionId": sessionID, "text": text, "image": image, "deviceId": deviceID, "mode": mode})
|
|
}
|