@bian-womp/spark-graph 0.3.31 → 0.3.32

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.
Files changed (37) hide show
  1. package/lib/cjs/index.cjs +194 -95
  2. package/lib/cjs/index.cjs.map +1 -1
  3. package/lib/cjs/src/builder/GraphBuilder.d.ts +0 -1
  4. package/lib/cjs/src/builder/GraphBuilder.d.ts.map +1 -1
  5. package/lib/cjs/src/builder/Registry.d.ts +6 -0
  6. package/lib/cjs/src/builder/Registry.d.ts.map +1 -1
  7. package/lib/cjs/src/core/type-utils.d.ts +11 -0
  8. package/lib/cjs/src/core/type-utils.d.ts.map +1 -1
  9. package/lib/cjs/src/index.d.ts +1 -1
  10. package/lib/cjs/src/index.d.ts.map +1 -1
  11. package/lib/cjs/src/misc/utils/merge.d.ts.map +1 -1
  12. package/lib/cjs/src/runtime/components/Graph.d.ts +1 -2
  13. package/lib/cjs/src/runtime/components/Graph.d.ts.map +1 -1
  14. package/lib/cjs/src/runtime/components/HandleResolver.d.ts.map +1 -1
  15. package/lib/cjs/src/runtime/components/graph-utils.d.ts +4 -4
  16. package/lib/cjs/src/runtime/components/graph-utils.d.ts.map +1 -1
  17. package/lib/cjs/src/runtime/components/types.d.ts +1 -2
  18. package/lib/cjs/src/runtime/components/types.d.ts.map +1 -1
  19. package/lib/esm/index.js +194 -96
  20. package/lib/esm/index.js.map +1 -1
  21. package/lib/esm/src/builder/GraphBuilder.d.ts +0 -1
  22. package/lib/esm/src/builder/GraphBuilder.d.ts.map +1 -1
  23. package/lib/esm/src/builder/Registry.d.ts +6 -0
  24. package/lib/esm/src/builder/Registry.d.ts.map +1 -1
  25. package/lib/esm/src/core/type-utils.d.ts +11 -0
  26. package/lib/esm/src/core/type-utils.d.ts.map +1 -1
  27. package/lib/esm/src/index.d.ts +1 -1
  28. package/lib/esm/src/index.d.ts.map +1 -1
  29. package/lib/esm/src/misc/utils/merge.d.ts.map +1 -1
  30. package/lib/esm/src/runtime/components/Graph.d.ts +1 -2
  31. package/lib/esm/src/runtime/components/Graph.d.ts.map +1 -1
  32. package/lib/esm/src/runtime/components/HandleResolver.d.ts.map +1 -1
  33. package/lib/esm/src/runtime/components/graph-utils.d.ts +4 -4
  34. package/lib/esm/src/runtime/components/graph-utils.d.ts.map +1 -1
  35. package/lib/esm/src/runtime/components/types.d.ts +1 -2
  36. package/lib/esm/src/runtime/components/types.d.ts.map +1 -1
  37. package/package.json +2 -2
package/lib/cjs/index.cjs CHANGED
@@ -18,19 +18,42 @@ function getTypedOutputValue(v) {
18
18
  return v.__spark_value;
19
19
  return v;
20
20
  }
