@theoremjs/agents 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +1264 -2
- package/esm/_dnt.polyfills.d.ts +66 -0
- package/esm/_dnt.polyfills.js +51 -0
- package/esm/mod.d.ts +65 -0
- package/esm/mod.js +42 -0
- package/esm/package.json +3 -0
- package/esm/src/cli/bin.d.ts +7 -0
- package/esm/src/cli/bin.js +9 -0
- package/esm/src/cli/commands/bench.d.ts +6 -0
- package/esm/src/cli/commands/bench.js +458 -0
- package/esm/src/cli/commands/egress-compile.d.ts +17 -0
- package/esm/src/cli/commands/egress-compile.js +25 -0
- package/esm/src/cli/commands/eval.d.ts +37 -0
- package/esm/src/cli/commands/eval.js +190 -0
- package/esm/src/cli/commands/fuzz-canary.d.ts +15 -0
- package/esm/src/cli/commands/fuzz-canary.js +202 -0
- package/esm/src/cli/commands/fuzz-guardrails.d.ts +2 -0
- package/esm/src/cli/commands/fuzz-guardrails.js +5 -0
- package/esm/src/cli/commands/profile.d.ts +2 -0
- package/esm/src/cli/commands/profile.js +80 -0
- package/esm/src/cli/commands/run.d.ts +13 -0
- package/esm/src/cli/commands/run.js +45 -0
- package/esm/src/cli/commands/test.d.ts +25 -0
- package/esm/src/cli/commands/test.js +143 -0
- package/esm/src/cli/event-log.d.ts +15 -0
- package/esm/src/cli/event-log.js +136 -0
- package/esm/src/cli/index.d.ts +10 -0
- package/esm/src/cli/index.js +267 -0
- package/esm/src/cli/matrix/fixtures.d.ts +10 -0
- package/esm/src/cli/matrix/fixtures.js +72 -0
- package/esm/src/cli/matrix/synthesizer.d.ts +18 -0
- package/esm/src/cli/matrix/synthesizer.js +103 -0
- package/esm/src/evals/attachments.d.ts +4 -0
- package/esm/src/evals/attachments.js +53 -0
- package/esm/src/evals/breakdown.d.ts +28 -0
- package/esm/src/evals/breakdown.js +68 -0
- package/esm/src/evals/graders/answer.d.ts +13 -0
- package/esm/src/evals/graders/answer.js +133 -0
- package/esm/src/evals/graders/code.d.ts +64 -0
- package/esm/src/evals/graders/code.js +205 -0
- package/esm/src/evals/graders/judge.d.ts +20 -0
- package/esm/src/evals/graders/judge.js +365 -0
- package/esm/src/evals/graders/latency.d.ts +5 -0
- package/esm/src/evals/graders/latency.js +81 -0
- package/esm/src/evals/graders/live.d.ts +11 -0
- package/esm/src/evals/graders/live.js +40 -0
- package/esm/src/evals/graders/media.d.ts +18 -0
- package/esm/src/evals/graders/media.js +92 -0
- package/esm/src/evals/graders/shared.d.ts +11 -0
- package/esm/src/evals/graders/shared.js +62 -0
- package/esm/src/evals/graders/transcript.d.ts +17 -0
- package/esm/src/evals/graders/transcript.js +200 -0
- package/esm/src/evals/mod.d.ts +42 -0
- package/esm/src/evals/mod.js +30 -0
- package/esm/src/evals/record.d.ts +35 -0
- package/esm/src/evals/record.js +105 -0
- package/esm/src/evals/rubrics/catalog.d.ts +148 -0
- package/esm/src/evals/rubrics/catalog.js +846 -0
- package/esm/src/evals/rubrics/mod.d.ts +24 -0
- package/esm/src/evals/rubrics/mod.js +55 -0
- package/esm/src/evals/rubrics/mustache.d.ts +23 -0
- package/esm/src/evals/rubrics/mustache.js +154 -0
- package/esm/src/evals/rubrics/types.d.ts +54 -0
- package/esm/src/evals/rubrics/types.js +74 -0
- package/esm/src/evals/run.d.ts +118 -0
- package/esm/src/evals/run.js +452 -0
- package/esm/src/evals/suite.d.ts +19 -0
- package/esm/src/evals/suite.js +92 -0
- package/esm/src/evals/summary.d.ts +32 -0
- package/esm/src/evals/summary.js +30 -0
- package/esm/src/evals/trial.d.ts +27 -0
- package/esm/src/evals/trial.js +158 -0
- package/esm/src/evals/types.d.ts +260 -0
- package/esm/src/evals/types.js +126 -0
- package/esm/src/evals/verdict.d.ts +18 -0
- package/esm/src/evals/verdict.js +45 -0
- package/esm/src/guardrails/canary-gate.d.ts +21 -0
- package/esm/src/guardrails/canary-gate.js +40 -0
- package/esm/src/guardrails/canary.d.ts +112 -0
- package/esm/src/guardrails/canary.js +901 -0
- package/esm/src/guardrails/compile-egress.d.ts +11 -0
- package/esm/src/guardrails/compile-egress.js +11 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +22 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.js +303 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.d.ts +5 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.js +202 -0
- package/esm/src/guardrails/corpus/inbound-payloads.d.ts +3 -0
- package/esm/src/guardrails/corpus/inbound-payloads.js +118 -0
- package/esm/src/guardrails/corpus/live-attacks.d.ts +13 -0
- package/esm/src/guardrails/corpus/live-attacks.js +224 -0
- package/esm/src/guardrails/corpus/mod.d.ts +7 -0
- package/esm/src/guardrails/corpus/mod.js +4 -0
- package/esm/src/guardrails/corpus/secrets.d.ts +12 -0
- package/esm/src/guardrails/corpus/secrets.js +16 -0
- package/esm/src/guardrails/corpus/strings.d.ts +23 -0
- package/esm/src/guardrails/corpus/strings.js +29 -0
- package/esm/src/guardrails/corpus/types.d.ts +32 -0
- package/esm/src/guardrails/corpus/types.js +1 -0
- package/esm/src/guardrails/egress-automata.d.ts +14 -0
- package/esm/src/guardrails/egress-automata.js +6059 -0
- package/esm/src/guardrails/egress-compiler.d.ts +33 -0
- package/esm/src/guardrails/egress-compiler.js +237 -0
- package/esm/src/guardrails/egress-patterns.d.ts +24 -0
- package/esm/src/guardrails/egress-patterns.js +32 -0
- package/esm/src/guardrails/egress-policy.d.ts +27 -0
- package/esm/src/guardrails/egress-policy.js +76 -0
- package/esm/src/guardrails/egress-rules.d.ts +54 -0
- package/esm/src/guardrails/egress-rules.js +49 -0
- package/esm/src/guardrails/egress-stream.d.ts +105 -0
- package/esm/src/guardrails/egress-stream.js +645 -0
- package/esm/src/guardrails/egress-urls.d.ts +83 -0
- package/esm/src/guardrails/egress-urls.js +917 -0
- package/esm/src/guardrails/egress.d.ts +106 -0
- package/esm/src/guardrails/egress.js +374 -0
- package/esm/src/guardrails/error.d.ts +26 -0
- package/esm/src/guardrails/error.js +91 -0
- package/esm/src/guardrails/event-schemas.d.ts +57 -0
- package/esm/src/guardrails/event-schemas.js +41 -0
- package/esm/src/guardrails/events.d.ts +9 -0
- package/esm/src/guardrails/events.js +42 -0
- package/esm/src/guardrails/hits.d.ts +8 -0
- package/esm/src/guardrails/hits.js +19 -0
- package/esm/src/guardrails/injection-patterns.d.ts +15 -0
- package/esm/src/guardrails/injection-patterns.js +94 -0
- package/esm/src/guardrails/injection.d.ts +14 -0
- package/esm/src/guardrails/injection.js +237 -0
- package/esm/src/guardrails/lexicon.d.ts +30 -0
- package/esm/src/guardrails/lexicon.js +478 -0
- package/esm/src/guardrails/live-outbound-gate.d.ts +54 -0
- package/esm/src/guardrails/live-outbound-gate.js +305 -0
- package/esm/src/guardrails/mod.d.ts +46 -0
- package/esm/src/guardrails/mod.js +29 -0
- package/esm/src/guardrails/network.d.ts +42 -0
- package/esm/src/guardrails/network.js +328 -0
- package/esm/src/guardrails/normalize.d.ts +6 -0
- package/esm/src/guardrails/normalize.js +132 -0
- package/esm/src/guardrails/policy.d.ts +19 -0
- package/esm/src/guardrails/policy.js +50 -0
- package/esm/src/guardrails/progressive-yield.d.ts +69 -0
- package/esm/src/guardrails/progressive-yield.js +237 -0
- package/esm/src/guardrails/prompt-echo.d.ts +38 -0
- package/esm/src/guardrails/prompt-echo.js +225 -0
- package/esm/src/guardrails/quota.d.ts +20 -0
- package/esm/src/guardrails/quota.js +80 -0
- package/esm/src/guardrails/rules.d.ts +56 -0
- package/esm/src/guardrails/rules.js +54 -0
- package/esm/src/guardrails/sanitize.d.ts +23 -0
- package/esm/src/guardrails/sanitize.js +184 -0
- package/esm/src/guardrails/sensitive.d.ts +29 -0
- package/esm/src/guardrails/sensitive.js +113 -0
- package/esm/src/guardrails/serialize.d.ts +14 -0
- package/esm/src/guardrails/serialize.js +37 -0
- package/esm/src/guardrails/testing.d.ts +9 -0
- package/esm/src/guardrails/testing.js +8 -0
- package/esm/src/guardrails/theorem-error.d.ts +18 -0
- package/esm/src/guardrails/theorem-error.js +53 -0
- package/esm/src/guardrails/thought-guard.d.ts +55 -0
- package/esm/src/guardrails/thought-guard.js +464 -0
- package/esm/src/guardrails/tool-directives.d.ts +19 -0
- package/esm/src/guardrails/tool-directives.js +109 -0
- package/esm/src/guardrails/tool-result.d.ts +61 -0
- package/esm/src/guardrails/tool-result.js +248 -0
- package/esm/src/guardrails/types.d.ts +242 -0
- package/esm/src/guardrails/types.js +54 -0
- package/esm/src/host/client-turn.d.ts +9 -0
- package/esm/src/host/client-turn.js +38 -0
- package/esm/src/host/mint-trace.d.ts +28 -0
- package/esm/src/host/mint-trace.js +59 -0
- package/esm/src/host/mod.d.ts +13 -0
- package/esm/src/host/mod.js +11 -0
- package/esm/src/host/readStreamingJsonStringField.d.ts +2 -0
- package/esm/src/host/readStreamingJsonStringField.js +68 -0
- package/esm/src/host/reply.d.ts +7 -0
- package/esm/src/host/reply.js +35 -0
- package/esm/src/interface/blocks.d.ts +8 -0
- package/esm/src/interface/blocks.js +213 -0
- package/esm/src/interface/composer-actions.d.ts +20 -0
- package/esm/src/interface/composer-actions.js +31 -0
- package/esm/src/interface/draft.d.ts +14 -0
- package/esm/src/interface/draft.js +28 -0
- package/esm/src/interface/from-profile.d.ts +12 -0
- package/esm/src/interface/from-profile.js +144 -0
- package/esm/src/interface/history.d.ts +40 -0
- package/esm/src/interface/history.js +269 -0
- package/esm/src/interface/inputs.d.ts +10 -0
- package/esm/src/interface/inputs.js +64 -0
- package/esm/src/interface/mod.d.ts +31 -0
- package/esm/src/interface/mod.js +21 -0
- package/esm/src/interface/models.d.ts +19 -0
- package/esm/src/interface/models.js +45 -0
- package/esm/src/interface/pending.d.ts +48 -0
- package/esm/src/interface/pending.js +146 -0
- package/esm/src/interface/profile-interface.d.ts +4 -0
- package/esm/src/interface/profile-interface.js +223 -0
- package/esm/src/interface/session.d.ts +64 -0
- package/esm/src/interface/session.js +121 -0
- package/esm/src/interface/tool-calls.d.ts +20 -0
- package/esm/src/interface/tool-calls.js +73 -0
- package/esm/src/interface/tool-invoke.d.ts +4 -0
- package/esm/src/interface/tool-invoke.js +20 -0
- package/esm/src/interface/tool-media.d.ts +9 -0
- package/esm/src/interface/tool-media.js +112 -0
- package/esm/src/interface/types.d.ts +213 -0
- package/esm/src/interface/types.js +4 -0
- package/esm/src/kernel/auth/credential-source.d.ts +23 -0
- package/esm/src/kernel/auth/credential-source.js +11 -0
- package/esm/src/kernel/auth/crypto.d.ts +51 -0
- package/esm/src/kernel/auth/crypto.js +161 -0
- package/esm/src/kernel/auth/mod.d.ts +5 -0
- package/esm/src/kernel/auth/mod.js +5 -0
- package/esm/src/kernel/auth/oauth.d.ts +17 -0
- package/esm/src/kernel/auth/oauth.js +438 -0
- package/esm/src/kernel/auth/scope-refusal.d.ts +9 -0
- package/esm/src/kernel/auth/scope-refusal.js +8 -0
- package/esm/src/kernel/auth/typed-secret.d.ts +13 -0
- package/esm/src/kernel/auth/typed-secret.js +23 -0
- package/esm/src/kernel/auth/types.d.ts +139 -0
- package/esm/src/kernel/auth/types.js +1 -0
- package/esm/src/kernel/default-scope.d.ts +34 -0
- package/esm/src/kernel/default-scope.js +72 -0
- package/esm/src/kernel/engine/compaction.d.ts +70 -0
- package/esm/src/kernel/engine/compaction.js +202 -0
- package/esm/src/kernel/engine/decision.d.ts +41 -0
- package/esm/src/kernel/engine/decision.js +412 -0
- package/esm/src/kernel/engine/hash.d.ts +6 -0
- package/esm/src/kernel/engine/hash.js +22 -0
- package/esm/src/kernel/engine/live-inbound.d.ts +9 -0
- package/esm/src/kernel/engine/live-inbound.js +19 -0
- package/esm/src/kernel/engine/live-ingress.d.ts +9 -0
- package/esm/src/kernel/engine/live-ingress.js +37 -0
- package/esm/src/kernel/engine/media-probe/audio.d.ts +7 -0
- package/esm/src/kernel/engine/media-probe/audio.js +342 -0
- package/esm/src/kernel/engine/media-probe/bytes.d.ts +6 -0
- package/esm/src/kernel/engine/media-probe/bytes.js +26 -0
- package/esm/src/kernel/engine/media-probe/image.d.ts +5 -0
- package/esm/src/kernel/engine/media-probe/image.js +171 -0
- package/esm/src/kernel/engine/media-probe/iso-bmff.d.ts +19 -0
- package/esm/src/kernel/engine/media-probe/iso-bmff.js +48 -0
- package/esm/src/kernel/engine/media-probe/matroska.d.ts +35 -0
- package/esm/src/kernel/engine/media-probe/matroska.js +226 -0
- package/esm/src/kernel/engine/media-probe/mp4.d.ts +22 -0
- package/esm/src/kernel/engine/media-probe/mp4.js +279 -0
- package/esm/src/kernel/engine/media-probe/opus.d.ts +4 -0
- package/esm/src/kernel/engine/media-probe/opus.js +23 -0
- package/esm/src/kernel/engine/media-probe/pdf.d.ts +5 -0
- package/esm/src/kernel/engine/media-probe/pdf.js +117 -0
- package/esm/src/kernel/engine/media-probe/video.d.ts +15 -0
- package/esm/src/kernel/engine/media-probe/video.js +49 -0
- package/esm/src/kernel/engine/record.d.ts +3 -0
- package/esm/src/kernel/engine/record.js +11 -0
- package/esm/src/kernel/engine/repair.d.ts +7 -0
- package/esm/src/kernel/engine/repair.js +36 -0
- package/esm/src/kernel/engine/runner/gates.d.ts +5 -0
- package/esm/src/kernel/engine/runner/gates.js +322 -0
- package/esm/src/kernel/engine/runner/mod.d.ts +15 -0
- package/esm/src/kernel/engine/runner/mod.js +574 -0
- package/esm/src/kernel/engine/runner/schema-validation.d.ts +14 -0
- package/esm/src/kernel/engine/runner/schema-validation.js +105 -0
- package/esm/src/kernel/engine/runner/stages.d.ts +28 -0
- package/esm/src/kernel/engine/runner/stages.js +48 -0
- package/esm/src/kernel/engine/runner/state.d.ts +76 -0
- package/esm/src/kernel/engine/runner/state.js +40 -0
- package/esm/src/kernel/engine/runner/steps.d.ts +14 -0
- package/esm/src/kernel/engine/runner/steps.js +360 -0
- package/esm/src/kernel/engine/runner/stream.d.ts +40 -0
- package/esm/src/kernel/engine/runner/stream.js +225 -0
- package/esm/src/kernel/engine/runner/usage.d.ts +45 -0
- package/esm/src/kernel/engine/runner/usage.js +150 -0
- package/esm/src/kernel/engine/session/mod.d.ts +31 -0
- package/esm/src/kernel/engine/session/mod.js +951 -0
- package/esm/src/kernel/engine/session/session-trace.d.ts +115 -0
- package/esm/src/kernel/engine/session/session-trace.js +489 -0
- package/esm/src/kernel/engine/token-estimate.d.ts +77 -0
- package/esm/src/kernel/engine/token-estimate.js +218 -0
- package/esm/src/kernel/engine/tool-trace.d.ts +58 -0
- package/esm/src/kernel/engine/tool-trace.js +131 -0
- package/esm/src/kernel/engine/tree.d.ts +2 -0
- package/esm/src/kernel/engine/tree.js +17 -0
- package/esm/src/kernel/engine/turn-trace.d.ts +118 -0
- package/esm/src/kernel/engine/turn-trace.js +863 -0
- package/esm/src/kernel/engine/usage.d.ts +30 -0
- package/esm/src/kernel/engine/usage.js +134 -0
- package/esm/src/kernel/interaction-parts.d.ts +10 -0
- package/esm/src/kernel/interaction-parts.js +22 -0
- package/esm/src/kernel/mod.d.ts +47 -0
- package/esm/src/kernel/mod.js +30 -0
- package/esm/src/kernel/profile-graph.d.ts +140 -0
- package/esm/src/kernel/profile-graph.js +151 -0
- package/esm/src/kernel/profile-presence.d.ts +9 -0
- package/esm/src/kernel/profile-presence.js +143 -0
- package/esm/src/kernel/profile-scope.d.ts +36 -0
- package/esm/src/kernel/profile-scope.js +262 -0
- package/esm/src/kernel/registry/attachments.d.ts +35 -0
- package/esm/src/kernel/registry/attachments.js +211 -0
- package/esm/src/kernel/registry/catalog.d.ts +19 -0
- package/esm/src/kernel/registry/catalog.js +78 -0
- package/esm/src/kernel/registry/ingress.d.ts +8 -0
- package/esm/src/kernel/registry/ingress.js +134 -0
- package/esm/src/kernel/registry/kernel-registry.d.ts +12 -0
- package/esm/src/kernel/registry/kernel-registry.js +8 -0
- package/esm/src/kernel/registry/profile-outputs.d.ts +3 -0
- package/esm/src/kernel/registry/profile-outputs.js +7 -0
- package/esm/src/kernel/registry/profiles.d.ts +92 -0
- package/esm/src/kernel/registry/profiles.js +698 -0
- package/esm/src/kernel/registry/provider-request.d.ts +6 -0
- package/esm/src/kernel/registry/provider-request.js +42 -0
- package/esm/src/kernel/registry/resolve.d.ts +14 -0
- package/esm/src/kernel/registry/resolve.js +237 -0
- package/esm/src/kernel/registry/schemas.d.ts +9 -0
- package/esm/src/kernel/registry/schemas.js +25 -0
- package/esm/src/kernel/registry/sole-model.d.ts +2 -0
- package/esm/src/kernel/registry/sole-model.js +4 -0
- package/esm/src/kernel/registry/system-prompt.d.ts +8 -0
- package/esm/src/kernel/registry/system-prompt.js +33 -0
- package/esm/src/kernel/registry/system-role.d.ts +3 -0
- package/esm/src/kernel/registry/system-role.js +9 -0
- package/esm/src/kernel/registry/vault.d.ts +13 -0
- package/esm/src/kernel/registry/vault.js +14 -0
- package/esm/src/kernel/schema.d.ts +172 -0
- package/esm/src/kernel/schema.js +653 -0
- package/esm/src/kernel/scope.d.ts +22 -0
- package/esm/src/kernel/scope.js +25 -0
- package/esm/src/kernel/stages.d.ts +141 -0
- package/esm/src/kernel/stages.js +388 -0
- package/esm/src/kernel/stop.d.ts +93 -0
- package/esm/src/kernel/stop.js +125 -0
- package/esm/src/kernel/system-parts.d.ts +26 -0
- package/esm/src/kernel/system-parts.js +98 -0
- package/esm/src/kernel/tools/activity-label.d.ts +16 -0
- package/esm/src/kernel/tools/activity-label.js +209 -0
- package/esm/src/kernel/tools/events.d.ts +51 -0
- package/esm/src/kernel/tools/events.js +149 -0
- package/esm/src/kernel/tools/execute.d.ts +64 -0
- package/esm/src/kernel/tools/execute.js +801 -0
- package/esm/src/kernel/tools/gate-answer.d.ts +50 -0
- package/esm/src/kernel/tools/gate-answer.js +54 -0
- package/esm/src/kernel/tools/harness.d.ts +16 -0
- package/esm/src/kernel/tools/harness.js +42 -0
- package/esm/src/kernel/tools/invoke.d.ts +10 -0
- package/esm/src/kernel/tools/invoke.js +129 -0
- package/esm/src/kernel/tools/mcp-sessions.d.ts +20 -0
- package/esm/src/kernel/tools/mcp-sessions.js +64 -0
- package/esm/src/kernel/tools/mod.d.ts +9 -0
- package/esm/src/kernel/tools/mod.js +6 -0
- package/esm/src/kernel/tools/model-text.d.ts +11 -0
- package/esm/src/kernel/tools/model-text.js +25 -0
- package/esm/src/kernel/tools/permission.d.ts +6 -0
- package/esm/src/kernel/tools/permission.js +38 -0
- package/esm/src/kernel/tools/project.d.ts +8 -0
- package/esm/src/kernel/tools/project.js +25 -0
- package/esm/src/kernel/tools/registry.d.ts +20 -0
- package/esm/src/kernel/tools/registry.js +84 -0
- package/esm/src/kernel/tools/remote.d.ts +110 -0
- package/esm/src/kernel/tools/remote.js +998 -0
- package/esm/src/kernel/tools/resolve.d.ts +28 -0
- package/esm/src/kernel/tools/resolve.js +259 -0
- package/esm/src/kernel/tools/schema.d.ts +10 -0
- package/esm/src/kernel/tools/schema.js +174 -0
- package/esm/src/kernel/tools/signed-in-fetch.d.ts +18 -0
- package/esm/src/kernel/tools/signed-in-fetch.js +45 -0
- package/esm/src/kernel/tools/stage-run.d.ts +93 -0
- package/esm/src/kernel/tools/stage-run.js +140 -0
- package/esm/src/kernel/tools/types.d.ts +287 -0
- package/esm/src/kernel/tools/types.js +1 -0
- package/esm/src/kernel/turn-events.d.ts +599 -0
- package/esm/src/kernel/turn-events.js +425 -0
- package/esm/src/kernel/types.d.ts +884 -0
- package/esm/src/kernel/types.js +1 -0
- package/esm/src/kernel/util/base64.d.ts +2 -0
- package/esm/src/kernel/util/base64.js +13 -0
- package/esm/src/kernel/util/exact-type.d.ts +31 -0
- package/esm/src/kernel/util/exact-type.js +1 -0
- package/esm/src/kernel/util/find-last.d.ts +3 -0
- package/esm/src/kernel/util/find-last.js +9 -0
- package/esm/src/kernel/util/mime.d.ts +2 -0
- package/esm/src/kernel/util/mime.js +4 -0
- package/esm/src/kernel/util/record.d.ts +1 -0
- package/esm/src/kernel/util/record.js +3 -0
- package/esm/src/observability/destinations.d.ts +9 -0
- package/esm/src/observability/destinations.js +38 -0
- package/esm/src/observability/jsonl.d.ts +18 -0
- package/esm/src/observability/jsonl.js +117 -0
- package/esm/src/observability/mod.d.ts +22 -0
- package/esm/src/observability/mod.js +16 -0
- package/esm/src/observability/openinference.d.ts +11 -0
- package/esm/src/observability/openinference.js +205 -0
- package/esm/src/observability/otlp.d.ts +68 -0
- package/esm/src/observability/otlp.js +98 -0
- package/esm/src/observability/phoenix.d.ts +29 -0
- package/esm/src/observability/phoenix.js +71 -0
- package/esm/src/observability/policy.d.ts +14 -0
- package/esm/src/observability/policy.js +69 -0
- package/esm/src/observability/resolve-policy.d.ts +8 -0
- package/esm/src/observability/resolve-policy.js +58 -0
- package/esm/src/observability/spans.d.ts +16 -0
- package/esm/src/observability/spans.js +56 -0
- package/esm/src/observability/trace-catalog.d.ts +111 -0
- package/esm/src/observability/trace-catalog.js +865 -0
- package/esm/src/observability/trace-record.d.ts +23 -0
- package/esm/src/observability/trace-record.js +223 -0
- package/esm/src/observability/trace-schema.d.ts +54 -0
- package/esm/src/observability/trace-schema.js +59 -0
- package/esm/src/observability/trace-sink.d.ts +13 -0
- package/esm/src/observability/trace-sink.js +3 -0
- package/esm/src/observability/trace-span.d.ts +92 -0
- package/esm/src/observability/trace-span.js +199 -0
- package/esm/src/observability/trace.d.ts +11 -0
- package/esm/src/observability/trace.js +35 -0
- package/esm/src/observability/types.d.ts +85 -0
- package/esm/src/observability/types.js +3 -0
- package/esm/src/presets/google/speech-voices.d.ts +8 -0
- package/esm/src/presets/google/speech-voices.js +38 -0
- package/esm/src/presets/google-limits.d.ts +12 -0
- package/esm/src/presets/google-limits.js +18 -0
- package/esm/src/presets/google.d.ts +104 -0
- package/esm/src/presets/google.js +188 -0
- package/esm/src/presets/mod.d.ts +10 -0
- package/esm/src/presets/mod.js +9 -0
- package/esm/src/presets/openrouter.d.ts +13 -0
- package/esm/src/presets/openrouter.js +13 -0
- package/esm/src/presets/typesafe.d.ts +2 -0
- package/esm/src/presets/typesafe.js +2 -0
- package/esm/src/providers/create-provider.d.ts +21 -0
- package/esm/src/providers/create-provider.js +90 -0
- package/esm/src/providers/decision/usage.d.ts +3 -0
- package/esm/src/providers/decision/usage.js +25 -0
- package/esm/src/providers/google/api-error.d.ts +8 -0
- package/esm/src/providers/google/api-error.js +123 -0
- package/esm/src/providers/google/grounding.d.ts +5 -0
- package/esm/src/providers/google/grounding.js +226 -0
- package/esm/src/providers/google/interactions/framing.d.ts +15 -0
- package/esm/src/providers/google/interactions/framing.js +263 -0
- package/esm/src/providers/google/interactions/mod.d.ts +2 -0
- package/esm/src/providers/google/interactions/mod.js +2 -0
- package/esm/src/providers/google/interactions/steps.d.ts +12 -0
- package/esm/src/providers/google/interactions/steps.js +200 -0
- package/esm/src/providers/google/interactions/stream.d.ts +24 -0
- package/esm/src/providers/google/interactions/stream.js +297 -0
- package/esm/src/providers/google/keys.d.ts +20 -0
- package/esm/src/providers/google/keys.js +87 -0
- package/esm/src/providers/google/live/framing.d.ts +51 -0
- package/esm/src/providers/google/live/framing.js +597 -0
- package/esm/src/providers/google/live/mod.d.ts +7 -0
- package/esm/src/providers/google/live/mod.js +7 -0
- package/esm/src/providers/google/live/openapi-schema.d.ts +1 -0
- package/esm/src/providers/google/live/openapi-schema.js +42 -0
- package/esm/src/providers/google/live/session.d.ts +14 -0
- package/esm/src/providers/google/live/session.js +150 -0
- package/esm/src/providers/google/live/stream.d.ts +51 -0
- package/esm/src/providers/google/live/stream.js +263 -0
- package/esm/src/providers/google/thinking.d.ts +3 -0
- package/esm/src/providers/google/thinking.js +8 -0
- package/esm/src/providers/google/urls.d.ts +6 -0
- package/esm/src/providers/google/urls.js +6 -0
- package/esm/src/providers/google/usage.d.ts +7 -0
- package/esm/src/providers/google/usage.js +49 -0
- package/esm/src/providers/local/local.d.ts +12 -0
- package/esm/src/providers/local/local.js +186 -0
- package/esm/src/providers/local/mod.d.ts +8 -0
- package/esm/src/providers/local/mod.js +8 -0
- package/esm/src/providers/mod.d.ts +11 -0
- package/esm/src/providers/mod.js +7 -0
- package/esm/src/providers/openrouter/cache-control.d.ts +5 -0
- package/esm/src/providers/openrouter/cache-control.js +4 -0
- package/esm/src/providers/openrouter/chat.d.ts +64 -0
- package/esm/src/providers/openrouter/chat.js +490 -0
- package/esm/src/providers/openrouter/image.d.ts +24 -0
- package/esm/src/providers/openrouter/image.js +187 -0
- package/esm/src/providers/openrouter/openai/compat.d.ts +19 -0
- package/esm/src/providers/openrouter/openai/compat.js +162 -0
- package/esm/src/providers/openrouter/openai/image-payload.d.ts +9 -0
- package/esm/src/providers/openrouter/openai/image-payload.js +115 -0
- package/esm/src/providers/openrouter/openai/sdk-messages.d.ts +10 -0
- package/esm/src/providers/openrouter/openai/sdk-messages.js +99 -0
- package/esm/src/providers/openrouter/openai/usage.d.ts +8 -0
- package/esm/src/providers/openrouter/openai/usage.js +56 -0
- package/esm/src/providers/openrouter/resolve-api-key.d.ts +3 -0
- package/esm/src/providers/openrouter/resolve-api-key.js +8 -0
- package/esm/src/providers/openrouter/speech.d.ts +18 -0
- package/esm/src/providers/openrouter/speech.js +109 -0
- package/esm/src/providers/probe.d.ts +5 -0
- package/esm/src/providers/probe.js +15 -0
- package/esm/src/providers/shared/builtin-wire.d.ts +4 -0
- package/esm/src/providers/shared/builtin-wire.js +9 -0
- package/esm/src/providers/shared/pcm.d.ts +14 -0
- package/esm/src/providers/shared/pcm.js +78 -0
- package/esm/src/providers/shared/response-identity.d.ts +11 -0
- package/esm/src/providers/shared/response-identity.js +12 -0
- package/esm/src/providers/shared/sse.d.ts +10 -0
- package/esm/src/providers/shared/sse.js +79 -0
- package/esm/src/providers/shared/structured-output.d.ts +4 -0
- package/esm/src/providers/shared/structured-output.js +11 -0
- package/esm/src/providers/shared/tool-args.d.ts +25 -0
- package/esm/src/providers/shared/tool-args.js +84 -0
- package/esm/src/providers/shared/upstream-tap.d.ts +8 -0
- package/esm/src/providers/shared/upstream-tap.js +85 -0
- package/esm/src/providers/shared/upstream-tape.d.ts +6 -0
- package/esm/src/providers/shared/upstream-tape.js +88 -0
- package/esm/src/providers/shared/vault.d.ts +13 -0
- package/esm/src/providers/shared/vault.js +41 -0
- package/esm/src/providers/types.d.ts +19 -0
- package/esm/src/providers/types.js +1 -0
- package/esm/src/surface/formats.d.ts +28 -0
- package/esm/src/surface/formats.js +139 -0
- package/esm/src/surface/mod.d.ts +14 -0
- package/esm/src/surface/mod.js +10 -0
- package/esm/src/surface/runtime.d.ts +40 -0
- package/esm/src/surface/runtime.js +439 -0
- package/esm/src/surface/tools.d.ts +15 -0
- package/esm/src/surface/tools.js +80 -0
- package/esm/src/surface/types.d.ts +104 -0
- package/esm/src/surface/types.js +4 -0
- package/package.json +108 -4
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import { TheoremError, toErrorEvent } from '../../../guardrails/error.js';
|
|
2
|
+
import { asRecord } from '../../../kernel/engine/record.js';
|
|
3
|
+
import { reportedTokens, usageCount } from '../../../kernel/engine/usage.js';
|
|
4
|
+
import { historyMessageParts, isMediaRefPart } from '../../../kernel/interaction-parts.js';
|
|
5
|
+
import { mediaKindForMime } from '../../../kernel/registry/catalog.js';
|
|
6
|
+
import { toolEvent } from '../../../kernel/tools/events.js';
|
|
7
|
+
import { isRecord } from '../../../kernel/util/record.js';
|
|
8
|
+
import { builtinWire } from '../../shared/builtin-wire.js';
|
|
9
|
+
import { pcmMediaAsWav } from '../../shared/pcm.js';
|
|
10
|
+
import { historyToolArguments, historyToolIdentity, toolCallEvents, } from '../../shared/tool-args.js';
|
|
11
|
+
import { groundingFromLiveMetadata } from '../grounding.js';
|
|
12
|
+
import { assertGoogleThinkingLevel } from '../thinking.js';
|
|
13
|
+
import { GEMINI_LIVE_WS_URL } from '../urls.js';
|
|
14
|
+
import { byModality, modalityCounts } from '../usage.js';
|
|
15
|
+
import { toGeminiOpenApiSchema } from './openapi-schema.js';
|
|
16
|
+
export function buildGeminiLiveWebSocketUrl(apiKey) {
|
|
17
|
+
return `${GEMINI_LIVE_WS_URL}?key=${encodeURIComponent(apiKey)}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Non-blocking: the host runs each call via `LiveSession.executeTool` while the
|
|
21
|
+
* model keeps speaking, and a cancel reaches it as a `tool` event with `phase: 'cancel'`.
|
|
22
|
+
*/
|
|
23
|
+
const LIVE_FUNCTION_BEHAVIOR = 'NON_BLOCKING';
|
|
24
|
+
export function wireFunctionDeclaration(decl) {
|
|
25
|
+
const parameters = toGeminiOpenApiSchema(decl.parameters);
|
|
26
|
+
return {
|
|
27
|
+
name: decl.name,
|
|
28
|
+
description: decl.description,
|
|
29
|
+
behavior: LIVE_FUNCTION_BEHAVIOR,
|
|
30
|
+
parameters: parameters && typeof parameters === 'object'
|
|
31
|
+
? parameters
|
|
32
|
+
: { type: 'OBJECT', properties: {} },
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Builtins are their own tool entries (`{ googleSearch: {} }`), functions share
|
|
37
|
+
* one `functionDeclarations` entry. Which builtins a model takes is the API's
|
|
38
|
+
* answer (probe 23/09/2026: a model without one closes the socket with 1007).
|
|
39
|
+
*/
|
|
40
|
+
function wireLiveTools(req) {
|
|
41
|
+
const tools = [];
|
|
42
|
+
for (const builtin of req.builtins) {
|
|
43
|
+
tools.push({ [builtinWire(builtin, 'live')]: {} });
|
|
44
|
+
}
|
|
45
|
+
const functionDeclarations = (req.wireTools ?? []).map(wireFunctionDeclaration);
|
|
46
|
+
if (functionDeclarations.length > 0) {
|
|
47
|
+
tools.push({ functionDeclarations });
|
|
48
|
+
}
|
|
49
|
+
return tools;
|
|
50
|
+
}
|
|
51
|
+
function buildLiveGenerationConfig(req) {
|
|
52
|
+
const generationConfig = {
|
|
53
|
+
responseModalities: ['AUDIO'],
|
|
54
|
+
temperature: req.temperature,
|
|
55
|
+
maxOutputTokens: req.maxOutputTokens,
|
|
56
|
+
};
|
|
57
|
+
if (req.live?.voice) {
|
|
58
|
+
generationConfig.speechConfig = {
|
|
59
|
+
voiceConfig: {
|
|
60
|
+
prebuiltVoiceConfig: {
|
|
61
|
+
voiceName: req.live.voice,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
assertGoogleThinkingLevel(req.thinking);
|
|
67
|
+
if (req.thinking) {
|
|
68
|
+
generationConfig.thinkingConfig = {
|
|
69
|
+
thinkingLevel: req.thinking,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return generationConfig;
|
|
73
|
+
}
|
|
74
|
+
// Unset numbers are left to Gemini's defaults.
|
|
75
|
+
function buildContextWindowCompression(compression) {
|
|
76
|
+
const { triggerTokens } = compression;
|
|
77
|
+
const { targetTokens } = compression.slidingWindow;
|
|
78
|
+
return {
|
|
79
|
+
...(triggerTokens !== undefined ? { triggerTokens } : {}),
|
|
80
|
+
slidingWindow: targetTokens !== undefined ? { targetTokens } : {},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function buildLiveRealtimeInputConfig(vad) {
|
|
84
|
+
const automaticActivityDetection = {};
|
|
85
|
+
if (vad.startSensitivity !== undefined) {
|
|
86
|
+
automaticActivityDetection.startOfSpeechSensitivity = vad.startSensitivity;
|
|
87
|
+
}
|
|
88
|
+
if (vad.endSensitivity !== undefined) {
|
|
89
|
+
automaticActivityDetection.endOfSpeechSensitivity = vad.endSensitivity;
|
|
90
|
+
}
|
|
91
|
+
if (vad.prefixPaddingMs !== undefined) {
|
|
92
|
+
automaticActivityDetection.prefixPaddingMs = vad.prefixPaddingMs;
|
|
93
|
+
}
|
|
94
|
+
if (vad.silenceDurationMs !== undefined) {
|
|
95
|
+
automaticActivityDetection.silenceDurationMs = vad.silenceDurationMs;
|
|
96
|
+
}
|
|
97
|
+
const config = {};
|
|
98
|
+
if (vad.activityHandling !== undefined) {
|
|
99
|
+
config.activityHandling = vad.activityHandling;
|
|
100
|
+
}
|
|
101
|
+
if (Object.keys(automaticActivityDetection).length > 0) {
|
|
102
|
+
config.automaticActivityDetection = automaticActivityDetection;
|
|
103
|
+
}
|
|
104
|
+
return Object.keys(config).length > 0 ? config : undefined;
|
|
105
|
+
}
|
|
106
|
+
function buildLiveSessionResumption(req) {
|
|
107
|
+
if (req.sessionResumptionHandle) {
|
|
108
|
+
return { handle: req.sessionResumptionHandle };
|
|
109
|
+
}
|
|
110
|
+
if (req.live?.sessionResumption) {
|
|
111
|
+
return {};
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
function liveModelName(apiId) {
|
|
116
|
+
return apiId.startsWith('models/') ? apiId : `models/${apiId}`;
|
|
117
|
+
}
|
|
118
|
+
function applyLiveOptionalFeatures(live, setup) {
|
|
119
|
+
if (live?.transcription?.input) {
|
|
120
|
+
setup.inputAudioTranscription = {};
|
|
121
|
+
}
|
|
122
|
+
if (live?.transcription?.output) {
|
|
123
|
+
setup.outputAudioTranscription = {};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export function buildGeminiLiveSetupMessage(req) {
|
|
127
|
+
const live = req.live;
|
|
128
|
+
const realtimeInputConfig = live?.vad ? buildLiveRealtimeInputConfig(live.vad) : undefined;
|
|
129
|
+
const tools = wireLiveTools(req);
|
|
130
|
+
const sessionResumption = buildLiveSessionResumption(req);
|
|
131
|
+
// `initialHistoryInClientContent` makes Gemini wait for clientContent before
|
|
132
|
+
// realtime generation, so an empty session (e.g. Th30) would hang forever.
|
|
133
|
+
const seedInitialHistory = Boolean(req.history && req.history.length > 0);
|
|
134
|
+
const setup = {
|
|
135
|
+
model: liveModelName(req.apiId),
|
|
136
|
+
generationConfig: buildLiveGenerationConfig(req),
|
|
137
|
+
systemInstruction: {
|
|
138
|
+
parts: [{ text: req.system }],
|
|
139
|
+
},
|
|
140
|
+
...(tools.length > 0 ? { tools } : {}),
|
|
141
|
+
...(sessionResumption ? { sessionResumption } : {}),
|
|
142
|
+
...(live?.contextCompression
|
|
143
|
+
? { contextWindowCompression: buildContextWindowCompression(live.contextCompression) }
|
|
144
|
+
: {}),
|
|
145
|
+
...(seedInitialHistory ? { historyConfig: { initialHistoryInClientContent: true } } : {}),
|
|
146
|
+
...(realtimeInputConfig ? { realtimeInputConfig } : {}),
|
|
147
|
+
};
|
|
148
|
+
applyLiveOptionalFeatures(live, setup);
|
|
149
|
+
return { setup };
|
|
150
|
+
}
|
|
151
|
+
/** Live carries inline bytes only — provider file references are rejected until support is verified. */
|
|
152
|
+
function inlineMediaPart(part) {
|
|
153
|
+
if (isMediaRefPart(part)) {
|
|
154
|
+
throw new TheoremError('unsupported', 'media references are not supported on geminiLive');
|
|
155
|
+
}
|
|
156
|
+
return part;
|
|
157
|
+
}
|
|
158
|
+
function inlineData(part) {
|
|
159
|
+
const inline = inlineMediaPart(part);
|
|
160
|
+
return { mimeType: inline.mimeType, data: inline.data };
|
|
161
|
+
}
|
|
162
|
+
function contentPart(part) {
|
|
163
|
+
return part.type === 'text' ? { text: part.text } : { inlineData: inlineData(part) };
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* A `tool` message as one `functionResponse` part in a `user` turn (probed 23/09/2026).
|
|
167
|
+
* Text parts are the `result`; media rides nested in `functionResponse.parts`.
|
|
168
|
+
*/
|
|
169
|
+
function functionResponseTurn(msg) {
|
|
170
|
+
const parts = historyMessageParts(msg);
|
|
171
|
+
const result = parts
|
|
172
|
+
.map((part) => (part.type === 'text' ? part.text : ''))
|
|
173
|
+
.filter(Boolean)
|
|
174
|
+
.join('\n');
|
|
175
|
+
const media = parts.filter((part) => part.type !== 'text').map(contentPart);
|
|
176
|
+
return {
|
|
177
|
+
role: 'user',
|
|
178
|
+
parts: [
|
|
179
|
+
{
|
|
180
|
+
functionResponse: {
|
|
181
|
+
...historyToolIdentity({ id: msg.tool_call_id, name: msg.name }),
|
|
182
|
+
response: { result },
|
|
183
|
+
...(media.length > 0 ? { parts: media } : {}),
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function historyTurnToGoogleTurn(msg) {
|
|
190
|
+
if (msg.role === 'tool') {
|
|
191
|
+
return functionResponseTurn(msg);
|
|
192
|
+
}
|
|
193
|
+
const role = msg.role === 'assistant' ? 'model' : 'user';
|
|
194
|
+
const parts = historyMessageParts(msg).map(contentPart);
|
|
195
|
+
for (const call of msg.role === 'assistant' ? (msg.tool_calls ?? []) : []) {
|
|
196
|
+
parts.push({
|
|
197
|
+
functionCall: {
|
|
198
|
+
id: call.id,
|
|
199
|
+
name: call.function.name,
|
|
200
|
+
args: historyToolArguments(call.function.arguments),
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return { role, parts };
|
|
205
|
+
}
|
|
206
|
+
export function buildGeminiLiveClientContent(history) {
|
|
207
|
+
if (!history || history.length === 0) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
clientContent: {
|
|
212
|
+
turns: history.map(historyTurnToGoogleTurn),
|
|
213
|
+
turnComplete: true,
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
export function buildGeminiLiveContext(text) {
|
|
218
|
+
return {
|
|
219
|
+
clientContent: { turns: [{ role: 'user', parts: [{ text }] }], turnComplete: false },
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
export function buildGeminiLiveRealtimeInput(input) {
|
|
223
|
+
if (input.type === 'text') {
|
|
224
|
+
return {
|
|
225
|
+
realtimeInput: {
|
|
226
|
+
text: input.text,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const part = inlineMediaPart(input);
|
|
231
|
+
// The part's mime as given: the API reads the rate from it and rejects what it cannot take.
|
|
232
|
+
const media = { mimeType: part.mimeType, data: part.data };
|
|
233
|
+
return { realtimeInput: part.type === 'audio' ? { audio: media } : { video: media } };
|
|
234
|
+
}
|
|
235
|
+
export function liveFunctionResponsePayload(output) {
|
|
236
|
+
const error = asRecord(output)?.error;
|
|
237
|
+
if (typeof error === 'string') {
|
|
238
|
+
return { error };
|
|
239
|
+
}
|
|
240
|
+
return { result: output };
|
|
241
|
+
}
|
|
242
|
+
export function buildGeminiLiveToolResponse(id, name, output) {
|
|
243
|
+
return buildGeminiLiveToolResponses([{ id, name, output }]);
|
|
244
|
+
}
|
|
245
|
+
export function buildGeminiLiveToolResponses(responses) {
|
|
246
|
+
return {
|
|
247
|
+
toolResponse: {
|
|
248
|
+
functionResponses: responses.map(({ id, name, output }) => ({
|
|
249
|
+
id,
|
|
250
|
+
name,
|
|
251
|
+
response: liveFunctionResponsePayload(output),
|
|
252
|
+
})),
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function functionResponseMessage(response) {
|
|
257
|
+
return {
|
|
258
|
+
role: 'tool',
|
|
259
|
+
...(typeof response.id === 'string' ? { tool_call_id: response.id } : {}),
|
|
260
|
+
...(typeof response.name === 'string' ? { name: response.name } : {}),
|
|
261
|
+
content: JSON.stringify(response.response ?? null),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function records(value) {
|
|
265
|
+
if (!Array.isArray(value))
|
|
266
|
+
return [];
|
|
267
|
+
return value.flatMap((item) => {
|
|
268
|
+
const record = asRecord(item);
|
|
269
|
+
return record ? [record] : [];
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
function clientTurnMessages(turn) {
|
|
273
|
+
const parts = [];
|
|
274
|
+
const calls = [];
|
|
275
|
+
const responses = [];
|
|
276
|
+
for (const part of records(turn.parts)) {
|
|
277
|
+
const inline = asRecord(part.inlineData);
|
|
278
|
+
const call = asRecord(part.functionCall);
|
|
279
|
+
const response = asRecord(part.functionResponse);
|
|
280
|
+
if (typeof part.text === 'string') {
|
|
281
|
+
parts.push({ type: 'text', text: part.text });
|
|
282
|
+
}
|
|
283
|
+
else if (inline && typeof inline.mimeType === 'string' && typeof inline.data === 'string') {
|
|
284
|
+
const type = mediaKindForMime(inline.mimeType) ?? 'document';
|
|
285
|
+
parts.push({ type, mimeType: inline.mimeType, data: inline.data });
|
|
286
|
+
}
|
|
287
|
+
else if (call) {
|
|
288
|
+
calls.push({
|
|
289
|
+
id: typeof call.id === 'string' ? call.id : '',
|
|
290
|
+
type: 'function',
|
|
291
|
+
function: { name: String(call.name ?? ''), arguments: JSON.stringify(call.args ?? {}) },
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
else if (response) {
|
|
295
|
+
responses.push(functionResponseMessage(response));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const role = turn.role === 'model' ? 'assistant' : 'user';
|
|
299
|
+
const content = parts.length > 0 || calls.length > 0
|
|
300
|
+
? [{ role, parts, ...(calls.length > 0 ? { tool_calls: calls } : {}) }]
|
|
301
|
+
: [];
|
|
302
|
+
return [...content, ...responses];
|
|
303
|
+
}
|
|
304
|
+
/** What one outbound frame gives the model to read; setup and control frames give none. */
|
|
305
|
+
export function liveFrameInput(frame) {
|
|
306
|
+
const realtime = asRecord(frame.realtimeInput);
|
|
307
|
+
if (realtime) {
|
|
308
|
+
const audio = asRecord(realtime.audio);
|
|
309
|
+
const video = asRecord(realtime.video);
|
|
310
|
+
const media = audio ?? video;
|
|
311
|
+
if (media && typeof media.mimeType === 'string' && typeof media.data === 'string') {
|
|
312
|
+
const type = audio ? 'audio' : 'video';
|
|
313
|
+
return [{ role: 'user', parts: [{ type, mimeType: media.mimeType, data: media.data }] }];
|
|
314
|
+
}
|
|
315
|
+
return typeof realtime.text === 'string'
|
|
316
|
+
? [{ role: 'user', parts: [{ type: 'text', text: realtime.text }] }]
|
|
317
|
+
: [];
|
|
318
|
+
}
|
|
319
|
+
const client = asRecord(frame.clientContent);
|
|
320
|
+
if (client) {
|
|
321
|
+
return records(client.turns).flatMap(clientTurnMessages);
|
|
322
|
+
}
|
|
323
|
+
const toolResponse = asRecord(frame.toolResponse);
|
|
324
|
+
return toolResponse ? records(toolResponse.functionResponses).map(functionResponseMessage) : [];
|
|
325
|
+
}
|
|
326
|
+
export function parseGeminiLiveMessage(raw) {
|
|
327
|
+
if (isRecord(raw)) {
|
|
328
|
+
if (raw instanceof ArrayBuffer || raw instanceof Uint8Array) {
|
|
329
|
+
const text = new TextDecoder().decode(raw);
|
|
330
|
+
return parseGeminiLiveMessage(text);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (typeof raw !== 'string' || !raw.trim()) {
|
|
334
|
+
return { ok: false, reason: 'empty' };
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
const parsed = JSON.parse(raw);
|
|
338
|
+
if (isRecord(parsed)) {
|
|
339
|
+
return { ok: true, value: parsed };
|
|
340
|
+
}
|
|
341
|
+
return { ok: false, reason: 'malformed' };
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
return { ok: false, reason: 'malformed' };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Live sends one usage row per model response, at its `turnComplete`, covering that
|
|
349
|
+
* response alone; the prompt count grows because each response re-reads the session.
|
|
350
|
+
* Probe (gemini-3.1-flash-live-preview, 22/09/2026): `thoughtsTokenCount` sits outside
|
|
351
|
+
* `responseTokenCount` and `totalTokenCount`, so it is added to output.
|
|
352
|
+
* `toolUsePromptTokenCount` (not seen in that probe) is added to input, as on Interactions.
|
|
353
|
+
*/
|
|
354
|
+
export function extractLiveUsageTokens(metadata) {
|
|
355
|
+
const prompt = usageCount(metadata.promptTokenCount);
|
|
356
|
+
const response = usageCount(metadata.responseTokenCount);
|
|
357
|
+
const thoughts = usageCount(metadata.thoughtsTokenCount) ?? 0;
|
|
358
|
+
const toolUse = usageCount(metadata.toolUsePromptTokenCount) ?? 0;
|
|
359
|
+
return reportedTokens({
|
|
360
|
+
input: prompt ? prompt + toolUse : undefined,
|
|
361
|
+
output: response === undefined ? undefined : response + thoughts,
|
|
362
|
+
thinking: thoughts,
|
|
363
|
+
toolUse,
|
|
364
|
+
cached: usageCount(metadata.cachedContentTokenCount),
|
|
365
|
+
byModality: byModality(modalityCounts(metadata.promptTokensDetails, 'tokenCount'), modalityCounts(metadata.responseTokensDetails, 'tokenCount')),
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
function foldSessionUpdate(message, events) {
|
|
369
|
+
const sessionUpdate = asRecord(message.sessionResumptionUpdate);
|
|
370
|
+
if (!sessionUpdate)
|
|
371
|
+
return;
|
|
372
|
+
const handle = typeof sessionUpdate.newHandle === 'string' && sessionUpdate.newHandle.length > 0
|
|
373
|
+
? sessionUpdate.newHandle
|
|
374
|
+
: undefined;
|
|
375
|
+
const resumable = typeof sessionUpdate.resumable === 'boolean' ? sessionUpdate.resumable : undefined;
|
|
376
|
+
if (handle === undefined && resumable === undefined)
|
|
377
|
+
return;
|
|
378
|
+
events.push({
|
|
379
|
+
type: 'evidence',
|
|
380
|
+
...(handle !== undefined ? { sessionResumptionHandle: handle } : {}),
|
|
381
|
+
evidence: {
|
|
382
|
+
provider: 'google',
|
|
383
|
+
kind: 'session_resumption',
|
|
384
|
+
resumable: resumable ?? handle !== undefined,
|
|
385
|
+
raw: sessionUpdate,
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
export function newLiveFold() {
|
|
390
|
+
return { calls: new Map() };
|
|
391
|
+
}
|
|
392
|
+
function foldToolCalls(message, fold, events) {
|
|
393
|
+
const calls = asRecord(message.toolCall)?.functionCalls;
|
|
394
|
+
if (!Array.isArray(calls))
|
|
395
|
+
return;
|
|
396
|
+
for (const value of calls) {
|
|
397
|
+
const call = asRecord(value) ?? {};
|
|
398
|
+
const id = typeof call.id === 'string' && call.id ? call.id : undefined;
|
|
399
|
+
const name = typeof call.name === 'string' ? call.name : '';
|
|
400
|
+
if (id && name)
|
|
401
|
+
fold.calls.set(id, name);
|
|
402
|
+
events.push(...toolCallEvents({ id, name }, call.args));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* `toolCallCancellation: { ids }` (probe 23/09/2026: gemini-3.1-flash-live and
|
|
407
|
+
* gemini-2.5-flash-native-audio on barge-in; gemini-3.8-live never sends it).
|
|
408
|
+
* It names only ids, so each cancel takes its name from the call this
|
|
409
|
+
* connection issued.
|
|
410
|
+
*/
|
|
411
|
+
function foldToolCancellations(message, fold, events) {
|
|
412
|
+
const ids = asRecord(message.toolCallCancellation)?.ids;
|
|
413
|
+
if (!Array.isArray(ids))
|
|
414
|
+
return;
|
|
415
|
+
for (const id of ids) {
|
|
416
|
+
const name = typeof id === 'string' ? fold.calls.get(id) : undefined;
|
|
417
|
+
if (typeof id !== 'string' || !name) {
|
|
418
|
+
// Every observed cancel names a call this connection issued; anything else is a wire change.
|
|
419
|
+
events.push(toErrorEvent(new TheoremError('bad_response', `Live cancelled a tool call it never issued: ${String(id)}`)));
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
fold.calls.delete(id);
|
|
423
|
+
events.push(toolEvent({ name, callId: id }, { phase: 'cancel' }));
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* `voiceActivity: { type: 'ACTIVITY_START' | 'ACTIVITY_END', audioOffset: '0.360s' }`
|
|
428
|
+
* (probe 23/09/2026: gemini-3.1-flash-live only), as `evidence` with the
|
|
429
|
+
* message as `raw`.
|
|
430
|
+
*/
|
|
431
|
+
function foldVoiceActivity(message, events) {
|
|
432
|
+
const voiceActivity = asRecord(message.voiceActivity);
|
|
433
|
+
if (!voiceActivity)
|
|
434
|
+
return;
|
|
435
|
+
events.push({
|
|
436
|
+
type: 'evidence',
|
|
437
|
+
evidence: { provider: 'google', kind: 'voice_activity', raw: voiceActivity },
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* `goAway.timeLeft` → milliseconds. It is a protobuf Duration, which JSON
|
|
442
|
+
* encodes as seconds with an `s` suffix (`"10s"`, `"1.5s"`).
|
|
443
|
+
*/
|
|
444
|
+
export function parseGoAwayTimeLeftMs(timeLeft) {
|
|
445
|
+
if (typeof timeLeft !== 'string')
|
|
446
|
+
return undefined;
|
|
447
|
+
const match = /^(\d+(?:\.\d+)?)s$/.exec(timeLeft);
|
|
448
|
+
return match?.[1] ? Math.round(Number(match[1]) * 1000) : undefined;
|
|
449
|
+
}
|
|
450
|
+
function foldGoAway(message, events) {
|
|
451
|
+
const goAway = asRecord(message.goAway);
|
|
452
|
+
if (!goAway)
|
|
453
|
+
return;
|
|
454
|
+
const timeLeftMs = parseGoAwayTimeLeftMs(goAway.timeLeft);
|
|
455
|
+
events.push({
|
|
456
|
+
type: 'session',
|
|
457
|
+
session: {
|
|
458
|
+
kind: 'closing_soon',
|
|
459
|
+
...(timeLeftMs !== undefined ? { timeLeftMs } : {}),
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Output audio arrives as `inlineData` with `mimeType: 'audio/pcm;rate=24000'`
|
|
465
|
+
* (probes 23/09/2026, every Live model); it becomes WAV at the stated rate.
|
|
466
|
+
* Thinking models mark reasoning text with `thought: true`.
|
|
467
|
+
*/
|
|
468
|
+
function foldModelPart(part, events) {
|
|
469
|
+
if (typeof part.text === 'string' && part.text) {
|
|
470
|
+
events.push({ type: part.thought === true ? 'thought' : 'text', text: part.text });
|
|
471
|
+
}
|
|
472
|
+
const inlineData = asRecord(part.inlineData);
|
|
473
|
+
const mimeType = inlineData?.mimeType;
|
|
474
|
+
const data = inlineData?.data;
|
|
475
|
+
if (typeof mimeType === 'string' && mimeType && typeof data === 'string' && data) {
|
|
476
|
+
events.push({ type: 'media', media: pcmMediaAsWav({ mimeType, data }) });
|
|
477
|
+
}
|
|
478
|
+
const codeExecutionResult = asRecord(part.codeExecutionResult);
|
|
479
|
+
if (codeExecutionResult) {
|
|
480
|
+
events.push(codeExecutionResultEvidence(codeExecutionResult));
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* `codeExecutionResult: { outcome, output }` (probe 23/09/2026:
|
|
485
|
+
* gemini-2.5-flash-native-audio reports each search / URL fetch this way, e.g.
|
|
486
|
+
* `OUTCOME_OK` / `Browsing the web.`). No `executableCode` part was seen on any
|
|
487
|
+
* Live model.
|
|
488
|
+
*/
|
|
489
|
+
function codeExecutionResultEvidence(result) {
|
|
490
|
+
return {
|
|
491
|
+
type: 'evidence',
|
|
492
|
+
evidence: {
|
|
493
|
+
provider: 'google',
|
|
494
|
+
kind: 'code_execution_result',
|
|
495
|
+
...(typeof result.output === 'string' ? { result: result.output } : {}),
|
|
496
|
+
...(typeof result.outcome === 'string' ? { isError: result.outcome !== 'OUTCOME_OK' } : {}),
|
|
497
|
+
raw: result,
|
|
498
|
+
},
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
function transcriptionText(value) {
|
|
502
|
+
const text = asRecord(value)?.text;
|
|
503
|
+
return typeof text === 'string' && text ? text : undefined;
|
|
504
|
+
}
|
|
505
|
+
function foldTranscription(value, kind, events, interim) {
|
|
506
|
+
const text = transcriptionText(value);
|
|
507
|
+
if (!text)
|
|
508
|
+
return;
|
|
509
|
+
events.push({
|
|
510
|
+
type: 'evidence',
|
|
511
|
+
text,
|
|
512
|
+
evidence: {
|
|
513
|
+
provider: 'google',
|
|
514
|
+
kind,
|
|
515
|
+
...(interim ? { interim: true } : {}),
|
|
516
|
+
},
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
function foldLiveGrounding(serverContent, events) {
|
|
520
|
+
events.push(...groundingFromLiveMetadata(serverContent.groundingMetadata));
|
|
521
|
+
const urlContext = asRecord(serverContent.urlContextMetadata);
|
|
522
|
+
if (urlContext) {
|
|
523
|
+
events.push({
|
|
524
|
+
type: 'evidence',
|
|
525
|
+
evidence: { provider: 'google', kind: 'url_context', raw: urlContext },
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* `serverContent.interactionStatus`, sent beside `turnComplete` by models that
|
|
531
|
+
* keep working after a turn (gemini-3.8-live-extended-thinking, probe
|
|
532
|
+
* 23/09/2026: `IN_PROGRESS` on each intermediate `turnComplete` of a tool
|
|
533
|
+
* flow, `IDLE` on the last).
|
|
534
|
+
*/
|
|
535
|
+
export function readLiveInteractionStatus(message) {
|
|
536
|
+
const raw = asRecord(message.serverContent)?.interactionStatus;
|
|
537
|
+
if (raw === 'IN_PROGRESS' || raw === 'IDLE')
|
|
538
|
+
return raw;
|
|
539
|
+
return undefined;
|
|
540
|
+
}
|
|
541
|
+
function foldInteractionStatus(message, events) {
|
|
542
|
+
const status = readLiveInteractionStatus(message);
|
|
543
|
+
if (!status)
|
|
544
|
+
return;
|
|
545
|
+
events.push({
|
|
546
|
+
type: 'session',
|
|
547
|
+
session: { kind: status === 'IDLE' ? 'idle' : 'working' },
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
function foldServerContent(message, events) {
|
|
551
|
+
const serverContent = asRecord(message.serverContent);
|
|
552
|
+
if (!serverContent)
|
|
553
|
+
return;
|
|
554
|
+
if (serverContent.interrupted === true) {
|
|
555
|
+
events.push({ type: 'done', interrupted: true, stop: { kind: 'interrupted' } });
|
|
556
|
+
}
|
|
557
|
+
if (serverContent.waitingForInput === true) {
|
|
558
|
+
events.push({ type: 'session', session: { kind: 'waiting_for_input' } });
|
|
559
|
+
}
|
|
560
|
+
if (serverContent.generationComplete === true) {
|
|
561
|
+
events.push({ type: 'done', stop: { kind: 'generation_complete' } });
|
|
562
|
+
}
|
|
563
|
+
foldTranscription(serverContent.inputTranscription, 'input_transcription', events);
|
|
564
|
+
foldTranscription(serverContent.interimInputTranscription, 'input_transcription', events, true);
|
|
565
|
+
foldTranscription(serverContent.outputTranscription, 'output_transcription', events);
|
|
566
|
+
const parts = asRecord(serverContent.modelTurn)?.parts;
|
|
567
|
+
for (const part of Array.isArray(parts) ? parts : []) {
|
|
568
|
+
const record = asRecord(part);
|
|
569
|
+
if (record)
|
|
570
|
+
foldModelPart(record, events);
|
|
571
|
+
}
|
|
572
|
+
foldLiveGrounding(serverContent, events);
|
|
573
|
+
if (serverContent.turnComplete === true) {
|
|
574
|
+
events.push({ type: 'session', session: { kind: 'turn_complete' } });
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
function foldUsageMetadata(message, events) {
|
|
578
|
+
const usageMetadata = asRecord(message.usageMetadata);
|
|
579
|
+
const tokens = usageMetadata ? extractLiveUsageTokens(usageMetadata) : undefined;
|
|
580
|
+
if (tokens) {
|
|
581
|
+
events.push({ type: 'tokens', tokens });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
export function foldGeminiLiveServerMessage(message, fold) {
|
|
585
|
+
if (!message || typeof message !== 'object')
|
|
586
|
+
return [];
|
|
587
|
+
const events = [];
|
|
588
|
+
foldGoAway(message, events);
|
|
589
|
+
foldSessionUpdate(message, events);
|
|
590
|
+
foldToolCalls(message, fold, events);
|
|
591
|
+
foldToolCancellations(message, fold, events);
|
|
592
|
+
foldVoiceActivity(message, events);
|
|
593
|
+
foldServerContent(message, events);
|
|
594
|
+
foldInteractionStatus(message, events);
|
|
595
|
+
foldUsageMetadata(message, events);
|
|
596
|
+
return events;
|
|
597
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gemini Live socket transport, opened by `runSession`; it applies no guardrails of its own.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import "../../../../_dnt.polyfills.js";
|
|
7
|
+
export { type GoogleLiveConnection, type OpenLiveWebSocket, openGoogleLiveSession, } from './session.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toGeminiOpenApiSchema(value: unknown): unknown;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// JSON Schema uses lowercase `object`/`string`; Gemini Live expects OpenAPI 3.0 `OBJECT`/`STRING`.
|
|
2
|
+
function convertGeminiSchemaType(value) {
|
|
3
|
+
if (typeof value === 'string')
|
|
4
|
+
return { type: value.toUpperCase() };
|
|
5
|
+
if (!Array.isArray(value))
|
|
6
|
+
return { type: toGeminiOpenApiSchema(value) };
|
|
7
|
+
const schemaTypes = value.filter((item) => typeof item === 'string');
|
|
8
|
+
const nonNullTypes = schemaTypes.filter((item) => item !== 'null');
|
|
9
|
+
const converted = {};
|
|
10
|
+
if (schemaTypes.includes('null'))
|
|
11
|
+
converted.nullable = true;
|
|
12
|
+
const [onlyType] = nonNullTypes;
|
|
13
|
+
if (onlyType !== undefined && nonNullTypes.length === 1)
|
|
14
|
+
converted.type = onlyType.toUpperCase();
|
|
15
|
+
if (nonNullTypes.length > 1) {
|
|
16
|
+
converted.anyOf = nonNullTypes.map((item) => ({ type: item.toUpperCase() }));
|
|
17
|
+
}
|
|
18
|
+
return converted;
|
|
19
|
+
}
|
|
20
|
+
function convertGeminiSchemaObject(obj) {
|
|
21
|
+
const result = {};
|
|
22
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
23
|
+
if (key === 'type') {
|
|
24
|
+
Object.assign(result, convertGeminiSchemaType(value));
|
|
25
|
+
}
|
|
26
|
+
else if (key === 'enum' && Array.isArray(value)) {
|
|
27
|
+
result.enum = value.filter((item) => item !== null);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
result[key] = toGeminiOpenApiSchema(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
export function toGeminiOpenApiSchema(value) {
|
|
36
|
+
if (Array.isArray(value))
|
|
37
|
+
return value.map(toGeminiOpenApiSchema);
|
|
38
|
+
if (value !== null && typeof value === 'object') {
|
|
39
|
+
return convertGeminiSchemaObject(value);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ProviderCompleteRequest } from '../../../kernel/types.js';
|
|
2
|
+
import type { GeminiTransport } from '../keys.js';
|
|
3
|
+
import { type SessionQueueItem } from './stream.js';
|
|
4
|
+
export interface GoogleLiveConnection {
|
|
5
|
+
readonly setup: Record<string, unknown>;
|
|
6
|
+
send(payload: Record<string, unknown>): void;
|
|
7
|
+
batches(): AsyncGenerator<SessionQueueItem>;
|
|
8
|
+
close(code?: number, reason?: string): void;
|
|
9
|
+
}
|
|
10
|
+
export type OpenLiveWebSocket = (url: string) => Promise<WebSocket>;
|
|
11
|
+
/** Tape row: setup on the pinned key was refused for quota, so the session opens on the fallback slot. */
|
|
12
|
+
export declare const LIVE_FALLBACK_ROW = "ws_fallback";
|
|
13
|
+
/** @param openWebSocket Host override, e.g. for a Cloudflare fetch-upgrade. */
|
|
14
|
+
export declare function openGoogleLiveSession(req: ProviderCompleteRequest, transport: GeminiTransport, openWebSocket?: OpenLiveWebSocket): Promise<GoogleLiveConnection>;
|