@voltro/ai 0.83.0 → 0.84.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,4 +1,4 @@
1
- import { c as e, o as t, r as n } from "./provider-BsyDya-H.js";
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
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
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";
@@ -376,18 +376,25 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
376
376
  receiptKey: E().maxLength(191).backfill((e) => `aiu-pre-receipt-${String(e.id)}`),
377
377
  calledAt: D().default("now")
378
378
  }).index(["tenantId"]).index(["promptDigest"]).unique("byAiUsageReceipt", ["receiptKey"]), Re = (e) => {
379
- let t = e?.gateway?.cost, n = typeof t == "string" ? t.trim() : void 0, r = typeof t == "number" ? t : n !== void 0 && /^(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(n) ? Number(n) : NaN;
380
- return Number.isFinite(r) && r >= 0 ? r : void 0;
381
- }, ze = class extends p.TaggedError("AiUsageReceiptConflict") {
379
+ 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
+ return Number.isFinite(n) && n >= 0 ? n : void 0;
381
+ }, ze = (e) => {
382
+ let t = e?.gateway, n = Re(t?.cost);
383
+ if (n === void 0) return;
384
+ let r = (Array.isArray(t?.routing?.modelAttempts) ? t.routing.modelAttempts : []).findLast((e) => e?.success === !0);
385
+ if ((Array.isArray(r?.providerAttempts) ? r.providerAttempts : []).findLast((e) => e?.success === !0)?.credentialType !== "byok") return n;
386
+ let i = Re(t?.marketCost);
387
+ return i === void 0 ? void 0 : n + i;
388
+ }, Be = class extends p.TaggedError("AiUsageReceiptConflict") {
382
389
  get message() {
383
390
  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])}`);
384
391
  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.`;
385
392
  }
