admin / Synapse-Cortex
publicSelf Hosted ITSM Tool with RBAC/Tenanting and MFA
Synapse-Cortex / Synapse-Cortexv2 / frontend / src / playbook / serialize.ts
1728 B · main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import type { Edge } from '@xyflow/react' import type { PlaybookGraph, PlaybookGraphNode, PlaybookNodeType } from '../api/types' import { makeStartNode, type PlaybookNode } from './types' /** Strips React Flow's runtime-only fields (selected/dragging/measured/ * width/height/positionAbsolute/...) down to exactly what the backend's * app/ai/guardrails.py reads: id, type, data, position. */ export function toGraphJson(nodes: PlaybookNode[], edges: Edge[]): PlaybookGraph { return { nodes: nodes.map((n) => ({ id: n.id, type: (n.type ?? 'command') as PlaybookNodeType, data: n.data, position: n.position, })), edges: edges.map((e) => ({ id: e.id, source: e.source, target: e.target, sourceHandle: e.sourceHandle ?? null, })), } } /** The inverse conversion. A brand-new playbook has no graph yet, so an * empty/missing graph_json falls back to a single Start node rather than * an empty canvas. */ export function fromGraphJson(graph: PlaybookGraph | null | undefined): { nodes: PlaybookNode[]; edges: Edge[] } { if (!graph || !graph.nodes || graph.nodes.length === 0) { return { nodes: [makeStartNode()], edges: [] } } const nodes: PlaybookNode[] = graph.nodes.map((n: PlaybookGraphNode) => ({ id: n.id, type: n.type, position: n.position ?? { x: 0, y: 0 }, data: n.data ?? {}, deletable: n.type !== 'start', })) as PlaybookNode[] const edges: Edge[] = graph.edges.map((e) => ({ id: e.id, source: e.source, target: e.target, sourceHandle: e.sourceHandle ?? undefined, label: e.sourceHandle === 'true' ? 'True' : e.sourceHandle === 'false' ? 'False' : undefined, })) return { nodes, edges } } |