@codemation/core-nodes 0.0.18 → 0.0.21

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/index.js CHANGED
@@ -1,6 +1,8 @@
1
- import { AgentConfigInspector, AgentGuardrailDefaults, AgentMessageConfigNormalizer, ConnectionInvocationIdFactory, ConnectionNodeIdFactory, CoreTokens, DefinedNodeRegistry, ItemsInputNormalizer, NodeBackedToolConfig, RetryPolicy, WorkflowBuilder, chatModel, inject, injectable, node } from "@codemation/core";
1
+ import { AgentConfigInspector, AgentConnectionNodeCollector, AgentGuardrailDefaults, AgentMessageConfigNormalizer, ConnectionInvocationIdFactory, ConnectionNodeIdFactory, CoreTokens, DefinedNodeRegistry, ItemsInputNormalizer, NodeBackedToolConfig, RetryPolicy, WorkflowBuilder, chatModel, inject, injectable, node } from "@codemation/core";
2
2
  import { ChatOpenAI } from "@langchain/openai";
3
3
  import { AIMessage, HumanMessage, SystemMessage, ToolMessage } from "@langchain/core/messages";
4
+ import { isInteropZodSchema } from "@langchain/core/utils/types";
5
+ import { toJsonSchema } from "@langchain/core/utils/json_schema";
4
6
  import { DynamicStructuredTool } from "@langchain/core/tools";
5
7
  import { CredentialResolverFactory } from "@codemation/core/bootstrap";
6
8
 
@@ -147,6 +149,821 @@ var AgentToolCallPortMap = class {
147
149
  }
148
150
  };
149
151
 
