@voltro/ai 0.85.0 → 0.87.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 +5987 -0
- package/THIRD-PARTY-NOTICES.md +4723 -519
- package/dist/agent.js +64 -2
- package/dist/agentConstants.js +4 -0
- package/dist/agentExecutor.js +55 -0
- package/dist/aiSettings.js +11 -0
- package/dist/api.js +199 -0
- package/dist/appTools.js +80 -0
- package/dist/budget.js +201 -0
- package/dist/callEvidence.js +51 -0
- package/dist/copilot.js +126 -0
- package/dist/cost.js +242 -0
- package/dist/embed.js +41 -0
- package/dist/embeddingProvider.js +69 -0
- package/dist/eval.js +130 -0
- package/dist/events.d.ts +4 -0
- package/dist/events.js +81 -2
- package/dist/executableModalities.js +13 -0
- package/dist/gateway.js +58 -0
- package/dist/gatewayCatalog.js +47 -0
- package/dist/gatewayMedia.js +88 -0
- package/dist/index.d.ts +60 -2
- package/dist/index.js +42 -2750
- package/dist/inferenceDispatch.js +493 -0
- package/dist/inferenceQueue.js +96 -0
- package/dist/jsonOutputValidation.js +27 -0
- package/dist/mcpClient.js +332 -0
- package/dist/mcpTools.js +80 -0
- package/dist/media.js +182 -0
- package/dist/mediaBytes.js +19 -0
- package/dist/mediaCall.js +13 -0
- package/dist/mediaTaskProvider.js +16 -0
- package/dist/middleware.js +32 -0
- package/dist/observe.js +52 -0
- package/dist/prompts.js +103 -0
- package/dist/provider.js +319 -0
- package/dist/realtimeEvent.js +97 -0
- package/dist/realtimeSession.js +154 -0
- package/dist/rerank.js +61 -0
- package/dist/resumableStream.js +321 -0
- package/dist/retrieveReranked.js +16 -0
- package/dist/runAndPersist.js +147 -0
- package/dist/semanticCache.js +66 -0
- package/dist/speechProvider.js +16 -0
- package/dist/streamRegistry.js +14 -0
- package/dist/streamText.js +170 -0
- package/dist/streamTranscribe.js +74 -0
- package/dist/streamWakeups.js +44 -0
- package/dist/structuredEvidence.js +48 -0
- package/dist/test.js +1 -1
- package/dist/threads.js +92 -0
- package/dist/tools.js +68 -0
- package/dist/transcribe.js +64 -0
- package/dist/transcriptionEvent.js +46 -0
- package/dist/workflow.d.ts +2 -0
- package/dist/workflow.js +115 -110
- package/package.json +6 -5
- package/dist/agentDescriptor-DhoiXVqO.js +0 -63
- package/dist/agentEvent-Bl4egdM5.js +0 -211
- package/dist/inferenceQueue-DcEulZep.js +0 -1077
- package/dist/provider-BnemgE4D.js +0 -409
package/dist/agent.js
CHANGED
|
@@ -1,2 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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,55 @@
|
|
|
1
|
+
import { providerFromEnv as e } from "./provider.js";
|
|
2
|
+
import { AgentThreadAccessDenied as t } from "./agent.js";
|
|
3
|
+
import { appendMessage as n, ensureThreadOwned as r, getMessages as i, getThread as a } from "./threads.js";
|
|
4
|
+
import { historyToModelMessages as o, runAssistant as s } from "./runAndPersist.js";
|
|
5
|
+
import { Effect as c } from "effect";
|
|
6
|
+
//#region src/agentExecutor.ts
|
|
7
|
+
var l = "@voltro/ai/AgentExecutor", u = (e, t) => ({
|
|
8
|
+
_brand: l,
|
|
9
|
+
descriptor: e,
|
|
10
|
+
config: t
|
|
11
|
+
}), d = (e) => typeof e == "object" && !!e && e._brand === l, f = (e, t) => {
|
|
12
|
+
let n = e.config.system;
|
|
13
|
+
if (n !== void 0) return typeof n == "function" ? n(t) : n;
|
|
14
|
+
}, p = (e, t) => {
|
|
15
|
+
let n = e.config.model;
|
|
16
|
+
return typeof n == "function" ? n(t) : n;
|
|
17
|
+
}, m = (t, a, l) => c.gen(function* () {
|
|
18
|
+
yield* r(a, {
|
|
19
|
+
threadId: l.threadId,
|
|
20
|
+
subjectId: l.subjectId ?? null,
|
|
21
|
+
tenantId: l.tenantId ?? null
|
|
22
|
+
});
|
|
23
|
+
let u = l.promptOf ? l.promptOf(l.input) : String(l.input?.prompt ?? ""), d = f(t, l.input), m = p(t, l.input) ?? (yield* c.try({
|
|
24
|
+
try: () => e(),
|
|
25
|
+
catch: (e) => e instanceof Error ? e : Error(String(e))
|
|
26
|
+
}));
|
|
27
|
+
yield* n(a, {
|
|
28
|
+
threadId: l.threadId,
|
|
29
|
+
role: "user",
|
|
30
|
+
content: u,
|
|
31
|
+
tenantId: l.tenantId ?? null,
|
|
32
|
+
order: l.order
|
|
33
|
+
});
|
|
34
|
+
let h = yield* i(a, l.threadId), g = o(h);
|
|
35
|
+
return yield* s(a, {
|
|
36
|
+
threadId: l.threadId,
|
|
37
|
+
tenantId: l.tenantId ?? null,
|
|
38
|
+
prompt: u,
|
|
39
|
+
...g.length > 0 ? { messages: g } : {},
|
|
40
|
+
...d === void 0 ? {} : { system: d },
|
|
41
|
+
...t.config.tools === void 0 ? {} : { tools: t.config.tools },
|
|
42
|
+
provider: m,
|
|
43
|
+
...t.config.maxSteps === void 0 ? {} : { maxSteps: t.config.maxSteps },
|
|
44
|
+
...t.config.providerOptions === void 0 ? {} : { providerOptions: t.config.providerOptions },
|
|
45
|
+
...l.toolContext === void 0 ? {} : { toolContext: l.toolContext },
|
|
46
|
+
order: l.order + 1,
|
|
47
|
+
...l.throttleMs === void 0 ? {} : { throttleMs: l.throttleMs },
|
|
48
|
+
agent: t.descriptor.name
|
|
49
|
+
});
|
|
50
|
+
}), h = (e, n, r = null) => c.gen(function* () {
|
|
51
|
+
let o = yield* a(e, n);
|
|
52
|
+
return o !== null && o.subjectId !== null && o.subjectId !== r ? yield* c.fail(new t({ threadId: n })) : yield* i(e, n);
|
|
53
|
+
});
|
|
54
|
+
//#endregion
|
|
55
|
+
export { u as defineAgentExecutor, d as isAgentExecutor, h as loadAgentMessages, p as resolveAgentModel, f as resolveSystem, m as runAgentTurn };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { readEnvValue as e } from "@voltro/env";
|
|
2
|
+
//#region src/aiSettings.ts
|
|
3
|
+
var t = (t, n) => {
|
|
4
|
+
let r = e(t);
|
|
5
|
+
if (typeof r == "string" && r !== "") return r;
|
|
6
|
+
if (typeof r == "number" || typeof r == "boolean") return String(r);
|
|
7
|
+
let i = n[t];
|
|
8
|
+
return i === void 0 || i === "" ? void 0 : i;
|
|
9
|
+
}, n = (e) => e.NODE_ENV === "production";
|
|
10
|
+
//#endregion
|
|
11
|
+
export { t as aiSetting, n as isProductionEnv };
|
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 };
|
package/dist/appTools.js
ADDED
|
@@ -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 };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { gatewayCostUsd as e } from "./cost.js";
|
|
2
|
+
import { Schema as t } from "effect";
|
|
3
|
+
//#region src/callEvidence.ts
|
|
4
|
+
var n = t.Number.pipe(t.check(t.isInt()), t.check(t.isGreaterThanOrEqualTo(0)), t.check(t.isLessThanOrEqualTo(2 ** 53 - 1))), r = t.Struct({
|
|
5
|
+
provider: t.String,
|
|
6
|
+
model: t.String,
|
|
7
|
+
usage: t.Struct({
|
|
8
|
+
inputTokens: t.optional(n),
|
|
9
|
+
outputTokens: t.optional(n),
|
|
10
|
+
totalTokens: t.optional(n),
|
|
11
|
+
completeness: t.Literals([
|
|
12
|
+
"complete",
|
|
13
|
+
"partial",
|
|
14
|
+
"unknown"
|
|
15
|
+
])
|
|
16
|
+
}),
|
|
17
|
+
cost: t.Union([t.Struct({
|
|
18
|
+
status: t.Literal("actual"),
|
|
19
|
+
source: t.Literal("gateway"),
|
|
20
|
+
usd: t.Number.pipe(t.check(t.isFinite()), t.check(t.isGreaterThanOrEqualTo(0)))
|
|
21
|
+
}), t.Struct({
|
|
22
|
+
status: t.Literal("unknown"),
|
|
23
|
+
source: t.Literal("unreported")
|
|
24
|
+
})])
|
|
25
|
+
}), i = (e) => typeof e == "number" && Number.isSafeInteger(e) && e >= 0 ? e : void 0, a = (t, n, r, a) => {
|
|
26
|
+
let o = typeof r == "object" && r ? r : {}, s = i("inputTokens" in o ? o.inputTokens : void 0), c = i("outputTokens" in o ? o.outputTokens : void 0), l = i("totalTokens" in o ? o.totalTokens : void 0), u = [
|
|
27
|
+
s,
|
|
28
|
+
c,
|
|
29
|
+
l
|
|
30
|
+
].filter((e) => e !== void 0).length, d = e(a);
|
|
31
|
+
return {
|
|
32
|
+
provider: t,
|
|
33
|
+
model: n,
|
|
34
|
+
usage: {
|
|
35
|
+
...s === void 0 ? {} : { inputTokens: s },
|
|
36
|
+
...c === void 0 ? {} : { outputTokens: c },
|
|
37
|
+
...l === void 0 ? {} : { totalTokens: l },
|
|
38
|
+
completeness: u === 3 ? "complete" : u === 0 ? "unknown" : "partial"
|
|
39
|
+
},
|
|
40
|
+
cost: d === void 0 ? {
|
|
41
|
+
status: "unknown",
|
|
42
|
+
source: "unreported"
|
|
43
|
+
} : {
|
|
44
|
+
status: "actual",
|
|
45
|
+
source: "gateway",
|
|
46
|
+
usd: d
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
//#endregion
|
|
51
|
+
export { r as AiCallEvidenceSchema, a as normalizeAiCallEvidence };
|