v1.1 慢思考模式 (Thinking Phase)
将 ReAct 循环从单阶段升级为 Thinking + Action 双阶段架构: - Phase 1:剥夺工具访问权,强制模型先输出纯文本推理规划 - Phase 2:恢复工具挂载,模型按推理结果精准执行 - 新增 EnableThinking 开关,兼容旧模式
This commit is contained in:
+82
-63
@@ -17,92 +17,111 @@ type AgentEngine struct {
|
||||
|
||||
// WorkDir (工作区): 借鉴 OpenClaw 的理念,Agent 必须有一个明确的物理边界
|
||||
WorkDir string
|
||||
EnableThinking bool // 【新增】慢思考模式开关
|
||||
}
|
||||
|
||||
func NewAgentEngine(p provider.LLMProvider, r tools.Registry, workDir string) *AgentEngine {
|
||||
func NewAgentEngine(p provider.LLMProvider, r tools.Registry, workDir string, enableThinking bool) *AgentEngine {
|
||||
return &AgentEngine{
|
||||
provider: p,
|
||||
registry: r,
|
||||
WorkDir: workDir,
|
||||
EnableThinking: enableThinking,
|
||||
}
|
||||
}
|
||||
|
||||
// Run 启动 Agent 的生命周期
|
||||
// internal/engine/loop.go (续)
|
||||
func (e *AgentEngine) Run(ctx context.Context, userPrompt string) error {
|
||||
log.Printf("[Engine] 引擎启动,锁定工作区: %s\n", e.WorkDir)
|
||||
log.Printf("[Engine] 引擎启动,锁定工作区: %s\n", e.WorkDir)
|
||||
log.Printf("[Engine] 慢思考模式 (Thinking Phase): %v\n", e.EnableThinking)
|
||||
|
||||
// 1. 初始化会话的 Context (上下文内存)
|
||||
// 在真实的场景中,这里会由动态 Prompt 组装器加载 AGENTS.md。目前我们先硬编码。
|
||||
contextHistory := []schema.Message{
|
||||
{
|
||||
Role: schema.RoleSystem,
|
||||
Content: "You are go-tiny-claw, an expert coding assistant. You have full access to tools in the workspace.",
|
||||
},
|
||||
{
|
||||
Role: schema.RoleUser,
|
||||
Content: userPrompt,
|
||||
},
|
||||
}
|
||||
contextHistory := []schema.Message{
|
||||
{
|
||||
Role: schema.RoleSystem,
|
||||
Content: "You are go-tiny-claw, an expert coding assistant. You have full access to tools in the workspace.",
|
||||
},
|
||||
{
|
||||
Role: schema.RoleUser,
|
||||
Content: userPrompt,
|
||||
},
|
||||
}
|
||||
|
||||
turnCount := 0
|
||||
turnCount := 0
|
||||
|
||||
// 2. The Main Loop: 心跳开始 (标准的 ReAct 循环)
|
||||
for {
|
||||
turnCount++
|
||||
log.Printf("========== [Turn %d] 开始 ==========\n", turnCount)
|
||||
for {
|
||||
turnCount++
|
||||
log.Printf("\n========== [Turn %d] 开始 ==========\n", turnCount)
|
||||
|
||||
// 获取当前挂载的所有工具定义
|
||||
availableTools := e.registry.GetAvailableTools()
|
||||
// 获取当前挂载的所有工具定义
|
||||
availableTools := e.registry.GetAvailableTools()
|
||||
|
||||
// 向大模型发起推理请求 (包含 Reasoning)
|
||||
log.Println("[Engine] 正在思考 (Reasoning)...")
|
||||
responseMsg, err := e.provider.Generate(ctx, contextHistory, availableTools)
|
||||
if err != nil {
|
||||
return fmt.Errorf("模型生成失败: %w", err)
|
||||
}
|
||||
// ====================================================================
|
||||
// Phase 1: 慢思考阶段 (Thinking) - 剥夺工具,强制规划
|
||||
// ====================================================================
|
||||
if e.EnableThinking {
|
||||
log.Println("[Engine][Phase 1] 剥夺工具访问权,强制进入慢思考与规划阶段...")
|
||||
|
||||
// 将模型的响应完整追加到上下文历史中
|
||||
contextHistory = append(contextHistory, *responseMsg)
|
||||
// 核心机制:传入的 availableTools 为 nil!
|
||||
// 大模型看不到任何 JSON Schema,被迫只能输出纯文本的思考过程。
|
||||
thinkResp, err := e.provider.Generate(ctx, contextHistory, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Thinking 阶段生成失败: %w", err)
|
||||
}
|
||||
|
||||
// 如果模型回复了纯文本,打印出来 (这通常是它的思考过程,或是最终结果)
|
||||
if responseMsg.Content != "" {
|
||||
fmt.Printf("🤖 模型: %s\n", responseMsg.Content)
|
||||
}
|
||||
// 如果模型输出了思考过程,我们将其作为 Assistant 消息追加到上下文中
|
||||
if thinkResp.Content != "" {
|
||||
fmt.Printf("🧠 [内部思考 Trace]: %s\n", thinkResp.Content)
|
||||
contextHistory = append(contextHistory, *thinkResp)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 退出条件判断
|
||||
// 如果模型没有请求任何工具调用,说明它认为任务已经完成,跳出循环。
|
||||
if len(responseMsg.ToolCalls) == 0 {
|
||||
log.Println("[Engine] 任务完成,退出循环。")
|
||||
break
|
||||
}
|
||||
// ====================================================================
|
||||
// Phase 2: 行动阶段 (Action) - 恢复工具,顺着规划执行
|
||||
// ====================================================================
|
||||
log.Println("[Engine][Phase 2] 恢复工具挂载,等待模型采取行动...")
|
||||
|
||||
// 4. 执行行动 (Action) 与 获取观察结果 (Observation)
|
||||
log.Printf("[Engine] 模型请求调用 %d 个工具...\n", len(responseMsg.ToolCalls))
|
||||
// 此时的 contextHistory 中已经包含了上一阶段模型自己的 Thinking Trace。
|
||||
// 模型会顺着自己的逻辑,结合恢复的 availableTools 发起精准的工具调用。
|
||||
actionResp, err := e.provider.Generate(ctx, contextHistory, availableTools)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Action 阶段生成失败: %w", err)
|
||||
}
|
||||
|
||||
for _, toolCall := range responseMsg.ToolCalls {
|
||||
log.Printf(" -> 🛠️ 执行工具: %s, 参数: %s\n", toolCall.Name, string(toolCall.Arguments))
|
||||
contextHistory = append(contextHistory, *actionResp)
|
||||
|
||||
// 通过 Registry 路由并执行底层工具
|
||||
result := e.registry.Execute(ctx, toolCall)
|
||||
if actionResp.Content != "" {
|
||||
fmt.Printf("🤖 [对外回复]: %s\n", actionResp.Content)
|
||||
}
|
||||
|
||||
if result.IsError {
|
||||
log.Printf(" -> ❌ 工具执行报错: %s\n", result.Output)
|
||||
} else {
|
||||
log.Printf(" -> ✅ 工具执行成功 (返回 %d 字节)\n", len(result.Output))
|
||||
}
|
||||
// ====================================================================
|
||||
// 退出与执行逻辑 (与上一讲保持一致)
|
||||
// ====================================================================
|
||||
if len(actionResp.ToolCalls) == 0 {
|
||||
log.Println("[Engine] 模型未请求调用工具,任务宣告完成。")
|
||||
break
|
||||
}
|
||||
|
||||
// 将工具执行的观察结果 (Observation) 封装为 User Message 追加到上下文中
|
||||
// 注意:ToolCallID 必须携带!这是维系大模型推理链条的关键
|
||||
observationMsg := schema.Message{
|
||||
Role: schema.RoleUser,
|
||||
Content: result.Output,
|
||||
ToolCallID: toolCall.ID,
|
||||
}
|
||||
contextHistory = append(contextHistory, observationMsg)
|
||||
}
|
||||
log.Printf("[Engine] 模型请求调用 %d 个工具...\n", len(actionResp.ToolCalls))
|
||||
|
||||
// 循环回到开头,模型将带着新加入的 Observation 继续它的下一轮思考...
|
||||
}
|
||||
for _, toolCall := range actionResp.ToolCalls {
|
||||
log.Printf(" -> 🛠️ 执行工具: %s, 参数: %s\n", toolCall.Name, string(toolCall.Arguments))
|
||||
|
||||
return nil
|
||||
result := e.registry.Execute(ctx, toolCall)
|
||||
|
||||
if result.IsError {
|
||||
log.Printf(" -> ❌ 工具执行报错: %s\n", result.Output)
|
||||
} else {
|
||||
log.Printf(" -> ✅ 工具执行成功 (返回 %d 字节)\n", len(result.Output))
|
||||
}
|
||||
|
||||
// 将工具执行的观察结果追加到 Context,准备进入下一轮
|
||||
observationMsg := schema.Message{
|
||||
Role: schema.RoleUser,
|
||||
Content: result.Output,
|
||||
ToolCallID: toolCall.ID,
|
||||
}
|
||||
contextHistory = append(contextHistory, observationMsg)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user