feat(platform): 开源发布 - 初始化 Open AI Canvas
This commit is contained in:
59
web/src/services/api/announcements.ts
Normal file
59
web/src/services/api/announcements.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import axios from "axios";
|
||||
|
||||
import type { BackendEnvelope } from "@/services/api/task-center";
|
||||
|
||||
export type AnnouncementLevel = "info" | "success" | "warning" | "critical";
|
||||
export type AnnouncementStatus = "active" | "closed";
|
||||
|
||||
export type SystemAnnouncement = {
|
||||
id: string;
|
||||
title: string;
|
||||
content: string;
|
||||
level: AnnouncementLevel;
|
||||
status: AnnouncementStatus;
|
||||
createdBy: string;
|
||||
publishedAt: string;
|
||||
closedAt?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type AnnouncementFeed = {
|
||||
announcements: SystemAnnouncement[];
|
||||
unreadCount: number;
|
||||
};
|
||||
|
||||
export type AdminAnnouncementListParams = {
|
||||
keyword?: string;
|
||||
status?: AnnouncementStatus;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
};
|
||||
|
||||
const api = axios.create({ baseURL: import.meta.env.VITE_CANVAS_BACKEND_URL || "/api", withCredentials: true });
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
export function getAnnouncementFeed() {
|
||||
return request<AnnouncementFeed>(api.get("/announcements"));
|
||||
}
|
||||
|
||||
export function markAnnouncementsRead(announcementIds: string[]) {
|
||||
return request<{ unreadCount: number }>(api.post("/announcements/read", { announcementIds }));
|
||||
}
|
||||
|
||||
export function listAdminAnnouncements(params: AdminAnnouncementListParams = {}) {
|
||||
return request<{ announcements: SystemAnnouncement[]; total: number; page: number; limit: number }>(api.get("/admin/announcements", { params }));
|
||||
}
|
||||
|
||||
export function createAdminAnnouncement(input: { title: string; content: string; level: AnnouncementLevel }) {
|
||||
return request<{ announcement: SystemAnnouncement }>(api.post("/admin/announcements", input));
|
||||
}
|
||||
|
||||
export function closeAdminAnnouncement(id: string) {
|
||||
return request<{ announcement: SystemAnnouncement }>(api.post(`/admin/announcements/${encodeURIComponent(id)}/close`));
|
||||
}
|
||||
85
web/src/services/api/audio.ts
Normal file
85
web/src/services/api/audio.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { audioMimeType, normalizeAudioFormatValue, normalizeAudioSpeedValue, normalizeAudioVoiceValue } from "@/lib/audio-generation";
|
||||
import { uploadMediaFile, type UploadedFile } from "@/services/file-storage";
|
||||
import { buildApiUrl, isSystemProxyBaseUrl, resolveModelRequestConfig, type AiConfig } from "@/stores/use-config-store";
|
||||
|
||||
type RequestOptions = { signal?: AbortSignal };
|
||||
|
||||
function aiApiUrl(config: AiConfig, path: string) {
|
||||
return buildApiUrl(config.baseUrl, path);
|
||||
}
|
||||
|
||||
function aiHeaders(config: AiConfig) {
|
||||
return {
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
...(isSystemProxyBaseUrl(config.baseUrl) ? { "X-Canvas-Scene": "audio", "X-Idempotency-Key": crypto.randomUUID() } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export async function requestAudioGeneration(config: AiConfig, prompt: string, options?: RequestOptions): Promise<Blob> {
|
||||
const requestConfig = resolveModelRequestConfig(config, config.model || config.audioModel);
|
||||
const model = requestConfig.model.trim();
|
||||
assertAudioConfig(requestConfig, model);
|
||||
const format = normalizeAudioFormatValue(config.audioFormat);
|
||||
const instructions = config.audioInstructions.trim();
|
||||
|
||||
try {
|
||||
const response = await axios.post<Blob>(
|
||||
aiApiUrl(requestConfig, "/audio/speech"),
|
||||
{
|
||||
model,
|
||||
input: prompt,
|
||||
voice: normalizeAudioVoiceValue(config.audioVoice),
|
||||
response_format: format,
|
||||
speed: Number(normalizeAudioSpeedValue(config.audioSpeed)),
|
||||
...(instructions ? { instructions } : {}),
|
||||
},
|
||||
{ headers: aiHeaders(requestConfig), responseType: "blob", signal: options?.signal },
|
||||
);
|
||||
await assertAudioBlob(response.data);
|
||||
return response.data.type.startsWith("audio/") ? response.data : new Blob([response.data], { type: audioMimeType(format) });
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "音频生成失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function storeGeneratedAudio(blob: Blob, format = "mp3"): Promise<UploadedFile> {
|
||||
const audio = blob.type.startsWith("audio/") ? blob : new Blob([blob], { type: audioMimeType(format) });
|
||||
return uploadMediaFile(audio, "audio");
|
||||
}
|
||||
|
||||
function assertAudioConfig(config: AiConfig, 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") throw new Error("Gemini 调用格式暂不支持音频生成,请使用 OpenAI 格式渠道");
|
||||
}
|
||||
|
||||
async function assertAudioBlob(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 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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
359
web/src/services/api/auth.ts
Normal file
359
web/src/services/api/auth.ts
Normal file
@@ -0,0 +1,359 @@
|
||||
import axios from "axios";
|
||||
|
||||
import type { ModelChannel } from "@/stores/use-config-store";
|
||||
import type { CreditLedgerEntry } from "@/services/api/wallet";
|
||||
|
||||
const api = axios.create({ baseURL: import.meta.env.VITE_CANVAS_BACKEND_URL || "/api", withCredentials: true });
|
||||
|
||||
export type LocalUser = {
|
||||
id: string;
|
||||
username: string;
|
||||
email?: string;
|
||||
displayName: string;
|
||||
avatarUrl?: string;
|
||||
identityProvider?: string;
|
||||
identityId?: string;
|
||||
identityUsername?: string;
|
||||
role: "admin" | "user";
|
||||
status: "active" | "disabled";
|
||||
lastLoginAt?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type AdminUser = LocalUser & {
|
||||
availableMicrocredits: number;
|
||||
reservedMicrocredits: number;
|
||||
};
|
||||
|
||||
export type AuthSessionPayload = {
|
||||
user: LocalUser | null;
|
||||
systemChannels?: ModelChannel[];
|
||||
};
|
||||
|
||||
export type ApiCallLog = {
|
||||
id: string;
|
||||
userId: string;
|
||||
channelId: string;
|
||||
channelName: string;
|
||||
taskId?: string;
|
||||
source: string;
|
||||
capability: "text" | "image" | "video" | "audio" | "";
|
||||
operation?: string;
|
||||
requestKind: "create" | "poll" | "download" | "repair" | "";
|
||||
billable: boolean;
|
||||
apiFormat: string;
|
||||
method: string;
|
||||
path: string;
|
||||
model: string;
|
||||
status: "succeeded" | "failed";
|
||||
statusCode: number;
|
||||
durationMs: number;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
cachedTokens: number;
|
||||
usageAvailable: boolean;
|
||||
mediaCount: number;
|
||||
videoSeconds: number;
|
||||
providerRequestId?: string;
|
||||
estimatedCostMicros: number;
|
||||
costAvailable: boolean;
|
||||
currency?: string;
|
||||
errorCode?: string;
|
||||
error?: string;
|
||||
upstreamUrl: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type AdminAuditEvent = {
|
||||
id: string;
|
||||
actorUserId: string;
|
||||
action: string;
|
||||
targetType: string;
|
||||
targetId: string;
|
||||
summary: string;
|
||||
metadataJson?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type AdminUserDetail = {
|
||||
user: LocalUser;
|
||||
account: { userId: string; availableMicrocredits: number; reservedMicrocredits: number; version: number };
|
||||
counts: { ledgerEntries: number; tasks: number; apiCalls: number; auditEvents: number };
|
||||
};
|
||||
|
||||
export type AdminUserTask = {
|
||||
id: string;
|
||||
type: string;
|
||||
status: "queued" | "running" | "succeeded" | "failed" | "cancelled";
|
||||
stage: string;
|
||||
progress: number;
|
||||
model?: string;
|
||||
providerRequestId?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type AnalyticsFilters = {
|
||||
from?: string;
|
||||
to?: string;
|
||||
userId?: string;
|
||||
model?: string;
|
||||
channelId?: string;
|
||||
capability?: string;
|
||||
};
|
||||
|
||||
export type AdminReferenceData = {
|
||||
users: Array<{ id: string; username: string; displayName: string }>;
|
||||
channels: Array<{ id: string; name: string; models: string[] }>;
|
||||
};
|
||||
|
||||
export type AdminAnalytics = {
|
||||
from: string;
|
||||
to: string;
|
||||
kpi: {
|
||||
activeUsers: number;
|
||||
dau: number;
|
||||
wau: number;
|
||||
mau: number;
|
||||
generationTasks: number;
|
||||
upstreamRequests: number;
|
||||
successRate: number;
|
||||
p95DurationMs: number;
|
||||
currentQueuedTasks: number;
|
||||
estimatedCostMicros: number;
|
||||
costAvailable: boolean;
|
||||
currency?: string;
|
||||
};
|
||||
trend: Array<{ day: string; tasks: number; requests: number; activeUsers: number; requestSuccessRate: number }>;
|
||||
models: Array<{
|
||||
model: string;
|
||||
capability: string;
|
||||
tasks: number;
|
||||
requests: number;
|
||||
uniqueUsers: number;
|
||||
taskSuccessRate: number;
|
||||
requestSuccessRate: number;
|
||||
p50DurationMs: number;
|
||||
p95DurationMs: number;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
cachedTokens: number;
|
||||
usageAvailable: boolean;
|
||||
mediaCount: number;
|
||||
videoSeconds: number;
|
||||
estimatedCostMicros: number;
|
||||
costAvailable: boolean;
|
||||
currency?: string;
|
||||
}>;
|
||||
users: Array<{ userId: string; name: string; activeDays: number; tasks: number; agentMessages: number; canvasDays: number; assets: number; resources: number; commonModel?: string }>;
|
||||
failures: Array<{ type: string; model: string; count: number; lastError?: string; lastSeenAt: string }>;
|
||||
};
|
||||
|
||||
export type ModelPricing = {
|
||||
id: string;
|
||||
channelId?: string;
|
||||
model: string;
|
||||
capability: "text" | "image" | "video" | "audio";
|
||||
currency: string;
|
||||
inputPerMillionMicros: number;
|
||||
outputPerMillionMicros: number;
|
||||
cachedPerMillionMicros: number;
|
||||
perRequestMicros: number;
|
||||
perMediaMicros: number;
|
||||
perVideoSecondMicros: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type StoryboardPromptTemplate = {
|
||||
id: string;
|
||||
name: string;
|
||||
content: string;
|
||||
enabled: boolean;
|
||||
createdBy?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type StoryboardPromptVariable = {
|
||||
label: string;
|
||||
placeholder: string;
|
||||
};
|
||||
|
||||
export type AdminOSSSetting = {
|
||||
enabled: boolean;
|
||||
provider: "aliyun";
|
||||
region: string;
|
||||
endpoint: string;
|
||||
bucket: string;
|
||||
accessKeyId: string;
|
||||
accessKeySecret?: string;
|
||||
hasAccessKeySecret: boolean;
|
||||
publicBaseUrl: string;
|
||||
pathPrefix: string;
|
||||
updatedBy?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
type BackendEnvelope<T> = { code: number; data: T; msg: string };
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
try {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BackendEnvelope<unknown>>(error)) throw new Error(error.response?.data?.msg || error.message || "请求失败");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function getAuthSettings() {
|
||||
return request<{ firstUser: boolean; registrationEnabled: boolean; linuxdoEnabled: boolean; emailEnabled: boolean; emailCodeRequired: boolean }>(api.get("/auth/settings"));
|
||||
}
|
||||
|
||||
export function linuxDOLoginURL(next: string) {
|
||||
const base = String(api.defaults.baseURL || "/api").replace(/\/$/, "");
|
||||
return `${base}/auth/linuxdo/start?next=${encodeURIComponent(next)}`;
|
||||
}
|
||||
|
||||
export function getAuthSession() {
|
||||
return request<AuthSessionPayload>(api.get("/auth/session"));
|
||||
}
|
||||
|
||||
export function getSystemChannels() {
|
||||
return request<{ channels: ModelChannel[] }>(api.get("/channels/system"));
|
||||
}
|
||||
|
||||
export function login(input: { username: string; password: string }) {
|
||||
return request<{ user: LocalUser }>(api.post("/auth/login", input));
|
||||
}
|
||||
|
||||
export function sendRegistrationEmailCode(email: string) {
|
||||
return request<{ sent: boolean }>(api.post("/auth/email-code", { email }));
|
||||
}
|
||||
|
||||
export function register(input: { username: string; email?: string; emailCode?: string; displayName?: string; password: string }) {
|
||||
return request<{ user: LocalUser }>(api.post("/auth/register", input));
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
return request<{ ok: boolean }>(api.post("/auth/logout"));
|
||||
}
|
||||
|
||||
export type AdminListParams = { keyword?: string; status?: string; role?: string; interfaceType?: string; page?: number; limit?: number };
|
||||
|
||||
export function listAdminUsers(params: AdminListParams = {}) {
|
||||
return request<{ users: AdminUser[]; total: number; page: number; limit: number }>(api.get("/admin/users", { params }));
|
||||
}
|
||||
|
||||
export function getAdminReferences() {
|
||||
return request<AdminReferenceData>(api.get("/admin/references"));
|
||||
}
|
||||
|
||||
export function getAdminUserDetail(id: string) {
|
||||
return request<AdminUserDetail>(api.get(`/admin/users/${encodeURIComponent(id)}/detail`));
|
||||
}
|
||||
|
||||
export function listAdminUserLedger(id: string, params: { page?: number; limit?: number; type?: string } = {}) {
|
||||
return request<{ entries: CreditLedgerEntry[]; total: number; page: number; limit: number }>(api.get(`/admin/users/${encodeURIComponent(id)}/ledger`, { params }));
|
||||
}
|
||||
|
||||
export function listAdminUserTasks(id: string, params: { page?: number; limit?: number } = {}) {
|
||||
return request<{ tasks: AdminUserTask[]; total: number; page: number; limit: number }>(api.get(`/admin/users/${encodeURIComponent(id)}/tasks`, { params }));
|
||||
}
|
||||
|
||||
export function listAdminUserAuditEvents(id: string, params: { page?: number; limit?: number } = {}) {
|
||||
return request<{ events: AdminAuditEvent[]; total: number; page: number; limit: number }>(api.get(`/admin/users/${encodeURIComponent(id)}/audit-events`, { params }));
|
||||
}
|
||||
|
||||
export function updateAdminUser(id: string, input: Partial<Pick<LocalUser, "displayName" | "email" | "role" | "status">> & { password?: string }) {
|
||||
return request<{ user: LocalUser }>(api.patch(`/admin/users/${encodeURIComponent(id)}`, input));
|
||||
}
|
||||
|
||||
export function deleteAdminUser(id: string) {
|
||||
return request<{ ok: boolean }>(api.delete(`/admin/users/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function bulkDisableAdminUsers(userIds: string[]) {
|
||||
return request<{ users: LocalUser[]; disabledCount: number }>(api.post("/admin/users/bulk-disable", { userIds }));
|
||||
}
|
||||
|
||||
export function listAdminChannels(params: AdminListParams = {}) {
|
||||
return request<{ channels: ModelChannel[]; total: number; page: number; limit: number }>(api.get("/admin/channels", { params }));
|
||||
}
|
||||
|
||||
export function createAdminChannel(input: Partial<ModelChannel>) {
|
||||
return request<{ channel: ModelChannel }>(api.post("/admin/channels", input));
|
||||
}
|
||||
|
||||
export function updateAdminChannel(id: string, input: Partial<ModelChannel>) {
|
||||
return request<{ channel: ModelChannel }>(api.patch(`/admin/channels/${encodeURIComponent(id)}`, input));
|
||||
}
|
||||
|
||||
export function deleteAdminChannel(id: string) {
|
||||
return request<{ ok: boolean }>(api.delete(`/admin/channels/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function listAdminStoryboardPromptTemplates() {
|
||||
return request<{ templates: StoryboardPromptTemplate[]; variables: StoryboardPromptVariable[] }>(api.get("/admin/storyboard-prompts"));
|
||||
}
|
||||
|
||||
export function createAdminStoryboardPromptTemplate(input: Partial<Pick<StoryboardPromptTemplate, "name" | "content" | "enabled">>) {
|
||||
return request<{ template: StoryboardPromptTemplate }>(api.post("/admin/storyboard-prompts", input));
|
||||
}
|
||||
|
||||
export function updateAdminStoryboardPromptTemplate(id: string, input: Partial<Pick<StoryboardPromptTemplate, "name" | "content" | "enabled">>) {
|
||||
return request<{ template: StoryboardPromptTemplate }>(api.patch(`/admin/storyboard-prompts/${encodeURIComponent(id)}`, input));
|
||||
}
|
||||
|
||||
export function deleteAdminStoryboardPromptTemplate(id: string) {
|
||||
return request<{ ok: boolean }>(api.delete(`/admin/storyboard-prompts/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function getAdminOSSSetting() {
|
||||
return request<{ setting: AdminOSSSetting }>(api.get("/admin/settings/oss"));
|
||||
}
|
||||
|
||||
export function updateAdminOSSSetting(input: Partial<AdminOSSSetting>) {
|
||||
return request<{ setting: AdminOSSSetting }>(api.patch("/admin/settings/oss", input));
|
||||
}
|
||||
|
||||
export function listAdminApiLogs(params: AdminListParams = {}) {
|
||||
return request<{ logs: ApiCallLog[]; total: number; page: number; limit: number }>(api.get("/admin/api-logs", { params }));
|
||||
}
|
||||
|
||||
export function getAdminApiLog(id: string) {
|
||||
return request<{ log: ApiCallLog }>(api.get(`/admin/api-logs/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export async function exportAdminApiLogs(params: AdminListParams & { ids?: string[] } = {}) {
|
||||
const response = await api.get<Blob>("/admin/api-logs-export.csv", { params: { ...params, ids: params.ids?.join(",") }, responseType: "blob" });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export function getAdminAnalytics(params: AnalyticsFilters) {
|
||||
return request<AdminAnalytics>(api.get("/admin/analytics/overview", { params }));
|
||||
}
|
||||
|
||||
export async function exportAdminAnalytics(params: AnalyticsFilters) {
|
||||
const response = await api.get<Blob>("/admin/analytics/export.csv", { params, responseType: "blob" });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export function listAdminModelPricings() {
|
||||
return request<{ pricings: ModelPricing[] }>(api.get("/admin/model-pricings"));
|
||||
}
|
||||
|
||||
export function createAdminModelPricing(input: Omit<ModelPricing, "id" | "createdAt" | "updatedAt">) {
|
||||
return request<{ pricing: ModelPricing }>(api.post("/admin/model-pricings", input));
|
||||
}
|
||||
|
||||
export function updateAdminModelPricing(id: string, input: Omit<ModelPricing, "id" | "createdAt" | "updatedAt">) {
|
||||
return request<{ pricing: ModelPricing }>(api.patch(`/admin/model-pricings/${encodeURIComponent(id)}`, input));
|
||||
}
|
||||
|
||||
export function deleteAdminModelPricing(id: string) {
|
||||
return request<{ ok: boolean }>(api.delete(`/admin/model-pricings/${encodeURIComponent(id)}`));
|
||||
}
|
||||
40
web/src/services/api/canvas-share.ts
Normal file
40
web/src/services/api/canvas-share.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import axios from "axios";
|
||||
|
||||
import type { BackendEnvelope } from "@/services/api/task-center";
|
||||
import type { CanvasProject } from "@/stores/canvas/use-canvas-store";
|
||||
|
||||
const api = axios.create({ baseURL: import.meta.env.VITE_CANVAS_BACKEND_URL || "/api", withCredentials: true });
|
||||
|
||||
export type CanvasShareStatus = {
|
||||
enabled: boolean;
|
||||
token?: string;
|
||||
expiresAt?: string;
|
||||
createdAt?: string;
|
||||
};
|
||||
|
||||
export type PublicCanvasShare = {
|
||||
project: CanvasProject;
|
||||
expiresAt?: string;
|
||||
};
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
export function getCanvasShare(projectId: string) {
|
||||
return request<{ share: CanvasShareStatus }>(api.get(`/canvas-projects/${encodeURIComponent(projectId)}/share`));
|
||||
}
|
||||
|
||||
export function createCanvasShare(projectId: string, params: { expiresDays: number; rotate?: boolean }) {
|
||||
return request<{ share: CanvasShareStatus }>(api.post(`/canvas-projects/${encodeURIComponent(projectId)}/share`, params));
|
||||
}
|
||||
|
||||
export function deleteCanvasShare(projectId: string) {
|
||||
return request<{ id: string }>(api.delete(`/canvas-projects/${encodeURIComponent(projectId)}/share`));
|
||||
}
|
||||
|
||||
export function getPublicCanvasShare(token: string) {
|
||||
return request<PublicCanvasShare>(api.get(`/public/canvas-shares/${encodeURIComponent(token)}`));
|
||||
}
|
||||
921
web/src/services/api/image.ts
Normal file
921
web/src/services/api/image.ts
Normal file
@@ -0,0 +1,921 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { buildApiUrl, isSystemProxyBaseUrl, resolveBackendApiUrl, resolveModelRequestConfig, type AiConfig, type ModelChannel } from "@/stores/use-config-store";
|
||||
import { nanoid } from "nanoid";
|
||||
import { dataUrlToFile } from "@/lib/image-utils";
|
||||
import { buildImageReferencePromptText } from "@/lib/image-reference-prompt";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
import type { ReferenceImage } from "@/types/image";
|
||||
|
||||
export type AiTextMessage = {
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string | Array<{ type: "text"; text: string } | { type: "image_url"; image_url: { url: 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[];
|
||||
};
|
||||
|
||||
type ToolChoice = "auto" | "required" | { type: "function"; name: string };
|
||||
type ResponseMessageContent = AiTextMessage["content"] | string;
|
||||
type ResponseInputContent = { type: "input_text"; text: string } | { type: "input_image"; image_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; payload?: ResponseApiPayload; error?: string };
|
||||
type ChatCompletionToolCall = { id?: string; type?: "function"; function?: { name?: string; arguments?: string } };
|
||||
type ChatCompletionPayload = {
|
||||
choices?: Array<{ message?: { 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; toolCalls: Map<number, ChatCompletionStreamToolCall>; error?: string };
|
||||
|
||||
type ImageApiResponse = {
|
||||
data?: Array<Record<string, unknown>>;
|
||||
error?: { message?: string };
|
||||
code?: number;
|
||||
msg?: string;
|
||||
};
|
||||
type GeminiPart = {
|
||||
text?: string;
|
||||
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; toolCalls: ResponseToolCall[]; error?: string };
|
||||
type RequestOptions = { signal?: AbortSignal };
|
||||
|
||||
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";
|
||||
|
||||
function normalizeQuality(quality: string) {
|
||||
const value = quality.trim().toLowerCase();
|
||||
const normalized = QUALITY_ALIASES[value] || value;
|
||||
return QUALITY_BASE[normalized] ? normalized : 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 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;
|
||||
}
|
||||
|
||||
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": crypto.randomUUID() } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function aiFetchCredentials(config: Pick<AiConfig, "baseUrl">): RequestCredentials {
|
||||
return isSystemProxyBaseUrl(config.baseUrl) ? "include" : "same-origin";
|
||||
}
|
||||
|
||||
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) => (item.type === "text" ? { type: "input_text" as const, text: item.text } : { type: "input_image" as const, image_url: item.image_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: message.content });
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function toChatCompletionToolChoice(toolChoice: ToolChoice) {
|
||||
return typeof toolChoice === "object" ? { type: "function", function: { name: toolChoice.name } } : toolChoice;
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
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) {
|
||||
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 (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, 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);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeResponseStreamBlock(state.buffer, state, onDelta);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function requestStreamingResponse(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const response = await fetch(aiApiUrl(config, "/responses"), {
|
||||
method: "POST",
|
||||
headers: { ...aiHeaders(config, "application/json"), Accept: "text/event-stream" },
|
||||
body: JSON.stringify({ ...body, stream: true }),
|
||||
signal: options?.signal,
|
||||
credentials: aiFetchCredentials(config),
|
||||
});
|
||||
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: "" };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeResponseStreamText(state, decoder.decode(value, { stream: true }), onDelta);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeResponseStreamText(state, decoder.decode(), onDelta, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
if (!state.payload) return { content: state.text, toolCalls: [] };
|
||||
validateResponsePayload(state.payload);
|
||||
const result = parseToolResponse(state.payload);
|
||||
return { ...result, content: state.text || result.content };
|
||||
}
|
||||
|
||||
function consumeChatCompletionStreamBlock(block: string, state: ChatCompletionStreamState, onDelta?: (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 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, 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);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeChatCompletionStreamBlock(state.buffer, state, onDelta);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function requestStreamingChatCompletion(config: AiConfig, body: Record<string, unknown>, onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const response = await fetch(aiApiUrl(config, "/chat/completions"), {
|
||||
method: "POST",
|
||||
headers: { ...aiHeaders(config, "application/json"), Accept: "text/event-stream" },
|
||||
body: JSON.stringify({ ...body, stream: true }),
|
||||
signal: options?.signal,
|
||||
credentials: aiFetchCredentials(config),
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
if (!response.body || !contentType.includes("text/event-stream")) return parseChatCompletionPayload(await readJsonPayload<ChatCompletionPayload>(response, "请求失败"));
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: ChatCompletionStreamState = { buffer: "", text: "", toolCalls: new Map() };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeChatCompletionStreamText(state, decoder.decode(value, { stream: true }), onDelta);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeChatCompletionStreamText(state, decoder.decode(), onDelta, 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 };
|
||||
}
|
||||
|
||||
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) => (item.type === "text" ? { text: item.text } : toGeminiImagePart(item.image_url.url)));
|
||||
}
|
||||
|
||||
function toGeminiImagePart(url: string): GeminiPart {
|
||||
const match = url.match(/^data:([^;,]+);base64,(.+)$/);
|
||||
if (match) return { inlineData: { mimeType: match[1], data: match[2] } };
|
||||
return { fileData: { fileUri: url, mimeType: "image/png" } };
|
||||
}
|
||||
|
||||
function geminiTextContent(content: ResponseMessageContent) {
|
||||
if (!Array.isArray(content)) return String(content || "");
|
||||
return content.map((item) => (item.type === "text" ? item.text : item.image_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 response = await fetch(`${geminiApiUrl(config, "streamGenerateContent")}?alt=sse`, {
|
||||
method: "POST",
|
||||
headers: geminiHeaders(config),
|
||||
body: JSON.stringify(body),
|
||||
signal: options?.signal,
|
||||
credentials: aiFetchCredentials(config),
|
||||
});
|
||||
if (!response.ok) throw new Error(await readFetchError(response, "请求失败"));
|
||||
if (!response.body) {
|
||||
const payload = (await response.json()) as GeminiPayload;
|
||||
return parseGeminiToolResponse(payload);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const state: GeminiStreamState = { buffer: "", text: "", toolCalls: [] };
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
consumeGeminiStreamText(state, decoder.decode(value, { stream: true }), onDelta);
|
||||
if (state.error) throw new Error(state.error);
|
||||
}
|
||||
consumeGeminiStreamText(state, decoder.decode(), onDelta, true);
|
||||
if (state.error) throw new Error(state.error);
|
||||
return { content: state.text, toolCalls: state.toolCalls };
|
||||
}
|
||||
|
||||
function consumeGeminiStreamText(state: GeminiStreamState, text: string, onDelta?: (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);
|
||||
state.buffer = state.buffer.slice(index + match[0].length);
|
||||
}
|
||||
if (flush && state.buffer.trim()) {
|
||||
consumeGeminiStreamBlock(state.buffer, state, onDelta);
|
||||
state.buffer = "";
|
||||
}
|
||||
}
|
||||
|
||||
function consumeGeminiStreamBlock(block: string, state: GeminiStreamState, onDelta?: (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.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.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 };
|
||||
}
|
||||
|
||||
async function requestGeminiImages(config: AiConfig, prompt: string, references: ReferenceImage[], count: number, options?: RequestOptions) {
|
||||
const requests = Array.from({ length: count }, () => requestGeminiImagesOnce(config, prompt, references, options));
|
||||
return (await Promise.all(requests)).flat();
|
||||
}
|
||||
|
||||
async function requestGeminiImagesOnce(config: AiConfig, prompt: string, references: ReferenceImage[], options?: RequestOptions) {
|
||||
const parts: GeminiPart[] = [{ text: prompt }];
|
||||
for (const image of references) {
|
||||
parts.push(toGeminiImagePart(await imageToDataUrl(image)));
|
||||
}
|
||||
const response = await axios.post<GeminiPayload>(
|
||||
geminiApiUrl(config, "generateContent"),
|
||||
{
|
||||
...toGeminiBody(config, [{ role: "user", content: prompt }], { generationConfig: { responseModalities: ["TEXT", "IMAGE"] } }),
|
||||
contents: [{ role: "user", parts }],
|
||||
},
|
||||
{ headers: geminiHeaders(config), signal: options?.signal },
|
||||
);
|
||||
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;
|
||||
}
|
||||
|
||||
export async function requestGeneration(config: AiConfig, prompt: string, options?: RequestOptions) {
|
||||
const requestConfig = resolveModelRequestConfig(config, config.model || config.imageModel);
|
||||
const n = Math.max(1, Math.min(15, Math.floor(Math.abs(Number(config.count)) || 1)));
|
||||
if (requestConfig.apiFormat === "gemini") {
|
||||
try {
|
||||
return await requestGeminiImages(requestConfig, prompt, [], n, options);
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "请求失败"));
|
||||
}
|
||||
}
|
||||
const quality = normalizeQuality(config.quality);
|
||||
const requestSize = resolveRequestSize(quality, config.size);
|
||||
try {
|
||||
const response = await axios.post<ImageApiResponse>(
|
||||
aiApiUrl(requestConfig, "/images/generations"),
|
||||
{
|
||||
model: requestConfig.model,
|
||||
prompt: withSystemPrompt(requestConfig, prompt),
|
||||
n,
|
||||
...(quality ? { quality } : {}),
|
||||
...(requestSize ? { size: requestSize } : {}),
|
||||
response_format: "b64_json",
|
||||
output_format: IMAGE_OUTPUT_FORMAT,
|
||||
},
|
||||
{
|
||||
headers: aiHeaders(requestConfig, "application/json"),
|
||||
signal: options?.signal,
|
||||
},
|
||||
);
|
||||
const images = parseImagePayload(response.data);
|
||||
return images;
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "请求失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function requestEdit(config: AiConfig, prompt: string, references: ReferenceImage[], mask?: ReferenceImage, options?: RequestOptions) {
|
||||
const requestConfig = resolveModelRequestConfig(config, config.model || config.imageModel);
|
||||
const n = Math.max(1, Math.min(15, Math.floor(Math.abs(Number(config.count)) || 1)));
|
||||
const requestPrompt = buildImageReferencePromptText(prompt, references);
|
||||
if (requestConfig.apiFormat === "gemini") {
|
||||
if (mask) throw new Error("Gemini 调用格式暂不支持蒙版编辑");
|
||||
try {
|
||||
return await requestGeminiImages(requestConfig, requestPrompt, references, n, options);
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "请求失败"));
|
||||
}
|
||||
}
|
||||
const quality = normalizeQuality(config.quality);
|
||||
const requestSize = resolveRequestSize(quality, config.size);
|
||||
const formData = new FormData();
|
||||
formData.set("model", requestConfig.model);
|
||||
formData.set("prompt", withSystemPrompt(requestConfig, requestPrompt));
|
||||
formData.set("n", String(n));
|
||||
formData.set("response_format", "b64_json");
|
||||
formData.set("output_format", IMAGE_OUTPUT_FORMAT);
|
||||
if (quality) {
|
||||
formData.set("quality", quality);
|
||||
}
|
||||
if (requestSize) {
|
||||
formData.set("size", requestSize);
|
||||
}
|
||||
const files = await Promise.all(references.map(async (image) => dataUrlToFile({ ...image, dataUrl: await imageToDataUrl(image) })));
|
||||
files.forEach((file) => formData.append("image", file));
|
||||
if (mask) formData.set("mask", dataUrlToFile(mask));
|
||||
|
||||
try {
|
||||
const response = await axios.post<ImageApiResponse>(aiApiUrl(requestConfig, "/images/edits"), formData, { headers: aiHeaders(requestConfig), signal: options?.signal });
|
||||
const images = parseImagePayload(response.data);
|
||||
return images;
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "请求失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function requestImageQuestion(config: AiConfig, messages: AiTextMessage[], onDelta: (text: string) => void, options?: RequestOptions) {
|
||||
const requestConfig = resolveModelRequestConfig(config, config.model || config.textModel);
|
||||
try {
|
||||
if (requestConfig.apiFormat === "gemini") {
|
||||
const answer = (await requestGeminiStreamingResponse(requestConfig, toGeminiBody(requestConfig, messages), onDelta, options)).content || "没有返回内容";
|
||||
if (answer === "没有返回内容") onDelta(answer);
|
||||
return answer;
|
||||
}
|
||||
if (requestConfig.interfaceType === "chat-completion") {
|
||||
const answer = (await requestStreamingChatCompletion(requestConfig, {
|
||||
model: requestConfig.model,
|
||||
messages: toChatCompletionMessages(withSystemMessage(requestConfig, messages)),
|
||||
}, onDelta, options)).content || "没有返回内容";
|
||||
if (answer === "没有返回内容") onDelta(answer);
|
||||
return answer;
|
||||
}
|
||||
const answer = (await requestStreamingResponse(requestConfig, {
|
||||
model: requestConfig.model,
|
||||
input: toResponseInput(withSystemMessage(requestConfig, messages)),
|
||||
}, onDelta, options)).content || "没有返回内容";
|
||||
if (answer === "没有返回内容") onDelta(answer);
|
||||
return answer;
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "请求失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function requestToolResponse(config: AiConfig, messages: ResponseInputMessage[], tools: ResponseFunctionTool[], toolChoice: ToolChoice = "auto", onDelta?: (text: string) => void, options?: RequestOptions): Promise<ToolResponseResult> {
|
||||
const requestConfig = resolveModelRequestConfig(config, config.model || config.textModel);
|
||||
try {
|
||||
if (requestConfig.apiFormat === "gemini") {
|
||||
return await requestGeminiStreamingResponse(requestConfig, toGeminiBody(requestConfig, messages, toGeminiToolOptions(tools, toolChoice)), onDelta, options);
|
||||
}
|
||||
if (requestConfig.interfaceType === "chat-completion") {
|
||||
return await requestStreamingChatCompletion(requestConfig, {
|
||||
model: requestConfig.model,
|
||||
messages: toChatCompletionMessages(withSystemMessage(requestConfig, messages)),
|
||||
tools,
|
||||
tool_choice: toChatCompletionToolChoice(toolChoice),
|
||||
parallel_tool_calls: false,
|
||||
}, onDelta, options);
|
||||
}
|
||||
return await requestStreamingResponse(requestConfig, {
|
||||
model: requestConfig.model,
|
||||
input: toResponseInput(withSystemMessage(requestConfig, messages)),
|
||||
tools: tools.map(toResponseTool),
|
||||
tool_choice: toolChoice,
|
||||
parallel_tool_calls: false,
|
||||
}, onDelta, options);
|
||||
} catch (error) {
|
||||
throw new Error(readAxiosError(error, "请求失败"));
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchImageModels(config: Pick<AiConfig, "baseUrl" | "apiKey" | "apiFormat">) {
|
||||
try {
|
||||
if (config.apiFormat === "gemini") {
|
||||
const response = await axios.get<GeminiPayload>(geminiApiUrl({ ...defaultGeminiConfig, ...config }), { headers: geminiHeaders({ ...defaultGeminiConfig, ...config }) });
|
||||
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 response = await axios.get<{ data?: Array<{ id?: string }>; error?: { message?: string } }>(buildApiUrl(config.baseUrl, "/models"), {
|
||||
headers: {
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
},
|
||||
});
|
||||
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 async function fetchChannelModels(channel: ModelChannel) {
|
||||
return fetchImageModels({ baseUrl: channel.baseUrl, apiKey: channel.apiKey, apiFormat: channel.apiFormat });
|
||||
}
|
||||
|
||||
const defaultGeminiConfig: Pick<AiConfig, "baseUrl" | "apiKey" | "apiFormat" | "model" | "systemPrompt"> = {
|
||||
baseUrl: "https://generativelanguage.googleapis.com",
|
||||
apiKey: "",
|
||||
apiFormat: "gemini",
|
||||
model: "",
|
||||
systemPrompt: "",
|
||||
};
|
||||
15
web/src/services/api/request.ts
Normal file
15
web/src/services/api/request.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export type ApiParams = Record<string, string | string[] | number | number[] | undefined>;
|
||||
|
||||
export function compactApiParams(params: ApiParams) {
|
||||
return Object.fromEntries(Object.entries(params).filter(([, value]) => value !== "" && value !== undefined && (!Array.isArray(value) || value.length > 0))) as ApiParams;
|
||||
}
|
||||
|
||||
export function serializeApiParams(params?: ApiParams) {
|
||||
const queryParams = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(params || {})) {
|
||||
if (value === undefined) continue;
|
||||
if (Array.isArray(value)) value.forEach((item) => queryParams.append(key, String(item)));
|
||||
else queryParams.set(key, String(value));
|
||||
}
|
||||
return queryParams;
|
||||
}
|
||||
158
web/src/services/api/resources.ts
Normal file
158
web/src/services/api/resources.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { getActiveUserScope } from "@/lib/user-scope";
|
||||
import type { BackendEnvelope } from "@/services/api/task-center";
|
||||
|
||||
export type RemoteResource = {
|
||||
id: string;
|
||||
userId: string;
|
||||
kind: "image" | "video" | "audio" | "file" | string;
|
||||
status: "pending" | "ready" | "failed" | "deleted" | string;
|
||||
provider: string;
|
||||
endpoint: string;
|
||||
bucket: string;
|
||||
objectKey: string;
|
||||
publicUrl: string;
|
||||
mimeType: string;
|
||||
size: number;
|
||||
width?: number;
|
||||
height?: number;
|
||||
durationMs?: number;
|
||||
etag?: string;
|
||||
error?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type UserOSSSetting = {
|
||||
enabled: boolean;
|
||||
provider: "aliyun";
|
||||
region: string;
|
||||
endpoint: string;
|
||||
bucket: string;
|
||||
accessKeyId: string;
|
||||
hasAccessKeySecret: boolean;
|
||||
publicBaseUrl: string;
|
||||
pathPrefix: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
export type UserOSSSettingInput = Pick<UserOSSSetting, "enabled" | "provider" | "region" | "endpoint" | "bucket" | "accessKeyId" | "pathPrefix"> & {
|
||||
accessKeySecret?: string;
|
||||
};
|
||||
|
||||
const apiBaseURL = import.meta.env.VITE_CANVAS_BACKEND_URL || "/api";
|
||||
const api = axios.create({ baseURL: apiBaseURL, withCredentials: true });
|
||||
const resourceCache = new Map<string, RemoteResource>();
|
||||
const resourceRequests = new Map<string, Promise<RemoteResource>>();
|
||||
const missingResourceIds = new Set<string>();
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
export function resourceStorageKey(id: string) {
|
||||
return `resource:${id}`;
|
||||
}
|
||||
|
||||
export function getUserOSSSetting() {
|
||||
return request<{ setting: UserOSSSetting }>(api.get("/settings/oss"));
|
||||
}
|
||||
|
||||
export function updateUserOSSSetting(input: UserOSSSettingInput) {
|
||||
return request<{ setting: UserOSSSetting }>(api.patch("/settings/oss", input));
|
||||
}
|
||||
|
||||
export function resourceIdFromStorageKey(storageKey?: string) {
|
||||
return storageKey?.startsWith("resource:") ? storageKey.slice("resource:".length) : "";
|
||||
}
|
||||
|
||||
export function isResourceUrl(url?: string) {
|
||||
const base = String(apiBaseURL).replace(/\/+$/, "");
|
||||
const path = url?.split(/[?#]/, 1)[0] || "";
|
||||
return path.startsWith(`${base}/resources/`) && path.endsWith("/file");
|
||||
}
|
||||
|
||||
export async function uploadResourceFile(file: Blob, kind: "image" | "video" | "audio" | "file", meta?: { width?: number; height?: number; durationMs?: number; fileName?: string }) {
|
||||
const formData = new FormData();
|
||||
const name = meta?.fileName || (file instanceof File ? file.name : `${kind}.${extensionFromMime(file.type, kind)}`);
|
||||
formData.append("kind", kind);
|
||||
formData.append("file", file, name);
|
||||
if (meta?.width) formData.append("width", String(Math.round(meta.width)));
|
||||
if (meta?.height) formData.append("height", String(Math.round(meta.height)));
|
||||
if (meta?.durationMs) formData.append("durationMs", String(Math.round(meta.durationMs)));
|
||||
const data = await request<{ resource: RemoteResource }>(api.post("/resources", formData));
|
||||
resourceCache.set(resourceCacheKey(data.resource.id), data.resource);
|
||||
return data.resource;
|
||||
}
|
||||
|
||||
export async function importResourceFromUrl(url: string, kind: "image" | "video" | "audio" | "file", meta?: { width?: number; height?: number; durationMs?: number }) {
|
||||
const data = await request<{ resource: RemoteResource }>(api.post("/resources/import", { url, kind, width: meta?.width, height: meta?.height, durationMs: meta?.durationMs }));
|
||||
resourceCache.set(resourceCacheKey(data.resource.id), data.resource);
|
||||
return data.resource;
|
||||
}
|
||||
|
||||
export function getResource(id: string): Promise<RemoteResource> {
|
||||
const cacheKey = resourceCacheKey(id);
|
||||
const cached = resourceCache.get(cacheKey);
|
||||
if (cached) return Promise.resolve(cached);
|
||||
if (missingResourceIds.has(cacheKey)) return Promise.reject(new Error("资源不存在或已被删除"));
|
||||
const pending = resourceRequests.get(cacheKey);
|
||||
if (pending) return pending;
|
||||
const task = request<{ resource: RemoteResource }>(api.get(`/resources/${encodeURIComponent(id)}`))
|
||||
.then((data) => {
|
||||
resourceCache.set(cacheKey, data.resource);
|
||||
return data.resource;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 404) missingResourceIds.add(cacheKey);
|
||||
throw error;
|
||||
})
|
||||
.finally(() => resourceRequests.delete(cacheKey));
|
||||
resourceRequests.set(cacheKey, task);
|
||||
return task;
|
||||
}
|
||||
|
||||
function resourceCacheKey(id: string) {
|
||||
return `${getActiveUserScope()}:${id}`;
|
||||
}
|
||||
|
||||
export function resourceFileUrl(id: string) {
|
||||
const base = String(apiBaseURL).replace(/\/+$/, "");
|
||||
return `${base}/resources/${encodeURIComponent(id)}/file?direct=1`;
|
||||
}
|
||||
|
||||
function resourceProxyFileUrl(id: string) {
|
||||
const base = String(apiBaseURL).replace(/\/+$/, "");
|
||||
return `${base}/resources/${encodeURIComponent(id)}/file`;
|
||||
}
|
||||
|
||||
export async function resolveResourceUrl(storageKey?: string, fallback = "") {
|
||||
const id = resourceIdFromStorageKey(storageKey);
|
||||
if (!id) return fallback;
|
||||
const resource = await getResource(id).catch(() => null);
|
||||
return resource ? resource.publicUrl || resourceFileUrl(id) : fallback;
|
||||
}
|
||||
|
||||
export async function getResourceBlob(storageKey: string) {
|
||||
const id = resourceIdFromStorageKey(storageKey);
|
||||
if (!id) return null;
|
||||
const url = resourceProxyFileUrl(id);
|
||||
const response = await fetch(url, { credentials: isResourceUrl(url) ? "include" : "same-origin" });
|
||||
if (!response.ok) return null;
|
||||
return response.blob();
|
||||
}
|
||||
|
||||
function extensionFromMime(mimeType: string, kind: string) {
|
||||
if (mimeType.includes("png")) return "png";
|
||||
if (mimeType.includes("jpeg")) return "jpg";
|
||||
if (mimeType.includes("webp")) return "webp";
|
||||
if (mimeType.includes("gif")) return "gif";
|
||||
if (mimeType.includes("mp4")) return "mp4";
|
||||
if (mimeType.includes("webm")) return "webm";
|
||||
if (mimeType.includes("mpeg")) return "mp3";
|
||||
if (mimeType.includes("wav")) return "wav";
|
||||
return kind === "image" ? "png" : "bin";
|
||||
}
|
||||
115
web/src/services/api/skills.ts
Normal file
115
web/src/services/api/skills.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { compactApiParams, serializeApiParams, type ApiParams } from "@/services/api/request";
|
||||
import type { BackendEnvelope } from "@/services/api/task-center";
|
||||
|
||||
const apiBaseURL = import.meta.env.VITE_CANVAS_BACKEND_URL || "/api";
|
||||
const api = axios.create({ baseURL: apiBaseURL, withCredentials: true });
|
||||
|
||||
export type UpdreamSkillSort = "hot" | "top_rated" | "new";
|
||||
|
||||
export type UpdreamSkill = {
|
||||
dir: string;
|
||||
name: string;
|
||||
description: string;
|
||||
icon_url: string;
|
||||
cover_url: string;
|
||||
detail_content: string;
|
||||
detail_text: string;
|
||||
categories: string[];
|
||||
version: number;
|
||||
uploader_id: number;
|
||||
uploader_name: string;
|
||||
uploader_avatar: string;
|
||||
is_private: boolean;
|
||||
review_status: string;
|
||||
ctime: string;
|
||||
mtime: string;
|
||||
featured_label?: string;
|
||||
activation_count: number;
|
||||
like_count: number;
|
||||
usage_count: number;
|
||||
comment_count: number;
|
||||
rating_count: number;
|
||||
avg_rating: number | null;
|
||||
hot_score?: number;
|
||||
liked: boolean;
|
||||
activated: boolean;
|
||||
user_rating?: number | null;
|
||||
share_scope?: string;
|
||||
share_team_id?: unknown;
|
||||
};
|
||||
|
||||
export type CommunitySkillList = {
|
||||
skills: UpdreamSkill[];
|
||||
total: number;
|
||||
page: number;
|
||||
page_size: number;
|
||||
categories: string[];
|
||||
};
|
||||
|
||||
export type SkillIntegrationCapabilities = {
|
||||
provider: "updream";
|
||||
publicCommunity: boolean;
|
||||
categoryFilter: boolean;
|
||||
publicRankings: boolean;
|
||||
privateAuthorization: "not_configured" | "configured";
|
||||
upload: boolean;
|
||||
comments: boolean;
|
||||
};
|
||||
|
||||
export type ListCommunitySkillsInput = {
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
sort?: UpdreamSkillSort;
|
||||
search?: string;
|
||||
categories?: string[];
|
||||
};
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
export function listCommunitySkills(input: ListCommunitySkillsInput = {}) {
|
||||
const params = serializeApiParams(compactApiParams(input as ApiParams));
|
||||
return request<CommunitySkillList>(api.get(`/skills/community?${params.toString()}`));
|
||||
}
|
||||
|
||||
export function getSkillIntegrationCapabilities() {
|
||||
return request<{ capabilities: SkillIntegrationCapabilities }>(api.get("/skills/capabilities"));
|
||||
}
|
||||
|
||||
export function getCommunitySkill(dir: string) {
|
||||
return request<{ skill: UpdreamSkill }>(api.get(`/skills/community/${encodeURIComponent(dir)}`));
|
||||
}
|
||||
|
||||
export function listActivatedSkills() {
|
||||
return request<{ skills: UpdreamSkill[] }>(api.get("/skills/activated"));
|
||||
}
|
||||
|
||||
export function listFavoriteSkills() {
|
||||
return request<{ skills: UpdreamSkill[] }>(api.get("/skills/favorites"));
|
||||
}
|
||||
|
||||
export function activateSkill(dir: string) {
|
||||
return request<{ skill: UpdreamSkill }>(api.post(`/skills/${encodeURIComponent(dir)}/activate`));
|
||||
}
|
||||
|
||||
export function deactivateSkill(dir: string) {
|
||||
return request<{ skill: UpdreamSkill }>(api.delete(`/skills/${encodeURIComponent(dir)}/activate`));
|
||||
}
|
||||
|
||||
export function favoriteSkill(dir: string) {
|
||||
return request<{ skill: UpdreamSkill }>(api.post(`/skills/${encodeURIComponent(dir)}/favorite`));
|
||||
}
|
||||
|
||||
export function unfavoriteSkill(dir: string) {
|
||||
return request<{ skill: UpdreamSkill }>(api.delete(`/skills/${encodeURIComponent(dir)}/favorite`));
|
||||
}
|
||||
|
||||
export function skillImageUrl(value?: string) {
|
||||
if (!value || !/^https?:\/\//i.test(value)) return value || "";
|
||||
return `${apiBaseURL.replace(/\/$/, "")}/skills/image?url=${encodeURIComponent(value)}`;
|
||||
}
|
||||
242
web/src/services/api/task-center.ts
Normal file
242
web/src/services/api/task-center.ts
Normal file
@@ -0,0 +1,242 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { generationErrorMessage } from "@/lib/generation-error";
|
||||
|
||||
export type TaskStatus = "queued" | "running" | "succeeded" | "failed" | "cancelled";
|
||||
export type TaskBillingStatus = "reserved" | "running" | "settled" | "refunded" | "uncertain";
|
||||
export type AgentSessionStatus = "active" | "completed" | "failed";
|
||||
|
||||
export type BackendEnvelope<T> = {
|
||||
code: number;
|
||||
data: T;
|
||||
msg: string;
|
||||
};
|
||||
|
||||
export type GenerationTask = {
|
||||
id: string;
|
||||
sessionId?: string;
|
||||
projectId?: string;
|
||||
type: string;
|
||||
status: TaskStatus;
|
||||
progress?: number;
|
||||
stage?: string;
|
||||
prompt: string;
|
||||
operation?: string;
|
||||
provider?: string;
|
||||
model?: string;
|
||||
errorCode?: string;
|
||||
inputJson?: string;
|
||||
resultJson?: string;
|
||||
error?: string;
|
||||
attempts: number;
|
||||
startedAt?: string;
|
||||
completedAt?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
billing?: {
|
||||
amountMicrocredits: number;
|
||||
status: TaskBillingStatus;
|
||||
};
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
};
|
||||
|
||||
export type AgentSession = {
|
||||
id: string;
|
||||
projectId?: string;
|
||||
status: AgentSessionStatus;
|
||||
prompt: string;
|
||||
canvasSnapshotJson?: string;
|
||||
canvasOpsJson?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type AgentMessage = {
|
||||
id: string;
|
||||
sessionId: string;
|
||||
role: "user" | "assistant" | "system" | "tool" | string;
|
||||
content: string;
|
||||
payload?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type TaskResult = {
|
||||
id: string;
|
||||
taskId: string;
|
||||
sessionId?: string;
|
||||
kind: string;
|
||||
url?: string;
|
||||
payload?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type SessionFile = {
|
||||
id: string;
|
||||
sessionId: string;
|
||||
fileName: string;
|
||||
mimeType: string;
|
||||
size: number;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type TaskLog = {
|
||||
id: string;
|
||||
taskId: string;
|
||||
level: "info" | "warn" | "error" | string;
|
||||
message: string;
|
||||
payload?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type AgentSessionDetail = {
|
||||
session: AgentSession;
|
||||
messages: AgentMessage[];
|
||||
tasks: GenerationTask[];
|
||||
results: TaskResult[];
|
||||
};
|
||||
|
||||
export type CreateSessionInput = {
|
||||
projectId?: string;
|
||||
prompt: string;
|
||||
canvasSnapshot?: Record<string, unknown>;
|
||||
references?: string[];
|
||||
config?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type CreateTaskInput = {
|
||||
sessionId?: string;
|
||||
projectId?: string;
|
||||
type?: string;
|
||||
operation?: string;
|
||||
prompt: string;
|
||||
provider?: string;
|
||||
model?: string;
|
||||
input?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
const api = axios.create({ baseURL: import.meta.env.VITE_CANVAS_BACKEND_URL || "/api", withCredentials: true });
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
try {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BackendEnvelope<unknown>>(error)) {
|
||||
throw new Error(error.response?.data?.msg || error.message || "请求失败");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function createAgentSession(input: CreateSessionInput) {
|
||||
return request<AgentSessionDetail>(api.post("/create_session", input)).then((detail) => {
|
||||
detail.tasks.forEach((task) => notifyCanvasTaskCreated(task));
|
||||
return detail;
|
||||
});
|
||||
}
|
||||
|
||||
export function queryAgentSession(id: string) {
|
||||
return request<AgentSessionDetail>(api.get(`/query_session/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function downloadSessionResults(id: string) {
|
||||
return request<TaskResult[]>(api.get(`/download_results/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function uploadAgentFile(sessionId: string, file: File) {
|
||||
const formData = new FormData();
|
||||
formData.append("sessionId", sessionId);
|
||||
formData.append("file", file);
|
||||
return request<SessionFile>(api.post("/upload_file", formData));
|
||||
}
|
||||
|
||||
export function createGenerationTask(input: CreateTaskInput) {
|
||||
return request<GenerationTask>(api.post("/tasks", input)).then((task) => {
|
||||
notifyCanvasTaskCreated(task);
|
||||
return task;
|
||||
});
|
||||
}
|
||||
|
||||
export function listGenerationTasks(limit = 30, options?: { projectId?: string; activeOnly?: boolean }) {
|
||||
return request<GenerationTask[]>(api.get("/tasks", { params: { limit, projectId: options?.projectId, activeOnly: options?.activeOnly || undefined } }));
|
||||
}
|
||||
|
||||
export function queryGenerationTask(id: string) {
|
||||
return request<GenerationTask>(api.get(`/tasks/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function retryGenerationTask(id: string) {
|
||||
return request<GenerationTask>(api.post(`/tasks/${encodeURIComponent(id)}/retry`));
|
||||
}
|
||||
|
||||
export function cancelGenerationTask(id: string) {
|
||||
return request<GenerationTask>(api.post(`/tasks/${encodeURIComponent(id)}/cancel`));
|
||||
}
|
||||
|
||||
export function listTaskLogs(id: string) {
|
||||
return request<TaskLog[]>(api.get(`/tasks/${encodeURIComponent(id)}/logs`));
|
||||
}
|
||||
|
||||
export async function waitForGenerationTask(id: string, options?: { signal?: AbortSignal; intervalMs?: number; timeoutMs?: number; initialTask?: GenerationTask; onTaskUpdate?: (task: GenerationTask) => void }) {
|
||||
const startedAt = Date.now();
|
||||
const intervalMs = options?.intervalMs || 2000;
|
||||
let lastTask = options?.initialTask;
|
||||
let lastQueryError: unknown;
|
||||
while (Date.now() - startedAt < (options?.timeoutMs || taskWaitTimeoutMs(lastTask))) {
|
||||
if (options?.signal?.aborted) {
|
||||
await cancelGenerationTask(id).catch(() => undefined);
|
||||
throw new DOMException("Aborted", "AbortError");
|
||||
}
|
||||
let task: GenerationTask;
|
||||
try {
|
||||
task = await queryGenerationTask(id);
|
||||
lastTask = task;
|
||||
lastQueryError = undefined;
|
||||
options?.onTaskUpdate?.(task);
|
||||
} catch (error) {
|
||||
lastQueryError = error;
|
||||
await delay(intervalMs, options?.signal);
|
||||
continue;
|
||||
}
|
||||
if (task.status === "succeeded") {
|
||||
window.dispatchEvent(new CustomEvent("wallet:updated"));
|
||||
return task;
|
||||
}
|
||||
if (task.status === "failed" || task.status === "cancelled") {
|
||||
window.dispatchEvent(new CustomEvent("wallet:updated"));
|
||||
throw new Error(task.error ? generationErrorMessage(task.error) : `任务${task.status === "cancelled" ? "已取消" : "失败"}`);
|
||||
}
|
||||
await delay(intervalMs, options?.signal);
|
||||
}
|
||||
throw new Error(lastQueryError instanceof Error ? `任务状态同步失败:${lastQueryError.message}` : "任务执行超时,请稍后重试");
|
||||
}
|
||||
|
||||
function taskWaitTimeoutMs(task?: GenerationTask) {
|
||||
const type = task?.type || "";
|
||||
if (type.includes("storyboard")) return 13 * 60 * 1000;
|
||||
if (type.includes("video")) return 32 * 60 * 1000;
|
||||
if (type.includes("image")) return 10 * 60 * 1000;
|
||||
if (type.includes("text") || type.includes("audio")) return 12 * 60 * 1000;
|
||||
return 10 * 60 * 1000;
|
||||
}
|
||||
|
||||
function delay(ms: number, signal?: AbortSignal) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const timer = window.setTimeout(resolve, ms);
|
||||
signal?.addEventListener(
|
||||
"abort",
|
||||
() => {
|
||||
window.clearTimeout(timer);
|
||||
reject(new DOMException("Aborted", "AbortError"));
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function notifyCanvasTaskCreated(task: GenerationTask) {
|
||||
if (typeof window === "undefined" || !task.projectId) return;
|
||||
window.dispatchEvent(new CustomEvent("canvas:task-created", { detail: { task } }));
|
||||
}
|
||||
58
web/src/services/api/user-data.ts
Normal file
58
web/src/services/api/user-data.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import axios from "axios";
|
||||
|
||||
import type { BackendEnvelope } from "@/services/api/task-center";
|
||||
import type { Asset } from "@/stores/use-asset-store";
|
||||
import type { CanvasProject } from "@/stores/canvas/use-canvas-store";
|
||||
|
||||
const api = axios.create({ baseURL: import.meta.env.VITE_CANVAS_BACKEND_URL || "/api", withCredentials: true });
|
||||
|
||||
export type RemoteUserDataSummary = {
|
||||
id: string;
|
||||
kind?: string;
|
||||
title: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
try {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BackendEnvelope<unknown>>(error)) throw new Error(error.response?.data?.msg || error.message || "请求失败");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function listRemoteAssets() {
|
||||
return request<{ assets: RemoteUserDataSummary[] }>(api.get("/assets"));
|
||||
}
|
||||
|
||||
export function getRemoteAsset(id: string) {
|
||||
return request<{ asset: Asset }>(api.get(`/assets/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function upsertRemoteAsset(asset: Asset) {
|
||||
return request<{ asset: RemoteUserDataSummary }>(api.put(`/assets/${encodeURIComponent(asset.id)}`, { asset }));
|
||||
}
|
||||
|
||||
export function deleteRemoteAsset(id: string) {
|
||||
return request<{ id: string }>(api.delete(`/assets/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function listRemoteCanvasProjects() {
|
||||
return request<{ projects: RemoteUserDataSummary[] }>(api.get("/canvas-projects"));
|
||||
}
|
||||
|
||||
export function getRemoteCanvasProject(id: string) {
|
||||
return request<{ project: CanvasProject }>(api.get(`/canvas-projects/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function upsertRemoteCanvasProject(project: CanvasProject) {
|
||||
return request<{ project: RemoteUserDataSummary }>(api.put(`/canvas-projects/${encodeURIComponent(project.id)}`, { project }));
|
||||
}
|
||||
|
||||
export function deleteRemoteCanvasProject(id: string) {
|
||||
return request<{ id: string }>(api.delete(`/canvas-projects/${encodeURIComponent(id)}`));
|
||||
}
|
||||
412
web/src/services/api/video.ts
Normal file
412
web/src/services/api/video.ts
Normal file
@@ -0,0 +1,412 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { dataUrlToFile } from "@/lib/image-utils";
|
||||
import { getMediaBlob, uploadMediaFile, type UploadedFile } from "@/services/file-storage";
|
||||
import { imageToDataUrl } from "@/services/image-storage";
|
||||
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 type { ReferenceImage } from "@/types/image";
|
||||
import type { ReferenceAudio, ReferenceVideo } from "@/types/media";
|
||||
|
||||
type VideoResponse = { id: string; status?: string; error?: { message?: string } };
|
||||
type ApiVideoResponse = VideoResponse | { code?: number; data?: VideoResponse | null; msg?: string };
|
||||
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 };
|
||||
|
||||
export type VideoGenerationResult = { blob?: Blob; url?: string; mimeType?: string };
|
||||
export type VideoGenerationTask = { id: string; provider: "openai" | "seedance"; 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": crypto.randomUUID() } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
const delayMs = task.provider === "seedance" ? 5000 : 2500;
|
||||
for (let attempt = 0; attempt < 120; attempt += 1) {
|
||||
if (options?.signal?.aborted) throw new DOMException("Aborted", "AbortError");
|
||||
const state = await pollVideoGenerationTask(config, task, options);
|
||||
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);
|
||||
}
|
||||
throw new Error("视频生成超时,请稍后重试");
|
||||
}
|
||||
|
||||
export async function createVideoGenerationTask(config: AiConfig, prompt: string, references: ReferenceImage[] = [], videoReferences: ReferenceVideo[] = [], audioReferences: ReferenceAudio[] = [], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
const selectedModel = (config.model || config.videoModel).trim();
|
||||
const requestConfig = resolveModelRequestConfig(config, selectedModel);
|
||||
assertVideoConfig(requestConfig, requestConfig.model);
|
||||
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);
|
||||
}
|
||||
|
||||
export async function pollVideoGenerationTask(config: AiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
const requestConfig = resolveModelRequestConfig(config, task.model);
|
||||
assertVideoConfig(requestConfig, requestConfig.model);
|
||||
return task.provider === "seedance" ? pollSeedanceTask(requestConfig, task, options) : pollOpenAIVideoTask(requestConfig, task, options);
|
||||
}
|
||||
|
||||
export async function storeGeneratedVideo(result: VideoGenerationResult): Promise<UploadedFile> {
|
||||
if (result.blob) return uploadMediaFile(result.blob, "video");
|
||||
if (result.url) return { url: result.url, storageKey: "", bytes: 0, mimeType: result.mimeType || "video/mp4" };
|
||||
throw new Error("视频接口没有返回可播放的视频");
|
||||
}
|
||||
|
||||
async function createOpenAIVideoTask(config: AiConfig, model: string, prompt: string, references: ReferenceImage[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
const modelName = modelOptionName(model);
|
||||
if (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 created = unwrapVideoResponse((await axios.post<ApiVideoResponse>(aiApiUrl(config, "/videos"), payload, { headers: aiHeaders(config, "application/json"), signal: options?.signal })).data);
|
||||
if (!created.id) throw new Error("视频接口没有返回任务 ID");
|
||||
return { id: created.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)!);
|
||||
body.append("resolution_name", normalizeVideoResolution(config.vquality));
|
||||
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 axios.post<ApiVideoResponse>(aiApiUrl(config, "/videos"), body, { headers: aiHeaders(config), signal: options?.signal })).data);
|
||||
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: AiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const video = unwrapVideoResponse((await axios.get<ApiVideoResponse>(aiApiUrl(config, `/videos/${task.id}`), { headers: aiHeaders(config), signal: options?.signal })).data);
|
||||
if (video.status === "completed") {
|
||||
const content = await axios.get<Blob>(aiApiUrl(config, `/videos/${task.id}/content`), { headers: aiHeaders(config), responseType: "blob", signal: options?.signal });
|
||||
await assertVideoBlob(content.data);
|
||||
return { status: "completed", result: { blob: content.data } };
|
||||
}
|
||||
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: AiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[], options?: RequestOptions): Promise<VideoGenerationTask> {
|
||||
assertSeedanceVideoReferences(videoReferences);
|
||||
assertSeedanceAudioReferences(audioReferences);
|
||||
const payload = isArkPlanBaseUrl(config.baseUrl) ? await buildSeedanceAgentPlanPayload(config, model, prompt, references, videoReferences, audioReferences) : await buildSeedanceVideosPayload(config, model, prompt, references, videoReferences, audioReferences);
|
||||
|
||||
try {
|
||||
const created = unwrapSeedanceTask((await axios.post<ApiEnvelope<SeedanceTask>>(seedanceApiUrl(config), payload, { headers: aiHeaders(config, "application/json"), signal: options?.signal })).data);
|
||||
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: AiConfig, task: VideoGenerationTask, options?: RequestOptions): Promise<VideoGenerationTaskState> {
|
||||
try {
|
||||
const state = unwrapSeedanceTask((await axios.get<ApiEnvelope<SeedanceTask>>(seedanceApiUrl(config, task.id), { headers: aiHeaders(config), signal: options?.signal })).data);
|
||||
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 axios.get<Blob>(aiApiUrl(config, `/videos/${task.id}/content`), { headers: aiHeaders(config), responseType: "blob", signal: options?.signal });
|
||||
await assertVideoBlob(content.data);
|
||||
return { status: "completed", result: { blob: content.data } };
|
||||
}
|
||||
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: AiConfig, taskId?: string) {
|
||||
if (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: AiConfig, model: string, prompt: string, references: ReferenceImage[], videoReferences: ReferenceVideo[], audioReferences: ReferenceAudio[]) {
|
||||
if (audioReferences.length && !references.length && !videoReferences.length) {
|
||||
throw new Error("Seedance 参考音频不能单独使用,请同时添加参考图或参考视频");
|
||||
}
|
||||
const content = await buildSeedanceContent(config, prompt, references, videoReferences, audioReferences);
|
||||
if (!content.length) throw new Error("请输入视频提示词,或连接参考图片/视频/音频");
|
||||
return {
|
||||
model: modelOptionName(model),
|
||||
content,
|
||||
ratio: normalizeSeedanceRatio(config.size),
|
||||
resolution: normalizeSeedanceResolution(config.vquality, modelOptionName(model)),
|
||||
duration: normalizeSeedanceDuration(config.videoSeconds),
|
||||
generate_audio: boolConfig(config.videoGenerateAudio, true),
|
||||
watermark: boolConfig(config.videoWatermark, false),
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
return {
|
||||
model: modelOptionName(model),
|
||||
prompt: buildSeedanceVideosPromptText(prompt, imageUrls.length, videoUrls.length, audioUrls.length),
|
||||
aspect_ratio: ratio === "adaptive" ? "16:9" : ratio,
|
||||
duration: duration < 4 ? 5 : duration,
|
||||
...(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: AiConfig, 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") throw new Error("Gemini 调用格式暂不支持视频生成,请使用 OpenAI 格式渠道");
|
||||
}
|
||||
|
||||
function normalizeVideoSeconds(value: string) {
|
||||
const seconds = Math.floor(Number(value) || 6);
|
||||
return String(Math.max(1, Math.min(20, seconds)));
|
||||
}
|
||||
|
||||
function normalizeVideoSize(value: string) {
|
||||
if (value === "auto") return null;
|
||||
const size = value || "1280x720";
|
||||
if (/^\d+x\d+$/.test(size)) return size;
|
||||
return ["9:16", "2:3", "3:4"].includes(size) ? "720x1280" : "1280x720";
|
||||
}
|
||||
|
||||
function normalizeVideoResolution(value: string) {
|
||||
if (value === "low") return "480p";
|
||||
if (value === "auto" || value === "high" || value === "medium") return "720p";
|
||||
const resolution = value.replace(/p$/i, "") || "720";
|
||||
return `${resolution}p`;
|
||||
}
|
||||
|
||||
function unwrapVideoResponse(payload: ApiVideoResponse) {
|
||||
return unwrapEnvelope(payload, "接口没有返回视频任务");
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
265
web/src/services/api/wallet.ts
Normal file
265
web/src/services/api/wallet.ts
Normal file
@@ -0,0 +1,265 @@
|
||||
import axios from "axios";
|
||||
|
||||
const api = axios.create({ baseURL: import.meta.env.VITE_CANVAS_BACKEND_URL || "/api", withCredentials: true });
|
||||
|
||||
type BackendEnvelope<T> = { code: number; data: T; msg: string };
|
||||
|
||||
async function request<T>(promise: Promise<{ data: BackendEnvelope<T> }>) {
|
||||
try {
|
||||
const response = await promise;
|
||||
if (response.data.code !== 0) throw new Error(response.data.msg || "请求失败");
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BackendEnvelope<unknown>>(error)) throw new Error(error.response?.data?.msg || error.message || "请求失败");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export type CreditAccount = {
|
||||
userId: string;
|
||||
availableMicrocredits: number;
|
||||
reservedMicrocredits: number;
|
||||
version: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type CreditLedgerEntry = {
|
||||
id: string;
|
||||
userId: string;
|
||||
type: "redeem" | "admin_grant" | "consume" | "refund" | "admin_adjustment" | "signup_bonus" | "checkin_bonus";
|
||||
amountMicrocredits: number;
|
||||
availableAfterMicrocredits: number;
|
||||
reservedAfterMicrocredits: number;
|
||||
billingOrderId?: string;
|
||||
model?: string;
|
||||
channelId?: string;
|
||||
scene?: string;
|
||||
note?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type WalletSummary = {
|
||||
account: CreditAccount;
|
||||
entries: CreditLedgerEntry[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
policy: {
|
||||
signupBonusMicrocredits: number;
|
||||
checkinBonusMicrocredits: number;
|
||||
checkedInToday: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
export type CreditPolicy = {
|
||||
signupBonusMicrocredits: number;
|
||||
checkinBonusMicrocredits: number;
|
||||
defaultMultiplierBasisPoints: number;
|
||||
modelMultiplierBasisPoints: Record<string, number>;
|
||||
};
|
||||
|
||||
export type ChannelModel = {
|
||||
id: string;
|
||||
channelId: string;
|
||||
modelKey: string;
|
||||
displayName: string;
|
||||
capability: "text" | "image" | "video" | "audio";
|
||||
billingMode: "fixed_request";
|
||||
unitPriceMicrocredits: number;
|
||||
priceConfigured: boolean;
|
||||
enabled: boolean;
|
||||
priceVersion: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type LinuxDOSetting = {
|
||||
enabled: boolean;
|
||||
clientId: string;
|
||||
clientSecret?: string;
|
||||
hasClientSecret: boolean;
|
||||
authorizationUrl: string;
|
||||
tokenUrl: string;
|
||||
userInfoUrl: string;
|
||||
redirectUrl: string;
|
||||
scopes: string[];
|
||||
clientAuthMethod: "client_secret_post" | "client_secret_basic";
|
||||
subjectField: string;
|
||||
usernameField: string;
|
||||
displayNameField: string;
|
||||
emailField: string;
|
||||
avatarField: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
export type RegistrationSetting = {
|
||||
enabled: boolean;
|
||||
updatedBy?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
export type EmailSetting = {
|
||||
enabled: boolean;
|
||||
host: string;
|
||||
port: number;
|
||||
username: string;
|
||||
password?: string;
|
||||
encryption: "starttls" | "tls" | "none";
|
||||
fromEmail: string;
|
||||
fromName: string;
|
||||
hasPassword: boolean;
|
||||
updatedBy?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
export type RedeemBatch = {
|
||||
id: string;
|
||||
amountMicrocredits: number;
|
||||
count: number;
|
||||
note?: string;
|
||||
createdBy: string;
|
||||
expiresAt?: string;
|
||||
createdAt: string;
|
||||
availableCount: number;
|
||||
redeemedCount: number;
|
||||
disabledCount: number;
|
||||
expiredCount: number;
|
||||
};
|
||||
|
||||
export type AdminRedeemCode = {
|
||||
id: string;
|
||||
code?: string;
|
||||
codeSuffix: string;
|
||||
status: "unused" | "redeemed" | "disabled" | "expired";
|
||||
redeemedBy?: string;
|
||||
redeemedUsername?: string;
|
||||
redeemedDisplayName?: string;
|
||||
redeemedAt?: string;
|
||||
redeemedIp?: string;
|
||||
expiresAt?: string;
|
||||
amountMicrocredits: number;
|
||||
};
|
||||
|
||||
export type AdminRedeemCodePage = {
|
||||
batch: RedeemBatch;
|
||||
codes: AdminRedeemCode[];
|
||||
plaintextAvailable: boolean;
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
};
|
||||
|
||||
export type BillingOrder = {
|
||||
id: string;
|
||||
userId: string;
|
||||
taskId?: string;
|
||||
channelId: string;
|
||||
model: string;
|
||||
capability: string;
|
||||
scene: string;
|
||||
amountMicrocredits: number;
|
||||
status: "reserved" | "running" | "settled" | "refunded" | "uncertain";
|
||||
providerRequestId?: string;
|
||||
error?: string;
|
||||
resolvedBy?: string;
|
||||
resolutionNote?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export function getWallet(page = 1, limit = 30, type = "all") {
|
||||
return request<WalletSummary>(api.get("/wallet", { params: { type, page, limit } }));
|
||||
}
|
||||
|
||||
export function redeemCredits(code: string) {
|
||||
return request<{ account: CreditAccount }>(api.post("/wallet/redeem", { code }));
|
||||
}
|
||||
|
||||
export function checkinCredits() {
|
||||
return request<{ account: CreditAccount; granted: boolean }>(api.post("/wallet/checkin"));
|
||||
}
|
||||
|
||||
export function getAdminCreditPolicy() {
|
||||
return request<{ policy: CreditPolicy }>(api.get("/admin/settings/credits"));
|
||||
}
|
||||
|
||||
export function updateAdminCreditPolicy(policy: CreditPolicy) {
|
||||
return request<{ policy: CreditPolicy }>(api.patch("/admin/settings/credits", policy));
|
||||
}
|
||||
|
||||
export function getAdminLinuxDOSetting() {
|
||||
return request<{ setting: LinuxDOSetting }>(api.get("/admin/settings/linuxdo"));
|
||||
}
|
||||
|
||||
export function updateAdminLinuxDOSetting(input: Partial<LinuxDOSetting>) {
|
||||
return request<{ setting: LinuxDOSetting }>(api.patch("/admin/settings/linuxdo", input));
|
||||
}
|
||||
|
||||
export function getAdminRegistrationSetting() {
|
||||
return request<{ setting: RegistrationSetting }>(api.get("/admin/settings/registration"));
|
||||
}
|
||||
|
||||
export function updateAdminRegistrationSetting(enabled: boolean) {
|
||||
return request<{ setting: RegistrationSetting }>(api.patch("/admin/settings/registration", { enabled }));
|
||||
}
|
||||
|
||||
export function getAdminEmailSetting() {
|
||||
return request<{ setting: EmailSetting }>(api.get("/admin/settings/email"));
|
||||
}
|
||||
|
||||
export function updateAdminEmailSetting(input: Partial<EmailSetting>) {
|
||||
return request<{ setting: EmailSetting }>(api.patch("/admin/settings/email", input));
|
||||
}
|
||||
|
||||
export function listAdminChannelModels(channelId: string) {
|
||||
return request<{ models: ChannelModel[] }>(api.get(`/admin/channels/${encodeURIComponent(channelId)}/models`));
|
||||
}
|
||||
|
||||
export function createAdminChannelModel(channelId: string, input: Omit<ChannelModel, "id" | "channelId" | "priceVersion" | "createdAt" | "updatedAt">) {
|
||||
return request<{ model: ChannelModel }>(api.post(`/admin/channels/${encodeURIComponent(channelId)}/models`, input));
|
||||
}
|
||||
|
||||
export function updateAdminChannelModel(channelId: string, id: string, input: Omit<ChannelModel, "id" | "channelId" | "priceVersion" | "createdAt" | "updatedAt">) {
|
||||
return request<{ model: ChannelModel }>(api.patch(`/admin/channels/${encodeURIComponent(channelId)}/models/${encodeURIComponent(id)}`, input));
|
||||
}
|
||||
|
||||
export function disableAdminChannelModel(channelId: string, id: string) {
|
||||
return request<{ ok: boolean }>(api.delete(`/admin/channels/${encodeURIComponent(channelId)}/models/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export type AdminFinanceListParams = { keyword?: string; status?: string; validity?: string; page?: number; limit?: number };
|
||||
|
||||
export function listAdminRedeemBatches(params: AdminFinanceListParams = {}) {
|
||||
return request<{ batches: RedeemBatch[]; total: number; page: number; limit: number }>(api.get("/admin/redeem-batches", { params }));
|
||||
}
|
||||
|
||||
export function createAdminRedeemBatch(input: { amountMicrocredits: number; count: number; note?: string; expiresAt?: string }) {
|
||||
return request<{ batch: RedeemBatch; codes: string[] }>(api.post("/admin/redeem-batches", input, { timeout: 30_000 }));
|
||||
}
|
||||
|
||||
export function listAdminRedeemBatchCodes(batchId: string, params: { status?: string; page?: number; limit?: number } = {}) {
|
||||
return request<AdminRedeemCodePage>(api.get(`/admin/redeem-batches/${encodeURIComponent(batchId)}/codes`, { params }));
|
||||
}
|
||||
|
||||
export function disableAdminRedeemBatch(batchId: string) {
|
||||
return request<{ disabledCount: number }>(api.post(`/admin/redeem-batches/${encodeURIComponent(batchId)}/disable`));
|
||||
}
|
||||
|
||||
export function disableAdminRedeemCode(batchId: string, codeId: string) {
|
||||
return request<{ ok: boolean }>(api.post(`/admin/redeem-batches/${encodeURIComponent(batchId)}/codes/${encodeURIComponent(codeId)}/disable`));
|
||||
}
|
||||
|
||||
export function adjustAdminUserCredits(userId: string, input: { amountMicrocredits: number; note: string }) {
|
||||
return request<{ account: CreditAccount }>(api.post(`/admin/users/${encodeURIComponent(userId)}/credits/adjust`, input));
|
||||
}
|
||||
|
||||
export function listAdminBillingOrders(params: AdminFinanceListParams = {}) {
|
||||
return request<{ orders: BillingOrder[]; total: number; page: number; limit: number }>(api.get("/admin/billing-orders", { params }));
|
||||
}
|
||||
|
||||
export function resolveAdminBillingOrder(id: string, input: { action: "settle" | "refund"; note: string }) {
|
||||
return request<{ order: BillingOrder }>(api.post(`/admin/billing-orders/${encodeURIComponent(id)}/resolve`, input));
|
||||
}
|
||||
Reference in New Issue
Block a user