21
- function getInputTypeId(inputs, handle) {
21
+ /**
22
+ * Get the full declared type(s) for an input handle (supports union types)
23
+ * Returns the typeId as-is: string for single type, string[] for union types
24
+ */
25
+ function getInputDeclaredTypes(inputs, handle) {
22
26
  const v = inputs ? inputs[handle] : undefined;
23
27
  if (!v)
24
28
  return undefined;
25
- return typeof v === "string" ? v : v.typeId;
29
+ if (typeof v === "string")
30
+ return v;
31
+ if (Array.isArray(v))
32
+ return v;
33
+ return v.typeId;
34
+ }
35
+ /**
36
+ * Get the primary (first) type ID for an input handle.
37
+ * For union types, returns the first type in the array.
38
+ * This maintains backward compatibility for code that expects a single type.
39
+ */
40
+ function getInputTypeId(inputs, handle) {
41
+ const decl = getInputDeclaredTypes(inputs, handle);
42
+ if (!decl)
43
+ return undefined;
44
+ return Array.isArray(decl) ? decl[0] : decl;
26
45
  }
27
46
  function isInputPrivate(inputs, handle) {
28
47
  const v = inputs ? inputs[handle] : undefined;
29
- return !!(v && typeof v === "object" && v.private);
48
+ if (!v || typeof v === "string" || Array.isArray(v))
49
+ return false;
50
+ // At this point, v must be an object with optional private/metadata fields
51
+ return !!(typeof v === "object" && v !== null && "private" in v && v.private);
30
52
  }
31
53
  /**
32
54
  * Merge two InputHandleDescriptor values, with dynamic taking precedence.
33
55
  * If both have metadata, merge the metadata objects (dynamic overrides static).
56
+ * Supports union types (arrays) in both static and dynamic descriptors.
34
57
  */
35
58
  function mergeInputHandleDescriptors(staticDesc, dynamicDesc) {
36
59
  // If only one exists, return it
@@ -38,12 +61,17 @@ function mergeInputHandleDescriptors(staticDesc, dynamicDesc) {
38
61
  return dynamicDesc;
39
62
  if (!dynamicDesc)
40
63
  return staticDesc;
41
- // If both are strings, dynamic wins
42
- if (typeof staticDesc === "string" && typeof dynamicDesc === "string") {
64
+ // If both are primitive (string or array), dynamic wins
65
+ if ((typeof staticDesc === "string" || Array.isArray(staticDesc)) &&
66
+ (typeof dynamicDesc === "string" || Array.isArray(dynamicDesc))) {
43
67
  return dynamicDesc;
44
68
  }
45
- const staticObj = typeof staticDesc === "string" ? { typeId: staticDesc } : staticDesc;
46
- const dynamicObj = typeof dynamicDesc === "string" ? { typeId: dynamicDesc } : dynamicDesc;
69
+ const staticObj = typeof staticDesc === "string" || Array.isArray(staticDesc)
70
+ ? { typeId: staticDesc }
71
+ : staticDesc;
72
+ const dynamicObj = typeof dynamicDesc === "string" || Array.isArray(dynamicDesc)
73
+ ? { typeId: dynamicDesc }
74
+ : dynamicDesc;
47
75
  // Merge: dynamic takes precedence, but merge metadata objects
48
76
  const merged = {
49
77
  typeId: dynamicObj.typeId ?? staticObj.typeId,
@@ -64,7 +92,7 @@ function mergeInputHandleDescriptors(staticDesc, dynamicDesc) {
64
92
  */
65
93
  function getInputHandleMetadata(inputs, handle) {
66
94
  const v = inputs ? inputs[handle] : undefined;
67
- if (!v || typeof v === "string")
95
+ if (!v || typeof v === "string" || Array.isArray(v))
68
96
  return undefined;
69
97
  return v.metadata;
70
98
  }
@@ -259,7 +287,11 @@ class Registry {
259
287
  if (cached)
260
288
  return cached;
261
289
  if (fromTypeId === toTypeId) {
262
- const res = { kind: "sync", convert: (v) => v };
290
+ const res = {
291
+ kind: "sync",
292
+ convert: (v) => v,
293
+ cost: { edges: 0, async: 0 },
294
+ };
263
295
  this.resolvedCache.set(cacheKey, res);
264
296
  return res;
265
297
  }
@@ -269,6 +301,7 @@ class Registry {
269
301
  const res = {
270
302
  kind: "sync",
271
303
  convert: directSync.convert,
304
+ cost: { edges: 1, async: 0 },
272
305
  };
273
306
  this.resolvedCache.set(cacheKey, res);
274
307
  return res;
@@ -278,6 +311,7 @@ class Registry {
278
311
  const res = {
279
312
  kind: "async",
280
313
  convertAsync: directAsync.convertAsync,
314
+ cost: { edges: 1, async: 1 },
281
315
  };
282
316
  this.resolvedCache.set(cacheKey, res);
283
317
  return res;
@@ -334,6 +368,10 @@ class Registry {
334
368
  const cur = queue.shift();
335
369
  if (cur.node === toTypeId) {
336
370
  // Compose
371
+ const cost = {
372
+ edges: cur.cost.edges,
373
+ async: cur.cost.async,
374
+ };
337
375
  const hasAsync = cur.path.some((s) => s.kind === "async");
338
376
  if (!hasAsync) {
339
377
  const convert = (value) => {
@@ -344,7 +382,11 @@ class Registry {
344
382
  }
345
383
  return acc;
346
384
  };
347
- const res = { kind: "sync", convert };
385
+ const res = {
386
+ kind: "sync",
387
+ convert,
388
+ cost,
389
+ };
348
390
  this.resolvedCache.set(cacheKey, res);
349
391
  return res;
350
392
  }
@@ -361,7 +403,11 @@ class Registry {
361
403
  }
362
404
  return acc;
363
405
  };
364
- const res = { kind: "async", convertAsync };
406
+ const res = {
407
+ kind: "async",
408
+ convertAsync,
409
+ cost,
410
+ };
365
411
  this.resolvedCache.set(cacheKey, res);
366
412
  return res;
367
413
  }
@@ -818,9 +864,6 @@ class Graph {
818
864
  const edge = this.edges.find((e) => e.id === edgeId);
819
865
  if (!edge)
820
866
  return;
821
- if (updates.effectiveTypeId !== undefined) {
822
- edge.effectiveTypeId = updates.effectiveTypeId;
823
- }
824
867
  if (updates.dstDeclared !== undefined) {
825
868
  edge.dstDeclared = updates.dstDeclared;
826
869
  }
@@ -1414,14 +1457,13 @@ function buildEdges(def, registry, resolvedByNode) {
1414
1457
  return def.edges.map((e) => {
1415
1458
  const srcNode = def.nodes.find((n) => n.nodeId === e.source.nodeId);
1416
1459
  const dstNode = def.nodes.find((n) => n.nodeId === e.target.nodeId);
1417
- const { srcDeclared, dstDeclared, effectiveTypeId } = extractEdgeTypes(e.source.nodeId, e.source.handle, e.target.nodeId, e.target.handle, resolvedByNode, e.typeId);
1460
+ const { srcDeclared, dstDeclared } = extractEdgeTypes(e.source.nodeId, e.source.handle, e.target.nodeId, e.target.handle, resolvedByNode);
1418
1461
  const { convert, convertAsync } = buildEdgeConverters(srcDeclared, dstDeclared, registry, `buildEdges: ${srcNode?.typeId || ""}.${e.source.nodeId}.${e.source.handle} -> ${dstNode?.typeId || ""}.${e.target.nodeId}.${e.target.handle}`);
1419
1462
  return {
1420
1463
  id: e.id,
1421
1464
  source: { ...e.source },
1422
1465
  target: { ...e.target },
1423
1466
  typeId: e.typeId, // Preserve original (may be undefined)
1424
- effectiveTypeId, // Always present
1425
1467
  convert,
1426
1468
  convertAsync,
1427
1469
  srcUnionTypes: Array.isArray(srcDeclared) ? [...srcDeclared] : undefined,
@@ -1433,37 +1475,79 @@ function buildEdges(def, registry, resolvedByNode) {
1433
1475
  /**
1434
1476
  * Extract edge type information from resolved handles
1435
1477
  * Used by both buildEdges and updateNodeHandles to avoid duplication
1478
+ * Now supports union types on both source (output) and destination (input) handles
1436
1479
  */
1437
- function extractEdgeTypes(sourceNodeId, sourceHandle, targetNodeId, targetHandle, resolvedByNode, explicitTypeId) {
1480
+ function extractEdgeTypes(sourceNodeId, sourceHandle, targetNodeId, targetHandle, resolvedByNode) {
1438
1481
  const srcResolved = resolvedByNode.get(sourceNodeId);
1439
1482
  const dstResolved = resolvedByNode.get(targetNodeId);
1440
1483
  const srcDeclared = srcResolved
1441
1484
  ? srcResolved.outputs[sourceHandle]
1442
1485
  : undefined;
1443
1486
  const dstDeclared = dstResolved
1444
- ? getInputTypeId(dstResolved.inputs, targetHandle)
1487
+ ? getInputDeclaredTypes(dstResolved.inputs, targetHandle)
1445
1488
  : undefined;
1446
- let effectiveTypeId = explicitTypeId;
1447
- if (!effectiveTypeId) {
1448
- // Infer if not explicitly set
1449
- effectiveTypeId = Array.isArray(srcDeclared) ? srcDeclared[0] : srcDeclared;
1450
- }
1451
1489
  return {
1452
1490
  srcDeclared,
1453
1491
  dstDeclared,
1454
- effectiveTypeId: effectiveTypeId ?? "untyped",
1455
1492
  };
1456
1493
  }
1457
1494
  // Static helper: build edge converters for type coercion
1495
+ // Now supports union types on both source (output) and destination (input) handles
1458
1496
  function buildEdgeConverters(srcDeclared, dstDeclared, registry, edgeLabel) {
1459
1497
  if (!dstDeclared || !srcDeclared) {
1460
1498
  return {};
1461
1499
  }
1462
- const isUnion = Array.isArray(srcDeclared);
1463
- const srcTypes = isUnion ? srcDeclared : [srcDeclared];
1464
- // Helper to get the coercion for a specific type
1465
- const getCoercion = (typeId) => {
1466
- return registry.resolveCoercion(typeId, dstDeclared);
1500
+ const isSrcUnion = Array.isArray(srcDeclared);
1501
+ const srcTypes = isSrcUnion ? srcDeclared : [srcDeclared];
1502
+ const isDstUnion = Array.isArray(dstDeclared);
1503
+ const dstTypes = isDstUnion ? dstDeclared : [dstDeclared];
1504
+ // Helper to compare coercion costs (sync preferred, then fewer steps)
1505
+ const compareCost = (a, b) => {
1506
+ // Prefer sync over async
1507
+ if (a.kind === "sync" && b.kind === "async")
1508
+ return -1;
1509
+ if (a.kind === "async" && b.kind === "sync")
1510
+ return 1;
1511
+ // If same kind, prefer fewer edges
1512
+ if (a.cost.edges !== b.cost.edges)
1513
+ return a.cost.edges - b.cost.edges;
1514
+ // If same edges, prefer fewer async steps
1515
+ return a.cost.async - b.cost.async;
1516
+ };
1517
+ // Helper to find the best coercion from a source type to any destination type
1518
+ const getCoercion = (srcTypeId) => {
1519
+ const candidates = [];
1520
+ // Try all destination types and collect valid coercions
1521
+ for (const dstTypeId of dstTypes) {
1522
+ const coercion = registry.resolveCoercion(srcTypeId, dstTypeId);
1523
+ if (coercion) {
1524
+ candidates.push({
1525
+ dstType: dstTypeId,
1526
+ coercion,
1527
+ });
1528
+ }
1529
+ }
1530
+ if (candidates.length === 0)
1531
+ return null;
1532
+ // Select best by cost: sync preferred, then fewer edges, then fewer async steps
1533
+ const best = candidates.reduce((best, cur) => {
1534
+ return compareCost(cur.coercion, best.coercion) < 0 ? cur : best;
1535
+ });
1536
+ if (best.coercion.kind === "sync") {
1537
+ return {
1538
+ kind: "sync",
1539
+ convert: best.coercion.convert,
1540
+ dstType: best.dstType,
1541
+ };
1542
+ }
1543
+ else {
1544
+ return {
1545
+ kind: "async",
1546
+ convert: (v) => v, // placeholder, not used for async
1547
+ convertAsync: best.coercion.convertAsync,
1548
+ dstType: best.dstType,
1549
+ };
1550
+ }
1467
1551
  };
1468
1552
  // Resolve coercions for all source types
1469
1553
  const coercions = srcTypes.map(getCoercion);
@@ -1472,7 +1556,7 @@ function buildEdgeConverters(srcDeclared, dstDeclared, registry, edgeLabel) {
1472
1556
  const extractPayload = (v) => {
1473
1557
  const typeId = getTypedOutputTypeId(v);
1474
1558
  const payload = getTypedOutputValue(v);
1475
- if (isUnion) {
1559
+ if (isSrcUnion) {
1476
1560
  if (!typeId) {
1477
1561
  throw new Error(`Typed output required for union source (${edgeLabel}); allowed: ${srcTypes.join("|")}`);
1478
1562
  }
@@ -1486,17 +1570,27 @@ function buildEdgeConverters(srcDeclared, dstDeclared, registry, edgeLabel) {
1486
1570
  }
1487
1571
  return { typeId: typeId || srcTypes[0], payload };
1488
1572
  };
1573
+ const wrapIfDstUnion = (dstType, val) => {
1574
+ if (!isDstUnion || !dstType)
1575
+ return val;
1576
+ return typed(dstType, val);
1577
+ };
1489
1578
  if (hasAsync) {
1490
1579
  return {
1491
1580
  convertAsync: async (v, signal) => {
1492
1581
  const { typeId, payload } = extractPayload(v);
1493
1582
  const res = getCoercion(typeId);
1494
- if (!res)
1495
- return payload;
1496
- if (res.kind === "async") {
1497
- return await res.convertAsync(payload, signal);
1583
+ if (!res) {
1584
+ const fallbackType = isDstUnion && typeId && dstTypes.includes(typeId)
1585
+ ? typeId
1586
+ : undefined;
1587
+ return wrapIfDstUnion(fallbackType, payload);
1498
1588
  }
1499
- return res.convert(payload);
1589
+ if (res.kind === "async" && res.convertAsync) {
1590
+ const converted = await res.convertAsync(payload, signal);
1591
+ return wrapIfDstUnion(res.dstType, converted);
1592
+ }
1593
+ return wrapIfDstUnion(res.dstType, res.convert(payload));
1500
1594
  },
1501
1595
  };
1502
1596
  }
@@ -1509,12 +1603,17 @@ function buildEdgeConverters(srcDeclared, dstDeclared, registry, edgeLabel) {
1509
1603
  convert: (v) => {
1510
1604
  const { typeId, payload } = extractPayload(v);
1511
1605
  const res = getCoercion(typeId);
1512
- if (!res)
1513
- return payload;
1606
+ if (!res) {
1607
+ const fallbackType = isDstUnion && typeId && dstTypes.includes(typeId)
1608
+ ? typeId
1609
+ : undefined;
1610
+ return wrapIfDstUnion(fallbackType, payload);
1611
+ }
1514
1612
  if (res.kind === "async") {
1515
1613
  throw new Error(`Async coercion required but convert used (${edgeLabel})`);
1516
1614
  }
1517
- return res.convert(payload);
1615
+ const converted = res.convert(payload);
1616
+ return wrapIfDstUnion(res.dstType, converted);
1518
1617
  },
1519
1618
  };
1520
1619
  }
@@ -1617,12 +1716,11 @@ class HandleResolver {
1617
1716
  const dstNode = this.graph.getNode(e.target.nodeId);
1618
1717
  const oldDstDeclared = e.dstDeclared;
1619
1718
  // Extract edge types using shared helper (handles both source and target updates)
1620
- const { srcDeclared, dstDeclared, effectiveTypeId } = extractEdgeTypes(e.source.nodeId, e.source.handle, e.target.nodeId, e.target.handle, resolvedByNode, e.typeId);
1719
+ const { srcDeclared, dstDeclared } = extractEdgeTypes(e.source.nodeId, e.source.handle, e.target.nodeId, e.target.handle, resolvedByNode);
1621
1720
  // Update converters
1622
1721
  const conv = buildEdgeConverters(srcDeclared, dstDeclared, registry, `updateNodeHandles: ${srcNode?.typeId || ""}.${e.source.nodeId}.${e.source.handle} -> ${dstNode?.typeId || ""}.${e.target.nodeId}.${e.target.handle}`);
1623
1722
  // Update edge properties via Graph
1624
1723
  this.graph.updateEdgeProperties(e.id, {
1625
- effectiveTypeId: !e.typeId ? effectiveTypeId : undefined,
1626
1724
  dstDeclared,
1627
1725
  srcUnionTypes: Array.isArray(srcDeclared)
1628
1726
  ? [...srcDeclared]
@@ -3468,20 +3566,12 @@ class GraphBuilder {
3468
3566
  return { inputs, outputs };
3469
3567
  };
3470
3568
  const normOut = (decl) => Array.isArray(decl) ? decl : decl ? [decl] : [];
3471
- const inferEdgeType = (srcDeclared, dstDeclared, explicit) => {
3472
- if (explicit)
3473
- return explicit;
3474
- if (Array.isArray(srcDeclared) && dstDeclared)
3475
- return dstDeclared;
3476
- if (srcDeclared)
3477
- return Array.isArray(srcDeclared) ? srcDeclared[0] : srcDeclared;
3478
- return undefined;
3479
- };
3480
3569
  const canFlow = (from, to) => {
3481
3570
  if (!to || !from)
3482
3571
  return true;
3483
- const arr = Array.isArray(from) ? from : [from];
3484
- return arr.every((s) => s === to || !!this.registry.canCoerce(s, to));
3572
+ const srcTypes = Array.isArray(from) ? from : [from];
3573
+ const dstTypes = Array.isArray(to) ? to : [to];
3574
+ return srcTypes.some((s) => dstTypes.some((t) => s === t || !!this.registry.canCoerce(s, t)));
3485
3575
  };
3486
3576
  // Helper to validate enum value
3487
3577
  const validateEnumValue = (typeId, value, nodeId, handle) => {
@@ -3592,37 +3682,29 @@ class GraphBuilder {
3592
3682
  };
3593
3683
  const dstEff = effByNodeId.get(e.target.nodeId) || {
3594
3684
  inputs: {}};
3595
- const _srcDeclared = srcNode
3596
- ? srcEff.outputs[e.source.handle]
3597
- : undefined;
3598
- const _dstDeclared = dstNode
3599
- ? getInputTypeId(dstEff.inputs, e.target.handle)
3600
- : undefined;
3601
- // Effective edge type
3602
- const effectiveTypeId = inferEdgeType(_srcDeclared, _dstDeclared, e.typeId);
3603
- const type = effectiveTypeId
3604
- ? this.registry.types.get(effectiveTypeId)
3605
- : undefined;
3606
- if (!type) {
3607
- pushIssue("error", "TYPE_MISSING", `Edge ${e.id} type missing or unknown`, {
3608
- edgeId: e.id,
3609
- });
3685
+ // Validate explicit type if provided
3686
+ if (e.typeId) {
3687
+ const type = this.registry.types.get(e.typeId);
3688
+ if (!type) {
3689
+ pushIssue("error", "TYPE_MISSING", `Edge ${e.id} explicit type ${e.typeId} is missing or unknown`, { edgeId: e.id });
3690
+ }
3610
3691
  }
3611
3692
  if (srcNode) {
3612
3693
  if (!(e.source.handle in srcEff.outputs)) {
3613
3694
  pushIssue("error", "OUTPUT_MISSING", `Edge ${e.id} source output ${e.source.handle} missing on ${srcNode.typeId}`, { edgeId: e.id, nodeId: srcNode.nodeId, output: e.source.handle });
3614
3695
  }
3615
3696
  const declaredArr = normOut(srcEff.outputs[e.source.handle]);
3616
- if (declaredArr.length > 0 &&
3617
- effectiveTypeId &&
3618
- !canFlow(declaredArr, effectiveTypeId)) {
3619
- pushIssue("error", "TYPE_MISMATCH_OUTPUT", `Edge ${e.id} type ${effectiveTypeId} mismatches source output ${srcNode.typeId}.${e.source.handle} (${declaredArr.join("|")}) and no coercion exists`, {
3620
- edgeId: e.id,
3621
- nodeId: srcNode.nodeId,
3622
- output: e.source.handle,
3623
- declared: declaredArr.join("|"),
3624
- effectiveTypeId,
3625
- });
3697
+ if (declaredArr.length > 0) {
3698
+ // Check if explicit type matches source output
3699
+ if (e.typeId && !canFlow(declaredArr, e.typeId)) {
3700
+ pushIssue("error", "TYPE_MISMATCH_OUTPUT", `Edge ${e.id} explicit type ${e.typeId} mismatches source output ${srcNode.typeId}.${e.source.handle} (${declaredArr.join("|")}) and no coercion exists`, {
3701
+ edgeId: e.id,
3702
+ nodeId: srcNode.nodeId,
3703
+ output: e.source.handle,
3704
+ declared: declaredArr.join("|"),
3705
+ typeId: e.typeId,
3706
+ });
3707
+ }
3626
3708
  }
3627
3709
  }
3628
3710
  if (dstNode) {
@@ -3633,30 +3715,30 @@ class GraphBuilder {
3633
3715
  if (isInputPrivate(dstEff.inputs, e.target.handle)) {
3634
3716
  pushIssue("error", "INPUT_PRIVATE", `Edge ${e.id} targets private input ${dstNode.typeId}.${e.target.handle}`, { edgeId: e.id, nodeId: dstNode.nodeId, input: e.target.handle });
3635
3717
  }
3636
- const declaredIn = getInputTypeId(dstEff.inputs, e.target.handle);
3637
- if (declaredIn && effectiveTypeId) {
3718
+ const declaredIn = getInputDeclaredTypes(dstEff.inputs, e.target.handle);
3719
+ const declaredInArr = normOut(declaredIn);
3720
+ if (declaredInArr.length > 0) {
3638
3721
  if (srcNode) {
3639
3722
  const srcDeclared = srcEff.outputs[e.source.handle];
3640
- const srcArr = normOut(srcDeclared).length
3641
- ? normOut(srcDeclared)
3642
- : [effectiveTypeId];
3643
- if (!canFlow(srcArr, declaredIn)) {
3644
- pushIssue("error", "TYPE_MISMATCH_INPUT", `Edge ${e.id} output type ${srcArr.join("|")} not convertible to target input ${dstNode.typeId}.${e.target.handle} (${declaredIn})`, {
3723
+ const srcArr = normOut(srcDeclared);
3724
+ if (srcArr.length > 0 && !canFlow(srcArr, declaredInArr)) {
3725
+ pushIssue("error", "TYPE_MISMATCH_INPUT", `Edge ${e.id} output type ${srcArr.join("|")} not convertible to target input ${dstNode.typeId}.${e.target.handle} (${declaredInArr.join("|")})`, {
3645
3726
  edgeId: e.id,
3646
3727
  nodeId: dstNode.nodeId,
3647
3728
  input: e.target.handle,
3648
- declared: declaredIn,
3649
- effectiveTypeId: srcArr.join("|"),
3729
+ declared: declaredInArr.join("|"),
3730
+ typeId: srcArr.join("|"),
3650
3731
  });
3651
3732
  }
3652
3733
  }
3653
- else if (!canFlow(effectiveTypeId, declaredIn)) {
3654
- pushIssue("error", "TYPE_MISMATCH_INPUT", `Edge ${e.id} type ${effectiveTypeId} mismatches target input ${dstNode.typeId}.${e.target.handle} (${declaredIn}) and no coercion exists`, {
3734
+ else if (e.typeId && !canFlow([e.typeId], declaredInArr)) {
3735
+ // External source with explicit type
3736
+ pushIssue("error", "TYPE_MISMATCH_INPUT", `Edge ${e.id} explicit type ${e.typeId} mismatches target input ${dstNode.typeId}.${e.target.handle} (${declaredInArr.join("|")}) and no coercion exists`, {
3655
3737
  edgeId: e.id,
3656
3738
  nodeId: dstNode.nodeId,
3657
3739
  input: e.target.handle,
3658
- declared: declaredIn,
3659
- effectiveTypeId,
3740
+ declared: declaredInArr.join("|"),
3741
+ typeId: e.typeId,
3660
3742
  });
3661
3743
  }
3662
3744
  }
@@ -5592,9 +5674,17 @@ function buildTypeMaps(def) {
5592
5674
  const nodeOutputTypes = new Map();
5593
5675
  if (node.resolvedHandles?.inputs) {
5594
5676
  for (const [handleId, handleDesc] of Object.entries(node.resolvedHandles.inputs)) {
5595
- const typeId = typeof handleDesc === "string"
5596
- ? handleDesc
5597
- : handleDesc?.typeId;
5677
+ let typeId;
5678
+ if (typeof handleDesc === "string") {
5679
+ typeId = handleDesc;
5680
+ }
5681
+ else if (Array.isArray(handleDesc)) {
5682
+ typeId = handleDesc[0]; // Use first type for type map (backward compat)
5683
+ }
5684
+ else {
5685
+ const descTypeId = handleDesc.typeId;
5686
+ typeId = Array.isArray(descTypeId) ? descTypeId[0] : descTypeId;
5687
+ }
5598
5688
  if (typeId)
5599
5689
  nodeInputTypes.set(handleId, typeId);
5600
5690
  }
@@ -5617,9 +5707,17 @@ function buildTypeMaps(def) {
5617
5707
  if (!nodeInputTypes.has(handleId) && node.resolvedHandles?.inputs) {
5618
5708
  const inputDesc = node.resolvedHandles.inputs[handleId];
5619
5709
  if (inputDesc) {
5620
- const typeId = typeof inputDesc === "string"
5621
- ? inputDesc
5622
- : inputDesc?.typeId;
5710
+ let typeId;
5711
+ if (typeof inputDesc === "string") {
5712
+ typeId = inputDesc;
5713
+ }
5714
+ else if (Array.isArray(inputDesc)) {
5715
+ typeId = inputDesc[0]; // Use first type for type map (backward compat)
5716
+ }
5717
+ else {
5718
+ const descTypeId = inputDesc.typeId;
5719
+ typeId = Array.isArray(descTypeId) ? descTypeId[0] : descTypeId;
5720
+ }
5623
5721
  if (typeId)
5624
5722
  nodeInputTypes.set(handleId, typeId);
5625
5723
  }
@@ -6078,6 +6176,7 @@ exports.createValidationGraphDef = createValidationGraphDef;
6078
6176
  exports.createValidationGraphRegistry = createValidationGraphRegistry;
6079
6177
  exports.findMatchingPaths = findMatchingPaths;
6080
6178
  exports.generateId = generateId;
6179
+ exports.getInputDeclaredTypes = getInputDeclaredTypes;
6081
6180
  exports.getInputHandleMetadata = getInputHandleMetadata;
6082
6181
  exports.getInputTypeId = getInputTypeId;
6083
6182
  exports.getTypedOutputTypeId = getTypedOutputTypeId;