Documentation
¶
Index ¶
- Constants
- func FriendlyError(err error) string
- type Agent
- func (a *Agent) Cancel(sessionID string)
- func (a *Agent) Compress(ctx context.Context, opts *RunOptions, runningTokens map[string]int) error
- func (a *Agent) InitRunningTokens(messages []map[string]any) map[string]int
- func (a *Agent) IsRunning(sessionID string) bool
- func (a *Agent) RegisterCancel(sessionID string, cancel context.CancelFunc)
- func (a *Agent) Run(ctx context.Context, opts RunOptions) (AgentLoopResult, error)
- func (a *Agent) RunSubAgent(ctx context.Context, parentOpts RunOptions, req mcp_tools.SubAgentRequest) (string, error)
- func (a *Agent) SetSearchService(s *search.Service)
- func (a *Agent) UnregisterCancel(sessionID string)
- type AgentEvent
- type AgentEventType
- type AgentLoopResult
- type CancelManager
- type InterruptError
- type LoopInterrupt
- type MaxTurnsInterrupt
- type RunOptions
- type ToolFailureInterrupt
Constants ¶
const ( CancelPrefixChat = "chat:" CancelPrefixStyle = "style:" CancelPrefixPattern = "pattern:" )
不同领域的取消键前缀,防止 key 冲突。
Variables ¶
This section is empty.
Functions ¶
func FriendlyError ¶
FriendlyError 将 LLM 错误转换为用户友好的消息。 原始 error 应由调用方另行记录日志。
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent 是对话编排核心,持有运行所需的所有基础设施。
func New ¶
func New(llmClient *llm.Client, registry *mcp_tools.Registry, session *session.Store, chapterStore *chapter.Store, db *gorm.DB, approver approval.Approver, logger *slog.Logger, skillStore *skill.Store, cancelMgr *CancelManager) *Agent
New 创建 Agent 实例。
func (*Agent) Compress ¶
Compress 执行主 Agent 上下文压缩:调 LLM 生成摘要,重建 SkillCatalog/NovelState,保留近期关键消息。 opts 不会被修改,成功后才赋值新的 Messages / ActiveVersion / runningTokens。
func (*Agent) InitRunningTokens ¶
InitRunningTokens 对初始消息列表逐条 token 计数,返回按 role 的分组统计。
func (*Agent) RegisterCancel ¶
func (a *Agent) RegisterCancel(sessionID string, cancel context.CancelFunc)
RegisterCancel 注册一个可取消的对话。
func (*Agent) Run ¶
func (a *Agent) Run(ctx context.Context, opts RunOptions) (AgentLoopResult, error)
Run 执行 Agent 循环,返回最终文本和轮数。
func (*Agent) RunSubAgent ¶
func (a *Agent) RunSubAgent(ctx context.Context, parentOpts RunOptions, req mcp_tools.SubAgentRequest) (string, error)
RunSubAgent 启动子 Agent 并返回最终报告文本。
func (*Agent) SetSearchService ¶
SetSearchService 设置搜索服务,在搜索服务初始化完成后由 App 调用。
func (*Agent) UnregisterCancel ¶
UnregisterCancel 对话结束后清理,只删不 cancel。
type AgentEvent ¶
type AgentEvent struct {
TurnID int `json:"turn_id"`
SubTaskID string `json:"sub_task_id,omitempty"` // 子 Agent 事件路由 ID(对应 run_subagent 的 tool call ID)
Seq int `json:"seq,omitempty"` // turn 内事件序号,前端用于纠正 dev 模式 WebSocket 乱序
Type AgentEventType `json:"type"`
Data string `json:"data,omitempty"` // thinking / content 文本 chunk
ToolName string `json:"tool_name,omitempty"` // EventToolCall 时
ToolID string `json:"tool_id,omitempty"` // EventToolCall 时
Phase string `json:"phase,omitempty"` // "selected" | "executing" | "awaiting_approval" | "completed" | "failed" | "cancelled" | "loop_detected"
ToolArgs map[string]any `json:"tool_args,omitempty"` // 工具参数快照
Success bool `json:"success,omitempty"` // 工具执行结果摘要
ErrMsg string `json:"error,omitempty"` // 失败时的错误信息
DisplayText string `json:"display_text,omitempty"` // buildDisplay 产出的展示文本
ActivityKind string `json:"activity_kind,omitempty"` // 展示类别
Metadata map[string]any `json:"metadata,omitempty"` // buildDisplay 产出的附加信息(如 sub_agent_type)
Usage map[string]any `json:"usage,omitempty"` // token 用量详情(含 usage_ratio / detail)
CompressionPhase string `json:"compression_phase,omitempty"` // "compressing" | "done"
Summary string `json:"summary,omitempty"` // 压缩摘要文本
Attempt int `json:"attempt,omitempty"` // EventRetrying 时:第几次重试(1-indexed)
MaxRetries int `json:"max_retries,omitempty"` // EventRetrying 时:最大重试次数
BackoffMs int64 `json:"backoff_ms,omitempty"` // EventRetrying 时:本次退避毫秒数
ClearFromSeq int `json:"clear_from_seq,omitempty"` // EventRetrying 时:本轮 streamLoop 起点事件 seq(前端据此清空本轮 partial segments)
Timestamp time.Time `json:"timestamp"` // 事件生成时间
}
AgentEvent 是 loop 推送给前端的单次事件,对标 Python WebSocket push dict。 仅包含前端展示所需字段,不引用任何内部类型。
type AgentEventType ¶
type AgentEventType int
AgentEventType 定义推送给前端的事件类型。
const ( EventThinking AgentEventType = iota // DeepSeek reasoning_content 流式文本 EventThinkingDone // 思考阶段结束 EventContent // 正文流式文本 EventToolCall // 工具调用状态变化 EventUsage // 每次 LLM 调用的 token 用量 EventError // 不可恢复错误 EventRetrying // 可恢复错误重试中(agent 层自动重试 LLM 调用) EventCompression // 上下文压缩状态变化 )
type AgentLoopResult ¶
AgentLoopResult 是 Run() 的返回值。
type CancelManager ¶
type CancelManager struct {
// contains filtered or unexported fields
}
CancelManager 管理可取消的操作,通过 key 注册和取消。
func NewCancelManager ¶
func NewCancelManager() *CancelManager
NewCancelManager 创建一个新的 CancelManager。
func (*CancelManager) Cancel ¶
func (m *CancelManager) Cancel(key string)
Cancel 取消并清理指定 key 的操作。如果 key 不存在则无操作。
func (*CancelManager) IsRegistered ¶
func (m *CancelManager) IsRegistered(key string) bool
IsRegistered 检查指定 key 是否已注册。
func (*CancelManager) Register ¶
func (m *CancelManager) Register(key string, cancel context.CancelFunc)
Register 注册一个可取消的操作。
func (*CancelManager) Unregister ¶
func (m *CancelManager) Unregister(key string)
Unregister 清理已注册的操作,不调用 cancel。
type InterruptError ¶
InterruptError 是系统中断类错误的标记接口。 FriendlyError 遇到实现该接口的 error 时,直接透传 InterruptReason(), 不走 APIError/网络错误的兜底文案,确保用户看到具体中断原因。
type LoopInterrupt ¶
type LoopInterrupt struct{ Reason string }
LoopInterrupt 死循环检测触发的中断。
func (*LoopInterrupt) Error ¶
func (e *LoopInterrupt) Error() string
func (*LoopInterrupt) InterruptReason ¶
func (e *LoopInterrupt) InterruptReason() string
type MaxTurnsInterrupt ¶
type MaxTurnsInterrupt struct{ Reason string }
MaxTurnsInterrupt 最大轮数耗尽触发的中断。
func (*MaxTurnsInterrupt) Error ¶
func (e *MaxTurnsInterrupt) Error() string
func (*MaxTurnsInterrupt) InterruptReason ¶
func (e *MaxTurnsInterrupt) InterruptReason() string
type RunOptions ¶
type RunOptions struct {
TurnID int
SessionID string
NovelID int64
Messages []map[string]any
AllowedTools map[string]bool
ActiveVersion int
SubAgentVersion int // 子 Agent 内存版本计数器,不持久化
Model *llm.ModelInfo
ProviderName string
AgentType string
SubTaskID string // 子 Agent 事件路由 ID
EventSeq *int // 共享事件序号,nil 时自建(主Agent);子Agent传入父的指针
MaxTurns int
ReasoningEffort string // 用户选择的推理等级
}
RunOptions 是单次 Run() 的参数。
type ToolFailureInterrupt ¶
type ToolFailureInterrupt struct{ Reason string }
ToolFailureInterrupt 工具连续失败触发的中断(system 5 次 / args 5 次 / 全局 10 次)。
func (*ToolFailureInterrupt) Error ¶
func (e *ToolFailureInterrupt) Error() string
func (*ToolFailureInterrupt) InterruptReason ¶
func (e *ToolFailureInterrupt) InterruptReason() string