Add ability to add custom commands / tasks

Resolved 💬 3 comments Opened May 13, 2025 by homoky Closed Sep 3, 2025
💡 Likely answer: A maintainer (dedrisian-oai, contributor) responded on this thread — see the highlighted reply below.

List in JSON config list of files that could be run via / slash command:

example: /create-commit-message

View original on GitHub ↗

3 Comments

fettpl · 1 year ago

Hello @seratch @aibrahim-oai @nornagon-openai,

If possible, I would like to propose my solution for this feature request. As per "Contributing" section of README.me, here's the general overview of the proposal, but as I needed such solution privately, I already have it working quite decently:

What's New

Core Infrastructure (codex-rs/core/src/custom_commands/)

  • mod.rs - Main module with data structures (CustomCommand, CommandArgument, Command trait)
  • loader.rs - File discovery, parsing pipeline, caching, and command management
  • parser.rs - YAML frontmatter extraction and markdown content parsing
  • template.rs - Handlebars-powered template processing with system variables
  • validator.rs - Security validation (path traversal prevention, input sanitization)
  • registry.rs - Unified registry merging built-in and custom commands

TUI Integration (codex-rs/tui/src/custom_commands/)

  • executor.rs - Command execution logic and async processing
  • mod.rs - Module exports and feature gating

Dependencies

  • Added handlebars, serde_yaml, notify as optional dependencies behind custom-commands feature flag

What's Modified

codex-rs/tui/src/slash_command.rs

  • Added: Custom(String) variant to SlashCommand enum
  • Added: Command management variants (CommandCreate, CommandEdit, etc.)
  • Added: is_custom() method for type checking
  • Rationale: Extends existing enum to support dynamic custom commands while maintaining type safety

codex-rs/tui/src/bottom_pane/command_popup.rs

  • Added: update_with_custom_commands() method to merge command lists
  • Modified: Sorting logic to show built-in commands first, then custom commands
  • Modified: Display format to show "(user)" suffix for custom commands
  • Rationale: Maintains existing UI patterns while clearly differentiating command sources

codex-rs/tui/src/app.rs

  • Added: CommandExecutor initialization with registry setup
  • Added: Custom command execution in DispatchCommand handler
  • Added: Command management command handlers (create, edit, delete, list, reload)
  • Modified: Chat widget initialization to include custom commands
  • Rationale: Integrates custom commands into existing event flow without disrupting built-in functionality

codex-rs/core/Cargo.toml

  • Added: custom-commands feature flag with optional dependencies
  • Rationale: Allows users to opt-out of custom commands functionality if not needed

Key Features

Command Definition Format

---
description: Brief description for command popup
arguments:
  - name: arg_name
    description: Argument description
    required: true|false
    default: "default value"
---

Template content with {{arg_name}} placeholders.

Security Measures

  • Path canonicalization prevents directory traversal attacks
  • Template validation blocks dangerous constructs ({{exec}}, {{system}}, etc.)
  • Input sanitization for command names and arguments
  • File extension validation (.md only)

Performance Optimizations

  • Lazy loading with metadata-first approach
  • HashMap-based O(1) command lookup
  • Template compilation caching
  • Configurable command limits (default: 100)

User Experience

  • Commands appear in existing / popup with "(user)" indicator
  • Hot-reload support for command file changes
  • Comprehensive error messages for malformed commands
  • Built-in management commands (/command-list, /command-reload, etc.)

Testing Coverage

  • Unit tests: >95% coverage for all new modules
  • Integration tests: End-to-end command execution flows
  • Security tests: Path traversal and injection prevention
  • Performance tests: Startup time and memory usage benchmarks

Backward Compatibility

  • Zero breaking changes to existing functionality
  • Feature flag allows complete disabling if needed
  • Existing slash commands work unchanged
  • No impact on startup time when feature is disabled
fettpl · 1 year ago

@seratch @aibrahim-oai @nornagon-openai With the recent pull request at https://github.com/openai/codex/pull/2453 I believe my proposal is no longer needed.

dedrisian-oai contributor · 11 months ago