152
+ //#endregion
153
+ //#region ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/util.js
154
+ function getEnumValues(entries) {
155
+ const numericValues = Object.values(entries).filter((v) => typeof v === "number");
156
+ return Object.entries(entries).filter(([k, _]) => numericValues.indexOf(+k) === -1).map(([_, v]) => v);
157
+ }
158
+ function cached(getter) {
159
+ return { get value() {
160
+ {
161
+ const value = getter();
162
+ Object.defineProperty(this, "value", { value });
163
+ return value;
164
+ }
165
+ throw new Error("cached value already set");
166
+ } };
167
+ }
168
+ const EVALUATING = Symbol("evaluating");
169
+ const captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => {};
170
+ const allowsEval = cached(() => {
171
+ if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) return false;
172
+ try {
173
+ new Function("");
174
+ return true;
175
+ } catch (_) {
176
+ return false;
177
+ }
178
+ });
179
+ const NUMBER_FORMAT_RANGES = {
180
+ safeint: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
181
+ int32: [-2147483648, 2147483647],
182
+ uint32: [0, 4294967295],
183
+ float32: [-34028234663852886e22, 34028234663852886e22],
184
+ float64: [-Number.MAX_VALUE, Number.MAX_VALUE]
185
+ };
186
+
187
+ //#endregion
188
+ //#region ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/registries.js
189
+ var _a;
190
+ const $output = Symbol("ZodOutput");
191
+ const $input = Symbol("ZodInput");
192
+ var $ZodRegistry = class {
193
+ constructor() {
194
+ this._map = /* @__PURE__ */ new WeakMap();
195
+ this._idmap = /* @__PURE__ */ new Map();
196
+ }
197
+ add(schema, ..._meta) {
198
+ const meta = _meta[0];
199
+ this._map.set(schema, meta);
200
+ if (meta && typeof meta === "object" && "id" in meta) this._idmap.set(meta.id, schema);
201
+ return this;
202
+ }
203
+ clear() {
204
+ this._map = /* @__PURE__ */ new WeakMap();
205
+ this._idmap = /* @__PURE__ */ new Map();
206
+ return this;
207
+ }
208
+ remove(schema) {
209
+ const meta = this._map.get(schema);
210
+ if (meta && typeof meta === "object" && "id" in meta) this._idmap.delete(meta.id);
211
+ this._map.delete(schema);
212
+ return this;
213
+ }
214
+ get(schema) {
215
+ const p = schema._zod.parent;
216
+ if (p) {
217
+ const pm = { ...this.get(p) ?? {} };
218
+ delete pm.id;
219
+ const f = {
220
+ ...pm,
221
+ ...this._map.get(schema)
222
+ };
223
+ return Object.keys(f).length ? f : void 0;
224
+ }
225
+ return this._map.get(schema);
226
+ }
227
+ has(schema) {
228
+ return this._map.has(schema);
229
+ }
230
+ };
231
+ function registry() {
232
+ return new $ZodRegistry();
233
+ }
234
+ (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
235
+ const globalRegistry = globalThis.__zod_globalRegistry;
236
+
237
+ //#endregion
238
+ //#region ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/to-json-schema.js
239
+ function initializeContext(params) {
240
+ let target = params?.target ?? "draft-2020-12";
241
+ if (target === "draft-4") target = "draft-04";
242
+ if (target === "draft-7") target = "draft-07";
243
+ return {
244
+ processors: params.processors ?? {},
245
+ metadataRegistry: params?.metadata ?? globalRegistry,
246
+ target,
247
+ unrepresentable: params?.unrepresentable ?? "throw",
248
+ override: params?.override ?? (() => {}),
249
+ io: params?.io ?? "output",
250
+ counter: 0,
251
+ seen: /* @__PURE__ */ new Map(),
252
+ cycles: params?.cycles ?? "ref",
253
+ reused: params?.reused ?? "inline",
254
+ external: params?.external ?? void 0
255
+ };
256
+ }
257
+ function process(schema, ctx, _params = {
258
+ path: [],
259
+ schemaPath: []
260
+ }) {
261
+ var _a$1;
262
+ const def = schema._zod.def;
263
+ const seen = ctx.seen.get(schema);
264
+ if (seen) {
265
+ seen.count++;
266
+ if (_params.schemaPath.includes(schema)) seen.cycle = _params.path;
267
+ return seen.schema;
268
+ }
269
+ const result = {
270
+ schema: {},
271
+ count: 1,
272
+ cycle: void 0,
273
+ path: _params.path
274
+ };
275
+ ctx.seen.set(schema, result);
276
+ const overrideSchema = schema._zod.toJSONSchema?.();
277
+ if (overrideSchema) result.schema = overrideSchema;
278
+ else {
279
+ const params = {
280
+ ..._params,
281
+ schemaPath: [..._params.schemaPath, schema],
282
+ path: _params.path
283
+ };
284
+ if (schema._zod.processJSONSchema) schema._zod.processJSONSchema(ctx, result.schema, params);
285
+ else {
286
+ const _json = result.schema;
287
+ const processor = ctx.processors[def.type];
288
+ if (!processor) throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
289
+ processor(schema, ctx, _json, params);
290
+ }
291
+ const parent = schema._zod.parent;
292
+ if (parent) {
293
+ if (!result.ref) result.ref = parent;
294
+ process(parent, ctx, params);
295
+ ctx.seen.get(parent).isParent = true;
296
+ }
297
+ }
298
+ const meta = ctx.metadataRegistry.get(schema);
299
+ if (meta) Object.assign(result.schema, meta);
300
+ if (ctx.io === "input" && isTransforming(schema)) {
301
+ delete result.schema.examples;
302
+ delete result.schema.default;
303
+ }
304
+ if (ctx.io === "input" && result.schema._prefault) (_a$1 = result.schema).default ?? (_a$1.default = result.schema._prefault);
305
+ delete result.schema._prefault;
306
+ return ctx.seen.get(schema).schema;
307
+ }
308
+ function extractDefs(ctx, schema) {
309
+ const root = ctx.seen.get(schema);
310
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
311
+ const idToSchema = /* @__PURE__ */ new Map();
312
+ for (const entry of ctx.seen.entries()) {
313
+ const id = ctx.metadataRegistry.get(entry[0])?.id;
314
+ if (id) {
315
+ const existing = idToSchema.get(id);
316
+ if (existing && existing !== entry[0]) throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
317
+ idToSchema.set(id, entry[0]);
318
+ }
319
+ }
320
+ const makeURI = (entry) => {
321
+ const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
322
+ if (ctx.external) {
323
+ const externalId = ctx.external.registry.get(entry[0])?.id;
324
+ const uriGenerator = ctx.external.uri ?? ((id$1) => id$1);
325
+ if (externalId) return { ref: uriGenerator(externalId) };
326
+ const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
327
+ entry[1].defId = id;
328
+ return {
329
+ defId: id,
330
+ ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}`
331
+ };
332
+ }
333
+ if (entry[1] === root) return { ref: "#" };
334
+ const defUriPrefix = `#/${defsSegment}/`;
335
+ const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
336
+ return {
337
+ defId,
338
+ ref: defUriPrefix + defId
339
+ };
340
+ };
341
+ const extractToDef = (entry) => {
342
+ if (entry[1].schema.$ref) return;
343
+ const seen = entry[1];
344
+ const { ref, defId } = makeURI(entry);
345
+ seen.def = { ...seen.schema };
346
+ if (defId) seen.defId = defId;
347
+ const schema$1 = seen.schema;
348
+ for (const key in schema$1) delete schema$1[key];
349
+ schema$1.$ref = ref;
350
+ };
351
+ if (ctx.cycles === "throw") for (const entry of ctx.seen.entries()) {
352
+ const seen = entry[1];
353
+ if (seen.cycle) throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>
354
+
355
+ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
356
+ }
357
+ for (const entry of ctx.seen.entries()) {
358
+ const seen = entry[1];
359
+ if (schema === entry[0]) {
360
+ extractToDef(entry);
361
+ continue;
362
+ }
363
+ if (ctx.external) {
364
+ const ext = ctx.external.registry.get(entry[0])?.id;
365
+ if (schema !== entry[0] && ext) {
366
+ extractToDef(entry);
367
+ continue;
368
+ }
369
+ }
370
+ if (ctx.metadataRegistry.get(entry[0])?.id) {
371
+ extractToDef(entry);
372
+ continue;
373
+ }
374
+ if (seen.cycle) {
375
+ extractToDef(entry);
376
+ continue;
377
+ }
378
+ if (seen.count > 1) {
379
+ if (ctx.reused === "ref") {
380
+ extractToDef(entry);
381
+ continue;
382
+ }
383
+ }
384
+ }
385
+ }
386
+ function finalize(ctx, schema) {
387
+ const root = ctx.seen.get(schema);
388
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
389
+ const flattenRef = (zodSchema) => {
390
+ const seen = ctx.seen.get(zodSchema);
391
+ if (seen.ref === null) return;
392
+ const schema$1 = seen.def ?? seen.schema;
393
+ const _cached = { ...schema$1 };
394
+ const ref = seen.ref;
395
+ seen.ref = null;
396
+ if (ref) {
397
+ flattenRef(ref);
398
+ const refSeen = ctx.seen.get(ref);
399
+ const refSchema = refSeen.schema;
400
+ if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
401
+ schema$1.allOf = schema$1.allOf ?? [];
402
+ schema$1.allOf.push(refSchema);
403
+ } else Object.assign(schema$1, refSchema);
404
+ Object.assign(schema$1, _cached);
405
+ if (zodSchema._zod.parent === ref) for (const key in schema$1) {
406
+ if (key === "$ref" || key === "allOf") continue;
407
+ if (!(key in _cached)) delete schema$1[key];
408
+ }
409
+ if (refSchema.$ref && refSeen.def) for (const key in schema$1) {
410
+ if (key === "$ref" || key === "allOf") continue;
411
+ if (key in refSeen.def && JSON.stringify(schema$1[key]) === JSON.stringify(refSeen.def[key])) delete schema$1[key];
412
+ }
413
+ }
414
+ const parent = zodSchema._zod.parent;
415
+ if (parent && parent !== ref) {
416
+ flattenRef(parent);
417
+ const parentSeen = ctx.seen.get(parent);
418
+ if (parentSeen?.schema.$ref) {
419
+ schema$1.$ref = parentSeen.schema.$ref;
420
+ if (parentSeen.def) for (const key in schema$1) {
421
+ if (key === "$ref" || key === "allOf") continue;
422
+ if (key in parentSeen.def && JSON.stringify(schema$1[key]) === JSON.stringify(parentSeen.def[key])) delete schema$1[key];
423
+ }
424
+ }
425
+ }
426
+ ctx.override({
427
+ zodSchema,
428
+ jsonSchema: schema$1,
429
+ path: seen.path ?? []
430
+ });
431
+ };
432
+ for (const entry of [...ctx.seen.entries()].reverse()) flattenRef(entry[0]);
433
+ const result = {};
434
+ if (ctx.target === "draft-2020-12") result.$schema = "https://json-schema.org/draft/2020-12/schema";
435
+ else if (ctx.target === "draft-07") result.$schema = "http://json-schema.org/draft-07/schema#";
436
+ else if (ctx.target === "draft-04") result.$schema = "http://json-schema.org/draft-04/schema#";
437
+ else if (ctx.target === "openapi-3.0") {}
438
+ if (ctx.external?.uri) {
439
+ const id = ctx.external.registry.get(schema)?.id;
440
+ if (!id) throw new Error("Schema is missing an `id` property");
441
+ result.$id = ctx.external.uri(id);
442
+ }
443
+ Object.assign(result, root.def ?? root.schema);
444
+ const defs = ctx.external?.defs ?? {};
445
+ for (const entry of ctx.seen.entries()) {
446
+ const seen = entry[1];
447
+ if (seen.def && seen.defId) defs[seen.defId] = seen.def;
448
+ }
449
+ if (ctx.external) {} else if (Object.keys(defs).length > 0) if (ctx.target === "draft-2020-12") result.$defs = defs;
450
+ else result.definitions = defs;
451
+ try {
452
+ const finalized = JSON.parse(JSON.stringify(result));
453
+ Object.defineProperty(finalized, "~standard", {
454
+ value: {
455
+ ...schema["~standard"],
456
+ jsonSchema: {
457
+ input: createStandardJSONSchemaMethod(schema, "input", ctx.processors),
458
+ output: createStandardJSONSchemaMethod(schema, "output", ctx.processors)
459
+ }
460
+ },
461
+ enumerable: false,
462
+ writable: false
463
+ });
464
+ return finalized;
465
+ } catch (_err) {
466
+ throw new Error("Error converting schema to JSON.");
467
+ }
468
+ }
469
+ function isTransforming(_schema, _ctx) {
470
+ const ctx = _ctx ?? { seen: /* @__PURE__ */ new Set() };
471
+ if (ctx.seen.has(_schema)) return false;
472
+ ctx.seen.add(_schema);
473
+ const def = _schema._zod.def;
474
+ if (def.type === "transform") return true;
475
+ if (def.type === "array") return isTransforming(def.element, ctx);
476
+ if (def.type === "set") return isTransforming(def.valueType, ctx);
477
+ if (def.type === "lazy") return isTransforming(def.getter(), ctx);
478
+ if (def.type === "promise" || def.type === "optional" || def.type === "nonoptional" || def.type === "nullable" || def.type === "readonly" || def.type === "default" || def.type === "prefault") return isTransforming(def.innerType, ctx);
479
+ if (def.type === "intersection") return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
480
+ if (def.type === "record" || def.type === "map") return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
481
+ if (def.type === "pipe") return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
482
+ if (def.type === "object") {
483
+ for (const key in def.shape) if (isTransforming(def.shape[key], ctx)) return true;
484
+ return false;
485
+ }
486
+ if (def.type === "union") {
487
+ for (const option of def.options) if (isTransforming(option, ctx)) return true;
488
+ return false;
489
+ }
490
+ if (def.type === "tuple") {
491
+ for (const item of def.items) if (isTransforming(item, ctx)) return true;
492
+ if (def.rest && isTransforming(def.rest, ctx)) return true;
493
+ return false;
494
+ }
495
+ return false;
496
+ }
497
+ const createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
498
+ const { libraryOptions, target } = params ?? {};
499
+ const ctx = initializeContext({
500
+ ...libraryOptions ?? {},
501
+ target,
502
+ io,
503
+ processors
504
+ });
505
+ process(schema, ctx);
506
+ extractDefs(ctx, schema);
507
+ return finalize(ctx, schema);
508
+ };
509
+
510
+ //#endregion
511
+ //#region ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/json-schema-processors.js
512
+ const formatMap = {
513
+ guid: "uuid",
514
+ url: "uri",
515
+ datetime: "date-time",
516
+ json_string: "json-string",
517
+ regex: ""
518
+ };
519
+ const stringProcessor = (schema, ctx, _json, _params) => {
520
+ const json = _json;
521
+ json.type = "string";
522
+ const { minimum, maximum, format, patterns, contentEncoding } = schema._zod.bag;
523
+ if (typeof minimum === "number") json.minLength = minimum;
524
+ if (typeof maximum === "number") json.maxLength = maximum;
525
+ if (format) {
526
+ json.format = formatMap[format] ?? format;
527
+ if (json.format === "") delete json.format;
528
+ if (format === "time") delete json.format;
529
+ }
530
+ if (contentEncoding) json.contentEncoding = contentEncoding;
531
+ if (patterns && patterns.size > 0) {
532
+ const regexes = [...patterns];
533
+ if (regexes.length === 1) json.pattern = regexes[0].source;
534
+ else if (regexes.length > 1) json.allOf = [...regexes.map((regex) => ({
535
+ ...ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0" ? { type: "string" } : {},
536
+ pattern: regex.source
537
+ }))];
538
+ }
539
+ };
540
+ const numberProcessor = (schema, ctx, _json, _params) => {
541
+ const json = _json;
542
+ const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
543
+ if (typeof format === "string" && format.includes("int")) json.type = "integer";
544
+ else json.type = "number";
545
+ if (typeof exclusiveMinimum === "number") if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
546
+ json.minimum = exclusiveMinimum;
547
+ json.exclusiveMinimum = true;
548
+ } else json.exclusiveMinimum = exclusiveMinimum;
549
+ if (typeof minimum === "number") {
550
+ json.minimum = minimum;
551
+ if (typeof exclusiveMinimum === "number" && ctx.target !== "draft-04") if (exclusiveMinimum >= minimum) delete json.minimum;
552
+ else delete json.exclusiveMinimum;
553
+ }
554
+ if (typeof exclusiveMaximum === "number") if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
555
+ json.maximum = exclusiveMaximum;
556
+ json.exclusiveMaximum = true;
557
+ } else json.exclusiveMaximum = exclusiveMaximum;
558
+ if (typeof maximum === "number") {
559
+ json.maximum = maximum;
560
+ if (typeof exclusiveMaximum === "number" && ctx.target !== "draft-04") if (exclusiveMaximum <= maximum) delete json.maximum;
561
+ else delete json.exclusiveMaximum;
562
+ }
563
+ if (typeof multipleOf === "number") json.multipleOf = multipleOf;
564
+ };
565
+ const booleanProcessor = (_schema, _ctx, json, _params) => {
566
+ json.type = "boolean";
567
+ };
568
+ const bigintProcessor = (_schema, ctx, _json, _params) => {
569
+ if (ctx.unrepresentable === "throw") throw new Error("BigInt cannot be represented in JSON Schema");
570
+ };
571
+ const symbolProcessor = (_schema, ctx, _json, _params) => {
572
+ if (ctx.unrepresentable === "throw") throw new Error("Symbols cannot be represented in JSON Schema");
573
+ };
574
+ const nullProcessor = (_schema, ctx, json, _params) => {
575
+ if (ctx.target === "openapi-3.0") {
576
+ json.type = "string";
577
+ json.nullable = true;
578
+ json.enum = [null];
579
+ } else json.type = "null";
580
+ };
581
+ const undefinedProcessor = (_schema, ctx, _json, _params) => {
582
+ if (ctx.unrepresentable === "throw") throw new Error("Undefined cannot be represented in JSON Schema");
583
+ };
584
+ const voidProcessor = (_schema, ctx, _json, _params) => {
585
+ if (ctx.unrepresentable === "throw") throw new Error("Void cannot be represented in JSON Schema");
586
+ };
587
+ const neverProcessor = (_schema, _ctx, json, _params) => {
588
+ json.not = {};
589
+ };
590
+ const anyProcessor = (_schema, _ctx, _json, _params) => {};
591
+ const unknownProcessor = (_schema, _ctx, _json, _params) => {};
592
+ const dateProcessor = (_schema, ctx, _json, _params) => {
593
+ if (ctx.unrepresentable === "throw") throw new Error("Date cannot be represented in JSON Schema");
594
+ };
595
+ const enumProcessor = (schema, _ctx, json, _params) => {
596
+ const def = schema._zod.def;
597
+ const values = getEnumValues(def.entries);
598
+ if (values.every((v) => typeof v === "number")) json.type = "number";
599
+ if (values.every((v) => typeof v === "string")) json.type = "string";
600
+ json.enum = values;
601
+ };
602
+ const literalProcessor = (schema, ctx, json, _params) => {
603
+ const def = schema._zod.def;
604
+ const vals = [];
605
+ for (const val of def.values) if (val === void 0) {
606
+ if (ctx.unrepresentable === "throw") throw new Error("Literal `undefined` cannot be represented in JSON Schema");
607
+ } else if (typeof val === "bigint") if (ctx.unrepresentable === "throw") throw new Error("BigInt literals cannot be represented in JSON Schema");
608
+ else vals.push(Number(val));
609
+ else vals.push(val);
610
+ if (vals.length === 0) {} else if (vals.length === 1) {
611
+ const val = vals[0];
612
+ json.type = val === null ? "null" : typeof val;
613
+ if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") json.enum = [val];
614
+ else json.const = val;
615
+ } else {
616
+ if (vals.every((v) => typeof v === "number")) json.type = "number";
617
+ if (vals.every((v) => typeof v === "string")) json.type = "string";
618
+ if (vals.every((v) => typeof v === "boolean")) json.type = "boolean";
619
+ if (vals.every((v) => v === null)) json.type = "null";
620
+ json.enum = vals;
621
+ }
622
+ };
623
+ const nanProcessor = (_schema, ctx, _json, _params) => {
624
+ if (ctx.unrepresentable === "throw") throw new Error("NaN cannot be represented in JSON Schema");
625
+ };
626
+ const templateLiteralProcessor = (schema, _ctx, json, _params) => {
627
+ const _json = json;
628
+ const pattern = schema._zod.pattern;
629
+ if (!pattern) throw new Error("Pattern not found in template literal");
630
+ _json.type = "string";
631
+ _json.pattern = pattern.source;
632
+ };
633
+ const fileProcessor = (schema, _ctx, json, _params) => {
634
+ const _json = json;
635
+ const file = {
636
+ type: "string",
637
+ format: "binary",
638
+ contentEncoding: "binary"
639
+ };
640
+ const { minimum, maximum, mime } = schema._zod.bag;
641
+ if (minimum !== void 0) file.minLength = minimum;
642
+ if (maximum !== void 0) file.maxLength = maximum;
643
+ if (mime) if (mime.length === 1) {
644
+ file.contentMediaType = mime[0];
645
+ Object.assign(_json, file);
646
+ } else {
647
+ Object.assign(_json, file);
648
+ _json.anyOf = mime.map((m) => ({ contentMediaType: m }));
649
+ }
650
+ else Object.assign(_json, file);
651
+ };
652
+ const successProcessor = (_schema, _ctx, json, _params) => {
653
+ json.type = "boolean";
654
+ };
655
+ const customProcessor = (_schema, ctx, _json, _params) => {
656
+ if (ctx.unrepresentable === "throw") throw new Error("Custom types cannot be represented in JSON Schema");
657
+ };
658
+ const functionProcessor = (_schema, ctx, _json, _params) => {
659
+ if (ctx.unrepresentable === "throw") throw new Error("Function types cannot be represented in JSON Schema");
660
+ };
661
+ const transformProcessor = (_schema, ctx, _json, _params) => {
662
+ if (ctx.unrepresentable === "throw") throw new Error("Transforms cannot be represented in JSON Schema");
663
+ };
664
+ const mapProcessor = (_schema, ctx, _json, _params) => {
665
+ if (ctx.unrepresentable === "throw") throw new Error("Map cannot be represented in JSON Schema");
666
+ };
667
+ const setProcessor = (_schema, ctx, _json, _params) => {
668
+ if (ctx.unrepresentable === "throw") throw new Error("Set cannot be represented in JSON Schema");
669
+ };
670
+ const arrayProcessor = (schema, ctx, _json, params) => {
671
+ const json = _json;
672
+ const def = schema._zod.def;
673
+ const { minimum, maximum } = schema._zod.bag;
674
+ if (typeof minimum === "number") json.minItems = minimum;
675
+ if (typeof maximum === "number") json.maxItems = maximum;
676
+ json.type = "array";
677
+ json.items = process(def.element, ctx, {
678
+ ...params,
679
+ path: [...params.path, "items"]
680
+ });
681
+ };
682
+ const objectProcessor = (schema, ctx, _json, params) => {
683
+ const json = _json;
684
+ const def = schema._zod.def;
685
+ json.type = "object";
686
+ json.properties = {};
687
+ const shape = def.shape;
688
+ for (const key in shape) json.properties[key] = process(shape[key], ctx, {
689
+ ...params,
690
+ path: [
691
+ ...params.path,
692
+ "properties",
693
+ key
694
+ ]
695
+ });
696
+ const allKeys = new Set(Object.keys(shape));
697
+ const requiredKeys = new Set([...allKeys].filter((key) => {
698
+ const v = def.shape[key]._zod;
699
+ if (ctx.io === "input") return v.optin === void 0;
700
+ else return v.optout === void 0;
701
+ }));
702
+ if (requiredKeys.size > 0) json.required = Array.from(requiredKeys);
703
+ if (def.catchall?._zod.def.type === "never") json.additionalProperties = false;
704
+ else if (!def.catchall) {
705
+ if (ctx.io === "output") json.additionalProperties = false;
706
+ } else if (def.catchall) json.additionalProperties = process(def.catchall, ctx, {
707
+ ...params,
708
+ path: [...params.path, "additionalProperties"]
709
+ });
710
+ };
711
+ const unionProcessor = (schema, ctx, json, params) => {
712
+ const def = schema._zod.def;
713
+ const isExclusive = def.inclusive === false;
714
+ const options = def.options.map((x, i) => process(x, ctx, {
715
+ ...params,
716
+ path: [
717
+ ...params.path,
718
+ isExclusive ? "oneOf" : "anyOf",
719
+ i
720
+ ]
721
+ }));
722
+ if (isExclusive) json.oneOf = options;
723
+ else json.anyOf = options;
724
+ };
725
+ const intersectionProcessor = (schema, ctx, json, params) => {
726
+ const def = schema._zod.def;
727
+ const a = process(def.left, ctx, {
728
+ ...params,
729
+ path: [
730
+ ...params.path,
731
+ "allOf",
732
+ 0
733
+ ]
734
+ });
735
+ const b = process(def.right, ctx, {
736
+ ...params,
737
+ path: [
738
+ ...params.path,
739
+ "allOf",
740
+ 1
741
+ ]
742
+ });
743
+ const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
744
+ json.allOf = [...isSimpleIntersection(a) ? a.allOf : [a], ...isSimpleIntersection(b) ? b.allOf : [b]];
745
+ };
746
+ const tupleProcessor = (schema, ctx, _json, params) => {
747
+ const json = _json;
748
+ const def = schema._zod.def;
749
+ json.type = "array";
750
+ const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
751
+ const restPath = ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
752
+ const prefixItems = def.items.map((x, i) => process(x, ctx, {
753
+ ...params,
754
+ path: [
755
+ ...params.path,
756
+ prefixPath,
757
+ i
758
+ ]
759
+ }));
760
+ const rest = def.rest ? process(def.rest, ctx, {
761
+ ...params,
762
+ path: [
763
+ ...params.path,
764
+ restPath,
765
+ ...ctx.target === "openapi-3.0" ? [def.items.length] : []
766
+ ]
767
+ }) : null;
768
+ if (ctx.target === "draft-2020-12") {
769
+ json.prefixItems = prefixItems;
770
+ if (rest) json.items = rest;
771
+ } else if (ctx.target === "openapi-3.0") {
772
+ json.items = { anyOf: prefixItems };
773
+ if (rest) json.items.anyOf.push(rest);
774
+ json.minItems = prefixItems.length;
775
+ if (!rest) json.maxItems = prefixItems.length;
776
+ } else {
777
+ json.items = prefixItems;
778
+ if (rest) json.additionalItems = rest;
779
+ }
780
+ const { minimum, maximum } = schema._zod.bag;
781
+ if (typeof minimum === "number") json.minItems = minimum;
782
+ if (typeof maximum === "number") json.maxItems = maximum;
783
+ };
784
+ const recordProcessor = (schema, ctx, _json, params) => {
785
+ const json = _json;
786
+ const def = schema._zod.def;
787
+ json.type = "object";
788
+ const keyType = def.keyType;
789
+ const patterns = keyType._zod.bag?.patterns;
790
+ if (def.mode === "loose" && patterns && patterns.size > 0) {
791
+ const valueSchema = process(def.valueType, ctx, {
792
+ ...params,
793
+ path: [
794
+ ...params.path,
795
+ "patternProperties",
796
+ "*"
797
+ ]
798
+ });
799
+ json.patternProperties = {};
800
+ for (const pattern of patterns) json.patternProperties[pattern.source] = valueSchema;
801
+ } else {
802
+ if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") json.propertyNames = process(def.keyType, ctx, {
803
+ ...params,
804
+ path: [...params.path, "propertyNames"]
805
+ });
806
+ json.additionalProperties = process(def.valueType, ctx, {
807
+ ...params,
808
+ path: [...params.path, "additionalProperties"]
809
+ });
810
+ }
811
+ const keyValues = keyType._zod.values;
812
+ if (keyValues) {
813
+ const validKeyValues = [...keyValues].filter((v) => typeof v === "string" || typeof v === "number");
814
+ if (validKeyValues.length > 0) json.required = validKeyValues;
815
+ }
816
+ };
817
+ const nullableProcessor = (schema, ctx, json, params) => {
818
+ const def = schema._zod.def;
819
+ const inner = process(def.innerType, ctx, params);
820
+ const seen = ctx.seen.get(schema);
821
+ if (ctx.target === "openapi-3.0") {
822
+ seen.ref = def.innerType;
823
+ json.nullable = true;
824
+ } else json.anyOf = [inner, { type: "null" }];
825
+ };
826
+ const nonoptionalProcessor = (schema, ctx, _json, params) => {
827
+ const def = schema._zod.def;
828
+ process(def.innerType, ctx, params);
829
+ const seen = ctx.seen.get(schema);
830
+ seen.ref = def.innerType;
831
+ };
832
+ const defaultProcessor = (schema, ctx, json, params) => {
833
+ const def = schema._zod.def;
834
+ process(def.innerType, ctx, params);
835
+ const seen = ctx.seen.get(schema);
836
+ seen.ref = def.innerType;
837
+ json.default = JSON.parse(JSON.stringify(def.defaultValue));
838
+ };
839
+ const prefaultProcessor = (schema, ctx, json, params) => {
840
+ const def = schema._zod.def;
841
+ process(def.innerType, ctx, params);
842
+ const seen = ctx.seen.get(schema);
843
+ seen.ref = def.innerType;
844
+ if (ctx.io === "input") json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
845
+ };
846
+ const catchProcessor = (schema, ctx, json, params) => {
847
+ const def = schema._zod.def;
848
+ process(def.innerType, ctx, params);
849
+ const seen = ctx.seen.get(schema);
850
+ seen.ref = def.innerType;
851
+ let catchValue;
852
+ try {
853
+ catchValue = def.catchValue(void 0);
854
+ } catch {
855
+ throw new Error("Dynamic catch values are not supported in JSON Schema");
856
+ }
857
+ json.default = catchValue;
858
+ };
859
+ const pipeProcessor = (schema, ctx, _json, params) => {
860
+ const def = schema._zod.def;
861
+ const innerType = ctx.io === "input" ? def.in._zod.def.type === "transform" ? def.out : def.in : def.out;
862
+ process(innerType, ctx, params);
863
+ const seen = ctx.seen.get(schema);
864
+ seen.ref = innerType;
865
+ };
866
+ const readonlyProcessor = (schema, ctx, json, params) => {
867
+ const def = schema._zod.def;
868
+ process(def.innerType, ctx, params);
869
+ const seen = ctx.seen.get(schema);
870
+ seen.ref = def.innerType;
871
+ json.readOnly = true;
872
+ };
873
+ const promiseProcessor = (schema, ctx, _json, params) => {
874
+ const def = schema._zod.def;
875
+ process(def.innerType, ctx, params);
876
+ const seen = ctx.seen.get(schema);
877
+ seen.ref = def.innerType;
878
+ };
879
+ const optionalProcessor = (schema, ctx, _json, params) => {
880
+ const def = schema._zod.def;
881
+ process(def.innerType, ctx, params);
882
+ const seen = ctx.seen.get(schema);
883
+ seen.ref = def.innerType;
884
+ };
885
+ const lazyProcessor = (schema, ctx, _json, params) => {
886
+ const innerType = schema._zod.innerType;
887
+ process(innerType, ctx, params);
888
+ const seen = ctx.seen.get(schema);
889
+ seen.ref = innerType;
890
+ };
891
+ const allProcessors = {
892
+ string: stringProcessor,
893
+ number: numberProcessor,
894
+ boolean: booleanProcessor,
895
+ bigint: bigintProcessor,
896
+ symbol: symbolProcessor,
897
+ null: nullProcessor,
898
+ undefined: undefinedProcessor,
899
+ void: voidProcessor,
900
+ never: neverProcessor,
901
+ any: anyProcessor,
902
+ unknown: unknownProcessor,
903
+ date: dateProcessor,
904
+ enum: enumProcessor,
905
+ literal: literalProcessor,
906
+ nan: nanProcessor,
907
+ template_literal: templateLiteralProcessor,
908
+ file: fileProcessor,
909
+ success: successProcessor,
910
+ custom: customProcessor,
911
+ function: functionProcessor,
912
+ transform: transformProcessor,
913
+ map: mapProcessor,
914
+ set: setProcessor,
915
+ array: arrayProcessor,
916
+ object: objectProcessor,
917
+ union: unionProcessor,
918
+ intersection: intersectionProcessor,
919
+ tuple: tupleProcessor,
920
+ record: recordProcessor,
921
+ nullable: nullableProcessor,
922
+ nonoptional: nonoptionalProcessor,
923
+ default: defaultProcessor,
924
+ prefault: prefaultProcessor,
925
+ catch: catchProcessor,
926
+ pipe: pipeProcessor,
927
+ readonly: readonlyProcessor,
928
+ promise: promiseProcessor,
929
+ optional: optionalProcessor,
930
+ lazy: lazyProcessor
931
+ };
932
+ function toJSONSchema(input, params) {
933
+ if ("_idmap" in input) {
934
+ const registry$1 = input;
935
+ const ctx$1 = initializeContext({
936
+ ...params,
937
+ processors: allProcessors
938
+ });
939
+ const defs = {};
940
+ for (const entry of registry$1._idmap.entries()) {
941
+ const [_, schema] = entry;
942
+ process(schema, ctx$1);
943
+ }
944
+ const schemas = {};
945
+ ctx$1.external = {
946
+ registry: registry$1,
947
+ uri: params?.uri,
948
+ defs
949
+ };
950
+ for (const entry of registry$1._idmap.entries()) {
951
+ const [key, schema] = entry;
952
+ extractDefs(ctx$1, schema);
953
+ schemas[key] = finalize(ctx$1, schema);
954
+ }
955
+ if (Object.keys(defs).length > 0) schemas.__shared = { [ctx$1.target === "draft-2020-12" ? "$defs" : "definitions"]: defs };
956
+ return { schemas };
957
+ }
958
+ const ctx = initializeContext({
959
+ ...params,
960
+ processors: allProcessors
961
+ });
962
+ process(input, ctx);
963
+ extractDefs(ctx, input);
964
+ return finalize(ctx, input);
965
+ }
966
+
150
967
  //#endregion
