Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions pkg/agent/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func (al *AgentLoop) runAgentLoop(ctx context.Context, agent *AgentInstance, opt
agent.Sessions.AddMessage(opts.SessionKey, "user", opts.UserMessage)

// 4. Run LLM iteration loop
finalContent, iteration, err := al.runLLMIteration(ctx, agent, messages, opts)
finalContent, iteration, sentUserContent, err := al.runLLMIteration(ctx, agent, messages, opts)
if err != nil {
return "", err
}
Expand All @@ -519,20 +519,35 @@ func (al *AgentLoop) runAgentLoop(ctx context.Context, agent *AgentInstance, opt

// 5. Handle empty response
if finalContent == "" {
finalContent = opts.DefaultResponse
if iteration >= agent.MaxIterations {
finalContent = "I've reached the maximum number of steps for this request. Please try breaking it down into smaller tasks."
} else if sentUserContent {
// We already sent content to the user (via tools), so we can suppress the default response
finalContent = ""
} else if iteration > 1 {
// Tools were executed but didn't send output, and LLM is silent.
// Use a generic success message instead of "no response to give" which sounds like an error.
finalContent = "Task completed."
} else {
finalContent = opts.DefaultResponse
}
}
}

// 6. Save final assistant message to session
agent.Sessions.AddMessage(opts.SessionKey, "assistant", finalContent)
agent.Sessions.Save(opts.SessionKey)
// Only save if meaningful (LLM might have returned empty string if it thought it was done)
if finalContent != "" {
agent.Sessions.AddMessage(opts.SessionKey, "assistant", finalContent)
agent.Sessions.Save(opts.SessionKey)
}

// 7. Optional: summarization
if opts.EnableSummary {
al.maybeSummarize(agent, opts.SessionKey, opts.Channel, opts.ChatID)
}

// 8. Optional: send response via bus
if opts.SendResponse {
if opts.SendResponse && finalContent != "" {
al.bus.PublishOutbound(ctx, bus.OutboundMessage{
Channel: opts.Channel,
ChatID: opts.ChatID,
Expand Down Expand Up @@ -587,9 +602,10 @@ func (al *AgentLoop) runLLMIteration(
agent *AgentInstance,
messages []providers.Message,
opts processOptions,
) (string, int, error) {
) (string, int, bool, error) {
iteration := 0
var finalContent string
sentUserContent := false

for iteration < agent.MaxIterations {
iteration++
Expand Down Expand Up @@ -704,7 +720,7 @@ func (al *AgentLoop) runLLMIteration(
"iteration": iteration,
"error": err.Error(),
})
return "", iteration, fmt.Errorf("LLM call failed after retries: %w", err)
return "", iteration, sentUserContent, fmt.Errorf("LLM call failed after retries: %w", err)
}

go al.handleReasoning(ctx, response.Reasoning, opts.Channel, al.targetReasoningChannelID(opts.Channel))
Expand Down Expand Up @@ -825,6 +841,7 @@ func (al *AgentLoop) runLLMIteration(
ChatID: opts.ChatID,
Content: toolResult.ForUser,
})
sentUserContent = true
logger.DebugCF("agent", "Sent tool result to user",
map[string]any{
"tool": tc.Name,
Expand Down Expand Up @@ -872,7 +889,7 @@ func (al *AgentLoop) runLLMIteration(
}
}

return finalContent, iteration, nil
return finalContent, iteration, sentUserContent, nil
}

// updateToolContexts updates the context for tools that need channel/chatID info.
Expand Down