ai-chatbot/components/canvas/tvstar/generation-references.ts

21 lines
1000 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

type Reference = { title: string; url: string; type: "image" | "video" };
export async function resolveTVstarGenerationReferences(
references: Reference[],
fetcher: typeof fetch = fetch,
) {
const images = references.filter(item => item.type === "image").map(item => item.url).slice(0, 4);
// The video processing endpoint accepts one active job per user, so extract sequentially.
for (const item of references.filter(item => item.type === "video").slice(0, 2)) {
const response = await fetcher("/api/canvas/video/process", {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ operation: "frame", url: item.url, kind: "first", time: 0 }),
});
const result = await response.json();
if (!response.ok || !result.success || !result.data?.url)
throw new Error(`${item.title}:${result.error || "参考视频首帧提取失败"}`);
images.push(String(result.data.url));
}
return [...new Set(images)].slice(0, 6);
}