151
968
  //#region src/nodes/ConnectionCredentialExecutionContextFactory.ts
152
969
  /**
@@ -177,10 +994,12 @@ let AIAgentExecutionHelpersFactory = class AIAgentExecutionHelpersFactory$1 {
177
994
  return new ConnectionCredentialExecutionContextFactory(credentialSessions);
178
995
  }
179
996
  createDynamicStructuredTool(entry, toolCredentialContext, item, itemIndex, items) {
997
+ if (entry.runtime.inputSchema == null) throw new Error(`Cannot create LangChain tool "${entry.config.name}": missing inputSchema (broken tool runtime resolution).`);
998
+ const schemaForOpenAi = this.normalizeToolInputSchemaForOpenAiDynamicStructuredTool(entry.config.name, entry.runtime.inputSchema);
180
999
  return new DynamicStructuredTool({
181
1000
  name: entry.config.name,
182
1001
  description: entry.config.description ?? entry.runtime.defaultDescription,
183
- schema: entry.runtime.inputSchema,
1002
+ schema: schemaForOpenAi,
184
1003
  func: async (input) => {
185
1004
  const result = await entry.runtime.execute({
186
1005
  config: entry.config,
@@ -194,6 +1013,59 @@ let AIAgentExecutionHelpersFactory = class AIAgentExecutionHelpersFactory$1 {
194
1013
  }
195
1014
  });
196
1015
  }
1016
+ /**
1017
+ * Produces a plain JSON Schema object for OpenAI tool parameters and LangChain tool invocation:
1018
+ * - **Zod** → `toJSONSchema(..., { target: "draft-07" })` so shapes match what `@cfworker/json-schema`
1019
+ * expects (`required` must be an array; draft 2020-12 output can break validation).
1020
+ * - Otherwise LangChain `toJsonSchema` (Standard Schema + JSON passthrough); if the result is still Zod
1021
+ * (duplicate `zod` copies), fall back to Zod `toJSONSchema` with draft-07.
1022
+ * - Strip root `$schema` for OpenAI; normalize invalid `required` keywords for cfworker; ensure `properties`.
1023
+ */
1024
+ normalizeToolInputSchemaForOpenAiDynamicStructuredTool(toolName, inputSchema) {
1025
+ const draft07Params = { target: "draft-07" };
1026
+ let converted;
1027
+ if (isInteropZodSchema(inputSchema)) converted = toJSONSchema(inputSchema, draft07Params);
1028
+ else {
1029
+ converted = toJsonSchema(inputSchema);
1030
+ if (isInteropZodSchema(converted)) converted = toJSONSchema(inputSchema, draft07Params);
1031
+ }
1032
+ const { $schema: _draftSchemaOmitted,...rest } = converted;
1033
+ if (rest.type !== "object") throw new Error(`Cannot create LangChain tool "${toolName}": tool input schema must be a JSON Schema object type (got type=${String(rest.type)}).`);
1034
+ if (rest.properties !== void 0 && (typeof rest.properties !== "object" || Array.isArray(rest.properties))) throw new Error(`Cannot create LangChain tool "${toolName}": tool input schema "properties" must be an object (got ${JSON.stringify(rest.properties)}).`);
1035
+ if (rest.properties === void 0) rest.properties = {};
1036
+ this.sanitizeJsonSchemaRequiredKeywordsForCfworker(rest);
1037
+ return rest;
1038
+ }
1039
+ /**
1040
+ * `@cfworker/json-schema` iterates `schema.required` with `for...of`; it must be a string array or absent.
1041
+ */
1042
+ sanitizeJsonSchemaRequiredKeywordsForCfworker(node$1) {
1043
+ if (!node$1 || typeof node$1 !== "object" || Array.isArray(node$1)) return;
1044
+ const o = node$1;
1045
+ const req = o.required;
1046
+ if (req !== void 0 && !Array.isArray(req)) delete o.required;
1047
+ else if (Array.isArray(req)) {
1048
+ const strings = req.filter((x) => typeof x === "string");
1049
+ if (strings.length === 0) delete o.required;
1050
+ else if (strings.length !== req.length) o.required = strings;
1051
+ }
1052
+ const props = o.properties;
1053
+ if (props && typeof props === "object" && !Array.isArray(props)) for (const v of Object.values(props)) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(v);
1054
+ for (const key of [
1055
+ "allOf",
1056
+ "anyOf",
1057
+ "oneOf"
1058
+ ]) {
1059
+ const branch = o[key];
1060
+ if (Array.isArray(branch)) for (const sub of branch) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(sub);
1061
+ }
1062
+ if (o.if) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(o.if);
1063
+ if (o.then) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(o.then);
1064
+ if (o.else) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(o.else);
1065
+ if (o.not) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(o.not);
1066
+ if (o.items) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(o.items);
1067
+ if (Array.isArray(o.prefixItems)) for (const sub of o.prefixItems) this.sanitizeJsonSchemaRequiredKeywordsForCfworker(sub);
1068
+ }
197
1069
  };