386
- }, Be = class extends p.TaggedError("AiUsageLedgerError") {
393
+ }, Ve = class extends p.TaggedError("AiUsageLedgerError") {
387
394
  get message() {
388
395
  return `AI usage receipt ${this.receiptKey} could not be written: ${this.cause instanceof Error ? this.cause.message : String(this.cause)}`;
389
396
  }
390
- }, Ve = (e) => ({
397
+ }, He = (e) => ({
391
398
  tenantId: e.tenantId ?? null,
392
399
  provider: String(e.provider),
393
400
  model: String(e.model),
@@ -395,7 +402,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
395
402
  inputTokens: Number(e.inputTokens),
396
403
  outputTokens: Number(e.outputTokens),
397
404
  costMicroUsd: Number(e.costMicroUsd)
398
- }), He = (e, t) => (oe({
405
+ }), Ue = (e, t) => (oe({
399
406
  source: "plugin",
400
407
  table: B,
401
408
  timeColumn: "calledAt",
@@ -428,7 +435,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
428
435
  calledAt: t.calledAt ?? /* @__PURE__ */ new Date()
429
436
  }, a = yield* m.tryPromise({
430
437
  try: () => e.insertIgnoreWithOutcome(B, i, { conflictColumns: ["receiptKey"] }),
431
- catch: (e) => new Be({
438
+ catch: (e) => new Ve({
432
439
  receiptKey: String(r),
433
440
  cause: e
434
441
  })
@@ -437,16 +444,16 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
437
444
  ...n,
438
445
  receipt: "recorded"
439
446
  };
440
- let o = Ve(a.row), s = Ve(i);
441
- return Object.keys(o).some((e) => o[e] !== s[e]) ? yield* m.fail(new ze({
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({
442
449
  receiptKey: String(r),
443
450
  recorded: o,
444
451
  attempted: s
445
452
  })) : {
446
- ...Ue(a.row, n),
453
+ ...We(a.row, n),
447
454
  receipt: "replayed"
448
455
  };
449
- })), Ue = (e, t) => {
456
+ })), We = (e, t) => {
450
457
  let n = Number(e.costMicroUsd) / 1e6;
451
458
  return {
452
459
  ...t,
@@ -455,7 +462,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
455
462
  totalCostUsd: n,
456
463
  costSource: e.costSource ?? t.costSource
457
464
  };
458
- }, We = (e, t) => He(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({
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({
459
466
  scope: "voltro:ai:usage",
460
467
  provider: t.provider,
461
468
  model: t.model,
@@ -473,55 +480,55 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
473
480
  attempt: t.attempt ?? 0
474
481
  },
475
482
  ...t.actualCostUsd === void 0 ? {} : { actualCostUsd: t.actualCostUsd }
476
- })))), Ge = (e, t = {}) => m.promise(async () => {
483
+ })))), Ke = (e, t = {}) => m.promise(async () => {
477
484
  let n = [
478
485
  ...t.tenantId === void 0 ? [] : [y("tenantId", t.tenantId)],
479
486
  ...t.since === void 0 ? [] : [ie("calledAt", t.since)],
480
487
  ...t.promptDigest === void 0 ? [] : [y("promptDigest", t.promptDigest)]
481
488
  ], r = w(Le), i = n.length === 0 ? r : r.where(n.length === 1 ? n[0] : v(...n));
482
489
  return (await e.query(i.descriptor)).reduce((e, t) => e + (Number(t.costMicroUsd) || 0), 0) / 1e6;
483
- }), Ke = ee.exponential({
490
+ }), qe = ee.exponential({
484
491
  start: .001,
485
492
  factor: 2,
486
493
  count: 16
487
- }), V = g.counter("voltro_ai_calls_total", { description: "Total AI provider calls (generate + stream)." }), qe = g.counter("voltro_ai_call_errors_total", { description: "AI provider calls that errored." }), H = g.histogram("voltro_ai_call_duration_seconds", Ke, "AI provider call duration in seconds."), Je = g.counter("voltro_ai_input_tokens_total", {
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", {
488
495
  description: "Total prompt (input) tokens across AI calls.",
489
496
  incremental: !0
490
- }), Ye = g.counter("voltro_ai_output_tokens_total", {
497
+ }), Xe = g.counter("voltro_ai_output_tokens_total", {
491
498
  description: "Total completion (output) tokens across AI calls.",
492
499
  incremental: !0
493
- }), Xe = g.counter("voltro_ai_cost_microusd_total", {
500
+ }), Ze = g.counter("voltro_ai_cost_microusd_total", {
494
501
  description: "Total estimated AI spend in micro-USD (USD × 1e6).",
495
502
  incremental: !0
496
503
  }), U = (e, t) => ({
497
504
  provider: e.name,
498
505
  model: e.model ?? e.name,
499
506
  operation: t
500
- }), 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)), Ze = (e, 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) => {
501
508
  let n = t.inputTokens ?? 0, r = t.outputTokens ?? 0, i = z({
502
509
  inputTokens: n,
503
510
  outputTokens: r
504
511
  }, { model: e.model }), a = Math.round(i.totalCostUsd * 1e6);
505
512
  return m.all([
506
- W(g.incrementBy(Je, n), e, "ok"),
507
- W(g.incrementBy(Ye, r), e, "ok"),
508
- W(g.incrementBy(Xe, a), e, "ok")
513
+ W(g.incrementBy(Ye, n), e, "ok"),
514
+ W(g.incrementBy(Xe, r), e, "ok"),
515
+ W(g.incrementBy(Ze, a), e, "ok")
509
516
  ], { discard: !0 });
510
517
  }, G = (e, t, n) => m.gen(function* () {
511
518
  let r = yield* m.clockWith((e) => e.currentTimeMillis);
512
- return yield* n.pipe(m.tap((n) => Ze(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(qe), 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: {
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: {
513
520
  "ai.provider": e.provider,
514
521
  "ai.model": e.model,
515
522
  "ai.operation": e.operation
516
523
  } }));
517
- }), Qe = (e, t) => {
524
+ }), $e = (e, t) => {
518
525
  let n = Date.now();
519
- 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(Ze(e, t.usage ?? {}))) : t._tag === "error" ? W(g.increment(V), e, "error").pipe(m.zipRight(W(g.increment(qe), e, "error")), m.zipRight(W(g.update(H, (Date.now() - n) / 1e3), e, "error"))) : m.void), ne.withSpan("voltro.ai.call", { attributes: {
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: {
520
527
  "ai.provider": e.provider,
521
528
  "ai.model": e.model,
522
529
  "ai.operation": e.operation
523
530
  } }));
524
- }, $e = (e) => {
531
+ }, et = (e) => {
525
532
  let t = structuredClone(e), n = {
526
533
  strict: !0,
527
534
  strictTypes: !1,
@@ -539,13 +546,13 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
539
546
  success: !1,
540
547
  error: /* @__PURE__ */ Error("model output does not satisfy the declared JSON Schema")
541
548
  } });
542
- }, et = _.Number.pipe(_.int(), _.nonNegative(), _.lessThanOrEqualTo(2 ** 53 - 1)), tt = _.Struct({
549
+ }, tt = _.Number.pipe(_.int(), _.nonNegative(), _.lessThanOrEqualTo(2 ** 53 - 1)), nt = _.Struct({
543
550
  provider: _.String,
544
551
  model: _.String,
545
552
  usage: _.Struct({
546
- inputTokens: _.optional(et),
547
- outputTokens: _.optional(et),
548
- totalTokens: _.optional(et),
553
+ inputTokens: _.optional(tt),
554
+ outputTokens: _.optional(tt),
555
+ totalTokens: _.optional(tt),
549
556
  completeness: _.Literal("complete", "partial", "unknown")
550
557
  }),
551
558
  cost: _.Union(_.Struct({
@@ -556,12 +563,12 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
556
563
  status: _.Literal("unknown"),
557
564
  source: _.Literal("unreported")
558
565
  }))
559
- }), nt = (e) => typeof e == "number" && Number.isSafeInteger(e) && e >= 0 ? e : void 0, K = (e, t, n, r) => {
560
- let i = typeof n == "object" && n ? n : {}, a = nt("inputTokens" in i ? i.inputTokens : void 0), o = nt("outputTokens" in i ? i.outputTokens : void 0), s = nt("totalTokens" in i ? i.totalTokens : void 0), c = [
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 = [
561
568
  a,
562
569
  o,
563
570
  s
564
- ].filter((e) => e !== void 0).length, l = Re(r);
571
+ ].filter((e) => e !== void 0).length, l = ze(r);
565
572
  return {
566
573
  provider: e,
567
574
  model: t,
@@ -580,7 +587,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
580
587
  usd: l
581
588
  }
582
589
  };
583
- }, rt = (e, t) => {
590
+ }, it = (e, t) => {
584
591
  let n = [];
585
592
  return {
586
593
  model: d({
@@ -625,9 +632,9 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
625
632
  }, q = class extends _.TaggedError()("AiError", {
626
633
  reason: _.Literal("generation", "decode"),
627
634
  message: _.String,
628
- evidence: _.optional(tt)
635
+ evidence: _.optional(nt)
629
636
  }) {}, J = (e, t = {}) => {
630
- let n = String(e?.message ?? e), r = it(e), i = new q({
637
+ let n = String(e?.message ?? e), r = at(e), i = new q({
631
638
  reason: "generation",
632
639
  message: `${t.prefix === void 0 ? "" : `${t.prefix}: `}${n}${r.length > 0 ? ` (${r.join(" <- ")})` : ""}`,
633
640
  ...t.evidence === void 0 ? {} : { evidence: t.evidence }
@@ -638,17 +645,17 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
638
645
  configurable: !0,
639
646
  writable: !0
640
647
  }), i;
641
- }, it = (e) => {
648
+ }, at = (e) => {
642
649
  let t = [], n = e;
643
650
  for (let e = 0; e < 8 && typeof n == "object" && n; e++) {
644
651
  let e = n;
645
652
  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;
646
653
  }
647
654
  return t;
648
- }, at = (e, t, n) => {
655
+ }, ot = (e, t, n) => {
649
656
  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)));
650
657
  return i(0);
651
- }, ot = (e) => m.gen(function* () {
658
+ }, st = (e) => m.gen(function* () {
652
659
  let r = e.messages === void 0 ? e.prompt === void 0 ? void 0 : { prompt: e.prompt } : { messages: [...e.messages] };
653
660
  if (r === void 0 || e.prompt !== void 0 && e.messages !== void 0) return yield* m.fail(new q({
654
661
  reason: "generation",
@@ -660,7 +667,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
660
667
  ...e.abortSignal === void 0 ? {} : { abortSignal: e.abortSignal },
661
668
  runtime: i
662
669
  });
663
- return yield* at(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateText"), (e) => e.usage, m.tryPromise({
670
+ return yield* ot(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateText"), (e) => e.usage, m.tryPromise({
664
671
  try: async () => {
665
672
  let i = t(n), o = await s({
666
673
  model: i,
@@ -688,7 +695,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
688
695
  },
689
696
  catch: (e) => J(e)
690
697
  })));
691
- }), st = (e, t) => m.gen(function* () {
698
+ }), ct = (e, t) => m.gen(function* () {
692
699
  let n = h.make(e);
693
700
  if (n?.type !== "object") {
694
701
  let e = n?.anyOf === void 0 ? `type:${JSON.stringify(n?.type ?? null)}` : "anyOf (e.g. Schema.Struct({}) or a union)";
@@ -698,14 +705,14 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
698
705
  }));
699
706
  }
700
707
  return n;
701
- }), ct = (e, t) => st(e, t), lt = (e) => at(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateObject"), (e) => e.usage, m.gen(function* () {
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* () {
702
709
  let i = yield* m.try({
703
- try: () => $e(e.jsonSchema),
710
+ try: () => et(e.jsonSchema),
704
711
  catch: () => new q({
705
712
  reason: "decode",
706
713
  message: "generateObjectJson: invalid or unsupported output JSON Schema; no provider call was made"
707
714
  })
708
- }), a = rt(n.name, t(n)), s = a.model, c = yield* m.tryPromise({
715
+ }), a = it(n.name, t(n)), s = a.model, c = yield* m.tryPromise({
709
716
  try: () => o({
710
717
  model: s,
711
718
  schema: i,
@@ -734,9 +741,9 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
734
741
  },
735
742
  providerMetadata: c.providerMetadata
736
743
  };
737
- }))), ut = (e) => m.gen(function* () {
738
- let t = yield* lt({
739
- jsonSchema: yield* st(e.schema, "generateObject"),
744
+ }))), dt = (e) => m.gen(function* () {
745
+ let t = yield* ut({
746
+ jsonSchema: yield* ct(e.schema, "generateObject"),
740
747
  prompt: e.prompt,
741
748
  ...e.system === void 0 ? {} : { system: e.system },
742
749
  ...e.provider === void 0 ? {} : { provider: e.provider },
@@ -753,15 +760,15 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
753
760
  ...t,
754
761
  object: n
755
762
  };
756
- }), dt = (e) => m.gen(function* () {
757
- let a = yield* m.runtime(), o = yield* st(e.schema, "generateObjectWithTools"), u = {
763
+ }), ft = (e) => m.gen(function* () {
764
+ let a = yield* m.runtime(), o = yield* ct(e.schema, "generateObjectWithTools"), u = {
758
765
  store: void 0,
759
766
  ...e.toolContext,
760
767
  ...e.abortSignal === void 0 ? {} : { abortSignal: e.abortSignal },
761
768
  runtime: a
762
769
  }, d = O(e.tools, u);
763
- return yield* at(e.provider ?? n(), e.fallbacks, (n) => G(U(n, "generateObjectWithTools"), (e) => e.usage, m.gen(function* () {
764
- let a = rt(n.name, t(n)), u = a.model, f = yield* m.tryPromise({
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({
765
772
  try: () => s({
766
773
  model: u,
767
774
  prompt: e.prompt,
@@ -805,10 +812,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
805
812
  providerMetadata: f.providerMetadata
806
813
  };
807
814
  })));
808
- }), ft = class extends f.Tag("@voltro/AiService")() {}, pt = {
809
- generateText: (e) => ot(e),
810
- generateObject: (e) => ut(e)
811
- }, mt = async (e, t) => {
815
+ }), pt = class extends f.Tag("@voltro/AiService")() {}, mt = {
816
+ generateText: (e) => st(e),
817
+ generateObject: (e) => dt(e)
818
+ }, ht = async (e, t) => {
812
819
  if ("bytes" in e) return {
813
820
  audio: e.bytes,
814
821
  mediaType: e.mediaType
@@ -828,7 +835,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
828
835
  audio: n,
829
836
  mediaType: r
830
837
  };
831
- }, ht = (e) => {
838
+ }, gt = (e) => {
832
839
  if (!Array.isArray(e) || e.length === 0) return;
833
840
  let t = [];
834
841
  for (let n of e) {
@@ -843,16 +850,16 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
843
850
  });
844
851
  }
845
852
  return t.length > 0 ? t : void 0;
846
- }, gt = (t) => {
853
+ }, _t = (t) => {
847
854
  let r = t.provider ?? n();
848
855
  return G(U(r, "transcribe"), () => ({}), m.tryPromise({
849
856
  try: async () => {
850
- let { audio: n, mediaType: i } = await mt(t.source, t.resolveRef), o = await a({
857
+ let { audio: n, mediaType: i } = await ht(t.source, t.resolveRef), o = await a({
851
858
  model: e(r),
852
859
  audio: n,
853
860
  ...i === void 0 ? {} : { mediaType: i },
854
861
  ...t.providerOptions === void 0 ? {} : { providerOptions: t.providerOptions }
855
- }), s = t.timestamps === "none" ? void 0 : ht(o.segments);
862
+ }), s = t.timestamps === "none" ? void 0 : gt(o.segments);
856
863
  return {
857
864
  text: o.text,
858
865
  ...o.language === void 0 ? {} : { language: o.language },
@@ -863,31 +870,31 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
863
870
  },
864
871
  catch: (e) => J(e)
865
872
  }));
866
- }, _t = (e) => `sha256:${fe("sha256").update(e).digest("hex").slice(0, 16)}`, vt = class extends _.TaggedError()("PromptRenderError", {
873
+ }, vt = (e) => `sha256:${fe("sha256").update(e).digest("hex").slice(0, 16)}`, yt = class extends _.TaggedError()("PromptRenderError", {
867
874
  promptId: _.String,
868
875
  missing: _.Array(_.String),
869
876
  message: _.String
870
- }) {}, yt = /\{\{\s*([A-Za-z0-9_.]+)\s*\}\}/g, bt = (e) => {
877
+ }) {}, bt = /\{\{\s*([A-Za-z0-9_.]+)\s*\}\}/g, xt = (e) => {
871
878
  let t = /* @__PURE__ */ new Set();
872
- for (let n of e.matchAll(yt)) {
879
+ for (let n of e.matchAll(bt)) {
873
880
  let e = n[1];
874
881
  e !== void 0 && t.add(e);
875
882
  }
876
883
  return [...t];
877
- }, xt = (e, t) => _t(`${e.length}:${e}${(t ?? "").length}:${t ?? ""}`), St = (e) => {
878
- let t = xt(e.template, e.system), n = bt(e.template);
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);
879
886
  return {
880
887
  ...e,
881
888
  digest: t,
882
889
  variables: n,
883
890
  render: (r = {}) => m.suspend(() => {
884
891
  let i = n.filter((e) => r[e] === void 0);
885
- if (i.length > 0) return m.fail(new vt({
892
+ if (i.length > 0) return m.fail(new yt({
886
893
  promptId: e.id,
887
894
  missing: i,
888
895
  message: `prompt '${e.id}' needs ${i.map((e) => `{{${e}}}`).join(", ")}, which ${i.length === 1 ? "was" : "were"} not supplied.`
889
896
  }));
890
- let a = e.template.replace(yt, (e, t) => String(r[t]));
897
+ let a = e.template.replace(bt, (e, t) => String(r[t]));
891
898
  return m.succeed({
892
899
  promptId: e.id,
893
900
  digest: t,
@@ -898,7 +905,7 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
898
905
  });
899
906
  })
900
907
  };
901
- }, Ct = (e) => typeof e == "object" && !!e && typeof e.prompt == "string" && typeof e.digest == "string" && typeof e.promptId == "string", Y = "_voltro_prompts", X = T(Y, {
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, {
902
909
  id: x({ prefix: "prm" }),
903
910
  promptId: E().maxLength(191),
904
911
  digest: E().maxLength(191),
@@ -909,12 +916,12 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
909
916
  templateChars: S().default(0),
910
917
  firstSeenAt: D().default("now"),
911
918
  lastUsedAt: D().default("now")
912
- }).unique("byPromptsVersion", ["promptId", "digest"]).index(["promptId"]), wt = 8760, Tt = () => {
919
+ }).unique("byPromptsVersion", ["promptId", "digest"]).index(["promptId"]), Tt = 8760, Et = () => {
913
920
  oe({
914
921
  source: "framework",
915
922
  table: Y,
916
923
  timeColumn: "lastUsedAt",
917
- ttlMs: se(process.env.VOLTRO_AI_PROMPTS_TTL_HOURS, wt),
924
+ ttlMs: se(process.env.VOLTRO_AI_PROMPTS_TTL_HOURS, Tt),
918
925
  envVar: "VOLTRO_AI_PROMPTS_TTL_HOURS",
919
926
  label: "ai prompt versions"
920
927
  });
@@ -927,9 +934,9 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
927
934
  system: e.system ?? null,
928
935
  firstSeenAt: e.firstSeenAt ?? null,
929
936
  lastUsedAt: e.lastUsedAt ?? null
930
- }), Et = (e, t) => m.gen(function* () {
931
- Tt();
932
- let n = t.digest ?? St(t).digest;
937
+ }), Dt = (e, t) => m.gen(function* () {
938
+ Et();
939
+ let n = t.digest ?? Ct(t).digest;
933
940
  return yield* m.promise(async () => {
934
941
  let r = (await e.query(w(X).where(v(y("promptId", t.id), y("digest", n))).descriptor))[0];
935
942
  if (r !== void 0) {
@@ -956,10 +963,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
956
963
  };
957
964
  return Z(await e.insertIgnore(Y, o, { conflictColumns: ["promptId", "digest"] }) ?? o);
958
965
  });
959
- }), Dt = (e, t) => m.promise(async () => {
966
+ }), Ot = (e, t) => m.promise(async () => {
960
967
  let n = (await e.query(w(X).where(y("digest", t)).descriptor))[0];
961
968
  return n === void 0 ? null : Z(n);
962
- }), Ot = (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)), kt = "_voltro_ai_inferences", At = T(kt, {
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, {
963
970
  id: x({ prefix: "aiinf" }),
964
971
  tenantId: E().nullable(),
965
972
  executionId: E(),
@@ -1004,17 +1011,17 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
1004
1011
  startedAt: D().nullable(),
1005
1012
  completedAt: D().nullable(),
1006
1013
  durationMs: S().nullable()
1007
- }).index(["status", "enqueuedAt"]).index(["status", "leaseUntil"]).index(["executionId"]).index(["tenantId"]), jt = 3e5, Mt = 4, Q = (e, t) => {
1014
+ }).index(["status", "enqueuedAt"]).index(["status", "leaseUntil"]).index(["executionId"]).index(["tenantId"]), Mt = 3e5, Nt = 4, Q = (e, t) => {
1008
1015
  let n = typeof e == "number" ? e : Number(e);
1009
1016
  return Number.isFinite(n) ? Math.trunc(n) : t;
1010
- }, $ = (e) => e == null ? null : String(e), Nt = (e) => {
1017
+ }, $ = (e) => e == null ? null : String(e), Pt = (e) => {
1011
1018
  if (e instanceof Date) return e;
1012
1019
  if (typeof e == "string" || typeof e == "number") {
1013
1020
  let t = new Date(e);
1014
1021
  return Number.isNaN(t.getTime()) ? null : t;
1015
1022
  }
1016
1023
  return null;
1017
- }, Pt = (e) => ({
1024
+ }, Ft = (e) => ({
1018
1025
  id: String(e.id),
1019
1026
  tenantId: $(e.tenantId),
1020
1027
  executionId: String(e.executionId),
@@ -1036,10 +1043,10 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
1036
1043
  transcription: e.transcription ?? null,
1037
1044
  media: e.media ?? null,
1038
1045
  taskHandle: typeof e.taskHandle == "object" ? e.taskHandle : null,
1039
- taskSubmittedAt: Nt(e.taskSubmittedAt),
1046
+ taskSubmittedAt: Pt(e.taskSubmittedAt),
1040
1047
  attempts: Q(e.attempts, 0),
1041
1048
  fence: Q(e.fence, 0)
1042
- }), Ft = (e) => {
1049
+ }), It = (e) => {
1043
1050
  let t = e.attempts + 1;
1044
1051
  return t >= e.maxAttempts ? {
1045
1052
  kind: "give-up",
@@ -1049,6 +1056,6 @@ var pe = (e, t) => e(t), me = Symbol.for("@voltro/ai/Tool"), he = (e) => ({
1049
1056
  attempts: t,
1050
1057
  delayMs: e.retryAfterMs !== void 0 && e.retryAfterMs > 0 ? e.retryAfterMs : Math.min(e.baseDelayMs * 2 ** (t - 1), e.maxDelayMs)
1051
1058
  };
1052
- }, It = (e) => `voltro.ai.infer:${e}`, Lt = (e, t) => `aiinf_${fe("sha256").update(`${e}\u0000${t}`).digest("hex").slice(0, 40)}`;
1059
+ }, Lt = (e) => `voltro.ai.infer:${e}`, Rt = (e, t) => `aiinf_${fe("sha256").update(`${e}\u0000${t}`).digest("hex").slice(0, 40)}`;
1053
1060
  //#endregion
1054
- export { j as $, tt as A, z as B, J as C, dt as D, lt as E, Be as F, Fe as G, R as H, ze as I, Ne as J, He as K, Me as L, Qe as M, G as N, ot as O, B as P, A as Q, Ge as R, ft as S, ut as T, Pe as U, Re as V, Ie as W, M as X, N as Y, k as Z, Ot as _, Ft as a, he as at, gt as b, Pt as c, vt as d, Ce as et, St as f, xt as g, Dt as h, At as i, O as it, U as j, ct as k, wt as l, _t as m, Mt as n, je as nt, It as o, ge as ot, Ct as p, We as q, jt as r, Ae as rt, Lt as s, _e as st, kt as t, P as tt, Y as u, X as v, pt as w, q as x, Et as y, Le as z };
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 };