import { computed, Default, generateText, ifElse, NAME, pattern, UI, type VNode, } from "commonfabric"; // ===== Types ===== type SvgDiagramInput = { topic?: string | Default<"">; context?: Record | Default>; }; export type SvgDiagramOutput = { [NAME]: string; [UI]: VNode; topic: string; diagram: string; pending: boolean; }; // ===== Pattern ===== /** * Generates an SVG diagram illustrating relationships, flows, or structures. * Designed as "suggestion fuel" - a lightweight utility pattern for visual * representation of concepts using scalable vector graphics. */ const SvgDiagram = pattern( ({ topic, context }) => { // An empty prompt holds the request back: `generateText` clears its // state and makes no call until one arrives. A diagram with no topic has // nothing to draw, so the model is asked only once a caller names the // subject. const prompt = computed(() => { if (!topic) return ""; return `Create a clear SVG diagram illustrating: ${topic}`; }); const response = generateText({ system: "You create clear, well-structured SVG diagrams. Output a single element with an appropriate viewBox. Use shapes (rect, circle, ellipse), paths, lines, text, and arrows to illustrate concepts. Use readable fonts and clear colors. Output ONLY the SVG element with no surrounding explanation or markdown.", prompt, context, }); return { [NAME]: computed(() => (topic ? `SVG Diagram: ${topic}` : "SVG Diagram")), [UI]: ( {computed(() => topic || "SVG Diagram")} {ifElse( response.pending,
Generating diagram...
, , )} ), topic, diagram: computed(() => response.result || ""), pending: response.pending, }; }, ); export default SvgDiagram;