theorum 0.1.3 → 0.1.4
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/README.md +30 -9
- package/deno.json +57 -0
- package/deno.lock +469 -0
- package/{esm/mod.d.ts → mod.ts} +57 -15
- package/package.json +35 -44
- package/src/cli/commands/profile.ts +57 -0
- package/src/cli/commands/run.ts +66 -0
- package/src/cli/commands/test.ts +195 -0
- package/src/cli/index.ts +161 -0
- package/src/cli/matrix/fixtures.ts +96 -0
- package/src/cli/matrix/synthesizer.ts +240 -0
- package/src/guardrails/error.ts +142 -0
- package/src/guardrails/injection.ts +254 -0
- package/src/guardrails/mod.ts +40 -0
- package/src/guardrails/quota.ts +87 -0
- package/src/guardrails/sanitize.ts +165 -0
- package/src/guardrails/sensitive.ts +96 -0
- package/src/host/mint-trace.ts +54 -0
- package/{esm/src/host/mod.js → src/host/mod.ts} +11 -3
- package/src/host/reply.ts +32 -0
- package/src/kernel/engine/assert.ts +29 -0
- package/src/kernel/engine/boundary.ts +75 -0
- package/src/kernel/engine/delta.ts +440 -0
- package/src/kernel/engine/hash.ts +11 -0
- package/src/kernel/engine/record.ts +8 -0
- package/src/kernel/engine/repair.ts +56 -0
- package/src/kernel/engine/runner/gates.ts +338 -0
- package/src/kernel/engine/runner/mod.ts +135 -0
- package/src/kernel/engine/runner/state.ts +31 -0
- package/src/kernel/engine/runner/steps.ts +183 -0
- package/src/kernel/engine/runner/stream.ts +83 -0
- package/src/kernel/engine/runner/tokens.ts +47 -0
- package/src/kernel/engine/runner/tools.ts +266 -0
- package/{esm/src/kernel/engine/runner.d.ts → src/kernel/engine/runner.ts} +2 -1
- package/src/kernel/engine/tree.ts +18 -0
- package/src/kernel/mod.ts +39 -0
- package/src/kernel/registry/attachments.ts +195 -0
- package/src/kernel/registry/catalog.ts +181 -0
- package/src/kernel/registry/ingress.ts +223 -0
- package/src/kernel/registry/profiles.ts +165 -0
- package/src/kernel/registry/provider-request.ts +31 -0
- package/src/kernel/registry/resolve.ts +290 -0
- package/src/kernel/registry/schemas.ts +29 -0
- package/src/kernel/registry/tools.ts +45 -0
- package/{esm/src/kernel/registry/vault.d.ts → src/kernel/registry/vault.ts} +17 -2
- package/src/kernel/types.ts +576 -0
- package/{esm/src/observability/mod.js → src/observability/mod.ts} +11 -2
- package/src/observability/spans.ts +70 -0
- package/src/observability/trace-attach.ts +96 -0
- package/src/observability/trace-record.ts +254 -0
- package/src/observability/trace-usage.ts +36 -0
- package/src/observability/trace.ts +143 -0
- package/src/presets/google.ts +147 -0
- package/src/presets/mod.ts +24 -0
- package/src/providers/create-provider.ts +64 -0
- package/src/providers/gemini-tape.ts +59 -0
- package/src/providers/google-tap.ts +57 -0
- package/src/providers/interactions.ts +182 -0
- package/src/providers/keys.ts +182 -0
- package/{esm/src/providers/mod.js → src/providers/mod.ts} +4 -2
- package/{esm/src/providers/openrouter-mod.js → src/providers/openrouter-mod.ts} +7 -3
- package/src/providers/openrouter-payload.ts +232 -0
- package/src/providers/openrouter.ts +758 -0
- package/src/providers/pcm.ts +37 -0
- package/src/providers/provider.ts +206 -0
- package/src/providers/speech.ts +180 -0
- package/src/providers/sse.ts +59 -0
- package/esm/_dnt.polyfills.d.ts +0 -110
- package/esm/_dnt.polyfills.js +0 -144
- package/esm/_dnt.shims.d.ts +0 -5
- package/esm/_dnt.shims.js +0 -61
- package/esm/mod.js +0 -49
- package/esm/package.json +0 -3
- package/esm/src/cli/commands/profile.d.ts +0 -2
- package/esm/src/cli/commands/profile.js +0 -53
- package/esm/src/cli/commands/run.d.ts +0 -10
- package/esm/src/cli/commands/run.js +0 -55
- package/esm/src/cli/commands/test.d.ts +0 -15
- package/esm/src/cli/commands/test.js +0 -140
- package/esm/src/cli/index.d.ts +0 -4
- package/esm/src/cli/index.js +0 -148
- package/esm/src/cli/matrix/fixtures.d.ts +0 -13
- package/esm/src/cli/matrix/fixtures.js +0 -77
- package/esm/src/cli/matrix/synthesizer.d.ts +0 -29
- package/esm/src/cli/matrix/synthesizer.js +0 -187
- package/esm/src/guardrails/error.d.ts +0 -33
- package/esm/src/guardrails/error.js +0 -114
- package/esm/src/guardrails/injection.d.ts +0 -12
- package/esm/src/guardrails/injection.js +0 -220
- package/esm/src/guardrails/mod.d.ts +0 -14
- package/esm/src/guardrails/mod.js +0 -14
- package/esm/src/guardrails/sanitize.d.ts +0 -22
- package/esm/src/guardrails/sanitize.js +0 -133
- package/esm/src/guardrails/sensitive.d.ts +0 -12
- package/esm/src/guardrails/sensitive.js +0 -88
- package/esm/src/host/mint-trace.d.ts +0 -27
- package/esm/src/host/mint-trace.js +0 -33
- package/esm/src/host/mod.d.ts +0 -13
- package/esm/src/host/reply.d.ts +0 -14
- package/esm/src/host/reply.js +0 -27
- package/esm/src/kernel/engine/boundary.d.ts +0 -10
- package/esm/src/kernel/engine/boundary.js +0 -55
- package/esm/src/kernel/engine/delta.d.ts +0 -8
- package/esm/src/kernel/engine/delta.js +0 -389
- package/esm/src/kernel/engine/hash.d.ts +0 -1
- package/esm/src/kernel/engine/hash.js +0 -9
- package/esm/src/kernel/engine/record.d.ts +0 -2
- package/esm/src/kernel/engine/record.js +0 -7
- package/esm/src/kernel/engine/repair.d.ts +0 -7
- package/esm/src/kernel/engine/repair.js +0 -38
- package/esm/src/kernel/engine/runner/gates.d.ts +0 -4
- package/esm/src/kernel/engine/runner/gates.js +0 -206
- package/esm/src/kernel/engine/runner/mod.d.ts +0 -14
- package/esm/src/kernel/engine/runner/mod.js +0 -103
- package/esm/src/kernel/engine/runner/state.d.ts +0 -16
- package/esm/src/kernel/engine/runner/state.js +0 -8
- package/esm/src/kernel/engine/runner/steps.d.ts +0 -15
- package/esm/src/kernel/engine/runner/steps.js +0 -119
- package/esm/src/kernel/engine/runner/stream.d.ts +0 -11
- package/esm/src/kernel/engine/runner/stream.js +0 -65
- package/esm/src/kernel/engine/runner/tokens.d.ts +0 -3
- package/esm/src/kernel/engine/runner/tokens.js +0 -38
- package/esm/src/kernel/engine/runner/tools.d.ts +0 -13
- package/esm/src/kernel/engine/runner/tools.js +0 -197
- package/esm/src/kernel/engine/runner.js +0 -6
- package/esm/src/kernel/engine/tree.d.ts +0 -2
- package/esm/src/kernel/engine/tree.js +0 -17
- package/esm/src/kernel/mod.d.ts +0 -18
- package/esm/src/kernel/mod.js +0 -16
- package/esm/src/kernel/registry/attachments.d.ts +0 -16
- package/esm/src/kernel/registry/attachments.js +0 -156
- package/esm/src/kernel/registry/catalog.d.ts +0 -31
- package/esm/src/kernel/registry/catalog.js +0 -135
- package/esm/src/kernel/registry/ingress.d.ts +0 -13
- package/esm/src/kernel/registry/ingress.js +0 -163
- package/esm/src/kernel/registry/profiles.d.ts +0 -34
- package/esm/src/kernel/registry/profiles.js +0 -120
- package/esm/src/kernel/registry/provider-request.d.ts +0 -4
- package/esm/src/kernel/registry/provider-request.js +0 -25
- package/esm/src/kernel/registry/resolve.d.ts +0 -19
- package/esm/src/kernel/registry/resolve.js +0 -217
- package/esm/src/kernel/registry/schemas.d.ts +0 -14
- package/esm/src/kernel/registry/schemas.js +0 -23
- package/esm/src/kernel/registry/tools.d.ts +0 -12
- package/esm/src/kernel/registry/tools.js +0 -36
- package/esm/src/kernel/registry/vault.js +0 -18
- package/esm/src/kernel/types.d.ts +0 -521
- package/esm/src/kernel/types.js +0 -10
- package/esm/src/observability/mod.d.ts +0 -12
- package/esm/src/observability/spans.d.ts +0 -16
- package/esm/src/observability/spans.js +0 -56
- package/esm/src/observability/trace-attach.d.ts +0 -16
- package/esm/src/observability/trace-attach.js +0 -67
- package/esm/src/observability/trace-record.d.ts +0 -112
- package/esm/src/observability/trace-record.js +0 -140
- package/esm/src/observability/trace-usage.d.ts +0 -3
- package/esm/src/observability/trace-usage.js +0 -32
- package/esm/src/observability/trace.d.ts +0 -23
- package/esm/src/observability/trace.js +0 -121
- package/esm/src/presets/google.d.ts +0 -50
- package/esm/src/presets/google.js +0 -96
- package/esm/src/presets/mod.d.ts +0 -11
- package/esm/src/presets/mod.js +0 -10
- package/esm/src/providers/create-provider.d.ts +0 -29
- package/esm/src/providers/create-provider.js +0 -38
- package/esm/src/providers/gemini-tape.d.ts +0 -2
- package/esm/src/providers/gemini-tape.js +0 -46
- package/esm/src/providers/google-tap.d.ts +0 -3
- package/esm/src/providers/google-tap.js +0 -48
- package/esm/src/providers/interactions.d.ts +0 -5
- package/esm/src/providers/interactions.js +0 -152
- package/esm/src/providers/keys.d.ts +0 -19
- package/esm/src/providers/keys.js +0 -129
- package/esm/src/providers/mod.d.ts +0 -12
- package/esm/src/providers/openrouter-mod.d.ts +0 -13
- package/esm/src/providers/openrouter-payload.d.ts +0 -32
- package/esm/src/providers/openrouter-payload.js +0 -179
- package/esm/src/providers/openrouter.d.ts +0 -15
- package/esm/src/providers/openrouter.js +0 -589
- package/esm/src/providers/pcm.d.ts +0 -7
- package/esm/src/providers/pcm.js +0 -35
- package/esm/src/providers/provider.d.ts +0 -15
- package/esm/src/providers/provider.js +0 -166
- package/esm/src/providers/speech.d.ts +0 -23
- package/esm/src/providers/speech.js +0 -125
- package/esm/src/providers/sse.d.ts +0 -7
- package/esm/src/providers/sse.js +0 -53
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { publicError } from '../../../guardrails/error.js';
|
|
2
|
-
import { CATALOG } from '../../registry/catalog.js';
|
|
3
|
-
import { executeTool } from '../../registry/tools.js';
|
|
4
|
-
function formatToolFinding(res) {
|
|
5
|
-
if (res.finding) {
|
|
6
|
-
return res.finding;
|
|
7
|
-
}
|
|
8
|
-
if (res.data) {
|
|
9
|
-
return JSON.stringify(res.data);
|
|
10
|
-
}
|
|
11
|
-
return 'ok';
|
|
12
|
-
}
|
|
13
|
-
function* invokeFromUi(profile, req) {
|
|
14
|
-
const invoke = req.toolInvoke;
|
|
15
|
-
if (!invoke) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
try {
|
|
19
|
-
const result = executeTool(profile, invoke.name, invoke.arguments);
|
|
20
|
-
yield {
|
|
21
|
-
type: 'tool',
|
|
22
|
-
tool: { name: invoke.name, arguments: invoke.arguments, result },
|
|
23
|
-
};
|
|
24
|
-
if (result.status === 'error') {
|
|
25
|
-
yield { type: 'error', error: publicError(formatToolFinding(result)) };
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
yield { type: 'done' };
|
|
29
|
-
}
|
|
30
|
-
catch (err) {
|
|
31
|
-
yield { type: 'error', error: publicError(err) };
|
|
32
|
-
yield { type: 'done' };
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function executeCustomModelTool(profile, name, args) {
|
|
36
|
-
try {
|
|
37
|
-
const result = executeTool(profile, name, args);
|
|
38
|
-
const events = [
|
|
39
|
-
{
|
|
40
|
-
type: 'tool',
|
|
41
|
-
tool: { name, arguments: args, result },
|
|
42
|
-
},
|
|
43
|
-
];
|
|
44
|
-
if (result.status === 'error') {
|
|
45
|
-
events.push({ type: 'error', error: publicError(formatToolFinding(result)) });
|
|
46
|
-
}
|
|
47
|
-
return events;
|
|
48
|
-
}
|
|
49
|
-
catch (err) {
|
|
50
|
-
return [{ type: 'error', error: publicError(err) }];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function dispatchModelTool(profile, event, gated) {
|
|
54
|
-
const { tool } = event;
|
|
55
|
-
if (!tool) {
|
|
56
|
-
return [];
|
|
57
|
-
}
|
|
58
|
-
const name = tool.name;
|
|
59
|
-
if (CATALOG.tools[name]?.kind === 'builtin') {
|
|
60
|
-
return [event];
|
|
61
|
-
}
|
|
62
|
-
if (!gated.includes(name)) {
|
|
63
|
-
return [
|
|
64
|
-
{
|
|
65
|
-
type: 'error',
|
|
66
|
-
error: publicError(`Tool '${name}' is not gated on this turn`),
|
|
67
|
-
},
|
|
68
|
-
];
|
|
69
|
-
}
|
|
70
|
-
const args = tool.arguments ?? {};
|
|
71
|
-
return executeCustomModelTool(profile, name, args);
|
|
72
|
-
}
|
|
73
|
-
function findDynamicDeclaration(tools, name) {
|
|
74
|
-
return tools?.find((t) => t.name === name);
|
|
75
|
-
}
|
|
76
|
-
async function checkDynamicAuthorization(decl, args, profile, sessionPermissions) {
|
|
77
|
-
if (!decl.canExecute) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
const decision = await decl.canExecute({
|
|
82
|
-
args,
|
|
83
|
-
profile,
|
|
84
|
-
sessionPermissions,
|
|
85
|
-
});
|
|
86
|
-
if (typeof decision === 'boolean' && !decision) {
|
|
87
|
-
return {
|
|
88
|
-
status: 'error',
|
|
89
|
-
finding: `Tool '${decl.name}' execution not authorized.`,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
if (typeof decision === 'object' && decision !== null) {
|
|
93
|
-
return decision;
|
|
94
|
-
}
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
catch (err) {
|
|
98
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
99
|
-
return {
|
|
100
|
-
status: 'error',
|
|
101
|
-
finding: `Authorization error for '${decl.name}': ${msg}`,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
function checkDynamicPermissionTier(decl, args, sessionPermissions) {
|
|
106
|
-
if (decl.permissionTier === 'session_consent' || decl.permissionTier === 'always_confirm') {
|
|
107
|
-
const isGranted = sessionPermissions?.includes(decl.name) || sessionPermissions?.includes('*');
|
|
108
|
-
if (!isGranted) {
|
|
109
|
-
return {
|
|
110
|
-
status: 'pause',
|
|
111
|
-
finding: `Tool '${decl.name}' requires ${decl.permissionTier} authorization.`,
|
|
112
|
-
data: { tool: decl.name, permissionTier: decl.permissionTier, args },
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return null;
|
|
117
|
-
}
|
|
118
|
-
async function runDynamicHandler(decl, args) {
|
|
119
|
-
if (!decl.handler) {
|
|
120
|
-
return {
|
|
121
|
-
status: 'ok',
|
|
122
|
-
finding: `${decl.name} accepted (no handler)`,
|
|
123
|
-
data: args,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
try {
|
|
127
|
-
return await decl.handler(args);
|
|
128
|
-
}
|
|
129
|
-
catch (err) {
|
|
130
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
131
|
-
return { status: 'error', finding: msg };
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
async function executeDynamicTool(decl, args, profile, sessionPermissions) {
|
|
135
|
-
const authEnvelope = await checkDynamicAuthorization(decl, args, profile, sessionPermissions);
|
|
136
|
-
if (authEnvelope) {
|
|
137
|
-
return authEnvelope;
|
|
138
|
-
}
|
|
139
|
-
const permissionEnvelope = checkDynamicPermissionTier(decl, args, sessionPermissions);
|
|
140
|
-
if (permissionEnvelope) {
|
|
141
|
-
return permissionEnvelope;
|
|
142
|
-
}
|
|
143
|
-
return runDynamicHandler(decl, args);
|
|
144
|
-
}
|
|
145
|
-
function mergeDynamicTools(current, loaded) {
|
|
146
|
-
const merged = [...(current ?? [])];
|
|
147
|
-
for (const tool of loaded) {
|
|
148
|
-
const existing = merged.findIndex((item) => item.name === tool.name);
|
|
149
|
-
if (existing >= 0) {
|
|
150
|
-
merged[existing] = tool;
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
merged.push(tool);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return merged;
|
|
157
|
-
}
|
|
158
|
-
async function executeDynamicToolLoader(args) {
|
|
159
|
-
const { decl, toolArgs, profile, generation } = args;
|
|
160
|
-
const loader = generation.dynamicToolLoader;
|
|
161
|
-
if (!loader) {
|
|
162
|
-
return {
|
|
163
|
-
status: 'error',
|
|
164
|
-
finding: `Tool '${decl.name}' is marked as a loader but no loader is configured.`,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
const loaded = await loader({
|
|
168
|
-
name: decl.name,
|
|
169
|
-
args: toolArgs,
|
|
170
|
-
profile,
|
|
171
|
-
currentTools: generation.dynamicTools ?? [],
|
|
172
|
-
sessionPermissions: generation.sessionPermissions,
|
|
173
|
-
});
|
|
174
|
-
generation.dynamicTools = mergeDynamicTools(generation.dynamicTools, loaded);
|
|
175
|
-
return {
|
|
176
|
-
status: 'ok',
|
|
177
|
-
finding: `Loaded ${String(loaded.length)} dynamic tool schema(s).`,
|
|
178
|
-
data: { loadedTools: loaded.map((tool) => tool.name) },
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
function isActionableDynamicDeclaration(decl) {
|
|
182
|
-
return Boolean(decl?.handler || decl?.canExecute || decl?.permissionTier || decl?.loadsDynamicTools);
|
|
183
|
-
}
|
|
184
|
-
async function executeDynamicDeclaration(args) {
|
|
185
|
-
const { decl, toolArgs, profile, generation } = args;
|
|
186
|
-
const res = await executeDynamicTool(decl, toolArgs, profile, generation.sessionPermissions);
|
|
187
|
-
if (res.status === 'ok' && decl.loadsDynamicTools) {
|
|
188
|
-
return await executeDynamicToolLoader({
|
|
189
|
-
decl,
|
|
190
|
-
toolArgs,
|
|
191
|
-
profile,
|
|
192
|
-
generation,
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
return res;
|
|
196
|
-
}
|
|
197
|
-
export { dispatchModelTool, executeDynamicDeclaration, findDynamicDeclaration, formatToolFinding, invokeFromUi, isActionableDynamicDeclaration, };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
function mapStrings(value, map) {
|
|
2
|
-
if (typeof value === 'string') {
|
|
3
|
-
return map(value);
|
|
4
|
-
}
|
|
5
|
-
if (Array.isArray(value)) {
|
|
6
|
-
return value.map((item) => mapStrings(item, map));
|
|
7
|
-
}
|
|
8
|
-
if (value && typeof value === 'object') {
|
|
9
|
-
const out = {};
|
|
10
|
-
for (const [key, nested] of Object.entries(value)) {
|
|
11
|
-
out[key] = mapStrings(nested, map);
|
|
12
|
-
}
|
|
13
|
-
return out;
|
|
14
|
-
}
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
export { mapStrings };
|
package/esm/src/kernel/mod.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Profile, turn, tool, provider, egress, and event contracts for THEORUM.
|
|
3
|
-
*
|
|
4
|
-
* This entrypoint is type-first. Host applications import from here when they
|
|
5
|
-
* want to declare profiles, tool schemas, provider adapters, turn requests, or
|
|
6
|
-
* trace-safe event handlers without importing provider implementations.
|
|
7
|
-
*
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
import "../../_dnt.polyfills.js";
|
|
11
|
-
export { runTurn } from './engine/runner.js';
|
|
12
|
-
export { CATALOG, clampThinkingLevel, clampThinkingLevelForApiId, getTool, listBuiltinIds, modelEntryByApiId, registerTools, requireModelSpec, resetTools, } from './registry/catalog.js';
|
|
13
|
-
export type { ProfileDefinition } from './registry/profiles.js';
|
|
14
|
-
export { clearProfiles, defineProfile, getProfile, hasProfile, listProfiles, registerProfile, registerProfiles, } from './registry/profiles.js';
|
|
15
|
-
export { projectProfile, resolveTurn } from './registry/resolve.js';
|
|
16
|
-
export { getStructured, registerStructured } from './registry/schemas.js';
|
|
17
|
-
export { executeTool } from './registry/tools.js';
|
|
18
|
-
export type * from './types.js';
|
package/esm/src/kernel/mod.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Profile, turn, tool, provider, egress, and event contracts for THEORUM.
|
|
3
|
-
*
|
|
4
|
-
* This entrypoint is type-first. Host applications import from here when they
|
|
5
|
-
* want to declare profiles, tool schemas, provider adapters, turn requests, or
|
|
6
|
-
* trace-safe event handlers without importing provider implementations.
|
|
7
|
-
*
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
import "../../_dnt.polyfills.js";
|
|
11
|
-
export { runTurn } from './engine/runner.js';
|
|
12
|
-
export { CATALOG, clampThinkingLevel, clampThinkingLevelForApiId, getTool, listBuiltinIds, modelEntryByApiId, registerTools, requireModelSpec, resetTools, } from './registry/catalog.js';
|
|
13
|
-
export { clearProfiles, defineProfile, getProfile, hasProfile, listProfiles, registerProfile, registerProfiles, } from './registry/profiles.js';
|
|
14
|
-
export { projectProfile, resolveTurn } from './registry/resolve.js';
|
|
15
|
-
export { getStructured, registerStructured } from './registry/schemas.js';
|
|
16
|
-
export { executeTool } from './registry/tools.js';
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { MediaLimits, Profile, TurnBlob } from '../types.js';
|
|
2
|
-
declare function tooManyFilesMessage(maxFiles: number): string;
|
|
3
|
-
declare function fileTooLargeMessage(maxBytes: number): string;
|
|
4
|
-
declare function turnTooLargeMessage(maxTurnBytes: number): string;
|
|
5
|
-
declare function requireMediaLimits(profile: Profile): MediaLimits;
|
|
6
|
-
declare function sanitizeCsvText(text: string): string;
|
|
7
|
-
declare function assertAttachmentLimits(blobs: TurnBlob[], limits: MediaLimits): void;
|
|
8
|
-
declare function sanitizeTurnBlobs(attachments: TurnBlob[] | undefined, voice: TurnBlob[] | undefined, limits: MediaLimits | undefined): {
|
|
9
|
-
attachments?: TurnBlob[];
|
|
10
|
-
voice?: TurnBlob[];
|
|
11
|
-
};
|
|
12
|
-
declare function sanitizeTurnBlobsForProfile(profileId: string, attachments: TurnBlob[] | undefined, voice: TurnBlob[] | undefined): {
|
|
13
|
-
attachments?: TurnBlob[];
|
|
14
|
-
voice?: TurnBlob[];
|
|
15
|
-
};
|
|
16
|
-
export { assertAttachmentLimits, fileTooLargeMessage, requireMediaLimits, sanitizeCsvText, sanitizeTurnBlobs, sanitizeTurnBlobsForProfile, tooManyFilesMessage, turnTooLargeMessage, };
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { TheorumError } from '../../guardrails/error.js';
|
|
2
|
-
import { injectionSpans } from '../../guardrails/injection.js';
|
|
3
|
-
import { sensitiveSpans } from '../../guardrails/sensitive.js';
|
|
4
|
-
import { applySpans } from '../../observability/spans.js';
|
|
5
|
-
import { getProfile } from './profiles.js';
|
|
6
|
-
const B64_PAD = 2;
|
|
7
|
-
const B64_WORD = 4;
|
|
8
|
-
const B64_TRIPLET = 3;
|
|
9
|
-
const CSV_FORMULA = /(^|,)(\s*)("?)(?:([=@])|([+-])(?![0-9."]))/gm;
|
|
10
|
-
const B64_BODY = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
11
|
-
const TEXT_MIMES = new Set(['text/csv', 'text/plain', 'text/markdown']);
|
|
12
|
-
const BYTES_PER_KIB = 1024;
|
|
13
|
-
function formatMb(bytes) {
|
|
14
|
-
const mb = bytes / (BYTES_PER_KIB * BYTES_PER_KIB);
|
|
15
|
-
return Number.isInteger(mb) ? `${String(mb)} MB` : `${mb.toFixed(1)} MB`;
|
|
16
|
-
}
|
|
17
|
-
function tooManyFilesMessage(maxFiles) {
|
|
18
|
-
return maxFiles === 1
|
|
19
|
-
? 'Only 1 file per message.'
|
|
20
|
-
: `Only ${String(maxFiles)} files per message.`;
|
|
21
|
-
}
|
|
22
|
-
function fileTooLargeMessage(maxBytes) {
|
|
23
|
-
return `Each file must be ${formatMb(maxBytes)} or smaller.`;
|
|
24
|
-
}
|
|
25
|
-
function turnTooLargeMessage(maxTurnBytes) {
|
|
26
|
-
return `Those files together are too large for one message (${formatMb(maxTurnBytes)} max).`;
|
|
27
|
-
}
|
|
28
|
-
function mediaLimits(inputs) {
|
|
29
|
-
const { maxFiles, maxBytes, maxTurnBytes, limitsByMime } = inputs;
|
|
30
|
-
if (maxFiles && maxBytes && maxTurnBytes) {
|
|
31
|
-
return { maxFiles, maxBytes, maxTurnBytes, limitsByMime };
|
|
32
|
-
}
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
function maxBytesForMime(mimeType, limits) {
|
|
36
|
-
if (limits.limitsByMime) {
|
|
37
|
-
const cleanMime = mimeType.split(';')[0]?.trim().toLowerCase() ?? '';
|
|
38
|
-
if (limits.limitsByMime[cleanMime]) {
|
|
39
|
-
return limits.limitsByMime[cleanMime];
|
|
40
|
-
}
|
|
41
|
-
const [category] = cleanMime.split('/');
|
|
42
|
-
const wildCard = `${category}/*`;
|
|
43
|
-
if (limits.limitsByMime[wildCard]) {
|
|
44
|
-
return limits.limitsByMime[wildCard];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return limits.maxBytes;
|
|
48
|
-
}
|
|
49
|
-
function requireMediaLimits(profile) {
|
|
50
|
-
const limits = mediaLimits(profile.inputs);
|
|
51
|
-
if (!limits) {
|
|
52
|
-
throw new TheorumError(`Profile ${profile.id} must set maxFiles, maxBytes, and maxTurnBytes`);
|
|
53
|
-
}
|
|
54
|
-
return limits;
|
|
55
|
-
}
|
|
56
|
-
function b64DecodedLen(data) {
|
|
57
|
-
let pad = 0;
|
|
58
|
-
if (data.endsWith('==')) {
|
|
59
|
-
pad = B64_PAD;
|
|
60
|
-
}
|
|
61
|
-
else if (data.endsWith('=')) {
|
|
62
|
-
pad = 1;
|
|
63
|
-
}
|
|
64
|
-
return Math.floor((data.length * B64_TRIPLET) / B64_WORD) - pad;
|
|
65
|
-
}
|
|
66
|
-
function decodeB64(data) {
|
|
67
|
-
const binary = atob(data);
|
|
68
|
-
const bytes = new Uint8Array(binary.length);
|
|
69
|
-
for (let i = 0; i < binary.length; i += 1) {
|
|
70
|
-
bytes[i] = binary.charCodeAt(i);
|
|
71
|
-
}
|
|
72
|
-
return bytes;
|
|
73
|
-
}
|
|
74
|
-
function encodeB64(bytes) {
|
|
75
|
-
let binary = '';
|
|
76
|
-
for (const byte of bytes) {
|
|
77
|
-
binary += String.fromCharCode(byte);
|
|
78
|
-
}
|
|
79
|
-
return btoa(binary);
|
|
80
|
-
}
|
|
81
|
-
function decodeText(bytes) {
|
|
82
|
-
try {
|
|
83
|
-
return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
return new TextDecoder('latin1').decode(bytes);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function sanitizeCsvText(text) {
|
|
90
|
-
return text.replace(CSV_FORMULA, (_full, ...groups) => {
|
|
91
|
-
const [a, b, c, d, e] = groups;
|
|
92
|
-
return `${a}${b}${c}'${d ?? ''}${e ?? ''}`;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function sanitizeTextBytes(mime, bytes) {
|
|
96
|
-
let text = decodeText(bytes);
|
|
97
|
-
if (mime === 'text/csv') {
|
|
98
|
-
text = sanitizeCsvText(text);
|
|
99
|
-
}
|
|
100
|
-
return new TextEncoder().encode(applySpans(text, [...injectionSpans(text), ...sensitiveSpans(text)]));
|
|
101
|
-
}
|
|
102
|
-
function assertAttachmentLimits(blobs, limits) {
|
|
103
|
-
if (blobs.length > limits.maxFiles) {
|
|
104
|
-
throw new TheorumError(tooManyFilesMessage(limits.maxFiles));
|
|
105
|
-
}
|
|
106
|
-
let total = 0;
|
|
107
|
-
for (const blob of blobs) {
|
|
108
|
-
const { data, mimeType } = blob;
|
|
109
|
-
if (!B64_BODY.test(data)) {
|
|
110
|
-
throw new TheorumError('attachment data must be base64');
|
|
111
|
-
}
|
|
112
|
-
const size = b64DecodedLen(data);
|
|
113
|
-
const maxAllowed = maxBytesForMime(mimeType, limits);
|
|
114
|
-
if (size > maxAllowed) {
|
|
115
|
-
throw new TheorumError(fileTooLargeMessage(maxAllowed));
|
|
116
|
-
}
|
|
117
|
-
total += size;
|
|
118
|
-
}
|
|
119
|
-
if (total > limits.maxTurnBytes) {
|
|
120
|
-
throw new TheorumError(turnTooLargeMessage(limits.maxTurnBytes));
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
function sanitizeAttachment(blob) {
|
|
124
|
-
const { mimeType, data } = blob;
|
|
125
|
-
if (!TEXT_MIMES.has(mimeType.split(';')[0]?.trim().toLowerCase() ?? '')) {
|
|
126
|
-
return blob;
|
|
127
|
-
}
|
|
128
|
-
const bytes = sanitizeTextBytes(mimeType, decodeB64(data));
|
|
129
|
-
return { mimeType, data: encodeB64(bytes) };
|
|
130
|
-
}
|
|
131
|
-
function hasTurnBlobs(attachments, voice) {
|
|
132
|
-
return (attachments?.length ?? 0) > 0 || (voice?.length ?? 0) > 0;
|
|
133
|
-
}
|
|
134
|
-
function sanitizeTurnBlobs(attachments, voice, limits) {
|
|
135
|
-
if (!hasTurnBlobs(attachments, voice)) {
|
|
136
|
-
return { attachments, voice };
|
|
137
|
-
}
|
|
138
|
-
const files = attachments ?? [];
|
|
139
|
-
const clips = voice ?? [];
|
|
140
|
-
if (!limits) {
|
|
141
|
-
throw new TheorumError('This profile does not accept files.');
|
|
142
|
-
}
|
|
143
|
-
assertAttachmentLimits([...files, ...clips], limits);
|
|
144
|
-
return {
|
|
145
|
-
attachments: files.length > 0 ? files.map(sanitizeAttachment) : attachments,
|
|
146
|
-
voice: clips.length > 0 ? clips.map(sanitizeAttachment) : voice,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
function sanitizeTurnBlobsForProfile(profileId, attachments, voice) {
|
|
150
|
-
if (!hasTurnBlobs(attachments, voice)) {
|
|
151
|
-
return { attachments, voice };
|
|
152
|
-
}
|
|
153
|
-
const limits = requireMediaLimits(getProfile(profileId));
|
|
154
|
-
return sanitizeTurnBlobs(attachments, voice, limits);
|
|
155
|
-
}
|
|
156
|
-
export { assertAttachmentLimits, fileTooLargeMessage, requireMediaLimits, sanitizeCsvText, sanitizeTurnBlobs, sanitizeTurnBlobsForProfile, tooManyFilesMessage, turnTooLargeMessage, };
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tool catalog and MIME helpers.
|
|
3
|
-
*
|
|
4
|
-
* Model wire metadata is host-owned on `profile.model.config`. This module only
|
|
5
|
-
* keeps builtin/custom tool descriptors and shared MIME utilities.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
import type { BuiltinToolId, Catalog, GeminiInputKind, ModelId, ModelSpec, Profile, ThinkingLevel, ToolCatalogEntry, ToolId } from '../types.js';
|
|
10
|
-
/** Live tool catalog. Starts with harness tools; presets/hosts register more. */
|
|
11
|
-
declare const CATALOG: Catalog;
|
|
12
|
-
/** Register or replace tool descriptors (idempotent per id). */
|
|
13
|
-
declare function registerTools(entries: Record<string, ToolCatalogEntry>): void;
|
|
14
|
-
/** Look up one registered tool descriptor. */
|
|
15
|
-
declare function getTool(id: ToolId): ToolCatalogEntry | undefined;
|
|
16
|
-
/** Ids of all registered provider builtins. */
|
|
17
|
-
declare function listBuiltinIds(): BuiltinToolId[];
|
|
18
|
-
/** Restore harness-only tools (tests / host reloads). */
|
|
19
|
-
declare function resetTools(): void;
|
|
20
|
-
declare function mimeEssence(mime: string): string;
|
|
21
|
-
declare function mimeAllowed(accept: string[], mime: string): boolean;
|
|
22
|
-
declare function geminiKindForMime(mime: string): GeminiInputKind | undefined;
|
|
23
|
-
/** Require a host-declared model spec for an allowed profile model id. */
|
|
24
|
-
declare function requireModelSpec(profile: Profile, modelId: ModelId): ModelSpec;
|
|
25
|
-
/** Clamp a requested thinking level to what the model spec accepts. */
|
|
26
|
-
declare function clampThinkingLevel(spec: ModelSpec, level: ThinkingLevel): ThinkingLevel;
|
|
27
|
-
/** Look up a model spec by provider-native API id within a host specs map. */
|
|
28
|
-
declare function modelEntryByApiId(specs: Record<string, ModelSpec>, apiId: string): ModelSpec | undefined;
|
|
29
|
-
/** Clamp thinking level using a provider-native API id within a host specs map. */
|
|
30
|
-
declare function clampThinkingLevelForApiId(specs: Record<string, ModelSpec>, apiId: string, level: ThinkingLevel): ThinkingLevel;
|
|
31
|
-
export { CATALOG, clampThinkingLevel, clampThinkingLevelForApiId, geminiKindForMime, getTool, listBuiltinIds, mimeAllowed, mimeEssence, modelEntryByApiId, registerTools, requireModelSpec, resetTools, };
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tool catalog and MIME helpers.
|
|
3
|
-
*
|
|
4
|
-
* Model wire metadata is host-owned on `profile.model.config`. This module only
|
|
5
|
-
* keeps builtin/custom tool descriptors and shared MIME utilities.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
import { TheorumError } from '../../guardrails/error.js';
|
|
10
|
-
const ASK_USER_SCHEMA = {
|
|
11
|
-
type: 'object',
|
|
12
|
-
properties: {
|
|
13
|
-
kind: { type: 'string', enum: ['confirm', 'choice', 'text'] },
|
|
14
|
-
prompt: { type: 'string' },
|
|
15
|
-
options: { type: 'array', items: { type: 'string' } },
|
|
16
|
-
},
|
|
17
|
-
required: ['kind', 'prompt'],
|
|
18
|
-
};
|
|
19
|
-
/** Harness tools that always ship with THEORUM. */
|
|
20
|
-
const HARNESS_TOOLS = {
|
|
21
|
-
askUser: { kind: 'custom', ui: true, schema: ASK_USER_SCHEMA },
|
|
22
|
-
};
|
|
23
|
-
/** Live tool catalog. Starts with harness tools; presets/hosts register more. */
|
|
24
|
-
const CATALOG = {
|
|
25
|
-
tools: { ...HARNESS_TOOLS },
|
|
26
|
-
};
|
|
27
|
-
/** Register or replace tool descriptors (idempotent per id). */
|
|
28
|
-
function registerTools(entries) {
|
|
29
|
-
Object.assign(CATALOG.tools, entries);
|
|
30
|
-
}
|
|
31
|
-
/** Look up one registered tool descriptor. */
|
|
32
|
-
function getTool(id) {
|
|
33
|
-
return CATALOG.tools[id];
|
|
34
|
-
}
|
|
35
|
-
/** Ids of all registered provider builtins. */
|
|
36
|
-
function listBuiltinIds() {
|
|
37
|
-
return Object.entries(CATALOG.tools)
|
|
38
|
-
.filter(([, entry]) => entry.kind === 'builtin')
|
|
39
|
-
.map(([id]) => id);
|
|
40
|
-
}
|
|
41
|
-
/** Restore harness-only tools (tests / host reloads). */
|
|
42
|
-
function resetTools() {
|
|
43
|
-
for (const id of Object.keys(CATALOG.tools)) {
|
|
44
|
-
delete CATALOG.tools[id];
|
|
45
|
-
}
|
|
46
|
-
Object.assign(CATALOG.tools, HARNESS_TOOLS);
|
|
47
|
-
}
|
|
48
|
-
/** Gemini Interactions inline MIME → part type (adapter wire map). */
|
|
49
|
-
const GEMINI_INPUT_KINDS = {
|
|
50
|
-
'image/png': 'image',
|
|
51
|
-
'image/jpeg': 'image',
|
|
52
|
-
'image/jpg': 'image',
|
|
53
|
-
'image/webp': 'image',
|
|
54
|
-
'image/heic': 'image',
|
|
55
|
-
'image/heif': 'image',
|
|
56
|
-
'audio/wav': 'audio',
|
|
57
|
-
'audio/x-wav': 'audio',
|
|
58
|
-
'audio/mpeg': 'audio',
|
|
59
|
-
'audio/mp3': 'audio',
|
|
60
|
-
'audio/aiff': 'audio',
|
|
61
|
-
'audio/aac': 'audio',
|
|
62
|
-
'audio/ogg': 'audio',
|
|
63
|
-
'audio/flac': 'audio',
|
|
64
|
-
'audio/webm': 'audio',
|
|
65
|
-
'audio/mp4': 'audio',
|
|
66
|
-
'audio/pcm': 'audio',
|
|
67
|
-
'video/mp4': 'video',
|
|
68
|
-
'video/mpeg': 'video',
|
|
69
|
-
'video/quicktime': 'video',
|
|
70
|
-
'video/x-msvideo': 'video',
|
|
71
|
-
'video/x-flv': 'video',
|
|
72
|
-
'video/mpg': 'video',
|
|
73
|
-
'video/webm': 'video',
|
|
74
|
-
'video/wmv': 'video',
|
|
75
|
-
'video/x-ms-wmv': 'video',
|
|
76
|
-
'video/3gpp': 'video',
|
|
77
|
-
'application/pdf': 'document',
|
|
78
|
-
'text/plain': 'document',
|
|
79
|
-
'text/csv': 'document',
|
|
80
|
-
'text/markdown': 'document',
|
|
81
|
-
'text/html': 'document',
|
|
82
|
-
'application/json': 'document',
|
|
83
|
-
};
|
|
84
|
-
function mimeEssence(mime) {
|
|
85
|
-
const [base] = mime.split(';');
|
|
86
|
-
return (base ?? '').trim().toLowerCase();
|
|
87
|
-
}
|
|
88
|
-
function mimeAllowed(accept, mime) {
|
|
89
|
-
const actual = mimeEssence(mime);
|
|
90
|
-
return accept.some((rule) => {
|
|
91
|
-
const allowed = mimeEssence(rule);
|
|
92
|
-
if (allowed.endsWith('/*')) {
|
|
93
|
-
return actual.startsWith(allowed.slice(0, -1));
|
|
94
|
-
}
|
|
95
|
-
return allowed === actual;
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
function geminiKindForMime(mime) {
|
|
99
|
-
return GEMINI_INPUT_KINDS[mimeEssence(mime)];
|
|
100
|
-
}
|
|
101
|
-
/** Require a host-declared model spec for an allowed profile model id. */
|
|
102
|
-
function requireModelSpec(profile, modelId) {
|
|
103
|
-
const spec = profile.model.config[modelId];
|
|
104
|
-
if (!spec) {
|
|
105
|
-
throw new TheorumError(`Profile ${profile.id} has no model spec for '${modelId}'`);
|
|
106
|
-
}
|
|
107
|
-
return spec;
|
|
108
|
-
}
|
|
109
|
-
function clampLevels(entry, level) {
|
|
110
|
-
if (!entry?.thinkingLevels || entry.thinkingLevels.length === 0) {
|
|
111
|
-
return level;
|
|
112
|
-
}
|
|
113
|
-
if (entry.thinkingLevels.includes(level)) {
|
|
114
|
-
return level;
|
|
115
|
-
}
|
|
116
|
-
const fallback = entry.thinking.off;
|
|
117
|
-
if (entry.thinkingLevels.includes(fallback)) {
|
|
118
|
-
return fallback;
|
|
119
|
-
}
|
|
120
|
-
const first = entry.thinkingLevels[0];
|
|
121
|
-
return first ?? level;
|
|
122
|
-
}
|
|
123
|
-
/** Clamp a requested thinking level to what the model spec accepts. */
|
|
124
|
-
function clampThinkingLevel(spec, level) {
|
|
125
|
-
return clampLevels(spec, level);
|
|
126
|
-
}
|
|
127
|
-
/** Look up a model spec by provider-native API id within a host specs map. */
|
|
128
|
-
function modelEntryByApiId(specs, apiId) {
|
|
129
|
-
return Object.values(specs).find((m) => m.apiId === apiId);
|
|
130
|
-
}
|
|
131
|
-
/** Clamp thinking level using a provider-native API id within a host specs map. */
|
|
132
|
-
function clampThinkingLevelForApiId(specs, apiId, level) {
|
|
133
|
-
return clampLevels(modelEntryByApiId(specs, apiId), level);
|
|
134
|
-
}
|
|
135
|
-
export { CATALOG, clampThinkingLevel, clampThinkingLevelForApiId, geminiKindForMime, getTool, listBuiltinIds, mimeAllowed, mimeEssence, modelEntryByApiId, registerTools, requireModelSpec, resetTools, };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Kernel ingress: input parts, image pins, and speech-role checks.
|
|
3
|
-
*
|
|
4
|
-
* Owned by the kernel so `resolveTurn` does not import provider adapters.
|
|
5
|
-
*
|
|
6
|
-
* @module
|
|
7
|
-
*/
|
|
8
|
-
import type { BuiltinToolId, ImageResponseFormat, InteractionPart, ModelId, Profile, TurnRequest } from '../types.js';
|
|
9
|
-
declare function assertSpeechRole(profile: Profile): void;
|
|
10
|
-
declare function resolveImageFormat(profile: Profile, _model: ModelId, slots?: Record<string, string>): ImageResponseFormat | null;
|
|
11
|
-
declare function resolveInputParts(profile: Profile, model: ModelId, req: TurnRequest): InteractionPart[];
|
|
12
|
-
declare function assertImageGrounding(profile: Profile, model: ModelId, builtins: BuiltinToolId[]): void;
|
|
13
|
-
export { assertImageGrounding, assertSpeechRole, resolveImageFormat, resolveInputParts };
|