198
1070
  AIAgentExecutionHelpersFactory = __decorate([injectable()], AIAgentExecutionHelpersFactory);
199
1071
 
@@ -546,18 +1418,31 @@ let AIAgentNode = class AIAgentNode$1 {
546
1418
  }));
547
1419
  }
548
1420
  resolveToolRuntime(config) {
549
- if (config instanceof NodeBackedToolConfig) return {
550
- defaultDescription: `Run workflow node "${config.node.name ?? config.name}" as an AI tool.`,
551
- inputSchema: config.getInputSchema(),
552
- execute: async (args) => await this.nodeBackedToolRuntime.execute(config, args)
553
- };
1421
+ if (this.isNodeBackedToolConfig(config)) {
1422
+ const inputSchema = config.getInputSchema();
1423
+ if (inputSchema == null) throw new Error(`AIAgent tool "${config.name}": node-backed tool is missing inputSchema (cannot build LangChain tool).`);
1424
+ return {
1425
+ defaultDescription: `Run workflow node "${config.node.name ?? config.name}" as an AI tool.`,
1426
+ inputSchema,
1427
+ execute: async (args) => await this.nodeBackedToolRuntime.execute(config, args)
1428
+ };
1429
+ }
554
1430
  const tool = this.nodeResolver.resolve(config.type);
1431
+ if (tool.inputSchema == null) throw new Error(`AIAgent tool "${config.name}": plugin tool "${String(config.type)}" is missing inputSchema.`);
555
1432
  return {
556
1433
  defaultDescription: tool.defaultDescription,
557
1434
  inputSchema: tool.inputSchema,
558
1435
  execute: async (args) => await Promise.resolve(tool.execute(args))
559
1436
  };
560
1437
  }
