MCPcopy Create free account
hub / github.com/astercloud/aster / executeTools

Method executeTools

pkg/agent/processor.go:297–380  ·  view source on GitHub ↗

executeTools 执行工具

(ctx context.Context, toolUses []*types.ToolUseBlock)

Source from the content-addressed store, hash-verified

295
296// executeTools 执行工具
297func (a *Agent) executeTools(ctx context.Context, toolUses []*types.ToolUseBlock) error {
298 toolResults := make([]types.ContentBlock, 0, len(toolUses))
299
300 for _, tu := range toolUses {
301 result := a.executeSingleTool(ctx, tu)
302 toolResults = append(toolResults, result)
303 }
304
305 // 保存工具结果
306 a.mu.Lock()
307 a.messages = append(a.messages, types.Message{
308 Role: types.MessageRoleUser,
309 ContentBlocks: toolResults,
310 })
311
312 // ✅ 修复:保存前在内存中修剪
313 if a.shouldTrimMessages() {
314 a.messages = a.trimMessagesInMemory(a.messages, a.config.Store.MaxMessages)
315 procLog.Debug(ctx, "messages trimmed in memory before save (after tool execution)", map[string]any{
316 "agent_id": a.id,
317 "max_messages": a.config.Store.MaxMessages,
318 "actual_count": len(a.messages),
319 })
320 }
321
322 a.stepCount++
323 a.mu.Unlock()
324
325 // 持久化(已修剪的消息)
326 if err := a.deps.Store.SaveMessages(ctx, a.id, a.messages); err != nil {
327 return fmt.Errorf("save messages: %w", err)
328 }
329
330 // 持久化工具记录
331 records := make([]types.ToolCallRecord, 0, len(a.toolRecords))
332 for _, record := range a.toolRecords {
333 records = append(records, *record)
334 }
335 if err := a.deps.Store.SaveToolCallRecords(ctx, a.id, records); err != nil {
336 return fmt.Errorf("save tool records: %w", err)
337 }
338
339 // 检查迭代限制(防止无限循环)
340 a.mu.Lock()
341 a.iterationCount++
342 currentIter := a.iterationCount
343 maxIter := a.maxIterations
344 if maxIter <= 0 {
345 maxIter = 50
346 }
347 a.mu.Unlock()
348
349 if currentIter > maxIter {
350 procLog.Warn(ctx, "iteration limit reached, waiting for user confirmation", map[string]any{
351 "agent_id": a.id, "iteration": currentIter, "max": maxIter,
352 })
353
354 // 发送迭代限制事件,等待用户确认是否继续

Callers 2

runModelStepMethod · 0.95
runNonStreamingStepMethod · 0.95

Calls 10

executeSingleToolMethod · 0.95
shouldTrimMessagesMethod · 0.95
trimMessagesInMemoryMethod · 0.95
runModelStepMethod · 0.95
EmitControlMethod · 0.80
DebugMethod · 0.65
SaveMessagesMethod · 0.65
SaveToolCallRecordsMethod · 0.65
WarnMethod · 0.65
InfoMethod · 0.65

Tested by

no test coverage detected