theorum 0.1.14 → 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.
Files changed (229) hide show
  1. package/README.md +178 -89
  2. package/esm/mod.d.ts +21 -11
  3. package/esm/mod.js +15 -8
  4. package/esm/src/cli/commands/bench.js +6 -3
  5. package/esm/src/cli/commands/fuzz-canary.d.ts +13 -0
  6. package/esm/src/cli/commands/fuzz-canary.js +195 -0
  7. package/esm/src/cli/commands/fuzz-guardrails.d.ts +3 -5
  8. package/esm/src/cli/commands/fuzz-guardrails.js +4 -581
  9. package/esm/src/cli/commands/profile.js +25 -14
  10. package/esm/src/cli/commands/run.d.ts +3 -0
  11. package/esm/src/cli/commands/run.js +22 -30
  12. package/esm/src/cli/commands/test.d.ts +10 -1
  13. package/esm/src/cli/commands/test.js +31 -31
  14. package/esm/src/cli/event-log.d.ts +19 -0
  15. package/esm/src/cli/event-log.js +147 -0
  16. package/esm/src/cli/index.js +45 -11
  17. package/esm/src/cli/matrix/synthesizer.d.ts +7 -9
  18. package/esm/src/cli/matrix/synthesizer.js +32 -106
  19. package/esm/src/guardrails/canary-gate.d.ts +20 -0
  20. package/esm/src/guardrails/canary-gate.js +31 -0
  21. package/esm/src/guardrails/canary.d.ts +26 -0
  22. package/esm/src/guardrails/canary.js +139 -0
  23. package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +16 -0
  24. package/esm/src/guardrails/corpus/canary-egress-attacks.js +151 -0
  25. package/esm/src/guardrails/corpus/fuzz-inbound.d.ts +11 -0
  26. package/esm/src/guardrails/corpus/fuzz-inbound.js +216 -0
  27. package/esm/src/guardrails/corpus/inbound-payloads.d.ts +10 -0
  28. package/esm/src/guardrails/corpus/inbound-payloads.js +124 -0
  29. package/esm/src/guardrails/corpus/live-attacks.d.ts +20 -0
  30. package/esm/src/guardrails/corpus/live-attacks.js +232 -0
  31. package/esm/src/guardrails/corpus/mod.d.ts +13 -0
  32. package/esm/src/guardrails/corpus/mod.js +11 -0
  33. package/esm/src/guardrails/corpus/secrets.d.ts +16 -0
  34. package/esm/src/guardrails/corpus/secrets.js +16 -0
  35. package/esm/src/guardrails/corpus/strings.d.ts +27 -0
  36. package/esm/src/guardrails/corpus/strings.js +33 -0
  37. package/esm/src/guardrails/corpus/types.d.ts +37 -0
  38. package/esm/src/guardrails/corpus/types.js +6 -0
  39. package/esm/src/guardrails/egress.d.ts +9 -0
  40. package/esm/src/guardrails/egress.js +36 -0
  41. package/esm/src/guardrails/error.js +6 -4
  42. package/esm/src/guardrails/injection.js +28 -5
  43. package/esm/src/guardrails/live-outbound-gate.d.ts +39 -0
  44. package/esm/src/guardrails/live-outbound-gate.js +180 -0
  45. package/esm/src/guardrails/mod.d.ts +13 -4
  46. package/esm/src/guardrails/mod.js +10 -4
  47. package/esm/src/guardrails/quota.js +1 -1
  48. package/esm/src/guardrails/sanitize.d.ts +11 -15
  49. package/esm/src/guardrails/sanitize.js +40 -49
  50. package/esm/src/guardrails/sensitive.js +2 -1
  51. package/esm/src/guardrails/testing.d.ts +10 -0
  52. package/esm/src/guardrails/testing.js +9 -0
  53. package/esm/src/host/client-turn.d.ts +19 -0
  54. package/esm/src/host/client-turn.js +32 -0
  55. package/esm/src/host/mod.d.ts +5 -3
  56. package/esm/src/host/mod.js +4 -3
  57. package/esm/src/interface/blocks.d.ts +20 -0
  58. package/esm/src/interface/blocks.js +180 -0
  59. package/esm/src/interface/from-profile.d.ts +14 -0
  60. package/esm/src/interface/from-profile.js +104 -0
  61. package/esm/src/interface/inputs.d.ts +8 -0
  62. package/esm/src/interface/inputs.js +127 -0
  63. package/esm/src/interface/mod.d.ts +10 -0
  64. package/esm/src/interface/mod.js +9 -0
  65. package/esm/src/interface/types.d.ts +153 -0
  66. package/esm/src/interface/types.js +13 -0
  67. package/esm/src/kernel/engine/delta.d.ts +24 -2
  68. package/esm/src/kernel/engine/delta.js +519 -29
  69. package/esm/src/kernel/engine/live-inbound.d.ts +9 -0
  70. package/esm/src/kernel/engine/live-inbound.js +17 -0
  71. package/esm/src/kernel/engine/repair.js +1 -1
  72. package/esm/src/kernel/engine/runner/gates.d.ts +1 -1
  73. package/esm/src/kernel/engine/runner/gates.js +7 -7
  74. package/esm/src/kernel/engine/runner/mod.js +13 -13
  75. package/esm/src/kernel/engine/runner/state.d.ts +7 -0
  76. package/esm/src/kernel/engine/runner/steps.d.ts +1 -1
  77. package/esm/src/kernel/engine/runner/steps.js +171 -37
  78. package/esm/src/kernel/engine/runner/stream.d.ts +1 -2
  79. package/esm/src/kernel/engine/runner/stream.js +66 -29
  80. package/esm/src/kernel/engine/session/mod.d.ts +23 -0
  81. package/esm/src/kernel/engine/session/mod.js +205 -0
  82. package/esm/src/kernel/mod.d.ts +9 -10
  83. package/esm/src/kernel/mod.js +6 -8
  84. package/esm/src/kernel/registry/attachments.d.ts +4 -2
  85. package/esm/src/kernel/registry/attachments.js +6 -3
  86. package/esm/src/kernel/registry/catalog.d.ts +3 -16
  87. package/esm/src/kernel/registry/catalog.js +5 -81
  88. package/esm/src/kernel/registry/ingress.d.ts +9 -4
  89. package/esm/src/kernel/registry/ingress.js +73 -63
  90. package/esm/src/kernel/registry/profiles.d.ts +35 -12
  91. package/esm/src/kernel/registry/profiles.js +159 -66
  92. package/esm/src/kernel/registry/provider-request.js +11 -7
  93. package/esm/src/kernel/registry/resolve.d.ts +5 -6
  94. package/esm/src/kernel/registry/resolve.js +100 -70
  95. package/esm/src/kernel/registry/vault.d.ts +12 -7
  96. package/esm/src/kernel/registry/vault.js +30 -8
  97. package/esm/src/kernel/schema.d.ts +145 -0
  98. package/esm/src/kernel/schema.js +462 -0
  99. package/esm/src/kernel/stop.d.ts +15 -10
  100. package/esm/src/kernel/stop.js +5 -5
  101. package/esm/src/kernel/tools/execute.d.ts +45 -0
  102. package/esm/src/kernel/tools/execute.js +363 -0
  103. package/esm/src/kernel/tools/harness.d.ts +8 -0
  104. package/esm/src/kernel/tools/harness.js +44 -0
  105. package/esm/src/kernel/tools/invoke.d.ts +10 -0
  106. package/esm/src/kernel/tools/invoke.js +83 -0
  107. package/esm/src/kernel/tools/mod.d.ts +11 -0
  108. package/esm/src/kernel/tools/mod.js +10 -0
  109. package/esm/src/kernel/tools/project.d.ts +12 -0
  110. package/esm/src/kernel/tools/project.js +34 -0
  111. package/esm/src/kernel/tools/registry.d.ts +23 -0
  112. package/esm/src/kernel/tools/registry.js +71 -0
  113. package/esm/src/kernel/tools/resolve.d.ts +29 -0
  114. package/esm/src/kernel/tools/resolve.js +262 -0
  115. package/esm/src/kernel/tools/schema.d.ts +13 -0
  116. package/esm/src/kernel/tools/schema.js +159 -0
  117. package/esm/src/kernel/tools/types.d.ts +216 -0
  118. package/esm/src/kernel/tools/types.js +9 -0
  119. package/esm/src/kernel/types.d.ts +338 -193
  120. package/esm/src/observability/trace-attach.d.ts +6 -4
  121. package/esm/src/observability/trace-attach.js +39 -25
  122. package/esm/src/observability/trace-record.d.ts +11 -9
  123. package/esm/src/observability/trace-record.js +36 -23
  124. package/esm/src/observability/trace-usage.d.ts +10 -3
  125. package/esm/src/observability/trace-usage.js +70 -17
  126. package/esm/src/observability/trace.d.ts +9 -1
  127. package/esm/src/observability/trace.js +11 -3
  128. package/esm/src/presets/google/speech-voices.d.ts +11 -0
  129. package/esm/src/presets/google/speech-voices.js +41 -0
  130. package/esm/src/presets/google.d.ts +36 -24
  131. package/esm/src/presets/google.js +50 -63
  132. package/esm/src/presets/mod.d.ts +2 -2
  133. package/esm/src/presets/mod.js +1 -1
  134. package/esm/src/providers/create-provider.d.ts +14 -14
  135. package/esm/src/providers/create-provider.js +55 -23
  136. package/esm/src/providers/google/interactions/framing.d.ts +15 -0
  137. package/esm/src/providers/google/interactions/framing.js +208 -0
  138. package/esm/src/providers/google/interactions/mod.d.ts +7 -0
  139. package/esm/src/providers/google/interactions/mod.js +7 -0
  140. package/esm/src/providers/google/interactions/stream.d.ts +83 -0
  141. package/esm/src/providers/google/interactions/stream.js +588 -0
  142. package/esm/src/providers/google/keys.d.ts +26 -0
  143. package/esm/src/providers/{keys.js → google/keys.js} +19 -31
  144. package/esm/src/providers/google/live/framing.d.ts +46 -0
  145. package/esm/src/providers/google/live/framing.js +515 -0
  146. package/esm/src/providers/google/live/openapi-schema.d.ts +6 -0
  147. package/esm/src/providers/google/live/openapi-schema.js +45 -0
  148. package/esm/src/providers/google/live/session.d.ts +25 -0
  149. package/esm/src/providers/google/live/session.js +134 -0
  150. package/esm/src/providers/google/live/stream.d.ts +36 -0
  151. package/esm/src/providers/google/live/stream.js +201 -0
  152. package/esm/src/providers/google/urls.d.ts +6 -0
  153. package/esm/src/providers/google/urls.js +6 -0
  154. package/esm/src/providers/local/local.d.ts +30 -0
  155. package/esm/src/providers/{local.js → local/local.js} +66 -126
  156. package/esm/src/providers/local/mod.d.ts +9 -0
  157. package/esm/src/providers/local/mod.js +9 -0
  158. package/esm/src/providers/mod.d.ts +6 -3
  159. package/esm/src/providers/mod.js +3 -1
  160. package/esm/src/providers/openrouter/chat.d.ts +87 -0
  161. package/esm/src/providers/{openrouter.js → openrouter/chat.js} +70 -229
  162. package/esm/src/providers/openrouter/image.d.ts +34 -0
  163. package/esm/src/providers/openrouter/image.js +286 -0
  164. package/esm/src/providers/openrouter/openai/chat-payload.d.ts +24 -0
  165. package/esm/src/providers/openrouter/openai/chat-payload.js +60 -0
  166. package/esm/src/providers/openrouter/openai/compat.d.ts +47 -0
  167. package/esm/src/providers/openrouter/openai/compat.js +194 -0
  168. package/esm/src/providers/openrouter/openai/image-payload.d.ts +18 -0
  169. package/esm/src/providers/openrouter/openai/image-payload.js +84 -0
  170. package/esm/src/providers/openrouter/openai/sdk-messages.d.ts +22 -0
  171. package/esm/src/providers/openrouter/openai/sdk-messages.js +95 -0
  172. package/esm/src/providers/openrouter/resolve-api-key.d.ts +9 -0
  173. package/esm/src/providers/openrouter/resolve-api-key.js +24 -0
  174. package/esm/src/providers/openrouter/speech.d.ts +23 -0
  175. package/esm/src/providers/{speech.js → openrouter/speech.js} +32 -55
  176. package/esm/src/providers/probe.d.ts +1 -0
  177. package/esm/src/providers/probe.js +22 -0
  178. package/esm/src/providers/shared/pcm.d.ts +12 -0
  179. package/esm/src/providers/{pcm.js → shared/pcm.js} +16 -3
  180. package/esm/src/providers/shared/sse.d.ts +18 -0
  181. package/esm/src/providers/shared/sse.js +87 -0
  182. package/esm/src/providers/shared/tool-args.d.ts +17 -0
  183. package/esm/src/providers/shared/tool-args.js +45 -0
  184. package/esm/src/providers/shared/upstream-tap.d.ts +5 -0
  185. package/esm/src/providers/{google-tap.js → shared/upstream-tap.js} +4 -7
  186. package/esm/src/providers/shared/upstream-tape.d.ts +6 -0
  187. package/esm/src/providers/{gemini-tape.js → shared/upstream-tape.js} +12 -22
  188. package/esm/src/providers/types.d.ts +27 -0
  189. package/esm/src/providers/types.js +1 -0
  190. package/package.json +11 -7
  191. package/docs/cli.md +0 -97
  192. package/docs/guardrails.md +0 -178
  193. package/docs/host.md +0 -97
  194. package/docs/kernel.md +0 -404
  195. package/docs/observability.md +0 -105
  196. package/docs/openrouter.md +0 -125
  197. package/docs/presets-google.md +0 -91
  198. package/docs/presets.md +0 -88
  199. package/docs/providers.md +0 -201
  200. package/docs/streaming.md +0 -96
  201. package/esm/src/kernel/engine/boundary.d.ts +0 -10
  202. package/esm/src/kernel/engine/boundary.js +0 -55
  203. package/esm/src/kernel/engine/runner/tools.d.ts +0 -13
  204. package/esm/src/kernel/engine/runner/tools.js +0 -198
  205. package/esm/src/kernel/registry/tools.d.ts +0 -12
  206. package/esm/src/kernel/registry/tools.js +0 -36
  207. package/esm/src/providers/expose-for-tests.d.ts +0 -1
  208. package/esm/src/providers/expose-for-tests.js +0 -25
  209. package/esm/src/providers/gemini-tape.d.ts +0 -2
  210. package/esm/src/providers/google-tap.d.ts +0 -3
  211. package/esm/src/providers/interactions.d.ts +0 -5
  212. package/esm/src/providers/interactions.js +0 -169
  213. package/esm/src/providers/keys.d.ts +0 -19
  214. package/esm/src/providers/local.d.ts +0 -29
  215. package/esm/src/providers/openrouter-mod.d.ts +0 -13
  216. package/esm/src/providers/openrouter-mod.js +0 -12
  217. package/esm/src/providers/openrouter-payload.d.ts +0 -39
  218. package/esm/src/providers/openrouter-payload.js +0 -195
  219. package/esm/src/providers/openrouter.d.ts +0 -15
  220. package/esm/src/providers/pcm.d.ts +0 -7
  221. package/esm/src/providers/provider.d.ts +0 -15
  222. package/esm/src/providers/provider.js +0 -176
  223. package/esm/src/providers/speech.d.ts +0 -23
  224. package/esm/src/providers/sse.d.ts +0 -7
  225. package/esm/src/providers/sse.js +0 -55
  226. package/esm/src/streaming/mod.d.ts +0 -9
  227. package/esm/src/streaming/mod.js +0 -8
  228. /package/esm/src/{streaming → host}/readStreamingJsonStringField.d.ts +0 -0
  229. /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,7 +0,0 @@
