Files
Cyrene/backend/ai-core/internal/model/sub_session.go
T
AskaEth 87214b9441 feat: Phase 1+2 架构进化 — 连续思考链/主动消息决策/情感状态机/离线自主思考 (86文件)
Phase 1 (基础设施):
- ThinkChain 思考链连续性 + 差异化思考提示词 (persistent)
- AutonomousToolPolicy 工具安全策略 (safe/unsafe/conditional)
- MessageScheduler 自适应消息节奏 (Idle/Available/Busy)
- SessionEnrichmentStore 渐进式上下文丰富 (5层)
- ConversationBus 事件总线 + ResponseCache (dedup)
- pkg/logger 统一日志 + 所有 handler 替换 fmt.Printf
- NPE 守卫/链路优化/数据库表修复/Go workspace

Phase 2 (人格交互):
- EmotionState/EmotionTracker 情感状态机 (5种心情, 情绪衰减)
- ProactiveGuard 主动消息多维决策 (静默时段/紧急度/频率/校验)
- Gateway↔ai-core 在线状态感知链路 (presence notification)
- 离线思考频率控制 + 重连问候 + 离线消息排队

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 15:25:12 +08:00

165 lines
6.5 KiB
Go

package model
import "time"
// SubSessionType 子会话类型
type SubSessionType string
const (
SubSessionMemory SubSessionType = "memory" // 记忆检索子会话
SubSessionIoT SubSessionType = "iot" // IoT 控制子会话
SubSessionGeneral SubSessionType = "general" // 通用对话子会话
SubSessionKnowledge SubSessionType = "knowledge" // 知识库查询子会话 (预留)
SubSessionWebSearch SubSessionType = "web_search" // 网络搜索子会话 (预留)
SubSessionReview SubSessionType = "review" // 最终审查子会话
)
// SubSessionStatus 子会话状态
type SubSessionStatus string
const (
SubSessionPending SubSessionStatus = "pending"
SubSessionRunning SubSessionStatus = "running"
SubSessionCompleted SubSessionStatus = "completed"
SubSessionFailed SubSessionStatus = "failed"
SubSessionTimeout SubSessionStatus = "timeout"
)
// SubSession 子会话 — 内部处理单元
type SubSession struct {
ID string `json:"id"`
ParentID string `json:"parent_id"` // 主会话 ID
Type SubSessionType `json:"type"`
Status SubSessionStatus `json:"status"`
SystemPrompt string `json:"system_prompt"` // 该子会话专用的系统提示词
Context []LLMMessage `json:"-"` // LLM 上下文 (内存中)
Result *SubSessionResult `json:"result,omitempty"`
CreatedAt time.Time `json:"created_at"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
Error string `json:"error,omitempty"`
}
// SubSessionResult 子会话处理结果
type SubSessionResult struct {
Type SubSessionType `json:"type"` // 子会话类型
Summary string `json:"summary"` // 结果摘要 (供主会话参考)
Details string `json:"details"` // 详细信息
ToolCalls []ToolCallRecord `json:"tool_calls"` // 工具调用记录
Memories []MemorySnippet `json:"memories"` // 检索到的记忆片段
Confidence float64 `json:"confidence"` // 置信度 0-1
Progress float64 `json:"progress"` // 执行进度 0.0 ~ 1.0
Error string `json:"error,omitempty"`
Metadata map[string]any `json:"metadata"` // 类型特定的元数据
}
// ToolCallRecord 工具调用记录
type ToolCallRecord struct {
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
Result any `json:"result"`
}
// MemorySnippet 记忆片段 (供子会话返回)
type MemorySnippet struct {
ID string `json:"id"`
Content string `json:"content"`
Category string `json:"category"`
Importance int `json:"importance"`
Relevance float64 `json:"relevance"` // 与当前查询的相关度
}
// IntentResult 意图分析结果
type IntentResult struct {
Primary string `json:"primary"` // 主要意图
SubIntents []string `json:"sub_intents"` // 次要意图
Entities map[string]string `json:"entities"` // 实体提取
NeedsIoT bool `json:"needs_iot"` // 是否需要 IoT 控制
NeedsMemory bool `json:"needs_memory"` // 是否需要深度记忆检索
NeedsKnowledge bool `json:"needs_knowledge"` // 是否需要知识库查询
Urgency string `json:"urgency"` // 紧急程度: low/medium/high
Sentiment string `json:"sentiment"` // 情感: positive/neutral/negative
}
// MainSessionStatus 主会话状态
type MainSessionStatus string
const (
MainSessionIdle MainSessionStatus = "idle"
MainSessionThinking MainSessionStatus = "thinking"
MainSessionStreaming MainSessionStatus = "streaming"
)
// MultiMessage 多条消息的容器 (用于单次发送多条短消息)
type MultiMessage struct {
Messages []MultiMessageItem `json:"messages"`
}
// MultiMessageItem 多消息中的单条
type MultiMessageItem struct {
Index int `json:"index"`
Content string `json:"content"`
}
// StreamEvent 流式事件
type StreamEvent struct {
Type StreamEventType `json:"type"` // delta, segments, done, error, review, thinking, tool_progress, system_info
Delta string `json:"delta,omitempty"` // 逐 token delta
Segments []Segment `json:"segments,omitempty"` // 断句片段
ReviewMessages []ReviewMessage `json:"review_messages,omitempty"` // 审查后的带类型消息
ThinkingContent string `json:"thinking_content,omitempty"` // 思考内容
ToolProgress *ToolProgressInfo `json:"tool_progress,omitempty"` // 工具进度
SystemInfo *SystemInfoPayload `json:"system_info,omitempty"` // 系统信息
ProtocolVersion int `json:"protocol_version,omitempty"` // 协议版本
Error error `json:"-"` // 内部错误
}
// ToolProgressInfo 工具执行进度
type ToolProgressInfo struct {
ToolName string `json:"tool_name"`
Status string `json:"status"` // started, running, completed, failed
Progress float64 `json:"progress"`
Message string `json:"message"`
}
// SystemInfoPayload 系统信息负载
type SystemInfoPayload struct {
Level string `json:"level"` // info, warning, error
Message string `json:"message"`
Action string `json:"action,omitempty"`
}
// StreamEventType 流式事件类型
type StreamEventType string
const (
StreamDelta StreamEventType = "delta"
StreamSegments StreamEventType = "segments"
StreamDone StreamEventType = "done"
StreamError StreamEventType = "error"
StreamReview StreamEventType = "review" // 审查后的带类型消息
StreamThinking StreamEventType = "thinking" // 思考内容
StreamToolProgress StreamEventType = "tool_progress" // 工具执行进度
StreamSystemInfo StreamEventType = "system_info" // 系统通知
)
// ReviewMessageType 审查消息类型
type ReviewMessageType string
const (
ReviewMessageAction ReviewMessageType = "action" // 动作消息 (括号内容)
ReviewMessageChat ReviewMessageType = "chat" // 聊天消息 (引号/普通内容)
)
// ReviewMessage 审查后的消息
type ReviewMessage struct {
Type ReviewMessageType `json:"type"`
Content string `json:"content"`
DelayMs int `json:"delay_ms,omitempty"` // ms to wait before sending (0 = immediate)
}
// Segment 语音片段
type Segment struct {
Index int `json:"index"`
Text string `json:"text"`
}