All files / src/providers/openai index.ts

91.66% Statements 11/12
66.66% Branches 4/6
100% Functions 2/2
90.9% Lines 10/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        63x             27x   17x 26x 26x 26x   26x       26x     17x           63x  
import { config as globalConfig } from "../../config.js";
import { providerRegistry } from "../registry.js";
import { OpenAIProvider } from "./OpenAIProvider.js";
 
let registered = false;
 
/**
 * Idempotent registration of the OpenAI provider.
 * Automatically called when using createLLM({ provider: 'openai' })
 */
export function registerOpenAIProvider() {
  if (registered) return;
 
  providerRegistry.register("openai", (config) => {
    const cfg = config || globalConfig;
    const apiKey = cfg.openaiApiKey;
    const baseUrl = cfg.openaiApiBase;
 
    Iif (!apiKey) {
      throw new Error("openaiApiKey is not set in config or OPENAI_API_KEY environment variable");
    }
 
    return new OpenAIProvider({ apiKey, baseUrl });
  });
 
  registered = true;
}
 
/**
 * Alias for registerOpenAIProvider for internal use.
 */
export const ensureOpenAIRegistered = registerOpenAIProvider;