@voltro/ai 0.85.0 → 0.86.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.
Files changed (60) hide show
  1. package/CHANGELOG.md +666 -0
  2. package/THIRD-PARTY-NOTICES.md +5 -33
  3. package/dist/agent.js +64 -2
  4. package/dist/agentConstants.js +4 -0
  5. package/dist/agentExecutor.js +51 -0
  6. package/dist/api.js +199 -0
  7. package/dist/appTools.js +80 -0
  8. package/dist/budget.js +201 -0
  9. package/dist/callEvidence.js +51 -0
  10. package/dist/copilot.js +126 -0
  11. package/dist/cost.js +242 -0
  12. package/dist/embed.js +41 -0
  13. package/dist/embeddingProvider.js +51 -0
  14. package/dist/eval.js +130 -0
  15. package/dist/events.d.ts +4 -0
  16. package/dist/events.js +81 -2
  17. package/dist/executableModalities.js +13 -0
  18. package/dist/gateway.js +58 -0
  19. package/dist/gatewayCatalog.js +47 -0
  20. package/dist/gatewayMedia.js +88 -0
  21. package/dist/index.d.ts +11 -0
  22. package/dist/index.js +42 -2750
  23. package/dist/inferenceDispatch.js +493 -0
  24. package/dist/inferenceQueue.js +96 -0
  25. package/dist/jsonOutputValidation.js +27 -0
  26. package/dist/mcpClient.js +332 -0
  27. package/dist/mcpTools.js +80 -0
  28. package/dist/media.js +182 -0
  29. package/dist/mediaBytes.js +19 -0
  30. package/dist/mediaCall.js +13 -0
  31. package/dist/mediaTaskProvider.js +16 -0
  32. package/dist/middleware.js +32 -0
  33. package/dist/observe.js +52 -0
  34. package/dist/prompts.js +103 -0
  35. package/dist/provider.js +303 -0
  36. package/dist/realtimeEvent.js +97 -0
  37. package/dist/realtimeSession.js +154 -0
  38. package/dist/rerank.js +61 -0
  39. package/dist/resumableStream.js +321 -0
  40. package/dist/retrieveReranked.js +16 -0
  41. package/dist/runAndPersist.js +148 -0
  42. package/dist/semanticCache.js +66 -0
  43. package/dist/speechProvider.js +16 -0
  44. package/dist/streamRegistry.js +14 -0
  45. package/dist/streamText.js +170 -0
  46. package/dist/streamTranscribe.js +74 -0
  47. package/dist/streamWakeups.js +44 -0
  48. package/dist/structuredEvidence.js +48 -0
  49. package/dist/test.js +1 -1
  50. package/dist/threads.js +92 -0
  51. package/dist/tools.js +68 -0
  52. package/dist/transcribe.js +64 -0
  53. package/dist/transcriptionEvent.js +46 -0
  54. package/dist/workflow.d.ts +2 -0
  55. package/dist/workflow.js +115 -110
  56. package/package.json +5 -5
  57. package/dist/agentDescriptor-DhoiXVqO.js +0 -63
  58. package/dist/agentEvent-Bl4egdM5.js +0 -211
  59. package/dist/inferenceQueue-DcEulZep.js +0 -1077
  60. package/dist/provider-BnemgE4D.js +0 -409
@@ -1,11 +1,11 @@
1
1
  # Third-Party Notices — @voltro/ai
2
2
 
3
- This package depends on the open-source software listed below. Each is the
3
+ This package uses the open-source software listed below. Each is the
4
4
  property of its respective copyright holders and is used under the terms of
5
5
  its license. This file is provided for attribution; it grants no rights in
6
6
  @voltro/ai itself, which is proprietary (see LICENSE).
7
7
 
8
- Generated from the resolved runtime dependency closure (32 packages).
8
+ Generated from the resolved runtime dependency closure (31 packages).
9
9
 
10
10
  ---
11
11
 
@@ -859,34 +859,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
859
859
  SOFTWARE.
860
860
  ```
861
861
 
862
- ## layerr@3.0.0
863
-
864
- License: MIT
865
-
866
- ```
867
- MIT License
868
-
869
- Copyright (c) 2020 Perry Mitchell
870
-
871
- Permission is hereby granted, free of charge, to any person obtaining a copy
872
- of this software and associated documentation files (the "Software"), to deal
873
- in the Software without restriction, including without limitation the rights
874
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
875
- copies of the Software, and to permit persons to whom the Software is
876
- furnished to do so, subject to the following conditions:
877
-
878
- The above copyright notice and this permission notice shall be included in all
879
- copies or substantial portions of the Software.
880
-
881
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
882
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
883
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
884
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
885
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
886
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
887
- SOFTWARE.
888
- ```
889
-
890
862
  ## ms@2.1.3
891
863
 
892
864
  License: MIT
@@ -1236,14 +1208,14 @@ Apache License
1236
1208
  limitations under the License.
1237
1209
  ```
