36 lines
956 B
TypeScript
36 lines
956 B
TypeScript
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
|
|
type OpenRouterEnv = Readonly<Record<string, string | undefined>>;
|
|
|
|
function normalizeOptionalEnvValue(value: string | undefined): string | undefined {
|
|
if (typeof value !== "string") {
|
|
return undefined;
|
|
}
|
|
|
|
const trimmed = value.trim();
|
|
return trimmed === "" ? undefined : trimmed;
|
|
}
|
|
|
|
export function createOpenRouterProvider(
|
|
env: OpenRouterEnv = process.env,
|
|
): ReturnType<typeof createOpenRouter> {
|
|
const apiKey = normalizeOptionalEnvValue(env.OPENROUTER_API_KEY);
|
|
|
|
if (!apiKey) {
|
|
throw new Error("OPENROUTER_API_KEY is required for OpenRouter provider.");
|
|
}
|
|
|
|
const appName = normalizeOptionalEnvValue(env.OPENROUTER_APP_NAME);
|
|
const appUrl = normalizeOptionalEnvValue(env.OPENROUTER_APP_URL);
|
|
|
|
return createOpenRouter({
|
|
apiKey,
|
|
appName,
|
|
appUrl,
|
|
compatibility: "strict",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
}
|