@voltro/ai 0.84.0 → 0.85.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,45 +1,46 @@
1
1
  import { c as e, o as t, r as n } from "./provider-BnemgE4D.js";
2
2
  import { NoObjectGeneratedError as r, Output as i, experimental_transcribe as a, generateObject as o, generateText as s, jsonSchema as c, stepCountIs as l, tool as u, wrapLanguageModel as d } from "ai";
3
- import { Context as f, Data as p, Effect as m, JSONSchema as h, Metric as g, MetricBoundaries as ee, Runtime as te, Schema as _, Stream as ne } from "effect";
4
- import { and as v, bigint as re, eq as y, generateId as b, gte as ie, id as x, integer as S, isNull as ae, json as C, queryFor as w, registerRetention as oe, retentionTtlMsFromEnv as se, table as T, text as E, timestamp as D } from "@voltro/database";
5
- import { Ajv as ce } from "ajv";
6
- import { Ajv2019 as le } from "ajv/dist/2019.js";
7
- import { Ajv2020 as ue } from "ajv/dist/2020.js";
8
- import de from "ajv-formats";
9
- import { createHash as fe } from "node:crypto";
3
+ import { Context as f, Data as p, Effect as m, Metric as h, Schema as g, Stream as _ } from "effect";
4
+ import { jsonSchemaOf as ee } from "@voltro/protocol";
5
+ import { and as v, bigint as te, eq as y, generateId as b, gte as ne, id as x, integer as S, isNull as re, json as C, queryFor as w, registerRetention as ie, retentionTtlMsFromEnv as ae, table as T, text as E, timestamp as D } from "@voltro/database";
6
+ import { Ajv as oe } from "ajv";
7
+ import { Ajv2019 as se } from "ajv/dist/2019.js";
8
+ import { Ajv2020 as ce } from "ajv/dist/2020.js";
9
+ import le from "ajv-formats";
10
+ import { createHash as ue } from "node:crypto";
10
11
  //#region src/tools.ts
