@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,90 @@
|
|
|
1
|
+
import { TheoremError } from '../guardrails/error.js';
|
|
2
|
+
import { requireModelProfile } from '../kernel/registry/resolve.js';
|
|
3
|
+
import { isValidPair } from '../kernel/schema.js';
|
|
4
|
+
import { markModuleLoad } from './probe.js';
|
|
5
|
+
export function isSpeechRole(profile) {
|
|
6
|
+
return profile.type === 'speech';
|
|
7
|
+
}
|
|
8
|
+
export function isImageRole(profile) {
|
|
9
|
+
return profile.type === 'image';
|
|
10
|
+
}
|
|
11
|
+
function bindingForProvider(input, modelId) {
|
|
12
|
+
const profile = requireModelProfile(input, 'createProvider');
|
|
13
|
+
const id = modelId ?? profile.defaultModel;
|
|
14
|
+
const binding = profile.models[id];
|
|
15
|
+
if (!binding) {
|
|
16
|
+
throw new TheoremError('config', `createProvider: profile '${profile.id}' has no model '${id}'`);
|
|
17
|
+
}
|
|
18
|
+
return binding;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Lazy-load an adapter on first `complete`. When `THEOREM_IMPORT_PROBE=1`,
|
|
22
|
+
* emits `LOADED:<label>` exactly once at load time (import-isolation tests).
|
|
23
|
+
*/
|
|
24
|
+
function lazyAdapter(label, load) {
|
|
25
|
+
let pending;
|
|
26
|
+
return {
|
|
27
|
+
async *complete(req) {
|
|
28
|
+
pending ??= (async () => {
|
|
29
|
+
markModuleLoad(label);
|
|
30
|
+
return await load();
|
|
31
|
+
})();
|
|
32
|
+
yield* (await pending).complete(req);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function lazyOpenRouterChat(config) {
|
|
37
|
+
return lazyAdapter('openrouter-chat', () => import('./openrouter/chat.js').then((m) => m.createOpenRouterProvider(config)));
|
|
38
|
+
}
|
|
39
|
+
function lazyGoogleInteractions(config) {
|
|
40
|
+
return lazyAdapter('google-interactions-adapter', () => import('./google/interactions/mod.js').then((m) => m.createInteractionsProvider(config)));
|
|
41
|
+
}
|
|
42
|
+
function lazySpeech(config) {
|
|
43
|
+
return lazyAdapter('openrouter-speech', () => import('./openrouter/speech.js').then((m) => m.createSpeechProvider(config)));
|
|
44
|
+
}
|
|
45
|
+
function lazyImage(config) {
|
|
46
|
+
return lazyAdapter('openrouter-image', () => import('./openrouter/image.js').then((m) => m.createImageProvider(config)));
|
|
47
|
+
}
|
|
48
|
+
function lazyLocal(config) {
|
|
49
|
+
return lazyAdapter('local-adapter', () => import('./local/local.js').then((m) => m.createLocalProvider(config)));
|
|
50
|
+
}
|
|
51
|
+
/** Live profiles use `runSession`. `modelId` picks the binding; default `defaultModel`. */
|
|
52
|
+
export function createProvider(profile, options = {}, modelId) {
|
|
53
|
+
const { protocol, provider } = bindingForProvider(profile, modelId);
|
|
54
|
+
if (!isValidPair(protocol, provider)) {
|
|
55
|
+
throw new TheoremError('config', `createProvider: unsupported protocol/provider pair '${protocol}'/'${provider}'`);
|
|
56
|
+
}
|
|
57
|
+
if (protocol === 'geminiInteractions' && provider === 'google') {
|
|
58
|
+
if (!options.vault) {
|
|
59
|
+
throw new TheoremError('config', 'createProvider requires a vault for google models');
|
|
60
|
+
}
|
|
61
|
+
return lazyGoogleInteractions({ ...options.gemini, vault: options.vault });
|
|
62
|
+
}
|
|
63
|
+
if (protocol === 'geminiLive' && provider === 'google') {
|
|
64
|
+
throw new TheoremError('request', "createProvider does not support type 'live' / geminiLive — use runSession(req, { vault })");
|
|
65
|
+
}
|
|
66
|
+
if (protocol === 'openAi' && provider === 'openrouter') {
|
|
67
|
+
if (!options.vault) {
|
|
68
|
+
throw new TheoremError('config', 'createProvider requires a vault for openrouter models');
|
|
69
|
+
}
|
|
70
|
+
const gateway = { ...options.openAiGateway, vault: options.vault };
|
|
71
|
+
if (isSpeechRole(profile)) {
|
|
72
|
+
return lazySpeech(gateway);
|
|
73
|
+
}
|
|
74
|
+
if (isImageRole(profile)) {
|
|
75
|
+
return lazyImage(gateway);
|
|
76
|
+
}
|
|
77
|
+
return lazyOpenRouterChat(gateway);
|
|
78
|
+
}
|
|
79
|
+
if (protocol === 'openAi' && provider === 'local') {
|
|
80
|
+
if (isImageRole(profile)) {
|
|
81
|
+
throw new TheoremError('config', 'createProvider: type image requires openrouter provider for openAi protocol');
|
|
82
|
+
}
|
|
83
|
+
// A profile can name `local`; only the host can say a local server is there to reach.
|
|
84
|
+
if (!options.local) {
|
|
85
|
+
throw new TheoremError('config', 'createProvider requires local config for openAi/local');
|
|
86
|
+
}
|
|
87
|
+
return lazyLocal({ ...options.local, vault: options.vault });
|
|
88
|
+
}
|
|
89
|
+
throw new TheoremError('config', `createProvider: unsupported protocol/provider pair '${protocol}'/'${provider}'`);
|
|
90
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { DecisionModelBinding, DecisionResult } from '../../kernel/types.js';
|
|
2
|
+
/** Provider-reported usage, with Jev's published tariff only for a direct Jev response. */
|
|
3
|
+
export declare function decisionUsage(usage: unknown, binding: Pick<DecisionModelBinding, 'provider' | 'apiId'>, responseModel: string): DecisionResult['usage'];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isRecord } from '../../kernel/util/record.js';
|
|
2
|
+
import { JEV_USD_PER_MILLION_INPUT_TOKENS } from '../../presets/typesafe.js';
|
|
3
|
+
/** Provider-reported usage, with Jev's published tariff only for a direct Jev response. */
|
|
4
|
+
export function decisionUsage(usage, binding, responseModel) {
|
|
5
|
+
if (!isRecord(usage) ||
|
|
6
|
+
!Number.isSafeInteger(usage.input_tokens) ||
|
|
7
|
+
!Number.isSafeInteger(usage.output_tokens) ||
|
|
8
|
+
usage.input_tokens < 0 ||
|
|
9
|
+
usage.output_tokens < 0)
|
|
10
|
+
return undefined;
|
|
11
|
+
const inputTokens = usage.input_tokens;
|
|
12
|
+
const reported = typeof usage.cost === 'number' && Number.isFinite(usage.cost) && usage.cost >= 0
|
|
13
|
+
? usage.cost
|
|
14
|
+
: undefined;
|
|
15
|
+
const directJev = binding.provider === 'typesafe' &&
|
|
16
|
+
binding.apiId.startsWith('jev-') &&
|
|
17
|
+
responseModel.startsWith('jev-');
|
|
18
|
+
const costUsd = reported ??
|
|
19
|
+
(directJev ? (inputTokens * JEV_USD_PER_MILLION_INPUT_TOKENS) / 1_000_000 : undefined);
|
|
20
|
+
return {
|
|
21
|
+
inputTokens,
|
|
22
|
+
outputTokens: usage.output_tokens,
|
|
23
|
+
...(costUsd === undefined ? {} : { costUsd }),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { TheoremError } from '../../guardrails/error.js';
|
|
2
|
+
/**
|
|
3
|
+
* Interactions (SSE `error` rows, non-OK bodies) and Live (error frames) both send
|
|
4
|
+
* `{ error: { code, message, status } }`; `code` is an HTTP status or a named code.
|
|
5
|
+
*/
|
|
6
|
+
export declare function readGeminiApiError(record: Record<string, unknown>): TheoremError | null;
|
|
7
|
+
/** The kind is the HTTP status's, unless the body names a refused key. */
|
|
8
|
+
export declare function readNonOkError(response: Response): Promise<TheoremError>;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { kindOfHttpStatus, TheoremError } from '../../guardrails/error.js';
|
|
2
|
+
import { asRecord } from '../../kernel/engine/record.js';
|
|
3
|
+
const HTTP_STATUS_MIN = 100;
|
|
4
|
+
const HTTP_STATUS_MAX = 599;
|
|
5
|
+
/**
|
|
6
|
+
* Google refuses a bad key as a 400 `INVALID_ARGUMENT`, so the status reads as
|
|
7
|
+
* a request THEOREM cannot make; the `ErrorInfo` reason is what says it is the
|
|
8
|
+
* key. An expired key carries the same reason.
|
|
9
|
+
*/
|
|
10
|
+
const AUTH_REASONS = new Set(['API_KEY_INVALID']);
|
|
11
|
+
/** Whether an error's `details` name a key Google refused. */
|
|
12
|
+
function refusesKey(details) {
|
|
13
|
+
return (Array.isArray(details) &&
|
|
14
|
+
details.some((detail) => {
|
|
15
|
+
const reason = asRecord(detail)?.reason;
|
|
16
|
+
return typeof reason === 'string' && AUTH_REASONS.has(reason);
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The HTTP status behind each named code Google puts on an Interactions error
|
|
21
|
+
* (https://ai.google.dev/gemini-api/docs/api-errors). A code it does not list
|
|
22
|
+
* is the snake_case name of the HTTP status, so those names are here too.
|
|
23
|
+
*/
|
|
24
|
+
const NAMED_CODE_STATUS = {
|
|
25
|
+
invalid_request: 400,
|
|
26
|
+
failed_precondition: 400,
|
|
27
|
+
parameter_unknown: 400,
|
|
28
|
+
authentication: 401,
|
|
29
|
+
payment_required: 402,
|
|
30
|
+
permission_denied: 403,
|
|
31
|
+
not_found: 404,
|
|
32
|
+
model_not_found: 404,
|
|
33
|
+
already_exists: 409,
|
|
34
|
+
aborted: 409,
|
|
35
|
+
out_of_range: 416,
|
|
36
|
+
rate_limit_exceeded: 429,
|
|
37
|
+
quota_exceeded: 429,
|
|
38
|
+
too_many_requests: 429,
|
|
39
|
+
api_error: 500,
|
|
40
|
+
unimplemented: 501,
|
|
41
|
+
service_unavailable: 503,
|
|
42
|
+
deadline_exceeded: 504,
|
|
43
|
+
bad_request: 400,
|
|
44
|
+
unauthorized: 401,
|
|
45
|
+
forbidden: 403,
|
|
46
|
+
method_not_allowed: 405,
|
|
47
|
+
request_timeout: 408,
|
|
48
|
+
conflict: 409,
|
|
49
|
+
payload_too_large: 413,
|
|
50
|
+
unprocessable_entity: 422,
|
|
51
|
+
internal_server_error: 500,
|
|
52
|
+
not_implemented: 501,
|
|
53
|
+
bad_gateway: 502,
|
|
54
|
+
gateway_timeout: 504,
|
|
55
|
+
};
|
|
56
|
+
/** Codes for a generation Google's safety filters held back. */
|
|
57
|
+
const BLOCKED_CODES = new Set([
|
|
58
|
+
'safety',
|
|
59
|
+
'recitation',
|
|
60
|
+
'language',
|
|
61
|
+
'prohibited_content',
|
|
62
|
+
'spii',
|
|
63
|
+
'blocklist',
|
|
64
|
+
'image_safety',
|
|
65
|
+
'image_prohibited_content',
|
|
66
|
+
'image_recitation',
|
|
67
|
+
'image_other',
|
|
68
|
+
'content_blocked',
|
|
69
|
+
]);
|
|
70
|
+
/**
|
|
71
|
+
* `cancelled` (499) is the request stopped on THEOREM's side. Generation error
|
|
72
|
+
* codes (`malformed_function_call`, `no_image`, …) and any code Google adds later
|
|
73
|
+
* are `bad_response`.
|
|
74
|
+
*/
|
|
75
|
+
function kindOfNamedCode(code) {
|
|
76
|
+
if (code === 'cancelled')
|
|
77
|
+
return 'cancelled';
|
|
78
|
+
if (BLOCKED_CODES.has(code))
|
|
79
|
+
return 'safety';
|
|
80
|
+
const status = NAMED_CODE_STATUS[code];
|
|
81
|
+
return status === undefined ? 'bad_response' : kindOfHttpStatus(status);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Interactions (SSE `error` rows, non-OK bodies) and Live (error frames) both send
|
|
85
|
+
* `{ error: { code, message, status } }`; `code` is an HTTP status or a named code.
|
|
86
|
+
*/
|
|
87
|
+
export function readGeminiApiError(record) {
|
|
88
|
+
const error = asRecord(record.error);
|
|
89
|
+
if (!error) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const { code, message, status, details } = error;
|
|
93
|
+
const kind = refusesKey(details)
|
|
94
|
+
? 'auth'
|
|
95
|
+
: typeof code === 'number' && code >= HTTP_STATUS_MIN && code <= HTTP_STATUS_MAX
|
|
96
|
+
? kindOfHttpStatus(code)
|
|
97
|
+
: typeof code === 'string'
|
|
98
|
+
? kindOfNamedCode(code)
|
|
99
|
+
: 'bad_response';
|
|
100
|
+
if (typeof message !== 'string' || message.length === 0) {
|
|
101
|
+
return new TheoremError(kind, 'Gemini returned an error.');
|
|
102
|
+
}
|
|
103
|
+
return new TheoremError(kind, typeof status === 'string' ? `${status}: ${message}` : message);
|
|
104
|
+
}
|
|
105
|
+
/** The kind is the HTTP status's, unless the body names a refused key. */
|
|
106
|
+
export async function readNonOkError(response) {
|
|
107
|
+
const kind = kindOfHttpStatus(response.status);
|
|
108
|
+
const text = await response.text().catch(() => '');
|
|
109
|
+
if (!text.trim()) {
|
|
110
|
+
return new TheoremError(kind, `HTTP ${response.status}`);
|
|
111
|
+
}
|
|
112
|
+
const parsed = parseRecord(text);
|
|
113
|
+
const stated = parsed ? readGeminiApiError(parsed) : null;
|
|
114
|
+
return new TheoremError(stated?.kind === 'auth' ? 'auth' : kind, stated?.message ?? `Gemini HTTP ${response.status}: ${text}`);
|
|
115
|
+
}
|
|
116
|
+
function parseRecord(text) {
|
|
117
|
+
try {
|
|
118
|
+
return asRecord(JSON.parse(text));
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ProviderEvent } from '../../kernel/types.js';
|
|
2
|
+
declare function groundingFromDelta(event: Record<string, unknown>): ProviderEvent[];
|
|
3
|
+
declare function groundingFromSteps(steps: unknown[]): ProviderEvent[];
|
|
4
|
+
declare function groundingFromLiveMetadata(value: unknown): ProviderEvent[];
|
|
5
|
+
export { groundingFromDelta, groundingFromLiveMetadata, groundingFromSteps };
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { asRecord, nonEmptyString } from '../../kernel/engine/record.js';
|
|
2
|
+
function groundedEvents(grounded) {
|
|
3
|
+
if (!grounded)
|
|
4
|
+
return [];
|
|
5
|
+
const { grounding, sources } = grounded;
|
|
6
|
+
return [
|
|
7
|
+
{ type: 'grounding', grounding },
|
|
8
|
+
...(sources.length > 0 ? [{ type: 'citation', sources }] : []),
|
|
9
|
+
];
|
|
10
|
+
}
|
|
11
|
+
function sourceFromPlace(place) {
|
|
12
|
+
const uri = nonEmptyString(place.url);
|
|
13
|
+
if (!uri) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const placeId = nonEmptyString(place.place_id);
|
|
17
|
+
return {
|
|
18
|
+
type: 'maps',
|
|
19
|
+
uri,
|
|
20
|
+
title: nonEmptyString(place.name) ?? uri,
|
|
21
|
+
...(placeId ? { placeId } : {}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function chunkFromMapsSource(source) {
|
|
25
|
+
return {
|
|
26
|
+
maps: {
|
|
27
|
+
title: source.title,
|
|
28
|
+
uri: source.uri,
|
|
29
|
+
...(source.placeId ? { placeId: source.placeId } : {}),
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function sourceFromWeb(web) {
|
|
34
|
+
const uri = nonEmptyString(web.uri);
|
|
35
|
+
if (!uri) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
return { type: 'web', uri, title: nonEmptyString(web.title) ?? uri };
|
|
39
|
+
}
|
|
40
|
+
function pushUniqueSource(sources, source) {
|
|
41
|
+
if (!source) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (sources.some((item) => {
|
|
45
|
+
if (item.type !== source.type) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (source.placeId && item.placeId && source.placeId === item.placeId) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return item.uri === source.uri;
|
|
52
|
+
})) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
sources.push(source);
|
|
56
|
+
}
|
|
57
|
+
function pushUniqueChunk(chunks, chunk) {
|
|
58
|
+
const maps = asRecord(asRecord(chunk)?.maps);
|
|
59
|
+
if (!maps) {
|
|
60
|
+
chunks.push(chunk);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const placeId = nonEmptyString(maps.placeId);
|
|
64
|
+
const uri = nonEmptyString(maps.uri);
|
|
65
|
+
if (chunks.some((existing) => {
|
|
66
|
+
const existingMaps = asRecord(asRecord(existing)?.maps);
|
|
67
|
+
if (!existingMaps) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
const existingId = nonEmptyString(existingMaps.placeId);
|
|
71
|
+
if (placeId && existingId && placeId === existingId) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return Boolean(uri && nonEmptyString(existingMaps.uri) === uri);
|
|
75
|
+
})) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
chunks.push(chunk);
|
|
79
|
+
}
|
|
80
|
+
function searchSuggestionsHtml(result) {
|
|
81
|
+
if (!Array.isArray(result)) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
for (const entry of result) {
|
|
85
|
+
const html = nonEmptyString(asRecord(entry)?.search_suggestions);
|
|
86
|
+
if (html) {
|
|
87
|
+
return html;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
function sourceFromAnnotation(ann) {
|
|
93
|
+
const record = asRecord(ann);
|
|
94
|
+
const uri = nonEmptyString(record?.url);
|
|
95
|
+
if (!record || !uri) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
if (record.type === 'place_citation') {
|
|
99
|
+
const title = nonEmptyString(record.name) ?? uri;
|
|
100
|
+
const placeId = nonEmptyString(record.place_id);
|
|
101
|
+
return { type: 'maps', uri, title, ...(placeId ? { placeId } : {}) };
|
|
102
|
+
}
|
|
103
|
+
if (record.type === 'url_citation') {
|
|
104
|
+
return { type: 'web', uri, title: nonEmptyString(record.title) ?? uri };
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
function appendAnnotationSources(into, annotations) {
|
|
109
|
+
if (!Array.isArray(annotations)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
for (const ann of annotations) {
|
|
113
|
+
pushUniqueSource(into, sourceFromAnnotation(ann));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function appendPlaceSources(into, result) {
|
|
117
|
+
if (!Array.isArray(result)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
for (const entry of result) {
|
|
121
|
+
const places = asRecord(entry)?.places;
|
|
122
|
+
if (!Array.isArray(places)) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
for (const placeValue of places) {
|
|
126
|
+
const place = asRecord(placeValue);
|
|
127
|
+
const source = place ? sourceFromPlace(place) : undefined;
|
|
128
|
+
if (source) {
|
|
129
|
+
pushUniqueSource(into, source);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Maps sources also emit normalized `chunks[].maps` so hosts share one parse shape with Live. */
|
|
135
|
+
function groundingFromInteractionsStep(step) {
|
|
136
|
+
const sources = [];
|
|
137
|
+
appendAnnotationSources(sources, step.annotations);
|
|
138
|
+
if (Array.isArray(step.content)) {
|
|
139
|
+
for (const block of step.content) {
|
|
140
|
+
appendAnnotationSources(sources, asRecord(block)?.annotations);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
appendPlaceSources(sources, step.result);
|
|
144
|
+
const chunks = [];
|
|
145
|
+
for (const source of sources) {
|
|
146
|
+
if (source.type === 'maps') {
|
|
147
|
+
pushUniqueChunk(chunks, chunkFromMapsSource(source));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const html = searchSuggestionsHtml(step.result);
|
|
151
|
+
if (!html && sources.length === 0) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
grounding: {
|
|
156
|
+
metadata: step,
|
|
157
|
+
...(chunks.length > 0 ? { chunks } : {}),
|
|
158
|
+
...(html ? { searchHtml: html } : {}),
|
|
159
|
+
},
|
|
160
|
+
sources,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function mergeGrounding(a, b) {
|
|
164
|
+
if (!a) {
|
|
165
|
+
return b;
|
|
166
|
+
}
|
|
167
|
+
if (!b) {
|
|
168
|
+
return a;
|
|
169
|
+
}
|
|
170
|
+
const chunks = [];
|
|
171
|
+
for (const chunk of [...(a.grounding.chunks ?? []), ...(b.grounding.chunks ?? [])]) {
|
|
172
|
+
pushUniqueChunk(chunks, chunk);
|
|
173
|
+
}
|
|
174
|
+
const sources = [...a.sources];
|
|
175
|
+
for (const source of b.sources) {
|
|
176
|
+
pushUniqueSource(sources, source);
|
|
177
|
+
}
|
|
178
|
+
const searchHtml = b.grounding.searchHtml ?? a.grounding.searchHtml;
|
|
179
|
+
const metadata = b.grounding.metadata ?? a.grounding.metadata;
|
|
180
|
+
return {
|
|
181
|
+
grounding: {
|
|
182
|
+
...(metadata ? { metadata } : {}),
|
|
183
|
+
...(chunks.length > 0 ? { chunks } : {}),
|
|
184
|
+
...(searchHtml ? { searchHtml } : {}),
|
|
185
|
+
},
|
|
186
|
+
sources,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function groundingFromDelta(event) {
|
|
190
|
+
const delta = asRecord(event.delta);
|
|
191
|
+
return groundedEvents(delta ? groundingFromInteractionsStep(delta) : undefined);
|
|
192
|
+
}
|
|
193
|
+
function groundingFromSteps(steps) {
|
|
194
|
+
let grounded;
|
|
195
|
+
for (const stepValue of steps) {
|
|
196
|
+
const step = asRecord(stepValue);
|
|
197
|
+
if (step) {
|
|
198
|
+
grounded = mergeGrounding(grounded, groundingFromInteractionsStep(step));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return groundedEvents(grounded);
|
|
202
|
+
}
|
|
203
|
+
function groundingFromLiveMetadata(value) {
|
|
204
|
+
const metadata = asRecord(value);
|
|
205
|
+
if (!metadata) {
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
const chunks = Array.isArray(metadata.groundingChunks) ? metadata.groundingChunks : [];
|
|
209
|
+
const sources = [];
|
|
210
|
+
for (const chunk of chunks) {
|
|
211
|
+
const web = asRecord(asRecord(chunk)?.web);
|
|
212
|
+
if (web) {
|
|
213
|
+
pushUniqueSource(sources, sourceFromWeb(web));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const html = nonEmptyString(asRecord(metadata.searchEntryPoint)?.renderedContent);
|
|
217
|
+
return groundedEvents({
|
|
218
|
+
grounding: {
|
|
219
|
+
metadata,
|
|
220
|
+
...(chunks.length > 0 ? { chunks } : {}),
|
|
221
|
+
...(html ? { searchHtml: html } : {}),
|
|
222
|
+
},
|
|
223
|
+
sources,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
export { groundingFromDelta, groundingFromLiveMetadata, groundingFromSteps };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { InteractionPart, ProviderCompleteRequest, TurnHistoryMessage } from '../../../kernel/types.js';
|
|
2
|
+
export declare function camelToSnake(key: string): string;
|
|
3
|
+
export declare function toGoogleValue(value: unknown): unknown;
|
|
4
|
+
export declare function wirePart(part: InteractionPart): Record<string, string>;
|
|
5
|
+
export declare function userInputStep(parts: InteractionPart[]): Record<string, unknown>;
|
|
6
|
+
/** Assistant `tool_calls` (often with no `content`) become `function_call` steps, never empty `model_output`. */
|
|
7
|
+
export declare function historySteps(msg: TurnHistoryMessage): Record<string, unknown>[];
|
|
8
|
+
export declare function historyStep(msg: TurnHistoryMessage): Record<string, unknown>;
|
|
9
|
+
export declare function jsonResponseFormat(schema: Record<string, unknown>): unknown[];
|
|
10
|
+
export declare function attachResponseFormat(req: ProviderCompleteRequest, camel: Record<string, unknown>): void;
|
|
11
|
+
export declare function attachSpeechConfig(req: ProviderCompleteRequest, generationConfig: Record<string, unknown>): void;
|
|
12
|
+
export declare function inputStepsFromRequest(req: ProviderCompleteRequest): Record<string, unknown>[];
|
|
13
|
+
export declare function applyOptionalRequestFields(req: ProviderCompleteRequest, camel: Record<string, unknown>): void;
|
|
14
|
+
export declare function baseInteractionsBody(req: ProviderCompleteRequest): Record<string, unknown>;
|
|
15
|
+
export declare function toInteractionsBody(req: ProviderCompleteRequest): Record<string, unknown>;
|