Commands/Slash Commands/ — Parsing & Dispatch

Slash command parsing, routing, and dispatch infrastructure

The Slash Commands sub-module implements the core command processing pipeline for OpenClaw. It parses user input into structured command objects, resolves them against a registry of handlers, manages command aliases, and dispatches execution through the agent binding system with support for channel-specific routing and peer-level targeting.

11
Agent Files
9
Channel Files
18
Model Files
~4.5k
Lines

📚 What is this & why does it exist?

Slash Commands is the front door of OpenClaw’s CLI. Every time a user types a command like openclaw agents add or openclaw models set gpt-4o, this sub-module is responsible for figuring out what the user wants, finding the right handler function, and routing the request to it. Without it, the system would have no way to interpret user intent.

💡 Real-world analogy

Think of Slash Commands like the reception desk at a large office building. When a visitor (user input) arrives, the receptionist (command parser) reads the visitor’s badge, checks their name against the directory (command registry), handles any name aliases (“go to Bob” actually means “go to Robert in Suite 401”), and then routes them to the correct office (command handler). The receptionist also checks if the visitor has an appointment with a specific person (agent binding) on a specific floor (channel routing).

Contents

Architecture

flowchart TD
  Input["User Input
CLI args"] --> Parser["Command Parser
parse args & flags"] Parser --> Registry["Command Registry
resolve handler"] Registry --> AliasCheck{"Alias?"} AliasCheck -->|Yes| AliasResolve["Alias Resolution
models/aliases.ts"] AliasCheck -->|No| BindingCheck["Binding Resolution
agents.bindings.ts"] AliasResolve --> BindingCheck BindingCheck --> ConfigLoad["Config Snapshot
requireValidConfigFileSnapshot()"] ConfigLoad --> Handler["Command Handler
domain-specific function"] Handler --> Output["Runtime Output
JSON or formatted text"]

Key Classes

Class / FunctionFilePurpose
agentsAddCommand()agents.commands.add.tsWizard-driven agent creation with model selection and binding
agentsBindCommand()agents.commands.bind.tsBind agent to ACP provider/platform with auth flow
agentsUnbindCommand()agents.commands.bind.tsRemove agent route bindings
agentsListCommand()agents.commands.list.tsDisplay agent summaries with binding details
applyAgentBindings()agents.bindings.tsMerge bindings into config with conflict detection
describeBinding()agents.bindings.tsHuman-readable binding description
buildAgentSummaries()agents.config.tsConstruct display-ready agent info from config
requireValidConfigFileSnapshot()agents.command-shared.tsLoad and validate config with content hash
channelsAddCommand()channels/add.tsInteractive channel configuration wizard

Data Structures

TypeNameDescription
interfaceAgentsAddOptionsOptions bag for agent add: name, workspace, model, agentDir, bind[], nonInteractive, json
interfaceAgentRouteBindingBinding model with agentId and match criteria (channel, accountId, peer, guildId, teamId, roles)
interfaceAgentSummaryDisplay model with id, name, identityName, workspace, model, bindings count, isDefault
interfaceAgentsBindOptionsOptions for bind command: agent, bind[], json
interfaceChannelAddResultResult of channel add: channelId, configured boolean, optional error
typeChannelsAddOptionsOptions for channel add: token, channel, nonInteractive

Interactions

DirectionPartnerInterfaceDescription
→config/requireValidConfigFileSnapshot()Load config snapshot with hash for conflict detection
→config/replaceConfigFile()Atomically write updated config
→wizard/createClackPrompter()Interactive user prompting for wizards
→routing/parseSessionKey()Agent/session key parsing for binding resolution
→channels/resolveChannelAdapter()Channel adapter loading for capability detection
←terminal/RuntimeEnvOutput abstraction for log/warn/error/exit

Configuration

Knob / ParameterDefaultDescription
--namepromptedAgent display name for creation
--workspacecurrent dirAgent workspace directory path
--modeldefault modelPrimary model for the agent
--bindnoneBinding spec in provider:value format (repeatable)
--non-interactivefalseSkip all prompts, require all flags
--jsonfalseOutput structured JSON instead of formatted text
--agent-dirautoOverride agent directory path

Directory Map

Source tree for Slash Commands
src/commands/
├── agents.commands.add.ts          ← Wizard-driven agent creation
├── agents.commands.bind.ts         ← Route bind/unbind operations
├── agents.commands.delete.ts       ← Agent removal with cleanup
├── agents.commands.list.ts         ← Agent listing & display
├── agents.commands.identity.ts     ← Agent identity management
├── agents.bindings.ts              ← Binding model & conflict detection
├── agents.config.ts                ← Agent configuration & summaries
├── agents.providers.ts             ← Provider resolution for agents
├── agents.command-shared.ts        ← Shared utilities (config load)
├── channels/
│   ├── add.ts                      ← Interactive channel add wizard
│   ├── remove.ts                   ← Channel removal
│   ├── list.ts                     ← Channel listing
│   ├── status.ts                   ← Channel status & probing
│   ├── capabilities.ts             ← Feature detection
│   ├── resolve.ts                  ← Adapter resolution
│   ├── shared.ts                   ← Shared channel utilities
│   ├── logs.ts                     ← Channel log display
│   └── add-mutators.ts             ← Account config helpers
└── models/
    ├── aliases.ts                  ← Alias CRUD commands
    ├── set.ts                      ← Default model selection
    └── list.ts                     ← Model listing

Deep-Dive Highlights

Core mechanisms from this sub-module have been extracted into dedicated focus pages for in-depth exploration. Each page covers one algorithm, protocol, or subsystem with diagrams, code walks, and configuration details.

Deep-Dive Pages

Command Parsing Focus

How user input is tokenized, validated, and transformed into structured command objects with typed options. Covers the full parse pipeline from raw string to handler invocation.

Extracted topic page

Command Registry Focus

The handler registration and lookup system. How commands are registered, matched by name, and dispatched to their corresponding async handler functions with dependency injection.

Extracted topic page

Command Aliases Focus

Model alias management system allowing users to create shorthand names for models. Covers alias normalization, storage in config, and resolution during command dispatch.

Extracted topic page

Code Examples

Example: Agent Route Binding

export function describeBinding(binding: AgentRouteBinding): string {
  const match = binding.match;
  const parts = [match.channel];
  if (match.accountId) parts.push(`accountId=${match.accountId}`);
  if (match.peer) parts.push(`peer=${match.peer.kind}:${match.peer.id}`);
  if (match.guildId) parts.push(`guild=${match.guildId}`);
  if (match.teamId) parts.push(`team=${match.teamId}`);
  return parts.join(" ");
}

Example: Agent Add Command Signature

type AgentsAddOptions = {
  name?: string;
  workspace?: string;
  model?: string;
  agentDir?: string;
  bind?: string[];
  nonInteractive?: boolean;
  json?: boolean;
};

export async function agentsAddCommand(
  opts: AgentsAddOptions,
  runtime: RuntimeEnv = defaultRuntime,
  params?: { hasFlags?: boolean }
): Promise<void>