theorum 0.1.15 → 1.1.3
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 +241 -98
- package/esm/mod.d.ts +57 -28
- package/esm/mod.js +43 -23
- package/esm/src/cli/commands/bench.js +18 -18
- package/esm/src/cli/commands/fuzz-canary.d.ts +13 -0
- package/esm/src/cli/commands/fuzz-canary.js +191 -0
- package/esm/src/cli/commands/fuzz-guardrails.d.ts +3 -5
- package/esm/src/cli/commands/fuzz-guardrails.js +4 -581
- package/esm/src/cli/commands/guardrails-eval.d.ts +14 -0
- package/esm/src/cli/commands/guardrails-eval.js +15 -0
- package/esm/src/cli/commands/profile.js +35 -15
- package/esm/src/cli/commands/run.d.ts +3 -0
- package/esm/src/cli/commands/run.js +23 -32
- package/esm/src/cli/commands/test.d.ts +10 -1
- package/esm/src/cli/commands/test.js +34 -34
- package/esm/src/cli/event-log.d.ts +19 -0
- package/esm/src/cli/event-log.js +147 -0
- package/esm/src/cli/index.js +57 -11
- package/esm/src/cli/matrix/synthesizer.d.ts +10 -12
- package/esm/src/cli/matrix/synthesizer.js +45 -118
- package/esm/src/guardrails/canary-gate.d.ts +21 -0
- package/esm/src/guardrails/canary-gate.js +32 -0
- package/esm/src/guardrails/canary.d.ts +34 -0
- package/esm/src/guardrails/canary.js +150 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +17 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.js +151 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.d.ts +11 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.js +213 -0
- package/esm/src/guardrails/corpus/inbound-payloads.d.ts +10 -0
- package/esm/src/guardrails/corpus/inbound-payloads.js +125 -0
- package/esm/src/guardrails/corpus/live-attacks.d.ts +20 -0
- package/esm/src/guardrails/corpus/live-attacks.js +231 -0
- package/esm/src/guardrails/corpus/mod.d.ts +14 -0
- package/esm/src/guardrails/corpus/mod.js +11 -0
- package/esm/src/guardrails/corpus/secrets.d.ts +17 -0
- package/esm/src/guardrails/corpus/secrets.js +17 -0
- package/esm/src/guardrails/corpus/strings.d.ts +28 -0
- package/esm/src/guardrails/corpus/strings.js +34 -0
- package/esm/src/guardrails/corpus/types.d.ts +38 -0
- package/esm/src/guardrails/corpus/types.js +6 -0
- package/esm/src/guardrails/egress.d.ts +32 -0
- package/esm/src/guardrails/egress.js +87 -0
- package/esm/src/guardrails/error.d.ts +14 -23
- package/esm/src/guardrails/error.js +87 -76
- package/esm/src/guardrails/eval/corpus.d.ts +108 -0
- package/esm/src/guardrails/eval/corpus.js +978 -0
- package/esm/src/guardrails/eval/mod.d.ts +51 -0
- package/esm/src/guardrails/eval/mod.js +133 -0
- package/esm/src/guardrails/eval/score.d.ts +66 -0
- package/esm/src/guardrails/eval/score.js +114 -0
- package/esm/src/guardrails/events.d.ts +25 -0
- package/esm/src/guardrails/events.js +56 -0
- package/esm/src/guardrails/hits.d.ts +24 -0
- package/esm/src/guardrails/hits.js +45 -0
- package/esm/src/guardrails/injection.js +28 -5
- package/esm/src/guardrails/lexicon.d.ts +39 -0
- package/esm/src/guardrails/lexicon.js +200 -0
- package/esm/src/guardrails/live-outbound-gate.d.ts +41 -0
- package/esm/src/guardrails/live-outbound-gate.js +222 -0
- package/esm/src/guardrails/mod.d.ts +30 -6
- package/esm/src/guardrails/mod.js +20 -5
- package/esm/src/guardrails/network.d.ts +19 -0
- package/esm/src/guardrails/network.js +234 -0
- package/esm/src/guardrails/policy.d.ts +35 -0
- package/esm/src/guardrails/policy.js +50 -0
- package/esm/src/guardrails/progressive-yield.d.ts +51 -0
- package/esm/src/guardrails/progressive-yield.js +98 -0
- package/esm/src/guardrails/quota.d.ts +17 -3
- package/esm/src/guardrails/quota.js +18 -4
- package/esm/src/guardrails/sanitize.d.ts +45 -19
- package/esm/src/guardrails/sanitize.js +177 -94
- package/esm/src/guardrails/sensitive.js +2 -1
- package/esm/src/guardrails/serialize.d.ts +35 -0
- package/esm/src/guardrails/serialize.js +58 -0
- package/esm/src/guardrails/testing.d.ts +17 -0
- package/esm/src/guardrails/testing.js +13 -0
- package/esm/src/guardrails/theorum-error.d.ts +12 -0
- package/esm/src/guardrails/theorum-error.js +15 -0
- package/esm/src/guardrails/tool-directives.d.ts +48 -0
- package/esm/src/guardrails/tool-directives.js +124 -0
- package/esm/src/guardrails/tool-result.d.ts +93 -0
- package/esm/src/guardrails/tool-result.js +276 -0
- package/esm/src/guardrails/types.d.ts +291 -0
- package/esm/src/guardrails/types.js +72 -0
- package/esm/src/host/client-turn.d.ts +19 -0
- package/esm/src/host/client-turn.js +36 -0
- package/esm/src/host/mint-trace.d.ts +1 -1
- package/esm/src/host/mod.d.ts +5 -3
- package/esm/src/host/mod.js +4 -3
- package/esm/src/kernel/auth/crypto.d.ts +42 -0
- package/esm/src/kernel/auth/crypto.js +106 -0
- package/esm/src/kernel/auth/mod.d.ts +11 -0
- package/esm/src/kernel/auth/mod.js +11 -0
- package/esm/src/kernel/auth/oauth.d.ts +47 -0
- package/esm/src/kernel/auth/oauth.js +278 -0
- package/esm/src/kernel/auth/types.d.ts +133 -0
- package/esm/src/kernel/auth/types.js +13 -0
- package/esm/src/kernel/engine/delta.d.ts +24 -2
- package/esm/src/kernel/engine/delta.js +478 -39
- package/esm/src/kernel/engine/live-inbound.d.ts +21 -0
- package/esm/src/kernel/engine/live-inbound.js +31 -0
- package/esm/src/kernel/engine/live-ingress.d.ts +19 -0
- package/esm/src/kernel/engine/live-ingress.js +47 -0
- package/esm/src/kernel/engine/repair.js +13 -12
- package/esm/src/kernel/engine/runner/gates.d.ts +1 -1
- package/esm/src/kernel/engine/runner/gates.js +130 -43
- package/esm/src/kernel/engine/runner/mod.d.ts +6 -4
- package/esm/src/kernel/engine/runner/mod.js +192 -53
- package/esm/src/kernel/engine/runner/schema-validation.js +3 -3
- package/esm/src/kernel/engine/runner/stages.d.ts +39 -0
- package/esm/src/kernel/engine/runner/stages.js +89 -0
- package/esm/src/kernel/engine/runner/state.d.ts +31 -0
- package/esm/src/kernel/engine/runner/steps.d.ts +1 -1
- package/esm/src/kernel/engine/runner/steps.js +244 -43
- package/esm/src/kernel/engine/runner/stream.d.ts +9 -3
- package/esm/src/kernel/engine/runner/stream.js +140 -44
- package/esm/src/kernel/engine/session/mod.d.ts +25 -0
- package/esm/src/kernel/engine/session/mod.js +557 -0
- package/esm/src/kernel/interaction-parts.d.ts +14 -0
- package/esm/src/kernel/interaction-parts.js +23 -0
- package/esm/src/kernel/mod.d.ts +21 -10
- package/esm/src/kernel/mod.js +11 -8
- package/esm/src/kernel/profile-graph.d.ts +159 -0
- package/esm/src/kernel/profile-graph.js +156 -0
- package/esm/src/kernel/registry/attachments.d.ts +12 -10
- package/esm/src/kernel/registry/attachments.js +33 -27
- package/esm/src/kernel/registry/catalog.d.ts +25 -24
- package/esm/src/kernel/registry/catalog.js +60 -101
- package/esm/src/kernel/registry/ingress.d.ts +9 -4
- package/esm/src/kernel/registry/ingress.js +97 -75
- package/esm/src/kernel/registry/profile-outputs.d.ts +4 -0
- package/esm/src/kernel/registry/profile-outputs.js +8 -0
- package/esm/src/kernel/registry/profiles.d.ts +55 -12
- package/esm/src/kernel/registry/profiles.js +413 -73
- package/esm/src/kernel/registry/provider-request.js +13 -7
- package/esm/src/kernel/registry/resolve.d.ts +8 -8
- package/esm/src/kernel/registry/resolve.js +169 -154
- package/esm/src/kernel/registry/schemas.js +1 -1
- package/esm/src/kernel/registry/sole-model.d.ts +8 -0
- package/esm/src/kernel/registry/sole-model.js +10 -0
- package/esm/src/kernel/registry/system-prompt.d.ts +10 -0
- package/esm/src/kernel/registry/system-prompt.js +40 -0
- package/esm/src/kernel/registry/system-role.d.ts +8 -0
- package/esm/src/kernel/registry/system-role.js +14 -0
- package/esm/src/kernel/registry/vault.d.ts +12 -7
- package/esm/src/kernel/registry/vault.js +32 -10
- package/esm/src/kernel/schema.d.ts +231 -0
- package/esm/src/kernel/schema.js +607 -0
- package/esm/src/kernel/stages.d.ts +175 -0
- package/esm/src/kernel/stages.js +476 -0
- package/esm/src/kernel/stop.d.ts +78 -19
- package/esm/src/kernel/stop.js +51 -16
- package/esm/src/kernel/tools/events.d.ts +41 -0
- package/esm/src/kernel/tools/events.js +71 -0
- package/esm/src/kernel/tools/execute.d.ts +84 -0
- package/esm/src/kernel/tools/execute.js +614 -0
- package/esm/src/kernel/tools/harness.d.ts +8 -0
- package/esm/src/kernel/tools/harness.js +46 -0
- package/esm/src/kernel/tools/invoke.d.ts +10 -0
- package/esm/src/kernel/tools/invoke.js +101 -0
- package/esm/src/kernel/tools/mod.d.ts +13 -0
- package/esm/src/kernel/tools/mod.js +11 -0
- package/esm/src/kernel/tools/permission.d.ts +15 -0
- package/esm/src/kernel/tools/permission.js +47 -0
- package/esm/src/kernel/tools/project.d.ts +12 -0
- package/esm/src/kernel/tools/project.js +36 -0
- package/esm/src/kernel/tools/registry.d.ts +23 -0
- package/esm/src/kernel/tools/registry.js +81 -0
- package/esm/src/kernel/tools/remote.d.ts +94 -0
- package/esm/src/kernel/tools/remote.js +577 -0
- package/esm/src/kernel/tools/resolve.d.ts +39 -0
- package/esm/src/kernel/tools/resolve.js +283 -0
- package/esm/src/kernel/tools/schema.d.ts +15 -0
- package/esm/src/kernel/tools/schema.js +176 -0
- package/esm/src/kernel/tools/stage-run.d.ts +105 -0
- package/esm/src/kernel/tools/stage-run.js +155 -0
- package/esm/src/kernel/tools/types.d.ts +394 -0
- package/esm/src/kernel/tools/types.js +9 -0
- package/esm/src/kernel/types.d.ts +540 -256
- package/esm/src/kernel/util/find-last.d.ts +2 -0
- package/esm/src/kernel/util/find-last.js +10 -0
- package/esm/src/observability/destinations.d.ts +31 -0
- package/esm/src/observability/destinations.js +67 -0
- package/esm/src/observability/mod.d.ts +10 -3
- package/esm/src/observability/mod.js +6 -2
- package/esm/src/observability/policy.d.ts +27 -0
- package/esm/src/observability/policy.js +80 -0
- package/esm/src/observability/resolve-policy.d.ts +16 -0
- package/esm/src/observability/resolve-policy.js +64 -0
- package/esm/src/observability/trace-attach.d.ts +8 -4
- package/esm/src/observability/trace-attach.js +50 -29
- package/esm/src/observability/trace-record.d.ts +23 -13
- package/esm/src/observability/trace-record.js +96 -39
- package/esm/src/observability/trace-sink.d.ts +19 -0
- package/esm/src/observability/trace-sink.js +10 -0
- package/esm/src/observability/trace-usage.d.ts +10 -3
- package/esm/src/observability/trace-usage.js +70 -17
- package/esm/src/observability/trace.d.ts +18 -7
- package/esm/src/observability/trace.js +34 -17
- package/esm/src/observability/types.d.ts +113 -0
- package/esm/src/observability/types.js +11 -0
- package/esm/src/presets/google/speech-voices.d.ts +11 -0
- package/esm/src/presets/google/speech-voices.js +41 -0
- package/esm/src/presets/google.d.ts +36 -24
- package/esm/src/presets/google.js +50 -63
- package/esm/src/presets/mod.d.ts +2 -2
- package/esm/src/presets/mod.js +1 -1
- package/esm/src/providers/create-provider.d.ts +20 -17
- package/esm/src/providers/create-provider.js +72 -26
- package/esm/src/providers/google/interactions/framing.d.ts +23 -0
- package/esm/src/providers/google/interactions/framing.js +269 -0
- package/esm/src/providers/google/interactions/mod.d.ts +7 -0
- package/esm/src/providers/google/interactions/mod.js +7 -0
- package/esm/src/providers/google/interactions/stream.d.ts +83 -0
- package/esm/src/providers/google/interactions/stream.js +588 -0
- package/esm/src/providers/google/keys.d.ts +26 -0
- package/esm/src/providers/{keys.js → google/keys.js} +19 -31
- package/esm/src/providers/google/live/framing.d.ts +49 -0
- package/esm/src/providers/google/live/framing.js +552 -0
- package/esm/src/providers/google/live/openapi-schema.d.ts +6 -0
- package/esm/src/providers/google/live/openapi-schema.js +46 -0
- package/esm/src/providers/google/live/session.d.ts +25 -0
- package/esm/src/providers/google/live/session.js +134 -0
- package/esm/src/providers/google/live/stream.d.ts +45 -0
- package/esm/src/providers/google/live/stream.js +214 -0
- package/esm/src/providers/google/urls.d.ts +6 -0
- package/esm/src/providers/google/urls.js +6 -0
- package/esm/src/providers/local/local.d.ts +30 -0
- package/esm/src/providers/{local.js → local/local.js} +66 -126
- package/esm/src/providers/local/mod.d.ts +9 -0
- package/esm/src/providers/local/mod.js +9 -0
- package/esm/src/providers/mod.d.ts +6 -3
- package/esm/src/providers/mod.js +3 -1
- package/esm/src/providers/openrouter/cache-control.d.ts +24 -0
- package/esm/src/providers/openrouter/cache-control.js +23 -0
- package/esm/src/providers/openrouter/chat.d.ts +107 -0
- package/esm/src/providers/{openrouter.js → openrouter/chat.js} +117 -231
- package/esm/src/providers/openrouter/image.d.ts +34 -0
- package/esm/src/providers/openrouter/image.js +275 -0
- package/esm/src/providers/openrouter/openai/chat-payload.d.ts +24 -0
- package/esm/src/providers/openrouter/openai/chat-payload.js +82 -0
- package/esm/src/providers/openrouter/openai/compat.d.ts +53 -0
- package/esm/src/providers/openrouter/openai/compat.js +213 -0
- package/esm/src/providers/openrouter/openai/image-payload.d.ts +18 -0
- package/esm/src/providers/openrouter/openai/image-payload.js +90 -0
- package/esm/src/providers/openrouter/openai/sdk-messages.d.ts +22 -0
- package/esm/src/providers/openrouter/openai/sdk-messages.js +122 -0
- package/esm/src/providers/openrouter/resolve-api-key.d.ts +9 -0
- package/esm/src/providers/openrouter/resolve-api-key.js +24 -0
- package/esm/src/providers/openrouter/speech.d.ts +23 -0
- package/esm/src/providers/{speech.js → openrouter/speech.js} +32 -55
- package/esm/src/providers/probe.d.ts +1 -0
- package/esm/src/providers/probe.js +22 -0
- package/esm/src/providers/shared/pcm.d.ts +12 -0
- package/esm/src/providers/{pcm.js → shared/pcm.js} +16 -3
- package/esm/src/providers/shared/sse.d.ts +18 -0
- package/esm/src/providers/shared/sse.js +87 -0
- package/esm/src/providers/shared/tool-args.d.ts +17 -0
- package/esm/src/providers/shared/tool-args.js +45 -0
- package/esm/src/providers/shared/upstream-tap.d.ts +5 -0
- package/esm/src/providers/{google-tap.js → shared/upstream-tap.js} +4 -7
- package/esm/src/providers/shared/upstream-tape.d.ts +6 -0
- package/esm/src/providers/{gemini-tape.js → shared/upstream-tape.js} +12 -22
- package/esm/src/providers/types.d.ts +27 -0
- package/esm/src/providers/types.js +1 -0
- package/package.json +11 -7
- package/docs/cli.md +0 -97
- package/docs/guardrails.md +0 -178
- package/docs/host.md +0 -97
- package/docs/kernel.md +0 -404
- package/docs/observability.md +0 -105
- package/docs/openrouter.md +0 -125
- package/docs/presets-google.md +0 -91
- package/docs/presets.md +0 -88
- package/docs/providers.md +0 -202
- package/docs/streaming.md +0 -96
- package/esm/src/kernel/engine/boundary.d.ts +0 -10
- package/esm/src/kernel/engine/boundary.js +0 -55
- package/esm/src/kernel/engine/runner/tools.d.ts +0 -13
- package/esm/src/kernel/engine/runner/tools.js +0 -198
- package/esm/src/kernel/registry/tools.d.ts +0 -12
- package/esm/src/kernel/registry/tools.js +0 -36
- package/esm/src/providers/expose-for-tests.d.ts +0 -1
- package/esm/src/providers/expose-for-tests.js +0 -25
- package/esm/src/providers/gemini-tape.d.ts +0 -2
- package/esm/src/providers/google-tap.d.ts +0 -3
- package/esm/src/providers/interactions.d.ts +0 -5
- package/esm/src/providers/interactions.js +0 -169
- package/esm/src/providers/keys.d.ts +0 -19
- package/esm/src/providers/local.d.ts +0 -29
- package/esm/src/providers/openrouter-mod.d.ts +0 -13
- package/esm/src/providers/openrouter-mod.js +0 -12
- package/esm/src/providers/openrouter-payload.d.ts +0 -39
- package/esm/src/providers/openrouter-payload.js +0 -195
- package/esm/src/providers/openrouter.d.ts +0 -15
- package/esm/src/providers/pcm.d.ts +0 -7
- package/esm/src/providers/provider.d.ts +0 -15
- package/esm/src/providers/provider.js +0 -202
- package/esm/src/providers/speech.d.ts +0 -23
- package/esm/src/providers/sse.d.ts +0 -7
- package/esm/src/providers/sse.js +0 -55
- package/esm/src/streaming/mod.d.ts +0 -9
- package/esm/src/streaming/mod.js +0 -8
- /package/esm/src/{streaming → host}/readStreamingJsonStringField.d.ts +0 -0
- /package/esm/src/{streaming → host}/readStreamingJsonStringField.js +0 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-initiated tool execution entrypoint.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { toErrorEvent } from '../../guardrails/error.js';
|
|
7
|
+
import { getProfile } from '../registry/profiles.js';
|
|
8
|
+
import { pickModel } from '../registry/resolve.js';
|
|
9
|
+
import { executeRegisteredTool, newCallId } from './execute.js';
|
|
10
|
+
import { cloneTurnToolSnapshot, prepareTurnToolSnapshot, promoteLoadedTools } from './resolve.js';
|
|
11
|
+
function turnRequestFromInvoke(request) {
|
|
12
|
+
return {
|
|
13
|
+
profile: request.profile,
|
|
14
|
+
path: request.path,
|
|
15
|
+
sessionPermissions: request.sessionPermissions,
|
|
16
|
+
input: request.turnInput,
|
|
17
|
+
model: request.model,
|
|
18
|
+
host: request.host,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
async function prepareInvokeSnapshot(request, profile) {
|
|
22
|
+
const req = turnRequestFromInvoke(request);
|
|
23
|
+
// Host profiles bind no model — the allow list is the whole snapshot.
|
|
24
|
+
const model = profile.type === 'host' ? undefined : pickModel(profile, request.model);
|
|
25
|
+
return await prepareTurnToolSnapshot(profile, req, model);
|
|
26
|
+
}
|
|
27
|
+
/** Execute a registered tool without calling a model provider. */
|
|
28
|
+
async function* invokeTool(request) {
|
|
29
|
+
const profile = getProfile(request.profile);
|
|
30
|
+
const callId = newCallId(request.name);
|
|
31
|
+
const snapshot = request.snapshot
|
|
32
|
+
? cloneTurnToolSnapshot(request.snapshot)
|
|
33
|
+
: await prepareInvokeSnapshot(request, profile);
|
|
34
|
+
if (request.promoted?.length) {
|
|
35
|
+
const { failure } = promoteLoadedTools(snapshot, request.promoted, profile);
|
|
36
|
+
if (failure) {
|
|
37
|
+
yield {
|
|
38
|
+
type: 'tool',
|
|
39
|
+
tool: {
|
|
40
|
+
name: request.name,
|
|
41
|
+
callId,
|
|
42
|
+
phase: 'error',
|
|
43
|
+
failure,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
yield { type: 'done', stop: { kind: 'completed' } };
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
let sawGate = false;
|
|
51
|
+
let sawError = false;
|
|
52
|
+
try {
|
|
53
|
+
const handlers = request.onStage ? [request.onStage] : [];
|
|
54
|
+
for await (const event of executeRegisteredTool({
|
|
55
|
+
profile,
|
|
56
|
+
name: request.name,
|
|
57
|
+
input: request.input,
|
|
58
|
+
callId,
|
|
59
|
+
ctx: {
|
|
60
|
+
sessionPermissions: request.sessionPermissions,
|
|
61
|
+
credentials: request.credentials,
|
|
62
|
+
path: request.path,
|
|
63
|
+
signal: request.signal,
|
|
64
|
+
resume: request.resume,
|
|
65
|
+
host: request.host,
|
|
66
|
+
},
|
|
67
|
+
snapshot,
|
|
68
|
+
stages: {
|
|
69
|
+
handlers,
|
|
70
|
+
profile,
|
|
71
|
+
step: 1,
|
|
72
|
+
history: () => [],
|
|
73
|
+
injectAllowed: false,
|
|
74
|
+
host: request.host,
|
|
75
|
+
signal: request.signal,
|
|
76
|
+
},
|
|
77
|
+
})) {
|
|
78
|
+
yield event;
|
|
79
|
+
if (event.type !== 'tool') {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (event.tool?.phase === 'gate') {
|
|
83
|
+
sawGate = true;
|
|
84
|
+
}
|
|
85
|
+
if (event.tool?.phase === 'error') {
|
|
86
|
+
sawError = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
yield toErrorEvent(err);
|
|
92
|
+
sawError = true;
|
|
93
|
+
}
|
|
94
|
+
const stopKind = sawGate ? 'gate' : sawError ? 'completed' : 'completed';
|
|
95
|
+
yield {
|
|
96
|
+
type: 'done',
|
|
97
|
+
stop: { kind: stopKind },
|
|
98
|
+
...(sawGate ? { tools: snapshot } : {}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export { invokeTool };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THEORUM tool registry and execution.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export { coerceToolResultParts, executeRegisteredTool, formatToolResult, leanToolResultData, projectForModel, } from './execute.js';
|
|
7
|
+
export { registerHarnessTools } from './harness.js';
|
|
8
|
+
export { invokeTool } from './invoke.js';
|
|
9
|
+
export { getTool, hasTool, listBuiltinIds, listFunctionIds, listTools, registerTool, registerTools, requireTool, resetTools, } from './registry.js';
|
|
10
|
+
export type { McpProtocolVersion, McpRpcResponse } from './remote.js';
|
|
11
|
+
export { buildHttpToolTarget, executeHttpTool, executeMcpTool, isUnsupportedMcpProtocolError, MCP_PROTOCOL_VERSIONS, parseMcpRpcResponse, resolveToolAuth, } from './remote.js';
|
|
12
|
+
export { cloneTurnToolSnapshot, prepareTurnToolSnapshot } from './resolve.js';
|
|
13
|
+
export type * from './types.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THEORUM tool registry and execution.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export { coerceToolResultParts, executeRegisteredTool, formatToolResult, leanToolResultData, projectForModel, } from './execute.js';
|
|
7
|
+
export { registerHarnessTools } from './harness.js';
|
|
8
|
+
export { invokeTool } from './invoke.js';
|
|
9
|
+
export { getTool, hasTool, listBuiltinIds, listFunctionIds, listTools, registerTool, registerTools, requireTool, resetTools, } from './registry.js';
|
|
10
|
+
export { buildHttpToolTarget, executeHttpTool, executeMcpTool, isUnsupportedMcpProtocolError, MCP_PROTOCOL_VERSIONS, parseMcpRpcResponse, resolveToolAuth, } from './remote.js';
|
|
11
|
+
export { cloneTurnToolSnapshot, prepareTurnToolSnapshot } from './resolve.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative catalog permission + gate-resume helpers.
|
|
3
|
+
*
|
|
4
|
+
* Shared by function and HTTP/MCP execute paths (no circular import with remote).
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { InvokeToolResume, ToolGate, ToolPermission } from './types.js';
|
|
9
|
+
export declare function isResumeContinuation(resume?: InvokeToolResume): boolean;
|
|
10
|
+
/** Gate resume — only `granted: true` skips confirm/permission/`preTool` re-ask. */
|
|
11
|
+
export declare function isGateResumeGranted(resume?: InvokeToolResume): boolean;
|
|
12
|
+
/** Host denied after a gate — settle without running the body. */
|
|
13
|
+
export declare function isGateResumeDenied(resume?: InvokeToolResume): boolean;
|
|
14
|
+
export declare function permissionGranted(toolName: string, sessionPermissions?: string[]): boolean;
|
|
15
|
+
export declare function checkPermission(toolName: string, permission: ToolPermission, sessionPermissions?: string[], resume?: InvokeToolResume): ToolGate | null;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative catalog permission + gate-resume helpers.
|
|
3
|
+
*
|
|
4
|
+
* Shared by function and HTTP/MCP execute paths (no circular import with remote).
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
export function isResumeContinuation(resume) {
|
|
9
|
+
return resume?.value !== undefined || typeof resume?.granted === 'boolean';
|
|
10
|
+
}
|
|
11
|
+
/** Gate resume — only `granted: true` skips confirm/permission/`preTool` re-ask. */
|
|
12
|
+
export function isGateResumeGranted(resume) {
|
|
13
|
+
return resume?.granted === true;
|
|
14
|
+
}
|
|
15
|
+
/** Host denied after a gate — settle without running the body. */
|
|
16
|
+
export function isGateResumeDenied(resume) {
|
|
17
|
+
return resume?.granted === false;
|
|
18
|
+
}
|
|
19
|
+
export function permissionGranted(toolName, sessionPermissions) {
|
|
20
|
+
if (!sessionPermissions) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return sessionPermissions.includes('*') || sessionPermissions.includes(toolName);
|
|
24
|
+
}
|
|
25
|
+
export function checkPermission(toolName, permission, sessionPermissions, resume) {
|
|
26
|
+
if (permission === 'auto') {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
if (permission === 'always_confirm') {
|
|
30
|
+
if (resume?.granted === true) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
kind: 'permission',
|
|
35
|
+
tool: toolName,
|
|
36
|
+
permission,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (permissionGranted(toolName, sessionPermissions)) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
kind: 'permission',
|
|
44
|
+
tool: toolName,
|
|
45
|
+
permission,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool projection for host/UI inspection.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { Profile, ToolId } from '../types.js';
|
|
7
|
+
import type { RegisteredTool } from './types.js';
|
|
8
|
+
declare function projectTools(profile: Profile): Array<RegisteredTool | {
|
|
9
|
+
name: ToolId;
|
|
10
|
+
missing: true;
|
|
11
|
+
}>;
|
|
12
|
+
export { projectTools };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool projection for host/UI inspection.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { getTool } from './registry.js';
|
|
7
|
+
function projectTool(name) {
|
|
8
|
+
const tool = getTool(name);
|
|
9
|
+
if (!tool) {
|
|
10
|
+
return { name, missing: true };
|
|
11
|
+
}
|
|
12
|
+
return tool;
|
|
13
|
+
}
|
|
14
|
+
function builtInToolIds(profile) {
|
|
15
|
+
const seen = new Set();
|
|
16
|
+
if (profile.type === 'host') {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
for (const binding of Object.values(profile.models)) {
|
|
20
|
+
for (const id of binding.builtInTools ?? []) {
|
|
21
|
+
seen.add(id);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return [...seen];
|
|
25
|
+
}
|
|
26
|
+
function profileAllow(profile) {
|
|
27
|
+
if (profile.type === 'speech') {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return profile.tools.allow;
|
|
31
|
+
}
|
|
32
|
+
function projectTools(profile) {
|
|
33
|
+
const ids = [...profileAllow(profile), ...builtInToolIds(profile)];
|
|
34
|
+
return ids.map((name) => projectTool(name));
|
|
35
|
+
}
|
|
36
|
+
export { projectTools };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-local tool registry.
|
|
3
|
+
*
|
|
4
|
+
* Registration is not synchronized — hosts must register tools at startup before
|
|
5
|
+
* concurrent turns or invokeTool calls. Reads during execution are safe under Deno's
|
|
6
|
+
* single-threaded event loop; concurrent mutation of a shared TurnToolSnapshot is
|
|
7
|
+
* avoided by cloneTurnToolSnapshot on invokeTool entry.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { RegisteredTool, ToolDefinitionInput } from './types.js';
|
|
12
|
+
/** Register or replace a tool definition. */
|
|
13
|
+
declare function registerTool<TIn, TOut>(def: ToolDefinitionInput<TIn, TOut>): RegisteredTool<TIn, TOut>;
|
|
14
|
+
/** Register several tools in order. */
|
|
15
|
+
declare function registerTools(defs: ToolDefinitionInput[]): RegisteredTool[];
|
|
16
|
+
declare function getTool(name: string): RegisteredTool | undefined;
|
|
17
|
+
declare function requireTool(name: string): RegisteredTool;
|
|
18
|
+
declare function hasTool(name: string): boolean;
|
|
19
|
+
declare function listTools(): RegisteredTool[];
|
|
20
|
+
declare function listBuiltinIds(): string[];
|
|
21
|
+
declare function listFunctionIds(): string[];
|
|
22
|
+
declare function resetTools(): void;
|
|
23
|
+
export { getTool, hasTool, listBuiltinIds, listFunctionIds, listTools, registerTool, registerTools, requireTool, resetTools, };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-local tool registry.
|
|
3
|
+
*
|
|
4
|
+
* Registration is not synchronized — hosts must register tools at startup before
|
|
5
|
+
* concurrent turns or invokeTool calls. Reads during execution are safe under Deno's
|
|
6
|
+
* single-threaded event loop; concurrent mutation of a shared TurnToolSnapshot is
|
|
7
|
+
* avoided by cloneTurnToolSnapshot on invokeTool entry.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { TheorumError } from '../../guardrails/error.js';
|
|
12
|
+
import { jsonSchemaFromZod, validateToolInputSchema, validateToolOutputSchema } from './schema.js';
|
|
13
|
+
const tools = new Map();
|
|
14
|
+
function schemasFromZod(input, output) {
|
|
15
|
+
const inputSchema = jsonSchemaFromZod(input, 'input');
|
|
16
|
+
validateToolInputSchema(inputSchema);
|
|
17
|
+
const outputSchema = jsonSchemaFromZod(output, 'output');
|
|
18
|
+
validateToolOutputSchema(outputSchema);
|
|
19
|
+
return { inputSchema, outputSchema };
|
|
20
|
+
}
|
|
21
|
+
function normalizeHttp(def) {
|
|
22
|
+
return { ...def, type: 'http', ...schemasFromZod(def.input, def.output) };
|
|
23
|
+
}
|
|
24
|
+
function normalizeMcp(def) {
|
|
25
|
+
return { ...def, type: 'mcp', ...schemasFromZod(def.input, def.output) };
|
|
26
|
+
}
|
|
27
|
+
function normalizeFunction(def) {
|
|
28
|
+
return { ...def, type: 'function', ...schemasFromZod(def.input, def.output) };
|
|
29
|
+
}
|
|
30
|
+
function normalizeToolDefinition(def) {
|
|
31
|
+
if (def.type === 'builtin') {
|
|
32
|
+
return def;
|
|
33
|
+
}
|
|
34
|
+
if (def.type === 'http') {
|
|
35
|
+
return normalizeHttp(def);
|
|
36
|
+
}
|
|
37
|
+
if (def.type === 'mcp') {
|
|
38
|
+
return normalizeMcp(def);
|
|
39
|
+
}
|
|
40
|
+
return normalizeFunction(def);
|
|
41
|
+
}
|
|
42
|
+
/** Register or replace a tool definition. */
|
|
43
|
+
function registerTool(def) {
|
|
44
|
+
const normalized = normalizeToolDefinition(def);
|
|
45
|
+
tools.set(normalized.name, normalized);
|
|
46
|
+
return normalized;
|
|
47
|
+
}
|
|
48
|
+
/** Register several tools in order. */
|
|
49
|
+
function registerTools(defs) {
|
|
50
|
+
return defs.map((def) => registerTool(def));
|
|
51
|
+
}
|
|
52
|
+
function getTool(name) {
|
|
53
|
+
return tools.get(name);
|
|
54
|
+
}
|
|
55
|
+
function requireTool(name) {
|
|
56
|
+
const tool = getTool(name);
|
|
57
|
+
if (!tool) {
|
|
58
|
+
throw new TheorumError(`Tool '${name}' is not registered`); // lexicon-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
|
|
59
|
+
}
|
|
60
|
+
return tool;
|
|
61
|
+
}
|
|
62
|
+
function hasTool(name) {
|
|
63
|
+
return tools.has(name);
|
|
64
|
+
}
|
|
65
|
+
function listTools() {
|
|
66
|
+
return [...tools.values()];
|
|
67
|
+
}
|
|
68
|
+
function listBuiltinIds() {
|
|
69
|
+
return listTools()
|
|
70
|
+
.filter((t) => t.type === 'builtin')
|
|
71
|
+
.map((t) => t.name);
|
|
72
|
+
}
|
|
73
|
+
function listFunctionIds() {
|
|
74
|
+
return listTools()
|
|
75
|
+
.filter((t) => t.type === 'function')
|
|
76
|
+
.map((t) => t.name);
|
|
77
|
+
}
|
|
78
|
+
function resetTools() {
|
|
79
|
+
tools.clear();
|
|
80
|
+
}
|
|
81
|
+
export { getTool, hasTool, listBuiltinIds, listFunctionIds, listTools, registerTool, registerTools, requireTool, resetTools, };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execution runners for Declarative HTTP and Remote MCP tools.
|
|
3
|
+
*
|
|
4
|
+
* Implements:
|
|
5
|
+
* - URL parameter substitution and query/body mapping
|
|
6
|
+
* - Network SSRF guardrail enforcement via `assertSafeUrl`
|
|
7
|
+
* - Credential resolution (bearer, api_key, oauth2)
|
|
8
|
+
* - Proactive OAuth token refresh with progress event emission
|
|
9
|
+
* - ToolGate { kind: 'auth' } emission or model error reporting
|
|
10
|
+
* - Streamable HTTP MCP JSON-RPC protocol (`tools/call`) per 2026-07-28 spec
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type { TurnEvent } from '../types.js';
|
|
15
|
+
import { type ToolCallBase } from './events.js';
|
|
16
|
+
import { type ToolStageSupport } from './stage-run.js';
|
|
17
|
+
import type { HttpToolAuthConfig, HttpToolDef, McpToolDef, ModelToolResult, ToolBodyOutcome, ToolContext, ToolGate } from './types.js';
|
|
18
|
+
export type AuthResolveResult = {
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
unauthenticated?: boolean;
|
|
21
|
+
modelMessage?: string;
|
|
22
|
+
gate?: ToolGate;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Resolves credentials and handles missing/expired tokens.
|
|
26
|
+
* Yields TurnEvents for auth pauses or progress (e.g. refreshed tokens).
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveToolAuth(toolName: string, authConfig: HttpToolAuthConfig | undefined, ctx: ToolContext, base: ToolCallBase): AsyncGenerator<TurnEvent, AuthResolveResult>;
|
|
29
|
+
export type HttpToolMapping = NonNullable<HttpToolDef['mapping']>;
|
|
30
|
+
export type HttpToolTarget = {
|
|
31
|
+
url: string;
|
|
32
|
+
body?: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Build the request URL (and optional JSON body) for a declarative HTTP tool
|
|
36
|
+
* from endpoint template, mapping, and validated input.
|
|
37
|
+
*/
|
|
38
|
+
export declare function buildHttpToolTarget(endpoint: string, method: HttpToolDef['method'], input: Record<string, unknown>, mapping?: HttpToolMapping): HttpToolTarget;
|
|
39
|
+
export declare function parseToolOutput<T>(schema: {
|
|
40
|
+
safeParse: (data: unknown) => {
|
|
41
|
+
success: true;
|
|
42
|
+
data: T;
|
|
43
|
+
} | {
|
|
44
|
+
success: false;
|
|
45
|
+
error: {
|
|
46
|
+
flatten: () => unknown;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
}, responseData: unknown): {
|
|
50
|
+
success: true;
|
|
51
|
+
data: T;
|
|
52
|
+
} | {
|
|
53
|
+
success: false;
|
|
54
|
+
error: {
|
|
55
|
+
flatten: () => unknown;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export declare function modelResultFromOutput(data: unknown): ModelToolResult;
|
|
59
|
+
/**
|
|
60
|
+
* Executes a Declarative HTTP tool.
|
|
61
|
+
* Order: schema → permission → auth → preTool/host stages → body.
|
|
62
|
+
*/
|
|
63
|
+
export declare function executeHttpTool(tool: HttpToolDef, rawInput: unknown, ctx: ToolContext, base: ToolCallBase, stages?: ToolStageSupport): AsyncGenerator<TurnEvent, ToolBodyOutcome>;
|
|
64
|
+
/** Preferred-first Streamable HTTP protocol revisions the kernel will negotiate. */
|
|
65
|
+
export declare const MCP_PROTOCOL_VERSIONS: readonly ["2026-07-28", "2025-11-25", "2025-06-18", "2025-03-26"];
|
|
66
|
+
export type McpProtocolVersion = (typeof MCP_PROTOCOL_VERSIONS)[number];
|
|
67
|
+
export type McpRpcResponse = {
|
|
68
|
+
jsonrpc?: string;
|
|
69
|
+
id?: unknown;
|
|
70
|
+
result?: {
|
|
71
|
+
content?: Array<{
|
|
72
|
+
type: string;
|
|
73
|
+
text?: string;
|
|
74
|
+
[key: string]: unknown;
|
|
75
|
+
}>;
|
|
76
|
+
isError?: boolean;
|
|
77
|
+
[key: string]: unknown;
|
|
78
|
+
};
|
|
79
|
+
error?: {
|
|
80
|
+
code: number;
|
|
81
|
+
message: string;
|
|
82
|
+
data?: unknown;
|
|
83
|
+
};
|
|
84
|
+
method?: string;
|
|
85
|
+
};
|
|
86
|
+
/** Parse MCP JSON or SSE (`event: message` / `data:`) bodies into a JSON-RPC object. */
|
|
87
|
+
export declare function parseMcpRpcResponse(text: string): McpRpcResponse;
|
|
88
|
+
/** True when a JSON-RPC error indicates the server rejected our protocol revision. */
|
|
89
|
+
export declare function isUnsupportedMcpProtocolError(error: McpRpcResponse['error']): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Executes a Remote MCP tool over Streamable HTTP (spec revision 2026-07-28).
|
|
92
|
+
* Order: schema → permission → auth → preTool/host stages → body.
|
|
93
|
+
*/
|
|
94
|
+
export declare function executeMcpTool(tool: McpToolDef, rawInput: unknown, ctx: ToolContext, base: ToolCallBase, stages?: ToolStageSupport): AsyncGenerator<TurnEvent, ToolBodyOutcome>;
|