@graph-compose/client 1.0.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.
@@ -0,0 +1,180 @@
1
+ import { ActivityConfig, AgentNode, ErrorBoundaryNode, HttpNode, JsonataParam, Node, NodeConditions } from "@graph-compose/core";
2
+ import { z } from "zod";
3
+ import { GraphCompose } from "./builder";
4
+ /**
5
+ * Fluent API for configuring individual nodes within a GraphCompose workflow.
6
+ * Chain configuration methods (e.g., `.get()`, `.withHeaders()`, `.withDependencies()`).
7
+ * Call `.end()` to finalize the node definition and add it to the graph.
8
+ */
9
+ export declare class NodeBuilder {
10
+ private readonly graph;
11
+ protected node: HttpNode | ErrorBoundaryNode | AgentNode;
12
+ private isHttpNode;
13
+ private isAgentNode;
14
+ private readonly id;
15
+ /**
16
+ * Creates an instance of NodeBuilder.
17
+ * @param graph The parent GraphCompose instance.
18
+ * @param id The unique identifier for the node.
19
+ * @param type The type of the node ('http', 'error_boundary', 'agent'). Defaults to 'http'.
20
+ * @param protectedNodes Optional array of node IDs to protect if type is 'error_boundary'.
21
+ */
22
+ constructor(graph: GraphCompose, id: string, type?: "http" | "error_boundary" | "agent", protectedNodes?: string[]);
23
+ /**
24
+ * Validates a JSONata expression within the context of the current node.
25
+ * @param expression The JSONata expression string to validate.
26
+ * @param context A descriptive string indicating where the expression is used (e.g., 'header 'X-Api-Key'').
27
+ * @throws {Error} If the expression is invalid.
28
+ */
29
+ private validateJsonataExpression;
30
+ /**
31
+ * Sets the HTTP method to GET and specifies the URL for the node.
32
+ * @param url The URL endpoint for the GET request.
33
+ * @returns The NodeBuilder instance for chaining.
34
+ */
35
+ get(url: string): NodeBuilder;
36
+ /**
37
+ * Sets the HTTP method to POST and specifies the URL for the node.
38
+ * @param url The URL endpoint for the POST request.
39
+ * @returns The NodeBuilder instance for chaining.
40
+ */
41
+ post(url: string): NodeBuilder;
42
+ /**
43
+ * Sets the HTTP method to PUT and specifies the URL for the node.
44
+ * @param url The URL endpoint for the PUT request.
45
+ * @returns The NodeBuilder instance for chaining.
46
+ */
47
+ put(url: string): NodeBuilder;
48
+ /**
49
+ * Sets the HTTP method to DELETE and specifies the URL for the node.
50
+ * @param url The URL endpoint for the DELETE request.
51
+ * @returns The NodeBuilder instance for chaining.
52
+ */
53
+ delete(url: string): NodeBuilder;
54
+ /**
55
+ * Sets the HTTP method to PATCH and specifies the URL for the node.
56
+ * @param url The URL endpoint for the PATCH request.
57
+ * @returns The NodeBuilder instance for chaining.
58
+ */
59
+ patch(url: string): NodeBuilder;
60
+ /**
61
+ * Configures conditional execution logic for the HTTP node.
62
+ * This includes termination, continuation, and polling conditions based on JSONata expressions.
63
+ * @param conditions An object defining the conditional logic (`terminateWhen`, `continueTo`, `pollUntil`).
64
+ * @returns The NodeBuilder instance for chaining.
65
+ * @throws {Error} If called on a non-HTTP node.
66
+ * @throws {Error} If any JSONata expression within conditions is invalid.
67
+ */
68
+ withConditions(conditions: NodeConditions): NodeBuilder;
69
+ /**
70
+ * Sets the HTTP headers for the node's request.
71
+ * Values can be static strings or dynamic JSONata expressions.
72
+ * @param headers A record of header names to string values or JsonataParam objects.
73
+ * @returns The NodeBuilder instance for chaining.
74
+ * @throws {Error} If any JSONata expression within headers is invalid.
75
+ */
76
+ withHeaders(headers: Record<string, string | JsonataParam>): NodeBuilder;
77
+ /**
78
+ * Sets the body for the HTTP request.
79
+ * Can be a JSON object or a string (potentially containing template expressions).
80
+ * Template expression validation occurs during final workflow validation.
81
+ * @param body The request body as an object or string.
82
+ * @returns The NodeBuilder instance for chaining.
83
+ */
84
+ withBody(body: Record<string, any> | string): NodeBuilder;
85
+ /**
86
+ * Configures the retry policy for the HTTP request.
87
+ * Merges provided partial configuration with any existing policy.
88
+ * @param config Partial configuration for the RetryPolicy.
89
+ * @returns The NodeBuilder instance for chaining.
90
+ */
91
+ withRetries(config: Partial<ActivityConfig["retryPolicy"]>): NodeBuilder;
92
+ /**
93
+ * Sets the start-to-close timeout for the activity's execution.
94
+ * Maximum time allowed for the activity to complete once it starts executing.
95
+ * @param duration The timeout duration (e.g., '30s', 5000).
96
+ * @returns The NodeBuilder instance for chaining.
97
+ */
98
+ withStartToCloseTimeout(duration: string): NodeBuilder;
99
+ /**
100
+ * Sets the schedule-to-close timeout for the activity's execution.
101
+ * Maximum time from when the activity is scheduled until it completes (includes queue time + execution time).
102
+ * @param duration The timeout duration (e.g., '30s', 5000).
103
+ * @returns The NodeBuilder instance for chaining.
104
+ */
105
+ withScheduleToCloseTimeout(duration: string): NodeBuilder;
106
+ /**
107
+ * Specifies the nodes that this node depends on.
108
+ * The node will only execute after all its dependencies have successfully completed.
109
+ * Applicable only to HTTP and agent nodes.
110
+ * @param nodeIds An array of node IDs that this node depends on.
111
+ * @returns The NodeBuilder instance for chaining.
112
+ * @throws {Error} If called on a non-HTTP or non-agent node.
113
+ */
114
+ withDependencies(nodeIds: string[]): NodeBuilder;
115
+ /**
116
+ * Configures input and/or output validation schemas for the HTTP node using Zod schemas.
117
+ * The Zod schemas are converted to JSON Schema for runtime validation.
118
+ * @param config An object containing optional `input` and `output` Zod schemas.
119
+ * @returns The NodeBuilder instance for chaining.
120
+ * @throws {Error} If called on a non-HTTP node.
121
+ */
122
+ withValidation(config: {
123
+ input?: z.ZodType<any>;
124
+ output?: z.ZodType<any>;
125
+ }): NodeBuilder;
126
+ /**
127
+ * Set the maximum number of iterations for an agent node.
128
+ * An agent runs until it returns a completion schema response.
129
+ * You are responsible for ensuring the agent returns a `complete` response before this limit.
130
+ * If the agent reaches the max iterations without completing, it will fail.
131
+ *
132
+ * @default 5
133
+ * @param maxIterations Must be greater than 0
134
+ * @throws {Error} If called on a non-agent node
135
+ * @throws {ValidationError} If maxIterations is less than 1
136
+ */
137
+ withMaxIterations(maxIterations: number): NodeBuilder;
138
+ /**
139
+ * Set the tools available to the agent.
140
+ * Tools must be defined in the global tools section of the workflow.
141
+ * Each tool name must be unique within the workflow and have no dashes or spaces.
142
+ *
143
+ * @param tools Array of tool names that the agent can use
144
+ * @throws {Error} If called on a non-agent node
145
+ * @throws {ValidationError} If any tool is not found in global tools
146
+ */
147
+ withTools(tools: string[]): NodeBuilder;
148
+ /**
149
+ * Add a single tool to the agent's available tools.
150
+ * The tool must be defined in the global tools section of the workflow.
151
+ * The tool name must be unique within the workflow and have no dashes or spaces.
152
+ *
153
+ * @param tool Name of the tool to add
154
+ * @throws {Error} If called on a non-agent node
155
+ * @throws {ValidationError} If the tool is not found in global tools
156
+ */
157
+ addTool(tool: string): NodeBuilder;
158
+ /**
159
+ * Finalizes the node configuration and adds it to the parent GraphCompose builder.
160
+ * This method MUST be called to complete the node definition.
161
+ *
162
+ * @returns The parent GraphCompose instance, allowing for further chaining on the main graph.
163
+ */
164
+ end(): GraphCompose;
165
+ /**
166
+ * @internal
167
+ * Retrieves the fully configured node object.
168
+ * This method is intended for internal use by the GraphCompose builder.
169
+ * @returns The configured Node object.
170
+ */
171
+ build(): Node;
172
+ /**
173
+ * @internal
174
+ * Retrieves the ID of the node being built.
175
+ * Used for generating informative error messages.
176
+ * @returns The ID of the node.
177
+ */
178
+ getId(): string;
179
+ }
180
+ //# sourceMappingURL=node-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-builder.d.ts","sourceRoot":"","sources":["../../src/core/node-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,SAAS,EACT,iBAAiB,EACjB,QAAQ,EACR,YAAY,EACZ,IAAI,EACJ,cAAc,EACf,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC;;;;GAIG;AACH,qBAAa,WAAW;IAgBpB,OAAO,CAAC,QAAQ,CAAC,KAAK;IAfxB,SAAS,CAAC,IAAI,EAAE,QAAQ,GAAG,iBAAiB,GAAG,SAAS,CAAC;IACzD,OAAO,CAAC,UAAU,CAAU;IAC5B,OA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;QAAE,KAAK,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAAC,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;KAAE,GAAG,WAAW;IAuBxF;;;;;;;;;;OAUG;IACH,iBAAiB,CAAC,aAAa,EAAE,MAAM,GAAG,WAAW;IASrD;;;;;;;;OAQG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,WAAW;IASvC;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW;IAYlC;;;;;OAKG;IACH,GAAG,IAAI,YAAY;IAOnB;;;;;OAKG;IACH,KAAK,IAAI,IAAI;IASb;;;;;OAKG;IACH,KAAK,IAAI,MAAM;CAGhB"}
@@ -0,0 +1,387 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NodeBuilder = void 0;
4
+ const zod_to_json_schema_1 = require("zod-to-json-schema");
5
+ const validation_1 = require("../validation");
6
+ /**
7
+ * Fluent API for configuring individual nodes within a GraphCompose workflow.
8
+ * Chain configuration methods (e.g., `.get()`, `.withHeaders()`, `.withDependencies()`).
9
+ * Call `.end()` to finalize the node definition and add it to the graph.
10
+ */
11
+ class NodeBuilder {
12
+ /**
13
+ * Creates an instance of NodeBuilder.
14
+ * @param graph The parent GraphCompose instance.
15
+ * @param id The unique identifier for the node.
16
+ * @param type The type of the node ('http', 'error_boundary', 'agent'). Defaults to 'http'.
17
+ * @param protectedNodes Optional array of node IDs to protect if type is 'error_boundary'.
18
+ */
19
+ constructor(graph, id, type = "http", protectedNodes) {
20
+ this.graph = graph;
21
+ this.isHttpNode = type === "http";
22
+ this.isAgentNode = type === "agent";
23
+ this.id = id; // Store the ID
24
+ if (type === "error_boundary") {
25
+ this.node = {
26
+ id,
27
+ type: "error_boundary",
28
+ protectedNodes: protectedNodes || [],
29
+ activityConfig: {},
30
+ http: {
31
+ method: "POST",
32
+ url: "",
33
+ },
34
+ };
35
+ }
36
+ else if (type === "agent") {
37
+ this.node = {
38
+ id,
39
+ type: "agent",
40
+ dependencies: [],
41
+ tools: [],
42
+ activityConfig: {},
43
+ http: {
44
+ method: "POST",
45
+ url: "",
46
+ config: {
47
+ max_iterations: undefined,
48
+ },
49
+ },
50
+ };
51
+ }
52
+ else {
53
+ this.node = {
54
+ id,
55
+ type: "http",
56
+ dependencies: [],
57
+ http: {
58
+ method: "GET",
59
+ url: "",
60
+ },
61
+ activityConfig: {},
62
+ };
63
+ }
64
+ }
65
+ /**
66
+ * Validates a JSONata expression within the context of the current node.
67
+ * @param expression The JSONata expression string to validate.
68
+ * @param context A descriptive string indicating where the expression is used (e.g., 'header 'X-Api-Key'').
69
+ * @throws {Error} If the expression is invalid.
70
+ */
71
+ validateJsonataExpression(expression, context) {
72
+ try {
73
+ const result = (0, validation_1.validateExpression)(expression, this.node.id);
74
+ if (!result.isValid) {
75
+ throw result.errors[0];
76
+ }
77
+ }
78
+ catch (e) {
79
+ console.error(`Error validating expression for node ${this.node.id}:`, e);
80
+ throw e;
81
+ }
82
+ }
83
+ // Update HTTP methods to validate URLs
84
+ /**
85
+ * Sets the HTTP method to GET and specifies the URL for the node.
86
+ * @param url The URL endpoint for the GET request.
87
+ * @returns The NodeBuilder instance for chaining.
88
+ */
89
+ get(url) {
90
+ this.node.http.method = "GET";
91
+ this.node.http.url = url;
92
+ return this;
93
+ }
94
+ /**
95
+ * Sets the HTTP method to POST and specifies the URL for the node.
96
+ * @param url The URL endpoint for the POST request.
97
+ * @returns The NodeBuilder instance for chaining.
98
+ */
99
+ post(url) {
100
+ this.node.http.method = "POST";
101
+ this.node.http.url = url;
102
+ return this;
103
+ }
104
+ /**
105
+ * Sets the HTTP method to PUT and specifies the URL for the node.
106
+ * @param url The URL endpoint for the PUT request.
107
+ * @returns The NodeBuilder instance for chaining.
108
+ */
109
+ put(url) {
110
+ this.node.http.method = "PUT";
111
+ this.node.http.url = url;
112
+ return this;
113
+ }
114
+ /**
115
+ * Sets the HTTP method to DELETE and specifies the URL for the node.
116
+ * @param url The URL endpoint for the DELETE request.
117
+ * @returns The NodeBuilder instance for chaining.
118
+ */
119
+ delete(url) {
120
+ this.node.http.method = "DELETE";
121
+ this.node.http.url = url;
122
+ return this;
123
+ }
124
+ /**
125
+ * Sets the HTTP method to PATCH and specifies the URL for the node.
126
+ * @param url The URL endpoint for the PATCH request.
127
+ * @returns The NodeBuilder instance for chaining.
128
+ */
129
+ patch(url) {
130
+ this.node.http.method = "PATCH";
131
+ this.node.http.url = url;
132
+ return this;
133
+ }
134
+ // Conditional branching methods - only available for HTTP nodes
135
+ /**
136
+ * Configures conditional execution logic for the HTTP node.
137
+ * This includes termination, continuation, and polling conditions based on JSONata expressions.
138
+ * @param conditions An object defining the conditional logic (`terminateWhen`, `continueTo`, `pollUntil`).
139
+ * @returns The NodeBuilder instance for chaining.
140
+ * @throws {Error} If called on a non-HTTP node.
141
+ * @throws {Error} If any JSONata expression within conditions is invalid.
142
+ */
143
+ withConditions(conditions) {
144
+ if (!this.isHttpNode) {
145
+ throw new Error("Conditional branching is only available for HTTP nodes");
146
+ }
147
+ // Validate JSONata expressions in conditions
148
+ if (conditions.terminateWhen) {
149
+ conditions.terminateWhen.forEach((expression) => {
150
+ this.validateJsonataExpression(expression, "terminateWhen condition");
151
+ });
152
+ }
153
+ if (conditions.continueTo) {
154
+ conditions.continueTo.forEach(({ when }) => {
155
+ this.validateJsonataExpression(when, "continueTo condition");
156
+ });
157
+ }
158
+ if (conditions.pollUntil) {
159
+ conditions.pollUntil.forEach((expression) => {
160
+ this.validateJsonataExpression(expression, "pollUntil condition");
161
+ });
162
+ }
163
+ const httpNode = this.node;
164
+ httpNode.conditions = conditions;
165
+ return this;
166
+ }
167
+ // Configuration methods
168
+ /**
169
+ * Sets the HTTP headers for the node's request.
170
+ * Values can be static strings or dynamic JSONata expressions.
171
+ * @param headers A record of header names to string values or JsonataParam objects.
172
+ * @returns The NodeBuilder instance for chaining.
173
+ * @throws {Error} If any JSONata expression within headers is invalid.
174
+ */
175
+ withHeaders(headers) {
176
+ // Validate any JSONata expressions in headers
177
+ Object.entries(headers).forEach(([key, value]) => {
178
+ if (typeof value === "object" && "jsonataExpression" in value) {
179
+ this.validateJsonataExpression(value.jsonataExpression, `header '${key}'`);
180
+ }
181
+ });
182
+ this.node.http.headers = headers;
183
+ return this;
184
+ }
185
+ /**
186
+ * Sets the body for the HTTP request.
187
+ * Can be a JSON object or a string (potentially containing template expressions).
188
+ * Template expression validation occurs during final workflow validation.
189
+ * @param body The request body as an object or string.
190
+ * @returns The NodeBuilder instance for chaining.
191
+ */
192
+ withBody(body) {
193
+ // Note: Template expression validation happens during final workflow validation
194
+ // This allows us to collect all validation errors at once and maintain a single source of truth
195
+ if (typeof body === "string") {
196
+ this.node.http.body = body;
197
+ }
198
+ else {
199
+ this.node.http.body = body;
200
+ }
201
+ return this;
202
+ }
203
+ /**
204
+ * Configures the retry policy for the HTTP request.
205
+ * Merges provided partial configuration with any existing policy.
206
+ * @param config Partial configuration for the RetryPolicy.
207
+ * @returns The NodeBuilder instance for chaining.
208
+ */
209
+ withRetries(config) {
210
+ // Ensure activityConfig exists
211
+ if (!this.node.activityConfig) {
212
+ this.node.activityConfig = {};
213
+ }
214
+ this.node.activityConfig.retryPolicy = {
215
+ ...this.node.activityConfig.retryPolicy,
216
+ ...config,
217
+ };
218
+ return this;
219
+ }
220
+ /**
221
+ * Sets the start-to-close timeout for the activity's execution.
222
+ * Maximum time allowed for the activity to complete once it starts executing.
223
+ * @param duration The timeout duration (e.g., '30s', 5000).
224
+ * @returns The NodeBuilder instance for chaining.
225
+ */
226
+ withStartToCloseTimeout(duration) {
227
+ if (!this.node.activityConfig) {
228
+ this.node.activityConfig = {};
229
+ }
230
+ this.node.activityConfig.startToCloseTimeout = duration;
231
+ return this;
232
+ }
233
+ /**
234
+ * Sets the schedule-to-close timeout for the activity's execution.
235
+ * Maximum time from when the activity is scheduled until it completes (includes queue time + execution time).
236
+ * @param duration The timeout duration (e.g., '30s', 5000).
237
+ * @returns The NodeBuilder instance for chaining.
238
+ */
239
+ withScheduleToCloseTimeout(duration) {
240
+ if (!this.node.activityConfig) {
241
+ this.node.activityConfig = {};
242
+ }
243
+ this.node.activityConfig.scheduleToCloseTimeout = duration;
244
+ return this;
245
+ }
246
+ /**
247
+ * Specifies the nodes that this node depends on.
248
+ * The node will only execute after all its dependencies have successfully completed.
249
+ * Applicable only to HTTP and agent nodes.
250
+ * @param nodeIds An array of node IDs that this node depends on.
251
+ * @returns The NodeBuilder instance for chaining.
252
+ * @throws {Error} If called on a non-HTTP or non-agent node.
253
+ */
254
+ withDependencies(nodeIds) {
255
+ if (this.isHttpNode) {
256
+ this.node.dependencies = nodeIds;
257
+ }
258
+ else if (this.isAgentNode) {
259
+ this.node.dependencies = nodeIds;
260
+ }
261
+ else {
262
+ throw new Error("Dependencies can only be set on HTTP or agent nodes");
263
+ }
264
+ return this;
265
+ }
266
+ /**
267
+ * Configures input and/or output validation schemas for the HTTP node using Zod schemas.
268
+ * The Zod schemas are converted to JSON Schema for runtime validation.
269
+ * @param config An object containing optional `input` and `output` Zod schemas.
270
+ * @returns The NodeBuilder instance for chaining.
271
+ * @throws {Error} If called on a non-HTTP node.
272
+ */
273
+ withValidation(config) {
274
+ if (!this.isHttpNode) {
275
+ throw new Error("Validation can only be set on HTTP nodes");
276
+ }
277
+ if (config.input || config.output) {
278
+ this.node.validation = {};
279
+ if (config.input) {
280
+ // Use 'as any' to prevent deep type instantiation errors
281
+ const inputSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(config.input);
282
+ this.node.validation.input =
283
+ inputSchema.definitions?.[Object.keys(inputSchema.definitions || {})[0]] || inputSchema;
284
+ }
285
+ if (config.output) {
286
+ // Use 'as any' to prevent deep type instantiation errors
287
+ const outputSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(config.output);
288
+ this.node.validation.output =
289
+ outputSchema.definitions?.[Object.keys(outputSchema.definitions || {})[0]] ||
290
+ outputSchema;
291
+ }
292
+ }
293
+ return this;
294
+ }
295
+ /**
296
+ * Set the maximum number of iterations for an agent node.
297
+ * An agent runs until it returns a completion schema response.
298
+ * You are responsible for ensuring the agent returns a `complete` response before this limit.
299
+ * If the agent reaches the max iterations without completing, it will fail.
300
+ *
301
+ * @default 5
302
+ * @param maxIterations Must be greater than 0
303
+ * @throws {Error} If called on a non-agent node
304
+ * @throws {ValidationError} If maxIterations is less than 1
305
+ */
306
+ withMaxIterations(maxIterations) {
307
+ if (!this.isAgentNode) {
308
+ throw new Error("Max iterations can only be set for agent nodes");
309
+ }
310
+ const agentNode = this.node;
311
+ agentNode.http.config.max_iterations = maxIterations;
312
+ return this;
313
+ }
314
+ /**
315
+ * Set the tools available to the agent.
316
+ * Tools must be defined in the global tools section of the workflow.
317
+ * Each tool name must be unique within the workflow and have no dashes or spaces.
318
+ *
319
+ * @param tools Array of tool names that the agent can use
320
+ * @throws {Error} If called on a non-agent node
321
+ * @throws {ValidationError} If any tool is not found in global tools
322
+ */
323
+ withTools(tools) {
324
+ if (!this.isAgentNode) {
325
+ throw new Error("Tools can only be set for agent nodes");
326
+ }
327
+ const agentNode = this.node;
328
+ agentNode.tools = tools;
329
+ return this;
330
+ }
331
+ /**
332
+ * Add a single tool to the agent's available tools.
333
+ * The tool must be defined in the global tools section of the workflow.
334
+ * The tool name must be unique within the workflow and have no dashes or spaces.
335
+ *
336
+ * @param tool Name of the tool to add
337
+ * @throws {Error} If called on a non-agent node
338
+ * @throws {ValidationError} If the tool is not found in global tools
339
+ */
340
+ addTool(tool) {
341
+ if (!this.isAgentNode) {
342
+ throw new Error("Tools can only be added to agent nodes");
343
+ }
344
+ const agentNode = this.node;
345
+ if (!agentNode.tools.includes(tool)) {
346
+ agentNode.tools.push(tool);
347
+ }
348
+ return this;
349
+ }
350
+ // Terminal operation
351
+ /**
352
+ * Finalizes the node configuration and adds it to the parent GraphCompose builder.
353
+ * This method MUST be called to complete the node definition.
354
+ *
355
+ * @returns The parent GraphCompose instance, allowing for further chaining on the main graph.
356
+ */
357
+ end() {
358
+ // Call the internal add method on the parent graph
359
+ this.graph._addNodeDefinition(this.build());
360
+ return this.graph;
361
+ }
362
+ // Internal method to get the built node
363
+ /**
364
+ * @internal
365
+ * Retrieves the fully configured node object.
366
+ * This method is intended for internal use by the GraphCompose builder.
367
+ * @returns The configured Node object.
368
+ */
369
+ build() {
370
+ // Remove empty activityConfig if no activity settings were provided
371
+ if (this.node.activityConfig && Object.keys(this.node.activityConfig).length === 0) {
372
+ delete this.node.activityConfig;
373
+ }
374
+ return this.node;
375
+ }
376
+ /**
377
+ * @internal
378
+ * Retrieves the ID of the node being built.
379
+ * Used for generating informative error messages.
380
+ * @returns The ID of the node.
381
+ */
382
+ getId() {
383
+ return this.id;
384
+ }
385
+ }
386
+ exports.NodeBuilder = NodeBuilder;
387
+ //# sourceMappingURL=node-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-builder.js","sourceRoot":"","sources":["../../src/core/node-builder.ts"],"names":[],"mappings":";;;AAUA,2DAAqD;AACrD,8CAAmD;AAGnD;;;;GAIG;AACH,MAAa,WAAW;IAQtB;;;;;;OAMG;IACH,YACmB,KAAmB,EACpC,EAAU,EACV,OAA4C,MAAM,EAClD,cAAyB;QAHR,UAAK,GAAL,KAAK,CAAc;QAKpC,IAAI,CAAC,UAAU,GAAG,IAAI,KAAK,MAAM,CAAC;QAClC,IAAI,CAAC,WAAW,GAAG,IAAI,KAAK,OAAO,CAAC;QACpC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,eAAe;QAE7B,IAAI,IAAI,KAAK,gBAAgB,EAAE,CAAC;YAC9B,IAAI,CAAC,IAAI,GAAG;gBACV,EAAE;gBACF,IAAI,EAAE,gBAAgB;gBACtB,cAAc,EAAE,cAAc,IAAI,EAAE;gBACpC,cAAc,EAAE,EAAE;gBAClB,IAAI,EAAE;oBACJ,MAAM,EAAE,MAAM;oBACd,GAA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