feat(ai-client): multi-provider compat — native Anthropic/Google + URL tolerance

- TEXT/VISION: add native Anthropic & Google Gemini paths via Vercel AI SDK,
  selectable through TEXT_PROVIDER / VISION_PROVIDER (default openai_compatible)
- IMAGE: expand to openai (gpt-image) / google (Nano Banana) via AI SDK
  alongside the existing Runware task-array and OpenAI-compatible REST paths
- normalizeBaseUrl: tolerate URLs with/without /v1 (or /chat/completions);
  append the per-protocol version segment only for bare hosts
- config: readProvider() reads *_PROVIDER; types: ProviderProtocol + provider?
- deps: @ai-sdk/anthropic, @ai-sdk/google; docs in .env.example + README

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
yuanzonghao
2026-06-04 15:51:53 +08:00
parent a4dc57a1b6
commit 83fd5717e7
10 changed files with 614 additions and 67 deletions
+88 -2
View File
@@ -1,5 +1,10 @@
import type { ProviderConfig } from "@infiplot/types";
import { generateText } from "ai";
import type { LanguageModelUsage, ModelMessage } from "ai";
import { createAnthropic } from "@ai-sdk/anthropic";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import type { ProviderConfig, ProviderProtocol } from "@infiplot/types";
import { fetchWithRetry } from "./fetchWithRetry";
import { normalizeBaseUrl } from "./normalizeUrl";
export type ChatMessage = {
role: "system" | "user" | "assistant";
@@ -57,6 +62,31 @@ function summarizeUsage(tag: string, usage: Usage | undefined): string {
return `[cache] ${tag} prompt=${prompt} completion=${completion} (provider didn't report cache stats)`;
}
// AI SDK 6 unifies cache stats across providers into usage.inputTokenDetails,
// so a single shape covers Anthropic + Gemini (no per-provider probing).
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)`;
}
// text/vision default to the OpenAI-compatible wire protocol when unset.
function resolveTextProtocol(config: ProviderConfig): ProviderProtocol {
return config.provider ?? "openai_compatible";
}
export async function chat(
config: ProviderConfig,
messages: ChatMessage[],
@@ -66,7 +96,63 @@ export async function chat(
tag?: string;
},
): Promise<string> {
const url = `${config.baseUrl.replace(/\/$/, "")}/chat/completions`;
const protocol = resolveTextProtocol(config);
if (protocol === "anthropic" || protocol === "google") {
return chatViaAiSdk(config, messages, opts, protocol);
}
return chatOpenAiCompatible(config, messages, opts);
}
// Native Anthropic / Gemini via the Vercel AI SDK. response_format is not sent
// (Anthropic has no JSON mode); the engine relies on parseJsonLoose downstream,
// matching how it already tolerates loose JSON from every provider.
async function chatViaAiSdk(
config: ProviderConfig,
messages: ChatMessage[],
opts: { temperature?: number; tag?: string } | undefined,
protocol: "anthropic" | "google",
): Promise<string> {
const baseURL = normalizeBaseUrl(config.baseUrl, protocol);
const model =
protocol === "anthropic"
? createAnthropic({ apiKey: config.apiKey, baseURL })(config.model)
: createGoogleGenerativeAI({ apiKey: config.apiKey, baseURL })(
config.model,
);
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;
}
async function chatOpenAiCompatible(
config: ProviderConfig,
messages: ChatMessage[],
opts?: {
temperature?: number;
responseFormat?: "json_object" | "text";
tag?: string;
},
): Promise<string> {
const url = `${normalizeBaseUrl(config.baseUrl, "openai_compatible")}/chat/completions`;
const body: Record<string, unknown> = {
model: config.model,
messages,