Files
wehub-resource-sync e30e75b5d4
Code Quality / Oxlint + Oxfmt (push) Waiting to run
Code Quality / Template Sync (push) Waiting to run
Code Quality / Build Changed Packages (push) Waiting to run
Code Quality / Test Changed Packages (push) Waiting to run
Deploy Expo Example / Deploy Production (push) Waiting to run
Deploy Ink Example / Deploy Production (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.12) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Waiting to run
Deploy Shadcn Registry / Deploy Production (push) Waiting to run
Template Metrics / LOC + Bundle Size (push) Waiting to run
Changesets / Create Version PR (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:40:13 +08:00

35 lines
1.1 KiB
TypeScript

import { createOpenAI } from "@ai-sdk/openai";
import { withTracing as posthogWithTracing } from "@posthog/ai";
import { resolveModelId } from "@/constants/model";
export const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY!,
baseURL: process.env.OPENAI_BASE_URL!,
});
export function getModel(modelId?: string) {
const raw = typeof modelId === "string" ? modelId.trim() : undefined;
const id = resolveModelId(raw);
if (raw && raw !== id) {
console.warn(
`[ai/provider] invalid model "${raw}", falling back to "${id}"`,
);
}
return openai.chat(id);
}
type AppModel = ReturnType<typeof getModel>;
// @posthog/ai types withTracing against @ai-sdk/provider v2/v3 (LanguageModelV2),
// while AI SDK v7 models are LanguageModelV4; the wrapped model is identical at
// runtime, so the provider-version gap is bridged here at the single boundary.
export function withTracing(
model: AppModel,
client: Parameters<typeof posthogWithTracing>[1],
options: Parameters<typeof posthogWithTracing>[2],
): AppModel {
return posthogWithTracing(model as never, client, options) as AppModel;
}