MCPcopy Create free account
hub / github.com/diillson/chatcli / Run

Method Run

cli/agent_mode.go:661–917  ·  view source on GitHub ↗

Run inicia o modo agente com uma consulta do usuário, utilizando um loop de Raciocínio-Ação (ReAct). Agora aceita systemPromptOverride para definir personas específicas (ex: Coder).

(ctx context.Context, query string, additionalContext string, systemPromptOverride string)

Source from the content-addressed store, hash-verified

659// Run inicia o modo agente com uma consulta do usuário, utilizando um loop de Raciocínio-Ação (ReAct).
660// Agora aceita systemPromptOverride para definir personas específicas (ex: Coder).
661func (a *AgentMode) Run(ctx context.Context, query string, additionalContext string, systemPromptOverride string) error {
662 // Reentrancy guard. AgentMode is not safe to Run concurrently on the
663 // same instance — see runInflight comment in the struct. We CAS so
664 // that any caller stepping on an in-flight Run gets a clean error
665 // instead of corrupting shared state (taskTracker, history, TUI).
666 if !a.runInflight.CompareAndSwap(false, true) {
667 return fmt.Errorf("agent: another Run is already in flight on this AgentMode instance")
668 }
669 defer a.runInflight.Store(false)
670
671 // Pull turns that arrived on other channels (Telegram/…) into history so the
672 // agent/coder has cross-channel context. No-op outside local hub/connected
673 // mode (e.g. the gateway daemon, which manages its own context). Silent.
674 a.cli.syncHubContext(ctx)
675
676 // Item 8: defeat typeahead-in-the-dark. The user may have just
677 // pressed Enter on /coder or /agent — go-prompt's teardown can
678 // leave the controlling TTY in raw mode (no echo, ICRNL off),
679 // meaning any character typed during the spinner doesn't appear
680 // on screen even though the kernel IS capturing it. Restoring
681 // cooked terminal state up front ensures the user SEES what they
682 // type while the LLM streams.
683 coder.RestoreCookedMode()
684
685 // --- 1. CONFIGURAÇÃO E PREPARAÇÃO DO AGENTE ---
686 maxTurns := AgentMaxTurns()
687
688 // Load the seven-pattern quality config eagerly so HyDE retrieval
689 // (built BEFORE initMultiAgent) sees the right toggles. The
690 // dispatcher pipeline wiring inside initMultiAgent re-reads the
691 // same env, so the two views stay consistent.
692 a.qualityConfig = quality.LoadFromEnv()
693 // Apply session-level /refine and /verify overrides on top of env.
694 if a.cli.qualityOverrides.Refine != nil {
695 a.qualityConfig.Refine.Enabled = *a.cli.qualityOverrides.Refine
696 }
697 if a.cli.qualityOverrides.Verify != nil {
698 a.qualityConfig.Verify.Enabled = *a.cli.qualityOverrides.Verify
699 }
700
701 // Save checkpoint before agent starts (for rewind support)
702 a.cli.saveCheckpoint()
703
704 a.logger.Info("Modo Agente iniciado", zap.Int("max_turns_limit", maxTurns))
705
706 isCoder := (systemPromptOverride == CoderSystemPrompt)
707 hasActivePersona := a.cli.personaHandler != nil && a.cli.personaHandler.GetManager().HasActiveAgent()
708
709 // SYSTEM PROMPT COMPOSITION — structured for provider KV cache reuse.
710 //
711 // The agent system prompt is composed from four semantic blocks that
712 // are built here and surfaced both as:
713 // • a flat string (systemInstruction) — used by providers without
714 // native cache_control (OpenAI, Gemini, Ollama, …) and by all the
715 // legacy accounting code that measures history by `len(Content)`.
716 // • structured SystemParts []ContentBlock — Anthropic consumes these
717 // directly, stamping cache_control:ephemeral on each boundary so
718 // identical prefixes are served as cache reads on subsequent turns.

Callers

nothing calls this directly

Calls 15

composeCoreTextMethod · 0.95
buildWorkspaceBlocksMethod · 0.95
getToolContextStringMethod · 0.95
buildAgentSkillBlocksMethod · 0.95
initMultiAgentMethod · 0.95
RestoreCookedModeFunction · 0.92
LoadFromEnvFunction · 0.92
OrchestratorSystemPromptFunction · 0.92
TFunction · 0.92

Tested by

no test coverage detected