171 lines
6.0 KiB
TypeScript
171 lines
6.0 KiB
TypeScript
import type {
|
|
AgentCategory,
|
|
AgentCategoryCreatePayload,
|
|
AgentCreatePayload,
|
|
AgentPreset,
|
|
AgentPresetListResponse,
|
|
AgentUpdatePayload,
|
|
} from "@/types/agent";
|
|
|
|
interface ApiResponse<T = any> {
|
|
success: boolean;
|
|
data?: T;
|
|
error?: string;
|
|
}
|
|
|
|
async function parseJsonSafely<T>(response: Response): Promise<T | null> {
|
|
try {
|
|
return (await response.json()) as T;
|
|
} catch (_error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const agentCategoryService = {
|
|
async getCategories(scope: "all" | "public" | "private" = "public"): Promise<ApiResponse<AgentCategory[]>> {
|
|
try {
|
|
const response = await fetch(`/api/agent-categories?scope=${scope}`, {
|
|
cache: "no-store",
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentCategory[]>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "获取风格化分类失败" };
|
|
}
|
|
return data || { success: false, error: "获取风格化分类失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "获取风格化分类失败" };
|
|
}
|
|
},
|
|
|
|
async createCategory(payload: AgentCategoryCreatePayload): Promise<ApiResponse<AgentCategory>> {
|
|
try {
|
|
const response = await fetch("/api/agent-categories", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentCategory>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "创建风格化分类失败" };
|
|
}
|
|
return data || { success: false, error: "创建风格化分类失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "创建风格化分类失败" };
|
|
}
|
|
},
|
|
};
|
|
|
|
export const agentService = {
|
|
async getPresets(params?: {
|
|
scope?: "all" | "public" | "private";
|
|
type?: "image" | "video";
|
|
categoryId?: string;
|
|
keyword?: string;
|
|
sort?: "latest" | "hottest" | "most_used";
|
|
}): Promise<ApiResponse<AgentPresetListResponse>> {
|
|
try {
|
|
const searchParams = new URLSearchParams();
|
|
if (params?.scope) searchParams.set("scope", params.scope);
|
|
if (params?.type) searchParams.set("type", params.type);
|
|
if (params?.categoryId) searchParams.set("categoryId", params.categoryId);
|
|
if (params?.keyword) searchParams.set("keyword", params.keyword);
|
|
if (params?.sort) searchParams.set("sort", params.sort);
|
|
const query = searchParams.toString();
|
|
const response = await fetch(`/api/agents${query ? `?${query}` : ""}`, {
|
|
cache: "no-store",
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentPresetListResponse>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "获取风格化列表失败" };
|
|
}
|
|
return data || { success: false, error: "获取风格化列表失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "获取风格化列表失败" };
|
|
}
|
|
},
|
|
|
|
async getPreset(id: string): Promise<ApiResponse<AgentPreset>> {
|
|
try {
|
|
const response = await fetch(`/api/agents/${id}`, { cache: "no-store" });
|
|
const data = await parseJsonSafely<ApiResponse<AgentPreset>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "获取风格化失败" };
|
|
}
|
|
return data || { success: false, error: "获取风格化失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "获取风格化失败" };
|
|
}
|
|
},
|
|
|
|
async createPreset(payload: AgentCreatePayload): Promise<ApiResponse<AgentPreset>> {
|
|
try {
|
|
const response = await fetch("/api/agents", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentPreset>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "创建风格化失败" };
|
|
}
|
|
return data || { success: false, error: "创建风格化失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "创建风格化失败" };
|
|
}
|
|
},
|
|
|
|
async updatePreset(id: string, payload: AgentUpdatePayload): Promise<ApiResponse<AgentPreset>> {
|
|
try {
|
|
const response = await fetch(`/api/agents/${id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentPreset>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "更新风格化失败" };
|
|
}
|
|
return data || { success: false, error: "更新风格化失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "更新风格化失败" };
|
|
}
|
|
},
|
|
|
|
async deletePreset(id: string): Promise<ApiResponse<AgentPreset>> {
|
|
try {
|
|
const response = await fetch(`/api/agents/${id}`, {
|
|
method: "DELETE",
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentPreset>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "删除风格化失败" };
|
|
}
|
|
return data || { success: false, error: "删除风格化失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "删除风格化失败" };
|
|
}
|
|
},
|
|
|
|
async forkPreset(id: string, name?: string): Promise<ApiResponse<AgentPreset>> {
|
|
try {
|
|
const response = await fetch(`/api/agents/${id}/fork`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(name ? { name } : {}),
|
|
});
|
|
const data = await parseJsonSafely<ApiResponse<AgentPreset>>(response);
|
|
if (!response.ok) {
|
|
return { success: false, error: data?.error || "复制风格化失败" };
|
|
}
|
|
return data || { success: false, error: "复制风格化失败" };
|
|
} catch (_error) {
|
|
return { success: false, error: "复制风格化失败" };
|
|
}
|
|
},
|
|
};
|
|
|
|
export default {
|
|
preset: agentService,
|
|
category: agentCategoryService,
|
|
};
|