# All files / src/providers registry.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 4/4
100% Lines 11/11

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

    63x

    76x
    13x

    63x

    88x

    88x
    1x

    87x

    1x

    3x

    63x

     import { Provider } from "./Provider.js";
    import { registerOpenAIProvider } from "./openai/index.js";
    import { registerAnthropicProvider } from "./anthropic/index.js";
    import { registerGeminiProvider } from "./gemini/index.js";
    import { registerDeepSeekProvider } from "./deepseek/index.js";
    import { registerOllamaProvider } from "./ollama/index.js";
    import { registerOpenRouterProvider } from "./openrouter/index.js";

    import { NodeLLMConfig } from "../config.js";

    type ProviderFactory = (config?: NodeLLMConfig) => Provider;

    class ProviderRegistry {
      private providers = new Map<string, ProviderFactory>();

      /**
       * Register a provider factory
       */
      register(name: string, factory: ProviderFactory): void {
        if (this.providers.has(name)) {
          return;
        }

        this.providers.set(name, factory);
      }

      /**
       * Resolve a provider by name
       */
      resolve(name: string, config?: NodeLLMConfig): Provider {
        const factory = this.providers.get(name);

        if (!factory) {
          throw new Error(`Unknown LLM provider '${name}'`);
        }

        return factory(config);
      }

      /**
       * Check if a provider is registered
       */
      has(name: string): boolean {
        return this.providers.has(name);
      }

      /**
       * Introspection / debugging
       */
      list(): string[] {
        return [...this.providers.keys()];
      }
    }

    /**
     * Global provider registry.
     *
     * @internal
     * This is an internal implementation detail. Use `NodeLLM.registerProvider()`
     * or `createLLM()` instead of accessing this directly.
     *
     * **For custom providers**, use the public API:
     * ```typescript
     * import { NodeLLM, BaseProvider } from '@node-llm/core';
     *
     * class MyProvider extends BaseProvider { ... }
     * NodeLLM.registerProvider("my-provider", () => new MyProvider());
     * ```
     */
    export const providerRegistry = new ProviderRegistry();

    // Exported registration functions (delegates to provider-specific index files)
    export {
      registerOpenAIProvider as ensureOpenAIRegistered,
      registerOpenAIProvider,
      registerAnthropicProvider,
      registerGeminiProvider,
      registerDeepSeekProvider,
      registerOllamaProvider,
      registerOpenRouterProvider
    };