import { and, eq } from "drizzle-orm"; import { db } from "@/lib/db"; import { canvasProject } from "@/lib/db/schema"; import { AgentRunError } from "./store"; export type RuntimeImageNode = { id: string; type: string; position: { x: number; y: number }; size?: { width?: number; height?: number }; data: { src?: string; url?: string; title?: string; prompt?: string; status?: string }; }; export async function readOwnedCanvas(userId: string, projectId: string) { const [project] = await db .select() .from(canvasProject) .where(and(eq(canvasProject.id, projectId), eq(canvasProject.userId, userId))) .limit(1); if (!project) throw new AgentRunError("画布不存在", 404); const data = project.canvasData as { nodes?: RuntimeImageNode[] } | null; return { project, nodes: Array.isArray(data?.nodes) ? data.nodes : [] }; } export async function readCanvasImages(userId: string, projectId: string, ids: string[]) { const { nodes } = await readOwnedCanvas(userId, projectId); return ids.map((id) => { const node = nodes.find((item) => item.id === id && item.type === "image"); if (!node) throw new AgentRunError(`图片节点不存在:${id}`, 404); if (!/^https?:\/\//i.test(node.data.src || node.data.url || "")) { throw new AgentRunError("图片仍在上传,请等待上传完成。"); } return node; }); }