agent: avoid no-response fallback after long tool runs by summarizing tool outputs

This commit is contained in:
DBT
2026-02-27 09:41:08 +00:00
parent 602264df6e
commit 4c2a08396e

View File

@@ -539,6 +539,8 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
iteration := 0 iteration := 0
var finalContent string var finalContent string
hasToolActivity := false
lastToolOutputs := make([]string, 0, 4)
for iteration < al.maxIterations { for iteration < al.maxIterations {
iteration++ iteration++
@@ -665,6 +667,7 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
// Persist assistant message with tool calls. // Persist assistant message with tool calls.
al.sessions.AddMessageFull(msg.SessionKey, assistantMsg) al.sessions.AddMessageFull(msg.SessionKey, assistantMsg)
hasToolActivity = true
for _, tc := range response.ToolCalls { for _, tc := range response.ToolCalls {
// Log tool call with arguments preview // Log tool call with arguments preview
argsJSON, _ := json.Marshal(tc.Arguments) argsJSON, _ := json.Marshal(tc.Arguments)
@@ -679,6 +682,9 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
if err != nil { if err != nil {
result = fmt.Sprintf("Error: %v", err) result = fmt.Sprintf("Error: %v", err)
} }
if len(lastToolOutputs) < 4 {
lastToolOutputs = append(lastToolOutputs, fmt.Sprintf("%s: %s", tc.Name, truncate(strings.ReplaceAll(result, "\n", " "), 180)))
}
toolResultMsg := providers.Message{ toolResultMsg := providers.Message{
Role: "tool", Role: "tool",
Content: result, Content: result,
@@ -691,11 +697,15 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
} }
if strings.TrimSpace(finalContent) == "" { if strings.TrimSpace(finalContent) == "" {
fallback := strings.TrimSpace(al.noResponseFallback) if hasToolActivity && len(lastToolOutputs) > 0 {
if fallback == "" { finalContent = "我已执行完成,关键信息如下:\n- " + strings.Join(lastToolOutputs, "\n- ")
fallback = "在的,我刚刚这条回复丢了。请再说一次,我马上处理。" } else {
fallback := strings.TrimSpace(al.noResponseFallback)
if fallback == "" {
fallback = "在的,我刚刚这条回复丢了。请再说一次,我马上处理。"
}
finalContent = fallback
} }
finalContent = fallback
} }
// Filter out <think>...</think> content from user-facing response // Filter out <think>...</think> content from user-facing response