ai-chatbot/lib/client/canvas-site-settings.ts

41 lines
1.6 KiB
TypeScript

import type { AgentWorkflowComposerContext } from "@/types/agent";
const settingsCache = new Map<string, { etag: string; data: unknown }>();
export async function fetchWorkflowTemplatePrompts([url, selectionRequest]: readonly [string, string]) {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: selectionRequest,
signal: AbortSignal.timeout(15_000),
});
if (!response.ok) throw new Error("风格模板提示词加载失败");
const data = await response.json() as { selections?: AgentWorkflowComposerContext["selections"] };
if (!Array.isArray(data.selections)) throw new Error("风格模板提示词返回异常");
return { selectionRequest, selections: data.selections };
}
export async function fetchCanvasSiteSettings(url: string) {
const cached = settingsCache.get(url);
const controller = new AbortController();
const timeoutId = setTimeout(
() => controller.abort(),
url === "/api/site-settings?scope=agent-canvas" ||
url === "/api/site-settings?scope=agent-workflow-templates" ? 30_000 : 10_000
);
try {
const response = await fetch(url, {
headers: cached ? { "If-None-Match": cached.etag } : undefined,
signal: controller.signal,
});
if (response.status === 304 && cached) return cached.data;
if (!response.ok) throw new Error("Failed to fetch canvas site settings");
const data = await response.json();
const etag = response.headers.get("ETag");
if (etag) settingsCache.set(url, { etag, data });
return data;
} finally {
clearTimeout(timeoutId);
}
}