@vinhnt-sdk/durable-state 0.0.0-stage → 0.1.0-beta.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,121 @@
|
|
|
1
|
+
import { DurableStateError, ChannelNotFoundError } from "../errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Build a typed state graph definition.
|
|
4
|
+
*
|
|
5
|
+
* The builder collects channels, reducers, and validation rules, then
|
|
6
|
+
* `build()` produces an executable graph definition.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { StateGraphBuilder } from "@vinhnt-sdk/durable-state";
|
|
11
|
+
*
|
|
12
|
+
* const builder = new StateGraphBuilder("my-graph");
|
|
13
|
+
* builder.addChannel("messages", createChannel("messages", []));
|
|
14
|
+
* builder.addChannel("step", createChannel("step", 0));
|
|
15
|
+
* const graph = builder.build();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export class StateGraphBuilder {
|
|
19
|
+
id;
|
|
20
|
+
name;
|
|
21
|
+
description;
|
|
22
|
+
channels = new Map();
|
|
23
|
+
reducers = new Map();
|
|
24
|
+
constructor(id, name, description) {
|
|
25
|
+
this.id = id;
|
|
26
|
+
this.name = name;
|
|
27
|
+
this.description = description;
|
|
28
|
+
}
|
|
29
|
+
/** Register a channel in the graph state schema. */
|
|
30
|
+
addChannel(name, channel) {
|
|
31
|
+
if (this.channels.has(name)) {
|
|
32
|
+
throw new DurableStateError("DUPLICATE_CHANNEL", `Channel "${name}" already registered in graph "${this.name}"`, { context: { name, graphId: this.id } });
|
|
33
|
+
}
|
|
34
|
+
this.channels.set(name, channel);
|
|
35
|
+
this.reducers.set(name, channel.reducer);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/** Override the reducer for an existing channel. */
|
|
39
|
+
setReducer(name, reducer) {
|
|
40
|
+
if (!this.channels.has(name)) {
|
|
41
|
+
throw new ChannelNotFoundError(name, this.id);
|
|
42
|
+
}
|
|
43
|
+
this.reducers.set(name, reducer);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/** Build and validate the state graph definition. */
|
|
47
|
+
build() {
|
|
48
|
+
if (this.channels.size === 0) {
|
|
49
|
+
throw new DurableStateError("EMPTY_SCHEMA", `Graph "${this.name}" has no channels — at least one channel is required`, { context: { graphId: this.id } });
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
id: this.id,
|
|
53
|
+
name: this.name,
|
|
54
|
+
...(this.description !== undefined ? { description: this.description } : {}),
|
|
55
|
+
channels: [...this.channels.values()],
|
|
56
|
+
reducers: [...this.reducers.entries()],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Look up a channel by name from a graph definition.
|
|
62
|
+
*
|
|
63
|
+
* @throws {ChannelNotFoundError} if the channel is not declared.
|
|
64
|
+
*/
|
|
65
|
+
export function findChannel(definition, name) {
|
|
66
|
+
const channel = definition.channels.find((c) => c.name === name);
|
|
67
|
+
if (channel === undefined) {
|
|
68
|
+
throw new ChannelNotFoundError(name, definition.id);
|
|
69
|
+
}
|
|
70
|
+
return channel;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Look up a reducer by channel name from a graph definition.
|
|
74
|
+
*/
|
|
75
|
+
export function findReducer(definition, name) {
|
|
76
|
+
const entry = definition.reducers.find(([n]) => n === name);
|
|
77
|
+
if (entry === undefined) {
|
|
78
|
+
throw new ChannelNotFoundError(name, definition.id);
|
|
79
|
+
}
|
|
80
|
+
return entry[1];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Initialize state values from a graph definition — every channel gets its
|
|
84
|
+
* default value.
|
|
85
|
+
*/
|
|
86
|
+
export function initialState(definition) {
|
|
87
|
+
const values = {};
|
|
88
|
+
for (const channel of definition.channels) {
|
|
89
|
+
values[channel.name] = channel.default;
|
|
90
|
+
}
|
|
91
|
+
return values;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Apply a partial state delta to current values, running each channel's
|
|
95
|
+
* reducer. Channels not in the delta are left unchanged.
|
|
96
|
+
*
|
|
97
|
+
* @throws {ChannelNotFoundError} if the delta references an undeclared channel.
|
|
98
|
+
*/
|
|
99
|
+
export function applyDelta(definition, current, delta) {
|
|
100
|
+
const next = { ...current };
|
|
101
|
+
for (const [channelName, update] of Object.entries(delta)) {
|
|
102
|
+
const reducer = findReducer(definition, channelName);
|
|
103
|
+
if (update !== null && typeof update === "object" && "__overwriteBrand" in update) {
|
|
104
|
+
// Overwrite marker — bypass reducer
|
|
105
|
+
next[channelName] = update.value;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
next[channelName] = reducer(next[channelName], update);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return next;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Check whether a value is an Overwrite marker.
|
|
115
|
+
*/
|
|
116
|
+
export function isOverwrite(value) {
|
|
117
|
+
return (typeof value === "object" &&
|
|
118
|
+
value !== null &&
|
|
119
|
+
"__overwriteBrand" in value);
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=graph-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-state.js","sourceRoot":"","sources":["../../src/state/graph-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAkDvE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,iBAAiB;IACX,EAAE,CAAS;IACX,IAAI,CAAS;IACb,WAAW,CAAU;IACrB,QAAQ,GAAG,IAAI,GAAG,EAAkC,CAAC;IACrD,QAAQ,GAAG,IAAI,GAAG,EAA6C,CAAC;IAEjF,YAAY,EAAU,EAAE,IAAY,EAAE,WAAoB;QACxD,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAED,oDAAoD;IACpD,UAAU,CAAW,IAAY,EAAE,OAA2B;QAC5D,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,iBAAiB,CACz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GAA4B,EAAE,GAAG,OAAO,EAAE,CAAC;IACrD,KAAK,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1D,MAAM,OAAO,GAAG,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;QACrD,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,kBAAkB,IAAI,MAAM,EAAE,CAAC;YAClF,oCAAoC;YACpC,IAAI,CAAC,WAAW,CAAC,GAAI,MAA6B,CAAC,KAAK,CAAC;QAC3D,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,MAAa,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IACD,OAAO,IAAmB,CAAC;AAC7B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,kBAAkB,IAAI,KAAK,CAC5B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Command returned by a node or passed to resume a graph.
|
|
3
|
+
*
|
|
4
|
+
* Commands combine state updates with control-flow instructions:
|
|
5
|
+
* - `resume` — provide the value an interrupted node is waiting for
|
|
6
|
+
* - `update` — apply state changes before continuing
|
|
7
|
+
* - `goto` — jump to a specific node
|
|
8
|
+
* - `graph` — navigate to a parent graph (Command.PARENT) or subgraph
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Resume an interrupt with a value
|
|
13
|
+
* const cmd = Command.resume("user approved");
|
|
14
|
+
*
|
|
15
|
+
* // Resume + update state + goto a node
|
|
16
|
+
* const cmd = Command({
|
|
17
|
+
* resume: "approved",
|
|
18
|
+
* update: { step: 5 },
|
|
19
|
+
* goto: "next-node",
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export interface Command<ResumeValue = unknown> {
|
|
24
|
+
/** Resume value for an interrupt — if set, the graph continues from interrupt. */
|
|
25
|
+
readonly resume?: ResumeValue;
|
|
26
|
+
/** State delta to apply before continuing. */
|
|
27
|
+
readonly update?: Record<string, unknown>;
|
|
28
|
+
/** Node to jump to next. */
|
|
29
|
+
readonly goto?: string;
|
|
30
|
+
/** Navigate to parent graph (Command.PARENT) or a named subgraph. */
|
|
31
|
+
readonly graph?: string | typeof Command.PARENT;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Factory functions for creating {@link Command} instances.
|
|
35
|
+
*/
|
|
36
|
+
export declare const Command: {
|
|
37
|
+
/** Special value to navigate from a subgraph back to its parent graph. */
|
|
38
|
+
readonly PARENT: "__parent__";
|
|
39
|
+
/** Create a resume command — continue from an interrupt with a value. */
|
|
40
|
+
readonly resume: <Value = unknown>(value: Value) => Command<Value>;
|
|
41
|
+
/** Create a resume command with state update. */
|
|
42
|
+
readonly resumeWithUpdate: <Value = unknown>(value: Value, update: Record<string, unknown>) => Command<Value>;
|
|
43
|
+
/** Create a resume command that jumps to a node. */
|
|
44
|
+
readonly resumeAndGoto: <Value = unknown>(value: Value, goto: string) => Command<Value>;
|
|
45
|
+
/** Create an update-only command (no resume). */
|
|
46
|
+
readonly update: (update: Record<string, unknown>) => Command;
|
|
47
|
+
/** Create a goto-only command. */
|
|
48
|
+
readonly goto: (node: string) => Command;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Result of calling interrupt() inside a node — never returns normally.
|
|
52
|
+
* Thrown as GraphInterrupt to pause execution.
|
|
53
|
+
*/
|
|
54
|
+
export interface InterruptResult<Value = unknown> {
|
|
55
|
+
readonly type: "interrupt";
|
|
56
|
+
readonly value: Value;
|
|
57
|
+
readonly interruptId: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Pause graph execution at this point and wait for external input.
|
|
61
|
+
*
|
|
62
|
+
* The graph saves a checkpoint and throws GraphInterrupt. On resume with
|
|
63
|
+
* `Command({ resume: value })`, the node that called interrupt() receives
|
|
64
|
+
* `value` as its return value.
|
|
65
|
+
*
|
|
66
|
+
* @typeParam T - The type of value expected on resume.
|
|
67
|
+
* @param value - The interrupt payload (what the UI shows to the user).
|
|
68
|
+
* @param options - Interrupt options (nodeId, interruptId).
|
|
69
|
+
* @returns Never returns — throws GraphInterrupt.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // Inside a node function:
|
|
74
|
+
* const userChoice = interrupt("Approve this action?", { nodeId: "approval-node" });
|
|
75
|
+
* // Execution pauses here. On resume:
|
|
76
|
+
* // const userChoice = "yes"; // value from Command({ resume: "yes" })
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function interrupt<T = unknown>(value: T, options?: {
|
|
80
|
+
nodeId?: string;
|
|
81
|
+
interruptId?: string;
|
|
82
|
+
}): never;
|
|
83
|
+
/**
|
|
84
|
+
* Type guard: is a value a Command?
|
|
85
|
+
*/
|
|
86
|
+
export declare function isCommand(value: unknown): value is Command;
|
|
87
|
+
/**
|
|
88
|
+
* Branded type for an interrupt id.
|
|
89
|
+
*/
|
|
90
|
+
export type InterruptId = string & {
|
|
91
|
+
readonly __interruptBrand: unique symbol;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Create a branded interrupt id.
|
|
95
|
+
*/
|
|
96
|
+
export declare function interruptId(id: string): InterruptId;
|
|
97
|
+
//# sourceMappingURL=interrupt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interrupt.d.ts","sourceRoot":"","sources":["../../src/state/interrupt.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,OAAO,CAAC,WAAW,GAAG,OAAO;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AK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAO1D;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,gBAAgB,EAAE,OAAO,MAAM,CAAA;CAAE,CAAC;AAEhF;;GAEG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW,CAEnD"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { GraphInterrupt } from "../errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Factory functions for creating {@link Command} instances.
|
|
4
|
+
*/
|
|
5
|
+
export const Command = {
|
|
6
|
+
/** Special value to navigate from a subgraph back to its parent graph. */
|
|
7
|
+
PARENT: "__parent__",
|
|
8
|
+
/** Create a resume command — continue from an interrupt with a value. */
|
|
9
|
+
resume(value) {
|
|
10
|
+
return { resume: value };
|
|
11
|
+
},
|
|
12
|
+
/** Create a resume command with state update. */
|
|
13
|
+
resumeWithUpdate(value, update) {
|
|
14
|
+
return { resume: value, update };
|
|
15
|
+
},
|
|
16
|
+
/** Create a resume command that jumps to a node. */
|
|
17
|
+
resumeAndGoto(value, goto) {
|
|
18
|
+
return { resume: value, goto };
|
|
19
|
+
},
|
|
20
|
+
/** Create an update-only command (no resume). */
|
|
21
|
+
update(update) {
|
|
22
|
+
return { update };
|
|
23
|
+
},
|
|
24
|
+
/** Create a goto-only command. */
|
|
25
|
+
goto(node) {
|
|
26
|
+
return { goto: node };
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Pause graph execution at this point and wait for external input.
|
|
31
|
+
*
|
|
32
|
+
* The graph saves a checkpoint and throws GraphInterrupt. On resume with
|
|
33
|
+
* `Command({ resume: value })`, the node that called interrupt() receives
|
|
34
|
+
* `value` as its return value.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam T - The type of value expected on resume.
|
|
37
|
+
* @param value - The interrupt payload (what the UI shows to the user).
|
|
38
|
+
* @param options - Interrupt options (nodeId, interruptId).
|
|
39
|
+
* @returns Never returns — throws GraphInterrupt.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* // Inside a node function:
|
|
44
|
+
* const userChoice = interrupt("Approve this action?", { nodeId: "approval-node" });
|
|
45
|
+
* // Execution pauses here. On resume:
|
|
46
|
+
* // const userChoice = "yes"; // value from Command({ resume: "yes" })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function interrupt(value, options) {
|
|
50
|
+
throw new GraphInterrupt(value, {
|
|
51
|
+
nodeId: options?.nodeId,
|
|
52
|
+
interruptId: options?.interruptId,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Type guard: is a value a Command?
|
|
57
|
+
*/
|
|
58
|
+
export function isCommand(value) {
|
|
59
|
+
return (typeof value === "object" &&
|
|
60
|
+
value !== null &&
|
|
61
|
+
!Array.isArray(value) &&
|
|
62
|
+
("resume" in value || "update" in value || "goto" in value || "graph" in value));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Create a branded interrupt id.
|
|
66
|
+
*/
|
|
67
|
+
export function interruptId(id) {
|
|
68
|
+
return id;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=interrupt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interrupt.js","sourceRoot":"","sources":["../../src/state/interrupt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAmC9C;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,0EAA0E;IAC1E,MAAM,EAAE,YAAqB;IAE7B,yEAAyE;IACzE,MAAM,CAAkB,KAAY;QAClC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAC3B,CAAC;IAED,iDAAiD;IACjD,gBAAgB,CACd,KAAY,EACZ,MAA+B;QAE/B,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;IAED,oDAAoD;IACpD,aAAa,CACX,KAAY,EACZ,IAAY;QAEZ,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACjC,CAAC;IAED,iDAAiD;IACjD,MAAM,CAAC,MAA+B;QACpC,OAAO,EAAE,MAAM,EAAE,CAAC;IACpB,CAAC;IAED,kCAAkC;IAClC,IAAI,CAAC,IAAY;QACf,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;CACO,CAAC;AAYX;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,SAAS,CACvB,KAAQ,EACR,OAAmD;IAEnD,MAAM,IAAI,cAAc,CAAC,KAAK,EAAE;QAC9B,MAAM,EAAE,OAAO,EAAE,MAAM;QACvB,WAAW,EAAE,OAAO,EAAE,WAAW;KAClC,CAAU,CAAC;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,CAAC,QAAQ,IAAI,KAAK,IAAI,QAAQ,IAAI,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,OAAO,IAAI,KAAK,CAAC,CAChF,CAAC;AACJ,CAAC;AAOD;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,EAAU;IACpC,OAAO,EAAiB,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { SerializedRunState } from "@vinhnt-sdk/session";
|
|
2
|
+
import type { StateSnapshot, GraphConfig, Checkpointer } from "./checkpoint.js";
|
|
3
|
+
import { StateGraphDefinition } from "./graph-state.js";
|
|
4
|
+
import { ChannelNotFoundError } from "../errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* Convert a {@link SerializedRunState} into a {@link StateSnapshot}.
|
|
7
|
+
*
|
|
8
|
+
* The run state's `messages`, `step`, `toolCallCount`, `model`, `provider`,
|
|
9
|
+
* `agentId`, `finalOutput` and `userContentParts` are packed into the
|
|
10
|
+
* snapshot's `values` under well-known channel names so they survive
|
|
11
|
+
* checkpoint/resume cycles.
|
|
12
|
+
*
|
|
13
|
+
* @param runState - The serialized run state to convert.
|
|
14
|
+
* @param config - Graph config (threadId = sessionId).
|
|
15
|
+
* @returns A StateSnapshot capturing the run state.
|
|
16
|
+
*/
|
|
17
|
+
export declare function runStateToSnapshot(runState: SerializedRunState, config: GraphConfig): StateSnapshot;
|
|
18
|
+
/**
|
|
19
|
+
* Convert a {@link StateSnapshot} back into a {@link SerializedRunState}.
|
|
20
|
+
*
|
|
21
|
+
* Extracts well-known channel names from `values` and maps them to the
|
|
22
|
+
* run state fields. Unknown channels are dropped (they belong to the
|
|
23
|
+
* durable-state channel layer, not the run loop).
|
|
24
|
+
*
|
|
25
|
+
* @param snapshot - The snapshot to convert.
|
|
26
|
+
* @returns A SerializedRunState.
|
|
27
|
+
*/
|
|
28
|
+
export declare function snapshotToRunState(snapshot: StateSnapshot): SerializedRunState;
|
|
29
|
+
/**
|
|
30
|
+
* Build an initial {@link StateSnapshot} from a {@link StateGraphDefinition}
|
|
31
|
+
* and a {@link GraphConfig} — all channels set to their defaults.
|
|
32
|
+
*
|
|
33
|
+
* @param definition - The compiled state graph definition.
|
|
34
|
+
* @param config - Graph config (threadId = sessionId).
|
|
35
|
+
* @returns A StateSnapshot with default channel values.
|
|
36
|
+
*/
|
|
37
|
+
export declare function snapshotFromDefinition(definition: StateGraphDefinition, config: GraphConfig): StateSnapshot;
|
|
38
|
+
/**
|
|
39
|
+
* Apply a `PartialState` delta to a snapshot's values using the graph
|
|
40
|
+
* definition's reducers, returning a new snapshot.
|
|
41
|
+
*
|
|
42
|
+
* @param definition - The compiled state graph definition.
|
|
43
|
+
* @param snapshot - The current snapshot.
|
|
44
|
+
* @param delta - Partial state delta to apply.
|
|
45
|
+
* @returns A new snapshot with updated values.
|
|
46
|
+
*/
|
|
47
|
+
export declare function applyDeltaToSnapshot(definition: StateGraphDefinition, snapshot: StateSnapshot, delta: Record<string, unknown>): StateSnapshot;
|
|
48
|
+
/**
|
|
49
|
+
* A Checkpointer backed by a vinhnt {@link SessionStore} and {@link RunEventStore}.
|
|
50
|
+
*
|
|
51
|
+
* Snapshots are serialized as JSON and stored via the event store's
|
|
52
|
+
* `saveSnapshot`/`getSnapshot` (per runId = threadId). The runId doubles as
|
|
53
|
+
* the graph threadId, so `threadId` maps 1:1 to a durable run.
|
|
54
|
+
*
|
|
55
|
+
* Requires `@vinhnt-sdk/session` to be installed (peer dependency).
|
|
56
|
+
*/
|
|
57
|
+
export declare class SessionStoreCheckpointer implements Checkpointer {
|
|
58
|
+
private readonly eventStore;
|
|
59
|
+
constructor(eventStore: {
|
|
60
|
+
saveSnapshot(runId: string, state: Record<string, unknown>): Promise<void>;
|
|
61
|
+
getSnapshot(runId: string): Promise<{
|
|
62
|
+
state: Record<string, unknown>;
|
|
63
|
+
} | null>;
|
|
64
|
+
});
|
|
65
|
+
put(config: GraphConfig, snapshot: StateSnapshot): Promise<void>;
|
|
66
|
+
putWrites(_config: GraphConfig, _writes: readonly [string, unknown][]): Promise<void>;
|
|
67
|
+
getTuple(config: GraphConfig): Promise<StateSnapshot | null>;
|
|
68
|
+
list(_threadId: string): Promise<readonly StateSnapshot[]>;
|
|
69
|
+
delete(_config: GraphConfig): Promise<void>;
|
|
70
|
+
private deserialize;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Type guard: is a value a ChannelNotFoundError?
|
|
74
|
+
*/
|
|
75
|
+
export declare function isChannelNotFoundError(value: unknown): value is ChannelNotFoundError;
|
|
76
|
+
//# sourceMappingURL=session-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-adapter.d.ts","sourceRoot":"","sources":["../../src/state/session-adapter.ts"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E,SAAS,CACb,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GACpC,OAAO,CAAC,IAAI,CAAC;IAIV,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAM5D,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC;IAK1D,MAAM,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAIjD,OAAO,CAAC,WAAW;CAkBpB;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,oBAAoB,CAE/B"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { createSnapshot } from "./checkpoint.js";
|
|
2
|
+
import { applyDelta, initialState } from "./graph-state.js";
|
|
3
|
+
import { ChannelNotFoundError } from "../errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* Convert a {@link SerializedRunState} into a {@link StateSnapshot}.
|
|
6
|
+
*
|
|
7
|
+
* The run state's `messages`, `step`, `toolCallCount`, `model`, `provider`,
|
|
8
|
+
* `agentId`, `finalOutput` and `userContentParts` are packed into the
|
|
9
|
+
* snapshot's `values` under well-known channel names so they survive
|
|
10
|
+
* checkpoint/resume cycles.
|
|
11
|
+
*
|
|
12
|
+
* @param runState - The serialized run state to convert.
|
|
13
|
+
* @param config - Graph config (threadId = sessionId).
|
|
14
|
+
* @returns A StateSnapshot capturing the run state.
|
|
15
|
+
*/
|
|
16
|
+
export function runStateToSnapshot(runState, config) {
|
|
17
|
+
const values = {
|
|
18
|
+
messages: runState.messages,
|
|
19
|
+
step: runState.step,
|
|
20
|
+
toolCallCount: runState.toolCallCount,
|
|
21
|
+
...(runState.model !== undefined ? { model: runState.model } : {}),
|
|
22
|
+
...(runState.provider !== undefined ? { provider: runState.provider } : {}),
|
|
23
|
+
...(runState.agentId !== undefined ? { agentId: runState.agentId } : {}),
|
|
24
|
+
...(runState.totalInputTokens !== undefined
|
|
25
|
+
? { totalInputTokens: runState.totalInputTokens }
|
|
26
|
+
: {}),
|
|
27
|
+
...(runState.totalOutputTokens !== undefined
|
|
28
|
+
? { totalOutputTokens: runState.totalOutputTokens }
|
|
29
|
+
: {}),
|
|
30
|
+
...(runState.finalOutput !== undefined
|
|
31
|
+
? { finalOutput: runState.finalOutput }
|
|
32
|
+
: {}),
|
|
33
|
+
...(runState.userContentParts !== undefined
|
|
34
|
+
? { userContentParts: runState.userContentParts }
|
|
35
|
+
: {}),
|
|
36
|
+
};
|
|
37
|
+
const tasks = [];
|
|
38
|
+
return createSnapshot(config, values, {
|
|
39
|
+
next: [],
|
|
40
|
+
metadata: {
|
|
41
|
+
source: "run-state",
|
|
42
|
+
version: runState.version,
|
|
43
|
+
runId: runState.runId,
|
|
44
|
+
savedAt: runState.savedAt,
|
|
45
|
+
},
|
|
46
|
+
parentConfig: null,
|
|
47
|
+
tasks,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Convert a {@link StateSnapshot} back into a {@link SerializedRunState}.
|
|
52
|
+
*
|
|
53
|
+
* Extracts well-known channel names from `values` and maps them to the
|
|
54
|
+
* run state fields. Unknown channels are dropped (they belong to the
|
|
55
|
+
* durable-state channel layer, not the run loop).
|
|
56
|
+
*
|
|
57
|
+
* @param snapshot - The snapshot to convert.
|
|
58
|
+
* @returns A SerializedRunState.
|
|
59
|
+
*/
|
|
60
|
+
export function snapshotToRunState(snapshot) {
|
|
61
|
+
const values = snapshot.values;
|
|
62
|
+
const messages = Array.isArray(values.messages) ? values.messages : [];
|
|
63
|
+
const step = typeof values.step === "number" ? values.step : 0;
|
|
64
|
+
const toolCallCount = typeof values.toolCallCount === "number" ? values.toolCallCount : 0;
|
|
65
|
+
const prompt = "";
|
|
66
|
+
return {
|
|
67
|
+
version: 1,
|
|
68
|
+
savedAt: snapshot.createdAt,
|
|
69
|
+
prompt,
|
|
70
|
+
messages: messages,
|
|
71
|
+
step,
|
|
72
|
+
toolCallCount,
|
|
73
|
+
...(typeof values.sessionId === "string" ? { sessionId: values.sessionId } : {}),
|
|
74
|
+
...(typeof snapshot.config.threadId === "string"
|
|
75
|
+
? { sessionId: snapshot.config.threadId }
|
|
76
|
+
: {}),
|
|
77
|
+
...(typeof values.model === "string" ? { model: values.model } : {}),
|
|
78
|
+
...(typeof values.provider === "string" ? { provider: values.provider } : {}),
|
|
79
|
+
...(typeof values.agentId === "string" ? { agentId: values.agentId } : {}),
|
|
80
|
+
...(typeof values.totalInputTokens === "number"
|
|
81
|
+
? { totalInputTokens: values.totalInputTokens }
|
|
82
|
+
: {}),
|
|
83
|
+
...(typeof values.totalOutputTokens === "number"
|
|
84
|
+
? { totalOutputTokens: values.totalOutputTokens }
|
|
85
|
+
: {}),
|
|
86
|
+
...(typeof values.finalOutput === "string"
|
|
87
|
+
? { finalOutput: values.finalOutput }
|
|
88
|
+
: {}),
|
|
89
|
+
...(Array.isArray(values.userContentParts)
|
|
90
|
+
? { userContentParts: values.userContentParts }
|
|
91
|
+
: {}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Build an initial {@link StateSnapshot} from a {@link StateGraphDefinition}
|
|
96
|
+
* and a {@link GraphConfig} — all channels set to their defaults.
|
|
97
|
+
*
|
|
98
|
+
* @param definition - The compiled state graph definition.
|
|
99
|
+
* @param config - Graph config (threadId = sessionId).
|
|
100
|
+
* @returns A StateSnapshot with default channel values.
|
|
101
|
+
*/
|
|
102
|
+
export function snapshotFromDefinition(definition, config) {
|
|
103
|
+
return createSnapshot(config, initialState(definition), {
|
|
104
|
+
next: [],
|
|
105
|
+
metadata: {
|
|
106
|
+
source: "definition",
|
|
107
|
+
graphId: definition.id,
|
|
108
|
+
graphName: definition.name,
|
|
109
|
+
},
|
|
110
|
+
parentConfig: null,
|
|
111
|
+
tasks: [],
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Apply a `PartialState` delta to a snapshot's values using the graph
|
|
116
|
+
* definition's reducers, returning a new snapshot.
|
|
117
|
+
*
|
|
118
|
+
* @param definition - The compiled state graph definition.
|
|
119
|
+
* @param snapshot - The current snapshot.
|
|
120
|
+
* @param delta - Partial state delta to apply.
|
|
121
|
+
* @returns A new snapshot with updated values.
|
|
122
|
+
*/
|
|
123
|
+
export function applyDeltaToSnapshot(definition, snapshot, delta) {
|
|
124
|
+
const next = applyDelta(definition, snapshot.values, delta);
|
|
125
|
+
return createSnapshot(snapshot.config, next, {
|
|
126
|
+
next: snapshot.next,
|
|
127
|
+
metadata: {
|
|
128
|
+
...snapshot.metadata,
|
|
129
|
+
step: (snapshot.metadata.step ?? 0) + 1,
|
|
130
|
+
},
|
|
131
|
+
createdAt: new Date().toISOString(),
|
|
132
|
+
parentConfig: snapshot.parentConfig,
|
|
133
|
+
tasks: snapshot.tasks,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A Checkpointer backed by a vinhnt {@link SessionStore} and {@link RunEventStore}.
|
|
138
|
+
*
|
|
139
|
+
* Snapshots are serialized as JSON and stored via the event store's
|
|
140
|
+
* `saveSnapshot`/`getSnapshot` (per runId = threadId). The runId doubles as
|
|
141
|
+
* the graph threadId, so `threadId` maps 1:1 to a durable run.
|
|
142
|
+
*
|
|
143
|
+
* Requires `@vinhnt-sdk/session` to be installed (peer dependency).
|
|
144
|
+
*/
|
|
145
|
+
export class SessionStoreCheckpointer {
|
|
146
|
+
eventStore;
|
|
147
|
+
constructor(eventStore) {
|
|
148
|
+
this.eventStore = eventStore;
|
|
149
|
+
}
|
|
150
|
+
async put(config, snapshot) {
|
|
151
|
+
const ensured = snapshot.config.checkpointId
|
|
152
|
+
? snapshot
|
|
153
|
+
: {
|
|
154
|
+
...snapshot,
|
|
155
|
+
config: {
|
|
156
|
+
...snapshot.config,
|
|
157
|
+
checkpointId: `cp_${crypto.randomUUID()}`,
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
await this.eventStore.saveSnapshot(config.threadId, {
|
|
161
|
+
values: ensured.values,
|
|
162
|
+
next: ensured.next,
|
|
163
|
+
config: ensured.config,
|
|
164
|
+
metadata: ensured.metadata,
|
|
165
|
+
createdAt: ensured.createdAt,
|
|
166
|
+
parentConfig: ensured.parentConfig,
|
|
167
|
+
tasks: ensured.tasks,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async putWrites(_config, _writes) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
async getTuple(config) {
|
|
174
|
+
const row = await this.eventStore.getSnapshot(config.threadId);
|
|
175
|
+
if (!row)
|
|
176
|
+
return null;
|
|
177
|
+
return this.deserialize(row.state);
|
|
178
|
+
}
|
|
179
|
+
async list(_threadId) {
|
|
180
|
+
const row = await this.eventStore.getSnapshot(_threadId);
|
|
181
|
+
return row ? [this.deserialize(row.state)] : [];
|
|
182
|
+
}
|
|
183
|
+
async delete(_config) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
deserialize(state) {
|
|
187
|
+
return {
|
|
188
|
+
values: state.values ?? {},
|
|
189
|
+
next: (Array.isArray(state.next) ? state.next : []),
|
|
190
|
+
config: (typeof state.config === "object" && state.config !== null
|
|
191
|
+
? state.config
|
|
192
|
+
: { threadId: "" }),
|
|
193
|
+
metadata: (typeof state.metadata === "object" && state.metadata !== null
|
|
194
|
+
? state.metadata
|
|
195
|
+
: {}),
|
|
196
|
+
createdAt: typeof state.createdAt === "string" ? state.createdAt : new Date().toISOString(),
|
|
197
|
+
parentConfig: state.parentConfig === null || state.parentConfig === undefined
|
|
198
|
+
? null
|
|
199
|
+
: state.parentConfig,
|
|
200
|
+
tasks: (Array.isArray(state.tasks) ? state.tasks : []),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Type guard: is a value a ChannelNotFoundError?
|
|
206
|
+
*/
|
|
207
|
+
export function isChannelNotFoundError(value) {
|
|
208
|
+
return value instanceof ChannelNotFoundError;
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=session-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-adapter.js","sourceRoot":"","sources":["../../src/state/session-adapter.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAwB,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAA4B,EAC5B,MAAmB;IAEnB,MAAM,MAAM,GAA4B;QACtC,QAAQ,EAAE,QAAQ,CAAC,QAAQ;QAC3B,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,aAAa,EAAE,QAAQ,CAAC,aAAa;QACrC,GAAG,CAAC,QAAQ,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAClE,GAAG,CAAC,QAAQ,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3E,GAAG,CAAC,QAAQ,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACxE,GAAG,CAAC,QAAQ,CAAC,gBAAgB,KAAK,SAAS;YACzC,CAAC,CAAC,EAAE,gBAAgB,EAAE,QAAQ,CAAC,gBAAgB,EAAE;YACjD,CAAC,CAAC,EAAE,CAAC;QACP,GAAG,CAAC,QAA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AK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAA2B;SACjF,CAAC;IACJ,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAc;IAEd,OAAO,KAAK,YAAY,oBAAoB,CAAC;AAC/C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vinhnt-sdk/durable-state",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Durable state management for agent orchestration — StateChannel, Reducer, Checkpointer, Interrupt/Resume, AttachmentRef",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": ["dist", "LICENSE", "README.md"],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"engines": { "node": ">=20.0.0" },
|
|
20
|
+
"publishConfig": { "access": "public" },
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/vinhnt-develop/vinhnt-sdk.git",
|
|
24
|
+
"directory": "packages/durable-state"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/vinhnt-develop/vinhnt-sdk/tree/main/packages/durable-state",
|
|
27
|
+
"bugs": { "url": "https://github.com/vinhnt-develop/vinhnt-sdk/issues" },
|
|
28
|
+
"keywords": ["vnt", "durable", "state", "checkpoint", "interrupt", "resume", "graph-state", "orchestration"],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -b",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"lint": "eslint .",
|
|
34
|
+
"clean": "node -e \"try{require('fs').rmSync('dist',{recursive:true,force:true})}catch(e){}\" && node -e \"try{require('fs').rmSync('tsconfig.tsbuildinfo',{force:true})}catch(e){}\"",
|
|
35
|
+
"prepublishOnly": "pnpm build"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@vinhnt-sdk/schema": "workspace:*"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@vinhnt-sdk/session": "workspace:*"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"vitest": "^3.2.7",
|
|
45
|
+
"@types/node": "^20.0.0",
|
|
46
|
+
"@vinhnt-sdk/session": "workspace:*"
|
|
47
|
+
}
|
|
48
|
+
}
|