Store session history, preferences, and dated records in one database per home. Commit complete turns, checkpoint tool batches, and compact history by prefix. Add memory search and writes, with previews for ambiguous corrections. Review conversations in the background and support SQLite backup and restore.
834 lines
26 KiB
Go
834 lines
26 KiB
Go
// PicoClaw - Ultra-lightweight personal AI agent
|
|
|
|
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/constants"
|
|
runtimeevents "github.com/sipeed/picoclaw/pkg/events"
|
|
"github.com/sipeed/picoclaw/pkg/logger"
|
|
"github.com/sipeed/picoclaw/pkg/providers"
|
|
)
|
|
|
|
// CallLLM performs an LLM call with fallback support, hook invocation, and retry logic.
|
|
// It handles PreLLM setup, the actual LLM invocation with retry, and AfterLLM processing.
|
|
// Returns Control indicating what the coordinator should do next.
|
|
func (p *Pipeline) CallLLM(
|
|
ctx context.Context,
|
|
turnCtx context.Context,
|
|
ts *turnState,
|
|
exec *turnExecution,
|
|
iteration int,
|
|
) (Control, error) {
|
|
al := p.al
|
|
maxMediaSize := p.Cfg.Agents.Defaults.GetMaxMediaSize()
|
|
|
|
// PreLLM: resolve media refs (except on iteration 1 where user media is already resolved)
|
|
if iteration > 1 {
|
|
exec.messages = resolveMediaRefs(exec.messages, p.MediaStore, maxMediaSize, exec.currentTurnStart)
|
|
}
|
|
|
|
// PreLLM: graceful terminal handling
|
|
exec.gracefulTerminal, _ = ts.gracefulInterruptRequested()
|
|
exec.providerToolDefs = ts.agent.Tools.ToProviderDefs()
|
|
exec.providerToolDefs = turnToolDefs(exec.providerToolDefs, ts)
|
|
|
|
// Native web search support
|
|
webSearchEnabled := al.cfg.Tools.IsToolEnabled("web") && turnProfileToolAllowed(ts.profile, "web_search")
|
|
exec.useNativeSearch = webSearchEnabled && al.cfg.Tools.Web.PreferNative &&
|
|
func() bool {
|
|
if ns, ok := exec.activeProvider.(providers.NativeSearchCapable); ok {
|
|
return ns.SupportsNativeSearch()
|
|
}
|
|
return false
|
|
}()
|
|
if exec.useNativeSearch {
|
|
filtered := make([]providers.ToolDefinition, 0, len(exec.providerToolDefs))
|
|
for _, td := range exec.providerToolDefs {
|
|
if td.Function.Name != "web_search" {
|
|
filtered = append(filtered, td)
|
|
}
|
|
}
|
|
exec.providerToolDefs = filtered
|
|
}
|
|
|
|
exec.callMessages = exec.messages
|
|
if exec.gracefulTerminal {
|
|
exec.callMessages = append(append([]providers.Message(nil), exec.messages...), ts.interruptHintMessage())
|
|
exec.providerToolDefs = nil
|
|
ts.markGracefulTerminalUsed()
|
|
}
|
|
if err := p.routeMediaTurn(ts, exec); err != nil {
|
|
return ControlBreak, err
|
|
}
|
|
|
|
exec.llmOpts = map[string]any{
|
|
"max_tokens": ts.agent.MaxTokens,
|
|
"temperature": ts.agent.Temperature,
|
|
"prompt_cache_key": ts.agent.ID,
|
|
}
|
|
if exec.useNativeSearch {
|
|
exec.llmOpts["native_search"] = true
|
|
}
|
|
applyTurnThinkingOptions(exec, ts.agent, exec.activeProvider, true)
|
|
|
|
exec.llmModel = exec.activeModel
|
|
nativeSearchBeforeHook := exec.useNativeSearch
|
|
|
|
// BeforeLLM hook
|
|
if p.Hooks != nil && !ts.opts.privateMemory {
|
|
llmReq, decision := p.Hooks.BeforeLLM(turnCtx, &LLMHookRequest{
|
|
Meta: ts.eventMeta("runTurn", "turn.llm.request"),
|
|
Context: cloneTurnContext(ts.turnCtx),
|
|
Model: exec.llmModel,
|
|
Messages: exec.callMessages,
|
|
Tools: exec.providerToolDefs,
|
|
Options: exec.llmOpts,
|
|
GracefulTerminal: exec.gracefulTerminal,
|
|
})
|
|
switch decision.normalizedAction() {
|
|
case HookActionContinue, HookActionModify:
|
|
if llmReq != nil {
|
|
prevModel := exec.llmModel
|
|
exec.llmModel = llmReq.Model
|
|
exec.callMessages = llmReq.Messages
|
|
exec.providerToolDefs = turnToolDefs(llmReq.Tools, ts)
|
|
exec.llmOpts = llmReq.Options
|
|
if strings.TrimSpace(exec.llmModel) != "" && exec.llmModel != prevModel {
|
|
if err := p.applyBeforeLLMModelRewrite(ts, exec); err != nil {
|
|
return ControlBreak, err
|
|
}
|
|
applyTurnThinkingOptions(exec, ts.agent, exec.activeProvider, true)
|
|
}
|
|
}
|
|
case HookActionAbortTurn:
|
|
cancelConfiguredStreamingLLM(turnCtx, exec)
|
|
exec.abortedByHook = true
|
|
return ControlBreak, nil
|
|
case HookActionHardAbort:
|
|
cancelConfiguredStreamingLLM(turnCtx, exec)
|
|
_ = ts.requestHardAbort()
|
|
exec.abortedByHardAbort = true
|
|
return ControlBreak, nil
|
|
}
|
|
}
|
|
exec.useNativeSearch = webSearchEnabled && al.cfg.Tools.Web.PreferNative &&
|
|
func() bool {
|
|
if ns, ok := exec.activeProvider.(providers.NativeSearchCapable); ok {
|
|
return ns.SupportsNativeSearch()
|
|
}
|
|
return false
|
|
}()
|
|
if nativeSearchBeforeHook && !exec.useNativeSearch {
|
|
exec.providerToolDefs = restoreToolDefinition(
|
|
exec.providerToolDefs,
|
|
turnToolDefs(ts.agent.Tools.ToProviderDefs(), ts),
|
|
"web_search",
|
|
)
|
|
}
|
|
if exec.useNativeSearch {
|
|
exec.providerToolDefs = filterClientWebSearch(exec.providerToolDefs)
|
|
if exec.llmOpts == nil {
|
|
exec.llmOpts = make(map[string]any)
|
|
}
|
|
exec.llmOpts["native_search"] = true
|
|
} else {
|
|
delete(exec.llmOpts, "native_search")
|
|
}
|
|
|
|
al.emitEvent(
|
|
runtimeevents.KindAgentLLMRequest,
|
|
ts.eventMeta("runTurn", "turn.llm.request"),
|
|
LLMRequestPayload{
|
|
Model: exec.llmModel,
|
|
MessagesCount: len(exec.callMessages),
|
|
ToolsCount: len(exec.providerToolDefs),
|
|
MaxTokens: ts.agent.MaxTokens,
|
|
Temperature: ts.agent.Temperature,
|
|
},
|
|
)
|
|
|
|
logger.DebugCF("agent", "LLM request",
|
|
map[string]any{
|
|
"agent_id": ts.agent.ID,
|
|
"iteration": iteration,
|
|
"model": exec.llmModel,
|
|
"messages_count": len(exec.callMessages),
|
|
"tools_count": len(exec.providerToolDefs),
|
|
"max_tokens": ts.agent.MaxTokens,
|
|
"temperature": ts.agent.Temperature,
|
|
"system_prompt_len": len(exec.callMessages[0].Content),
|
|
})
|
|
if !ts.opts.privateMemory {
|
|
logger.DebugCF("agent", "Full LLM request",
|
|
map[string]any{
|
|
"iteration": iteration,
|
|
"messages_json": formatMessagesForLog(exec.callMessages),
|
|
"tools_json": formatToolsForLog(exec.providerToolDefs),
|
|
})
|
|
}
|
|
|
|
// LLM call closure with fallback support
|
|
callLLM := func(
|
|
messagesForCall []providers.Message,
|
|
toolDefsForCall []providers.ToolDefinition,
|
|
) (*providers.LLMResponse, error) {
|
|
providerCtx, providerCancel := context.WithCancel(turnCtx)
|
|
ts.setProviderCancel(providerCancel)
|
|
defer func() {
|
|
providerCancel()
|
|
ts.clearProviderCancel(providerCancel)
|
|
}()
|
|
|
|
al.activeRequestsInc()
|
|
defer al.activeRequestsDec()
|
|
|
|
if response, handled, streamErr := p.tryConfiguredStreamingLLM(
|
|
providerCtx,
|
|
ts,
|
|
exec,
|
|
messagesForCall,
|
|
toolDefsForCall,
|
|
); handled {
|
|
return response, streamErr
|
|
}
|
|
|
|
runCandidate := func(
|
|
ctx context.Context,
|
|
candidate providers.FallbackCandidate,
|
|
) (*providers.LLMResponse, error) {
|
|
candidateProvider, err := providerForFallbackCandidate(
|
|
ts.agent,
|
|
exec.activeProvider,
|
|
exec.activeCandidates,
|
|
candidate,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
callOpts := shallowCloneLLMOptions(exec.llmOpts)
|
|
delete(callOpts, "thinking_level")
|
|
candidateTools := toolDefsForCall
|
|
candidateNativeSearch := webSearchEnabled && al.cfg.Tools.Web.PreferNative &&
|
|
func() bool {
|
|
if ns, ok := candidateProvider.(providers.NativeSearchCapable); ok {
|
|
return ns.SupportsNativeSearch()
|
|
}
|
|
return false
|
|
}()
|
|
if candidateNativeSearch {
|
|
candidateTools = filterClientWebSearch(candidateTools)
|
|
callOpts["native_search"] = true
|
|
} else {
|
|
delete(callOpts, "native_search")
|
|
if exec.useNativeSearch {
|
|
candidateTools = restoreToolDefinition(
|
|
candidateTools,
|
|
turnToolDefs(ts.agent.Tools.ToProviderDefs(), ts),
|
|
"web_search",
|
|
)
|
|
}
|
|
}
|
|
candidateCfg := resolveActiveModelConfig(
|
|
p.Cfg,
|
|
ts.agent.Workspace,
|
|
[]providers.FallbackCandidate{candidate},
|
|
candidate.Model,
|
|
p.Cfg.Agents.Defaults.Provider,
|
|
)
|
|
candidateThinking := thinkingSettingsFromModelConfig(candidateCfg)
|
|
applyThinkingOption(callOpts, candidateProvider, candidateThinking, true, ts.agent.ID)
|
|
exec.suppressReasoning = shouldSuppressReasoningFor(candidateThinking)
|
|
return candidateProvider.Chat(ctx, messagesForCall, candidateTools, candidate.Model, callOpts)
|
|
}
|
|
|
|
if len(exec.activeCandidates) > 1 && p.Fallback != nil {
|
|
var (
|
|
fbResult *providers.FallbackResult
|
|
fbErr error
|
|
)
|
|
if hasMediaRefs(messagesForCall) {
|
|
fbResult, fbErr = p.Fallback.ExecuteImageCandidate(
|
|
providerCtx,
|
|
exec.activeCandidates,
|
|
func(ctx context.Context, candidate providers.FallbackCandidate) (*providers.LLMResponse, error) {
|
|
return runCandidate(ctx, candidate)
|
|
},
|
|
)
|
|
} else {
|
|
fbResult, fbErr = p.Fallback.ExecuteCandidate(
|
|
providerCtx,
|
|
exec.activeCandidates,
|
|
runCandidate,
|
|
)
|
|
}
|
|
if fbErr != nil {
|
|
return nil, fbErr
|
|
}
|
|
if fbResult.Provider != "" && len(fbResult.Attempts) > 0 {
|
|
logger.InfoCF(
|
|
"agent",
|
|
fmt.Sprintf("Fallback: succeeded with %s/%s after %d attempts",
|
|
fbResult.Provider, fbResult.Model, len(fbResult.Attempts)+1),
|
|
map[string]any{"agent_id": ts.agent.ID, "iteration": iteration},
|
|
)
|
|
}
|
|
for _, candidate := range exec.activeCandidates {
|
|
if candidate.StableKey() != fbResult.IdentityKey {
|
|
continue
|
|
}
|
|
exec.llmModelName = resolvedCandidateModelName(
|
|
[]providers.FallbackCandidate{candidate},
|
|
exec.llmModelName,
|
|
)
|
|
break
|
|
}
|
|
return fbResult.Response, nil
|
|
}
|
|
return exec.activeProvider.Chat(providerCtx, messagesForCall, toolDefsForCall, exec.llmModel, exec.llmOpts)
|
|
}
|
|
|
|
// Retry loop
|
|
var err error
|
|
maxRetries := p.Cfg.Agents.Defaults.MaxLLMRetries
|
|
if maxRetries <= 0 {
|
|
maxRetries = 2
|
|
}
|
|
backoffSecs := p.Cfg.Agents.Defaults.LLMRetryBackoffSecs
|
|
if backoffSecs <= 0 {
|
|
backoffSecs = 2
|
|
}
|
|
for retry := 0; retry <= maxRetries; retry++ {
|
|
exec.response, err = callLLM(exec.callMessages, exec.providerToolDefs)
|
|
if err == nil {
|
|
break
|
|
}
|
|
if ts.hardAbortRequested() && errors.Is(err, context.Canceled) {
|
|
_ = ts.requestHardAbort()
|
|
exec.abortedByHardAbort = true
|
|
return ControlBreak, nil
|
|
}
|
|
if isConfiguredStreamingVisibleError(err) {
|
|
break
|
|
}
|
|
|
|
if hasMediaRefs(exec.callMessages) && isVisionUnsupportedError(err) {
|
|
return ControlBreak, visionUnsupportedModelError(
|
|
exec.llmModelName,
|
|
len(ts.agent.ImageCandidates) > 0,
|
|
)
|
|
}
|
|
|
|
errMsg := strings.ToLower(err.Error())
|
|
retryReason, isTransientError := transientLLMRetryReason(err)
|
|
isContextError := !isTransientError && (strings.Contains(errMsg, "context_length_exceeded") ||
|
|
strings.Contains(errMsg, "context window") ||
|
|
strings.Contains(errMsg, "context_window") ||
|
|
strings.Contains(errMsg, "maximum context length") ||
|
|
strings.Contains(errMsg, "token limit") ||
|
|
strings.Contains(errMsg, "too many tokens") ||
|
|
strings.Contains(errMsg, "max_tokens") ||
|
|
strings.Contains(errMsg, "invalidparameter") ||
|
|
strings.Contains(errMsg, "prompt is too long") ||
|
|
strings.Contains(errMsg, "request too large"))
|
|
|
|
if isTransientError && retry < maxRetries {
|
|
backoff := time.Duration(retry+1) * time.Duration(backoffSecs) * time.Second
|
|
al.emitEvent(
|
|
runtimeevents.KindAgentLLMRetry,
|
|
ts.eventMeta("runTurn", "turn.llm.retry"),
|
|
LLMRetryPayload{
|
|
Attempt: retry + 1,
|
|
MaxRetries: maxRetries,
|
|
Reason: retryReason,
|
|
Error: err.Error(),
|
|
Backoff: backoff,
|
|
},
|
|
)
|
|
logger.WarnCF("agent", "Transient LLM error, retrying after backoff", map[string]any{
|
|
"error": err.Error(),
|
|
"reason": retryReason,
|
|
"retry": retry,
|
|
"backoff": backoff.String(),
|
|
})
|
|
if sleepErr := sleepWithContext(turnCtx, backoff); sleepErr != nil {
|
|
if ts.hardAbortRequested() {
|
|
_ = ts.requestHardAbort()
|
|
return ControlBreak, nil
|
|
}
|
|
err = sleepErr
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
|
|
if isContextError && retry < maxRetries && !ts.opts.NoHistory {
|
|
al.emitEvent(
|
|
runtimeevents.KindAgentLLMRetry,
|
|
ts.eventMeta("runTurn", "turn.llm.retry"),
|
|
LLMRetryPayload{
|
|
Attempt: retry + 1,
|
|
MaxRetries: maxRetries,
|
|
Reason: "context_limit",
|
|
Error: err.Error(),
|
|
},
|
|
)
|
|
logger.WarnCF(
|
|
"agent",
|
|
"Context window error detected, attempting compression",
|
|
map[string]any{
|
|
"error": err.Error(),
|
|
"retry": retry,
|
|
},
|
|
)
|
|
|
|
if retry == 0 && !constants.IsInternalChannel(ts.channel) {
|
|
al.bus.PublishOutbound(ctx, outboundMessageForTurn(
|
|
ts,
|
|
"Context window exceeded. Compressing history and retrying...",
|
|
))
|
|
}
|
|
|
|
contextualSkills := ts.activeSkills
|
|
if ts.agent.ContextBuilder != nil {
|
|
contextualSkills = ts.agent.ContextBuilder.ResolveActiveSkillsForContext(ts.activeSkills)
|
|
}
|
|
ts.recordSkillContextSnapshot(skillContextTriggerContextRetryRebuild, contextualSkills)
|
|
stableHistory := exec.history
|
|
// The provider can have a smaller effective window than our estimate.
|
|
// Drop a complete historical prefix on retry, even when the estimate fits.
|
|
if len(stableHistory) > 0 {
|
|
cut := findSafeBoundary(stableHistory, len(stableHistory)/2)
|
|
if cut <= 0 || !isSafeBoundary(stableHistory, cut) {
|
|
cut = len(stableHistory)
|
|
}
|
|
stableHistory = stableHistory[cut:]
|
|
al.emitEvent(
|
|
runtimeevents.KindAgentContextCompress,
|
|
ts.eventMeta("runTurn", "turn.context.compress"),
|
|
ContextCompressPayload{
|
|
Reason: ContextCompressReasonRetry,
|
|
DroppedMessages: cut,
|
|
RemainingMessages: len(stableHistory),
|
|
},
|
|
)
|
|
}
|
|
protectedTurnTail := append([]providers.Message(nil), exec.messages[exec.currentTurnStart:]...)
|
|
buildMessages := func(trimmedHistory []providers.Message) []providers.Message {
|
|
fullHistory := append(append([]providers.Message(nil), trimmedHistory...), protectedTurnTail...)
|
|
rebuildPromptReq := promptBuildRequestForTurn(ts, fullHistory, exec.summary, "", nil, p.Cfg)
|
|
rebuildPromptReq.ActiveSkills = append([]string(nil), contextualSkills...)
|
|
rebuilt := ts.agent.ContextBuilder.BuildMessagesFromPrompt(rebuildPromptReq)
|
|
return resolveMediaRefs(
|
|
rebuilt,
|
|
p.MediaStore,
|
|
maxMediaSize,
|
|
len(rebuilt)-len(protectedTurnTail),
|
|
)
|
|
}
|
|
originalHistoryCount := len(exec.history)
|
|
var fit bool
|
|
var trimmedStableHistory []providers.Message
|
|
trimmedStableHistory, exec.callMessages, fit = trimHistoryToFitContextWindow(
|
|
stableHistory,
|
|
func(trimmedHistory []providers.Message) []providers.Message {
|
|
rebuilt := buildMessages(trimmedHistory)
|
|
if exec.gracefulTerminal {
|
|
return append(append([]providers.Message(nil), rebuilt...), ts.interruptHintMessage())
|
|
}
|
|
return rebuilt
|
|
},
|
|
ts.agent.ContextWindow,
|
|
exec.providerToolDefs,
|
|
ts.agent.MaxTokens,
|
|
)
|
|
exec.history = trimmedStableHistory
|
|
exec.messages = buildMessages(trimmedStableHistory)
|
|
exec.currentTurnStart = len(exec.messages) - len(protectedTurnTail)
|
|
if exec.gracefulTerminal {
|
|
msgs := append([]providers.Message(nil), exec.messages...)
|
|
exec.callMessages = append(msgs, ts.interruptHintMessage())
|
|
}
|
|
if dropped := originalHistoryCount - len(exec.history); dropped > 0 {
|
|
logger.WarnCF("agent", "Trimmed rebuilt history after context retry compaction", map[string]any{
|
|
"session_key": ts.sessionKey,
|
|
"retry": retry,
|
|
"dropped_msgs": dropped,
|
|
"remaining_msgs": len(exec.history),
|
|
"context_window": ts.agent.ContextWindow,
|
|
"max_tokens": ts.agent.MaxTokens,
|
|
"still_overlimit": !fit,
|
|
})
|
|
} else if !fit {
|
|
logger.WarnCF("agent", "Context still exceeds budget after retry compaction rebuild", map[string]any{
|
|
"session_key": ts.sessionKey,
|
|
"retry": retry,
|
|
"history_msgs": len(exec.history),
|
|
"protected_turn_msgs": len(protectedTurnTail),
|
|
"context_window": ts.agent.ContextWindow,
|
|
"max_tokens": ts.agent.MaxTokens,
|
|
})
|
|
}
|
|
if !fit {
|
|
err = fmt.Errorf(
|
|
"context window still exceeded after retry compaction; refusing to drop active turn messages: %w",
|
|
err,
|
|
)
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
al.emitEvent(
|
|
runtimeevents.KindAgentError,
|
|
ts.eventMeta("runTurn", "turn.error"),
|
|
ErrorPayload{
|
|
Stage: "llm",
|
|
Message: err.Error(),
|
|
},
|
|
)
|
|
logger.ErrorCF("agent", "LLM call failed",
|
|
map[string]any{
|
|
"agent_id": ts.agent.ID,
|
|
"iteration": iteration,
|
|
"model": exec.llmModel,
|
|
"error": err.Error(),
|
|
})
|
|
return ControlBreak, fmt.Errorf("LLM call failed after retries: %w", err)
|
|
}
|
|
|
|
// AfterLLM hook
|
|
if p.Hooks != nil && !ts.opts.privateMemory {
|
|
llmResp, decision := p.Hooks.AfterLLM(turnCtx, &LLMHookResponse{
|
|
Meta: ts.eventMeta("runTurn", "turn.llm.response"),
|
|
Context: cloneTurnContext(ts.turnCtx),
|
|
Model: exec.llmModel,
|
|
Response: exec.response,
|
|
})
|
|
switch decision.normalizedAction() {
|
|
case HookActionContinue, HookActionModify:
|
|
if llmResp != nil && llmResp.Response != nil {
|
|
exec.response = llmResp.Response
|
|
}
|
|
case HookActionAbortTurn:
|
|
cancelConfiguredStreamingLLM(turnCtx, exec)
|
|
exec.abortedByHook = true
|
|
return ControlBreak, nil
|
|
case HookActionHardAbort:
|
|
cancelConfiguredStreamingLLM(turnCtx, exec)
|
|
_ = ts.requestHardAbort()
|
|
exec.abortedByHardAbort = true
|
|
return ControlBreak, nil
|
|
}
|
|
}
|
|
|
|
// Save finishReason and usage on the turn state. Use ts directly (the
|
|
// authoritative turn state for this call) rather than a context lookup:
|
|
// the raw ctx passed to CallLLM is not seeded with turnState (only turnCtx
|
|
// is), so turnStateFromContext(ctx) returns nil here and silently dropped
|
|
// both the finish reason and the per-turn token usage. ts is also exactly
|
|
// what the streaming publisher reads via GetLastUsage at finalize.
|
|
if ts != nil {
|
|
ts.SetLastFinishReason(exec.response.FinishReason)
|
|
if exec.response.Usage != nil {
|
|
ts.SetLastUsage(exec.response.Usage)
|
|
}
|
|
}
|
|
|
|
if exec.suppressReasoning {
|
|
exec.response.Reasoning = ""
|
|
exec.response.ReasoningContent = ""
|
|
exec.response.ReasoningDetails = nil
|
|
}
|
|
reasoningContent := responseReasoningContent(exec.response)
|
|
if !ts.opts.privateMemory {
|
|
go al.handleReasoning(
|
|
turnCtx,
|
|
reasoningContent,
|
|
ts.channel,
|
|
al.targetReasoningChannelID(ts.channel),
|
|
)
|
|
}
|
|
|
|
al.emitEvent(
|
|
runtimeevents.KindAgentLLMResponse,
|
|
ts.eventMeta("runTurn", "turn.llm.response"),
|
|
LLMResponsePayload{
|
|
ContentLen: len(exec.response.Content),
|
|
ToolCalls: len(exec.response.ToolCalls),
|
|
HasReasoning: exec.response.Reasoning != "" || exec.response.ReasoningContent != "",
|
|
},
|
|
)
|
|
|
|
llmResponseFields := map[string]any{
|
|
"agent_id": ts.agent.ID,
|
|
"iteration": iteration,
|
|
"content_chars": len(exec.response.Content),
|
|
"tool_calls": len(exec.response.ToolCalls),
|
|
"reasoning": exec.response.Reasoning,
|
|
"target_channel": al.targetReasoningChannelID(ts.channel),
|
|
"channel": ts.channel,
|
|
}
|
|
if exec.response.Usage != nil {
|
|
llmResponseFields["prompt_tokens"] = exec.response.Usage.PromptTokens
|
|
llmResponseFields["completion_tokens"] = exec.response.Usage.CompletionTokens
|
|
llmResponseFields["total_tokens"] = exec.response.Usage.TotalTokens
|
|
}
|
|
if ts.opts.privateMemory {
|
|
delete(llmResponseFields, "reasoning")
|
|
}
|
|
logger.DebugCF("agent", "LLM response", llmResponseFields)
|
|
|
|
// No-tool-call path: steering check and direct response
|
|
if len(exec.response.ToolCalls) == 0 || exec.gracefulTerminal {
|
|
responseContent := exec.response.Content
|
|
if responseContent == "" && exec.response.ReasoningContent != "" {
|
|
responseContent = exec.response.ReasoningContent
|
|
}
|
|
if steerMsgs := al.dequeueSteeringMessagesForScope(ts.sessionKey); len(steerMsgs) > 0 {
|
|
cancelConfiguredStreamingLLM(turnCtx, exec)
|
|
logger.InfoCF("agent", "Steering arrived after direct LLM response; continuing turn",
|
|
map[string]any{
|
|
"agent_id": ts.agent.ID,
|
|
"iteration": iteration,
|
|
"steering_count": len(steerMsgs),
|
|
})
|
|
exec.pendingMessages = append(exec.pendingMessages, steerMsgs...)
|
|
return ControlContinue, nil
|
|
}
|
|
|
|
exec.finalContent = responseContent
|
|
logger.InfoCF("agent", "LLM response without tool calls (direct answer)",
|
|
map[string]any{
|
|
"agent_id": ts.agent.ID,
|
|
"iteration": iteration,
|
|
"content_chars": len(exec.finalContent),
|
|
})
|
|
return ControlBreak, nil
|
|
}
|
|
cancelConfiguredStreamingLLM(turnCtx, exec)
|
|
|
|
// Tool-call path: normalize and prepare for tool execution
|
|
exec.normalizedToolCalls = make([]providers.ToolCall, 0, len(exec.response.ToolCalls))
|
|
for _, tc := range exec.response.ToolCalls {
|
|
exec.normalizedToolCalls = append(exec.normalizedToolCalls, providers.NormalizeToolCall(tc))
|
|
}
|
|
|
|
toolNames := make([]string, 0, len(exec.normalizedToolCalls))
|
|
for _, tc := range exec.normalizedToolCalls {
|
|
toolNames = append(toolNames, tc.Name)
|
|
}
|
|
logger.InfoCF("agent", "LLM requested tool calls",
|
|
map[string]any{
|
|
"agent_id": ts.agent.ID,
|
|
"tools": toolNames,
|
|
"count": len(exec.normalizedToolCalls),
|
|
"iteration": iteration,
|
|
})
|
|
|
|
exec.allResponsesHandled = len(exec.normalizedToolCalls) > 0
|
|
assistantMsg := providers.Message{
|
|
Role: "assistant",
|
|
Content: exec.response.Content,
|
|
ModelName: exec.llmModelName,
|
|
ReasoningContent: reasoningContent,
|
|
}
|
|
for _, tc := range exec.normalizedToolCalls {
|
|
argumentsJSON, _ := json.Marshal(tc.Arguments)
|
|
toolFeedbackExplanation := toolFeedbackExplanationForToolCall(
|
|
exec.response,
|
|
tc,
|
|
exec.messages,
|
|
)
|
|
extraContent := tc.ExtraContent
|
|
if strings.TrimSpace(toolFeedbackExplanation) != "" {
|
|
if extraContent == nil {
|
|
extraContent = &providers.ExtraContent{}
|
|
}
|
|
extraContent.ToolFeedbackExplanation = toolFeedbackExplanation
|
|
}
|
|
thoughtSignature := ""
|
|
if tc.Function != nil {
|
|
thoughtSignature = tc.Function.ThoughtSignature
|
|
}
|
|
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
|
|
ID: tc.ID,
|
|
Type: "function",
|
|
Name: tc.Name,
|
|
Function: &providers.FunctionCall{
|
|
Name: tc.Name,
|
|
Arguments: string(argumentsJSON),
|
|
ThoughtSignature: thoughtSignature,
|
|
},
|
|
ExtraContent: extraContent,
|
|
ThoughtSignature: thoughtSignature,
|
|
})
|
|
}
|
|
exec.messages = append(exec.messages, assistantMsg)
|
|
if !ts.opts.NoHistory {
|
|
ts.addTurnMessage(assistantMsg)
|
|
}
|
|
|
|
return ControlToolLoop, nil
|
|
}
|
|
|
|
func restoreToolDefinition(
|
|
current,
|
|
available []providers.ToolDefinition,
|
|
name string,
|
|
) []providers.ToolDefinition {
|
|
for _, tool := range current {
|
|
if strings.EqualFold(tool.Function.Name, name) {
|
|
return current
|
|
}
|
|
}
|
|
for _, tool := range available {
|
|
if strings.EqualFold(tool.Function.Name, name) {
|
|
return append(current, tool)
|
|
}
|
|
}
|
|
return current
|
|
}
|
|
|
|
func (p *Pipeline) applyBeforeLLMModelRewrite(ts *turnState, exec *turnExecution) error {
|
|
if p == nil || ts == nil || ts.agent == nil || exec == nil {
|
|
return nil
|
|
}
|
|
rawModel := strings.TrimSpace(exec.llmModel)
|
|
if rawModel == "" {
|
|
return nil
|
|
}
|
|
|
|
defaultProvider := "openai"
|
|
if p.Cfg != nil {
|
|
if provider := strings.TrimSpace(p.Cfg.Agents.Defaults.Provider); provider != "" {
|
|
defaultProvider = provider
|
|
}
|
|
}
|
|
defaultProvider = effectiveDefaultProvider(defaultProvider)
|
|
candidates := resolveModelCandidates(p.Cfg, defaultProvider, rawModel, nil)
|
|
if len(candidates) == 0 {
|
|
return fmt.Errorf("hook-selected model %q could not be resolved", rawModel)
|
|
}
|
|
candidate := candidates[0]
|
|
provider := ts.agent.CandidateProviders[candidateProviderKey(candidate)]
|
|
if provider == nil {
|
|
if candidate.ConfigKey == "" && candidate.ConfigIndex == 0 {
|
|
if len(exec.activeCandidates) > 0 &&
|
|
providers.NormalizeProvider(exec.activeCandidates[0].Provider) !=
|
|
providers.NormalizeProvider(candidate.Provider) {
|
|
return fmt.Errorf("hook-selected model %q has no configured provider %q", rawModel, candidate.Provider)
|
|
}
|
|
provider = exec.activeProvider
|
|
} else if len(exec.activeCandidates) > 0 &&
|
|
providers.NormalizeProvider(exec.activeCandidates[0].Provider) ==
|
|
providers.NormalizeProvider(candidate.Provider) &&
|
|
candidateCanInheritProvider(p.Cfg, ts.agent.Workspace, candidate) {
|
|
provider = exec.activeProvider
|
|
} else {
|
|
modelCfg, err := resolvedCandidateModelConfig(p.Cfg, candidate, ts.agent.Workspace)
|
|
if err != nil {
|
|
return fmt.Errorf("resolve hook-selected model %q: %w", rawModel, err)
|
|
}
|
|
factory := providers.CreateProviderFromConfig
|
|
if p.al != nil && p.al.providerFactory != nil {
|
|
factory = p.al.providerFactory
|
|
}
|
|
provider, _, err = factory(modelCfg)
|
|
if err != nil {
|
|
return fmt.Errorf("initialize hook-selected model %q: %w", rawModel, err)
|
|
}
|
|
exec.ownedProviders = append(exec.ownedProviders, provider)
|
|
}
|
|
}
|
|
if provider == nil {
|
|
return fmt.Errorf("hook-selected model %q has no active provider", rawModel)
|
|
}
|
|
exec.activeCandidates = candidates
|
|
exec.activeProvider = provider
|
|
exec.activeModel = resolvedCandidateModel(candidates, rawModel)
|
|
exec.llmModel = exec.activeModel
|
|
exec.activeModelConfig = resolveActiveModelConfig(p.Cfg, ts.agent.Workspace, candidates, rawModel, defaultProvider)
|
|
return nil
|
|
}
|
|
|
|
func providerForFallbackCandidate(
|
|
agent *AgentInstance,
|
|
activeProvider providers.LLMProvider,
|
|
activeCandidates []providers.FallbackCandidate,
|
|
candidate providers.FallbackCandidate,
|
|
) (providers.LLMProvider, error) {
|
|
if agent != nil {
|
|
if cp, ok := agent.CandidateProviders[candidateProviderKey(candidate)]; ok && cp != nil {
|
|
return cp, nil
|
|
}
|
|
}
|
|
if len(activeCandidates) > 0 &&
|
|
activeCandidates[0].StableKey() == candidate.StableKey() &&
|
|
activeProvider != nil {
|
|
return activeProvider, nil
|
|
}
|
|
if candidate.ConfigKey != "" || candidate.ConfigIndex > 0 {
|
|
return nil, fmt.Errorf("fallback model %q has no initialized configured provider", candidate.Model)
|
|
}
|
|
if len(activeCandidates) > 0 &&
|
|
providers.NormalizeProvider(activeCandidates[0].Provider) != providers.NormalizeProvider(candidate.Provider) {
|
|
return nil, fmt.Errorf("fallback model %q has no configured provider %q", candidate.Model, candidate.Provider)
|
|
}
|
|
if activeProvider == nil {
|
|
return nil, fmt.Errorf("fallback model %q has no active provider", candidate.Model)
|
|
}
|
|
return activeProvider, nil
|
|
}
|
|
|
|
func transientLLMRetryReason(err error) (string, bool) {
|
|
if err == nil {
|
|
return "", false
|
|
}
|
|
|
|
if failErr := providers.ClassifyError(err, "", ""); failErr != nil {
|
|
switch failErr.Reason {
|
|
case providers.FailoverTimeout:
|
|
if failErr.Status >= 500 {
|
|
return "server_error", true
|
|
}
|
|
return "timeout", true
|
|
case providers.FailoverNetwork:
|
|
return "network", true
|
|
case providers.FailoverRateLimit, providers.FailoverOverloaded:
|
|
return "rate_limit", true
|
|
}
|
|
}
|
|
|
|
errMsg := strings.ToLower(err.Error())
|
|
if errors.Is(err, context.DeadlineExceeded) ||
|
|
strings.Contains(errMsg, "deadline exceeded") ||
|
|
strings.Contains(errMsg, "client.timeout") ||
|
|
strings.Contains(errMsg, "timed out") ||
|
|
strings.Contains(errMsg, "timeout exceeded") {
|
|
return "timeout", true
|
|
}
|
|
|
|
if strings.Contains(errMsg, "connection reset") ||
|
|
strings.Contains(errMsg, "connection refused") ||
|
|
strings.Contains(errMsg, "broken pipe") ||
|
|
strings.Contains(errMsg, "no such host") ||
|
|
strings.Contains(errMsg, "network is unreachable") ||
|
|
strings.Contains(errMsg, "read tcp") ||
|
|
strings.Contains(errMsg, "write tcp") ||
|
|
strings.Contains(errMsg, "eof") {
|
|
return "network", true
|
|
}
|
|
|
|
return "", false
|
|
}
|