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
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Google transport credentials, quota overflow, and fetch retries.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Hosts supply a provider-neutral `KeyVault` via `GeminiTransport`.
|
|
5
5
|
* THEORUM does not read environment variables for these keys.
|
|
6
6
|
*
|
|
7
7
|
* @module
|
|
8
8
|
*/
|
|
9
|
-
import { isAbortError, TheorumError, UPSTREAM_FAILED } from '
|
|
10
|
-
import { exposeForTests } from './expose-for-tests.js';
|
|
9
|
+
import { isAbortError, TheorumError, UPSTREAM_FAILED } from '../../guardrails/error.js';
|
|
11
10
|
const ATTEMPTS = 3;
|
|
12
11
|
const LAST_ATTEMPT = ATTEMPTS - 1;
|
|
13
12
|
const BACKOFF_FIRST_MS = 1000;
|
|
@@ -22,16 +21,16 @@ const HTTP_UNAVAILABLE = 503;
|
|
|
22
21
|
const HTTP_GATEWAY_TIMEOUT = 504;
|
|
23
22
|
const QUOTA_RE = /quota/i;
|
|
24
23
|
const TRANSIENT_THROWN_RE = /name resolution|dns|econnreset|econnrefused|etimedout|network|fetch failed|temporarily unavailable|socket|503|502|504/i;
|
|
25
|
-
function waitDefault(ms) {
|
|
24
|
+
export function waitDefault(ms) {
|
|
26
25
|
return new Promise((resolve) => {
|
|
27
26
|
setTimeout(resolve, ms);
|
|
28
27
|
});
|
|
29
28
|
}
|
|
30
|
-
function isQuota(err) {
|
|
29
|
+
export function isQuota(err) {
|
|
31
30
|
const s = String(err);
|
|
32
31
|
return s.includes(String(HTTP_QUOTA)) || s.includes('RESOURCE_EXHAUSTED') || QUOTA_RE.test(s);
|
|
33
32
|
}
|
|
34
|
-
function isTransientHttp(status) {
|
|
33
|
+
export function isTransientHttp(status) {
|
|
35
34
|
return (status === HTTP_TIMEOUT ||
|
|
36
35
|
status === HTTP_QUOTA ||
|
|
37
36
|
status === HTTP_SERVER ||
|
|
@@ -39,20 +38,20 @@ function isTransientHttp(status) {
|
|
|
39
38
|
status === HTTP_UNAVAILABLE ||
|
|
40
39
|
status === HTTP_GATEWAY_TIMEOUT);
|
|
41
40
|
}
|
|
42
|
-
function isTransientThrown(err) {
|
|
41
|
+
export function isTransientThrown(err) {
|
|
43
42
|
if (isAbortError(err)) {
|
|
44
43
|
return false;
|
|
45
44
|
}
|
|
46
45
|
return TRANSIENT_THROWN_RE.test(String(err));
|
|
47
46
|
}
|
|
48
|
-
function requireKey(vault,
|
|
49
|
-
const key = vault[
|
|
47
|
+
export function requireKey(vault, slot) {
|
|
48
|
+
const key = vault[slot];
|
|
50
49
|
if (!key) {
|
|
51
50
|
throw new TheorumError(UPSTREAM_FAILED);
|
|
52
51
|
}
|
|
53
52
|
return key;
|
|
54
53
|
}
|
|
55
|
-
function backoffMs(attempt) {
|
|
54
|
+
export function backoffMs(attempt) {
|
|
56
55
|
return BACKOFF_MS[attempt] ?? BACKOFF_SECOND_MS;
|
|
57
56
|
}
|
|
58
57
|
async function runWithBackoff(apiKey, run, wait, attempt) {
|
|
@@ -69,8 +68,8 @@ async function runWithBackoff(apiKey, run, wait, attempt) {
|
|
|
69
68
|
return runWithBackoff(apiKey, run, wait, attempt + 1);
|
|
70
69
|
}
|
|
71
70
|
}
|
|
72
|
-
function canOverflow(
|
|
73
|
-
if (
|
|
71
|
+
export function canOverflow(slot, vault, primary) {
|
|
72
|
+
if (slot === 'paid') {
|
|
74
73
|
return undefined;
|
|
75
74
|
}
|
|
76
75
|
const { paid } = vault;
|
|
@@ -79,21 +78,21 @@ function canOverflow(bucket, vault, primary) {
|
|
|
79
78
|
}
|
|
80
79
|
return paid;
|
|
81
80
|
}
|
|
82
|
-
async function withGeminiKey(
|
|
81
|
+
export async function withGeminiKey(slot, run, transport) {
|
|
83
82
|
const wait = transport.wait ?? waitDefault;
|
|
84
|
-
const primary = requireKey(transport.vault,
|
|
83
|
+
const primary = requireKey(transport.vault, slot);
|
|
85
84
|
try {
|
|
86
85
|
return await runWithBackoff(primary, run, wait, 0);
|
|
87
86
|
}
|
|
88
87
|
catch (err) {
|
|
89
|
-
const paid = canOverflow(
|
|
88
|
+
const paid = canOverflow(slot, transport.vault, primary);
|
|
90
89
|
if (!(isQuota(err) && paid)) {
|
|
91
90
|
throw err;
|
|
92
91
|
}
|
|
93
92
|
return await runWithBackoff(paid, run, wait, 0);
|
|
94
93
|
}
|
|
95
94
|
}
|
|
96
|
-
function withApiKey(init, apiKey) {
|
|
95
|
+
export function withApiKey(init, apiKey) {
|
|
97
96
|
const headers = new Headers(init.headers);
|
|
98
97
|
headers.set('x-goog-api-key', apiKey);
|
|
99
98
|
if (!headers.has('Content-Type') && (init.method || 'GET').toUpperCase() !== 'GET') {
|
|
@@ -125,26 +124,15 @@ async function fetchWithBackoff(args) {
|
|
|
125
124
|
return fetchWithBackoff({ ...args, attempt: args.attempt + 1 });
|
|
126
125
|
}
|
|
127
126
|
}
|
|
128
|
-
async function fetchGemini(url, init,
|
|
127
|
+
export async function fetchGemini(url, init, slot, transport) {
|
|
129
128
|
const parsed = new URL(url);
|
|
130
129
|
parsed.searchParams.delete('key');
|
|
131
130
|
const href = parsed.toString();
|
|
132
|
-
const primary = requireKey(transport.vault,
|
|
131
|
+
const primary = requireKey(transport.vault, slot);
|
|
133
132
|
let last = await fetchWithBackoff({ href, init, apiKey: primary, transport, attempt: 0 });
|
|
134
|
-
const paid = canOverflow(
|
|
133
|
+
const paid = canOverflow(slot, transport.vault, primary);
|
|
135
134
|
if (last.status === HTTP_QUOTA && paid) {
|
|
136
135
|
last = await fetchWithBackoff({ href, init, apiKey: paid, transport, attempt: 0 });
|
|
137
136
|
}
|
|
138
137
|
return last;
|
|
139
138
|
}
|
|
140
|
-
export { fetchGemini, withGeminiKey };
|
|
141
|
-
exposeForTests('keys', {
|
|
142
|
-
waitDefault,
|
|
143
|
-
isQuota,
|
|
144
|
-
isTransientHttp,
|
|
145
|
-
isTransientThrown,
|
|
146
|
-
requireKey,
|
|
147
|
-
backoffMs,
|
|
148
|
-
canOverflow,
|
|
149
|
-
withApiKey,
|
|
150
|
-
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure protocol framing & serialization for Google Gemini Live WebSocket API (`BidiGenerateContent`).
|
|
3
|
+
*
|
|
4
|
+
* All functions are pure data transformations with no network I/O.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { InteractionPart, ProviderCompleteRequest, TurnEvent, TurnHistoryMessage, TurnTokens, WireFunctionTool } from '../../../kernel/types.js';
|
|
9
|
+
import { parseToolArgumentsObject } from '../../shared/tool-args.js';
|
|
10
|
+
/** Construct authenticated WebSocket URL for Gemini Live API. */
|
|
11
|
+
export declare function buildGeminiLiveWebSocketUrl(apiKey: string): string;
|
|
12
|
+
export declare function wireFunctionDeclaration(decl: WireFunctionTool): Record<string, unknown>;
|
|
13
|
+
export declare function wireLiveTools(req: ProviderCompleteRequest): Array<Record<string, unknown>>;
|
|
14
|
+
/** Build the initial `setup` message sent once immediately after WebSocket open. */
|
|
15
|
+
export declare function buildGeminiLiveSetupMessage(req: ProviderCompleteRequest): Record<string, unknown>;
|
|
16
|
+
/** Build the `clientContent` message used for seeding conversation history before realtime streaming. */
|
|
17
|
+
export declare function buildGeminiLiveClientContent(history: TurnHistoryMessage[]): Record<string, unknown> | null;
|
|
18
|
+
/** Build a `realtimeInput` message for streaming audio, video, or text chunks. */
|
|
19
|
+
export declare function buildGeminiLiveRealtimeInput(input: InteractionPart): Record<string, unknown>;
|
|
20
|
+
/** Build a `realtimeInput` message with text. */
|
|
21
|
+
export declare function buildGeminiLiveRealtimeText(text: string): Record<string, unknown>;
|
|
22
|
+
/** Build a `toolResponse` message returning the execution result of a tool call. */
|
|
23
|
+
export declare function buildGeminiLiveToolResponse(id: string, name: string, output: unknown): Record<string, unknown>;
|
|
24
|
+
/** Build a batched `toolResponse` for one or more function results. */
|
|
25
|
+
export declare function buildGeminiLiveToolResponses(responses: Array<{
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
output: unknown;
|
|
29
|
+
}>): Record<string, unknown>;
|
|
30
|
+
/** Parse raw WebSocket message text / buffer into a JSON record. */
|
|
31
|
+
export type ParsedLiveMessage = {
|
|
32
|
+
ok: true;
|
|
33
|
+
value: Record<string, unknown>;
|
|
34
|
+
} | {
|
|
35
|
+
ok: false;
|
|
36
|
+
reason: 'empty' | 'malformed';
|
|
37
|
+
};
|
|
38
|
+
export declare function parseGeminiLiveMessage(raw: unknown): ParsedLiveMessage;
|
|
39
|
+
export declare function parseFunctionArguments(raw: unknown): ReturnType<typeof parseToolArgumentsObject>;
|
|
40
|
+
export declare function extractLiveUsageTokens(metadata: Record<string, unknown>): TurnTokens | undefined;
|
|
41
|
+
/** Parse Gemini goAway.timeLeft (seconds number, "10s", duration string) → ms when known. */
|
|
42
|
+
export declare function parseGoAwayTimeLeftMs(timeLeft: unknown): number | undefined;
|
|
43
|
+
export type LiveInteractionStatus = 'IN_PROGRESS' | 'IDLE';
|
|
44
|
+
/** Read the server-side `interactionStatus` when the message carries one. */
|
|
45
|
+
export declare function readLiveInteractionStatus(message: Record<string, unknown>): LiveInteractionStatus | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Fold a raw `BidiGenerateContentServerMessage` into normalized `TurnEvent` items.
|
|
48
|
+
*/
|
|
49
|
+
export declare function foldGeminiLiveServerMessage(message: Record<string, unknown> | null | undefined): TurnEvent[];
|