// PicoClaw - Ultra-lightweight personal AI agent // Inspired by and based on nanobot: https://github.com/HKUDS/nanobot // License: MIT // // Copyright (c) 2026 PicoClaw contributors package agent import ( "context" "encoding/json" "fmt" "os" "os/exec" "path/filepath" "regexp" "strings" "sync" "sync/atomic" "time" "unicode/utf8" "github.com/sipeed/picoclaw/pkg/bus" "github.com/sipeed/picoclaw/pkg/channels" "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/constants" "github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/metrics" "github.com/sipeed/picoclaw/pkg/providers" "github.com/sipeed/picoclaw/pkg/session" "github.com/sipeed/picoclaw/pkg/state" "github.com/sipeed/picoclaw/pkg/tools" "github.com/sipeed/picoclaw/pkg/utils" ) type ToolCacheEntry struct { Result *tools.ToolResult ExpiresAt time.Time } type AgentLoop struct { bus *bus.MessageBus provider providers.LLMProvider workspace string model string fallbackModels []string contextWindow int // Maximum context window size in tokens maxIterations int sessions *session.SessionManager state *state.Manager contextBuilder *ContextBuilder tools *tools.ToolRegistry running atomic.Bool summarizing sync.Map // Tracks which sessions are currently being summarized channelManager *channels.Manager config *config.Config cache map[string]ToolCacheEntry cacheMutex sync.RWMutex } // processOptions configures how a message is processed type processOptions struct { SessionKey string // Session identifier for history/context Channel string // Target channel for tool execution ChatID string // Target chat ID for tool execution UserMessage string // User message content (may include prefix) DefaultResponse string // Response when LLM returns empty EnableSummary bool // Whether to trigger summarization SendResponse bool // Whether to send response via bus NoHistory bool // If true, don't load session history (for heartbeat) Media []string // Attached media file paths MaxIterations int // Maximum iterations for this specific request } // createToolRegistry creates a tool registry with common tools. // This is shared between main agent and subagents. func createToolRegistry(workspace string, restrict bool, cfg *config.Config, msgBus *bus.MessageBus) *tools.ToolRegistry { registry := tools.NewToolRegistry() // File system tools registry.Register(tools.NewReadFileTool(workspace, restrict)) registry.Register(tools.NewWriteFileTool(workspace, restrict)) registry.Register(tools.NewListDirTool(workspace, restrict)) registry.Register(tools.NewEditFileTool(workspace, restrict)) registry.Register(tools.NewAppendFileTool(workspace, restrict)) // Shell execution registry.Register(tools.NewExecTool(workspace, restrict, cfg.Tools.ResourceLimits)) if searchTool := tools.NewWebSearchTool(tools.WebSearchToolOptions{ BraveAPIKey: cfg.Tools.Web.Brave.APIKey, BraveMaxResults: cfg.Tools.Web.Brave.MaxResults, BraveEnabled: cfg.Tools.Web.Brave.Enabled, DuckDuckGoMaxResults: cfg.Tools.Web.DuckDuckGo.MaxResults, DuckDuckGoEnabled: cfg.Tools.Web.DuckDuckGo.Enabled, }); searchTool != nil { registry.Register(searchTool) } registry.Register(tools.NewWebFetchTool(50000)) // Hardware tools (I2C, SPI) - Linux only, returns error on other platforms registry.Register(tools.NewI2CTool()) registry.Register(tools.NewSPITool()) // Message tool - available to both agent and subagent // Subagent uses it to communicate directly with user messageTool := tools.NewMessageTool() messageTool.SetSendCallback(func(channel, chatID, content string) error { var audioPath string // Check for [NO_VOICE] prefix to skip TTS if strings.HasPrefix(strings.TrimSpace(content), "[NO_VOICE]") { content = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(content), "[NO_VOICE]")) } else { // Try to generate audio if path, err := generateTTS(workspace, content); err == nil { audioPath = path } else { logger.DebugCF("agent", "TTS generation skipped/failed (message tool)", map[string]interface{}{ "error": err.Error(), }) } } msgBus.PublishOutbound(bus.OutboundMessage{ Channel: channel, ChatID: chatID, Content: content, AudioPath: audioPath, }) return nil }) registry.Register(messageTool) return registry } func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers.LLMProvider) *AgentLoop { workspace := cfg.WorkspacePath() os.MkdirAll(workspace, 0755) restrict := cfg.Agents.Defaults.RestrictToWorkspace // Create tool registry for main agent toolsRegistry := createToolRegistry(workspace, restrict, cfg, msgBus) // Create subagent manager with its own tool registry subagentManager := tools.NewSubagentManager(provider, cfg.Agents.Defaults.Model, workspace, msgBus) subagentTools := createToolRegistry(workspace, restrict, cfg, msgBus) // Subagent doesn't need spawn/subagent tools to avoid recursion subagentManager.SetTools(subagentTools) // Register spawn tool (for main agent) spawnTool := tools.NewSpawnTool(subagentManager) toolsRegistry.Register(spawnTool) // Register subagent tool (synchronous execution) subagentTool := tools.NewSubagentTool(subagentManager) toolsRegistry.Register(subagentTool) sessionsManager := session.NewSessionManager(filepath.Join(workspace, "sessions")) // Create state manager for atomic state persistence stateManager := state.NewManager(workspace) // Create context builder and set tools registry contextBuilder := NewContextBuilder(workspace) contextBuilder.SetToolsRegistry(toolsRegistry) return &AgentLoop{ config: cfg, bus: msgBus, provider: provider, workspace: workspace, model: cfg.Agents.Defaults.Model, fallbackModels: cfg.Agents.Defaults.FallbackModels, contextWindow: cfg.Agents.Defaults.MaxTokens, // Restore context window for summarization maxIterations: cfg.Agents.Defaults.MaxToolIterations, sessions: sessionsManager, state: stateManager, contextBuilder: contextBuilder, tools: toolsRegistry, summarizing: sync.Map{}, cache: make(map[string]ToolCacheEntry), } } func generateTTS(workspace, text string) (string, error) { // Clean text for TTS (remove markdown symbols) // 1. Remove code blocks reCode := regexp.MustCompile("```[\\s\\S]*?```") cleanText := reCode.ReplaceAllString(text, "code block") // 2. Remove inline code reInline := regexp.MustCompile("`[^`]*`") cleanText = reInline.ReplaceAllString(cleanText, "code") // 3. Remove links [text](url) -> text reLink := regexp.MustCompile(`\[([^\]]+)\]\([^\)]+\)`) cleanText = reLink.ReplaceAllString(cleanText, "$1") // 4. Remove bold/italic/header markers (*, _, #) reSymbols := regexp.MustCompile(`[\*\_#]`) cleanText = reSymbols.ReplaceAllString(cleanText, "") // Search in workspace/skills and system skills skillPaths := []string{ filepath.Join(workspace, "skills", "voice-tts"), "/picoclaw/skills/voice-tts", "workspace/skills/voice-tts", } var scriptPath string for _, p := range skillPaths { sp := filepath.Join(p, "scripts", "speak.py") if _, err := os.Stat(sp); err == nil { scriptPath = sp break } } if scriptPath == "" { return "", fmt.Errorf("voice-tts skill not found") } // Generate temp file outFile := filepath.Join(os.TempDir(), fmt.Sprintf("tts_%d.mp3", time.Now().UnixNano())) // Execute script // python3 script.py "text" --file outFile --no-play cmd := exec.Command("python3", scriptPath, cleanText, "--file", outFile, "--no-play") if out, err := cmd.CombinedOutput(); err != nil { return "", fmt.Errorf("tts execution failed: %v, output: %s", err, out) } return outFile, nil } func (al *AgentLoop) Run(ctx context.Context) error { al.running.Store(true) for al.running.Load() { select { case <-ctx.Done(): return nil default: msg, ok := al.bus.ConsumeInbound(ctx) if !ok { continue } response, err := al.processMessage(ctx, msg) if err != nil { response = fmt.Sprintf("Error processing message: %v", err) } if response != "" { // Check if the message tool already sent a response during this round. // If so, skip publishing to avoid duplicate messages to the user. alreadySent := false if tool, ok := al.tools.Get("message"); ok { if mt, ok := tool.(*tools.MessageTool); ok { alreadySent = mt.HasSentInRound() } } if !alreadySent { var audioPath string // Try to generate audio if path, err := generateTTS(al.workspace, response); err == nil { audioPath = path } else { // Debug log if TTS fails (e.g. skill not found) logger.DebugCF("agent", "TTS generation skipped/failed", map[string]interface{}{ "error": err.Error(), }) } al.bus.PublishOutbound(bus.OutboundMessage{ Channel: msg.Channel, ChatID: msg.ChatID, Content: response, AudioPath: audioPath, }) } } } } return nil } func (al *AgentLoop) Stop() { al.running.Store(false) } func (al *AgentLoop) RegisterTool(tool tools.Tool) { al.tools.Register(tool) } func (al *AgentLoop) SetChannelManager(cm *channels.Manager) { al.channelManager = cm } // RecordLastChannel records the last active channel for this workspace. // This uses the atomic state save mechanism to prevent data loss on crash. func (al *AgentLoop) RecordLastChannel(channel string) error { return al.state.SetLastChannel(channel) } // RecordLastChatID records the last active chat ID for this workspace. // This uses the atomic state save mechanism to prevent data loss on crash. func (al *AgentLoop) RecordLastChatID(chatID string) error { return al.state.SetLastChatID(chatID) } func (al *AgentLoop) ProcessDirect(ctx context.Context, content, sessionKey string) (string, error) { return al.ProcessDirectWithChannel(ctx, content, sessionKey, "cli", "direct") } func (al *AgentLoop) ProcessDirectWithChannel(ctx context.Context, content, sessionKey, channel, chatID string) (string, error) { msg := bus.InboundMessage{ Channel: channel, SenderID: "cron", ChatID: chatID, Content: content, SessionKey: sessionKey, } return al.processMessage(ctx, msg) } // ProcessHeartbeat processes a heartbeat request without session history. // Each heartbeat is independent and doesn't accumulate context. func (al *AgentLoop) ProcessHeartbeat(ctx context.Context, content, channel, chatID string) (string, error) { return al.runAgentLoop(ctx, processOptions{ SessionKey: "heartbeat", Channel: channel, ChatID: chatID, UserMessage: content, DefaultResponse: "Saya telah selesai memproses pesan, namun tidak ada respons tambahan.", EnableSummary: false, SendResponse: false, NoHistory: true, // Don't load session history for heartbeat }) } func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage) (string, error) { // Add message preview to log (show full content for error messages) var logContent string if strings.Contains(msg.Content, "Error:") || strings.Contains(msg.Content, "error") { logContent = msg.Content // Full content for errors } else { logContent = utils.Truncate(msg.Content, 80) } logger.InfoCF("agent", fmt.Sprintf("Processing message from %s:%s: %s", msg.Channel, msg.SenderID, logContent), map[string]interface{}{ "channel": msg.Channel, "chat_id": msg.ChatID, "sender_id": msg.SenderID, "session_key": msg.SessionKey, }) // Route system messages to processSystemMessage if msg.Channel == "system" { return al.processSystemMessage(ctx, msg) } // Check for commands if response, handled := al.handleCommand(ctx, msg); handled { return response, nil } // Process as user message return al.runAgentLoop(ctx, processOptions{ SessionKey: msg.SessionKey, Channel: msg.Channel, ChatID: msg.ChatID, UserMessage: msg.Content, DefaultResponse: "Saya telah selesai memproses pesan, namun tidak ada respons tambahan.", EnableSummary: true, SendResponse: false, Media: msg.Media, }) } func (al *AgentLoop) processSystemMessage(ctx context.Context, msg bus.InboundMessage) (string, error) { // Verify this is a system message if msg.Channel != "system" { return "", fmt.Errorf("processSystemMessage called with non-system message channel: %s", msg.Channel) } logger.InfoCF("agent", "Processing system message", map[string]interface{}{ "sender_id": msg.SenderID, "chat_id": msg.ChatID, }) // Parse origin channel from chat_id (format: "channel:chat_id") var originChannel string if idx := strings.Index(msg.ChatID, ":"); idx > 0 { originChannel = msg.ChatID[:idx] } else { // Fallback originChannel = "cli" } // Extract subagent result from message content // Format: "Task 'label' completed.\n\nResult:\n" content := msg.Content if idx := strings.Index(content, "Result:\n"); idx >= 0 { content = content[idx+8:] // Extract just the result part } // Skip internal channels - only log, don't send to user if constants.IsInternalChannel(originChannel) { logger.InfoCF("agent", "Subagent completed (internal channel)", map[string]interface{}{ "sender_id": msg.SenderID, "content_len": len(content), "channel": originChannel, }) return "", nil } // Agent acts as dispatcher only - subagent handles user interaction via message tool // Don't forward result here, subagent should use message tool to communicate with user logger.InfoCF("agent", "Subagent completed", map[string]interface{}{ "sender_id": msg.SenderID, "channel": originChannel, "content_len": len(content), }) // Agent only logs, does not respond to user return "", nil } // runAgentLoop is the core message processing logic. // It handles context building, LLM calls, tool execution, and response handling. func (al *AgentLoop) runAgentLoop(ctx context.Context, opts processOptions) (string, error) { // 0. Record last channel for heartbeat notifications (skip internal channels) if opts.Channel != "" && opts.ChatID != "" { // Don't record internal channels (cli, system, subagent) if !constants.IsInternalChannel(opts.Channel) { channelKey := fmt.Sprintf("%s:%s", opts.Channel, opts.ChatID) if err := al.RecordLastChannel(channelKey); err != nil { logger.WarnCF("agent", "Failed to record last channel: %v", map[string]interface{}{"error": err.Error()}) } } } // 1. Update tool contexts al.updateToolContexts(opts.Channel, opts.ChatID) // Determine adaptive iteration limit if opts.MaxIterations == 0 { // Heuristic: // - Short messages (< 50 chars): likely simple queries -> 10 iterations // - Medium messages (< 200 chars): standard tasks -> 25 iterations // - Long messages or complex keywords: complex tasks -> al.maxIterations (usually 50) msgLen := len(opts.UserMessage) opts.MaxIterations = 25 // Default medium if msgLen < 50 { opts.MaxIterations = 10 } else if msgLen >= 200 { opts.MaxIterations = al.maxIterations } // Check for complexity keywords keywords := []string{"plan", "analyze", "research", "scan", "crawl", "recursive", "complex"} lowerMsg := strings.ToLower(opts.UserMessage) for _, kw := range keywords { if strings.Contains(lowerMsg, kw) { opts.MaxIterations = al.maxIterations + 10 // Boost for complex tasks break } } // Ensure we don't exceed a hard safety limit (e.g., 100) unless configured if opts.MaxIterations > 100 { opts.MaxIterations = 100 } } logger.InfoCF("agent", "Adaptive iteration limit set", map[string]interface{}{ "limit": opts.MaxIterations, "msg_len": len(opts.UserMessage), }) // 2. Build messages (skip history for heartbeat) var history []providers.Message var summary string if !opts.NoHistory { history = al.sessions.GetHistory(opts.SessionKey) summary = al.sessions.GetSummary(opts.SessionKey) } messages := al.contextBuilder.BuildMessages( history, summary, opts.UserMessage, opts.Media, opts.Channel, opts.ChatID, ) // 3. Save user message to session al.sessions.AddMessage(opts.SessionKey, "user", opts.UserMessage) // 4. Run LLM iteration loop finalContent, iteration, err := al.runLLMIteration(ctx, messages, opts) if err != nil { return "", err } // If last tool had ForUser content and we already sent it, we might not need to send final response // This is controlled by the tool's Silent flag and ForUser content // 5. Handle empty response if finalContent == "" { // Try to construct a meaningful response from the last action history := al.sessions.GetHistory(opts.SessionKey) if len(history) > 0 { lastMsg := history[len(history)-1] if lastMsg.Role == "tool" { // Find tool name from previous assistant message var toolName string if len(history) > 1 { prevMsg := history[len(history)-2] if prevMsg.Role == "assistant" { for _, tc := range prevMsg.ToolCalls { if tc.ID == lastMsg.ToolCallID { toolName = tc.Name break } } } } contentPreview := utils.Truncate(lastMsg.Content, 200) if toolName != "" { finalContent = fmt.Sprintf("Saya telah selesai menjalankan **%s**.\n\nHasil:\n%s", toolName, contentPreview) } else { finalContent = fmt.Sprintf("Proses selesai.\n\nHasil:\n%s", contentPreview) } } } // Fallback to default if still empty if finalContent == "" { finalContent = opts.DefaultResponse } } // 6. Save final assistant message to session al.sessions.AddMessage(opts.SessionKey, "assistant", finalContent) al.sessions.Save(opts.SessionKey) // 7. Optional: summarization if opts.EnableSummary { al.maybeSummarize(opts.SessionKey, opts.Channel, opts.ChatID) } // 8. Optional: send response via bus if opts.SendResponse { al.bus.PublishOutbound(bus.OutboundMessage{ Channel: opts.Channel, ChatID: opts.ChatID, Content: finalContent, }) } // 9. Log response responsePreview := utils.Truncate(finalContent, 120) logger.InfoCF("agent", fmt.Sprintf("Response: %s", responsePreview), map[string]interface{}{ "session_key": opts.SessionKey, "iterations": iteration, "final_length": len(finalContent), }) return finalContent, nil } // runLLMIteration executes the LLM call loop with tool handling. // Returns the final content, iteration count, and any error. func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.Message, opts processOptions) (string, int, error) { iteration := 0 var finalContent string // Use config for dynamic values if not overridden maxIterations := opts.MaxIterations if maxIterations <= 0 { if al.config != nil { maxIterations = al.config.GetAgentDefaults().MaxToolIterations } if maxIterations <= 0 { maxIterations = al.maxIterations } } for iteration < maxIterations { iteration++ metrics.AgentIterations.Inc() // Adaptive iteration limit: // If we are nearing the limit but the agent is still using tools effectively (not looping), // we might want to extend the limit slightly. // For now, let's implement a simple check: if we hit the limit, but the last action was a successful tool call // that produced new information (not an error), we could allow 1-2 more iterations. // However, to keep it simple and safe, we'll just log a warning if we hit the limit. if iteration == maxIterations { logger.WarnCF("agent", "Max iterations reached", map[string]interface{}{ "limit": maxIterations, }) } logger.DebugCF("agent", "LLM iteration", map[string]interface{}{ "iteration": iteration, "max": maxIterations, }) // Build tool definitions providerToolDefs := al.tools.ToProviderDefs() // Log LLM request details logger.DebugCF("agent", "LLM request", map[string]interface{}{ "iteration": iteration, "model": al.model, "messages_count": len(messages), "tools_count": len(providerToolDefs), "max_tokens": 8192, "temperature": 0.7, "system_prompt_len": len(messages[0].Content), }) // Log full messages (detailed) logger.DebugCF("agent", "Full LLM request", map[string]interface{}{ "iteration": iteration, "messages_json": formatMessagesForLog(messages), "tools_json": formatToolsForLog(providerToolDefs), }) var response *providers.LLMResponse var err error // Create list of models to try: [current_model, fallback_1, fallback_2, ...] modelsToTry := []string{al.model} if len(al.fallbackModels) > 0 { modelsToTry = append(modelsToTry, al.fallbackModels...) } else { // Legacy/Fallback hardcoded if config is empty backupModels := []string{ "arcee-ai/trinity-large-preview:free", } modelsToTry = append(modelsToTry, backupModels...) } modelSuccess := false for modelIdx, modelToUse := range modelsToTry { // Retry loop for context/token errors maxRetries := 2 for retry := 0; retry <= maxRetries; retry++ { response, err = al.provider.Chat(ctx, messages, providerToolDefs, modelToUse, map[string]interface{}{ "max_tokens": 8192, "temperature": 0.7, }) if err == nil { modelSuccess = true metrics.LLMRequests.WithLabelValues(modelToUse, "success").Inc() break // Success } metrics.LLMRequests.WithLabelValues(modelToUse, "error").Inc() errMsg := strings.ToLower(err.Error()) // Check for context window errors (provider specific, but usually contain "token" or "invalid") isContextError := strings.Contains(errMsg, "token") || strings.Contains(errMsg, "context") || strings.Contains(errMsg, "invalidparameter") || strings.Contains(errMsg, "length") // Check for transient errors (network, server error) isTransientError := strings.Contains(errMsg, "timeout") || strings.Contains(errMsg, "connection") || strings.Contains(errMsg, "500") || strings.Contains(errMsg, "502") || strings.Contains(errMsg, "503") || strings.Contains(errMsg, "504") || strings.Contains(errMsg, "rate limit") || strings.Contains(errMsg, "429") if (isContextError || isTransientError) && retry < maxRetries { logger.WarnCF("agent", "Recoverable error detected, retrying with backoff", map[string]interface{}{ "error": err.Error(), "retry": retry, "model": modelToUse, "is_context": isContextError, "is_transient": isTransientError, }) // Exponential backoff: 1s, 2s, 4s backoff := time.Duration(1< 2 { // Drop a few oldest messages from the middle (preserve system prompt and last few) // messages[0] is system. // Remove messages[1] and messages[2] if available if len(messages) > 4 { // Remove 2 messages (user+assistant pair usually) newMessages := make([]providers.Message, 0, len(messages)-2) newMessages = append(newMessages, messages[0]) newMessages = append(newMessages, messages[3:]...) messages = newMessages logger.WarnCF("agent", "Dropped 2 oldest messages for retry", nil) } } } } else { // Non-recoverable or max retries reached break } } if modelSuccess { if modelToUse != al.model { logger.WarnCF("agent", "Switched to fallback model", map[string]interface{}{ "original": al.model, "current": modelToUse, }) } break } logger.WarnCF("agent", "Model failed, checking fallbacks", map[string]interface{}{ "model": modelToUse, "error": err.Error(), "has_more": modelIdx < len(modelsToTry)-1, }) } if !modelSuccess { logger.ErrorCF("agent", "LLM call failed after all models tried", map[string]interface{}{ "error": err.Error(), }) return "", iteration, fmt.Errorf("LLM call failed after all models tried: %w", err) } // Check if no tool calls - we're done if len(response.ToolCalls) == 0 { finalContent = response.Content logger.InfoCF("agent", "LLM response without tool calls (direct answer)", map[string]interface{}{ "iteration": iteration, "content_chars": len(finalContent), }) break } // Log tool calls toolNames := make([]string, 0, len(response.ToolCalls)) for _, tc := range response.ToolCalls { toolNames = append(toolNames, tc.Name) } logger.InfoCF("agent", "LLM requested tool calls", map[string]interface{}{ "tools": toolNames, "count": len(response.ToolCalls), "iteration": iteration, }) // Build assistant message with tool calls assistantMsg := providers.Message{ Role: "assistant", Content: response.Content, } for _, tc := range response.ToolCalls { argumentsJSON, _ := json.Marshal(tc.Arguments) assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{ ID: tc.ID, Type: "function", Function: &providers.FunctionCall{ Name: tc.Name, Arguments: string(argumentsJSON), }, }) } messages = append(messages, assistantMsg) // Save assistant message with tool calls to session al.sessions.AddFullMessage(opts.SessionKey, assistantMsg) // Execute tool calls // We execute read-only tools in parallel, and side-effect tools sequentially to preserve causality. // Results must be appended to messages in the original order. results := make([]providers.Message, len(response.ToolCalls)) executed := make([]bool, len(response.ToolCalls)) // Helper to check if tool is safe for parallel execution isSafe := func(name string) bool { switch name { case "read_file", "list_dir", "web_fetch", "web_search", "weather": return true default: return false } } // Helper to check if tool result is cacheable isCacheable := func(name string) bool { switch name { case "read_file", "list_dir": return true default: return false } } i := 0 for i < len(response.ToolCalls) { // Identify a batch of safe tools start := i end := i if isSafe(response.ToolCalls[i].Name) { // Extend batch while tools are safe for end < len(response.ToolCalls) && isSafe(response.ToolCalls[end].Name) { end++ } } else { // Single unsafe tool end = i + 1 } // Execute batch var wg sync.WaitGroup for j := start; j < end; j++ { wg.Add(1) go func(idx int, tc providers.ToolCall) { defer wg.Done() // Log tool call with arguments preview argsJSON, _ := json.Marshal(tc.Arguments) argsPreview := utils.Truncate(string(argsJSON), 200) logger.InfoCF("agent", fmt.Sprintf("Tool call: %s(%s)", tc.Name, argsPreview), map[string]interface{}{ "tool": tc.Name, "iteration": iteration, }) var toolResult *tools.ToolResult var cacheKey string // Check cache for deterministic tools if isCacheable(tc.Name) { cacheKey = tc.Name + ":" + string(argsJSON) al.cacheMutex.RLock() if entry, ok := al.cache[cacheKey]; ok && time.Now().Before(entry.ExpiresAt) { toolResult = entry.Result logger.DebugCF("agent", "Cache hit for tool", map[string]interface{}{"tool": tc.Name}) metrics.ToolCacheHits.WithLabelValues(tc.Name).Inc() } else { metrics.ToolCacheMisses.WithLabelValues(tc.Name).Inc() } al.cacheMutex.RUnlock() } if toolResult == nil { // Create async callback asyncCallback := func(callbackCtx context.Context, result *tools.ToolResult) { if !result.Silent && result.ForUser != "" { logger.InfoCF("agent", "Async tool completed, agent will handle notification", map[string]interface{}{ "tool": tc.Name, "content_len": len(result.ForUser), }) } } // Retry logic for tool execution maxRetries := 2 for retry := 0; retry <= maxRetries; retry++ { toolResult = al.tools.ExecuteWithContext(ctx, tc.Name, tc.Arguments, opts.Channel, opts.ChatID, asyncCallback) if toolResult.Err == nil { break } // Check for transient errors errMsg := strings.ToLower(toolResult.Err.Error()) isTransient := strings.Contains(errMsg, "timeout") || strings.Contains(errMsg, "connection") || strings.Contains(errMsg, "rate limit") || strings.Contains(errMsg, "429") || strings.Contains(errMsg, "500") || strings.Contains(errMsg, "502") || strings.Contains(errMsg, "503") || strings.Contains(errMsg, "504") || strings.Contains(errMsg, "temporary") if !isTransient { break } if retry < maxRetries { logger.WarnCF("agent", "Tool execution failed, retrying", map[string]interface{}{ "tool": tc.Name, "error": toolResult.Err.Error(), "retry": retry, }) time.Sleep(time.Duration(1<= maxIterations { hardLimit := 100 // Default safety hard limit // If config has a higher limit, respect that + buffer if al.config != nil { configLimit := al.config.GetAgentDefaults().MaxToolIterations if configLimit > hardLimit { hardLimit = configLimit + 20 } } if maxIterations < hardLimit { extension := 10 newLimit := maxIterations + extension if newLimit > hardLimit { newLimit = hardLimit } if newLimit > maxIterations { logger.WarnCF("agent", "Auto-extending iteration limit", map[string]interface{}{ "old_limit": maxIterations, "new_limit": newLimit, "reason": "task ongoing", }) maxIterations = newLimit // Notify user slightly if it's a significant extension if maxIterations > 30 && !constants.IsInternalChannel(opts.Channel) && opts.SendResponse { // Only send a subtle notification once or if really long if maxIterations == 40 || maxIterations == 70 { al.bus.PublishOutbound(bus.OutboundMessage{ Channel: opts.Channel, ChatID: opts.ChatID, Content: fmt.Sprintf("๐Ÿ”„ Memperpanjang waktu proses (%d langkah)...", maxIterations), }) } } } } } } return finalContent, iteration, nil } // updateToolContexts updates the context for tools that need channel/chatID info. func (al *AgentLoop) updateToolContexts(channel, chatID string) { // Use ContextualTool interface instead of type assertions if tool, ok := al.tools.Get("message"); ok { if mt, ok := tool.(tools.ContextualTool); ok { mt.SetContext(channel, chatID) } } if tool, ok := al.tools.Get("spawn"); ok { if st, ok := tool.(tools.ContextualTool); ok { st.SetContext(channel, chatID) } } if tool, ok := al.tools.Get("subagent"); ok { if st, ok := tool.(tools.ContextualTool); ok { st.SetContext(channel, chatID) } } } // maybeSummarize triggers summarization if the session history exceeds thresholds. func (al *AgentLoop) maybeSummarize(sessionKey, channel, chatID string) { newHistory := al.sessions.GetHistory(sessionKey) tokenEstimate := al.estimateTokens(newHistory) threshold := al.contextWindow * 75 / 100 if len(newHistory) > 20 || tokenEstimate > threshold { if _, loading := al.summarizing.LoadOrStore(sessionKey, true); !loading { go func() { defer al.summarizing.Delete(sessionKey) // Notify user about optimization if not an internal channel if !constants.IsInternalChannel(channel) { al.bus.PublishOutbound(bus.OutboundMessage{ Channel: channel, ChatID: chatID, Content: "๐Ÿงน Sedang merapikan ingatan percakapan untuk menjaga performa...", }) } al.summarizeSession(sessionKey) }() } } } // forceCompression aggressively reduces context when the limit is hit. // It drops the oldest 50% of messages (keeping system prompt and last user message). func (al *AgentLoop) forceCompression(sessionKey string) { history := al.sessions.GetHistory(sessionKey) if len(history) <= 4 { return } // Keep system prompt (usually [0]) and the very last message (user's trigger) // We want to drop the oldest half of the *conversation* // Assuming [0] is system, [1:] is conversation conversation := history[1 : len(history)-1] if len(conversation) == 0 { return } // Helper to find the mid-point of the conversation mid := len(conversation) / 2 // New history structure: // 1. System Prompt // 2. [Summary of dropped part] - synthesized // 3. Second half of conversation // 4. Last message // Simplified approach for emergency: Drop first half of conversation // and rely on existing summary if present, or create a placeholder. droppedCount := mid keptConversation := conversation[mid:] newHistory := make([]providers.Message, 0) newHistory = append(newHistory, history[0]) // System prompt // Add a note about compression compressionNote := fmt.Sprintf("[System: Emergency compression dropped %d oldest messages due to context limit]", droppedCount) // If there was an existing summary, we might lose it if it was in the dropped part (which is just messages). // The summary is stored separately in session.Summary, so it persists! // We just need to ensure the user knows there's a gap. // We only modify the messages list here newHistory = append(newHistory, providers.Message{ Role: "system", Content: compressionNote, }) newHistory = append(newHistory, keptConversation...) newHistory = append(newHistory, history[len(history)-1]) // Last message // Update session al.sessions.SetHistory(sessionKey, newHistory) al.sessions.Save(sessionKey) logger.WarnCF("agent", "Forced compression executed", map[string]interface{}{ "session_key": sessionKey, "dropped_msgs": droppedCount, "new_count": len(newHistory), }) } // GetStartupInfo returns information about loaded tools and skills for logging. func (al *AgentLoop) GetStartupInfo() map[string]interface{} { info := make(map[string]interface{}) // Tools info tools := al.tools.List() info["tools"] = map[string]interface{}{ "count": len(tools), "names": tools, } // Skills info info["skills"] = al.contextBuilder.GetSkillsInfo() return info } // formatMessagesForLog formats messages for logging func formatMessagesForLog(messages []providers.Message) string { if len(messages) == 0 { return "[]" } buf := utils.GetBuffer() defer utils.PutBuffer(buf) buf.WriteString("[\n") for i, msg := range messages { buf.WriteString(fmt.Sprintf(" [%d] Role: %s\n", i, msg.Role)) if len(msg.ToolCalls) > 0 { buf.WriteString(" ToolCalls:\n") for _, tc := range msg.ToolCalls { buf.WriteString(fmt.Sprintf(" - ID: %s, Type: %s, Name: %s\n", tc.ID, tc.Type, tc.Name)) if tc.Function != nil { buf.WriteString(fmt.Sprintf(" Arguments: %s\n", utils.Truncate(tc.Function.Arguments, 200))) } } } if msg.Content != "" { content := utils.Truncate(msg.Content, 200) buf.WriteString(fmt.Sprintf(" Content: %s\n", content)) } if msg.ToolCallID != "" { buf.WriteString(fmt.Sprintf(" ToolCallID: %s\n", msg.ToolCallID)) } buf.WriteString("\n") } buf.WriteString("]") return buf.String() } // formatToolsForLog formats tool definitions for logging func formatToolsForLog(tools []providers.ToolDefinition) string { if len(tools) == 0 { return "[]" } buf := utils.GetBuffer() defer utils.PutBuffer(buf) buf.WriteString("[\n") for i, tool := range tools { buf.WriteString(fmt.Sprintf(" [%d] Type: %s, Name: %s\n", i, tool.Type, tool.Function.Name)) buf.WriteString(fmt.Sprintf(" Description: %s\n", tool.Function.Description)) if len(tool.Function.Parameters) > 0 { buf.WriteString(fmt.Sprintf(" Parameters: %s\n", utils.Truncate(fmt.Sprintf("%v", tool.Function.Parameters), 200))) } } buf.WriteString("]") return buf.String() } // summarizeSession summarizes the conversation history for a session. func (al *AgentLoop) summarizeSession(sessionKey string) { ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) defer cancel() history := al.sessions.GetHistory(sessionKey) summary := al.sessions.GetSummary(sessionKey) // Keep last 4 messages for continuity if len(history) <= 4 { return } toSummarize := history[:len(history)-4] // Oversized Message Guard // Skip messages larger than 50% of context window to prevent summarizer overflow maxMessageTokens := al.contextWindow / 2 validMessages := make([]providers.Message, 0) omitted := false for _, m := range toSummarize { if m.Role != "user" && m.Role != "assistant" { continue } // Estimate tokens for this message msgTokens := len(m.Content) / 2 // Use safer estimate here too (2.5 -> 2 for integer division safety) if msgTokens > maxMessageTokens { omitted = true continue } validMessages = append(validMessages, m) } if len(validMessages) == 0 { return } // Multi-Part Summarization // Split into two parts if history is significant var finalSummary string if len(validMessages) > 10 { mid := len(validMessages) / 2 part1 := validMessages[:mid] part2 := validMessages[mid:] s1, _ := al.summarizeBatch(ctx, part1, "") s2, _ := al.summarizeBatch(ctx, part2, "") // Merge them mergePrompt := fmt.Sprintf("Merge these two conversation summaries into one cohesive summary:\n\n1: %s\n\n2: %s", s1, s2) resp, err := al.provider.Chat(ctx, []providers.Message{{Role: "user", Content: mergePrompt}}, nil, al.model, map[string]interface{}{ "max_tokens": 1024, "temperature": 0.3, }) if err == nil { finalSummary = resp.Content } else { finalSummary = s1 + " " + s2 } } else { finalSummary, _ = al.summarizeBatch(ctx, validMessages, summary) } if omitted && finalSummary != "" { finalSummary += "\n[Note: Some oversized messages were omitted from this summary for efficiency.]" } if finalSummary != "" { al.sessions.SetSummary(sessionKey, finalSummary) al.sessions.TruncateHistory(sessionKey, 4) al.sessions.Save(sessionKey) } } // summarizeBatch summarizes a batch of messages. func (al *AgentLoop) summarizeBatch(ctx context.Context, batch []providers.Message, existingSummary string) (string, error) { prompt := "Provide a concise summary of this conversation segment, preserving core context and key points.\n" if existingSummary != "" { prompt += "Existing context: " + existingSummary + "\n" } prompt += "\nCONVERSATION:\n" for _, m := range batch { prompt += fmt.Sprintf("%s: %s\n", m.Role, m.Content) } response, err := al.provider.Chat(ctx, []providers.Message{{Role: "user", Content: prompt}}, nil, al.model, map[string]interface{}{ "max_tokens": 1024, "temperature": 0.3, }) if err != nil { return "", err } return response.Content, nil } // estimateTokens estimates the number of tokens in a message list. // Uses a safe heuristic of 2.5 characters per token to account for CJK and other // overheads better than the previous 3 chars/token. func (al *AgentLoop) estimateTokens(messages []providers.Message) int { totalChars := 0 for _, m := range messages { totalChars += utf8.RuneCountInString(m.Content) } // 2.5 chars per token = totalChars * 2 / 5 return totalChars * 2 / 5 } func (al *AgentLoop) handleCommand(ctx context.Context, msg bus.InboundMessage) (string, bool) { content := strings.TrimSpace(msg.Content) if !strings.HasPrefix(content, "/") { return "", false } parts := strings.Fields(content) if len(parts) == 0 { return "", false } cmd := parts[0] args := parts[1:] switch cmd { case "/show": if len(args) < 1 { return "Usage: /show [model|channel]", true } switch args[0] { case "model": return fmt.Sprintf("Current model: %s", al.model), true case "channel": return fmt.Sprintf("Current channel: %s", msg.Channel), true default: return fmt.Sprintf("Unknown show target: %s", args[0]), true } case "/list": if len(args) < 1 { return "Usage: /list [models|channels]", true } switch args[0] { case "models": // TODO: Fetch available models dynamically if possible return "Available models: glm-4.7, claude-3-5-sonnet, gpt-4o (configured in config.json/env)", true case "channels": if al.channelManager == nil { return "Channel manager not initialized", true } channels := al.channelManager.GetEnabledChannels() if len(channels) == 0 { return "No channels enabled", true } return fmt.Sprintf("Enabled channels: %s", strings.Join(channels, ", ")), true default: return fmt.Sprintf("Unknown list target: %s", args[0]), true } case "/switch": if len(args) < 3 || args[1] != "to" { return "Usage: /switch [model|channel] to ", true } target := args[0] value := args[2] switch target { case "model": oldModel := al.model al.model = value return fmt.Sprintf("Switched model from %s to %s", oldModel, value), true case "channel": // This changes the 'default' channel for some operations, or effectively redirects output? // For now, let's just validate if the channel exists if al.channelManager == nil { return "Channel manager not initialized", true } if _, exists := al.channelManager.GetChannel(value); !exists && value != "cli" { return fmt.Sprintf("Channel '%s' not found or not enabled", value), true } // If message came from CLI, maybe we want to redirect CLI output to this channel? // That would require state persistence about "redirected channel" // For now, just acknowledged. return fmt.Sprintf("Switched target channel to %s (Note: this currently only validates existence)", value), true default: return fmt.Sprintf("Unknown switch target: %s", target), true } } return "", false }