1438
+ /**
1439
+ * Consumer apps can resolve two copies of `@codemation/core`, breaking `instanceof NodeBackedToolConfig` and
1440
+ * sending node-backed tools down the plugin-tool branch with `inputSchema: undefined` (LangChain then crashes in
1441
+ * json-schema validation). {@link NodeBackedToolConfig#toolKind} is stable across copies.
1442
+ */
1443
+ isNodeBackedToolConfig(config) {
1444
+ return config instanceof NodeBackedToolConfig || typeof config === "object" && config !== null && config.toolKind === "nodeBacked";
1445
+ }
561
1446
  resolveGuardrails(guardrails) {
562
1447
  const maxTurns = guardrails?.maxTurns ?? AgentGuardrailDefaults.maxTurns;
563
1448
  if (!Number.isInteger(maxTurns) || maxTurns < 1) throw new Error(`AIAgent maxTurns must be a positive integer. Received: ${String(maxTurns)}`);
@@ -1361,60 +2246,66 @@ var AIAgentConnectionWorkflowExpander = class {
1361
2246
  this.connectionCredentialNodeConfigFactory = connectionCredentialNodeConfigFactory;
1362
2247
  }
1363
2248
  expand(workflow$1) {
1364
- const existingByParentAndName = /* @__PURE__ */ new Map();
1365
- for (const c of workflow$1.connections ?? []) existingByParentAndName.set(`${c.parentNodeId}\0${c.connectionName}`, c);
2249
+ const existingChildIds = this.collectExistingChildIds(workflow$1);
2250
+ const connectionsByParentAndName = this.createConnectionsByParentAndName(workflow$1);
1366
2251
  const extraNodes = [];
1367
- const extraConnections = [];
2252
+ let connectionsChanged = false;
1368
2253
  for (const node$1 of workflow$1.nodes) {
1369
2254
  if (node$1.type !== AIAgentNode || !AgentConfigInspector.isAgentNodeConfig(node$1.config)) continue;
1370
- const agentId = node$1.id;
1371
- const agentConfig = node$1.config;
1372
- if (!existingByParentAndName.has(`${agentId}\0llm`)) {
1373
- const llmId = ConnectionNodeIdFactory.languageModelConnectionNodeId(agentId);
1374
- this.assertNoIdCollision(workflow$1, extraNodes, llmId);
1375
- extraNodes.push({
1376
- id: llmId,
1377
- kind: "node",
1378
- type: ConnectionCredentialNode,
1379
- name: agentConfig.chatModel.presentation?.label ?? agentConfig.chatModel.name,
1380
- config: this.connectionCredentialNodeConfigFactory.create(agentConfig.chatModel.name, agentConfig.chatModel)
1381
- });
1382
- extraConnections.push({
1383
- parentNodeId: agentId,
1384
- connectionName: "llm",
1385
- childNodeIds: [llmId]
1386
- });
1387
- }
1388
- if (!existingByParentAndName.has(`${agentId}\0tools`) && (agentConfig.tools?.length ?? 0) > 0) {
1389
- const toolIds = [];
1390
- for (const tool of agentConfig.tools ?? []) {
1391
- const toolId = ConnectionNodeIdFactory.toolConnectionNodeId(agentId, tool.name);
1392
- this.assertNoIdCollision(workflow$1, extraNodes, toolId);
1393
- toolIds.push(toolId);
2255
+ for (const connectionNode of AgentConnectionNodeCollector.collect(node$1.id, node$1.config)) {
2256
+ if (!existingChildIds.has(connectionNode.nodeId)) {
2257
+ this.assertNoIdCollision(workflow$1, extraNodes, existingChildIds, connectionNode.nodeId);
1394
2258
  extraNodes.push({
1395
- id: toolId,
2259
+ id: connectionNode.nodeId,
1396
2260
  kind: "node",
1397
2261
  type: ConnectionCredentialNode,
1398
- name: tool.presentation?.label ?? tool.name,
1399
- config: this.connectionCredentialNodeConfigFactory.create(tool.name, tool)
2262
+ name: connectionNode.name,
2263
+ config: this.connectionCredentialNodeConfigFactory.create(connectionNode.typeName, connectionNode.credentialSource)
1400
2264
  });
1401
2265
  }
1402
- extraConnections.push({
1403
- parentNodeId: agentId,
1404
- connectionName: "tools",
1405
- childNodeIds: toolIds
1406
- });
2266
+ const connectionKey = this.connectionKey(connectionNode.parentNodeId, connectionNode.connectionName);
2267
+ const existingConnection = connectionsByParentAndName.get(connectionKey);
2268
+ if (!existingConnection) {
2269
+ connectionsByParentAndName.set(connectionKey, {
2270
+ parentNodeId: connectionNode.parentNodeId,
2271
+ connectionName: connectionNode.connectionName,
2272
+ childNodeIds: [connectionNode.nodeId]
2273
+ });
2274
+ connectionsChanged = true;
2275
+ continue;
2276
+ }
2277
+ if (!existingConnection.childNodeIds.includes(connectionNode.nodeId)) {
2278
+ connectionsByParentAndName.set(connectionKey, {
2279
+ ...existingConnection,
2280
+ childNodeIds: [...existingConnection.childNodeIds, connectionNode.nodeId]
2281
+ });
2282
+ connectionsChanged = true;
2283
+ }
1407
2284
  }
1408
2285
  }
1409
- if (extraNodes.length === 0) return workflow$1;
2286
+ if (extraNodes.length === 0 && !connectionsChanged) return workflow$1;
1410
2287
  return {
1411
2288
  ...workflow$1,
1412
2289
  nodes: [...workflow$1.nodes, ...extraNodes],
1413
- connections: [...workflow$1.connections ?? [], ...extraConnections]
2290
+ connections: [...connectionsByParentAndName.values()]
1414
2291
  };
1415
2292
  }
1416
- assertNoIdCollision(workflow$1, pending, id) {
1417
- if (workflow$1.nodes.some((n) => n.id === id) || pending.some((n) => n.id === id)) throw new Error(`AIAgent connection expansion: node id "${id}" already exists. Rename the conflicting node or adjust the workflow.`);
2293
+ createConnectionsByParentAndName(workflow$1) {
2294
+ const existingByParentAndName = /* @__PURE__ */ new Map();
2295
+ for (const connection of workflow$1.connections ?? []) existingByParentAndName.set(this.connectionKey(connection.parentNodeId, connection.connectionName), connection);
2296
+ return existingByParentAndName;
2297
+ }
2298
+ collectExistingChildIds(workflow$1) {
2299
+ const ids = /* @__PURE__ */ new Set();
2300
+ for (const connection of workflow$1.connections ?? []) for (const childId of connection.childNodeIds) ids.add(childId);
2301
+ return ids;
2302
+ }
2303
+ connectionKey(parentNodeId, connectionName) {
2304
+ return `${parentNodeId}\0${connectionName}`;
2305
+ }
2306
+ assertNoIdCollision(workflow$1, pending, existingChildIds, id) {
2307
+ if (pending.some((n) => n.id === id)) throw new Error(`AIAgent connection expansion: node id "${id}" already exists. Rename the conflicting node or adjust the workflow.`);
2308
+ if (workflow$1.nodes.some((n) => n.id === id) && !existingChildIds.has(id)) throw new Error(`AIAgent connection expansion: node id "${id}" already exists. Rename the conflicting node or adjust the workflow.`);
1418
2309
  }
1419
2310
  };
1420
2311