ai-sdk-graph 0.4.0 → 0.5.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/dist/compiled-graph.d.ts +50 -0
- package/dist/graph.d.ts +5 -41
- package/dist/index.d.ts +1 -0
- package/dist/index.js +70 -53
- package/dist/types.d.ts +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { GraphSDK } from './types';
|
|
2
|
+
import type { Graph } from './graph';
|
|
3
|
+
export declare class SuspenseError extends Error {
|
|
4
|
+
readonly data?: unknown;
|
|
5
|
+
constructor(data?: unknown);
|
|
6
|
+
}
|
|
7
|
+
export declare class CompiledGraph<State extends Record<string, unknown>, NodeKeys extends string = 'START' | 'END'> {
|
|
8
|
+
private readonly nodeRegistry;
|
|
9
|
+
private readonly edgeRegistry;
|
|
10
|
+
private readonly subgraphRegistry;
|
|
11
|
+
private readonly storage;
|
|
12
|
+
private readonly emitter;
|
|
13
|
+
private readonly stateManager;
|
|
14
|
+
private readonly onFinish;
|
|
15
|
+
private readonly onStart;
|
|
16
|
+
constructor(nodeRegistry: ReadonlyMap<NodeKeys, GraphSDK.Node<State, NodeKeys>>, edgeRegistry: ReadonlyMap<NodeKeys, GraphSDK.Edge<State, NodeKeys>[]>, subgraphRegistry: ReadonlyMap<NodeKeys, {
|
|
17
|
+
subgraph: Graph<any, any>;
|
|
18
|
+
options: GraphSDK.SubgraphOptions<State, any>;
|
|
19
|
+
}>, options?: GraphSDK.CompileOptions<State, NodeKeys>);
|
|
20
|
+
execute(runId: string, initialState: State | ((state: State | undefined) => State)): ReadableStream<import("ai").InferUIMessageChunk<import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>>>;
|
|
21
|
+
executeInternal(runId: string, initialState: State, writer: GraphSDK.Writer): Promise<State>;
|
|
22
|
+
private createExecutionContext;
|
|
23
|
+
private runExecutionLoop;
|
|
24
|
+
private runExecutionLoopInternal;
|
|
25
|
+
private executeWithStrategy;
|
|
26
|
+
private resumeWithStrategy;
|
|
27
|
+
private executeNodesWithStrategy;
|
|
28
|
+
private handleBatchResultWithStrategy;
|
|
29
|
+
private hasSuspendedNodes;
|
|
30
|
+
private hasNodesToExecute;
|
|
31
|
+
private restoreCheckpoint;
|
|
32
|
+
private isValidCheckpoint;
|
|
33
|
+
private hasNodeIds;
|
|
34
|
+
private hasAtLeastOneNode;
|
|
35
|
+
private restoreFromCheckpoint;
|
|
36
|
+
private createFreshExecution;
|
|
37
|
+
private persistCheckpoint;
|
|
38
|
+
private executeBatch;
|
|
39
|
+
private executeSingleNode;
|
|
40
|
+
private createNodeExecutionParams;
|
|
41
|
+
private executeSubgraphNode;
|
|
42
|
+
private generateSubgraphRunId;
|
|
43
|
+
private createSuspenseFunction;
|
|
44
|
+
private resolveNodeIds;
|
|
45
|
+
private computeNextNodes;
|
|
46
|
+
private findSuccessors;
|
|
47
|
+
private resolveEdgeTarget;
|
|
48
|
+
private deduplicateNodes;
|
|
49
|
+
private excludeTerminalNodes;
|
|
50
|
+
}
|
package/dist/graph.d.ts
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import type { GraphSDK } from './types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
constructor(data?: unknown);
|
|
5
|
-
}
|
|
2
|
+
import { CompiledGraph } from './compiled-graph';
|
|
3
|
+
export { SuspenseError } from './compiled-graph';
|
|
6
4
|
export declare class Graph<State extends Record<string, unknown>, NodeKeys extends string = 'START' | 'END'> {
|
|
7
5
|
private readonly nodeRegistry;
|
|
8
6
|
private readonly edgeRegistry;
|
|
9
7
|
private readonly subgraphRegistry;
|
|
10
|
-
|
|
11
|
-
private readonly emitter;
|
|
12
|
-
private readonly stateManager;
|
|
13
|
-
private readonly onFinish;
|
|
14
|
-
private readonly onStart;
|
|
15
|
-
constructor(options?: GraphSDK.GraphOptions<State, NodeKeys>);
|
|
8
|
+
constructor();
|
|
16
9
|
node<NewKey extends string>(id: NewKey, execute: ({ state, writer, suspense, update }: {
|
|
17
10
|
state: () => Readonly<State>;
|
|
18
11
|
writer: GraphSDK.Writer;
|
|
@@ -27,40 +20,11 @@ export declare class Graph<State extends Record<string, unknown>, NodeKeys exten
|
|
|
27
20
|
subgraph: Graph<any, any>;
|
|
28
21
|
options: GraphSDK.SubgraphOptions<State, any>;
|
|
29
22
|
}>;
|
|
23
|
+
compile(options?: GraphSDK.CompileOptions<State, NodeKeys>): CompiledGraph<State, NodeKeys>;
|
|
30
24
|
toMermaid(options?: {
|
|
31
25
|
direction?: 'TB' | 'LR';
|
|
32
26
|
}): string;
|
|
33
|
-
execute(runId: string, initialState: State | ((state: State | undefined) => State)): ReadableStream<import("ai").InferUIMessageChunk<import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>>>;
|
|
34
|
-
executeInternal(runId: string, initialState: State, writer: GraphSDK.Writer): Promise<State>;
|
|
35
27
|
private registerBuiltInNodes;
|
|
36
28
|
private addEdgeToRegistry;
|
|
37
|
-
private createExecutionContext;
|
|
38
|
-
private runExecutionLoop;
|
|
39
|
-
private runExecutionLoopInternal;
|
|
40
|
-
private executeWithStrategy;
|
|
41
|
-
private resumeWithStrategy;
|
|
42
|
-
private executeNodesWithStrategy;
|
|
43
|
-
private handleBatchResultWithStrategy;
|
|
44
|
-
private hasSuspendedNodes;
|
|
45
|
-
private hasNodesToExecute;
|
|
46
|
-
private restoreCheckpoint;
|
|
47
|
-
private isValidCheckpoint;
|
|
48
|
-
private hasNodeIds;
|
|
49
|
-
private hasAtLeastOneNode;
|
|
50
|
-
private restoreFromCheckpoint;
|
|
51
|
-
private createFreshExecution;
|
|
52
|
-
private persistCheckpoint;
|
|
53
|
-
private executeBatch;
|
|
54
|
-
private executeSingleNode;
|
|
55
|
-
private createNodeExecutionParams;
|
|
56
|
-
private executeSubgraphNode;
|
|
57
|
-
private generateSubgraphRunId;
|
|
58
|
-
private createSuspenseFunction;
|
|
59
|
-
private resolveNodeIds;
|
|
60
|
-
private computeNextNodes;
|
|
61
|
-
private findSuccessors;
|
|
62
|
-
private resolveEdgeTarget;
|
|
63
|
-
private deduplicateNodes;
|
|
64
|
-
private excludeTerminalNodes;
|
|
65
29
|
}
|
|
66
|
-
export declare function graph<State extends Record<string, unknown>, NodeKeys extends string = 'START' | 'END'>(
|
|
30
|
+
export declare function graph<State extends Record<string, unknown>, NodeKeys extends string = 'START' | 'END'>(): Graph<State, NodeKeys>;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ class RedisStorage {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
// src/graph.ts
|
|
34
|
+
// src/compiled-graph.ts
|
|
35
35
|
import { createUIMessageStream } from "ai";
|
|
36
36
|
var BUILT_IN_NODES = {
|
|
37
37
|
START: "START",
|
|
@@ -47,53 +47,23 @@ class SuspenseError extends Error {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
class
|
|
51
|
-
nodeRegistry
|
|
52
|
-
edgeRegistry
|
|
53
|
-
subgraphRegistry
|
|
50
|
+
class CompiledGraph {
|
|
51
|
+
nodeRegistry;
|
|
52
|
+
edgeRegistry;
|
|
53
|
+
subgraphRegistry;
|
|
54
54
|
storage;
|
|
55
55
|
emitter = new NodeEventEmitter;
|
|
56
56
|
stateManager = new StateManager;
|
|
57
57
|
onFinish;
|
|
58
58
|
onStart;
|
|
59
|
-
constructor(options = {}) {
|
|
59
|
+
constructor(nodeRegistry, edgeRegistry, subgraphRegistry, options = {}) {
|
|
60
|
+
this.nodeRegistry = nodeRegistry;
|
|
61
|
+
this.edgeRegistry = edgeRegistry;
|
|
62
|
+
this.subgraphRegistry = subgraphRegistry;
|
|
60
63
|
this.storage = options.storage ?? new InMemoryStorage;
|
|
61
|
-
this.registerBuiltInNodes();
|
|
62
64
|
this.onFinish = options.onFinish ?? (() => {});
|
|
63
65
|
this.onStart = options.onStart ?? (() => {});
|
|
64
66
|
}
|
|
65
|
-
node(id, execute) {
|
|
66
|
-
const node = { id, execute };
|
|
67
|
-
this.nodeRegistry.set(node.id, node);
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
|
-
edge(from, to) {
|
|
71
|
-
const edge = { from, to };
|
|
72
|
-
this.addEdgeToRegistry(edge);
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
graph(id, subgraph, options) {
|
|
76
|
-
this.subgraphRegistry.set(id, { subgraph, options });
|
|
77
|
-
const node = {
|
|
78
|
-
id,
|
|
79
|
-
execute: async () => {}
|
|
80
|
-
};
|
|
81
|
-
this.nodeRegistry.set(node.id, node);
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
get nodes() {
|
|
85
|
-
return this.nodeRegistry;
|
|
86
|
-
}
|
|
87
|
-
get edges() {
|
|
88
|
-
return this.edgeRegistry;
|
|
89
|
-
}
|
|
90
|
-
get subgraphs() {
|
|
91
|
-
return this.subgraphRegistry;
|
|
92
|
-
}
|
|
93
|
-
toMermaid(options) {
|
|
94
|
-
const generator = new MermaidGenerator(this.nodeRegistry, this.edgeRegistry, this.subgraphRegistry);
|
|
95
|
-
return generator.generate(options);
|
|
96
|
-
}
|
|
97
67
|
execute(runId, initialState) {
|
|
98
68
|
let context;
|
|
99
69
|
return createUIMessageStream({
|
|
@@ -118,15 +88,6 @@ class Graph {
|
|
|
118
88
|
await this.runExecutionLoopInternal(context);
|
|
119
89
|
return context.state;
|
|
120
90
|
}
|
|
121
|
-
registerBuiltInNodes() {
|
|
122
|
-
this.node(BUILT_IN_NODES.START, () => {});
|
|
123
|
-
this.node(BUILT_IN_NODES.END, () => {});
|
|
124
|
-
}
|
|
125
|
-
addEdgeToRegistry(edge) {
|
|
126
|
-
const existingEdges = this.edgeRegistry.get(edge.from) ?? [];
|
|
127
|
-
existingEdges.push(edge);
|
|
128
|
-
this.edgeRegistry.set(edge.from, existingEdges);
|
|
129
|
-
}
|
|
130
91
|
async createExecutionContext(runId, initialState, writer) {
|
|
131
92
|
const { context, firstTime } = await this.restoreCheckpoint(runId, initialState, writer);
|
|
132
93
|
return { context: { ...context, runId, writer }, firstTime };
|
|
@@ -255,7 +216,8 @@ class Graph {
|
|
|
255
216
|
this.emitter.emitStart(context.writer, node.id);
|
|
256
217
|
const subgraphRunId = this.generateSubgraphRunId(context.runId, node.id);
|
|
257
218
|
try {
|
|
258
|
-
const
|
|
219
|
+
const childRunner = new CompiledGraph(subgraph.nodes, subgraph.edges, subgraph.subgraphs, { storage: this.storage });
|
|
220
|
+
const childFinalState = await childRunner.executeInternal(subgraphRunId, options.input(context.state), context.writer);
|
|
259
221
|
const parentUpdate = options.output(childFinalState, context.state);
|
|
260
222
|
context.state = this.stateManager.apply(context.state, parentUpdate, context.writer);
|
|
261
223
|
this.emitter.emitEnd(context.writer, node.id);
|
|
@@ -299,9 +261,6 @@ class Graph {
|
|
|
299
261
|
return nodes.filter((node) => node.id !== BUILT_IN_NODES.END);
|
|
300
262
|
}
|
|
301
263
|
}
|
|
302
|
-
function graph(options = {}) {
|
|
303
|
-
return new Graph(options);
|
|
304
|
-
}
|
|
305
264
|
|
|
306
265
|
class NodeEventEmitter {
|
|
307
266
|
emitStart(writer, nodeId) {
|
|
@@ -339,6 +298,63 @@ class StateManager {
|
|
|
339
298
|
}
|
|
340
299
|
}
|
|
341
300
|
|
|
301
|
+
// src/graph.ts
|
|
302
|
+
class Graph {
|
|
303
|
+
nodeRegistry = new Map;
|
|
304
|
+
edgeRegistry = new Map;
|
|
305
|
+
subgraphRegistry = new Map;
|
|
306
|
+
constructor() {
|
|
307
|
+
this.registerBuiltInNodes();
|
|
308
|
+
}
|
|
309
|
+
node(id, execute) {
|
|
310
|
+
const node = { id, execute };
|
|
311
|
+
this.nodeRegistry.set(node.id, node);
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
edge(from, to) {
|
|
315
|
+
const edge = { from, to };
|
|
316
|
+
this.addEdgeToRegistry(edge);
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
graph(id, subgraph, options) {
|
|
320
|
+
this.subgraphRegistry.set(id, { subgraph, options });
|
|
321
|
+
const node = {
|
|
322
|
+
id,
|
|
323
|
+
execute: async () => {}
|
|
324
|
+
};
|
|
325
|
+
this.nodeRegistry.set(node.id, node);
|
|
326
|
+
return this;
|
|
327
|
+
}
|
|
328
|
+
get nodes() {
|
|
329
|
+
return this.nodeRegistry;
|
|
330
|
+
}
|
|
331
|
+
get edges() {
|
|
332
|
+
return this.edgeRegistry;
|
|
333
|
+
}
|
|
334
|
+
get subgraphs() {
|
|
335
|
+
return this.subgraphRegistry;
|
|
336
|
+
}
|
|
337
|
+
compile(options = {}) {
|
|
338
|
+
return new CompiledGraph(this.nodeRegistry, this.edgeRegistry, this.subgraphRegistry, options);
|
|
339
|
+
}
|
|
340
|
+
toMermaid(options) {
|
|
341
|
+
const generator = new MermaidGenerator(this.nodeRegistry, this.edgeRegistry, this.subgraphRegistry);
|
|
342
|
+
return generator.generate(options);
|
|
343
|
+
}
|
|
344
|
+
registerBuiltInNodes() {
|
|
345
|
+
this.node("START", () => {});
|
|
346
|
+
this.node("END", () => {});
|
|
347
|
+
}
|
|
348
|
+
addEdgeToRegistry(edge) {
|
|
349
|
+
const existingEdges = this.edgeRegistry.get(edge.from) ?? [];
|
|
350
|
+
existingEdges.push(edge);
|
|
351
|
+
this.edgeRegistry.set(edge.from, existingEdges);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function graph() {
|
|
355
|
+
return new Graph;
|
|
356
|
+
}
|
|
357
|
+
|
|
342
358
|
class MermaidGenerator {
|
|
343
359
|
nodeRegistry;
|
|
344
360
|
edgeRegistry;
|
|
@@ -456,5 +472,6 @@ export {
|
|
|
456
472
|
RedisStorage,
|
|
457
473
|
InMemoryStorage,
|
|
458
474
|
Graph,
|
|
459
|
-
GRAPH_DATA_PART_TYPES
|
|
475
|
+
GRAPH_DATA_PART_TYPES,
|
|
476
|
+
CompiledGraph
|
|
460
477
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -49,5 +49,15 @@ export declare namespace GraphSDK {
|
|
|
49
49
|
writer: UIMessageStreamWriter;
|
|
50
50
|
}) => Promise<void> | void;
|
|
51
51
|
}
|
|
52
|
+
interface CompileOptions<State extends Record<string, unknown>, NodeKeys extends string> {
|
|
53
|
+
storage?: GraphStorage<State, NodeKeys>;
|
|
54
|
+
onFinish?: (args: {
|
|
55
|
+
state: State;
|
|
56
|
+
}) => Promise<void> | void;
|
|
57
|
+
onStart?: (args: {
|
|
58
|
+
state: State;
|
|
59
|
+
writer: UIMessageStreamWriter;
|
|
60
|
+
}) => Promise<void> | void;
|
|
61
|
+
}
|
|
52
62
|
type Writer = Parameters<Parameters<typeof createUIMessageStream>[0]['execute']>[0]['writer'];
|
|
53
63
|
}
|