1
- /**
2
- * PCM → WAV helpers shared by speech transports.
3
- *
4
- * @module
5
- */
6
- /** Wrap raw PCM bytes in a RIFF/WAVE container. */
7
- export declare function wrapPcmAsWav(pcm: Uint8Array, sampleRate?: number): Uint8Array;
@@ -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,176 +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 { exposeForTests } from './expose-for-tests.js';
14
- import { tapFetch } from './google-tap.js';
15
- import { toInteractionsBody } from './interactions.js';
16
- import { fetchGemini } from './keys.js';
17
- import { wrapPcmAsWav } from './pcm.js';
18
- import { INTERACTIONS_URL, takeSsePayloads } from './sse.js';
19
- const HTTP_OK = 200;
20
- function base64ToBytes(data) {
21
- const bin = atob(data);
22
- const out = new Uint8Array(bin.length);
23
- for (let i = 0; i < bin.length; i++)
24
- out[i] = bin.charCodeAt(i);
25
- return out;
26
- }
27
- function bytesToBase64(bytes) {
28
- let bin = '';
29
- for (let i = 0; i < bytes.length; i++)
30
- bin += String.fromCharCode(bytes[i]);
31
- return btoa(bin);
32
- }
33
- function isRawPcmMime(mime) {
34
- const lower = mime.toLowerCase();
35
- return lower === 'audio/pcm' || lower === 'audio/l16' || lower === 'audio/raw';
36
- }
37
- /** Google TTS returns raw PCM; wrap as WAV for hosts (matches OpenRouter pcm path). */
38
- function normalizeSpeechMedia(event, speech) {
39
- if (!(speech && event.type === 'media' && event.media)) {
40
- return event;
41
- }
42
- const { mimeType, data } = event.media;
43
- if (!isRawPcmMime(mimeType)) {
44
- return event;
45
- }
46
- const wav = wrapPcmAsWav(base64ToBytes(data));
47
- return {
48
- type: 'media',
49
- media: { mimeType: 'audio/wav', data: bytesToBase64(wav) },
50
- };
51
- }
52
- async function* readSseRecords(res) {
53
- if (!res.body) {
54
- throw new TheorumError('empty Gemini stream');
55
- }
56
- const reader = res.body.getReader();
57
- const decoder = new TextDecoder();
58
- yield* pumpSse(reader, decoder, '', '');
59
- }
60
- async function* pumpSse(reader, decoder, buffer, pendingEvent) {
61
- const { done, value } = await reader.read();
62
- if (done) {
63
- return;
64
- }
65
- const next = buffer + decoder.decode(value, { stream: true });
66
- const taken = takeSsePayloads(next, pendingEvent);
67
- for (const payload of taken.payloads) {
68
- yield payload;
69
- }
70
- yield* pumpSse(reader, decoder, taken.rest, taken.pendingEvent);
71
- }
72
- function eventType(event) {
73
- return String(event.event_type ?? event.type ?? '');
74
- }
75
- function isDeltaEvent(kind) {
76
- return kind === 'content.delta' || kind === 'step.delta';
77
- }
78
- function isCompleteEvent(kind) {
79
- return (kind === 'interaction.complete' ||
80
- kind === 'interaction.completed' ||
81
- kind.startsWith('interaction.'));
82
- }
83
- function foldDeltaPayload(event, acc) {
84
- const deltaEvents = eventsFromDelta(event.delta);
85
- for (const item of deltaEvents) {
86
- if (item.type === 'text' && item.text) {
87
- acc.text += item.text;
88
- }
89
- }
90
- return deltaEvents;
91
- }
92
- function foldPayload(event, acc) {
93
- const kind = eventType(event);
94
- const events = [];
95
- if (isDeltaEvent(kind)) {
96
- events.push(...foldDeltaPayload(event, acc));
97
- }
98
- else if (isCompleteEvent(kind)) {
99
- events.push(...eventsFromComplete(event, acc.text.length > 0));
100
- }
101
- // Also check if usage was included on the event directly
102
- if (!events.some((e) => e.type === 'tokens')) {
103
- const tokenEvent = extractTokenEvent(event);
104
- if (tokenEvent) {
105
- events.push(tokenEvent);
106
- }
107
- }
108
- const groundingEvent = groundingFromEvent(event);
109
- if (groundingEvent && !events.some((e) => e.type === 'grounding')) {
110
- events.push(groundingEvent);
111
- }
112
- return events;
113
- }
114
- function withTap(req, transport) {
115
- return {
116
- ...transport,
117
- fetch: tapFetch(req.tapGemini, transport.fetch),
118
- };
119
- }
120
- async function* streamComplete(req, transport) {
121
- if (!req.geminiBucket) {
122
- yield toErrorEvent('missing Gemini vault bucket for Interactions');
123
- return;
124
- }
125
- const res = await fetchGemini(INTERACTIONS_URL, { method: 'POST', body: JSON.stringify(toInteractionsBody(req)), signal: req.signal }, req.geminiBucket, withTap(req, transport));
126
- if (res.status !== HTTP_OK) {
127
- const errorBody = await res.text().catch(() => '');
128
- yield toErrorEvent(`Gemini HTTP ${String(res.status)}: ${errorBody}`);
129
- return;
130
- }
131
- const acc = { text: '' };
132
- const speech = Boolean(req.speech);
133
- for await (const payload of readSseRecords(res)) {
134
- req.tapGemini?.(payload);
135
- const events = foldPayload(payload, acc);
136
- for (const event of events) {
137
- yield normalizeSpeechMedia(event, speech);
138
- }
139
- }
140
- if (req.structured && acc.text) {
141
- const structured = tryStructured(acc.text);
142
- if (structured) {
143
- yield structured;
144
- }
145
- }
146
- }
147
- async function* streamGuarded(req, transport) {
148
- try {
149
- yield* streamComplete(req, transport);
150
- }
151
- catch (err) {
152
- if (isAbortError(err)) {
153
- throw err;
154
- }
155
- yield toErrorEvent(err);
156
- }
157
- }
158
- /** Create a `ModelProvider` backed by Google Interactions streaming. */
159
- function createInteractionsProvider(transport) {
160
- return {
161
- complete: (req) => streamGuarded(req, transport),
162
- };
163
- }
164
- export { createInteractionsProvider };
165
- exposeForTests('provider', {
166
- base64ToBytes,
167
- bytesToBase64,
168
- isRawPcmMime,
169
- normalizeSpeechMedia,
170
- eventType,
171
- isDeltaEvent,
172
- isCompleteEvent,
173
- foldDeltaPayload,
174
- foldPayload,
175
- withTap,
176
- });
@@ -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 };
@@ -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';
@@ -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';