@ellyco/agentic 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +560 -0
- package/dist/graphs/graph.d.ts +218 -0
- package/dist/graphs/graph.d.ts.map +1 -0
- package/dist/graphs/graph.js +334 -0
- package/dist/graphs/graph.js.map +1 -0
- package/dist/graphs/index.d.ts +7 -0
- package/dist/graphs/index.d.ts.map +1 -0
- package/dist/graphs/index.js +15 -0
- package/dist/graphs/index.js.map +1 -0
- package/dist/graphs/iterator.d.ts +138 -0
- package/dist/graphs/iterator.d.ts.map +1 -0
- package/dist/graphs/iterator.js +184 -0
- package/dist/graphs/iterator.js.map +1 -0
- package/dist/graphs/merge-state.d.ts +22 -0
- package/dist/graphs/merge-state.d.ts.map +1 -0
- package/dist/graphs/merge-state.js +56 -0
- package/dist/graphs/merge-state.js.map +1 -0
- package/dist/graphs/node-sequence.d.ts +63 -0
- package/dist/graphs/node-sequence.d.ts.map +1 -0
- package/dist/graphs/node-sequence.js +84 -0
- package/dist/graphs/node-sequence.js.map +1 -0
- package/dist/graphs/registry.d.ts +5 -0
- package/dist/graphs/registry.d.ts.map +1 -0
- package/dist/graphs/registry.js +6 -0
- package/dist/graphs/registry.js.map +1 -0
- package/dist/graphs/runtime-context.d.ts +189 -0
- package/dist/graphs/runtime-context.d.ts.map +1 -0
- package/dist/graphs/runtime-context.js +254 -0
- package/dist/graphs/runtime-context.js.map +1 -0
- package/dist/graphs/state-machine.d.ts +105 -0
- package/dist/graphs/state-machine.d.ts.map +1 -0
- package/dist/graphs/state-machine.js +130 -0
- package/dist/graphs/state-machine.js.map +1 -0
- package/dist/graphs/store/base-store.d.ts +90 -0
- package/dist/graphs/store/base-store.d.ts.map +1 -0
- package/dist/graphs/store/base-store.js +50 -0
- package/dist/graphs/store/base-store.js.map +1 -0
- package/dist/graphs/store/sqlite-store.d.ts +88 -0
- package/dist/graphs/store/sqlite-store.d.ts.map +1 -0
- package/dist/graphs/store/sqlite-store.js +109 -0
- package/dist/graphs/store/sqlite-store.js.map +1 -0
- package/dist/graphs/store/stored-run.d.ts +77 -0
- package/dist/graphs/store/stored-run.d.ts.map +1 -0
- package/dist/graphs/store/stored-run.js +88 -0
- package/dist/graphs/store/stored-run.js.map +1 -0
- package/dist/graphs/types.d.ts +15 -0
- package/dist/graphs/types.d.ts.map +1 -0
- package/dist/graphs/types.js +3 -0
- package/dist/graphs/types.js.map +1 -0
- package/dist/messages/index.d.ts +6 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +19 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/messages/message.d.ts +143 -0
- package/dist/messages/message.d.ts.map +1 -0
- package/dist/messages/message.js +172 -0
- package/dist/messages/message.js.map +1 -0
- package/dist/messages/tool.d.ts +160 -0
- package/dist/messages/tool.d.ts.map +1 -0
- package/dist/messages/tool.js +173 -0
- package/dist/messages/tool.js.map +1 -0
- package/dist/models/BaseModel.d.ts +232 -0
- package/dist/models/BaseModel.d.ts.map +1 -0
- package/dist/models/BaseModel.js +247 -0
- package/dist/models/BaseModel.js.map +1 -0
- package/dist/models/BedrockModel.d.ts +112 -0
- package/dist/models/BedrockModel.d.ts.map +1 -0
- package/dist/models/BedrockModel.js +315 -0
- package/dist/models/BedrockModel.js.map +1 -0
- package/dist/models/TestModel.d.ts +135 -0
- package/dist/models/TestModel.d.ts.map +1 -0
- package/dist/models/TestModel.js +191 -0
- package/dist/models/TestModel.js.map +1 -0
- package/dist/nodes/function-node.d.ts +59 -0
- package/dist/nodes/function-node.d.ts.map +1 -0
- package/dist/nodes/function-node.js +72 -0
- package/dist/nodes/function-node.js.map +1 -0
- package/dist/nodes/index.d.ts +4 -0
- package/dist/nodes/index.d.ts.map +1 -0
- package/dist/nodes/index.js +9 -0
- package/dist/nodes/index.js.map +1 -0
- package/dist/nodes/interrupt-node.d.ts +51 -0
- package/dist/nodes/interrupt-node.d.ts.map +1 -0
- package/dist/nodes/interrupt-node.js +65 -0
- package/dist/nodes/interrupt-node.js.map +1 -0
- package/dist/nodes/model-node.d.ts +72 -0
- package/dist/nodes/model-node.d.ts.map +1 -0
- package/dist/nodes/model-node.js +80 -0
- package/dist/nodes/model-node.js.map +1 -0
- package/dist/nodes/types.d.ts +5 -0
- package/dist/nodes/types.d.ts.map +1 -0
- package/dist/nodes/types.js +3 -0
- package/dist/nodes/types.js.map +1 -0
- package/dist/tools.d.ts +65 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +56 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +32 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { StoredRun } from "./store/stored-run";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a layer of execution context within a graph traversal.
|
|
4
|
+
* Layers are stacked when graphs call other graphs (nested execution).
|
|
5
|
+
* Each layer tracks which node is currently executing.
|
|
6
|
+
*
|
|
7
|
+
* @class ContextLayer
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Each nested graph creates a new context layer
|
|
12
|
+
* const layer = runtime.nextLayer();
|
|
13
|
+
* layer.currentNode = "myNode";
|
|
14
|
+
* layer.custom.data = { ... };
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class ContextLayer {
|
|
18
|
+
readonly index: number;
|
|
19
|
+
readonly runtime: RuntimeContext;
|
|
20
|
+
/**
|
|
21
|
+
* The name of the current node being executed
|
|
22
|
+
*/
|
|
23
|
+
currentNode?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Custom data storage for nodes and graphs to share information
|
|
26
|
+
*/
|
|
27
|
+
custom: Record<string, any>;
|
|
28
|
+
/**
|
|
29
|
+
* Gets a unique identifier for this layer based on its position in the stack.
|
|
30
|
+
* Format: "ROOT" for the root layer, or "PARENT_ID.CURRENT_NODE" for nested layers.
|
|
31
|
+
*
|
|
32
|
+
* @returns {string} Unique identifier for this context layer
|
|
33
|
+
*/
|
|
34
|
+
get id(): string;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new context layer.
|
|
37
|
+
*
|
|
38
|
+
* @param {number} index - The position of this layer in the context stack
|
|
39
|
+
* @param {RuntimeContext} runtime - Reference to the parent runtime context
|
|
40
|
+
*/
|
|
41
|
+
constructor(index: number, runtime: RuntimeContext);
|
|
42
|
+
/**
|
|
43
|
+
* Creates and returns the next layer for nested execution.
|
|
44
|
+
*
|
|
45
|
+
* @returns {ContextLayer} A new child context layer
|
|
46
|
+
*/
|
|
47
|
+
nextLayer(): ContextLayer;
|
|
48
|
+
/**
|
|
49
|
+
* Marks this layer as done executing.
|
|
50
|
+
* Removes it from the stack unless the runtime is interrupted.
|
|
51
|
+
*/
|
|
52
|
+
done(): void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Manages runtime state across graph execution and nested subgraphs.
|
|
56
|
+
* Maintains a stack of context layers for tracking nested execution.
|
|
57
|
+
* Handles interruption, resumption, and cursor management for checkpointing.
|
|
58
|
+
*
|
|
59
|
+
* @class RuntimeContext
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const runtime = new RuntimeContext("run-123");
|
|
64
|
+
*
|
|
65
|
+
* // Track execution through nodes
|
|
66
|
+
* const layer = runtime.nextLayer();
|
|
67
|
+
* layer.currentNode = "node-1";
|
|
68
|
+
*
|
|
69
|
+
* // Handle interruption
|
|
70
|
+
* runtime.markInterrupted("Waiting for user input");
|
|
71
|
+
*
|
|
72
|
+
* // Later, wrap cursor for storage
|
|
73
|
+
* const cursor = runtime.wrapCursor(); // "node-1"
|
|
74
|
+
*
|
|
75
|
+
* // Restore from checkpoint
|
|
76
|
+
* const runtime2 = new RuntimeContext("run-123");
|
|
77
|
+
* runtime2.unwrapCursor(cursor);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare class RuntimeContext {
|
|
81
|
+
readonly runId: string;
|
|
82
|
+
readonly storedRun?: StoredRun | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Stack of context layers for nested graph execution
|
|
85
|
+
*/
|
|
86
|
+
private readonly contextLayers;
|
|
87
|
+
/**
|
|
88
|
+
* Current index in the context layer stack
|
|
89
|
+
*/
|
|
90
|
+
private currentLayer;
|
|
91
|
+
/**
|
|
92
|
+
* Whether execution has been interrupted
|
|
93
|
+
*/
|
|
94
|
+
private _interrupted;
|
|
95
|
+
/**
|
|
96
|
+
* Message describing why execution was interrupted
|
|
97
|
+
*/
|
|
98
|
+
private _exitMessage;
|
|
99
|
+
/**
|
|
100
|
+
* Whether currently resuming from an interruption
|
|
101
|
+
*/
|
|
102
|
+
private _resuming;
|
|
103
|
+
/**
|
|
104
|
+
* Gets whether the runtime is currently interrupted.
|
|
105
|
+
*
|
|
106
|
+
* @returns {boolean} True if interrupted, false otherwise
|
|
107
|
+
*/
|
|
108
|
+
get interrupted(): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Gets the message associated with an interruption.
|
|
111
|
+
*
|
|
112
|
+
* @returns {string} The exit message (empty if not interrupted)
|
|
113
|
+
*/
|
|
114
|
+
get exitMessage(): string;
|
|
115
|
+
/**
|
|
116
|
+
* Gets whether currently resuming from an interruption.
|
|
117
|
+
*
|
|
118
|
+
* @returns {boolean} True if resuming, false otherwise
|
|
119
|
+
*/
|
|
120
|
+
get resuming(): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Creates a new runtime context.
|
|
123
|
+
*
|
|
124
|
+
* @param {string} runId - Unique identifier for this execution run
|
|
125
|
+
* @param {StoredRun} [storedRun] - Optional stored run for persistence
|
|
126
|
+
*/
|
|
127
|
+
constructor(runId: string, storedRun?: StoredRun | undefined);
|
|
128
|
+
/**
|
|
129
|
+
* Restores execution state from a cursor (checkpoint).
|
|
130
|
+
* Sets up the context layer stack based on the cursor path.
|
|
131
|
+
* Marks the runtime as resuming.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} cursor - Cursor string encoding the path through nested contexts
|
|
134
|
+
* Format: "node1.node2.node3" where each segment is a node name
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* runtime.unwrapCursor("start.process.end");
|
|
139
|
+
* // Sets up 3 context layers with currentNode set appropriately
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
unwrapCursor(cursor: string): void;
|
|
143
|
+
/**
|
|
144
|
+
* Encodes the current execution path into a cursor for checkpointing.
|
|
145
|
+
* The cursor can later be used with unwrapCursor() to resume execution.
|
|
146
|
+
*
|
|
147
|
+
* @returns {string} Cursor encoding the current context path
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const cursor = runtime.wrapCursor(); // "start.process.end"
|
|
152
|
+
* // Later restore with: runtime.unwrapCursor(cursor);
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
wrapCursor(): string;
|
|
156
|
+
/**
|
|
157
|
+
* Retrieves a context layer by its index.
|
|
158
|
+
*
|
|
159
|
+
* @param {number} index - The layer index to retrieve
|
|
160
|
+
* @returns {ContextLayer} The context layer at the specified index
|
|
161
|
+
*/
|
|
162
|
+
getLayer(index: number): ContextLayer;
|
|
163
|
+
/**
|
|
164
|
+
* Gets or creates the next context layer for nested execution.
|
|
165
|
+
* If a layer already exists at the next depth, returns it.
|
|
166
|
+
* Otherwise, creates a new layer.
|
|
167
|
+
*
|
|
168
|
+
* @returns {ContextLayer} The next context layer
|
|
169
|
+
*/
|
|
170
|
+
nextLayer(): ContextLayer;
|
|
171
|
+
/**
|
|
172
|
+
* Removes the top (most recent) context layer from the stack.
|
|
173
|
+
* Called when a nested graph or layer completes execution.
|
|
174
|
+
*/
|
|
175
|
+
removeTopLayer(): void;
|
|
176
|
+
/**
|
|
177
|
+
* Marks the runtime as interrupted with an optional reason message.
|
|
178
|
+
* Used by nodes to signal that execution should pause.
|
|
179
|
+
*
|
|
180
|
+
* @param {string} [reason] - Optional message explaining the interruption
|
|
181
|
+
*/
|
|
182
|
+
markInterrupted(reason?: string): void;
|
|
183
|
+
/**
|
|
184
|
+
* Clears the resuming flag after resumption is complete.
|
|
185
|
+
* Called after resuming nodes acknowledge they've been resumed.
|
|
186
|
+
*/
|
|
187
|
+
markResumed(): void;
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=runtime-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-context.d.ts","sourceRoot":"","sources":["../../src/graphs/runtime-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C;;;;;;;;;;;;;;GAcG;AACH,qBAAa,YAAY;aAgCD,KAAK,EAAE,MAAM;aACb,OAAO,EAAE,cAAc;IAhC3C;;OAEG;IACI,WAAW,CAAC,EAAE,MAAM,CAAC;IAE5B;;OAEG;IACI,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAM;IAExC;;;;;OAKG;IACH,IAAI,EAAE,IAAI,MAAM,CAMf;IAED;;;;;OAKG;gBAEiB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,cAAc;IAG3C;;;;OAIG;IACH,SAAS,IAAI,YAAY;IAIzB;;;OAGG;IACH,IAAI,IAAI,IAAI;CAKf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,cAAc;aA2DK,KAAK,EAAE,MAAM;aAAkB,SAAS,CAAC,EAAE,SAAS;IA1DhF;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAsB;IAEpD;;OAEG;IACH,OAAO,CAAC,YAAY,CAAc;IAElC;;OAEG;IACH,OAAO,CAAC,YAAY,CAAS;IAE7B;;OAEG;IACH,OAAO,CAAC,YAAY,CAAM;IAE1B;;OAEG;IACH,OAAO,CAAC,SAAS,CAAS;IAE1B;;;;OAIG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;;;OAIG;IACH,IAAI,WAAW,IAAI,MAAM,CAExB;IAED;;;;OAIG;IACH,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED;;;;;OAKG;gBACyB,KAAK,EAAE,MAAM,EAAkB,SAAS,CAAC,EAAE,SAAS,YAAA;IAEhF;;;;;;;;;;;;;OAaG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAelC;;;;;;;;;;;OAWG;IACH,UAAU,IAAI,MAAM;IAQpB;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;IAIrC;;;;;;OAMG;IACH,SAAS,IAAI,YAAY;IAgBzB;;;OAGG;IACH,cAAc,IAAI,IAAI;IAKtB;;;;;OAKG;IACH,eAAe,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAKtC;;;OAGG;IACH,WAAW,IAAI,IAAI;CAGtB"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RuntimeContext = exports.ContextLayer = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Represents a layer of execution context within a graph traversal.
|
|
6
|
+
* Layers are stacked when graphs call other graphs (nested execution).
|
|
7
|
+
* Each layer tracks which node is currently executing.
|
|
8
|
+
*
|
|
9
|
+
* @class ContextLayer
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Each nested graph creates a new context layer
|
|
14
|
+
* const layer = runtime.nextLayer();
|
|
15
|
+
* layer.currentNode = "myNode";
|
|
16
|
+
* layer.custom.data = { ... };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
class ContextLayer {
|
|
20
|
+
index;
|
|
21
|
+
runtime;
|
|
22
|
+
/**
|
|
23
|
+
* The name of the current node being executed
|
|
24
|
+
*/
|
|
25
|
+
currentNode;
|
|
26
|
+
/**
|
|
27
|
+
* Custom data storage for nodes and graphs to share information
|
|
28
|
+
*/
|
|
29
|
+
custom = {};
|
|
30
|
+
/**
|
|
31
|
+
* Gets a unique identifier for this layer based on its position in the stack.
|
|
32
|
+
* Format: "ROOT" for the root layer, or "PARENT_ID.CURRENT_NODE" for nested layers.
|
|
33
|
+
*
|
|
34
|
+
* @returns {string} Unique identifier for this context layer
|
|
35
|
+
*/
|
|
36
|
+
get id() {
|
|
37
|
+
if (this.index === 0) {
|
|
38
|
+
return "ROOT";
|
|
39
|
+
}
|
|
40
|
+
const lastLayer = this.runtime.getLayer(this.index - 1);
|
|
41
|
+
return `${lastLayer.id}.${lastLayer.currentNode}`;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new context layer.
|
|
45
|
+
*
|
|
46
|
+
* @param {number} index - The position of this layer in the context stack
|
|
47
|
+
* @param {RuntimeContext} runtime - Reference to the parent runtime context
|
|
48
|
+
*/
|
|
49
|
+
constructor(index, runtime) {
|
|
50
|
+
this.index = index;
|
|
51
|
+
this.runtime = runtime;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates and returns the next layer for nested execution.
|
|
55
|
+
*
|
|
56
|
+
* @returns {ContextLayer} A new child context layer
|
|
57
|
+
*/
|
|
58
|
+
nextLayer() {
|
|
59
|
+
return this.runtime.nextLayer();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Marks this layer as done executing.
|
|
63
|
+
* Removes it from the stack unless the runtime is interrupted.
|
|
64
|
+
*/
|
|
65
|
+
done() {
|
|
66
|
+
if (!this.runtime.interrupted) {
|
|
67
|
+
this.runtime.removeTopLayer();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.ContextLayer = ContextLayer;
|
|
72
|
+
/**
|
|
73
|
+
* Manages runtime state across graph execution and nested subgraphs.
|
|
74
|
+
* Maintains a stack of context layers for tracking nested execution.
|
|
75
|
+
* Handles interruption, resumption, and cursor management for checkpointing.
|
|
76
|
+
*
|
|
77
|
+
* @class RuntimeContext
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const runtime = new RuntimeContext("run-123");
|
|
82
|
+
*
|
|
83
|
+
* // Track execution through nodes
|
|
84
|
+
* const layer = runtime.nextLayer();
|
|
85
|
+
* layer.currentNode = "node-1";
|
|
86
|
+
*
|
|
87
|
+
* // Handle interruption
|
|
88
|
+
* runtime.markInterrupted("Waiting for user input");
|
|
89
|
+
*
|
|
90
|
+
* // Later, wrap cursor for storage
|
|
91
|
+
* const cursor = runtime.wrapCursor(); // "node-1"
|
|
92
|
+
*
|
|
93
|
+
* // Restore from checkpoint
|
|
94
|
+
* const runtime2 = new RuntimeContext("run-123");
|
|
95
|
+
* runtime2.unwrapCursor(cursor);
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
class RuntimeContext {
|
|
99
|
+
runId;
|
|
100
|
+
storedRun;
|
|
101
|
+
/**
|
|
102
|
+
* Stack of context layers for nested graph execution
|
|
103
|
+
*/
|
|
104
|
+
contextLayers = [];
|
|
105
|
+
/**
|
|
106
|
+
* Current index in the context layer stack
|
|
107
|
+
*/
|
|
108
|
+
currentLayer = -1;
|
|
109
|
+
/**
|
|
110
|
+
* Whether execution has been interrupted
|
|
111
|
+
*/
|
|
112
|
+
_interrupted = false;
|
|
113
|
+
/**
|
|
114
|
+
* Message describing why execution was interrupted
|
|
115
|
+
*/
|
|
116
|
+
_exitMessage = "";
|
|
117
|
+
/**
|
|
118
|
+
* Whether currently resuming from an interruption
|
|
119
|
+
*/
|
|
120
|
+
_resuming = false;
|
|
121
|
+
/**
|
|
122
|
+
* Gets whether the runtime is currently interrupted.
|
|
123
|
+
*
|
|
124
|
+
* @returns {boolean} True if interrupted, false otherwise
|
|
125
|
+
*/
|
|
126
|
+
get interrupted() {
|
|
127
|
+
return this._interrupted;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the message associated with an interruption.
|
|
131
|
+
*
|
|
132
|
+
* @returns {string} The exit message (empty if not interrupted)
|
|
133
|
+
*/
|
|
134
|
+
get exitMessage() {
|
|
135
|
+
return this._exitMessage;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Gets whether currently resuming from an interruption.
|
|
139
|
+
*
|
|
140
|
+
* @returns {boolean} True if resuming, false otherwise
|
|
141
|
+
*/
|
|
142
|
+
get resuming() {
|
|
143
|
+
return this._resuming;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Creates a new runtime context.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} runId - Unique identifier for this execution run
|
|
149
|
+
* @param {StoredRun} [storedRun] - Optional stored run for persistence
|
|
150
|
+
*/
|
|
151
|
+
constructor(runId, storedRun) {
|
|
152
|
+
this.runId = runId;
|
|
153
|
+
this.storedRun = storedRun;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Restores execution state from a cursor (checkpoint).
|
|
157
|
+
* Sets up the context layer stack based on the cursor path.
|
|
158
|
+
* Marks the runtime as resuming.
|
|
159
|
+
*
|
|
160
|
+
* @param {string} cursor - Cursor string encoding the path through nested contexts
|
|
161
|
+
* Format: "node1.node2.node3" where each segment is a node name
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* runtime.unwrapCursor("start.process.end");
|
|
166
|
+
* // Sets up 3 context layers with currentNode set appropriately
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
unwrapCursor(cursor) {
|
|
170
|
+
this._resuming = true;
|
|
171
|
+
const layers = cursor.split(".");
|
|
172
|
+
let idPrefix = "";
|
|
173
|
+
for (const layer of layers) {
|
|
174
|
+
const contextLayer = new ContextLayer(this.contextLayers.length, this);
|
|
175
|
+
contextLayer.currentNode = layer;
|
|
176
|
+
this.contextLayers.push(contextLayer);
|
|
177
|
+
idPrefix = idPrefix.length > 0 ? `${idPrefix}.${layer}` : layer;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Encodes the current execution path into a cursor for checkpointing.
|
|
182
|
+
* The cursor can later be used with unwrapCursor() to resume execution.
|
|
183
|
+
*
|
|
184
|
+
* @returns {string} Cursor encoding the current context path
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const cursor = runtime.wrapCursor(); // "start.process.end"
|
|
189
|
+
* // Later restore with: runtime.unwrapCursor(cursor);
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
wrapCursor() {
|
|
193
|
+
const layerCursors = [];
|
|
194
|
+
for (const layer of this.contextLayers) {
|
|
195
|
+
layerCursors.push(layer.currentNode);
|
|
196
|
+
}
|
|
197
|
+
return layerCursors.join(".");
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Retrieves a context layer by its index.
|
|
201
|
+
*
|
|
202
|
+
* @param {number} index - The layer index to retrieve
|
|
203
|
+
* @returns {ContextLayer} The context layer at the specified index
|
|
204
|
+
*/
|
|
205
|
+
getLayer(index) {
|
|
206
|
+
return this.contextLayers[index];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Gets or creates the next context layer for nested execution.
|
|
210
|
+
* If a layer already exists at the next depth, returns it.
|
|
211
|
+
* Otherwise, creates a new layer.
|
|
212
|
+
*
|
|
213
|
+
* @returns {ContextLayer} The next context layer
|
|
214
|
+
*/
|
|
215
|
+
nextLayer() {
|
|
216
|
+
// already a contextLayer present at this depth
|
|
217
|
+
if (this.currentLayer < this.contextLayers.length - 1) {
|
|
218
|
+
const layer = this.contextLayers[this.currentLayer + 1];
|
|
219
|
+
this.currentLayer++;
|
|
220
|
+
return layer;
|
|
221
|
+
}
|
|
222
|
+
const contextLayer = new ContextLayer(this.contextLayers.length, this);
|
|
223
|
+
this.contextLayers.push(contextLayer);
|
|
224
|
+
this.currentLayer++;
|
|
225
|
+
return contextLayer;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Removes the top (most recent) context layer from the stack.
|
|
229
|
+
* Called when a nested graph or layer completes execution.
|
|
230
|
+
*/
|
|
231
|
+
removeTopLayer() {
|
|
232
|
+
this.contextLayers.pop();
|
|
233
|
+
this.currentLayer--;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Marks the runtime as interrupted with an optional reason message.
|
|
237
|
+
* Used by nodes to signal that execution should pause.
|
|
238
|
+
*
|
|
239
|
+
* @param {string} [reason] - Optional message explaining the interruption
|
|
240
|
+
*/
|
|
241
|
+
markInterrupted(reason) {
|
|
242
|
+
this._interrupted = true;
|
|
243
|
+
this._exitMessage = reason ?? "";
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Clears the resuming flag after resumption is complete.
|
|
247
|
+
* Called after resuming nodes acknowledge they've been resumed.
|
|
248
|
+
*/
|
|
249
|
+
markResumed() {
|
|
250
|
+
this._resuming = false;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
exports.RuntimeContext = RuntimeContext;
|
|
254
|
+
//# sourceMappingURL=runtime-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-context.js","sourceRoot":"","sources":["../../src/graphs/runtime-context.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Graph } from "./graph";
|
|
2
|
+
import { type NodeLike } from "../nodes/types";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/**
|
|
5
|
+
* A general-purpose graph with arbitrary node and edge definitions.
|
|
6
|
+
* Supports both simple edges and conditional edges for complex workflows.
|
|
7
|
+
* Nodes can be added manually and edges can be configured dynamically.
|
|
8
|
+
*
|
|
9
|
+
* @class StateMachine
|
|
10
|
+
* @extends {Graph<T, S>}
|
|
11
|
+
* @template T - The Zod schema for state
|
|
12
|
+
* @template S - The inferred state type from T
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const schema = z.object({
|
|
17
|
+
* status: z.enum(["pending", "processing", "done"]),
|
|
18
|
+
* data: z.string()
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* const sm = new StateMachine(schema);
|
|
22
|
+
* sm.addNode("process", new FunctionNode(...));
|
|
23
|
+
* sm.addNode("validate", new FunctionNode(...));
|
|
24
|
+
*
|
|
25
|
+
* sm.addEdge(START, "process");
|
|
26
|
+
* sm.addConditionalEdge("process", ["validate", END], (state) => {
|
|
27
|
+
* return state.data ? "validate" : END;
|
|
28
|
+
* });
|
|
29
|
+
* sm.addEdge("validate", END);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class StateMachine<T extends z.ZodObject, S extends Record<string, unknown> = z.infer<T>> extends Graph<T, S> {
|
|
33
|
+
/**
|
|
34
|
+
* Adds a node to the state machine.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} name - Unique name for the node (cannot be "start" or "end")
|
|
37
|
+
* @param {NodeLike<S> | Graph<any, S>} node - The node implementation
|
|
38
|
+
* @returns {this} The state machine instance for method chaining
|
|
39
|
+
* @throws {Error} If name is reserved or node already exists
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* sm.addNode("myNode", new FunctionNode((state) => ({ ...state })));
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
addNode(name: string, node: NodeLike<S> | Graph<any, S>): this;
|
|
47
|
+
/**
|
|
48
|
+
* Adds a simple edge from one node to another.
|
|
49
|
+
* The graph will always transition from 'from' to 'to'.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} from - Source node name
|
|
52
|
+
* @param {string} to - Destination node name
|
|
53
|
+
* @returns {this} The state machine instance for method chaining
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* sm.addEdge("start", "nodeA");
|
|
58
|
+
* sm.addEdge("nodeA", "nodeB");
|
|
59
|
+
* sm.addEdge("nodeB", "end");
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
addEdge(from: string, to: string): this;
|
|
63
|
+
/**
|
|
64
|
+
* Adds a conditional edge from one node to multiple possible destinations.
|
|
65
|
+
* A function determines which destination to transition to based on state.
|
|
66
|
+
*
|
|
67
|
+
* @template K - Tuple of possible destination names
|
|
68
|
+
* @param {string} from - Source node name
|
|
69
|
+
* @param {K} to - Tuple of possible destination node names
|
|
70
|
+
* @param {(state: S) => K[number]} func - Function that returns the destination
|
|
71
|
+
* @returns {this} The state machine instance for method chaining
|
|
72
|
+
* @throws {Error} If no destinations are provided
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* sm.addConditionalEdge(
|
|
77
|
+
* "decision",
|
|
78
|
+
* ["path1", "path2", "end"],
|
|
79
|
+
* (state) => {
|
|
80
|
+
* if (state.priority > 5) return "path1";
|
|
81
|
+
* if (state.priority > 2) return "path2";
|
|
82
|
+
* return "end";
|
|
83
|
+
* }
|
|
84
|
+
* );
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
addConditionalEdge<K extends string[]>(from: string, to: K, func: (state: S) => K[number]): this;
|
|
88
|
+
/**
|
|
89
|
+
* StateMachine doesn't transform state, so node state is the same as graph state.
|
|
90
|
+
*
|
|
91
|
+
* @protected
|
|
92
|
+
* @param {S} state - The graph state
|
|
93
|
+
* @returns {S} The same state
|
|
94
|
+
*/
|
|
95
|
+
protected stateToNodeState(state: S): S;
|
|
96
|
+
/**
|
|
97
|
+
* StateMachine doesn't transform state, so node state is the same as graph state.
|
|
98
|
+
*
|
|
99
|
+
* @protected
|
|
100
|
+
* @param {Partial<S>} nodeState - The partial node state
|
|
101
|
+
* @returns {Partial<S>} The same partial state
|
|
102
|
+
*/
|
|
103
|
+
protected nodeStateToState(nodeState: Partial<S>): Partial<S>;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=state-machine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-machine.d.ts","sourceRoot":"","sources":["../../src/graphs/state-machine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,EAAS,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,YAAY,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAE,SAAQ,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;IAChH;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,IAAI;IAW9D;;;;;;;;;;;;;;OAcG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI;IAKvC;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,kBAAkB,CAAC,CAAC,SAAS,MAAM,EAAE,EACjC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,EACL,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,GAC9B,IAAI;IAWP;;;;;;OAMG;IACH,SAAS,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC;IAIvC;;;;;;OAMG;IACH,SAAS,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAGhE"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StateMachine = void 0;
|
|
4
|
+
const graph_1 = require("./graph");
|
|
5
|
+
/**
|
|
6
|
+
* A general-purpose graph with arbitrary node and edge definitions.
|
|
7
|
+
* Supports both simple edges and conditional edges for complex workflows.
|
|
8
|
+
* Nodes can be added manually and edges can be configured dynamically.
|
|
9
|
+
*
|
|
10
|
+
* @class StateMachine
|
|
11
|
+
* @extends {Graph<T, S>}
|
|
12
|
+
* @template T - The Zod schema for state
|
|
13
|
+
* @template S - The inferred state type from T
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const schema = z.object({
|
|
18
|
+
* status: z.enum(["pending", "processing", "done"]),
|
|
19
|
+
* data: z.string()
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const sm = new StateMachine(schema);
|
|
23
|
+
* sm.addNode("process", new FunctionNode(...));
|
|
24
|
+
* sm.addNode("validate", new FunctionNode(...));
|
|
25
|
+
*
|
|
26
|
+
* sm.addEdge(START, "process");
|
|
27
|
+
* sm.addConditionalEdge("process", ["validate", END], (state) => {
|
|
28
|
+
* return state.data ? "validate" : END;
|
|
29
|
+
* });
|
|
30
|
+
* sm.addEdge("validate", END);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
class StateMachine extends graph_1.Graph {
|
|
34
|
+
/**
|
|
35
|
+
* Adds a node to the state machine.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} name - Unique name for the node (cannot be "start" or "end")
|
|
38
|
+
* @param {NodeLike<S> | Graph<any, S>} node - The node implementation
|
|
39
|
+
* @returns {this} The state machine instance for method chaining
|
|
40
|
+
* @throws {Error} If name is reserved or node already exists
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* sm.addNode("myNode", new FunctionNode((state) => ({ ...state })));
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
addNode(name, node) {
|
|
48
|
+
if (name === graph_1.START || name === graph_1.END) {
|
|
49
|
+
throw new Error(`Node ${name} is reserved`);
|
|
50
|
+
}
|
|
51
|
+
if (name in this.nodes) {
|
|
52
|
+
throw new Error(`Node ${name} already exists`);
|
|
53
|
+
}
|
|
54
|
+
this.nodes[name] = node;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Adds a simple edge from one node to another.
|
|
59
|
+
* The graph will always transition from 'from' to 'to'.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} from - Source node name
|
|
62
|
+
* @param {string} to - Destination node name
|
|
63
|
+
* @returns {this} The state machine instance for method chaining
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* sm.addEdge("start", "nodeA");
|
|
68
|
+
* sm.addEdge("nodeA", "nodeB");
|
|
69
|
+
* sm.addEdge("nodeB", "end");
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
addEdge(from, to) {
|
|
73
|
+
this.edges[from] = to;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Adds a conditional edge from one node to multiple possible destinations.
|
|
78
|
+
* A function determines which destination to transition to based on state.
|
|
79
|
+
*
|
|
80
|
+
* @template K - Tuple of possible destination names
|
|
81
|
+
* @param {string} from - Source node name
|
|
82
|
+
* @param {K} to - Tuple of possible destination node names
|
|
83
|
+
* @param {(state: S) => K[number]} func - Function that returns the destination
|
|
84
|
+
* @returns {this} The state machine instance for method chaining
|
|
85
|
+
* @throws {Error} If no destinations are provided
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* sm.addConditionalEdge(
|
|
90
|
+
* "decision",
|
|
91
|
+
* ["path1", "path2", "end"],
|
|
92
|
+
* (state) => {
|
|
93
|
+
* if (state.priority > 5) return "path1";
|
|
94
|
+
* if (state.priority > 2) return "path2";
|
|
95
|
+
* return "end";
|
|
96
|
+
* }
|
|
97
|
+
* );
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
addConditionalEdge(from, to, func) {
|
|
101
|
+
if (to.length === 0) {
|
|
102
|
+
throw new Error(`No edges defined for conditional edge from ${from}`);
|
|
103
|
+
}
|
|
104
|
+
this.conditionalEdges[from] = to;
|
|
105
|
+
this.conditionalFuncs[from] = func;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* StateMachine doesn't transform state, so node state is the same as graph state.
|
|
110
|
+
*
|
|
111
|
+
* @protected
|
|
112
|
+
* @param {S} state - The graph state
|
|
113
|
+
* @returns {S} The same state
|
|
114
|
+
*/
|
|
115
|
+
stateToNodeState(state) {
|
|
116
|
+
return state;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* StateMachine doesn't transform state, so node state is the same as graph state.
|
|
120
|
+
*
|
|
121
|
+
* @protected
|
|
122
|
+
* @param {Partial<S>} nodeState - The partial node state
|
|
123
|
+
* @returns {Partial<S>} The same partial state
|
|
124
|
+
*/
|
|
125
|
+
nodeStateToState(nodeState) {
|
|
126
|
+
return nodeState;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.StateMachine = StateMachine;
|
|
130
|
+
//# sourceMappingURL=state-machine.js.map
|