@adhd/apigen-engine-runtime 0.2.4 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs CHANGED
@@ -1,2058 +1,1921 @@
1
1
  import "node:fs";
2
- import { createRequire as he } from "node:module";
2
+ import { createRequire as e } from "node:module";
3
3
  import "node:path";
4
- import N from "pino";
5
- import _e from "ajv";
6
- import ke from "ajv-formats";
7
- const M = "$apigen";
8
- let Z = class extends Error {
9
- constructor(t) {
10
- super(t), this.code = "E_DUP_CODEC", this.name = "CodecRegistryError";
11
- }
4
+ import t from "pino";
5
+ import n from "ajv";
6
+ import r from "ajv-formats";
7
+ //#region ../apigen-base-logical/src/lib/contracts.ts
8
+ var i = "$apigen", a = class extends Error {
9
+ constructor(e) {
10
+ super(e), this.code = "E_DUP_CODEC", this.name = "CodecRegistryError";
11
+ }
12
12
  };
13
- function Ie(e) {
14
- const t = /* @__PURE__ */ new Map();
15
- return {
16
- register(n, r) {
17
- if (t.has(n.id) && !(r != null && r.override))
18
- throw new Z(
19
- `E_DUP_CODEC: a codec is already registered for id "${n.id}"`
20
- );
21
- t.set(n.id, n);
22
- },
23
- resolve(n) {
24
- for (const r of t.values())
25
- if (r.matches(n))
26
- return r;
27
- },
28
- get(n) {
29
- return t.get(n);
30
- },
31
- ids() {
32
- return [...t.keys()];
33
- },
34
- freeze() {
35
- const n = new Map(t), r = {
36
- register() {
37
- throw new Z(
38
- "E_DUP_CODEC: registry is frozen and cannot accept new codecs"
39
- );
40
- },
41
- resolve(o) {
42
- for (const i of n.values())
43
- if (i.matches(o))
44
- return i;
45
- },
46
- get(o) {
47
- return n.get(o);
48
- },
49
- ids() {
50
- return [...n.keys()];
51
- },
52
- freeze() {
53
- return r;
54
- }
55
- };
56
- return r;
57
- }
58
- };
59
- }
60
- const F = "x-apigen-logical", V = "x-apigen-codec", T = "x-apigen-ctor", P = "x-apigen-tojson", Ce = "0.1.0";
61
- function Q(e, t) {
62
- return {
63
- registry: e,
64
- resolve: (n) => {
65
- throw new Error(
66
- `[apigen-logical] $ref "${n}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`
67
- );
68
- },
69
- seen: /* @__PURE__ */ new WeakSet(),
70
- path: "",
71
- mode: "strict",
72
- ...t
73
- };
13
+ function o(e) {
14
+ e?.wellKnown;
15
+ let t = /* @__PURE__ */ new Map();
16
+ return {
17
+ register(e, n) {
18
+ if (t.has(e.id) && !n?.override) throw new a(`E_DUP_CODEC: a codec is already registered for id "${e.id}"`);
19
+ t.set(e.id, e);
20
+ },
21
+ resolve(e) {
22
+ for (let n of t.values()) if (n.matches(e)) return n;
23
+ },
24
+ get(e) {
25
+ return t.get(e);
26
+ },
27
+ ids() {
28
+ return [...t.keys()];
29
+ },
30
+ freeze() {
31
+ let e = new Map(t), n = {
32
+ register() {
33
+ throw new a("E_DUP_CODEC: registry is frozen and cannot accept new codecs");
34
+ },
35
+ resolve(t) {
36
+ for (let n of e.values()) if (n.matches(t)) return n;
37
+ },
38
+ get(t) {
39
+ return e.get(t);
40
+ },
41
+ ids() {
42
+ return [...e.keys()];
43
+ },
44
+ freeze() {
45
+ return n;
46
+ }
47
+ };
48
+ return n;
49
+ }
50
+ };
51
+ }
52
+ //#endregion
53
+ //#region ../apigen-base-logical/src/lib/descriptor-ext.ts
54
+ var s = "x-apigen-logical", c = "x-apigen-codec", l = "x-apigen-ctor", u = "x-apigen-tojson", d = "0.1.0";
55
+ //#endregion
56
+ //#region ../apigen-base-logical/src/lib/runmode.ts
57
+ function f(e, t) {
58
+ return {
59
+ registry: e,
60
+ resolve: (e) => {
61
+ throw Error(`[apigen-logical] $ref "${e}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`);
62
+ },
63
+ seen: /* @__PURE__ */ new WeakSet(),
64
+ path: "",
65
+ mode: "strict",
66
+ ...t
67
+ };
68
+ }
69
+ function p(e, t, n) {
70
+ let r = n.registry.resolve(t);
71
+ if (r) return r.encode(e, t, n);
72
+ let i = t.$ref;
73
+ if (typeof i == "string") return p(e, n.resolve(i), n);
74
+ let a = t.oneOf;
75
+ if (Array.isArray(a)) return p(e, ee(e, a, t, n), n);
76
+ let o = t.type;
77
+ if (o === "array") {
78
+ if (!Array.isArray(e)) return v(e, t, n);
79
+ let r = t.items;
80
+ if (!r) return e.map((e) => b(e));
81
+ let i = n.path;
82
+ return Array.isArray(r) ? e.map((e, t) => {
83
+ let a = r[t];
84
+ return a === void 0 ? b(e) : p(e, a, {
85
+ ...n,
86
+ path: `${i}/${t}`
87
+ });
88
+ }) : e.map((e, t) => p(e, r, {
89
+ ...n,
90
+ path: `${i}/${t}`
91
+ }));
92
+ }
93
+ if (o === "object") {
94
+ if (typeof e != "object" || !e || Array.isArray(e)) return v(e, t, n);
95
+ let r = t.properties;
96
+ if (!r) return b(e);
97
+ let i = n.path, a = {};
98
+ for (let [t, o] of Object.entries(r)) {
99
+ let r = e[t];
100
+ r !== void 0 && (a[t] = p(r, o, {
101
+ ...n,
102
+ path: `${i}/${t}`
103
+ }));
104
+ }
105
+ return a;
106
+ }
107
+ return o == null ? v(e, t, n) : b(e);
108
+ }
109
+ function m(e, t, n) {
110
+ let r = n.registry.resolve(t);
111
+ if (r) return r.decode(e, t, n);
112
+ let i = t.$ref;
113
+ if (typeof i == "string") return m(e, n.resolve(i), n);
114
+ let a = t.oneOf;
115
+ if (Array.isArray(a)) return m(e, ee(e, a, t, n), n);
116
+ let o = t.type;
117
+ if (o === "array") {
118
+ if (!Array.isArray(e)) return e;
119
+ let r = t.items;
120
+ if (!r) return e;
121
+ let i = n.path;
122
+ return Array.isArray(r) ? e.map((e, t) => {
123
+ let a = r[t];
124
+ return a === void 0 ? e : m(e, a, {
125
+ ...n,
126
+ path: `${i}/${t}`
127
+ });
128
+ }) : e.map((e, t) => m(e, r, {
129
+ ...n,
130
+ path: `${i}/${t}`
131
+ }));
132
+ }
133
+ if (o === "object") {
134
+ if (typeof e != "object" || !e || Array.isArray(e)) return e;
135
+ let r = t.properties;
136
+ if (!r) return e;
137
+ let i = n.path, a = {};
138
+ for (let [t, o] of Object.entries(r)) {
139
+ let r = e[t];
140
+ r !== void 0 && (a[t] = m(r, o, {
141
+ ...n,
142
+ path: `${i}/${t}`
143
+ }));
144
+ }
145
+ for (let [t, n] of Object.entries(e)) !(t in r) && n !== void 0 && (a[t] = n);
146
+ return a;
147
+ }
148
+ return o == null ? y(e, n) : e;
149
+ }
150
+ function ee(e, t, n, r) {
151
+ let i = n.discriminator;
152
+ if (i?.propertyName) {
153
+ let n = i.propertyName, a = typeof e == "object" && e ? e[n] : void 0;
154
+ if (typeof a == "string") {
155
+ if (i.mapping) {
156
+ let e = i.mapping[a];
157
+ if (e) {
158
+ let n = t.find((t) => t.$ref === e);
159
+ if (n) return n;
160
+ }
161
+ }
162
+ for (let e of t) {
163
+ let t = h(e, r).properties?.[n];
164
+ if (!t) continue;
165
+ let i = t.enum;
166
+ if (t.const === a || Array.isArray(i) && i.includes(a)) return e;
167
+ }
168
+ }
169
+ }
170
+ let a = te(e, t, r);
171
+ if (a) return a;
172
+ let o, s = -1;
173
+ for (let n of t) {
174
+ let t = _(e, h(n, r), r);
175
+ t !== null && t > s && (s = t, o = n);
176
+ }
177
+ return o === void 0 ? t[0] ?? {} : o;
178
+ }
179
+ function h(e, t) {
180
+ let n = e.$ref;
181
+ return typeof n == "string" ? t.resolve(n) : e;
182
+ }
183
+ function g(e) {
184
+ if ("const" in e) return e.const;
185
+ let t = e.enum;
186
+ if (Array.isArray(t) && t.length === 1) return t[0];
74
187
  }
75
- function v(e, t, n) {
76
- const r = n.registry.resolve(t);
77
- if (r)
78
- return r.encode(e, t, n);
79
- const o = t.$ref;
80
- if (typeof o == "string") {
81
- const a = n.resolve(o);
82
- return v(e, a, n);
83
- }
84
- const i = t.oneOf;
85
- if (Array.isArray(i)) {
86
- const a = ge(e, i, t, n);
87
- return v(e, a, n);
88
- }
89
- const s = t.type;
90
- if (s === "array") {
91
- if (!Array.isArray(e))
92
- return A(e, t, n);
93
- const a = t.items;
94
- if (!a)
95
- return e.map((u) => w(u));
96
- const f = n.path;
97
- return Array.isArray(a) ? e.map((u, c) => {
98
- const l = a[c];
99
- return l === void 0 ? w(u) : v(u, l, { ...n, path: `${f}/${c}` });
100
- }) : e.map(
101
- (u, c) => v(u, a, { ...n, path: `${f}/${c}` })
102
- );
103
- }
104
- if (s === "object") {
105
- if (e === null || typeof e != "object" || Array.isArray(e))
106
- return A(e, t, n);
107
- const a = t.properties;
108
- if (!a)
109
- return w(e);
110
- const f = n.path, u = {};
111
- for (const [c, l] of Object.entries(a)) {
112
- const p = e[c];
113
- p !== void 0 && (u[c] = v(p, l, {
114
- ...n,
115
- path: `${f}/${c}`
116
- }));
117
- }
118
- return u;
119
- }
120
- return s == null ? A(e, t, n) : w(e);
121
- }
122
- function $(e, t, n) {
123
- const r = n.registry.resolve(t);
124
- if (r)
125
- return r.decode(e, t, n);
126
- const o = t.$ref;
127
- if (typeof o == "string") {
128
- const a = n.resolve(o);
129
- return $(e, a, n);
130
- }
131
- const i = t.oneOf;
132
- if (Array.isArray(i)) {
133
- const a = ge(e, i, t, n);
134
- return $(e, a, n);
135
- }
136
- const s = t.type;
137
- if (s === "array") {
138
- if (!Array.isArray(e))
139
- return e;
140
- const a = t.items;
141
- if (!a)
142
- return e;
143
- const f = n.path;
144
- return Array.isArray(a) ? e.map((u, c) => {
145
- const l = a[c];
146
- return l === void 0 ? u : $(u, l, { ...n, path: `${f}/${c}` });
147
- }) : e.map(
148
- (u, c) => $(u, a, { ...n, path: `${f}/${c}` })
149
- );
150
- }
151
- if (s === "object") {
152
- if (e === null || typeof e != "object" || Array.isArray(e))
153
- return e;
154
- const a = t.properties;
155
- if (!a)
156
- return e;
157
- const f = n.path, u = {};
158
- for (const [c, l] of Object.entries(a)) {
159
- const p = e[c];
160
- p !== void 0 && (u[c] = $(p, l, {
161
- ...n,
162
- path: `${f}/${c}`
163
- }));
164
- }
165
- for (const [c, l] of Object.entries(e))
166
- !(c in a) && l !== void 0 && (u[c] = l);
167
- return u;
168
- }
169
- return s == null ? D(e, n) : e;
170
- }
171
- function ge(e, t, n, r) {
172
- var o;
173
- const i = n.discriminator;
174
- if (i != null && i.propertyName) {
175
- const u = i.propertyName, c = e !== null && typeof e == "object" ? e[u] : void 0;
176
- if (typeof c == "string") {
177
- if (i.mapping) {
178
- const l = i.mapping[c];
179
- if (l) {
180
- const p = t.find((y) => y.$ref === l);
181
- if (p)
182
- return p;
183
- }
184
- }
185
- for (const l of t) {
186
- const p = (o = _(l, r).properties) == null ? void 0 : o[u];
187
- if (!p)
188
- continue;
189
- const y = p.enum;
190
- if (p.const === c || Array.isArray(y) && y.includes(c))
191
- return l;
192
- }
193
- }
194
- }
195
- const s = xe(e, t, r);
196
- if (s)
197
- return s;
198
- let a, f = -1;
199
- for (const u of t) {
200
- const c = _(u, r), l = we(e, c, r);
201
- l !== null && l > f && (f = l, a = u);
202
- }
203
- return a !== void 0 ? a : t[0] ?? {};
204
- }
205
- function _(e, t) {
206
- const n = e.$ref;
207
- return typeof n == "string" ? t.resolve(n) : e;
208
- }
209
- function X(e) {
210
- if ("const" in e)
211
- return e.const;
212
- const t = e.enum;
213
- if (Array.isArray(t) && t.length === 1)
214
- return t[0];
215
- }
216
- function xe(e, t, n) {
217
- if (e === null || typeof e != "object" || Array.isArray(e))
218
- return;
219
- const r = e, o = t.map((s) => _(s, n)), i = /* @__PURE__ */ new Set();
220
- for (const s of o) {
221
- const a = s.properties;
222
- if (a)
223
- for (const [f, u] of Object.entries(a))
224
- X(u) !== void 0 && i.add(f);
225
- }
226
- for (const s of i) {
227
- const a = o.map((p) => {
228
- const y = p.properties, d = y == null ? void 0 : y[s];
229
- return d ? X(d) : void 0;
230
- }), f = a.filter((p) => p !== void 0), u = new Set(f);
231
- if (f.length < 2 || u.size !== f.length)
232
- continue;
233
- const c = r[s];
234
- if (c === void 0)
235
- continue;
236
- const l = a.findIndex((p) => p === c);
237
- if (l !== -1)
238
- return t[l];
239
- }
240
- }
241
- function Se(e) {
242
- if (e === null)
243
- return "null";
244
- if (Array.isArray(e))
245
- return "array";
246
- const t = typeof e;
247
- return t === "number" ? Number.isInteger(e) ? "integer" : "number" : t;
248
- }
249
- function we(e, t, n) {
250
- const r = t.oneOf;
251
- if (Array.isArray(r)) {
252
- let c = null;
253
- for (const l of r) {
254
- const p = we(e, _(l, n), n);
255
- p !== null && (c === null || p > c) && (c = p);
256
- }
257
- return c;
258
- }
259
- const o = t.type, i = Se(e);
260
- if (typeof o == "string") {
261
- if (!(o === i || o === "number" && i === "integer"))
262
- return null;
263
- } else if (Array.isArray(o) && !o.some(
264
- (c) => c === i || c === "number" && i === "integer"
265
- ))
266
- return null;
267
- if (e === null || typeof e != "object" || Array.isArray(e))
268
- return typeof o == "string" ? 1 : 0;
269
- const s = e, a = t.required;
270
- let f = 0;
271
- if (Array.isArray(a)) {
272
- for (const c of a)
273
- if (typeof c == "string") {
274
- if (s[c] === void 0)
275
- return null;
276
- f += 2;
277
- }
278
- }
279
- const u = t.properties;
280
- if (u) {
281
- for (const c of Object.keys(u))
282
- s[c] !== void 0 && (f += 1);
283
- for (const c of Object.keys(s))
284
- u[c] === void 0 && (f -= 1);
285
- }
286
- return f;
287
- }
288
- function A(e, t, n) {
289
- var r;
290
- for (const o of n.registry.ids()) {
291
- const i = n.registry.get(o);
292
- if ((r = i == null ? void 0 : i.ownsValue) != null && r.call(i, e))
293
- return {
294
- [M]: o,
295
- v: i.encode(e, i.schema, n)
296
- };
297
- }
298
- if (Array.isArray(e))
299
- return e.map((o) => A(o, {}, n));
300
- if (be(e)) {
301
- const o = {};
302
- for (const [i, s] of Object.entries(e))
303
- s !== void 0 && (o[i] = A(s, {}, n));
304
- return o;
305
- }
306
- return w(e);
307
- }
308
- function be(e) {
309
- if (e === null || typeof e != "object" || Array.isArray(e))
310
- return !1;
311
- const t = Object.getPrototypeOf(e);
312
- return t === Object.prototype || t === null;
313
- }
314
- function D(e, t) {
315
- if (e !== null && typeof e == "object" && !Array.isArray(e) && M in e) {
316
- const n = e, r = n[M], o = t.registry.get(r);
317
- if (o)
318
- return o.decode(n.v, o.schema, t);
319
- }
320
- if (Array.isArray(e))
321
- return e.map((n) => D(n, t));
322
- if (be(e)) {
323
- const n = {};
324
- for (const [r, o] of Object.entries(e))
325
- n[r] = D(o, t);
326
- return n;
327
- }
328
- return e;
329
- }
330
- function w(e) {
331
- if (e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean")
332
- return e;
333
- if (Array.isArray(e))
334
- return e.map(w);
335
- if (typeof e == "object") {
336
- const t = {};
337
- for (const [n, r] of Object.entries(e))
338
- r !== void 0 && (t[n] = w(r));
339
- return t;
340
- }
341
- return null;
188
+ function te(e, t, n) {
189
+ if (typeof e != "object" || !e || Array.isArray(e)) return;
190
+ let r = e, i = t.map((e) => h(e, n)), a = /* @__PURE__ */ new Set();
191
+ for (let e of i) {
192
+ let t = e.properties;
193
+ if (t) for (let [e, n] of Object.entries(t)) g(n) !== void 0 && a.add(e);
194
+ }
195
+ for (let o of a) {
196
+ let a = i.map((e) => {
197
+ let t = e.properties?.[o];
198
+ return t ? g(t) : void 0;
199
+ }), s = a.filter((e) => e !== void 0), c = new Set(s);
200
+ if (s.length < 2 || c.size !== s.length) continue;
201
+ let l = r[o];
202
+ if (l === void 0) continue;
203
+ let u = a.findIndex((e) => e === l);
204
+ if (u !== -1 && _(e, i[u], n) !== null) return t[u];
205
+ }
206
+ }
207
+ function ne(e) {
208
+ if (e === null) return "null";
209
+ if (Array.isArray(e)) return "array";
210
+ let t = typeof e;
211
+ return t === "number" ? Number.isInteger(e) ? "integer" : "number" : t;
212
+ }
213
+ function _(e, t, n) {
214
+ let r = t.oneOf;
215
+ if (Array.isArray(r)) {
216
+ let t = null;
217
+ for (let i of r) {
218
+ let r = _(e, h(i, n), n);
219
+ r !== null && (t === null || r > t) && (t = r);
220
+ }
221
+ return t;
222
+ }
223
+ let i = t.type, a = ne(e);
224
+ if (typeof i == "string") {
225
+ if (!(i === a || i === "number" && a === "integer")) return null;
226
+ } else if (Array.isArray(i) && !i.some((e) => e === a || e === "number" && a === "integer")) return null;
227
+ if (typeof e != "object" || !e || Array.isArray(e)) return +(typeof i == "string");
228
+ let o = e, s = t.required, c = 0;
229
+ if (Array.isArray(s)) {
230
+ for (let e of s) if (typeof e == "string") {
231
+ if (o[e] === void 0) return null;
232
+ c += 2;
233
+ }
234
+ }
235
+ let l = t.properties;
236
+ if (l) {
237
+ for (let e of Object.keys(l)) o[e] !== void 0 && (c += 1);
238
+ for (let e of Object.keys(o)) l[e] === void 0 && --c;
239
+ }
240
+ return c;
342
241
  }
343
- function Me(e) {
344
- return {
345
- encode(t, n, r) {
346
- const o = Q(e, r);
347
- return v(t, n, o);
348
- },
349
- decode(t, n, r) {
350
- const o = Q(e, r);
351
- return $(t, n, o);
352
- }
353
- };
354
- }
355
- const H = {
356
- id: "date-time",
357
- kind: "scalar",
358
- schema: { type: "string", format: "date-time" },
359
- matches(e) {
360
- return e.type === "string" && e.format === "date-time";
361
- },
362
- ownsValue(e) {
363
- return e instanceof Date;
364
- },
365
- encode(e, t, n) {
366
- return e.toISOString();
367
- },
368
- decode(e, t, n) {
369
- if (typeof e != "string") {
370
- if (n.mode === "strict")
371
- throw new TypeError(
372
- `[date-time] expected a string on the wire at "${n.path}", got ${typeof e}`
373
- );
374
- return new Date(String(e));
375
- }
376
- if (n.mode === "strict" && Number.isNaN(new Date(e).getTime()))
377
- throw new TypeError(
378
- `[date-time] invalid date-time string at "${n.path}": ${JSON.stringify(e)}`
379
- );
380
- return new Date(e);
381
- }
382
- }, K = {
383
- id: "int64",
384
- kind: "scalar",
385
- schema: { type: "string", format: "int64" },
386
- matches(e) {
387
- return e.type === "string" && e.format === "int64";
388
- },
389
- ownsValue(e) {
390
- return typeof e == "bigint";
391
- },
392
- encode(e, t, n) {
393
- return String(e);
394
- },
395
- decode(e, t, n) {
396
- if (typeof e != "string") {
397
- if (n.mode === "strict")
398
- throw new TypeError(
399
- `[int64] expected a string on the wire at "${n.path}", got ${typeof e}`
400
- );
401
- try {
402
- return BigInt(Math.trunc(Number(e)));
403
- } catch {
404
- return BigInt(0);
405
- }
406
- }
407
- if (n.mode === "strict")
408
- return BigInt(e);
409
- if (/^-?\d+$/.test(e))
410
- try {
411
- return BigInt(e);
412
- } catch {
413
- return BigInt(0);
414
- }
415
- return BigInt(0);
416
- }
417
- }, z = {
418
- id: "decimal",
419
- kind: "scalar",
420
- schema: { type: "string", format: "decimal" },
421
- matches(e) {
422
- return e.type === "string" && e.format === "decimal";
423
- },
424
- encode(e, t, n) {
425
- return e;
426
- },
427
- decode(e, t, n) {
428
- if (typeof e != "string") {
429
- if (n.mode === "strict")
430
- throw new TypeError(
431
- `[decimal] expected a string on the wire at "${n.path}", got ${typeof e}`
432
- );
433
- return String(e);
434
- }
435
- if (n.mode === "strict" && !/^-?\d+(\.\d+)?$/.test(e))
436
- throw new TypeError(
437
- `[decimal] wire value "${e}" at "${n.path}" is not a valid decimal string`
438
- );
439
- return e;
440
- }
441
- }, J = {
442
- id: "byte",
443
- kind: "scalar",
444
- schema: { type: "string", format: "byte" },
445
- matches(e) {
446
- return e.type === "string" && e.format === "byte";
447
- },
448
- ownsValue(e) {
449
- return e instanceof Uint8Array;
450
- },
451
- encode(e, t, n) {
452
- return Buffer.from(e).toString("base64");
453
- },
454
- decode(e, t, n) {
455
- if (typeof e != "string") {
456
- if (n.mode === "strict")
457
- throw new TypeError(
458
- `[byte] expected a base64 string on the wire at "${n.path}", got ${typeof e}`
459
- );
460
- return new Uint8Array(0);
461
- }
462
- if (n.mode === "strict" && !/^[A-Za-z0-9+/]*={0,2}$/.test(e))
463
- throw new TypeError(
464
- `[byte] wire value at "${n.path}" is not standard base64 (format:byte): "${e}"`
465
- );
466
- return new Uint8Array(Buffer.from(e, "base64"));
467
- }
468
- }, Te = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, W = {
469
- id: "uuid",
470
- kind: "scalar",
471
- schema: { type: "string", format: "uuid" },
472
- matches(e) {
473
- return e.type === "string" && e.format === "uuid";
474
- },
475
- encode(e, t, n) {
476
- return e.toLowerCase();
477
- },
478
- decode(e, t, n) {
479
- if (typeof e != "string") {
480
- if (n.mode === "strict")
481
- throw new TypeError(
482
- `[uuid] expected a string on the wire at "${n.path}", got ${typeof e}`
483
- );
484
- return String(e).toLowerCase();
485
- }
486
- if (n.mode === "strict" && !Te.test(e))
487
- throw new TypeError(
488
- `[uuid] wire value "${e}" at "${n.path}" is not a lowercase-hyphenated RFC 4122 UUID`
489
- );
490
- return e;
491
- }
492
- }, U = "NaN", R = "Infinity", L = "-Infinity", Pe = /* @__PURE__ */ new Set([
493
- U,
494
- R,
495
- L
496
- ]), G = {
497
- id: "number-special",
498
- kind: "scalar",
499
- schema: { type: "number" },
500
- matches(e) {
501
- return e.type === "number" && e.format === void 0;
502
- },
503
- ownsValue(e) {
504
- return typeof e == "number" && !Number.isFinite(e);
505
- },
506
- encode(e, t, n) {
507
- return Number.isNaN(e) ? U : e === 1 / 0 ? R : e === -1 / 0 ? L : e;
508
- },
509
- decode(e, t, n) {
510
- if (typeof e == "number")
511
- return e;
512
- if (typeof e == "string") {
513
- if (e === U)
514
- return NaN;
515
- if (e === R)
516
- return 1 / 0;
517
- if (e === L)
518
- return -1 / 0;
519
- const r = Number(e);
520
- if (!Number.isNaN(r))
521
- return r;
522
- }
523
- if (n.mode === "strict")
524
- throw new TypeError(
525
- `[number-special] unrecognized wire value at "${n.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...Pe].join(", ")}.`
526
- );
527
- return NaN;
528
- }
529
- }, De = [
530
- H,
531
- K,
532
- z,
533
- J,
534
- W,
535
- G
242
+ function v(e, t, n) {
243
+ for (let t of n.registry.ids()) {
244
+ let r = n.registry.get(t);
245
+ if (r?.ownsValue?.(e)) return {
246
+ [i]: t,
247
+ v: r.encode(e, r.schema, n)
248
+ };
249
+ }
250
+ if (Array.isArray(e)) return e.map((e) => v(e, {}, n));
251
+ if (re(e)) {
252
+ let t = {};
253
+ for (let [r, i] of Object.entries(e)) i !== void 0 && (t[r] = v(i, {}, n));
254
+ return t;
255
+ }
256
+ return b(e);
257
+ }
258
+ function re(e) {
259
+ if (typeof e != "object" || !e || Array.isArray(e)) return !1;
260
+ let t = Object.getPrototypeOf(e);
261
+ return t === Object.prototype || t === null;
262
+ }
263
+ function y(e, t) {
264
+ if (typeof e == "object" && e && !Array.isArray(e) && "$apigen" in e) {
265
+ let n = e, r = n[i], a = t.registry.get(r);
266
+ if (a) return a.decode(n.v, a.schema, t);
267
+ }
268
+ if (Array.isArray(e)) return e.map((e) => y(e, t));
269
+ if (re(e)) {
270
+ let n = {};
271
+ for (let [r, i] of Object.entries(e)) n[r] = y(i, t);
272
+ return n;
273
+ }
274
+ return e;
275
+ }
276
+ function b(e) {
277
+ if (e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean") return e;
278
+ if (Array.isArray(e)) return e.map(b);
279
+ if (typeof e == "object") {
280
+ let t = {};
281
+ for (let [n, r] of Object.entries(e)) r !== void 0 && (t[n] = b(r));
282
+ return t;
283
+ }
284
+ return null;
285
+ }
286
+ function ie(e, t) {
287
+ let n = /* @__PURE__ */ new Set(), r = [e];
288
+ for (; r.length > 0;) {
289
+ let e = r.pop();
290
+ if (!e || n.has(e)) continue;
291
+ n.add(e);
292
+ let i = e.$ref;
293
+ if (typeof i == "string") {
294
+ if (t) {
295
+ let e = t[i];
296
+ if (!e) {
297
+ let e = Object.keys(t).join(", ") || "(none)";
298
+ throw Error(`[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${e}`);
299
+ }
300
+ r.push(e);
301
+ }
302
+ continue;
303
+ }
304
+ let a = e.oneOf;
305
+ if (Array.isArray(a)) for (let e of a) typeof e == "object" && e && r.push(e);
306
+ let o = e.properties;
307
+ if (o) for (let e of Object.values(o)) r.push(e);
308
+ let s = e.items;
309
+ if (Array.isArray(s)) for (let e of s) typeof e == "object" && e && r.push(e);
310
+ else typeof s == "object" && s && r.push(s);
311
+ let c = e.additionalProperties;
312
+ typeof c == "object" && c && r.push(c);
313
+ let l = e.propertyNames;
314
+ typeof l == "object" && l && r.push(l);
315
+ }
316
+ }
317
+ function ae(e) {
318
+ return {
319
+ encode(t, n, r) {
320
+ return p(t, n, f(e, r));
321
+ },
322
+ decode(t, n, r) {
323
+ return m(t, n, f(e, r));
324
+ }
325
+ };
326
+ }
327
+ //#endregion
328
+ //#region ../apigen-base-logical/src/lib/codecs/date-time.ts
329
+ var x = {
330
+ id: "date-time",
331
+ kind: "scalar",
332
+ schema: {
333
+ type: "string",
334
+ format: "date-time"
335
+ },
336
+ matches(e) {
337
+ return e.type === "string" && e.format === "date-time";
338
+ },
339
+ ownsValue(e) {
340
+ return e instanceof Date;
341
+ },
342
+ encode(e, t, n) {
343
+ return e.toISOString();
344
+ },
345
+ decode(e, t, n) {
346
+ if (typeof e != "string") {
347
+ if (n.mode === "strict") throw TypeError(`[date-time] expected a string on the wire at "${n.path}", got ${typeof e}`);
348
+ return new Date(String(e));
349
+ }
350
+ if (n.mode === "strict" && Number.isNaN(new Date(e).getTime())) throw TypeError(`[date-time] invalid date-time string at "${n.path}": ${JSON.stringify(e)}`);
351
+ return new Date(e);
352
+ }
353
+ }, S = {
354
+ id: "int64",
355
+ kind: "scalar",
356
+ schema: {
357
+ type: "string",
358
+ format: "int64"
359
+ },
360
+ matches(e) {
361
+ return e.type === "string" && e.format === "int64";
362
+ },
363
+ ownsValue(e) {
364
+ return typeof e == "bigint";
365
+ },
366
+ encode(e, t, n) {
367
+ return String(e);
368
+ },
369
+ decode(e, t, n) {
370
+ if (typeof e != "string") {
371
+ if (n.mode === "strict") throw TypeError(`[int64] expected a string on the wire at "${n.path}", got ${typeof e}`);
372
+ try {
373
+ return BigInt(Math.trunc(Number(e)));
374
+ } catch {
375
+ return BigInt(0);
376
+ }
377
+ }
378
+ if (n.mode === "strict") return BigInt(e);
379
+ if (/^-?\d+$/.test(e)) try {
380
+ return BigInt(e);
381
+ } catch {
382
+ return BigInt(0);
383
+ }
384
+ return BigInt(0);
385
+ }
386
+ };
387
+ //#endregion
388
+ //#region ../apigen-base-logical/src/lib/codecs/decimal.ts
389
+ function C(e) {
390
+ return e;
391
+ }
392
+ var w = {
393
+ id: "decimal",
394
+ kind: "scalar",
395
+ schema: {
396
+ type: "string",
397
+ format: "decimal"
398
+ },
399
+ matches(e) {
400
+ return e.type === "string" && e.format === "decimal";
401
+ },
402
+ encode(e, t, n) {
403
+ return e;
404
+ },
405
+ decode(e, t, n) {
406
+ if (typeof e != "string") {
407
+ if (n.mode === "strict") throw TypeError(`[decimal] expected a string on the wire at "${n.path}", got ${typeof e}`);
408
+ return C(String(e));
409
+ }
410
+ if (n.mode === "strict" && !/^-?\d+(\.\d+)?$/.test(e)) throw TypeError(`[decimal] wire value "${e}" at "${n.path}" is not a valid decimal string`);
411
+ return C(e);
412
+ }
413
+ }, T = {
414
+ id: "byte",
415
+ kind: "scalar",
416
+ schema: {
417
+ type: "string",
418
+ format: "byte"
419
+ },
420
+ matches(e) {
421
+ return e.type === "string" && e.format === "byte";
422
+ },
423
+ ownsValue(e) {
424
+ return e instanceof Uint8Array;
425
+ },
426
+ encode(e, t, n) {
427
+ return Buffer.from(e).toString("base64");
428
+ },
429
+ decode(e, t, n) {
430
+ if (typeof e != "string") {
431
+ if (n.mode === "strict") throw TypeError(`[byte] expected a base64 string on the wire at "${n.path}", got ${typeof e}`);
432
+ return /* @__PURE__ */ new Uint8Array();
433
+ }
434
+ if (n.mode === "strict" && !/^[A-Za-z0-9+/]*={0,2}$/.test(e)) throw TypeError(`[byte] wire value at "${n.path}" is not standard base64 (format:byte): "${e}"`);
435
+ return new Uint8Array(Buffer.from(e, "base64"));
436
+ }
437
+ }, oe = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, E = {
438
+ id: "uuid",
439
+ kind: "scalar",
440
+ schema: {
441
+ type: "string",
442
+ format: "uuid"
443
+ },
444
+ matches(e) {
445
+ return e.type === "string" && e.format === "uuid";
446
+ },
447
+ encode(e, t, n) {
448
+ return e.toLowerCase();
449
+ },
450
+ decode(e, t, n) {
451
+ if (typeof e != "string") {
452
+ if (n.mode === "strict") throw TypeError(`[uuid] expected a string on the wire at "${n.path}", got ${typeof e}`);
453
+ return String(e).toLowerCase();
454
+ }
455
+ if (n.mode === "strict" && !oe.test(e)) throw TypeError(`[uuid] wire value "${e}" at "${n.path}" is not a lowercase-hyphenated RFC 4122 UUID`);
456
+ return e;
457
+ }
458
+ }, D = "NaN", O = "Infinity", k = "-Infinity", se = /* @__PURE__ */ new Set([
459
+ D,
460
+ O,
461
+ k
462
+ ]), A = {
463
+ id: "number-special",
464
+ kind: "scalar",
465
+ schema: { type: "number" },
466
+ matches(e) {
467
+ return e.type === "number" && e.format === void 0;
468
+ },
469
+ ownsValue(e) {
470
+ return typeof e == "number" && !Number.isFinite(e);
471
+ },
472
+ encode(e, t, n) {
473
+ return Number.isNaN(e) ? D : e === Infinity ? O : e === -Infinity ? k : e;
474
+ },
475
+ decode(e, t, n) {
476
+ if (typeof e == "number") return e;
477
+ if (typeof e == "string") {
478
+ if (e === D) return NaN;
479
+ if (e === O) return Infinity;
480
+ if (e === k) return -Infinity;
481
+ let t = Number(e);
482
+ if (!Number.isNaN(t)) return t;
483
+ }
484
+ if (n.mode === "strict") throw TypeError(`[number-special] unrecognized wire value at "${n.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...se].join(", ")}.`);
485
+ return NaN;
486
+ }
487
+ }, ce = [
488
+ x,
489
+ S,
490
+ w,
491
+ T,
492
+ E,
493
+ A
536
494
  ];
537
- function Ue(e, t = {}) {
538
- for (const n of De)
539
- e.register(n, { override: t.override ?? !1 });
540
- }
541
- const Re = 12;
542
- function Le(e, t) {
543
- const n = /^#\/(definitions|\$defs)\/(.+)$/.exec(e);
544
- if (!n)
545
- return;
546
- const [, r, o] = n, i = r === "$defs" ? t.$defs : t.definitions;
547
- return i == null ? void 0 : i[decodeURIComponent(o)];
548
- }
549
- function ee(e) {
550
- return Array.isArray(e) && e.length > 0 ? e[0] : void 0;
551
- }
552
- function Be(e) {
553
- if (Array.isArray(e.type))
554
- return e.type.find((t) => t !== "null") ?? e.type[0];
555
- if (typeof e.type == "string")
556
- return e.type;
557
- if (e.properties)
558
- return "object";
559
- }
560
- function qe(e) {
561
- switch (e.format) {
562
- case "date-time":
563
- return "1970-01-01T00:00:00.000Z";
564
- case "date":
565
- return "1970-01-01";
566
- case "time":
567
- return "00:00:00Z";
568
- case "uuid":
569
- return "00000000-0000-0000-0000-000000000000";
570
- case "email":
571
- return "user@example.com";
572
- case "uri":
573
- case "url":
574
- case "uri-reference":
575
- return "https://example.com";
576
- case "hostname":
577
- return "example.com";
578
- case "ipv4":
579
- return "127.0.0.1";
580
- case "ipv6":
581
- return "::1";
582
- case "decimal":
583
- return "0";
584
- case "int64":
585
- return "0";
586
- case "byte":
587
- return "";
588
- default:
589
- return "<string>";
590
- }
495
+ function le(e, t = {}) {
496
+ for (let n of ce) e.register(n, { override: t.override ?? !1 });
591
497
  }
592
- function te(e, t, n) {
593
- const r = e.properties ?? {}, o = e.required ?? [], i = {};
594
- for (const s of o) {
595
- const a = r[s];
596
- i[s] = a !== void 0 ? j(a, t, n + 1) : `<${s}>`;
597
- }
598
- return i;
498
+ //#endregion
499
+ //#region ../apigen-base-logical/src/lib/example.ts
500
+ var ue = 12;
501
+ function de(e, t) {
502
+ let n = /^#\/(definitions|\$defs)\/(.+)$/.exec(e);
503
+ if (!n) return;
504
+ let [, r, i] = n;
505
+ return (r === "$defs" ? t.$defs : t.definitions)?.[decodeURIComponent(i)];
599
506
  }
600
- function Fe(e, t, n) {
601
- const r = Array.isArray(e.items) ? e.items[0] : e.items;
602
- return r ? [j(r, t, n + 1)] : [];
507
+ function fe(e) {
508
+ return Array.isArray(e) && e.length > 0 ? e[0] : void 0;
509
+ }
510
+ function pe(e) {
511
+ if (Array.isArray(e.type)) return e.type.find((e) => e !== "null") ?? e.type[0];
512
+ if (typeof e.type == "string") return e.type;
513
+ if (e.properties) return "object";
514
+ }
515
+ function me(e) {
516
+ switch (e.format) {
517
+ case "date-time": return "1970-01-01T00:00:00.000Z";
518
+ case "date": return "1970-01-01";
519
+ case "time": return "00:00:00Z";
520
+ case "uuid": return "00000000-0000-0000-0000-000000000000";
521
+ case "email": return "user@example.com";
522
+ case "uri":
523
+ case "url":
524
+ case "uri-reference": return "https://example.com";
525
+ case "hostname": return "example.com";
526
+ case "ipv4": return "127.0.0.1";
527
+ case "ipv6": return "::1";
528
+ case "decimal": return "0";
529
+ case "int64": return "0";
530
+ case "byte": return "";
531
+ default: return "<string>";
532
+ }
533
+ }
534
+ function he(e, t, n) {
535
+ let r = e.properties ?? {}, i = e.required ?? [], a = {};
536
+ for (let e of i) {
537
+ let i = r[e];
538
+ a[e] = i === void 0 ? `<${e}>` : j(i, t, n + 1);
539
+ }
540
+ return a;
541
+ }
542
+ function ge(e, t, n) {
543
+ let r = Array.isArray(e.items) ? e.items[0] : e.items;
544
+ return r ? [j(r, t, n + 1)] : [];
603
545
  }
604
546
  function j(e, t = e ?? {}, n = 0) {
605
- if (!e || n > Re)
606
- return null;
607
- if (typeof e.$ref == "string") {
608
- const o = Le(e.$ref, t);
609
- return o !== void 0 ? j(o, t, n + 1) : null;
610
- }
611
- if (e.const !== void 0)
612
- return e.const;
613
- if (Array.isArray(e.enum) && e.enum.length > 0)
614
- return e.enum[0];
615
- if (Array.isArray(e.allOf) && e.allOf.length > 0) {
616
- const o = {}, i = [];
617
- for (const s of e.allOf) {
618
- Object.assign(o, s.properties ?? {});
619
- for (const a of s.required ?? [])
620
- i.includes(a) || i.push(a);
621
- }
622
- return te(
623
- { ...e, properties: o, required: i },
624
- t,
625
- n
626
- );
627
- }
628
- const r = ee(e.oneOf) ?? ee(e.anyOf);
629
- if (r)
630
- return j(r, t, n + 1);
631
- switch (Be(e)) {
632
- case "object":
633
- return te(e, t, n);
634
- case "array":
635
- return Fe(e, t, n);
636
- case "string":
637
- return qe(e);
638
- case "integer":
639
- case "number":
640
- return 0;
641
- case "boolean":
642
- return !1;
643
- case "null":
644
- return null;
645
- default:
646
- return null;
647
- }
648
- }
649
- function ve(e) {
650
- if (!e)
651
- return;
652
- const t = j(e);
653
- return `Example: ${JSON.stringify(t)}`;
654
- }
655
- H.id, // 'date-time'
656
- K.id, // 'int64'
657
- z.id, // 'decimal'
658
- J.id, // 'byte'
659
- W.id, // 'uuid'
660
- G.id;
661
- const $e = Ie();
662
- Ue($e);
663
- const Ae = Me($e.freeze());
664
- function Ve(e, t) {
665
- var r;
666
- const n = ((r = e.input) == null ? void 0 : r.properties) ?? {};
667
- return t in n;
668
- }
669
- function He(e) {
670
- var n, r;
671
- const t = (r = (n = e.input) == null ? void 0 : n.properties) == null ? void 0 : r.data;
672
- return Object.keys((t == null ? void 0 : t.properties) ?? {});
547
+ if (!e || n > ue) return null;
548
+ if (typeof e.$ref == "string") {
549
+ let r = de(e.$ref, t);
550
+ return r === void 0 ? null : j(r, t, n + 1);
551
+ }
552
+ if (e.const !== void 0) return e.const;
553
+ if (Array.isArray(e.enum) && e.enum.length > 0) return e.enum[0];
554
+ if (Array.isArray(e.allOf) && e.allOf.length > 0) {
555
+ let r = {}, i = [];
556
+ for (let t of e.allOf) {
557
+ Object.assign(r, t.properties ?? {});
558
+ for (let e of t.required ?? []) i.includes(e) || i.push(e);
559
+ }
560
+ return he({
561
+ ...e,
562
+ properties: r,
563
+ required: i
564
+ }, t, n);
565
+ }
566
+ let r = fe(e.oneOf) ?? fe(e.anyOf);
567
+ if (r) return j(r, t, n + 1);
568
+ switch (pe(e)) {
569
+ case "object": return he(e, t, n);
570
+ case "array": return ge(e, t, n);
571
+ case "string": return me(e);
572
+ case "integer":
573
+ case "number": return 0;
574
+ case "boolean": return !1;
575
+ case "null": return null;
576
+ default: return null;
577
+ }
578
+ }
579
+ function _e(e) {
580
+ if (!e) return;
581
+ let t = j(e);
582
+ return `Example: ${JSON.stringify(t)}`;
583
+ }
584
+ x.id, S.id, w.id, T.id, E.id, A.id, Object.freeze({
585
+ typescript: {
586
+ "date-time": {
587
+ encode: "$.toISOString()",
588
+ decode: "new Date($)",
589
+ mode: "native"
590
+ },
591
+ int64: {
592
+ encode: "String($)",
593
+ decode: "BigInt($)",
594
+ mode: "native"
595
+ },
596
+ decimal: {
597
+ encode: "$.toString()",
598
+ decode: "new Decimal($)",
599
+ imports: [],
600
+ dep: {
601
+ name: "decimal.js",
602
+ version: "^10"
603
+ },
604
+ mode: "branded"
605
+ },
606
+ byte: {
607
+ encode: "Buffer.from($).toString('base64')",
608
+ decode: "new Uint8Array(Buffer.from($, 'base64'))",
609
+ mode: "native"
610
+ },
611
+ uuid: {
612
+ encode: "$.toLowerCase()",
613
+ decode: "$",
614
+ mode: "native"
615
+ },
616
+ "number-special": {
617
+ encode: "numToWire($)",
618
+ decode: "wireToNum($)",
619
+ mode: "native"
620
+ }
621
+ },
622
+ python: {
623
+ "date-time": {
624
+ encode: "$.isoformat()",
625
+ decode: "datetime.fromisoformat($)",
626
+ imports: ["from datetime import datetime"],
627
+ mode: "native"
628
+ },
629
+ int64: {
630
+ encode: "str($)",
631
+ decode: "int($)",
632
+ mode: "native"
633
+ },
634
+ decimal: {
635
+ encode: "str($)",
636
+ decode: "Decimal($)",
637
+ imports: ["from decimal import Decimal"],
638
+ mode: "native"
639
+ },
640
+ byte: {
641
+ encode: "b64encode($).decode()",
642
+ decode: "b64decode($)",
643
+ imports: ["from base64 import b64encode, b64decode"],
644
+ mode: "native"
645
+ },
646
+ uuid: {
647
+ encode: "str($)",
648
+ decode: "UUID($)",
649
+ imports: ["from uuid import UUID"],
650
+ mode: "native"
651
+ },
652
+ "number-special": {
653
+ encode: "num_to_wire($)",
654
+ decode: "wire_to_num($)",
655
+ mode: "native"
656
+ }
657
+ },
658
+ rust: {
659
+ "date-time": {
660
+ encode: "__SCAFFOLD_RUST_DATETIME_ENCODE__",
661
+ decode: "__SCAFFOLD_RUST_DATETIME_DECODE__",
662
+ imports: ["use chrono::{DateTime, Utc};"],
663
+ dep: {
664
+ name: "chrono",
665
+ version: "^0.4"
666
+ },
667
+ mode: "lib"
668
+ },
669
+ int64: {
670
+ encode: "__SCAFFOLD_RUST_INT64_ENCODE__",
671
+ decode: "__SCAFFOLD_RUST_INT64_DECODE__",
672
+ imports: ["use serde_with::DisplayFromStr;"],
673
+ dep: {
674
+ name: "serde_with",
675
+ version: "^3"
676
+ },
677
+ mode: "lib"
678
+ },
679
+ decimal: {
680
+ encode: "__SCAFFOLD_RUST_DECIMAL_ENCODE__",
681
+ decode: "__SCAFFOLD_RUST_DECIMAL_DECODE__",
682
+ imports: ["use rust_decimal::Decimal;"],
683
+ dep: {
684
+ name: "rust_decimal",
685
+ version: "^1"
686
+ },
687
+ mode: "lib"
688
+ },
689
+ byte: {
690
+ encode: "__SCAFFOLD_RUST_BYTE_ENCODE__",
691
+ decode: "__SCAFFOLD_RUST_BYTE_DECODE__",
692
+ imports: ["use serde_with::base64::Base64;"],
693
+ dep: {
694
+ name: "serde_with",
695
+ version: "^3"
696
+ },
697
+ mode: "lib"
698
+ },
699
+ uuid: {
700
+ encode: "__SCAFFOLD_RUST_UUID_ENCODE__",
701
+ decode: "__SCAFFOLD_RUST_UUID_DECODE__",
702
+ imports: ["use uuid::Uuid;"],
703
+ dep: {
704
+ name: "uuid",
705
+ version: "^1"
706
+ },
707
+ mode: "lib"
708
+ },
709
+ "number-special": {
710
+ encode: "__SCAFFOLD_RUST_NUMSPECIAL_ENCODE__",
711
+ decode: "__SCAFFOLD_RUST_NUMSPECIAL_DECODE__",
712
+ mode: "native"
713
+ }
714
+ },
715
+ go: {
716
+ "date-time": {
717
+ encode: "__SCAFFOLD_GO_DATETIME_ENCODE__",
718
+ decode: "__SCAFFOLD_GO_DATETIME_DECODE__",
719
+ imports: ["\"time\""],
720
+ mode: "native"
721
+ },
722
+ int64: {
723
+ encode: "__SCAFFOLD_GO_INT64_ENCODE__",
724
+ decode: "__SCAFFOLD_GO_INT64_DECODE__",
725
+ imports: ["\"strconv\""],
726
+ mode: "native"
727
+ },
728
+ decimal: {
729
+ encode: "__SCAFFOLD_GO_DECIMAL_ENCODE__",
730
+ decode: "__SCAFFOLD_GO_DECIMAL_DECODE__",
731
+ imports: ["\"github.com/shopspring/decimal\""],
732
+ dep: {
733
+ name: "github.com/shopspring/decimal",
734
+ version: "v1"
735
+ },
736
+ mode: "lib"
737
+ },
738
+ byte: {
739
+ encode: "__SCAFFOLD_GO_BYTE_ENCODE__",
740
+ decode: "__SCAFFOLD_GO_BYTE_DECODE__",
741
+ imports: ["\"encoding/base64\""],
742
+ mode: "native"
743
+ },
744
+ uuid: {
745
+ encode: "__SCAFFOLD_GO_UUID_ENCODE__",
746
+ decode: "__SCAFFOLD_GO_UUID_DECODE__",
747
+ imports: ["\"github.com/google/uuid\""],
748
+ dep: {
749
+ name: "github.com/google/uuid",
750
+ version: "v1"
751
+ },
752
+ mode: "lib"
753
+ },
754
+ "number-special": {
755
+ encode: "__SCAFFOLD_GO_NUMSPECIAL_ENCODE__",
756
+ decode: "__SCAFFOLD_GO_NUMSPECIAL_DECODE__",
757
+ mode: "native"
758
+ }
759
+ },
760
+ java: {
761
+ "date-time": {
762
+ encode: "$.toString()",
763
+ decode: "Instant.parse($)",
764
+ imports: ["import java.time.Instant;"],
765
+ dep: {
766
+ name: "jackson-datatype-jsr310",
767
+ version: "2.x"
768
+ },
769
+ mode: "lib"
770
+ },
771
+ int64: {
772
+ encode: "@JsonFormat(shape = JsonFormat.Shape.STRING)",
773
+ decode: "@JsonFormat(shape = JsonFormat.Shape.STRING)",
774
+ imports: ["import com.fasterxml.jackson.annotation.JsonFormat;"],
775
+ mode: "native"
776
+ },
777
+ decimal: {
778
+ encode: "@JsonSerialize(using = ToStringSerializer.class)",
779
+ decode: "new BigDecimal($)",
780
+ imports: [
781
+ "import java.math.BigDecimal;",
782
+ "import com.fasterxml.jackson.databind.annotation.JsonSerialize;",
783
+ "import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;"
784
+ ],
785
+ mode: "native"
786
+ },
787
+ byte: {
788
+ encode: "$",
789
+ decode: "$",
790
+ mode: "native"
791
+ },
792
+ uuid: {
793
+ encode: "$.toString()",
794
+ decode: "UUID.fromString($)",
795
+ imports: ["import java.util.UUID;"],
796
+ mode: "native"
797
+ },
798
+ "number-special": {
799
+ encode: "@JsonSerialize(using = NumberSpecialSerializer.class)",
800
+ decode: "@JsonDeserialize(using = NumberSpecialDeserializer.class)",
801
+ imports: ["import com.fasterxml.jackson.databind.annotation.JsonSerialize;", "import com.fasterxml.jackson.databind.annotation.JsonDeserialize;"],
802
+ mode: "native"
803
+ }
804
+ }
805
+ });
806
+ //#endregion
807
+ //#region src/lib/dispatch.ts
808
+ var ve = o();
809
+ le(ve);
810
+ var ye = ae(ve.freeze());
811
+ function be(e, t) {
812
+ return t in (e.input?.properties ?? {});
813
+ }
814
+ function xe(e) {
815
+ let t = e.input?.properties?.data;
816
+ return Object.keys(t?.properties ?? {});
817
+ }
818
+ function Se(e, t) {
819
+ let n = e.input?.properties?.data?.properties?.[t];
820
+ return n === void 0 ? void 0 : n;
821
+ }
822
+ function Ce(e, t) {
823
+ return t === void 0 || e === void 0 ? e : ye.decode(e, t);
824
+ }
825
+ function M(e, t) {
826
+ let n = t.output;
827
+ return !n || Object.keys(n).length === 0 ? e : ye.encode(e, n);
828
+ }
829
+ async function we(e, t, n, r, i, a) {
830
+ let o = xe(n).map((e) => Ce(a[e], Se(n, e)));
831
+ if (be(n, "session") && t) {
832
+ let a = await t({ session: i.session });
833
+ return M(await e[r](a, ...o), n);
834
+ }
835
+ if (n.hasCtx) {
836
+ let a = t ? await t(i) : void 0;
837
+ return M(await e[r](a, ...o), n);
838
+ }
839
+ return M(await e[r](...o), n);
840
+ }
841
+ //#endregion
842
+ //#region src/lib/invoke.ts
843
+ var N = class {
844
+ constructor() {
845
+ this._map = /* @__PURE__ */ new Map();
846
+ }
847
+ set(e, t) {
848
+ this._map.set(e, t);
849
+ }
850
+ get(e) {
851
+ return this._map.get(e);
852
+ }
853
+ has(e) {
854
+ return this._map.has(e);
855
+ }
856
+ };
857
+ function P(e = []) {
858
+ return async function(t, n, r) {
859
+ let i = r.schemas[t];
860
+ if (!i) throw Error(`apigen/invoke: no schema found for operation "${t}"`);
861
+ return e.reduceRight((e, t) => () => t(n, e), () => we(r.fns, r.createClient, i, t, n.envelope, n.domainArgs))();
862
+ };
863
+ }
864
+ //#endregion
865
+ //#region src/lib/define-middleware.ts
866
+ function Te(e) {
867
+ return e;
868
+ }
869
+ //#endregion
870
+ //#region src/lib/event-bus.ts
871
+ var F = class {
872
+ constructor() {
873
+ this.handlers = /* @__PURE__ */ new Map();
874
+ }
875
+ on(e, t) {
876
+ let n = this.handlers.get(e) ?? [];
877
+ return this.handlers.set(e, [...n, t]), () => this.off(e, t);
878
+ }
879
+ off(e, t) {
880
+ let n = this.handlers.get(e);
881
+ if (!n) return;
882
+ let r = n.filter((e) => e !== t);
883
+ r.length === 0 ? this.handlers.delete(e) : this.handlers.set(e, r);
884
+ }
885
+ clear() {
886
+ this.handlers.clear();
887
+ }
888
+ async emit(e) {
889
+ for (let [t, n] of this.handlers) if (Ee(t, e)) for (let t of n) await t(e);
890
+ }
891
+ };
892
+ function Ee(e, t) {
893
+ let [n, r, i] = e.split(":");
894
+ return !(n !== "*" && n !== t.module || r && r !== "*" && r !== t.method || i && i !== "*" && i !== t.lifecycle);
895
+ }
896
+ function I(e, t) {
897
+ for (let n of e) if (n.eventMapping) for (let [e, r] of Object.entries(n.eventMapping)) t.on(e, r);
898
+ }
899
+ //#endregion
900
+ //#region src/lib/build-context.ts
901
+ async function L(e, t, n) {
902
+ let r = { ...t };
903
+ for (let t of e) if (t.createContext) {
904
+ await n.emit({
905
+ module: t.id,
906
+ method: "createContext",
907
+ lifecycle: "start",
908
+ ctx: r
909
+ });
910
+ try {
911
+ let e = await t.createContext(r);
912
+ r = {
913
+ ...r,
914
+ ...e
915
+ }, await n.emit({
916
+ module: t.id,
917
+ method: "createContext",
918
+ lifecycle: "complete",
919
+ ctx: r
920
+ });
921
+ } catch (e) {
922
+ throw await n.emit({
923
+ module: t.id,
924
+ method: "createContext",
925
+ lifecycle: "error",
926
+ ctx: r,
927
+ error: e
928
+ }), e;
929
+ }
930
+ }
931
+ return r;
932
+ }
933
+ e(import.meta.url);
934
+ //#endregion
935
+ //#region ../apigen-core-client/src/lib/get-safety.ts
936
+ var R = /* @__PURE__ */ new Set([
937
+ "string",
938
+ "number",
939
+ "boolean",
940
+ "integer"
941
+ ]);
942
+ function De(e) {
943
+ if (typeof e != "object" || !e) return !1;
944
+ let t = e;
945
+ if (t.$ref !== void 0 || t.oneOf || t.anyOf || t.allOf) return !1;
946
+ let { type: n } = t;
947
+ return typeof n == "string" ? R.has(n) : Array.isArray(n) ? n.every((e) => R.has(e) || e === "null") : Array.isArray(t.enum) && t.enum.length > 0 ? t.enum.every((e) => [
948
+ "string",
949
+ "number",
950
+ "boolean"
951
+ ].includes(typeof e)) : !1;
952
+ }
953
+ function z(e) {
954
+ if (!e || typeof e != "object") return !1;
955
+ let t = e.properties;
956
+ return t === void 0 || typeof t != "object" || !t ? !1 : Object.values(t).every(De);
957
+ }
958
+ //#endregion
959
+ //#region ../apigen-core-client/src/lib/compose-schemas.ts
960
+ function Oe(e) {
961
+ let t = {}, n = (e) => {
962
+ for (let n of ["definitions", "$defs"]) {
963
+ let r = e[n];
964
+ if (r) for (let [e, i] of Object.entries(r)) {
965
+ let r = e.startsWith("#/") ? e : `#/${n}/${e}`;
966
+ t[r] = i;
967
+ }
968
+ }
969
+ };
970
+ for (let t of Object.values(e.schemas)) n(t.input), n(t.output);
971
+ if (Object.keys(t).length !== 0) for (let [n, r] of Object.entries(e.schemas)) try {
972
+ ie(r.input, t), ie(r.output, t);
973
+ } catch (e) {
974
+ throw Error(`[apigen-core-client] Schema validation failed for function "${n}": ${e.message}`);
975
+ }
976
+ }
977
+ function ke(e, t) {
978
+ let n = ["apigen calling convention: all domain parameters go inside a \"data\" envelope — e.g. { \"data\": { ... } }" + (t ? "." : " (an empty object for this zero-parameter tool).")];
979
+ return e.length > 0 && n.push(`Field(s) ${e.map((e) => `"${e}"`).join(", ")} are transport-level envelope metadata, NOT domain data — do not nest them under "data". Over MCP they are read from arguments._meta["x-<pluginId>-<field>"] (default pluginId "adhd"); see @adhd/apigen-engine-naming's envelopeMetaKey/envelopeCliFlag/envelopeEnvVar for the HTTP-header / CLI-flag / env-var equivalents.`), n.join(" ");
980
+ }
981
+ function Ae(e, t, n) {
982
+ Oe(e);
983
+ let r = {};
984
+ for (let [i, a] of Object.entries(e.schemas)) {
985
+ let e = n?.[i] ?? {}, o = a.input.properties ?? {}, s = a.input.required ?? [], c = {}, l = [], u = {};
986
+ for (let n of t) if (n.envelope && e[n.id] !== !1) for (let [e, t] of Object.entries(n.envelope)) c[e] = t, u[e] = n.id, l.includes(e) || l.push(e);
987
+ let d = {
988
+ type: "object",
989
+ properties: o,
990
+ additionalProperties: !1,
991
+ ...s.length > 0 ? { required: s } : {}
992
+ }, f = a.safe === !0 || z(a.input), p = a.input.definitions, m = a.input.$defs;
993
+ r[i] = {
994
+ input: {
995
+ type: "object",
996
+ properties: {
997
+ ...c,
998
+ data: d
999
+ },
1000
+ required: [...l, ...s.length > 0 ? ["data"] : []],
1001
+ additionalProperties: !1,
1002
+ description: ke(Object.keys(c), Object.keys(o).length > 0),
1003
+ ...p ? { definitions: p } : {},
1004
+ ...m ? { $defs: m } : {}
1005
+ },
1006
+ output: a.output,
1007
+ ...a.hasCtx ? { hasCtx: !0 } : {},
1008
+ "x-apigen-safe": f,
1009
+ ...Object.keys(u).length > 0 ? { "x-apigen-envelope": u } : {}
1010
+ };
1011
+ }
1012
+ return r;
1013
+ }
1014
+ //#endregion
1015
+ //#region ../apigen-core-client/src/lib/schema-builders/ts-json-schema.ts
1016
+ var je = { "decimal.js": "Decimal" };
1017
+ function Me(e) {
1018
+ return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1019
+ }
1020
+ Object.entries(je).map(([e, t]) => ({
1021
+ pattern: RegExp(`import\\(["'][^"']*${Me(e)}[^"']*["']\\)\\.\\w+`, "g"),
1022
+ key: t
1023
+ }));
1024
+ //#endregion
1025
+ //#region src/lib/types.ts
1026
+ var B = class extends Error {
1027
+ constructor(e) {
1028
+ super(e), this.name = "ConfigurationError";
1029
+ }
1030
+ };
1031
+ //#endregion
1032
+ //#region src/lib/api-package.ts
1033
+ function V(e) {
1034
+ for (let t of e) for (let e of Object.keys(t.eventMapping ?? {})) {
1035
+ let [n] = e.split(":");
1036
+ if (n !== "*" && n === t.id) throw new B(`Middleware "${t.id}" subscribes to its own events via "${e}". This would cause infinite recursion.`);
1037
+ }
1038
+ }
1039
+ function Ne(e) {
1040
+ let { domainSchemas: t, middlewares: n, overrides: r = {}, strict: i = !1 } = e;
1041
+ V(n);
1042
+ for (let e of Object.keys(r)) if (!(e in t.schemas)) {
1043
+ let t = `Override key "${e}" not found in domain schemas.`;
1044
+ if (i) throw new B(t);
1045
+ console.warn("[apigen-runtime]", t);
1046
+ }
1047
+ let a = new Set(n.map((e) => e.id));
1048
+ for (let [e, t] of Object.entries(r)) for (let n of Object.keys(t ?? {})) if (!a.has(n)) {
1049
+ let t = `Override key "${e}.${n}" does not match any declared middleware id.`;
1050
+ if (i) throw new B(t);
1051
+ console.warn("[apigen-runtime]", t);
1052
+ }
1053
+ let o = Ae(t, n, r), s = new F();
1054
+ return I(n, s), {
1055
+ schemas: o,
1056
+ createClient: async (e) => L(n, e, s),
1057
+ dispose: () => s.clear()
1058
+ };
1059
+ }
1060
+ //#endregion
1061
+ //#region src/lib/logger.ts
1062
+ function Pe(e = {}) {
1063
+ let n = e.level ?? "info", r = typeof e.destination == "string" && e.destination.length > 0, i = !r && !!process.stderr.isTTY, a = e.format ?? (i ? "pretty" : "json"), o = { level: n };
1064
+ if (a === "pretty") return o.transport = {
1065
+ target: "pino-pretty",
1066
+ options: {
1067
+ colorize: !r,
1068
+ translateTime: "SYS:HH:MM:ss.l",
1069
+ ignore: "pid,hostname",
1070
+ ...r ? {
1071
+ destination: e.destination,
1072
+ mkdir: !0
1073
+ } : { destination: 2 }
1074
+ }
1075
+ }, t(o);
1076
+ let s = r ? t.destination({
1077
+ dest: e.destination,
1078
+ mkdir: !0,
1079
+ sync: !0
1080
+ }) : t.destination(2);
1081
+ return t(o, s);
1082
+ }
1083
+ //#endregion
1084
+ //#region src/lib/describe-params.ts
1085
+ function Fe(e) {
1086
+ if (e?.type !== "object" || !e.properties) return;
1087
+ let t = Object.entries(e.properties);
1088
+ if (t.length === 0) return;
1089
+ let n = new Set(e.required ?? []);
1090
+ return `{ ${t.map(([e, t]) => `${e}${n.has(e) ? "" : "?"}: ${H(t)}`).join(", ")} }`;
1091
+ }
1092
+ function Ie(e) {
1093
+ return typeof e == "string" ? `'${e}'` : JSON.stringify(e);
1094
+ }
1095
+ function Le(e) {
1096
+ let t = e.enum ?? [];
1097
+ return t.length === 0 ? "unknown" : t.map(Ie).join("|");
1098
+ }
1099
+ function Re(e, t) {
1100
+ return e.map((e) => H(e, t)).join(" | ");
1101
+ }
1102
+ function H(e, t = 0) {
1103
+ if (!e) return "unknown";
1104
+ if (e.type === "array") return `${H(e.items, 0)}[]`;
1105
+ if (e.enum) return Le(e);
1106
+ if (e.$ref) return String(e.$ref).split("/").pop() || "object";
1107
+ let n = e.anyOf ?? e.oneOf;
1108
+ return n ? n.length === 0 ? "unknown" : t >= 2 ? Re(n, t - 1) : "union" : t >= 1 && e.type === "object" ? Fe(e) ?? "object" : e.type ?? "object";
1109
+ }
1110
+ function U(e) {
1111
+ let t = (e?.input ?? {}).properties?.data, n = t?.properties ?? {}, r = new Set(t?.required ?? []), i = Object.entries(n).map(([e, t]) => ({
1112
+ name: e,
1113
+ type: H(t, 2),
1114
+ required: r.has(e)
1115
+ }));
1116
+ return {
1117
+ params: i,
1118
+ text: i.map((e) => `${e.name}${e.required ? "" : "?"}: ${e.type}`).join(", ")
1119
+ };
1120
+ }
1121
+ //#endregion
1122
+ //#region src/lib/coerce-query.ts
1123
+ function ze(e) {
1124
+ let t;
1125
+ return t = Array.isArray(e) ? e : e ? [e] : [], t.find((e) => e === "number" || e === "integer" || e === "boolean");
1126
+ }
1127
+ function Be(e, t) {
1128
+ let n = (t?.input ?? {}).properties?.data?.properties ?? {}, r = { ...e };
1129
+ for (let [e, t] of Object.entries(n)) {
1130
+ let n = r[e];
1131
+ if (typeof n != "string") continue;
1132
+ let i = ze(t?.type);
1133
+ if (i === "number" || i === "integer") {
1134
+ let t = Number(n);
1135
+ !Number.isNaN(t) && n.trim() !== "" && (r[e] = t);
1136
+ } else i === "boolean" && (n === "true" ? r[e] = !0 : n === "false" && (r[e] = !1));
1137
+ }
1138
+ return r;
1139
+ }
1140
+ //#endregion
1141
+ //#region src/lib/fn-table.ts
1142
+ var W = /* @__PURE__ */ new Set([
1143
+ "default",
1144
+ "module.exports",
1145
+ "__esModule"
1146
+ ]);
1147
+ function Ve(e) {
1148
+ let t = {}, n = (e, r) => {
1149
+ for (let [i, a] of Object.entries(e)) if (typeof a == "function") {
1150
+ !W.has(i) && !(i in t) && (t[i] = a);
1151
+ let e = a.name;
1152
+ e && !(e in t) && (t[e] = a);
1153
+ } else a && typeof a == "object" && r < 2 && W.has(i) && n(a, r + 1);
1154
+ };
1155
+ return n(e, 0), t;
1156
+ }
1157
+ //#endregion
1158
+ //#region ../apigen-base-errors/src/lib/errors.ts
1159
+ var He = [
1160
+ "invalid_argument",
1161
+ "unauthenticated",
1162
+ "permission_denied",
1163
+ "not_found",
1164
+ "internal"
1165
+ ], G = class extends Error {
1166
+ constructor(e, t, n) {
1167
+ super(t), this.name = "ApiError", this.code = e, this.details = n, Object.setPrototypeOf(this, new.target.prototype);
1168
+ }
1169
+ toJSON() {
1170
+ let e = {
1171
+ code: this.code,
1172
+ message: this.message
1173
+ };
1174
+ return this.details !== void 0 && (e.details = this.details), e;
1175
+ }
1176
+ };
1177
+ function Ue(e) {
1178
+ return typeof e == "object" && !!e && e.name === "ApiError" && He.includes(e.code) && typeof e.toJSON == "function";
1179
+ }
1180
+ function We(e, t, n = 0) {
1181
+ return e === "before-first-chunk" ? {
1182
+ phase: e,
1183
+ error: t
1184
+ } : {
1185
+ phase: e,
1186
+ error: t,
1187
+ chunksDelivered: n
1188
+ };
1189
+ }
1190
+ //#endregion
1191
+ //#region src/lib/validate-layer.ts
1192
+ var K = new n({ allErrors: !0 });
1193
+ r(K);
1194
+ for (let e of [
1195
+ s,
1196
+ c,
1197
+ l,
1198
+ u,
1199
+ "discriminator"
1200
+ ]) K.addKeyword({
1201
+ keyword: e,
1202
+ valid: !0
1203
+ });
1204
+ K.addFormat("decimal", /^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$/);
1205
+ function Ge(e) {
1206
+ return e.map((e) => `${e.instancePath || "(root)"} ${e.message ?? "failed schema check"}`).join("; ");
673
1207
  }
674
1208
  function Ke(e, t) {
675
- var o, i, s;
676
- const n = (s = (i = (o = e.input) == null ? void 0 : o.properties) == null ? void 0 : i.data) == null ? void 0 : s.properties, r = n == null ? void 0 : n[t];
677
- return r !== void 0 ? r : void 0;
678
- }
679
- function ze(e, t) {
680
- return t === void 0 || e === void 0 ? e : Ae.decode(e, t);
681
- }
682
- function C(e, t) {
683
- const n = t.output;
684
- return !n || Object.keys(n).length === 0 ? e : Ae.encode(e, n);
685
- }
686
- async function Je(e, t, n, r, o, i) {
687
- const a = He(n).map(
688
- (u) => ze(i[u], Ke(n, u))
689
- );
690
- if (Ve(n, "session") && t) {
691
- const u = await t({ session: o.session }), c = await e[r](u, ...a);
692
- return C(c, n);
693
- }
694
- if (n.hasCtx) {
695
- const u = t ? await t(o) : void 0, c = await e[r](u, ...a);
696
- return C(c, n);
697
- }
698
- const f = await e[r](...a);
699
- return C(f, n);
700
- }
701
- class We {
702
- constructor() {
703
- this._map = /* @__PURE__ */ new Map();
704
- }
705
- set(t, n) {
706
- this._map.set(t, n);
707
- }
708
- get(t) {
709
- return this._map.get(t);
710
- }
711
- /** Returns true when the token has a stored value. */
712
- has(t) {
713
- return this._map.has(t);
714
- }
715
- }
716
- function Ge(e = []) {
717
- return async function(n, r, o) {
718
- const i = o.schemas[n];
719
- if (!i)
720
- throw new Error(
721
- `apigen/invoke: no schema found for operation "${n}"`
722
- );
723
- const s = () => Je(
724
- o.fns,
725
- o.createClient,
726
- i,
727
- n,
728
- r.envelope,
729
- r.domainArgs
730
- );
731
- return e.reduceRight(
732
- (f, u) => () => u(r, f),
733
- s
734
- )();
735
- };
736
- }
737
- function ln(e) {
738
- return e;
739
- }
740
- class Ye {
741
- constructor() {
742
- this.handlers = /* @__PURE__ */ new Map();
743
- }
744
- /**
745
- * Register a handler. Returns an unsubscribe function — handlers registered
746
- * per-request/per-package MUST be removed (via the returned fn, {@link off},
747
- * or {@link clear}) or they accumulate for the bus's lifetime.
748
- */
749
- on(t, n) {
750
- const r = this.handlers.get(t) ?? [];
751
- return this.handlers.set(t, [...r, n]), () => this.off(t, n);
752
- }
753
- /** Remove a previously registered handler (no-op if absent). */
754
- off(t, n) {
755
- const r = this.handlers.get(t);
756
- if (!r)
757
- return;
758
- const o = r.filter((i) => i !== n);
759
- o.length === 0 ? this.handlers.delete(t) : this.handlers.set(t, o);
760
- }
761
- /** Remove every handler — call on package/server teardown. */
762
- clear() {
763
- this.handlers.clear();
764
- }
765
- async emit(t) {
766
- for (const [n, r] of this.handlers)
767
- if (Ze(n, t))
768
- for (const o of r)
769
- await o(t);
770
- }
771
- }
772
- function Ze(e, t) {
773
- const [n, r, o] = e.split(":");
774
- return !(n !== "*" && n !== t.module || r && r !== "*" && r !== t.method || o && o !== "*" && o !== t.lifecycle);
775
- }
776
- function Qe(e, t) {
777
- for (const n of e)
778
- if (n.eventMapping)
779
- for (const [r, o] of Object.entries(n.eventMapping))
780
- t.on(r, o);
781
- }
782
- async function Xe(e, t, n) {
783
- let r = { ...t };
784
- for (const o of e)
785
- if (o.createContext) {
786
- await n.emit({
787
- module: o.id,
788
- method: "createContext",
789
- lifecycle: "start",
790
- ctx: r
791
- });
792
- try {
793
- const i = await o.createContext(r);
794
- r = { ...r, ...i }, await n.emit({
795
- module: o.id,
796
- method: "createContext",
797
- lifecycle: "complete",
798
- ctx: r
799
- });
800
- } catch (i) {
801
- throw await n.emit({
802
- module: o.id,
803
- method: "createContext",
804
- lifecycle: "error",
805
- ctx: r,
806
- error: i
807
- }), i;
808
- }
809
- }
810
- return r;
811
- }
812
- he(import.meta.url);
813
- function ne(e, t) {
814
- const n = /* @__PURE__ */ new Set(), r = [e];
815
- for (; r.length > 0; ) {
816
- const o = r.pop();
817
- if (!o || n.has(o))
818
- continue;
819
- n.add(o);
820
- const i = o.$ref;
821
- if (typeof i == "string") {
822
- if (t) {
823
- const l = t[i];
824
- if (!l) {
825
- const p = Object.keys(t).join(", ") || "(none)";
826
- throw new Error(
827
- `[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${p}`
828
- );
829
- }
830
- r.push(l);
831
- }
832
- continue;
833
- }
834
- const s = o.oneOf;
835
- if (Array.isArray(s))
836
- for (const l of s)
837
- typeof l == "object" && l !== null && r.push(l);
838
- const a = o.properties;
839
- if (a)
840
- for (const l of Object.values(a))
841
- r.push(l);
842
- const f = o.items;
843
- if (Array.isArray(f))
844
- for (const l of f)
845
- typeof l == "object" && l !== null && r.push(l);
846
- else
847
- typeof f == "object" && f !== null && r.push(f);
848
- const u = o.additionalProperties;
849
- typeof u == "object" && u !== null && r.push(u);
850
- const c = o.propertyNames;
851
- typeof c == "object" && c !== null && r.push(c);
852
- }
853
- }
854
- const re = /* @__PURE__ */ new Set(["string", "number", "boolean", "integer"]);
855
- function et(e) {
856
- if (typeof e != "object" || e === null)
857
- return !1;
858
- const t = e;
859
- if (t.$ref !== void 0 || t.oneOf || t.anyOf || t.allOf)
860
- return !1;
861
- const { type: n } = t;
862
- return typeof n == "string" ? re.has(n) : Array.isArray(n) ? n.every((r) => re.has(r) || r === "null") : Array.isArray(t.enum) && t.enum.length > 0 ? t.enum.every((r) => ["string", "number", "boolean"].includes(typeof r)) : !1;
1209
+ let n = _e(t), r = [`Validation failed: ${Ge(e)}`];
1210
+ return n && r.push(n), r.join(" — ");
1211
+ }
1212
+ var qe = async (e, t) => {
1213
+ let n = e.ctx.get(Je);
1214
+ if (n === void 0) return t();
1215
+ let r = n[e.operation.id];
1216
+ if (r === void 0) return t();
1217
+ let i = {
1218
+ data: e.domainArgs,
1219
+ ...e.envelope
1220
+ }, a = K.compile(r.input);
1221
+ if (!a(i)) {
1222
+ let e = a.errors ?? [];
1223
+ throw new G("invalid_argument", Ke(e, r.input), e);
1224
+ }
1225
+ return t();
1226
+ }, Je = Symbol("ValidateSchemasToken");
1227
+ function Ye(e) {
1228
+ return async function(t, n) {
1229
+ let r = e[t.operation.id];
1230
+ if (r === void 0) return n();
1231
+ let i = {
1232
+ data: t.domainArgs,
1233
+ ...t.envelope
1234
+ }, a = K.compile(r.input);
1235
+ if (!a(i)) {
1236
+ let e = a.errors ?? [];
1237
+ throw new G("invalid_argument", Ke(e, r.input), e);
1238
+ }
1239
+ return n();
1240
+ };
1241
+ }
1242
+ //#endregion
1243
+ //#region src/lib/instance-registry.ts
1244
+ function Xe() {
1245
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (e) => {
1246
+ let t = Math.random() * 16 | 0;
1247
+ return (e === "x" ? t : t & 3 | 8).toString(16);
1248
+ });
1249
+ }
1250
+ var Ze = class {
1251
+ constructor(e = {}) {
1252
+ this._store = /* @__PURE__ */ new Map(), this._sweeper = null, this._defaultTtlMs = e.defaultTtlMs;
1253
+ let t = null;
1254
+ t = e.sweepIntervalMs === 0 ? null : e.sweepIntervalMs === void 0 ? this._defaultTtlMs === void 0 ? null : Math.max(1e3, Math.floor(this._defaultTtlMs / 4)) : e.sweepIntervalMs, t !== null && t > 0 && (this._sweeper = setInterval(() => this._sweep(), t), this._sweeper.unref?.());
1255
+ }
1256
+ create(e, t = [], n) {
1257
+ let r = new e(...t), i = Xe(), a = n ?? this._defaultTtlMs, o = a === void 0 ? null : Date.now() + a;
1258
+ return this._store.set(i, {
1259
+ instance: r,
1260
+ expiresAt: o
1261
+ }), { instanceId: i };
1262
+ }
1263
+ get(e) {
1264
+ let t = this._store.get(e);
1265
+ if (!t) throw Error(`[instance-registry] Unknown instanceId: "${e}"`);
1266
+ if (t.expiresAt !== null && Date.now() > t.expiresAt) throw this._evict(e, t), Error(`[instance-registry] Instance "${e}" has expired`);
1267
+ return t.instance;
1268
+ }
1269
+ async dispatch(e, t, n = []) {
1270
+ let r = this.get(e), i = r[t];
1271
+ if (typeof i != "function") throw Error(`[instance-registry] Method "${t}" not found on instance "${e}"`);
1272
+ return i.call(r, ...n);
1273
+ }
1274
+ async dispose(e) {
1275
+ let t = this._store.get(e);
1276
+ t && (this._store.delete(e), await q(t.instance));
1277
+ }
1278
+ async disposeAll() {
1279
+ this._sweeper !== null && (clearInterval(this._sweeper), this._sweeper = null);
1280
+ let e = [...this._store.entries()];
1281
+ this._store.clear(), await Promise.all(e.map(([, e]) => q(e.instance)));
1282
+ }
1283
+ get size() {
1284
+ return this._store.size;
1285
+ }
1286
+ _sweep() {
1287
+ let e = Date.now();
1288
+ for (let [t, n] of this._store) n.expiresAt !== null && e > n.expiresAt && this._evict(t, n);
1289
+ }
1290
+ _evict(e, t) {
1291
+ this._store.delete(e), q(t.instance);
1292
+ }
1293
+ };
1294
+ async function q(e) {
1295
+ if (typeof e == "object" && e && "dispose" in e) {
1296
+ let t = e.dispose;
1297
+ typeof t == "function" && await t.call(e);
1298
+ }
1299
+ }
1300
+ //#endregion
1301
+ //#region src/lib/stream.ts
1302
+ function Qe(e) {
1303
+ let { produce: t, signal: n } = e, r = n ?? new AbortController().signal;
1304
+ return { [Symbol.asyncIterator]() {
1305
+ let e = t(r);
1306
+ return {
1307
+ async next() {
1308
+ return r.aborted ? (await e.return(void 0), {
1309
+ done: !0,
1310
+ value: void 0
1311
+ }) : e.next();
1312
+ },
1313
+ async return(t) {
1314
+ return await e.return(t), {
1315
+ done: !0,
1316
+ value: void 0
1317
+ };
1318
+ },
1319
+ async throw(t) {
1320
+ return await e.throw(t), {
1321
+ done: !0,
1322
+ value: void 0
1323
+ };
1324
+ }
1325
+ };
1326
+ } };
1327
+ }
1328
+ async function $e(e) {
1329
+ let t = [];
1330
+ for await (let n of e) t.push(n);
1331
+ return t;
1332
+ }
1333
+ async function et(e) {
1334
+ let t = [];
1335
+ try {
1336
+ for await (let n of e) t.push(n);
1337
+ return {
1338
+ ok: !0,
1339
+ chunks: t
1340
+ };
1341
+ } catch (e) {
1342
+ let n = Ue(e) ? e : new G("internal", e instanceof Error ? e.message : String(e));
1343
+ return t.length === 0 ? {
1344
+ ok: !1,
1345
+ carrier: We("before-first-chunk", n)
1346
+ } : {
1347
+ ok: !1,
1348
+ carrier: We("after-first-chunk", n, t.length)
1349
+ };
1350
+ }
863
1351
  }
864
1352
  function tt(e) {
865
- if (!e || typeof e != "object")
866
- return !1;
867
- const t = e.properties;
868
- return t === void 0 || typeof t != "object" || t === null ? !1 : Object.values(t).every(
869
- et
870
- );
871
- }
872
- function nt(e) {
873
- const t = {}, n = (r) => {
874
- for (const o of ["definitions", "$defs"]) {
875
- const i = r[o];
876
- if (i)
877
- for (const [s, a] of Object.entries(i)) {
878
- const f = s.startsWith("#/") ? s : `#/${o}/${s}`;
879
- t[f] = a;
880
- }
881
- }
882
- };
883
- for (const r of Object.values(e.schemas))
884
- n(r.input), n(r.output);
885
- if (Object.keys(t).length !== 0)
886
- for (const [r, o] of Object.entries(e.schemas))
887
- try {
888
- ne(o.input, t), ne(o.output, t);
889
- } catch (i) {
890
- throw new Error(
891
- `[apigen-core-client] Schema validation failed for function "${r}": ${i.message}`
892
- );
893
- }
894
- }
895
- function rt(e, t) {
896
- const n = [
897
- 'apigen calling convention: all domain parameters go inside a "data" envelope — e.g. { "data": { ... } }' + (t ? "." : " (an empty object for this zero-parameter tool).")
898
- ];
899
- return e.length > 0 && n.push(
900
- `Field(s) ${e.map((r) => `"${r}"`).join(", ")} are transport-level envelope metadata, NOT domain data — do not nest them under "data". Over MCP they are read from arguments._meta["x-<pluginId>-<field>"] (default pluginId "adhd"); see @adhd/apigen-engine-naming's envelopeMetaKey/envelopeCliFlag/envelopeEnvVar for the HTTP-header / CLI-flag / env-var equivalents.`
901
- ), n.join(" ");
902
- }
903
- function ot(e, t, n) {
904
- nt(e);
905
- const r = {};
906
- for (const [o, i] of Object.entries(e.schemas)) {
907
- const s = (n == null ? void 0 : n[o]) ?? {}, a = i.input.properties ?? {}, f = i.input.required ?? [], u = {}, c = [], l = {};
908
- for (const h of t)
909
- if (h.envelope && s[h.id] !== !1)
910
- for (const [b, Ee] of Object.entries(h.envelope))
911
- u[b] = Ee, l[b] = h.id, c.includes(b) || c.push(b);
912
- const p = {
913
- type: "object",
914
- properties: a,
915
- additionalProperties: !1,
916
- ...f.length > 0 ? { required: f } : {}
917
- }, y = i.safe === !0 || tt(i.input), d = i.input.definitions, m = i.input.$defs;
918
- r[o] = {
919
- input: {
920
- type: "object",
921
- properties: { ...u, data: p },
922
- required: [
923
- ...c,
924
- ...f.length > 0 ? ["data"] : []
925
- ],
926
- // BUG-APIGEN-017: reject any property that isn't a declared envelope
927
- // field or the "data" wrapper — no silently-ignored junk params.
928
- additionalProperties: !1,
929
- // BUG-APIGEN-020: document the envelope/data calling convention inline.
930
- description: rt(
931
- Object.keys(u),
932
- Object.keys(a).length > 0
933
- ),
934
- ...d ? { definitions: d } : {},
935
- ...m ? { $defs: m } : {}
936
- },
937
- output: i.output,
938
- // Carry the ctx-param flag through to dispatch (BUG-APIGEN-001).
939
- ...i.hasCtx ? { hasCtx: !0 } : {},
940
- "x-apigen-safe": y,
941
- // DEBT-APIGEN-ENVELOPE-CAPABILITY-UNWIRED-001: field → pluginId map,
942
- // read by `op-plan.ts`'s `computeEnvelopeFields`. Omitted (not an empty
943
- // object) when no middleware contributed an envelope field, matching
944
- // `x-apigen-safe`'s own "absent means nothing to report" convention.
945
- ...Object.keys(l).length > 0 ? { "x-apigen-envelope": l } : {}
946
- };
947
- }
948
- return r;
949
- }
950
- const it = {
951
- "decimal.js": "Decimal"
1353
+ return typeof e == "object" && !!e && Symbol.asyncIterator in e;
1354
+ }
1355
+ //#endregion
1356
+ //#region src/lib/logical/union-codec.ts
1357
+ var J = class extends Error {
1358
+ constructor(e, t) {
1359
+ super(t), this.name = "UnionCodecError", this.code = e;
1360
+ }
952
1361
  };
953
- function st(e) {
954
- return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
955
- }
956
- Object.entries(it).map(([e, t]) => ({
957
- // Matches: import("...{modSpec}...").<anyMemberName> (single or double quotes)
958
- // The module specifier appears inside an absolute path, so we allow any
959
- // characters between the quote and the specifier, and between the specifier
960
- // and the closing quote. The trailing member accessor (`.default`, `.Decimal`,
961
- // …) is matched generically (`\w+`) rather than anchored to one spelling —
962
- // see the doc comment above for why.
963
- pattern: new RegExp(
964
- `import\\(["'][^"']*${st(e)}[^"']*["']\\)\\.\\w+`,
965
- "g"
966
- ),
967
- key: t
968
- }));
969
- class B extends Error {
970
- constructor(t) {
971
- super(t), this.name = "ConfigurationError";
972
- }
1362
+ function Y(e) {
1363
+ let t = e.discriminator;
1364
+ if (typeof t != "object" || !t || Array.isArray(t)) return;
1365
+ let n = t;
1366
+ if (typeof n.propertyName != "string") return;
1367
+ let r = n.mapping;
1368
+ if (typeof r == "object" && r && !Array.isArray(r)) return {
1369
+ propertyName: n.propertyName,
1370
+ mapping: r
1371
+ };
1372
+ }
1373
+ function nt(e, t) {
1374
+ let n = e[c];
1375
+ if (typeof n == "string") {
1376
+ let e = t.registry.get(n);
1377
+ if (e) return e;
1378
+ }
1379
+ return t.registry.resolve(e);
1380
+ }
1381
+ function rt(e, t, n) {
1382
+ let r = nt(t, n);
1383
+ return r ? r.encode(e, t, n) : e;
1384
+ }
1385
+ function it(e, t, n) {
1386
+ let r = nt(t, n);
1387
+ return r ? r.decode(e, t, n) : e;
973
1388
  }
974
1389
  function at(e) {
975
- for (const t of e)
976
- for (const n of Object.keys(t.eventMapping ?? {})) {
977
- const [r] = n.split(":");
978
- if (r !== "*" && r === t.id)
979
- throw new B(
980
- `Middleware "${t.id}" subscribes to its own events via "${n}". This would cause infinite recursion.`
981
- );
982
- }
983
- }
984
- function pn(e) {
985
- const {
986
- domainSchemas: t,
987
- middlewares: n,
988
- overrides: r = {},
989
- strict: o = !1
990
- } = e;
991
- at(n);
992
- for (const u of Object.keys(r))
993
- if (!(u in t.schemas)) {
994
- const c = `Override key "${u}" not found in domain schemas.`;
995
- if (o)
996
- throw new B(c);
997
- console.warn("[apigen-runtime]", c);
998
- }
999
- const i = new Set(n.map((u) => u.id));
1000
- for (const [u, c] of Object.entries(r))
1001
- for (const l of Object.keys(c ?? {}))
1002
- if (!i.has(l)) {
1003
- const p = `Override key "${u}.${l}" does not match any declared middleware id.`;
1004
- if (o)
1005
- throw new B(p);
1006
- console.warn("[apigen-runtime]", p);
1007
- }
1008
- const s = ot(
1009
- t,
1010
- n,
1011
- r
1012
- ), a = new Ye();
1013
- return Qe(n, a), { schemas: s, createClient: async (u) => Xe(n, u, a), dispose: () => a.clear() };
1014
- }
1015
- function dn(e = {}) {
1016
- const t = e.level ?? "info", n = typeof e.destination == "string" && e.destination.length > 0, r = !n && !!process.stderr.isTTY, o = e.format ?? (r ? "pretty" : "json"), i = { level: t };
1017
- if (o === "pretty")
1018
- return i.transport = {
1019
- target: "pino-pretty",
1020
- options: {
1021
- colorize: !n,
1022
- translateTime: "SYS:HH:MM:ss.l",
1023
- ignore: "pid,hostname",
1024
- ...n ? { destination: e.destination, mkdir: !0 } : { destination: 2 }
1025
- }
1026
- }, N(i);
1027
- const s = n ? N.destination({ dest: e.destination, mkdir: !0, sync: !0 }) : N.destination(2);
1028
- return N(i, s);
1029
- }
1030
- function ct(e) {
1031
- if (!e || e.type !== "object" || !e.properties)
1032
- return;
1033
- const t = Object.entries(e.properties);
1034
- if (t.length === 0)
1035
- return;
1036
- const n = new Set(e.required ?? []);
1037
- return `{ ${t.map(([o, i]) => `${o}${n.has(o) ? "" : "?"}: ${k(i)}`).join(", ")} }`;
1390
+ let { id: t, schema: n } = e;
1391
+ return {
1392
+ id: t,
1393
+ kind: "union",
1394
+ schema: n,
1395
+ matches(e) {
1396
+ if (e["x-apigen-logical"] === "union") return !0;
1397
+ let t = e.oneOf;
1398
+ return !Array.isArray(t) || t.length === 0 ? !1 : Y(e) !== void 0;
1399
+ },
1400
+ encode(e, n, r) {
1401
+ let i = Y(n);
1402
+ if (!i) throw new J("E_UNION_NO_DISCRIMINATOR", `[${t}] schema node at "${r.path}" has no discriminator.propertyName; cannot dispatch union branch.`);
1403
+ let { propertyName: a, mapping: o } = i, s = e[a];
1404
+ if (s == null) throw new J("E_UNION_NO_DISCRIMINATOR", `[${t}] value at "${r.path}" is missing the discriminator property "${a}".`);
1405
+ let c = String(s), l = o[c];
1406
+ if (!l) throw new J("E_UNION_UNKNOWN_TAG", `[${t}] discriminator tag "${c}" at "${r.path}" is not in the mapping (known: ${Object.keys(o).join(", ")}).`);
1407
+ return rt(e, r.resolve(l), {
1408
+ ...r,
1409
+ path: `${r.path}[${c}]`
1410
+ });
1411
+ },
1412
+ decode(e, n, r) {
1413
+ if (typeof e != "object" || !e || Array.isArray(e)) {
1414
+ let n;
1415
+ throw n = e === null ? "null" : Array.isArray(e) ? "array" : typeof e, new J("E_UNION_INVALID_WIRE", `[${t}] expected a JSON object on the wire at "${r.path}", got ${n}.`);
1416
+ }
1417
+ let i = e, a = Y(n);
1418
+ if (!a) throw new J("E_UNION_NO_DISCRIMINATOR", `[${t}] schema node at "${r.path}" has no discriminator.propertyName; cannot dispatch union branch.`);
1419
+ let { propertyName: o, mapping: s } = a, c = i[o];
1420
+ if (c == null) throw new J("E_UNION_NO_DISCRIMINATOR", `[${t}] wire object at "${r.path}" is missing the discriminator property "${o}".`);
1421
+ let l = String(c), u = s[l];
1422
+ if (!u) throw new J("E_UNION_UNKNOWN_TAG", `[${t}] discriminator tag "${l}" on the wire at "${r.path}" is not in the mapping (known: ${Object.keys(s).join(", ")}).`);
1423
+ return it(e, r.resolve(u), {
1424
+ ...r,
1425
+ path: `${r.path}[${l}]`
1426
+ });
1427
+ }
1428
+ };
1429
+ }
1430
+ var X = class extends Error {
1431
+ constructor(e, t) {
1432
+ super(t), this.name = "NominalCodecError", this.code = e;
1433
+ }
1434
+ };
1435
+ function ot(e) {
1436
+ let t = e.properties;
1437
+ return t && typeof t == "object" && !Array.isArray(t) ? t : {};
1438
+ }
1439
+ function st(e) {
1440
+ let t = e.required;
1441
+ return Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
1442
+ }
1443
+ function ct(e, t, n) {
1444
+ if (t) {
1445
+ let r = n.registry.resolve(t);
1446
+ if (r) return r.encode(e, t, n);
1447
+ }
1448
+ return e;
1449
+ }
1450
+ function lt(e, t, n) {
1451
+ if (t) {
1452
+ let r = n.registry.resolve(t);
1453
+ if (r) return r.decode(e, t, n);
1454
+ }
1455
+ return e;
1038
1456
  }
1039
1457
  function ut(e) {
1040
- return typeof e == "string" ? `'${e}'` : JSON.stringify(e);
1041
- }
1042
- function ft(e) {
1043
- const t = e.enum ?? [];
1044
- return t.length === 0 ? "unknown" : t.map(ut).join("|");
1045
- }
1046
- function lt(e, t) {
1047
- return e.map((n) => k(n, t)).join(" | ");
1048
- }
1049
- function k(e, t = 0) {
1050
- if (!e)
1051
- return "unknown";
1052
- if (e.type === "array")
1053
- return `${k(e.items, 0)}[]`;
1054
- if (e.enum)
1055
- return ft(e);
1056
- if (e.$ref)
1057
- return String(e.$ref).split("/").pop() || "object";
1058
- const n = e.anyOf ?? e.oneOf;
1059
- return n ? n.length === 0 ? "unknown" : t >= 2 ? lt(n, t - 1) : "union" : t >= 1 && e.type === "object" ? ct(e) ?? "object" : e.type ?? "object";
1060
- }
1061
- function pt(e) {
1062
- var a;
1063
- const n = (a = ((e == null ? void 0 : e.input) ?? {}).properties) == null ? void 0 : a.data, r = (n == null ? void 0 : n.properties) ?? {}, o = new Set((n == null ? void 0 : n.required) ?? []), i = Object.entries(r).map(([f, u]) => ({
1064
- name: f,
1065
- type: k(u, 2),
1066
- required: o.has(f)
1067
- })), s = i.map((f) => `${f.name}${f.required ? "" : "?"}: ${f.type}`).join(", ");
1068
- return { params: i, text: s };
1069
- }
1070
- function dt(e) {
1071
- let t;
1072
- return Array.isArray(e) ? t = e : e ? t = [e] : t = [], t.find((n) => n === "number" || n === "integer" || n === "boolean");
1073
- }
1074
- function yn(e, t) {
1075
- var i, s;
1076
- const r = ((s = (i = ((t == null ? void 0 : t.input) ?? {}).properties) == null ? void 0 : i.data) == null ? void 0 : s.properties) ?? {}, o = { ...e };
1077
- for (const [a, f] of Object.entries(r)) {
1078
- const u = o[a];
1079
- if (typeof u != "string")
1080
- continue;
1081
- const c = dt(f == null ? void 0 : f.type);
1082
- if (c === "number" || c === "integer") {
1083
- const l = Number(u);
1084
- !Number.isNaN(l) && u.trim() !== "" && (o[a] = l);
1085
- } else
1086
- c === "boolean" && (u === "true" ? o[a] = !0 : u === "false" && (o[a] = !1));
1087
- }
1088
- return o;
1089
- }
1090
- const oe = /* @__PURE__ */ new Set(["default", "module.exports", "__esModule"]);
1091
- function mn(e) {
1092
- const t = {}, n = (r, o) => {
1093
- for (const [i, s] of Object.entries(r))
1094
- if (typeof s == "function") {
1095
- !oe.has(i) && !(i in t) && (t[i] = s);
1096
- const a = s.name;
1097
- a && !(a in t) && (t[a] = s);
1098
- } else
1099
- s && typeof s == "object" && o < 2 && oe.has(i) && n(s, o + 1);
1100
- };
1101
- return n(e, 0), t;
1102
- }
1103
- const yt = [
1104
- "invalid_argument",
1105
- "unauthenticated",
1106
- "permission_denied",
1107
- "not_found",
1108
- "internal"
1109
- ];
1110
- class I extends Error {
1111
- constructor(t, n, r) {
1112
- super(n), this.name = "ApiError", this.code = t, this.details = r, Object.setPrototypeOf(this, new.target.prototype);
1113
- }
1114
- /** Serialise to a plain object suitable for JSON transport. */
1115
- toJSON() {
1116
- const t = {
1117
- code: this.code,
1118
- message: this.message
1119
- };
1120
- return this.details !== void 0 && (t.details = this.details), t;
1121
- }
1122
- }
1123
- function je(e) {
1124
- return typeof e == "object" && e !== null && e.name === "ApiError" && yt.includes(e.code) && typeof e.toJSON == "function";
1125
- }
1126
- function ie(e, t, n = 0) {
1127
- return e === "before-first-chunk" ? { phase: e, error: t } : { phase: e, error: t, chunksDelivered: n };
1128
- }
1129
- const O = new _e({ allErrors: !0 });
1130
- ke(O);
1131
- for (const e of [
1132
- F,
1133
- V,
1134
- T,
1135
- P,
1136
- "discriminator"
1137
- ])
1138
- O.addKeyword({ keyword: e, valid: !0 });
1139
- O.addFormat("decimal", /^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$/);
1140
- function mt(e) {
1141
- return e.map(
1142
- (t) => `${t.instancePath || "(root)"} ${t.message ?? "failed schema check"}`
1143
- ).join("; ");
1144
- }
1145
- function Oe(e, t) {
1146
- const n = ve(t), r = [`Validation failed: ${mt(e)}`];
1147
- return n && r.push(n), r.join(" — ");
1148
- }
1149
- const hn = async (e, t) => {
1150
- const n = e.ctx.get(ht);
1151
- if (n === void 0)
1152
- return t();
1153
- const r = e.operation.id, o = n[r];
1154
- if (o === void 0)
1155
- return t();
1156
- const i = {
1157
- data: e.domainArgs,
1158
- ...e.envelope
1159
- }, s = O.compile(o.input);
1160
- if (!s(i)) {
1161
- const f = s.errors ?? [];
1162
- throw new I(
1163
- "invalid_argument",
1164
- Oe(f, o.input),
1165
- f
1166
- );
1167
- }
1168
- return t();
1169
- }, ht = Symbol("ValidateSchemasToken");
1458
+ let { id: t, schema: n, ctor: r } = e;
1459
+ return {
1460
+ id: t,
1461
+ kind: "nominal",
1462
+ schema: n,
1463
+ matches(e) {
1464
+ return e["x-apigen-logical"] === "nominal" || e[c] === t;
1465
+ },
1466
+ encode(e, n, r) {
1467
+ if (typeof e != "object" || !e) return e;
1468
+ if (r.seen.has(e)) {
1469
+ if (r.mode === "strict") throw new X("E_NOMINAL_CYCLE", `[${t}] cycle detected at "${r.path}": a nominal instance references itself. Strict mode rejects cycles (DESIGN §8.3); break the cycle or use 'lossy' mode.`);
1470
+ return null;
1471
+ }
1472
+ r.seen.add(e);
1473
+ try {
1474
+ let t = ot(n), i = e[typeof n["x-apigen-tojson"] == "string" ? n[u] : "toJSON"];
1475
+ if (typeof i == "function") {
1476
+ let n = i.call(e);
1477
+ if (typeof n == "object" && n && !Array.isArray(n)) {
1478
+ let e = {};
1479
+ for (let [i, a] of Object.entries(n)) e[i] = ct(a, t[i], {
1480
+ ...r,
1481
+ path: `${r.path}/${i}`
1482
+ });
1483
+ return e;
1484
+ }
1485
+ return n;
1486
+ }
1487
+ let a = {}, o = Object.keys(t), s = e, c = o.length > 0 ? o : Object.keys(s);
1488
+ for (let e of c) {
1489
+ let n = s[e];
1490
+ n !== void 0 && (a[e] = ct(n, t[e], {
1491
+ ...r,
1492
+ path: `${r.path}/${e}`
1493
+ }));
1494
+ }
1495
+ return a;
1496
+ } finally {
1497
+ r.seen.delete(e);
1498
+ }
1499
+ },
1500
+ decode(e, n, i) {
1501
+ if (typeof e != "object" || !e || Array.isArray(e)) {
1502
+ let n;
1503
+ throw n = e === null ? "null" : Array.isArray(e) ? "array" : typeof e, new X("E_NOMINAL_INVALID_WIRE", `[${t}] expected a JSON object on the wire at "${i.path}", got ${n}.`);
1504
+ }
1505
+ let a = e;
1506
+ for (let e of st(n)) if (!(e in a)) throw new X("E_NOMINAL_INVALID_WIRE", `[${t}] required field "${e}" missing on the wire at "${i.path}".`);
1507
+ if (n["x-apigen-instances"] === !1) throw new X("E_NOMINAL_NONRECONSTRUCTABLE", `[${t}] is encode-only: the extractor marked it non-reconstructable (x-apigen-instances:false). It cannot be decoded back to a live instance.`);
1508
+ let o = ot(n), s = {};
1509
+ for (let [e, t] of Object.entries(a)) s[e] = lt(t, o[e], {
1510
+ ...i,
1511
+ path: `${i.path}/${e}`
1512
+ });
1513
+ if (r) {
1514
+ let e = typeof n["x-apigen-ctor"] == "string" ? n[l] : "fromJSON", t = r[e];
1515
+ return typeof t == "function" ? t.call(r, s) : new r(s);
1516
+ }
1517
+ return s;
1518
+ }
1519
+ };
1520
+ }
1521
+ //#endregion
1522
+ //#region src/lib/logical/host-ts.ts
1523
+ var dt = [
1524
+ x,
1525
+ S,
1526
+ w,
1527
+ T,
1528
+ E,
1529
+ A
1530
+ ], ft = ut({
1531
+ id: "ts.NominalSentinel",
1532
+ schema: {
1533
+ type: "object",
1534
+ "x-apigen-logical": "nominal",
1535
+ "x-apigen-codec": "ts.NominalSentinel"
1536
+ }
1537
+ }), Z = at({
1538
+ id: "ts.UnionSentinel",
1539
+ schema: {
1540
+ oneOf: [],
1541
+ discriminator: {
1542
+ propertyName: "kind",
1543
+ mapping: {}
1544
+ },
1545
+ "x-apigen-logical": "union",
1546
+ "x-apigen-codec": "ts.UnionSentinel"
1547
+ }
1548
+ });
1549
+ function pt() {
1550
+ let e = [
1551
+ ...dt.map((e) => [e.id, e]),
1552
+ [ft.id, ft],
1553
+ [Z.id, Z]
1554
+ ];
1555
+ return Object.freeze(new Map(e));
1556
+ }
1557
+ var mt = Object.freeze({
1558
+ host: "ts",
1559
+ logicalTypeVersion: d,
1560
+ codecs: pt()
1561
+ });
1562
+ //#endregion
1563
+ //#region src/lib/tool-description.ts
1564
+ function ht(e, t, n) {
1565
+ let r = [
1566
+ n,
1567
+ typeof t?.input?.description == "string" ? t.input.description : void 0,
1568
+ _e(t?.input)
1569
+ ].filter((e) => typeof e == "string" && e.length > 0);
1570
+ return r.length > 0 ? r.join(" — ") : e;
1571
+ }
1572
+ //#endregion
1573
+ //#region src/lib/mcp-output-schema.ts
1170
1574
  function gt(e) {
1171
- return async function(n, r) {
1172
- const o = n.operation.id, i = e[o];
1173
- if (i === void 0)
1174
- return r();
1175
- const s = {
1176
- data: n.domainArgs,
1177
- ...n.envelope
1178
- }, a = O.compile(i.input);
1179
- if (!a(s)) {
1180
- const u = a.errors ?? [];
1181
- throw new I(
1182
- "invalid_argument",
1183
- Oe(u, i.input),
1184
- u
1185
- );
1186
- }
1187
- return r();
1188
- };
1189
- }
1190
- function wt() {
1191
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (e) => {
1192
- const t = Math.random() * 16 | 0;
1193
- return (e === "x" ? t : t & 3 | 8).toString(16);
1194
- });
1195
- }
1196
- class gn {
1197
- constructor(t = {}) {
1198
- var r, o;
1199
- this._store = /* @__PURE__ */ new Map(), this._sweeper = null, this._defaultTtlMs = t.defaultTtlMs;
1200
- let n = null;
1201
- t.sweepIntervalMs === 0 ? n = null : t.sweepIntervalMs !== void 0 ? n = t.sweepIntervalMs : this._defaultTtlMs !== void 0 ? n = Math.max(1e3, Math.floor(this._defaultTtlMs / 4)) : n = null, n !== null && n > 0 && (this._sweeper = setInterval(() => this._sweep(), n), (o = (r = this._sweeper).unref) == null || o.call(r));
1202
- }
1203
- // ---------------------------------------------------------------------------
1204
- // create — construct + register
1205
- // ---------------------------------------------------------------------------
1206
- /**
1207
- * Construct an instance of `Ctor` with `args` and store it in the registry.
1208
- *
1209
- * @param Ctor - The class to instantiate.
1210
- * @param args - Constructor arguments (positional).
1211
- * @param ttlMs - Per-entry TTL override. Falls back to `defaultTtlMs`.
1212
- * @returns `{ instanceId }` — pass this to `dispatch()` / `get()`.
1213
- */
1214
- create(t, n = [], r) {
1215
- const o = new t(...n), i = wt(), s = r ?? this._defaultTtlMs, a = s !== void 0 ? Date.now() + s : null;
1216
- return this._store.set(i, { instance: o, expiresAt: a }), { instanceId: i };
1217
- }
1218
- // ---------------------------------------------------------------------------
1219
- // get — retrieve a live instance
1220
- // ---------------------------------------------------------------------------
1221
- /**
1222
- * Retrieve the instance stored under `instanceId`.
1223
- *
1224
- * @throws When `instanceId` is unknown or the entry has expired.
1225
- */
1226
- get(t) {
1227
- const n = this._store.get(t);
1228
- if (!n)
1229
- throw new Error(
1230
- `[instance-registry] Unknown instanceId: "${t}"`
1231
- );
1232
- if (n.expiresAt !== null && Date.now() > n.expiresAt)
1233
- throw this._evict(t, n), new Error(
1234
- `[instance-registry] Instance "${t}" has expired`
1235
- );
1236
- return n.instance;
1237
- }
1238
- // ---------------------------------------------------------------------------
1239
- // dispatch — call a method on a live instance
1240
- // ---------------------------------------------------------------------------
1241
- /**
1242
- * Look up `instanceId` and call `method` on it with `args`.
1243
- *
1244
- * @throws When `instanceId` is unknown/expired, or `method` is not a function
1245
- * on the instance.
1246
- */
1247
- async dispatch(t, n, r = []) {
1248
- const o = this.get(t), i = o[n];
1249
- if (typeof i != "function")
1250
- throw new Error(
1251
- `[instance-registry] Method "${n}" not found on instance "${t}"`
1252
- );
1253
- return i.call(o, ...r);
1254
- }
1255
- // ---------------------------------------------------------------------------
1256
- // dispose — explicit teardown
1257
- // ---------------------------------------------------------------------------
1258
- /**
1259
- * Remove `instanceId` from the registry and call `instance.dispose()` if the
1260
- * instance exposes one (opt-in lifecycle hook).
1261
- */
1262
- async dispose(t) {
1263
- const n = this._store.get(t);
1264
- n && (this._store.delete(t), await x(n.instance));
1265
- }
1266
- // ---------------------------------------------------------------------------
1267
- // disposeAll — full teardown
1268
- // ---------------------------------------------------------------------------
1269
- /**
1270
- * Dispose every live instance and stop the background sweeper. Call this
1271
- * when the server is shutting down.
1272
- */
1273
- async disposeAll() {
1274
- this._sweeper !== null && (clearInterval(this._sweeper), this._sweeper = null);
1275
- const t = [...this._store.entries()];
1276
- this._store.clear(), await Promise.all(t.map(([, n]) => x(n.instance)));
1277
- }
1278
- // ---------------------------------------------------------------------------
1279
- // size — diagnostic helper
1280
- // ---------------------------------------------------------------------------
1281
- /** Number of live (non-expired) entries currently in the registry. */
1282
- get size() {
1283
- return this._store.size;
1284
- }
1285
- // ---------------------------------------------------------------------------
1286
- // Internal sweeper
1287
- // ---------------------------------------------------------------------------
1288
- _sweep() {
1289
- const t = Date.now();
1290
- for (const [n, r] of this._store)
1291
- r.expiresAt !== null && t > r.expiresAt && this._evict(n, r);
1292
- }
1293
- _evict(t, n) {
1294
- this._store.delete(t), x(n.instance);
1295
- }
1296
- }
1297
- async function x(e) {
1298
- if (e !== null && typeof e == "object" && "dispose" in e) {
1299
- const t = e.dispose;
1300
- typeof t == "function" && await t.call(e);
1301
- }
1302
- }
1303
- function wn(e) {
1304
- const { produce: t, signal: n } = e, r = n ?? new AbortController().signal;
1305
- return {
1306
- [Symbol.asyncIterator]() {
1307
- const o = t(r);
1308
- return {
1309
- async next() {
1310
- return r.aborted ? (await o.return(void 0), { done: !0, value: void 0 }) : o.next();
1311
- },
1312
- async return(i) {
1313
- return await o.return(i), { done: !0, value: void 0 };
1314
- },
1315
- async throw(i) {
1316
- return await o.throw(i), { done: !0, value: void 0 };
1317
- }
1318
- };
1319
- }
1320
- };
1321
- }
1322
- async function bn(e) {
1323
- const t = [];
1324
- for await (const n of e)
1325
- t.push(n);
1326
- return t;
1327
- }
1328
- async function bt(e) {
1329
- const t = [];
1330
- try {
1331
- for await (const n of e)
1332
- t.push(n);
1333
- return { ok: !0, chunks: t };
1334
- } catch (n) {
1335
- const r = je(n) ? n : new I(
1336
- "internal",
1337
- n instanceof Error ? n.message : String(n)
1338
- );
1339
- return t.length === 0 ? {
1340
- ok: !1,
1341
- carrier: ie("before-first-chunk", r)
1342
- } : {
1343
- ok: !1,
1344
- carrier: ie("after-first-chunk", r, t.length)
1345
- };
1346
- }
1347
- }
1348
- function vt(e) {
1349
- return e !== null && typeof e == "object" && Symbol.asyncIterator in e;
1350
- }
1351
- class g extends Error {
1352
- constructor(t, n) {
1353
- super(n), this.name = "UnionCodecError", this.code = t;
1354
- }
1355
- }
1356
- function S(e) {
1357
- const t = e.discriminator;
1358
- if (t === null || typeof t != "object" || Array.isArray(t))
1359
- return;
1360
- const n = t;
1361
- if (typeof n.propertyName != "string")
1362
- return;
1363
- const r = n.mapping;
1364
- if (!(r === null || typeof r != "object" || Array.isArray(r)))
1365
- return {
1366
- propertyName: n.propertyName,
1367
- mapping: r
1368
- };
1369
- }
1370
- function Ne(e, t) {
1371
- const n = e[V];
1372
- if (typeof n == "string") {
1373
- const r = t.registry.get(n);
1374
- if (r)
1375
- return r;
1376
- }
1377
- return t.registry.resolve(e);
1378
- }
1379
- function $t(e, t, n) {
1380
- const r = Ne(t, n);
1381
- return r ? r.encode(e, t, n) : e;
1382
- }
1383
- function At(e, t, n) {
1384
- const r = Ne(t, n);
1385
- return r ? r.decode(e, t, n) : e;
1386
- }
1387
- function jt(e) {
1388
- const { id: t, schema: n } = e;
1389
- return {
1390
- id: t,
1391
- kind: "union",
1392
- schema: n,
1393
- /**
1394
- * Structural ownership test: the node carries both `oneOf` (non-empty array)
1395
- * and `discriminator.propertyName`. The `x-apigen-logical:"union"` hint
1396
- * accelerates the match but is advisory — the structure is authoritative.
1397
- */
1398
- matches(r) {
1399
- if (r[F] === "union")
1400
- return !0;
1401
- const o = r.oneOf;
1402
- return !Array.isArray(o) || o.length === 0 ? !1 : S(r) !== void 0;
1403
- },
1404
- encode(r, o, i) {
1405
- const s = S(o);
1406
- if (!s)
1407
- throw new g(
1408
- "E_UNION_NO_DISCRIMINATOR",
1409
- `[${t}] schema node at "${i.path}" has no discriminator.propertyName; cannot dispatch union branch.`
1410
- );
1411
- const { propertyName: a, mapping: f } = s, u = r[a];
1412
- if (u == null)
1413
- throw new g(
1414
- "E_UNION_NO_DISCRIMINATOR",
1415
- `[${t}] value at "${i.path}" is missing the discriminator property "${a}".`
1416
- );
1417
- const c = String(u), l = f[c];
1418
- if (!l)
1419
- throw new g(
1420
- "E_UNION_UNKNOWN_TAG",
1421
- `[${t}] discriminator tag "${c}" at "${i.path}" is not in the mapping (known: ${Object.keys(f).join(", ")}).`
1422
- );
1423
- const p = i.resolve(l);
1424
- return $t(r, p, {
1425
- ...i,
1426
- path: `${i.path}[${c}]`
1427
- });
1428
- },
1429
- decode(r, o, i) {
1430
- if (r === null || typeof r != "object" || Array.isArray(r)) {
1431
- let d;
1432
- throw r === null ? d = "null" : Array.isArray(r) ? d = "array" : d = typeof r, new g(
1433
- "E_UNION_INVALID_WIRE",
1434
- `[${t}] expected a JSON object on the wire at "${i.path}", got ${d}.`
1435
- );
1436
- }
1437
- const s = r, a = S(o);
1438
- if (!a)
1439
- throw new g(
1440
- "E_UNION_NO_DISCRIMINATOR",
1441
- `[${t}] schema node at "${i.path}" has no discriminator.propertyName; cannot dispatch union branch.`
1442
- );
1443
- const { propertyName: f, mapping: u } = a, c = s[f];
1444
- if (c == null)
1445
- throw new g(
1446
- "E_UNION_NO_DISCRIMINATOR",
1447
- `[${t}] wire object at "${i.path}" is missing the discriminator property "${f}".`
1448
- );
1449
- const l = String(c), p = u[l];
1450
- if (!p)
1451
- throw new g(
1452
- "E_UNION_UNKNOWN_TAG",
1453
- `[${t}] discriminator tag "${l}" on the wire at "${i.path}" is not in the mapping (known: ${Object.keys(u).join(", ")}).`
1454
- );
1455
- const y = i.resolve(p);
1456
- return At(r, y, {
1457
- ...i,
1458
- path: `${i.path}[${l}]`
1459
- });
1460
- }
1461
- };
1462
- }
1463
- const Ot = "x-apigen-instances";
1464
- class E extends Error {
1465
- constructor(t, n) {
1466
- super(n), this.name = "NominalCodecError", this.code = t;
1467
- }
1468
- }
1469
- function se(e) {
1470
- const t = e.properties;
1471
- return t && typeof t == "object" && !Array.isArray(t) ? t : {};
1575
+ if (!e || typeof e != "object" || Array.isArray(e) || Object.keys(e).length === 0) return {
1576
+ outputSchema: void 0,
1577
+ wrapped: !1
1578
+ };
1579
+ let t = e;
1580
+ return t.type === "object" ? {
1581
+ outputSchema: t,
1582
+ wrapped: !0
1583
+ } : {
1584
+ outputSchema: void 0,
1585
+ wrapped: !1
1586
+ };
1587
+ }
1588
+ function _t(e, t) {
1589
+ if (e) return t && typeof t == "object" && !Array.isArray(t) ? t : void 0;
1590
+ }
1591
+ //#endregion
1592
+ //#region src/lib/batch.ts
1593
+ var vt = 4, yt = 32;
1594
+ function Q(e) {
1595
+ return Ue(e) ? e : e instanceof Error ? new G("internal", e.message) : e;
1596
+ }
1597
+ function bt(e, t) {
1598
+ if (e === "serial" || e === "chained") return 1;
1599
+ let n = t ?? 4;
1600
+ return !Number.isFinite(n) || n < 1 ? 1 : Math.min(Math.floor(n), 32);
1601
+ }
1602
+ function xt() {}
1603
+ function St(e, t) {
1604
+ if (!e && !t) return {
1605
+ signal: void 0,
1606
+ dispose: xt
1607
+ };
1608
+ let n = new AbortController(), r = () => {
1609
+ n.abort(e?.reason);
1610
+ };
1611
+ e && (e.aborted ? n.abort(e.reason) : e.addEventListener("abort", r, { once: !0 }));
1612
+ let i;
1613
+ return t !== void 0 && !n.signal.aborted && (i = setTimeout(() => {
1614
+ n.abort(new DOMException(`apigen/invokeBatch: item timed out after ${t}ms`, "TimeoutError"));
1615
+ }, t), i.unref?.()), {
1616
+ signal: n.signal,
1617
+ dispose() {
1618
+ e && e.removeEventListener("abort", r), i && clearTimeout(i);
1619
+ }
1620
+ };
1621
+ }
1622
+ async function Ct(e, t, n, r, i, a) {
1623
+ let o = St(r.signal, a);
1624
+ try {
1625
+ let a = await e(t, o.signal ? {
1626
+ ...r,
1627
+ signal: o.signal
1628
+ } : r, i);
1629
+ if (tt(a)) {
1630
+ let e = await et(a);
1631
+ if (!e.ok) {
1632
+ let t = e.carrier;
1633
+ return {
1634
+ index: n,
1635
+ status: "rejected",
1636
+ reason: Q(t.error),
1637
+ ...t.phase === "after-first-chunk" ? { chunksDelivered: t.chunksDelivered } : {}
1638
+ };
1639
+ }
1640
+ return {
1641
+ index: n,
1642
+ status: "fulfilled",
1643
+ chunks: e.chunks
1644
+ };
1645
+ }
1646
+ return {
1647
+ index: n,
1648
+ status: "fulfilled",
1649
+ value: a
1650
+ };
1651
+ } catch (e) {
1652
+ return {
1653
+ index: n,
1654
+ status: "rejected",
1655
+ reason: Q(e)
1656
+ };
1657
+ } finally {
1658
+ o.dispose();
1659
+ }
1660
+ }
1661
+ async function wt(e, t, n, r, i = {}) {
1662
+ let a = i.mode ?? "parallel", o = i.onItemError ?? "continue", s = bt(a, i.concurrency), c = Array(n.length), l = (e) => ({
1663
+ index: e,
1664
+ status: "rejected",
1665
+ reason: Q(/* @__PURE__ */ Error("apigen/invokeBatch: not attempted — an earlier item failed and " + (a === "chained" ? "mode is \"chained\"" : "onItemError is \"abort\"")))
1666
+ });
1667
+ if (a === "serial" || a === "chained") {
1668
+ let s = !1;
1669
+ for (let u = 0; u < n.length; u++) {
1670
+ if (s) {
1671
+ c[u] = l(u);
1672
+ continue;
1673
+ }
1674
+ let d = await Ct(e, t, u, n[u], r, i.itemTimeoutMs);
1675
+ c[u] = d, d.status === "rejected" && (a === "chained" || o === "abort") && (s = !0);
1676
+ }
1677
+ return c;
1678
+ }
1679
+ let u = 0, d = !1, f = Math.max(1, Math.min(s, n.length)), p = Array.from({ length: f }, async () => {
1680
+ for (;;) {
1681
+ let a = u++;
1682
+ if (a >= n.length) return;
1683
+ if (d) {
1684
+ c[a] = l(a);
1685
+ continue;
1686
+ }
1687
+ let s = await Ct(e, t, a, n[a], r, i.itemTimeoutMs);
1688
+ c[a] = s, s.status === "rejected" && o === "abort" && (d = !0);
1689
+ }
1690
+ });
1691
+ return await Promise.all(p), c;
1692
+ }
1693
+ //#endregion
1694
+ //#region ../apigen-engine-naming/src/lib/naming.ts
1695
+ function Tt(e) {
1696
+ return e.words.join("-");
1697
+ }
1698
+ function Et(e) {
1699
+ return e.words.map(Mt).join("");
1700
+ }
1701
+ function Dt(e) {
1702
+ return e.words.join("_");
1703
+ }
1704
+ function Ot(e, t = {}) {
1705
+ let n = [e.namespace, ...e.path], r = e.safe || z(e.input) ? "GET" : "POST", i = t.http?.verb?.[e.id] ?? r, a = "/" + n.map((e) => Tt(e)).join("/"), o = n.map((e) => Dt(e)).join("_"), s = n.map((e) => Dt(e)).slice(0, -1).join("."), c = Et(n[n.length - 2] ?? n[0]), l = Et(n[n.length - 1]), u = n.map((e) => Tt(e));
1706
+ return {
1707
+ http: {
1708
+ verb: i,
1709
+ route: a
1710
+ },
1711
+ mcp: { name: o },
1712
+ grpc: {
1713
+ package: s,
1714
+ service: c,
1715
+ method: l
1716
+ },
1717
+ cli: { path: u }
1718
+ };
1719
+ }
1720
+ function kt(e, t) {
1721
+ return `x-${e}-${t}`;
1722
+ }
1723
+ function At(e, t) {
1724
+ return `--${e}-${t}`;
1725
+ }
1726
+ function jt(e, t) {
1727
+ let n = (e) => e.toUpperCase().replace(/-/g, "_");
1728
+ return e === "adhd" ? `APIGEN_${n(t)}` : `APIGEN_${n(e)}_${n(t)}`;
1729
+ }
1730
+ function Mt(e) {
1731
+ return e.length === 0 ? e : e[0].toUpperCase() + e.slice(1);
1732
+ }
1733
+ //#endregion
1734
+ //#region src/lib/op-plan.ts
1735
+ function $(e) {
1736
+ return e.anyOf ?? e.oneOf;
1472
1737
  }
1473
1738
  function Nt(e) {
1474
- const t = e.required;
1475
- return Array.isArray(t) ? t.filter((n) => typeof n == "string") : [];
1476
- }
1477
- function ae(e, t, n) {
1478
- if (t) {
1479
- const r = n.registry.resolve(t);
1480
- if (r)
1481
- return r.encode(e, t, n);
1482
- }
1483
- return e;
1484
- }
1485
- function Et(e, t, n) {
1486
- if (t) {
1487
- const r = n.registry.resolve(t);
1488
- if (r)
1489
- return r.decode(e, t, n);
1490
- }
1491
- return e;
1492
- }
1493
- function _t(e) {
1494
- const { id: t, schema: n, ctor: r } = e;
1495
- return {
1496
- id: t,
1497
- kind: "nominal",
1498
- schema: n,
1499
- /**
1500
- * Structural ownership test: the node advertises `x-apigen-logical:'nominal'`
1501
- * or carries an `x-apigen-codec` matching this codec's id. Per
1502
- * `[inv:hints-advisory]` these are accelerators; a later structural fallback
1503
- * (object `$def` with `properties`) is the authoritative signal and is
1504
- * handled by the transcoder walk, not by this cheap test.
1505
- */
1506
- matches(o) {
1507
- return o[F] === "nominal" ? !0 : o[V] === t;
1508
- },
1509
- encode(o, i, s) {
1510
- if (o === null || typeof o != "object")
1511
- return o;
1512
- if (s.seen.has(o)) {
1513
- if (s.mode === "strict")
1514
- throw new E(
1515
- "E_NOMINAL_CYCLE",
1516
- `[${t}] cycle detected at "${s.path}": a nominal instance references itself. Strict mode rejects cycles (DESIGN §8.3); break the cycle or use 'lossy' mode.`
1517
- );
1518
- return null;
1519
- }
1520
- s.seen.add(o);
1521
- try {
1522
- const a = se(i), f = typeof i[P] == "string" ? i[P] : "toJSON", u = o[f];
1523
- if (typeof u == "function") {
1524
- const d = u.call(o);
1525
- if (d !== null && typeof d == "object" && !Array.isArray(d)) {
1526
- const m = {};
1527
- for (const [h, b] of Object.entries(
1528
- d
1529
- ))
1530
- m[h] = ae(b, a[h], {
1531
- ...s,
1532
- path: `${s.path}/${h}`
1533
- });
1534
- return m;
1535
- }
1536
- return d;
1537
- }
1538
- const c = {}, l = Object.keys(a), p = o, y = l.length > 0 ? l : Object.keys(p);
1539
- for (const d of y) {
1540
- const m = p[d];
1541
- m !== void 0 && (c[d] = ae(m, a[d], {
1542
- ...s,
1543
- path: `${s.path}/${d}`
1544
- }));
1545
- }
1546
- return c;
1547
- } finally {
1548
- s.seen.delete(o);
1549
- }
1550
- },
1551
- decode(o, i, s) {
1552
- if (o === null || typeof o != "object" || Array.isArray(o)) {
1553
- let c;
1554
- throw o === null ? c = "null" : Array.isArray(o) ? c = "array" : c = typeof o, new E(
1555
- "E_NOMINAL_INVALID_WIRE",
1556
- `[${t}] expected a JSON object on the wire at "${s.path}", got ${c}.`
1557
- );
1558
- }
1559
- const a = o;
1560
- for (const c of Nt(i))
1561
- if (!(c in a))
1562
- throw new E(
1563
- "E_NOMINAL_INVALID_WIRE",
1564
- `[${t}] required field "${c}" missing on the wire at "${s.path}".`
1565
- );
1566
- if (i[Ot] === !1)
1567
- throw new E(
1568
- "E_NOMINAL_NONRECONSTRUCTABLE",
1569
- `[${t}] is encode-only: the extractor marked it non-reconstructable (x-apigen-instances:false). It cannot be decoded back to a live instance.`
1570
- );
1571
- const f = se(i), u = {};
1572
- for (const [c, l] of Object.entries(a))
1573
- u[c] = Et(l, f[c], {
1574
- ...s,
1575
- path: `${s.path}/${c}`
1576
- });
1577
- if (r) {
1578
- const c = typeof i[T] == "string" ? i[T] : "fromJSON", l = r[c];
1579
- if (typeof l == "function")
1580
- return l.call(
1581
- r,
1582
- u
1583
- );
1584
- const p = r;
1585
- return new p(u);
1586
- }
1587
- return u;
1588
- }
1589
- };
1590
- }
1591
- const kt = [
1592
- H,
1593
- K,
1594
- z,
1595
- J,
1596
- W,
1597
- G
1598
- ], It = {
1599
- type: "object",
1600
- "x-apigen-logical": "nominal",
1601
- "x-apigen-codec": "ts.NominalSentinel"
1602
- }, ce = _t({
1603
- id: "ts.NominalSentinel",
1604
- schema: It
1605
- }), Ct = {
1606
- oneOf: [],
1607
- discriminator: { propertyName: "kind", mapping: {} },
1608
- "x-apigen-logical": "union",
1609
- "x-apigen-codec": "ts.UnionSentinel"
1610
- }, ue = jt({
1611
- id: "ts.UnionSentinel",
1612
- schema: Ct
1613
- });
1614
- function xt() {
1615
- const e = [
1616
- // 6 well-known scalars
1617
- ...kt.map((t) => [
1618
- t.id,
1619
- t
1620
- ]),
1621
- // nominal kind sentinel
1622
- [ce.id, ce],
1623
- // union kind sentinel
1624
- [ue.id, ue]
1625
- ];
1626
- return Object.freeze(new Map(e));
1627
- }
1628
- const vn = Object.freeze({
1629
- host: "ts",
1630
- logicalTypeVersion: Ce,
1631
- codecs: xt()
1632
- });
1633
- function $n(e, t, n) {
1634
- var s;
1635
- const r = typeof ((s = t == null ? void 0 : t.input) == null ? void 0 : s.description) == "string" ? t.input.description : void 0, o = ve(t == null ? void 0 : t.input), i = [n, r, o].filter(
1636
- (a) => typeof a == "string" && a.length > 0
1637
- );
1638
- return i.length > 0 ? i.join(" — ") : e;
1639
- }
1640
- function An(e) {
1641
- if (!e || typeof e != "object" || Array.isArray(e) || Object.keys(e).length === 0)
1642
- return { outputSchema: void 0, wrapped: !1 };
1643
- const t = e;
1644
- return t.type === "object" ? { outputSchema: t, wrapped: !1 } : {
1645
- outputSchema: {
1646
- type: "object",
1647
- properties: { result: t },
1648
- required: ["result"]
1649
- },
1650
- wrapped: !0
1651
- };
1652
- }
1653
- function jn(e, t) {
1654
- return e ? { result: t } : t && typeof t == "object" && !Array.isArray(t) ? t : void 0;
1655
- }
1656
- const St = 4, Mt = 32;
1657
- function q(e) {
1658
- return je(e) ? e : e instanceof Error ? new I("internal", e.message) : e;
1659
- }
1660
- function Tt(e, t) {
1661
- if (e === "serial" || e === "chained")
1662
- return 1;
1663
- const n = t ?? St;
1664
- return !Number.isFinite(n) || n < 1 ? 1 : Math.min(Math.floor(n), Mt);
1665
- }
1666
- function Pt() {
1667
- }
1668
- function Dt(e, t) {
1669
- var i;
1670
- if (!e && !t)
1671
- return { signal: void 0, dispose: Pt };
1672
- const n = new AbortController(), r = () => {
1673
- n.abort(e == null ? void 0 : e.reason);
1674
- };
1675
- e && (e.aborted ? n.abort(e.reason) : e.addEventListener("abort", r, { once: !0 }));
1676
- let o;
1677
- return t !== void 0 && !n.signal.aborted && (o = setTimeout(() => {
1678
- n.abort(
1679
- new DOMException(
1680
- `apigen/invokeBatch: item timed out after ${t}ms`,
1681
- "TimeoutError"
1682
- )
1683
- );
1684
- }, t), (i = o.unref) == null || i.call(o)), {
1685
- signal: n.signal,
1686
- dispose() {
1687
- e && e.removeEventListener("abort", r), o && clearTimeout(o);
1688
- }
1689
- };
1690
- }
1691
- async function fe(e, t, n, r, o, i) {
1692
- const s = Dt(r.signal, i);
1693
- try {
1694
- const a = s.signal ? { ...r, signal: s.signal } : r, f = await e(t, a, o);
1695
- if (vt(f)) {
1696
- const u = await bt(f);
1697
- if (u.ok)
1698
- return { index: n, status: "fulfilled", chunks: u.chunks };
1699
- const c = u.carrier;
1700
- return {
1701
- index: n,
1702
- status: "rejected",
1703
- reason: q(c.error),
1704
- ...c.phase === "after-first-chunk" ? { chunksDelivered: c.chunksDelivered } : {}
1705
- };
1706
- }
1707
- return { index: n, status: "fulfilled", value: f };
1708
- } catch (a) {
1709
- return { index: n, status: "rejected", reason: q(a) };
1710
- } finally {
1711
- s.dispose();
1712
- }
1713
- }
1714
- async function On(e, t, n, r, o = {}) {
1715
- const i = o.mode ?? "parallel", s = o.onItemError ?? "continue", a = Tt(i, o.concurrency), f = new Array(n.length), u = (d) => ({
1716
- index: d,
1717
- status: "rejected",
1718
- reason: q(
1719
- new Error(
1720
- "apigen/invokeBatch: not attempted — an earlier item failed and " + (i === "chained" ? 'mode is "chained"' : 'onItemError is "abort"')
1721
- )
1722
- )
1723
- });
1724
- if (i === "serial" || i === "chained") {
1725
- let d = !1;
1726
- for (let m = 0; m < n.length; m++) {
1727
- if (d) {
1728
- f[m] = u(m);
1729
- continue;
1730
- }
1731
- const h = await fe(
1732
- e,
1733
- t,
1734
- m,
1735
- n[m],
1736
- r,
1737
- o.itemTimeoutMs
1738
- );
1739
- f[m] = h, h.status === "rejected" && (i === "chained" || s === "abort") && (d = !0);
1740
- }
1741
- return f;
1742
- }
1743
- let c = 0, l = !1;
1744
- const p = Math.max(1, Math.min(a, n.length)), y = Array.from({ length: p }, async () => {
1745
- for (; ; ) {
1746
- const d = c++;
1747
- if (d >= n.length)
1748
- return;
1749
- if (l) {
1750
- f[d] = u(d);
1751
- continue;
1752
- }
1753
- const m = await fe(
1754
- e,
1755
- t,
1756
- d,
1757
- n[d],
1758
- r,
1759
- o.itemTimeoutMs
1760
- );
1761
- f[d] = m, m.status === "rejected" && s === "abort" && (l = !0);
1762
- }
1763
- });
1764
- return await Promise.all(y), f;
1765
- }
1766
- he(import.meta.url);
1767
- const le = /* @__PURE__ */ new Set(["string", "number", "boolean", "integer"]);
1768
- function Ut(e) {
1769
- if (typeof e != "object" || e === null)
1770
- return !1;
1771
- const t = e;
1772
- if (t.$ref !== void 0 || t.oneOf || t.anyOf || t.allOf)
1773
- return !1;
1774
- const { type: n } = t;
1775
- return typeof n == "string" ? le.has(n) : Array.isArray(n) ? n.every((r) => le.has(r) || r === "null") : Array.isArray(t.enum) && t.enum.length > 0 ? t.enum.every((r) => ["string", "number", "boolean"].includes(typeof r)) : !1;
1739
+ if (!e) return !1;
1740
+ if (e.type === "array" || e.type === "object") return !0;
1741
+ let t = $(e);
1742
+ if (t) {
1743
+ let e = t.filter((e) => e.type !== "null");
1744
+ return e.length > 0 && e.every((e) => e.type === "array" || e.type === "object");
1745
+ }
1746
+ return !1;
1747
+ }
1748
+ function Pt(e) {
1749
+ if (!e) return !1;
1750
+ if (e.type === "boolean") return !0;
1751
+ let t = $(e);
1752
+ if (t) {
1753
+ let e = t.filter((e) => e.type !== "null");
1754
+ return e.length > 0 && e.every((e) => e.type === "boolean");
1755
+ }
1756
+ return !1;
1757
+ }
1758
+ function Ft(e) {
1759
+ if (!e) return !1;
1760
+ if (e.type === "number" || e.type === "integer") return !0;
1761
+ let t = $(e);
1762
+ if (t) {
1763
+ let e = t.filter((e) => e.type !== "null");
1764
+ return e.length > 0 && e.every((e) => e.type === "number" || e.type === "integer");
1765
+ }
1766
+ return !1;
1767
+ }
1768
+ function It(e) {
1769
+ return e.replace(/[A-Z]/g, (e) => "-" + e.toLowerCase());
1770
+ }
1771
+ function Lt(e) {
1772
+ let t = e.input?.properties?.data;
1773
+ return {
1774
+ props: t?.properties ?? {},
1775
+ required: t?.required ?? []
1776
+ };
1776
1777
  }
1777
1778
  function Rt(e) {
1778
- if (!e || typeof e != "object")
1779
- return !1;
1780
- const t = e.properties;
1781
- return t === void 0 || typeof t != "object" || t === null ? !1 : Object.values(t).every(
1782
- Ut
1783
- );
1784
- }
1785
- const Lt = {
1786
- "decimal.js": "Decimal"
1787
- };
1788
- function Bt(e) {
1789
- return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1790
- }
1791
- Object.entries(Lt).map(([e, t]) => ({
1792
- // Matches: import("...{modSpec}...").<anyMemberName> (single or double quotes)
1793
- // The module specifier appears inside an absolute path, so we allow any
1794
- // characters between the quote and the specifier, and between the specifier
1795
- // and the closing quote. The trailing member accessor (`.default`, `.Decimal`,
1796
- // …) is matched generically (`\w+`) rather than anchored to one spelling —
1797
- // see the doc comment above for why.
1798
- pattern: new RegExp(
1799
- `import\\(["'][^"']*${Bt(e)}[^"']*["']\\)\\.\\w+`,
1800
- "g"
1801
- ),
1802
- key: t
1803
- }));
1804
- function pe(e) {
1805
- return e.words.join("-");
1806
- }
1807
- function de(e) {
1808
- return e.words.map(Kt).join("");
1809
- }
1810
- function ye(e) {
1811
- return e.words.join("_");
1812
- }
1813
- function qt(e, t = {}) {
1814
- var n, r;
1815
- const o = [e.namespace, ...e.path], i = e.safe || Rt(e.input) ? "GET" : "POST", s = ((r = (n = t.http) == null ? void 0 : n.verb) == null ? void 0 : r[e.id]) ?? i, a = "/" + o.map((y) => pe(y)).join("/"), f = o.map((y) => ye(y)).join("_"), u = o.map((y) => ye(y)).slice(0, -1).join("."), c = de(o[o.length - 2] ?? o[0]), l = de(o[o.length - 1]), p = o.map((y) => pe(y));
1816
- return {
1817
- http: { verb: s, route: a },
1818
- mcp: { name: f },
1819
- grpc: { package: u, service: c, method: l },
1820
- cli: { path: p }
1821
- };
1779
+ return typeof e.handler == "function";
1822
1780
  }
1823
- function Ft(e, t) {
1824
- return `x-${e}-${t}`;
1781
+ function zt(e) {
1782
+ let t = e.input?.properties ?? {}, n = e["x-apigen-envelope"], r = [];
1783
+ for (let e of Object.keys(t)) {
1784
+ if (e === "data") continue;
1785
+ let t = n?.[e] ?? "adhd", i = kt(t, e);
1786
+ r.push({
1787
+ field: e,
1788
+ pluginId: t,
1789
+ httpHeader: i,
1790
+ mcpMetaKey: i,
1791
+ cliFlag: At(t, e),
1792
+ envVar: jt(t, e)
1793
+ });
1794
+ }
1795
+ return r;
1796
+ }
1797
+ function Bt(e, t, n, r) {
1798
+ t === n || e.has(t) || e.set(t, {
1799
+ ...r,
1800
+ aliasOf: n
1801
+ });
1825
1802
  }
1826
1803
  function Vt(e, t) {
1827
- return `--${e}-${t}`;
1828
- }
1829
- function Ht(e, t) {
1830
- const n = (r) => r.toUpperCase().replace(/-/g, "_");
1831
- return e === "adhd" ? `APIGEN_${n(t)}` : `APIGEN_${n(e)}_${n(t)}`;
1832
- }
1833
- function Kt(e) {
1834
- return e.length === 0 ? e : e[0].toUpperCase() + e.slice(1);
1835
- }
1836
- function Y(e) {
1837
- return e.anyOf ?? e.oneOf;
1838
- }
1839
- function zt(e) {
1840
- if (!e)
1841
- return !1;
1842
- if (e.type === "array" || e.type === "object")
1843
- return !0;
1844
- const t = Y(e);
1845
- if (t) {
1846
- const n = t.filter((r) => r.type !== "null");
1847
- return n.length > 0 && n.every((r) => r.type === "array" || r.type === "object");
1848
- }
1849
- return !1;
1850
- }
1851
- function Jt(e) {
1852
- if (!e)
1853
- return !1;
1854
- if (e.type === "boolean")
1855
- return !0;
1856
- const t = Y(e);
1857
- if (t) {
1858
- const n = t.filter((r) => r.type !== "null");
1859
- return n.length > 0 && n.every((r) => r.type === "boolean");
1860
- }
1861
- return !1;
1804
+ let n = /* @__PURE__ */ new Map(), { props: r } = Lt(e);
1805
+ for (let [e, t] of Object.entries(r)) {
1806
+ let r = "string";
1807
+ Pt(t) ? r = "boolean" : (Nt(t) || Ft(t)) && (r = "json");
1808
+ let i = It(e), a = {
1809
+ camelKey: e,
1810
+ kind: "domain",
1811
+ valueKind: r
1812
+ };
1813
+ n.set(i, a), Bt(n, e, i, a);
1814
+ }
1815
+ for (let e of t) {
1816
+ let t = e.cliFlag.replace(/^--/, ""), r = {
1817
+ camelKey: e.field,
1818
+ kind: "envelope",
1819
+ valueKind: "string",
1820
+ envVar: e.envVar
1821
+ };
1822
+ n.set(t, r);
1823
+ }
1824
+ return n;
1825
+ }
1826
+ function Ht(e) {
1827
+ let { op: t, schema: n, transport: r, projection: i } = e, a = Ot(t, i), o = n ? zt(n) : [], s = n ? Vt(n, o) : /* @__PURE__ */ new Map(), c = n ? U(n).params : void 0, l = {
1828
+ op: t,
1829
+ transport: r,
1830
+ http: a.http,
1831
+ mcp: a.mcp,
1832
+ cli: a.cli,
1833
+ grpc: a.grpc,
1834
+ params: c,
1835
+ envelope: o,
1836
+ cliFlags: s,
1837
+ streaming: t.streaming,
1838
+ isMount: Rt(t)
1839
+ };
1840
+ if (Rt(t)) {
1841
+ let e = t.handler;
1842
+ l.mountHandler = async (t) => e(t);
1843
+ }
1844
+ return l;
1845
+ }
1846
+ //#endregion
1847
+ //#region src/lib/package-invoker.ts
1848
+ function Ut(e) {
1849
+ let t = e.usePlugins;
1850
+ return Array.isArray(t) ? t : [];
1862
1851
  }
1863
1852
  function Wt(e) {
1864
- if (!e)
1865
- return !1;
1866
- if (e.type === "number" || e.type === "integer")
1867
- return !0;
1868
- const t = Y(e);
1869
- if (t) {
1870
- const n = t.filter((r) => r.type !== "null");
1871
- return n.length > 0 && n.every((r) => r.type === "number" || r.type === "integer");
1872
- }
1873
- return !1;
1853
+ let t = e.exitCode;
1854
+ return typeof t == "function" ? t : void 0;
1874
1855
  }
1875
1856
  function Gt(e) {
1876
- return e.replace(/[A-Z]/g, (t) => "-" + t.toLowerCase());
1877
- }
1878
- function Yt(e) {
1879
- var o, i;
1880
- const t = (i = (o = e.input) == null ? void 0 : o.properties) == null ? void 0 : i.data, n = (t == null ? void 0 : t.properties) ?? {}, r = (t == null ? void 0 : t.required) ?? [];
1881
- return { props: n, required: r };
1857
+ let t = e.useOptions;
1858
+ return t && typeof t == "object" ? t : {};
1882
1859
  }
1883
- function me(e) {
1884
- return typeof e.handler == "function";
1885
- }
1886
- function Zt(e) {
1887
- var o;
1888
- const t = ((o = e.input) == null ? void 0 : o.properties) ?? {}, n = e["x-apigen-envelope"], r = [];
1889
- for (const i of Object.keys(t)) {
1890
- if (i === "data")
1891
- continue;
1892
- const s = (n == null ? void 0 : n[i]) ?? "adhd", a = Ft(s, i);
1893
- r.push({
1894
- field: i,
1895
- pluginId: s,
1896
- httpHeader: a,
1897
- mcpMetaKey: a,
1898
- cliFlag: Vt(s, i),
1899
- envVar: Ht(s, i)
1900
- });
1901
- }
1902
- return r;
1903
- }
1904
- function Qt(e, t, n, r) {
1905
- t === n || e.has(t) || e.set(t, { ...r, aliasOf: n });
1906
- }
1907
- function Xt(e, t) {
1908
- const n = /* @__PURE__ */ new Map(), { props: r } = Yt(e);
1909
- for (const [o, i] of Object.entries(r)) {
1910
- let s = "string";
1911
- Jt(i) ? s = "boolean" : (zt(i) || Wt(i)) && (s = "json");
1912
- const a = Gt(o), f = { camelKey: o, kind: "domain", valueKind: s };
1913
- n.set(a, f), Qt(n, o, a, f);
1914
- }
1915
- for (const o of t) {
1916
- const i = o.cliFlag.replace(/^--/, ""), s = {
1917
- camelKey: o.field,
1918
- kind: "envelope",
1919
- valueKind: "string",
1920
- envVar: o.envVar
1921
- };
1922
- n.set(i, s);
1923
- }
1924
- return n;
1925
- }
1926
- function Nn(e) {
1927
- const { op: t, schema: n, transport: r, projection: o } = e, i = qt(t, o), s = n ? Zt(n) : [], a = n ? Xt(n, s) : /* @__PURE__ */ new Map(), f = n ? pt(n).params : void 0, u = {
1928
- op: t,
1929
- transport: r,
1930
- http: i.http,
1931
- mcp: i.mcp,
1932
- cli: i.cli,
1933
- grpc: i.grpc,
1934
- params: f,
1935
- envelope: s,
1936
- cliFlags: a,
1937
- streaming: t.streaming,
1938
- isMount: me(t)
1939
- };
1940
- if (me(t)) {
1941
- const c = t.handler;
1942
- u.mountHandler = async (l) => c(l);
1943
- }
1944
- return u;
1945
- }
1946
- function En(e) {
1947
- const t = e.usePlugins;
1948
- return Array.isArray(t) ? t : [];
1949
- }
1950
- function _n(e) {
1951
- const t = e.exitCode;
1952
- return typeof t == "function" ? t : void 0;
1953
- }
1954
- function kn(e) {
1955
- const t = e.useOptions;
1956
- return t && typeof t == "object" ? t : {};
1957
- }
1958
- function en(e) {
1959
- return async function(n, r) {
1960
- const o = Object.assign(n, { data: n.domainArgs });
1961
- return await e.layer(o, r);
1962
- };
1963
- }
1964
- function In(e, t) {
1965
- var r;
1966
- const n = [];
1967
- for (const o of t) {
1968
- const i = (r = o.capabilities) == null ? void 0 : r.layer;
1969
- i && n.push(en(i));
1970
- }
1971
- return n.push(gt(e)), Ge(n);
1972
- }
1973
- function tn() {
1974
- const e = /* @__PURE__ */ new Map();
1975
- return {
1976
- get: (t) => e.get(t),
1977
- set: (t, n) => {
1978
- e.set(t, n);
1979
- }
1980
- };
1981
- }
1982
- const nn = {
1983
- input: {
1984
- type: "object",
1985
- properties: { data: { type: "object", properties: {} } }
1986
- },
1987
- output: {}
1988
- };
1989
- async function Cn(e, t, n, r) {
1990
- const o = {
1991
- ...n,
1992
- operation: { id: e.op.id },
1993
- ctx: new We()
1994
- };
1995
- if (e.isMount) {
1996
- const i = e.mountHandler;
1997
- if (!i)
1998
- throw new Error(
1999
- `apigen-engine-runtime/dispatchForPlan: OpPlan for mount op "${e.op.id}" has isMount=true but no mountHandler`
2000
- );
2001
- const s = {
2002
- operation: e.op,
2003
- data: n.domainArgs,
2004
- envelope: n.envelope,
2005
- ctx: tn(),
2006
- transport: e.transport,
2007
- signal: n.signal ?? new AbortController().signal
2008
- }, a = {
2009
- ...r,
2010
- fns: { [e.op.id]: () => i(s) },
2011
- schemas: { [e.op.id]: nn }
2012
- };
2013
- return t(e.op.id, o, a);
2014
- }
2015
- return t(e.op.id, o, r);
2016
- }
2017
- export {
2018
- St as BATCH_DEFAULT_CONCURRENCY,
2019
- Mt as BATCH_MAX_CONCURRENCY,
2020
- Ye as EventBus,
2021
- gn as InstanceRegistry,
2022
- We as LayerContext,
2023
- g as UnionCodecError,
2024
- ht as ValidateSchemasToken,
2025
- kt as WELL_KNOWN_TS_CODECS,
2026
- en as adaptCoreLayer,
2027
- at as assertNoSelfSubscription,
2028
- Xe as buildContext,
2029
- mn as buildFnTable,
2030
- An as buildMcpOutputSchema,
2031
- Nn as buildOpPlan,
2032
- $n as buildToolDescription,
2033
- yn as coerceQueryParams,
2034
- bt as collectWithPhase,
2035
- pn as createApiPackage,
2036
- Ge as createInvoker,
2037
- dn as createLogger,
2038
- In as createPackageInvoker,
2039
- wn as createStream,
2040
- jt as createUnionCodec,
2041
- He as dataParamNames,
2042
- ln as defineMiddleware,
2043
- pt as describeParams,
2044
- Je as dispatch,
2045
- Cn as dispatchForPlan,
2046
- bn as drainStream,
2047
- On as invokeBatch,
2048
- vt as isApiStream,
2049
- gt as makeValidateLayer,
2050
- Ve as needsEnvelopeField,
2051
- _n as readExitCodeHook,
2052
- kn as readUseOptions,
2053
- En as readUsePlugins,
2054
- vn as tsHostBinding,
2055
- hn as validateLayer,
2056
- Qe as wireObservers,
2057
- jn as wrapMcpStructuredContent
1860
+ function Kt(e) {
1861
+ return async function(t, n) {
1862
+ let r = Object.assign(t, { data: t.domainArgs });
1863
+ return await e.layer(r, n);
1864
+ };
1865
+ }
1866
+ function qt(e, t) {
1867
+ let n = [];
1868
+ for (let e of t) {
1869
+ let t = e.capabilities?.layer;
1870
+ t && n.push(Kt(t));
1871
+ }
1872
+ return n.push(Ye(e)), P(n);
1873
+ }
1874
+ //#endregion
1875
+ //#region src/lib/dispatch-for-plan.ts
1876
+ function Jt() {
1877
+ let e = /* @__PURE__ */ new Map();
1878
+ return {
1879
+ get: (t) => e.get(t),
1880
+ set: (t, n) => {
1881
+ e.set(t, n);
1882
+ }
1883
+ };
1884
+ }
1885
+ var Yt = {
1886
+ input: {
1887
+ type: "object",
1888
+ properties: { data: {
1889
+ type: "object",
1890
+ properties: {}
1891
+ } }
1892
+ },
1893
+ output: {}
2058
1894
  };
1895
+ async function Xt(e, t, n, r) {
1896
+ let i = {
1897
+ ...n,
1898
+ operation: { id: e.op.id },
1899
+ ctx: new N()
1900
+ };
1901
+ if (e.isMount) {
1902
+ let a = e.mountHandler;
1903
+ if (!a) throw Error(`apigen-engine-runtime/dispatchForPlan: OpPlan for mount op "${e.op.id}" has isMount=true but no mountHandler`);
1904
+ let o = {
1905
+ operation: e.op,
1906
+ data: n.domainArgs,
1907
+ envelope: n.envelope,
1908
+ ctx: Jt(),
1909
+ transport: e.transport,
1910
+ signal: n.signal ?? new AbortController().signal
1911
+ }, s = {
1912
+ ...r,
1913
+ fns: { [e.op.id]: () => a(o) },
1914
+ schemas: { [e.op.id]: Yt }
1915
+ };
1916
+ return t(e.op.id, i, s);
1917
+ }
1918
+ return t(e.op.id, i, r);
1919
+ }
1920
+ //#endregion
1921
+ export { vt as BATCH_DEFAULT_CONCURRENCY, yt as BATCH_MAX_CONCURRENCY, F as EventBus, Ze as InstanceRegistry, N as LayerContext, J as UnionCodecError, Je as ValidateSchemasToken, dt as WELL_KNOWN_TS_CODECS, Kt as adaptCoreLayer, V as assertNoSelfSubscription, L as buildContext, Ve as buildFnTable, gt as buildMcpOutputSchema, Ht as buildOpPlan, ht as buildToolDescription, Be as coerceQueryParams, et as collectWithPhase, Ne as createApiPackage, P as createInvoker, Pe as createLogger, qt as createPackageInvoker, Qe as createStream, at as createUnionCodec, xe as dataParamNames, Te as defineMiddleware, U as describeParams, we as dispatch, Xt as dispatchForPlan, $e as drainStream, wt as invokeBatch, tt as isApiStream, Ye as makeValidateLayer, be as needsEnvelopeField, Wt as readExitCodeHook, Gt as readUseOptions, Ut as readUsePlugins, mt as tsHostBinding, qe as validateLayer, I as wireObservers, _t as wrapMcpStructuredContent };