refactor(*): 代码质量治理 - 收敛接口客户端、模型职责与异步计费巡检
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { generationErrorMessage } from "@/lib/generation-error";
|
||||
import { apiClient, request, type BackendEnvelope } from "@/services/api/request";
|
||||
|
||||
export type { BackendEnvelope } from "@/services/api/request";
|
||||
|
||||
export type TaskStatus = "queued" | "running" | "succeeded" | "failed" | "cancelled";
|
||||
export type TaskBillingStatus = "reserved" | "running" | "settled" | "refunded" | "uncertain";
|
||||
export type ProviderCancelStatus = "requested" | "confirmed" | "uncertain";
|
||||
export type AgentSessionStatus = "active" | "completed" | "failed";
|
||||
|
||||
export type BackendEnvelope<T> = {
|
||||
code: number;
|
||||
data: T;
|
||||
msg: string;
|
||||
};
|
||||
|
||||
export type GenerationTask = {
|
||||
id: string;
|
||||
sessionId?: string;
|
||||
@@ -116,7 +111,7 @@ export type SessionFile = {
|
||||
sessionId: string;
|
||||
fileName: string;
|
||||
mimeType: string;
|
||||
size: number;
|
||||
size: number;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
@@ -157,30 +152,17 @@ export type CreateTaskInput = {
|
||||
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;
|
||||
}
|
||||
}
|
||||
const api = apiClient;
|
||||
|
||||
export function createAgentSession(input: CreateSessionInput) {
|
||||
return request<AgentSessionDetail>(api.post("/create_session", input)).then((detail) => {
|
||||
return request<AgentSessionDetail>(api.post("/sessions", 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)}`));
|
||||
return request<AgentSessionDetail>(api.get(`/sessions/${encodeURIComponent(id)}`));
|
||||
}
|
||||
|
||||
export function agentSessionFailureMessage(detail: AgentSessionDetail, fallback = "后端影视 Agent 会话失败") {
|
||||
@@ -196,14 +178,14 @@ export function agentSessionFailureMessage(detail: AgentSessionDetail, fallback
|
||||
}
|
||||
|
||||
export function downloadSessionResults(id: string) {
|
||||
return request<TaskResult[]>(api.get(`/download_results/${encodeURIComponent(id)}`));
|
||||
return request<TaskResult[]>(api.get(`/sessions/${encodeURIComponent(id)}/results`));
|
||||
}
|
||||
|
||||
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));
|
||||
return request<SessionFile>(api.post("/files", formData));
|
||||
}
|
||||
|
||||
export function createGenerationTask(input: CreateTaskInput) {
|
||||
|
||||
Reference in New Issue
Block a user