src — commands

Module: src-commands Cohesion: 0.80 Members: 0

src — commands

The src/commands module serves as the central hub for defining and dispatching both command-line interface (CLI) commands and interactive commands within the Code Buddy application. It provides a structured way to extend the application's functionality, allowing users to interact with various subsystems like the daemon, device nodes, skills hub, and more, directly from the terminal or an interactive client.

This module is critical for user interaction, system configuration, and developer workflows.

Architecture and Structure

The src/commands module is broadly divided into two main categories:

  1. CLI Commands (src/commands/cli/): These are standard commander.js commands registered directly with the main buddy program. Each file typically registers a top-level command (e.g., buddy approvals, buddy daemon) and its subcommands.
  2. Interactive Client Commands (src/commands/client-dispatcher.ts, src/commands/custom-commands.ts, etc.): These commands are designed for the interactive chat interface (e.g., a TUI or GUI client). They include slash commands (e.g., /commit), shell bypass commands (e.g., !ls), and direct bash execution.

CLI Command Registration Flow

The main buddy executable (likely src/index.ts) imports and calls registration functions from src/commands/cli/ files. Each registration function takes a commander.Command instance and adds its specific commands and options.

graph TD
    A[src/index.ts] --> B(registerApprovalsCommands)
    A --> C(registerDaemonCommands)
    A --> D(registerConfigCommand)
    A --> E(registerDeployCommands)
    A --> F(registerDeviceCommands)
    A --> G(registerNodeCommands)
    A --> H(registerHeartbeatCommands)
    A --> I(registerHubCommands)
    A --> J(registerIdentityCommands)
    A --> K(registerGroupCommands)
    A --> L(registerAuthProfileCommands)
    A --> M(registerSecretsCommands)
    A --> N(registerSpeakCommand)
    A --> O(registerUtilityCommands)
    A --> P(registerDevCommands)
    B,C,D,E,F,G,H,I,J,K,L,M,N,O,P --> Q(Commander.js Program)

Interactive Client Command Dispatching

The ClientCommandDispatcher is the core component for handling user input in an interactive client. It determines if an input is a special command (slash command, shell bypass, or direct bash) or a message to be processed by the AI agent.

graph TD
    A[User Input] --> B{ClientCommandDispatcher.dispatch}
    B --> |Starts with /| C{handleSlashCommand}
    B --> |Starts with !| D{handleShellBypass}
    B --> |Is direct bash| E{handleDirectBashCommand}
    B --> |Else| F[Pass to AI Agent]

    C --> |Internal Command (__TOKEN__)| G{handleInternalCommand}
    C --> |Legacy /commit-and-push| H[GitWorkflowHandler]
    C --> |Legacy /models| I[Show Model Selection UI]
    C --> |Legacy /models <name>| J[handleModelSwitch]
    C --> |Slash Command returns prompt| F
    C --> |Unknown Slash Command| K[Display Error]

    G --> |__CLEAR_CHAT__| L[Clear UI State + Delegate to Enhanced]
    G --> |__CHANGE_MODEL__ (no args)| I
    G --> |Other __TOKEN__| M[Delegate to EnhancedCommandHandler]

    D --> N[Execute Bash Command via Agent]
    E --> N

    L --> O[EnhancedCommandHandler]
    M --> O

Key Components and Their Roles

src/commands/cli/* (CLI Commands)

These files define the various command-line utilities available through the buddy executable. Each file typically exports a registerCommands function that takes a commander.Command instance and adds subcommands, options, and actions.

client-dispatcher.ts

The ClientCommandDispatcher class is responsible for parsing user input in an interactive client and routing it to the appropriate handler.

compress.ts

This file contains the core logic for the /compress command, which aims to reduce the token count of the conversation history by summarizing it.

custom-commands.ts

The CustomCommandLoader class enables users to define their own slash commands using markdown files.

delegate.ts

This file implements the /delegate command, which automates the process of creating a GitHub Pull Request for a given task.

Integration with the Codebase

The commands module integrates deeply with almost every other major part of the Code Buddy codebase:

This extensive integration highlights the commands module as the primary interface layer between the user and the underlying Code Buddy systems.