MCPcopy Create free account
hub / github.com/allocnode/oh-my-sage / validateGraph

Function validateGraph

src/core/tools/base.ts:14–150  ·  view source on GitHub ↗
(graph: Graph)

Source from the content-addressed store, hash-verified

12
13const DUAL_OUTPUT_TYPES = new Set(['deviceGet', 'varGet']);
14
15const STATE_CONDITION_INPUT_TYPES = new Set(['condition', 'logicOr', 'logicAnd', 'logicNot']);
16
17/** Canvas annotations do not participate in execution. */
18const ANNOTATION_NODE_TYPES = new Set(['nop']);
19
20/** Accept both legacy plain text and rich-text inserts. */
21function annotationText(node: GraphNode): string {
22 const contents = (node.cfg as Record<string, unknown> | undefined)?.contents;
23 if (typeof contents === 'string') return contents;
24 if (!Array.isArray(contents)) return '';
25 return contents
26 .map((seg) => {
27 const insert = (seg as Record<string, unknown> | null)?.insert;
28 return typeof insert === 'string' ? insert : '';
29 })
30 .join('');
31}
32
33function targetInfo(target: string): { id: string; port: string } | null {
34 const dotIdx = target.lastIndexOf('.');
35 if (dotIdx === -1) return null;
36 return { id: target.substring(0, dotIdx), port: target.substring(dotIdx + 1) };
37}
38
39function isDeclaredConditionInput(node: GraphNode, port: string): boolean {
40 if (!Object.prototype.hasOwnProperty.call(node.inputs || {}, port)) return false;
41 if (node.type === 'condition') return port === 'condition';
42 if (node.type === 'logicNot') return port === 'input';
43 return (node.type === 'logicOr' || node.type === 'logicAnd') && /^input\d+$/.test(port);
44}
45
46function reachesConditionInput(nodeId: string, nodeMap: Map<string, GraphNode>, visited: Set<string>): boolean {
47 if (visited.has(nodeId)) return false;
48 visited.add(nodeId);
49
50 const node = nodeMap.get(nodeId);
51 if (!node) return false;
52
53 for (const target of node.outputs?.output || []) {
54 const info = targetInfo(target);
55 if (!info) continue;
56 const targetNode = nodeMap.get(info.id);
57 if (!targetNode || !isDeclaredConditionInput(targetNode, info.port)) continue;
58 if (targetNode.type === 'condition') return true;
59 if (reachesConditionInput(targetNode.id, nodeMap, new Set(visited))) return true;
60 }
61
62 return false;
63}
64
65function canAcceptStateCondition(target: string, nodeMap: Map<string, GraphNode>): boolean {
66 const info = targetInfo(target);
67 if (!info) return false;
68
69 const targetNode = nodeMap.get(info.id);
70 if (!targetNode || !STATE_CONDITION_INPUT_TYPES.has(targetNode.type)) return false;
71 if (!isDeclaredConditionInput(targetNode, info.port)) return false;

Callers 4

createGraphFunction · 0.90
updateGraphFunction · 0.90
createCoreToolsFunction · 0.90
registerGraphToolsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected