57bc6556ab
Eliminate the dual code path (raw fetch vs AI SDK) for text and vision. All providers now go through createLanguageModel() + generateText(), removing chatOpenAiCompatible/analyzeOpenAiCompatible, the manual Usage type, summarizeUsage, and responseFormat plumbing from 8 call sites. Key fix: @ai-sdk/openai v3 defaults to the Responses API (/responses); DeepSeek only supports Chat Completions, so we use .chat() explicitly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
import { generateText } from "ai";
|
|
import type { LanguageModelUsage, ModelMessage } from "ai";
|
|
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
import { createOpenAI } from "@ai-sdk/openai";
|
|
import type { ProviderConfig, ProviderProtocol } from "@infiplot/types";
|
|
import { normalizeBaseUrl } from "./normalizeUrl";
|
|
|
|
export type ChatMessage = {
|
|
role: "system" | "user" | "assistant";
|
|
content: string;
|
|
};
|
|
|
|
// AI SDK 6 unifies cache stats across providers into usage.inputTokenDetails,
|
|
// so a single shape covers Anthropic, Gemini, and OpenAI-compatible providers.
|
|
function summarizeSdkUsage(
|
|
tag: string,
|
|
usage: LanguageModelUsage | undefined,
|
|
): string {
|
|
if (!usage) return `[cache] ${tag} no-usage`;
|
|
const input = usage.inputTokens ?? 0;
|
|
const output = usage.outputTokens ?? 0;
|
|
const read = usage.inputTokenDetails?.cacheReadTokens;
|
|
const write = usage.inputTokenDetails?.cacheWriteTokens;
|
|
if (typeof read === "number" || typeof write === "number") {
|
|
const hit = read ?? 0;
|
|
const create = write ?? 0;
|
|
const rate = input > 0 ? ((hit / input) * 100).toFixed(1) : "n/a";
|
|
return `[cache] ${tag} hit=${hit} create=${create} input=${input} rate=${rate}% completion=${output}`;
|
|
}
|
|
return `[cache] ${tag} input=${input} completion=${output} (provider didn't report cache stats)`;
|
|
}
|
|
|
|
function resolveTextProtocol(config: ProviderConfig): ProviderProtocol {
|
|
return config.provider ?? "openai_compatible";
|
|
}
|
|
|
|
function createLanguageModel(config: ProviderConfig, protocol: ProviderProtocol) {
|
|
const baseURL = normalizeBaseUrl(config.baseUrl, protocol);
|
|
switch (protocol) {
|
|
case "anthropic":
|
|
return createAnthropic({ apiKey: config.apiKey, baseURL })(config.model);
|
|
case "google":
|
|
return createGoogleGenerativeAI({ apiKey: config.apiKey, baseURL })(config.model);
|
|
case "openai_compatible":
|
|
case "openai":
|
|
default:
|
|
return createOpenAI({ apiKey: config.apiKey, baseURL }).chat(config.model);
|
|
}
|
|
}
|
|
|
|
export async function chat(
|
|
config: ProviderConfig,
|
|
messages: ChatMessage[],
|
|
opts?: {
|
|
temperature?: number;
|
|
tag?: string;
|
|
},
|
|
): Promise<string> {
|
|
const protocol = resolveTextProtocol(config);
|
|
const model = createLanguageModel(config, protocol);
|
|
|
|
const system = messages.find((m) => m.role === "system")?.content;
|
|
const convo: ModelMessage[] = messages
|
|
.filter((m) => m.role !== "system")
|
|
.map((m) => ({
|
|
role: m.role as "user" | "assistant",
|
|
content: m.content,
|
|
}));
|
|
|
|
const { text, usage } = await generateText({
|
|
model,
|
|
system,
|
|
messages: convo,
|
|
temperature: opts?.temperature ?? 0.9,
|
|
});
|
|
|
|
console.log(summarizeSdkUsage(opts?.tag ?? "chat", usage));
|
|
|
|
if (typeof text !== "string" || text.length === 0) {
|
|
throw new Error(`Chat API (AI SDK ${protocol}) returned no content.`);
|
|
}
|
|
return text;
|
|
}
|