@theoremjs/agents 0.0.0-stage → 0.3.1
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 +545 -2
- package/esm/_dnt.polyfills.d.ts +66 -0
- package/esm/_dnt.polyfills.js +51 -0
- package/esm/mod.d.ts +79 -0
- package/esm/mod.js +49 -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 +480 -0
- package/esm/src/cli/commands/detect-compile.d.ts +17 -0
- package/esm/src/cli/commands/detect-compile.js +22 -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 +241 -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 +26 -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 +67 -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 +5 -0
- package/esm/src/evals/attachments.js +55 -0
- package/esm/src/evals/breakdown.d.ts +30 -0
- package/esm/src/evals/breakdown.js +69 -0
- package/esm/src/evals/graders/answer.d.ts +16 -0
- package/esm/src/evals/graders/answer.js +134 -0
- package/esm/src/evals/graders/code.d.ts +40 -0
- package/esm/src/evals/graders/code.js +184 -0
- package/esm/src/evals/graders/judge.d.ts +24 -0
- package/esm/src/evals/graders/judge.js +367 -0
- package/esm/src/evals/graders/latency.d.ts +6 -0
- package/esm/src/evals/graders/latency.js +82 -0
- package/esm/src/evals/graders/live.d.ts +14 -0
- package/esm/src/evals/graders/live.js +42 -0
- package/esm/src/evals/graders/media.d.ts +18 -0
- package/esm/src/evals/graders/media.js +91 -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 +19 -0
- package/esm/src/evals/graders/transcript.js +202 -0
- package/esm/src/evals/mod.d.ts +34 -0
- package/esm/src/evals/mod.js +22 -0
- package/esm/src/evals/record.d.ts +41 -0
- package/esm/src/evals/record.js +107 -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 +75 -0
- package/esm/src/evals/run.js +418 -0
- package/esm/src/evals/suite.d.ts +28 -0
- package/esm/src/evals/suite.js +97 -0
- package/esm/src/evals/summary.d.ts +35 -0
- package/esm/src/evals/summary.js +31 -0
- package/esm/src/evals/trial.d.ts +14 -0
- package/esm/src/evals/trial.js +135 -0
- package/esm/src/evals/types.d.ts +169 -0
- package/esm/src/evals/types.js +113 -0
- package/esm/src/evals/verdict.d.ts +23 -0
- package/esm/src/evals/verdict.js +48 -0
- package/esm/src/guardrails/boundaries.d.ts +41 -0
- package/esm/src/guardrails/boundaries.js +103 -0
- package/esm/src/guardrails/canary-gate.d.ts +22 -0
- package/esm/src/guardrails/canary-gate.js +40 -0
- package/esm/src/guardrails/canary.d.ts +146 -0
- package/esm/src/guardrails/canary.js +974 -0
- package/esm/src/guardrails/compile-egress.d.ts +12 -0
- package/esm/src/guardrails/compile-egress.js +12 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +24 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.js +305 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.d.ts +5 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.js +199 -0
- package/esm/src/guardrails/corpus/inbound-payloads.d.ts +5 -0
- package/esm/src/guardrails/corpus/inbound-payloads.js +120 -0
- package/esm/src/guardrails/corpus/live-attacks.d.ts +15 -0
- package/esm/src/guardrails/corpus/live-attacks.js +229 -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 +36 -0
- package/esm/src/guardrails/corpus/types.js +1 -0
- package/esm/src/guardrails/credential-rules.d.ts +36 -0
- package/esm/src/guardrails/credential-rules.js +3001 -0
- package/esm/src/guardrails/credential-scan.d.ts +45 -0
- package/esm/src/guardrails/credential-scan.js +172 -0
- package/esm/src/guardrails/destinations.d.ts +45 -0
- package/esm/src/guardrails/destinations.js +234 -0
- package/esm/src/guardrails/detect-at.d.ts +107 -0
- package/esm/src/guardrails/detect-at.js +434 -0
- package/esm/src/guardrails/detect-reply.d.ts +61 -0
- package/esm/src/guardrails/detect-reply.js +95 -0
- package/esm/src/guardrails/detect-stream.d.ts +33 -0
- package/esm/src/guardrails/detect-stream.js +77 -0
- package/esm/src/guardrails/detectors.d.ts +216 -0
- package/esm/src/guardrails/detectors.js +503 -0
- package/esm/src/guardrails/egress-automata.d.ts +12 -0
- package/esm/src/guardrails/egress-automata.js +25582 -0
- package/esm/src/guardrails/egress-compiler.d.ts +55 -0
- package/esm/src/guardrails/egress-compiler.js +431 -0
- package/esm/src/guardrails/egress-patterns.d.ts +26 -0
- package/esm/src/guardrails/egress-patterns.js +36 -0
- package/esm/src/guardrails/egress-stream.d.ts +123 -0
- package/esm/src/guardrails/egress-stream.js +774 -0
- package/esm/src/guardrails/egress-urls.d.ts +86 -0
- package/esm/src/guardrails/egress-urls.js +909 -0
- package/esm/src/guardrails/egress.d.ts +46 -0
- package/esm/src/guardrails/egress.js +124 -0
- package/esm/src/guardrails/error.d.ts +31 -0
- package/esm/src/guardrails/error.js +96 -0
- package/esm/src/guardrails/event-schemas.d.ts +100 -0
- package/esm/src/guardrails/event-schemas.js +48 -0
- package/esm/src/guardrails/events.d.ts +13 -0
- package/esm/src/guardrails/events.js +45 -0
- package/esm/src/guardrails/hints.d.ts +14 -0
- package/esm/src/guardrails/hints.js +57 -0
- package/esm/src/guardrails/hits.d.ts +14 -0
- package/esm/src/guardrails/hits.js +30 -0
- package/esm/src/guardrails/host-patterns.d.ts +85 -0
- package/esm/src/guardrails/host-patterns.js +173 -0
- package/esm/src/guardrails/injection-patterns.d.ts +20 -0
- package/esm/src/guardrails/injection-patterns.js +126 -0
- package/esm/src/guardrails/injection-reversed.d.ts +9 -0
- package/esm/src/guardrails/injection-reversed.js +49 -0
- package/esm/src/guardrails/injection.d.ts +30 -0
- package/esm/src/guardrails/injection.js +460 -0
- package/esm/src/guardrails/labels.d.ts +40 -0
- package/esm/src/guardrails/labels.js +874 -0
- package/esm/src/guardrails/live-outbound-gate.d.ts +55 -0
- package/esm/src/guardrails/live-outbound-gate.js +298 -0
- package/esm/src/guardrails/mod.d.ts +54 -0
- package/esm/src/guardrails/mod.js +34 -0
- package/esm/src/guardrails/network.d.ts +46 -0
- package/esm/src/guardrails/network.js +330 -0
- package/esm/src/guardrails/normalize.d.ts +20 -0
- package/esm/src/guardrails/normalize.js +165 -0
- package/esm/src/guardrails/policy.d.ts +4 -0
- package/esm/src/guardrails/policy.js +21 -0
- package/esm/src/guardrails/progressive-yield.d.ts +110 -0
- package/esm/src/guardrails/progressive-yield.js +281 -0
- package/esm/src/guardrails/prompt-echo.d.ts +40 -0
- package/esm/src/guardrails/prompt-echo.js +306 -0
- package/esm/src/guardrails/quota.d.ts +21 -0
- package/esm/src/guardrails/quota.js +80 -0
- package/esm/src/guardrails/rules.d.ts +47 -0
- package/esm/src/guardrails/rules.js +47 -0
- package/esm/src/guardrails/sanitize.d.ts +40 -0
- package/esm/src/guardrails/sanitize.js +160 -0
- package/esm/src/guardrails/sensitive-groups.d.ts +21 -0
- package/esm/src/guardrails/sensitive-groups.js +12 -0
- package/esm/src/guardrails/sensitive.d.ts +22 -0
- package/esm/src/guardrails/sensitive.js +234 -0
- package/esm/src/guardrails/serialize.d.ts +15 -0
- package/esm/src/guardrails/serialize.js +37 -0
- package/esm/src/guardrails/testing.d.ts +11 -0
- package/esm/src/guardrails/testing.js +10 -0
- package/esm/src/guardrails/theorem-error.d.ts +21 -0
- package/esm/src/guardrails/theorem-error.js +54 -0
- package/esm/src/guardrails/thought-guard.d.ts +76 -0
- package/esm/src/guardrails/thought-guard.js +664 -0
- package/esm/src/guardrails/tool-directives.d.ts +38 -0
- package/esm/src/guardrails/tool-directives.js +195 -0
- package/esm/src/guardrails/tool-leak.d.ts +26 -0
- package/esm/src/guardrails/tool-leak.js +45 -0
- package/esm/src/guardrails/tool-result.d.ts +92 -0
- package/esm/src/guardrails/tool-result.js +272 -0
- package/esm/src/guardrails/types.d.ts +250 -0
- package/esm/src/guardrails/types.js +58 -0
- package/esm/src/host/client-turn.d.ts +36 -0
- package/esm/src/host/client-turn.js +80 -0
- package/esm/src/host/mint-trace.d.ts +29 -0
- package/esm/src/host/mint-trace.js +60 -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 +27 -0
- package/esm/src/host/reply.d.ts +13 -0
- package/esm/src/host/reply.js +41 -0
- package/esm/src/interface/blocks.d.ts +11 -0
- package/esm/src/interface/blocks.js +283 -0
- package/esm/src/interface/composer-actions.d.ts +24 -0
- package/esm/src/interface/composer-actions.js +33 -0
- package/esm/src/interface/draft.d.ts +20 -0
- package/esm/src/interface/draft.js +39 -0
- package/esm/src/interface/from-profile.d.ts +24 -0
- package/esm/src/interface/from-profile.js +179 -0
- package/esm/src/interface/history.d.ts +46 -0
- package/esm/src/interface/history.js +278 -0
- package/esm/src/interface/inputs.d.ts +11 -0
- package/esm/src/interface/inputs.js +67 -0
- package/esm/src/interface/mod.d.ts +33 -0
- package/esm/src/interface/mod.js +22 -0
- package/esm/src/interface/models.d.ts +26 -0
- package/esm/src/interface/models.js +50 -0
- package/esm/src/interface/pending.d.ts +57 -0
- package/esm/src/interface/pending.js +152 -0
- package/esm/src/interface/profile-interface.d.ts +4 -0
- package/esm/src/interface/profile-interface.js +237 -0
- package/esm/src/interface/session.d.ts +71 -0
- package/esm/src/interface/session.js +128 -0
- package/esm/src/interface/stop.d.ts +13 -0
- package/esm/src/interface/stop.js +28 -0
- package/esm/src/interface/tool-calls.d.ts +32 -0
- package/esm/src/interface/tool-calls.js +127 -0
- package/esm/src/interface/tool-invoke.d.ts +6 -0
- package/esm/src/interface/tool-invoke.js +22 -0
- package/esm/src/interface/tool-media.d.ts +11 -0
- package/esm/src/interface/tool-media.js +113 -0
- package/esm/src/interface/types.d.ts +278 -0
- package/esm/src/interface/types.js +5 -0
- package/esm/src/kernel/api-catalog.d.ts +85 -0
- package/esm/src/kernel/api-catalog.js +229 -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 +56 -0
- package/esm/src/kernel/auth/crypto.js +163 -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 +18 -0
- package/esm/src/kernel/auth/oauth.js +439 -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 +152 -0
- package/esm/src/kernel/auth/types.js +1 -0
- package/esm/src/kernel/checkpoint.d.ts +43 -0
- package/esm/src/kernel/checkpoint.js +58 -0
- package/esm/src/kernel/default-scope.d.ts +72 -0
- package/esm/src/kernel/default-scope.js +121 -0
- package/esm/src/kernel/engine/call-trace.d.ts +19 -0
- package/esm/src/kernel/engine/call-trace.js +29 -0
- package/esm/src/kernel/engine/compaction.d.ts +71 -0
- package/esm/src/kernel/engine/compaction.js +203 -0
- package/esm/src/kernel/engine/decision.d.ts +43 -0
- package/esm/src/kernel/engine/decision.js +439 -0
- package/esm/src/kernel/engine/embedding.d.ts +8 -0
- package/esm/src/kernel/engine/embedding.js +118 -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 +19 -0
- package/esm/src/kernel/engine/live-inbound.js +44 -0
- package/esm/src/kernel/engine/live-ingress.d.ts +15 -0
- package/esm/src/kernel/engine/live-ingress.js +42 -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 +508 -0
- package/esm/src/kernel/engine/runner/mod.d.ts +29 -0
- package/esm/src/kernel/engine/runner/mod.js +625 -0
- package/esm/src/kernel/engine/runner/nested.d.ts +32 -0
- package/esm/src/kernel/engine/runner/nested.js +134 -0
- package/esm/src/kernel/engine/runner/schema-validation.d.ts +35 -0
- package/esm/src/kernel/engine/runner/schema-validation.js +179 -0
- package/esm/src/kernel/engine/runner/stages.d.ts +28 -0
- package/esm/src/kernel/engine/runner/stages.js +41 -0
- package/esm/src/kernel/engine/runner/state.d.ts +97 -0
- package/esm/src/kernel/engine/runner/state.js +50 -0
- package/esm/src/kernel/engine/runner/step-order.d.ts +27 -0
- package/esm/src/kernel/engine/runner/step-order.js +165 -0
- package/esm/src/kernel/engine/runner/steps.d.ts +14 -0
- package/esm/src/kernel/engine/runner/steps.js +519 -0
- package/esm/src/kernel/engine/runner/stream.d.ts +71 -0
- package/esm/src/kernel/engine/runner/stream.js +376 -0
- package/esm/src/kernel/engine/runner/usage.d.ts +45 -0
- package/esm/src/kernel/engine/runner/usage.js +148 -0
- package/esm/src/kernel/engine/session/mod.d.ts +33 -0
- package/esm/src/kernel/engine/session/mod.js +1182 -0
- package/esm/src/kernel/engine/session/session-trace.d.ts +115 -0
- package/esm/src/kernel/engine/session/session-trace.js +500 -0
- package/esm/src/kernel/engine/token-estimate.d.ts +23 -0
- package/esm/src/kernel/engine/token-estimate.js +38 -0
- package/esm/src/kernel/engine/tool-trace.d.ts +58 -0
- package/esm/src/kernel/engine/tool-trace.js +132 -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 +119 -0
- package/esm/src/kernel/engine/turn-trace.js +855 -0
- package/esm/src/kernel/engine/usage.d.ts +36 -0
- package/esm/src/kernel/engine/usage.js +151 -0
- package/esm/src/kernel/interaction-parts.d.ts +13 -0
- package/esm/src/kernel/interaction-parts.js +40 -0
- package/esm/src/kernel/json.d.ts +10 -0
- package/esm/src/kernel/json.js +3 -0
- package/esm/src/kernel/mod.d.ts +74 -0
- package/esm/src/kernel/mod.js +45 -0
- package/esm/src/kernel/profile-graph.d.ts +145 -0
- package/esm/src/kernel/profile-graph.js +158 -0
- package/esm/src/kernel/profile-presence.d.ts +18 -0
- package/esm/src/kernel/profile-presence.js +158 -0
- package/esm/src/kernel/profile-scope.d.ts +40 -0
- package/esm/src/kernel/profile-scope.js +274 -0
- package/esm/src/kernel/protocols/binding.d.ts +65 -0
- package/esm/src/kernel/protocols/binding.js +125 -0
- package/esm/src/kernel/protocols/limits.d.ts +15 -0
- package/esm/src/kernel/protocols/limits.js +28 -0
- package/esm/src/kernel/protocols/mod.d.ts +5 -0
- package/esm/src/kernel/protocols/mod.js +5 -0
- package/esm/src/kernel/protocols/prices.d.ts +36 -0
- package/esm/src/kernel/protocols/prices.js +37 -0
- package/esm/src/kernel/protocols/protocol.d.ts +253 -0
- package/esm/src/kernel/protocols/protocol.js +253 -0
- package/esm/src/kernel/protocols/provider.d.ts +108 -0
- package/esm/src/kernel/protocols/provider.js +143 -0
- package/esm/src/kernel/protocols/runtime.d.ts +55 -0
- package/esm/src/kernel/protocols/runtime.js +534 -0
- package/esm/src/kernel/provider-budget.d.ts +32 -0
- package/esm/src/kernel/provider-budget.js +262 -0
- package/esm/src/kernel/provider-dispatch.d.ts +13 -0
- package/esm/src/kernel/provider-dispatch.js +36 -0
- package/esm/src/kernel/provider-live.d.ts +7 -0
- package/esm/src/kernel/provider-live.js +189 -0
- package/esm/src/kernel/provider-runtime.d.ts +57 -0
- package/esm/src/kernel/provider-runtime.js +524 -0
- package/esm/src/kernel/registry/attachments.d.ts +46 -0
- package/esm/src/kernel/registry/attachments.js +220 -0
- package/esm/src/kernel/registry/catalog.d.ts +25 -0
- package/esm/src/kernel/registry/catalog.js +85 -0
- package/esm/src/kernel/registry/embedding-rules.d.ts +28 -0
- package/esm/src/kernel/registry/embedding-rules.js +66 -0
- package/esm/src/kernel/registry/ingress.d.ts +12 -0
- package/esm/src/kernel/registry/ingress.js +179 -0
- package/esm/src/kernel/registry/kernel-registry.d.ts +15 -0
- package/esm/src/kernel/registry/kernel-registry.js +15 -0
- package/esm/src/kernel/registry/profile-outputs.d.ts +3 -0
- package/esm/src/kernel/registry/profile-outputs.js +10 -0
- package/esm/src/kernel/registry/profiles.d.ts +116 -0
- package/esm/src/kernel/registry/profiles.js +773 -0
- package/esm/src/kernel/registry/provider-request.d.ts +6 -0
- package/esm/src/kernel/registry/provider-request.js +34 -0
- package/esm/src/kernel/registry/resolve.d.ts +17 -0
- package/esm/src/kernel/registry/resolve.js +238 -0
- package/esm/src/kernel/registry/schemas.d.ts +12 -0
- package/esm/src/kernel/registry/schemas.js +27 -0
- package/esm/src/kernel/registry/slot-fill.d.ts +7 -0
- package/esm/src/kernel/registry/slot-fill.js +22 -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 +32 -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 +14 -0
- package/esm/src/kernel/registry/vault.js +1 -0
- package/esm/src/kernel/schema.d.ts +226 -0
- package/esm/src/kernel/schema.js +895 -0
- package/esm/src/kernel/scope.d.ts +29 -0
- package/esm/src/kernel/scope.js +45 -0
- package/esm/src/kernel/stages.d.ts +176 -0
- package/esm/src/kernel/stages.js +431 -0
- package/esm/src/kernel/stop.d.ts +116 -0
- package/esm/src/kernel/stop.js +137 -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 +22 -0
- package/esm/src/kernel/tools/activity-label.js +435 -0
- package/esm/src/kernel/tools/agent.d.ts +66 -0
- package/esm/src/kernel/tools/agent.js +212 -0
- package/esm/src/kernel/tools/coordinate.d.ts +4 -0
- package/esm/src/kernel/tools/coordinate.js +165 -0
- package/esm/src/kernel/tools/events.d.ts +66 -0
- package/esm/src/kernel/tools/events.js +184 -0
- package/esm/src/kernel/tools/execute.d.ts +100 -0
- package/esm/src/kernel/tools/execute.js +1173 -0
- package/esm/src/kernel/tools/gate-answer.d.ts +60 -0
- package/esm/src/kernel/tools/gate-answer.js +65 -0
- package/esm/src/kernel/tools/handler-events.d.ts +45 -0
- package/esm/src/kernel/tools/handler-events.js +106 -0
- package/esm/src/kernel/tools/harness.d.ts +17 -0
- package/esm/src/kernel/tools/harness.js +43 -0
- package/esm/src/kernel/tools/http-limit.d.ts +27 -0
- package/esm/src/kernel/tools/http-limit.js +85 -0
- package/esm/src/kernel/tools/invoke.d.ts +32 -0
- package/esm/src/kernel/tools/invoke.js +282 -0
- package/esm/src/kernel/tools/mcp-sessions.d.ts +23 -0
- package/esm/src/kernel/tools/mcp-sessions.js +69 -0
- package/esm/src/kernel/tools/mod.d.ts +10 -0
- package/esm/src/kernel/tools/mod.js +7 -0
- package/esm/src/kernel/tools/model-text.d.ts +16 -0
- package/esm/src/kernel/tools/model-text.js +31 -0
- package/esm/src/kernel/tools/operation.d.ts +76 -0
- package/esm/src/kernel/tools/operation.js +100 -0
- package/esm/src/kernel/tools/page-answer.d.ts +8 -0
- package/esm/src/kernel/tools/page-answer.js +19 -0
- package/esm/src/kernel/tools/permission.d.ts +15 -0
- package/esm/src/kernel/tools/permission.js +38 -0
- package/esm/src/kernel/tools/project.d.ts +11 -0
- package/esm/src/kernel/tools/project.js +55 -0
- package/esm/src/kernel/tools/registry.d.ts +27 -0
- package/esm/src/kernel/tools/registry.js +143 -0
- package/esm/src/kernel/tools/remote.d.ts +144 -0
- package/esm/src/kernel/tools/remote.js +1125 -0
- package/esm/src/kernel/tools/resolve.d.ts +31 -0
- package/esm/src/kernel/tools/resolve.js +267 -0
- package/esm/src/kernel/tools/run.d.ts +28 -0
- package/esm/src/kernel/tools/run.js +86 -0
- package/esm/src/kernel/tools/schema.d.ts +10 -0
- package/esm/src/kernel/tools/schema.js +95 -0
- package/esm/src/kernel/tools/signed-in-fetch.d.ts +19 -0
- package/esm/src/kernel/tools/signed-in-fetch.js +45 -0
- package/esm/src/kernel/tools/stage-run.d.ts +116 -0
- package/esm/src/kernel/tools/stage-run.js +198 -0
- package/esm/src/kernel/tools/types.d.ts +497 -0
- package/esm/src/kernel/tools/types.js +1 -0
- package/esm/src/kernel/tools/unchecked-output.d.ts +14 -0
- package/esm/src/kernel/tools/unchecked-output.js +18 -0
- package/esm/src/kernel/turn-events.d.ts +776 -0
- package/esm/src/kernel/turn-events.js +543 -0
- package/esm/src/kernel/types.d.ts +1025 -0
- package/esm/src/kernel/types.js +1 -0
- package/esm/src/kernel/util/base64.d.ts +4 -0
- package/esm/src/kernel/util/base64.js +15 -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/partial-json.d.ts +23 -0
- package/esm/src/kernel/util/partial-json.js +128 -0
- package/esm/src/kernel/util/record.d.ts +2 -0
- package/esm/src/kernel/util/record.js +4 -0
- package/esm/src/observability/destinations.d.ts +14 -0
- package/esm/src/observability/destinations.js +43 -0
- package/esm/src/observability/jsonl.d.ts +18 -0
- package/esm/src/observability/jsonl.js +110 -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 +71 -0
- package/esm/src/observability/otlp.js +98 -0
- package/esm/src/observability/phoenix.d.ts +30 -0
- package/esm/src/observability/phoenix.js +71 -0
- package/esm/src/observability/policy.d.ts +17 -0
- package/esm/src/observability/policy.js +73 -0
- package/esm/src/observability/resolve-policy.d.ts +8 -0
- package/esm/src/observability/resolve-policy.js +64 -0
- package/esm/src/observability/spans.d.ts +22 -0
- package/esm/src/observability/spans.js +72 -0
- package/esm/src/observability/trace-catalog.d.ts +128 -0
- package/esm/src/observability/trace-catalog.js +906 -0
- package/esm/src/observability/trace-record.d.ts +23 -0
- package/esm/src/observability/trace-record.js +228 -0
- package/esm/src/observability/trace-schema.d.ts +57 -0
- package/esm/src/observability/trace-schema.js +59 -0
- package/esm/src/observability/trace-sink.d.ts +14 -0
- package/esm/src/observability/trace-sink.js +3 -0
- package/esm/src/observability/trace-span.d.ts +98 -0
- package/esm/src/observability/trace-span.js +201 -0
- package/esm/src/observability/trace.d.ts +13 -0
- package/esm/src/observability/trace.js +37 -0
- package/esm/src/observability/types.d.ts +109 -0
- package/esm/src/observability/types.js +3 -0
- package/esm/src/presets/google/speech-voices.d.ts +9 -0
- package/esm/src/presets/google/speech-voices.js +38 -0
- package/esm/src/presets/google-limits.d.ts +13 -0
- package/esm/src/presets/google-limits.js +18 -0
- package/esm/src/presets/google.d.ts +119 -0
- package/esm/src/presets/google.js +197 -0
- package/esm/src/presets/local.d.ts +26 -0
- package/esm/src/presets/local.js +30 -0
- package/esm/src/presets/mod.d.ts +11 -0
- package/esm/src/presets/mod.js +9 -0
- package/esm/src/presets/openrouter.d.ts +26 -0
- package/esm/src/presets/openrouter.js +31 -0
- package/esm/src/providers/embedding-limits.d.ts +7 -0
- package/esm/src/providers/embedding-limits.js +12 -0
- package/esm/src/providers/gemini/embedding-protocol.d.ts +8 -0
- package/esm/src/providers/gemini/embedding-protocol.js +125 -0
- package/esm/src/providers/gemini/interactions-protocol.d.ts +8 -0
- package/esm/src/providers/gemini/interactions-protocol.js +105 -0
- package/esm/src/providers/gemini/live-protocol.d.ts +3 -0
- package/esm/src/providers/gemini/live-protocol.js +191 -0
- package/esm/src/providers/gemini/prices.d.ts +3 -0
- package/esm/src/providers/gemini/prices.js +63 -0
- package/esm/src/providers/gemini/provider.d.ts +7 -0
- package/esm/src/providers/gemini/provider.js +39 -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/builtins.d.ts +14 -0
- package/esm/src/providers/google/builtins.js +57 -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 +27 -0
- package/esm/src/providers/google/interactions/framing.js +304 -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 +23 -0
- package/esm/src/providers/google/interactions/stream.js +230 -0
- package/esm/src/providers/google/live/framing.d.ts +61 -0
- package/esm/src/providers/google/live/framing.js +597 -0
- package/esm/src/providers/google/live/stream.d.ts +13 -0
- package/esm/src/providers/google/live/stream.js +57 -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 +4 -0
- package/esm/src/providers/google/urls.js +4 -0
- package/esm/src/providers/google/usage.d.ts +7 -0
- package/esm/src/providers/google/usage.js +49 -0
- package/esm/src/providers/live.d.ts +6 -0
- package/esm/src/providers/live.js +6 -0
- package/esm/src/providers/local/embedding-protocol.d.ts +7 -0
- package/esm/src/providers/local/embedding-protocol.js +60 -0
- package/esm/src/providers/local/provider.d.ts +7 -0
- package/esm/src/providers/local/provider.js +28 -0
- package/esm/src/providers/mod.d.ts +32 -0
- package/esm/src/providers/mod.js +23 -0
- package/esm/src/providers/open-ai/protocol.d.ts +3 -0
- package/esm/src/providers/open-ai/protocol.js +189 -0
- package/esm/src/providers/openrouter/embedding-protocol.d.ts +6 -0
- package/esm/src/providers/openrouter/embedding-protocol.js +86 -0
- package/esm/src/providers/openrouter/image.d.ts +15 -0
- package/esm/src/providers/openrouter/image.js +34 -0
- package/esm/src/providers/openrouter/images-protocol.d.ts +3 -0
- package/esm/src/providers/openrouter/images-protocol.js +82 -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/reasoning-state.d.ts +2 -0
- package/esm/src/providers/openrouter/openai/reasoning-state.js +31 -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/protocol.d.ts +8 -0
- package/esm/src/providers/openrouter/protocol.js +171 -0
- package/esm/src/providers/openrouter/provider.d.ts +10 -0
- package/esm/src/providers/openrouter/provider.js +50 -0
- package/esm/src/providers/shared/carried-tool-files.d.ts +7 -0
- package/esm/src/providers/shared/carried-tool-files.js +43 -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/protocol-events.d.ts +8 -0
- package/esm/src/providers/shared/protocol-events.js +20 -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/retry.d.ts +17 -0
- package/esm/src/providers/shared/retry.js +90 -0
- package/esm/src/providers/shared/sse.d.ts +6 -0
- package/esm/src/providers/shared/sse.js +93 -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 +24 -0
- package/esm/src/providers/shared/tool-args.js +88 -0
- package/esm/src/providers/shared/tool-draft.d.ts +16 -0
- package/esm/src/providers/shared/tool-draft.js +14 -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/system-one/protocol.d.ts +8 -0
- package/esm/src/providers/system-one/protocol.js +93 -0
- package/esm/src/providers/types.d.ts +51 -0
- package/esm/src/providers/types.js +2 -0
- package/esm/src/providers/typesafe/provider.d.ts +7 -0
- package/esm/src/providers/typesafe/provider.js +24 -0
- package/esm/src/surface/formats.d.ts +46 -0
- package/esm/src/surface/formats.js +178 -0
- package/esm/src/surface/mod.d.ts +16 -0
- package/esm/src/surface/mod.js +12 -0
- package/esm/src/surface/runtime.d.ts +44 -0
- package/esm/src/surface/runtime.js +441 -0
- package/esm/src/surface/tools.d.ts +9 -0
- package/esm/src/surface/tools.js +62 -0
- package/esm/src/surface/types.d.ts +110 -0
- package/esm/src/surface/types.js +4 -0
- package/package.json +100 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type JsonValue } from './json.js';
|
|
3
|
+
import type { TurnHistoryMessage } from './turn-events.js';
|
|
4
|
+
/**
|
|
5
|
+
* A protocol's saved continuation, and what it was saved for. It is used only when the next call
|
|
6
|
+
* goes to the same protocol, provider registration, connection, model, key slot, and keyed options.
|
|
7
|
+
*/
|
|
8
|
+
export interface ProviderCheckpoint {
|
|
9
|
+
protocol: string;
|
|
10
|
+
provider: string;
|
|
11
|
+
/** The protocol's `continuation.version` the data was written under. */
|
|
12
|
+
version: number;
|
|
13
|
+
apiId: string;
|
|
14
|
+
/** The slot whose key the saved state belongs to. */
|
|
15
|
+
keySlot?: string;
|
|
16
|
+
/** Hash of the provider's connection and the options the protocol keys its state on. */
|
|
17
|
+
compatibilityKey: string;
|
|
18
|
+
coveredHistoryLength: number;
|
|
19
|
+
coveredHistoryHash: string;
|
|
20
|
+
data: JsonValue;
|
|
21
|
+
/** The wire's own id for each Theorem call id the saved state holds, sent back in its place. */
|
|
22
|
+
wireCallIds?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
export declare const providerCheckpointSchema: z.ZodType<ProviderCheckpoint>;
|
|
25
|
+
/** Why a saved continuation was dropped and the conversation sent from the transcript. */
|
|
26
|
+
export type ProviderStateRebuilt = 'provider_changed' | 'model_changed' | 'connection_changed' | 'key_changed' | 'history_changed' | 'version_changed';
|
|
27
|
+
export interface ProviderWarning {
|
|
28
|
+
code: 'provider_state_rebuilt';
|
|
29
|
+
reason: ProviderStateRebuilt;
|
|
30
|
+
}
|
|
31
|
+
export declare const providerWarningSchema: z.ZodType<ProviderWarning>;
|
|
32
|
+
export interface ProviderContinuationPolicy {
|
|
33
|
+
/**
|
|
34
|
+
* Who holds the conversation a checkpoint continues. `'host'` (unset) passes the covered
|
|
35
|
+
* history back with the checkpoint. `'provider'` passes the checkpoint alone: the provider's
|
|
36
|
+
* stored conversation is the history, and a turn that goes elsewhere needs the host's history.
|
|
37
|
+
* `'stateless'` keeps no conversation: a request that carries history or a checkpoint is refused,
|
|
38
|
+
* and completion carries no checkpoint.
|
|
39
|
+
*/
|
|
40
|
+
history?: 'host' | 'stateless' | 'provider';
|
|
41
|
+
}
|
|
42
|
+
export declare const providerContinuationSchema: z.ZodType<ProviderContinuationPolicy>;
|
|
43
|
+
export declare function historyHash(history: readonly TurnHistoryMessage[]): Promise<string>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { jsonValueSchema } from './json.js';
|
|
3
|
+
export const providerCheckpointSchema = z.strictObject({
|
|
4
|
+
protocol: z.string().min(1),
|
|
5
|
+
provider: z.string().min(1),
|
|
6
|
+
version: z.number().int().positive(),
|
|
7
|
+
apiId: z.string().min(1),
|
|
8
|
+
keySlot: z.string().min(1).optional(),
|
|
9
|
+
compatibilityKey: z.string(),
|
|
10
|
+
coveredHistoryLength: z.number().int().nonnegative(),
|
|
11
|
+
coveredHistoryHash: z.string(),
|
|
12
|
+
data: jsonValueSchema,
|
|
13
|
+
wireCallIds: z.record(z.string().min(1), z.string().min(1)).optional(),
|
|
14
|
+
});
|
|
15
|
+
export const providerWarningSchema = z.strictObject({
|
|
16
|
+
code: z.literal('provider_state_rebuilt'),
|
|
17
|
+
reason: z.enum([
|
|
18
|
+
'provider_changed',
|
|
19
|
+
'model_changed',
|
|
20
|
+
'connection_changed',
|
|
21
|
+
'key_changed',
|
|
22
|
+
'history_changed',
|
|
23
|
+
'version_changed',
|
|
24
|
+
]),
|
|
25
|
+
});
|
|
26
|
+
export const providerContinuationSchema = z.strictObject({
|
|
27
|
+
history: z.enum(['host', 'stateless', 'provider']).optional(),
|
|
28
|
+
});
|
|
29
|
+
export async function historyHash(history) {
|
|
30
|
+
const stable = (value) => {
|
|
31
|
+
if (Array.isArray(value))
|
|
32
|
+
return value.map(stable);
|
|
33
|
+
if (value && typeof value === 'object') {
|
|
34
|
+
const ordered = {};
|
|
35
|
+
for (const key of Object.keys(value).sort())
|
|
36
|
+
ordered[key] = stable(value[key]);
|
|
37
|
+
return ordered;
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
};
|
|
41
|
+
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(JSON.stringify(stable(history.map((message) => {
|
|
42
|
+
const canonicalText = (text) => message.role === 'user'
|
|
43
|
+
? (text.match(/^<user_data>\n([\s\S]*)\n<\/user_data>$/)?.[1] ?? text)
|
|
44
|
+
: text;
|
|
45
|
+
const parts = [
|
|
46
|
+
...(message.content === undefined
|
|
47
|
+
? []
|
|
48
|
+
: [{ type: 'text', text: canonicalText(message.content) }]),
|
|
49
|
+
...(message.parts ?? []).map((part) => part.type === 'text' ? { ...part, text: canonicalText(part.text) } : part),
|
|
50
|
+
];
|
|
51
|
+
const { content: _content, parts: _parts, ...rest } = message;
|
|
52
|
+
return { ...rest, ...(parts.length ? { parts } : {}) };
|
|
53
|
+
})))));
|
|
54
|
+
let hex = '';
|
|
55
|
+
for (const byte of new Uint8Array(hash))
|
|
56
|
+
hex += byte.toString(16).padStart(2, '0');
|
|
57
|
+
return hex;
|
|
58
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { TraceSink } from '../observability/trace-sink.js';
|
|
2
|
+
import type { RunDecisionOptions } from './engine/decision.js';
|
|
3
|
+
import type { RunEmbeddingOptions } from './engine/embedding.js';
|
|
4
|
+
import type { RunSessionOptions } from './engine/session/mod.js';
|
|
5
|
+
import type { Protocol } from './protocols/protocol.js';
|
|
6
|
+
import type { Provider, RegisteredProvider, Registration } from './protocols/provider.js';
|
|
7
|
+
import type { ProviderHostOptions } from './protocols/runtime.js';
|
|
8
|
+
import type { ProfileDefinition } from './registry/profiles.js';
|
|
9
|
+
import { defaultKernelScope as scope } from './scope.js';
|
|
10
|
+
import type { ToolCallResult, ToolStepCallResult } from './tools/run.js';
|
|
11
|
+
import type { InvokeToolRequest, InvokeToolStepRequest, RegisteredTool, ToolDefinitionInput } from './tools/types.js';
|
|
12
|
+
import type { CompactHistoryRequest, CompactionResult, DecisionRequest, DecisionResult, EmbeddingRequest, EmbeddingResult, LiveSession, Profile, ProjectedProfile, SessionRequest, StructuredSpec, TurnEvent, TurnRequest } from './types.js';
|
|
13
|
+
/** Register or replace a tool in the default scope. */
|
|
14
|
+
declare function registerTool<TIn, TOut>(def: ToolDefinitionInput<TIn, TOut>): RegisteredTool<TIn, TOut>;
|
|
15
|
+
/** Register or replace several tools in the default scope. */
|
|
16
|
+
declare function registerTools(defs: ToolDefinitionInput[]): RegisteredTool[];
|
|
17
|
+
/** The default scope's tool `name`, or `undefined`. */
|
|
18
|
+
declare function getTool(name: string): RegisteredTool | undefined;
|
|
19
|
+
/** The default scope's tool `name`; throws when there is none. */
|
|
20
|
+
declare function requireTool(name: string): RegisteredTool;
|
|
21
|
+
/** True when the default scope has a tool `name`. */
|
|
22
|
+
declare function hasTool(name: string): boolean;
|
|
23
|
+
/** Every tool in the default scope. */
|
|
24
|
+
declare function listTools(): RegisteredTool[];
|
|
25
|
+
/** Remove every tool from the default scope. */
|
|
26
|
+
declare function resetTools(): void;
|
|
27
|
+
/** Define, validate, and register a profile in the default scope. */
|
|
28
|
+
declare function registerProfile(profile: Profile | ProfileDefinition): void;
|
|
29
|
+
/** Define, validate, and register several profiles in the default scope. */
|
|
30
|
+
declare function registerProfiles(profiles: Array<Profile | ProfileDefinition>): void;
|
|
31
|
+
/** The default scope's profile `id`; throws when there is none. */
|
|
32
|
+
declare function getProfile(id: string): Profile;
|
|
33
|
+
/** True when the default scope has a profile `id`. */
|
|
34
|
+
declare function hasProfile(id: string): boolean;
|
|
35
|
+
/** Every profile in the default scope. */
|
|
36
|
+
declare function listProfiles(): Profile[];
|
|
37
|
+
/** Remove every profile from the default scope. */
|
|
38
|
+
declare function clearProfiles(): void;
|
|
39
|
+
/** Register a structured-output schema in the default scope under `id`. */
|
|
40
|
+
declare function registerStructured(id: string, spec: StructuredSpec): void;
|
|
41
|
+
/** The default scope's schema `id`; throws when there is none. */
|
|
42
|
+
declare function getStructured(id: string): StructuredSpec;
|
|
43
|
+
/** Run one turn of a profile in the default scope, streaming its events. */
|
|
44
|
+
declare function runTurn(req: TurnRequest, options?: ProviderHostOptions, sinkOverride?: TraceSink): AsyncGenerator<TurnEvent>;
|
|
45
|
+
/** Open a live session for a live profile in the default scope. */
|
|
46
|
+
declare function runSession(req: SessionRequest, options: RunSessionOptions, sinkOverride?: TraceSink): Promise<LiveSession>;
|
|
47
|
+
/** Compact a conversation history with the model, or `undefined` when there is nothing to compact. */
|
|
48
|
+
declare function compactHistory(req: CompactHistoryRequest, options?: ProviderHostOptions, sinkOverride?: TraceSink): Promise<CompactionResult | undefined>;
|
|
49
|
+
/** Run a held tool call in the default scope, streaming the events of the turn it resumes. */
|
|
50
|
+
declare function invokeTool(request: InvokeToolRequest, sinkOverride?: TraceSink): AsyncGenerator<TurnEvent>;
|
|
51
|
+
/** Run the host's calls as one step in the default scope, streaming each call's events as they come. */
|
|
52
|
+
declare function invokeToolStep(request: InvokeToolStepRequest, sinkOverride?: TraceSink): AsyncGenerator<TurnEvent>;
|
|
53
|
+
/** Run one tool call in the default scope to its end: how it settled, with every event it emitted. */
|
|
54
|
+
declare function runTool(request: InvokeToolRequest, sinkOverride?: TraceSink): Promise<ToolCallResult>;
|
|
55
|
+
/** Run the host's calls as one step in the default scope, giving each call's end as it settles. */
|
|
56
|
+
declare function runToolStep(request: InvokeToolStepRequest, sinkOverride?: TraceSink): AsyncGenerator<ToolStepCallResult>;
|
|
57
|
+
/** Resolve a turn request in the default scope: its profile, model, and generation settings. */
|
|
58
|
+
declare function resolveTurn(req: TurnRequest): ReturnType<typeof scope.resolveTurn>;
|
|
59
|
+
/** Each model of profile `id` in the default scope that cannot run a plain turn on its provider, and why. */
|
|
60
|
+
/** The profile `id` projected for a client, with server-only fields removed. */
|
|
61
|
+
declare function projectProfile(id: string): ProjectedProfile;
|
|
62
|
+
/** Answer a decision request with the profile's model in the default scope. */
|
|
63
|
+
declare function runDecision(request: DecisionRequest, options: RunDecisionOptions): Promise<DecisionResult>;
|
|
64
|
+
/** Create vectors for one or more inputs in the default scope. */
|
|
65
|
+
declare function runEmbedding(request: EmbeddingRequest, options?: RunEmbeddingOptions): Promise<EmbeddingResult>;
|
|
66
|
+
export { clearProfiles, compactHistory, getProfile, getStructured, getTool, hasProfile, hasTool, invokeTool, invokeToolStep, listProfiles, listTools, projectProfile, registerProfile, registerProfiles, registerStructured, registerTool, registerTools, requireTool, resetTools, resolveTurn, runDecision, runEmbedding, runSession, runTool, runToolStep, runTurn, };
|
|
67
|
+
/** Registers a provider in the default scope, with this project's connection and key slots. */
|
|
68
|
+
export declare function registerProvider<C, K>(provider: Provider<C, K>, registration?: Registration<C>): RegisteredProvider;
|
|
69
|
+
export declare function getProvider(id: string): RegisteredProvider | undefined;
|
|
70
|
+
export declare function listProviders(): RegisteredProvider[];
|
|
71
|
+
/** Every protocol the default scope's providers serve. */
|
|
72
|
+
export declare function listProtocols(): Protocol[];
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { defaultKernelScope as scope } from './scope.js';
|
|
2
|
+
/** Register or replace a tool in the default scope. */
|
|
3
|
+
function registerTool(def) {
|
|
4
|
+
return scope.tools.register(def);
|
|
5
|
+
}
|
|
6
|
+
/** Register or replace several tools in the default scope. */
|
|
7
|
+
function registerTools(defs) {
|
|
8
|
+
return scope.tools.registerMany(defs);
|
|
9
|
+
}
|
|
10
|
+
/** The default scope's tool `name`, or `undefined`. */
|
|
11
|
+
function getTool(name) {
|
|
12
|
+
return scope.tools.get(name);
|
|
13
|
+
}
|
|
14
|
+
/** The default scope's tool `name`; throws when there is none. */
|
|
15
|
+
function requireTool(name) {
|
|
16
|
+
return scope.tools.require(name);
|
|
17
|
+
}
|
|
18
|
+
/** True when the default scope has a tool `name`. */
|
|
19
|
+
function hasTool(name) {
|
|
20
|
+
return scope.tools.has(name);
|
|
21
|
+
}
|
|
22
|
+
/** Every tool in the default scope. */
|
|
23
|
+
function listTools() {
|
|
24
|
+
return scope.tools.list();
|
|
25
|
+
}
|
|
26
|
+
/** Remove every tool from the default scope. */
|
|
27
|
+
function resetTools() {
|
|
28
|
+
scope.tools.reset();
|
|
29
|
+
}
|
|
30
|
+
/** Define, validate, and register a profile in the default scope. */
|
|
31
|
+
function registerProfile(profile) {
|
|
32
|
+
scope.profiles.register(profile);
|
|
33
|
+
}
|
|
34
|
+
/** Define, validate, and register several profiles in the default scope. */
|
|
35
|
+
function registerProfiles(profiles) {
|
|
36
|
+
scope.profiles.registerMany(profiles);
|
|
37
|
+
}
|
|
38
|
+
/** The default scope's profile `id`; throws when there is none. */
|
|
39
|
+
function getProfile(id) {
|
|
40
|
+
return scope.profiles.get(id);
|
|
41
|
+
}
|
|
42
|
+
/** True when the default scope has a profile `id`. */
|
|
43
|
+
function hasProfile(id) {
|
|
44
|
+
return scope.profiles.has(id);
|
|
45
|
+
}
|
|
46
|
+
/** Every profile in the default scope. */
|
|
47
|
+
function listProfiles() {
|
|
48
|
+
return scope.profiles.list();
|
|
49
|
+
}
|
|
50
|
+
/** Remove every profile from the default scope. */
|
|
51
|
+
function clearProfiles() {
|
|
52
|
+
scope.profiles.clear();
|
|
53
|
+
}
|
|
54
|
+
/** Register a structured-output schema in the default scope under `id`. */
|
|
55
|
+
function registerStructured(id, spec) {
|
|
56
|
+
scope.schemas.register(id, spec);
|
|
57
|
+
}
|
|
58
|
+
/** The default scope's schema `id`; throws when there is none. */
|
|
59
|
+
function getStructured(id) {
|
|
60
|
+
return scope.schemas.get(id);
|
|
61
|
+
}
|
|
62
|
+
/** Run one turn of a profile in the default scope, streaming its events. */
|
|
63
|
+
function runTurn(req, options = {}, sinkOverride) {
|
|
64
|
+
return scope.runTurn(req, options, sinkOverride);
|
|
65
|
+
}
|
|
66
|
+
/** Open a live session for a live profile in the default scope. */
|
|
67
|
+
function runSession(req, options, sinkOverride) {
|
|
68
|
+
return scope.runSession(req, options, sinkOverride);
|
|
69
|
+
}
|
|
70
|
+
/** Compact a conversation history with the model, or `undefined` when there is nothing to compact. */
|
|
71
|
+
function compactHistory(req, options = {}, sinkOverride) {
|
|
72
|
+
return scope.compactHistory(req, options, sinkOverride);
|
|
73
|
+
}
|
|
74
|
+
/** Run a held tool call in the default scope, streaming the events of the turn it resumes. */
|
|
75
|
+
function invokeTool(request, sinkOverride) {
|
|
76
|
+
return scope.invokeTool(request, sinkOverride);
|
|
77
|
+
}
|
|
78
|
+
/** Run the host's calls as one step in the default scope, streaming each call's events as they come. */
|
|
79
|
+
function invokeToolStep(request, sinkOverride) {
|
|
80
|
+
return scope.invokeToolStep(request, sinkOverride);
|
|
81
|
+
}
|
|
82
|
+
/** Run one tool call in the default scope to its end: how it settled, with every event it emitted. */
|
|
83
|
+
function runTool(request, sinkOverride) {
|
|
84
|
+
return scope.runTool(request, sinkOverride);
|
|
85
|
+
}
|
|
86
|
+
/** Run the host's calls as one step in the default scope, giving each call's end as it settles. */
|
|
87
|
+
function runToolStep(request, sinkOverride) {
|
|
88
|
+
return scope.runToolStep(request, sinkOverride);
|
|
89
|
+
}
|
|
90
|
+
/** Resolve a turn request in the default scope: its profile, model, and generation settings. */
|
|
91
|
+
function resolveTurn(req) {
|
|
92
|
+
return scope.resolveTurn(req);
|
|
93
|
+
}
|
|
94
|
+
/** Each model of profile `id` in the default scope that cannot run a plain turn on its provider, and why. */
|
|
95
|
+
/** The profile `id` projected for a client, with server-only fields removed. */
|
|
96
|
+
function projectProfile(id) {
|
|
97
|
+
return scope.projectProfile(id);
|
|
98
|
+
}
|
|
99
|
+
/** Answer a decision request with the profile's model in the default scope. */
|
|
100
|
+
function runDecision(request, options) {
|
|
101
|
+
return scope.runDecision(request, options);
|
|
102
|
+
}
|
|
103
|
+
/** Create vectors for one or more inputs in the default scope. */
|
|
104
|
+
function runEmbedding(request, options = {}) {
|
|
105
|
+
return scope.runEmbedding(request, options);
|
|
106
|
+
}
|
|
107
|
+
export { clearProfiles, compactHistory, getProfile, getStructured, getTool, hasProfile, hasTool, invokeTool, invokeToolStep, listProfiles, listTools, projectProfile, registerProfile, registerProfiles, registerStructured, registerTool, registerTools, requireTool, resetTools, resolveTurn, runDecision, runEmbedding, runSession, runTool, runToolStep, runTurn, };
|
|
108
|
+
/** Registers a provider in the default scope, with this project's connection and key slots. */
|
|
109
|
+
export function registerProvider(provider, registration) {
|
|
110
|
+
return scope.providers.register(provider, registration);
|
|
111
|
+
}
|
|
112
|
+
export function getProvider(id) {
|
|
113
|
+
return scope.providers.get(id);
|
|
114
|
+
}
|
|
115
|
+
export function listProviders() {
|
|
116
|
+
return scope.providers.list();
|
|
117
|
+
}
|
|
118
|
+
/** Every protocol the default scope's providers serve. */
|
|
119
|
+
export function listProtocols() {
|
|
120
|
+
return scope.providers.protocols();
|
|
121
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { resolveTraceWriter } from '../../observability/policy.js';
|
|
2
|
+
import { type SpanHandle, type SpanOptions } from '../../observability/trace-span.js';
|
|
3
|
+
interface TracedCall {
|
|
4
|
+
/** The root span's name, such as `decide <apiId>`. */
|
|
5
|
+
name: string;
|
|
6
|
+
attributes: SpanOptions['attributes'];
|
|
7
|
+
traceparent?: string;
|
|
8
|
+
/** Host metadata kept on the record. */
|
|
9
|
+
metadata?: Record<string, unknown>;
|
|
10
|
+
/** Where the record goes and how it is cleaned: the profile's settings, or the caller's sink. */
|
|
11
|
+
writer: Parameters<typeof resolveTraceWriter>[0];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* One request that isn't a turn, under a root span: ended OK when `run` returns, with the
|
|
15
|
+
* exception when it throws, and written either way. A failed write never changes the outcome.
|
|
16
|
+
*/
|
|
17
|
+
declare function tracedCall<T>({ name, attributes, traceparent, metadata, writer }: TracedCall, run: (root: SpanHandle) => Promise<T>): Promise<T>;
|
|
18
|
+
export type { TracedCall };
|
|
19
|
+
export { tracedCall };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { resolveTraceWriter } from '../../observability/policy.js';
|
|
2
|
+
import { writeSpans } from '../../observability/trace.js';
|
|
3
|
+
import { startTrace } from '../../observability/trace-span.js';
|
|
4
|
+
import { endThrownSpan } from './turn-trace.js';
|
|
5
|
+
/**
|
|
6
|
+
* One request that isn't a turn, under a root span: ended OK when `run` returns, with the
|
|
7
|
+
* exception when it throws, and written either way. A failed write never changes the outcome.
|
|
8
|
+
*/
|
|
9
|
+
async function tracedCall({ name, attributes, traceparent, metadata, writer }, run) {
|
|
10
|
+
const tree = startTrace(name, {
|
|
11
|
+
kind: 'CLIENT',
|
|
12
|
+
attributes,
|
|
13
|
+
...(traceparent ? { traceparent } : {}),
|
|
14
|
+
});
|
|
15
|
+
const { sink, policy } = resolveTraceWriter(writer);
|
|
16
|
+
try {
|
|
17
|
+
const result = await run(tree.root);
|
|
18
|
+
tree.root.end({ code: 'OK' });
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
endThrownSpan(tree.root, error);
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
await writeSpans(sink, tree.collect(), policy, metadata);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export { tracedCall };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { CompactionFailure, CompactionMeter, CompactionResult, CompactionSpec, Profile, TurnHistoryMessage, TurnInput, TurnTokens } from '../types.js';
|
|
2
|
+
/** A history divided into the messages to compact and the messages to keep. */
|
|
3
|
+
export interface CompactionSplit {
|
|
4
|
+
toCompact: TurnHistoryMessage[];
|
|
5
|
+
toRetain: TurnHistoryMessage[];
|
|
6
|
+
}
|
|
7
|
+
/** The count a compaction trigger sees: the tokens, the meter they were counted by and the media parts left out. */
|
|
8
|
+
export interface CompactionTokens {
|
|
9
|
+
meter: CompactionMeter;
|
|
10
|
+
/** Token count compared to `compactAt * maxTokens`. */
|
|
11
|
+
tokens: number;
|
|
12
|
+
/**
|
|
13
|
+
* Media parts left out of `tokens` because no verified rule counts them for this model.
|
|
14
|
+
* Always 0 for host-supplied and provider-reported counts.
|
|
15
|
+
*/
|
|
16
|
+
unknownMedia: number;
|
|
17
|
+
}
|
|
18
|
+
/** Effective meter; defaults to `'history'`. */
|
|
19
|
+
export declare function compactionMeter(spec: CompactionSpec): CompactionMeter;
|
|
20
|
+
/** Prefers host-supplied `input.historyTokens`; otherwise estimates `input.history`. */
|
|
21
|
+
export declare function resolveHistoryTokens(input: TurnInput | undefined): Promise<Omit<CompactionTokens, 'meter'>>;
|
|
22
|
+
/**
|
|
23
|
+
* `meter: 'input'` prefers this turn's last model call (`timing: 'after'`), else the host's
|
|
24
|
+
* `input.inputTokens` from the previous turn (`timing: 'before'`); `undefined` (do not fire) when
|
|
25
|
+
* neither is positive. It never estimates history.
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveCompactionTokens(args: {
|
|
28
|
+
spec: CompactionSpec;
|
|
29
|
+
input?: TurnInput;
|
|
30
|
+
/** Tokens of this turn's last model call, when one completed. */
|
|
31
|
+
prompt?: TurnTokens;
|
|
32
|
+
}): Promise<CompactionTokens | undefined>;
|
|
33
|
+
/** True when the tokens exceed `compactAt` times `maxTokens`. */
|
|
34
|
+
export declare function compactionNeeded(tokens: number, spec: CompactionSpec): boolean;
|
|
35
|
+
/** `spec.trigger`, when set, decides alone; otherwise the token threshold does. */
|
|
36
|
+
export declare function shouldCompact(resolved: CompactionTokens, spec: CompactionSpec): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
* `previousExchanges` semantics:
|
|
39
|
+
* - `0` — compact everything, retain nothing.
|
|
40
|
+
* - `≥ 1` (integer) — retain the last N exchanges.
|
|
41
|
+
* - `(0, 1)` — retain exchanges that fit within this fraction of `maxTokens`,
|
|
42
|
+
* walking backwards from the most recent (token estimator; unknown media is
|
|
43
|
+
* not counted).
|
|
44
|
+
*/
|
|
45
|
+
export declare function splitForCompaction(history: TurnHistoryMessage[], spec: CompactionSpec): Promise<CompactionSplit>;
|
|
46
|
+
/**
|
|
47
|
+
* `toCompact` as the compactor reads it: media its profile does not accept is
|
|
48
|
+
* left out, and tool calls and results become the assistant's text, so no
|
|
49
|
+
* provider needs the tools declared to read them.
|
|
50
|
+
*/
|
|
51
|
+
export declare function compactorHistory(toCompact: readonly TurnHistoryMessage[], compactor: Profile): {
|
|
52
|
+
history: TurnHistoryMessage[];
|
|
53
|
+
droppedMedia: number;
|
|
54
|
+
};
|
|
55
|
+
/** What the compactor returned: a summary, or why there is none. */
|
|
56
|
+
export type CompactorRun = {
|
|
57
|
+
summary: string;
|
|
58
|
+
droppedMedia: number;
|
|
59
|
+
tokens?: TurnTokens;
|
|
60
|
+
} | {
|
|
61
|
+
failure: CompactionFailure;
|
|
62
|
+
droppedMedia: number;
|
|
63
|
+
tokens?: TurnTokens;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The history after one compaction. A failed compactor never leaves a hollow
|
|
67
|
+
* summary: under `maxTokens` the history stays whole for the next turn to try
|
|
68
|
+
* again; over it, `toCompact` is dropped (keeping any earlier summary) so the
|
|
69
|
+
* turn still fits.
|
|
70
|
+
*/
|
|
71
|
+
export declare function compactionResult(split: CompactionSplit, run: CompactorRun, tokens: number, spec: CompactionSpec): CompactionResult;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { labelText } from '../../guardrails/labels.js';
|
|
2
|
+
import { mediaChannelForMime } from '../registry/catalog.js';
|
|
3
|
+
import { loadTokenEstimator } from './token-estimate.js';
|
|
4
|
+
/** Effective meter; defaults to `'history'`. */
|
|
5
|
+
export function compactionMeter(spec) {
|
|
6
|
+
return spec.meter ?? 'history';
|
|
7
|
+
}
|
|
8
|
+
// why: An exchange starts at each `user` message; system messages before the first one belong to none.
|
|
9
|
+
function findExchangeBoundaries(history) {
|
|
10
|
+
const boundaries = [];
|
|
11
|
+
for (let i = 0; i < history.length; i++) {
|
|
12
|
+
if (history[i].role === 'user') {
|
|
13
|
+
boundaries.push(i);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return boundaries;
|
|
17
|
+
}
|
|
18
|
+
/** Prefers host-supplied `input.historyTokens`; otherwise estimates `input.history`. */
|
|
19
|
+
export async function resolveHistoryTokens(input) {
|
|
20
|
+
if (input?.historyTokens != null) {
|
|
21
|
+
return { tokens: input.historyTokens, unknownMedia: 0 };
|
|
22
|
+
}
|
|
23
|
+
const history = input?.history ?? [];
|
|
24
|
+
if (history.length === 0)
|
|
25
|
+
return { tokens: 0, unknownMedia: 0 };
|
|
26
|
+
return await (await loadTokenEstimator()).messages(history);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `meter: 'input'` prefers this turn's last model call (`timing: 'after'`), else the host's
|
|
30
|
+
* `input.inputTokens` from the previous turn (`timing: 'before'`); `undefined` (do not fire) when
|
|
31
|
+
* neither is positive. It never estimates history.
|
|
32
|
+
*/
|
|
33
|
+
export async function resolveCompactionTokens(args) {
|
|
34
|
+
const meter = compactionMeter(args.spec);
|
|
35
|
+
if (meter === 'history') {
|
|
36
|
+
return { meter, ...(await resolveHistoryTokens(args.input)) };
|
|
37
|
+
}
|
|
38
|
+
if (args.prompt && args.prompt.input > 0) {
|
|
39
|
+
return { meter, tokens: args.prompt.input, unknownMedia: args.prompt.unknownMedia?.input ?? 0 };
|
|
40
|
+
}
|
|
41
|
+
const fromHost = args.input?.inputTokens;
|
|
42
|
+
if (fromHost == null || fromHost <= 0)
|
|
43
|
+
return undefined;
|
|
44
|
+
return { meter, tokens: fromHost, unknownMedia: 0 };
|
|
45
|
+
}
|
|
46
|
+
/** True when the tokens exceed `compactAt` times `maxTokens`. */
|
|
47
|
+
export function compactionNeeded(tokens, spec) {
|
|
48
|
+
return tokens > spec.compactAt * spec.maxTokens;
|
|
49
|
+
}
|
|
50
|
+
/** `spec.trigger`, when set, decides alone; otherwise the token threshold does. */
|
|
51
|
+
export async function shouldCompact(resolved, spec) {
|
|
52
|
+
if (spec.trigger) {
|
|
53
|
+
const ctx = {
|
|
54
|
+
tokens: resolved.tokens,
|
|
55
|
+
maxTokens: spec.maxTokens,
|
|
56
|
+
compactAt: spec.compactAt,
|
|
57
|
+
meter: resolved.meter,
|
|
58
|
+
unknownMedia: resolved.unknownMedia,
|
|
59
|
+
};
|
|
60
|
+
return await spec.trigger(ctx);
|
|
61
|
+
}
|
|
62
|
+
return compactionNeeded(resolved.tokens, spec);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* `previousExchanges` semantics:
|
|
66
|
+
* - `0` — compact everything, retain nothing.
|
|
67
|
+
* - `≥ 1` (integer) — retain the last N exchanges.
|
|
68
|
+
* - `(0, 1)` — retain exchanges that fit within this fraction of `maxTokens`,
|
|
69
|
+
* walking backwards from the most recent (token estimator; unknown media is
|
|
70
|
+
* not counted).
|
|
71
|
+
*/
|
|
72
|
+
export async function splitForCompaction(history, spec) {
|
|
73
|
+
if (history.length === 0) {
|
|
74
|
+
return { toCompact: [], toRetain: [] };
|
|
75
|
+
}
|
|
76
|
+
if (spec.previousExchanges === 0) {
|
|
77
|
+
return { toCompact: [...history], toRetain: [] };
|
|
78
|
+
}
|
|
79
|
+
const boundaries = findExchangeBoundaries(history);
|
|
80
|
+
if (boundaries.length === 0) {
|
|
81
|
+
return { toCompact: [...history], toRetain: [] };
|
|
82
|
+
}
|
|
83
|
+
let cutIndex;
|
|
84
|
+
if (spec.previousExchanges >= 1) {
|
|
85
|
+
const keep = Math.min(spec.previousExchanges, boundaries.length);
|
|
86
|
+
cutIndex = boundaries[boundaries.length - keep];
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const budget = spec.previousExchanges * spec.maxTokens;
|
|
90
|
+
const estimator = await loadTokenEstimator();
|
|
91
|
+
let accumulated = 0;
|
|
92
|
+
cutIndex = history.length;
|
|
93
|
+
for (let i = boundaries.length - 1; i >= 0; i--) {
|
|
94
|
+
const exchangeStart = boundaries[i];
|
|
95
|
+
const exchangeEnd = i < boundaries.length - 1 ? boundaries[i + 1] : history.length;
|
|
96
|
+
const exchangeMessages = history.slice(exchangeStart, exchangeEnd);
|
|
97
|
+
const exchangeTokens = estimator.messages(exchangeMessages).tokens;
|
|
98
|
+
if (accumulated + exchangeTokens > budget) {
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
accumulated += exchangeTokens;
|
|
102
|
+
cutIndex = exchangeStart;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (cutIndex <= 0) {
|
|
106
|
+
return { toCompact: [], toRetain: [...history] };
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
toCompact: history.slice(0, cutIndex),
|
|
110
|
+
toRetain: history.slice(cutIndex),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function isCompactionSummary(message) {
|
|
114
|
+
return message.metadata?.compactionSummary === true;
|
|
115
|
+
}
|
|
116
|
+
function textPart(text) {
|
|
117
|
+
return { type: 'text', text };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* `toCompact` as the compactor reads it: media its profile does not accept is
|
|
121
|
+
* left out, and tool calls and results become the assistant's text, so no
|
|
122
|
+
* provider needs the tools declared to read them.
|
|
123
|
+
*/
|
|
124
|
+
export function compactorHistory(toCompact, compactor) {
|
|
125
|
+
const labels = compactor.labels;
|
|
126
|
+
const toolNames = new Map();
|
|
127
|
+
let droppedMedia = 0;
|
|
128
|
+
const readable = (parts) => (parts ?? []).filter((part) => {
|
|
129
|
+
if (part.type === 'text' || mediaChannelForMime(compactor, part.mimeType))
|
|
130
|
+
return true;
|
|
131
|
+
droppedMedia += 1;
|
|
132
|
+
return false;
|
|
133
|
+
});
|
|
134
|
+
const history = [];
|
|
135
|
+
for (const message of toCompact) {
|
|
136
|
+
for (const call of message.tool_calls ?? [])
|
|
137
|
+
toolNames.set(call.id, call.function.name);
|
|
138
|
+
const parts = readable(message.parts);
|
|
139
|
+
if (message.role === 'tool') {
|
|
140
|
+
const tool = message.name ?? (message.tool_call_id ? toolNames.get(message.tool_call_id) : undefined);
|
|
141
|
+
const result = message.content ?? '';
|
|
142
|
+
history.push({
|
|
143
|
+
role: 'assistant',
|
|
144
|
+
parts: [
|
|
145
|
+
textPart(labelText('compaction.tool_result', { tool: tool ?? '', result }, labels)),
|
|
146
|
+
...parts,
|
|
147
|
+
],
|
|
148
|
+
});
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const calls = (message.tool_calls ?? []).map((call) => textPart(labelText('compaction.tool_call', { tool: call.function.name, arguments: call.function.arguments }, labels)));
|
|
152
|
+
const all = [...parts, ...calls];
|
|
153
|
+
if (!message.content && all.length === 0)
|
|
154
|
+
continue;
|
|
155
|
+
history.push({
|
|
156
|
+
role: message.role,
|
|
157
|
+
...(message.content ? { content: message.content } : {}),
|
|
158
|
+
...(all.length > 0 ? { parts: all } : {}),
|
|
159
|
+
...(message.name ? { name: message.name } : {}),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return { history, droppedMedia };
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The history after one compaction. A failed compactor never leaves a hollow
|
|
166
|
+
* summary: under `maxTokens` the history stays whole for the next turn to try
|
|
167
|
+
* again; over it, `toCompact` is dropped (keeping any earlier summary) so the
|
|
168
|
+
* turn still fits.
|
|
169
|
+
*/
|
|
170
|
+
export function compactionResult(split, run, tokens, spec) {
|
|
171
|
+
const shared = {
|
|
172
|
+
toCompact: split.toCompact,
|
|
173
|
+
droppedMedia: run.droppedMedia,
|
|
174
|
+
...(run.tokens ? { tokens: run.tokens } : {}),
|
|
175
|
+
};
|
|
176
|
+
if ('summary' in run) {
|
|
177
|
+
const summary = {
|
|
178
|
+
role: 'assistant',
|
|
179
|
+
content: run.summary,
|
|
180
|
+
metadata: { compactionSummary: true },
|
|
181
|
+
};
|
|
182
|
+
return {
|
|
183
|
+
...shared,
|
|
184
|
+
outcome: 'compacted',
|
|
185
|
+
summary: run.summary,
|
|
186
|
+
history: [summary, ...split.toRetain],
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
if (tokens > spec.maxTokens) {
|
|
190
|
+
return {
|
|
191
|
+
...shared,
|
|
192
|
+
outcome: 'dropped',
|
|
193
|
+
failure: run.failure,
|
|
194
|
+
history: [...split.toCompact.filter(isCompactionSummary), ...split.toRetain],
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
...shared,
|
|
199
|
+
outcome: 'deferred',
|
|
200
|
+
failure: run.failure,
|
|
201
|
+
history: [...split.toCompact, ...split.toRetain],
|
|
202
|
+
};
|
|
203
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { TheoremError } from '../../guardrails/error.js';
|
|
2
|
+
import type { TraceSink } from '../../observability/trace-sink.js';
|
|
3
|
+
import type { ProviderHostOptions } from '../protocols/runtime.js';
|
|
4
|
+
import type { KernelRegistry } from '../registry/kernel-registry.js';
|
|
5
|
+
import type { DecisionProfile, DecisionRequest, DecisionResult } from '../types.js';
|
|
6
|
+
/** Where the provider takes decision requests, as its preset states. */
|
|
7
|
+
declare const DECISION_ERROR_KINDS: {
|
|
8
|
+
readonly invalid_request: "request";
|
|
9
|
+
readonly authentication: "auth";
|
|
10
|
+
readonly permission: "auth";
|
|
11
|
+
readonly rate_limited: "rate_limit";
|
|
12
|
+
readonly unavailable: "unavailable";
|
|
13
|
+
readonly network: "network";
|
|
14
|
+
readonly timeout: "timeout";
|
|
15
|
+
readonly cancelled: "cancelled";
|
|
16
|
+
readonly malformed_response: "bad_response";
|
|
17
|
+
readonly disclosure_blocked: "blocked";
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A normalized native-decision failure; response bodies are deliberately omitted. What the
|
|
21
|
+
* provider path threw is its `cause`, and a failure the framework named reads as that failure.
|
|
22
|
+
*/
|
|
23
|
+
export declare class DecisionError extends TheoremError {
|
|
24
|
+
readonly code: keyof typeof DECISION_ERROR_KINDS;
|
|
25
|
+
readonly status?: number | undefined;
|
|
26
|
+
constructor(code: keyof typeof DECISION_ERROR_KINDS, message: string, status?: number | undefined, options?: ErrorOptions);
|
|
27
|
+
}
|
|
28
|
+
/** Options for running a decision: the key vault, transport utilities, and a trace sink that replaces the profile's. */
|
|
29
|
+
export interface RunDecisionOptions extends ProviderHostOptions {
|
|
30
|
+
/** Replaces the profile's `observability.writeTo`; an explicit sink always records, unsampled. */
|
|
31
|
+
sink?: TraceSink;
|
|
32
|
+
}
|
|
33
|
+
/** Check a decision before a host spends quota or dispatches it. Does not apply model-specific policy. */
|
|
34
|
+
export declare function validateDecisionRequest(request: Omit<DecisionRequest, 'state' | 'questions'> & {
|
|
35
|
+
state: unknown;
|
|
36
|
+
questions: unknown;
|
|
37
|
+
}, profile: DecisionProfile): asserts request is DecisionRequest;
|
|
38
|
+
/**
|
|
39
|
+
* Exactly one decision request, traced however it ends. An ambiguous POST is never retried,
|
|
40
|
+
* and a trace write failure never changes the decision's outcome.
|
|
41
|
+
*/
|
|
42
|
+
export declare function runDecisionInRegistry(registry: KernelRegistry, request: DecisionRequest, options: RunDecisionOptions): Promise<DecisionResult>;
|
|
43
|
+
export {};
|