@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,138 @@
|
|
|
1
|
+
import { ContextLayer, RuntimeContext } from ".";
|
|
2
|
+
import { NodeLike } from "../nodes";
|
|
3
|
+
import { TypedKeys } from "../types";
|
|
4
|
+
import { Graph } from "./graph";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
/**
|
|
7
|
+
* Type representing state with an index for tracking iteration position.
|
|
8
|
+
*
|
|
9
|
+
* @template Prefix - The prefix for the index key (e.g., "item" -> "itemIndex")
|
|
10
|
+
*/
|
|
11
|
+
type IndexState<Prefix extends string> = {
|
|
12
|
+
[K in `${Prefix}Index`]: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Type representing state with the current item being iterated over.
|
|
16
|
+
*
|
|
17
|
+
* @template Prefix - The prefix for the item key (e.g., "item" -> "itemItem")
|
|
18
|
+
* @template Item - The type of items in the array
|
|
19
|
+
*/
|
|
20
|
+
type ItemState<Prefix extends string, Item extends any = any> = {
|
|
21
|
+
[K in `${Prefix}Item`]: Item;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Type representing the complete state for iterator nodes.
|
|
25
|
+
* Combines the base state with index and item tracking.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The base state object type
|
|
28
|
+
* @template Prefix - Prefix for index/item keys
|
|
29
|
+
* @template Item - Type of items being iterated
|
|
30
|
+
*/
|
|
31
|
+
export type IteratorNodeState<T extends object, Prefix extends string, Item extends any = any> = IndexState<Prefix> & ItemState<Prefix, Item> & T;
|
|
32
|
+
/**
|
|
33
|
+
* Zod schema for index state
|
|
34
|
+
*/
|
|
35
|
+
type ZodIndexState<Prefix extends string> = z.ZodObject<{
|
|
36
|
+
[K in `${Prefix}Index`]: z.ZodNumber;
|
|
37
|
+
}>;
|
|
38
|
+
/**
|
|
39
|
+
* Zod schema for item state
|
|
40
|
+
*/
|
|
41
|
+
type ZodItemState<Prefix extends string, Item extends any = any> = z.ZodObject<{
|
|
42
|
+
[K in `${Prefix}Item`]: z.ZodType<Item>;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Zod schema for complete iterator node state
|
|
46
|
+
*/
|
|
47
|
+
type ZodIteratorNodeState<T extends z.ZodObject, Prefix extends string, Item extends any = any> = ZodIndexState<Prefix> & ZodItemState<Prefix, Item> & T;
|
|
48
|
+
/**
|
|
49
|
+
* Helper type to extract keys that have array values from state
|
|
50
|
+
*/
|
|
51
|
+
type ArrayKeys<T> = TypedKeys<T, any[]>;
|
|
52
|
+
/**
|
|
53
|
+
* A graph that iterates over an array in state, running a looped node for each item.
|
|
54
|
+
* Manages index tracking and item extraction automatically.
|
|
55
|
+
*
|
|
56
|
+
* @class Iterator
|
|
57
|
+
* @extends {Graph<T, S, NS>}
|
|
58
|
+
* @template Item - The type of items in the array
|
|
59
|
+
* @template T - The Zod schema for base state
|
|
60
|
+
* @template Prefix - Prefix for index/item state keys
|
|
61
|
+
* @template S - The inferred state type from T
|
|
62
|
+
* @template NS - The node state type including index and item
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const schema = z.object({
|
|
67
|
+
* items: z.array(z.object({ name: z.string(), value: z.number() }))
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* const iterator = new Iterator(schema, "item", "items");
|
|
71
|
+
* const looped = new NodeSequence(iterator.getNodeSchema());
|
|
72
|
+
* looped.next(new FunctionNode((state) => ({
|
|
73
|
+
* itemValue: state.itemItem.value * 2
|
|
74
|
+
* })));
|
|
75
|
+
*
|
|
76
|
+
* iterator.setLoopedNode(looped);
|
|
77
|
+
*
|
|
78
|
+
* const result = await iterator.invoke({
|
|
79
|
+
* items: [{ name: "a", value: 1 }, { name: "b", value: 2 }]
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare class Iterator<Item extends any, T extends z.ZodObject, Prefix extends string, S extends Record<string, unknown> = z.infer<T>, NS extends Record<string, unknown> = IteratorNodeState<S, Prefix, Item>> extends Graph<T, S, NS> {
|
|
84
|
+
protected readonly schema: T;
|
|
85
|
+
private readonly prefix;
|
|
86
|
+
private readonly iteratorKey;
|
|
87
|
+
/**
|
|
88
|
+
* Creates an iterator graph.
|
|
89
|
+
*
|
|
90
|
+
* @param {T} schema - The base state schema
|
|
91
|
+
* @param {Prefix} prefix - Prefix for index and item keys
|
|
92
|
+
* @param {ArrayKeys<z.infer<T>>} iteratorKey - The state key containing the array to iterate
|
|
93
|
+
*/
|
|
94
|
+
constructor(schema: T, prefix: Prefix, iteratorKey: ArrayKeys<z.infer<T>>);
|
|
95
|
+
/**
|
|
96
|
+
* Sets the node to be executed for each item in the array.
|
|
97
|
+
*
|
|
98
|
+
* @param {NodeLike<NS> | Graph<ZodIteratorNodeState<T, Prefix, Item>, NS>} loopedNode - The node to loop
|
|
99
|
+
* @returns {this} The iterator instance for method chaining
|
|
100
|
+
*/
|
|
101
|
+
setLoopedNode(loopedNode: NodeLike<NS> | Graph<ZodIteratorNodeState<T, Prefix, Item>, NS>): this;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the schema for nodes being looped over.
|
|
104
|
+
* Extends the base schema with index and item tracking.
|
|
105
|
+
*
|
|
106
|
+
* @returns {ZodIteratorNodeState<T, Prefix, Item>} Schema for looped nodes
|
|
107
|
+
*/
|
|
108
|
+
getNodeSchema(): ZodIteratorNodeState<T, Prefix, Item>;
|
|
109
|
+
/**
|
|
110
|
+
* Converts the graph state to node state by extracting the current item.
|
|
111
|
+
*
|
|
112
|
+
* @protected
|
|
113
|
+
* @param {S} state - The graph state
|
|
114
|
+
* @param {ContextLayer} context - The execution context
|
|
115
|
+
* @returns {NS} The node state with current index and item
|
|
116
|
+
*/
|
|
117
|
+
protected stateToNodeState(state: S, context: ContextLayer): NS;
|
|
118
|
+
/**
|
|
119
|
+
* Converts node state back to graph state, updating the array with modified item.
|
|
120
|
+
*
|
|
121
|
+
* @protected
|
|
122
|
+
* @param {Partial<NS>} nodeState - The partial node state
|
|
123
|
+
* @param {ContextLayer} context - The execution context
|
|
124
|
+
* @returns {S} The partial graph state
|
|
125
|
+
*/
|
|
126
|
+
protected nodeStateToState(nodeState: Partial<NS>, context: ContextLayer): S;
|
|
127
|
+
/**
|
|
128
|
+
* Runs the iterator over all items in the array.
|
|
129
|
+
* Manages index tracking and item extraction for looped node execution.
|
|
130
|
+
*
|
|
131
|
+
* @param {S} input - Initial state
|
|
132
|
+
* @param {ContextLayer | RuntimeContext} contextOrRuntime - Execution context
|
|
133
|
+
* @returns {Promise<Partial<S>>} Final state with updated array
|
|
134
|
+
*/
|
|
135
|
+
run(input: S, contextOrRuntime: ContextLayer | RuntimeContext): Promise<Partial<S>>;
|
|
136
|
+
}
|
|
137
|
+
export {};
|
|
138
|
+
//# sourceMappingURL=iterator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iterator.d.ts","sourceRoot":"","sources":["../../src/graphs/iterator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,CAAC;AACjD,OAAO,EAAgB,QAAQ,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,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AC;IAaJ;;;;;;;OAOG;IACY,GAAG,CACd,KAAK,EAAE,CAAC,EACR,gBAAgB,EAAE,YAAY,GAAG,cAAc,GAChD,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;CAsBzB"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Iterator = void 0;
|
|
4
|
+
const nodes_1 = require("../nodes");
|
|
5
|
+
const graph_1 = require("./graph");
|
|
6
|
+
const zod_1 = require("zod");
|
|
7
|
+
/**
|
|
8
|
+
* Node name for the loop body
|
|
9
|
+
*/
|
|
10
|
+
const ITERATOR_LOOP_NODE = "iterator-loop";
|
|
11
|
+
/**
|
|
12
|
+
* Node name for incrementing the index
|
|
13
|
+
*/
|
|
14
|
+
const INCREMENT_INDEX_NODE = "increment-index";
|
|
15
|
+
/**
|
|
16
|
+
* A graph that iterates over an array in state, running a looped node for each item.
|
|
17
|
+
* Manages index tracking and item extraction automatically.
|
|
18
|
+
*
|
|
19
|
+
* @class Iterator
|
|
20
|
+
* @extends {Graph<T, S, NS>}
|
|
21
|
+
* @template Item - The type of items in the array
|
|
22
|
+
* @template T - The Zod schema for base state
|
|
23
|
+
* @template Prefix - Prefix for index/item state keys
|
|
24
|
+
* @template S - The inferred state type from T
|
|
25
|
+
* @template NS - The node state type including index and item
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const schema = z.object({
|
|
30
|
+
* items: z.array(z.object({ name: z.string(), value: z.number() }))
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const iterator = new Iterator(schema, "item", "items");
|
|
34
|
+
* const looped = new NodeSequence(iterator.getNodeSchema());
|
|
35
|
+
* looped.next(new FunctionNode((state) => ({
|
|
36
|
+
* itemValue: state.itemItem.value * 2
|
|
37
|
+
* })));
|
|
38
|
+
*
|
|
39
|
+
* iterator.setLoopedNode(looped);
|
|
40
|
+
*
|
|
41
|
+
* const result = await iterator.invoke({
|
|
42
|
+
* items: [{ name: "a", value: 1 }, { name: "b", value: 2 }]
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
class Iterator extends graph_1.Graph {
|
|
47
|
+
schema;
|
|
48
|
+
prefix;
|
|
49
|
+
iteratorKey;
|
|
50
|
+
/**
|
|
51
|
+
* Creates an iterator graph.
|
|
52
|
+
*
|
|
53
|
+
* @param {T} schema - The base state schema
|
|
54
|
+
* @param {Prefix} prefix - Prefix for index and item keys
|
|
55
|
+
* @param {ArrayKeys<z.infer<T>>} iteratorKey - The state key containing the array to iterate
|
|
56
|
+
*/
|
|
57
|
+
constructor(schema, prefix,
|
|
58
|
+
// Disabled for now, until I can figure out a better approach for this
|
|
59
|
+
// private readonly iteratorSelector: (state: S | NS) => Item[],
|
|
60
|
+
iteratorKey) {
|
|
61
|
+
super(schema);
|
|
62
|
+
this.schema = schema;
|
|
63
|
+
this.prefix = prefix;
|
|
64
|
+
this.iteratorKey = iteratorKey;
|
|
65
|
+
this.nodes[INCREMENT_INDEX_NODE] = new nodes_1.FunctionNode((_, context) => {
|
|
66
|
+
const indexContext = context.custom.indexCtx;
|
|
67
|
+
indexContext.currentNode = (Number(indexContext.currentNode) + 1).toString();
|
|
68
|
+
return {};
|
|
69
|
+
});
|
|
70
|
+
this.conditionalEdges[graph_1.START] = [ITERATOR_LOOP_NODE, graph_1.END];
|
|
71
|
+
this.conditionalFuncs[graph_1.START] = (state) => {
|
|
72
|
+
const iterator = state[this.iteratorKey];
|
|
73
|
+
return iterator.length > 0 ? ITERATOR_LOOP_NODE : graph_1.END;
|
|
74
|
+
};
|
|
75
|
+
this.conditionalEdges[ITERATOR_LOOP_NODE] = [INCREMENT_INDEX_NODE, graph_1.END];
|
|
76
|
+
this.conditionalFuncs[ITERATOR_LOOP_NODE] = (state, context) => {
|
|
77
|
+
const indexContext = context.custom.indexCtx;
|
|
78
|
+
const index = Number(indexContext.currentNode);
|
|
79
|
+
const iterator = state[this.iteratorKey];
|
|
80
|
+
return index < iterator.length - 1 ? INCREMENT_INDEX_NODE : graph_1.END;
|
|
81
|
+
};
|
|
82
|
+
this.edges[INCREMENT_INDEX_NODE] = ITERATOR_LOOP_NODE;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Sets the node to be executed for each item in the array.
|
|
86
|
+
*
|
|
87
|
+
* @param {NodeLike<NS> | Graph<ZodIteratorNodeState<T, Prefix, Item>, NS>} loopedNode - The node to loop
|
|
88
|
+
* @returns {this} The iterator instance for method chaining
|
|
89
|
+
*/
|
|
90
|
+
setLoopedNode(loopedNode) {
|
|
91
|
+
this.nodes[ITERATOR_LOOP_NODE] = loopedNode;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Gets the schema for nodes being looped over.
|
|
96
|
+
* Extends the base schema with index and item tracking.
|
|
97
|
+
*
|
|
98
|
+
* @returns {ZodIteratorNodeState<T, Prefix, Item>} Schema for looped nodes
|
|
99
|
+
*/
|
|
100
|
+
getNodeSchema() {
|
|
101
|
+
return zod_1.z.object({
|
|
102
|
+
[`${this.prefix}Index`]: zod_1.z.number(),
|
|
103
|
+
[`${this.prefix}Item`]: this.schema.shape[this.iteratorKey],
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Converts the graph state to node state by extracting the current item.
|
|
108
|
+
*
|
|
109
|
+
* @protected
|
|
110
|
+
* @param {S} state - The graph state
|
|
111
|
+
* @param {ContextLayer} context - The execution context
|
|
112
|
+
* @returns {NS} The node state with current index and item
|
|
113
|
+
*/
|
|
114
|
+
stateToNodeState(state, context) {
|
|
115
|
+
const indexContext = context.custom.indexCtx;
|
|
116
|
+
const index = Number(indexContext.currentNode);
|
|
117
|
+
const iterator = state[this.iteratorKey];
|
|
118
|
+
if (iterator === undefined) {
|
|
119
|
+
throw new Error(`Selected iterator is not found`);
|
|
120
|
+
}
|
|
121
|
+
if (!Array.isArray(iterator)) {
|
|
122
|
+
throw new Error(`Seletected iterator is not an array`);
|
|
123
|
+
}
|
|
124
|
+
const item = iterator[index];
|
|
125
|
+
if (item === undefined) {
|
|
126
|
+
throw new Error(`Item ${index} not found`);
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
...state,
|
|
130
|
+
[`${this.prefix}Index`]: index,
|
|
131
|
+
[`${this.prefix}Item`]: item,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Converts node state back to graph state, updating the array with modified item.
|
|
136
|
+
*
|
|
137
|
+
* @protected
|
|
138
|
+
* @param {Partial<NS>} nodeState - The partial node state
|
|
139
|
+
* @param {ContextLayer} context - The execution context
|
|
140
|
+
* @returns {S} The partial graph state
|
|
141
|
+
*/
|
|
142
|
+
nodeStateToState(nodeState, context) {
|
|
143
|
+
const arr = context.custom.arr;
|
|
144
|
+
const indexContext = context.custom.indexCtx;
|
|
145
|
+
const index = Number(indexContext.currentNode);
|
|
146
|
+
if (`${this.prefix}Item` in nodeState) {
|
|
147
|
+
const { [`${this.prefix}Item`]: item, ...rest } = nodeState;
|
|
148
|
+
arr[index] = item;
|
|
149
|
+
delete rest[`${this.prefix}Index`];
|
|
150
|
+
return rest;
|
|
151
|
+
}
|
|
152
|
+
return nodeState;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Runs the iterator over all items in the array.
|
|
156
|
+
* Manages index tracking and item extraction for looped node execution.
|
|
157
|
+
*
|
|
158
|
+
* @param {S} input - Initial state
|
|
159
|
+
* @param {ContextLayer | RuntimeContext} contextOrRuntime - Execution context
|
|
160
|
+
* @returns {Promise<Partial<S>>} Final state with updated array
|
|
161
|
+
*/
|
|
162
|
+
async run(input, contextOrRuntime) {
|
|
163
|
+
let state = structuredClone(input);
|
|
164
|
+
if (this.nodes[ITERATOR_LOOP_NODE] === undefined) {
|
|
165
|
+
throw new Error(`Looped node is not set`);
|
|
166
|
+
}
|
|
167
|
+
const indexContext = contextOrRuntime.nextLayer();
|
|
168
|
+
if (indexContext.currentNode === undefined) {
|
|
169
|
+
indexContext.currentNode = "0";
|
|
170
|
+
}
|
|
171
|
+
const context = indexContext.nextLayer();
|
|
172
|
+
if (context.currentNode === undefined) {
|
|
173
|
+
context.currentNode = graph_1.START;
|
|
174
|
+
}
|
|
175
|
+
context.custom.indexCtx = indexContext;
|
|
176
|
+
context.custom.arr = state[this.iteratorKey];
|
|
177
|
+
const result = await this.runInternal(state, context);
|
|
178
|
+
context.done();
|
|
179
|
+
indexContext.done();
|
|
180
|
+
return { ...result, [this.iteratorKey]: structuredClone(context.custom.arr) };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
exports.Iterator = Iterator;
|
|
184
|
+
//# sourceMappingURL=iterator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iterator.js","sourceRoot":"","sources":["../../src/graphs/iterator.ts"],"names":[],"mappings":";;;AACA,oCAAkD;AAElD,mCAA4C;AAC5C,6BAAwB;AAmDxB;;GAEG;AACH,MAAM,kBAAkB,GAAG,eAAe,CAAC;AAE3C;;GAEG;AACH,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAa,QAMX,SAAQ,aAAe;IASE;IACF;IAGA;IAZrB;;;;;;OAMG;IACH,YACuB,MAAS,EACX,MAAc;IAC/B,sEAAsE;IACtE,gEAAgE;IAC/C,WAAkC;QAEnD,KAAK,CAAC,MAAM,CAAC,CAAC;QANK,WAAM,GAAN,MAAM,CAAG;QACX,WAAM,GAAN,MAAM,CAAQ;QAGd,gBAAW,GAAX,WAAW,CAAuB;QAGnD,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC,GAAG,IAAI,oBAAY,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE;YAC/D,MAAM,YAAY,GAAG,OAAO,CAAC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AAC,IAAI,CAAC,WAAsB,CAAW,CAAC;QAElE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAEtD,OAAO,CAAC,IAAI,EAAE,CAAC;QACf,YAAY,CAAC,IAAI,EAAE,CAAC;QACpB,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;IAClF,CAAC;CACJ;AA5JD,4BA4JC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Merges partial state updates into a base state object.
|
|
4
|
+
* Uses registered custom merge strategies for specific types.
|
|
5
|
+
* Falls back to standard object/array merge behavior.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The state object type
|
|
8
|
+
* @param {T} base - The base state to merge into
|
|
9
|
+
* @param {Partial<T>} changes - Partial state with updates
|
|
10
|
+
* @param {z.ZodObject} schema - Zod schema for the state type
|
|
11
|
+
* @returns {T} Merged state object
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const base = { count: 5, items: [1, 2, 3] };
|
|
16
|
+
* const changes = { count: 10, items: [4] };
|
|
17
|
+
* const merged = mergeState(base, changes, schema);
|
|
18
|
+
* // Results in { count: 10, items: [4] }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function mergeState<T extends Record<string, any>>(base: T, changes: Partial<T>, schema: z.ZodObject): T;
|
|
22
|
+
//# sourceMappingURL=merge-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-state.d.ts","sourceRoot":"","sources":["../../src/graphs/merge-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,CA4B9G"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeState = mergeState;
|
|
4
|
+
const registry_1 = require("./registry");
|
|
5
|
+
/**
|
|
6
|
+
* Merges partial state updates into a base state object.
|
|
7
|
+
* Uses registered custom merge strategies for specific types.
|
|
8
|
+
* Falls back to standard object/array merge behavior.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The state object type
|
|
11
|
+
* @param {T} base - The base state to merge into
|
|
12
|
+
* @param {Partial<T>} changes - Partial state with updates
|
|
13
|
+
* @param {z.ZodObject} schema - Zod schema for the state type
|
|
14
|
+
* @returns {T} Merged state object
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const base = { count: 5, items: [1, 2, 3] };
|
|
19
|
+
* const changes = { count: 10, items: [4] };
|
|
20
|
+
* const merged = mergeState(base, changes, schema);
|
|
21
|
+
* // Results in { count: 10, items: [4] }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function mergeState(base, changes, schema) {
|
|
25
|
+
const acc = structuredClone(base);
|
|
26
|
+
for (const [key, value] of Object.entries(changes)) {
|
|
27
|
+
// if there's nothing already present for the key, then we just set the value
|
|
28
|
+
if (acc[key] === undefined || acc[key] === null) {
|
|
29
|
+
acc[key] = value;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
// if there's a registered merge function, then we assume the function handles the entirety of the value, including any nested values
|
|
33
|
+
if (registry_1.STATE_MERGE.has(schema.shape[key])) {
|
|
34
|
+
acc[key] = registry_1.STATE_MERGE.get(schema.shape[key]).merge(acc[key], value);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (typeof value === "object") {
|
|
38
|
+
if (Array.isArray(acc[key])) {
|
|
39
|
+
acc[key] = value;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
acc[key] = mergeState(acc[key], value, schema.shape[key]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
if (registry_1.STATE_MERGE.has(schema.shape[key]) && acc[key] !== undefined) {
|
|
47
|
+
acc[key] = registry_1.STATE_MERGE.get(schema.shape[key]).merge(acc[key], value);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
acc[key] = value;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return acc;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=merge-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-state.js","sourceRoot":"","sources":["../../src/graphs/merge-state.ts"],"names":[],"mappings":";;AAsBA,gCA4BC;AAjDD,yCAAyC;AAEzC;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,UAAU,CAAgC,IAAO,EAAE,OAAmB,EAAE,MAAmB;IACvG,MAAM,GAAG,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IAClC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACjD,6EAA6E;QAC7E,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC;YAC7C,GAA2B,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YAC1C,SAAS;QACb,CAAC;QACD,qIAAqI;QACrI,IAAI,sBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACpC,GAA2B,CAAC,GAAG,CAAC,GAAG,sBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAE,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;YAC/F,SAAS;QACb,CAAC;QACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC5B,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;gBACzB,GAA2B,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YAC9C,CAAC;iBAAM,CAAC;gBACH,GAA2B,CAAC,GAAG,CAAC,GAAG,UAAU,CAAE,GAA2B,CAAC,GAAG,CAAC,EAAE,KAAmB,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,CAAgB,CAAC,CAAC;YAC7I,CAAC;QACL,CAAC;aAAM,CAAC;YACJ,IAAI,sBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;gBAC9D,GAA2B,CAAC,GAAG,CAAC,GAAG,sBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAE,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;YACnG,CAAC;iBAAM,CAAC;gBACH,GAA2B,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YAC9C,CAAC;QACL,CAAC;IACL,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type NodeLike } from "../nodes";
|
|
2
|
+
import { Graph } from "./graph";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/**
|
|
5
|
+
* A graph that executes a sequence of nodes linearly.
|
|
6
|
+
* Nodes are executed one after another without branching.
|
|
7
|
+
* Each call to next() adds a node to the sequence.
|
|
8
|
+
*
|
|
9
|
+
* @class NodeSequence
|
|
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({ count: z.number() });
|
|
17
|
+
* const sequence = new NodeSequence(schema);
|
|
18
|
+
*
|
|
19
|
+
* sequence
|
|
20
|
+
* .next(new FunctionNode((state) => ({ count: state.count + 1 })))
|
|
21
|
+
* .next(new FunctionNode((state) => ({ count: state.count * 2 })));
|
|
22
|
+
*
|
|
23
|
+
* const result = await sequence.invoke({ count: 5 });
|
|
24
|
+
* // State transitions: 5 -> 6 -> 12
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare class NodeSequence<T extends z.ZodObject, S extends Record<string, unknown> = z.infer<T>> extends Graph<T, S> {
|
|
28
|
+
/**
|
|
29
|
+
* NodeSequence doesn't transform state, so node state is the same as graph state.
|
|
30
|
+
*
|
|
31
|
+
* @protected
|
|
32
|
+
* @param {S} state - The graph state
|
|
33
|
+
* @returns {S} The same state (no transformation)
|
|
34
|
+
*/
|
|
35
|
+
protected stateToNodeState(state: S): S;
|
|
36
|
+
/**
|
|
37
|
+
* NodeSequence doesn't transform state, so node state is the same as graph state.
|
|
38
|
+
*
|
|
39
|
+
* @protected
|
|
40
|
+
* @param {Partial<S>} nodeState - The partial node state
|
|
41
|
+
* @returns {Partial<S>} The same partial state (no transformation)
|
|
42
|
+
*/
|
|
43
|
+
protected nodeStateToState(nodeState: Partial<S>): Partial<S>;
|
|
44
|
+
/**
|
|
45
|
+
* Adds the next node in the sequence.
|
|
46
|
+
* Automatically wires up edges from START to the first node,
|
|
47
|
+
* between sequential nodes, and from the last node to END.
|
|
48
|
+
*
|
|
49
|
+
* @param {NodeLike<S> | Graph<T, S>} node - The node to add
|
|
50
|
+
* @returns {this} The sequence instance for method chaining
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const sequence = new NodeSequence(schema);
|
|
55
|
+
* sequence
|
|
56
|
+
* .next(node1)
|
|
57
|
+
* .next(node2)
|
|
58
|
+
* .next(node3);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
next(node: NodeLike<S> | Graph<T, S>): this;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=node-sequence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-sequence.d.ts","sourceRoot":"","sources":["../../src/graphs/node-sequence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAO,KAAK,EAAS,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;;;;;;;;;;;;;GAsBG;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;IAEhH;;;;;;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;IAI7D;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI;CAgB9C"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NodeSequence = void 0;
|
|
4
|
+
const graph_1 = require("./graph");
|
|
5
|
+
/**
|
|
6
|
+
* A graph that executes a sequence of nodes linearly.
|
|
7
|
+
* Nodes are executed one after another without branching.
|
|
8
|
+
* Each call to next() adds a node to the sequence.
|
|
9
|
+
*
|
|
10
|
+
* @class NodeSequence
|
|
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({ count: z.number() });
|
|
18
|
+
* const sequence = new NodeSequence(schema);
|
|
19
|
+
*
|
|
20
|
+
* sequence
|
|
21
|
+
* .next(new FunctionNode((state) => ({ count: state.count + 1 })))
|
|
22
|
+
* .next(new FunctionNode((state) => ({ count: state.count * 2 })));
|
|
23
|
+
*
|
|
24
|
+
* const result = await sequence.invoke({ count: 5 });
|
|
25
|
+
* // State transitions: 5 -> 6 -> 12
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
class NodeSequence extends graph_1.Graph {
|
|
29
|
+
/**
|
|
30
|
+
* NodeSequence doesn't transform state, so node state is the same as graph state.
|
|
31
|
+
*
|
|
32
|
+
* @protected
|
|
33
|
+
* @param {S} state - The graph state
|
|
34
|
+
* @returns {S} The same state (no transformation)
|
|
35
|
+
*/
|
|
36
|
+
stateToNodeState(state) {
|
|
37
|
+
return state;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* NodeSequence doesn't transform state, so node state is the same as graph state.
|
|
41
|
+
*
|
|
42
|
+
* @protected
|
|
43
|
+
* @param {Partial<S>} nodeState - The partial node state
|
|
44
|
+
* @returns {Partial<S>} The same partial state (no transformation)
|
|
45
|
+
*/
|
|
46
|
+
nodeStateToState(nodeState) {
|
|
47
|
+
return nodeState;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adds the next node in the sequence.
|
|
51
|
+
* Automatically wires up edges from START to the first node,
|
|
52
|
+
* between sequential nodes, and from the last node to END.
|
|
53
|
+
*
|
|
54
|
+
* @param {NodeLike<S> | Graph<T, S>} node - The node to add
|
|
55
|
+
* @returns {this} The sequence instance for method chaining
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const sequence = new NodeSequence(schema);
|
|
60
|
+
* sequence
|
|
61
|
+
* .next(node1)
|
|
62
|
+
* .next(node2)
|
|
63
|
+
* .next(node3);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
next(node) {
|
|
67
|
+
const nodeCount = Object.keys(this.nodes).length;
|
|
68
|
+
const name = `node-${nodeCount}`;
|
|
69
|
+
const isFirstNode = nodeCount === 0;
|
|
70
|
+
this.nodes[name] = node;
|
|
71
|
+
if (isFirstNode) {
|
|
72
|
+
this.edges[graph_1.START] = name;
|
|
73
|
+
this.edges[name] = graph_1.END;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const previousNodeName = `node-${nodeCount - 1}`;
|
|
77
|
+
this.edges[previousNodeName] = name;
|
|
78
|
+
this.edges[name] = graph_1.END;
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.NodeSequence = NodeSequence;
|
|
84
|
+
//# sourceMappingURL=node-sequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-sequence.js","sourceRoot":"","sources":["../../src/graphs/node-sequence.ts"],"names":[],"mappings":";;;AACA,mCAA4C;AAG5C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAa,YAAoF,SAAQ,aAAW;IAEhH;;;;;;OAMG;IACO,gBAAgB,CAAC,KAAQ;QAC/B,OAAO,KAAK,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACO,gBAAgB,CAAC,SAAqB;QAC5C,OAAO,SAAS,CAAC;IACrB,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,CAAC,IAA+B;QAChC,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC;QACjD,MAAM,IAAI,GAAG,QAAQ,SAAS,EAAE,CAAC;QACjC,MAAM,WAAW,GAAG,SAAS,KAAK,CAAC,CAAC;QACpC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;QAExB,IAAI,WAAW,EAAE,CAAC;YACd,IAAI,CAAC,KAAK,CAAC,aAAK,CAAC,GAAG,IAAI,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,WAAG,CAAC;QAC3B,CAAC;aAAM,CAAC;YACJ,MAAM,gBAAgB,GAAG,QAAQ,SAAS,GAAG,CAAC,EAAE,CAAC;YACjD,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC;YACpC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,WAAG,CAAC;QAC3B,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAzDD,oCAyDC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const STATE_MERGE: z.core.$ZodRegistry<{
|
|
3
|
+
merge: (old: z.$output, change: Partial<z.$output>) => z.$output;
|
|
4
|
+
}, z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
|
|
5
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/graphs/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,WAAW;WAAuB,CAAC,GAAG,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO;iFAAK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/graphs/registry.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAEX,QAAA,WAAW,GAAG,OAAC,CAAC,QAAQ,EAAwE,CAAC"}
|