-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
263 lines (224 loc) · 8.32 KB
/
Copy pathutils.ts
File metadata and controls
263 lines (224 loc) · 8.32 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Utility functions for node validation
import {
DataType,
Flow,
NodeFunction,
NodeFunctionIdWrapper,
NodeParameter,
ReferencePath,
ReferenceValue
} from "@code0-tech/sagittarius-graphql-types";
import ts from "typescript";
import {createSystem, createVirtualTypeScriptEnvironment, VirtualTypeScriptEnvironment} from "@typescript/vfs"
/**
* Result of a node or flow validation.
*/
export interface ValidationResult {
isValid: boolean;
returnType: string;
diagnostics: Array<{
message: string
code: number
severity: "error" | "warning"
nodeId?: NodeFunction["id"]
parameterIndex?: number
}>;
}
/**
* Minimal TypeScript library definitions for the virtual compiler environment.
*/
export const MINIMAL_LIB = `
interface Array<T> {
[n: number]: T;
length: number;
}
interface String { readonly length: number; }
interface Number { }
interface Boolean { }
interface Object { }
interface Function { }
interface CallableFunction extends Function {}
interface NewableFunction extends Function {}
interface IArguments { }
interface RegExp { }
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
`;
/**
* Common configuration for the TypeScript compiler host across different validation/inference tasks.
*/
export function createCompilerHost(
fileName: string,
sourceCode: string
): VirtualTypeScriptEnvironment {
const fsMap = new Map<string, string>()
fsMap.set(fileName, sourceCode)
fsMap.set("lib.codezero.d.ts", MINIMAL_LIB)
const system = createSystem(fsMap)
return createVirtualTypeScriptEnvironment(system, [fileName, "lib.codezero.d.ts"], ts, DEFAULT_COMPILER_OPTIONS)
}
/**
* Common TypeScript compiler options used for validation and type inference.
*/
export const DEFAULT_COMPILER_OPTIONS: ts.CompilerOptions = {
target: ts.ScriptTarget.Latest,
lib: ["lib.codezero.d.ts"],
noEmit: true,
strictNullChecks: true,
};
/**
* Extracts and returns common type and generic declarations from DATA_TYPES.
*/
export function getSharedTypeDeclarations(dataTypes?: DataType[]): string {
const genericDeclarations = Array.from(new Set(dataTypes?.flatMap(dt => dt.genericKeys || [])))
.map(g => `type ${g} = any;`)
.join("\n");
const typeAliasDeclarations = dataTypes?.map(dt =>
`type ${dt.identifier}${dt.genericKeys ? `<${dt.genericKeys.join(",")}>` : ""} = ${dt.type};`
).join("\n");
return `${genericDeclarations}\n${typeAliasDeclarations}`;
}
/**
* Determines the type along a reference path for objects.
* @param value The base value to traverse.
* @param referencePath The path of properties to follow.
* @returns The typeof the final value or 'unknown' if path is broken.
*/
export function getTypeFromReferencePath(value: any, referencePath: ReferencePath[]): string {
let current = value;
for (const ref of referencePath) {
if (current == null) return 'unknown';
if (typeof ref.path === 'string') {
current = current[ref.path];
}
}
return typeof current;
}
/**
* Helper to find a node by ID within the flow structure.
*/
function findNodeById(flow?: Flow, nodeId?: string): NodeFunction | undefined {
const nodes = flow?.nodes;
if (!nodes) return undefined;
if (Array.isArray(nodes)) {
return nodes.find((n: any) => n.id === nodeId);
}
return nodes.nodes?.find((n: any) => n.id === nodeId)!;
}
/**
* Extracts and returns the TypeScript code representation for a NodeParameter.
*/
export function getParameterCode(
param?: NodeParameter,
flow?: Flow,
getNodeValidation?: (flow?: Flow, node?: NodeFunction) => ValidationResult
): string {
const value = param?.value;
if (!value) return 'undefined';
if (value.__typename === "ReferenceValue") {
const refValue = value as ReferenceValue;
const refNode = findNodeById(flow, refValue.nodeFunctionId!);
if (!refNode) return 'undefined';
let refType = getNodeValidation?.(flow, refNode).returnType;
if (refValue.referencePath && refValue.referencePath.length > 0) {
let refVal: any = undefined;
const nodes = refNode.parameters?.nodes;
if (nodes && nodes.length > 0) {
const firstParam = nodes[0];
if (firstParam?.value?.__typename === "LiteralValue") {
refVal = firstParam.value.value;
}
}
refType = getTypeFromReferencePath(refVal, refValue.referencePath);
}
return `({} as ${refType})`;
}
if (value.__typename === "NodeFunctionIdWrapper") {
const wrapperId = (value as NodeFunctionIdWrapper).id;
const refNode = findNodeById(flow, wrapperId!);
if (!refNode) return '(() => undefined)';
const findReturnNode = (node: NodeFunction): NodeFunction | undefined => {
if (node.functionDefinition?.identifier === "std::control::return") {
return node;
}
const nextNode = node.nextNodeId ? findNodeById(flow, node.nextNodeId) : undefined;
return nextNode ? findReturnNode(nextNode) : undefined;
};
const returnNode = findReturnNode(refNode);
if (!returnNode) return '(() => undefined)';
const validation = getNodeValidation?.(flow, returnNode);
return `(() => ({} as ${validation?.returnType}))`;
}
if (value.__typename === "LiteralValue") {
return JSON.stringify(value.value);
}
return 'undefined';
}
/**
* Finds the parent node that initiated this sub-tree via a NodeFunctionIdWrapper.
*/
const getParentScopeNode = (flow?: Flow, currentNodeId?: string): NodeFunction | undefined => {
const nodes = flow?.nodes?.nodes;
if (!nodes) return undefined;
return nodes.find(n =>
n?.parameters?.nodes?.some(p =>
p?.value?.__typename === "NodeFunctionIdWrapper" && p.value.id === currentNodeId
)
)!;
};
/**
* Checks if a target node is reachable (executed before) the current node.
*/
const isNodeReachable = (flow?: Flow, currentNode?: NodeFunction, targetId?: string, visited = new Set<string>()): boolean => {
const currentId = currentNode?.id;
if (!currentId || visited.has(currentId)) return false;
visited.add(currentId);
// Scenario 1: Is the node a predecessor in the same execution chain?
const isPredecessor = (startId: string): boolean => {
const pred = flow?.nodes?.nodes?.find(n => n?.nextNodeId === startId);
if (!pred) return false;
if (pred.id === targetId) return true;
return isPredecessor(pred.id!);
};
if (isPredecessor(currentId)) return true;
// Scenario 2: Nested scope check (e.g., inside a forEach loop)
const parentNode = getParentScopeNode(flow, currentId);
if (parentNode) {
if (parentNode.id === targetId) return true;
return isNodeReachable(flow, parentNode, targetId, visited);
}
return false;
};
/**
* Validates if a reference is accessible from the current node's scope.
*/
export const validateReference = (
flow?: Flow,
currentNode?: NodeFunction,
ref?: ReferenceValue
): { isValid: boolean, error?: string } => {
// Scenario 3: Global flow input
if (!ref?.nodeFunctionId) {
return {isValid: true};
}
// Scenario 2: Parameter input reference (e.g., "item" in CONSUMER)
if (ref.parameterIndex !== undefined && ref.inputIndex !== undefined) {
if (currentNode?.id === ref.nodeFunctionId) return {isValid: true};
let tempParent = getParentScopeNode(flow, currentNode?.id!);
while (tempParent) {
if (tempParent.id === ref.nodeFunctionId) return {isValid: true};
tempParent = getParentScopeNode(flow, tempParent.id!);
}
return {
isValid: false,
error: `Invalid input reference: Node ${currentNode?.id} is not in the scope of Node ${ref.nodeFunctionId}.`
};
}
// Scenario 1: Ordinary return value reference
if (!isNodeReachable(flow, currentNode, ref.nodeFunctionId)) {
return {
isValid: false,
error: `Node ${ref.nodeFunctionId} has not been executed yet or is not visible in this scope.`
};
}
return {isValid: true};
};