import { toolInputSchemas, toolNames, type ToolName } from "./schemas.js"; import type { CanvasNode, CanvasSnapshot } from "./types.js"; export function isToolName(name: unknown): name is ToolName { return typeof name === "string" && toolNames.includes(name as ToolName); } export function parseToolInput(name: ToolName, input: unknown) { return toolInputSchemas[name].parse(input ?? {}); } export function compactCanvasState(state: CanvasSnapshot | null) { if (!state) throw new Error("当前没有已连接画布"); return { ...state, nodes: (state.nodes || []).map(compactNode) }; } export function compactNode(node: CanvasNode) { const metadata = { ...(node.metadata || {}) }; if (typeof metadata.content === "string" && metadata.content.length > 240) metadata.content = `${metadata.content.slice(0, 120)}...`; return { id: node.id, type: node.type, title: node.title, position: node.position, width: node.width, height: node.height, metadata }; } export function nextCanvasX(state: CanvasSnapshot | null) { const nodes = state?.nodes || []; return nodes.length ? Math.max(...nodes.map((node) => node.position.x + node.width)) + 80 : 0; }