refactor(core): 任务与模型协议治理 - 拆分服务职责并禁止已发起任务取消
This commit is contained in:
98
web/src/services/api/image-contracts.ts
Normal file
98
web/src/services/api/image-contracts.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
export type AiTextMessage = {
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string | AiTextContentPart[];
|
||||
};
|
||||
|
||||
export type AiTextContentPart = { type: "text"; text: string } | { type: "image_url"; image_url: { url: string } } | { type: "file_url"; file_url: { url: string; name: string; mimeType: string } };
|
||||
|
||||
export type ResponseToolCall = {
|
||||
id: string;
|
||||
type: "function";
|
||||
function: { name: string; arguments: string };
|
||||
thoughtSignature?: string;
|
||||
};
|
||||
|
||||
export type ResponseInputMessage = AiTextMessage | { type: "function_call"; call_id: string; name: string; arguments: string; thoughtSignature?: string } | { role: "tool"; tool_call_id: string; content: string };
|
||||
|
||||
export type ResponseFunctionTool = {
|
||||
type: "function";
|
||||
function: {
|
||||
name: string;
|
||||
description?: string;
|
||||
parameters: Record<string, unknown>;
|
||||
strict?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
export type ToolResponseResult = {
|
||||
content: string;
|
||||
toolCalls: ResponseToolCall[];
|
||||
reasoning?: string;
|
||||
};
|
||||
|
||||
export type ToolChoice = "auto" | "required" | { type: "function"; name: string };
|
||||
|
||||
export type BackendToolRequests = {
|
||||
responses: Record<string, unknown>;
|
||||
chatCompletion: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type ResponseMessageContent = AiTextMessage["content"] | string;
|
||||
export type ResponseInputContent = { type: "input_text"; text: string } | { type: "input_image"; image_url: string } | { type: "input_file"; filename: string; file_data?: string; file_url?: string };
|
||||
export type ResponseInputItem =
|
||||
{ role: "system" | "user" | "assistant"; content: string | ResponseInputContent[] } | { type: "function_call"; call_id: string; name: string; arguments: string } | { type: "function_call_output"; call_id: string; output: string };
|
||||
export type ResponseApiToolDefinition = {
|
||||
type: "function";
|
||||
name: string;
|
||||
description?: string;
|
||||
parameters: Record<string, unknown>;
|
||||
strict?: boolean;
|
||||
};
|
||||
export type ResponseApiOutputItem = { type?: "message"; content?: Array<{ type?: string; text?: string }> } | { type?: "function_call"; id?: string; call_id?: string; name?: string; arguments?: string };
|
||||
export type ResponseApiPayload = {
|
||||
id?: string;
|
||||
output?: ResponseApiOutputItem[];
|
||||
output_text?: string;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
export type ResponseStreamState = { buffer: string; text: string; reasoning: string; payload?: ResponseApiPayload; error?: string };
|
||||
export type ChatCompletionToolCall = { id?: string; type?: "function"; function?: { name?: string; arguments?: string } };
|
||||
export type ChatCompletionPayload = {
|
||||
choices?: Array<{ message?: { content?: string | null; reasoning_content?: string | null; tool_calls?: ChatCompletionToolCall[] } }>;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
export type ChatCompletionStreamToolCall = { id: string; name: string; arguments: string };
|
||||
export type ChatCompletionStreamState = { buffer: string; text: string; reasoning: string; toolCalls: Map<number, ChatCompletionStreamToolCall>; error?: string };
|
||||
|
||||
export type ImageApiResponse = {
|
||||
data?: Array<Record<string, unknown>>;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
|
||||
export type GeminiPart = {
|
||||
text?: string;
|
||||
thought?: boolean;
|
||||
inlineData?: { mimeType?: string; data?: string };
|
||||
inline_data?: { mime_type?: string; mimeType?: string; data?: string };
|
||||
fileData?: { mimeType?: string; fileUri?: string };
|
||||
functionCall?: { id?: string; name?: string; args?: Record<string, unknown> };
|
||||
functionResponse?: { id?: string; name?: string; response?: Record<string, unknown> };
|
||||
thoughtSignature?: string;
|
||||
thought_signature?: string;
|
||||
};
|
||||
export type GeminiContent = { role?: "user" | "model"; parts: GeminiPart[] };
|
||||
export type GeminiPayload = {
|
||||
candidates?: Array<{ content?: { parts?: GeminiPart[] }; finishReason?: string }>;
|
||||
models?: Array<{ name?: string }>;
|
||||
error?: { message?: string };
|
||||
promptFeedback?: { blockReason?: string };
|
||||
};
|
||||
export type GeminiStreamState = { buffer: string; text: string; reasoning: string; toolCalls: ResponseToolCall[]; error?: string };
|
||||
|
||||
export type RequestOptions = { signal?: AbortSignal; promptCacheKey?: string; onReasoning?: (text: string) => void };
|
||||
81
web/src/services/api/image-models.ts
Normal file
81
web/src/services/api/image-models.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { sanitizeChannelModelCatalogItem, type ChannelModelCatalogItem } from "@/lib/channel-model-catalog";
|
||||
import { projectDesktopLocalChannelRuntime } from "@/lib/desktop-local-channel";
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import { readAxiosError, validateGeminiPayload } from "@/services/api/image-response";
|
||||
import { geminiApiUrl, geminiHeaders } from "@/services/api/image-transport";
|
||||
import { buildApiUrl, resolveBackendApiUrl, type AiConfig, type ModelChannel } from "@/stores/use-config-store";
|
||||
|
||||
const defaultGeminiConfig: Pick<AiConfig, "baseUrl" | "apiKey" | "apiFormat" | "model" | "systemPrompt"> = {
|
||||
baseUrl: "https://generativelanguage.googleapis.com",
|
||||
apiKey: "",
|
||||
apiFormat: "gemini",
|
||||
model: "",
|
||||
systemPrompt: "",
|
||||
};
|
||||
|
||||
type GeminiModelPayload = { models?: Array<{ name?: string }> };
|
||||
type OpenAIModelPayload = { data?: Array<{ id?: string }>; error?: { message?: string } };
|
||||
|
||||
export async function fetchImageModels(config: Pick<AiConfig, "baseUrl" | "apiKey" | "apiFormat"> & { allowLocalChannel?: boolean }) {
|
||||
try {
|
||||
if (config.apiFormat === "gemini") {
|
||||
const requestConfig = { ...defaultGeminiConfig, ...config };
|
||||
const request = channelRequest(requestConfig, geminiApiUrl(requestConfig), geminiHeaders(requestConfig));
|
||||
const response = await axios.get<GeminiModelPayload>(request.url, { headers: request.headers, withCredentials: request.credentials === "include" });
|
||||
validateGeminiPayload(response.data);
|
||||
return (response.data.models || [])
|
||||
.map((model) => model.name?.replace(/^models\//, ""))
|
||||
.filter((id): id is string => Boolean(id))
|
||||
.sort((a, b) => a.localeCompare(b));
|
||||
}
|
||||
const request = channelRequest(config, buildApiUrl(config.baseUrl, "/models"), { Authorization: `Bearer ${config.apiKey}` });
|
||||
const response = await axios.get<OpenAIModelPayload>(request.url, { headers: request.headers, withCredentials: request.credentials === "include" });
|
||||
return (response.data.data || [])
|
||||
.map((model) => model.id)
|
||||
.filter((id): id is string => Boolean(id))
|
||||
.sort((a, b) => a.localeCompare(b));
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "读取模型失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export type ChannelModelFetchResult = { models: string[]; catalog: ChannelModelCatalogItem[] };
|
||||
|
||||
export async function fetchChannelModels(channel: ModelChannel, viaBackend = false): Promise<ChannelModelFetchResult> {
|
||||
const runtimeChannel = projectDesktopLocalChannelRuntime(channel);
|
||||
if (!viaBackend) {
|
||||
const models = await fetchImageModels({ baseUrl: runtimeChannel.baseUrl, allowLocalChannel: runtimeChannel.allowLocalChannel === true, apiKey: runtimeChannel.apiKey, apiFormat: runtimeChannel.apiFormat });
|
||||
return { models, catalog: models.map((id) => ({ id })) };
|
||||
}
|
||||
try {
|
||||
// 登录态由同源后端代取模型目录,避免每个 OpenAI 兼容服务分别维护浏览器 CORS 白名单。
|
||||
const response = await axios.post<{ code?: number; data?: { models?: Array<string | ChannelModelCatalogItem> }; msg?: string }>(
|
||||
resolveBackendApiUrl("/api/ai/models"),
|
||||
{
|
||||
baseUrl: runtimeChannel.baseUrl,
|
||||
allowLocalChannel: runtimeChannel.allowLocalChannel === true,
|
||||
apiKey: runtimeChannel.apiKey,
|
||||
apiFormat: runtimeChannel.apiFormat,
|
||||
headers: runtimeChannel.headers,
|
||||
},
|
||||
{ withCredentials: true },
|
||||
);
|
||||
if (typeof response.data.code === "number" && response.data.code !== 0) {
|
||||
throw new Error(response.data.msg || "读取模型失败");
|
||||
}
|
||||
const catalog = new Map<string, ChannelModelCatalogItem>();
|
||||
for (const item of response.data.data?.models || []) {
|
||||
const entry = typeof item === "string" ? sanitizeChannelModelCatalogItem({ id: item }) : sanitizeChannelModelCatalogItem(item);
|
||||
if (!entry) continue;
|
||||
const existing = catalog.get(entry.id);
|
||||
catalog.set(entry.id, existing || entry);
|
||||
}
|
||||
const models = Array.from(catalog.keys()).sort((a, b) => a.localeCompare(b));
|
||||
const sortedCatalog = Array.from(catalog.values()).sort((a, b) => a.id.localeCompare(b.id));
|
||||
return { models, catalog: sortedCatalog };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "读取模型失败"));
|
||||
}
|
||||
}
|
||||
161
web/src/services/api/image-protocols.ts
Normal file
161
web/src/services/api/image-protocols.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
import type { AiConfig } from "@/stores/use-config-store";
|
||||
import type { BackendToolRequests, GeminiContent, GeminiPart, ResponseApiToolDefinition, ResponseFunctionTool, ResponseInputContent, ResponseInputItem, ResponseInputMessage, ResponseMessageContent, ToolChoice } from "@/services/api/image-contracts";
|
||||
|
||||
export function withSystemMessage<T extends ResponseInputMessage>(config: AiConfig, messages: T[]): ResponseInputMessage[] {
|
||||
const systemPrompt = config.systemPrompt.trim();
|
||||
return systemPrompt ? [{ role: "system" as const, content: systemPrompt }, ...messages] : messages;
|
||||
}
|
||||
|
||||
export function toResponseInput(messages: ResponseInputMessage[]): ResponseInputItem[] {
|
||||
return messages.flatMap((message): ResponseInputItem[] => {
|
||||
if ("type" in message) return [message];
|
||||
if (message.role === "tool") return [{ type: "function_call_output", call_id: message.tool_call_id, output: message.content }];
|
||||
return [{ role: message.role, content: toResponseContent(message.content || "") }];
|
||||
});
|
||||
}
|
||||
|
||||
function toResponseContent(content: ResponseMessageContent): string | ResponseInputContent[] {
|
||||
if (!Array.isArray(content)) return String(content || "");
|
||||
return content.map((item) => {
|
||||
if (item.type === "text") return { type: "input_text" as const, text: item.text };
|
||||
if (item.type === "image_url") return { type: "input_image" as const, image_url: item.image_url.url };
|
||||
return item.file_url.url.startsWith("data:") ? { type: "input_file" as const, filename: item.file_url.name, file_data: item.file_url.url } : { type: "input_file" as const, filename: item.file_url.name, file_url: item.file_url.url };
|
||||
});
|
||||
}
|
||||
|
||||
export function toResponseTool(tool: ResponseFunctionTool): ResponseApiToolDefinition {
|
||||
return {
|
||||
type: "function",
|
||||
name: tool.function.name,
|
||||
description: tool.function.description,
|
||||
parameters: tool.function.parameters,
|
||||
strict: tool.function.strict,
|
||||
};
|
||||
}
|
||||
|
||||
export function toChatCompletionMessages(messages: ResponseInputMessage[]) {
|
||||
const result: Array<Record<string, unknown>> = [];
|
||||
for (let index = 0; index < messages.length;) {
|
||||
const message = messages[index];
|
||||
if ("type" in message) {
|
||||
const toolCalls: Array<Record<string, unknown>> = [];
|
||||
while (index < messages.length && "type" in messages[index]) {
|
||||
const call = messages[index] as Extract<ResponseInputMessage, { type: "function_call" }>;
|
||||
toolCalls.push({ id: call.call_id, type: "function", function: { name: call.name, arguments: call.arguments } });
|
||||
index += 1;
|
||||
}
|
||||
result.push({ role: "assistant", content: null, tool_calls: toolCalls });
|
||||
continue;
|
||||
}
|
||||
if (message.role === "tool") result.push({ role: "tool", tool_call_id: message.tool_call_id, content: message.content });
|
||||
else result.push({ role: message.role, content: toChatCompletionContent(message.content) });
|
||||
index += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function toChatCompletionContent(content: ResponseMessageContent) {
|
||||
if (!Array.isArray(content)) return content;
|
||||
return content.map((item) => {
|
||||
if (item.type !== "file_url") return item;
|
||||
const file = item.file_url.url.startsWith("data:") ? { filename: item.file_url.name, file_data: item.file_url.url } : { filename: item.file_url.name, file_url: item.file_url.url };
|
||||
return { type: "file", file };
|
||||
});
|
||||
}
|
||||
|
||||
export function toChatCompletionToolChoice(toolChoice: ToolChoice) {
|
||||
return typeof toolChoice === "object" ? { type: "function", function: { name: toolChoice.name } } : toolChoice;
|
||||
}
|
||||
|
||||
export function buildBackendToolRequests(messages: ResponseInputMessage[], tools: ResponseFunctionTool[], toolChoice: ToolChoice): BackendToolRequests {
|
||||
return {
|
||||
responses: {
|
||||
input: toResponseInput(messages),
|
||||
tools: tools.map(toResponseTool),
|
||||
tool_choice: toolChoice,
|
||||
parallel_tool_calls: false,
|
||||
},
|
||||
chatCompletion: {
|
||||
messages: toChatCompletionMessages(messages),
|
||||
tools,
|
||||
tool_choice: toChatCompletionToolChoice(toolChoice),
|
||||
parallel_tool_calls: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function toGeminiBody(config: AiConfig, messages: ResponseInputMessage[], extra?: Record<string, unknown>) {
|
||||
const systemText = [config.systemPrompt.trim(), ...messages.flatMap((message) => (!("type" in message) && message.role === "system" ? [geminiTextContent(message.content)] : []))].filter(Boolean).join("\n\n");
|
||||
const contents = toGeminiContents(messages.filter((message) => ("type" in message ? true : message.role !== "system")));
|
||||
return {
|
||||
contents,
|
||||
...(systemText ? { systemInstruction: { parts: [{ text: systemText }] } } : {}),
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
function toGeminiContents(messages: ResponseInputMessage[]): GeminiContent[] {
|
||||
const callNameById = new Map<string, string>();
|
||||
return messages.flatMap((message): GeminiContent[] => {
|
||||
if ("type" in message) {
|
||||
callNameById.set(message.call_id, message.name);
|
||||
return [{ role: "model", parts: [{ functionCall: { id: message.call_id, name: message.name, args: jsonObject(message.arguments) }, ...(message.thoughtSignature ? { thoughtSignature: message.thoughtSignature } : {}) }] }];
|
||||
}
|
||||
if (message.role === "tool") {
|
||||
const name = callNameById.get(message.tool_call_id) || "tool_result";
|
||||
return [{ role: "user", parts: [{ functionResponse: { id: message.tool_call_id, name, response: { result: jsonValue(message.content) } } }] }];
|
||||
}
|
||||
return [{ role: message.role === "assistant" ? "model" : "user", parts: toGeminiParts(message.content) }];
|
||||
});
|
||||
}
|
||||
|
||||
function toGeminiParts(content: ResponseMessageContent): GeminiPart[] {
|
||||
if (!Array.isArray(content)) return [{ text: String(content || "") }];
|
||||
return content.map((item) => {
|
||||
if (item.type === "text") return { text: item.text };
|
||||
if (item.type === "image_url") return toGeminiFilePart(item.image_url.url, "image/png");
|
||||
return toGeminiFilePart(item.file_url.url, item.file_url.mimeType);
|
||||
});
|
||||
}
|
||||
|
||||
function toGeminiFilePart(url: string, mimeType: string): GeminiPart {
|
||||
const match = url.match(/^data:([^;,]+);base64,(.+)$/);
|
||||
if (match) return { inlineData: { mimeType: match[1], data: match[2] } };
|
||||
return { fileData: { fileUri: url, mimeType } };
|
||||
}
|
||||
|
||||
function geminiTextContent(content: ResponseMessageContent) {
|
||||
if (!Array.isArray(content)) return String(content || "");
|
||||
return content.map((item) => (item.type === "text" ? item.text : item.type === "image_url" ? item.image_url.url : `${item.file_url.name}: ${item.file_url.url}`)).join("\n");
|
||||
}
|
||||
|
||||
function jsonObject(value: string): Record<string, unknown> {
|
||||
const parsed = jsonValue(value);
|
||||
return isRecord(parsed) ? parsed : {};
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
||||
}
|
||||
|
||||
function jsonValue(value: string): unknown {
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
export function toGeminiToolOptions(tools: ResponseFunctionTool[], toolChoice: ToolChoice) {
|
||||
if (!tools.length) return {};
|
||||
const functionDeclarations = tools.map((tool) => ({
|
||||
name: tool.function.name,
|
||||
description: tool.function.description,
|
||||
parameters: tool.function.parameters,
|
||||
}));
|
||||
const functionCallingConfig = typeof toolChoice === "object" ? { mode: "ANY", allowedFunctionNames: [toolChoice.name] } : { mode: toolChoice === "required" ? "ANY" : "AUTO" };
|
||||
return {
|
||||
tools: [{ functionDeclarations }],
|
||||
toolConfig: { functionCallingConfig },
|
||||
};
|
||||
}
|
||||
301
web/src/services/api/image-response.ts
Normal file
301
web/src/services/api/image-response.ts
Normal file
@@ -0,0 +1,301 @@
|
||||
import axios from "axios";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
import type {
|
||||
ChatCompletionPayload,
|
||||
ChatCompletionStreamState,
|
||||
GeminiPart,
|
||||
GeminiPayload,
|
||||
GeminiStreamState,
|
||||
ImageApiResponse,
|
||||
ResponseApiOutputItem,
|
||||
ResponseApiPayload,
|
||||
ResponseStreamState,
|
||||
ResponseToolCall,
|
||||
ToolResponseResult,
|
||||
} from "@/services/api/image-contracts";
|
||||
|
||||
function resolveImageDataUrl(item: Record<string, unknown>) {
|
||||
if (typeof item.b64_json === "string" && item.b64_json) return `data:image/png;base64,${item.b64_json}`;
|
||||
if (typeof item.url === "string" && item.url) return item.url;
|
||||
return null;
|
||||
}
|
||||
|
||||
export function parseImagePayload(payload: ImageApiResponse) {
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
const images =
|
||||
payload.data
|
||||
?.map(resolveImageDataUrl)
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.map((dataUrl) => ({ id: nanoid(), dataUrl })) || [];
|
||||
if (images.length === 0) throw new Error("接口没有返回图片");
|
||||
return images;
|
||||
}
|
||||
|
||||
export function readAxiosError(error: unknown, fallback: string) {
|
||||
if (axios.isCancel(error)) return "请求已取消";
|
||||
if (axios.isAxiosError<{ error?: { message?: string }; msg?: string; code?: number }>(error)) {
|
||||
const responseData = error.response?.data;
|
||||
return responseData?.msg || responseData?.error?.message || readStatusError(error.response?.status, fallback);
|
||||
}
|
||||
if (error instanceof DOMException && error.name === "AbortError") return "请求已取消";
|
||||
return error instanceof Error ? error.message : fallback;
|
||||
}
|
||||
|
||||
export function readStatusError(status: number | undefined, fallback: string) {
|
||||
if (status === 401 || status === 403) return "鉴权失败,请检查 API Key、套餐权限或模型权限";
|
||||
if (status === 429) return "请求被限流或额度不足,请稍后重试";
|
||||
return status ? `${fallback}:${status}` : fallback;
|
||||
}
|
||||
|
||||
export function parseChatCompletionPayload(payload: ChatCompletionPayload): ToolResponseResult {
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
const message = payload.choices?.[0]?.message;
|
||||
const toolCalls = (message?.tool_calls || [])
|
||||
.map((call) => ({
|
||||
id: call.id || "",
|
||||
type: "function" as const,
|
||||
function: { name: call.function?.name || "", arguments: call.function?.arguments || "{}" },
|
||||
}))
|
||||
.filter((call) => call.id && call.function.name);
|
||||
return { content: message?.content || "", toolCalls, ...(message?.reasoning_content ? { reasoning: message.reasoning_content } : {}) };
|
||||
}
|
||||
|
||||
export function parseToolResponse(payload: ResponseApiPayload): ToolResponseResult {
|
||||
const output = payload.output || [];
|
||||
const content =
|
||||
payload.output_text ||
|
||||
output
|
||||
.flatMap((item) => (item.type === "message" ? item.content || [] : []))
|
||||
.map((item) => item.text || "")
|
||||
.join("");
|
||||
const toolCalls = output
|
||||
.filter((item): item is Extract<ResponseApiOutputItem, { type?: "function_call" }> => item.type === "function_call")
|
||||
.map((item) => ({
|
||||
id: item.call_id || item.id || "",
|
||||
type: "function" as const,
|
||||
function: { name: item.name || "", arguments: item.arguments || "{}" },
|
||||
}))
|
||||
.filter((item) => item.id && item.function.name);
|
||||
return { content, toolCalls };
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
||||
}
|
||||
|
||||
function stringValue(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
export function responseErrorMessage(value: unknown) {
|
||||
if (!isRecord(value)) return "";
|
||||
const error = isRecord(value.error) ? value.error : undefined;
|
||||
const response = isRecord(value.response) ? value.response : undefined;
|
||||
const responseError = response && isRecord(response.error) ? response.error : undefined;
|
||||
return stringValue(value.msg) || stringValue(error?.message) || stringValue(responseError?.message);
|
||||
}
|
||||
|
||||
export function validateResponsePayload(payload: ResponseApiPayload) {
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
}
|
||||
|
||||
export function validateGeminiPayload(payload: GeminiPayload) {
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
if (payload.promptFeedback?.blockReason) throw new Error(`Gemini 拒绝了本次请求:${payload.promptFeedback.blockReason}`);
|
||||
}
|
||||
|
||||
export async function readFetchError(response: Response, fallback: string) {
|
||||
const text = await response.text();
|
||||
if (!text) return readStatusError(response.status, fallback);
|
||||
try {
|
||||
return responseErrorMessage(JSON.parse(text)) || readStatusError(response.status, fallback);
|
||||
} catch {
|
||||
return text.slice(0, 300) || readStatusError(response.status, fallback);
|
||||
}
|
||||
}
|
||||
|
||||
export async function readJsonPayload<T>(response: Response, fallback: string): Promise<T> {
|
||||
const text = await response.text();
|
||||
try {
|
||||
return JSON.parse(text) as T;
|
||||
} catch {
|
||||
if (/^\s*(?:<!doctype|<html)/i.test(text)) throw new Error("后端代理返回了前端网页,请检查 VITE_CANVAS_BACKEND_URL 和反向代理配置");
|
||||
throw new Error(`${fallback}:接口没有返回有效 JSON`);
|
||||
}
|
||||
}
|
||||
|
||||
function parseEventBlock(block: string) {
|
||||
const data = block
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).replace(/^ /, ""))
|
||||
.join("\n")
|
||||
.trim();
|
||||
return data && data !== "[DONE]" ? data : undefined;
|
||||
}
|
||||
|
||||
function consumeResponseStreamBlock(block: string, state: ResponseStreamState, onDelta?: (text: string) => void, onReasoning?: (text: string) => void) {
|
||||
const data = parseEventBlock(block);
|
||||
if (!data) return;
|
||||
const event = JSON.parse(data) as Record<string, unknown>;
|
||||
const type = stringValue(event.type);
|
||||
const errorMessage = responseErrorMessage(event);
|
||||
if (errorMessage) state.error = errorMessage;
|
||||
if (type === "response.output_text.delta" && typeof event.delta === "string") {
|
||||
state.text += event.delta;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
if (type === "response.output_text.done" && !state.text && typeof event.text === "string") {
|
||||
state.text = event.text;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
if (["response.reasoning.delta", "response.reasoning_text.delta", "response.reasoning_summary_text.delta"].includes(type) && typeof event.delta === "string") {
|
||||
state.reasoning += event.delta;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
if (["response.reasoning.done", "response.reasoning_text.done", "response.reasoning_summary_text.done"].includes(type) && !state.reasoning && typeof event.text === "string") {
|
||||
state.reasoning = event.text;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
if (type === "response.completed" && isRecord(event.response)) state.payload = event.response as ResponseApiPayload;
|
||||
else if (Array.isArray(event.output)) state.payload = event as ResponseApiPayload;
|
||||
}
|
||||
|
||||
export function consumeResponseStreamText(state: ResponseStreamState, text: string, onDelta?: (text: string) => void, onReasoning?: (text: string) => void, flush = false) {
|
||||
state.buffer += text;
|
||||
for (;;) {
|
||||
const match = state.buffer.match(/\r?\n\r?\n/);
|
||||
if (!match) break;
|
||||
const index = match.index ?? 0;
|
||||
consumeResponseStreamBlock(state.buffer.slice(0, index), state, onDelta, onReasoning);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeResponseStreamBlock(state.buffer, state, onDelta, onReasoning);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
function consumeChatCompletionStreamBlock(block: string, state: ChatCompletionStreamState, onDelta?: (text: string) => void, onReasoning?: (text: string) => void) {
|
||||
const data = parseEventBlock(block);
|
||||
if (!data) return;
|
||||
const event = JSON.parse(data) as Record<string, unknown>;
|
||||
const errorMessage = responseErrorMessage(event);
|
||||
if (errorMessage) state.error = errorMessage;
|
||||
const choices = Array.isArray(event.choices) ? event.choices : [];
|
||||
const choice = isRecord(choices[0]) ? choices[0] : undefined;
|
||||
const delta = choice && isRecord(choice.delta) ? choice.delta : undefined;
|
||||
if (!delta) return;
|
||||
if (typeof delta.content === "string") {
|
||||
state.text += delta.content;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
const reasoningDelta = stringValue(delta.reasoning_content) || stringValue(delta.reasoning) || stringValue(delta.reasoning_text);
|
||||
if (reasoningDelta) {
|
||||
state.reasoning += reasoningDelta;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
const chunks = Array.isArray(delta.tool_calls) ? delta.tool_calls : [];
|
||||
chunks.forEach((value, fallbackIndex) => {
|
||||
if (!isRecord(value)) return;
|
||||
const callIndex = typeof value.index === "number" ? value.index : fallbackIndex;
|
||||
const current = state.toolCalls.get(callIndex) || { id: "", name: "", arguments: "" };
|
||||
const fn = isRecord(value.function) ? value.function : undefined;
|
||||
state.toolCalls.set(callIndex, {
|
||||
id: stringValue(value.id) || current.id,
|
||||
name: stringValue(fn?.name) || current.name,
|
||||
arguments: current.arguments + stringValue(fn?.arguments),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function consumeChatCompletionStreamText(state: ChatCompletionStreamState, text: string, onDelta?: (text: string) => void, onReasoning?: (text: string) => void, flush = false) {
|
||||
state.buffer += text;
|
||||
for (;;) {
|
||||
const match = state.buffer.match(/\r?\n\r?\n/);
|
||||
if (!match) break;
|
||||
const index = match.index ?? 0;
|
||||
consumeChatCompletionStreamBlock(state.buffer.slice(0, index), state, onDelta, onReasoning);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeChatCompletionStreamBlock(state.buffer, state, onDelta, onReasoning);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
function consumeGeminiStreamBlock(block: string, state: GeminiStreamState, onDelta?: (text: string) => void, onReasoning?: (text: string) => void) {
|
||||
const data = parseEventBlock(block);
|
||||
if (!data) return;
|
||||
const result = parseGeminiToolResponse(JSON.parse(data) as GeminiPayload);
|
||||
if (result.reasoning) {
|
||||
state.reasoning += result.reasoning;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
if (result.content) {
|
||||
state.text += result.content;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
state.toolCalls.push(...result.toolCalls);
|
||||
}
|
||||
|
||||
export function consumeGeminiStreamText(state: GeminiStreamState, text: string, onDelta?: (text: string) => void, onReasoning?: (text: string) => void, flush = false) {
|
||||
state.buffer += text;
|
||||
for (;;) {
|
||||
const match = state.buffer.match(/\r?\n\r?\n/);
|
||||
if (!match) break;
|
||||
const index = match.index ?? 0;
|
||||
consumeGeminiStreamBlock(state.buffer.slice(0, index), state, onDelta, onReasoning);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeGeminiStreamBlock(state.buffer, state, onDelta, onReasoning);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
export function parseGeminiToolResponse(payload: GeminiPayload): ToolResponseResult {
|
||||
validateGeminiPayload(payload);
|
||||
const parts = payload.candidates?.flatMap((candidate) => candidate.content?.parts || []) || [];
|
||||
const content = parts
|
||||
.filter((part) => !part.thought)
|
||||
.map((part) => part.text || "")
|
||||
.join("");
|
||||
const reasoning = parts
|
||||
.filter((part) => part.thought)
|
||||
.map((part) => part.text || "")
|
||||
.join("");
|
||||
const toolCalls = parts
|
||||
.map((part) => part.functionCall)
|
||||
.filter((call): call is NonNullable<GeminiPart["functionCall"]> => Boolean(call?.name))
|
||||
.map((call) => {
|
||||
const part = parts.find((item) => item.functionCall === call);
|
||||
const thoughtSignature = part?.thoughtSignature || part?.thought_signature;
|
||||
return {
|
||||
id: call.id || nanoid(),
|
||||
type: "function" as const,
|
||||
function: { name: call.name || "", arguments: JSON.stringify(call.args || {}) },
|
||||
...(thoughtSignature ? { thoughtSignature } : {}),
|
||||
};
|
||||
});
|
||||
return { content, toolCalls, ...(reasoning ? { reasoning } : {}) };
|
||||
}
|
||||
|
||||
export function parseGeminiImagePayload(payload: GeminiPayload) {
|
||||
validateGeminiPayload(payload);
|
||||
const images =
|
||||
payload.candidates
|
||||
?.flatMap((candidate) => candidate.content?.parts || [])
|
||||
.map((part) => {
|
||||
const inlineData = part.inlineData || (part.inline_data ? { mimeType: part.inline_data.mimeType || part.inline_data.mime_type, data: part.inline_data.data } : undefined);
|
||||
if (inlineData?.data) return `data:${inlineData.mimeType || "image/png"};base64,${inlineData.data}`;
|
||||
return part.fileData?.fileUri || null;
|
||||
})
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.map((dataUrl) => ({ id: nanoid(), dataUrl })) || [];
|
||||
if (!images.length) throw new Error("Gemini 接口没有返回图片");
|
||||
return images;
|
||||
}
|
||||
114
web/src/services/api/image-streaming.ts
Normal file
114
web/src/services/api/image-streaming.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import type { ChatCompletionPayload, ChatCompletionStreamState, GeminiPayload, GeminiStreamState, RequestOptions, ResponseApiPayload, ResponseStreamState, ToolResponseResult } from "@/services/api/image-contracts";
|
||||
import type { AiConfig } from "@/stores/use-config-store";
|
||||
import {
|
||||
consumeChatCompletionStreamText,
|
||||
consumeGeminiStreamText,
|
||||
consumeResponseStreamText,
|
||||
parseChatCompletionPayload,
|
||||
parseGeminiToolResponse,
|
||||
parseToolResponse,
|
||||
readFetchError,
|
||||
readJsonPayload,
|
||||
validateResponsePayload,
|
||||
} from "@/services/api/image-response";
|
||||
import { aiApiUrl, aiHeaders, geminiApiUrl, geminiHeaders } from "@/services/api/image-transport";
|
||||
|
||||
export async function requestStreamingResponse(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const request = channelRequest(config, aiApiUrl(config, "/responses"), { ...aiHeaders(config, "application/json"), Accept: "text/event-stream" });
|
||||
const response = await fetch(request.url, {
|
||||
method: "POST",
|
||||
headers: request.headers,
|
||||
body: JSON.stringify({ ...body, stream: true }),
|
||||
signal: options?.signal,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
if (!response.body) {
|
||||
const payload = (await response.json()) as ResponseApiPayload;
|
||||
validateResponsePayload(payload);
|
||||
return parseToolResponse(payload);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: ResponseStreamState = { buffer: "", text: "", reasoning: "" };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeResponseStreamText(state, decoder.decode(value, { stream: true }), onDelta, options?.onReasoning);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeResponseStreamText(state, decoder.decode(), onDelta, options?.onReasoning, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
if (!state.payload) return { content: state.text, toolCalls: [], ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
validateResponsePayload(state.payload);
|
||||
const result = parseToolResponse(state.payload);
|
||||
return { ...result, content: state.text || result.content, ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
}
|
||||
|
||||
export async function requestStreamingChatCompletion(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const request = channelRequest(config, aiApiUrl(config, "/chat/completions"), { ...aiHeaders(config, "application/json"), Accept: "text/event-stream" });
|
||||
const response = await fetch(request.url, {
|
||||
method: "POST",
|
||||
headers: request.headers,
|
||||
body: JSON.stringify({ ...body, stream: true }),
|
||||
signal: options?.signal,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
if (!response.body || !contentType.includes("text/event-stream")) {
|
||||
const result = parseChatCompletionPayload(await readJsonPayload<ChatCompletionPayload>(response, "请求失败"));
|
||||
if (result.reasoning) options?.onReasoning?.(result.reasoning);
|
||||
return result;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: ChatCompletionStreamState = { buffer: "", text: "", reasoning: "", toolCalls: new Map() };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeChatCompletionStreamText(state, decoder.decode(value, { stream: true }), onDelta, options?.onReasoning);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeChatCompletionStreamText(state, decoder.decode(), onDelta, options?.onReasoning, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
const toolCalls = Array.from(state.toolCalls.entries())
|
||||
.sort(([left], [right]) => left - right)
|
||||
.map(([, call]) => ({ id: call.id, type: "function" as const, function: { name: call.name, arguments: call.arguments || "{}" } }))
|
||||
.filter((call) => call.id && call.function.name);
|
||||
return { content: state.text, toolCalls, ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
}
|
||||
|
||||
export async function requestGeminiStreamingResponse(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const request = channelRequest(config, `${geminiApiUrl(config, "streamGenerateContent")}?alt=sse`, geminiHeaders(config));
|
||||
const response = await fetch(request.url, {
|
||||
method: "POST",
|
||||
headers: request.headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: options?.signal,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
if (!response.body) {
|
||||
const payload = (await response.json()) as GeminiPayload;
|
||||
const result = parseGeminiToolResponse(payload);
|
||||
if (result.reasoning) options?.onReasoning?.(result.reasoning);
|
||||
return result;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: GeminiStreamState = { buffer: "", text: "", reasoning: "", toolCalls: [] };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeGeminiStreamText(state, decoder.decode(value, { stream: true }), onDelta, options?.onReasoning);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeGeminiStreamText(state, decoder.decode(), onDelta, options?.onReasoning, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
return { content: state.text, toolCalls: state.toolCalls, ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
}
|
||||
75
web/src/services/api/image-transport.ts
Normal file
75
web/src/services/api/image-transport.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { buildApiUrl, isSystemProxyBaseUrl, resolveBackendApiUrl, type AiConfig } from "@/stores/use-config-store";
|
||||
import { createClientId } from "@/lib/client-id";
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import type { GeminiPayload, ImageApiResponse, RequestOptions } from "@/services/api/image-contracts";
|
||||
|
||||
export function aiApiUrl(config: Pick<AiConfig, "baseUrl">, path: string) {
|
||||
return buildApiUrl(config.baseUrl, path);
|
||||
}
|
||||
|
||||
export function aiHeaders(config: Pick<AiConfig, "apiKey" | "baseUrl">, contentType?: string) {
|
||||
return {
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
...(contentType ? { "Content-Type": contentType } : {}),
|
||||
...(isSystemProxyBaseUrl(config.baseUrl) ? { "X-Canvas-Scene": "image", "X-Idempotency-Key": createClientId() } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export async function postVolcengineArkImage(config: Parameters<typeof channelRequest>[0], payload: Record<string, unknown>, options?: RequestOptions) {
|
||||
const upstreamUrl = aiApiUrl(config, "/images/generations");
|
||||
const request = channelRequest(config, upstreamUrl, aiHeaders(config, "application/json"));
|
||||
return (
|
||||
await axios.post<ImageApiResponse>(request.url, payload, {
|
||||
headers: request.headers,
|
||||
withCredentials: request.credentials === "include",
|
||||
signal: options?.signal,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
export function geminiBaseUrl(config: Pick<AiConfig, "baseUrl">) {
|
||||
const normalizedBaseUrl = resolveBackendApiUrl(config.baseUrl).replace(/\/+$/, "");
|
||||
const lowerBaseUrl = normalizedBaseUrl.toLowerCase();
|
||||
return isSystemProxyBaseUrl(normalizedBaseUrl) || lowerBaseUrl.endsWith("/v1") || lowerBaseUrl.endsWith("/v1beta") ? normalizedBaseUrl : `${normalizedBaseUrl}/v1beta`;
|
||||
}
|
||||
|
||||
export function geminiModelName(model: string) {
|
||||
return model.trim().replace(/^models\//, "");
|
||||
}
|
||||
|
||||
export function geminiApiUrl(config: Pick<AiConfig, "baseUrl" | "model">, action?: "generateContent" | "streamGenerateContent") {
|
||||
const baseUrl = geminiBaseUrl(config);
|
||||
if (!action) return `${baseUrl}/models`;
|
||||
return `${baseUrl}/models/${encodeURIComponent(geminiModelName(config.model))}:${action}`;
|
||||
}
|
||||
|
||||
export function geminiHeaders(config: Pick<AiConfig, "apiKey">) {
|
||||
return {
|
||||
"x-goog-api-key": config.apiKey,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
}
|
||||
|
||||
export async function postChannelJSON<T>(config: Parameters<typeof channelRequest>[0], upstreamUrl: string, body: unknown, options?: RequestOptions) {
|
||||
const request = channelRequest(config, upstreamUrl, aiHeaders(config, "application/json"));
|
||||
return (
|
||||
await axios.post<T>(request.url, body, {
|
||||
headers: request.headers,
|
||||
withCredentials: request.credentials === "include",
|
||||
signal: options?.signal,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
export async function postGeminiJSON(config: Parameters<typeof channelRequest>[0] & Pick<AiConfig, "model">, body: unknown, options?: RequestOptions) {
|
||||
const request = channelRequest(config, geminiApiUrl(config, "generateContent"), geminiHeaders(config));
|
||||
return (
|
||||
await axios.post<GeminiPayload>(request.url, body, {
|
||||
headers: request.headers,
|
||||
withCredentials: request.credentials === "include",
|
||||
signal: options?.signal,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
140
web/src/services/api/image-validation.ts
Normal file
140
web/src/services/api/image-validation.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { imageSizeRequest, type ImageCapabilityConfig } from "@/lib/model-capabilities";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
|
||||
const QUALITY_BASE: Record<string, number> = {
|
||||
low: 1024,
|
||||
medium: 2048,
|
||||
high: 2880,
|
||||
standard: 1024,
|
||||
hd: 2048,
|
||||
};
|
||||
const QUALITY_ALIASES: Record<string, string> = {
|
||||
"1k": "low",
|
||||
"2k": "medium",
|
||||
"4k": "high",
|
||||
};
|
||||
const DEFAULT_IMAGE_SHORT_SIDE = 1024;
|
||||
const IMAGE_SIZE_STEP = 16;
|
||||
const IMAGE_MIN_PIXELS = 655360;
|
||||
const IMAGE_MAX_PIXELS = 8294400;
|
||||
const IMAGE_MAX_EDGE = 3840;
|
||||
const IMAGE_MAX_RATIO = 3;
|
||||
const VOLCENGINE_ARK_IMAGE_MAX_PIXELS = 4624220;
|
||||
|
||||
export function normalizeQuality(quality: string) {
|
||||
const value = quality.trim().toLowerCase();
|
||||
const normalized = QUALITY_ALIASES[value] || value;
|
||||
return QUALITY_BASE[normalized] ? normalized : undefined;
|
||||
}
|
||||
|
||||
/** grok2api / xAI Imagine:画布 quality 映射为 resolution(1k/2k)。 */
|
||||
export function normalizeGrokImageResolution(quality: string | undefined) {
|
||||
const value = (quality || "").trim().toLowerCase();
|
||||
if (!value || value === "auto") return undefined;
|
||||
if (value === "1k" || value === "low" || value === "standard") return "1k";
|
||||
if (value === "2k" || value === "medium" || value === "hd" || value === "high" || value === "4k") return "2k";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Map "quality + ratio" to an explicit pixel dimension like "3840x2160". */
|
||||
export function resolveSize(quality: string | undefined, ratio: string): string {
|
||||
const parsedRatio = parseImageRatio(ratio);
|
||||
const basePixels = quality ? QUALITY_BASE[quality] : undefined;
|
||||
const isLandscape = parsedRatio.width >= parsedRatio.height;
|
||||
const longRatio = isLandscape ? parsedRatio.width / parsedRatio.height : parsedRatio.height / parsedRatio.width;
|
||||
let longSide: number;
|
||||
let shortSide: number;
|
||||
|
||||
if (basePixels) {
|
||||
const targetPixels = basePixels * basePixels;
|
||||
const longSideRaw = Math.sqrt(targetPixels * longRatio);
|
||||
longSide = Math.floor(longSideRaw / IMAGE_SIZE_STEP) * IMAGE_SIZE_STEP;
|
||||
shortSide = Math.round(longSide / longRatio / IMAGE_SIZE_STEP) * IMAGE_SIZE_STEP;
|
||||
} else {
|
||||
shortSide = DEFAULT_IMAGE_SHORT_SIDE;
|
||||
longSide = Math.round((shortSide * longRatio) / IMAGE_SIZE_STEP) * IMAGE_SIZE_STEP;
|
||||
}
|
||||
|
||||
const width = isLandscape ? longSide : shortSide;
|
||||
const height = isLandscape ? shortSide : longSide;
|
||||
validateImageSize(width, height);
|
||||
return `${width}x${height}`;
|
||||
}
|
||||
|
||||
export function parseImageRatio(value: string) {
|
||||
const parts = value.split(":");
|
||||
if (parts.length !== 2) throw new Error("图像尺寸格式不支持,请使用 auto、9:16 或 1024x1024");
|
||||
const w = Number(parts[0]);
|
||||
const h = Number(parts[1]);
|
||||
if (!Number.isFinite(w) || !Number.isFinite(h) || w <= 0 || h <= 0) throw new Error("图像比例必须是正数,例如 9:16");
|
||||
if (Math.max(w, h) / Math.min(w, h) > IMAGE_MAX_RATIO) throw new Error("图像宽高比不能超过 3:1,请调整尺寸");
|
||||
return { width: w, height: h };
|
||||
}
|
||||
|
||||
export function parseImageDimensions(value: string) {
|
||||
const match = value.match(/^(\d+)x(\d+)$/i);
|
||||
if (!match) return null;
|
||||
return { width: Number(match[1]), height: Number(match[2]) };
|
||||
}
|
||||
|
||||
export function validateImageSize(width: number, height: number) {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) throw new Error("图像尺寸必须是正整数,例如 1024x1024");
|
||||
if (width % IMAGE_SIZE_STEP !== 0 || height % IMAGE_SIZE_STEP !== 0) throw new Error("图像尺寸的宽高必须是 16 的倍数,请调整尺寸");
|
||||
if (Math.max(width, height) > IMAGE_MAX_EDGE) throw new Error("图像尺寸最长边不能超过 3840px,请调整尺寸");
|
||||
if (Math.max(width, height) / Math.min(width, height) > IMAGE_MAX_RATIO) throw new Error("图像宽高比不能超过 3:1,请调整尺寸");
|
||||
const pixels = width * height;
|
||||
if (pixels < IMAGE_MIN_PIXELS || pixels > IMAGE_MAX_PIXELS) throw new Error("图像总像素需在 655360 到 8294400 之间,请调整尺寸");
|
||||
}
|
||||
|
||||
export function resolveRequestSize(quality: string | undefined, size: string) {
|
||||
const value = size.trim();
|
||||
if (!value || value.toLowerCase() === "auto") return undefined;
|
||||
const dimensions = parseImageDimensions(value);
|
||||
if (dimensions) {
|
||||
validateImageSize(dimensions.width, dimensions.height);
|
||||
return `${dimensions.width}x${dimensions.height}`;
|
||||
}
|
||||
if (value.includes(":")) return resolveSize(quality, value);
|
||||
throw new Error("图像尺寸格式不支持,请使用 auto、9:16 或 1024x1024");
|
||||
}
|
||||
|
||||
export function resolveAspectRatio(value: string) {
|
||||
const normalized = value.trim().toLowerCase().replace("×", "x");
|
||||
if (normalized.includes(":")) return normalized;
|
||||
const dimensions = parseImageDimensions(normalized);
|
||||
if (!dimensions) throw new Error("图像比例格式不支持,请使用 3:4 或 1024x1360");
|
||||
const divisor = dimensionGCD(dimensions.width, dimensions.height);
|
||||
return `${dimensions.width / divisor}:${dimensions.height / divisor}`;
|
||||
}
|
||||
|
||||
function dimensionGCD(left: number, right: number) {
|
||||
while (right) [left, right] = [right, left % right];
|
||||
return Math.max(1, left);
|
||||
}
|
||||
|
||||
export function resolveImageRequestSize(profile: ImageCapabilityConfig, quality: string | undefined, size: string) {
|
||||
const request = imageSizeRequest(profile, size);
|
||||
if (!request) return undefined;
|
||||
const value = request.parameter === "size" ? resolveRequestSize(quality, request.value) : resolveAspectRatio(request.value);
|
||||
return value ? { parameter: request.parameter, value } : undefined;
|
||||
}
|
||||
|
||||
export function validateImageCapability(profile: ImageCapabilityConfig, references: ReferenceImage[], mask?: ReferenceImage) {
|
||||
if (references.length > profile.references.maxImages) throw new Error(`当前图片模型最多支持 ${profile.references.maxImages} 张参考图`);
|
||||
if (mask && !profile.references.maskSupported) throw new Error("当前图片模型不支持蒙版编辑");
|
||||
if (profile.references.maxImageBytes > 0 && references.some((image) => (image.bytes || 0) > profile.references.maxImageBytes)) throw new Error("参考图片文件超过当前模型大小限制");
|
||||
}
|
||||
|
||||
export function normalizeVolcengineArkImageSize(size: string | undefined) {
|
||||
if (!size) return undefined;
|
||||
const dimensions = parseImageDimensions(size);
|
||||
if (!dimensions || dimensions.width * dimensions.height <= VOLCENGINE_ARK_IMAGE_MAX_PIXELS) return size;
|
||||
const scale = Math.sqrt(VOLCENGINE_ARK_IMAGE_MAX_PIXELS / (dimensions.width * dimensions.height));
|
||||
let width = Math.floor((dimensions.width * scale) / 2) * 2;
|
||||
let height = Math.floor((dimensions.height * scale) / 2) * 2;
|
||||
while (width > 2 && height > 2 && width * height > VOLCENGINE_ARK_IMAGE_MAX_PIXELS) {
|
||||
if (width >= height) width -= 2;
|
||||
else height -= 2;
|
||||
}
|
||||
return `${width}x${height}`;
|
||||
}
|
||||
@@ -1,853 +1,35 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { buildApiUrl, isSystemProxyBaseUrl, resolveBackendApiUrl, resolveModelRequestConfig, type AiConfig, type ModelChannel } from "@/stores/use-config-store";
|
||||
import { sanitizeChannelModelCatalogItem, type ChannelModelCatalogItem } from "@/lib/channel-model-catalog";
|
||||
import { nanoid } from "nanoid";
|
||||
import { resolveModelRequestConfig, type AiConfig } from "@/stores/use-config-store";
|
||||
import { dataUrlToFile } from "@/lib/image-utils";
|
||||
import { buildImageReferencePromptText } from "@/lib/image-reference-prompt";
|
||||
import { createClientId } from "@/lib/client-id";
|
||||
import { projectDesktopLocalChannelRuntime } from "@/lib/desktop-local-channel";
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import { withOpenAIPromptCacheKey } from "@/lib/openai-prompt-cache";
|
||||
import { imageSizeRequest, modelCapabilityConfigFor, normalizeImageValue, type ImageCapabilityConfig } from "@/lib/model-capabilities";
|
||||
import { modelCapabilityConfigFor, normalizeImageValue } from "@/lib/model-capabilities";
|
||||
import { buildGeminiImageGenerationConfig, parseGeminiImageDataUrl, type GeminiImageGenerationConfig } from "@/lib/gemini-image";
|
||||
import { aiApiUrl, aiHeaders, geminiApiUrl, geminiHeaders, postChannelJSON, postGeminiJSON, postVolcengineArkImage } from "@/services/api/image-transport";
|
||||
|
||||
export type AiTextMessage = {
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string | AiTextContentPart[];
|
||||
};
|
||||
|
||||
export type AiTextContentPart = { type: "text"; text: string } | { type: "image_url"; image_url: { url: string } } | { type: "file_url"; file_url: { url: string; name: string; mimeType: string } };
|
||||
|
||||
export type ResponseToolCall = {
|
||||
id: string;
|
||||
type: "function";
|
||||
function: { name: string; arguments: string };
|
||||
thoughtSignature?: string;
|
||||
};
|
||||
|
||||
export type ResponseInputMessage = AiTextMessage | { type: "function_call"; call_id: string; name: string; arguments: string; thoughtSignature?: string } | { role: "tool"; tool_call_id: string; content: string };
|
||||
|
||||
export type ResponseFunctionTool = {
|
||||
type: "function";
|
||||
function: {
|
||||
name: string;
|
||||
description?: string;
|
||||
parameters: Record<string, unknown>;
|
||||
strict?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
export type ToolResponseResult = {
|
||||
content: string;
|
||||
toolCalls: ResponseToolCall[];
|
||||
reasoning?: string;
|
||||
};
|
||||
|
||||
export type ToolChoice = "auto" | "required" | { type: "function"; name: string };
|
||||
export type BackendToolRequests = {
|
||||
responses: Record<string, unknown>;
|
||||
chatCompletion: Record<string, unknown>;
|
||||
};
|
||||
type ResponseMessageContent = AiTextMessage["content"] | string;
|
||||
type ResponseInputContent = { type: "input_text"; text: string } | { type: "input_image"; image_url: string } | { type: "input_file"; filename: string; file_data?: string; file_url?: string };
|
||||
type ResponseInputItem = { role: "system" | "user" | "assistant"; content: string | ResponseInputContent[] } | { type: "function_call"; call_id: string; name: string; arguments: string } | { type: "function_call_output"; call_id: string; output: string };
|
||||
type ResponseApiToolDefinition = {
|
||||
type: "function";
|
||||
name: string;
|
||||
description?: string;
|
||||
parameters: Record<string, unknown>;
|
||||
strict?: boolean;
|
||||
};
|
||||
type ResponseApiOutputItem = { type?: "message"; content?: Array<{ type?: string; text?: string }> } | { type?: "function_call"; id?: string; call_id?: string; name?: string; arguments?: string };
|
||||
type ResponseApiPayload = {
|
||||
id?: string;
|
||||
output?: ResponseApiOutputItem[];
|
||||
output_text?: string;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
type ResponseStreamState = { buffer: string; text: string; reasoning: string; payload?: ResponseApiPayload; error?: string };
|
||||
type ChatCompletionToolCall = { id?: string; type?: "function"; function?: { name?: string; arguments?: string } };
|
||||
type ChatCompletionPayload = {
|
||||
choices?: Array<{ message?: { content?: string | null; reasoning_content?: string | null; tool_calls?: ChatCompletionToolCall[] } }>;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
type ChatCompletionStreamToolCall = { id: string; name: string; arguments: string };
|
||||
type ChatCompletionStreamState = { buffer: string; text: string; reasoning: string; toolCalls: Map<number, ChatCompletionStreamToolCall>; error?: string };
|
||||
|
||||
type ImageApiResponse = {
|
||||
data?: Array<Record<string, unknown>>;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
type GeminiPart = {
|
||||
text?: string;
|
||||
thought?: boolean;
|
||||
inlineData?: { mimeType?: string; data?: string };
|
||||
inline_data?: { mime_type?: string; mimeType?: string; data?: string };
|
||||
fileData?: { mimeType?: string; fileUri?: string };
|
||||
functionCall?: { id?: string; name?: string; args?: Record<string, unknown> };
|
||||
functionResponse?: { id?: string; name?: string; response?: Record<string, unknown> };
|
||||
thoughtSignature?: string;
|
||||
thought_signature?: string;
|
||||
};
|
||||
type GeminiContent = { role?: "user" | "model"; parts: GeminiPart[] };
|
||||
type GeminiPayload = {
|
||||
candidates?: Array<{ content?: { parts?: GeminiPart[] }; finishReason?: string }>;
|
||||
models?: Array<{ name?: string }>;
|
||||
error?: { message?: string };
|
||||
promptFeedback?: { blockReason?: string };
|
||||
};
|
||||
type GeminiStreamState = { buffer: string; text: string; reasoning: string; toolCalls: ResponseToolCall[]; error?: string };
|
||||
type RequestOptions = { signal?: AbortSignal; promptCacheKey?: string; onReasoning?: (text: string) => void };
|
||||
|
||||
const QUALITY_BASE: Record<string, number> = {
|
||||
low: 1024,
|
||||
medium: 2048,
|
||||
high: 2880,
|
||||
standard: 1024,
|
||||
hd: 2048,
|
||||
};
|
||||
const QUALITY_ALIASES: Record<string, string> = {
|
||||
"1k": "low",
|
||||
"2k": "medium",
|
||||
"4k": "high",
|
||||
};
|
||||
const DEFAULT_IMAGE_SHORT_SIDE = 1024;
|
||||
const IMAGE_SIZE_STEP = 16;
|
||||
const IMAGE_MIN_PIXELS = 655360;
|
||||
const IMAGE_MAX_PIXELS = 8294400;
|
||||
const IMAGE_MAX_EDGE = 3840;
|
||||
const IMAGE_MAX_RATIO = 3;
|
||||
const IMAGE_OUTPUT_FORMAT = "png";
|
||||
const VOLCENGINE_ARK_IMAGE_MAX_PIXELS = 4624220;
|
||||
|
||||
function normalizeQuality(quality: string) {
|
||||
const value = quality.trim().toLowerCase();
|
||||
const normalized = QUALITY_ALIASES[value] || value;
|
||||
return QUALITY_BASE[normalized] ? normalized : undefined;
|
||||
}
|
||||
|
||||
/** grok2api / xAI Imagine:画布 quality 映射为 resolution(1k/2k)。 */
|
||||
function normalizeGrokImageResolution(quality: string | undefined) {
|
||||
const value = (quality || "").trim().toLowerCase();
|
||||
if (!value || value === "auto") return undefined;
|
||||
if (value === "1k" || value === "low" || value === "standard") return "1k";
|
||||
if (value === "2k" || value === "medium" || value === "hd" || value === "high" || value === "4k") return "2k";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Map "quality + ratio" to an explicit pixel dimension like "3840x2160". */
|
||||
function resolveSize(quality: string | undefined, ratio: string): string {
|
||||
const parsedRatio = parseImageRatio(ratio);
|
||||
const basePixels = quality ? QUALITY_BASE[quality] : undefined;
|
||||
const isLandscape = parsedRatio.width >= parsedRatio.height;
|
||||
const longRatio = isLandscape ? parsedRatio.width / parsedRatio.height : parsedRatio.height / parsedRatio.width;
|
||||
let longSide: number;
|
||||
let shortSide: number;
|
||||
|
||||
if (basePixels) {
|
||||
const targetPixels = basePixels * basePixels;
|
||||
const longSideRaw = Math.sqrt(targetPixels * longRatio);
|
||||
longSide = Math.floor(longSideRaw / IMAGE_SIZE_STEP) * IMAGE_SIZE_STEP;
|
||||
shortSide = Math.round(longSide / longRatio / IMAGE_SIZE_STEP) * IMAGE_SIZE_STEP;
|
||||
} else {
|
||||
shortSide = DEFAULT_IMAGE_SHORT_SIDE;
|
||||
longSide = Math.round((shortSide * longRatio) / IMAGE_SIZE_STEP) * IMAGE_SIZE_STEP;
|
||||
}
|
||||
|
||||
const width = isLandscape ? longSide : shortSide;
|
||||
const height = isLandscape ? shortSide : longSide;
|
||||
validateImageSize(width, height);
|
||||
return `${width}x${height}`;
|
||||
}
|
||||
|
||||
function parseImageRatio(value: string) {
|
||||
const parts = value.split(":");
|
||||
if (parts.length !== 2) throw new Error("图像尺寸格式不支持,请使用 auto、9:16 或 1024x1024");
|
||||
const w = Number(parts[0]);
|
||||
const h = Number(parts[1]);
|
||||
if (!Number.isFinite(w) || !Number.isFinite(h) || w <= 0 || h <= 0) throw new Error("图像比例必须是正数,例如 9:16");
|
||||
if (Math.max(w, h) / Math.min(w, h) > IMAGE_MAX_RATIO) throw new Error("图像宽高比不能超过 3:1,请调整尺寸");
|
||||
return { width: w, height: h };
|
||||
}
|
||||
|
||||
function parseImageDimensions(value: string) {
|
||||
const match = value.match(/^(\d+)x(\d+)$/i);
|
||||
if (!match) return null;
|
||||
return { width: Number(match[1]), height: Number(match[2]) };
|
||||
}
|
||||
|
||||
function validateImageSize(width: number, height: number) {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) throw new Error("图像尺寸必须是正整数,例如 1024x1024");
|
||||
if (width % IMAGE_SIZE_STEP !== 0 || height % IMAGE_SIZE_STEP !== 0) throw new Error("图像尺寸的宽高必须是 16 的倍数,请调整尺寸");
|
||||
if (Math.max(width, height) > IMAGE_MAX_EDGE) throw new Error("图像尺寸最长边不能超过 3840px,请调整尺寸");
|
||||
if (Math.max(width, height) / Math.min(width, height) > IMAGE_MAX_RATIO) throw new Error("图像宽高比不能超过 3:1,请调整尺寸");
|
||||
const pixels = width * height;
|
||||
if (pixels < IMAGE_MIN_PIXELS || pixels > IMAGE_MAX_PIXELS) throw new Error("图像总像素需在 655360 到 8294400 之间,请调整尺寸");
|
||||
}
|
||||
|
||||
function resolveRequestSize(quality: string | undefined, size: string) {
|
||||
const value = size.trim();
|
||||
if (!value || value.toLowerCase() === "auto") return undefined;
|
||||
const dimensions = parseImageDimensions(value);
|
||||
if (dimensions) {
|
||||
validateImageSize(dimensions.width, dimensions.height);
|
||||
return `${dimensions.width}x${dimensions.height}`;
|
||||
}
|
||||
if (value.includes(":")) return resolveSize(quality, value);
|
||||
throw new Error("图像尺寸格式不支持,请使用 auto、9:16 或 1024x1024");
|
||||
}
|
||||
|
||||
function resolveAspectRatio(value: string) {
|
||||
const normalized = value.trim().toLowerCase().replace("×", "x");
|
||||
if (normalized.includes(":")) return normalized;
|
||||
const dimensions = parseImageDimensions(normalized);
|
||||
if (!dimensions) throw new Error("图像比例格式不支持,请使用 3:4 或 1024x1360");
|
||||
const divisor = dimensionGCD(dimensions.width, dimensions.height);
|
||||
return `${dimensions.width / divisor}:${dimensions.height / divisor}`;
|
||||
}
|
||||
|
||||
function dimensionGCD(left: number, right: number) {
|
||||
while (right) [left, right] = [right, left % right];
|
||||
return Math.max(1, left);
|
||||
}
|
||||
|
||||
function resolveImageRequestSize(profile: ImageCapabilityConfig, quality: string | undefined, size: string) {
|
||||
const request = imageSizeRequest(profile, size);
|
||||
if (!request) return undefined;
|
||||
const value = request.parameter === "size" ? resolveRequestSize(quality, request.value) : resolveAspectRatio(request.value);
|
||||
return value ? { parameter: request.parameter, value } : undefined;
|
||||
}
|
||||
|
||||
function validateImageCapability(profile: ImageCapabilityConfig, references: ReferenceImage[], mask?: ReferenceImage) {
|
||||
if (references.length > profile.references.maxImages) throw new Error(`当前图片模型最多支持 ${profile.references.maxImages} 张参考图`);
|
||||
if (mask && !profile.references.maskSupported) throw new Error("当前图片模型不支持蒙版编辑");
|
||||
if (profile.references.maxImageBytes > 0 && references.some((image) => (image.bytes || 0) > profile.references.maxImageBytes)) throw new Error("参考图片文件超过当前模型大小限制");
|
||||
}
|
||||
|
||||
function normalizeVolcengineArkImageSize(size: string | undefined) {
|
||||
if (!size) return undefined;
|
||||
const dimensions = parseImageDimensions(size);
|
||||
if (!dimensions || dimensions.width * dimensions.height <= VOLCENGINE_ARK_IMAGE_MAX_PIXELS) return size;
|
||||
const scale = Math.sqrt(VOLCENGINE_ARK_IMAGE_MAX_PIXELS / (dimensions.width * dimensions.height));
|
||||
let width = Math.floor((dimensions.width * scale) / 2) * 2;
|
||||
let height = Math.floor((dimensions.height * scale) / 2) * 2;
|
||||
while (width > 2 && height > 2 && width * height > VOLCENGINE_ARK_IMAGE_MAX_PIXELS) {
|
||||
if (width >= height) width -= 2;
|
||||
else height -= 2;
|
||||
}
|
||||
return `${width}x${height}`;
|
||||
}
|
||||
|
||||
function resolveImageDataUrl(item: Record<string, unknown>) {
|
||||
if (typeof item.b64_json === "string" && item.b64_json) {
|
||||
return `data:image/png;base64,${item.b64_json}`;
|
||||
}
|
||||
if (typeof item.url === "string" && item.url) {
|
||||
return item.url;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseImagePayload(payload: ImageApiResponse) {
|
||||
if (typeof payload.code === "number" && payload.code !== 0) {
|
||||
throw new Error(payload.msg || "请求失败");
|
||||
}
|
||||
const images =
|
||||
payload.data
|
||||
?.map(resolveImageDataUrl)
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.map((dataUrl) => ({ id: nanoid(), dataUrl })) || [];
|
||||
|
||||
if (images.length === 0) {
|
||||
throw new Error("接口没有返回图片");
|
||||
}
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
function readAxiosError(error: unknown, fallback: string) {
|
||||
if (axios.isCancel(error)) return "请求已取消";
|
||||
if (axios.isAxiosError<{ error?: { message?: string }; msg?: string; code?: number }>(error)) {
|
||||
const responseData = error.response?.data;
|
||||
return responseData?.msg || responseData?.error?.message || readStatusError(error.response?.status, fallback);
|
||||
}
|
||||
if (error instanceof DOMException && error.name === "AbortError") return "请求已取消";
|
||||
return error instanceof Error ? error.message : fallback;
|
||||
}
|
||||
|
||||
function readStatusError(status: number | undefined, fallback: string) {
|
||||
if (status === 401 || status === 403) return "鉴权失败,请检查 API Key、套餐权限或模型权限";
|
||||
if (status === 429) return "请求被限流或额度不足,请稍后重试";
|
||||
return status ? `${fallback}:${status}` : fallback;
|
||||
}
|
||||
import type { AiTextMessage, GeminiPart, ImageApiResponse, RequestOptions, ResponseApiPayload, ResponseFunctionTool, ResponseInputMessage, ToolChoice, ToolResponseResult } from "@/services/api/image-contracts";
|
||||
import { normalizeGrokImageResolution, normalizeQuality, normalizeVolcengineArkImageSize, resolveImageRequestSize, validateImageCapability } from "@/services/api/image-validation";
|
||||
import { parseGeminiImagePayload, parseImagePayload, readAxiosError } from "@/services/api/image-response";
|
||||
import { toChatCompletionMessages, toChatCompletionToolChoice, toGeminiBody, toGeminiToolOptions, toResponseInput, toResponseTool, withSystemMessage } from "@/services/api/image-protocols";
|
||||
import { requestGeminiStreamingResponse, requestStreamingChatCompletion, requestStreamingResponse } from "@/services/api/image-streaming";
|
||||
export { buildBackendToolRequests } from "@/services/api/image-protocols";
|
||||
export type { AiTextContentPart, AiTextMessage, ResponseFunctionTool, ResponseInputMessage, ResponseToolCall, ToolChoice, ToolResponseResult } from "@/services/api/image-contracts";
|
||||
|
||||
function withSystemPrompt(config: AiConfig, prompt: string) {
|
||||
const systemPrompt = config.systemPrompt.trim();
|
||||
return systemPrompt ? `${systemPrompt}\n\n${prompt}` : prompt;
|
||||
}
|
||||
|
||||
function aiApiUrl(config: AiConfig, path: string) {
|
||||
return buildApiUrl(config.baseUrl, path);
|
||||
}
|
||||
|
||||
function aiHeaders(config: AiConfig, contentType?: string) {
|
||||
return {
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
...(contentType ? { "Content-Type": contentType } : {}),
|
||||
...(isSystemProxyBaseUrl(config.baseUrl) ? { "X-Canvas-Scene": "image", "X-Idempotency-Key": createClientId() } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function postVolcengineArkImage(config: ReturnType<typeof resolveModelRequestConfig>, payload: Record<string, unknown>, options?: RequestOptions) {
|
||||
const upstreamUrl = aiApiUrl(config, "/images/generations");
|
||||
const request = channelRequest(config, upstreamUrl, aiHeaders(config, "application/json"));
|
||||
return (
|
||||
await axios.post<ImageApiResponse>(request.url, payload, {
|
||||
headers: request.headers,
|
||||
withCredentials: request.credentials === "include",
|
||||
signal: options?.signal,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
function geminiBaseUrl(config: Pick<AiConfig, "baseUrl">) {
|
||||
const normalizedBaseUrl = resolveBackendApiUrl(config.baseUrl).replace(/\/+$/, "");
|
||||
const lowerBaseUrl = normalizedBaseUrl.toLowerCase();
|
||||
return isSystemProxyBaseUrl(normalizedBaseUrl) || lowerBaseUrl.endsWith("/v1") || lowerBaseUrl.endsWith("/v1beta") ? normalizedBaseUrl : `${normalizedBaseUrl}/v1beta`;
|
||||
}
|
||||
|
||||
function geminiModelName(model: string) {
|
||||
return model.trim().replace(/^models\//, "");
|
||||
}
|
||||
|
||||
function geminiApiUrl(config: Pick<AiConfig, "baseUrl" | "model">, action?: "generateContent" | "streamGenerateContent") {
|
||||
const baseUrl = geminiBaseUrl(config);
|
||||
if (!action) return `${baseUrl}/models`;
|
||||
return `${baseUrl}/models/${encodeURIComponent(geminiModelName(config.model))}:${action}`;
|
||||
}
|
||||
|
||||
function geminiHeaders(config: Pick<AiConfig, "apiKey">) {
|
||||
return {
|
||||
"x-goog-api-key": config.apiKey,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
}
|
||||
|
||||
function withSystemMessage<T extends ResponseInputMessage>(config: AiConfig, messages: T[]): ResponseInputMessage[] {
|
||||
const systemPrompt = config.systemPrompt.trim();
|
||||
return systemPrompt ? [{ role: "system" as const, content: systemPrompt }, ...messages] : messages;
|
||||
}
|
||||
|
||||
function toResponseInput(messages: ResponseInputMessage[]): ResponseInputItem[] {
|
||||
return messages.flatMap((message): ResponseInputItem[] => {
|
||||
if ("type" in message) return [message];
|
||||
if (message.role === "tool") return [{ type: "function_call_output", call_id: message.tool_call_id, output: message.content }];
|
||||
return [{ role: message.role, content: toResponseContent(message.content || "") }];
|
||||
});
|
||||
}
|
||||
|
||||
function toResponseContent(content: ResponseMessageContent): string | ResponseInputContent[] {
|
||||
if (!Array.isArray(content)) return String(content || "");
|
||||
return content.map((item) => {
|
||||
if (item.type === "text") return { type: "input_text" as const, text: item.text };
|
||||
if (item.type === "image_url") return { type: "input_image" as const, image_url: item.image_url.url };
|
||||
return item.file_url.url.startsWith("data:") ? { type: "input_file" as const, filename: item.file_url.name, file_data: item.file_url.url } : { type: "input_file" as const, filename: item.file_url.name, file_url: item.file_url.url };
|
||||
});
|
||||
}
|
||||
|
||||
function toResponseTool(tool: ResponseFunctionTool): ResponseApiToolDefinition {
|
||||
return {
|
||||
type: "function",
|
||||
name: tool.function.name,
|
||||
description: tool.function.description,
|
||||
parameters: tool.function.parameters,
|
||||
strict: tool.function.strict,
|
||||
};
|
||||
}
|
||||
|
||||
function toChatCompletionMessages(messages: ResponseInputMessage[]) {
|
||||
const result: Array<Record<string, unknown>> = [];
|
||||
for (let index = 0; index < messages.length;) {
|
||||
const message = messages[index];
|
||||
if ("type" in message) {
|
||||
const toolCalls: Array<Record<string, unknown>> = [];
|
||||
while (index < messages.length && "type" in messages[index]) {
|
||||
const call = messages[index] as Extract<ResponseInputMessage, { type: "function_call" }>;
|
||||
toolCalls.push({ id: call.call_id, type: "function", function: { name: call.name, arguments: call.arguments } });
|
||||
index += 1;
|
||||
}
|
||||
result.push({ role: "assistant", content: null, tool_calls: toolCalls });
|
||||
continue;
|
||||
}
|
||||
if (message.role === "tool") {
|
||||
result.push({ role: "tool", tool_call_id: message.tool_call_id, content: message.content });
|
||||
} else {
|
||||
result.push({ role: message.role, content: toChatCompletionContent(message.content) });
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function toChatCompletionContent(content: ResponseMessageContent) {
|
||||
if (!Array.isArray(content)) return content;
|
||||
return content.map((item) => {
|
||||
if (item.type !== "file_url") return item;
|
||||
const file = item.file_url.url.startsWith("data:") ? { filename: item.file_url.name, file_data: item.file_url.url } : { filename: item.file_url.name, file_url: item.file_url.url };
|
||||
return { type: "file", file };
|
||||
});
|
||||
}
|
||||
|
||||
function toChatCompletionToolChoice(toolChoice: ToolChoice) {
|
||||
return typeof toolChoice === "object" ? { type: "function", function: { name: toolChoice.name } } : toolChoice;
|
||||
}
|
||||
|
||||
export function buildBackendToolRequests(messages: ResponseInputMessage[], tools: ResponseFunctionTool[], toolChoice: ToolChoice): BackendToolRequests {
|
||||
return {
|
||||
responses: {
|
||||
input: toResponseInput(messages),
|
||||
tools: tools.map(toResponseTool),
|
||||
tool_choice: toolChoice,
|
||||
parallel_tool_calls: false,
|
||||
},
|
||||
chatCompletion: {
|
||||
messages: toChatCompletionMessages(messages),
|
||||
tools,
|
||||
tool_choice: toChatCompletionToolChoice(toolChoice),
|
||||
parallel_tool_calls: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function isToolChoiceCompatibilityError(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "";
|
||||
return /tool[_\s-]?choice|thinking\s+mode/i.test(message);
|
||||
}
|
||||
|
||||
function parseChatCompletionPayload(payload: ChatCompletionPayload): ToolResponseResult {
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
const message = payload.choices?.[0]?.message;
|
||||
const toolCalls = (message?.tool_calls || [])
|
||||
.map((call) => ({
|
||||
id: call.id || "",
|
||||
type: "function" as const,
|
||||
function: { name: call.function?.name || "", arguments: call.function?.arguments || "{}" },
|
||||
}))
|
||||
.filter((call) => call.id && call.function.name);
|
||||
return { content: message?.content || "", toolCalls, ...(message?.reasoning_content ? { reasoning: message.reasoning_content } : {}) };
|
||||
}
|
||||
|
||||
function parseToolResponse(payload: ResponseApiPayload): ToolResponseResult {
|
||||
const output = payload.output || [];
|
||||
const content =
|
||||
payload.output_text ||
|
||||
output
|
||||
.flatMap((item) => (item.type === "message" ? item.content || [] : []))
|
||||
.map((item) => item.text || "")
|
||||
.join("");
|
||||
const toolCalls = output
|
||||
.filter((item): item is Extract<ResponseApiOutputItem, { type?: "function_call" }> => item.type === "function_call")
|
||||
.map((item) => ({
|
||||
id: item.call_id || item.id || "",
|
||||
type: "function" as const,
|
||||
function: { name: item.name || "", arguments: item.arguments || "{}" },
|
||||
}))
|
||||
.filter((item) => item.id && item.function.name);
|
||||
return { content, toolCalls };
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
||||
}
|
||||
|
||||
function responseErrorMessage(value: unknown) {
|
||||
if (!isRecord(value)) return "";
|
||||
const error = isRecord(value.error) ? value.error : undefined;
|
||||
const response = isRecord(value.response) ? value.response : undefined;
|
||||
const responseError = response && isRecord(response.error) ? response.error : undefined;
|
||||
return stringValue(value.msg) || stringValue(error?.message) || stringValue(responseError?.message);
|
||||
}
|
||||
|
||||
function stringValue(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
function validateResponsePayload(payload: ResponseApiPayload) {
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
}
|
||||
|
||||
function validateGeminiPayload(payload: GeminiPayload) {
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
if (payload.promptFeedback?.blockReason) throw new Error(`Gemini 拒绝了本次请求:${payload.promptFeedback.blockReason}`);
|
||||
}
|
||||
|
||||
async function readFetchError(response: Response, fallback: string) {
|
||||
const text = await response.text();
|
||||
if (!text) return readStatusError(response.status, fallback);
|
||||
try {
|
||||
return responseErrorMessage(JSON.parse(text)) || readStatusError(response.status, fallback);
|
||||
} catch {
|
||||
return text.slice(0, 300) || readStatusError(response.status, fallback);
|
||||
}
|
||||
}
|
||||
|
||||
async function readJsonPayload<T>(response: Response, fallback: string): Promise<T> {
|
||||
const text = await response.text();
|
||||
try {
|
||||
return JSON.parse(text) as T;
|
||||
} catch {
|
||||
if (/^\s*(?:<!doctype|<html)/i.test(text)) throw new Error("后端代理返回了前端网页,请检查 VITE_CANVAS_BACKEND_URL 和反向代理配置");
|
||||
throw new Error(`${fallback}:接口没有返回有效 JSON`);
|
||||
}
|
||||
}
|
||||
|
||||
function consumeResponseStreamBlock(block: string, state: ResponseStreamState, onDelta?: (text: string) => void, onReasoning?: (text: string) => void) {
|
||||
const data = block
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).replace(/^ /, ""))
|
||||
.join("\n")
|
||||
.trim();
|
||||
if (!data || data === "[DONE]") return;
|
||||
const event = JSON.parse(data) as Record<string, unknown>;
|
||||
const type = stringValue(event.type);
|
||||
const errorMessage = responseErrorMessage(event);
|
||||
if (errorMessage) state.error = errorMessage;
|
||||
if (type === "response.output_text.delta" && typeof event.delta === "string") {
|
||||
state.text += event.delta;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
if (type === "response.output_text.done" && !state.text && typeof event.text === "string") {
|
||||
state.text = event.text;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
if (["response.reasoning.delta", "response.reasoning_text.delta", "response.reasoning_summary_text.delta"].includes(type) && typeof event.delta === "string") {
|
||||
state.reasoning += event.delta;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
if (["response.reasoning.done", "response.reasoning_text.done", "response.reasoning_summary_text.done"].includes(type) && !state.reasoning && typeof event.text === "string") {
|
||||
state.reasoning = event.text;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
if (type === "response.completed" && isRecord(event.response)) {
|
||||
state.payload = event.response as ResponseApiPayload;
|
||||
} else if (Array.isArray(event.output)) {
|
||||
state.payload = event as ResponseApiPayload;
|
||||
}
|
||||
}
|
||||
|
||||
function consumeResponseStreamText(state: ResponseStreamState, text: string, onDelta?: (text: string) => void, onReasoning?: (text: string) => void, flush = false) {
|
||||
state.buffer += text;
|
||||
for (;;) {
|
||||
const match = state.buffer.match(/\r?\n\r?\n/);
|
||||
if (!match) break;
|
||||
const index = match.index ?? 0;
|
||||
consumeResponseStreamBlock(state.buffer.slice(0, index), state, onDelta, onReasoning);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeResponseStreamBlock(state.buffer, state, onDelta, onReasoning);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function requestStreamingResponse(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const request = channelRequest(config, aiApiUrl(config, "/responses"), { ...aiHeaders(config, "application/json"), Accept: "text/event-stream" });
|
||||
const response = await fetch(request.url, {
|
||||
method: "POST",
|
||||
headers: request.headers,
|
||||
body: JSON.stringify({ ...body, stream: true }),
|
||||
signal: options?.signal,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
if (!response.body) {
|
||||
const payload = (await response.json()) as ResponseApiPayload;
|
||||
validateResponsePayload(payload);
|
||||
return parseToolResponse(payload);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: ResponseStreamState = { buffer: "", text: "", reasoning: "" };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeResponseStreamText(state, decoder.decode(value, { stream: true }), onDelta, options?.onReasoning);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeResponseStreamText(state, decoder.decode(), onDelta, options?.onReasoning, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
if (!state.payload) return { content: state.text, toolCalls: [], ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
validateResponsePayload(state.payload);
|
||||
const result = parseToolResponse(state.payload);
|
||||
return { ...result, content: state.text || result.content, ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
}
|
||||
|
||||
function consumeChatCompletionStreamBlock(block: string, state: ChatCompletionStreamState, onDelta?: (text: string) => void, onReasoning?: (text: string) => void) {
|
||||
const data = block
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).replace(/^ /, ""))
|
||||
.join("\n")
|
||||
.trim();
|
||||
if (!data || data === "[DONE]") return;
|
||||
const event = JSON.parse(data) as Record<string, unknown>;
|
||||
const errorMessage = responseErrorMessage(event);
|
||||
if (errorMessage) state.error = errorMessage;
|
||||
const choices = Array.isArray(event.choices) ? event.choices : [];
|
||||
const choice = isRecord(choices[0]) ? choices[0] : undefined;
|
||||
const delta = choice && isRecord(choice.delta) ? choice.delta : undefined;
|
||||
if (!delta) return;
|
||||
if (typeof delta.content === "string") {
|
||||
state.text += delta.content;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
const reasoningDelta = stringValue(delta.reasoning_content) || stringValue(delta.reasoning) || stringValue(delta.reasoning_text);
|
||||
if (reasoningDelta) {
|
||||
state.reasoning += reasoningDelta;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
const chunks = Array.isArray(delta.tool_calls) ? delta.tool_calls : [];
|
||||
chunks.forEach((value, fallbackIndex) => {
|
||||
if (!isRecord(value)) return;
|
||||
const callIndex = typeof value.index === "number" ? value.index : fallbackIndex;
|
||||
const current = state.toolCalls.get(callIndex) || { id: "", name: "", arguments: "" };
|
||||
const fn = isRecord(value.function) ? value.function : undefined;
|
||||
state.toolCalls.set(callIndex, {
|
||||
id: stringValue(value.id) || current.id,
|
||||
name: stringValue(fn?.name) || current.name,
|
||||
arguments: current.arguments + stringValue(fn?.arguments),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function consumeChatCompletionStreamText(state: ChatCompletionStreamState, text: string, onDelta?: (text: string) => void, onReasoning?: (text: string) => void, flush = false) {
|
||||
state.buffer += text;
|
||||
for (;;) {
|
||||
const match = state.buffer.match(/\r?\n\r?\n/);
|
||||
if (!match) break;
|
||||
const index = match.index ?? 0;
|
||||
consumeChatCompletionStreamBlock(state.buffer.slice(0, index), state, onDelta, onReasoning);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeChatCompletionStreamBlock(state.buffer, state, onDelta, onReasoning);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function requestStreamingChatCompletion(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const request = channelRequest(config, aiApiUrl(config, "/chat/completions"), { ...aiHeaders(config, "application/json"), Accept: "text/event-stream" });
|
||||
const response = await fetch(request.url, {
|
||||
method: "POST",
|
||||
headers: request.headers,
|
||||
body: JSON.stringify({ ...body, stream: true }),
|
||||
signal: options?.signal,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
if (!response.body || !contentType.includes("text/event-stream")) {
|
||||
const result = parseChatCompletionPayload(await readJsonPayload<ChatCompletionPayload>(response, "请求失败"));
|
||||
if (result.reasoning) options?.onReasoning?.(result.reasoning);
|
||||
return result;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: ChatCompletionStreamState = { buffer: "", text: "", reasoning: "", toolCalls: new Map() };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeChatCompletionStreamText(state, decoder.decode(value, { stream: true }), onDelta, options?.onReasoning);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeChatCompletionStreamText(state, decoder.decode(), onDelta, options?.onReasoning, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
const toolCalls = Array.from(state.toolCalls.entries())
|
||||
.sort(([left], [right]) => left - right)
|
||||
.map(([, call]) => ({ id: call.id, type: "function" as const, function: { name: call.name, arguments: call.arguments || "{}" } }))
|
||||
.filter((call) => call.id && call.function.name);
|
||||
return { content: state.text, toolCalls, ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
}
|
||||
|
||||
function toGeminiBody(config: AiConfig, messages: ResponseInputMessage[], extra?: Record<string, unknown>) {
|
||||
const systemText = [config.systemPrompt.trim(), ...messages.flatMap((message) => (!("type" in message) && message.role === "system" ? [geminiTextContent(message.content)] : []))].filter(Boolean).join("\n\n");
|
||||
const contents = toGeminiContents(messages.filter((message) => ("type" in message ? true : message.role !== "system")));
|
||||
return {
|
||||
contents,
|
||||
...(systemText ? { systemInstruction: { parts: [{ text: systemText }] } } : {}),
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
function toGeminiContents(messages: ResponseInputMessage[]): GeminiContent[] {
|
||||
const callNameById = new Map<string, string>();
|
||||
return messages.flatMap((message): GeminiContent[] => {
|
||||
if ("type" in message) {
|
||||
callNameById.set(message.call_id, message.name);
|
||||
return [{ role: "model", parts: [{ functionCall: { id: message.call_id, name: message.name, args: jsonObject(message.arguments) }, ...(message.thoughtSignature ? { thoughtSignature: message.thoughtSignature } : {}) }] }];
|
||||
}
|
||||
if (message.role === "tool") {
|
||||
const name = callNameById.get(message.tool_call_id) || "tool_result";
|
||||
return [{ role: "user", parts: [{ functionResponse: { id: message.tool_call_id, name, response: { result: jsonValue(message.content) } } }] }];
|
||||
}
|
||||
return [{ role: message.role === "assistant" ? "model" : "user", parts: toGeminiParts(message.content) }];
|
||||
});
|
||||
}
|
||||
|
||||
function toGeminiParts(content: ResponseMessageContent): GeminiPart[] {
|
||||
if (!Array.isArray(content)) return [{ text: String(content || "") }];
|
||||
return content.map((item) => {
|
||||
if (item.type === "text") return { text: item.text };
|
||||
if (item.type === "image_url") return toGeminiFilePart(item.image_url.url, "image/png");
|
||||
return toGeminiFilePart(item.file_url.url, item.file_url.mimeType);
|
||||
});
|
||||
}
|
||||
|
||||
function toGeminiFilePart(url: string, mimeType: string): GeminiPart {
|
||||
const match = url.match(/^data:([^;,]+);base64,(.+)$/);
|
||||
if (match) return { inlineData: { mimeType: match[1], data: match[2] } };
|
||||
return { fileData: { fileUri: url, mimeType } };
|
||||
}
|
||||
|
||||
function geminiTextContent(content: ResponseMessageContent) {
|
||||
if (!Array.isArray(content)) return String(content || "");
|
||||
return content.map((item) => (item.type === "text" ? item.text : item.type === "image_url" ? item.image_url.url : `${item.file_url.name}: ${item.file_url.url}`)).join("\n");
|
||||
}
|
||||
|
||||
function jsonObject(value: string): Record<string, unknown> {
|
||||
const parsed = jsonValue(value);
|
||||
return isRecord(parsed) ? parsed : {};
|
||||
}
|
||||
|
||||
function jsonValue(value: string): unknown {
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
function toGeminiToolOptions(tools: ResponseFunctionTool[], toolChoice: ToolChoice) {
|
||||
if (!tools.length) return {};
|
||||
const functionDeclarations = tools.map((tool) => ({
|
||||
name: tool.function.name,
|
||||
description: tool.function.description,
|
||||
parameters: tool.function.parameters,
|
||||
}));
|
||||
const functionCallingConfig = typeof toolChoice === "object" ? { mode: "ANY", allowedFunctionNames: [toolChoice.name] } : { mode: toolChoice === "required" ? "ANY" : "AUTO" };
|
||||
return {
|
||||
tools: [{ functionDeclarations }],
|
||||
toolConfig: { functionCallingConfig },
|
||||
};
|
||||
}
|
||||
|
||||
async function requestGeminiStreamingResponse(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const request = channelRequest(config, `${geminiApiUrl(config, "streamGenerateContent")}?alt=sse`, geminiHeaders(config));
|
||||
const response = await fetch(request.url, {
|
||||
method: "POST",
|
||||
headers: request.headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: options?.signal,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
if (!response.body) {
|
||||
const payload = (await response.json()) as GeminiPayload;
|
||||
const result = parseGeminiToolResponse(payload);
|
||||
if (result.reasoning) options?.onReasoning?.(result.reasoning);
|
||||
return result;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: GeminiStreamState = { buffer: "", text: "", reasoning: "", toolCalls: [] };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeGeminiStreamText(state, decoder.decode(value, { stream: true }), onDelta, options?.onReasoning);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeGeminiStreamText(state, decoder.decode(), onDelta, options?.onReasoning, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
return { content: state.text, toolCalls: state.toolCalls, ...(state.reasoning ? { reasoning: state.reasoning } : {}) };
|
||||
}
|
||||
|
||||
function consumeGeminiStreamText(state: GeminiStreamState, text: string, onDelta?: (text: string) => void, onReasoning?: (text: string) => void, flush = false) {
|
||||
state.buffer += text;
|
||||
for (;;) {
|
||||
const match = state.buffer.match(/\r?\n\r?\n/);
|
||||
if (!match) break;
|
||||
const index = match.index ?? 0;
|
||||
consumeGeminiStreamBlock(state.buffer.slice(0, index), state, onDelta, onReasoning);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeGeminiStreamBlock(state.buffer, state, onDelta, onReasoning);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
function consumeGeminiStreamBlock(block: string, state: GeminiStreamState, onDelta?: (text: string) => void, onReasoning?: (text: string) => void) {
|
||||
const data = block
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).replace(/^ /, ""))
|
||||
.join("\n")
|
||||
.trim();
|
||||
if (!data || data === "[DONE]") return;
|
||||
const result = parseGeminiToolResponse(JSON.parse(data) as GeminiPayload);
|
||||
if (result.reasoning) {
|
||||
state.reasoning += result.reasoning;
|
||||
onReasoning?.(state.reasoning);
|
||||
}
|
||||
if (result.content) {
|
||||
state.text += result.content;
|
||||
onDelta?.(state.text);
|
||||
}
|
||||
state.toolCalls.push(...result.toolCalls);
|
||||
}
|
||||
|
||||
function parseGeminiToolResponse(payload: GeminiPayload): ToolResponseResult {
|
||||
validateGeminiPayload(payload);
|
||||
const parts = payload.candidates?.flatMap((candidate) => candidate.content?.parts || []) || [];
|
||||
const content = parts
|
||||
.filter((part) => !part.thought)
|
||||
.map((part) => part.text || "")
|
||||
.join("");
|
||||
const reasoning = parts
|
||||
.filter((part) => part.thought)
|
||||
.map((part) => part.text || "")
|
||||
.join("");
|
||||
const toolCalls = parts
|
||||
.map((part) => part.functionCall)
|
||||
.filter((call): call is NonNullable<GeminiPart["functionCall"]> => Boolean(call?.name))
|
||||
.map((call) => {
|
||||
const part = parts.find((item) => item.functionCall === call);
|
||||
const thoughtSignature = part?.thoughtSignature || part?.thought_signature;
|
||||
return {
|
||||
id: call.id || nanoid(),
|
||||
type: "function" as const,
|
||||
function: { name: call.name || "", arguments: JSON.stringify(call.args || {}) },
|
||||
...(thoughtSignature ? { thoughtSignature } : {}),
|
||||
};
|
||||
});
|
||||
return { content, toolCalls, ...(reasoning ? { reasoning } : {}) };
|
||||
}
|
||||
|
||||
async function requestGeminiImages(config: AiConfig, prompt: string, references: ReferenceImage[], count: number, generationConfig: GeminiImageGenerationConfig, options?: RequestOptions) {
|
||||
// 参考图先完整读取并校验一次,再复用已解析的 inlineData;不能让每个输出重新读取 storageKey,
|
||||
// 否则资源缓存瞬时未命中时会出现“偶发缺参考图”或把 storageKey 当 URL 请求的问题。
|
||||
@@ -864,32 +46,15 @@ async function requestGeminiImages(config: AiConfig, prompt: string, references:
|
||||
|
||||
async function requestGeminiImagesOnce(config: AiConfig, prompt: string, referenceParts: GeminiPart[], generationConfig: GeminiImageGenerationConfig, options?: RequestOptions) {
|
||||
const parts: GeminiPart[] = [{ text: prompt }, ...referenceParts];
|
||||
const request = channelRequest(config, geminiApiUrl(config, "generateContent"), geminiHeaders(config));
|
||||
const response = await axios.post<GeminiPayload>(
|
||||
request.url,
|
||||
const response = await postGeminiJSON(
|
||||
config,
|
||||
{
|
||||
...toGeminiBody(config, [{ role: "user", content: prompt }], { generationConfig }),
|
||||
contents: [{ role: "user", parts }],
|
||||
},
|
||||
{ headers: request.headers, withCredentials: request.credentials === "include", signal: options?.signal },
|
||||
options,
|
||||
);
|
||||
return parseGeminiImagePayload(response.data);
|
||||
}
|
||||
|
||||
function parseGeminiImagePayload(payload: GeminiPayload) {
|
||||
validateGeminiPayload(payload);
|
||||
const images =
|
||||
payload.candidates
|
||||
?.flatMap((candidate) => candidate.content?.parts || [])
|
||||
.map((part) => {
|
||||
const inlineData = part.inlineData || (part.inline_data ? { mimeType: part.inline_data.mimeType || part.inline_data.mime_type, data: part.inline_data.data } : undefined);
|
||||
if (inlineData?.data) return `data:${inlineData.mimeType || "image/png"};base64,${inlineData.data}`;
|
||||
return part.fileData?.fileUri || null;
|
||||
})
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.map((dataUrl) => ({ id: nanoid(), dataUrl })) || [];
|
||||
if (!images.length) throw new Error("Gemini 接口没有返回图片");
|
||||
return images;
|
||||
return parseGeminiImagePayload(response);
|
||||
}
|
||||
|
||||
export async function requestGeneration(config: AiConfig, prompt: string, options?: RequestOptions) {
|
||||
@@ -962,17 +127,6 @@ export async function requestGeneration(config: AiConfig, prompt: string, option
|
||||
}
|
||||
}
|
||||
|
||||
async function postChannelJSON<T>(config: ReturnType<typeof resolveModelRequestConfig>, upstreamUrl: string, body: unknown, options?: RequestOptions) {
|
||||
const request = channelRequest(config, upstreamUrl, aiHeaders(config, "application/json"));
|
||||
return (
|
||||
await axios.post<T>(request.url, body, {
|
||||
headers: request.headers,
|
||||
withCredentials: request.credentials === "include",
|
||||
signal: options?.signal,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
async function grokImageInputURL(image: ReferenceImage) {
|
||||
const candidate = image.url?.trim() || "";
|
||||
if (/^https?:\/\//i.test(candidate)) return candidate;
|
||||
@@ -1174,72 +328,5 @@ export async function requestToolResponse(config: AiConfig, messages: ResponseIn
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchImageModels(config: Pick<AiConfig, "baseUrl" | "apiKey" | "apiFormat"> & { allowLocalChannel?: boolean }) {
|
||||
try {
|
||||
if (config.apiFormat === "gemini") {
|
||||
const requestConfig = { ...defaultGeminiConfig, ...config };
|
||||
const request = channelRequest(requestConfig, geminiApiUrl(requestConfig), geminiHeaders(requestConfig));
|
||||
const response = await axios.get<GeminiPayload>(request.url, { headers: request.headers, withCredentials: request.credentials === "include" });
|
||||
validateGeminiPayload(response.data);
|
||||
return (response.data.models || [])
|
||||
.map((model) => model.name?.replace(/^models\//, ""))
|
||||
.filter((id): id is string => Boolean(id))
|
||||
.sort((a, b) => a.localeCompare(b));
|
||||
}
|
||||
const request = channelRequest(config, buildApiUrl(config.baseUrl, "/models"), { Authorization: `Bearer ${config.apiKey}` });
|
||||
const response = await axios.get<{ data?: Array<{ id?: string }>; error?: { message?: string } }>(request.url, { headers: request.headers, withCredentials: request.credentials === "include" });
|
||||
return (response.data.data || [])
|
||||
.map((model) => model.id)
|
||||
.filter((id): id is string => Boolean(id))
|
||||
.sort((a, b) => a.localeCompare(b));
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "读取模型失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export type ChannelModelFetchResult = { models: string[]; catalog: ChannelModelCatalogItem[] };
|
||||
|
||||
export async function fetchChannelModels(channel: ModelChannel, viaBackend = false): Promise<ChannelModelFetchResult> {
|
||||
const runtimeChannel = projectDesktopLocalChannelRuntime(channel);
|
||||
if (!viaBackend) {
|
||||
const models = await fetchImageModels({ baseUrl: runtimeChannel.baseUrl, allowLocalChannel: runtimeChannel.allowLocalChannel === true, apiKey: runtimeChannel.apiKey, apiFormat: runtimeChannel.apiFormat });
|
||||
return { models, catalog: models.map((id) => ({ id })) };
|
||||
}
|
||||
try {
|
||||
// 登录态由同源后端代取模型目录,避免每个 OpenAI 兼容服务分别维护浏览器 CORS 白名单。
|
||||
const response = await axios.post<{ code?: number; data?: { models?: Array<string | ChannelModelCatalogItem> }; msg?: string }>(
|
||||
resolveBackendApiUrl("/api/ai/models"),
|
||||
{
|
||||
baseUrl: runtimeChannel.baseUrl,
|
||||
allowLocalChannel: runtimeChannel.allowLocalChannel === true,
|
||||
apiKey: runtimeChannel.apiKey,
|
||||
apiFormat: runtimeChannel.apiFormat,
|
||||
headers: runtimeChannel.headers,
|
||||
},
|
||||
{ withCredentials: true },
|
||||
);
|
||||
if (typeof response.data.code === "number" && response.data.code !== 0) {
|
||||
throw new Error(response.data.msg || "读取模型失败");
|
||||
}
|
||||
const catalog = new Map<string, ChannelModelCatalogItem>();
|
||||
for (const item of response.data.data?.models || []) {
|
||||
const entry = typeof item === "string" ? sanitizeChannelModelCatalogItem({ id: item }) : sanitizeChannelModelCatalogItem(item);
|
||||
if (!entry) continue;
|
||||
const existing = catalog.get(entry.id);
|
||||
catalog.set(entry.id, existing || entry);
|
||||
}
|
||||
const models = Array.from(catalog.keys()).sort((a, b) => a.localeCompare(b));
|
||||
const sortedCatalog = Array.from(catalog.values()).sort((a, b) => a.id.localeCompare(b.id));
|
||||
return { models, catalog: sortedCatalog };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "读取模型失败"));
|
||||
}
|
||||
}
|
||||
|
||||
const defaultGeminiConfig: Pick<AiConfig, "baseUrl" | "apiKey" | "apiFormat" | "model" | "systemPrompt"> = {
|
||||
baseUrl: "https://generativelanguage.googleapis.com",
|
||||
apiKey: "",
|
||||
apiFormat: "gemini",
|
||||
model: "",
|
||||
systemPrompt: "",
|
||||
};
|
||||
export { fetchChannelModels, fetchImageModels } from "@/services/api/image-models";
|
||||
export type { ChannelModelFetchResult } from "@/services/api/image-models";
|
||||
|
||||
@@ -8,23 +8,59 @@ export type BackendEnvelope<T> = {
|
||||
msg: string;
|
||||
};
|
||||
|
||||
export class ApiError extends Error {
|
||||
readonly status?: number;
|
||||
readonly code?: number;
|
||||
readonly retryable: boolean;
|
||||
readonly cause?: unknown;
|
||||
|
||||
constructor(message: string, options: { status?: number; code?: number; retryable?: boolean; cause?: unknown } = {}) {
|
||||
super(message);
|
||||
this.name = "ApiError";
|
||||
this.status = options.status;
|
||||
this.code = options.code;
|
||||
this.retryable = options.retryable ?? isRetryableStatus(options.status ?? options.code);
|
||||
this.cause = options.cause;
|
||||
}
|
||||
}
|
||||
|
||||
// 所有后端 JSON 请求共用同一实例,避免认证、Base URL 和错误语义在模块间漂移。
|
||||
export const apiBaseURL = import.meta.env.VITE_CANVAS_BACKEND_URL || "/api";
|
||||
export const apiClient = axios.create({ baseURL: apiBaseURL, withCredentials: true });
|
||||
|
||||
export async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
export async function request<T>(promise: Promise<{ data: BackendEnvelope<T>; status?: number }>) {
|
||||
try {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
if (response.data.code !== 0) {
|
||||
throw new ApiError(response.data.msg || "请求失败", {
|
||||
status: response.status,
|
||||
code: response.data.code,
|
||||
retryable: isRetryableStatus(response.status) || isRetryableStatus(response.data.code),
|
||||
});
|
||||
}
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
if (axios.isCancel(error) || (axios.isAxiosError(error) && error.code === axios.AxiosError.ERR_CANCELED)) {
|
||||
throw new DOMException("请求已取消", "AbortError");
|
||||
}
|
||||
if (axios.isAxiosError<BackendEnvelope<unknown>>(error)) {
|
||||
throw new Error(error.response?.data?.msg || error.message || "请求失败");
|
||||
const status = error.response?.status;
|
||||
const code = error.response?.data?.code;
|
||||
throw new ApiError(error.response?.data?.msg || error.message || "请求失败", {
|
||||
status,
|
||||
code,
|
||||
retryable: isRetryableStatus(status) || isRetryableStatus(code),
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function isRetryableStatus(status?: number) {
|
||||
return status === 408 || status === 425 || status === 429 || (status !== undefined && status >= 500 && status <= 599);
|
||||
}
|
||||
|
||||
export function compactApiParams(params: ApiParams) {
|
||||
return Object.fromEntries(Object.entries(params).filter(([, value]) => value !== "" && value !== undefined && (!Array.isArray(value) || value.length > 0))) as ApiParams;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { DREAMINA_SUBMIT_ERROR_MESSAGES, generationErrorMessage } from "@/lib/generation-error";
|
||||
import { apiClient, request, type BackendEnvelope } from "@/services/api/request";
|
||||
import {
|
||||
cancelLocalDreaminaGenerationTask,
|
||||
deleteLocalDreaminaGenerationTask,
|
||||
listLocalDreaminaGenerationTaskPage,
|
||||
queryLocalDreaminaGenerationTask,
|
||||
@@ -407,13 +406,6 @@ export function queryFailedVideoProviderTask(id: string) {
|
||||
return request<ProviderTaskQueryResult>(api.post(`/tasks/${encodeURIComponent(id)}/query-provider`));
|
||||
}
|
||||
|
||||
export function cancelGenerationTask(id: string) {
|
||||
if (isLocalDreaminaTaskId(id)) {
|
||||
return cancelLocalDreaminaGenerationTask(stripLocalDreaminaTaskPrefix(id)).then((task) => projectLocalDreaminaTask(task));
|
||||
}
|
||||
return request<GenerationTask>(api.post(`/tasks/${encodeURIComponent(id)}/cancel`));
|
||||
}
|
||||
|
||||
export function refreshGenerationTaskStatus(id: string, options?: { signal?: AbortSignal }) {
|
||||
if (!isLocalDreaminaTaskId(id)) return Promise.reject(new Error("当前任务不支持手动更新官方状态"));
|
||||
return refreshLocalDreaminaGenerationTask(stripLocalDreaminaTaskPrefix(id), {}, options?.signal).then((task) => projectLocalDreaminaTask(task));
|
||||
@@ -531,8 +523,7 @@ export async function waitForGenerationTask(id: string, options?: { signal?: Abo
|
||||
}
|
||||
} catch (error) {
|
||||
if (options?.signal?.aborted) {
|
||||
await cancelGenerationTask(id).catch(() => undefined);
|
||||
window.dispatchEvent(new CustomEvent("wallet:updated"));
|
||||
// Abort 只停止当前页面的状态监听,不能把已发起的上游任务改成取消状态。
|
||||
throw new DOMException("Aborted", "AbortError");
|
||||
}
|
||||
throw error;
|
||||
|
||||
45
web/src/services/api/video-contracts.ts
Normal file
45
web/src/services/api/video-contracts.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { resolveModelRequestConfig } from "@/stores/use-config-store";
|
||||
|
||||
export type VideoResponse = {
|
||||
id?: string;
|
||||
request_id?: string;
|
||||
task_id?: string;
|
||||
status?: string;
|
||||
error?: { message?: string };
|
||||
video?: { url?: string };
|
||||
video_url?: string;
|
||||
result_url?: string;
|
||||
};
|
||||
|
||||
export type MiniMaxVideoTask = {
|
||||
id?: string;
|
||||
status?: string;
|
||||
content?: { url?: string };
|
||||
error?: { code?: string | number; message?: string };
|
||||
};
|
||||
|
||||
export type MiniMaxVideoCreateResponse = {
|
||||
task_id?: string;
|
||||
request_id?: string;
|
||||
data?: { task_id?: string; id?: string };
|
||||
};
|
||||
|
||||
export type ApiVideoResponse = VideoResponse | { code?: number; data?: VideoResponse | null; msg?: string };
|
||||
export type ResolvedAiConfig = ReturnType<typeof resolveModelRequestConfig>;
|
||||
|
||||
export type SeedanceTask = {
|
||||
id: string;
|
||||
task_id?: string;
|
||||
status?: "queued" | "running" | "succeeded" | "completed" | "failed" | "cancelled" | "expired";
|
||||
error?: { code?: string; message?: string } | null;
|
||||
error_code?: string | null;
|
||||
video_url?: string | null;
|
||||
content?: { video_url?: string; last_frame_url?: string } | null;
|
||||
};
|
||||
|
||||
export type ApiEnvelope<T> = T | { code?: number; data?: T | null; msg?: string };
|
||||
export type RequestOptions = { signal?: AbortSignal };
|
||||
|
||||
export type VideoGenerationResult = { blob?: Blob; url?: string; mimeType?: string };
|
||||
export type VideoGenerationTask = { id: string; provider: "openai" | "seedance" | "video-generations" | "gemini-veo" | "novita" | "minimax"; model: string };
|
||||
export type VideoGenerationTaskState = { status: "pending" } | { status: "completed"; result: VideoGenerationResult } | { status: "failed"; error: string };
|
||||
8
web/src/services/api/video-provider-deps.ts
Normal file
8
web/src/services/api/video-provider-deps.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { VideoResponseTools } from "./video-response";
|
||||
import type { VideoTransport } from "./video-transport";
|
||||
|
||||
/** Provider 依赖显式注入,避免各协议重新创建客户端或重复解包响应。 */
|
||||
export type VideoProviderDeps = {
|
||||
transport: VideoTransport;
|
||||
response: VideoResponseTools;
|
||||
};
|
||||
94
web/src/services/api/video-provider-gemini.ts
Normal file
94
web/src/services/api/video-provider-gemini.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import { modelOptionName } from "@/stores/use-config-store";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
|
||||
import { normalizeVideoResolution, normalizeVideoSeconds, normalizeVideoSize } from "./video-validation";
|
||||
import type { RequestOptions, ResolvedAiConfig, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
|
||||
type GeminiVeoOperation = {
|
||||
name?: string;
|
||||
done?: boolean;
|
||||
error?: { message?: string };
|
||||
response?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export async function createGeminiVeoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
if (references.length > 1 || videoReferences.length || audioReferences.length) throw new Error("Gemini Veo 当前只支持 1 张起始图,不支持参考视频或音频");
|
||||
const instance: Record<string, unknown> = { prompt: prompt.trim() };
|
||||
if (references[0]) {
|
||||
const dataUrl = await imageToDataUrl(references[0]);
|
||||
const matched = /^data:([^;,]+);base64,(.+)$/s.exec(dataUrl);
|
||||
if (!matched) throw new Error("Gemini Veo 起始图读取失败");
|
||||
instance.image = { bytesBase64Encoded: matched[2], mimeType: matched[1] };
|
||||
}
|
||||
const payload = {
|
||||
instances: [instance],
|
||||
parameters: {
|
||||
aspectRatio: normalizeVideoSize(config.size) || "16:9",
|
||||
durationSeconds: Number.parseInt(normalizeVideoSeconds(config.videoSeconds), 10) || 6,
|
||||
resolution: normalizeVideoResolution(config.vquality),
|
||||
sampleCount: 1,
|
||||
},
|
||||
};
|
||||
try {
|
||||
const response = await deps.transport.post<GeminiVeoOperation>(geminiVeoCreateUrl(config, modelOptionName(model)), payload, options);
|
||||
if (!response.name) throw new Error("Gemini Veo 没有返回 operation name");
|
||||
return { id: response.name, provider: "gemini-veo", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "Gemini Veo 任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function pollGeminiVeoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const operation = await deps.transport.get<GeminiVeoOperation>(geminiVeoOperationUrl(config, task.id), options);
|
||||
if (operation.error?.message) return { status: "failed", error: operation.error.message };
|
||||
if (!operation.done) return { status: "pending" };
|
||||
const url = findGeminiVideoURL(operation.response);
|
||||
if (!url) return { status: "failed", error: "Gemini Veo 任务已完成但没有返回视频地址" };
|
||||
const blob = await deps.transport.getExternalBlob(url, geminiVeoHeaders(config), options);
|
||||
await deps.response.assertVideoBlob(blob);
|
||||
return { status: "completed", result: { blob } };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "Gemini Veo 任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function geminiVeoCreateUrl(config: ResolvedAiConfig, model: string) {
|
||||
return `${geminiVeoBaseUrl(config)}/models/${encodeURIComponent(model)}:predictLongRunning`;
|
||||
}
|
||||
|
||||
function geminiVeoOperationUrl(config: ResolvedAiConfig, operationName: string) {
|
||||
return `${geminiVeoBaseUrl(config)}/${operationName.replace(/^\/+/, "")}`;
|
||||
}
|
||||
|
||||
function geminiVeoBaseUrl(config: ResolvedAiConfig) {
|
||||
const base = config.baseUrl.replace(/\/+$/, "");
|
||||
return /\/v1beta$/i.test(base) ? base : `${base}/v1beta`;
|
||||
}
|
||||
|
||||
function geminiVeoHeaders(config: ResolvedAiConfig) {
|
||||
return { "x-goog-api-key": config.apiKey };
|
||||
}
|
||||
|
||||
function findGeminiVideoURL(value: unknown): string {
|
||||
if (!value || typeof value !== "object") return "";
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
const found = findGeminiVideoURL(item);
|
||||
if (found) return found;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
const record = value as Record<string, unknown>;
|
||||
for (const key of ["uri", "url", "videoUri", "video_url"]) {
|
||||
if (typeof record[key] === "string" && /^https?:\/\//i.test(record[key])) return record[key];
|
||||
}
|
||||
for (const child of Object.values(record)) {
|
||||
const found = findGeminiVideoURL(child);
|
||||
if (found) return found;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
99
web/src/services/api/video-provider-minimax.ts
Normal file
99
web/src/services/api/video-provider-minimax.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { getMediaBlob } from "@/services/file-storage";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import { boolConfig } from "@/lib/seedance-video";
|
||||
import { modelOptionName } from "@/stores/use-config-store";
|
||||
import { isPublicMediaUrl } from "./video-validation";
|
||||
import { blobToDataUrl } from "./video-response";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
|
||||
import type { MiniMaxVideoCreateResponse, MiniMaxVideoTask, RequestOptions, ResolvedAiConfig, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
|
||||
export async function createMiniMaxVideoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
const imageUrls = await Promise.all(references.slice(0, 9).map((image) => resolveMiniMaxImageUrl(image)));
|
||||
const videoUrls = await Promise.all(videoReferences.slice(0, 3).map((video) => resolveMiniMaxMediaUrl(video, "参考视频")));
|
||||
const audioUrls = await Promise.all(audioReferences.slice(0, 3).map((audio) => resolveMiniMaxMediaUrl(audio, "参考音频")));
|
||||
const content: Array<Record<string, unknown>> = [{ type: "text", text: prompt.trim() }];
|
||||
imageUrls.forEach((url, index) => {
|
||||
// 首尾帧只允许成对出现;三张及以上图片统一作为多模态参考图,避免提交非法组合。
|
||||
const role = videoUrls.length || audioUrls.length || imageUrls.length > 2 ? "reference_image" : index === 0 ? "first_frame" : "last_frame";
|
||||
content.push({ type: "image_url", image_url: { url }, role });
|
||||
});
|
||||
videoUrls.forEach((url) => content.push({ type: "video_url", video_url: { url }, role: "reference_video" }));
|
||||
audioUrls.forEach((url) => content.push({ type: "audio_url", audio_url: { url }, role: "reference_audio" }));
|
||||
const frameMode = imageUrls.length > 0 && imageUrls.length <= 2 && videoUrls.length === 0 && audioUrls.length === 0;
|
||||
const payload = {
|
||||
model: modelOptionName(model),
|
||||
content,
|
||||
resolution: normalizeMiniMaxResolution(config.vquality),
|
||||
duration: normalizeMiniMaxDuration(config.videoSeconds),
|
||||
ratio: normalizeMiniMaxRatio(config.size, frameMode),
|
||||
aigc_watermark: boolConfig(config.videoWatermark, false),
|
||||
};
|
||||
try {
|
||||
const created = await deps.transport.post<MiniMaxVideoCreateResponse>(miniMaxVideoUrl(config, "/video_generation"), payload, options);
|
||||
const id = created.task_id || created.request_id || created.data?.task_id || created.data?.id || "";
|
||||
if (!id) throw new Error("MiniMax 视频接口没有返回任务 ID");
|
||||
return { id, provider: "minimax", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "MiniMax 视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function pollMiniMaxVideoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const response = await deps.transport.get<{ task?: MiniMaxVideoTask }>(miniMaxVideoUrl(config, `/query/video_generation/${encodeURIComponent(task.id)}`), options);
|
||||
const state = response.task || {};
|
||||
const status = String(state.status || "").toLowerCase();
|
||||
if (status === "succeeded" || status === "completed") {
|
||||
const url = state.content?.url || "";
|
||||
if (!url) return { status: "failed", error: "MiniMax 视频任务已完成但没有返回视频地址" };
|
||||
return { status: "completed", result: await deps.response.videoResultFromUrl(url, options) };
|
||||
}
|
||||
if (status === "failed" || status === "cancelled") {
|
||||
const code = state.error?.code ? `${state.error.code}:` : "";
|
||||
return { status: "failed", error: `${code}${state.error?.message || "MiniMax 视频生成失败"}` };
|
||||
}
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "MiniMax 视频任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function miniMaxVideoUrl(config: ResolvedAiConfig, path: string) {
|
||||
const base = config.baseUrl.replace(/\/+$/, "");
|
||||
return /\/v2$/i.test(base) ? `${base}${path}` : `${base}/v2${path}`;
|
||||
}
|
||||
|
||||
function normalizeMiniMaxResolution(value: string) {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return ["2k", "4k", "high", "1080", "1080p", "1440p", "2160p"].includes(normalized) ? "2K" : "768P";
|
||||
}
|
||||
|
||||
function normalizeMiniMaxDuration(value: string) {
|
||||
const seconds = Math.floor(Number(value) || 5);
|
||||
return Math.max(4, Math.min(15, seconds));
|
||||
}
|
||||
|
||||
function normalizeMiniMaxRatio(value: string, frameMode: boolean) {
|
||||
if (frameMode) return "adaptive";
|
||||
return ["adaptive", "21:9", "16:9", "4:3", "1:1", "3:4", "9:16"].includes(value) ? value : "16:9";
|
||||
}
|
||||
|
||||
async function resolveMiniMaxImageUrl(image: ReferenceImage) {
|
||||
const directUrl = image.url || image.dataUrl;
|
||||
if (isPublicMediaUrl(directUrl) || directUrl.startsWith("data:")) return directUrl;
|
||||
const dataUrl = await imageToDataUrl(image);
|
||||
if (!dataUrl) throw new Error("MiniMax 参考图读取失败,请换一张图片或重新上传");
|
||||
return dataUrl;
|
||||
}
|
||||
|
||||
async function resolveMiniMaxMediaUrl(media: ReferenceVideo | ReferenceAudio, label: string) {
|
||||
if (isPublicMediaUrl(media.url) || media.url?.startsWith("data:")) return media.url;
|
||||
let blob: Blob | null = null;
|
||||
if (media.storageKey) blob = await getMediaBlob(media.storageKey);
|
||||
if (!blob && media.url?.startsWith("blob:")) blob = await (await fetch(media.url)).blob();
|
||||
if (!blob) throw new Error(`MiniMax ${label}必须是公网 URL 或本地已保存素材`);
|
||||
return blobToDataUrl(blob);
|
||||
}
|
||||
73
web/src/services/api/video-provider-newapi.ts
Normal file
73
web/src/services/api/video-provider-newapi.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { modelCapabilityConfigFor, videoResolutionRequest } from "@/lib/model-capabilities";
|
||||
import { boolConfig } from "@/lib/seedance-video";
|
||||
import { getResourceOSSUrl } from "@/services/api/resources";
|
||||
import { modelOptionName } from "@/stores/use-config-store";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
|
||||
import type { ApiEnvelope, ApiVideoResponse, RequestOptions, ResolvedAiConfig, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
import { normalizeVideoSeconds, normalizeVideoSize } from "./video-validation";
|
||||
|
||||
export async function createVideoGenerationsTask(deps: VideoProviderDeps, config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
if (references.length > 9 || videoReferences.length > 3 || audioReferences.length > 3) throw new Error("NewAPI Video Generations 最多支持 9 张参考图、3 个参考视频和 3 个参考音频");
|
||||
if (audioReferences.length > 0 && videoReferences.length === 0) throw new Error("NewAPI Video Generations 的参考音频必须同时提供至少 1 个参考视频;纯音频生视频请切换到支持该模式的渠道");
|
||||
const [imageUrls, videoUrls, audioUrls] = await Promise.all([
|
||||
Promise.all(references.map((item) => resolveVideoGenerationsUrl(item.url || item.dataUrl, item.storageKey))),
|
||||
Promise.all(videoReferences.map((item) => resolveVideoGenerationsUrl(item.url, item.storageKey))),
|
||||
Promise.all(audioReferences.map((item) => resolveVideoGenerationsUrl(item.url, item.storageKey))),
|
||||
]);
|
||||
const profile = modelCapabilityConfigFor(config, model).video!;
|
||||
const resolution = newAPIVideoResolutionRequest(profile, config.vquality, modelOptionName(model));
|
||||
const payload = {
|
||||
model: modelOptionName(model),
|
||||
prompt: prompt.trim(),
|
||||
seconds: normalizeVideoSeconds(config.videoSeconds),
|
||||
aspect_ratio: normalizeVideoSize(config.size) || "16:9",
|
||||
...(resolution ? { resolution } : {}),
|
||||
...(profile.generateAudio.supported ? { generate_audio: boolConfig(config.videoGenerateAudio, profile.generateAudio.default) } : {}),
|
||||
...(imageUrls.length ? { image_urls: imageUrls } : {}),
|
||||
...(videoUrls.length ? { video_urls: videoUrls } : {}),
|
||||
...(audioUrls.length ? { audio_urls: audioUrls } : {}),
|
||||
};
|
||||
try {
|
||||
const created = deps.response.unwrapVideoResponse(await deps.transport.post<ApiVideoResponse>(deps.transport.apiUrl("/video/generations"), payload, options));
|
||||
const id = deps.response.videoTaskId(created);
|
||||
if (!id) throw new Error("NewAPI Video Generations 没有返回任务 ID");
|
||||
return { id, provider: "video-generations", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "NewAPI Video Generations 任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function pollVideoGenerationsTask(deps: VideoProviderDeps, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const raw = await deps.transport.get<ApiEnvelope<Record<string, unknown>>>(deps.transport.apiUrl(`/video/generations/${encodeURIComponent(task.id)}`), options);
|
||||
const state = deps.response.unwrapEnvelopeRecord(raw);
|
||||
const status = String(state.status || "").toUpperCase();
|
||||
if (status === "SUCCESS" || status === "SUCCEEDED" || status === "COMPLETED") {
|
||||
const url = String(state.result_url || state.video_url || state.url || "");
|
||||
if (!url) return { status: "failed", error: "视频任务已完成但没有返回视频地址" };
|
||||
return { status: "completed", result: await deps.response.videoResultFromUrl(url, options) };
|
||||
}
|
||||
if (status === "FAILURE" || status === "FAILED" || status === "CANCELLED") return { status: "failed", error: String(state.fail_reason || state.error || "视频生成失败") };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "NewAPI Video Generations 任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function newAPIVideoResolutionRequest(profile: NonNullable<ReturnType<typeof modelCapabilityConfigFor>["video"]>, value: string, model: string) {
|
||||
if (model.trim().toLowerCase() === "grok-video-1.5-1080p") return "1080p";
|
||||
return videoResolutionRequest(profile, value);
|
||||
}
|
||||
|
||||
async function resolveVideoGenerationsUrl(value: string | undefined, storageKey?: string) {
|
||||
if (storageKey?.startsWith("resource:")) return getResourceOSSUrl(storageKey);
|
||||
if (isPublicMediaUrl(value || "")) return String(value);
|
||||
throw new Error("NewAPI Video Generations 的参考素材需要公网 URL;请先把素材保存到对象存储");
|
||||
}
|
||||
|
||||
function isPublicMediaUrl(value: string) {
|
||||
return /^https?:\/\//i.test(value || "");
|
||||
}
|
||||
60
web/src/services/api/video-provider-novita.ts
Normal file
60
web/src/services/api/video-provider-novita.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import { modelOptionName } from "@/stores/use-config-store";
|
||||
import { isPublicMediaUrl } from "./video-validation";
|
||||
import { normalizeSeedanceDuration } from "@/lib/seedance-video";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
|
||||
import type { RequestOptions, ResolvedAiConfig, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
|
||||
type NovitaVideoResult = { task?: { status?: string; reason?: string }; videos?: Array<{ video_url?: string }> };
|
||||
|
||||
export async function createNovitaVideoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
if (references.length > 1 || videoReferences.length || audioReferences.length) throw new Error("Novita 视频当前只支持 1 张起始图,不支持参考视频或音频");
|
||||
const payload: Record<string, unknown> = {
|
||||
model: modelOptionName(model),
|
||||
prompt: prompt.trim(),
|
||||
duration: normalizeNovitaVideoDuration(config.videoSeconds),
|
||||
};
|
||||
if (references[0]) {
|
||||
payload.image = isPublicMediaUrl(references[0].url || "") ? references[0].url : await imageToDataUrl(references[0]);
|
||||
} else {
|
||||
payload.aspect_ratio = normalizeNovitaVideoRatio(config.size);
|
||||
}
|
||||
try {
|
||||
const created = await deps.transport.post<{ task_id?: string }>(novitaVideoUrl(config, "/video/create"), payload, options);
|
||||
if (!created.task_id) throw new Error("Novita 视频接口没有返回任务 ID");
|
||||
return { id: created.task_id, provider: "novita", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "Novita 视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function pollNovitaVideoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const result = await deps.transport.get<NovitaVideoResult>(novitaVideoUrl(config, `/async/task-result?task_id=${encodeURIComponent(task.id)}`), options);
|
||||
const status = result.task?.status || "";
|
||||
if (status === "TASK_STATUS_SUCCEED") {
|
||||
const url = result.videos?.[0]?.video_url || "";
|
||||
if (!url) return { status: "failed", error: "Novita 视频任务已完成但没有返回视频地址" };
|
||||
return { status: "completed", result: await deps.response.videoResultFromUrl(url, options) };
|
||||
}
|
||||
if (status === "TASK_STATUS_FAILED") return { status: "failed", error: result.task?.reason || "视频生成失败" };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "Novita 视频任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function novitaVideoUrl(config: ResolvedAiConfig, path: string) {
|
||||
return `${config.baseUrl.replace(/\/+$/, "")}${path}`;
|
||||
}
|
||||
|
||||
function normalizeNovitaVideoDuration(value: string) {
|
||||
return normalizeSeedanceDuration(value) >= 8 ? "10" : "5";
|
||||
}
|
||||
|
||||
function normalizeNovitaVideoRatio(value: string) {
|
||||
return value === "16:9" || value === "9:16" || value === "1:1" ? value : "16:9";
|
||||
}
|
||||
68
web/src/services/api/video-provider-openai.ts
Normal file
68
web/src/services/api/video-provider-openai.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { dataUrlToFile } from "@/lib/image-utils";
|
||||
import { modelCapabilityConfigFor, videoResolutionRequest } from "@/lib/model-capabilities";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import { modelOptionName } from "@/stores/use-config-store";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
|
||||
import { normalizeVideoSeconds, normalizeVideoSize } from "./video-validation";
|
||||
import type { RequestOptions, ResolvedAiConfig, ApiVideoResponse, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
|
||||
export async function createOpenAIVideoTask(deps: VideoProviderDeps, config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
const modelName = modelOptionName(model);
|
||||
if (config.interfaceType === "xai-video" || modelName.toLowerCase().includes("grok")) {
|
||||
const images = await Promise.all(references.slice(0, 7).map((image) => imageToDataUrl(image)));
|
||||
const seconds = normalizeVideoSeconds(config.videoSeconds);
|
||||
const payload = {
|
||||
model: modelName,
|
||||
prompt,
|
||||
duration: Number.parseInt(seconds, 10) || 6,
|
||||
seconds,
|
||||
...(normalizeVideoSize(config.size) ? { size: normalizeVideoSize(config.size) } : {}),
|
||||
...(images.length ? { image: images[0], images } : {}),
|
||||
};
|
||||
try {
|
||||
const createPath = config.interfaceType === "xai-video" ? "/videos/generations" : "/videos";
|
||||
const created = deps.response.unwrapVideoResponse(await deps.transport.post<ApiVideoResponse>(deps.transport.apiUrl(createPath), payload, options));
|
||||
const id = deps.response.videoTaskId(created);
|
||||
if (!id) throw new Error("视频接口没有返回任务 ID");
|
||||
return { id, provider: "openai", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
const body = new FormData();
|
||||
body.append("model", modelName);
|
||||
body.append("prompt", prompt);
|
||||
body.append("seconds", normalizeVideoSeconds(config.videoSeconds));
|
||||
if (normalizeVideoSize(config.size)) body.append("size", normalizeVideoSize(config.size)!);
|
||||
const resolution = videoResolutionRequest(modelCapabilityConfigFor(config, model).video!, config.vquality);
|
||||
if (resolution) body.append("resolution_name", resolution);
|
||||
body.append("preset", "normal");
|
||||
const files = await Promise.all(references.slice(0, 7).map(async (image) => dataUrlToFile({ ...image, dataUrl: await imageToDataUrl(image) })));
|
||||
files.forEach((file) => body.append("input_reference[]", file));
|
||||
try {
|
||||
const created = deps.response.unwrapVideoResponse(await deps.transport.postForm<ApiVideoResponse>(deps.transport.apiUrl("/videos"), body, options));
|
||||
if (!created.id) throw new Error("视频接口没有返回任务 ID");
|
||||
return { id: created.id, provider: "openai", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function pollOpenAIVideoTask(deps: VideoProviderDeps, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const video = deps.response.unwrapVideoResponse(await deps.transport.get<ApiVideoResponse>(deps.transport.apiUrl(`/videos/${task.id}`), options));
|
||||
if (video.status === "completed" || video.status === "succeeded" || video.status === "success" || video.status === "done") {
|
||||
const resultUrl = video.video?.url || video.video_url || video.result_url;
|
||||
if (resultUrl) return { status: "completed", result: await deps.response.videoResultFromUrl(resultUrl, options) };
|
||||
const content = await deps.transport.getBlob(deps.transport.apiUrl(`/videos/${task.id}/content`), options);
|
||||
await deps.response.assertVideoBlob(content);
|
||||
return { status: "completed", result: { blob: content } };
|
||||
}
|
||||
if (video.status === "failed" || video.status === "cancelled") return { status: "failed", error: video.error?.message || "视频生成失败" };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "视频任务查询失败"));
|
||||
}
|
||||
}
|
||||
200
web/src/services/api/video-provider-seedance.ts
Normal file
200
web/src/services/api/video-provider-seedance.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import { modelCapabilityConfigFor } from "@/lib/model-capabilities";
|
||||
import { boolConfig, buildSeedancePromptText, isArkPlanBaseUrl, isSeedanceVideoConfig, normalizeSeedanceDuration, normalizeSeedanceRatio, normalizeSeedanceResolution, seedanceVideoReferenceError, SEEDANCE_REFERENCE_LIMITS } from "@/lib/seedance-video";
|
||||
import { getResourceOSSUrl } from "@/services/api/resources";
|
||||
import { getMediaBlob } from "@/services/file-storage";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import { buildApiUrl, modelOptionName, type AiConfig } from "@/stores/use-config-store";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
|
||||
import { isPublicMediaUrl } from "./video-validation";
|
||||
import type { ApiEnvelope, RequestOptions, ResolvedAiConfig, SeedanceTask, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
|
||||
export function isSeedanceConfig(config: ResolvedAiConfig) {
|
||||
return config.interfaceType === "volcengine-ark-video" || isSeedanceVideoConfig(config);
|
||||
}
|
||||
|
||||
export async function createSeedanceTask(deps: VideoProviderDeps, config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
assertSeedanceVideoReferences(videoReferences);
|
||||
assertSeedanceAudioReferences(audioReferences);
|
||||
const isVolcengineArk = config.interfaceType === "volcengine-ark-video";
|
||||
const payload = isVolcengineArk || isArkPlanBaseUrl(config.baseUrl)
|
||||
? await buildSeedanceAgentPlanPayload(config, model, prompt, references, videoReferences, audioReferences, deps)
|
||||
: await buildSeedanceVideosPayload(config, model, prompt, references, videoReferences, audioReferences, deps);
|
||||
|
||||
try {
|
||||
const raw = await deps.transport.post<ApiEnvelope<SeedanceTask>>(seedanceApiUrl(config), payload, options);
|
||||
const created = deps.response.unwrapSeedanceTask(raw);
|
||||
const id = created.id || created.task_id;
|
||||
if (!id) throw new Error("Seedance 接口没有返回任务 ID");
|
||||
return { id, provider: "seedance", model };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "Seedance 任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function pollSeedanceTask(deps: VideoProviderDeps, config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const raw = await deps.transport.get<ApiEnvelope<SeedanceTask>>(seedanceApiUrl(config, task.id), options);
|
||||
const state = deps.response.unwrapSeedanceTask(raw);
|
||||
if (state.status === "succeeded" || state.status === "completed") {
|
||||
const url = state.video_url || state.content?.video_url;
|
||||
if (url) return { status: "completed", result: await deps.response.videoResultFromUrl(url, options) };
|
||||
if (isArkPlanBaseUrl(config.baseUrl)) return { status: "failed", error: "Seedance 任务成功但没有返回视频 URL" };
|
||||
const content = await deps.transport.getBlob(deps.transport.apiUrl(`/videos/${task.id}/content`), options);
|
||||
await deps.response.assertVideoBlob(content);
|
||||
return { status: "completed", result: { blob: content } };
|
||||
}
|
||||
if (state.status === "failed" || state.status === "cancelled" || state.status === "expired") return { status: "failed", error: seedanceErrorMessage(state) || `Seedance 视频生成${state.status === "expired" ? "超时" : "失败"}` };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(deps.response.readAxiosError(error, "Seedance 任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function assertSeedanceVideoReferences(videoReferences: ReferenceVideo[]) {
|
||||
const error = seedanceVideoReferenceError(videoReferences);
|
||||
if (error) throw new Error(error);
|
||||
let total = 0;
|
||||
for (const video of videoReferences) {
|
||||
if (!video.durationMs) continue;
|
||||
if (video.durationMs < 2000 || video.durationMs > 15000) throw new Error("Seedance 参考视频单个时长需要在 2-15 秒之间");
|
||||
total += video.durationMs;
|
||||
}
|
||||
if (total > 15000) throw new Error("Seedance 参考视频总时长不能超过 15 秒");
|
||||
}
|
||||
|
||||
function assertSeedanceAudioReferences(audioReferences: ReferenceAudio[]) {
|
||||
let total = 0;
|
||||
for (const audio of audioReferences) {
|
||||
if (!audio.durationMs) continue;
|
||||
if (audio.durationMs < 2000 || audio.durationMs > 15000) throw new Error("Seedance 参考音频单个时长需要在 2-15 秒之间");
|
||||
total += audio.durationMs;
|
||||
}
|
||||
if (total > 15000) throw new Error("Seedance 参考音频总时长不能超过 15 秒");
|
||||
}
|
||||
|
||||
function seedanceApiUrl(config: ResolvedAiConfig, taskId?: string) {
|
||||
if (config.interfaceType === "volcengine-ark-video" || isArkPlanBaseUrl(config.baseUrl)) return buildApiUrl(config.baseUrl, `/contents/generations/tasks${taskId ? `/${encodeURIComponent(taskId)}` : ""}`);
|
||||
return buildApiUrl(config.baseUrl, `/videos${taskId ? `/${encodeURIComponent(taskId)}` : ""}`);
|
||||
}
|
||||
|
||||
async function buildSeedanceAgentPlanPayload(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], deps: VideoProviderDeps) {
|
||||
if (config.interfaceType !== "volcengine-ark-video" && audioReferences.length && !references.length && !videoReferences.length) {
|
||||
throw new Error("Seedance 参考音频不能单独使用,请同时添加参考图或参考视频");
|
||||
}
|
||||
const content = config.interfaceType === "volcengine-ark-video"
|
||||
? await buildVolcengineArkContent(prompt, references, videoReferences, audioReferences)
|
||||
: await buildSeedanceContent(config, prompt, references, videoReferences, audioReferences, deps);
|
||||
if (!content.length) throw new Error("请输入视频提示词,或连接参考图片/视频/音频");
|
||||
const profile = modelCapabilityConfigFor(config, model).video!;
|
||||
return {
|
||||
model: modelOptionName(model),
|
||||
content,
|
||||
ratio: normalizeSeedanceRatio(config.size),
|
||||
resolution: normalizeSeedanceResolution(config.vquality, modelOptionName(model)),
|
||||
duration: normalizeSeedanceDuration(config.videoSeconds),
|
||||
...(profile.generateAudio.supported ? { generate_audio: boolConfig(config.videoGenerateAudio, profile.generateAudio.default) } : {}),
|
||||
...(profile.watermark.supported ? { watermark: boolConfig(config.videoWatermark, profile.watermark.default) } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function buildVolcengineArkContent(prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[]) {
|
||||
const content: Array<Record<string, unknown>> = [];
|
||||
if (prompt.trim()) content.push({ type: "text", text: prompt.trim() });
|
||||
for (const image of references.slice(0, SEEDANCE_REFERENCE_LIMITS.images)) {
|
||||
content.push({ type: "image_url", image_url: { url: await resolveVolcengineArkReferenceUrl(image.url || image.dataUrl, image.storageKey) }, role: "reference_image" });
|
||||
}
|
||||
for (const video of videoReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.videos)) {
|
||||
content.push({ type: "video_url", video_url: { url: await resolveVolcengineArkReferenceUrl(video.url, video.storageKey) }, role: "reference_video" });
|
||||
}
|
||||
for (const audio of audioReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.audios)) {
|
||||
content.push({ type: "audio_url", audio_url: { url: await resolveVolcengineArkReferenceUrl(audio.url, audio.storageKey) }, role: "reference_audio" });
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
async function resolveVolcengineArkReferenceUrl(value: string | undefined, storageKey?: string) {
|
||||
if (storageKey?.startsWith("resource:")) return getResourceOSSUrl(storageKey);
|
||||
if (isPublicMediaUrl(value || "") || String(value || "").startsWith("asset://")) return String(value);
|
||||
throw new Error("火山方舟视频参考素材需要公网 URL 或 asset:// 素材 ID;请先将本地素材保存到对象存储");
|
||||
}
|
||||
|
||||
async function buildSeedanceVideosPayload(config: AiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], deps: VideoProviderDeps) {
|
||||
if ((videoReferences.length || audioReferences.length) && !references.length) {
|
||||
throw new Error("Seedance 参考视频或参考音频需要同时连接至少 1 张主参考图");
|
||||
}
|
||||
const imageUrls = await Promise.all(references.slice(0, SEEDANCE_REFERENCE_LIMITS.images).map(resolveSeedanceVideosImageUrl));
|
||||
const videoUrls = await Promise.all(videoReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.videos).map((media) => resolveSeedanceVideosMediaUrl(media, deps)));
|
||||
const audioUrls = await Promise.all(audioReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.audios).map((media) => resolveSeedanceVideosMediaUrl(media, deps)));
|
||||
const ratio = normalizeSeedanceRatio(config.size);
|
||||
const duration = normalizeSeedanceDuration(config.videoSeconds);
|
||||
const profile = modelCapabilityConfigFor(config, model).video!;
|
||||
return {
|
||||
model: modelOptionName(model),
|
||||
prompt: prompt.trim(),
|
||||
aspect_ratio: ratio === "adaptive" ? "16:9" : ratio,
|
||||
duration,
|
||||
...(profile.generateAudio.supported ? { generate_audio: boolConfig(config.videoGenerateAudio, profile.generateAudio.default) } : {}),
|
||||
...(imageUrls[0] ? { image_url: imageUrls[0] } : {}),
|
||||
...(imageUrls.length > 1 ? { reference_image_urls: imageUrls.slice(1) } : {}),
|
||||
...(videoUrls.length ? { reference_videos: videoUrls } : {}),
|
||||
...(audioUrls.length ? { reference_audios: audioUrls } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function buildSeedanceContent(config: AiConfig, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], deps: VideoProviderDeps) {
|
||||
const content: Array<Record<string, unknown>> = [];
|
||||
const text = buildSeedancePromptText(prompt, references, videoReferences, audioReferences);
|
||||
if (text) content.push({ type: "text", text });
|
||||
for (const image of references.slice(0, SEEDANCE_REFERENCE_LIMITS.images)) {
|
||||
content.push({ type: "image_url", image_url: { url: await resolveSeedanceImageUrl(image) }, role: "reference_image" });
|
||||
}
|
||||
for (const video of videoReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.videos)) {
|
||||
content.push({ type: "video_url", video_url: { url: await resolveSeedanceMediaUrl(video, deps, "参考视频") }, role: "reference_video" });
|
||||
}
|
||||
for (const audio of audioReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.audios)) {
|
||||
content.push({ type: "audio_url", audio_url: { url: await resolveSeedanceMediaUrl(audio, deps, "参考音频") }, role: "reference_audio" });
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
async function resolveSeedanceImageUrl(image: ReferenceImage) {
|
||||
const directUrl = image.url || image.dataUrl;
|
||||
if (isPublicMediaUrl(directUrl) || directUrl.startsWith("asset://")) return directUrl;
|
||||
const dataUrl = await imageToDataUrl(image);
|
||||
if (!dataUrl) throw new Error("参考图读取失败,请换一张图片或重新上传");
|
||||
return dataUrl;
|
||||
}
|
||||
|
||||
async function resolveSeedanceVideosImageUrl(image: ReferenceImage) {
|
||||
const directUrl = image.url || image.dataUrl;
|
||||
if (isPublicMediaUrl(directUrl) || directUrl.startsWith("data:")) return directUrl;
|
||||
const dataUrl = await imageToDataUrl(image);
|
||||
if (!dataUrl) throw new Error("参考图读取失败,请换一张图片或重新上传");
|
||||
return dataUrl;
|
||||
}
|
||||
|
||||
async function resolveSeedanceMediaUrl(media: ReferenceVideo | ReferenceAudio, deps: VideoProviderDeps, label: string) {
|
||||
if (isPublicMediaUrl(media.url) || media.url.startsWith("asset://")) return media.url;
|
||||
let blob: Blob | null = null;
|
||||
if (media.storageKey) blob = await getMediaBlob(media.storageKey);
|
||||
if (!blob && media.url?.startsWith("blob:")) blob = await (await fetch(media.url)).blob();
|
||||
if (!blob) throw new Error(`${label}必须是公网 URL、素材 ID,或本地已保存素材`);
|
||||
return deps.response.blobToDataUrl(blob);
|
||||
}
|
||||
|
||||
async function resolveSeedanceVideosMediaUrl(media: ReferenceVideo | ReferenceAudio, deps: VideoProviderDeps) {
|
||||
if (isPublicMediaUrl(media.url) || media.url?.startsWith("data:")) return media.url;
|
||||
let blob: Blob | null = null;
|
||||
if (media.storageKey) blob = await getMediaBlob(media.storageKey);
|
||||
if (!blob && media.url?.startsWith("blob:")) blob = await (await fetch(media.url)).blob();
|
||||
if (!blob) throw new Error("Seedance /videos 参考素材必须是公网 URL、data URL,或本地已保存素材");
|
||||
return deps.response.blobToDataUrl(blob);
|
||||
}
|
||||
|
||||
function seedanceErrorMessage(state: SeedanceTask) {
|
||||
if (state.error?.message && state.error.code) return `${state.error.code}:${state.error.message}`;
|
||||
return state.error?.message || state.error_code || "";
|
||||
}
|
||||
122
web/src/services/api/video-response.ts
Normal file
122
web/src/services/api/video-response.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import axios from "axios";
|
||||
|
||||
import type { ApiEnvelope, ApiVideoResponse, RequestOptions, SeedanceTask, VideoGenerationResult } from "./video-contracts";
|
||||
|
||||
export function videoTaskId(payload: { id?: string; request_id?: string; task_id?: string }) {
|
||||
return payload.id || payload.request_id || payload.task_id || "";
|
||||
}
|
||||
|
||||
export function unwrapVideoResponse(payload: ApiVideoResponse) {
|
||||
return unwrapEnvelope(payload, "接口没有返回视频任务");
|
||||
}
|
||||
|
||||
export function unwrapSeedanceTask(payload: ApiEnvelope<SeedanceTask>) {
|
||||
return unwrapEnvelope(payload, "Seedance 接口没有返回任务");
|
||||
}
|
||||
|
||||
export function unwrapEnvelope<T>(payload: ApiEnvelope<T>, emptyMessage: string): T {
|
||||
if (!payload) throw new Error(emptyMessage);
|
||||
if (typeof payload === "object" && "code" in payload && typeof payload.code === "number") {
|
||||
if (payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
if (!payload.data) throw new Error(emptyMessage);
|
||||
return payload.data;
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
export function readAxiosError(error: unknown, fallback: string) {
|
||||
if (axios.isCancel(error)) return "请求已取消";
|
||||
if (axios.isAxiosError<{ error?: { message?: string }; msg?: string; code?: number }>(error)) {
|
||||
const responseData = error.response?.data;
|
||||
return responseData?.msg || responseData?.error?.message || statusMessage(error.response?.status, fallback);
|
||||
}
|
||||
if (error instanceof DOMException && error.name === "AbortError") return "请求已取消";
|
||||
return error instanceof Error ? error.message : fallback;
|
||||
}
|
||||
|
||||
export function statusMessage(status: number | undefined, fallback: string) {
|
||||
if (status === 401 || status === 403) return "鉴权失败,请检查 API Key、套餐权限或模型权限";
|
||||
if (status === 429) return "请求被限流或额度不足,请稍后重试";
|
||||
return status ? `${fallback}(${status})` : fallback;
|
||||
}
|
||||
|
||||
export async function assertVideoBlob(blob: Blob) {
|
||||
if (!blob.type.includes("json")) return;
|
||||
let payload: { code?: number; msg?: string; error?: { message?: string } };
|
||||
try {
|
||||
payload = JSON.parse(await blob.text()) as { code?: number; msg?: string; error?: { message?: string } };
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "视频下载失败");
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
}
|
||||
|
||||
export function delay(ms: number, signal?: AbortSignal) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
if (signal?.aborted) {
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
return;
|
||||
}
|
||||
const timer = setTimeout(resolve, ms);
|
||||
signal?.addEventListener(
|
||||
"abort",
|
||||
() => {
|
||||
clearTimeout(timer);
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function blobToDataUrl(blob: Blob) {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(String(reader.result || ""));
|
||||
reader.onerror = () => reject(new Error("读取本地素材失败"));
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
|
||||
export async function videoResultFromUrl(url: string, options?: RequestOptions): Promise<VideoGenerationResult> {
|
||||
try {
|
||||
const response = await axios.get<Blob>(url, { responseType: "blob", signal: options?.signal });
|
||||
await assertVideoBlob(response.data);
|
||||
return { blob: response.data };
|
||||
} catch (error) {
|
||||
if (axios.isCancel(error) || options?.signal?.aborted) throw error;
|
||||
return { url, mimeType: "video/mp4" };
|
||||
}
|
||||
}
|
||||
|
||||
export type VideoResponseTools = {
|
||||
assertVideoBlob: typeof assertVideoBlob;
|
||||
blobToDataUrl: typeof blobToDataUrl;
|
||||
delay: typeof delay;
|
||||
readAxiosError: typeof readAxiosError;
|
||||
unwrapEnvelope: typeof unwrapEnvelope;
|
||||
unwrapEnvelopeRecord: typeof unwrapEnvelopeRecord;
|
||||
unwrapSeedanceTask: typeof unwrapSeedanceTask;
|
||||
unwrapVideoResponse: typeof unwrapVideoResponse;
|
||||
videoResultFromUrl: typeof videoResultFromUrl;
|
||||
videoTaskId: typeof videoTaskId;
|
||||
};
|
||||
|
||||
function unwrapEnvelopeRecord(value: ApiEnvelope<Record<string, unknown>>): Record<string, unknown> {
|
||||
if (value && typeof value === "object" && "data" in value && value.data && typeof value.data === "object") return value.data as Record<string, unknown>;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
export const videoResponseTools: VideoResponseTools = {
|
||||
assertVideoBlob,
|
||||
blobToDataUrl,
|
||||
delay,
|
||||
readAxiosError,
|
||||
unwrapEnvelope,
|
||||
unwrapEnvelopeRecord,
|
||||
unwrapSeedanceTask,
|
||||
unwrapVideoResponse,
|
||||
videoResultFromUrl,
|
||||
videoTaskId,
|
||||
};
|
||||
54
web/src/services/api/video-transport.ts
Normal file
54
web/src/services/api/video-transport.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { createClientId } from "@/lib/client-id";
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import { buildApiUrl, isSystemProxyBaseUrl } from "@/stores/use-config-store";
|
||||
|
||||
import type { RequestOptions, ResolvedAiConfig } from "./video-contracts";
|
||||
|
||||
export type VideoTransport = {
|
||||
apiUrl: (path: string) => string;
|
||||
post: <T>(upstreamUrl: string, body: unknown, options?: RequestOptions) => Promise<T>;
|
||||
postForm: <T>(upstreamUrl: string, body: FormData, options?: RequestOptions) => Promise<T>;
|
||||
get: <T>(upstreamUrl: string, options?: RequestOptions) => Promise<T>;
|
||||
getBlob: (upstreamUrl: string, options?: RequestOptions) => Promise<Blob>;
|
||||
getExternalBlob: (url: string, headers: Record<string, string>, options?: RequestOptions) => Promise<Blob>;
|
||||
};
|
||||
|
||||
/**
|
||||
* 统一视频 Provider 的 HTTP 边界。Provider 只负责协议和 payload,不再重复拼接中转请求。
|
||||
*/
|
||||
export function createVideoTransport(config: ResolvedAiConfig): VideoTransport {
|
||||
const headers = (contentType?: string) => ({
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
...(contentType ? { "Content-Type": contentType } : {}),
|
||||
...(isSystemProxyBaseUrl(config.baseUrl) ? { "X-Canvas-Scene": "video", "X-Idempotency-Key": createClientId() } : {}),
|
||||
});
|
||||
const requestOptions = (options?: RequestOptions) => ({
|
||||
withCredentials: false,
|
||||
signal: options?.signal,
|
||||
});
|
||||
|
||||
return {
|
||||
apiUrl: (path) => buildApiUrl(config.baseUrl, path),
|
||||
post: async <T>(upstreamUrl: string, body: unknown, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl, headers("application/json"));
|
||||
return (await axios.post<T>(request.url, body, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include" })).data;
|
||||
},
|
||||
postForm: async <T>(upstreamUrl: string, body: FormData, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl, headers());
|
||||
return (await axios.post<T>(request.url, body, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include" })).data;
|
||||
},
|
||||
get: async <T>(upstreamUrl: string, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl);
|
||||
return (await axios.get<T>(request.url, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include" })).data;
|
||||
},
|
||||
getBlob: async (upstreamUrl: string, options?: RequestOptions) => {
|
||||
const request = channelRequest(config, upstreamUrl);
|
||||
return (await axios.get<Blob>(request.url, { ...requestOptions(options), headers: request.headers, withCredentials: request.credentials === "include", responseType: "blob" })).data;
|
||||
},
|
||||
getExternalBlob: async (url: string, externalHeaders: Record<string, string>, options?: RequestOptions) => {
|
||||
return (await axios.get<Blob>(url, { headers: externalHeaders, responseType: "blob", signal: options?.signal })).data;
|
||||
},
|
||||
};
|
||||
}
|
||||
67
web/src/services/api/video-validation.ts
Normal file
67
web/src/services/api/video-validation.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { modelCapabilityConfigFor, videoDurationAllowed } from "@/lib/model-capabilities";
|
||||
import { resolveModelRequestConfig } from "@/stores/use-config-store";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
|
||||
import type { ResolvedAiConfig } from "./video-contracts";
|
||||
|
||||
export function assertVideoCapability(
|
||||
profile: NonNullable<ReturnType<typeof modelCapabilityConfigFor>["video"]>,
|
||||
references: ReferenceImage[],
|
||||
videoReferences: ReferenceVideo[],
|
||||
audioReferences: ReferenceAudio[],
|
||||
seconds: string,
|
||||
) {
|
||||
if (references.length > profile.references.maxImages || videoReferences.length > profile.references.maxVideos || audioReferences.length > profile.references.maxAudios) throw new Error("参考素材数量超过当前模型限制");
|
||||
if (references.length < profile.references.minImages) throw new Error(`当前视频模型至少需要 ${profile.references.minImages} 张参考图`);
|
||||
if (!videoDurationAllowed(profile, Number(seconds))) throw new Error("视频时长不在当前模型支持范围内");
|
||||
if (profile.references.maxImageBytes > 0 && references.some((image) => (image.bytes || 0) > profile.references.maxImageBytes)) throw new Error("参考图片文件超过当前模型大小限制");
|
||||
for (const video of videoReferences) {
|
||||
if (profile.references.maxVideoBytes > 0 && (video.bytes || 0) > profile.references.maxVideoBytes) throw new Error("参考视频文件超过当前模型大小限制");
|
||||
if (profile.references.maxVideoDurationSeconds > 0 && (video.durationMs || 0) > profile.references.maxVideoDurationSeconds * 1000) throw new Error("参考视频时长超过当前模型限制");
|
||||
}
|
||||
for (const audio of audioReferences) {
|
||||
if (profile.references.maxAudioBytes > 0 && (audio.bytes || 0) > profile.references.maxAudioBytes) throw new Error("参考音频文件超过当前模型大小限制");
|
||||
if (profile.references.maxAudioDurationSeconds > 0 && (audio.durationMs || 0) > profile.references.maxAudioDurationSeconds * 1000) throw new Error("参考音频时长超过当前模型限制");
|
||||
}
|
||||
}
|
||||
|
||||
export function assertVideoConfig(config: ResolvedAiConfig, model: string) {
|
||||
if (!model) throw new Error("请先配置视频模型");
|
||||
if (!config.baseUrl.trim()) throw new Error("请先配置 Base URL");
|
||||
if (!config.apiKey.trim()) throw new Error("请先配置 API Key");
|
||||
if (config.apiFormat === "gemini" && config.interfaceType !== "gemini-veo") throw new Error("当前 Gemini 文本协议不支持视频生成,请为该模型选择 Gemini Veo 协议");
|
||||
}
|
||||
|
||||
export function normalizeVideoSeconds(value: string) {
|
||||
const seconds = Math.floor(Number(value) || 6);
|
||||
return String(Math.max(1, seconds));
|
||||
}
|
||||
|
||||
export function normalizeVideoSize(value: string) {
|
||||
if (value === "auto") return null;
|
||||
const size = (value || "1280x720").trim().toLowerCase().replace("×", "x");
|
||||
if (/^\d+x\d+$/.test(size)) return size;
|
||||
const ratio = size.match(/^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/);
|
||||
if (!ratio) return "1280x720";
|
||||
const widthRatio = Number(ratio[1]);
|
||||
const heightRatio = Number(ratio[2]);
|
||||
if (!Number.isFinite(widthRatio) || !Number.isFinite(heightRatio) || widthRatio <= 0 || heightRatio <= 0) return "1280x720";
|
||||
const aspect = widthRatio / heightRatio;
|
||||
const width = aspect >= 1 ? 1280 : Math.max(256, Math.round((720 * aspect) / 2) * 2);
|
||||
const height = aspect >= 1 ? Math.max(256, Math.round((1280 / aspect) / 2) * 2) : 720;
|
||||
return `${width}x${height}`;
|
||||
}
|
||||
|
||||
export function normalizeVideoResolution(value: string) {
|
||||
if (value === "low") return "480p";
|
||||
if (value === "auto" || value === "high" || value === "medium") return "720p";
|
||||
if (value.toLowerCase() === "2k") return "1440p";
|
||||
if (value.toLowerCase() === "4k") return "2160p";
|
||||
const resolution = value.replace(/p$/i, "") || "720";
|
||||
return `${resolution}p`;
|
||||
}
|
||||
|
||||
export function isPublicMediaUrl(value: string) {
|
||||
return /^https?:\/\//i.test(value || "");
|
||||
}
|
||||
@@ -1,49 +1,22 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { createClientId } from "@/lib/client-id";
|
||||
import { dataUrlToFile } from "@/lib/image-utils";
|
||||
import { getMediaBlob, uploadMediaFile, type UploadedFile } from "@/services/file-storage";
|
||||
import { getResourceOSSUrl } from "@/services/api/resources";
|
||||
import { channelRequest } from "@/services/api/custom-channel-relay";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import { modelCapabilityConfigFor, videoDurationAllowed, videoResolutionRequest } from "@/lib/model-capabilities";
|
||||
import { boolConfig, buildSeedancePromptText, isArkPlanBaseUrl, isSeedanceVideoConfig, normalizeSeedanceDuration, normalizeSeedanceRatio, normalizeSeedanceResolution, seedanceVideoReferenceError, SEEDANCE_REFERENCE_LIMITS } from "@/lib/seedance-video";
|
||||
import { buildApiUrl, isSystemProxyBaseUrl, modelOptionName, resolveModelRequestConfig, type AiConfig } from "@/stores/use-config-store";
|
||||
import { modelCapabilityConfigFor } from "@/lib/model-capabilities";
|
||||
import { uploadMediaFile, type UploadedFile } from "@/services/file-storage";
|
||||
import { resolveModelRequestConfig, type AiConfig } from "@/stores/use-config-store";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
|
||||
type VideoResponse = { id?: string; request_id?: string; task_id?: string; status?: string; error?: { message?: string }; video?: { url?: string }; video_url?: string; result_url?: string };
|
||||
type MiniMaxVideoTask = { id?: string; status?: string; content?: { url?: string }; error?: { code?: string | number; message?: string } };
|
||||
type MiniMaxVideoCreateResponse = { task_id?: string; request_id?: string; data?: { task_id?: string; id?: string } };
|
||||
type ApiVideoResponse = VideoResponse | { code?: number; data?: VideoResponse | null; msg?: string };
|
||||
type ResolvedAiConfig = ReturnType<typeof resolveModelRequestConfig>;
|
||||
type SeedanceTask = {
|
||||
id: string;
|
||||
task_id?: string;
|
||||
status?: "queued" | "running" | "succeeded" | "completed" | "failed" | "cancelled" | "expired";
|
||||
error?: { code?: string; message?: string } | null;
|
||||
error_code?: string | null;
|
||||
video_url?: string | null;
|
||||
content?: { video_url?: string; last_frame_url?: string } | null;
|
||||
};
|
||||
type ApiEnvelope<T> = T | { code?: number; data?: T | null; msg?: string };
|
||||
type RequestOptions = { signal?: AbortSignal };
|
||||
import { assertVideoCapability, assertVideoConfig } from "./video-validation";
|
||||
import type { RequestOptions, VideoGenerationResult, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
import { videoResponseTools } from "./video-response";
|
||||
import type { VideoProviderDeps } from "./video-provider-deps";
|
||||
import { createGeminiVeoTask, pollGeminiVeoTask } from "./video-provider-gemini";
|
||||
import { createMiniMaxVideoTask, pollMiniMaxVideoTask } from "./video-provider-minimax";
|
||||
import { createVideoGenerationsTask, pollVideoGenerationsTask } from "./video-provider-newapi";
|
||||
import { createNovitaVideoTask, pollNovitaVideoTask } from "./video-provider-novita";
|
||||
import { createOpenAIVideoTask, pollOpenAIVideoTask } from "./video-provider-openai";
|
||||
import { createSeedanceTask, isSeedanceConfig, pollSeedanceTask } from "./video-provider-seedance";
|
||||
import { createVideoTransport } from "./video-transport";
|
||||
|
||||
export type VideoGenerationResult = { blob?: Blob; url?: string; mimeType?: string };
|
||||
export type VideoGenerationTask = { id: string; provider: "openai" | "seedance" | "video-generations" | "gemini-veo" | "novita" | "minimax"; model: string };
|
||||
export type VideoGenerationTaskState = { status: "pending" } | { status: "completed"; result: VideoGenerationResult } | { status: "failed"; error: string };
|
||||
|
||||
function aiApiUrl(config: AiConfig, path: string) {
|
||||
return buildApiUrl(config.baseUrl, path);
|
||||
}
|
||||
|
||||
function aiHeaders(config: AiConfig, contentType?: string) {
|
||||
return {
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
...(contentType ? { "Content-Type": contentType } : {}),
|
||||
...(isSystemProxyBaseUrl(config.baseUrl) ? { "X-Canvas-Scene": "video", "X-Idempotency-Key": createClientId() } : {}),
|
||||
};
|
||||
}
|
||||
export type { VideoGenerationResult, VideoGenerationTask, VideoGenerationTaskState } from "./video-contracts";
|
||||
|
||||
export async function requestVideoGeneration(config: AiConfig, prompt: string, references: ReferenceImage[] = [], videoReferences: ReferenceVideo[] = [], audioReferences: ReferenceAudio[] = [], options?: RequestOptions): Promise<VideoGenerationResult> {
|
||||
const task = await createVideoGenerationTask(config, prompt, references, videoReferences, audioReferences, options);
|
||||
@@ -54,7 +27,7 @@ export async function requestVideoGeneration(config: AiConfig, prompt: string, r
|
||||
if (state.status === "completed") return state.result;
|
||||
if (state.status === "failed") throw new Error(state.error);
|
||||
if (attempt === 119) throw new Error(`${task.provider === "seedance" ? "Seedance " : ""}视频生成超时,请稍后重试`);
|
||||
await delay(delayMs, options?.signal);
|
||||
await videoResponseTools.delay(delayMs, options?.signal);
|
||||
}
|
||||
throw new Error("视频生成超时,请稍后重试");
|
||||
}
|
||||
@@ -64,367 +37,26 @@ export async function createVideoGenerationTask(config: AiConfig, prompt: string
|
||||
const requestConfig = resolveModelRequestConfig(config, selectedModel);
|
||||
assertVideoConfig(requestConfig, requestConfig.model);
|
||||
assertVideoCapability(modelCapabilityConfigFor(config, selectedModel).video!, references, videoReferences, audioReferences, config.videoSeconds);
|
||||
if (requestConfig.interfaceType === "newapi-channel-2") {
|
||||
return createVideoGenerationsTask(requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
}
|
||||
if (requestConfig.interfaceType === "gemini-veo") {
|
||||
return createGeminiVeoTask(requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
}
|
||||
if (requestConfig.interfaceType === "novita-video") {
|
||||
return createNovitaVideoTask(requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
}
|
||||
if (requestConfig.interfaceType === "minimax-video") {
|
||||
return createMiniMaxVideoTask(requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
}
|
||||
if (requestConfig.interfaceType === "volcengine-ark-video") {
|
||||
return createSeedanceTask(requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
}
|
||||
if (isSeedanceVideoConfig(requestConfig)) {
|
||||
return createSeedanceTask(requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
}
|
||||
if (videoReferences.length || audioReferences.length) {
|
||||
throw new Error("当前视频接口不支持参考视频或参考音频,请切换到 Seedance 2.0 / 火山 Agent Plan 模型,或移除参考素材");
|
||||
}
|
||||
return createOpenAIVideoTask(requestConfig, selectedModel, prompt, references, options);
|
||||
}
|
||||
|
||||
function assertVideoCapability(profile: NonNullable<ReturnType<typeof modelCapabilityConfigFor>["video"]>, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], seconds: string) {
|
||||
if (references.length > profile.references.maxImages || videoReferences.length > profile.references.maxVideos || audioReferences.length > profile.references.maxAudios) throw new Error("参考素材数量超过当前模型限制");
|
||||
if (references.length < profile.references.minImages) throw new Error(`当前视频模型至少需要 ${profile.references.minImages} 张参考图`);
|
||||
if (!videoDurationAllowed(profile, Number(seconds))) throw new Error("视频时长不在当前模型支持范围内");
|
||||
if (profile.references.maxImageBytes > 0 && references.some((image) => (image.bytes || 0) > profile.references.maxImageBytes)) throw new Error("参考图片文件超过当前模型大小限制");
|
||||
for (const video of videoReferences) {
|
||||
if (profile.references.maxVideoBytes > 0 && (video.bytes || 0) > profile.references.maxVideoBytes) throw new Error("参考视频文件超过当前模型大小限制");
|
||||
if (profile.references.maxVideoDurationSeconds > 0 && (video.durationMs || 0) > profile.references.maxVideoDurationSeconds * 1000) throw new Error("参考视频时长超过当前模型限制");
|
||||
}
|
||||
for (const audio of audioReferences) {
|
||||
if (profile.references.maxAudioBytes > 0 && (audio.bytes || 0) > profile.references.maxAudioBytes) throw new Error("参考音频文件超过当前模型大小限制");
|
||||
if (profile.references.maxAudioDurationSeconds > 0 && (audio.durationMs || 0) > profile.references.maxAudioDurationSeconds * 1000) throw new Error("参考音频时长超过当前模型限制");
|
||||
}
|
||||
const deps: VideoProviderDeps = { transport: createVideoTransport(requestConfig), response: videoResponseTools };
|
||||
if (requestConfig.interfaceType === "newapi-channel-2") return createVideoGenerationsTask(deps, requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
if (requestConfig.interfaceType === "gemini-veo") return createGeminiVeoTask(deps, requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
if (requestConfig.interfaceType === "novita-video") return createNovitaVideoTask(deps, requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
if (requestConfig.interfaceType === "minimax-video") return createMiniMaxVideoTask(deps, requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
if (isSeedanceConfig(requestConfig)) return createSeedanceTask(deps, requestConfig, selectedModel, prompt, references, videoReferences, audioReferences, options);
|
||||
if (videoReferences.length || audioReferences.length) throw new Error("当前视频接口不支持参考视频或参考音频,请切换到 Seedance 2.0 / 火山 Agent Plan 模型,或移除参考素材");
|
||||
return createOpenAIVideoTask(deps, requestConfig, selectedModel, prompt, references, options);
|
||||
}
|
||||
|
||||
export async function pollVideoGenerationTask(config: AiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
const requestConfig = resolveModelRequestConfig(config, task.model);
|
||||
assertVideoConfig(requestConfig, requestConfig.model);
|
||||
if (task.provider === "seedance") return pollSeedanceTask(requestConfig, task, options);
|
||||
if (task.provider === "video-generations") return pollVideoGenerationsTask(requestConfig, task, options);
|
||||
if (task.provider === "gemini-veo") return pollGeminiVeoTask(requestConfig, task, options);
|
||||
if (task.provider === "novita") return pollNovitaVideoTask(requestConfig, task, options);
|
||||
if (task.provider === "minimax") return pollMiniMaxVideoTask(requestConfig, task, options);
|
||||
return pollOpenAIVideoTask(requestConfig, task, options);
|
||||
}
|
||||
|
||||
async function createVideoGenerationsTask(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
if (references.length > 9 || videoReferences.length > 3 || audioReferences.length > 3) throw new Error("NewAPI Video Generations 最多支持 9 张参考图、3 个参考视频和 3 个参考音频");
|
||||
if (audioReferences.length > 0 && videoReferences.length === 0) throw new Error("NewAPI Video Generations 的参考音频必须同时提供至少 1 个参考视频;纯音频生视频请切换到支持该模式的渠道");
|
||||
const [imageUrls, videoUrls, audioUrls] = await Promise.all([
|
||||
Promise.all(references.map((item) => resolveVideoGenerationsUrl(item.url || item.dataUrl, item.storageKey))),
|
||||
Promise.all(videoReferences.map((item) => resolveVideoGenerationsUrl(item.url, item.storageKey))),
|
||||
Promise.all(audioReferences.map((item) => resolveVideoGenerationsUrl(item.url, item.storageKey))),
|
||||
]);
|
||||
const profile = modelCapabilityConfigFor(config, model).video!;
|
||||
const resolution = newAPIVideoResolutionRequest(profile, config.vquality, modelOptionName(model));
|
||||
const payload = {
|
||||
model: modelOptionName(model),
|
||||
prompt: prompt.trim(),
|
||||
seconds: normalizeVideoSeconds(config.videoSeconds),
|
||||
aspect_ratio: normalizeVideoSize(config.size) || "16:9",
|
||||
...(resolution ? { resolution } : {}),
|
||||
...(profile.generateAudio.supported ? { generate_audio: boolConfig(config.videoGenerateAudio, profile.generateAudio.default) } : {}),
|
||||
...(imageUrls.length ? { image_urls: imageUrls } : {}),
|
||||
...(videoUrls.length ? { video_urls: videoUrls } : {}),
|
||||
...(audioUrls.length ? { audio_urls: audioUrls } : {}),
|
||||
};
|
||||
try {
|
||||
const created = unwrapVideoResponse(await channelPost<ApiVideoResponse>(config, aiApiUrl(config, "/video/generations"), payload, options));
|
||||
const id = videoTaskId(created);
|
||||
if (!id) throw new Error("NewAPI Video Generations 没有返回任务 ID");
|
||||
return { id, provider: "video-generations", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "NewAPI Video Generations 任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function newAPIVideoResolutionRequest(profile: NonNullable<ReturnType<typeof modelCapabilityConfigFor>["video"]>, value: string, model: string) {
|
||||
if (model.trim().toLowerCase() === "grok-video-1.5-1080p") return "1080p";
|
||||
return videoResolutionRequest(profile, value);
|
||||
}
|
||||
|
||||
async function pollVideoGenerationsTask(config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const raw = await channelGet<ApiEnvelope<Record<string, unknown>>>(config, aiApiUrl(config, `/video/generations/${encodeURIComponent(task.id)}`), options);
|
||||
const state = unwrapEnvelopeRecord(raw);
|
||||
const status = String(state.status || "").toUpperCase();
|
||||
if (status === "SUCCESS" || status === "SUCCEEDED" || status === "COMPLETED") {
|
||||
const url = String(state.result_url || state.video_url || state.url || "");
|
||||
if (!url) return { status: "failed", error: "视频任务已完成但没有返回视频地址" };
|
||||
return { status: "completed", result: await videoResultFromUrl(url, options) };
|
||||
}
|
||||
if (status === "FAILURE" || status === "FAILED" || status === "CANCELLED") return { status: "failed", error: String(state.fail_reason || state.error || "视频生成失败") };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "NewAPI Video Generations 任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveVideoGenerationsUrl(value: string | undefined, storageKey?: string) {
|
||||
if (storageKey?.startsWith("resource:")) return getResourceOSSUrl(storageKey);
|
||||
if (isPublicMediaUrl(value || "")) return String(value);
|
||||
throw new Error("NewAPI Video Generations 的参考素材需要公网 URL;请先把素材保存到对象存储");
|
||||
}
|
||||
|
||||
type GeminiVeoOperation = {
|
||||
name?: string;
|
||||
done?: boolean;
|
||||
error?: { message?: string };
|
||||
response?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
async function createGeminiVeoTask(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
if (references.length > 1 || videoReferences.length || audioReferences.length) throw new Error("Gemini Veo 当前只支持 1 张起始图,不支持参考视频或音频");
|
||||
const instance: Record<string, unknown> = { prompt: prompt.trim() };
|
||||
if (references[0]) {
|
||||
const dataUrl = await imageToDataUrl(references[0]);
|
||||
const matched = /^data:([^;,]+);base64,(.+)$/s.exec(dataUrl);
|
||||
if (!matched) throw new Error("Gemini Veo 起始图读取失败");
|
||||
instance.image = { bytesBase64Encoded: matched[2], mimeType: matched[1] };
|
||||
}
|
||||
const payload = {
|
||||
instances: [instance],
|
||||
parameters: {
|
||||
aspectRatio: normalizeVideoSize(config.size) || "16:9",
|
||||
durationSeconds: Number.parseInt(normalizeVideoSeconds(config.videoSeconds), 10) || 6,
|
||||
resolution: normalizeVideoResolution(config.vquality),
|
||||
sampleCount: 1,
|
||||
},
|
||||
};
|
||||
try {
|
||||
const response = await channelPost<GeminiVeoOperation>(config, geminiVeoCreateUrl(config, modelOptionName(model)), payload, options);
|
||||
if (!response.name) throw new Error("Gemini Veo 没有返回 operation name");
|
||||
return { id: response.name, provider: "gemini-veo", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "Gemini Veo 任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function pollGeminiVeoTask(config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const operation = await channelGet<GeminiVeoOperation>(config, geminiVeoOperationUrl(config, task.id), options);
|
||||
if (operation.error?.message) return { status: "failed", error: operation.error.message };
|
||||
if (!operation.done) return { status: "pending" };
|
||||
const url = findGeminiVideoURL(operation.response);
|
||||
if (!url) return { status: "failed", error: "Gemini Veo 任务已完成但没有返回视频地址" };
|
||||
const blob = (await axios.get<Blob>(url, { headers: geminiVeoHeaders(config), responseType: "blob", signal: options?.signal })).data;
|
||||
await assertVideoBlob(blob);
|
||||
return { status: "completed", result: { blob } };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "Gemini Veo 任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function geminiVeoCreateUrl(config: ResolvedAiConfig, model: string) {
|
||||
return `${geminiVeoBaseUrl(config)}/models/${encodeURIComponent(model)}:predictLongRunning`;
|
||||
}
|
||||
|
||||
function geminiVeoOperationUrl(config: ResolvedAiConfig, operationName: string) {
|
||||
return `${geminiVeoBaseUrl(config)}/${operationName.replace(/^\/+/, "")}`;
|
||||
}
|
||||
|
||||
function geminiVeoBaseUrl(config: ResolvedAiConfig) {
|
||||
const base = config.baseUrl.replace(/\/+$/, "");
|
||||
return /\/v1beta$/i.test(base) ? base : `${base}/v1beta`;
|
||||
}
|
||||
|
||||
function geminiVeoHeaders(config: ResolvedAiConfig, contentType?: string) {
|
||||
return { "x-goog-api-key": config.apiKey, ...(contentType ? { "Content-Type": contentType } : {}) };
|
||||
}
|
||||
|
||||
type NovitaVideoResult = { task?: { status?: string; reason?: string }; videos?: Array<{ video_url?: string }> };
|
||||
|
||||
async function createNovitaVideoTask(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
if (references.length > 1 || videoReferences.length || audioReferences.length) throw new Error("Novita 视频当前只支持 1 张起始图,不支持参考视频或音频");
|
||||
const payload: Record<string, unknown> = {
|
||||
model: modelOptionName(model),
|
||||
prompt: prompt.trim(),
|
||||
duration: normalizeNovitaVideoDuration(config.videoSeconds),
|
||||
};
|
||||
if (references[0]) {
|
||||
payload.image = isPublicMediaUrl(references[0].url || "") ? references[0].url : await imageToDataUrl(references[0]);
|
||||
} else {
|
||||
payload.aspect_ratio = normalizeNovitaVideoRatio(config.size);
|
||||
}
|
||||
try {
|
||||
const created = await channelPost<{ task_id?: string }>(config, novitaVideoUrl(config, "/video/create"), payload, options);
|
||||
if (!created.task_id) throw new Error("Novita 视频接口没有返回任务 ID");
|
||||
return { id: created.task_id, provider: "novita", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "Novita 视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function pollNovitaVideoTask(config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const result = await channelGet<NovitaVideoResult>(config, novitaVideoUrl(config, `/async/task-result?task_id=${encodeURIComponent(task.id)}`), options);
|
||||
const status = result.task?.status || "";
|
||||
if (status === "TASK_STATUS_SUCCEED") {
|
||||
const url = result.videos?.[0]?.video_url || "";
|
||||
if (!url) return { status: "failed", error: "Novita 视频任务已完成但没有返回视频地址" };
|
||||
return { status: "completed", result: await videoResultFromUrl(url, options) };
|
||||
}
|
||||
if (status === "TASK_STATUS_FAILED") return { status: "failed", error: result.task?.reason || "视频生成失败" };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "Novita 视频任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function createMiniMaxVideoTask(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
const imageUrls = await Promise.all(references.slice(0, 9).map((image) => resolveMiniMaxImageUrl(image)));
|
||||
const videoUrls = await Promise.all(videoReferences.slice(0, 3).map((video) => resolveMiniMaxMediaUrl(video, "参考视频")));
|
||||
const audioUrls = await Promise.all(audioReferences.slice(0, 3).map((audio) => resolveMiniMaxMediaUrl(audio, "参考音频")));
|
||||
const content: Array<Record<string, unknown>> = [{ type: "text", text: prompt.trim() }];
|
||||
imageUrls.forEach((url, index) => {
|
||||
// 首尾帧只允许成对出现;三张及以上图片统一作为多模态参考图,避免提交 first/last 与 reference 混合的非法组合。
|
||||
const role = videoUrls.length || audioUrls.length || imageUrls.length > 2 ? "reference_image" : index === 0 ? "first_frame" : "last_frame";
|
||||
content.push({ type: "image_url", image_url: { url }, role });
|
||||
});
|
||||
videoUrls.forEach((url) => content.push({ type: "video_url", video_url: { url }, role: "reference_video" }));
|
||||
audioUrls.forEach((url) => content.push({ type: "audio_url", audio_url: { url }, role: "reference_audio" }));
|
||||
const frameMode = imageUrls.length > 0 &&
|
||||
imageUrls.length <= 2 &&
|
||||
videoUrls.length === 0 &&
|
||||
audioUrls.length === 0;
|
||||
const payload = {
|
||||
model: modelOptionName(model),
|
||||
content,
|
||||
resolution: normalizeMiniMaxResolution(config.vquality),
|
||||
duration: normalizeMiniMaxDuration(config.videoSeconds),
|
||||
ratio: normalizeMiniMaxRatio(config.size, frameMode),
|
||||
aigc_watermark: boolConfig(config.videoWatermark, false),
|
||||
};
|
||||
try {
|
||||
const created = await channelPost<MiniMaxVideoCreateResponse>(config, miniMaxVideoUrl(config, "/video_generation"), payload, options);
|
||||
const id = created.task_id || created.request_id || created.data?.task_id || created.data?.id || "";
|
||||
if (!id) throw new Error("MiniMax 视频接口没有返回任务 ID");
|
||||
return { id, provider: "minimax", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "MiniMax 视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function pollMiniMaxVideoTask(config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const response = await channelGet<{ task?: MiniMaxVideoTask }>(config, miniMaxVideoUrl(config, `/query/video_generation/${encodeURIComponent(task.id)}`), options);
|
||||
const state = response.task || {};
|
||||
const status = String(state.status || "").toLowerCase();
|
||||
if (status === "succeeded" || status === "completed") {
|
||||
const url = state.content?.url || "";
|
||||
if (!url) return { status: "failed", error: "MiniMax 视频任务已完成但没有返回视频地址" };
|
||||
return { status: "completed", result: await videoResultFromUrl(url, options) };
|
||||
}
|
||||
if (status === "failed" || status === "cancelled") {
|
||||
const code = state.error?.code ? `${state.error.code}:` : "";
|
||||
return { status: "failed", error: `${code}${state.error?.message || "MiniMax 视频生成失败"}` };
|
||||
}
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "MiniMax 视频任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function miniMaxVideoUrl(config: ResolvedAiConfig, path: string) {
|
||||
const base = config.baseUrl.replace(/\/+$/, "");
|
||||
if (/\/v2$/i.test(base)) return `${base}${path}`;
|
||||
return `${base}/v2${path}`;
|
||||
}
|
||||
|
||||
function normalizeMiniMaxResolution(value: string) {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return ["2k", "4k", "high", "1080", "1080p", "1440p", "2160p"].includes(normalized) ? "2K" : "768P";
|
||||
}
|
||||
|
||||
function normalizeMiniMaxDuration(value: string) {
|
||||
const seconds = Math.floor(Number(value) || 5);
|
||||
return Math.max(4, Math.min(15, seconds));
|
||||
}
|
||||
|
||||
function normalizeMiniMaxRatio(value: string, frameMode: boolean) {
|
||||
if (frameMode) return "adaptive";
|
||||
return ["adaptive", "21:9", "16:9", "4:3", "1:1", "3:4", "9:16"].includes(value) ? value : "16:9";
|
||||
}
|
||||
|
||||
async function resolveMiniMaxImageUrl(image: ReferenceImage) {
|
||||
const directUrl = image.url || image.dataUrl;
|
||||
if (isPublicMediaUrl(directUrl) || directUrl.startsWith("data:")) return directUrl;
|
||||
const dataUrl = await imageToDataUrl(image);
|
||||
if (!dataUrl) throw new Error("MiniMax 参考图读取失败,请换一张图片或重新上传");
|
||||
return dataUrl;
|
||||
}
|
||||
|
||||
async function resolveMiniMaxMediaUrl(media: ReferenceVideo | ReferenceAudio, label: string) {
|
||||
if (isPublicMediaUrl(media.url) || media.url?.startsWith("data:")) return media.url;
|
||||
let blob: Blob | null = null;
|
||||
if (media.storageKey) blob = await getMediaBlob(media.storageKey);
|
||||
if (!blob && media.url?.startsWith("blob:")) blob = await (await fetch(media.url)).blob();
|
||||
if (!blob) throw new Error(`MiniMax ${label}必须是公网 URL 或本地已保存素材`);
|
||||
return blobToDataUrl(blob);
|
||||
}
|
||||
|
||||
function novitaVideoUrl(config: ResolvedAiConfig, path: string) {
|
||||
return `${config.baseUrl.replace(/\/+$/, "")}${path}`;
|
||||
}
|
||||
|
||||
function normalizeNovitaVideoDuration(value: string) {
|
||||
return normalizeSeedanceDuration(value) >= 8 ? "10" : "5";
|
||||
}
|
||||
|
||||
function normalizeNovitaVideoRatio(value: string) {
|
||||
return value === "16:9" || value === "9:16" || value === "1:1" ? value : "16:9";
|
||||
}
|
||||
|
||||
async function channelPost<T>(config: ResolvedAiConfig, upstreamUrl: string, body: unknown, options?: RequestOptions) {
|
||||
const request = channelRequest(config, upstreamUrl, aiHeaders(config, "application/json"));
|
||||
return (await axios.post<T>(request.url, body, { headers: request.headers, withCredentials: request.credentials === "include", signal: options?.signal })).data;
|
||||
}
|
||||
|
||||
async function channelPostForm<T>(config: ResolvedAiConfig, upstreamUrl: string, body: FormData, options?: RequestOptions) {
|
||||
const request = channelRequest(config, upstreamUrl, aiHeaders(config));
|
||||
return (await axios.post<T>(request.url, body, { headers: request.headers, withCredentials: request.credentials === "include", signal: options?.signal })).data;
|
||||
}
|
||||
|
||||
async function channelGet<T>(config: ResolvedAiConfig, upstreamUrl: string, options?: RequestOptions) {
|
||||
const request = channelRequest(config, upstreamUrl);
|
||||
return (await axios.get<T>(request.url, { headers: request.headers, withCredentials: request.credentials === "include", signal: options?.signal })).data;
|
||||
}
|
||||
|
||||
async function channelGetBlob(config: ResolvedAiConfig, upstreamUrl: string, options?: RequestOptions) {
|
||||
const request = channelRequest(config, upstreamUrl);
|
||||
return (await axios.get<Blob>(request.url, { headers: request.headers, withCredentials: request.credentials === "include", responseType: "blob", signal: options?.signal })).data;
|
||||
}
|
||||
|
||||
function findGeminiVideoURL(value: unknown): string {
|
||||
if (!value || typeof value !== "object") return "";
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
const found = findGeminiVideoURL(item);
|
||||
if (found) return found;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
const record = value as Record<string, unknown>;
|
||||
for (const key of ["uri", "url", "videoUri", "video_url"]) {
|
||||
if (typeof record[key] === "string" && /^https?:\/\//i.test(record[key])) return record[key];
|
||||
}
|
||||
for (const child of Object.values(record)) {
|
||||
const found = findGeminiVideoURL(child);
|
||||
if (found) return found;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function unwrapEnvelopeRecord(value: ApiEnvelope<Record<string, unknown>>): Record<string, unknown> {
|
||||
if (value && typeof value === "object" && "data" in value && value.data && typeof value.data === "object") return value.data as Record<string, unknown>;
|
||||
return value as Record<string, unknown>;
|
||||
const deps: VideoProviderDeps = { transport: createVideoTransport(requestConfig), response: videoResponseTools };
|
||||
if (task.provider === "video-generations") return pollVideoGenerationsTask(deps, task, options);
|
||||
if (task.provider === "gemini-veo") return pollGeminiVeoTask(deps, requestConfig, task, options);
|
||||
if (task.provider === "novita") return pollNovitaVideoTask(deps, requestConfig, task, options);
|
||||
if (task.provider === "minimax") return pollMiniMaxVideoTask(deps, requestConfig, task, options);
|
||||
if (task.provider === "seedance") return pollSeedanceTask(deps, requestConfig, task, options);
|
||||
return pollOpenAIVideoTask(deps, task, options);
|
||||
}
|
||||
|
||||
export async function storeGeneratedVideo(result: VideoGenerationResult): Promise<UploadedFile> {
|
||||
@@ -433,385 +65,4 @@ export async function storeGeneratedVideo(result: VideoGenerationResult): Promis
|
||||
throw new Error("视频接口没有返回可播放的视频");
|
||||
}
|
||||
|
||||
async function createOpenAIVideoTask(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
const modelName = modelOptionName(model);
|
||||
if (config.interfaceType === "xai-video" || modelName.toLowerCase().includes("grok")) {
|
||||
const images = await Promise.all(references.slice(0, 7).map((image) => imageToDataUrl(image)));
|
||||
const seconds = normalizeVideoSeconds(config.videoSeconds);
|
||||
const payload = {
|
||||
model: modelName,
|
||||
prompt,
|
||||
duration: Number.parseInt(seconds, 10) || 6,
|
||||
seconds,
|
||||
...(normalizeVideoSize(config.size) ? { size: normalizeVideoSize(config.size) } : {}),
|
||||
...(images.length ? { image: images[0], images } : {}),
|
||||
};
|
||||
try {
|
||||
const createPath = config.interfaceType === "xai-video" ? "/videos/generations" : "/videos";
|
||||
const created = unwrapVideoResponse(await channelPost<ApiVideoResponse>(config, aiApiUrl(config, createPath), payload, options));
|
||||
const id = videoTaskId(created);
|
||||
if (!id) throw new Error("视频接口没有返回任务 ID");
|
||||
return { id, provider: "openai", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
const body = new FormData();
|
||||
body.append("model", modelName);
|
||||
body.append("prompt", prompt);
|
||||
body.append("seconds", normalizeVideoSeconds(config.videoSeconds));
|
||||
if (normalizeVideoSize(config.size)) body.append("size", normalizeVideoSize(config.size)!);
|
||||
const resolution = videoResolutionRequest(modelCapabilityConfigFor(config, model).video!, config.vquality);
|
||||
if (resolution) body.append("resolution_name", resolution);
|
||||
body.append("preset", "normal");
|
||||
const files = await Promise.all(references.slice(0, 7).map(async (image) => dataUrlToFile({ ...image, dataUrl: await imageToDataUrl(image) })));
|
||||
files.forEach((file) => body.append("input_reference[]", file));
|
||||
try {
|
||||
const created = unwrapVideoResponse(await channelPostForm<ApiVideoResponse>(config, aiApiUrl(config, "/videos"), body, options));
|
||||
if (!created.id) throw new Error("视频接口没有返回任务 ID");
|
||||
return { id: created.id, provider: "openai", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "视频任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function pollOpenAIVideoTask(config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const video = unwrapVideoResponse(await channelGet<ApiVideoResponse>(config, aiApiUrl(config, `/videos/${task.id}`), options));
|
||||
if (video.status === "completed" || video.status === "succeeded" || video.status === "success" || video.status === "done") {
|
||||
const resultUrl = video.video?.url || video.video_url || video.result_url;
|
||||
if (resultUrl) return { status: "completed", result: await videoResultFromUrl(resultUrl, options) };
|
||||
const content = await channelGetBlob(config, aiApiUrl(config, `/videos/${task.id}/content`), options);
|
||||
await assertVideoBlob(content);
|
||||
return { status: "completed", result: { blob: content } };
|
||||
}
|
||||
if (video.status === "failed" || video.status === "cancelled") return { status: "failed", error: video.error?.message || "视频生成失败" };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "视频任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function createSeedanceTask(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
assertSeedanceVideoReferences(videoReferences);
|
||||
assertSeedanceAudioReferences(audioReferences);
|
||||
const isVolcengineArk = config.interfaceType === "volcengine-ark-video";
|
||||
const payload =
|
||||
isVolcengineArk || isArkPlanBaseUrl(config.baseUrl)
|
||||
? await buildSeedanceAgentPlanPayload(config, model, prompt, references, videoReferences, audioReferences)
|
||||
: await buildSeedanceVideosPayload(config, model, prompt, references, videoReferences, audioReferences);
|
||||
|
||||
try {
|
||||
const raw = await channelPost<ApiEnvelope<SeedanceTask>>(config, seedanceApiUrl(config), payload, options);
|
||||
const created = unwrapSeedanceTask(raw);
|
||||
const id = created.id || created.task_id;
|
||||
if (!id) throw new Error("Seedance 接口没有返回任务 ID");
|
||||
return { id, provider: "seedance", model };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "Seedance 任务创建失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function pollSeedanceTask(config: ResolvedAiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const raw = await channelGet<ApiEnvelope<SeedanceTask>>(config, seedanceApiUrl(config, task.id), options);
|
||||
const state = unwrapSeedanceTask(raw);
|
||||
if (state.status === "succeeded" || state.status === "completed") {
|
||||
const url = state.video_url || state.content?.video_url;
|
||||
if (url) return { status: "completed", result: await videoResultFromUrl(url, options) };
|
||||
if (isArkPlanBaseUrl(config.baseUrl)) return { status: "failed", error: "Seedance 任务成功但没有返回视频 URL" };
|
||||
const content = await channelGetBlob(config, aiApiUrl(config, `/videos/${task.id}/content`), options);
|
||||
await assertVideoBlob(content);
|
||||
return { status: "completed", result: { blob: content } };
|
||||
}
|
||||
if (state.status === "failed" || state.status === "cancelled" || state.status === "expired") return { status: "failed", error: seedanceErrorMessage(state) || `Seedance 视频生成${state.status === "expired" ? "超时" : "失败"}` };
|
||||
return { status: "pending" };
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "Seedance 任务查询失败"));
|
||||
}
|
||||
}
|
||||
|
||||
function assertSeedanceVideoReferences(videoReferences: ReferenceVideo[]) {
|
||||
const error = seedanceVideoReferenceError(videoReferences);
|
||||
if (error) throw new Error(error);
|
||||
let total = 0;
|
||||
for (const video of videoReferences) {
|
||||
if (!video.durationMs) continue;
|
||||
if (video.durationMs < 2000 || video.durationMs > 15000) throw new Error("Seedance 参考视频单个时长需要在 2-15 秒之间");
|
||||
total += video.durationMs;
|
||||
}
|
||||
if (total > 15000) throw new Error("Seedance 参考视频总时长不能超过 15 秒");
|
||||
}
|
||||
|
||||
function assertSeedanceAudioReferences(audioReferences: ReferenceAudio[]) {
|
||||
let total = 0;
|
||||
for (const audio of audioReferences) {
|
||||
if (!audio.durationMs) continue;
|
||||
if (audio.durationMs < 2000 || audio.durationMs > 15000) throw new Error("Seedance 参考音频单个时长需要在 2-15 秒之间");
|
||||
total += audio.durationMs;
|
||||
}
|
||||
if (total > 15000) throw new Error("Seedance 参考音频总时长不能超过 15 秒");
|
||||
}
|
||||
|
||||
function seedanceApiUrl(config: ResolvedAiConfig, taskId?: string) {
|
||||
if (config.interfaceType === "volcengine-ark-video" || isArkPlanBaseUrl(config.baseUrl)) return buildApiUrl(config.baseUrl, `/contents/generations/tasks${taskId ? `/${encodeURIComponent(taskId)}` : ""}`);
|
||||
return buildApiUrl(config.baseUrl, `/videos${taskId ? `/${encodeURIComponent(taskId)}` : ""}`);
|
||||
}
|
||||
|
||||
async function buildSeedanceAgentPlanPayload(config: ResolvedAiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[]) {
|
||||
if (config.interfaceType !== "volcengine-ark-video" && audioReferences.length && !references.length && !videoReferences.length) {
|
||||
throw new Error("Seedance 参考音频不能单独使用,请同时添加参考图或参考视频");
|
||||
}
|
||||
const content = config.interfaceType === "volcengine-ark-video" ? await buildVolcengineArkContent(prompt, references, videoReferences, audioReferences) : await buildSeedanceContent(config, prompt, references, videoReferences, audioReferences);
|
||||
if (!content.length) throw new Error("请输入视频提示词,或连接参考图片/视频/音频");
|
||||
const profile = modelCapabilityConfigFor(config, model).video!;
|
||||
return {
|
||||
model: modelOptionName(model),
|
||||
content,
|
||||
ratio: normalizeSeedanceRatio(config.size),
|
||||
resolution: normalizeSeedanceResolution(config.vquality, modelOptionName(model)),
|
||||
duration: normalizeSeedanceDuration(config.videoSeconds),
|
||||
...(profile.generateAudio.supported ? { generate_audio: boolConfig(config.videoGenerateAudio, profile.generateAudio.default) } : {}),
|
||||
...(profile.watermark.supported ? { watermark: boolConfig(config.videoWatermark, profile.watermark.default) } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function buildVolcengineArkContent(prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[]) {
|
||||
const content: Array<Record<string, unknown>> = [];
|
||||
if (prompt.trim()) content.push({ type: "text", text: prompt.trim() });
|
||||
for (const image of references.slice(0, SEEDANCE_REFERENCE_LIMITS.images)) {
|
||||
content.push({ type: "image_url", image_url: { url: await resolveVolcengineArkReferenceUrl(image.url || image.dataUrl, image.storageKey) }, role: "reference_image" });
|
||||
}
|
||||
for (const video of videoReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.videos)) {
|
||||
content.push({ type: "video_url", video_url: { url: await resolveVolcengineArkReferenceUrl(video.url, video.storageKey) }, role: "reference_video" });
|
||||
}
|
||||
for (const audio of audioReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.audios)) {
|
||||
content.push({ type: "audio_url", audio_url: { url: await resolveVolcengineArkReferenceUrl(audio.url, audio.storageKey) }, role: "reference_audio" });
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
async function resolveVolcengineArkReferenceUrl(value: string | undefined, storageKey?: string) {
|
||||
if (storageKey?.startsWith("resource:")) return getResourceOSSUrl(storageKey);
|
||||
if (isPublicMediaUrl(value || "") || String(value || "").startsWith("asset://")) return String(value);
|
||||
throw new Error("火山方舟视频参考素材需要公网 URL 或 asset:// 素材 ID;请先将本地素材保存到对象存储");
|
||||
}
|
||||
|
||||
async function buildSeedanceVideosPayload(config: AiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[]) {
|
||||
if ((videoReferences.length || audioReferences.length) && !references.length) {
|
||||
throw new Error("Seedance 参考视频或参考音频需要同时连接至少 1 张主参考图");
|
||||
}
|
||||
const imageUrls = await Promise.all(references.slice(0, SEEDANCE_REFERENCE_LIMITS.images).map(resolveSeedanceVideosImageUrl));
|
||||
const videoUrls = await Promise.all(videoReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.videos).map(resolveSeedanceVideosMediaUrl));
|
||||
const audioUrls = await Promise.all(audioReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.audios).map(resolveSeedanceVideosMediaUrl));
|
||||
const ratio = normalizeSeedanceRatio(config.size);
|
||||
const duration = normalizeSeedanceDuration(config.videoSeconds);
|
||||
const profile = modelCapabilityConfigFor(config, model).video!;
|
||||
return {
|
||||
model: modelOptionName(model),
|
||||
prompt: buildSeedanceVideosPromptText(prompt, imageUrls.length, videoUrls.length, audioUrls.length),
|
||||
aspect_ratio: ratio === "adaptive" ? "16:9" : ratio,
|
||||
duration,
|
||||
...(profile.generateAudio.supported ? { generate_audio: boolConfig(config.videoGenerateAudio, profile.generateAudio.default) } : {}),
|
||||
...(imageUrls[0] ? { image_url: imageUrls[0] } : {}),
|
||||
...(imageUrls.length > 1 ? { reference_image_urls: imageUrls.slice(1) } : {}),
|
||||
...(videoUrls.length ? { reference_videos: videoUrls } : {}),
|
||||
...(audioUrls.length ? { reference_audios: audioUrls } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function buildSeedanceContent(config: AiConfig, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[]) {
|
||||
const content: Array<Record<string, unknown>> = [];
|
||||
const text = buildSeedancePromptText(prompt, references, videoReferences, audioReferences);
|
||||
if (text) content.push({ type: "text", text });
|
||||
for (const image of references.slice(0, SEEDANCE_REFERENCE_LIMITS.images)) {
|
||||
content.push({ type: "image_url", image_url: { url: await resolveSeedanceImageUrl(config, image) }, role: "reference_image" });
|
||||
}
|
||||
for (const video of videoReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.videos)) {
|
||||
content.push({ type: "video_url", video_url: { url: await resolveSeedanceVideoUrl(video) }, role: "reference_video" });
|
||||
}
|
||||
for (const audio of audioReferences.slice(0, SEEDANCE_REFERENCE_LIMITS.audios)) {
|
||||
content.push({ type: "audio_url", audio_url: { url: await resolveSeedanceAudioUrl(audio) }, role: "reference_audio" });
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
function buildSeedanceVideosPromptText(prompt: string, _imageCount: number, _videoCount: number, _audioCount: number) {
|
||||
return prompt.trim();
|
||||
}
|
||||
|
||||
async function resolveSeedanceImageUrl(config: AiConfig, image: ReferenceImage) {
|
||||
const directUrl = image.url || image.dataUrl;
|
||||
if (isPublicMediaUrl(directUrl) || directUrl.startsWith("asset://")) return directUrl;
|
||||
const dataUrl = await imageToDataUrl(image);
|
||||
if (!dataUrl) throw new Error("参考图读取失败,请换一张图片或重新上传");
|
||||
return dataUrl;
|
||||
}
|
||||
|
||||
async function resolveSeedanceVideosImageUrl(image: ReferenceImage) {
|
||||
const directUrl = image.url || image.dataUrl;
|
||||
if (isPublicMediaUrl(directUrl) || directUrl.startsWith("data:")) return directUrl;
|
||||
const dataUrl = await imageToDataUrl(image);
|
||||
if (!dataUrl) throw new Error("参考图读取失败,请换一张图片或重新上传");
|
||||
return dataUrl;
|
||||
}
|
||||
|
||||
async function resolveSeedanceVideoUrl(video: ReferenceVideo) {
|
||||
if (isPublicMediaUrl(video.url) || video.url.startsWith("asset://")) return video.url;
|
||||
let blob: Blob | null = null;
|
||||
if (video.storageKey) blob = await getMediaBlob(video.storageKey);
|
||||
if (!blob && video.url?.startsWith("blob:")) blob = await (await fetch(video.url)).blob();
|
||||
if (!blob) throw new Error("参考视频必须是公网 URL、素材 ID,或本地已保存的视频");
|
||||
return blobToDataUrl(blob);
|
||||
}
|
||||
|
||||
async function resolveSeedanceAudioUrl(audio: ReferenceAudio) {
|
||||
if (isPublicMediaUrl(audio.url) || audio.url.startsWith("asset://")) return audio.url;
|
||||
let blob: Blob | null = null;
|
||||
if (audio.storageKey) blob = await getMediaBlob(audio.storageKey);
|
||||
if (!blob && audio.url?.startsWith("blob:")) blob = await (await fetch(audio.url)).blob();
|
||||
if (!blob) throw new Error("参考音频必须是公网 URL、素材 ID,或本地已保存的音频");
|
||||
return blobToDataUrl(blob);
|
||||
}
|
||||
|
||||
async function resolveSeedanceVideosMediaUrl(media: ReferenceVideo | ReferenceAudio) {
|
||||
if (isPublicMediaUrl(media.url) || media.url?.startsWith("data:")) return media.url;
|
||||
let blob: Blob | null = null;
|
||||
if (media.storageKey) blob = await getMediaBlob(media.storageKey);
|
||||
if (!blob && media.url?.startsWith("blob:")) blob = await (await fetch(media.url)).blob();
|
||||
if (!blob) throw new Error("Seedance /videos 参考素材必须是公网 URL、data URL,或本地已保存素材");
|
||||
return blobToDataUrl(blob);
|
||||
}
|
||||
|
||||
async function videoResultFromUrl(url: string, options?: RequestOptions): Promise<VideoGenerationResult> {
|
||||
try {
|
||||
const response = await axios.get<Blob>(url, { responseType: "blob", signal: options?.signal });
|
||||
await assertVideoBlob(response.data);
|
||||
return { blob: response.data };
|
||||
} catch (error) {
|
||||
if (axios.isCancel(error) || options?.signal?.aborted) throw error;
|
||||
return { url, mimeType: "video/mp4" };
|
||||
}
|
||||
}
|
||||
|
||||
function seedanceErrorMessage(state: SeedanceTask) {
|
||||
if (state.error?.message && state.error.code) return `${state.error.code}:${state.error.message}`;
|
||||
return state.error?.message || state.error_code || "";
|
||||
}
|
||||
|
||||
function assertVideoConfig(config: ResolvedAiConfig, model: string) {
|
||||
if (!model) throw new Error("请先配置视频模型");
|
||||
if (!config.baseUrl.trim()) throw new Error("请先配置 Base URL");
|
||||
if (!config.apiKey.trim()) throw new Error("请先配置 API Key");
|
||||
if (config.apiFormat === "gemini" && config.interfaceType !== "gemini-veo") throw new Error("当前 Gemini 文本协议不支持视频生成,请为该模型选择 Gemini Veo 协议");
|
||||
}
|
||||
|
||||
function normalizeVideoSeconds(value: string) {
|
||||
const seconds = Math.floor(Number(value) || 6);
|
||||
return String(Math.max(1, seconds));
|
||||
}
|
||||
|
||||
function normalizeVideoSize(value: string) {
|
||||
if (value === "auto") return null;
|
||||
const size = (value || "1280x720").trim().toLowerCase().replace("×", "x");
|
||||
if (/^\d+x\d+$/.test(size)) return size;
|
||||
const ratio = size.match(/^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/);
|
||||
if (!ratio) return "1280x720";
|
||||
const widthRatio = Number(ratio[1]);
|
||||
const heightRatio = Number(ratio[2]);
|
||||
if (!Number.isFinite(widthRatio) || !Number.isFinite(heightRatio) || widthRatio <= 0 || heightRatio <= 0) return "1280x720";
|
||||
const aspect = widthRatio / heightRatio;
|
||||
const width = aspect >= 1 ? 1280 : Math.max(256, Math.round((720 * aspect) / 2) * 2);
|
||||
const height = aspect >= 1 ? Math.max(256, Math.round((1280 / aspect) / 2) * 2) : 720;
|
||||
return `${width}x${height}`;
|
||||
}
|
||||
|
||||
function normalizeVideoResolution(value: string) {
|
||||
if (value === "low") return "480p";
|
||||
if (value === "auto" || value === "high" || value === "medium") return "720p";
|
||||
if (value.toLowerCase() === "2k") return "1440p";
|
||||
if (value.toLowerCase() === "4k") return "2160p";
|
||||
const resolution = value.replace(/p$/i, "") || "720";
|
||||
return `${resolution}p`;
|
||||
}
|
||||
|
||||
function unwrapVideoResponse(payload: ApiVideoResponse) {
|
||||
return unwrapEnvelope(payload, "接口没有返回视频任务");
|
||||
}
|
||||
|
||||
function videoTaskId(payload: VideoResponse) {
|
||||
return payload.id || payload.request_id || payload.task_id || "";
|
||||
}
|
||||
|
||||
function unwrapSeedanceTask(payload: ApiEnvelope<SeedanceTask>) {
|
||||
return unwrapEnvelope(payload, "Seedance 接口没有返回任务");
|
||||
}
|
||||
|
||||
function unwrapEnvelope<T>(payload: ApiEnvelope<T>, emptyMessage: string): T {
|
||||
if (!payload) throw new Error(emptyMessage);
|
||||
if (typeof payload === "object" && "code" in payload && typeof payload.code === "number") {
|
||||
if (payload.code !== 0) throw new Error(payload.msg || "请求失败");
|
||||
if (!payload.data) throw new Error(emptyMessage);
|
||||
return payload.data;
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
function readAxiosError(error: unknown, fallback: string) {
|
||||
if (axios.isCancel(error)) return "请求已取消";
|
||||
if (axios.isAxiosError<{ error?: { message?: string }; msg?: string; code?: number }>(error)) {
|
||||
const responseData = error.response?.data;
|
||||
return responseData?.msg || responseData?.error?.message || statusMessage(error.response?.status, fallback);
|
||||
}
|
||||
if (error instanceof DOMException && error.name === "AbortError") return "请求已取消";
|
||||
return error instanceof Error ? error.message : fallback;
|
||||
}
|
||||
|
||||
function statusMessage(status: number | undefined, fallback: string) {
|
||||
if (status === 401 || status === 403) return "鉴权失败,请检查 API Key、套餐权限或模型权限";
|
||||
if (status === 429) return "请求被限流或额度不足,请稍后重试";
|
||||
return status ? `${fallback}(${status})` : fallback;
|
||||
}
|
||||
|
||||
async function assertVideoBlob(blob: Blob) {
|
||||
if (!blob.type.includes("json")) return;
|
||||
let payload: { code?: number; msg?: string; error?: { message?: string } };
|
||||
try {
|
||||
payload = JSON.parse(await blob.text()) as { code?: number; msg?: string; error?: { message?: string } };
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "视频下载失败");
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
}
|
||||
|
||||
function isPublicMediaUrl(value: string) {
|
||||
return /^https?:\/\//i.test(value || "");
|
||||
}
|
||||
|
||||
function delay(ms: number, signal?: AbortSignal) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
if (signal?.aborted) {
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
return;
|
||||
}
|
||||
const timer = setTimeout(resolve, ms);
|
||||
signal?.addEventListener(
|
||||
"abort",
|
||||
() => {
|
||||
clearTimeout(timer);
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function blobToDataUrl(blob: Blob) {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(String(reader.result || ""));
|
||||
reader.onerror = () => reject(new Error("读取本地素材失败"));
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
export type { VideoProviderDeps } from "./video-provider-deps";
|
||||
|
||||
Reference in New Issue
Block a user