theorum 0.1.15 → 1.0.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/README.md +177 -90
- package/esm/mod.d.ts +21 -11
- package/esm/mod.js +15 -8
- package/esm/src/cli/commands/bench.js +6 -3
- package/esm/src/cli/commands/fuzz-canary.d.ts +13 -0
- package/esm/src/cli/commands/fuzz-canary.js +195 -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/profile.js +25 -14
- package/esm/src/cli/commands/run.d.ts +3 -0
- package/esm/src/cli/commands/run.js +22 -30
- package/esm/src/cli/commands/test.d.ts +10 -1
- package/esm/src/cli/commands/test.js +31 -31
- 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 +45 -11
- package/esm/src/cli/matrix/synthesizer.d.ts +7 -9
- package/esm/src/cli/matrix/synthesizer.js +32 -106
- package/esm/src/guardrails/canary-gate.d.ts +20 -0
- package/esm/src/guardrails/canary-gate.js +31 -0
- package/esm/src/guardrails/canary.d.ts +26 -0
- package/esm/src/guardrails/canary.js +139 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +16 -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 +216 -0
- package/esm/src/guardrails/corpus/inbound-payloads.d.ts +10 -0
- package/esm/src/guardrails/corpus/inbound-payloads.js +124 -0
- package/esm/src/guardrails/corpus/live-attacks.d.ts +20 -0
- package/esm/src/guardrails/corpus/live-attacks.js +232 -0
- package/esm/src/guardrails/corpus/mod.d.ts +13 -0
- package/esm/src/guardrails/corpus/mod.js +11 -0
- package/esm/src/guardrails/corpus/secrets.d.ts +16 -0
- package/esm/src/guardrails/corpus/secrets.js +16 -0
- package/esm/src/guardrails/corpus/strings.d.ts +27 -0
- package/esm/src/guardrails/corpus/strings.js +33 -0
- package/esm/src/guardrails/corpus/types.d.ts +37 -0
- package/esm/src/guardrails/corpus/types.js +6 -0
- package/esm/src/guardrails/egress.d.ts +9 -0
- package/esm/src/guardrails/egress.js +36 -0
- package/esm/src/guardrails/error.js +6 -4
- package/esm/src/guardrails/injection.js +28 -5
- package/esm/src/guardrails/live-outbound-gate.d.ts +39 -0
- package/esm/src/guardrails/live-outbound-gate.js +180 -0
- package/esm/src/guardrails/mod.d.ts +13 -4
- package/esm/src/guardrails/mod.js +10 -4
- package/esm/src/guardrails/quota.js +1 -1
- package/esm/src/guardrails/sanitize.d.ts +11 -15
- package/esm/src/guardrails/sanitize.js +40 -49
- package/esm/src/guardrails/sensitive.js +2 -1
- package/esm/src/guardrails/testing.d.ts +10 -0
- package/esm/src/guardrails/testing.js +9 -0
- package/esm/src/host/client-turn.d.ts +19 -0
- package/esm/src/host/client-turn.js +32 -0
- package/esm/src/host/mod.d.ts +5 -3
- package/esm/src/host/mod.js +4 -3
- package/esm/src/interface/blocks.d.ts +20 -0
- package/esm/src/interface/blocks.js +180 -0
- package/esm/src/interface/from-profile.d.ts +14 -0
- package/esm/src/interface/from-profile.js +104 -0
- package/esm/src/interface/inputs.d.ts +8 -0
- package/esm/src/interface/inputs.js +127 -0
- package/esm/src/interface/mod.d.ts +10 -0
- package/esm/src/interface/mod.js +9 -0
- package/esm/src/interface/types.d.ts +153 -0
- package/esm/src/interface/types.js +13 -0
- package/esm/src/kernel/engine/delta.d.ts +24 -2
- package/esm/src/kernel/engine/delta.js +437 -39
- package/esm/src/kernel/engine/live-inbound.d.ts +9 -0
- package/esm/src/kernel/engine/live-inbound.js +17 -0
- package/esm/src/kernel/engine/repair.js +1 -1
- package/esm/src/kernel/engine/runner/gates.d.ts +1 -1
- package/esm/src/kernel/engine/runner/gates.js +7 -7
- package/esm/src/kernel/engine/runner/mod.js +13 -13
- package/esm/src/kernel/engine/runner/state.d.ts +7 -0
- package/esm/src/kernel/engine/runner/steps.d.ts +1 -1
- package/esm/src/kernel/engine/runner/steps.js +171 -37
- package/esm/src/kernel/engine/runner/stream.d.ts +1 -2
- package/esm/src/kernel/engine/runner/stream.js +66 -29
- package/esm/src/kernel/engine/session/mod.d.ts +23 -0
- package/esm/src/kernel/engine/session/mod.js +205 -0
- package/esm/src/kernel/mod.d.ts +9 -10
- package/esm/src/kernel/mod.js +6 -8
- package/esm/src/kernel/registry/attachments.d.ts +4 -2
- package/esm/src/kernel/registry/attachments.js +6 -3
- package/esm/src/kernel/registry/catalog.d.ts +3 -16
- package/esm/src/kernel/registry/catalog.js +5 -81
- package/esm/src/kernel/registry/ingress.d.ts +9 -4
- package/esm/src/kernel/registry/ingress.js +73 -63
- package/esm/src/kernel/registry/profiles.d.ts +35 -12
- package/esm/src/kernel/registry/profiles.js +159 -66
- package/esm/src/kernel/registry/provider-request.js +11 -7
- package/esm/src/kernel/registry/resolve.d.ts +5 -6
- package/esm/src/kernel/registry/resolve.js +100 -70
- package/esm/src/kernel/registry/vault.d.ts +12 -7
- package/esm/src/kernel/registry/vault.js +30 -8
- package/esm/src/kernel/schema.d.ts +145 -0
- package/esm/src/kernel/schema.js +462 -0
- package/esm/src/kernel/stop.d.ts +15 -10
- package/esm/src/kernel/stop.js +5 -5
- package/esm/src/kernel/tools/execute.d.ts +45 -0
- package/esm/src/kernel/tools/execute.js +363 -0
- package/esm/src/kernel/tools/harness.d.ts +8 -0
- package/esm/src/kernel/tools/harness.js +44 -0
- package/esm/src/kernel/tools/invoke.d.ts +10 -0
- package/esm/src/kernel/tools/invoke.js +83 -0
- package/esm/src/kernel/tools/mod.d.ts +11 -0
- package/esm/src/kernel/tools/mod.js +10 -0
- package/esm/src/kernel/tools/project.d.ts +12 -0
- package/esm/src/kernel/tools/project.js +34 -0
- package/esm/src/kernel/tools/registry.d.ts +23 -0
- package/esm/src/kernel/tools/registry.js +71 -0
- package/esm/src/kernel/tools/resolve.d.ts +29 -0
- package/esm/src/kernel/tools/resolve.js +262 -0
- package/esm/src/kernel/tools/schema.d.ts +13 -0
- package/esm/src/kernel/tools/schema.js +159 -0
- package/esm/src/kernel/tools/types.d.ts +216 -0
- package/esm/src/kernel/tools/types.js +9 -0
- package/esm/src/kernel/types.d.ts +338 -193
- package/esm/src/observability/trace-attach.d.ts +6 -4
- package/esm/src/observability/trace-attach.js +39 -25
- package/esm/src/observability/trace-record.d.ts +11 -9
- package/esm/src/observability/trace-record.js +36 -23
- 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 +9 -1
- package/esm/src/observability/trace.js +11 -3
- 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 +14 -14
- package/esm/src/providers/create-provider.js +55 -23
- package/esm/src/providers/google/interactions/framing.d.ts +15 -0
- package/esm/src/providers/google/interactions/framing.js +208 -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 +46 -0
- package/esm/src/providers/google/live/framing.js +515 -0
- package/esm/src/providers/google/live/openapi-schema.d.ts +6 -0
- package/esm/src/providers/google/live/openapi-schema.js +45 -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 +36 -0
- package/esm/src/providers/google/live/stream.js +201 -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/chat.d.ts +87 -0
- package/esm/src/providers/{openrouter.js → openrouter/chat.js} +70 -229
- package/esm/src/providers/openrouter/image.d.ts +34 -0
- package/esm/src/providers/openrouter/image.js +286 -0
- package/esm/src/providers/openrouter/openai/chat-payload.d.ts +24 -0
- package/esm/src/providers/openrouter/openai/chat-payload.js +60 -0
- package/esm/src/providers/openrouter/openai/compat.d.ts +47 -0
- package/esm/src/providers/openrouter/openai/compat.js +194 -0
- package/esm/src/providers/openrouter/openai/image-payload.d.ts +18 -0
- package/esm/src/providers/openrouter/openai/image-payload.js +84 -0
- package/esm/src/providers/openrouter/openai/sdk-messages.d.ts +22 -0
- package/esm/src/providers/openrouter/openai/sdk-messages.js +95 -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
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenRouter payload builder.
|
|
3
|
-
*
|
|
4
|
-
* Converts THEORUM provider requests into OpenAI-compatible chat completion
|
|
5
|
-
* payloads for OpenRouter and compatible gateways.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
import { getTool } from '../kernel/registry/catalog.js';
|
|
10
|
-
import { getStructured } from '../kernel/registry/schemas.js';
|
|
11
|
-
/**
|
|
12
|
-
* Resolve a THEORUM model id to an OpenRouter model string.
|
|
13
|
-
* Prefer `openRouterId` / `apiId` from the profile model spec on the request.
|
|
14
|
-
*/
|
|
15
|
-
function resolveOpenRouterModel(modelId, customMap, wire) {
|
|
16
|
-
if (customMap?.[modelId]) {
|
|
17
|
-
return customMap[modelId];
|
|
18
|
-
}
|
|
19
|
-
if (wire?.openRouterId) {
|
|
20
|
-
return wire.openRouterId;
|
|
21
|
-
}
|
|
22
|
-
if (wire?.apiId?.includes('/')) {
|
|
23
|
-
return wire.apiId;
|
|
24
|
-
}
|
|
25
|
-
if (wire?.apiId) {
|
|
26
|
-
return `google/${wire.apiId}`;
|
|
27
|
-
}
|
|
28
|
-
if (String(modelId).includes('/')) {
|
|
29
|
-
return String(modelId);
|
|
30
|
-
}
|
|
31
|
-
return String(modelId);
|
|
32
|
-
}
|
|
33
|
-
/** Map THEORUM thinking level to OpenRouter/OpenAI `reasoning.effort`. */
|
|
34
|
-
function mapThinkingEffort(thinking) {
|
|
35
|
-
return thinking;
|
|
36
|
-
}
|
|
37
|
-
function wireAudioPart(part) {
|
|
38
|
-
let format = 'mp3';
|
|
39
|
-
if (part.mimeType.includes('wav')) {
|
|
40
|
-
format = 'wav';
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
type: 'input_audio',
|
|
44
|
-
input_audio: {
|
|
45
|
-
data: part.data,
|
|
46
|
-
format,
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function wireMessageContent(parts) {
|
|
51
|
-
const isAllText = parts.every((p) => p.type === 'text');
|
|
52
|
-
if (isAllText) {
|
|
53
|
-
return parts
|
|
54
|
-
.map((p) => {
|
|
55
|
-
if (p.type === 'text') {
|
|
56
|
-
return p.text;
|
|
57
|
-
}
|
|
58
|
-
return '';
|
|
59
|
-
})
|
|
60
|
-
.join('\n');
|
|
61
|
-
}
|
|
62
|
-
return parts.map((part) => {
|
|
63
|
-
if (part.type === 'text') {
|
|
64
|
-
return { type: 'text', text: part.text };
|
|
65
|
-
}
|
|
66
|
-
if (part.type === 'image') {
|
|
67
|
-
return {
|
|
68
|
-
type: 'image_url',
|
|
69
|
-
image_url: { url: `data:${part.mimeType};base64,${part.data}` },
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
if (part.type === 'audio') {
|
|
73
|
-
return wireAudioPart(part);
|
|
74
|
-
}
|
|
75
|
-
return { type: 'text', text: '' };
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
function wireHistoryMessage(msg) {
|
|
79
|
-
let content = msg.content ?? '';
|
|
80
|
-
if (msg.parts && msg.parts.length > 0) {
|
|
81
|
-
content = wireMessageContent(msg.parts);
|
|
82
|
-
}
|
|
83
|
-
const wired = {
|
|
84
|
-
role: msg.role,
|
|
85
|
-
content,
|
|
86
|
-
};
|
|
87
|
-
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
88
|
-
wired.tool_calls = msg.tool_calls;
|
|
89
|
-
}
|
|
90
|
-
if (msg.tool_call_id) {
|
|
91
|
-
wired.tool_call_id = msg.tool_call_id;
|
|
92
|
-
}
|
|
93
|
-
if (msg.name) {
|
|
94
|
-
wired.name = msg.name;
|
|
95
|
-
}
|
|
96
|
-
return wired;
|
|
97
|
-
}
|
|
98
|
-
function wireTools(dynamicTools) {
|
|
99
|
-
if (!dynamicTools || dynamicTools.length === 0) {
|
|
100
|
-
return [];
|
|
101
|
-
}
|
|
102
|
-
return dynamicTools.map((t) => ({
|
|
103
|
-
type: 'function',
|
|
104
|
-
function: {
|
|
105
|
-
name: t.name,
|
|
106
|
-
description: t.description ?? '',
|
|
107
|
-
parameters: t.parameters ?? { type: 'object', properties: {} },
|
|
108
|
-
},
|
|
109
|
-
}));
|
|
110
|
-
}
|
|
111
|
-
function buildMessages(req) {
|
|
112
|
-
const messages = [];
|
|
113
|
-
if (req.system) {
|
|
114
|
-
messages.push({ role: 'system', content: req.system });
|
|
115
|
-
}
|
|
116
|
-
if (req.history && req.history.length > 0) {
|
|
117
|
-
for (const h of req.history) {
|
|
118
|
-
messages.push(wireHistoryMessage(h));
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (req.input.length > 0) {
|
|
122
|
-
messages.push({
|
|
123
|
-
role: 'user',
|
|
124
|
-
content: wireMessageContent(req.input),
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
return messages;
|
|
128
|
-
}
|
|
129
|
-
function resolveResponseFormat(structured) {
|
|
130
|
-
if (!structured) {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
const spec = getStructured(structured);
|
|
134
|
-
if (!spec.jsonSchema) {
|
|
135
|
-
return undefined;
|
|
136
|
-
}
|
|
137
|
-
return {
|
|
138
|
-
type: 'json_schema',
|
|
139
|
-
json_schema: {
|
|
140
|
-
name: String(structured),
|
|
141
|
-
strict: true,
|
|
142
|
-
schema: spec.jsonSchema,
|
|
143
|
-
},
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
/** Convert a provider-neutral request into an OpenRouter chat completion payload. */
|
|
147
|
-
function toOpenRouterPayload(req, config) {
|
|
148
|
-
const model = resolveOpenRouterModel(req.model, config.modelMap, {
|
|
149
|
-
apiId: req.apiId,
|
|
150
|
-
openRouterId: req.openRouterId,
|
|
151
|
-
});
|
|
152
|
-
const messages = buildMessages(req);
|
|
153
|
-
const payload = {
|
|
154
|
-
model,
|
|
155
|
-
stream: true,
|
|
156
|
-
messages,
|
|
157
|
-
temperature: req.temperature,
|
|
158
|
-
max_tokens: req.maxOutputTokens,
|
|
159
|
-
};
|
|
160
|
-
const effort = mapThinkingEffort(req.thinking);
|
|
161
|
-
payload.reasoning = { effort };
|
|
162
|
-
const responseFormat = resolveResponseFormat(req.structured);
|
|
163
|
-
if (responseFormat) {
|
|
164
|
-
payload.response_format = responseFormat;
|
|
165
|
-
}
|
|
166
|
-
const tools = wireTools(req.dynamicTools);
|
|
167
|
-
if (tools.length > 0) {
|
|
168
|
-
payload.tools = tools;
|
|
169
|
-
}
|
|
170
|
-
const resolved = resolveOpenRouterPlugins(req.builtins);
|
|
171
|
-
if (resolved.webSearch) {
|
|
172
|
-
payload.web_search_options = {};
|
|
173
|
-
}
|
|
174
|
-
if (resolved.plugins.length > 0) {
|
|
175
|
-
payload.plugins = resolved.plugins;
|
|
176
|
-
}
|
|
177
|
-
return payload;
|
|
178
|
-
}
|
|
179
|
-
function resolveOpenRouterPlugins(builtins) {
|
|
180
|
-
let webSearch = false;
|
|
181
|
-
const plugins = [];
|
|
182
|
-
for (const id of builtins) {
|
|
183
|
-
const pluginId = getTool(id)?.openRouterPlugin;
|
|
184
|
-
if (!pluginId)
|
|
185
|
-
continue;
|
|
186
|
-
if (pluginId === 'web') {
|
|
187
|
-
webSearch = true;
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
plugins.push({ id: pluginId });
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return { plugins, webSearch };
|
|
194
|
-
}
|
|
195
|
-
export { resolveOpenRouterModel, resolveOpenRouterPlugins, toOpenRouterPayload };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenRouter provider adapter powered by Vercel AI SDK Core.
|
|
3
|
-
*
|
|
4
|
-
* THEORUM keeps the public `ModelProvider` and `TurnEvent` contract; AI SDK
|
|
5
|
-
* owns the OpenRouter call, stream parsing, provider compatibility, and tool
|
|
6
|
-
* call normalization.
|
|
7
|
-
*
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
import type { ModelProvider } from '../kernel/types.js';
|
|
11
|
-
import { type OpenRouterConfig, resolveOpenRouterModel, toOpenRouterPayload } from './openrouter-payload.js';
|
|
12
|
-
/** Create a `ModelProvider` backed by OpenRouter through AI SDK Core. */
|
|
13
|
-
declare function createOpenRouterProvider(config?: OpenRouterConfig): ModelProvider;
|
|
14
|
-
export type { OpenRouterConfig };
|
|
15
|
-
export { createOpenRouterProvider, resolveOpenRouterModel, toOpenRouterPayload };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Google Interactions provider adapter.
|
|
3
|
-
*
|
|
4
|
-
* This adapter converts THEORUM's provider-neutral request into the Google
|
|
5
|
-
* Interactions wire format and streams normalized `TurnEvent` objects.
|
|
6
|
-
* Speech-role turns use `response_format: audio` + `speech_config` (same
|
|
7
|
-
* transport as chat/image).
|
|
8
|
-
*
|
|
9
|
-
* @module
|
|
10
|
-
*/
|
|
11
|
-
import type { ModelProvider } from '../kernel/types.js';
|
|
12
|
-
import { type GeminiTransport } from './keys.js';
|
|
13
|
-
/** Create a `ModelProvider` backed by Google Interactions streaming. */
|
|
14
|
-
declare function createInteractionsProvider(transport: GeminiTransport): ModelProvider;
|
|
15
|
-
export { createInteractionsProvider };
|
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Google Interactions provider adapter.
|
|
3
|
-
*
|
|
4
|
-
* This adapter converts THEORUM's provider-neutral request into the Google
|
|
5
|
-
* Interactions wire format and streams normalized `TurnEvent` objects.
|
|
6
|
-
* Speech-role turns use `response_format: audio` + `speech_config` (same
|
|
7
|
-
* transport as chat/image).
|
|
8
|
-
*
|
|
9
|
-
* @module
|
|
10
|
-
*/
|
|
11
|
-
import { isAbortError, TheorumError, toErrorEvent } from '../guardrails/error.js';
|
|
12
|
-
import { eventsFromComplete, eventsFromDelta, extractTokenEvent, groundingFromEvent, tryStructured, } from '../kernel/engine/delta.js';
|
|
13
|
-
import { asRecord } from '../kernel/engine/record.js';
|
|
14
|
-
import { exposeForTests } from './expose-for-tests.js';
|
|
15
|
-
import { tapFetch } from './google-tap.js';
|
|
16
|
-
import { toInteractionsBody } from './interactions.js';
|
|
17
|
-
import { fetchGemini } from './keys.js';
|
|
18
|
-
import { wrapPcmAsWav } from './pcm.js';
|
|
19
|
-
import { INTERACTIONS_URL, takeSsePayloads } from './sse.js';
|
|
20
|
-
const HTTP_OK = 200;
|
|
21
|
-
function base64ToBytes(data) {
|
|
22
|
-
const bin = atob(data);
|
|
23
|
-
const out = new Uint8Array(bin.length);
|
|
24
|
-
for (let i = 0; i < bin.length; i++)
|
|
25
|
-
out[i] = bin.charCodeAt(i);
|
|
26
|
-
return out;
|
|
27
|
-
}
|
|
28
|
-
function bytesToBase64(bytes) {
|
|
29
|
-
let bin = '';
|
|
30
|
-
for (let i = 0; i < bytes.length; i++)
|
|
31
|
-
bin += String.fromCharCode(bytes[i]);
|
|
32
|
-
return btoa(bin);
|
|
33
|
-
}
|
|
34
|
-
function isRawPcmMime(mime) {
|
|
35
|
-
const lower = mime.toLowerCase();
|
|
36
|
-
return lower === 'audio/pcm' || lower === 'audio/l16' || lower === 'audio/raw';
|
|
37
|
-
}
|
|
38
|
-
/** Google TTS returns raw PCM; wrap as WAV for hosts (matches OpenRouter pcm path). */
|
|
39
|
-
function normalizeSpeechMedia(event, speech) {
|
|
40
|
-
if (!(speech && event.type === 'media' && event.media)) {
|
|
41
|
-
return event;
|
|
42
|
-
}
|
|
43
|
-
const { mimeType, data } = event.media;
|
|
44
|
-
if (!isRawPcmMime(mimeType)) {
|
|
45
|
-
return event;
|
|
46
|
-
}
|
|
47
|
-
const wav = wrapPcmAsWav(base64ToBytes(data));
|
|
48
|
-
return {
|
|
49
|
-
type: 'media',
|
|
50
|
-
media: { mimeType: 'audio/wav', data: bytesToBase64(wav) },
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
async function* readSseRecords(res) {
|
|
54
|
-
if (!res.body) {
|
|
55
|
-
throw new TheorumError('empty Gemini stream');
|
|
56
|
-
}
|
|
57
|
-
const reader = res.body.getReader();
|
|
58
|
-
const decoder = new TextDecoder();
|
|
59
|
-
yield* pumpSse(reader, decoder, '', '');
|
|
60
|
-
}
|
|
61
|
-
async function* pumpSse(reader, decoder, buffer, pendingEvent) {
|
|
62
|
-
const { done, value } = await reader.read();
|
|
63
|
-
if (done) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const next = buffer + decoder.decode(value, { stream: true });
|
|
67
|
-
const taken = takeSsePayloads(next, pendingEvent);
|
|
68
|
-
for (const payload of taken.payloads) {
|
|
69
|
-
yield payload;
|
|
70
|
-
}
|
|
71
|
-
yield* pumpSse(reader, decoder, taken.rest, taken.pendingEvent);
|
|
72
|
-
}
|
|
73
|
-
function eventType(event) {
|
|
74
|
-
return String(event.event_type ?? event.type ?? '');
|
|
75
|
-
}
|
|
76
|
-
function isDeltaEvent(kind) {
|
|
77
|
-
return kind === 'content.delta' || kind === 'step.delta';
|
|
78
|
-
}
|
|
79
|
-
function isCompleteEvent(kind) {
|
|
80
|
-
return (kind === 'interaction.complete' ||
|
|
81
|
-
kind === 'interaction.completed' ||
|
|
82
|
-
kind.startsWith('interaction.'));
|
|
83
|
-
}
|
|
84
|
-
function foldDeltaPayload(event, acc) {
|
|
85
|
-
const deltaEvents = eventsFromDelta(event.delta);
|
|
86
|
-
for (const item of deltaEvents) {
|
|
87
|
-
if (item.type === 'text' && item.text) {
|
|
88
|
-
acc.text += item.text;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return deltaEvents;
|
|
92
|
-
}
|
|
93
|
-
function foldPayload(event, acc) {
|
|
94
|
-
const kind = eventType(event);
|
|
95
|
-
const events = [];
|
|
96
|
-
if (isDeltaEvent(kind)) {
|
|
97
|
-
events.push(...foldDeltaPayload(event, acc));
|
|
98
|
-
}
|
|
99
|
-
else if (isCompleteEvent(kind)) {
|
|
100
|
-
events.push(...eventsFromComplete(event, acc.text.length > 0));
|
|
101
|
-
}
|
|
102
|
-
// Also check if usage was included on the event directly
|
|
103
|
-
if (!events.some((e) => e.type === 'tokens')) {
|
|
104
|
-
const tokenEvent = extractTokenEvent(event);
|
|
105
|
-
if (tokenEvent) {
|
|
106
|
-
events.push(tokenEvent);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const groundingEvent = groundingFromEvent(event);
|
|
110
|
-
if (groundingEvent && !events.some((e) => e.type === 'grounding')) {
|
|
111
|
-
events.push(groundingEvent);
|
|
112
|
-
}
|
|
113
|
-
const evidenceEvent = evidenceFromGooglePayload(event);
|
|
114
|
-
if (evidenceEvent && !events.some((e) => e.type === 'evidence')) {
|
|
115
|
-
events.push(evidenceEvent);
|
|
116
|
-
}
|
|
117
|
-
return events;
|
|
118
|
-
}
|
|
119
|
-
/** Preserve raw Google Interactions tool payloads so hosts can inspect everything returned. */
|
|
120
|
-
function evidenceFromGooglePayload(event) {
|
|
121
|
-
for (const key of ['delta', 'step']) {
|
|
122
|
-
const payload = asRecord(event[key]);
|
|
123
|
-
if (!payload) {
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
const type = String(payload.type ?? '');
|
|
127
|
-
if (type === 'google_search_result' ||
|
|
128
|
-
type === 'google_maps' ||
|
|
129
|
-
type === 'google_maps_result' ||
|
|
130
|
-
type.startsWith('google_')) {
|
|
131
|
-
return {
|
|
132
|
-
type: 'evidence',
|
|
133
|
-
evidence: { provider: 'google', raw: payload },
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
function withTap(req, transport) {
|
|
140
|
-
return {
|
|
141
|
-
...transport,
|
|
142
|
-
fetch: tapFetch(req.tapGemini, transport.fetch),
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
async function* streamComplete(req, transport) {
|
|
146
|
-
if (!req.geminiBucket) {
|
|
147
|
-
yield toErrorEvent('missing Gemini vault bucket for Interactions');
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
const res = await fetchGemini(INTERACTIONS_URL, { method: 'POST', body: JSON.stringify(toInteractionsBody(req)), signal: req.signal }, req.geminiBucket, withTap(req, transport));
|
|
151
|
-
if (res.status !== HTTP_OK) {
|
|
152
|
-
const errorBody = await res.text().catch(() => '');
|
|
153
|
-
yield toErrorEvent(`Gemini HTTP ${String(res.status)}: ${errorBody}`);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
const acc = { text: '' };
|
|
157
|
-
const speech = Boolean(req.speech);
|
|
158
|
-
for await (const payload of readSseRecords(res)) {
|
|
159
|
-
req.tapGemini?.(payload);
|
|
160
|
-
const events = foldPayload(payload, acc);
|
|
161
|
-
for (const event of events) {
|
|
162
|
-
yield normalizeSpeechMedia(event, speech);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
if (req.structured && acc.text) {
|
|
166
|
-
const structured = tryStructured(acc.text);
|
|
167
|
-
if (structured) {
|
|
168
|
-
yield structured;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async function* streamGuarded(req, transport) {
|
|
173
|
-
try {
|
|
174
|
-
yield* streamComplete(req, transport);
|
|
175
|
-
}
|
|
176
|
-
catch (err) {
|
|
177
|
-
if (isAbortError(err)) {
|
|
178
|
-
throw err;
|
|
179
|
-
}
|
|
180
|
-
yield toErrorEvent(err);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
/** Create a `ModelProvider` backed by Google Interactions streaming. */
|
|
184
|
-
function createInteractionsProvider(transport) {
|
|
185
|
-
return {
|
|
186
|
-
complete: (req) => streamGuarded(req, transport),
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
export { createInteractionsProvider };
|
|
190
|
-
exposeForTests('provider', {
|
|
191
|
-
base64ToBytes,
|
|
192
|
-
bytesToBase64,
|
|
193
|
-
isRawPcmMime,
|
|
194
|
-
normalizeSpeechMedia,
|
|
195
|
-
eventType,
|
|
196
|
-
isDeltaEvent,
|
|
197
|
-
isCompleteEvent,
|
|
198
|
-
foldDeltaPayload,
|
|
199
|
-
foldPayload,
|
|
200
|
-
evidenceFromGooglePayload,
|
|
201
|
-
withTap,
|
|
202
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI-compatible `/audio/speech` transport (internal).
|
|
3
|
-
*
|
|
4
|
-
* Hosts use `createProvider(profile, { openRouter })` — this module is selected
|
|
5
|
-
* when the profile is an openAi speech role. Not a separate public door.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
import type { ModelProvider, ProviderCompleteRequest, TurnEvent } from '../kernel/types.js';
|
|
10
|
-
/** Credentials for the openAi speech path (same shape as OpenRouter chat config + voice). */
|
|
11
|
-
export interface SpeechProviderConfig {
|
|
12
|
-
apiKey?: string;
|
|
13
|
-
/** Fallback TTS voice when the profile does not pin `outputs.speech.voice`. */
|
|
14
|
-
voice?: string;
|
|
15
|
-
baseUrl?: string;
|
|
16
|
-
siteUrl?: string;
|
|
17
|
-
siteName?: string;
|
|
18
|
-
fetch?: typeof fetch;
|
|
19
|
-
}
|
|
20
|
-
declare function streamSpeech(req: ProviderCompleteRequest, config?: SpeechProviderConfig): AsyncGenerator<TurnEvent>;
|
|
21
|
-
/** Internal ModelProvider for openAi speech roles. */
|
|
22
|
-
declare function createSpeechProvider(config?: SpeechProviderConfig): ModelProvider;
|
|
23
|
-
export { createSpeechProvider, streamSpeech };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
declare const INTERACTIONS_URL = "https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse";
|
|
2
|
-
declare function takeSsePayloads(buffer: string, pendingEvent?: string): {
|
|
3
|
-
rest: string;
|
|
4
|
-
payloads: Record<string, unknown>[];
|
|
5
|
-
pendingEvent: string;
|
|
6
|
-
};
|
|
7
|
-
export { INTERACTIONS_URL, takeSsePayloads };
|
package/esm/src/providers/sse.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { exposeForTests } from './expose-for-tests.js';
|
|
2
|
-
const INTERACTIONS_URL = 'https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse';
|
|
3
|
-
const DATA_PREFIX = 'data: ';
|
|
4
|
-
const EVENT_PREFIX = 'event: ';
|
|
5
|
-
const DONE = '[DONE]';
|
|
6
|
-
function asObject(parsed) {
|
|
7
|
-
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
8
|
-
return parsed;
|
|
9
|
-
}
|
|
10
|
-
return undefined;
|
|
11
|
-
}
|
|
12
|
-
function dataRecord(raw, sseEvent) {
|
|
13
|
-
const data = raw.slice(DATA_PREFIX.length).trim();
|
|
14
|
-
const row = {};
|
|
15
|
-
if (sseEvent) {
|
|
16
|
-
row.sseEvent = sseEvent;
|
|
17
|
-
}
|
|
18
|
-
if (!data || data === DONE) {
|
|
19
|
-
row.eventType = 'sse_done';
|
|
20
|
-
return row;
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
const parsed = JSON.parse(data);
|
|
24
|
-
const obj = asObject(parsed);
|
|
25
|
-
if (obj) {
|
|
26
|
-
return { ...obj, ...row };
|
|
27
|
-
}
|
|
28
|
-
row.eventType = 'sse_unparsed';
|
|
29
|
-
row.data = parsed;
|
|
30
|
-
return row;
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
row.eventType = 'sse_unparsed';
|
|
34
|
-
row.data = data;
|
|
35
|
-
return row;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function takeSsePayloads(buffer, pendingEvent = '') {
|
|
39
|
-
const payloads = [];
|
|
40
|
-
const chunks = buffer.split('\n');
|
|
41
|
-
const rest = chunks.pop() ?? '';
|
|
42
|
-
let sseEvent = pendingEvent;
|
|
43
|
-
for (const line of chunks) {
|
|
44
|
-
if (line.startsWith(EVENT_PREFIX)) {
|
|
45
|
-
sseEvent = line.slice(EVENT_PREFIX.length).trim();
|
|
46
|
-
}
|
|
47
|
-
else if (line.startsWith(DATA_PREFIX)) {
|
|
48
|
-
payloads.push(dataRecord(line, sseEvent));
|
|
49
|
-
sseEvent = '';
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return { rest, payloads, pendingEvent: sseEvent };
|
|
53
|
-
}
|
|
54
|
-
export { INTERACTIONS_URL, takeSsePayloads };
|
|
55
|
-
exposeForTests('sse', { asObject, dataRecord, takeSsePayloads });
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Structured-output streaming helpers and turn-stop classification.
|
|
3
|
-
*
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
import "../../_dnt.polyfills.js";
|
|
7
|
-
export type { ProfileResumeSpec, TurnContinueFrom, TurnStop, TurnStopKind, } from '../kernel/stop.js';
|
|
8
|
-
export { AUTO_CONTINUE_DELAY_MS, CONTINUE_INSTRUCTION, DEFAULT_AUTO_CONTINUE, GenerationStopError, isGenerationStopError, isResumeableStop, isUserCancelledStop, shouldAutoContinue, turnStopFromClientStreamEnd, turnStopFromInteractionStatus, turnStopFromOpenRouter, } from '../kernel/stop.js';
|
|
9
|
-
export { readStreamingJsonStringField } from './readStreamingJsonStringField.js';
|
package/esm/src/streaming/mod.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Structured-output streaming helpers and turn-stop classification.
|
|
3
|
-
*
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
import "../../_dnt.polyfills.js";
|
|
7
|
-
export { AUTO_CONTINUE_DELAY_MS, CONTINUE_INSTRUCTION, DEFAULT_AUTO_CONTINUE, GenerationStopError, isGenerationStopError, isResumeableStop, isUserCancelledStop, shouldAutoContinue, turnStopFromClientStreamEnd, turnStopFromInteractionStatus, turnStopFromOpenRouter, } from '../kernel/stop.js';
|
|
8
|
-
export { readStreamingJsonStringField } from './readStreamingJsonStringField.js';
|
|
File without changes
|
|
File without changes
|