Tauri Command Handlers
Relevant source files
The following files were used as context for generating this wiki page:
Tauri Command Handlers serve as the primary bridge between the React-based frontend and the Rust backend in the opcode application. These handlers facilitate complex operations such as managing Claude Code sessions, interacting with the local filesystem, executing shell commands, and managing custom AI agents. They are registered within the Tauri runtime and invoked asynchronously by the frontend through a unified API adapter.
The architecture supports a dual-mode environment where commands can be executed either via Tauri's native IPC (Inter-Process Communication) bridge in desktop mode or via a REST/WebSocket API when running in a web server context. This ensures that the application logic remains consistent regardless of the deployment target.
Sources: src-tauri/src/main.rs:163-255, src/lib/apiAdapter.ts:89-111, web_server.design.md:9-16
Architecture and Invocation Flow
The command system follows a strict request-response pattern for most operations, while utilizing events for long-running processes like Claude Code execution. The apiAdapter.ts module acts as a dispatcher, detecting the environment (Tauri vs. Web) and choosing the appropriate communication method.
Command Dispatch Logic
The frontend uses a unified apiCall function. If a Tauri environment is detected, it utilizes invoke from @tauri-apps/api/core. If not, it maps the command name to a REST endpoint or initiates a WebSocket connection for streaming commands.
The diagram shows the decision path taken by the frontend when a user triggers an action that requires backend processing.
Sources: src/lib/apiAdapter.ts:16-52, src/lib/apiAdapter.ts:89-111, web_server.design.md:18-35
Core Command Categories
1. Slash Commands Management
Slash commands allow users to trigger custom prompts and tools within Claude sessions. These commands are stored as Markdown files with YAML frontmatter, categorized by scope: default, user (global), or project (local to a repository).
| Command Name | Scope | Description |
|---|---|---|
slash_commands_list | Global/Project | Discovers and returns all available slash commands from standard paths. |
slash_command_get | Internal | Retrieves a specific command's details and content by ID. |
slash_command_save | Write | Persists a custom command to .md files in ~/.claude/commands or .claude/commands. |
slash_command_delete | Write | Removes the corresponding command file and cleans up empty parent directories. |
Sources: src-tauri/src/commands/slash_commands.rs:179-340, src-tauri/src/main.rs:248-251
2. Claude Execution and Session Management
These commands handle the lifecycle of the claude subprocess, including initial execution, continuation of prompts, and session resumption.
This sequence illustrates how streaming commands maintain a live link between the subprocess output and the user interface.
Sources: web_server.design.md:46-56, src/lib/apiAdapter.ts:233-280
Implementation Details: Custom Slash Commands
Slash commands are parsed from Markdown files. The system looks for a YAML frontmatter block to define metadata such as allowed-tools and description.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlashCommand {
pub id: String,
pub name: String,
pub full_command: String,
pub scope: String,
pub namespace: Option<String>,
pub file_path: String,
pub content: String,
pub description: Option<String>,
pub allowed_tools: Vec<String>,
pub has_bash_commands: bool,
pub has_file_references: bool,
pub accepts_arguments: bool,
}
Sources: src-tauri/src/commands/slash_commands.rs:9-27
File Discovery Logic
The slash_commands_list handler scans three distinct locations:
- Default: Built-in commands like
/add-dirand/review. - Project: Scans the
.claude/commandsdirectory relative to the provided project path. - User: Scans the
~/.claude/commandsdirectory in the user's home folder.
Sources: src-tauri/src/commands/slash_commands.rs:207-246
Command Registration (Main Entry Point)
The handlers are registered in main.rs using the tauri::generate_handler! macro. This makes them accessible to the frontend via the string names defined in the macro.
Key Registered Modules:
- Agents:
list_agents,create_agent,execute_agent(viaagents.rs). - Claude:
list_projects,execute_claude_code,get_claude_settings. - MCP:
mcp_add,mcp_list,mcp_serve(Model Context Protocol management). - Storage:
storage_read_table,storage_execute_sql(SQLite interaction). - Usage:
get_usage_stats,get_usage_details(Token and cost tracking).
Sources: src-tauri/src/main.rs:163-255
Web Compatibility Layer
In web mode (non-Tauri), the apiAdapter.ts maps Tauri command strings to specific REST API endpoints defined in the Axum web server.
| Tauri Command | REST Endpoint | Method |
|---|---|---|
list_projects | /api/projects | GET |
create_agent | /api/agents | POST |
mcp_list | /api/mcp/servers | GET |
slash_commands_list | /api/slash-commands | GET |
execute_claude_code | /ws/claude | WebSocket |
Sources: src/lib/apiAdapter.ts:121-189, web_server.design.md:120-125
Summary
Tauri Command Handlers in opcode provide a robust, type-safe interface for the frontend to perform privileged system operations. By abstracting these operations into handlers, the project maintains a clean separation between UI logic and system-level execution, while the apiAdapter ensures the same code works seamlessly across desktop and web environments. The extensibility of this system is primarily demonstrated through the Custom Slash Command architecture, which allows users to define complex behaviors via simple Markdown files.