# All files / src config.ts

100% Statements 42/42
100% Branches 33/33
100% Functions 27/27
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175

    135x

    34x

    135x

    14x

    135x

    26x

    135x

    14x

    135x

    24x

    135x

    14x

    135x

    25x

    135x

    14x

    137x

    15x

    135x

    20x

    135x

    14x

    442x

    25x

    143x

    117x

    227x
    227x
    227x
    227x
    227x

    135x

    135x
    135x

    135x
    2025x

    1890x
    1890x

    1755x

    135x

    96x
     /**
     * Global configuration for LLM providers.
     * Values are initialized from environment variables but can be overridden programmatically.
     */
    export interface NodeLLMConfig {
      openaiApiKey?: string;
      openaiApiBase?: string;
      anthropicApiKey?: string;
      anthropicApiBase?: string;
      geminiApiKey?: string;
      geminiApiBase?: string;
      deepseekApiKey?: string;
      deepseekApiBase?: string;
      ollamaApiBase?: string;
      openrouterApiKey?: string;
      openrouterApiBase?: string;
      debug?: boolean;
      maxToolCalls?: number;
      maxRetries?: number;
      requestTimeout?: number;
      maxTokens?: number;
      toolExecution?: ToolExecutionMode;
      provider?: string;
    }

    import {
      DEFAULT_MAX_TOOL_CALLS,
      DEFAULT_MAX_RETRIES,
      DEFAULT_REQUEST_TIMEOUT,
      DEFAULT_MAX_TOKENS,
      DEFAULT_TOOL_EXECUTION,
      DEFAULT_OLLAMA_BASE_URL,
      ToolExecutionMode
    } from "./constants.js";

    export class Configuration implements NodeLLMConfig {
      private _openaiApiKey?: string;
      private _openaiApiBase?: string;
      private _anthropicApiKey?: string;
      private _anthropicApiBase?: string;
      private _geminiApiKey?: string;
      private _geminiApiBase?: string;
      private _deepseekApiKey?: string;
      private _deepseekApiBase?: string;
      private _ollamaApiBase?: string;
      private _openrouterApiKey?: string;
      private _openrouterApiBase?: string;
      private _debug?: boolean;
      private _provider?: string;

      public get openaiApiKey(): string | undefined {
        return this._openaiApiKey ?? process.env.OPENAI_API_KEY?.trim();
      }
      public set openaiApiKey(v: string | undefined) {
        this._openaiApiKey = v;
      }

      public get openaiApiBase(): string | undefined {
        return this._openaiApiBase ?? process.env.OPENAI_API_BASE?.trim();
      }
      public set openaiApiBase(v: string | undefined) {
        this._openaiApiBase = v;
      }

      public get anthropicApiKey(): string | undefined {
        return this._anthropicApiKey ?? process.env.ANTHROPIC_API_KEY?.trim();
      }
      public set anthropicApiKey(v: string | undefined) {
        this._anthropicApiKey = v;
      }

      public get anthropicApiBase(): string | undefined {
        return this._anthropicApiBase ?? process.env.ANTHROPIC_API_BASE?.trim();
      }
      public set anthropicApiBase(v: string | undefined) {
        this._anthropicApiBase = v;
      }

      public get geminiApiKey(): string | undefined {
        return this._geminiApiKey ?? process.env.GEMINI_API_KEY?.trim();
      }
      public set geminiApiKey(v: string | undefined) {
        this._geminiApiKey = v;
      }

      public get geminiApiBase(): string | undefined {
        return this._geminiApiBase ?? process.env.GEMINI_API_BASE?.trim();
      }
      public set geminiApiBase(v: string | undefined) {
        this._geminiApiBase = v;
      }

      public get deepseekApiKey(): string | undefined {
        return this._deepseekApiKey ?? process.env.DEEPSEEK_API_KEY?.trim();
      }
      public set deepseekApiKey(v: string | undefined) {
        this._deepseekApiKey = v;
      }

      public get deepseekApiBase(): string | undefined {
        return this._deepseekApiBase ?? process.env.DEEPSEEK_API_BASE?.trim();
      }
      public set deepseekApiBase(v: string | undefined) {
        this._deepseekApiBase = v;
      }

      public get ollamaApiBase(): string | undefined {
        return this._ollamaApiBase ?? process.env.OLLAMA_API_BASE?.trim() ?? DEFAULT_OLLAMA_BASE_URL;
      }
      public set ollamaApiBase(v: string | undefined) {
        this._ollamaApiBase = v;
      }

      public get openrouterApiKey(): string | undefined {
        return this._openrouterApiKey ?? process.env.OPENROUTER_API_KEY?.trim();
      }
      public set openrouterApiKey(v: string | undefined) {
        this._openrouterApiKey = v;
      }

      public get openrouterApiBase(): string | undefined {
        return this._openrouterApiBase ?? process.env.OPENROUTER_API_BASE?.trim();
      }
      public set openrouterApiBase(v: string | undefined) {
        this._openrouterApiBase = v;
      }

      public get debug(): boolean | undefined {
        return this._debug ?? process.env.NODELLM_DEBUG === "true";
      }
      public set debug(v: boolean | undefined) {
        this._debug = v;
      }

      public get provider(): string | undefined {
        return this._provider ?? process.env.NODELLM_PROVIDER?.trim();
      }
      public set provider(v: string | undefined) {
        this._provider = v;
      }

      public maxToolCalls: number = DEFAULT_MAX_TOOL_CALLS;
      public maxRetries: number = DEFAULT_MAX_RETRIES;
      public requestTimeout: number = DEFAULT_REQUEST_TIMEOUT;
      public maxTokens: number = DEFAULT_MAX_TOKENS;
      public toolExecution: ToolExecutionMode = DEFAULT_TOOL_EXECUTION;

      /**
       * Returns a plain object with all configuration values.
       * This is useful for cloning or serialization.
       * It handles getters (lazy-loaded values) correctly.
       */
      public toPlainObject(): NodeLLMConfig {
        const plain: Record<string, unknown> = { ...(this as unknown as Record<string, unknown>) }; // Capture all enumerable "own" properties (custom keys, overrides)

        // Capture all getters from the class prototype (lazy-loaded values)
        const prototype = Object.getPrototypeOf(this);
        const propertyNames = Object.getOwnPropertyNames(prototype);

        for (const name of propertyNames) {
          if (name === "constructor") continue;

          const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
          if (descriptor && descriptor.get) {
            // Trigger the getter to snapshot the live value (including env fallbacks)
            plain[name] = Reflect.get(this, name);
          }
        }

        return plain as NodeLLMConfig;
      }
    }

    export const config = new Configuration();