Version: 0.9.2.dev.260406

后端:
   1.Chat 四路由升级(二分类 chat/task → 四路由 direct_reply/execute/deep_answer/plan)
     - 新建model/chat_contract.go:路由决策模型,含 NeedsRoughBuild 粗排标记
     - 更新node/chat.go:四路由分流;新增 deep_answer 深度回答路径(二次 LLM 开 thinking)
     - 更新prompt/chat.go:意图分类 prompt 升级为四路由 prompt;新增 deep_answer prompt
   2.粗排节点(RoughBuild)全链路
     - 新建node/rough_build.go:粗排节点,调用注入的算法函数,结果写入 ScheduleState 后进 Execute 微调
     - 更新graph/common_graph.go:注册 RoughBuild 节点;Chat/Confirm 后可路由至粗排
     - 更新model/graph_run_state.go:新增 RoughBuildPlacement/RoughBuildFunc 类型;Deps 注入入口
     - 更新model/plan_contract.go:PlanDecision 新增 NeedsRoughBuild/TaskClassIDs 字段
     - 更新node/plan.go:plan_done 时写入粗排标记和 TaskClassIDs
   3.任务类约束元数据(TaskClassMeta)贯穿 prompt → tools → 持久化
     - 更新tools/state.go:新增 TaskClassMeta;ScheduleState.TaskClasses;ScheduleTask.TaskClassID;Clone 深拷贝
     - 更新conv/schedule_state.go:加载时构建 TaskClassMeta;Diff 支持 HostEventID 嵌入关系
     - 更新conv/schedule_provider.go:新增 LoadTaskClassMetas 按需加载
     - 更新model/state_store.go:ScheduleStateProvider 接口新增 LoadTaskClassMetas
     - 更新prompt/base.go:renderStateSummary 渲染任务类约束
     - 更新prompt/plan.go:注入任务类 ID 上下文和粗排识别规则
     - 更新tools/read_tools.go:GetOverview 展示任务类约束
     - 更新model/common_state.go:CommonState 新增 TaskClassIDs/TaskClasses/NeedsRoughBuild
   4.Execute 健壮性增强(correction 重试 + 纯 ReAct 模式)
     - 更新node/execute.go:未知工具名/空文本走 correction 重试而非 fatal;maxConsecutiveCorrections 提升为包级常量;新增无 plan 纯ReAct 模式;工具结果截断;speak 排除 ask_user/confirm
     - 更新prompt/execute.go:新增 ReAct 模式 system prompt 和 contract
   5.写入持久化完善(task_item source + 嵌入水课)
     - 更新conv/schedule_persist.go:place/move/unplace 支持 task_item source,含嵌入水课和普通 task event 两条路径
     - 新建conv/schedule_preview.go:ScheduleState → 排程预览缓存,复用旧格式,前端无需改动
   6.状态持久化体系(Redis → MySQL outbox 异步)
     - 更新dao/cache.go:Redis 快照 TTL 从 24h 改为 2h,配合 MySQL outbox
     - 新建model/agent_state_snapshot_record.go:快照 MySQL 记录模型
     - 新建service/events/agent_state_persist.go:outbox 异步持久化处理器
     - 更新cmd/start.go + inits/mysql.go:注册快照事件处理器 + AutoMigrate
     - 更新service/agentsvc/agent_newagent.go:注入 RoughBuildFunc;outbox 异步写快照;排程结果写 Redis 预览缓存
   7.基础设施与稳定性
     - 更新stream/sse_adapter.go:outChan 满时静默丢弃,保证持久化不被 SSE 阻断
     - 更新service/agentsvc/agent.go:新增 readAgentExtraIntSlice;outChan 容量 8→256
     - 更新node/agent_nodes.go:Chat 注入工具 schema;Deliver 改 saveAgentState 替代 deleteAgentState
前端:无
仓库:无
This commit is contained in:
Losita
2026-04-06 23:15:54 +08:00
parent b1eb6bedf9
commit 2038185730
30 changed files with 1866 additions and 298 deletions

View File

