src — cli

Module: src-cli Cohesion: 0.80 Members: 0

src — cli

The src/cli module serves as the core command-line interface (CLI) component for Code Buddy. It encapsulates all the logic required to parse command-line arguments, load configurations, execute non-interactive commands, list available resources, and manage chat sessions. This module acts as the bridge between user input on the terminal and the underlying Code Buddy agent and utility services.

Module Structure

The src/cli module is organized into several files, each responsible for a distinct set of CLI functionalities:

Key Components and Functionality

1. Configuration Management (config-loader.ts)

This component is responsible for establishing the operational parameters for Code Buddy. It aggregates configuration from a hierarchical set of sources, ensuring flexibility and ease of use.

Configuration Sources (in order of precedence):

  1. Command-line options: Values passed directly via CLI flags (e.g., --api-key).
  2. Environment variables: System-wide variables (e.g., GROK_API_KEY, GROK_MODEL).
  3. User settings file: A persistent JSON file located at ~/.codebuddy/user-settings.json.

Key Functions:

Dependencies: This module heavily relies on ../utils/settings-manager.js for abstracting the details of reading and writing user settings and environment variables.

Configuration Loading Flow:

graph TD
    A[CLI Options] --> B{loadConfig}
    C[Environment Variables] --> B
    D[User Settings File] --> B
    B --> E[CLIConfig Object]

    subgraph config-loader.ts
        B
        F[loadApiKey] --> C
        F --> D
        G[loadBaseURL] --> C
        G --> D
        H[loadModel] --> C
        H --> D
        B --> F
        B --> G
        B --> H
    end

    subgraph utils/settings-manager.ts
        D
        C
    end

2. Headless Operations (headless.ts)

This component provides functionality for non-interactive execution of Code Buddy, suitable for scripting or automated workflows. It bypasses the interactive chat loop and confirmation prompts.

Key Functions:

Dependencies: This module lazy-loads ../agent/codebuddy-agent.js and ../utils/confirmation-service.js to minimize initial startup overhead. It also uses ../utils/logger.js for error reporting.

3. Listing Commands (list-commands.ts)

This component provides utilities for users to discover available resources within their Code Buddy environment.

Key Functions:

Dependencies: Relies on ../utils/logger.js for error logging and config-loader.ts for base URL resolution. It also lazy-loads ../prompts/prompt-manager.js and ../agent/custom/custom-agent-loader.js.

4. Session Management (session-commands.ts)

This component handles the persistence and resumption of chat sessions, allowing users to continue conversations across different CLI invocations.

Key Functions:

Dependencies: Uses ../utils/logger.js for error reporting and lazy-loads ../persistence/session-store.js to interact with the session persistence layer.

Integration and Entry Point (index.ts)

The src/cli/index.ts file simply re-exports all public functions and interfaces from the other files within the src/cli directory. This allows the main Code Buddy CLI application (typically src/main.ts or src/index.ts at the project root) to import all CLI-related functionalities from a single, convenient module.

Developer Notes