@ellyco/agentic 0.2.0 → 0.2.1
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/graphs/graph.js +3 -3
- package/dist/graphs/graph.js.map +1 -1
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.d.ts.map +1 -1
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/index.js.map +1 -1
- package/dist/nodes/model-node.d.ts +207 -25
- package/dist/nodes/model-node.d.ts.map +1 -1
- package/dist/nodes/model-node.js +124 -24
- package/dist/nodes/model-node.js.map +1 -1
- package/dist/nodes/state-transform.d.ts +108 -0
- package/dist/nodes/state-transform.d.ts.map +1 -0
- package/dist/nodes/state-transform.js +141 -0
- package/dist/nodes/state-transform.js.map +1 -0
- package/dist/tools.d.ts +6 -6
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js.map +1 -1
- package/dist/util/merge-state.d.ts +37 -9
- package/dist/util/merge-state.d.ts.map +1 -1
- package/dist/util/merge-state.js +45 -15
- package/dist/util/merge-state.js.map +1 -1
- package/package.json +1 -1
package/dist/graphs/graph.js
CHANGED
|
@@ -7,7 +7,7 @@ const zod_1 = require("zod");
|
|
|
7
7
|
const merge_state_1 = require("../util/merge-state");
|
|
8
8
|
const api_1 = require("@opentelemetry/api");
|
|
9
9
|
const util_1 = require("../util");
|
|
10
|
-
const tracer = api_1.trace.getTracer("@ellyco/agentic", "0.2.
|
|
10
|
+
const tracer = api_1.trace.getTracer("@ellyco/agentic", "0.2.1");
|
|
11
11
|
/**
|
|
12
12
|
* Special node name indicating the start of graph execution
|
|
13
13
|
*/
|
|
@@ -309,7 +309,7 @@ class Graph {
|
|
|
309
309
|
runtime.unwrapCursor(load.cursor);
|
|
310
310
|
}
|
|
311
311
|
try {
|
|
312
|
-
const state = this.schema.parse(mergedState);
|
|
312
|
+
const state = this.schema.loose().parse(mergedState);
|
|
313
313
|
const result = await this.run(state, runtime);
|
|
314
314
|
const finalState = { ...state, ...result };
|
|
315
315
|
if (runtime.interrupted) {
|
|
@@ -348,7 +348,7 @@ class Graph {
|
|
|
348
348
|
runtime.unwrapCursor(config.resumeFrom);
|
|
349
349
|
}
|
|
350
350
|
try {
|
|
351
|
-
const state = this.schema.parse(fullInput);
|
|
351
|
+
const state = this.schema.loose().parse(fullInput);
|
|
352
352
|
const result = await this.run(state, runtime);
|
|
353
353
|
const finalState = { ...state, ...result };
|
|
354
354
|
if (runtime.interrupted) {
|
package/dist/graphs/graph.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/graphs/graph.ts"],"names":[],"mappings":";;;AAEA,uDAAiE;AAEjE,gDAAgD;AAChD,6BAAwB;AACxB,qDAAiD;AACjD,4CAAsD;AACtD,kCAAqC;AAErC,MAAM,MAAM,GAAG,WAAK,CAAC,SAAS,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC;AAO3D;;GAEG;AACU,QAAA,KAAK,GAAG,OAAO,CAAC;AAE7B;;GAEG;AACU,QAAA,GAAG,GAAG,KAAK,CAAC;AA4BzB;;;;GAIG;AACH,SAAS,iBAAiB,CAAC,MAAmC;IAC1D,OAAO,OAAO,IAAI,MAAM,CAAC;AAC7B,CAAC;AAQD;;;;;;;;;;;;;GAaG;AACI,MAAM,wBAAwB,GAAG,CAAI,WAAuC,EAAmC,EAAE;IACpH,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/D,OAAO,WAA8C,CAAC;AAC1D,CAAC,CAAA;AAHY,QAAA,wBAAwB,4BAGpC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAsB,KAAK;IA0DQ;IArD/B;;OAEG;IACa,OAAO,GAAG,IAAI,CAAC;IAE/B;;OAEG;IACO,KAAK,GAAiC,EAAE,CAAC;IAEnD;;OAEG;IACO,KAAK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"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nodes/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,SAAS,CAAC"}
|
package/dist/nodes/index.js
CHANGED
|
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./function-node"), exports);
|
|
18
18
|
__exportStar(require("./interrupt-node"), exports);
|
|
19
19
|
__exportStar(require("./model-node"), exports);
|
|
20
|
+
__exportStar(require("./state-transform"), exports);
|
|
20
21
|
__exportStar(require("./types"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/nodes/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nodes/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,mDAAiC;AACjC,+CAA6B;AAC7B,0CAAwB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nodes/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,mDAAiC;AACjC,+CAA6B;AAC7B,oDAAkC;AAClC,0CAAwB"}
|
|
@@ -1,70 +1,252 @@
|
|
|
1
|
-
import { ModelMessages } from "../messages";
|
|
2
|
-
import { BaseModel, StructuredOutputWrapper } from "../models/BaseModel";
|
|
1
|
+
import { AgentMessage, ModelMessages, SystemMessage, ToolRequest } from "../messages";
|
|
2
|
+
import { BaseModel, InvokeResponse, StructuredOutputWrapper } from "../models/BaseModel";
|
|
3
3
|
import { NodeLike } from "./types";
|
|
4
4
|
import { ContextLayer } from "../graphs/runtime-context";
|
|
5
5
|
import { TypedKeys } from "../types";
|
|
6
|
+
import { ToolDefinition } from "../tools";
|
|
6
7
|
/**
|
|
7
8
|
* Helper type to extract keys from an object that have ModelMessages[] values.
|
|
9
|
+
*
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
type InputMessagesKeys<T> = TypedKeys<T, ModelMessages[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Helper type to extract keys from an object that have (AgentMessage | ToolRequest)[] values.
|
|
15
|
+
*
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
type OutputMessagesKeys<T> = TypedKeys<T, (AgentMessage | ToolRequest)[]>;
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for selecting messages from state by key.
|
|
21
|
+
*
|
|
22
|
+
* @interface MessageSelected
|
|
23
|
+
* @template T - The state type
|
|
8
24
|
*/
|
|
9
|
-
|
|
25
|
+
interface MessageSelected<T> {
|
|
26
|
+
/** Key in state containing the messages array */
|
|
27
|
+
messages: InputMessagesKeys<T>;
|
|
28
|
+
}
|
|
10
29
|
/**
|
|
11
|
-
* Configuration for messages
|
|
30
|
+
* Configuration for constructing messages dynamically from state and context.
|
|
31
|
+
*
|
|
32
|
+
* @interface MessageConstructed
|
|
33
|
+
* @template T - The state type
|
|
12
34
|
*/
|
|
13
|
-
interface
|
|
35
|
+
interface MessageConstructed<T> {
|
|
36
|
+
/** Function that generates messages from current state and context */
|
|
14
37
|
messages: (state: T, context: ContextLayer) => ModelMessages[];
|
|
15
38
|
}
|
|
16
39
|
/**
|
|
17
|
-
*
|
|
40
|
+
* Union type for message configuration - either selected from state or constructed dynamically.
|
|
41
|
+
*
|
|
42
|
+
* @typedef {MessageSelected<T> | MessageConstructed<T>} MessageConfig
|
|
43
|
+
* @template T - The state type
|
|
44
|
+
*/
|
|
45
|
+
type MessageConfig<T> = MessageSelected<T> | MessageConstructed<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Configuration for selecting output location from state by key.
|
|
48
|
+
*
|
|
49
|
+
* @interface OutputSelected
|
|
50
|
+
* @template T - The state type
|
|
51
|
+
* @template M - The model type (BaseModel or StructuredOutputWrapper)
|
|
18
52
|
*/
|
|
19
|
-
interface
|
|
20
|
-
|
|
53
|
+
interface OutputSelected<T, M extends BaseModel | StructuredOutputWrapper<any>> {
|
|
54
|
+
/**
|
|
55
|
+
* Key in state where output should be stored.
|
|
56
|
+
* For StructuredOutputWrapper: any key in state.
|
|
57
|
+
* For BaseModel: key that contains (AgentMessage | ToolRequest)[] array.
|
|
58
|
+
*/
|
|
59
|
+
output: M extends StructuredOutputWrapper<any> ? keyof T : OutputMessagesKeys<T>;
|
|
21
60
|
}
|
|
22
61
|
/**
|
|
23
|
-
*
|
|
62
|
+
* Configuration for constructing output dynamically from model response.
|
|
63
|
+
*
|
|
64
|
+
* @interface OutputConstructed
|
|
65
|
+
* @template T - The state type
|
|
66
|
+
* @template M - The model type (BaseModel or StructuredOutputWrapper)
|
|
67
|
+
*/
|
|
68
|
+
interface OutputConstructed<T, M extends BaseModel | StructuredOutputWrapper<any>> {
|
|
69
|
+
/**
|
|
70
|
+
* Function that transforms model response into partial state update.
|
|
71
|
+
* For StructuredOutputWrapper: receives the structured output directly.
|
|
72
|
+
* For BaseModel: receives InvokeResponse with messages array.
|
|
73
|
+
*/
|
|
74
|
+
output: (response: M extends StructuredOutputWrapper<any> ? any : InvokeResponse, state: T, context: ContextLayer) => Partial<T>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Union type for output configuration - either selected key or constructed dynamically.
|
|
78
|
+
*
|
|
79
|
+
* @typedef {OutputSelected<T, M> | OutputConstructed<T, M>} OutputConfig
|
|
80
|
+
* @template T - The state type
|
|
81
|
+
* @template M - The model type (BaseModel or StructuredOutputWrapper)
|
|
24
82
|
*/
|
|
25
|
-
type
|
|
26
|
-
|
|
27
|
-
|
|
83
|
+
type OutputConfig<T, M extends BaseModel | StructuredOutputWrapper<any>> = OutputSelected<T, M> | OutputConstructed<T, M>;
|
|
84
|
+
/**
|
|
85
|
+
* Configuration for a static system message.
|
|
86
|
+
*
|
|
87
|
+
* @interface SystemMessageStatic
|
|
88
|
+
*/
|
|
89
|
+
interface SystemMessageStatic {
|
|
90
|
+
/** Static system message to use for all invocations */
|
|
91
|
+
systemMessage: SystemMessage;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Configuration for a dynamically generated system message.
|
|
95
|
+
*
|
|
96
|
+
* @interface SystemMessageDynamic
|
|
97
|
+
* @template T - The state type
|
|
98
|
+
*/
|
|
99
|
+
interface SystemMessageDynamic<T> {
|
|
100
|
+
/** Function that generates system message from current state and context */
|
|
101
|
+
systemMessage: (state: T, context: ContextLayer) => SystemMessage;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Union type for system message configuration - either static or dynamic.
|
|
105
|
+
*
|
|
106
|
+
* @typedef {SystemMessageStatic | SystemMessageDynamic<T>} SystemMessageConfig
|
|
107
|
+
* @template T - The state type
|
|
108
|
+
*/
|
|
109
|
+
type SystemMessageConfig<T> = SystemMessageStatic | SystemMessageDynamic<T>;
|
|
110
|
+
/**
|
|
111
|
+
* Configuration for static tool definitions.
|
|
112
|
+
*
|
|
113
|
+
* @interface ToolsStatic
|
|
114
|
+
*/
|
|
115
|
+
interface ToolsStatic {
|
|
116
|
+
/** Array of tool definitions to use for all invocations */
|
|
117
|
+
tools: ToolDefinition[];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Configuration for dynamically generated tool definitions.
|
|
121
|
+
*
|
|
122
|
+
* @interface ToolsDynamic
|
|
123
|
+
* @template T - The state type
|
|
124
|
+
*/
|
|
125
|
+
interface ToolsDynamic<T> {
|
|
126
|
+
/** Function that generates tool definitions from current state and context */
|
|
127
|
+
tools: (state: T, context: ContextLayer) => ToolDefinition[];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Union type for tools configuration - either static or dynamic.
|
|
131
|
+
*
|
|
132
|
+
* @typedef {ToolsStatic | ToolsDynamic<T>} ToolsConfig
|
|
133
|
+
* @template T - The state type
|
|
134
|
+
*/
|
|
135
|
+
type ToolsConfig<T> = ToolsStatic | ToolsDynamic<T>;
|
|
136
|
+
/**
|
|
137
|
+
* Configuration object for ModelNode.
|
|
138
|
+
*
|
|
139
|
+
* @typedef {Object} ModelNodeConfig
|
|
140
|
+
* @template T - The state type
|
|
141
|
+
* @template M - The model type (BaseModel or StructuredOutputWrapper)
|
|
142
|
+
*
|
|
143
|
+
* @property {MessageConfig<T>} messages - Configuration for message source (key or function)
|
|
144
|
+
* @property {OutputConfig<T, M>} output - Configuration for output destination (key or function)
|
|
145
|
+
* @property {SystemMessageConfig<T>} [systemMessage] - Optional system message (static or dynamic)
|
|
146
|
+
* @property {ToolsConfig<T>} [tools] - Optional tool definitions (static or dynamic). Ignored when using StructuredOutputWrapper.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* // Static configuration
|
|
151
|
+
* const config: ModelNodeConfig<MyState, BaseModel> = {
|
|
152
|
+
* messages: "conversationHistory",
|
|
153
|
+
* output: "modelResponse",
|
|
154
|
+
* systemMessage: new SystemMessage("You are helpful"),
|
|
155
|
+
* tools: [searchTool, calculatorTool]
|
|
156
|
+
* };
|
|
157
|
+
*
|
|
158
|
+
* // Dynamic configuration
|
|
159
|
+
* const dynamicConfig: ModelNodeConfig<MyState, BaseModel> = {
|
|
160
|
+
* messages: (state, context) => state.messages.filter(m => m.role === "user"),
|
|
161
|
+
* output: (response, state, context) => ({
|
|
162
|
+
* modelResponse: response.messages,
|
|
163
|
+
* tokenUsage: response.usage
|
|
164
|
+
* }),
|
|
165
|
+
* systemMessage: (state, context) => new SystemMessage(`Context: ${state.context}`),
|
|
166
|
+
* tools: (state, context) => state.availableTools
|
|
167
|
+
* };
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export type ModelNodeConfig<T extends Record<string, unknown>, M extends BaseModel | StructuredOutputWrapper<any>> = OutputConfig<T, M> & MessageConfig<T> & Partial<SystemMessageConfig<T> & ToolsConfig<T>>;
|
|
28
171
|
/**
|
|
29
172
|
* A node that invokes an AI model and stores the response in the state.
|
|
30
|
-
*
|
|
173
|
+
*
|
|
174
|
+
* ModelNode provides flexible configuration for:
|
|
175
|
+
* - Message sources: read from state or construct dynamically
|
|
176
|
+
* - Output handling: store in state key or transform via function
|
|
177
|
+
* - System messages: static or dynamic based on state/context
|
|
178
|
+
* - Tool definitions: static or dynamic (ignored for StructuredOutputWrapper)
|
|
179
|
+
*
|
|
180
|
+
* Supports both regular BaseModel instances (returns messages) and StructuredOutputWrapper
|
|
181
|
+
* instances (returns structured data matching a Zod schema).
|
|
31
182
|
*
|
|
32
183
|
* @class ModelNode
|
|
33
184
|
* @template T - The state type
|
|
34
|
-
* @
|
|
185
|
+
* @template M - The model type (BaseModel or StructuredOutputWrapper), defaults to BaseModel
|
|
186
|
+
* @implements {NodeLike<T>}
|
|
35
187
|
*
|
|
36
188
|
* @example
|
|
37
189
|
* ```typescript
|
|
38
|
-
* //
|
|
190
|
+
* // Basic usage with messages from state
|
|
39
191
|
* const node = new ModelNode(model, {
|
|
40
|
-
* messages:
|
|
192
|
+
* messages: "conversationMessages",
|
|
41
193
|
* output: "modelResponse"
|
|
42
194
|
* });
|
|
43
195
|
*
|
|
44
|
-
* //
|
|
196
|
+
* // Dynamic message construction
|
|
45
197
|
* const node = new ModelNode(model, {
|
|
46
|
-
* messages:
|
|
198
|
+
* messages: (state, context) => [new UserMessage(state.input)],
|
|
47
199
|
* output: "modelResponse"
|
|
48
200
|
* });
|
|
201
|
+
*
|
|
202
|
+
* // With structured output
|
|
203
|
+
* const structuredModel = model.withStructuredOutput(z.object({
|
|
204
|
+
* name: z.string(),
|
|
205
|
+
* age: z.number()
|
|
206
|
+
* }));
|
|
207
|
+
* const node = new ModelNode(structuredModel, {
|
|
208
|
+
* messages: "userInput",
|
|
209
|
+
* output: "extractedData"
|
|
210
|
+
* });
|
|
211
|
+
*
|
|
212
|
+
* // Dynamic output transformation
|
|
213
|
+
* const node = new ModelNode(model, {
|
|
214
|
+
* messages: "messages",
|
|
215
|
+
* output: (response, state, context) => ({
|
|
216
|
+
* modelResponse: response.messages,
|
|
217
|
+
* tokenUsage: response.usage,
|
|
218
|
+
* timestamp: Date.now()
|
|
219
|
+
* })
|
|
220
|
+
* });
|
|
49
221
|
* ```
|
|
50
222
|
*/
|
|
51
|
-
export declare class ModelNode<T extends Record<string, unknown
|
|
223
|
+
export declare class ModelNode<T extends Record<string, unknown>, M extends BaseModel | StructuredOutputWrapper<any> = BaseModel> implements NodeLike<T> {
|
|
52
224
|
private readonly model;
|
|
53
|
-
private readonly
|
|
225
|
+
private readonly config;
|
|
54
226
|
/**
|
|
55
227
|
* Creates a new model node.
|
|
56
228
|
*
|
|
57
|
-
* @param {
|
|
58
|
-
* @param {
|
|
229
|
+
* @param {M} model - The model to invoke (BaseModel or StructuredOutputWrapper)
|
|
230
|
+
* @param {ModelNodeConfig<T, M>} config - Configuration for messages, output, system message, and tools
|
|
59
231
|
*/
|
|
60
|
-
constructor(model:
|
|
232
|
+
constructor(model: M, config: ModelNodeConfig<T, M>);
|
|
61
233
|
/**
|
|
62
234
|
* Runs the model with configured messages and stores the response in state.
|
|
63
235
|
*
|
|
236
|
+
* Execution flow:
|
|
237
|
+
* 1. Resolves messages from config (state key or function)
|
|
238
|
+
* 2. Applies system message if configured (static or dynamic)
|
|
239
|
+
* 3. Applies tools if configured and model is not StructuredOutputWrapper (static or dynamic)
|
|
240
|
+
* 4. Invokes the model with resolved messages
|
|
241
|
+
* 5. Transforms response based on output config (state key or function)
|
|
242
|
+
*
|
|
243
|
+
* For StructuredOutputWrapper: the response is the structured data directly.
|
|
244
|
+
* For BaseModel: the response contains messages array and usage information.
|
|
245
|
+
*
|
|
64
246
|
* @param {T} state - The current state
|
|
65
247
|
* @param {ContextLayer} context - The execution context
|
|
66
|
-
* @returns {Promise<Partial<T>>} Partial state containing the model's response
|
|
67
|
-
* @throws {Error} If messages
|
|
248
|
+
* @returns {Promise<Partial<T>>} Partial state update containing the model's response
|
|
249
|
+
* @throws {Error} If messages key is specified but not found in state, or if the value is not an array
|
|
68
250
|
*/
|
|
69
251
|
run(state: T, context: ContextLayer): Promise<Partial<T>>;
|
|
70
252
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-node.d.ts","sourceRoot":"","sources":["../../src/nodes/model-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"model-node.d.ts","sourceRoot":"","sources":["../../src/nodes/model-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtF,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACzF,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AA8C1C;;;;GAIG;AACH,KAAK,iBAAiB,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;AAE1D;;;;GAIG;AACH,KAAK,kBAAkB,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,CAAC,YAAY,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;AAE1E;;;;;GAKG;AACH,UAAU,eAAe,CAAC,CAAC;IACvB,iDAAiD;IACjD,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;CAClC;AAED;;;;;GAKG;AACH,UAAU,kBAAkB,CAAC,CAAC;IAC1B,sEAAsE;IACtE,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,YAAY,KAAK,aAAa,EAAE,CAAC;CAClE;AAED;;;;;GAKG;AACH,K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GED
|
@@ -9,71 +9,171 @@ const BaseModel_1 = require("../models/BaseModel");
|
|
|
9
9
|
* @param {any} value - Value to check
|
|
10
10
|
* @returns {boolean} True if value is a messages-returning function
|
|
11
11
|
*/
|
|
12
|
-
function
|
|
12
|
+
function isMessagesConstructed(value) {
|
|
13
|
+
return typeof value === "function";
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Type guard to check if a value is a function that constructs output from the model response.
|
|
17
|
+
*
|
|
18
|
+
* @private
|
|
19
|
+
* @param {any} value - Value to check
|
|
20
|
+
* @returns {boolean} True if value is an output-constructing function
|
|
21
|
+
*/
|
|
22
|
+
function isOutputConstructed(value) {
|
|
23
|
+
return typeof value === "function";
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Type guard to check if a value is a function that dynamically generates a system message.
|
|
27
|
+
*
|
|
28
|
+
* @private
|
|
29
|
+
* @param {any} value - Value to check
|
|
30
|
+
* @returns {boolean} True if value is a system message function
|
|
31
|
+
*/
|
|
32
|
+
function isSystemMessageDynamic(value) {
|
|
33
|
+
return typeof value === "function";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Type guard to check if a value is a function that dynamically generates tool definitions.
|
|
37
|
+
*
|
|
38
|
+
* @private
|
|
39
|
+
* @param {any} value - Value to check
|
|
40
|
+
* @returns {boolean} True if value is a tools function
|
|
41
|
+
*/
|
|
42
|
+
function isToolsDynamic(value) {
|
|
13
43
|
return typeof value === "function";
|
|
14
44
|
}
|
|
15
45
|
/**
|
|
16
46
|
* A node that invokes an AI model and stores the response in the state.
|
|
17
|
-
*
|
|
47
|
+
*
|
|
48
|
+
* ModelNode provides flexible configuration for:
|
|
49
|
+
* - Message sources: read from state or construct dynamically
|
|
50
|
+
* - Output handling: store in state key or transform via function
|
|
51
|
+
* - System messages: static or dynamic based on state/context
|
|
52
|
+
* - Tool definitions: static or dynamic (ignored for StructuredOutputWrapper)
|
|
53
|
+
*
|
|
54
|
+
* Supports both regular BaseModel instances (returns messages) and StructuredOutputWrapper
|
|
55
|
+
* instances (returns structured data matching a Zod schema).
|
|
18
56
|
*
|
|
19
57
|
* @class ModelNode
|
|
20
58
|
* @template T - The state type
|
|
21
|
-
* @
|
|
59
|
+
* @template M - The model type (BaseModel or StructuredOutputWrapper), defaults to BaseModel
|
|
60
|
+
* @implements {NodeLike<T>}
|
|
22
61
|
*
|
|
23
62
|
* @example
|
|
24
63
|
* ```typescript
|
|
25
|
-
* //
|
|
64
|
+
* // Basic usage with messages from state
|
|
26
65
|
* const node = new ModelNode(model, {
|
|
27
|
-
* messages:
|
|
66
|
+
* messages: "conversationMessages",
|
|
28
67
|
* output: "modelResponse"
|
|
29
68
|
* });
|
|
30
69
|
*
|
|
31
|
-
* //
|
|
70
|
+
* // Dynamic message construction
|
|
32
71
|
* const node = new ModelNode(model, {
|
|
33
|
-
* messages:
|
|
72
|
+
* messages: (state, context) => [new UserMessage(state.input)],
|
|
34
73
|
* output: "modelResponse"
|
|
35
74
|
* });
|
|
75
|
+
*
|
|
76
|
+
* // With structured output
|
|
77
|
+
* const structuredModel = model.withStructuredOutput(z.object({
|
|
78
|
+
* name: z.string(),
|
|
79
|
+
* age: z.number()
|
|
80
|
+
* }));
|
|
81
|
+
* const node = new ModelNode(structuredModel, {
|
|
82
|
+
* messages: "userInput",
|
|
83
|
+
* output: "extractedData"
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* // Dynamic output transformation
|
|
87
|
+
* const node = new ModelNode(model, {
|
|
88
|
+
* messages: "messages",
|
|
89
|
+
* output: (response, state, context) => ({
|
|
90
|
+
* modelResponse: response.messages,
|
|
91
|
+
* tokenUsage: response.usage,
|
|
92
|
+
* timestamp: Date.now()
|
|
93
|
+
* })
|
|
94
|
+
* });
|
|
36
95
|
* ```
|
|
37
96
|
*/
|
|
38
97
|
class ModelNode {
|
|
39
98
|
model;
|
|
40
|
-
|
|
99
|
+
config;
|
|
41
100
|
/**
|
|
42
101
|
* Creates a new model node.
|
|
43
102
|
*
|
|
44
|
-
* @param {
|
|
45
|
-
* @param {
|
|
103
|
+
* @param {M} model - The model to invoke (BaseModel or StructuredOutputWrapper)
|
|
104
|
+
* @param {ModelNodeConfig<T, M>} config - Configuration for messages, output, system message, and tools
|
|
46
105
|
*/
|
|
47
|
-
constructor(model,
|
|
106
|
+
constructor(model, config) {
|
|
48
107
|
this.model = model;
|
|
49
|
-
this.
|
|
108
|
+
this.config = config;
|
|
50
109
|
}
|
|
51
110
|
/**
|
|
52
111
|
* Runs the model with configured messages and stores the response in state.
|
|
53
112
|
*
|
|
113
|
+
* Execution flow:
|
|
114
|
+
* 1. Resolves messages from config (state key or function)
|
|
115
|
+
* 2. Applies system message if configured (static or dynamic)
|
|
116
|
+
* 3. Applies tools if configured and model is not StructuredOutputWrapper (static or dynamic)
|
|
117
|
+
* 4. Invokes the model with resolved messages
|
|
118
|
+
* 5. Transforms response based on output config (state key or function)
|
|
119
|
+
*
|
|
120
|
+
* For StructuredOutputWrapper: the response is the structured data directly.
|
|
121
|
+
* For BaseModel: the response contains messages array and usage information.
|
|
122
|
+
*
|
|
54
123
|
* @param {T} state - The current state
|
|
55
124
|
* @param {ContextLayer} context - The execution context
|
|
56
|
-
* @returns {Promise<Partial<T>>} Partial state containing the model's response
|
|
57
|
-
* @throws {Error} If messages
|
|
125
|
+
* @returns {Promise<Partial<T>>} Partial state update containing the model's response
|
|
126
|
+
* @throws {Error} If messages key is specified but not found in state, or if the value is not an array
|
|
58
127
|
*/
|
|
59
128
|
async run(state, context) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
129
|
+
const { messages, output, systemMessage, tools } = this.config;
|
|
130
|
+
let inMessages = [];
|
|
131
|
+
if (isMessagesConstructed(messages)) {
|
|
132
|
+
inMessages = messages(state, context);
|
|
63
133
|
}
|
|
64
134
|
else {
|
|
65
|
-
const stateMessages = state[
|
|
135
|
+
const stateMessages = state[messages];
|
|
66
136
|
if (!stateMessages || !Array.isArray(stateMessages)) {
|
|
67
|
-
throw new Error(`No Messages array found for key ${
|
|
137
|
+
throw new Error(`No Messages array found for key ${messages}`);
|
|
68
138
|
}
|
|
69
|
-
|
|
139
|
+
inMessages = stateMessages;
|
|
140
|
+
}
|
|
141
|
+
if (systemMessage) {
|
|
142
|
+
let inSystemMessage;
|
|
143
|
+
if (isSystemMessageDynamic(systemMessage)) {
|
|
144
|
+
inSystemMessage = systemMessage(state, context);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
inSystemMessage = systemMessage;
|
|
148
|
+
}
|
|
149
|
+
this.model.withSystemMessage(inSystemMessage);
|
|
150
|
+
}
|
|
151
|
+
if (tools && !(this.model instanceof BaseModel_1.StructuredOutputWrapper)) {
|
|
152
|
+
let inTools;
|
|
153
|
+
if (isToolsDynamic(tools)) {
|
|
154
|
+
inTools = tools(state, context);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
inTools = tools;
|
|
158
|
+
}
|
|
159
|
+
this.model.withTools(inTools);
|
|
70
160
|
}
|
|
71
161
|
if (this.model instanceof BaseModel_1.StructuredOutputWrapper) {
|
|
72
|
-
const
|
|
73
|
-
|
|
162
|
+
const structuredOutput = await this.model.invoke(inMessages);
|
|
163
|
+
if (isOutputConstructed(output)) {
|
|
164
|
+
return output(structuredOutput, state, context);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
return { [output]: structuredOutput };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const response = await this.model.invoke(inMessages);
|
|
171
|
+
if (isOutputConstructed(output)) {
|
|
172
|
+
return output(response, state, context);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
return { [output]: response.messages };
|
|
74
176
|
}
|
|
75
|
-
const response = await this.model.invoke(messages);
|
|
76
|
-
return { [this.settings.output]: response.messages };
|
|
77
177
|
}
|
|
78
178
|
}
|
|
79
179
|
exports.ModelNode = ModelNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-node.js","sourceRoot":"","sources":["../../src/nodes/model-node.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"model-node.js","sourceRoot":"","sources":["../../src/nodes/model-node.ts"],"names":[],"mappings":";;;AACA,mDAAyF;AAOzF;;;;;;GAMG;AACH,SAAS,qBAAqB,CAAoC,KAAU;IACxE,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACvC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,mBAAmB,CAAoC,KAAU;IACtE,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACvC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,sBAAsB,CAAoC,KAAU;IACzE,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACvC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CAAoC,KAAU;IACjE,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACvC,CAAC;AA2LD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAa,SAAS;IAQG;IACA;IARrB;;;;;OAKG;IACH,YACqB,KAAQ,EACR,MAA6B;QAD7B,UAAK,GAAL,KAAK,CAAG;QACR,WAAM,GAAN,MAAM,CAAuB;IAC9C,CAAC;IAEL;;;;;;;;;;;;;;;;;OAiBG;IACH,KAAK,CAAC,GAAG,CAAC,KAAQ,EAAE,OAAqB;QACrC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QAE/D,IAAI,UAAU,GAAoB,EAAE,CAAC;QACrC,IAAI,qBAAqB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAClC,UAAU,GAAG,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC1C,CAAC;aAAM,CAAC;YACJ,MAAM,aAAa,GAAG,KAAK,CAAC,QAAmB,CAAC,CAAC;YACjD,IAAI,CAAC,aAAa,IAA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,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACrD,IAAI,mBAAmB,CAAC,MAAM,CAAC,EAAE,CAAC;YAC9B,OAAO,MAAM,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAe,CAAC;QAC1D,CAAC;aAAM,CAAC;YACJ,OAAO,EAAE,CAAC,MAAiB,CAAC,EAAE,QAAQ,CAAC,QAAQ,EAAgB,CAAC;QACpE,CAAC;IACL,CAAC;CACJ;AA/ED,8BA+EC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { NodeLike } from "./types";
|
|
3
|
+
import { ContextLayer } from "../graphs/runtime-context";
|
|
4
|
+
import { Graph } from "../graphs/graph";
|
|
5
|
+
/**
|
|
6
|
+
* A node that transforms state between different schemas, allowing nested nodes/graphs
|
|
7
|
+
* to operate on a different state structure than the parent graph.
|
|
8
|
+
*
|
|
9
|
+
* StateTransformNode wraps a node or graph that expects a different state schema.
|
|
10
|
+
* It handles:
|
|
11
|
+
* - Input transformation: converts parent state to child state schema
|
|
12
|
+
* - Schema validation: ensures transformed state matches the child schema
|
|
13
|
+
* - Output transformation: converts child state back to parent state updates
|
|
14
|
+
* - Interrupt handling: preserves wrapped state during interrupts for resumption
|
|
15
|
+
* - State isolation: child state is stored separately during execution
|
|
16
|
+
*
|
|
17
|
+
* This is useful when you need to:
|
|
18
|
+
* - Reuse nodes/graphs with different state schemas
|
|
19
|
+
* - Isolate state transformations within a workflow
|
|
20
|
+
* - Compose graphs with incompatible state structures
|
|
21
|
+
*
|
|
22
|
+
* @class StateTransformNode
|
|
23
|
+
* @template PS - Parent state type (the graph's state schema)
|
|
24
|
+
* @template N - Zod schema object for the child state
|
|
25
|
+
* @template NS - Child state type (inferred from N)
|
|
26
|
+
* @implements {NodeLike<PS>}
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Parent schema
|
|
31
|
+
* const parentSchema = z.object({
|
|
32
|
+
* userId: z.string(),
|
|
33
|
+
* userName: z.string(),
|
|
34
|
+
* count: z.number()
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Child schema
|
|
38
|
+
* const childSchema = z.object({
|
|
39
|
+
* id: z.string(),
|
|
40
|
+
* name: z.string(),
|
|
41
|
+
* value: z.number()
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // Transform parent -> child
|
|
45
|
+
* const inputTransform = (state: ParentState): ChildState => ({
|
|
46
|
+
* id: state.userId,
|
|
47
|
+
* name: state.userName,
|
|
48
|
+
* value: state.count
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* // Transform child -> parent
|
|
52
|
+
* const outputTransform = (state: ChildState): Partial<ParentState> => ({
|
|
53
|
+
* count: state.value
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* // Create transform node wrapping a child node
|
|
57
|
+
* const childNode = makeNode<ChildState>((state) => ({
|
|
58
|
+
* value: state.value + 1
|
|
59
|
+
* }));
|
|
60
|
+
*
|
|
61
|
+
* const transformNode = new StateTransformNode(
|
|
62
|
+
* childSchema,
|
|
63
|
+
* inputTransform,
|
|
64
|
+
* childNode,
|
|
65
|
+
* outputTransform
|
|
66
|
+
* );
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare class StateTransformNode<PS extends Record<string, unknown>, N extends z.ZodObject, NS extends Record<string, unknown> = z.infer<N>> implements NodeLike<PS> {
|
|
70
|
+
private readonly schema;
|
|
71
|
+
private readonly input;
|
|
72
|
+
private readonly node;
|
|
73
|
+
private readonly output;
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new state transform node.
|
|
76
|
+
*
|
|
77
|
+
* @param {N} schema - Zod schema object that validates the child state structure
|
|
78
|
+
* @param {Function} input - Function that transforms parent state to child state
|
|
79
|
+
* @param {NodeLike<NS> | Graph<N, NS>} node - The node or graph to wrap (operates on child state)
|
|
80
|
+
* @param {Function} output - Function that transforms child state back to parent state updates
|
|
81
|
+
*/
|
|
82
|
+
constructor(schema: N, input: (state: PS, context: ContextLayer) => NS, node: NodeLike<NS> | Graph<N, NS>, output: (state: NS, context: ContextLayer) => Partial<PS>);
|
|
83
|
+
/**
|
|
84
|
+
* Executes the wrapped node/graph with transformed state.
|
|
85
|
+
*
|
|
86
|
+
* Execution flow:
|
|
87
|
+
* 1. If resuming from interrupt: restores child state from parent state's wrapped key
|
|
88
|
+
* 2. If not resuming: transforms parent state to child state using input function
|
|
89
|
+
* 3. Validates transformed state against the child schema
|
|
90
|
+
* 4. Attaches parent state reference to child state (as __parentState)
|
|
91
|
+
* 5. Runs the wrapped node/graph with the child state
|
|
92
|
+
* 6. Merges results back into child state
|
|
93
|
+
* 7. If interrupted: stores child state in parent state under wrapped key and returns
|
|
94
|
+
* 8. If completed: transforms child state back to parent state using output function
|
|
95
|
+
* 9. Cleans up wrapped state key from parent state
|
|
96
|
+
*
|
|
97
|
+
* The wrapped state is stored under a key like `__wrappedState_{contextId}.{nodeName}`
|
|
98
|
+
* to allow resumption after interrupts. This key is automatically cleaned up on completion.
|
|
99
|
+
*
|
|
100
|
+
* @param {PS} state - The parent state
|
|
101
|
+
* @param {ContextLayer} context - The execution context
|
|
102
|
+
* @returns {Promise<Partial<PS>>} Partial parent state update
|
|
103
|
+
* @throws {Error} If input transformation doesn't match the child schema (wraps ZodError)
|
|
104
|
+
* @throws {Error} If the wrapped node/graph throws an error (re-thrown)
|
|
105
|
+
*/
|
|
106
|
+
run(state: PS, context: ContextLayer): Promise<Partial<PS>>;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=state-transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-transform.d.ts","sourceRoot":"","sources":["../../src/nodes/state-transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EA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|
|
5
|
+
const util_1 = require("../util");
|
|
6
|
+
/**
|
|
7
|
+
* A node that transforms state between different schemas, allowing nested nodes/graphs
|
|
8
|
+
* to operate on a different state structure than the parent graph.
|
|
9
|
+
*
|
|
10
|
+
* StateTransformNode wraps a node or graph that expects a different state schema.
|
|
11
|
+
* It handles:
|
|
12
|
+
* - Input transformation: converts parent state to child state schema
|
|
13
|
+
* - Schema validation: ensures transformed state matches the child schema
|
|
14
|
+
* - Output transformation: converts child state back to parent state updates
|
|
15
|
+
* - Interrupt handling: preserves wrapped state during interrupts for resumption
|
|
16
|
+
* - State isolation: child state is stored separately during execution
|
|
17
|
+
*
|
|
18
|
+
* This is useful when you need to:
|
|
19
|
+
* - Reuse nodes/graphs with different state schemas
|
|
20
|
+
* - Isolate state transformations within a workflow
|
|
21
|
+
* - Compose graphs with incompatible state structures
|
|
22
|
+
*
|
|
23
|
+
* @class StateTransformNode
|
|
24
|
+
* @template PS - Parent state type (the graph's state schema)
|
|
25
|
+
* @template N - Zod schema object for the child state
|
|
26
|
+
* @template NS - Child state type (inferred from N)
|
|
27
|
+
* @implements {NodeLike<PS>}
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // Parent schema
|
|
32
|
+
* const parentSchema = z.object({
|
|
33
|
+
* userId: z.string(),
|
|
34
|
+
* userName: z.string(),
|
|
35
|
+
* count: z.number()
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // Child schema
|
|
39
|
+
* const childSchema = z.object({
|
|
40
|
+
* id: z.string(),
|
|
41
|
+
* name: z.string(),
|
|
42
|
+
* value: z.number()
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Transform parent -> child
|
|
46
|
+
* const inputTransform = (state: ParentState): ChildState => ({
|
|
47
|
+
* id: state.userId,
|
|
48
|
+
* name: state.userName,
|
|
49
|
+
* value: state.count
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* // Transform child -> parent
|
|
53
|
+
* const outputTransform = (state: ChildState): Partial<ParentState> => ({
|
|
54
|
+
* count: state.value
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* // Create transform node wrapping a child node
|
|
58
|
+
* const childNode = makeNode<ChildState>((state) => ({
|
|
59
|
+
* value: state.value + 1
|
|
60
|
+
* }));
|
|
61
|
+
*
|
|
62
|
+
* const transformNode = new StateTransformNode(
|
|
63
|
+
* childSchema,
|
|
64
|
+
* inputTransform,
|
|
65
|
+
* childNode,
|
|
66
|
+
* outputTransform
|
|
67
|
+
* );
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
class StateTransformNode {
|
|
71
|
+
schema;
|
|
72
|
+
input;
|
|
73
|
+
node;
|
|
74
|
+
output;
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new state transform node.
|
|
77
|
+
*
|
|
78
|
+
* @param {N} schema - Zod schema object that validates the child state structure
|
|
79
|
+
* @param {Function} input - Function that transforms parent state to child state
|
|
80
|
+
* @param {NodeLike<NS> | Graph<N, NS>} node - The node or graph to wrap (operates on child state)
|
|
81
|
+
* @param {Function} output - Function that transforms child state back to parent state updates
|
|
82
|
+
*/
|
|
83
|
+
constructor(schema, input, node, output) {
|
|
84
|
+
this.schema = schema;
|
|
85
|
+
this.input = input;
|
|
86
|
+
this.node = node;
|
|
87
|
+
this.output = output;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Executes the wrapped node/graph with transformed state.
|
|
91
|
+
*
|
|
92
|
+
* Execution flow:
|
|
93
|
+
* 1. If resuming from interrupt: restores child state from parent state's wrapped key
|
|
94
|
+
* 2. If not resuming: transforms parent state to child state using input function
|
|
95
|
+
* 3. Validates transformed state against the child schema
|
|
96
|
+
* 4. Attaches parent state reference to child state (as __parentState)
|
|
97
|
+
* 5. Runs the wrapped node/graph with the child state
|
|
98
|
+
* 6. Merges results back into child state
|
|
99
|
+
* 7. If interrupted: stores child state in parent state under wrapped key and returns
|
|
100
|
+
* 8. If completed: transforms child state back to parent state using output function
|
|
101
|
+
* 9. Cleans up wrapped state key from parent state
|
|
102
|
+
*
|
|
103
|
+
* The wrapped state is stored under a key like `__wrappedState_{contextId}.{nodeName}`
|
|
104
|
+
* to allow resumption after interrupts. This key is automatically cleaned up on completion.
|
|
105
|
+
*
|
|
106
|
+
* @param {PS} state - The parent state
|
|
107
|
+
* @param {ContextLayer} context - The execution context
|
|
108
|
+
* @returns {Promise<Partial<PS>>} Partial parent state update
|
|
109
|
+
* @throws {Error} If input transformation doesn't match the child schema (wraps ZodError)
|
|
110
|
+
* @throws {Error} If the wrapped node/graph throws an error (re-thrown)
|
|
111
|
+
*/
|
|
112
|
+
async run(state, context) {
|
|
113
|
+
const wrappedStateKey = `__wrappedState_${context.id}.${context.currentNode}`;
|
|
114
|
+
try {
|
|
115
|
+
let nodeState;
|
|
116
|
+
if (context.runtime.resuming) {
|
|
117
|
+
nodeState = { ...state[wrappedStateKey], __parentState: state };
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
const inputTransformed = this.input(state, context);
|
|
121
|
+
nodeState = { ...this.schema.parse(inputTransformed), __parentState: state };
|
|
122
|
+
}
|
|
123
|
+
const result = await this.node.run((0, util_1.cloneAware)(nodeState), context);
|
|
124
|
+
nodeState = { ...nodeState, ...result };
|
|
125
|
+
if (context.runtime.interrupted) {
|
|
126
|
+
const { __parentState, ...rest } = nodeState;
|
|
127
|
+
return { [wrappedStateKey]: rest };
|
|
128
|
+
}
|
|
129
|
+
const outState = this.output(nodeState, context);
|
|
130
|
+
return { ...outState, [wrappedStateKey]: undefined };
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (error instanceof zod_1.z.ZodError) {
|
|
134
|
+
throw new Error(`Transformed input does not match schema: ${error.message}`);
|
|
135
|
+
}
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.StateTransformNode = StateTransformNode;
|
|
141
|
+
//# sourceMappingURL=state-transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-transform.js","sourceRoot":"","sources":["../../src/nodes/state-transform.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAIxB,kCAAqC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,MAAa,kBAAkB;IAcN;IACA;IACA;IACA;IAZrB;;;;;;;OAOG;IACH,YACqB,MAAS,EACT,KAA+C,EAC/C,IAAiC,EACjC,MAAyD;QAHzD,WAAM,GAAN,MAAM,CAAG;QACT,UAAK,GAAL,KAAK,CAA0C;QAC/C,SAAI,GAAJ,IAAI,CAA6B;QACjC,WAAM,GAAN,MAAM,CAAmD;IAC1E,CAAC;IAEL;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,GAAG,CAAC,KAAS,EAAE,OAAqB;QACtC,MAAM,eAAe,GAAG,kBAAkB,OAAO,CAAC,EAAE,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;QAC9E,IAAI,CAAC;YACD,IAAI,SAAa,CAAC;YAClB,IAAI,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAC3B,SAAS,GAAG,EAAE,GAAG,KAAK,CAAC,eAAe,CAAO,EAAE,aAAa,EAAE,KAAK,EAAQ,CAAC;YAChF,CAAC;iBAAM,CAAC;gBACJ,MAAM,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;gBACpD,SAAS,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,EAAE,aAAa,EAAE,KAAK,EAAQ,CAAC;YACvF,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAA,iBAAU,EAAC,SAAS,CAAC,EAAE,OAAO,CAAC,CAAC;YACnE,SAAS,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,MAAM,EAAQ,CAAC;YAC9C,IAAI,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;gBAC9B,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,EAAE,GAAG,SAAS,CAAC;gBAC7C,OAAO,EAAE,CAAC,eAAe,CAAC,EAAE,IAAI,EAAiB,CAAC;YACtD,CAAC;YACD,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YACjD,OAAO,EAAE,GAAG,QAAQ,EAAE,CAAC,eAAe,CAAC,EAAE,SAAS,EAAiB,CAAC;QACxE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,KAAK,YAAY,OAAC,CAAC,QAAQ,EAAE,CAAC;gBAC9B,MAAM,IAAI,KAAK,CAAC,4CAA4C,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YACjF,CAAC;YACD,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC;CACJ;AApED,gDAoEC"}
|
package/dist/tools.d.ts
CHANGED
|
@@ -7,10 +7,10 @@ import * as z from "zod";
|
|
|
7
7
|
* @property {string} [description] - Human-readable description of what the tool does
|
|
8
8
|
* @property {z.ZodSchema<T>} schema - Zod schema that validates the tool's input parameters
|
|
9
9
|
*/
|
|
10
|
-
export interface ToolDefinition<T =
|
|
10
|
+
export interface ToolDefinition<T = z.ZodObject> {
|
|
11
11
|
name: string;
|
|
12
12
|
description?: string;
|
|
13
|
-
schema:
|
|
13
|
+
schema: T;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Creates a tool definition with validation schema.
|
|
@@ -31,7 +31,7 @@ export interface ToolDefinition<T = object> {
|
|
|
31
31
|
* );
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
|
-
export declare function defineTool<T>(name: string, description: string, schema:
|
|
34
|
+
export declare function defineTool<T extends z.ZodObject>(name: string, description: string, schema: T): ToolDefinition<T>;
|
|
35
35
|
/**
|
|
36
36
|
* Internal interface extending ToolDefinition with the actual implementation function.
|
|
37
37
|
*
|
|
@@ -39,8 +39,8 @@ export declare function defineTool<T>(name: string, description: string, schema:
|
|
|
39
39
|
* @template K - The output type returned by the tool function
|
|
40
40
|
* @template A - The additional arguments type
|
|
41
41
|
*/
|
|
42
|
-
export interface ToolImplementation<T, K, A extends Record<string, any>> extends ToolDefinition<T> {
|
|
43
|
-
func: (input: T
|
|
42
|
+
export interface ToolImplementation<T extends z.ZodObject, K, A extends Record<string, any>> extends ToolDefinition<T> {
|
|
43
|
+
func: (input: z.infer<T>, additionalArgs?: A) => K | Promise<K>;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Creates a complete tool implementation by combining a tool definition with its function.
|
|
@@ -62,5 +62,5 @@ export interface ToolImplementation<T, K, A extends Record<string, any>> extends
|
|
|
62
62
|
* );
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
|
-
export declare function tool<T, K, A extends Record<string, any>>(toolDefinition: ToolDefinition<T>, func: (input: T
|
|
65
|
+
export declare function tool<T extends z.ZodObject, K, A extends Record<string, any>>(toolDefinition: ToolDefinition<T>, func: (input: z.infer<T>, additionalArgs?: A) => K | Promise<K>): ToolImplementation<T, K, A>;
|
|
66
66
|
//# sourceMappingURL=tools.d.ts.map
|
package/dist/tools.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,GAAG,
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,WAAW,cAA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;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,UAAU,CACtB,IAAY,EACZ,WAAmB,EACnB,
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":";;AAmCA,gCAUC;AAiCD,oBAQC;AAtED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,UAAU,CACtB,IAAY,EACZ,WAAmB,EACnB,MAAS;IAET,OAAO;QACH,IAAI;QACJ,WAAW;QACX,MAAM;KACT,CAAC;AACN,CAAC;AAaD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,IAAI,CAChB,cAAiC,EACjC,IAA+D;IAE/D,OAAO;QACH,GAAG,cAAc;QACjB,IAAI;KACP,CAAC;AACN,CAAC"}
|
|
@@ -1,21 +1,49 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
/**
|
|
3
3
|
* Merges partial state updates into a base state object.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
*
|
|
5
|
+
* Uses registered custom merge strategies for specific types when available.
|
|
6
|
+
* Falls back to standard merge behavior based on value types:
|
|
7
|
+
* - Undefined/null keys: Set the new value directly
|
|
8
|
+
* - Custom merge functions: Use registered strategy (handles entire value including nested)
|
|
9
|
+
* - Class instances: Replace directly (preserves object identity)
|
|
10
|
+
* - Arrays: Replace entire array (no element-level merging)
|
|
11
|
+
* - Plain objects: Recursively merge nested properties
|
|
12
|
+
* - Primitives: Replace the value
|
|
13
|
+
*
|
|
14
|
+
* The merge process uses `cloneAware()` to create a deep clone of the base state,
|
|
15
|
+
* ensuring the original state is not mutated while preserving class instances.
|
|
6
16
|
*
|
|
7
17
|
* @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}
|
|
18
|
+
* @param {T} base - The base state to merge into (will not be mutated)
|
|
19
|
+
* @param {Partial<T>} changes - Partial state with updates to apply
|
|
20
|
+
* @param {z.ZodObject} schema - Zod schema for the state type (used to look up custom merge strategies)
|
|
21
|
+
* @returns {T} New merged state object (base is not mutated)
|
|
12
22
|
*
|
|
13
23
|
* @example
|
|
14
24
|
* ```typescript
|
|
15
|
-
*
|
|
16
|
-
* const
|
|
25
|
+
* // Basic merge
|
|
26
|
+
* const base = { count: 5, items: [1, 2, 3], user: { name: "Alice" } };
|
|
27
|
+
* const changes = { count: 10, user: { age: 30 } };
|
|
17
28
|
* const merged = mergeState(base, changes, schema);
|
|
18
|
-
* // Results in { count: 10, items: [
|
|
29
|
+
* // Results in { count: 10, items: [1, 2, 3], user: { name: "Alice", age: 30 } }
|
|
30
|
+
*
|
|
31
|
+
* // Array replacement (arrays are replaced, not merged)
|
|
32
|
+
* const base2 = { items: [1, 2, 3] };
|
|
33
|
+
* const changes2 = { items: [4, 5] };
|
|
34
|
+
* const merged2 = mergeState(base2, changes2, schema);
|
|
35
|
+
* // Results in { items: [4, 5] } (not [1, 2, 3, 4, 5])
|
|
36
|
+
*
|
|
37
|
+
* // Custom merge strategy (if registered)
|
|
38
|
+
* const schema = z.object({
|
|
39
|
+
* count: z.number().register(STATE_MERGE, {
|
|
40
|
+
* merge: (old, change) => old + change // Additive merge
|
|
41
|
+
* })
|
|
42
|
+
* });
|
|
43
|
+
* const base3 = { count: 5 };
|
|
44
|
+
* const changes3 = { count: 3 };
|
|
45
|
+
* const merged3 = mergeState(base3, changes3, schema);
|
|
46
|
+
* // Results in { count: 8 } (5 + 3, not 3)
|
|
19
47
|
* ```
|
|
20
48
|
*/
|
|
21
49
|
export declare function mergeState<T extends Record<string, any>>(base: T, changes: Partial<T>, schema: z.ZodObject): T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge-state.d.ts","sourceRoot":"","sources":["../../src/util/merge-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB
|
|
1
|
+
{"version":3,"file":"merge-state.d.ts","sourceRoot":"","sources":["../../src/util/merge-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;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,CAkC9G"}
|
package/dist/util/merge-state.js
CHANGED
|
@@ -5,51 +5,81 @@ const registry_1 = require("../graphs/registry");
|
|
|
5
5
|
const clone_aware_1 = require("./clone-aware");
|
|
6
6
|
/**
|
|
7
7
|
* Merges partial state updates into a base state object.
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
*
|
|
9
|
+
* Uses registered custom merge strategies for specific types when available.
|
|
10
|
+
* Falls back to standard merge behavior based on value types:
|
|
11
|
+
* - Undefined/null keys: Set the new value directly
|
|
12
|
+
* - Custom merge functions: Use registered strategy (handles entire value including nested)
|
|
13
|
+
* - Class instances: Replace directly (preserves object identity)
|
|
14
|
+
* - Arrays: Replace entire array (no element-level merging)
|
|
15
|
+
* - Plain objects: Recursively merge nested properties
|
|
16
|
+
* - Primitives: Replace the value
|
|
17
|
+
*
|
|
18
|
+
* The merge process uses `cloneAware()` to create a deep clone of the base state,
|
|
19
|
+
* ensuring the original state is not mutated while preserving class instances.
|
|
10
20
|
*
|
|
11
21
|
* @template T - The state object type
|
|
12
|
-
* @param {T} base - The base state to merge into
|
|
13
|
-
* @param {Partial<T>} changes - Partial state with updates
|
|
14
|
-
* @param {z.ZodObject} schema - Zod schema for the state type
|
|
15
|
-
* @returns {T}
|
|
22
|
+
* @param {T} base - The base state to merge into (will not be mutated)
|
|
23
|
+
* @param {Partial<T>} changes - Partial state with updates to apply
|
|
24
|
+
* @param {z.ZodObject} schema - Zod schema for the state type (used to look up custom merge strategies)
|
|
25
|
+
* @returns {T} New merged state object (base is not mutated)
|
|
16
26
|
*
|
|
17
27
|
* @example
|
|
18
28
|
* ```typescript
|
|
19
|
-
*
|
|
20
|
-
* const
|
|
29
|
+
* // Basic merge
|
|
30
|
+
* const base = { count: 5, items: [1, 2, 3], user: { name: "Alice" } };
|
|
31
|
+
* const changes = { count: 10, user: { age: 30 } };
|
|
21
32
|
* const merged = mergeState(base, changes, schema);
|
|
22
|
-
* // Results in { count: 10, items: [
|
|
33
|
+
* // Results in { count: 10, items: [1, 2, 3], user: { name: "Alice", age: 30 } }
|
|
34
|
+
*
|
|
35
|
+
* // Array replacement (arrays are replaced, not merged)
|
|
36
|
+
* const base2 = { items: [1, 2, 3] };
|
|
37
|
+
* const changes2 = { items: [4, 5] };
|
|
38
|
+
* const merged2 = mergeState(base2, changes2, schema);
|
|
39
|
+
* // Results in { items: [4, 5] } (not [1, 2, 3, 4, 5])
|
|
40
|
+
*
|
|
41
|
+
* // Custom merge strategy (if registered)
|
|
42
|
+
* const schema = z.object({
|
|
43
|
+
* count: z.number().register(STATE_MERGE, {
|
|
44
|
+
* merge: (old, change) => old + change // Additive merge
|
|
45
|
+
* })
|
|
46
|
+
* });
|
|
47
|
+
* const base3 = { count: 5 };
|
|
48
|
+
* const changes3 = { count: 3 };
|
|
49
|
+
* const merged3 = mergeState(base3, changes3, schema);
|
|
50
|
+
* // Results in { count: 8 } (5 + 3, not 3)
|
|
23
51
|
* ```
|
|
24
52
|
*/
|
|
25
53
|
function mergeState(base, changes, schema) {
|
|
26
54
|
const acc = (0, clone_aware_1.cloneAware)(base);
|
|
27
55
|
for (const [key, value] of Object.entries(changes)) {
|
|
28
|
-
//
|
|
56
|
+
// If there's nothing already present for the key, then we just set the value
|
|
29
57
|
if (acc[key] === undefined || acc[key] === null) {
|
|
30
58
|
acc[key] = value;
|
|
31
59
|
continue;
|
|
32
60
|
}
|
|
33
|
-
//
|
|
34
|
-
|
|
61
|
+
// If there's a registered merge function, then we assume the function handles
|
|
62
|
+
// the entirety of the value, including any nested values
|
|
63
|
+
if (schema && registry_1.STATE_MERGE.has(schema.shape[key])) {
|
|
35
64
|
acc[key] = registry_1.STATE_MERGE.get(schema.shape[key]).merge(acc[key], value);
|
|
36
65
|
continue;
|
|
37
66
|
}
|
|
38
67
|
if (typeof value === "object") {
|
|
39
|
-
// The value is a class
|
|
68
|
+
// The value is a class instance - replace directly to preserve object identity
|
|
40
69
|
if ('constructor' in value && !["Object", "Array"].includes(value.constructor.name)) {
|
|
41
70
|
acc[key] = value;
|
|
42
71
|
continue;
|
|
43
72
|
}
|
|
44
|
-
// The value is an array
|
|
73
|
+
// The value is an array - replace the entire array (no element-level merging)
|
|
45
74
|
if (Array.isArray(acc[key])) {
|
|
46
75
|
acc[key] = value;
|
|
47
76
|
continue;
|
|
48
77
|
}
|
|
49
|
-
// The value is a plain object
|
|
78
|
+
// The value is a plain object - recursively merge nested properties
|
|
50
79
|
acc[key] = mergeState(acc[key], value, schema.shape[key]);
|
|
51
80
|
}
|
|
52
81
|
else {
|
|
82
|
+
// The value is a primitive - replace directly
|
|
53
83
|
acc[key] = value;
|
|
54
84
|
}
|
|
55
85
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge-state.js","sourceRoot":"","sources":["../../src/util/merge-state.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"merge-state.js","sourceRoot":"","sources":["../../src/util/merge-state.ts"],"names":[],"mappings":";;AAmDA,gCAkCC;AApFD,iDAAiD;AACjD,+CAA2C;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,SAAgB,UAAU,CAAgC,IAAO,EAAE,OAAmB,EAAE,MAAmB;IACvG,MAAM,GAAG,GAAG,IAAA,wBAAU,EAAC,IAAI,CAAC,CAAC;IAC7B,KAAK,MA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