@nodetool-ai/kernel 0.7.0-rc.11

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 (53) hide show
  1. package/dist/actor.d.ts +110 -0
  2. package/dist/actor.d.ts.map +1 -0
  3. package/dist/actor.js +430 -0
  4. package/dist/actor.js.map +1 -0
  5. package/dist/channel.d.ts +46 -0
  6. package/dist/channel.d.ts.map +1 -0
  7. package/dist/channel.js +191 -0
  8. package/dist/channel.js.map +1 -0
  9. package/dist/durable-inbox.d.ts +82 -0
  10. package/dist/durable-inbox.d.ts.map +1 -0
  11. package/dist/durable-inbox.js +158 -0
  12. package/dist/durable-inbox.js.map +1 -0
  13. package/dist/graph-utils.d.ts +60 -0
  14. package/dist/graph-utils.d.ts.map +1 -0
  15. package/dist/graph-utils.js +249 -0
  16. package/dist/graph-utils.js.map +1 -0
  17. package/dist/graph.d.ts +153 -0
  18. package/dist/graph.d.ts.map +1 -0
  19. package/dist/graph.js +656 -0
  20. package/dist/graph.js.map +1 -0
  21. package/dist/inbox.d.ts +109 -0
  22. package/dist/inbox.d.ts.map +1 -0
  23. package/dist/inbox.js +310 -0
  24. package/dist/inbox.js.map +1 -0
  25. package/dist/index.d.ts +16 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +16 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/io.d.ts +92 -0
  30. package/dist/io.d.ts.map +1 -0
  31. package/dist/io.js +128 -0
  32. package/dist/io.js.map +1 -0
  33. package/dist/runner.d.ts +221 -0
  34. package/dist/runner.d.ts.map +1 -0
  35. package/dist/runner.js +1018 -0
  36. package/dist/runner.js.map +1 -0
  37. package/dist/suspendable.d.ts +68 -0
  38. package/dist/suspendable.d.ts.map +1 -0
  39. package/dist/suspendable.js +108 -0
  40. package/dist/suspendable.js.map +1 -0
  41. package/dist/trigger-manager.d.ts +107 -0
  42. package/dist/trigger-manager.d.ts.map +1 -0
  43. package/dist/trigger-manager.js +214 -0
  44. package/dist/trigger-manager.js.map +1 -0
  45. package/dist/trigger-wakeup.d.ts +54 -0
  46. package/dist/trigger-wakeup.d.ts.map +1 -0
  47. package/dist/trigger-wakeup.js +99 -0
  48. package/dist/trigger-wakeup.js.map +1 -0
  49. package/dist/trigger.d.ts +57 -0
  50. package/dist/trigger.d.ts.map +1 -0
  51. package/dist/trigger.js +145 -0
  52. package/dist/trigger.js.map +1 -0
  53. package/package.json +49 -0
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Graph utilities for node and edge operations.
3
+ *
4
+ * Port of src/nodetool/workflows/graph_utils.py.
5
+ */
6
+ import { isControlEdge, TypeMetadata } from "@nodetool-ai/protocol";
7
+ /**
8
+ * Find a node by ID or throw.
9
+ */
10
+ export function findNodeOrThrow(graph, nodeId) {
11
+ const node = graph.findNode(nodeId);
12
+ if (!node) {
13
+ throw new Error(`Node with ID ${nodeId} does not exist`);
14
+ }
15
+ return node;
16
+ }
17
+ /**
18
+ * Returns mapping of targetHandle -> source output type string.
19
+ *
20
+ * For each incoming edge to nodeId, looks up the source node's outputs
21
+ * to find the type of the sourceHandle.
22
+ */
23
+ export function getNodeInputTypes(graph, nodeId) {
24
+ const result = {};
25
+ for (const edge of graph.findIncomingEdges(nodeId)) {
26
+ const sourceNode = graph.findNode(edge.source);
27
+ if (sourceNode && sourceNode.outputs) {
28
+ result[edge.targetHandle] = sourceNode.outputs[edge.sourceHandle];
29
+ }
30
+ else {
31
+ result[edge.targetHandle] = undefined;
32
+ }
33
+ }
34
+ return result;
35
+ }
36
+ /**
37
+ * BFS downstream subgraph starting from (nodeId, sourceHandle).
38
+ *
39
+ * Returns the initial edges from nodeId/sourceHandle, plus all
40
+ * reachable downstream nodes and edges.
41
+ */
42
+ export function getDownstreamSubgraph(graph, nodeId, sourceHandle) {
43
+ // Find initial edges from nodeId with matching sourceHandle
44
+ const initialEdges = graph
45
+ .findOutgoingEdges(nodeId)
46
+ .filter((e) => e.sourceHandle === sourceHandle);
47
+ const includedNodeIds = new Set();
48
+ const includedEdges = [];
49
+ // Seed BFS with targets of initial edges
50
+ const queue = [];
51
+ for (const e of initialEdges) {
52
+ includedEdges.push(e);
53
+ includedNodeIds.add(e.target);
54
+ includedNodeIds.add(e.source);
55
+ queue.push(e.target);
56
+ }
57
+ // BFS over outgoing edges
58
+ while (queue.length > 0) {
59
+ const current = queue.shift();
60
+ for (const edge of graph.findOutgoingEdges(current)) {
61
+ includedEdges.push(edge);
62
+ if (!includedNodeIds.has(edge.target)) {
63
+ includedNodeIds.add(edge.target);
64
+ queue.push(edge.target);
65
+ }
66
+ }
67
+ }
68
+ // Collect nodes, skipping any missing
69
+ const nodes = [];
70
+ for (const nid of includedNodeIds) {
71
+ const node = graph.findNode(nid);
72
+ if (node)
73
+ nodes.push(node);
74
+ }
75
+ // Filter edges to those with both endpoints in included set
76
+ const filteredEdges = includedEdges.filter((e) => includedNodeIds.has(e.source) && includedNodeIds.has(e.target));
77
+ return { initialEdges, nodes, edges: filteredEdges };
78
+ }
79
+ // ---------------------------------------------------------------------------
80
+ // Bypass rewriting
81
+ // ---------------------------------------------------------------------------
82
+ /**
83
+ * Returns true if a node is marked as bypassed via ui_properties.bypassed.
84
+ */
85
+ export function isNodeBypassed(node) {
86
+ const ui = node.ui_properties;
87
+ if (!ui || typeof ui !== "object")
88
+ return false;
89
+ return ui.bypassed === true;
90
+ }
91
+ /**
92
+ * Look up the declared type string of a node's output handle.
93
+ */
94
+ function getOutputTypeString(node, handle) {
95
+ if (!node)
96
+ return undefined;
97
+ return node.outputs?.[handle];
98
+ }
99
+ /**
100
+ * Look up the declared type string of a node's input (property) handle.
101
+ *
102
+ * Checks propertyTypes first (the authoritative map after graph load),
103
+ * then falls back to the property value's `type` field for backwards
104
+ * compatibility with raw graph payloads.
105
+ */
106
+ function getInputTypeString(node, handle) {
107
+ if (!node)
108
+ return undefined;
109
+ const fromPropertyTypes = node.propertyTypes?.[handle];
110
+ if (fromPropertyTypes)
111
+ return fromPropertyTypes;
112
+ const props = node.properties;
113
+ if (props) {
114
+ const val = props[handle];
115
+ if (typeof val === "object" && val !== null && "type" in val) {
116
+ const t = val.type;
117
+ if (typeof t === "string")
118
+ return t;
119
+ }
120
+ else if (typeof val === "string") {
121
+ return val;
122
+ }
123
+ }
124
+ return undefined;
125
+ }
126
+ /**
127
+ * Return true when two type strings are compatible according to
128
+ * TypeMetadata rules. Missing type info on either side is treated as
129
+ * compatible ("any") so that bypass still works on dynamic nodes where
130
+ * handle types are not statically declared.
131
+ */
132
+ function typesCompatible(sourceType, targetType) {
133
+ if (!sourceType || !targetType)
134
+ return true;
135
+ try {
136
+ const s = TypeMetadata.fromString(sourceType);
137
+ const t = TypeMetadata.fromString(targetType);
138
+ return s.isCompatibleWith(t);
139
+ }
140
+ catch {
141
+ return true;
142
+ }
143
+ }
144
+ /**
145
+ * Rewrite a graph to route around nodes marked with
146
+ * `ui_properties.bypassed === true`.
147
+ *
148
+ * For each bypassed node and each of its outgoing data edges, we look for
149
+ * an incoming data edge whose source output type is compatible with the
150
+ * outgoing edge's target input type. If one is found, a new edge is
151
+ * created directly from the upstream source to the downstream target,
152
+ * preserving the downstream handle and any edge metadata (id, ui_properties,
153
+ * edge_type). Outgoing edges with no type-compatible input are dropped.
154
+ *
155
+ * When multiple incoming edges would match an outgoing edge, preference is
156
+ * given to an incoming edge whose targetHandle matches the outgoing edge's
157
+ * sourceHandle (name match), falling back to the first type-compatible one.
158
+ *
159
+ * Chained bypassed nodes (A → B(bypassed) → C(bypassed) → D) are handled
160
+ * by iterating until no further bypassed node can be rewritten — each
161
+ * iteration replaces bypassed sources with their already-resolved upstream
162
+ * source so that a chain collapses into a direct edge from the first
163
+ * non-bypassed source to the first non-bypassed target.
164
+ *
165
+ * Control edges (edge_type === "control") touching a bypassed node are
166
+ * dropped: bypassing intentionally removes the node from execution, so
167
+ * control connections to/from it no longer have a meaningful target.
168
+ */
169
+ export function rewriteBypassedNodes(data) {
170
+ const bypassedIds = new Set(data.nodes.filter(isNodeBypassed).map((n) => n.id));
171
+ if (bypassedIds.size === 0) {
172
+ return data;
173
+ }
174
+ const nodeById = new Map(data.nodes.map((n) => [n.id, n]));
175
+ let currentEdges = [...data.edges];
176
+ const processed = new Set();
177
+ // Iterate: process bypassed nodes whose bypassed predecessors have all
178
+ // already been resolved. This guarantees a chain collapses in source→
179
+ // target order.
180
+ let didChange = true;
181
+ while (didChange) {
182
+ didChange = false;
183
+ for (const bypassId of bypassedIds) {
184
+ if (processed.has(bypassId))
185
+ continue;
186
+ const incoming = currentEdges.filter((e) => e.target === bypassId);
187
+ const hasUnprocessedBypassedUpstream = incoming.some((e) => bypassedIds.has(e.source) && !processed.has(e.source));
188
+ if (hasUnprocessedBypassedUpstream)
189
+ continue;
190
+ const incomingData = incoming.filter((e) => !isControlEdge(e));
191
+ const outgoing = currentEdges.filter((e) => e.source === bypassId);
192
+ const outgoingData = outgoing.filter((e) => !isControlEdge(e));
193
+ const reroutedEdges = [];
194
+ for (const outEdge of outgoingData) {
195
+ const bypassNode = nodeById.get(bypassId);
196
+ const bypassOutputType = getOutputTypeString(bypassNode, outEdge.sourceHandle);
197
+ const targetNode = nodeById.get(outEdge.target);
198
+ const targetInputType = getInputTypeString(targetNode, outEdge.targetHandle);
199
+ // Prefer an incoming edge whose target handle matches the
200
+ // outgoing source handle (name-based pairing), then fall back to
201
+ // the first incoming edge whose source output type is compatible
202
+ // with the downstream target's input type.
203
+ const candidatesCompatibleWithDownstream = [];
204
+ const candidatesCompatibleWithBypassOutput = [];
205
+ for (const inEdge of incomingData) {
206
+ const sourceNode = nodeById.get(inEdge.source);
207
+ const sourceOutputType = getOutputTypeString(sourceNode, inEdge.sourceHandle);
208
+ if (!typesCompatible(sourceOutputType, targetInputType))
209
+ continue;
210
+ candidatesCompatibleWithDownstream.push(inEdge);
211
+ if (typesCompatible(sourceOutputType, bypassOutputType)) {
212
+ candidatesCompatibleWithBypassOutput.push(inEdge);
213
+ }
214
+ }
215
+ const preferredCandidates = candidatesCompatibleWithBypassOutput.length > 0
216
+ ? candidatesCompatibleWithBypassOutput
217
+ : candidatesCompatibleWithDownstream;
218
+ const candidates = [];
219
+ for (const candidate of preferredCandidates) {
220
+ if (candidate.targetHandle === outEdge.sourceHandle) {
221
+ candidates.unshift(candidate);
222
+ }
223
+ else {
224
+ candidates.push(candidate);
225
+ }
226
+ }
227
+ const matched = candidates[0];
228
+ if (!matched)
229
+ continue;
230
+ reroutedEdges.push({
231
+ ...outEdge,
232
+ source: matched.source,
233
+ sourceHandle: matched.sourceHandle
234
+ });
235
+ }
236
+ // Drop all edges touching the bypassed node and append the rerouted
237
+ // ones. Control edges attached to the bypassed node are dropped
238
+ // here — bypassing removes the node from execution entirely.
239
+ currentEdges = currentEdges.filter((e) => e.source !== bypassId && e.target !== bypassId);
240
+ currentEdges.push(...reroutedEdges);
241
+ processed.add(bypassId);
242
+ didChange = true;
243
+ }
244
+ }
245
+ // Remove bypassed nodes themselves from the node list.
246
+ const remainingNodes = data.nodes.filter((n) => !bypassedIds.has(n.id));
247
+ return { nodes: remainingNodes, edges: currentEdges };
248
+ }
249
+ //# sourceMappingURL=graph-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-utils.js","sourceRoot":"","sources":["../src/graph-utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAGpE;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,KAAY,EAAE,MAAc;IAC1D,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IACpC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,iBAAiB,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAC/B,KAAY,EACZ,MAAc;IAEd,MAAM,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CAAC;YACxB,SAAS,GAAG,IAAI,CAAC;QACnB,CAAC;IACH,CAAC;IAED,uDAAuD;IACvD,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAExE,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;AACxD,CAAC"}
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Graph model and validation.
3
+ *
4
+ * Port of src/nodetool/workflows/graph.py:
5
+ * - Node/edge lookup with O(1) indexing
6
+ * - Edge type validation
7
+ * - Control edge validation with cycle detection
8
+ * - Topological sort (Kahn's algorithm)
9
+ * - Streaming upstream computation
10
+ */
11
+ import type { Edge, NodeDescriptor, GraphData } from "@nodetool-ai/protocol";
12
+ export interface GraphValidationIssue {
13
+ nodeId?: string;
14
+ nodeType?: string;
15
+ property?: string;
16
+ message: string;
17
+ }
18
+ export declare class GraphValidationError extends Error {
19
+ readonly issues: GraphValidationIssue[];
20
+ constructor(message: string, issues?: GraphValidationIssue[]);
21
+ }
22
+ export interface GraphFromDictOptions {
23
+ skipErrors?: boolean;
24
+ allowUndefinedProperties?: boolean;
25
+ validateNodeType?: (nodeType: string) => boolean;
26
+ }
27
+ export interface ResolvedNodeType {
28
+ nodeType: string;
29
+ propertyTypes?: Record<string, string>;
30
+ outputs?: Record<string, string>;
31
+ isDynamic?: boolean;
32
+ descriptorDefaults?: Partial<NodeDescriptor>;
33
+ }
34
+ export type NodeTypeResolver = ((nodeType: string) => Promise<ResolvedNodeType | null> | ResolvedNodeType | null) | {
35
+ resolveNodeType: (nodeType: string) => Promise<ResolvedNodeType | null> | ResolvedNodeType | null;
36
+ };
37
+ export interface GraphLoadOptions extends Omit<GraphFromDictOptions, "validateNodeType"> {
38
+ resolver: NodeTypeResolver;
39
+ }
40
+ export declare class Graph {
41
+ readonly nodes: ReadonlyArray<NodeDescriptor>;
42
+ readonly edges: ReadonlyArray<Edge>;
43
+ /** O(1) node lookup by id */
44
+ private _nodeIndex;
45
+ /** Edges keyed by target node id */
46
+ private _incomingEdges;
47
+ /** Edges keyed by source node id */
48
+ private _outgoingEdges;
49
+ /** Edges keyed by `${source}:${sourceHandle}` for O(1) lookup */
50
+ private _outgoingByHandle;
51
+ /** Cache: nodes that have streaming upstream */
52
+ private _streamingUpstream;
53
+ constructor(data: GraphData);
54
+ /**
55
+ * Create a Graph from a plain object, validating the input shape.
56
+ * Throws GraphValidationError if nodes or edges are missing or not arrays.
57
+ */
58
+ static fromDict(data: unknown, options?: GraphFromDictOptions): Graph;
59
+ static loadFromDict(data: unknown, options: GraphLoadOptions): Promise<Graph>;
60
+ private _buildIndices;
61
+ /**
62
+ * Auto-detect is_controlled from incoming control edges.
63
+ * In Python, BaseNode.is_controlled() is a runtime method that checks
64
+ * the graph context. In TS, we set the flag on the descriptor so that
65
+ * the actor knows to use _runControlled().
66
+ */
67
+ private _detectControlledNodes;
68
+ findNode(id: string): NodeDescriptor | undefined;
69
+ /**
70
+ * Return all edges where target == nodeId (incoming edges).
71
+ */
72
+ findIncomingEdges(nodeId: string): Edge[];
73
+ /**
74
+ * Return all edges where source == nodeId (outgoing edges).
75
+ */
76
+ findOutgoingEdges(nodeId: string): Edge[];
77
+ /**
78
+ * Return edges matching a specific (source, sourceHandle) pair. O(1) lookup.
79
+ * Mirrors Python's find_edges(source, source_handle).
80
+ */
81
+ findEdges(source: string, sourceHandle: string): Edge[];
82
+ /**
83
+ * Return data (non-control) edges targeting a node.
84
+ */
85
+ findDataEdges(nodeId: string): Edge[];
86
+ getControlEdges(): Edge[];
87
+ getControlEdges(targetId: string): Edge[];
88
+ getControllerNodes(): NodeDescriptor[];
89
+ getControllerNodes(targetId: string): NodeDescriptor[];
90
+ getControlledNodes(): NodeDescriptor[];
91
+ getControlledNodes(sourceId: string): string[];
92
+ /**
93
+ * Return nodes that have no incoming data edges (source nodes).
94
+ */
95
+ inputNodes(): NodeDescriptor[];
96
+ /**
97
+ * Return nodes that have no outgoing data edges (sink nodes).
98
+ */
99
+ outputNodes(): NodeDescriptor[];
100
+ /**
101
+ * Check whether a node has streaming upstream (i.e. is downstream
102
+ * of a streaming-output node via data edges).
103
+ */
104
+ hasStreamingUpstream(nodeId: string): boolean;
105
+ private _computeStreamingUpstream;
106
+ /**
107
+ * Returns nodes grouped into execution levels.
108
+ * Nodes in the same level have no inter-dependencies and can run
109
+ * concurrently. Only data edges define the ordering.
110
+ */
111
+ topologicalSort(parentId?: string | null): NodeDescriptor[][];
112
+ /**
113
+ * Build a JSON Schema object from input nodes (type contains "Input").
114
+ * Each input node contributes a property named after node.name (or node.id).
115
+ */
116
+ getInputSchema(): {
117
+ properties: Record<string, unknown>;
118
+ required: string[];
119
+ };
120
+ /**
121
+ * Build a JSON Schema object from output nodes (type contains "Output").
122
+ */
123
+ getOutputSchema(): {
124
+ properties: Record<string, unknown>;
125
+ required: string[];
126
+ };
127
+ private _buildSchema;
128
+ /**
129
+ * Validate the graph structure.
130
+ * Throws GraphValidationError on problems.
131
+ */
132
+ validate(): void;
133
+ /**
134
+ * Verify that every edge references existing nodes.
135
+ */
136
+ validateEdgeEndpoints(): void;
137
+ /**
138
+ * Validate control edges:
139
+ * - Source and target must exist.
140
+ * - Target handle must be "__control__".
141
+ * - No cycles in control edges.
142
+ */
143
+ validateControlEdges(): void;
144
+ /**
145
+ * Validate type compatibility between connected edge endpoints.
146
+ * For each data edge, checks if the source output type is compatible
147
+ * with the target input type. Compatible means: same type, one is "any",
148
+ * or numeric widening (int -> float).
149
+ */
150
+ validateEdgeTypes(): void;
151
+ private _checkCircularControl;
152
+ }
153
+ //# sourceMappingURL=graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../src/graph.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EACV,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