@vinhnt-sdk/durable-state 0.0.0-stage → 0.23.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 +186 -2
- package/dist/attachment/attachment-ref.d.ts +99 -0
- package/dist/attachment/attachment-ref.d.ts.map +1 -0
- package/dist/attachment/attachment-ref.js +117 -0
- package/dist/attachment/attachment-ref.js.map +1 -0
- package/dist/errors.d.ts +121 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +166 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist/index.js.map +1 -0
- package/dist/state/channel.d.ts +115 -0
- package/dist/state/channel.d.ts.map +1 -0
- package/dist/state/channel.js +65 -0
- package/dist/state/channel.js.map +1 -0
- package/dist/state/checkpoint.d.ts +141 -0
- package/dist/state/checkpoint.d.ts.map +1 -0
- package/dist/state/checkpoint.js +109 -0
- package/dist/state/checkpoint.js.map +1 -0
- package/dist/state/executor.d.ts +42 -0
- package/dist/state/executor.d.ts.map +1 -0
- package/dist/state/executor.js +254 -0
- package/dist/state/executor.js.map +1 -0
- package/dist/state/graph-state.d.ts +113 -0
- package/dist/state/graph-state.d.ts.map +1 -0
- package/dist/state/graph-state.js +121 -0
- package/dist/state/graph-state.js.map +1 -0
- package/dist/state/interrupt.d.ts +97 -0
- package/dist/state/interrupt.d.ts.map +1 -0
- package/dist/state/interrupt.js +70 -0
- package/dist/state/interrupt.js.map +1 -0
- package/dist/state/session-adapter.d.ts +76 -0
- package/dist/state/session-adapter.d.ts.map +1 -0
- package/dist/state/session-adapter.js +210 -0
- package/dist/state/session-adapter.js.map +1 -0
- package/package.json +46 -4
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { CheckpointError } from "../errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* In-memory checkpointer — suitable for development and testing. Not durable
|
|
4
|
+
* across process restarts. Use a durable implementation (SQLite, Postgres,
|
|
5
|
+
* etc.) for production.
|
|
6
|
+
*/
|
|
7
|
+
export class MemoryCheckpointer {
|
|
8
|
+
snapshots = new Map();
|
|
9
|
+
writes = new Map();
|
|
10
|
+
key(config) {
|
|
11
|
+
return `${config.threadId}|${config.checkpointNs ?? ""}`;
|
|
12
|
+
}
|
|
13
|
+
async put(config, snapshot) {
|
|
14
|
+
const key = this.key(config);
|
|
15
|
+
const existing = this.snapshots.get(key) ?? [];
|
|
16
|
+
this.snapshots.set(key, [...existing, snapshot]);
|
|
17
|
+
}
|
|
18
|
+
async putWrites(config, writes) {
|
|
19
|
+
const key = this.key(config);
|
|
20
|
+
const existing = this.writes.get(key) ?? [];
|
|
21
|
+
this.writes.set(key, [...existing, ...writes]);
|
|
22
|
+
}
|
|
23
|
+
async getTuple(config) {
|
|
24
|
+
const key = this.key(config);
|
|
25
|
+
const snapshots = this.snapshots.get(key);
|
|
26
|
+
if (snapshots === undefined || snapshots.length === 0)
|
|
27
|
+
return null;
|
|
28
|
+
return snapshots[snapshots.length - 1] ?? null;
|
|
29
|
+
}
|
|
30
|
+
async list(threadId) {
|
|
31
|
+
const results = [];
|
|
32
|
+
for (const [key, snapshots] of this.snapshots.entries()) {
|
|
33
|
+
if (key.startsWith(`${threadId}|`)) {
|
|
34
|
+
results.push(...snapshots);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return results.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
38
|
+
}
|
|
39
|
+
async delete(config) {
|
|
40
|
+
this.snapshots.delete(this.key(config));
|
|
41
|
+
this.writes.delete(this.key(config));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a default MemoryCheckpointer instance.
|
|
46
|
+
*/
|
|
47
|
+
export function createMemoryCheckpointer() {
|
|
48
|
+
return new MemoryCheckpointer();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Serialize a StateSnapshot to a plain JSON-safe object.
|
|
52
|
+
*
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export function serializeSnapshot(snapshot) {
|
|
56
|
+
return {
|
|
57
|
+
values: snapshot.values,
|
|
58
|
+
next: snapshot.next,
|
|
59
|
+
config: snapshot.config,
|
|
60
|
+
metadata: snapshot.metadata,
|
|
61
|
+
createdAt: snapshot.createdAt,
|
|
62
|
+
parentConfig: snapshot.parentConfig,
|
|
63
|
+
tasks: snapshot.tasks,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Deserialize a StateSnapshot from a plain object.
|
|
68
|
+
*
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
export function deserializeSnapshot(raw) {
|
|
72
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
73
|
+
throw new CheckpointError("Snapshot must be an object", {
|
|
74
|
+
requestId: undefined,
|
|
75
|
+
traceId: undefined,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const v = raw;
|
|
79
|
+
return {
|
|
80
|
+
values: v.values ?? {},
|
|
81
|
+
next: (Array.isArray(v.next) ? v.next : []),
|
|
82
|
+
config: (typeof v.config === "object" && v.config !== null
|
|
83
|
+
? v.config
|
|
84
|
+
: { threadId: "" }),
|
|
85
|
+
metadata: (typeof v.metadata === "object" && v.metadata !== null
|
|
86
|
+
? v.metadata
|
|
87
|
+
: {}),
|
|
88
|
+
createdAt: typeof v.createdAt === "string" ? v.createdAt : new Date().toISOString(),
|
|
89
|
+
parentConfig: v.parentConfig === null || v.parentConfig === undefined
|
|
90
|
+
? null
|
|
91
|
+
: v.parentConfig,
|
|
92
|
+
tasks: (Array.isArray(v.tasks) ? v.tasks : []),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create a new StateSnapshot.
|
|
97
|
+
*/
|
|
98
|
+
export function createSnapshot(config, values, options) {
|
|
99
|
+
return {
|
|
100
|
+
values,
|
|
101
|
+
next: options?.next ?? [],
|
|
102
|
+
config,
|
|
103
|
+
metadata: options?.metadata ?? {},
|
|
104
|
+
createdAt: options?.createdAt ?? new Date().toISOString(),
|
|
105
|
+
parentConfig: options?.parentConfig ?? null,
|
|
106
|
+
tasks: options?.tasks ?? [],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=checkpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoint.js","sourceRoot":"","sources":["../../src/state/checkpoint.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAyG/C;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IACZ,SAAS,GAAG,IAAI,GAAG,EAA2B,CAAC;IAC/C,MAAM,GAAG,IAAI,GAAG,EAAwC,CAAC;IAElE,GAAG,CAAC,MAAmB;QAC7B,OAAO,GAAG,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,YAAY,IAAI,EAAE,EAAE,CAAC;IAC3D,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,MAAmB,EAAE,QAAuB;QACpD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QAC/C,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,MAAmB,EAAE,MAAoC;QACvE,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QAC5C,IAAI,CAAC,MAAM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E,CAAC,CAAC,QAAoC;YACzC,CAAC,CAAC,EAAE,CAAsC;QAC5C,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACnF,YAAY,EACV,CAAC,CAAC,YAAY,KAAK,IAAI,IAAI,CAAC,CAAC,YAAY,KAAK,SAAS;YACrD,CAAC,CAAC,IAAI;YACN,CAAC,CAAE,CAAC,CAAC,YAA4B;QACrC,KAAK,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAA2B;KACzE,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAmB,EACnB,MAAmB,EACnB,OAMC;IAED,OAAO;QACL,MAAM;QACN,IAAI,EAAE,OAAO,EAAE,IAAI,IAAI,EAAE;QACzB,MAAM;QACN,QAAQ,EAAE,OAAO,EAAE,QAAQ,IAAI,EAAE;QACjC,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACzD,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,IAAI;QAC3C,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,EAAE;KAC5B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { StateGraphDefinition, StateValues, PartialState } from "./graph-state.js";
|
|
2
|
+
import type { GraphConfig, StateSnapshot, Checkpointer, InterruptRecord } from "./checkpoint.js";
|
|
3
|
+
import type { Command } from "./interrupt.js";
|
|
4
|
+
/**
|
|
5
|
+
* Result of a graph execution step.
|
|
6
|
+
*/
|
|
7
|
+
export interface StepResult {
|
|
8
|
+
readonly snapshot: StateSnapshot;
|
|
9
|
+
readonly completed: boolean;
|
|
10
|
+
readonly output?: unknown;
|
|
11
|
+
readonly interrupt?: InterruptRecord;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Node function type — receives current state and returns a partial delta
|
|
15
|
+
* (or a Command for control flow).
|
|
16
|
+
*/
|
|
17
|
+
export type GraphNode = (state: StateValues, config: GraphConfig) => PartialState | Command | Promise<PartialState | Command>;
|
|
18
|
+
/**
|
|
19
|
+
* Graph executor — drives a compiled StateGraphDefinition node by node,
|
|
20
|
+
* applying deltas via reducers and checkpointing at super-step boundaries.
|
|
21
|
+
*/
|
|
22
|
+
export declare class StateGraphExecutor {
|
|
23
|
+
private readonly definition;
|
|
24
|
+
private readonly checkpointer;
|
|
25
|
+
private readonly nodes;
|
|
26
|
+
constructor(definition: StateGraphDefinition, checkpointer: Checkpointer);
|
|
27
|
+
/** Register a node implementation by id. */
|
|
28
|
+
addNode(nodeId: string, node: GraphNode): this;
|
|
29
|
+
/** Run the graph from the beginning (or from checkpointId if provided). */
|
|
30
|
+
run(entryNode: string, config: GraphConfig): Promise<StepResult>;
|
|
31
|
+
/** Resume from an interrupt with a Command value. */
|
|
32
|
+
resume(config: GraphConfig, command: Command): Promise<StepResult>;
|
|
33
|
+
/** List all snapshots for a thread (time-travel history). */
|
|
34
|
+
history(threadId: string): Promise<readonly StateSnapshot[]>;
|
|
35
|
+
private initialState;
|
|
36
|
+
private applyDelta;
|
|
37
|
+
private isCommandLike;
|
|
38
|
+
private makeSnapshot;
|
|
39
|
+
/** Ensure every persisted snapshot carries a stable checkpointId for resume/time-travel. */
|
|
40
|
+
private ensureCheckpointId;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/state/executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACx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Aa;IASrB,OAAO,CAAC,YAAY;IAuBpB,4FAA4F;IAC5F,OAAO,CAAC,kBAAkB;CAa3B"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { DurableStateError, CheckpointError, GraphInterrupt, } from "../errors.js";
|
|
2
|
+
import { isOverwrite } from "./channel.js";
|
|
3
|
+
/**
|
|
4
|
+
* Graph executor — drives a compiled StateGraphDefinition node by node,
|
|
5
|
+
* applying deltas via reducers and checkpointing at super-step boundaries.
|
|
6
|
+
*/
|
|
7
|
+
export class StateGraphExecutor {
|
|
8
|
+
definition;
|
|
9
|
+
checkpointer;
|
|
10
|
+
nodes = new Map();
|
|
11
|
+
constructor(definition, checkpointer) {
|
|
12
|
+
this.definition = definition;
|
|
13
|
+
this.checkpointer = checkpointer;
|
|
14
|
+
}
|
|
15
|
+
/** Register a node implementation by id. */
|
|
16
|
+
addNode(nodeId, node) {
|
|
17
|
+
this.nodes.set(nodeId, node);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
/** Run the graph from the beginning (or from checkpointId if provided). */
|
|
21
|
+
async run(entryNode, config) {
|
|
22
|
+
let current;
|
|
23
|
+
let next;
|
|
24
|
+
let parentConfig = null;
|
|
25
|
+
const checkpointId = config.checkpointId;
|
|
26
|
+
if (checkpointId !== undefined && checkpointId.length > 0) {
|
|
27
|
+
const existing = await this.checkpointer.getTuple(config);
|
|
28
|
+
if (existing === null) {
|
|
29
|
+
throw new CheckpointError(`Checkpoint not found: ${checkpointId}`, { threadId: config.threadId, checkpointId });
|
|
30
|
+
}
|
|
31
|
+
current = { ...existing.values };
|
|
32
|
+
next = [...existing.next];
|
|
33
|
+
parentConfig = existing.config;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
current = this.initialState();
|
|
37
|
+
next = [entryNode];
|
|
38
|
+
}
|
|
39
|
+
const tasks = [];
|
|
40
|
+
let output;
|
|
41
|
+
let completed = false;
|
|
42
|
+
let interrupt;
|
|
43
|
+
if (next.length === 0) {
|
|
44
|
+
completed = true;
|
|
45
|
+
const finalSnapshot = this.ensureCheckpointId(this.makeSnapshot(config, current, next, config, []), config);
|
|
46
|
+
await this.checkpointer.put(config, finalSnapshot);
|
|
47
|
+
return { snapshot: finalSnapshot, completed, output: undefined, interrupt: undefined };
|
|
48
|
+
}
|
|
49
|
+
while (next.length > 0) {
|
|
50
|
+
const nodeId = next[0];
|
|
51
|
+
next = next.slice(1);
|
|
52
|
+
const node = this.nodes.get(nodeId);
|
|
53
|
+
if (node === undefined) {
|
|
54
|
+
throw new DurableStateError("NODE_NOT_FOUND", `Node "${nodeId}" not registered in graph executor`, {
|
|
55
|
+
context: { nodeId, graphId: this.definition.id },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const preSnapshot = this.ensureCheckpointId(this.makeSnapshot(config, current, next, parentConfig, tasks), config);
|
|
59
|
+
await this.checkpointer.put(config, preSnapshot);
|
|
60
|
+
try {
|
|
61
|
+
const result = await node(current, config);
|
|
62
|
+
if (this.isCommandLike(result)) {
|
|
63
|
+
if (result.resume !== undefined) {
|
|
64
|
+
if (result.update !== undefined) {
|
|
65
|
+
current = this.applyDelta(current, result.update);
|
|
66
|
+
}
|
|
67
|
+
completed = next.length === 0;
|
|
68
|
+
output = result.resume;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (result.goto !== undefined) {
|
|
72
|
+
next = [result.goto, ...next];
|
|
73
|
+
if (result.update !== undefined) {
|
|
74
|
+
current = this.applyDelta(current, result.update);
|
|
75
|
+
}
|
|
76
|
+
completed = next.length === 0;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (result.update !== undefined) {
|
|
80
|
+
current = this.applyDelta(current, result.update);
|
|
81
|
+
completed = next.length === 0;
|
|
82
|
+
}
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
current = this.applyDelta(current, result);
|
|
86
|
+
const postSnapshot = this.ensureCheckpointId(this.makeSnapshot(config, current, next, config, tasks), config);
|
|
87
|
+
await this.checkpointer.put(config, postSnapshot);
|
|
88
|
+
completed = next.length === 0;
|
|
89
|
+
output = result;
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
if (err instanceof GraphInterrupt) {
|
|
93
|
+
interrupt = {
|
|
94
|
+
id: err.interruptId,
|
|
95
|
+
value: err.interruptValue,
|
|
96
|
+
resumed: false,
|
|
97
|
+
};
|
|
98
|
+
tasks.push({
|
|
99
|
+
id: err.interruptId,
|
|
100
|
+
name: nodeId,
|
|
101
|
+
interrupts: [{ id: err.interruptId, value: err.interruptValue, resumed: false }],
|
|
102
|
+
state: current,
|
|
103
|
+
});
|
|
104
|
+
completed = false;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const finalSnapshot = this.ensureCheckpointId(this.makeSnapshot(config, current, next, config, tasks), config);
|
|
111
|
+
await this.checkpointer.put(config, finalSnapshot);
|
|
112
|
+
return { snapshot: finalSnapshot, completed, output, interrupt };
|
|
113
|
+
}
|
|
114
|
+
/** Resume from an interrupt with a Command value. */
|
|
115
|
+
async resume(config, command) {
|
|
116
|
+
const existing = await this.checkpointer.getTuple(config);
|
|
117
|
+
if (existing === null) {
|
|
118
|
+
throw new CheckpointError(`No checkpoint to resume from for thread ${config.threadId}`, { threadId: config.threadId });
|
|
119
|
+
}
|
|
120
|
+
let current = { ...existing.values };
|
|
121
|
+
let next = [...existing.next];
|
|
122
|
+
if (command.update !== undefined) {
|
|
123
|
+
current = this.applyDelta(current, command.update);
|
|
124
|
+
}
|
|
125
|
+
const tasks = existing.tasks.map((t) => ({
|
|
126
|
+
...t,
|
|
127
|
+
interrupts: t.interrupts.map((i) => ({ ...i, resumed: true })),
|
|
128
|
+
}));
|
|
129
|
+
let output;
|
|
130
|
+
let completed = next.length === 0;
|
|
131
|
+
let interrupt;
|
|
132
|
+
while (next.length > 0) {
|
|
133
|
+
const nodeId = next[0];
|
|
134
|
+
next = next.slice(1);
|
|
135
|
+
const node = this.nodes.get(nodeId);
|
|
136
|
+
if (node === undefined) {
|
|
137
|
+
throw new DurableStateError("NODE_NOT_FOUND", `Node "${nodeId}" not registered in graph executor`, {
|
|
138
|
+
context: { nodeId, graphId: this.definition.id },
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const preSnapshot = this.makeSnapshot(config, current, next, config, tasks);
|
|
142
|
+
await this.checkpointer.put(config, preSnapshot);
|
|
143
|
+
try {
|
|
144
|
+
const result = await node(current, config);
|
|
145
|
+
if (this.isCommandLike(result)) {
|
|
146
|
+
if (result.resume !== undefined) {
|
|
147
|
+
if (result.update !== undefined) {
|
|
148
|
+
current = this.applyDelta(current, result.update);
|
|
149
|
+
}
|
|
150
|
+
completed = next.length === 0;
|
|
151
|
+
output = result.resume;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (result.goto !== undefined) {
|
|
155
|
+
next = [result.goto, ...next];
|
|
156
|
+
if (result.update !== undefined) {
|
|
157
|
+
current = this.applyDelta(current, result.update);
|
|
158
|
+
}
|
|
159
|
+
completed = next.length === 0;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (result.update !== undefined) {
|
|
163
|
+
current = this.applyDelta(current, result.update);
|
|
164
|
+
completed = next.length === 0;
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
current = this.applyDelta(current, result);
|
|
169
|
+
const postSnapshot = this.ensureCheckpointId(this.makeSnapshot(config, current, next, config, tasks), config);
|
|
170
|
+
await this.checkpointer.put(config, postSnapshot);
|
|
171
|
+
completed = next.length === 0;
|
|
172
|
+
output = result;
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
if (err instanceof GraphInterrupt) {
|
|
176
|
+
interrupt = {
|
|
177
|
+
id: err.interruptId,
|
|
178
|
+
value: err.interruptValue,
|
|
179
|
+
resumed: false,
|
|
180
|
+
};
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
throw err;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const finalSnapshot = this.ensureCheckpointId(this.makeSnapshot(config, current, next, config, tasks), config);
|
|
187
|
+
await this.checkpointer.put(config, finalSnapshot);
|
|
188
|
+
return { snapshot: finalSnapshot, completed, output, interrupt };
|
|
189
|
+
}
|
|
190
|
+
/** List all snapshots for a thread (time-travel history). */
|
|
191
|
+
async history(threadId) {
|
|
192
|
+
return this.checkpointer.list(threadId);
|
|
193
|
+
}
|
|
194
|
+
initialState() {
|
|
195
|
+
const values = {};
|
|
196
|
+
for (const channel of this.definition.channels) {
|
|
197
|
+
values[channel.name] = channel.default;
|
|
198
|
+
}
|
|
199
|
+
return values;
|
|
200
|
+
}
|
|
201
|
+
applyDelta(current, delta) {
|
|
202
|
+
const next = { ...current };
|
|
203
|
+
for (const [channelName, update] of Object.entries(delta)) {
|
|
204
|
+
const channel = this.definition.channels.find((c) => c.name === channelName);
|
|
205
|
+
if (channel === undefined) {
|
|
206
|
+
throw new DurableStateError("CHANNEL_NOT_FOUND", `Channel "${channelName}" not declared in graph "${this.definition.id}"`, {
|
|
207
|
+
context: { channel: channelName, graphId: this.definition.id },
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
if (isOverwrite(update)) {
|
|
211
|
+
next[channelName] = update.value;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
next[channelName] = channel.reducer(next[channelName], update);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return next;
|
|
218
|
+
}
|
|
219
|
+
isCommandLike(value) {
|
|
220
|
+
return (typeof value === "object" &&
|
|
221
|
+
value !== null &&
|
|
222
|
+
!Array.isArray(value) &&
|
|
223
|
+
("resume" in value || "update" in value || "goto" in value || "graph" in value));
|
|
224
|
+
}
|
|
225
|
+
makeSnapshot(config, values, next, parentConfig, tasks) {
|
|
226
|
+
return {
|
|
227
|
+
values,
|
|
228
|
+
next,
|
|
229
|
+
config: { ...config },
|
|
230
|
+
metadata: {
|
|
231
|
+
source: "loop",
|
|
232
|
+
step: next.length,
|
|
233
|
+
graphId: this.definition.id,
|
|
234
|
+
graphName: this.definition.name,
|
|
235
|
+
},
|
|
236
|
+
createdAt: new Date().toISOString(),
|
|
237
|
+
parentConfig,
|
|
238
|
+
tasks,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/** Ensure every persisted snapshot carries a stable checkpointId for resume/time-travel. */
|
|
242
|
+
ensureCheckpointId(snapshot, _config) {
|
|
243
|
+
if (snapshot.config.checkpointId)
|
|
244
|
+
return snapshot;
|
|
245
|
+
return {
|
|
246
|
+
...snapshot,
|
|
247
|
+
config: {
|
|
248
|
+
...snapshot.config,
|
|
249
|
+
checkpointId: `cp_${crypto.randomUUID()}`,
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
//# sourceMappingURL=executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../src/state/executor.ts"],"names":[],"mappings":"AASA,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,cAAc,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAqB3C;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IACZ,UAAU,CAAuB;IACjC,YAAY,CAAe;IAC3B,KAAK,GAAG,IAAI,GAAG,EAAqB,CAAC;IAEtD,YAAY,UAAgC,EAAE,YAA0B;QACtE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED,4CAA4C;IAC5C,OAAO,CAAC,MAAc,EAAE,IAAe;QACrC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,2EAA2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AAK,CAAC,CAChF,CAAC;IACJ,CAAC;IAEO,YAAY,CAClB,MAAmB,EACnB,MAAmB,EACnB,IAAc,EACd,YAAgC,EAChC,KAA6B;QAE7B,OAAO;YACL,MAAM;YACN,IAAI;YACJ,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE;YACrB,QAAQ,EAAE;gBACR,MAAM,EAAE,MAAM;gBACd,IAAI,EAAE,IAAI,CAAC,MAAM;gBACjB,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,EAAE;gBAC3B,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI;aAChC;YACD,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,YAAY;YACZ,KAAK;SACN,CAAC;IACJ,CAAC;IAED,4FAA4F;IACpF,kBAAkB,CACxB,QAAuB,EACvB,OAAoB;QAEpB,IAAI,QAAQ,CAAC,MAAM,CAAC,YAAY;YAAE,OAAO,QAAQ,CAAC;QAClD,OAAO;YACL,GAAG,QAAQ;YACX,MAAM,EAAE;gBACN,GAAG,QAAQ,CAAC,MAAM;gBAClB,YAAY,EAAE,MAAM,MAAM,CAAC,UAAU,EAAE,EAAE;aAC1C;SACF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { StateChannel, Overwrite } from "./channel.js";
|
|
2
|
+
/**
|
|
3
|
+
* Typed state schema definition — a map of channel name to channel definition.
|
|
4
|
+
*
|
|
5
|
+
* This is the equivalent of LangGraph's `Annotation.Root`. Channels declared
|
|
6
|
+
* here are the only writable state slots in the graph.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { StateSchema, createChannel } from "@vinhnt-sdk/durable-state";
|
|
11
|
+
*
|
|
12
|
+
* const schema: StateSchema = {
|
|
13
|
+
* messages: createChannel<MessageContentPart[]>("messages", {
|
|
14
|
+
* reducer: appendReducer(),
|
|
15
|
+
* default: [],
|
|
16
|
+
* }),
|
|
17
|
+
* step: createChannel<number>("step", {
|
|
18
|
+
* reducer: overwriteReducer(),
|
|
19
|
+
* default: 0,
|
|
20
|
+
* }),
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export interface StateSchema {
|
|
25
|
+
readonly [channelName: string]: StateChannel<any, any>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Resolved state values — a map of channel name to current value.
|
|
29
|
+
*/
|
|
30
|
+
export interface StateValues {
|
|
31
|
+
readonly [channelName: string]: unknown;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A partial state delta — updates for specific channels only.
|
|
35
|
+
*
|
|
36
|
+
* Nodes return `PartialState` (or `PartialState` wrapped in `Overwrite`) to
|
|
37
|
+
* update channels without replacing the entire state.
|
|
38
|
+
*/
|
|
39
|
+
export interface PartialState {
|
|
40
|
+
readonly [channelName: string]: unknown | Overwrite<any>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Overwrite marker imported from channel module (re-exported for convenience).
|
|
44
|
+
*/
|
|
45
|
+
export type { Overwrite } from "./channel.js";
|
|
46
|
+
/**
|
|
47
|
+
* Build a typed state graph definition.
|
|
48
|
+
*
|
|
49
|
+
* The builder collects channels, reducers, and validation rules, then
|
|
50
|
+
* `build()` produces an executable graph definition.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { StateGraphBuilder } from "@vinhnt-sdk/durable-state";
|
|
55
|
+
*
|
|
56
|
+
* const builder = new StateGraphBuilder("my-graph");
|
|
57
|
+
* builder.addChannel("messages", createChannel("messages", []));
|
|
58
|
+
* builder.addChannel("step", createChannel("step", 0));
|
|
59
|
+
* const graph = builder.build();
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare class StateGraphBuilder {
|
|
63
|
+
private readonly id;
|
|
64
|
+
private readonly name;
|
|
65
|
+
private readonly description?;
|
|
66
|
+
private readonly channels;
|
|
67
|
+
private readonly reducers;
|
|
68
|
+
constructor(id: string, name: string, description?: string);
|
|
69
|
+
/** Register a channel in the graph state schema. */
|
|
70
|
+
addChannel<T, D = T>(name: string, channel: StateChannel<T, D>): this;
|
|
71
|
+
/** Override the reducer for an existing channel. */
|
|
72
|
+
setReducer<T, D>(name: string, reducer: (current: T, delta: D) => T): this;
|
|
73
|
+
/** Build and validate the state graph definition. */
|
|
74
|
+
build(): StateGraphDefinition;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Built, validated state graph definition — the immutable contract between
|
|
78
|
+
* the graph definition and its executor/checkpointer.
|
|
79
|
+
*/
|
|
80
|
+
export interface StateGraphDefinition {
|
|
81
|
+
readonly id: string;
|
|
82
|
+
readonly name: string;
|
|
83
|
+
readonly description?: string;
|
|
84
|
+
readonly channels: readonly StateChannel<any, any>[];
|
|
85
|
+
readonly reducers: readonly [string, (current: any, delta: any) => any][];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Look up a channel by name from a graph definition.
|
|
89
|
+
*
|
|
90
|
+
* @throws {ChannelNotFoundError} if the channel is not declared.
|
|
91
|
+
*/
|
|
92
|
+
export declare function findChannel(definition: StateGraphDefinition, name: string): StateChannel<any, any>;
|
|
93
|
+
/**
|
|
94
|
+
* Look up a reducer by channel name from a graph definition.
|
|
95
|
+
*/
|
|
96
|
+
export declare function findReducer(definition: StateGraphDefinition, name: string): (current: any, delta: any) => any;
|
|
97
|
+
/**
|
|
98
|
+
* Initialize state values from a graph definition — every channel gets its
|
|
99
|
+
* default value.
|
|
100
|
+
*/
|
|
101
|
+
export declare function initialState(definition: StateGraphDefinition): StateValues;
|
|
102
|
+
/**
|
|
103
|
+
* Apply a partial state delta to current values, running each channel's
|
|
104
|
+
* reducer. Channels not in the delta are left unchanged.
|
|
105
|
+
*
|
|
106
|
+
* @throws {ChannelNotFoundError} if the delta references an undeclared channel.
|
|
107
|
+
*/
|
|
108
|
+
export declare function applyDelta(definition: StateGraphDefinition, current: StateValues, delta: PartialState): StateValues;
|
|
109
|
+
/**
|
|
110
|
+
* Check whether a value is an Overwrite marker.
|
|
111
|
+
*/
|
|
112
|
+
export declare function isOverwrite(value: unknown): value is Overwrite<unknown>;
|
|
113
|
+
//# sourceMappingURL=graph-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-state.d.ts","sourceRoot":"","sources":["../../src/state/graph-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAG5D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,WAAW,EAAE,MAAM,GAAG,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;CACxD;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC;CACzC;AAED;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;CAC1D;AAED;;GAEG;AACH,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA6C;IACtE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAwD;gBAErE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM;IAM1D,oDAAoD;IACpD,UAAU,CAAC,CAAC,EAAE,CAAC,GAA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