Autonomous agent orchestration, tools, and execution flows in Devonz.
Agent Mode enables Devonz to act as an autonomous coding agent. Instead of generating code artifacts in the chat, the LLM uses structured tool calls to directly read, write, and execute code via the LocalRuntime on the host machine. This enables multi-step, iterative development with error detection and self-correction.
┌─────────────────────────────────────────────────┐
│ Chat Interface │
│ (agentMode toggle enabled) │
└──────────────────────┬──────────────────────────┘
│
▼
┌──────────────────────────────────────────────────┐
│ agentChatIntegration.ts │
│ Bridges chat API with agent orchestration │
└──────────────────────┬───────────────────────────┘
│
┌────────────▼────────────┐
│ AgentOrchestrator │
│ (agentOrchestratorService.ts) │
│ │
│ - Session lifecycle │
│ - Iteration tracking │
│ - Approval workflows │
│ - Status management │
└────────────┬────────────┘
│
┌────────────▼────────────┐
│ AgentToolsService │
│ (agentToolsService.ts) │
│ │
│ - Tool definitions │
│ - Tool execution │
│ - Result formatting │
└────────────┬────────────┘
│
┌────────────▼────────────┐
│ LocalRuntime │
│ (File I/O, Shell) │
│ Executes on host via │
│ /api/runtime/* routes │
└─────────────────────────┘
| File | Purpose |
|---|---|
app/lib/agent/index.ts |
Public API — re-exports all agent types and services |
app/lib/agent/types.ts |
TypeScript interfaces (464 lines of thorough type definitions) |
app/lib/agent/prompts.ts |
System prompts for agent mode (391 lines) |
app/lib/services/agentOrchestratorService.ts |
Session management, iteration control, approval flows (326 lines) |
app/lib/services/agentToolsService.ts |
Tool definitions and execution logic |
app/lib/services/agentChatIntegration.ts |
Integration layer between chat API and agent |
app/lib/stores/agentMode.ts |
Nanostore for agent mode UI state |
The LLM can call these tools during agent mode execution.
MCP Tools: In addition to the built-in
devonz_*tools listed below, any MCP tools registered throughmcpServiceare also available to the LLM during agent mode. MCP tools are registered alongside agent tools and appear in the same tool set passed to the model.
Extended Thinking: Extended thinking is fully compatible with agent mode. When enabled, the model's reasoning steps are shown before each tool call decision.
| Tool | Purpose | Approval Required |
|---|---|---|
devonz_read_file |
Read file contents (with optional line range) | No |
devonz_write_file |
Create or modify files | Configurable |
devonz_delete_file |
Delete files or directories | Configurable |
devonz_rename_file |
Rename or move files | Configurable |
devonz_list_directory |
Explore project structure | No |
devonz_run_command |
Execute shell commands (npm, node, etc.) | Configurable |
devonz_get_errors |
Check for build/runtime/preview errors | No |
devonz_search_code |
Search for code patterns across files (supports regex) | No |
devonz_patch_file |
Make targeted text replacements without rewriting entire files | Configurable |
{
path: string; // File path relative to project root
startLine?: number; // Optional start line (1-indexed)
endLine?: number; // Optional end line (inclusive)
}{
path: string; // File path relative to project root
content: string; // Complete file content
}{
path: string; // File or directory path relative to project root
recursive?: boolean; // Required for non-empty directories (default: false)
}{
oldPath: string; // Current file path relative to project root
newPath: string; // New file path (parent directories are auto-created)
}{
path?: string; // Directory path (default: "/")
recursive?: boolean; // List recursively (default: false)
maxDepth?: number; // Max depth for recursive (default: 3)
}{
command: string; // Shell command to execute
cwd?: string; // Working directory (relative to project root)
timeout?: number; // Timeout in ms (default: 30000)
}{
source?: 'terminal' | 'preview' | 'build' | 'all'; // Error source (default: 'all')
}{
query: string; // Search pattern — supports regex with automatic fallback to literal matching
path?: string; // Directory to search (default: "/")
maxResults?: number; // Max results (default: 50)
includePattern?: string; // File path include filter
excludePattern?: string; // File path exclude filter
}{
path: string; // Absolute path to file (e.g., "/src/App.tsx")
replacements: Array<{ // Array of targeted replacements
oldText: string; // Exact text to find
newText: string; // Replacement text
}>;
}Configurable via the Settings UI or programmatically:
interface AgentModeSettings {
enabled: boolean; // Toggle agent mode
autoApproveFileCreation: boolean; // Skip approval for new files (default: true)
autoApproveFileModification: boolean; // Skip approval for file edits (default: true)
autoApproveCommands: boolean; // Skip approval for shell commands (default: false)
maxIterations: number; // Max iterations per session (default: 25)
}idle → thinking → executing → thinking → ... → completed
│
▼
waiting_for_approval → (approved) → executing
│
▼
(denied) → thinking
| Status | Description |
|---|---|
idle |
No active session |
thinking |
LLM is generating next action |
executing |
Tool is being executed |
waiting_for_approval |
User approval needed for a tool call |
waiting_for_user |
Agent needs user input to continue |
error |
An error occurred |
completed |
Session finished |
- Start:
AgentOrchestrator.startSession(task)— initializes state, sets status tothinking - Iterate: LLM generates tool calls → orchestrator executes them → tracks results
- Approval: If a tool needs approval, status changes to
waiting_for_approval - Iteration Limit: If
maxIterationsreached, user is warned - Complete:
AgentOrchestrator.endSession()— finalizes state, logs summary
The orchestrator tracks everything during a session:
interface AgentExecutionState {
iteration: number; // Current iteration count
maxIterations: number; // Configured limit
status: AgentStatus; // Current status
isExecuting: boolean; // Whether actively executing
toolCalls: ToolCallRecord[]; // All tool calls with results
totalToolCalls: number; // Total count
filesCreated: string[]; // Files created this session
filesModified: string[]; // Files modified this session
commandsExecuted: string[]; // Commands run this session
sessionStartTime: number; // Start timestamp
sessionEndTime?: number; // End timestamp
}When agent mode is active, the standard system prompt is replaced with a single unified prompt — AGENT_MODE_FULL_SYSTEM_PROMPT — exported from app/lib/agent/prompts.ts. This replaces the previous multi-prompt system (the legacy AGENT_SYSTEM_PROMPT, AGENT_SYSTEM_PROMPT_COMPACT, AGENT_ERROR_CONTEXT_PROMPT, AGENT_ITERATION_WARNING_PROMPT, and helper functions like getAgentSystemPrompt() / enhanceSystemPromptWithAgentMode() have all been removed).
The prompt covers:
- Instructs the LLM to use
devonz_*tools instead of artifact XML tags - Defines LocalRuntime capabilities and constraints (native binaries ARE supported, git IS available; projects are sandboxed to
~/.devonz/projects/{projectId}/) - Establishes a tool selection hierarchy (prefer
write_fileover shell commands) - Forbids outputting file content in plain text (must use tools)
- Mobile-first design mandate — all UI must be responsive and mobile-first
- Design system / semantic tokens — enforces consistent use of design tokens
- Technology preferences — React 19, Tailwind v4, shadcn/ui
- Response brevity guidelines — keep responses concise and action-oriented
- Self-validation checklist — the agent validates its own output before finishing
See app/lib/agent/prompts.ts for the complete prompt.
The agentChatIntegration.ts module bridges agent mode with the standard chat API:
| Function | Purpose |
|---|---|
shouldUseAgentMode() |
Check if agent mode is enabled for the current request |
getAgentToolSetWithoutExecute() |
Get tool definitions (without execute functions) for the AI SDK |
initializeAgentSession() |
Start a new agent session |
incrementAgentIteration() |
Advance the iteration counter |
getAgentIterationWarning() |
Get a warning prompt when nearing iteration limit |
processAgentToolInvocations() |
Process tool invocations from LLM response |
processAgentToolCall() |
Execute a single tool call |
isAgentToolName() |
Check if a tool name is an agent tool |
The agent has built-in error recovery:
- Tool execution errors are captured and returned to the LLM as error results
- Build errors can be detected via
devonz_get_errorstool - Iteration warnings prompt the user when approaching the limit
- Session errors set status to
errorwith an error message
The LLM is prompted to check for errors after making changes and self-correct when possible.