src — app

Module: src-app Cohesion: 0.80 Members: 0

src — app

The src/app module serves as the central application factory and configuration hub. It orchestrates the initial setup of the application, including environment loading, configuration management, signal handling for graceful shutdowns, and persistence of user settings. This module ensures that the application starts in a consistent and well-defined state, regardless of how it's invoked (e.g., via command line, environment variables, or saved settings).

Purpose

The primary responsibilities of the src/app module are:

Core Concepts

The module introduces several key types and interfaces to manage application state and behavior:

Application Initialization Flow

The bootstrap function is the primary entry point for initializing the application. It orchestrates a series of steps to prepare the environment and gather configuration.

graph TD
    A[bootstrap(options)] --> B{options.skipEnv?}
    B -- No --> C[loadEnvironment()]
    C --> D{options.skipSignals?}
    B -- Yes --> D
    D -- No --> E[setupSignalHandlers()]
    E --> F[ensureUserSettings()]
    D -- Yes --> F
    F --> G[loadApiKey()]
    F --> H[loadBaseURL()]
    F --> I[loadModel()]
    G & H & I --> J[Merge with options.config]
    J --> K[Return ApplicationConfig]

  1. loadEnvironment(): If not skipped, this function loads environment variables from .env files using dotenv.
  2. setupSignalHandlers(): If not skipped, this function registers handlers for SIGTERM, SIGINT, uncaughtException, and unhandledRejection to ensure graceful shutdowns and robust crash reporting. It leverages the CrashHandler and disposeAll utilities.
  3. ensureUserSettings(): This function attempts to load user settings, ensuring that the necessary configuration directory exists. It silently handles cases where the directory might not yet exist.
  4. Configuration Loading: loadApiKey(), loadBaseURL(), and loadModel() are called to retrieve core configuration values from their prioritized sources (environment variables, secure credential manager, or settings manager).
  5. Configuration Assembly: The loaded values are combined with any custom configuration provided in options.config to form the final ApplicationConfig object, which is then returned.

Configuration Management

The module provides dedicated functions for loading and building the application's configuration:

  1. process.env.GROK_API_KEY
  2. getCredentialManager().getApiKey() (secure storage)
  3. getSettingsManager().getApiKey() (legacy settings)

  1. process.env.GROK_BASE_URL
  2. getSettingsManager().getBaseURL()

  1. process.env.GROK_MODEL
  2. getSettingsManager().getCurrentModel()

Error and Signal Handling

The setupSignalHandlers() function is crucial for application stability and user experience:

Persistent Settings

The saveSettings(apiKey?: string, baseURL?: string) function allows users to persist their API key and base URL for future sessions:

Module Exports

The src/app/index.ts file acts as the public interface for the src/app module, re-exporting all relevant types and factory functions from application-factory.ts and types.ts.

Types:

Factory Functions:

Dependencies

The src/app module interacts with several other core modules: