@voltro/ai 0.58.0 → 0.59.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.
- package/CHANGELOG.md +55 -0
- package/dist/index.d.ts +106 -9
- package/dist/index.js +451 -453
- package/dist/{inferenceQueue-EQJJepZ8.js → inferenceQueue-CS1VouiW.js} +213 -192
- package/dist/workflow.js +14 -14
- package/package.json +5 -5
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
import { a as e, r as t, s as n } from "./provider-BOgeCRkg.js";
|
|
2
2
|
import { Output as r, experimental_transcribe as i, generateObject as a, generateText as o, jsonSchema as s, stepCountIs as c, tool as l } from "ai";
|
|
3
|
-
import { Context as u, Effect as d, JSONSchema as ee, Metric as f, MetricBoundaries as te, Runtime as
|
|
4
|
-
import { and as h, eq as g, generateId as
|
|
5
|
-
import { createHash as
|
|
3
|
+
import { Context as u, Effect as d, JSONSchema as ee, Metric as f, MetricBoundaries as te, Runtime as ne, Schema as p, Stream as m } from "effect";
|
|
4
|
+
import { and as h, eq as g, generateId as _, gte as re, id as v, integer as y, json as b, queryFor as x, registerRetention as S, retentionTtlMsFromEnv as C, table as w, text as T, timestamp as E } from "@voltro/database";
|
|
5
|
+
import { createHash as ie } from "node:crypto";
|
|
6
6
|
//#region src/tools.ts
|
|
7
|
-
var ae = (e, t) => e(t),
|
|
7
|
+
var ae = (e, t) => e(t), oe = Symbol.for("@voltro/ai/Tool"), se = (e) => ({
|
|
8
8
|
...e,
|
|
9
|
-
[
|
|
10
|
-
}),
|
|
9
|
+
[oe]: !0
|
|
10
|
+
}), ce = (e) => typeof e == "object" && !!e && e[oe] === !0, le = (e) => {
|
|
11
11
|
let t = e.default;
|
|
12
12
|
if (typeof t != "function") return 0;
|
|
13
13
|
let n = 0;
|
|
14
|
-
for (let r of Object.values(e))
|
|
14
|
+
for (let r of Object.values(e)) ce(r) && r.execute === void 0 && r.handler === void 0 && (r.handler = t, n += 1);
|
|
15
15
|
return n;
|
|
16
|
-
},
|
|
17
|
-
let n = t ??
|
|
18
|
-
return
|
|
19
|
-
},
|
|
16
|
+
}, ue = (e, t) => {
|
|
17
|
+
let n = t ?? ne.defaultRuntime;
|
|
18
|
+
return ne.runPromise(n)(e);
|
|
19
|
+
}, de = (e, t, n) => n ? e.pipe(d.timeoutFail({
|
|
20
20
|
duration: `${n} millis`,
|
|
21
21
|
onTimeout: () => /* @__PURE__ */ Error(`tool '${t}' timed out`)
|
|
22
|
-
})) : e,
|
|
23
|
-
if (e.execute) return
|
|
22
|
+
})) : e, fe = (e, t, n) => {
|
|
23
|
+
if (e.execute) return ue(de(ae(e.execute, t), e.name, e.timeoutMs), n.runtime);
|
|
24
24
|
if (e.handler) {
|
|
25
25
|
let r = e.handler, i = r(t, n);
|
|
26
|
-
return d.isEffect(i) ?
|
|
26
|
+
return d.isEffect(i) ? ue(de(i, e.name, e.timeoutMs), n.runtime) : i;
|
|
27
27
|
}
|
|
28
28
|
return Promise.reject(/* @__PURE__ */ Error(`tool '${e.name}' has neither execute nor handler`));
|
|
29
|
-
},
|
|
29
|
+
}, pe = (e) => {
|
|
30
30
|
let t = ee.make(e);
|
|
31
31
|
return t && t.type === "object" ? t : {
|
|
32
32
|
type: "object",
|
|
33
33
|
properties: {},
|
|
34
34
|
additionalProperties: !1
|
|
35
35
|
};
|
|
36
|
-
},
|
|
36
|
+
}, me = (e) => {
|
|
37
37
|
let t = e.inputJsonSchema;
|
|
38
|
-
return t === void 0 ?
|
|
38
|
+
return t === void 0 ? pe(e.input) : t.type === "object" ? t : {
|
|
39
39
|
type: "object",
|
|
40
40
|
properties: {},
|
|
41
41
|
additionalProperties: !0
|
|
42
42
|
};
|
|
43
|
-
},
|
|
43
|
+
}, he = (e, t = { store: void 0 }) => {
|
|
44
44
|
let n = {};
|
|
45
45
|
for (let [r, i] of Object.entries(e)) n[i.name || r] = l({
|
|
46
46
|
description: i.description,
|
|
47
|
-
inputSchema: s(
|
|
47
|
+
inputSchema: s(me(i)),
|
|
48
48
|
execute: async (e) => {
|
|
49
|
-
let n = await d.runPromise(
|
|
50
|
-
return i.output ? d.runPromise(
|
|
49
|
+
let n = await d.runPromise(p.decodeUnknown(i.input)(e)), r = await fe(i, n, t);
|
|
50
|
+
return i.output ? d.runPromise(p.decodeUnknown(i.output)(r)) : r;
|
|
51
51
|
}
|
|
52
52
|
});
|
|
53
53
|
return n;
|
|
54
|
-
},
|
|
54
|
+
}, D = {
|
|
55
55
|
"claude-opus-4-8": {
|
|
56
56
|
inputPer1M: 15,
|
|
57
57
|
outputPer1M: 75
|
|
@@ -96,68 +96,88 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
96
96
|
inputPer1M: .1,
|
|
97
97
|
outputPer1M: .4
|
|
98
98
|
}
|
|
99
|
-
},
|
|
100
|
-
let t =
|
|
101
|
-
return
|
|
102
|
-
},
|
|
103
|
-
...
|
|
104
|
-
...
|
|
105
|
-
}),
|
|
106
|
-
let t =
|
|
99
|
+
}, O = {}, ge = (e) => {
|
|
100
|
+
let t = O;
|
|
101
|
+
return O = { ...e }, t;
|
|
102
|
+
}, k = () => ({
|
|
103
|
+
...D,
|
|
104
|
+
...O
|
|
105
|
+
}), A = (e) => {
|
|
106
|
+
let t = k();
|
|
107
107
|
if (t[e] !== void 0) return t[e];
|
|
108
108
|
let n = e.indexOf("/");
|
|
109
109
|
if (n > 0) {
|
|
110
110
|
let r = e.slice(n + 1);
|
|
111
111
|
if (t[r] !== void 0) return t[r];
|
|
112
112
|
}
|
|
113
|
+
}, j = (e) => typeof e.unit == "string", M = (e) => j(e) ? {
|
|
114
|
+
unit: e.unit,
|
|
115
|
+
input: e.input ?? 0,
|
|
116
|
+
output: e.output ?? 0
|
|
117
|
+
} : {
|
|
118
|
+
unit: "tokens",
|
|
119
|
+
input: e.inputTokens ?? 0,
|
|
120
|
+
output: e.outputTokens ?? 0
|
|
113
121
|
}, N = (e, t) => {
|
|
114
|
-
let
|
|
122
|
+
let { unit: n, input: r, output: i } = M(e);
|
|
115
123
|
if (t.actualCostUsd !== void 0) {
|
|
116
|
-
let e =
|
|
124
|
+
let e = r + i, a = e > 0 ? r / e : 0;
|
|
117
125
|
return {
|
|
118
|
-
inputTokens:
|
|
119
|
-
outputTokens:
|
|
120
|
-
inputCostUsd: t.actualCostUsd *
|
|
121
|
-
outputCostUsd: t.actualCostUsd * (1 -
|
|
126
|
+
inputTokens: r,
|
|
127
|
+
outputTokens: i,
|
|
128
|
+
inputCostUsd: t.actualCostUsd * a,
|
|
129
|
+
outputCostUsd: t.actualCostUsd * (1 - a),
|
|
122
130
|
totalCostUsd: t.actualCostUsd,
|
|
123
|
-
costSource: "gateway"
|
|
131
|
+
costSource: "gateway",
|
|
132
|
+
unit: n
|
|
124
133
|
};
|
|
125
134
|
}
|
|
126
|
-
|
|
135
|
+
if (n !== "tokens" && t.price === void 0) return {
|
|
136
|
+
inputTokens: r,
|
|
137
|
+
outputTokens: i,
|
|
138
|
+
inputCostUsd: 0,
|
|
139
|
+
outputCostUsd: 0,
|
|
140
|
+
totalCostUsd: 0,
|
|
141
|
+
costSource: "unpriced",
|
|
142
|
+
unit: n
|
|
143
|
+
};
|
|
144
|
+
let a = t.price ?? A(t.model) ?? {
|
|
127
145
|
inputPer1M: 0,
|
|
128
146
|
outputPer1M: 0
|
|
129
|
-
},
|
|
147
|
+
}, o = r / 1e6 * a.inputPer1M, s = i / 1e6 * a.outputPer1M;
|
|
130
148
|
return {
|
|
131
|
-
inputTokens:
|
|
132
|
-
outputTokens:
|
|
133
|
-
inputCostUsd:
|
|
134
|
-
outputCostUsd:
|
|
135
|
-
totalCostUsd:
|
|
136
|
-
costSource: "estimated"
|
|
149
|
+
inputTokens: r,
|
|
150
|
+
outputTokens: i,
|
|
151
|
+
inputCostUsd: o,
|
|
152
|
+
outputCostUsd: s,
|
|
153
|
+
totalCostUsd: o + s,
|
|
154
|
+
costSource: "estimated",
|
|
155
|
+
unit: n
|
|
137
156
|
};
|
|
138
|
-
}, P = "_voltro_ai_usage",
|
|
139
|
-
id:
|
|
140
|
-
tenantId:
|
|
141
|
-
provider:
|
|
142
|
-
model:
|
|
143
|
-
operation:
|
|
144
|
-
agent:
|
|
145
|
-
inputTokens:
|
|
146
|
-
outputTokens:
|
|
147
|
-
costMicroUsd:
|
|
148
|
-
costSource:
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
}, P = "_voltro_ai_usage", F = w(P, {
|
|
158
|
+
id: v({ prefix: "aiu" }),
|
|
159
|
+
tenantId: T().nullable(),
|
|
160
|
+
provider: T(),
|
|
161
|
+
model: T(),
|
|
162
|
+
operation: T(),
|
|
163
|
+
agent: T().nullable(),
|
|
164
|
+
inputTokens: y().default(0),
|
|
165
|
+
outputTokens: y().default(0),
|
|
166
|
+
costMicroUsd: y().default(0),
|
|
167
|
+
costSource: T().default("estimated"),
|
|
168
|
+
unit: T().default("tokens"),
|
|
169
|
+
promptId: T().nullable(),
|
|
170
|
+
promptDigest: T().nullable(),
|
|
171
|
+
promptRevision: y().nullable(),
|
|
172
|
+
calledAt: E().default("now")
|
|
173
|
+
}).index(["tenantId"]).index(["promptDigest"]), _e = (e) => {
|
|
154
174
|
let t = e?.gateway?.cost, n = typeof t == "number" ? t : typeof t == "string" ? Number(t) : NaN;
|
|
155
175
|
return Number.isFinite(n) ? n : void 0;
|
|
156
|
-
},
|
|
176
|
+
}, ve = (e, t) => (S({
|
|
157
177
|
source: "plugin",
|
|
158
178
|
table: P,
|
|
159
179
|
timeColumn: "calledAt",
|
|
160
|
-
ttlMs:
|
|
180
|
+
ttlMs: C(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760)
|
|
161
181
|
}), d.promise(async () => {
|
|
162
182
|
let n = N(t.usage, {
|
|
163
183
|
model: t.model,
|
|
@@ -174,45 +194,46 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
174
194
|
outputTokens: n.outputTokens,
|
|
175
195
|
costMicroUsd: Math.round(n.totalCostUsd * 1e6),
|
|
176
196
|
costSource: n.costSource,
|
|
197
|
+
unit: n.unit,
|
|
177
198
|
promptId: t.prompt?.promptId ?? null,
|
|
178
199
|
promptDigest: t.prompt?.digest ?? null,
|
|
179
200
|
promptRevision: t.prompt?.revision ?? null,
|
|
180
201
|
calledAt: t.calledAt ?? /* @__PURE__ */ new Date()
|
|
181
202
|
}), n;
|
|
182
|
-
})),
|
|
203
|
+
})), ye = (e, t = {}) => d.promise(async () => {
|
|
183
204
|
let n = [
|
|
184
205
|
...t.tenantId === void 0 ? [] : [g("tenantId", t.tenantId)],
|
|
185
|
-
...t.since === void 0 ? [] : [
|
|
206
|
+
...t.since === void 0 ? [] : [re("calledAt", t.since)],
|
|
186
207
|
...t.promptDigest === void 0 ? [] : [g("promptDigest", t.promptDigest)]
|
|
187
|
-
], r =
|
|
208
|
+
], r = x(F), i = n.length === 0 ? r : r.where(n.length === 1 ? n[0] : h(...n));
|
|
188
209
|
return (await e.query(i.descriptor)).reduce((e, t) => e + (Number(t.costMicroUsd) || 0), 0) / 1e6;
|
|
189
|
-
}),
|
|
190
|
-
tenantId:
|
|
191
|
-
limitUsd:
|
|
192
|
-
spentUsd:
|
|
193
|
-
attemptedUsd:
|
|
194
|
-
}) {},
|
|
195
|
-
id:
|
|
196
|
-
bucket:
|
|
197
|
-
tenantId:
|
|
198
|
-
reservedMicroUsd:
|
|
199
|
-
updatedAt:
|
|
200
|
-
}).unique("byAiBudgetBucket", ["bucket"]), be = (e, t) => `${e ?? "global"}::${t === void 0 ? "all" : String(t.getTime())}`,
|
|
201
|
-
|
|
210
|
+
}), I = class extends p.TaggedError()("AiBudgetExceeded", {
|
|
211
|
+
tenantId: p.NullOr(p.String),
|
|
212
|
+
limitUsd: p.Number,
|
|
213
|
+
spentUsd: p.Number,
|
|
214
|
+
attemptedUsd: p.Number
|
|
215
|
+
}) {}, L = "_voltro_ai_budget", R = w(L, {
|
|
216
|
+
id: v({ prefix: "aib" }),
|
|
217
|
+
bucket: T().maxLength(191),
|
|
218
|
+
tenantId: T().nullable(),
|
|
219
|
+
reservedMicroUsd: y().default(0),
|
|
220
|
+
updatedAt: E().default("now")
|
|
221
|
+
}).unique("byAiBudgetBucket", ["bucket"]), be = (e, t) => `${e ?? "global"}::${t === void 0 ? "all" : String(t.getTime())}`, z = 32, xe = (e, t) => d.gen(function* () {
|
|
222
|
+
S({
|
|
202
223
|
source: "plugin",
|
|
203
|
-
table:
|
|
224
|
+
table: L,
|
|
204
225
|
timeColumn: "updatedAt",
|
|
205
|
-
ttlMs:
|
|
226
|
+
ttlMs: C(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760)
|
|
206
227
|
});
|
|
207
|
-
let n = t.addUsd ?? 0, r = Math.round(n * 1e6), i = Math.round(t.limitUsd * 1e6), a = be(t.tenantId, t.since), o = t.tenantId ?? null, s = (e) => new
|
|
228
|
+
let n = t.addUsd ?? 0, r = Math.round(n * 1e6), i = Math.round(t.limitUsd * 1e6), a = be(t.tenantId, t.since), o = t.tenantId ?? null, s = (e) => new I({
|
|
208
229
|
tenantId: o,
|
|
209
230
|
limitUsd: t.limitUsd,
|
|
210
231
|
spentUsd: e / 1e6,
|
|
211
232
|
attemptedUsd: n
|
|
212
233
|
}), c = yield* d.promise(async () => {
|
|
213
|
-
for (let t = 0; t <
|
|
234
|
+
for (let t = 0; t < z; t++) {
|
|
214
235
|
t > 0 && await new Promise((e) => setTimeout(e, t));
|
|
215
|
-
let n = (await e.query(
|
|
236
|
+
let n = (await e.query(x(R).where(g("bucket", a)).descriptor))[0], s = n && Number(n.reservedMicroUsd) || 0;
|
|
216
237
|
if (s + r > i) return {
|
|
217
238
|
ok: !1,
|
|
218
239
|
reservedMicro: s
|
|
@@ -224,10 +245,10 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
224
245
|
updatedAt: /* @__PURE__ */ new Date()
|
|
225
246
|
}, { where: h(g("id", String(n.id)), g("reservedMicroUsd", s)) }) === 1) return { ok: !0 };
|
|
226
247
|
} else {
|
|
227
|
-
let t =
|
|
248
|
+
let t = _({
|
|
228
249
|
kind: "typeid",
|
|
229
250
|
prefix: "aib"
|
|
230
|
-
},
|
|
251
|
+
}, L), n = await e.insertIgnore(L, {
|
|
231
252
|
id: t,
|
|
232
253
|
bucket: a,
|
|
233
254
|
tenantId: o,
|
|
@@ -237,57 +258,57 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
237
258
|
if (String(n.id) === t) return { ok: !0 };
|
|
238
259
|
}
|
|
239
260
|
}
|
|
240
|
-
throw Error(`requireAiBudget: contention on bucket '${a}' exceeded ${
|
|
261
|
+
throw Error(`requireAiBudget: contention on bucket '${a}' exceeded ${z} attempts`);
|
|
241
262
|
});
|
|
242
263
|
if (!c.ok) return yield* d.fail(s(c.reservedMicro));
|
|
243
264
|
}), Se = te.exponential({
|
|
244
265
|
start: .001,
|
|
245
266
|
factor: 2,
|
|
246
267
|
count: 16
|
|
247
|
-
}),
|
|
268
|
+
}), B = f.counter("voltro_ai_calls_total", { description: "Total AI provider calls (generate + stream)." }), Ce = f.counter("voltro_ai_call_errors_total", { description: "AI provider calls that errored." }), V = f.histogram("voltro_ai_call_duration_seconds", Se, "AI provider call duration in seconds."), we = f.counter("voltro_ai_input_tokens_total", {
|
|
248
269
|
description: "Total prompt (input) tokens across AI calls.",
|
|
249
270
|
incremental: !0
|
|
250
|
-
}),
|
|
271
|
+
}), Te = f.counter("voltro_ai_output_tokens_total", {
|
|
251
272
|
description: "Total completion (output) tokens across AI calls.",
|
|
252
273
|
incremental: !0
|
|
253
|
-
}),
|
|
274
|
+
}), Ee = f.counter("voltro_ai_cost_microusd_total", {
|
|
254
275
|
description: "Total estimated AI spend in micro-USD (USD × 1e6).",
|
|
255
276
|
incremental: !0
|
|
256
|
-
}),
|
|
277
|
+
}), H = (e, t) => ({
|
|
257
278
|
provider: e.name,
|
|
258
279
|
model: e.model ?? e.name,
|
|
259
280
|
operation: t
|
|
260
|
-
}),
|
|
281
|
+
}), U = (e, t, n) => e.pipe(d.tagMetrics("provider", t.provider), d.tagMetrics("model", t.model), d.tagMetrics("operation", t.operation), d.tagMetrics("status", n)), De = (e, t) => {
|
|
261
282
|
let n = t.inputTokens ?? 0, r = t.outputTokens ?? 0, i = N({
|
|
262
283
|
inputTokens: n,
|
|
263
284
|
outputTokens: r
|
|
264
285
|
}, { model: e.model }), a = Math.round(i.totalCostUsd * 1e6);
|
|
265
286
|
return d.all([
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
287
|
+
U(f.incrementBy(we, n), e, "ok"),
|
|
288
|
+
U(f.incrementBy(Te, r), e, "ok"),
|
|
289
|
+
U(f.incrementBy(Ee, a), e, "ok")
|
|
269
290
|
], { discard: !0 });
|
|
270
291
|
}, W = (e, t, n) => d.gen(function* () {
|
|
271
292
|
let r = yield* d.clockWith((e) => e.currentTimeMillis);
|
|
272
|
-
return yield* n.pipe(d.tap((n) =>
|
|
293
|
+
return yield* n.pipe(d.tap((n) => De(e, t(n))), d.tap(() => U(f.increment(B), e, "ok").pipe(d.zipRight(d.clockWith((e) => e.currentTimeMillis).pipe(d.flatMap((t) => U(f.update(V, (t - r) / 1e3), e, "ok")))))), d.tapError(() => U(f.increment(B), e, "error").pipe(d.zipRight(U(f.increment(Ce), e, "error")), d.zipRight(d.clockWith((e) => e.currentTimeMillis).pipe(d.flatMap((t) => U(f.update(V, (t - r) / 1e3), e, "error")))))), d.withSpan("voltro.ai.call", { attributes: {
|
|
273
294
|
"ai.provider": e.provider,
|
|
274
295
|
"ai.model": e.model,
|
|
275
296
|
"ai.operation": e.operation
|
|
276
297
|
} }));
|
|
277
|
-
}),
|
|
298
|
+
}), Oe = (e, t) => {
|
|
278
299
|
let n = Date.now();
|
|
279
|
-
return t.pipe(
|
|
300
|
+
return t.pipe(m.tap((t) => t._tag === "done" ? U(f.increment(B), e, "ok").pipe(d.zipRight(U(f.update(V, (Date.now() - n) / 1e3), e, "ok")), d.zipRight(De(e, t.usage ?? {}))) : t._tag === "error" ? U(f.increment(B), e, "error").pipe(d.zipRight(U(f.increment(Ce), e, "error")), d.zipRight(U(f.update(V, (Date.now() - n) / 1e3), e, "error"))) : d.void), m.withSpan("voltro.ai.call", { attributes: {
|
|
280
301
|
"ai.provider": e.provider,
|
|
281
302
|
"ai.model": e.model,
|
|
282
303
|
"ai.operation": e.operation
|
|
283
304
|
} }));
|
|
284
|
-
}, G = class extends
|
|
285
|
-
reason:
|
|
286
|
-
message:
|
|
305
|
+
}, G = class extends p.TaggedError()("AiError", {
|
|
306
|
+
reason: p.Literal("generation", "decode"),
|
|
307
|
+
message: p.String
|
|
287
308
|
}) {}, K = (e, t, n) => {
|
|
288
309
|
let r = [e, ...t ?? []], i = (e) => n(r[e]).pipe(d.catchTag("AiError", (t) => t.reason === "generation" && e + 1 < r.length ? i(e + 1) : d.fail(t)));
|
|
289
310
|
return i(0);
|
|
290
|
-
},
|
|
311
|
+
}, ke = (n) => K(n.provider ?? t(), n.fallbacks, (t) => W(H(t, "generateText"), (e) => e.usage, d.tryPromise({
|
|
291
312
|
try: async () => {
|
|
292
313
|
let r = e(t), i = await o({
|
|
293
314
|
model: r,
|
|
@@ -320,7 +341,7 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
320
341
|
}));
|
|
321
342
|
}
|
|
322
343
|
return n;
|
|
323
|
-
}),
|
|
344
|
+
}), Ae = (e, t) => q(e, t), je = (n) => K(n.provider ?? t(), n.fallbacks, (t) => W(H(t, "generateObject"), (e) => e.usage, d.gen(function* () {
|
|
324
345
|
let r = e(t), i = yield* d.tryPromise({
|
|
325
346
|
try: () => a({
|
|
326
347
|
model: r,
|
|
@@ -344,8 +365,8 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
344
365
|
},
|
|
345
366
|
providerMetadata: i.providerMetadata
|
|
346
367
|
};
|
|
347
|
-
}))),
|
|
348
|
-
let t = yield*
|
|
368
|
+
}))), Me = (e) => d.gen(function* () {
|
|
369
|
+
let t = yield* je({
|
|
349
370
|
jsonSchema: yield* q(e.schema, "generateObject"),
|
|
350
371
|
prompt: e.prompt,
|
|
351
372
|
...e.system === void 0 ? {} : { system: e.system },
|
|
@@ -353,7 +374,7 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
353
374
|
...e.fallbacks === void 0 ? {} : { fallbacks: e.fallbacks },
|
|
354
375
|
...e.maxTokens === void 0 ? {} : { maxTokens: e.maxTokens },
|
|
355
376
|
...e.providerOptions === void 0 ? {} : { providerOptions: e.providerOptions }
|
|
356
|
-
}), n = yield*
|
|
377
|
+
}), n = yield* p.decodeUnknown(e.schema)(t.object).pipe(d.mapError((e) => new G({
|
|
357
378
|
reason: "decode",
|
|
358
379
|
message: `model output failed schema decode: ${String(e)}`
|
|
359
380
|
})));
|
|
@@ -361,13 +382,13 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
361
382
|
...t,
|
|
362
383
|
object: n
|
|
363
384
|
};
|
|
364
|
-
}),
|
|
385
|
+
}), Ne = (n) => d.gen(function* () {
|
|
365
386
|
let i = yield* d.runtime(), a = yield* q(n.schema, "generateObjectWithTools"), l = {
|
|
366
387
|
store: void 0,
|
|
367
388
|
...n.toolContext,
|
|
368
389
|
runtime: i
|
|
369
|
-
}, u =
|
|
370
|
-
return yield* K(n.provider ?? t(), n.fallbacks, (t) => W(
|
|
390
|
+
}, u = he(n.tools, l);
|
|
391
|
+
return yield* K(n.provider ?? t(), n.fallbacks, (t) => W(H(t, "generateObjectWithTools"), (e) => e.usage, d.gen(function* () {
|
|
371
392
|
let i = e(t), l = yield* d.tryPromise({
|
|
372
393
|
try: () => o({
|
|
373
394
|
model: i,
|
|
@@ -385,7 +406,7 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
385
406
|
})
|
|
386
407
|
});
|
|
387
408
|
return {
|
|
388
|
-
object: yield*
|
|
409
|
+
object: yield* p.decodeUnknown(n.schema)(l.output).pipe(d.mapError((e) => new G({
|
|
389
410
|
reason: "decode",
|
|
390
411
|
message: `model output failed schema decode: ${String(e)}`
|
|
391
412
|
}))),
|
|
@@ -397,10 +418,10 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
397
418
|
providerMetadata: l.providerMetadata
|
|
398
419
|
};
|
|
399
420
|
})));
|
|
400
|
-
}),
|
|
401
|
-
generateText: (e) =>
|
|
402
|
-
generateObject: (e) =>
|
|
403
|
-
},
|
|
421
|
+
}), Pe = class extends u.Tag("@voltro/AiService")() {}, Fe = {
|
|
422
|
+
generateText: (e) => ke(e),
|
|
423
|
+
generateObject: (e) => Me(e)
|
|
424
|
+
}, Ie = async (e, t) => {
|
|
404
425
|
if ("bytes" in e) return {
|
|
405
426
|
audio: e.bytes,
|
|
406
427
|
mediaType: e.mediaType
|
|
@@ -420,7 +441,7 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
420
441
|
audio: n,
|
|
421
442
|
mediaType: r
|
|
422
443
|
};
|
|
423
|
-
},
|
|
444
|
+
}, Le = (e) => {
|
|
424
445
|
if (!Array.isArray(e) || e.length === 0) return;
|
|
425
446
|
let t = [];
|
|
426
447
|
for (let n of e) {
|
|
@@ -435,16 +456,16 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
435
456
|
});
|
|
436
457
|
}
|
|
437
458
|
return t.length > 0 ? t : void 0;
|
|
438
|
-
},
|
|
459
|
+
}, Re = (e) => {
|
|
439
460
|
let r = e.provider ?? t();
|
|
440
|
-
return W(
|
|
461
|
+
return W(H(r, "transcribe"), () => ({}), d.tryPromise({
|
|
441
462
|
try: async () => {
|
|
442
|
-
let { audio: t, mediaType: a } = await
|
|
463
|
+
let { audio: t, mediaType: a } = await Ie(e.source, e.resolveRef), o = await i({
|
|
443
464
|
model: n(r),
|
|
444
465
|
audio: t,
|
|
445
466
|
...a === void 0 ? {} : { mediaType: a },
|
|
446
467
|
...e.providerOptions === void 0 ? {} : { providerOptions: e.providerOptions }
|
|
447
|
-
}), s = e.timestamps === "none" ? void 0 :
|
|
468
|
+
}), s = e.timestamps === "none" ? void 0 : Le(o.segments);
|
|
448
469
|
return {
|
|
449
470
|
text: o.text,
|
|
450
471
|
...o.language === void 0 ? {} : { language: o.language },
|
|
@@ -458,31 +479,31 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
458
479
|
message: String(e?.message ?? e)
|
|
459
480
|
})
|
|
460
481
|
}));
|
|
461
|
-
},
|
|
462
|
-
promptId:
|
|
463
|
-
missing:
|
|
464
|
-
message:
|
|
465
|
-
}) {},
|
|
482
|
+
}, ze = (e) => `sha256:${ie("sha256").update(e).digest("hex").slice(0, 16)}`, Be = class extends p.TaggedError()("PromptRenderError", {
|
|
483
|
+
promptId: p.String,
|
|
484
|
+
missing: p.Array(p.String),
|
|
485
|
+
message: p.String
|
|
486
|
+
}) {}, Ve = /\{\{\s*([A-Za-z0-9_.]+)\s*\}\}/g, He = (e) => {
|
|
466
487
|
let t = /* @__PURE__ */ new Set();
|
|
467
|
-
for (let n of e.matchAll(
|
|
488
|
+
for (let n of e.matchAll(Ve)) {
|
|
468
489
|
let e = n[1];
|
|
469
490
|
e !== void 0 && t.add(e);
|
|
470
491
|
}
|
|
471
492
|
return [...t];
|
|
472
|
-
},
|
|
473
|
-
let t =
|
|
493
|
+
}, Ue = (e, t) => ze(`${e.length}:${e}${(t ?? "").length}:${t ?? ""}`), We = (e) => {
|
|
494
|
+
let t = Ue(e.template, e.system), n = He(e.template);
|
|
474
495
|
return {
|
|
475
496
|
...e,
|
|
476
497
|
digest: t,
|
|
477
498
|
variables: n,
|
|
478
499
|
render: (r = {}) => d.suspend(() => {
|
|
479
500
|
let i = n.filter((e) => r[e] === void 0);
|
|
480
|
-
if (i.length > 0) return d.fail(new
|
|
501
|
+
if (i.length > 0) return d.fail(new Be({
|
|
481
502
|
promptId: e.id,
|
|
482
503
|
missing: i,
|
|
483
504
|
message: `prompt '${e.id}' needs ${i.map((e) => `{{${e}}}`).join(", ")}, which ${i.length === 1 ? "was" : "were"} not supplied.`
|
|
484
505
|
}));
|
|
485
|
-
let a = e.template.replace(
|
|
506
|
+
let a = e.template.replace(Ve, (e, t) => String(r[t]));
|
|
486
507
|
return d.succeed({
|
|
487
508
|
promptId: e.id,
|
|
488
509
|
digest: t,
|
|
@@ -493,27 +514,27 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
493
514
|
});
|
|
494
515
|
})
|
|
495
516
|
};
|
|
496
|
-
},
|
|
497
|
-
id:
|
|
498
|
-
promptId:
|
|
499
|
-
digest:
|
|
500
|
-
revision:
|
|
501
|
-
label:
|
|
502
|
-
template:
|
|
503
|
-
system:
|
|
504
|
-
templateChars:
|
|
505
|
-
firstSeenAt:
|
|
506
|
-
lastUsedAt:
|
|
507
|
-
}).unique("byPromptsVersion", ["promptId", "digest"]).index(["promptId"]),
|
|
508
|
-
|
|
517
|
+
}, Ge = (e) => typeof e == "object" && !!e && typeof e.prompt == "string" && typeof e.digest == "string" && typeof e.promptId == "string", J = "_voltro_prompts", Y = w(J, {
|
|
518
|
+
id: v({ prefix: "prm" }),
|
|
519
|
+
promptId: T().maxLength(191),
|
|
520
|
+
digest: T().maxLength(191),
|
|
521
|
+
revision: y().default(1),
|
|
522
|
+
label: T().nullable(),
|
|
523
|
+
template: T().nullable(),
|
|
524
|
+
system: T().nullable(),
|
|
525
|
+
templateChars: y().default(0),
|
|
526
|
+
firstSeenAt: E().default("now"),
|
|
527
|
+
lastUsedAt: E().default("now")
|
|
528
|
+
}).unique("byPromptsVersion", ["promptId", "digest"]).index(["promptId"]), Ke = 8760, qe = () => {
|
|
529
|
+
S({
|
|
509
530
|
source: "framework",
|
|
510
|
-
table:
|
|
531
|
+
table: J,
|
|
511
532
|
timeColumn: "lastUsedAt",
|
|
512
|
-
ttlMs:
|
|
533
|
+
ttlMs: C(process.env.VOLTRO_AI_PROMPTS_TTL_HOURS, Ke),
|
|
513
534
|
envVar: "VOLTRO_AI_PROMPTS_TTL_HOURS",
|
|
514
535
|
label: "ai prompt versions"
|
|
515
536
|
});
|
|
516
|
-
},
|
|
537
|
+
}, X = (e) => ({
|
|
517
538
|
promptId: String(e.promptId),
|
|
518
539
|
digest: String(e.digest),
|
|
519
540
|
revision: Number(e.revision) || 1,
|
|
@@ -522,23 +543,23 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
522
543
|
system: e.system ?? null,
|
|
523
544
|
firstSeenAt: e.firstSeenAt ?? null,
|
|
524
545
|
lastUsedAt: e.lastUsedAt ?? null
|
|
525
|
-
}),
|
|
526
|
-
|
|
527
|
-
let n = t.digest ??
|
|
546
|
+
}), Je = (e, t) => d.gen(function* () {
|
|
547
|
+
qe();
|
|
548
|
+
let n = t.digest ?? We(t).digest;
|
|
528
549
|
return yield* d.promise(async () => {
|
|
529
|
-
let r = (await e.query(
|
|
550
|
+
let r = (await e.query(x(Y).where(h(g("promptId", t.id), g("digest", n))).descriptor))[0];
|
|
530
551
|
if (r !== void 0) {
|
|
531
552
|
let t = /* @__PURE__ */ new Date();
|
|
532
|
-
return await e.updateMany(
|
|
553
|
+
return await e.updateMany(J, { lastUsedAt: t }, { where: g("id", String(r.id)) }), X({
|
|
533
554
|
...r,
|
|
534
555
|
lastUsedAt: t
|
|
535
556
|
});
|
|
536
557
|
}
|
|
537
|
-
let i = (await e.query(
|
|
538
|
-
id:
|
|
558
|
+
let i = (await e.query(x(Y).where(g("promptId", t.id)).descriptor)).reduce((e, t) => Math.max(e, Number(t.revision) || 0), 0) + 1, a = /* @__PURE__ */ new Date(), o = {
|
|
559
|
+
id: _({
|
|
539
560
|
kind: "typeid",
|
|
540
561
|
prefix: "prm"
|
|
541
|
-
},
|
|
562
|
+
}, J),
|
|
542
563
|
promptId: t.id,
|
|
543
564
|
digest: n,
|
|
544
565
|
revision: i,
|
|
@@ -549,54 +570,54 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
549
570
|
firstSeenAt: a,
|
|
550
571
|
lastUsedAt: a
|
|
551
572
|
};
|
|
552
|
-
return
|
|
573
|
+
return X(await e.insertIgnore(J, o, { conflictColumns: ["promptId", "digest"] }) ?? o);
|
|
553
574
|
});
|
|
554
|
-
}),
|
|
555
|
-
let n = (await e.query(
|
|
556
|
-
return n === void 0 ? null :
|
|
557
|
-
}),
|
|
558
|
-
id:
|
|
559
|
-
tenantId:
|
|
560
|
-
executionId:
|
|
561
|
-
workflowName:
|
|
562
|
-
runId:
|
|
563
|
-
signalName:
|
|
564
|
-
stepName:
|
|
565
|
-
status:
|
|
575
|
+
}), Ye = (e, t) => d.promise(async () => {
|
|
576
|
+
let n = (await e.query(x(Y).where(g("digest", t)).descriptor))[0];
|
|
577
|
+
return n === void 0 ? null : X(n);
|
|
578
|
+
}), Xe = (e, t) => d.promise(async () => [...await e.query(x(Y).where(g("promptId", t)).orderBy("revision", "desc").descriptor)].sort((e, t) => (Number(t.revision) || 0) - (Number(e.revision) || 0)).map(X)), Z = "_voltro_ai_inferences", Ze = w(Z, {
|
|
579
|
+
id: v({ prefix: "aiinf" }),
|
|
580
|
+
tenantId: T().nullable(),
|
|
581
|
+
executionId: T(),
|
|
582
|
+
workflowName: T(),
|
|
583
|
+
runId: T().nullable(),
|
|
584
|
+
signalName: T(),
|
|
585
|
+
stepName: T(),
|
|
586
|
+
status: T().oneOf([
|
|
566
587
|
"pending",
|
|
567
588
|
"running",
|
|
568
589
|
"succeeded",
|
|
569
590
|
"failed"
|
|
570
591
|
]).default("pending"),
|
|
571
|
-
kind:
|
|
592
|
+
kind: T().oneOf([
|
|
572
593
|
"text",
|
|
573
594
|
"object",
|
|
574
595
|
"transcription"
|
|
575
596
|
]).default("text"),
|
|
576
|
-
provider:
|
|
577
|
-
model:
|
|
578
|
-
prompt:
|
|
579
|
-
system:
|
|
580
|
-
promptId:
|
|
581
|
-
promptDigest:
|
|
582
|
-
promptRevision:
|
|
583
|
-
maxTokens:
|
|
584
|
-
providerOptions:
|
|
585
|
-
jsonSchema:
|
|
586
|
-
transcription:
|
|
587
|
-
result:
|
|
588
|
-
errorMessage:
|
|
589
|
-
attempts:
|
|
590
|
-
leaseUntil:
|
|
591
|
-
claimedBy:
|
|
592
|
-
enqueuedAt:
|
|
593
|
-
startedAt:
|
|
594
|
-
completedAt:
|
|
595
|
-
durationMs:
|
|
596
|
-
}).index(["status", "enqueuedAt"]).index(["status", "leaseUntil"]).index(["executionId"]).index(["tenantId"]),
|
|
597
|
+
provider: T(),
|
|
598
|
+
model: T(),
|
|
599
|
+
prompt: T(),
|
|
600
|
+
system: T().nullable(),
|
|
601
|
+
promptId: T().nullable(),
|
|
602
|
+
promptDigest: T().nullable(),
|
|
603
|
+
promptRevision: y().nullable(),
|
|
604
|
+
maxTokens: y().nullable(),
|
|
605
|
+
providerOptions: b().nullable(),
|
|
606
|
+
jsonSchema: b().nullable(),
|
|
607
|
+
transcription: b().nullable(),
|
|
608
|
+
result: b().nullable(),
|
|
609
|
+
errorMessage: T().nullable(),
|
|
610
|
+
attempts: y().default(0),
|
|
611
|
+
leaseUntil: E().nullable(),
|
|
612
|
+
claimedBy: T().nullable(),
|
|
613
|
+
enqueuedAt: E().default("now"),
|
|
614
|
+
startedAt: E().nullable(),
|
|
615
|
+
completedAt: E().nullable(),
|
|
616
|
+
durationMs: y().nullable()
|
|
617
|
+
}).index(["status", "enqueuedAt"]).index(["status", "leaseUntil"]).index(["executionId"]).index(["tenantId"]), Qe = 3e5, $e = 4, Q = (e, t) => {
|
|
597
618
|
let n = typeof e == "number" ? e : Number(e);
|
|
598
619
|
return Number.isFinite(n) ? Math.trunc(n) : t;
|
|
599
|
-
}, $ = (e) => e == null ? null : String(e),
|
|
620
|
+
}, $ = (e) => e == null ? null : String(e), et = (e) => ({
|
|
600
621
|
id: String(e.id),
|
|
601
622
|
tenantId: $(e.tenantId),
|
|
602
623
|
executionId: String(e.executionId),
|
|
@@ -617,7 +638,7 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
617
638
|
jsonSchema: e.jsonSchema ?? null,
|
|
618
639
|
transcription: e.transcription ?? null,
|
|
619
640
|
attempts: Q(e.attempts, 0)
|
|
620
|
-
}),
|
|
641
|
+
}), tt = (e) => {
|
|
621
642
|
let t = e.attempts + 1;
|
|
622
643
|
return t >= e.maxAttempts ? {
|
|
623
644
|
kind: "give-up",
|
|
@@ -627,6 +648,6 @@ var ae = (e, t) => e(t), D = Symbol.for("@voltro/ai/Tool"), oe = (e) => ({
|
|
|
627
648
|
attempts: t,
|
|
628
649
|
delayMs: e.retryAfterMs !== void 0 && e.retryAfterMs > 0 ? e.retryAfterMs : Math.min(e.baseDelayMs * 2 ** (t - 1), e.maxDelayMs)
|
|
629
650
|
};
|
|
630
|
-
},
|
|
651
|
+
}, nt = (e) => `voltro.ai.infer:${e}`, rt = (e, t) => `aiinf_${ie("sha256").update(`${e}\u0000${t}`).digest("hex").slice(0, 40)}`;
|
|
631
652
|
//#endregion
|
|
632
|
-
export {
|
|
653
|
+
export { Oe as A, _e as B, Fe as C, ke as D, Ne as E, D as F, ve as G, k as H, R as I, he as J, xe as K, ye as L, L as M, P as N, Ae as O, I as P, F as R, Pe as S, je as T, M as U, j as V, A as W, ce as X, se as Y, le as Z, Xe as _, tt as a, Re as b, et as c, Be as d, We as f, Ue as g, Ye as h, Ze as i, W as j, H as k, Ke as l, ze as m, $e as n, nt as o, Ge as p, ge as q, Qe as r, rt as s, Z as t, J as u, Y as v, Me as w, G as x, Je as y, N as z };
|