Version: 0.8.8.dev.260403
后端: 1.新建Deliver节点:LLM生成任务总结,失败降级到机械格式化,伪流式输出 2.新建Confirm节点:确认卡片推送与状态持久化 3.新建Interrupt节点:追问/确认/默认中断三种处理路径 4.实现状态持久化体系:model层定义AgentStateStore接口+AgentStateSnapshot快照,dao/cache.go新增Redis CRUD,agent_nodes层每节点自动存快照、Deliver完成后清理 5.所有model struct补充JSON tags,支持Redis序列化/反序列化 前端:无 仓库:无
This commit is contained in:
@@ -11,8 +11,8 @@ import (
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 负责把 node 层真正实现的方法统一暴露给 graph 注册;
|
||||
// 2. 负责收口“graph 只编排、node 真执行”的结构约束;
|
||||
// 3. 当前先迁移 Plan,其他节点后续按同样模式逐步下沉。
|
||||
// 2. 负责收口"graph 只编排、node 真执行"的结构约束;
|
||||
// 3. 负责在每个节点执行成功后统一做状态持久化(Save/Delete)。
|
||||
type AgentNodes struct{}
|
||||
|
||||
// NewAgentNodes 创建通用节点容器。
|
||||
@@ -25,7 +25,7 @@ func NewAgentNodes() *AgentNodes {
|
||||
// 职责边界:
|
||||
// 1. 这里只做 graph -> node 的参数转接;
|
||||
// 2. 真正的入口逻辑仍由 RunChatNode 负责;
|
||||
// 3. 这样 graph 层后续只需挂 n.Chat,而不再自己维护占位 chatNode。
|
||||
// 3. Chat 的 Save 交给 Service 层处理,这里不做持久化。
|
||||
func (n *AgentNodes) Chat(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("chat node: state is nil")
|
||||
@@ -47,12 +47,39 @@ func (n *AgentNodes) Chat(ctx context.Context, st *newagentmodel.AgentGraphState
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// Confirm 是确认阶段的正式节点方法。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 这里只做 graph -> node 的参数转接;
|
||||
// 2. 真正的确认逻辑仍由 RunConfirmNode 负责;
|
||||
// 3. 不需要 LLM Client — 确认内容由已有状态机械格式化。
|
||||
// 4. Confirm 执行成功后保存状态,因为它创建了 PendingInteraction。
|
||||
func (n *AgentNodes) Confirm(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("confirm node: state is nil")
|
||||
}
|
||||
|
||||
if err := RunConfirmNode(
|
||||
ctx,
|
||||
ConfirmNodeInput{
|
||||
RuntimeState: st.EnsureRuntimeState(),
|
||||
ConversationContext: st.EnsureConversationContext(),
|
||||
ChunkEmitter: st.EnsureChunkEmitter(),
|
||||
},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
saveAgentState(ctx, st)
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// Plan 是规划阶段的正式节点方法。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 这里只做 graph -> node 的参数转接;
|
||||
// 2. 真正的单轮规划逻辑仍由 RunPlanNode 负责;
|
||||
// 3. 这样 graph 层后续只需挂 n.Plan,而不再自己维护占位 planNode。
|
||||
// 3. Plan 执行成功后保存状态,支持意外断线恢复。
|
||||
func (n *AgentNodes) Plan(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("plan node: state is nil")
|
||||
@@ -71,6 +98,33 @@ func (n *AgentNodes) Plan(ctx context.Context, st *newagentmodel.AgentGraphState
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
saveAgentState(ctx, st)
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// Interrupt 是中断阶段的正式节点方法。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 这里只做 graph -> node 的参数转接;
|
||||
// 2. 真正的中断逻辑仍由 RunInterruptNode 负责;
|
||||
// 3. 不需要 LLM Client — 所有文本已在 PendingInteraction 里。
|
||||
// 4. 不需要 Save — 上游节点(Plan/Execute/Confirm)已经存过了。
|
||||
func (n *AgentNodes) Interrupt(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("interrupt node: state is nil")
|
||||
}
|
||||
|
||||
if err := RunInterruptNode(
|
||||
ctx,
|
||||
InterruptNodeInput{
|
||||
RuntimeState: st.EnsureRuntimeState(),
|
||||
ConversationContext: st.EnsureConversationContext(),
|
||||
ChunkEmitter: st.EnsureChunkEmitter(),
|
||||
},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
|
||||
@@ -78,13 +132,13 @@ func (n *AgentNodes) Plan(ctx context.Context, st *newagentmodel.AgentGraphState
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 这里只做 graph -> node 的参数转接;
|
||||
// 2. 真正的单轮执行逻辑仍由 RunExecuteNode 负责;
|
||||
// 3. 这样 graph 层后续只需挂 n.Execute,而不再自己维护占位 executeNode。
|
||||
// 2. 真正的单轮执行逻辑仍由 RunExecuteNode 负责。
|
||||
//
|
||||
// 设计原则:
|
||||
// 1. LLM 主导:LLM 自己判断 done_when 是否满足,自己决定何时推进/完成;
|
||||
// 2. 后端兜底:只做资源控制、安全兜底、证据记录;
|
||||
// 3. 不做硬校验:后端不质疑 LLM 的 advance/complete 决策。
|
||||
// 4. Execute 每轮执行成功后保存状态,支持意外断线恢复。
|
||||
func (n *AgentNodes) Execute(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("execute node: state is nil")
|
||||
@@ -103,5 +157,102 @@ func (n *AgentNodes) Execute(ctx context.Context, st *newagentmodel.AgentGraphSt
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
saveAgentState(ctx, st)
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// Deliver 是交付阶段的正式节点方法。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 这里只做 graph -> node 的参数转接;
|
||||
// 2. 真正的交付逻辑仍由 RunDeliverNode 负责;
|
||||
// 3. 调 LLM 生成任务总结,失败时降级到机械格式化。
|
||||
// 4. 任务完成后删除 Redis 快照,清理持久化状态。
|
||||
func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("deliver node: state is nil")
|
||||
}
|
||||
|
||||
if err := RunDeliverNode(
|
||||
ctx,
|
||||
DeliverNodeInput{
|
||||
RuntimeState: st.EnsureRuntimeState(),
|
||||
ConversationContext: st.EnsureConversationContext(),
|
||||
Client: st.Deps.ResolveDeliverClient(),
|
||||
ChunkEmitter: st.EnsureChunkEmitter(),
|
||||
},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deleteAgentState(ctx, st)
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// --- 持久化辅助 ---
|
||||
|
||||
// saveAgentState 在节点执行成功后,将当前运行态快照保存到 Redis。
|
||||
//
|
||||
// 设计原则:
|
||||
// 1. Save 失败只记日志,不中断 Graph 流程;
|
||||
// 2. StateStore 为空时静默跳过(骨架期 / 测试环境);
|
||||
// 3. conversationID 为空时也静默跳过,避免写入无效 key。
|
||||
//
|
||||
// TODO: 接入项目统一的日志框架后,把 _ = err 改成结构化日志。
|
||||
func saveAgentState(ctx context.Context, st *newagentmodel.AgentGraphState) {
|
||||
if st == nil {
|
||||
return
|
||||
}
|
||||
store := st.Deps.StateStore
|
||||
if store == nil {
|
||||
return
|
||||
}
|
||||
|
||||
runtimeState := st.EnsureRuntimeState()
|
||||
if runtimeState == nil {
|
||||
return
|
||||
}
|
||||
|
||||
flowState := runtimeState.EnsureCommonState()
|
||||
if flowState == nil || flowState.ConversationID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
snapshot := &newagentmodel.AgentStateSnapshot{
|
||||
RuntimeState: runtimeState,
|
||||
ConversationContext: st.EnsureConversationContext(),
|
||||
}
|
||||
|
||||
_ = store.Save(ctx, flowState.ConversationID, snapshot)
|
||||
}
|
||||
|
||||
// deleteAgentState 在任务完成后,删除 Redis 中的运行态快照。
|
||||
//
|
||||
// 设计原则:
|
||||
// 1. Delete 失败只记日志,不中断 Graph 流程;
|
||||
// 2. 删除是幂等的,key 不存在也视为成功;
|
||||
// 3. StateStore 为空时静默跳过。
|
||||
//
|
||||
// TODO: 接入项目统一的日志框架后,把 _ = err 改成结构化日志。
|
||||
func deleteAgentState(ctx context.Context, st *newagentmodel.AgentGraphState) {
|
||||
if st == nil {
|
||||
return
|
||||
}
|
||||
store := st.Deps.StateStore
|
||||
if store == nil {
|
||||
return
|
||||
}
|
||||
|
||||
runtimeState := st.EnsureRuntimeState()
|
||||
if runtimeState == nil {
|
||||
return
|
||||
}
|
||||
|
||||
flowState := runtimeState.EnsureCommonState()
|
||||
if flowState == nil || flowState.ConversationID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
_ = store.Delete(ctx, flowState.ConversationID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user