@statelyai/sdk 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,530 @@
1
+ //#region src/serializeJS.ts
2
+ /**
3
+ * Serializes JavaScript values as JS source code (not JSON).
4
+ * - Unquoted keys for valid identifiers
5
+ * - Single-quoted strings
6
+ * - Omits undefined values
7
+ * - Supports RawCode for verbatim expressions
8
+ */
9
+ const VALID_IDENT = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
10
+ var RawCode = class {
11
+ constructor(code) {
12
+ this.code = code;
13
+ }
14
+ };
15
+ function raw(code) {
16
+ return new RawCode(code);
17
+ }
18
+ function serializeJS(value, indent = 0, step = 2) {
19
+ if (value instanceof RawCode) return value.code;
20
+ if (value === void 0) return "undefined";
21
+ if (value === null) return "null";
22
+ if (typeof value === "string") return `'${escapeString(value)}'`;
23
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
24
+ if (Array.isArray(value)) return serializeArray(value, indent, step);
25
+ if (typeof value === "object") return serializeObject(value, indent, step);
26
+ return String(value);
27
+ }
28
+ function escapeString(s) {
29
+ return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
30
+ }
31
+ function serializeArray(arr, indent, step) {
32
+ const filtered = arr.filter((v) => v !== void 0);
33
+ if (filtered.length === 0) return "[]";
34
+ const inner = indent + step;
35
+ const pad = " ".repeat(inner);
36
+ const closePad = " ".repeat(indent);
37
+ return `[\n${filtered.map((v) => `${pad}${serializeJS(v, inner, step)}`).join(",\n")},\n${closePad}]`;
38
+ }
39
+ function serializeObject(obj, indent, step) {
40
+ const entries = Object.entries(obj).filter(([, v]) => v !== void 0);
41
+ if (entries.length === 0) return "{}";
42
+ const inner = indent + step;
43
+ const pad = " ".repeat(inner);
44
+ const closePad = " ".repeat(indent);
45
+ return `{\n${entries.map(([key, val]) => {
46
+ const k = VALID_IDENT.test(key) ? key : `'${escapeString(key)}'`;
47
+ const serialized = serializeJS(val, inner, step);
48
+ if (val instanceof RawCode && serialized.includes("\n")) return `${pad}${k}: ${indentRawCode(serialized, inner)}`;
49
+ return `${pad}${k}: ${serialized}`;
50
+ }).join(",\n")},\n${closePad}}`;
51
+ }
52
+ function indentRawCode(code, indent) {
53
+ const lines = code.split("\n");
54
+ if (lines.length <= 1) return code;
55
+ const pad = " ".repeat(indent);
56
+ return [lines[0], ...lines.slice(1).map((line) => line.trim() === "" ? "" : `${pad}${line}`)].join("\n");
57
+ }
58
+
59
+ //#endregion
60
+ //#region src/textUtils.ts
61
+ /**
62
+ * Pure string utilities used by codegen.
63
+ */
64
+ /**
65
+ * Removes common leading whitespace from all non-empty lines.
66
+ */
67
+ function dedent(text) {
68
+ const lines = text.split("\n");
69
+ const nonEmptyLines = lines.filter((l) => l.trim().length > 0);
70
+ if (nonEmptyLines.length === 0) return text;
71
+ const minIndent = Math.min(...nonEmptyLines.map((l) => l.match(/^(\s*)/)[1].length));
72
+ if (minIndent === 0) return text;
73
+ return lines.map((l) => l.trim().length > 0 ? l.slice(minIndent) : l).join("\n");
74
+ }
75
+ /**
76
+ * Strips `export default` wrapper from an expression string.
77
+ * E.g. `"export default assign({ ... })"` → `"assign({ ... })"`
78
+ */
79
+ function stripExportDefault(code) {
80
+ const trimmed = code.trim();
81
+ if (trimmed.startsWith("export default ")) return trimmed.slice(15).replace(/;$/, "");
82
+ return trimmed;
83
+ }
84
+
85
+ //#endregion
86
+ //#region src/graphToMachineConfig.ts
87
+ function getNodePath(graph, node) {
88
+ const path = [];
89
+ let currentNode = node;
90
+ while (currentNode) {
91
+ path.unshift(currentNode.data.key);
92
+ currentNode = graph.nodes.find((n) => n.id === currentNode.parentId);
93
+ }
94
+ return path;
95
+ }
96
+ /** Unwrap single-element arrays to a single value (XState single-or-array convention) */
97
+ function singleOrArray(arr) {
98
+ return arr.length === 1 ? arr[0] : arr;
99
+ }
100
+ /** Unwrap each value in a record of arrays */
101
+ function singleOrArrayRecord(record) {
102
+ const result = {};
103
+ for (const [key, arr] of Object.entries(record)) result[key] = singleOrArray(arr);
104
+ return result;
105
+ }
106
+ /**
107
+ * Remove empty arrays/objects/strings, unwrap single-element arrays.
108
+ * Matches studio's simplifyAttributes behavior.
109
+ */
110
+ function simplifyAttributes(obj) {
111
+ for (const key of Object.keys(obj)) {
112
+ const value = obj[key];
113
+ if (value === void 0 || value === null) delete obj[key];
114
+ else if (Array.isArray(value)) {
115
+ if (value.length === 0) delete obj[key];
116
+ else if (value.length === 1) obj[key] = value[0];
117
+ } else if (typeof value === "object" && Object.keys(value).length === 0) delete obj[key];
118
+ else if (typeof value === "string" && value === "") delete obj[key];
119
+ }
120
+ return obj;
121
+ }
122
+ /**
123
+ * Recursively apply simplifyAttributes to a config tree.
124
+ * Cleans up the config and all nested state nodes, transitions, and invokes.
125
+ */
126
+ function deepSimplify(config) {
127
+ for (const mapKey of ["on", "after"]) {
128
+ const map = config[mapKey];
129
+ if (map && typeof map === "object" && !Array.isArray(map)) {
130
+ for (const [, val] of Object.entries(map)) if (val && typeof val === "object" && !Array.isArray(val)) simplifyAttributes(val);
131
+ else if (Array.isArray(val)) {
132
+ for (const item of val) if (item && typeof item === "object") simplifyAttributes(item);
133
+ }
134
+ }
135
+ }
136
+ for (const key of ["always", "onDone"]) {
137
+ const val = config[key];
138
+ if (val && typeof val === "object" && !Array.isArray(val)) simplifyAttributes(val);
139
+ else if (Array.isArray(val)) {
140
+ for (const item of val) if (item && typeof item === "object") simplifyAttributes(item);
141
+ }
142
+ }
143
+ const invoke = config.invoke;
144
+ if (invoke && typeof invoke === "object" && !Array.isArray(invoke)) simplifyAttributes(invoke);
145
+ else if (Array.isArray(invoke)) {
146
+ for (const item of invoke) if (item && typeof item === "object") simplifyAttributes(item);
147
+ }
148
+ simplifyAttributes(config);
149
+ if (config.states && typeof config.states === "object") for (const stateConfig of Object.values(config.states)) deepSimplify(stateConfig);
150
+ return config;
151
+ }
152
+ function buildMeta(meta, color) {
153
+ const result = {};
154
+ if (meta) {
155
+ for (const [k, v] of Object.entries(meta)) if (v !== void 0 && v !== null) result[k] = v;
156
+ }
157
+ if (color) result["@statelyai.color"] = color;
158
+ return Object.keys(result).length > 0 ? result : void 0;
159
+ }
160
+ /** Format event param — supports { type: string } (new) and plain string (legacy) */
161
+ function formatEventParam(event) {
162
+ if (event && typeof event === "object" && "type" in event) return `{ type: '${event.type}' }`;
163
+ if (typeof event === "string") return `{ type: '${event}' }`;
164
+ return "{ type: \"unknown\" }";
165
+ }
166
+ /**
167
+ * Converts a built-in xstate ActionItem to a raw function call expression,
168
+ * or returns a plain { type, params } object for user-defined actions.
169
+ */
170
+ function serializeActionItem(action) {
171
+ const { type, params } = action;
172
+ switch (type) {
173
+ case "xstate.raise": {
174
+ const eventStr = formatEventParam(params?.event);
175
+ const opts = [];
176
+ if (params?.id) opts.push(`id: '${params.id}'`);
177
+ if (params?.delay != null) opts.push(`delay: ${JSON.stringify(params.delay)}`);
178
+ return raw(`raise(${eventStr}${opts.length > 0 ? `, { ${opts.join(", ")} }` : ""})`);
179
+ }
180
+ case "xstate.sendTo": {
181
+ const to = params?.to ? `'${params.to}'` : `'unknown'`;
182
+ const eventStr = formatEventParam(params?.event);
183
+ const opts = [];
184
+ if (params?.id) opts.push(`id: '${params.id}'`);
185
+ if (params?.delay != null) opts.push(`delay: ${JSON.stringify(params.delay)}`);
186
+ return raw(`sendTo(${to}, ${eventStr}${opts.length > 0 ? `, { ${opts.join(", ")} }` : ""})`);
187
+ }
188
+ case "xstate.cancel": return raw(`cancel(${params?.sendId ? `'${params.sendId}'` : `'unknown'`})`);
189
+ case "xstate.emit": return raw(`emit(${formatEventParam(params?.event)})`);
190
+ case "xstate.spawnChild": {
191
+ const src = params?.src ? `'${params.src}'` : `'unknown'`;
192
+ const opts = [];
193
+ if (params?.id) opts.push(`id: '${params.id}'`);
194
+ if (params?.systemId) opts.push(`systemId: '${params.systemId}'`);
195
+ return raw(`spawnChild(${src}${opts.length > 0 ? `, { ${opts.join(", ")} }` : ""})`);
196
+ }
197
+ case "xstate.stopChild": return raw(`stopChild(${params?.actorRef ? `'${params.actorRef}'` : `'unknown'`})`);
198
+ case "xstate.log": return raw(`log(${(params?.label ? `'${params.label}'` : void 0) ?? ""})`);
199
+ case "xstate.assign": return raw(`assign({ /* ... */ })`);
200
+ default:
201
+ if (action.code) return raw(stripExportDefault(action.code));
202
+ if (!params || Object.keys(params).length === 0) return { type };
203
+ return {
204
+ type,
205
+ params
206
+ };
207
+ }
208
+ }
209
+ /** Normalize tag to string — handles both `string` and `{ name: string }` */
210
+ function tagToString(tag) {
211
+ return typeof tag === "string" ? tag : tag.name;
212
+ }
213
+ function graphToMachineConfig(graph, options = {}) {
214
+ const { showDescriptions = true, showMeta = true } = options;
215
+ const config = {};
216
+ function getNodeConfig(node) {
217
+ const nodeConfig = node.parentId ? {} : config;
218
+ const nodePath = getNodePath(graph, node);
219
+ const resolvedNodeId = !node.parentId ? node.id : nodePath.join(".") === node.id ? void 0 : node.id;
220
+ const initialNode = node.data.initialId ? graph.nodes.find((n) => n.id === node.data.initialId) : null;
221
+ const nodeType = [
222
+ "final",
223
+ "history",
224
+ "parallel"
225
+ ].includes(node.data.type) ? node.data.type : void 0;
226
+ const tags = node.data.tags;
227
+ Object.assign(nodeConfig, {
228
+ id: resolvedNodeId,
229
+ type: nodeType === "normal" ? void 0 : nodeType ?? void 0,
230
+ initial: initialNode?.data.key,
231
+ ...node.data.entry?.length ? { entry: node.data.entry.map(serializeActionItem) } : void 0,
232
+ ...node.data.exit?.length ? { exit: node.data.exit.map(serializeActionItem) } : void 0,
233
+ ...node.data.invokes?.length ? { invoke: node.data.invokes.map((inv) => ({
234
+ src: inv.src,
235
+ id: inv.id,
236
+ ...inv.input ? { input: inv.input } : void 0
237
+ })) } : void 0,
238
+ ...tags?.length ? { tags: tags.map(tagToString) } : void 0,
239
+ ...showDescriptions && node.data.description ? { description: node.data.description } : void 0,
240
+ ...showMeta && node.data.meta && Object.keys(node.data.meta).length > 0 ? { meta: node.data.meta } : void 0,
241
+ ...node.data.history ? { history: node.data.history } : void 0
242
+ });
243
+ const childNodes = graph.nodes.filter((n) => n.parentId === node.id && !n.data.temp);
244
+ const edges = graph.edges.filter((edge) => edge.sourceId === node.id && !edge.data.temp);
245
+ if (edges.length > 0) {
246
+ const on = {};
247
+ const after = {};
248
+ const alwaysArr = [];
249
+ const onDone = {};
250
+ const invokeMap = {};
251
+ for (const edge of edges) {
252
+ const targetNode = graph.nodes.find((n) => n.id === edge.targetId);
253
+ const resolvedTarget = targetNode ? targetNode.parentId === node.parentId ? targetNode.data.key : `#${targetNode.id}` : "";
254
+ const type = edge.data.eventType ?? "";
255
+ const transitionMeta = showMeta ? buildMeta(edge.data.meta, edge.data.color) : void 0;
256
+ const transitionObject = {
257
+ target: `${resolvedTarget}`,
258
+ ...edge.data.transitionType === "reenter" ? { reenter: true } : void 0,
259
+ guard: edge.data.guard ? edge.data.guard.code ? raw(stripExportDefault(edge.data.guard.code)) : edge.data.guard : void 0,
260
+ actions: edge.data.actions?.length ? edge.data.actions.map(serializeActionItem) : void 0,
261
+ description: edge.data.description ?? void 0,
262
+ ...transitionMeta ? { meta: transitionMeta } : void 0
263
+ };
264
+ if (type === "") alwaysArr.push(transitionObject);
265
+ else if (type.startsWith("xstate.after.")) {
266
+ const delayKey = type.slice(13).split(".")[0];
267
+ if (!after[delayKey]) after[delayKey] = [];
268
+ after[delayKey].push(transitionObject);
269
+ } else if (type === "*") {
270
+ if (!on["*"]) on["*"] = [];
271
+ on["*"].push(transitionObject);
272
+ } else if (type.startsWith("xstate.done.state")) {
273
+ if (!onDone[type]) onDone[type] = [];
274
+ onDone[type].push(transitionObject);
275
+ } else if (type.startsWith("xstate.done.actor.")) {
276
+ const actorId = type.slice(18);
277
+ invokeMap[actorId] = {
278
+ ...invokeMap[actorId],
279
+ onDone: (invokeMap[actorId]?.onDone ?? []).concat(transitionObject)
280
+ };
281
+ } else if (type.startsWith("xstate.error.actor.")) {
282
+ const actorId = type.slice(19);
283
+ invokeMap[actorId] = {
284
+ ...invokeMap[actorId],
285
+ onError: (invokeMap[actorId]?.onError ?? []).concat(transitionObject)
286
+ };
287
+ } else {
288
+ if (!on[type]) on[type] = [];
289
+ on[type].push(transitionObject);
290
+ }
291
+ }
292
+ if (Object.keys(on).length > 0) nodeConfig.on = singleOrArrayRecord(on);
293
+ if (Object.keys(after).length > 0) nodeConfig.after = singleOrArrayRecord(after);
294
+ if (alwaysArr.length > 0) nodeConfig.always = singleOrArray(alwaysArr);
295
+ if (Object.keys(onDone).length > 0) nodeConfig.onDone = singleOrArray(Object.values(onDone).flat());
296
+ if (Object.keys(invokeMap).length > 0 && Array.isArray(nodeConfig.invoke)) nodeConfig.invoke = nodeConfig.invoke.map((inv) => {
297
+ const mapped = invokeMap[inv.id];
298
+ if (!mapped) return inv;
299
+ return {
300
+ ...inv,
301
+ ...mapped.onDone ? { onDone: singleOrArray(mapped.onDone) } : void 0,
302
+ ...mapped.onError ? { onError: singleOrArray(mapped.onError) } : void 0
303
+ };
304
+ });
305
+ }
306
+ if (childNodes.length > 0) {
307
+ nodeConfig.states = {};
308
+ for (const childState of childNodes) nodeConfig.states[childState.data.key] = getNodeConfig(childState);
309
+ }
310
+ return nodeConfig;
311
+ }
312
+ const rootNode = graph.nodes.find((node) => node.data.parentId === null || node.parentId == null);
313
+ if (!rootNode) throw new Error("No root node found");
314
+ getNodeConfig(rootNode);
315
+ return deepSimplify(config);
316
+ }
317
+
318
+ //#endregion
319
+ //#region src/jsonSchemaToTSType.ts
320
+ function jsonSchemaToTSType(schema) {
321
+ if (!schema) return "unknown";
322
+ if (schema.enum) return schema.enum.map((v) => typeof v === "string" ? `'${v}'` : String(v)).join(" | ");
323
+ if (schema.const !== void 0) return typeof schema.const === "string" ? `'${schema.const}'` : String(schema.const);
324
+ if (schema.oneOf) return schema.oneOf.map((s) => jsonSchemaToTSType(resolveDef(s))).join(" | ");
325
+ if (schema.anyOf) return schema.anyOf.map((s) => jsonSchemaToTSType(resolveDef(s))).join(" | ");
326
+ if (schema.allOf) return schema.allOf.map((s) => jsonSchemaToTSType(resolveDef(s))).join(" & ");
327
+ if (schema.type === "array") return `Array<${jsonSchemaToTSType(resolveDef(schema.items))}>`;
328
+ if (Array.isArray(schema.type)) return schema.type.map((t) => primitiveType(t)).join(" | ");
329
+ if (schema.type === "object" || schema.properties) return objectType(schema);
330
+ if (schema.type) return primitiveType(schema.type);
331
+ return "unknown";
332
+ }
333
+ function primitiveType(type) {
334
+ switch (type) {
335
+ case "string": return "string";
336
+ case "number":
337
+ case "integer": return "number";
338
+ case "boolean": return "boolean";
339
+ case "null": return "null";
340
+ default: return "unknown";
341
+ }
342
+ }
343
+ function objectType(schema) {
344
+ const props = schema.properties;
345
+ if (!props || Object.keys(props).length === 0) {
346
+ if (schema.additionalProperties) return `Record<string, ${jsonSchemaToTSType(resolveDef(schema.additionalProperties))}>`;
347
+ return "Record<string, unknown>";
348
+ }
349
+ const required = new Set(schema.required ?? []);
350
+ return `{ ${Object.entries(props).map(([key, def]) => {
351
+ const type = jsonSchemaToTSType(resolveDef(def));
352
+ return `${key}${required.has(key) ? "" : "?"}: ${type}`;
353
+ }).join("; ")} }`;
354
+ }
355
+ function resolveDef(def) {
356
+ if (!def) return void 0;
357
+ if (Array.isArray(def)) return resolveDef(def[0]);
358
+ if (typeof def === "boolean") return void 0;
359
+ return def;
360
+ }
361
+ /**
362
+ * Build an inline context type string from the schemas.context record.
363
+ * Each key is a context property name, value is its JSONSchema7.
364
+ */
365
+ function contextSchemaToTSType(context) {
366
+ if (!context || Object.keys(context).length === 0) return null;
367
+ return `{ ${Object.entries(context).map(([key, schema]) => {
368
+ return `${key}: ${jsonSchemaToTSType(schema)}`;
369
+ }).join("; ")} }`;
370
+ }
371
+ /**
372
+ * Build an event union type from the schemas.events record.
373
+ * Each key is the event type name, value describes the payload properties.
374
+ */
375
+ function eventsSchemaToTSType(events) {
376
+ if (!events || Object.keys(events).length === 0) return null;
377
+ return Object.entries(events).map(([eventType, schema]) => {
378
+ const props = schema.properties;
379
+ if (!props || Object.keys(props).length === 0) return `{ type: '${eventType}' }`;
380
+ const required = new Set(schema.required ?? []);
381
+ const extraProps = Object.entries(props).filter(([k]) => k !== "type").map(([key, def]) => {
382
+ const type = jsonSchemaToTSType(resolveDef(def));
383
+ return `${key}${required.has(key) ? "" : "?"}: ${type}`;
384
+ });
385
+ if (extraProps.length === 0) return `{ type: '${eventType}' }`;
386
+ return `{ type: '${eventType}'; ${extraProps.join("; ")} }`;
387
+ }).join("\n | ");
388
+ }
389
+
390
+ //#endregion
391
+ //#region src/graphToXStateTS.ts
392
+ function graphToXStateTS(graph, options = {}) {
393
+ const { exportStyle = "named", ...configOptions } = options;
394
+ const schemas = graph.data.schemas;
395
+ const impls = graph.data.implementations;
396
+ const hasSchemas = !!(schemas && (schemas.context || schemas.events || schemas.input || schemas.output));
397
+ const hasActions = !!impls?.actions.length;
398
+ const hasGuards = !!impls?.guards.length;
399
+ const hasActors = !!impls?.actors.length;
400
+ const hasDelays = !!impls?.delays.length;
401
+ const hasSetup = hasSchemas || hasActions || hasGuards || hasActors || hasDelays;
402
+ const builtInActions = getUsedBuiltInActions(graph);
403
+ const imports = buildImports({
404
+ hasSetup,
405
+ hasActors,
406
+ actors: impls?.actors ?? [],
407
+ builtInActions
408
+ });
409
+ const machineConfig = graphToMachineConfig(graph, configOptions);
410
+ let machineExpr;
411
+ if (hasSetup) {
412
+ const setupObj = buildSetupObject(schemas, impls);
413
+ const configStr = serializeJS(machineConfig, 2);
414
+ machineExpr = `setup(${serializeJS(setupObj, 0)}).createMachine(${configStr})`;
415
+ } else machineExpr = `createMachine(${serializeJS(machineConfig, 0)})`;
416
+ const lines = [];
417
+ lines.push(imports);
418
+ lines.push("");
419
+ lines.push(`const machine = ${machineExpr};`);
420
+ if (exportStyle === "named") {
421
+ lines.push("");
422
+ lines.push("export { machine };");
423
+ } else if (exportStyle === "default") {
424
+ lines.push("");
425
+ lines.push("export default machine;");
426
+ }
427
+ return lines.join("\n") + "\n";
428
+ }
429
+ /** Map from xstate action type to the import name */
430
+ const BUILTIN_ACTION_IMPORTS = {
431
+ "xstate.raise": "raise",
432
+ "xstate.sendTo": "sendTo",
433
+ "xstate.cancel": "cancel",
434
+ "xstate.emit": "emit",
435
+ "xstate.spawnChild": "spawnChild",
436
+ "xstate.stopChild": "stopChild",
437
+ "xstate.log": "log",
438
+ "xstate.assign": "assign"
439
+ };
440
+ /** Check inline action/entry/exit code for references to xstate builtins */
441
+ function scanInlineCodeForBuiltins(code, used) {
442
+ const expr = stripExportDefault(code);
443
+ for (const importName of Object.values(BUILTIN_ACTION_IMPORTS)) if (new RegExp(`\\b${importName}\\b`).test(expr)) used.add(importName);
444
+ }
445
+ function getUsedBuiltInActions(graph) {
446
+ const used = /* @__PURE__ */ new Set();
447
+ for (const node of graph.nodes) for (const action of [...node.data.entry ?? [], ...node.data.exit ?? []]) {
448
+ const imp = BUILTIN_ACTION_IMPORTS[action.type];
449
+ if (imp) used.add(imp);
450
+ if (action.code) scanInlineCodeForBuiltins(action.code, used);
451
+ }
452
+ for (const edge of graph.edges) for (const action of edge.data.actions ?? []) {
453
+ const imp = BUILTIN_ACTION_IMPORTS[action.type];
454
+ if (imp) used.add(imp);
455
+ if (action.code) scanInlineCodeForBuiltins(action.code, used);
456
+ }
457
+ return used;
458
+ }
459
+ function buildImports({ hasSetup, hasActors, actors, builtInActions }) {
460
+ const xstateImports = [];
461
+ if (hasSetup) xstateImports.push("setup");
462
+ else xstateImports.push("createMachine");
463
+ for (const name of builtInActions) xstateImports.push(name);
464
+ if (hasActors) {
465
+ if (actors.some((a) => a.code?.body)) xstateImports.push("fromPromise");
466
+ }
467
+ return `import { ${xstateImports.join(", ")} } from 'xstate';`;
468
+ }
469
+ function buildSetupObject(schemas, impls) {
470
+ const setup = {};
471
+ const types = buildTypesBlock(schemas);
472
+ if (types) setup.types = types;
473
+ if (impls?.actions.length) setup.actions = buildActionsBlock(impls.actions);
474
+ if (impls?.guards.length) setup.guards = buildGuardsBlock(impls.guards);
475
+ if (impls?.actors.length) setup.actors = buildActorsBlock(impls.actors);
476
+ if (impls?.delays.length) setup.delays = buildDelaysBlock(impls.delays);
477
+ return setup;
478
+ }
479
+ function buildTypesBlock(schemas) {
480
+ if (!schemas) return void 0;
481
+ const types = {};
482
+ const contextType = contextSchemaToTSType(schemas.context);
483
+ if (contextType) types.context = raw(`{} as ${contextType}`);
484
+ const eventsType = eventsSchemaToTSType(schemas.events);
485
+ if (eventsType) types.events = raw(`{} as\n | ${eventsType}`);
486
+ if (schemas.input) types.input = raw(`{} as ${jsonSchemaToTSType(schemas.input)}`);
487
+ if (schemas.output) types.output = raw(`{} as ${jsonSchemaToTSType(schemas.output)}`);
488
+ return Object.keys(types).length > 0 ? types : void 0;
489
+ }
490
+ function hasSchemaProperties(schema) {
491
+ if (!schema) return false;
492
+ if (schema.type === "object" || schema.properties) {
493
+ const props = schema.properties;
494
+ return !!props && Object.keys(props).length > 0;
495
+ }
496
+ return true;
497
+ }
498
+ function buildActionsBlock(actions) {
499
+ const block = {};
500
+ for (const action of actions) if (action.code?.body) {
501
+ const params = hasSchemaProperties(action.paramsSchema) ? `, params: ${jsonSchemaToTSType(action.paramsSchema)}` : "";
502
+ block[action.name] = raw(`function ({ context, event }${params ? params : ""}) {\n ${dedent(action.code.body)}\n}`);
503
+ } else block[action.name] = raw(`function ({ context, event }) {\n // TODO: implement ${action.name}\n}`);
504
+ return block;
505
+ }
506
+ function buildGuardsBlock(guards) {
507
+ const block = {};
508
+ for (const guard of guards) if (guard.code?.body) {
509
+ const params = hasSchemaProperties(guard.paramsSchema) ? `, params: ${jsonSchemaToTSType(guard.paramsSchema)}` : "";
510
+ block[guard.name] = raw(`function ({ context, event }${params ? params : ""}) {\n ${dedent(guard.code.body)}\n}`);
511
+ } else block[guard.name] = raw(`function ({ context, event }) {\n // TODO: implement ${guard.name}\n return false;\n}`);
512
+ return block;
513
+ }
514
+ function buildActorsBlock(actors) {
515
+ const block = {};
516
+ for (const actor of actors) if (actor.code?.body) {
517
+ const inputType = hasSchemaProperties(actor.inputSchema) ? `: ${jsonSchemaToTSType(actor.inputSchema)}` : "";
518
+ block[actor.name] = raw(`fromPromise(async ({ input }${inputType ? `: { input${inputType} }` : ""}) => {\n ${dedent(actor.code.body)}\n})`);
519
+ } else block[actor.name] = raw(`fromPromise(async ({ input }) => {\n // TODO: implement ${actor.name}\n})`);
520
+ return block;
521
+ }
522
+ function buildDelaysBlock(delays) {
523
+ const block = {};
524
+ for (const delay of delays) if (delay.code?.body) block[delay.name] = raw(`function ({ context, event }) {\n ${dedent(delay.code.body)}\n}`);
525
+ else block[delay.name] = raw(`function () {\n // TODO: implement ${delay.name}\n return 1000;\n}`);
526
+ return block;
527
+ }
528
+
529
+ //#endregion
530
+ export { graphToMachineConfig as a, serializeJS as c, jsonSchemaToTSType as i, contextSchemaToTSType as n, RawCode as o, eventsSchemaToTSType as r, raw as s, graphToXStateTS as t };