@@ -1,63 +1,122 @@
package newagentprompt
import (
"fmt"
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
"github.com/cloudwego/eino/schema"
)
const chatIntentSystemPrompt = `
你是 SmartFlow 的意图分类器
你的唯一任务是判断用户本轮输入是"纯闲聊"还是"包含任务意图"。
const chatRoutingSystemPrompt = `
你是 SmartFlow 的智能路由器。你的职责是判断用户意图的复杂度,并决定后续处理路径
判断规则
1. chat打招呼、感谢、简单问答、情感表达、闲聊不涉及任何具体任务或操作请求。
2. task包含任何需要规划/执行/操作的意图,包括但不限于查询信息、创建内容、修改数据、安排日程、继续已有任务等。
你会看到
- 历史对话
- 用户本轮输入
- 当前可用工具摘要(如有)
- 本次排课涉及的任务类约束(如有)
保守原则:当不确定时,倾向于判断为 task宁可多走一次规划也不要丢失用户意图。
请遵守以下规则:
1. 只输出严格 JSON不要输出 markdown不要输出额外解释。
2. 根据用户意图判断复杂度并选择路由。
3. speak 字段始终填写:给用户看的话。
严格输出以下 JSON不要输出 markdown不要在 JSON 外补文字)
{"intent":"chat或task","reply":"仅当intent=chat时填写你的闲聊回复task时留空","reason":"简短判断依据"}
路由规则
- direct_reply纯闲聊、简单问答、打招呼、感谢等。speak 直接写你的完整回复。
- execute需要用工具处理的请求查询日程、移动课程、排课等但不需要先制定计划。speak 写简短确认。
- deep_answer复杂问题但不需要工具如分析建议、深度解释等需要深度思考后直接回答。speak 写过渡语(如"让我想想")。
- plan用户明确要求先制定计划或涉及多阶段复杂规划。speak 写确认语。
粗排判断:当用户意图包含"批量安排/排课/把任务类排进日程",且上下文中有任务类 ID 时,设置 needs_rough_build=true。
输出协议(严格 JSON
{"route":"direct_reply / execute / deep_answer / plan","speak":"给用户看的话","needs_rough_build":false,"reason":"简短判断依据"}
合法示例:
{"route":"direct_reply","speak":"你好!我是 SmartFlow 助手,有什么可以帮你的?","reason":"用户打招呼"}
{"route":"execute","speak":"好的,我来帮你看看今天的安排。","reason":"需要调用工具查询日程","needs_rough_build":false}
{"route":"execute","speak":"好的,我来帮你排课。","reason":"批量排课需求,有任务类 ID","needs_rough_build":true}
{"route":"deep_answer","speak":"这是个好问题,让我仔细想想。","reason":"需要深度分析但不需要工具"}
{"route":"plan","speak":"明白,我来帮你制定一个完整的学习计划。","reason":"用户明确要求制定计划"}
`
// BuildChatIntentSystemPrompt 返回意图分类系统提示词。
func BuildChatIntentSystemPrompt() string {
return strings.TrimSpace(chatIntentSystemPrompt)
// BuildChatRoutingSystemPrompt 返回路由阶段的系统提示词。
func BuildChatRoutingSystemPrompt() string {
return strings.TrimSpace(chatRoutingSystemPrompt)
}
// BuildChatIntentMessages 组装意图分类的 messages。
//
// 职责边界:
// 1. 只取最近 6 条历史,保证分类高效;
// 2. 不注入 pinned blocks / tool schemas分类不需要这些信息
// 3. 不负责解析模型输出。
func BuildChatIntentMessages(conversationContext *newagentmodel.ConversationContext, userInput string) []*schema.Message {
messages := make([]*schema.Message, 0, 8)
// BuildChatRoutingMessages 组装路由阶段的 messages。
func BuildChatRoutingMessages(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) []*schema.Message {
return buildStageMessages(
BuildChatRoutingSystemPrompt(),
ctx,
BuildChatRoutingUserPrompt(ctx, userInput, state),
)
}
messages = append(messages, schema.SystemMessage(BuildChatIntentSystemPrompt()))
// BuildChatRoutingUserPrompt 构造路由阶段的用户提示词。
func BuildChatRoutingUserPrompt(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) string {
var sb strings.Builder
if conversationContext != nil {
history := conversationContext.HistorySnapshot()
if len(history) > 6 {
history = history[len(history)-6:]
sb.WriteString("请判断用户本轮意图的复杂度,并选择最合适的路由。\n")
// 注入任务类上下文(供粗排判断参考)。
if state != nil && len(state.TaskClassIDs) > 0 {
parts := make([]string, len(state.TaskClassIDs))
for i, id := range state.TaskClassIDs {
parts[i] = fmt.Sprintf("%d", id)
}
if len(history) > 0 {
messages = append(messages, history...)
sb.WriteString(fmt.Sprintf("\n本次请求涉及的任务类 ID[%s]\n", strings.Join(parts, ", ")))
}
if state != nil && len(state.TaskClasses) > 0 {
sb.WriteString("任务类约束:\n")
for _, tc := range state.TaskClasses {
line := fmt.Sprintf("- [ID=%d] %s策略=%s总时段预算=%d", tc.ID, tc.Name, tc.Strategy, tc.TotalSlots)
if tc.StartDate != "" || tc.EndDate != "" {
line += fmt.Sprintf(",日期范围=%s ~ %s", tc.StartDate, tc.EndDate)
}
sb.WriteString(line + "\n")
}
}
// 只在 history 末尾还没有当前用户消息时才追加,
// 避免与 loadConversationContext 的预追加产生重复。
trimmedInput := strings.TrimSpace(userInput)
if trimmedInput != "" {
alreadyLast := len(messages) > 0 &&
messages[len(messages)-1].Role == schema.User &&
messages[len(messages)-1].Content == trimmedInput
if !alreadyLast {
messages = append(messages, schema.UserMessage(trimmedInput))
}
sb.WriteString("\n用户本轮输入\n")
sb.WriteString(trimmedInput)
sb.WriteString("\n")
}
return messages
return strings.TrimSpace(sb.String())
}
// --- 深度回答 prompt ---
const deepAnswerSystemPrompt = `
你是 SmartFlow 的深度分析助手。用户提出了一个需要深入思考的问题,请认真分析后给出详细、有价值的回答。
请遵守以下规则:
1. 充分利用上下文中已有的信息(任务类约束、日程数据、历史对话等)。
2. 如果缺少关键信息,在回答中说明需要哪些额外信息。
3. 直接输出你的回答,不要输出 JSON。
`
// BuildDeepAnswerSystemPrompt 返回深度回答阶段的系统提示词。
func BuildDeepAnswerSystemPrompt() string {
return strings.TrimSpace(deepAnswerSystemPrompt)
}
// BuildDeepAnswerMessages 组装深度回答阶段的 messages。
func BuildDeepAnswerMessages(ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message {
return buildStageMessages(
BuildDeepAnswerSystemPrompt(),
ctx,
userInput,
)
}