runModelStep 运行模型步骤
(ctx context.Context)
| 92 | |
| 93 | // runModelStep 运行模型步骤 |
| 94 | func (a *Agent) runModelStep(ctx context.Context) error { |
| 95 | procLog.Info(ctx, "runModelStep started", map[string]any{"agent_id": a.id}) |
| 96 | |
| 97 | // 检查执行模式 |
| 98 | executionMode := a.getExecutionMode() |
| 99 | if executionMode == types.ExecutionModeNonStreaming { |
| 100 | procLog.Info(ctx, "using NON-STREAMING mode (fast execution)", map[string]any{"agent_id": a.id}) |
| 101 | return a.runNonStreamingStep(ctx) |
| 102 | } |
| 103 | |
| 104 | procLog.Info(ctx, "using STREAMING mode (real-time feedback)", map[string]any{"agent_id": a.id}) |
| 105 | a.setBreakpoint(types.BreakpointStreamingModel) |
| 106 | |
| 107 | // 准备工具Schema(包含使用示例) |
| 108 | toolSchemas := make([]provider.ToolSchema, 0, len(a.toolMap)) |
| 109 | for _, tool := range a.toolMap { |
| 110 | schema := provider.ToolSchema{ |
| 111 | Name: tool.Name(), |
| 112 | Description: tool.Description(), |
| 113 | InputSchema: tool.InputSchema(), |
| 114 | } |
| 115 | // 检查工具是否实现了 ExampleableTool 接口 |
| 116 | if exampleable, ok := tool.(tools.ExampleableTool); ok { |
| 117 | examples := exampleable.Examples() |
| 118 | if len(examples) > 0 { |
| 119 | providerExamples := make([]provider.ToolExample, len(examples)) |
| 120 | for i, ex := range examples { |
| 121 | providerExamples[i] = provider.ToolExample{ |
| 122 | Description: ex.Description, |
| 123 | Input: ex.Input, |
| 124 | Output: ex.Output, |
| 125 | } |
| 126 | } |
| 127 | schema.InputExamples = providerExamples |
| 128 | } |
| 129 | } |
| 130 | toolSchemas = append(toolSchemas, schema) |
| 131 | } |
| 132 | toolNames := make([]string, len(toolSchemas)) |
| 133 | for i, ts := range toolSchemas { |
| 134 | toolNames[i] = ts.Name |
| 135 | } |
| 136 | procLog.Debug(ctx, "prepared tool schemas", map[string]any{"agent_id": a.id, "count": len(toolSchemas), "names": toolNames}) |
| 137 | |
| 138 | // 调用模型 |
| 139 | // 确保系统提示词包含工具手册(如果还没有注入) |
| 140 | a.mu.RLock() |
| 141 | hasManual := strings.Contains(a.template.SystemPrompt, "### Tools Manual") |
| 142 | toolMapSize := len(a.toolMap) |
| 143 | currentSystemPrompt := a.template.SystemPrompt |
| 144 | messages := a.messages // 复制当前消息列表 |
| 145 | a.mu.RUnlock() |
| 146 | |
| 147 | if !hasManual && toolMapSize > 0 { |
| 148 | procLog.Debug(ctx, "manual not found, injecting", map[string]any{"agent_id": a.id, "tool_map_size": toolMapSize}) |
| 149 | a.injectToolManual() |
| 150 | a.mu.RLock() |
| 151 | currentSystemPrompt = a.template.SystemPrompt |
no test coverage detected