@codemation/core-nodes 0.0.19 → 0.0.22

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