11
- var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
12
+ var de = (e, t) => e(t), fe = Symbol.for("@voltro/ai/Tool"), pe = (e) => ({
12
13
  ...e,
13
- [me]: !0
14
- }), ge = (e) => typeof e == "object" && !!e && e[me] === !0, _e = (e) => {
14
+ [fe]: !0
15
+ }), me = (e) => typeof e == "object" && !!e && e[fe] === !0, he = (e) => {
15
16
  let t = e.default;
16
17
  if (typeof t != "function") return 0;
17
18
  let n = 0;
18
- for (let r of Object.values(e)) ge(r) && r.execute === void 0 && r.handler === void 0 && (r.handler = t, n += 1);
19
+ for (let r of Object.values(e)) me(r) && r.execute === void 0 && r.handler === void 0 && (r.handler = t, n += 1);
19
20
  return n;
20
- }, ve = (e, t, n) => {
21
- let r = t ?? te.defaultRuntime;
22
- return te.runPromise(r)(e, n === void 0 ? {} : { signal: n });
23
- }, ye = (e, t, n) => n ? e.pipe(m.timeoutFail({
21
+ }, ge = (e, t, n) => {
22
+ let r = t ?? f.empty();
23
+ return m.runPromiseWith(r)(e, n === void 0 ? {} : { signal: n });
24
+ }, _e = (e, t, n) => n ? e.pipe(m.timeoutOrElse({
24
25
  duration: `${n} millis`,
25
- onTimeout: () => /* @__PURE__ */ Error(`tool '${t}' timed out`)
26
- })) : e, be = (e, t, n) => {
27
- if (e.execute) return ve(ye(pe(e.execute, t), e.name, e.timeoutMs), n.runtime, n.abortSignal);
26
+ orElse: () => m.fail(/* @__PURE__ */ Error(`tool '${t}' timed out`))
27
+ })) : e, ve = (e, t, n) => {
28
+ if (e.execute) return ge(_e(de(e.execute, t), e.name, e.timeoutMs), n.runtime, n.abortSignal);
28
29
  if (e.handler) {
29
30
  let r = e.handler, i = r(t, n);
30
- return m.isEffect(i) ? ve(ye(i, e.name, e.timeoutMs), n.runtime, n.abortSignal) : i;
31
+ return m.isEffect(i) ? ge(_e(i, e.name, e.timeoutMs), n.runtime, n.abortSignal) : i;
31
32
  }
32
33
  return Promise.reject(/* @__PURE__ */ Error(`tool '${e.name}' has neither execute nor handler`));
33
- }, xe = (e) => {
34
- let t = h.make(e);
34
+ }, ye = (e) => {
35
+ let t = ee(e);
35
36
  return t && t.type === "object" ? t : {
36
37
  type: "object",
37
38
  properties: {},
38
39
  additionalProperties: !1
39
40
  };
40
- }, Se = (e) => {
41
+ }, be = (e) => {
41
42
  let t = e.inputJsonSchema;
42
- return t === void 0 ? xe(e.input) : t.type === "object" ? t : {
43
+ return t === void 0 ? ye(e.input) : t.type === "object" ? t : {
43
44
  type: "object",
44
45
  properties: {},
45
46
  additionalProperties: !0
@@ -48,7 +49,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
48
49
  let n = {};
49
50
  for (let [r, i] of Object.entries(e)) n[i.name || r] = u({
50
51
  description: i.description,
51
- inputSchema: c(Se(i)),
52
+ inputSchema: c(be(i)),
52
53
  execute: async (e, n) => {
53
54
  let r = t.abortSignal, a = n?.abortSignal, o = r !== void 0 && a !== void 0 && r !== a ? AbortSignal.any([r, a]) : a ?? r;
54
55
  o?.throwIfAborted();
@@ -56,39 +57,45 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
56
57
  ...t,
57
58
  ...n?.toolCallId === void 0 ? {} : { invocationId: n.toolCallId },
58
59
  ...o === void 0 ? {} : { abortSignal: o }
59
- }, c = await m.runPromise(_.decodeUnknown(i.input)(e));
60
+ }, c = await m.runPromise(g.decodeUnknownEffect(i.input)(e));
60
61
  o?.throwIfAborted();
61
- let l = await be(i, c, s);
62
- return i.output ? m.runPromise(_.decodeUnknown(i.output)(l)) : l;
62
+ let l = await ve(i, c, s);
63
+ return i.output ? m.runPromise(g.decodeUnknownEffect(i.output)(l)) : l;
63
64
  }
64
65
  });
65
66
  return n;
66
- }, k = class extends _.TaggedError()("AiBudgetExceeded", {
67
- tenantId: _.NullOr(_.String),
68
- limitUsd: _.Number,
69
- spentUsd: _.Number,
70
- attemptedUsd: _.Number
71
- }) {}, A = class extends _.TaggedError()("AiBudgetInvalidInput", {
72
- field: _.Literal("limitUsd", "addUsd", "since", "reservationId", "tenantId"),
73
- message: _.String
74
- }) {}, j = class extends _.TaggedError()("AiBudgetReservationConflict", {
75
- reason: _.Literal("identity-bound", "already-released"),
76
- message: _.String
67
+ }, k = class extends g.TaggedError()("AiBudgetExceeded", {
68
+ tenantId: g.NullOr(g.String),
69
+ limitUsd: g.Number,
70
+ spentUsd: g.Number,
71
+ attemptedUsd: g.Number
72
+ }) {}, A = class extends g.TaggedError()("AiBudgetInvalidInput", {
73
+ field: g.Literals([
74
+ "limitUsd",
75
+ "addUsd",
76
+ "since",
77
+ "reservationId",
78
+ "tenantId"
79
+ ]),
80
+ message: g.String
81
+ }) {}, j = class extends g.TaggedError()("AiBudgetReservationConflict", {
82
+ reason: g.Literals(["identity-bound", "already-released"]),
83
+ message: g.String
77
84
  }) {}, M = "_voltro_ai_budget", N = "_voltro_ai_budget_reservations", P = T(M, {
78
85
  id: x({ prefix: "aib" }),
79
86
  bucket: E().maxLength(191),
80
87
  tenantId: E().nullable(),
81
- reservedMicroUsd: re().default("0").narrowedFrom("integer"),
88
+ reservedMicroUsd: te().default("0").narrowedFrom("integer"),
82
89
  updatedAt: D().default("now")
83
- }).unique("byAiBudgetBucket", ["bucket"]), Ce = T(N, {
90
+ }).unique("byAiBudgetBucket", ["bucket"]), xe = T(N, {
84
91
  id: x({ prefix: "air" }),
85
92
  reservationId: E().maxLength(191),
86
93
  bucket: E().maxLength(191),
87
94
  tenantId: E().nullable(),
88
- amountMicroUsd: re(),
95
+ amountMicroUsd: te(),
89
96
  releasedAt: D().nullable(),
90
97
  updatedAt: D().default("now")
91
- }).unique("byAiBudgetReservation", ["reservationId"]), F = class extends Error {}, we = (e, t, n) => {
98
+ }).unique("byAiBudgetReservation", ["reservationId"]), F = class extends Error {}, Se = (e, t, n) => {
92
99
  if (!Number.isFinite(e) || e < 0) throw new A({
93
100
  field: t,
94
101
  message: `${t} must be finite and non-negative`
@@ -104,34 +111,34 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
104
111
  let t = String(e);
105
112
  if (!/^(0|[1-9][0-9]*)$/.test(t)) throw Error("AI budget counter is invalid; refusing to assume zero");
106
113
  return BigInt(t);
107
- }, Te = (e) => {
114
+ }, Ce = (e) => {
108
115
  if (typeof e != "string" || e.length === 0 || e.length > 191) throw new A({
109
116
  field: "reservationId",
110
117
  message: "AI budget reservationId must contain 1–191 characters"
111
118
  });
112
119
  return e;
113
- }, Ee = () => {
114
- let e = se(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760);
115
- for (let t of [M, N]) oe({
120
+ }, we = () => {
121
+ let e = ae(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760);
122
+ for (let t of [M, N]) ie({
116
123
  source: "plugin",
117
124
  table: t,
118
125
  timeColumn: "updatedAt",
119
126
  ttlMs: e
120
127
  });
121
- }, De = async (e, t) => {
128
+ }, Te = async (e, t) => {
122
129
  for (let n = 0;; n++) try {
123
130
  return await e.transactional(t);
124
131
  } catch (e) {
125
132
  if (!(e instanceof F) || n >= 31) throw e;
126
133
  await new Promise((e) => setTimeout(e, n + 1));
127
134
  }
128
- }, Oe = (e) => e instanceof k || e instanceof A || e instanceof j ? m.fail(e) : m.die(e), ke = (e) => m.tryPromise({
135
+ }, Ee = (e) => e instanceof k || e instanceof A || e instanceof j ? m.fail(e) : m.die(e), De = (e) => m.tryPromise({
129
136
  try: e,
130
137
  catch: (e) => e
131
- }).pipe(m.catchAll(Oe)), Ae = (e, t) => m.gen(function* () {
138
+ }).pipe(m.catch(Ee)), Oe = (e, t) => m.gen(function* () {
132
139
  let { limit: n, amount: r, tenantId: i, bucket: a, reservationId: o } = yield* m.try({
133
140
  try: () => {
134
- let e = we(t.limitUsd, "limitUsd", "down"), n = we(t.addUsd ?? 0, "addUsd", "up"), r = t.tenantId ?? null;
141
+ let e = Se(t.limitUsd, "limitUsd", "down"), n = Se(t.addUsd ?? 0, "addUsd", "up"), r = t.tenantId ?? null;
135
142
  if (t.since !== void 0 && (!(t.since instanceof Date) || !Number.isFinite(t.since.getTime()))) throw new A({
136
143
  field: "since",
137
144
  message: "AI budget since must be a valid Date"
@@ -146,15 +153,15 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
146
153
  amount: n,
147
154
  tenantId: r,
148
155
  bucket: i,
149
- reservationId: Te(t.reservationId ?? b({
156
+ reservationId: Ce(t.reservationId ?? b({
150
157
  kind: "typeid",
151
158
  prefix: "air"
152
159
  }, "_voltro_ai_budget_reservations"))
153
160
  };
154
161
  },
155
162
  catch: (e) => e
156
- }).pipe(m.catchAll(Oe));
157
- Ee();
163
+ }).pipe(m.catch(Ee));
164
+ we();
158
165
  let s = async (e) => {
159
166
  let [t] = await e.query(w(P).where(y("bucket", a)).descriptor);
160
167
  if (t && t.tenantId !== i) throw Error("AI budget bucket belongs to a different tenant");
@@ -167,10 +174,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
167
174
  attemptedUsd: t.addUsd ?? 0
168
175
  });
169
176
  };
170
- return r === 0n ? yield* ke(async () => {
177
+ return r === 0n ? yield* De(async () => {
171
178
  let t = await s(e);
172
179
  return c(t ? I(t.reservedMicroUsd) : 0n), null;
173
- }) : yield* ke(() => De(e, async (e) => {
180
+ }) : yield* De(() => Te(e, async (e) => {
174
181
  let t = b({
175
182
  kind: "typeid",
176
183
  prefix: "air"
@@ -225,19 +232,19 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
225
232
  status: "reserved"
226
233
  };
227
234
  }));
228
- }), je = (e, t) => m.gen(function* () {
235
+ }), ke = (e, t) => m.gen(function* () {
229
236
  return yield* m.try({
230
- try: () => Te(t.reservationId),
237
+ try: () => Ce(t.reservationId),
231
238
  catch: (e) => e
232
- }).pipe(m.catchAll((e) => e instanceof A ? m.fail(e) : m.die(e))), yield* m.promise(() => De(e, async (e) => {
233
- let n = w(Ce).where(v(y("reservationId", t.reservationId), t.tenantId === null ? ae("tenantId") : y("tenantId", t.tenantId))).descriptor, [r] = await e.query(n);
239
+ }).pipe(m.catch((e) => e instanceof A ? m.fail(e) : m.die(e))), yield* m.promise(() => Te(e, async (e) => {
240
+ let n = w(xe).where(v(y("reservationId", t.reservationId), t.tenantId === null ? re("tenantId") : y("tenantId", t.tenantId))).descriptor, [r] = await e.query(n);
234
241
  if (!r) return "not-found";
235
242
  if (r.releasedAt !== null) return "already-released";
236
243
  let i = /* @__PURE__ */ new Date();
237
244
  if (await e.updateMany("_voltro_ai_budget_reservations", {
238
245
  releasedAt: i,
239
246
  updatedAt: i
240
- }, { where: v(y("id", r.id), ae("releasedAt")) }) !== 1) {
247
+ }, { where: v(y("id", r.id), re("releasedAt")) }) !== 1) {
241
248
  let [t] = await e.query(n);
242
249
  if (t !== void 0 && t.releasedAt !== null) return "already-released";
243
250
  throw new F("AI budget release raced; retry");
@@ -252,7 +259,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
252
259
  }, { where: v(y("id", a.id), y("reservedMicroUsd", a.reservedMicroUsd)) }) !== 1) throw new F("AI budget counter changed; discard this release and retry");
253
260
  return "released";
254
261
  }));
255
- }), Me = {
262
+ }), Ae = {
256
263
  "claude-opus-4-8": {
257
264
  inputPer1M: 15,
258
265
  outputPer1M: 75
@@ -297,21 +304,21 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
297
304
  inputPer1M: .1,
298
305
  outputPer1M: .4
299
306
  }
300
- }, L = {}, Ne = (e) => {
307
+ }, L = {}, je = (e) => {
301
308
  let t = L;
302
309
  return L = { ...e }, t;
303
- }, Pe = () => ({
304
- ...Me,
310
+ }, Me = () => ({
311
+ ...Ae,
305
312
  ...L
306
- }), Fe = (e) => {
307
- let t = Pe();
313
+ }), Ne = (e) => {
314
+ let t = Me();
308
315
  if (t[e] !== void 0) return t[e];
309
316
  let n = e.indexOf("/");
310
317
  if (n > 0) {
311
318
  let r = e.slice(n + 1);
312
319
  if (t[r] !== void 0) return t[r];
313
320
  }
314
- }, R = (e) => typeof e.unit == "string", Ie = (e) => R(e) ? {
321
+ }, R = (e) => typeof e.unit == "string", Pe = (e) => R(e) ? {
315
322
  unit: e.unit,
316
323
  input: e.input ?? 0,
317
324
  output: e.output ?? 0
@@ -320,7 +327,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
320
327
  input: e.inputTokens ?? 0,
321
328
  output: e.outputTokens ?? 0
322
329
  }, z = (e, t) => {
323
- let { unit: n, input: r, output: i } = Ie(e);
330
+ let { unit: n, input: r, output: i } = Pe(e);
324
331
  if (t.actualCostUsd !== void 0) {
325
332
  if (!Number.isFinite(t.actualCostUsd) || t.actualCostUsd < 0) throw RangeError("estimateCostUsd: actualCostUsd must be a finite, non-negative USD amount.");
326
333
  let e = r + i, a = e > 0 ? r / e : 0;
@@ -343,7 +350,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
343
350
  costSource: "unpriced",
344
351
  unit: n
345
352
  };
346
- let a = t.price ?? Fe(t.model) ?? {
353
+ let a = t.price ?? Ne(t.model) ?? {
347
354
  inputPer1M: 0,
348
355
  outputPer1M: 0
349
356
  }, o = r / 1e6 * a.inputPer1M, s = i / 1e6 * a.outputPer1M;
@@ -356,7 +363,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
356
363
  costSource: "estimated",
357
364
  unit: n
358
365
  };
359
- }, B = "_voltro_ai_usage", Le = T(B, {
366
+ }, B = "_voltro_ai_usage", Fe = T(B, {
360
367
  id: x({ prefix: "aiu" }),
361
368
  tenantId: E().nullable(),
362
369
  provider: E(),
@@ -375,26 +382,26 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
375
382
  attempt: S().nullable(),
376
383
  receiptKey: E().maxLength(191).backfill((e) => `aiu-pre-receipt-${String(e.id)}`),
377
384
  calledAt: D().default("now")
378
- }).index(["tenantId"]).index(["promptDigest"]).unique("byAiUsageReceipt", ["receiptKey"]), Re = (e) => {
385
+ }).index(["tenantId"]).index(["promptDigest"]).unique("byAiUsageReceipt", ["receiptKey"]), Ie = (e) => {
379
386
  let t = typeof e == "string" ? e.trim() : void 0, n = typeof e == "number" ? e : t !== void 0 && /^(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(t) ? Number(t) : NaN;
380
387
  return Number.isFinite(n) && n >= 0 ? n : void 0;
381
- }, ze = (e) => {
382
- let t = e?.gateway, n = Re(t?.cost);
388
+ }, Le = (e) => {
389
+ let t = e?.gateway, n = Ie(t?.cost);
383
390
  if (n === void 0) return;
384
391
  let r = (Array.isArray(t?.routing?.modelAttempts) ? t.routing.modelAttempts : []).findLast((e) => e?.success === !0);
385
392
  if ((Array.isArray(r?.providerAttempts) ? r.providerAttempts : []).findLast((e) => e?.success === !0)?.credentialType !== "byok") return n;
386
- let i = Re(t?.marketCost);
393
+ let i = Ie(t?.marketCost);
387
394
  return i === void 0 ? void 0 : n + i;
388
- }, Be = class extends p.TaggedError("AiUsageReceiptConflict") {
395
+ }, Re = class extends p.TaggedError("AiUsageReceiptConflict") {
389
396
  get message() {
390
397
  let e = Object.keys(this.recorded).filter((e) => this.recorded[e] !== this.attempted[e]).map((e) => `${e} ${String(this.recorded[e])} → ${String(this.attempted[e])}`);
391
398
  return `AI usage receipt ${this.receiptKey} was recorded again with different values (${e.join(", ")}). A replay of the same paid call has the same values; the row keeps what was recorded first.`;
392
399
  }
393
- }, Ve = class extends p.TaggedError("AiUsageLedgerError") {
400
+ }, ze = class extends p.TaggedError("AiUsageLedgerError") {
394
401
  get message() {
395
402
  return `AI usage receipt ${this.receiptKey} could not be written: ${this.cause instanceof Error ? this.cause.message : String(this.cause)}`;
396
403
  }
397
- }, He = (e) => ({
404
+ }, Be = (e) => ({
398
405
  tenantId: e.tenantId ?? null,
399
406
  provider: String(e.provider),
400
407
  model: String(e.model),
@@ -402,11 +409,11 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
402
409
  inputTokens: Number(e.inputTokens),
403
410
  outputTokens: Number(e.outputTokens),
404
411
  costMicroUsd: Number(e.costMicroUsd)
405
- }), Ue = (e, t) => (oe({
412
+ }), Ve = (e, t) => (ie({
406
413
  source: "plugin",
407
414
  table: B,
408
415
  timeColumn: "calledAt",
409
- ttlMs: se(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760)
416
+ ttlMs: ae(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760)
410
417
  }), m.gen(function* () {
411
418
  let n = z(t.usage, {
412
419
  model: t.model,
@@ -435,7 +442,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
435
442
  calledAt: t.calledAt ?? /* @__PURE__ */ new Date()
436
443
  }, a = yield* m.tryPromise({
437
444
  try: () => e.insertIgnoreWithOutcome(B, i, { conflictColumns: ["receiptKey"] }),
438
- catch: (e) => new Ve({
445
+ catch: (e) => new ze({
439
446
  receiptKey: String(r),
440
447
  cause: e
441
448
  })
@@ -444,16 +451,16 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
444
451
  ...n,
445
452
  receipt: "recorded"
446
453
  };
447
- let o = He(a.row), s = He(i);
448
- return Object.keys(o).some((e) => o[e] !== s[e]) ? yield* m.fail(new Be({
454
+ let o = Be(a.row), s = Be(i);
455
+ return Object.keys(o).some((e) => o[e] !== s[e]) ? yield* m.fail(new Re({
449
456
  receiptKey: String(r),
450
457
  recorded: o,
451
458
  attempted: s
452
459
  })) : {
453
- ...We(a.row, n),
460
+ ...He(a.row, n),
454
461
  receipt: "replayed"
455
462
  };
456
- })), We = (e, t) => {
463
+ })), He = (e, t) => {
457
464
  let n = Number(e.costMicroUsd) / 1e6;
458
465
  return {
459
466
  ...t,
@@ -462,7 +469,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
462
469
  totalCostUsd: n,
463
470
  costSource: e.costSource ?? t.costSource
464
471
  };
465
- }, Ge = (e, t) => Ue(e, t).pipe(m.asVoid, m.catchAllCause((e) => m.logError("AI usage NOT booked as recorded — the call itself succeeded and was returned", e).pipe(m.annotateLogs({
472
+ }, Ue = (e, t) => Ve(e, t).pipe(m.asVoid, m.catchCause((e) => m.logError("AI usage NOT booked as recorded — the call itself succeeded and was returned", e).pipe(m.annotateLogs({
466
473
  scope: "voltro:ai:usage",
467
474
  provider: t.provider,
468
475
  model: t.model,
@@ -480,63 +487,68 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
480
487
  attempt: t.attempt ?? 0
481
488
  },
482
489
  ...t.actualCostUsd === void 0 ? {} : { actualCostUsd: t.actualCostUsd }
483
- })))), Ke = (e, t = {}) => m.promise(async () => {
490
+ })))), We = (e, t = {}) => m.promise(async () => {
484
491
  let n = [
485
492
  ...t.tenantId === void 0 ? [] : [y("tenantId", t.tenantId)],
486
- ...t.since === void 0 ? [] : [ie("calledAt", t.since)],
493
+ ...t.since === void 0 ? [] : [ne("calledAt", t.since)],
487
494
  ...t.promptDigest === void 0 ? [] : [y("promptDigest", t.promptDigest)]
488
- ], r = w(Le), i = n.length === 0 ? r : r.where(n.length === 1 ? n[0] : v(...n));
495
+ ], r = w(Fe), i = n.length === 0 ? r : r.where(n.length === 1 ? n[0] : v(...n));
489
496
  return (await e.query(i.descriptor)).reduce((e, t) => e + (Number(t.costMicroUsd) || 0), 0) / 1e6;
490
- }), qe = ee.exponential({
491
- start: .001,
492
- factor: 2,
493
- count: 16
494
- }), V = g.counter("voltro_ai_calls_total", { description: "Total AI provider calls (generate + stream)." }), Je = g.counter("voltro_ai_call_errors_total", { description: "AI provider calls that errored." }), H = g.histogram("voltro_ai_call_duration_seconds", qe, "AI provider call duration in seconds."), Ye = g.counter("voltro_ai_input_tokens_total", {
497
+ }), Ge = Array.from({ length: 16 }, (e, t) => .001 * 2 ** t), V = h.counter("voltro_ai_calls_total", { description: "Total AI provider calls (generate + stream)." }), Ke = h.counter("voltro_ai_call_errors_total", { description: "AI provider calls that errored." }), H = h.histogram("voltro_ai_call_duration_seconds", {
498
+ boundaries: Ge,
499
+ description: "AI provider call duration in seconds."
500
+ }), qe = h.counter("voltro_ai_input_tokens_total", {
495
501
  description: "Total prompt (input) tokens across AI calls.",
496
502
  incremental: !0
497
- }), Xe = g.counter("voltro_ai_output_tokens_total", {
503
+ }), Je = h.counter("voltro_ai_output_tokens_total", {
498
504
  description: "Total completion (output) tokens across AI calls.",
499
505
  incremental: !0
500
- }), Ze = g.counter("voltro_ai_cost_microusd_total", {
506
+ }), Ye = h.counter("voltro_ai_cost_microusd_total", {
501
507
  description: "Total estimated AI spend in micro-USD (USD × 1e6).",
502
508
  incremental: !0
503
509
  }), U = (e, t) => ({
504
510
  provider: e.name,
505
511
  model: e.model ?? e.name,
506
512
  operation: t
507
- }), W = (e, t, n) => e.pipe(m.tagMetrics("provider", t.provider), m.tagMetrics("model", t.model), m.tagMetrics("operation", t.operation), m.tagMetrics("status", n)), Qe = (e, t) => {
513
+ }), W = (e, t, n) => m.updateService(e, h.CurrentMetricAttributes, (e) => ({
514
+ ...e,
515
+ provider: t.provider,
516
+ model: t.model,
517
+ operation: t.operation,
518
+ status: n
519
+ })), Xe = (e, t) => {
508
520
  let n = t.inputTokens ?? 0, r = t.outputTokens ?? 0, i = z({
509
521
  inputTokens: n,
510
522
  outputTokens: r
511
523
  }, { model: e.model }), a = Math.round(i.totalCostUsd * 1e6);
512
524
  return m.all([
513
- W(g.incrementBy(Ye, n), e, "ok"),
514
- W(g.incrementBy(Xe, r), e, "ok"),
515
- W(g.incrementBy(Ze, a), e, "ok")
525
+ W(h.update(qe, n), e, "ok"),
526
+ W(h.update(Je, r), e, "ok"),
527
+ W(h.update(Ye, a), e, "ok")
516
528
  ], { discard: !0 });
517
529
  }, G = (e, t, n) => m.gen(function* () {
518
530
  let r = yield* m.clockWith((e) => e.currentTimeMillis);
519
- return yield* n.pipe(m.tap((n) => Qe(e, t(n))), m.tap(() => W(g.increment(V), e, "ok").pipe(m.zipRight(m.clockWith((e) => e.currentTimeMillis).pipe(m.flatMap((t) => W(g.update(H, (t - r) / 1e3), e, "ok")))))), m.tapError(() => W(g.increment(V), e, "error").pipe(m.zipRight(W(g.increment(Je), e, "error")), m.zipRight(m.clockWith((e) => e.currentTimeMillis).pipe(m.flatMap((t) => W(g.update(H, (t - r) / 1e3), e, "error")))))), m.withSpan("voltro.ai.call", { attributes: {
531
+ return yield* n.pipe(m.tap((n) => Xe(e, t(n))), m.tap(() => W(h.update(V, 1), e, "ok").pipe(m.andThen(m.clockWith((e) => e.currentTimeMillis).pipe(m.flatMap((t) => W(h.update(H, (t - r) / 1e3), e, "ok")))))), m.tapError(() => W(h.update(V, 1), e, "error").pipe(m.andThen(W(h.update(Ke, 1), e, "error")), m.andThen(m.clockWith((e) => e.currentTimeMillis).pipe(m.flatMap((t) => W(h.update(H, (t - r) / 1e3), e, "error")))))), m.withSpan("voltro.ai.call", { attributes: {
520
532
  "ai.provider": e.provider,
521
533
  "ai.model": e.model,
522
534
  "ai.operation": e.operation
523
535
  } }));
524
- }), $e = (e, t) => {
536
+ }), Ze = (e, t) => {
525
537
  let n = Date.now();
526
- return t.pipe(ne.tap((t) => t._tag === "done" ? W(g.increment(V), e, "ok").pipe(m.zipRight(W(g.update(H, (Date.now() - n) / 1e3), e, "ok")), m.zipRight(Qe(e, t.usage ?? {}))) : t._tag === "error" ? W(g.increment(V), e, "error").pipe(m.zipRight(W(g.increment(Je), e, "error")), m.zipRight(W(g.update(H, (Date.now() - n) / 1e3), e, "error"))) : m.void), ne.withSpan("voltro.ai.call", { attributes: {
538
+ return t.pipe(_.tap((t) => t._tag === "done" ? W(h.update(V, 1), e, "ok").pipe(m.andThen(W(h.update(H, (Date.now() - n) / 1e3), e, "ok")), m.andThen(Xe(e, t.usage ?? {}))) : t._tag === "error" ? W(h.update(V, 1), e, "error").pipe(m.andThen(W(h.update(Ke, 1), e, "error")), m.andThen(W(h.update(H, (Date.now() - n) / 1e3), e, "error"))) : m.void), _.withSpan("voltro.ai.call", { attributes: {
527
539
  "ai.provider": e.provider,
528
540
  "ai.model": e.model,
529
541
  "ai.operation": e.operation
530
542
  } }));
531
- }, et = (e) => {
543
+ }, Qe = (e) => {
532
544
  let t = structuredClone(e), n = {
533
545
  strict: !0,
534
546
  strictTypes: !1,
535
547
  strictTuples: !1,
536
548
  strictRequired: !1,
537
549
  allowUnionTypes: !0
538
- }, r = t.$schema, i = r === "https://json-schema.org/draft/2020-12/schema" ? new ue(n) : r === "https://json-schema.org/draft/2019-09/schema" ? new le(n) : new ce(n);
539
- de(i);
550
+ }, r = t.$schema, i = r === "https://json-schema.org/draft/2020-12/schema" ? new ce(n) : r === "https://json-schema.org/draft/2019-09/schema" ? new se(n) : new oe(n);
551
+ le(i);
540
552
  let a = i.compile(t);
541
553
  if ("$async" in a && a.$async === !0) throw Error("asynchronous output schemas are not supported");
542
554
  return c(t, { validate: (e) => a(e) ? {
@@ -546,29 +558,33 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
546
558
  success: !1,
547
559
  error: /* @__PURE__ */ Error("model output does not satisfy the declared JSON Schema")
548
560
  } });
549
- }, tt = _.Number.pipe(_.int(), _.nonNegative(), _.lessThanOrEqualTo(2 ** 53 - 1)), nt = _.Struct({
550
- provider: _.String,
551
- model: _.String,
552
- usage: _.Struct({
553
- inputTokens: _.optional(tt),
554
- outputTokens: _.optional(tt),
555
- totalTokens: _.optional(tt),
556
- completeness: _.Literal("complete", "partial", "unknown")
561
+ }, $e = g.Number.pipe(g.check(g.isInt()), g.check(g.isGreaterThanOrEqualTo(0)), g.check(g.isLessThanOrEqualTo(2 ** 53 - 1))), et = g.Struct({
562
+ provider: g.String,
563
+ model: g.String,
564
+ usage: g.Struct({
565
+ inputTokens: g.optional($e),
566
+ outputTokens: g.optional($e),
567
+ totalTokens: g.optional($e),
568
+ completeness: g.Literals([
569
+ "complete",
570
+ "partial",
571
+ "unknown"
572
+ ])
557
573
  }),
558
- cost: _.Union(_.Struct({
559
- status: _.Literal("actual"),
560
- source: _.Literal("gateway"),
561
- usd: _.Number.pipe(_.finite(), _.nonNegative())
562
- }), _.Struct({
563
- status: _.Literal("unknown"),
564
- source: _.Literal("unreported")
565
- }))
566
- }), rt = (e) => typeof e == "number" && Number.isSafeInteger(e) && e >= 0 ? e : void 0, K = (e, t, n, r) => {
567
- let i = typeof n == "object" && n ? n : {}, a = rt("inputTokens" in i ? i.inputTokens : void 0), o = rt("outputTokens" in i ? i.outputTokens : void 0), s = rt("totalTokens" in i ? i.totalTokens : void 0), c = [
574
+ cost: g.Union([g.Struct({
575
+ status: g.Literal("actual"),
576
+ source: g.Literal("gateway"),
577
+ usd: g.Number.pipe(g.check(g.isFinite()), g.check(g.isGreaterThanOrEqualTo(0)))
578
+ }), g.Struct({
579
+ status: g.Literal("unknown"),
580
+ source: g.Literal("unreported")
581
+ })])
582
+ }), tt = (e) => typeof e == "number" && Number.isSafeInteger(e) && e >= 0 ? e : void 0, K = (e, t, n, r) => {
583
+ let i = typeof n == "object" && n ? n : {}, a = tt("inputTokens" in i ? i.inputTokens : void 0), o = tt("outputTokens" in i ? i.outputTokens : void 0), s = tt("totalTokens" in i ? i.totalTokens : void 0), c = [
568
584
  a,
569
585
  o,
570
586
  s
571
- ].filter((e) => e !== void 0).length, l = ze(r);
587
+ ].filter((e) => e !== void 0).length, l = Le(r);
572
588
  return {
573
589
  provider: e,
574
590
  model: t,
@@ -587,7 +603,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
587
603
  usd: l
588
604
  }
589
605
  };
590
- }, it = (e, t) => {
606
+ }, nt = (e, t) => {
591
607
  let n = [];
592
608
  return {
593
609
  model: d({
@@ -629,12 +645,12 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
629
645
  }, a === void 0 ? void 0 : { gateway: { cost: a } });
630
646
  }
631
647
  };
632
- }, q = class extends _.TaggedError()("AiError", {
633
- reason: _.Literal("generation", "decode"),
634
- message: _.String,
635
- evidence: _.optional(nt)
648
+ }, q = class extends g.TaggedError()("AiError", {
649
+ reason: g.Literals(["generation", "decode"]),
650
+ message: g.String,
651
+ evidence: g.optional(et)
636
652
  }) {}, J = (e, t = {}) => {
637
- let n = String(e?.message ?? e), r = at(e), i = new q({
653
+ let n = String(e?.message ?? e), r = rt(e), i = new q({
638
654
  reason: "generation",
639
655
  message: `${t.prefix === void 0 ? "" : `${t.prefix}: `}${n}${r.length > 0 ? ` (${r.join(" <- ")})` : ""}`,
640
656
  ...t.evidence === void 0 ? {} : { evidence: t.evidence }
@@ -645,29 +661,29 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
645
661
  configurable: !0,
646
662
  writable: !0
647
663
  }), i;
648
- }, at = (e) => {
664
+ }, rt = (e) => {
649
665
  let t = [], n = e;
650
666
  for (let e = 0; e < 8 && typeof n == "object" && n; e++) {
651
667
  let e = n;
652
668
  typeof e.code == "string" && e.code.length > 0 && !t.includes(e.code) && t.push(e.code), n = typeof e.lastError == "object" && e.lastError !== null ? e.lastError : e.cause;
653
669
  }
654
670
  return t;
655
- }, ot = (e, t, n) => {
671
+ }, it = (e, t, n) => {
656
672
  let r = [e, ...t ?? []], i = (e) => n(r[e]).pipe(m.catchTag("AiError", (t) => t.reason === "generation" && e + 1 < r.length ? i(e + 1) : m.fail(t)));
657
673
  return i(0);
658
- }, st = (e) => m.gen(function* () {
674
+ }, at = (e) => m.gen(function* () {
659
675
  let r = e.messages === void 0 ? e.prompt === void 0 ? void 0 : { prompt: e.prompt } : { messages: [...e.messages] };
660
676
  if (r === void 0 || e.prompt !== void 0 && e.messages !== void 0) return yield* m.fail(new q({
661
677
  reason: "generation",
662
678
  message: "generateText: supply exactly one of prompt or messages"
663
679
  }));
664
- let i = e.tools === void 0 ? void 0 : yield* m.runtime(), a = e.tools === void 0 || i === void 0 ? void 0 : O(e.tools, {
680
+ let i = e.tools === void 0 ? void 0 : yield* m.context(), a = e.tools === void 0 || i === void 0 ? void 0 : O(e.tools, {
665
681
  store: void 0,
666
682
  ...e.toolContext,
667
683
  ...e.abortSignal === void 0 ? {} : { abortSignal: e.abortSignal },
668
684
  runtime: i
669
685
  });
670
- return yield* ot(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateText"), (e) => e.usage, m.tryPromise({
686
+ return yield* it(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateText"), (e) => e.usage, m.tryPromise({
671
687
  try: async () => {
672
688
  let i = t(n), o = await s({
673
689
  model: i,
@@ -695,8 +711,8 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
695
711
  },
696
712
  catch: (e) => J(e)
697
713
  })));
698
- }), ct = (e, t) => m.gen(function* () {
699
- let n = h.make(e);
714
+ }), ot = (e, t) => m.gen(function* () {
715
+ let n = ee(e);
700
716
  if (n?.type !== "object") {
701
717
  let e = n?.anyOf === void 0 ? `type:${JSON.stringify(n?.type ?? null)}` : "anyOf (e.g. Schema.Struct({}) or a union)";
702
718
  return yield* m.fail(new q({
@@ -705,14 +721,14 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
705
721
  }));
706
722
  }
707
723
  return n;
708
- }), lt = (e, t) => ct(e, t), ut = (e) => ot(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateObject"), (e) => e.usage, m.gen(function* () {
724
+ }), st = (e, t) => ot(e, t), ct = (e) => it(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateObject"), (e) => e.usage, m.gen(function* () {
709
725
  let i = yield* m.try({
710
- try: () => et(e.jsonSchema),
726
+ try: () => Qe(e.jsonSchema),
711
727
  catch: () => new q({
712
728
  reason: "decode",
713
729
  message: "generateObjectJson: invalid or unsupported output JSON Schema; no provider call was made"
714
730
  })
715
- }), a = it(n.name, t(n)), s = a.model, c = yield* m.tryPromise({
731
+ }), a = nt(n.name, t(n)), s = a.model, c = yield* m.tryPromise({
716
732
  try: () => o({
717
733
  model: s,
718
734
  schema: i,
@@ -741,9 +757,9 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
741
757
  },
742
758
  providerMetadata: c.providerMetadata
743
759
  };
744
- }))), dt = (e) => m.gen(function* () {
745
- let t = yield* ut({
746
- jsonSchema: yield* ct(e.schema, "generateObject"),
760
+ }))), lt = (e) => m.gen(function* () {
761
+ let t = yield* ct({
762
+ jsonSchema: yield* ot(e.schema, "generateObject"),
747
763
  prompt: e.prompt,
748
764
  ...e.system === void 0 ? {} : { system: e.system },
749
765
  ...e.provider === void 0 ? {} : { provider: e.provider },
@@ -751,7 +767,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
751
767
  ...e.maxTokens === void 0 ? {} : { maxTokens: e.maxTokens },
752
768
  ...e.temperature === void 0 ? {} : { temperature: e.temperature },
753
769
  ...e.providerOptions === void 0 ? {} : { providerOptions: e.providerOptions }
754
- }), n = yield* _.decodeUnknown(e.schema)(t.object).pipe(m.mapError(() => new q({
770
+ }), n = yield* g.decodeUnknownEffect(e.schema)(t.object).pipe(m.mapError(() => new q({
755
771
  reason: "decode",
756
772
  message: "model output failed schema decode",
757
773
  evidence: t.evidence
@@ -760,15 +776,15 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
760
776
  ...t,
761
777
  object: n
762
778
  };
763
- }), ft = (e) => m.gen(function* () {
764
- let a = yield* m.runtime(), o = yield* ct(e.schema, "generateObjectWithTools"), u = {
779
+ }), ut = (e) => m.gen(function* () {
780
+ let a = yield* m.context(), o = yield* ot(e.schema, "generateObjectWithTools"), u = {
765
781
  store: void 0,
766
782
  ...e.toolContext,
767
783
  ...e.abortSignal === void 0 ? {} : { abortSignal: e.abortSignal },
768
784
  runtime: a
769
785
  }, d = O(e.tools, u);
770
- return yield* ot(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateObjectWithTools"), (e) => e.usage, m.gen(function* () {
771
- let a = it(n.name, t(n)), u = a.model, f = yield* m.tryPromise({
786
+ return yield* it(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateObjectWithTools"), (e) => e.usage, m.gen(function* () {
787
+ let a = nt(n.name, t(n)), u = a.model, f = yield* m.tryPromise({
772
788
  try: () => s({
773
789
  model: u,
774
790
  prompt: e.prompt,
@@ -789,7 +805,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
789
805
  prefix: "structured provider call failed",
790
806
  evidence: K(n.name, u.modelId, void 0)
791
807
  })
792
- }), p = f.totalUsage, h = a.evidence(p), g = yield* m.try({
808
+ }), p = f.totalUsage, h = a.evidence(p), _ = yield* m.try({
793
809
  try: () => f.output,
794
810
  catch: () => new q({
795
811
  reason: "decode",
@@ -798,7 +814,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
798
814
  })
799
815
  });
800
816
  return {
801
- object: yield* _.decodeUnknown(e.schema)(g).pipe(m.mapError(() => new q({
817
+ object: yield* g.decodeUnknownEffect(e.schema)(_).pipe(m.mapError(() => new q({
802
818
  reason: "decode",
803
819
  message: "model output failed schema decode",
804
820
  evidence: h
@@ -812,10 +828,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
812
828
  providerMetadata: f.providerMetadata
813
829
  };
814
830
  })));
815
- }), pt = class extends f.Tag("@voltro/AiService")() {}, mt = {
816
- generateText: (e) => st(e),
817
- generateObject: (e) => dt(e)
818
- }, ht = async (e, t) => {
831
+ }), dt = class extends f.Service()("@voltro/AiService") {}, ft = {
832
+ generateText: (e) => at(e),
833
+ generateObject: (e) => lt(e)
834
+ }, pt = async (e, t) => {
819
835
  if ("bytes" in e) return {
820
836
  audio: e.bytes,
821
837
  mediaType: e.mediaType
@@ -835,7 +851,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
835
851
  audio: n,
836
852
  mediaType: r
837
853
  };
838
- }, gt = (e) => {
854
+ }, mt = (e) => {
839
855
  if (!Array.isArray(e) || e.length === 0) return;
840
856
  let t = [];
841
857
  for (let n of e) {
@@ -850,16 +866,16 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
850
866
  });
851
867
  }
852
868
  return t.length > 0 ? t : void 0;
853
- }, _t = (t) => {
869
+ }, ht = (t) => {
854
870
  let r = t.provider ?? n();
855
871
  return G(U(r, "transcribe"), () => ({}), m.tryPromise({
856
872
  try: async () => {
857
- let { audio: n, mediaType: i } = await ht(t.source, t.resolveRef), o = await a({
873
+ let { audio: n, mediaType: i } = await pt(t.source, t.resolveRef), o = await a({
858
874
  model: e(r),
859
875
  audio: n,
860
876
  ...i === void 0 ? {} : { mediaType: i },
861
877
  ...t.providerOptions === void 0 ? {} : { providerOptions: t.providerOptions }
862
- }), s = t.timestamps === "none" ? void 0 : gt(o.segments);
878
+ }), s = t.timestamps === "none" ? void 0 : mt(o.segments);
863
879
  return {
864
880
  text: o.text,
865
881
  ...o.language === void 0 ? {} : { language: o.language },
@@ -870,31 +886,31 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
870
886
  },
871
887
  catch: (e) => J(e)
872
888
  }));
873
- }, vt = (e) => `sha256:${fe("sha256").update(e).digest("hex").slice(0, 16)}`, yt = class extends _.TaggedError()("PromptRenderError", {
874
- promptId: _.String,
875
- missing: _.Array(_.String),
876
- message: _.String
877
- }) {}, bt = /\{\{\s*([A-Za-z0-9_.]+)\s*\}\}/g, xt = (e) => {
889
+ }, gt = (e) => `sha256:${ue("sha256").update(e).digest("hex").slice(0, 16)}`, _t = class extends g.TaggedError()("PromptRenderError", {
890
+ promptId: g.String,
891
+ missing: g.Array(g.String),
892
+ message: g.String
893
+ }) {}, vt = /\{\{\s*([A-Za-z0-9_.]+)\s*\}\}/g, yt = (e) => {
878
894
  let t = /* @__PURE__ */ new Set();
879
- for (let n of e.matchAll(bt)) {
895
+ for (let n of e.matchAll(vt)) {
880
896
  let e = n[1];
881
897
  e !== void 0 && t.add(e);
882
898
  }
883
899
  return [...t];
884
- }, St = (e, t) => vt(`${e.length}:${e}${(t ?? "").length}:${t ?? ""}`), Ct = (e) => {
885
- let t = St(e.template, e.system), n = xt(e.template);
900
+ }, bt = (e, t) => gt(`${e.length}:${e}${(t ?? "").length}:${t ?? ""}`), xt = (e) => {
901
+ let t = bt(e.template, e.system), n = yt(e.template);
886
902
  return {
887
903
  ...e,
888
904
  digest: t,
889
905
  variables: n,
890
906
  render: (r = {}) => m.suspend(() => {
891
907
  let i = n.filter((e) => r[e] === void 0);
892
- if (i.length > 0) return m.fail(new yt({
908
+ if (i.length > 0) return m.fail(new _t({
893
909
  promptId: e.id,
894
910
  missing: i,
895
911
  message: `prompt '${e.id}' needs ${i.map((e) => `{{${e}}}`).join(", ")}, which ${i.length === 1 ? "was" : "were"} not supplied.`
896
912
  }));
897
- let a = e.template.replace(bt, (e, t) => String(r[t]));
913
+ let a = e.template.replace(vt, (e, t) => String(r[t]));
898
914
  return m.succeed({
899
915
  promptId: e.id,
900
916
  digest: t,
@@ -905,7 +921,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
905
921
  });
906
922
  })
907
923
  };
908
- }, wt = (e) => typeof e == "object" && !!e && typeof e.prompt == "string" && typeof e.digest == "string" && typeof e.promptId == "string", Y = "_voltro_prompts", X = T(Y, {
924
+ }, St = (e) => typeof e == "object" && !!e && typeof e.prompt == "string" && typeof e.digest == "string" && typeof e.promptId == "string", Y = "_voltro_prompts", X = T(Y, {
909
925
  id: x({ prefix: "prm" }),
910
926
  promptId: E().maxLength(191),
911
927
  digest: E().maxLength(191),
@@ -916,12 +932,12 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
916
932
  templateChars: S().default(0),
917
933
  firstSeenAt: D().default("now"),
918
934
  lastUsedAt: D().default("now")
919
- }).unique("byPromptsVersion", ["promptId", "digest"]).index(["promptId"]), Tt = 8760, Et = () => {
920
- oe({
935
+ }).unique("byPromptsVersion", ["promptId", "digest"]).index(["promptId"]), Ct = 8760, wt = () => {
936
+ ie({
921
937
  source: "framework",
922
938
  table: Y,
923
939
  timeColumn: "lastUsedAt",
924
- ttlMs: se(process.env.VOLTRO_AI_PROMPTS_TTL_HOURS, Tt),
940
+ ttlMs: ae(process.env.VOLTRO_AI_PROMPTS_TTL_HOURS, Ct),
925
941
  envVar: "VOLTRO_AI_PROMPTS_TTL_HOURS",
926
942
  label: "ai prompt versions"
927
943
  });
@@ -934,9 +950,9 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
934
950
  system: e.system ?? null,
935
951
  firstSeenAt: e.firstSeenAt ?? null,
936
952
  lastUsedAt: e.lastUsedAt ?? null
937
- }), Dt = (e, t) => m.gen(function* () {
938
- Et();
939
- let n = t.digest ?? Ct(t).digest;
953
+ }), Tt = (e, t) => m.gen(function* () {
954
+ wt();
955
+ let n = t.digest ?? xt(t).digest;
940
956
  return yield* m.promise(async () => {
941
957
  let r = (await e.query(w(X).where(v(y("promptId", t.id), y("digest", n))).descriptor))[0];
942
958
  if (r !== void 0) {
@@ -963,10 +979,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
963
979
  };
964
980
  return Z(await e.insertIgnore(Y, o, { conflictColumns: ["promptId", "digest"] }) ?? o);
965
981
  });
966
- }), Ot = (e, t) => m.promise(async () => {
982
+ }), Et = (e, t) => m.promise(async () => {
967
983
  let n = (await e.query(w(X).where(y("digest", t)).descriptor))[0];
968
984
  return n === void 0 ? null : Z(n);
969
- }), kt = (e, t) => m.promise(async () => [...await e.query(w(X).where(y("promptId", t)).orderBy("revision", "desc").descriptor)].sort((e, t) => (Number(t.revision) || 0) - (Number(e.revision) || 0)).map(Z)), At = "_voltro_ai_inferences", jt = T(At, {
985
+ }), Dt = (e, t) => m.promise(async () => [...await e.query(w(X).where(y("promptId", t)).orderBy("revision", "desc").descriptor)].sort((e, t) => (Number(t.revision) || 0) - (Number(e.revision) || 0)).map(Z)), Ot = "_voltro_ai_inferences", kt = T(Ot, {
970
986
  id: x({ prefix: "aiinf" }),
971
987
  tenantId: E().nullable(),
972
988
  executionId: E(),
@@ -1011,17 +1027,17 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
1011
1027
  startedAt: D().nullable(),
1012
1028
  completedAt: D().nullable(),
1013
1029
  durationMs: S().nullable()
1014
- }).index(["status", "enqueuedAt"]).index(["status", "leaseUntil"]).index(["executionId"]).index(["tenantId"]), Mt = 3e5, Nt = 4, Q = (e, t) => {
1030
+ }).index(["status", "enqueuedAt"]).index(["status", "leaseUntil"]).index(["executionId"]).index(["tenantId"]), At = 3e5, jt = 4, Q = (e, t) => {
1015
1031
  let n = typeof e == "number" ? e : Number(e);
1016
1032
  return Number.isFinite(n) ? Math.trunc(n) : t;
1017
- }, $ = (e) => e == null ? null : String(e), Pt = (e) => {
1033
+ }, $ = (e) => e == null ? null : String(e), Mt = (e) => {
1018
1034
  if (e instanceof Date) return e;
1019
1035
  if (typeof e == "string" || typeof e == "number") {
1020
1036
  let t = new Date(e);
1021
1037
  return Number.isNaN(t.getTime()) ? null : t;
1022
1038
  }
1023
1039
  return null;
1024
- }, Ft = (e) => ({
1040
+ }, Nt = (e) => ({
1025
1041
  id: String(e.id),
1026
1042
  tenantId: $(e.tenantId),
1027
1043
  executionId: String(e.executionId),
@@ -1043,10 +1059,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
1043
1059
  transcription: e.transcription ?? null,
1044
1060
  media: e.media ?? null,
1045
1061
  taskHandle: typeof e.taskHandle == "object" ? e.taskHandle : null,
1046
- taskSubmittedAt: Pt(e.taskSubmittedAt),
1062
+ taskSubmittedAt: Mt(e.taskSubmittedAt),
1047
1063
  attempts: Q(e.attempts, 0),
1048
1064
  fence: Q(e.fence, 0)
1049
- }), It = (e) => {
1065
+ }), Pt = (e) => {
1050
1066
  let t = e.attempts + 1;
1051
1067
  return t >= e.maxAttempts ? {
1052
1068
  kind: "give-up",
@@ -1056,6 +1072,6 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
1056
1072
  attempts: t,
1057
1073
  delayMs: e.retryAfterMs !== void 0 && e.retryAfterMs > 0 ? e.retryAfterMs : Math.min(e.baseDelayMs * 2 ** (t - 1), e.maxDelayMs)
1058
1074
  };
1059
- }, Lt = (e) => `voltro.ai.infer:${e}`, Rt = (e, t) => `aiinf_${fe("sha256").update(`${e}\u0000${t}`).digest("hex").slice(0, 40)}`;
1075
+ }, Ft = (e) => `voltro.ai.infer:${e}`, It = (e, t) => `aiinf_${ue("sha256").update(`${e}\u0000${t}`).digest("hex").slice(0, 40)}`;
1060
1076
  //#endregion
1061
- export { j as $, nt as A, z as B, J as C, ft as D, ut as E, Ve as F, Fe as G, R as H, Be as I, Ne as J, Ue as K, Me as L, $e as M, G as N, st as O, B as P, A as Q, Ke as R, pt as S, dt as T, Pe as U, ze as V, Ie as W, M as X, N as Y, k as Z, kt as _, It as a, he as at, _t as b, Ft as c, yt as d, Ce as et, Ct as f, St as g, Ot as h, jt as i, O as it, U as j, lt as k, Tt as l, vt as m, Nt as n, je as nt, Lt as o, ge as ot, wt as p, Ge as q, Mt as r, Ae as rt, Rt as s, _e as st, At as t, P as tt, Y as u, X as v, mt as w, q as x, Dt as y, Le as z };
1077
+ export { j as $, et as A, z as B, J as C, ut as D, ct as E, ze as F, Ne as G, R as H, Re as I, je as J, Ve as K, Ae as L, Ze as M, G as N, at as O, B as P, A as Q, We as R, dt as S, lt as T, Me as U, Le as V, Pe as W, M as X, N as Y, k as Z, Dt as _, Pt as a, pe as at, ht as b, Nt as c, _t as d, xe as et, xt as f, bt as g, Et as h, kt as i, O as it, U as j, st as k, Ct as l, gt as m, jt as n, ke as nt, Ft as o, me as ot, St as p, Ue as q, At as r, Oe as rt, It as s, he as st, Ot as t, P as tt, Y as u, X as v, ft as w, q as x, Tt as y, Fe as z };