Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import { createOpenAICompatible } from "@ai-sdk/openai-compatible";import { wrapLanguageModel } from "ai";import packageInfo from "../package.json" with { type: "json" };import type { Secret } from "../configuration/secrets.ts";import { MODEL_PROVIDERS, type ModelConfiguration,} from "../shared/model-providers.ts";
/** Protocol SDKs own serialization/tool streaming; the native binding owns transport. */export function createGatewayModel( binding: Ai, configuration: ModelConfiguration, key: Secret,) { const { provider, model } = configuration; const definition = MODEL_PROVIDERS[provider]; const transport = definition.transport; if (transport.kind !== "gateway") throw new Error("Provider does not use AI Gateway"); const gateway = binding.gateway(transport.gatewayId); const endpoint = `${transport.baseURL}/chat/completions`; const fetchThroughGateway: typeof fetch = async (input, init) => { // Even SDK/per-call options cannot redirect the key or change providers. if (String(input) !== endpoint || init?.method !== "POST") throw new Error("Unsupported gateway model request"); init.signal?.throwIfAborted(); const headers = Object.fromEntries(new Headers(init.headers).entries()); delete headers["x-api-key"]; headers.authorization = `Bearer ${key.reveal()}`; headers["user-agent"] = `flarebot/${packageInfo.version}`; headers["cf-aig-skip-cache"] = "true"; headers["cf-aig-collect-log"] = "false"; return gateway.run( { provider: transport.provider, endpoint, headers, query: JSON.parse(String(init.body)), }, { signal: init.signal ?? undefined }, ); }; // Placeholder auth stays within the SDK; the transport injects the actual key. const options = { apiKey: "unused", baseURL: transport.baseURL, fetch: fetchThroughGateway, }; const languageModel = createOpenAICompatible({ ...options, name: provider })( model, ); return wrapLanguageModel({ model: languageModel, providerId: provider, middleware: {}, });}