Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import * as Effect from "effect/Effect";import { createOpenAICompatible } from "@ai-sdk/openai-compatible";import { wrapLanguageModel } from "ai";import { withToolMediaResults } from "./model-media.ts";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 = (input, init) => { init?.signal?.throwIfAborted(); return Effect.runPromise( Effect.gen(function* () { // 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 yield* Effect.tryPromise({ try: () => gateway.run( { provider: transport.provider, endpoint, headers, query: JSON.parse(String(init.body)), }, { signal: init.signal ?? undefined }, ), catch: (error) => error, }); }), ); }; // 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 withToolMediaResults( wrapLanguageModel({ model: languageModel, providerId: provider, middleware: {}, }), true, );}