cbd130ecae
dispatcher 不再手搓 pool.Stream,改用编译好的 Eino 图驱动;接入用户常驻画像,
推理前召回并注入 system prompt,实现个性化(架构'心脏'首次真跳)。
Eino 图(dispatcher/internal/eino): START→recall→prompt→model→END + 全局 State
- recall(Lambda): 取 Meta[user_id] → 调 MCP memory_get → ProcessState 写画像
- prompt(ChatTemplate): {profile} 注入 system,{query} 作 user
- model: poolModel 适配 LLM Pool 为 model.BaseChatModel(Generate+Stream, schema.Pipe)
- 写回: 流排空后异步 memorize(流式节点走 OnEndWithStreamOutput 非 OnEndFn)
记忆存储(mcp-go owns): GORM Profile→sundynix_user_profile(复合主键, AutoMigrate,
遵守前缀约定), 新工具 memory_get/memory_upsert, 连不上降级
Gateway: SubmitTask 注入 Meta[user_id](X-User-ID 头), PUT /api/v1/memory→memory_upsert
shared: contract.MetaUserID; llm.Pool 拆出 StreamText
验证: 4 模块 build✓ + 3 e2e PASS; live 跑通——PUT 偏好落 sundynix_user_profile,
带 X-User-ID 提交→Eino recall 召回→注入→SSE 流出含画像的个性化回答, writeback 触发
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
72 lines
3.2 KiB
Go
72 lines
3.2 KiB
Go
// Package contract 是 Gateway / Dispatcher / MCP 之间的共享契约:
|
|
// Task 数据结构与 NATS subject 命名约定。
|
|
package contract
|
|
|
|
import "encoding/json"
|
|
|
|
// NATS subject / stream 约定(与 README、各服务 config 保持一致)。
|
|
const (
|
|
StreamTasks = "SUNDYNIX_TASKS" // JetStream stream 名
|
|
SubjectTasks = "sundynix.tasks" // 任务发布主题前缀;实际为 sundynix.tasks.<id>
|
|
SubjectTasksAll = "sundynix.tasks.>" // stream 捕获的通配
|
|
SubjectStream = "sundynix.streams" // Token 回流前缀;实际 sundynix.streams.<id>
|
|
ConsumerDurable = "dispatchers" // Dispatcher 持久消费者(队列组负载均衡)
|
|
|
|
// HeaderStreamEnd 是 Token 流的结束信号(core NATS 消息头)。
|
|
// 置为 "1" 的消息体为空,表示该 task 的 Token 流结束。
|
|
HeaderStreamEnd = "X-Stream-End"
|
|
|
|
// MCP 工具调用约定(第 4 层 Dispatcher → 第 5 层 MCP Tools)。
|
|
// 用 core NATS request-reply:同步拿结果,队列组内负载均衡。
|
|
SubjectToolsGo = "sundynix.tools.go" // Go I/O 型工具前缀;实际 sundynix.tools.go.<tool>
|
|
SubjectToolsGoAll = "sundynix.tools.go.>" // mcp-go 通配订阅
|
|
SubjectToolsPy = "sundynix.tools.py" // Python 算法型工具前缀;实际 sundynix.tools.py.<tool>
|
|
SubjectToolsPyAll = "sundynix.tools.py.>" // mcp-py 通配订阅
|
|
QueueToolsGo = "mcp-go-workers" // mcp-go 队列组(多副本负载均衡)
|
|
QueueToolsPy = "mcp-py-workers" // mcp-py 队列组
|
|
|
|
// MetaUserID 是 Task.Meta 中承载已登录用户标识的键(用于偏好记忆召回)。
|
|
MetaUserID = "user_id"
|
|
)
|
|
|
|
// Task 是 DSL 解析组装后的可调度任务,在 NATS 上以 JSON 传输。
|
|
type Task struct {
|
|
ID string `json:"id"`
|
|
Graph json.RawMessage `json:"graph"` // React Flow 导出的 Agent 编排图
|
|
Meta map[string]any `json:"meta,omitempty"`
|
|
}
|
|
|
|
// TaskSubject 返回某任务的发布主题。
|
|
func TaskSubject(id string) string { return SubjectTasks + "." + id }
|
|
|
|
// StreamSubject 返回某任务的 Token 回流主题。
|
|
func StreamSubject(id string) string { return SubjectStream + "." + id }
|
|
|
|
// ToolSubjectGo / ToolSubjectPy 返回某工具的调用主题。
|
|
func ToolSubjectGo(tool string) string { return SubjectToolsGo + "." + tool }
|
|
func ToolSubjectPy(tool string) string { return SubjectToolsPy + "." + tool }
|
|
|
|
// ToolCall 是 Dispatcher 对一个 MCP 工具的调用请求(NATS request 体)。
|
|
type ToolCall struct {
|
|
Tool string `json:"tool"` // 工具名,如 wiki_search
|
|
Args map[string]any `json:"args,omitempty"` // 工具参数
|
|
TaskID string `json:"task_id,omitempty"` // 触发该调用的任务(便于追踪)
|
|
}
|
|
|
|
// ToolResult 是 MCP 工具的应答(NATS reply 体)。
|
|
type ToolResult struct {
|
|
OK bool `json:"ok"`
|
|
Content string `json:"content,omitempty"` // 工具产出(如检索结果文本)
|
|
Error string `json:"error,omitempty"` // 非空表示工具内部出错
|
|
}
|
|
|
|
// Marshal / Unmarshal 便捷方法。
|
|
func (t *Task) Marshal() ([]byte, error) { return json.Marshal(t) }
|
|
func Unmarshal(b []byte) (*Task, error) {
|
|
var t Task
|
|
if err := json.Unmarshal(b, &t); err != nil {
|
|
return nil, err
|
|
}
|
|
return &t, nil
|
|
}
|