# Slash commands

Every surface shares one registry, file format and menu. How others do it: [reference implementations](https://fzkuji.github.io/OpenProgram/docs/reference/design/cli/slash-commands-references.html).

## Source layers

| Layer, lowest priority first | Source | Author | Reload |
| --- | --- | --- | --- |
| L0 builtin | Source code | OpenProgram | Restart |
| L1 plugin | `entrypoints.commands` under `~/.openprogram/plugins/<pkg>/...` | Plugin authors | Plugin reload |
| L2 mcp | `list_prompts()` of connected servers | MCP servers | Session reconnect, `/api/commands?refresh=mcp` |
| L3 skill | `~/.openprogram/skills/<name>/SKILL.md` | Skill authors, users | Watcher |
| L4 user | `~/.openprogram/commands/**/*.md` | Current user | Watcher |
| L5 project | `<cwd>/.openprogram/commands/**/*.md` | Project maintainers | Watcher |

- Same name: the later layer wins. The overridden command stays reachable as `/(plugin)name`, `/(mcp:linear)name`, `/(skill)name`, `/(user)name`, `/(project)name`.
- Within one source, the same realpath loads once.
- Conflict in the menu: primary entry tagged `(+2 more)`; ⇥ switches source.
- The source label is always written by the loader; frontmatter cannot claim one.

## File format

```
---
name: review              # default: file name
aliases: [r, rev]
description: Review the current diff per team conventions
when-to-use: |
  Use when the user wants a style and bug review of uncommitted changes.
hidden: false
arguments:
  - name: target
    description: File path or directory; defaults to the current diff
    required: false
argument-hint: "[target]"
type: prompt
context: inline
agent: general-purpose    # only with context: fork
model: inherit
effort: medium
allowed-tools: [Read, Grep]
paths: ["src/**/*.{ts,tsx}", "**/*.py"]
requires:
  any-bins: [git]
  config: [openai_api_key]
hooks:
  PreToolUse: ...
version: 1.0.0
---
Please review this code at {{target}}.

Additional context:
$ARGUMENTS

Recent commits:
!`git log -5 --oneline`

Current diff:
@`git diff --staged`
```

| Field | Type | Default | Meaning | From |
| --- | --- | --- | --- | --- |
| `name` | string | file stem | Command name | common |
| `aliases` | string[] | [] | Also subject to override | claude-code |
| `description` | string | "" | One line, in the menu | common |
| `when-to-use` | string | "" | Detail panel | claude-code |
| `hidden` | bool | false | Out of completion, still triggerable | claude-code |
| `arguments` | list | [] | Positional declarations. Numeric names rejected: they collide with `$0` | opencode |
| `argument-hint` | string | generated | Grey hint in the menu | claude-code |
| `type` | enum | prompt | Execution mode | claude-code |
| `context` | enum | inline | `inline` or `fork` | claude-code |
| `agent` | string | general-purpose | `subagent_type` when forking | claude-code |
| `model` | string | inherit | `inherit`, `opus`, `sonnet`, `haiku` or a full id | claude-code |
| `effort` | enum, int | inherit | `low`, `medium`, `high`, `max` or a number | claude-code |
| `allowed-tools` | string[] | inherit | Tool allowlist in fork mode | claude-code |
| `paths` | string[] | null | Glob-conditioned activation | claude-code |
| `requires` | object | {} | Prerequisites | openclaw |
| `hooks` | object | {} | Temporary hooks | claude-code |
| `version` | semver | null | For upgrade prompts | claude-code |
| `user-invocable` | bool | true | False: only the model can call it | claude-code |
| `shell` | enum | inherit | `bash` or `powershell`, for `!` blocks | claude-code |

Undeclared fields go to an `extras` dict without error.

## Body template

| Syntax | Meaning | From |
| --- | --- | --- |
| `$ARGUMENTS` | Everything after the command | claude-code |
| `$0`..`$9` | Nth positional; missing is empty | opencode |
| `{{name}}` | Named argument declared in `arguments:` | opencode |
| `${OPENPROGRAM_COMMAND_DIR}` | Directory of the command file | claude-code |
| `${OPENPROGRAM_SESSION_ID}` | Current session id | claude-code |
| `${OPENPROGRAM_CWD}` | Current working directory | OpenProgram |
| `` !`cmd` `` or a ```` ```! ```` block | Host shell; stdout spliced in; 2 s timeout | claude-code, hermes |
| `` @`path` `` | File contents spliced in | OpenProgram |
| `<<command-name>>name<</command-name>>` | Expands another command, at most 3 levels | OpenProgram |

Arguments are shell-tokenized, falling back to a whitespace split.

## Execution

| Mode | Effect |
| --- | --- |
| `type: prompt` (default) | Rendered template becomes a user message, normal agent loop |
| `type: local` | Calls the host's handler; for `/compact`, `/clear`, `/new`, `/web`, `/model` and other builtins |
| `type: local-jsx` | Interface reserved for a server-pushed structured event |
| `context: inline` (default) | Rendered prompt enters the current session |
| `context: fork` | Runs as one `agent(prompt=...)` sub-agent: `agent` sets `subagent_type`, `allowed-tools` the tools. Its final message is the command result; the main context stays clean |

MCP prompts are `prompt` and `inline`, rendered on their server.

## Conditions and hooks

An unmet condition hides the command from completion. Typing it runs the check again and gives a clear error.

| Field | Passes when |
| --- | --- |
| `paths` | A file recently touched in the session, or named with `@file`, matches |
| `requires.any-bins` | `which` finds at least one; else "needs `git` / `rg`, please install first" |
| `requires.config` | The profile has the key |
| `requires.platform` | Matches, e.g. `[darwin, linux]` |

```
hooks:
  PreToolUse:
    - matcher: Bash
      command: !`echo "blocked by /review" >&2; exit 2`
  PostToolUse:
    - matcher: Edit
      handler: built-in:auto-stage
```

Effective only during that command. Event names reuse `runtime/events/registry.py`. `!` block: stdout to the log, exit code allows or denies. `built-in:<id>`: a registered handler. The schema is fixed ahead of use so enabling it later breaks nothing.

## Menu

```
/review                  Review the current diff per team conventions      [project]
  [target]

/review (project)                                                 ⇥ detail panel
─────────────────────────────────────
Review the current diff per team conventions

Arguments: [target] (optional)
Mode: inline · model: inherit · effort: medium
Source: .openprogram/commands/review.md
```

Grouped by source (project, user, skill, mcp, plugin, builtin), alphabetical within. Fuzzy search over name, description and when-to-use.

## Security

| Input | Rule |
| --- | --- |
| Command files | After `realpath`, must fall inside the trusted root being scanned (`~/.openprogram/`, `$cwd/.openprogram/`) |
| YAML | `yaml.safe_load` |
| Globs | `fnmatch`; `..` and absolute paths disabled |
| `!` blocks | Off unless `commands.allow_shell: true`; off in MCP context; 2 s timeout; fork bombs forbidden; stdout capped at 64 KB |
| `@` references | Inside trusted roots, or `--allow-file <abs>` |

## Implementation

| File in openprogram/runtime/commands/ | Role |
| --- | --- |
| `loader.py` | Scans user and project directories |
| `frontmatter.py` | YAML and field parsing |
| `template.py` | Body rendering |
| `registry.py` | Merged table, conflict index, MCP sync |
| `dispatch.py` | Resolves and renders; pure, the host carries out the result |
| `_plugin_adapter.py`, `_skill_adapter.py` | Project plugins and skills into the registry |

1. **User types** `/review xxx`
2. **`dispatch.invoke(text, session_id, cwd)`** `registry.resolve`, then template render
3. **Result by kind** `prompt`: the host sends the text. `local`: the handler runs. `mcp_prompt`: fetched from the server.

| Endpoint | Returns |
| --- | --- |
| `GET /api/commands` | Merged list; `?source=`, `?include_hidden=` |
| `GET /api/commands/{name}` | One entry with body |
| `POST /api/commands/invoke` | Body `{text, session_id, cwd}`: resolves and renders, does not send |
| `POST /api/commands/reload` | Forces a rescan |
| `GET /api/commands/conflicts` | Same name, several sources |
| `GET /api/plugins/commands` | Older plugin-only list |

| Front end | Reads the registry through |
| --- | --- |
| Web composer (`use-slash-menu.ts`) | `/api/commands`, with `/api/plugins/commands` as a fallback; rendered text lands in the textarea |
| Rich REPL (`repl/handlers.py`) | `_handle_slash` and `dispatch.invoke`. `register_repl_builtins` registers each local action with a marker string; `_LOCAL_ACTIONS` maps it back. Existence, aliases and `/help` read `list_all()` |
| Ink TUI (`apps/cli/src/commands/registry.ts`) | Hardcodes only TUI-local actions. `GET /api/commands` feeds completion, Ctrl+K and `/help`; `POST /api/commands/invoke` expands |

**Dropped on purpose**

| Source | Design | Why not |
| --- | --- | --- |
| openclaw | i18n (`descriptionLocalizations`) | English and Simplified Chinese only |
| openclaw | Provider routing (Slack, Mattermost) | Single host |
| hermes | `platforms` filter | Replaced by `requires.platform` |
| hermes | 134-pattern prompt-injection detection | Belongs in a standalone scanner |
| claude-code | `local-jsx` React components | Web UI uses structured events; the CLI has none |
| pi-mono | Pure hardcoding | A counterexample |

**Versioning**

- `version` is stored in the registry with the source repository's git hash, if any.
- L0 tracks the OpenProgram version. L1 uses plugin autoupdate. L3 uses skills discovery diff. L4 and L5 are user-written and do not auto-update.
- New designs from the references become fields here. Frontmatter is a stable contract: if upstream renames a field, an alias in `frontmatter.py` keeps both spellings parsing.

## Implementation status

| Status | Items |
| --- | --- |
| Implemented | All six layers, frontmatter parsing, `$ARGUMENTS`, positional, named and `OPENPROGRAM_*` substitutions, override and `(source)name`, conflict table, the API, all three front ends |
| Parsed, not acted on | `paths`, `requires`, `hooks`, `allowed-tools` limits, `context: fork` (returned by `invoke`, no sub-agent run) |
| Not built | `!` and `@` substitution, `<<command-name>>` expansion, `commands.allow_shell`, the watcher, `local-jsx`, the ⇥ source switcher and detail panel, the field alias table, version and git-hash storage |

Skills are registered `inline`, with `agent: general-purpose` set so a later switch to fork lands on the right pool.