1238
1210
 
1239
- ## ulidx@2.4.1
1211
+ ## ulid@3.0.2
1240
1212
 
1241
1213
  License: MIT
1242
1214
 
1243
1215
  ```
1244
- MIT License
1216
+ The MIT License (MIT)
1245
1217
 
1246
- Copyright (c) 2021 Perry Mitchell
1218
+ Copyright (c) 2017 Alizain Feerasta
1247
1219
 
1248
1220
  Permission is hereby granted, free of charge, to any person obtaining a copy
1249
1221
  of this software and associated documentation files (the "Software"), to deal
package/dist/agent.js CHANGED
@@ -1,2 +1,64 @@
1
- import { a as e, i as t, n, o as r, r as i, t as a } from "./agentDescriptor-DhoiXVqO.js";
2
- export { a as AgentThreadAccessDenied, n as agentInputSchema, i as agentMessageSchema, t as agentRouteDescriptors, e as defineAgent, r as isAgentDescriptor };
1
+ import { AGENT_MESSAGES_TABLE as e } from "./agentConstants.js";
2
+ import { Schema as t } from "effect";
3
+ import { defineAction as n, defineQuery as r } from "@voltro/protocol";
4
+ //#region src/agentDescriptor.ts
5
+ var i = t.Struct({ prompt: t.String }), a = "@voltro/ai/AgentDescriptor", o = class extends t.TaggedError()("AgentThreadAccessDenied", { threadId: t.String }) {
6
+ get message() {
7
+ return `agent thread '${this.threadId}' belongs to another subject`;
8
+ }
9
+ }, s = (e) => {
10
+ if (e.guards !== void 0 && e.guards.length === 0) throw Error(`defineAgent("${e.name}"): \`guards: []\` is empty, so it enforces nothing —\n but it reads at the call site as if the agent were protected. Omit the field
11
+ and decide with \`openAccess\`, or list the scopes that may talk to it.`);
12
+ if (e.openAccess !== void 0 && e.guards !== void 0 && e.guards.length > 0) throw Error(`defineAgent("${e.name}"): \`openAccess\` and \`guards\` are two different access\n decisions, so declaring both says the agent is protected AND open. Keep the
13
+ guards if a caller must hold a scope; drop them if anyone may talk to it.`);
14
+ return {
15
+ _brand: a,
16
+ name: e.name,
17
+ ...e.input === void 0 ? {} : { input: e.input },
18
+ ...e.guards === void 0 ? {} : { guards: e.guards },
19
+ ...e.openAccess === void 0 ? {} : { openAccess: e.openAccess }
20
+ };
21
+ }, c = (e) => typeof e == "object" && !!e && e._brand === a, l = (e) => e.input ?? i, u = t.StructWithRest(t.Struct({ type: t.String }), [t.Record(t.String, t.Unknown)]), d = t.Struct({
22
+ id: t.String,
23
+ threadId: t.String,
24
+ role: t.String,
25
+ content: t.String,
26
+ streaming: t.Boolean,
27
+ order: t.Number,
28
+ stepOrder: t.Number,
29
+ parts: t.Array(u)
30
+ }), f = (e) => {
31
+ let n = l(e), r = t.Struct({
32
+ threadId: t.String,
33
+ order: t.Number
34
+ });
35
+ try {
36
+ return t.Struct({
37
+ ...n.fields,
38
+ ...r.fields
39
+ });
40
+ } catch {
41
+ return r;
42
+ }
43
+ }, p = (i) => {
44
+ let a = {};
45
+ return i.openAccess === void 0 ? i.guards !== void 0 && (a.guards = i.guards) : a.openAccess = i.openAccess, {
46
+ send: n({
47
+ name: `${i.name}.send`,
48
+ input: f(i),
49
+ output: t.Struct({ text: t.String }),
50
+ error: o,
51
+ ...a
52
+ }),
53
+ messages: r({
54
+ name: `${i.name}.messages`,
55
+ input: t.Struct({ threadId: t.String }),
56
+ output: t.Array(d),
57
+ source: e,
58
+ error: o,
59
+ ...a
60
+ })
61
+ };
62
+ };
63
+ //#endregion
64
+ export { o as AgentThreadAccessDenied, l as agentInputSchema, d as agentMessageSchema, p as agentRouteDescriptors, s as defineAgent, c as isAgentDescriptor };
@@ -0,0 +1,4 @@
1
+ //#region src/agentConstants.ts
2
+ var e = "_voltro_agent_threads", t = "_voltro_agent_messages";
3
+ //#endregion
4
+ export { t as AGENT_MESSAGES_TABLE, e as AGENT_THREADS_TABLE };
@@ -0,0 +1,51 @@
1
+ import { AgentThreadAccessDenied as e } from "./agent.js";
2
+ import { appendMessage as t, ensureThreadOwned as n, getMessages as r, getThread as i } from "./threads.js";
3
+ import { historyToModelMessages as a, runAssistant as o } from "./runAndPersist.js";
4
+ import { Effect as s } from "effect";
5
+ //#region src/agentExecutor.ts
6
+ var c = "@voltro/ai/AgentExecutor", l = (e, t) => ({
7
+ _brand: c,
8
+ descriptor: e,
9
+ config: t
10
+ }), u = (e) => typeof e == "object" && !!e && e._brand === c, d = (e, t) => {
11
+ let n = e.config.system;
12
+ if (n !== void 0) return typeof n == "function" ? n(t) : n;
13
+ }, f = (e, t) => {
14
+ let n = e.config.model;
15
+ return typeof n == "function" ? n(t) : n;
16
+ }, p = (e, i, c) => s.gen(function* () {
17
+ yield* n(i, {
18
+ threadId: c.threadId,
19
+ subjectId: c.subjectId ?? null,
20
+ tenantId: c.tenantId ?? null
21
+ });
22
+ let s = c.promptOf ? c.promptOf(c.input) : String(c.input?.prompt ?? ""), l = d(e, c.input), u = f(e, c.input);
23
+ yield* t(i, {
24
+ threadId: c.threadId,
25
+ role: "user",
26
+ content: s,
27
+ tenantId: c.tenantId ?? null,
28
+ order: c.order
29
+ });
30
+ let p = yield* r(i, c.threadId), m = a(p);
31
+ return yield* o(i, {
32
+ threadId: c.threadId,
33
+ tenantId: c.tenantId ?? null,
34
+ prompt: s,
35
+ ...m.length > 0 ? { messages: m } : {},
36
+ ...l === void 0 ? {} : { system: l },
37
+ ...e.config.tools === void 0 ? {} : { tools: e.config.tools },
38
+ ...u === void 0 ? {} : { provider: u },
39
+ ...e.config.maxSteps === void 0 ? {} : { maxSteps: e.config.maxSteps },
40
+ ...e.config.providerOptions === void 0 ? {} : { providerOptions: e.config.providerOptions },
41
+ ...c.toolContext === void 0 ? {} : { toolContext: c.toolContext },
42
+ order: c.order + 1,
43
+ ...c.throttleMs === void 0 ? {} : { throttleMs: c.throttleMs },
44
+ agent: e.descriptor.name
45
+ });
46
+ }), m = (t, n, a = null) => s.gen(function* () {
47
+ let o = yield* i(t, n);
48
+ return o !== null && o.subjectId !== null && o.subjectId !== a ? yield* s.fail(new e({ threadId: n })) : yield* r(t, n);
49
+ });
50
+ //#endregion
51
+ export { l as defineAgentExecutor, u as isAgentExecutor, m as loadAgentMessages, f as resolveAgentModel, d as resolveSystem, p as runAgentTurn };
package/dist/api.js ADDED
@@ -0,0 +1,199 @@
1
+ import { providerFromEnv as e, resolveModel as t } from "./provider.js";
2
+ import { buildSdkTools as n } from "./tools.js";
3
+ import { aiCallAttributes as r, withAiCallSpan as i } from "./observe.js";
4
+ import { validatedOutputSchema as a } from "./jsonOutputValidation.js";
5
+ import { AiCallEvidenceSchema as o, normalizeAiCallEvidence as s } from "./callEvidence.js";
6
+ import { captureStructuredEvidence as c } from "./structuredEvidence.js";
7
+ import { NoObjectGeneratedError as l, Output as u, generateObject as d, generateText as f, jsonSchema as p, stepCountIs as m } from "ai";
8
+ import { Context as h, Effect as g, Schema as _ } from "effect";
9
+ import { jsonSchemaOf as v } from "@voltro/protocol";
10
+ //#region src/api.ts
11
+ var y = class extends _.TaggedError()("AiError", {
12
+ reason: _.Literals(["generation", "decode"]),
13
+ message: _.String,
14
+ evidence: _.optional(o)
15
+ }) {}, b = (e, t = {}) => {
16
+ let n = String(e?.message ?? e), r = x(e), i = new y({
17
+ reason: "generation",
18
+ message: `${t.prefix === void 0 ? "" : `${t.prefix}: `}${n}${r.length > 0 ? ` (${r.join(" <- ")})` : ""}`,
19
+ ...t.evidence === void 0 ? {} : { evidence: t.evidence }
20
+ });
21
+ return Object.defineProperty(i, "cause", {
22
+ value: e,
23
+ enumerable: !1,
24
+ configurable: !0,
25
+ writable: !0
26
+ }), i;
27
+ }, x = (e) => {
28
+ let t = [], n = e;
29
+ for (let e = 0; e < 8 && typeof n == "object" && n; e++) {
30
+ let e = n;
31
+ 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;
32
+ }
33
+ return t;
34
+ }, S = (e, t, n) => {
35
+ let r = [e, ...t ?? []], i = (e) => n(r[e]).pipe(g.catchTag("AiError", (t) => t.reason === "generation" && e + 1 < r.length ? i(e + 1) : g.fail(t)));
36
+ return i(0);
37
+ }, C = (a) => g.gen(function* () {
38
+ let o = a.messages === void 0 ? a.prompt === void 0 ? void 0 : { prompt: a.prompt } : { messages: [...a.messages] };
39
+ if (o === void 0 || a.prompt !== void 0 && a.messages !== void 0) return yield* g.fail(new y({
40
+ reason: "generation",
41
+ message: "generateText: supply exactly one of prompt or messages"
42
+ }));
43
+ let s = a.tools === void 0 ? void 0 : yield* g.context(), c = a.tools === void 0 || s === void 0 ? void 0 : n(a.tools, {
44
+ store: void 0,
45
+ ...a.toolContext,
46
+ ...a.abortSignal === void 0 ? {} : { abortSignal: a.abortSignal },
47
+ runtime: s
48
+ });
49
+ return yield* S(a.provider ?? e(), a.fallbacks, (e) => i(r(e, "generateText"), (e) => e.usage, g.tryPromise({
50
+ try: async () => {
51
+ let n = t(e), r = await f({
52
+ model: n,
53
+ ...o,
54
+ ...a.abortSignal === void 0 ? {} : { abortSignal: a.abortSignal },
55
+ ...c === void 0 ? {} : {
56
+ tools: c,
57
+ stopWhen: m(a.maxSteps ?? 8)
58
+ },
59
+ ...a.system === void 0 ? {} : { system: a.system },
60
+ ...a.maxTokens === void 0 ? {} : { maxOutputTokens: a.maxTokens },
61
+ ...a.temperature === void 0 ? {} : { temperature: a.temperature },
62
+ ...a.providerOptions === void 0 ? {} : { providerOptions: a.providerOptions }
63
+ }), i = c !== void 0 && r.totalUsage !== void 0 ? r.totalUsage : r.usage;
64
+ return {
65
+ text: r.text,
66
+ sources: r.sources,
67
+ usage: {
68
+ inputTokens: i.inputTokens,
69
+ outputTokens: i.outputTokens,
70
+ totalTokens: i.totalTokens
71
+ },
72
+ providerMetadata: r.providerMetadata
73
+ };
74
+ },
75
+ catch: (e) => b(e)
76
+ })));
77
+ }), w = (e, t) => g.gen(function* () {
78
+ let n = v(e);
79
+ if (n?.type !== "object") {
80
+ let e = n?.anyOf === void 0 ? `type:${JSON.stringify(n?.type ?? null)}` : "anyOf (e.g. Schema.Struct({}) or a union)";
81
+ return yield* g.fail(new y({
82
+ reason: "generation",
83
+ message: `${t}: the output schema must render as a top-level JSON object (type:"object"), but got ${e}. Use a Schema.Struct({ ... }) as the output schema — wrap arrays/scalars in a field (e.g. Schema.Struct({ items: Schema.Array(...) })).`
84
+ }));
85
+ }
86
+ return n;
87
+ }), T = (e, t) => w(e, t), E = (n) => S(n.provider ?? e(), n.fallbacks, (e) => i(r(e, "generateObject"), (e) => e.usage, g.gen(function* () {
88
+ let r = yield* g.try({
89
+ try: () => a(n.jsonSchema),
90
+ catch: () => new y({
91
+ reason: "decode",
92
+ message: "generateObjectJson: invalid or unsupported output JSON Schema; no provider call was made"
93
+ })
94
+ }), i = c(e.name, t(e)), o = i.model, u = yield* g.tryPromise({
95
+ try: () => d({
96
+ model: o,
97
+ schema: r,
98
+ prompt: n.prompt,
99
+ ...n.system === void 0 ? {} : { system: n.system },
100
+ ...n.maxTokens === void 0 ? {} : { maxOutputTokens: n.maxTokens },
101
+ ...n.temperature === void 0 ? {} : { temperature: n.temperature },
102
+ ...n.providerOptions === void 0 ? {} : { providerOptions: n.providerOptions }
103
+ }),
104
+ catch: (t) => l.isInstance(t) ? new y({
105
+ reason: "decode",
106
+ message: "model output failed JSON Schema validation",
107
+ evidence: i.evidence(t.usage)
108
+ }) : b(t, {
109
+ prefix: "structured provider call failed",
110
+ evidence: s(e.name, o.modelId, void 0)
111
+ })
112
+ });
113
+ return {
114
+ object: u.object,
115
+ evidence: s(e.name, o.modelId, u.usage, u.providerMetadata),
116
+ usage: {
117
+ inputTokens: u.usage.inputTokens,
118
+ outputTokens: u.usage.outputTokens,
119
+ totalTokens: u.usage.totalTokens
120
+ },
121
+ providerMetadata: u.providerMetadata
122
+ };
123
+ }))), D = (e) => g.gen(function* () {
124
+ let t = yield* E({
125
+ jsonSchema: yield* w(e.schema, "generateObject"),
126
+ prompt: e.prompt,
127
+ ...e.system === void 0 ? {} : { system: e.system },
128
+ ...e.provider === void 0 ? {} : { provider: e.provider },
129
+ ...e.fallbacks === void 0 ? {} : { fallbacks: e.fallbacks },
130
+ ...e.maxTokens === void 0 ? {} : { maxTokens: e.maxTokens },
131
+ ...e.temperature === void 0 ? {} : { temperature: e.temperature },
132
+ ...e.providerOptions === void 0 ? {} : { providerOptions: e.providerOptions }
133
+ }), n = yield* _.decodeUnknownEffect(e.schema)(t.object).pipe(g.mapError(() => new y({
134
+ reason: "decode",
135
+ message: "model output failed schema decode",
136
+ evidence: t.evidence
137
+ })));
138
+ return {
139
+ ...t,
140
+ object: n
141
+ };
142
+ }), O = (a) => g.gen(function* () {
143
+ let o = yield* g.context(), d = yield* w(a.schema, "generateObjectWithTools"), h = {
144
+ store: void 0,
145
+ ...a.toolContext,
146
+ ...a.abortSignal === void 0 ? {} : { abortSignal: a.abortSignal },
147
+ runtime: o
148
+ }, v = n(a.tools, h);
149
+ return yield* S(a.provider ?? e(), a.fallbacks, (e) => i(r(e, "generateObjectWithTools"), (e) => e.usage, g.gen(function* () {
150
+ let n = c(e.name, t(e)), r = n.model, i = yield* g.tryPromise({
151
+ try: () => f({
152
+ model: r,
153
+ prompt: a.prompt,
154
+ tools: v,
155
+ ...a.abortSignal === void 0 ? {} : { abortSignal: a.abortSignal },
156
+ stopWhen: m(a.maxSteps ?? 8),
157
+ output: u.object({ schema: p(d) }),
158
+ ...a.system === void 0 ? {} : { system: a.system },
159
+ ...a.maxTokens === void 0 ? {} : { maxOutputTokens: a.maxTokens },
160
+ ...a.temperature === void 0 ? {} : { temperature: a.temperature },
161
+ ...a.providerOptions === void 0 ? {} : { providerOptions: a.providerOptions }
162
+ }),
163
+ catch: (t) => l.isInstance(t) ? new y({
164
+ reason: "decode",
165
+ message: "model output failed structured decode",
166
+ evidence: n.evidence(t.usage)
167
+ }) : b(t, {
168
+ prefix: "structured provider call failed",
169
+ evidence: s(e.name, r.modelId, void 0)
170
+ })
171
+ }), o = i.totalUsage, h = n.evidence(o), x = yield* g.try({
172
+ try: () => i.output,
173
+ catch: () => new y({
174
+ reason: "decode",
175
+ message: "model output failed structured decode",
176
+ evidence: h
177
+ })
178
+ });
179
+ return {
180
+ object: yield* _.decodeUnknownEffect(a.schema)(x).pipe(g.mapError(() => new y({
181
+ reason: "decode",
182
+ message: "model output failed schema decode",
183
+ evidence: h
184
+ }))),
185
+ evidence: h,
186
+ usage: {
187
+ inputTokens: o.inputTokens,
188
+ outputTokens: o.outputTokens,
189
+ totalTokens: o.totalTokens
190
+ },
191
+ providerMetadata: i.providerMetadata
192
+ };
193
+ })));
194
+ }), k = class extends h.Service()("@voltro/AiService") {}, A = {
195
+ generateText: (e) => C(e),
196
+ generateObject: (e) => D(e)
197
+ };
198
+ //#endregion
199
+ export { y as AiError, k as AiService, b as aiGenerationError, A as defaultAiService, D as generateObject, E as generateObjectJson, O as generateObjectWithTools, C as generateText, T as outputJsonSchema };
@@ -0,0 +1,80 @@
1
+ import { defineTool as e } from "./tools.js";
2
+ //#region src/appTools.ts
3
+ var t = /* @__PURE__ */ new Map(), n = (e) => {
4
+ let n = t.get(e);
5
+ if (n !== void 0) return n;
6
+ let r = RegExp("^" + e.split("*").map((e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*") + "$");
7
+ return t.size >= 256 && t.delete(t.keys().next().value), t.set(e, r), r;
8
+ }, r = (e, t) => {
9
+ let n = t.lastIndex;
10
+ try {
11
+ return t.lastIndex = 0, t.test(e);
12
+ } finally {
13
+ t.lastIndex = n;
14
+ }
15
+ }, i = (e, t) => t instanceof RegExp ? r(e, t) : t.some((t) => n(t).test(e)), a = (e, t) => !(t.deny !== void 0 && i(e, t.deny) || t.allow !== void 0 && !i(e, t.allow)), o = (e) => e === "mutation" || e === "action", s = (e) => ({
16
+ id: e,
17
+ via: "agent"
18
+ }), c = (e, t) => {
19
+ let n = e.exposeAsTool;
20
+ if (n === void 0 || n === !1 || n === !0) return null;
21
+ let r = o(e.kind), i = n.confirm ?? (r && (t.requireConfirmForWrites ?? !0));
22
+ return {
23
+ description: n.description,
24
+ confirm: i,
25
+ ...n.maxPerRun === void 0 ? {} : { maxPerRun: n.maxPerRun }
26
+ };
27
+ }, l = (e, t) => {
28
+ if (!a(e.name, t)) return {
29
+ admitted: !1,
30
+ reason: "blocked by the allow/deny policy"
31
+ };
32
+ let n = c(e, t);
33
+ if (n === null) return {
34
+ admitted: !1,
35
+ reason: "not exposed as a tool (no `exposeAsTool: { description }` on the descriptor)"
36
+ };
37
+ let r = o(e.kind);
38
+ if (r && t.includeWrites !== !0) return {
39
+ admitted: !1,
40
+ reason: "a write tool, and includeWrites is not enabled"
41
+ };
42
+ let i = e.requiresApproval !== void 0 && e.requiresApproval !== null;
43
+ return n.confirm && !i ? {
44
+ admitted: !1,
45
+ reason: "the tool requires human confirmation (`confirm`) and nothing enforces it — add `requiresApproval: { approvers: [...] }` to the descriptor so the call parks as a durable approval, or set `exposeAsTool: { confirm: false }` if an agent may run it unattended"
46
+ } : {
47
+ admitted: !0,
48
+ spec: {
49
+ name: e.name,
50
+ kind: e.kind,
51
+ description: n.description,
52
+ write: r,
53
+ confirm: n.confirm,
54
+ approvalBacked: i,
55
+ ...n.maxPerRun === void 0 ? {} : { maxPerRun: n.maxPerRun }
56
+ }
57
+ };
58
+ }, u = (e, t = {}) => e.filter((e) => a(e.descriptor.name, t)).map((e) => ({
59
+ entry: e,
60
+ exposure: c(e.descriptor, t)
61
+ })).filter((e) => e.exposure !== null).map(({ entry: e, exposure: t }) => ({
62
+ name: e.descriptor.name,
63
+ kind: e.descriptor.kind,
64
+ description: t.description,
65
+ write: o(e.descriptor.kind),
66
+ confirm: t.confirm,
67
+ approvalBacked: e.descriptor.requiresApproval !== void 0 && e.descriptor.requiresApproval !== null,
68
+ ...t.maxPerRun === void 0 ? {} : { maxPerRun: t.maxPerRun }
69
+ })).sort((e, t) => e.name.localeCompare(t.name)), d = (t, n = {}) => t.map((e) => ({
70
+ entry: e,
71
+ decision: l(e.descriptor, n)
72
+ })).filter((e) => e.decision.admitted).map(({ entry: t, decision: n }) => e({
73
+ name: t.descriptor.name,
74
+ description: n.spec.description,
75
+ input: t.descriptor.input,
76
+ ...t.descriptor.output === void 0 ? {} : { output: t.descriptor.output },
77
+ handler: (e) => Promise.resolve(t.invoke(e))
78
+ }));
79
+ //#endregion
80
+ export { s as agentActor, l as appToolDecision, d as appTools, a as passesPolicy, u as synthesizeToolSpecs };
package/dist/budget.js ADDED
@@ -0,0 +1,201 @@
1
+ import { Effect as e, Schema as t } from "effect";
2
+ import { and as n, bigint as r, eq as i, generateId as a, id as o, isNull as s, queryFor as c, registerRetention as l, retentionTtlMsFromEnv as u, table as d, text as f, timestamp as p } from "@voltro/database";
3
+ //#region src/budget.ts
4
+ var m = class extends t.TaggedError()("AiBudgetExceeded", {
5
+ tenantId: t.NullOr(t.String),
6
+ limitUsd: t.Number,
7
+ spentUsd: t.Number,
8
+ attemptedUsd: t.Number
9
+ }) {}, h = class extends t.TaggedError()("AiBudgetInvalidInput", {
10
+ field: t.Literals([
11
+ "limitUsd",
12
+ "addUsd",
13
+ "since",
14
+ "reservationId",
15
+ "tenantId"
16
+ ]),
17
+ message: t.String
18
+ }) {}, g = class extends t.TaggedError()("AiBudgetReservationConflict", {
19
+ reason: t.Literals(["identity-bound", "already-released"]),
20
+ message: t.String
21
+ }) {}, _ = "_voltro_ai_budget", v = "_voltro_ai_budget_reservations", y = d(_, {
22
+ id: o({ prefix: "aib" }),
23
+ bucket: f().maxLength(191),
24
+ tenantId: f().nullable(),
25
+ reservedMicroUsd: r().default("0").narrowedFrom("integer"),
26
+ updatedAt: p().default("now")
27
+ }).unique("byAiBudgetBucket", ["bucket"]), b = d(v, {
28
+ id: o({ prefix: "air" }),
29
+ reservationId: f().maxLength(191),
30
+ bucket: f().maxLength(191),
31
+ tenantId: f().nullable(),
32
+ amountMicroUsd: r(),
33
+ releasedAt: p().nullable(),
34
+ updatedAt: p().default("now")
35
+ }).unique("byAiBudgetReservation", ["reservationId"]), x = class extends Error {}, S = (e, t, n) => {
36
+ if (!Number.isFinite(e) || e < 0) throw new h({
37
+ field: t,
38
+ message: `${t} must be finite and non-negative`
39
+ });
40
+ let r = e * 1e6, i = n === "up" ? Math.ceil(r) : Math.floor(r);
41
+ if (!Number.isSafeInteger(i)) throw new h({
42
+ field: t,
43
+ message: `${t} exceeds the safe micro-USD range`
44
+ });
45
+ return BigInt(i);
46
+ }, C = (e) => {
47
+ if (typeof e == "number" && !Number.isSafeInteger(e)) throw Error("AI budget counter is not an exact integer");
48
+ let t = String(e);
49
+ if (!/^(0|[1-9][0-9]*)$/.test(t)) throw Error("AI budget counter is invalid; refusing to assume zero");
50
+ return BigInt(t);
51
+ }, w = (e) => {
52
+ if (typeof e != "string" || e.length === 0 || e.length > 191) throw new h({
53
+ field: "reservationId",
54
+ message: "AI budget reservationId must contain 1–191 characters"
55
+ });
56
+ return e;
57
+ }, T = () => {
58
+ let e = u(process.env.VOLTRO_AI_USAGE_TTL_HOURS, 8760);
59
+ for (let t of [_, v]) l({
60
+ source: "plugin",
61
+ table: t,
62
+ timeColumn: "updatedAt",
63
+ ttlMs: e
64
+ });
65
+ }, E = async (e, t) => {
66
+ for (let n = 0;; n++) try {
67
+ return await e.transactional(t);
68
+ } catch (e) {
69
+ if (!(e instanceof x) || n >= 31) throw e;
70
+ await new Promise((e) => setTimeout(e, n + 1));
71
+ }
72
+ }, D = (t) => t instanceof m || t instanceof h || t instanceof g ? e.fail(t) : e.die(t), O = (t) => e.tryPromise({
73
+ try: t,
74
+ catch: (e) => e
75
+ }).pipe(e.catch(D)), k = (t, r) => e.gen(function* () {
76
+ let { limit: o, amount: s, tenantId: l, bucket: u, reservationId: d } = yield* e.try({
77
+ try: () => {
78
+ let e = S(r.limitUsd, "limitUsd", "down"), t = S(r.addUsd ?? 0, "addUsd", "up"), n = r.tenantId ?? null;
79
+ if (r.since !== void 0 && (!(r.since instanceof Date) || !Number.isFinite(r.since.getTime()))) throw new h({
80
+ field: "since",
81
+ message: "AI budget since must be a valid Date"
82
+ });
83
+ let i = `${n ?? "global"}::${r.since === void 0 ? "all" : r.since.getTime()}`;
84
+ if (i.length > 191) throw new h({
85
+ field: "tenantId",
86
+ message: "AI budget tenant/window key exceeds 191 characters"
87
+ });
88
+ return {
89
+ limit: e,
90
+ amount: t,
91
+ tenantId: n,
92
+ bucket: i,
93
+ reservationId: w(r.reservationId ?? a({
94
+ kind: "typeid",
95
+ prefix: "air"
96
+ }, "_voltro_ai_budget_reservations"))
97
+ };
98
+ },
99
+ catch: (e) => e
100
+ }).pipe(e.catch(D));
101
+ T();
102
+ let f = async (e) => {
103
+ let [t] = await e.query(c(y).where(i("bucket", u)).descriptor);
104
+ if (t && t.tenantId !== l) throw Error("AI budget bucket belongs to a different tenant");
105
+ return t;
106
+ }, p = (e) => {
107
+ if (s === 0n ? e >= o : e + s > o) throw new m({
108
+ tenantId: l,
109
+ limitUsd: r.limitUsd,
110
+ spentUsd: Number(e) / 1e6,
111
+ attemptedUsd: r.addUsd ?? 0
112
+ });
113
+ };
114
+ return s === 0n ? yield* O(async () => {
115
+ let e = await f(t);
116
+ return p(e ? C(e.reservedMicroUsd) : 0n), null;
117
+ }) : yield* O(() => E(t, async (e) => {
118
+ let t = a({
119
+ kind: "typeid",
120
+ prefix: "air"
121
+ }, v), r = /* @__PURE__ */ new Date(), o = await e.insertIgnore(v, {
122
+ id: t,
123
+ reservationId: d,
124
+ bucket: u,
125
+ tenantId: l,
126
+ amountMicroUsd: String(s),
127
+ releasedAt: null,
128
+ updatedAt: r
129
+ }, { conflictColumns: ["reservationId"] });
130
+ if (o.id !== t) {
131
+ if (o.tenantId !== l || o.bucket !== u || C(o.amountMicroUsd) !== s) throw new g({
132
+ reason: "identity-bound",
133
+ message: "AI budget reservationId is already bound to another tenant, amount or window"
134
+ });
135
+ if (o.releasedAt !== null) throw new g({
136
+ reason: "already-released",
137
+ message: "AI budget reservation was released; new spend requires a new reservationId"
138
+ });
139
+ return {
140
+ reservationId: d,
141
+ tenantId: l,
142
+ amountMicroUsd: String(s),
143
+ status: "replayed"
144
+ };
145
+ }
146
+ let c = await f(e), m = c ? C(c.reservedMicroUsd) : 0n;
147
+ if (p(m), c) {
148
+ if (await e.updateMany("_voltro_ai_budget", {
149
+ reservedMicroUsd: String(m + s),
150
+ updatedAt: r
151
+ }, { where: n(i("id", c.id), i("reservedMicroUsd", c.reservedMicroUsd)) }) !== 1) throw new x("AI budget counter changed; discard this receipt and retry");
152
+ } else {
153
+ let t = a({
154
+ kind: "typeid",
155
+ prefix: "aib"
156
+ }, _);
157
+ if ((await e.insertIgnore("_voltro_ai_budget", {
158
+ id: t,
159
+ bucket: u,
160
+ tenantId: l,
161
+ reservedMicroUsd: String(s),
162
+ updatedAt: r
163
+ }, { conflictColumns: ["bucket"] })).id !== t) throw new x("AI budget counter was created concurrently; discard this receipt and retry");
164
+ }
165
+ return {
166
+ reservationId: d,
167
+ tenantId: l,
168
+ amountMicroUsd: String(s),
169
+ status: "reserved"
170
+ };
171
+ }));
172
+ }), A = (t, r) => e.gen(function* () {
173
+ return yield* e.try({
174
+ try: () => w(r.reservationId),
175
+ catch: (e) => e
176
+ }).pipe(e.catch((t) => t instanceof h ? e.fail(t) : e.die(t))), yield* e.promise(() => E(t, async (e) => {
177
+ let t = c(b).where(n(i("reservationId", r.reservationId), r.tenantId === null ? s("tenantId") : i("tenantId", r.tenantId))).descriptor, [a] = await e.query(t);
178
+ if (!a) return "not-found";
179
+ if (a.releasedAt !== null) return "already-released";
180
+ let o = /* @__PURE__ */ new Date();
181
+ if (await e.updateMany("_voltro_ai_budget_reservations", {
182
+ releasedAt: o,
183
+ updatedAt: o
184
+ }, { where: n(i("id", a.id), s("releasedAt")) }) !== 1) {
185
+ let [n] = await e.query(t);
186
+ if (n !== void 0 && n.releasedAt !== null) return "already-released";
187
+ throw new x("AI budget release raced; retry");
188
+ }
189
+ let [l] = await e.query(c(y).where(i("bucket", a.bucket)).descriptor);
190
+ if (!l || l.tenantId !== r.tenantId) throw Error("AI budget counter missing or mismatched; refusing unproved credit");
191
+ let u = C(l.reservedMicroUsd), d = C(a.amountMicroUsd);
192
+ if (d > u) throw Error("AI budget receipt exceeds its counter; refusing unproved credit");
193
+ if (await e.updateMany("_voltro_ai_budget", {
194
+ reservedMicroUsd: String(u - d),
195
+ updatedAt: o
196
+ }, { where: n(i("id", l.id), i("reservedMicroUsd", l.reservedMicroUsd)) }) !== 1) throw new x("AI budget counter changed; discard this release and retry");
197
+ return "released";
198
+ }));
199
+ });
200
+ //#endregion
201
+ export { v as AI_BUDGET_RESERVATIONS_TABLE, _ as AI_BUDGET_TABLE, m as AiBudgetExceeded, h as AiBudgetInvalidInput, g as AiBudgetReservationConflict, b as aiBudgetReservationsTable, y as aiBudgetTable, A as releaseAiBudget, k as requireAiBudget };