@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,574 @@
|
|
|
1
|
+
import { canaryNote, userDataNote } from '../../../guardrails/canary.js';
|
|
2
|
+
import { isAbortError, TheoremError, throwIfAborted, withPublicWording, } from '../../../guardrails/error.js';
|
|
3
|
+
import { projectGuardrailTurnEvent } from '../../../guardrails/events.js';
|
|
4
|
+
import { lexiconText } from '../../../guardrails/lexicon.js';
|
|
5
|
+
import { sanitizeTurnRequestWithEvents } from '../../../guardrails/sanitize.js';
|
|
6
|
+
import { resolveTraceWriter } from '../../../observability/policy.js';
|
|
7
|
+
import { resolveObservabilityPolicy } from '../../../observability/resolve-policy.js';
|
|
8
|
+
import { writeTrace } from '../../../observability/trace.js';
|
|
9
|
+
import { buildRecord } from '../../../observability/trace-record.js';
|
|
10
|
+
import { startTrace, traceContent, } from '../../../observability/trace-span.js';
|
|
11
|
+
import { profileTypesForField } from '../../profile-scope.js';
|
|
12
|
+
import { requireModelBinding } from '../../registry/catalog.js';
|
|
13
|
+
import { resolveTurnInRegistry } from '../../registry/resolve.js';
|
|
14
|
+
import { bindSystem } from '../../system-parts.js';
|
|
15
|
+
import { cloneTurnToolSnapshot, expandT1Policy } from '../../tools/resolve.js';
|
|
16
|
+
import { turnDoneOf } from '../../turn-events.js';
|
|
17
|
+
import { findLast } from '../../util/find-last.js';
|
|
18
|
+
import { compactionMeter, compactionResult, compactorHistory, resolveCompactionTokens, shouldCompact, splitForCompaction, } from '../compaction.js';
|
|
19
|
+
import { mediaTokenFamily } from '../token-estimate.js';
|
|
20
|
+
import { endThrownSpan, endTurnSpan, guardrailAttributes, guardrailCheckAttributes, OutputFold, turnSpanOptions, } from '../turn-trace.js';
|
|
21
|
+
import { sumEventTokens } from '../usage.js';
|
|
22
|
+
import { runAttemptsWithValidation } from './gates.js';
|
|
23
|
+
import { applyTurnStage } from './stages.js';
|
|
24
|
+
import { openTurnState } from './state.js';
|
|
25
|
+
import { shouldSkipStreamEvent } from './stream.js';
|
|
26
|
+
function projectForObs(event, policy) {
|
|
27
|
+
return projectGuardrailTurnEvent(event, policy?.include.guardrailMatchPreview ?? false);
|
|
28
|
+
}
|
|
29
|
+
const MS_PER_S = 1000;
|
|
30
|
+
/**
|
|
31
|
+
* Record an event as delivered to the host; every yield to the host passes
|
|
32
|
+
* through here, so it is where an error gets the user's wording.
|
|
33
|
+
*/
|
|
34
|
+
function deliver(ctx, event) {
|
|
35
|
+
const out = withPublicWording(event, ctx.known?.lexicon);
|
|
36
|
+
if (out.type === 'text' && !ctx.shownText) {
|
|
37
|
+
ctx.shownText = true;
|
|
38
|
+
ctx.root.set({ 'theorem.turn.time_to_first_text': ctx.root.msSinceStart() / MS_PER_S });
|
|
39
|
+
}
|
|
40
|
+
ctx.seen.push(out);
|
|
41
|
+
ctx.delivered.add(out, ctx.root.nowUnixNano());
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
function* emitObservedStage(ctx, stageEv, profile) {
|
|
45
|
+
const stageOut = projectForObs(stageEv, ctx.observability);
|
|
46
|
+
if (shouldSkipStreamEvent(stageOut, profile))
|
|
47
|
+
return;
|
|
48
|
+
yield deliver(ctx, stageOut);
|
|
49
|
+
}
|
|
50
|
+
function getCompactionSpec(profile, modelId) {
|
|
51
|
+
return profile.models[modelId]?.compaction;
|
|
52
|
+
}
|
|
53
|
+
/** Errors only the host can fix: the compactor throws them rather than failing quietly every turn. */
|
|
54
|
+
/** A turn's tools when it offers none: an agent summarising its own history. */
|
|
55
|
+
function noTools() {
|
|
56
|
+
return { builtins: [], gated: [], visible: [], executable: [], wire: [] };
|
|
57
|
+
}
|
|
58
|
+
const COMPACTOR_THROWS = new Set(['config', 'request', 'auth', 'internal']);
|
|
59
|
+
function turnError(events) {
|
|
60
|
+
return events.find((e) => e.type === 'error');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Run the compaction profile as a turn under `span`: its spans join that
|
|
64
|
+
* record and it writes none of its own. Anything short of a completed,
|
|
65
|
+
* non-empty summary is a failure; the host's abort and `COMPACTOR_THROWS`
|
|
66
|
+
* errors are thrown. With no `spec.profile` the owner compacts itself, on the
|
|
67
|
+
* model it is compacting for and with no tools.
|
|
68
|
+
*/
|
|
69
|
+
async function runCompactor(args) {
|
|
70
|
+
const self = args.spec.profile === undefined;
|
|
71
|
+
const compactorId = args.spec.profile ?? args.owner;
|
|
72
|
+
const compactor = args.registry.profiles.get(compactorId);
|
|
73
|
+
const { history, droppedMedia } = compactorHistory(args.toCompact, compactor);
|
|
74
|
+
if (history.length === 0)
|
|
75
|
+
return { droppedMedia, failure: { unreadable: true } };
|
|
76
|
+
const req = {
|
|
77
|
+
profile: compactorId,
|
|
78
|
+
// The turn already passed model selection; its default needs no naming.
|
|
79
|
+
...(self && args.model !== compactor.defaultModel ? { model: args.model } : {}),
|
|
80
|
+
input: { text: lexiconText('compaction.request', {}, compactor.lexicon), history },
|
|
81
|
+
signal: args.signal,
|
|
82
|
+
};
|
|
83
|
+
const ctx = newTraceCtx(args.registry, req, args.parent.child(`invoke_agent ${req.profile}`, turnSpanOptions(req)), args.canaries);
|
|
84
|
+
ctx.compacting = self ? 'self' : 'other';
|
|
85
|
+
ctx.observability = resolveObservabilityPolicy(ctx.known?.observability);
|
|
86
|
+
const events = [];
|
|
87
|
+
try {
|
|
88
|
+
for await (const event of runTracedTurn(ctx, args.provider)) {
|
|
89
|
+
events.push(event);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
throwIfAborted(args.signal);
|
|
94
|
+
const error = err instanceof TheoremError ? err.kind : 'internal';
|
|
95
|
+
if (COMPACTOR_THROWS.has(error))
|
|
96
|
+
throw err;
|
|
97
|
+
return { droppedMedia, failure: { error } };
|
|
98
|
+
}
|
|
99
|
+
throwIfAborted(args.signal);
|
|
100
|
+
const tokens = sumEventTokens(events);
|
|
101
|
+
const usage = tokens ? { tokens } : {};
|
|
102
|
+
const stop = findLast(events, (e) => e.type === 'done')?.stop.kind;
|
|
103
|
+
const reported = turnError(events);
|
|
104
|
+
if (reported && COMPACTOR_THROWS.has(reported.errorKind)) {
|
|
105
|
+
throw new TheoremError(reported.errorKind, `Compactor '${compactorId}' failed: ${reported.errorInternal ?? reported.error}`);
|
|
106
|
+
}
|
|
107
|
+
const error = reported?.errorKind;
|
|
108
|
+
if (stop !== 'completed' || error) {
|
|
109
|
+
return {
|
|
110
|
+
droppedMedia,
|
|
111
|
+
failure: { ...(stop ? { stop } : {}), ...(error ? { error } : {}) },
|
|
112
|
+
...usage,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const structured = findLast(events, (e) => e.type === 'structured')?.structured;
|
|
116
|
+
const summary = structured === undefined
|
|
117
|
+
? events.flatMap((e) => (e.type === 'text' ? [e.text] : [])).join('')
|
|
118
|
+
: JSON.stringify(structured);
|
|
119
|
+
if (!summary.trim()) {
|
|
120
|
+
return { droppedMedia, failure: { stop, empty: true }, ...usage };
|
|
121
|
+
}
|
|
122
|
+
return { summary, droppedMedia, ...usage };
|
|
123
|
+
}
|
|
124
|
+
function lastTokensFromEvents(events) {
|
|
125
|
+
return findLast(events, (e) => e.type === 'tokens')?.tokens;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* One compaction decision as `theorem.compaction` attributes. The metered count
|
|
129
|
+
* is absent when it is unknown (meter `input` with no count to read).
|
|
130
|
+
*/
|
|
131
|
+
function compactionDecision(spec, decision, needed) {
|
|
132
|
+
return {
|
|
133
|
+
timing: spec.timing,
|
|
134
|
+
meter: compactionMeter(spec),
|
|
135
|
+
budget: spec.maxTokens,
|
|
136
|
+
threshold: spec.compactAt,
|
|
137
|
+
...(spec.trigger ? { trigger: 'custom' } : {}),
|
|
138
|
+
...(decision ? { tokens_before: decision.tokens, unknown_media: decision.unknownMedia } : {}),
|
|
139
|
+
needed,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/** A compaction that ran, as `theorem.compaction` attributes beside its decision. */
|
|
143
|
+
function compactionOutcome(result, messagesBefore) {
|
|
144
|
+
return {
|
|
145
|
+
outcome: result.outcome,
|
|
146
|
+
messages_before: messagesBefore,
|
|
147
|
+
messages_after: result.history.length,
|
|
148
|
+
dropped_media: result.droppedMedia,
|
|
149
|
+
...(result.failure?.stop ? { failure_stop: result.failure.stop } : {}),
|
|
150
|
+
...(result.failure?.error ? { failure_error: result.failure.error } : {}),
|
|
151
|
+
...(result.failure?.empty ? { failure_empty: true } : {}),
|
|
152
|
+
...(result.failure?.unreadable ? { failure_unreadable: true } : {}),
|
|
153
|
+
...(result.summary === undefined ? {} : { summary: traceContent(result.summary) }),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async function compactHistoryBeforeTurn(args) {
|
|
157
|
+
const decision = await resolveCompactionTokens({
|
|
158
|
+
spec: args.spec,
|
|
159
|
+
input: args.input,
|
|
160
|
+
family: args.family,
|
|
161
|
+
});
|
|
162
|
+
const needed = decision !== undefined && (await shouldCompact(decision, args.spec));
|
|
163
|
+
const attributes = compactionDecision(args.spec, decision, needed);
|
|
164
|
+
if (!(needed && decision)) {
|
|
165
|
+
args.parent.event('theorem.compaction', attributes);
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
const split = await splitForCompaction(args.history, args.spec, args.family);
|
|
169
|
+
if (split.toCompact.length === 0) {
|
|
170
|
+
args.parent.event('theorem.compaction', attributes);
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
const run = await runCompactor({ ...args, toCompact: split.toCompact });
|
|
174
|
+
const { toCompact: _compacted, ...result } = compactionResult(split, run, decision.tokens, args.spec);
|
|
175
|
+
args.parent.event('theorem.compaction', {
|
|
176
|
+
...attributes,
|
|
177
|
+
...compactionOutcome(result, args.history.length),
|
|
178
|
+
});
|
|
179
|
+
return {
|
|
180
|
+
type: 'compaction',
|
|
181
|
+
timing: 'before',
|
|
182
|
+
meter: decision.meter,
|
|
183
|
+
tokensBefore: decision.tokens,
|
|
184
|
+
unknownMedia: decision.unknownMedia,
|
|
185
|
+
messagesBefore: args.history.length,
|
|
186
|
+
messagesAfter: result.history.length,
|
|
187
|
+
...result,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
async function attachAfterCompaction(event, args) {
|
|
191
|
+
if (args.history.length === 0)
|
|
192
|
+
return event;
|
|
193
|
+
const prompt = lastTokensFromEvents(args.seen);
|
|
194
|
+
const decision = await resolveCompactionTokens({
|
|
195
|
+
spec: args.spec,
|
|
196
|
+
input: args.input,
|
|
197
|
+
prompt,
|
|
198
|
+
family: args.family,
|
|
199
|
+
});
|
|
200
|
+
const needed = decision !== undefined && (await shouldCompact(decision, args.spec));
|
|
201
|
+
args.span.event('theorem.compaction', compactionDecision(args.spec, decision, needed));
|
|
202
|
+
if (!(needed && decision))
|
|
203
|
+
return event;
|
|
204
|
+
const signal = {
|
|
205
|
+
needed: true,
|
|
206
|
+
meter: decision.meter,
|
|
207
|
+
tokens: decision.tokens,
|
|
208
|
+
unknownMedia: decision.unknownMedia,
|
|
209
|
+
history: args.history,
|
|
210
|
+
};
|
|
211
|
+
if (prompt && prompt.input > 0) {
|
|
212
|
+
signal.promptTokens = prompt.input;
|
|
213
|
+
if (prompt.estimated?.includes('input'))
|
|
214
|
+
signal.promptTokensEstimated = true;
|
|
215
|
+
}
|
|
216
|
+
return { ...event, compaction: signal };
|
|
217
|
+
}
|
|
218
|
+
async function* emitTurn(args) {
|
|
219
|
+
const { safe, profile, generation, system, provider } = args;
|
|
220
|
+
const state = openTurnState({
|
|
221
|
+
tools: args.registry.tools,
|
|
222
|
+
profile,
|
|
223
|
+
generation,
|
|
224
|
+
trace: args.trace,
|
|
225
|
+
mediaFamily: args.mediaFamily,
|
|
226
|
+
});
|
|
227
|
+
args.outState.state = state;
|
|
228
|
+
const pre = yield* applyTurnStage({
|
|
229
|
+
profile,
|
|
230
|
+
generation,
|
|
231
|
+
state,
|
|
232
|
+
stage: 'pre_turn',
|
|
233
|
+
step: 1,
|
|
234
|
+
onStage: safe.onStage,
|
|
235
|
+
signal: safe.signal,
|
|
236
|
+
host: generation.host,
|
|
237
|
+
});
|
|
238
|
+
if (pre.abort) {
|
|
239
|
+
const stop = {
|
|
240
|
+
kind: 'cancelled',
|
|
241
|
+
...(typeof pre.abort === 'object' && pre.abort.reason ? { native: pre.abort.reason } : {}),
|
|
242
|
+
};
|
|
243
|
+
const done = { type: 'done', stop, traceparent: state.trace.root.traceparent() };
|
|
244
|
+
state.allEmittedEvents.push(done);
|
|
245
|
+
yield done;
|
|
246
|
+
yield* applyTurnStage({
|
|
247
|
+
profile,
|
|
248
|
+
generation,
|
|
249
|
+
state,
|
|
250
|
+
stage: 'post_turn',
|
|
251
|
+
step: 1,
|
|
252
|
+
onStage: safe.onStage,
|
|
253
|
+
signal: safe.signal,
|
|
254
|
+
stop,
|
|
255
|
+
host: generation.host,
|
|
256
|
+
});
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
yield* runAttemptsWithValidation(safe, profile, generation, system, provider, state);
|
|
260
|
+
const tokens = sumEventTokens(state.allEmittedEvents);
|
|
261
|
+
const done = turnDoneOf({
|
|
262
|
+
stop: state.lastStop ?? { kind: 'completed' },
|
|
263
|
+
traceparent: state.trace.root.traceparent(),
|
|
264
|
+
...(tokens ? { tokens } : {}),
|
|
265
|
+
}, cloneTurnToolSnapshot(generation.tools));
|
|
266
|
+
state.allEmittedEvents.push(done);
|
|
267
|
+
yield done;
|
|
268
|
+
}
|
|
269
|
+
async function* streamTurnEvents(ctx, profile, gen, provider, compactionSpec) {
|
|
270
|
+
if (!ctx.safe || ctx.system === undefined || !ctx.trace)
|
|
271
|
+
return;
|
|
272
|
+
for await (const event of emitTurn({
|
|
273
|
+
registry: ctx.registry,
|
|
274
|
+
safe: ctx.safe,
|
|
275
|
+
profile,
|
|
276
|
+
generation: gen,
|
|
277
|
+
system: ctx.system,
|
|
278
|
+
provider,
|
|
279
|
+
trace: ctx.trace,
|
|
280
|
+
mediaFamily: ctx.mediaFamily,
|
|
281
|
+
outState: ctx,
|
|
282
|
+
})) {
|
|
283
|
+
const attached = await maybeAttachAfter(event, ctx, gen, compactionSpec);
|
|
284
|
+
const out = projectForObs(attached, ctx.observability);
|
|
285
|
+
if (!shouldSkipStreamEvent(out, profile))
|
|
286
|
+
yield deliver(ctx, out);
|
|
287
|
+
if (out.type === 'done' && ctx.state) {
|
|
288
|
+
// Skip duplicate post_turn when pre_turn abort already emitted it inside emitTurn.
|
|
289
|
+
const alreadyPost = ctx.state.allEmittedEvents.some((e) => e.type === 'stage' && e.stage === 'post_turn');
|
|
290
|
+
if (!alreadyPost) {
|
|
291
|
+
for await (const stageEv of applyTurnStage({
|
|
292
|
+
profile,
|
|
293
|
+
generation: gen,
|
|
294
|
+
state: ctx.state,
|
|
295
|
+
stage: 'post_turn',
|
|
296
|
+
step: Math.max(ctx.state.stepCount, 1),
|
|
297
|
+
onStage: ctx.safe.onStage,
|
|
298
|
+
signal: ctx.safe.signal,
|
|
299
|
+
stop: out.stop,
|
|
300
|
+
host: gen.host,
|
|
301
|
+
})) {
|
|
302
|
+
yield* emitObservedStage(ctx, stageEv, profile);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function newTraceCtx(registry, req, root, canaries) {
|
|
309
|
+
return {
|
|
310
|
+
registry,
|
|
311
|
+
req,
|
|
312
|
+
known: registry.profiles.find(req.profile),
|
|
313
|
+
seen: [],
|
|
314
|
+
delivered: new OutputFold(),
|
|
315
|
+
root,
|
|
316
|
+
compacting: false,
|
|
317
|
+
canary: '',
|
|
318
|
+
canaries,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function endTurn(ctx, thrown) {
|
|
322
|
+
const calls = ctx.trace?.calls ?? 0;
|
|
323
|
+
endTurnSpan(ctx.root, {
|
|
324
|
+
seen: ctx.seen,
|
|
325
|
+
delivered: ctx.delivered,
|
|
326
|
+
attempts: calls > 0 ? (ctx.trace?.attempt ?? 0) + 1 : 0,
|
|
327
|
+
calls,
|
|
328
|
+
...(thrown === undefined ? {} : { thrown }),
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
/** Run a turn under `ctx.root` and close it; an abort ends as a cancelled `done`. */
|
|
332
|
+
async function* runTracedTurn(ctx, provider) {
|
|
333
|
+
try {
|
|
334
|
+
yield* runTurnBody(ctx, provider);
|
|
335
|
+
}
|
|
336
|
+
catch (err) {
|
|
337
|
+
if (isAbortError(err)) {
|
|
338
|
+
yield* emitCancelledDoneAfterAbort(ctx);
|
|
339
|
+
endTurn(ctx);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
endTurn(ctx, err);
|
|
343
|
+
throw err;
|
|
344
|
+
}
|
|
345
|
+
endTurn(ctx);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The trace record is written however the turn ends, including when the host stops reading early;
|
|
349
|
+
* spans still open then close as `ERROR` / `unclosed`.
|
|
350
|
+
*/
|
|
351
|
+
async function* runTurnInRegistry(registry, req, provider, sinkOverride) {
|
|
352
|
+
const tree = startTrace(`invoke_agent ${req.profile}`, {
|
|
353
|
+
...turnSpanOptions(req),
|
|
354
|
+
...(req.traceparent ? { traceparent: req.traceparent } : {}),
|
|
355
|
+
});
|
|
356
|
+
const ctx = newTraceCtx(registry, req, tree.root, []);
|
|
357
|
+
const { sink, policy } = resolveTraceWriter({
|
|
358
|
+
override: sinkOverride,
|
|
359
|
+
observability: ctx.known?.observability,
|
|
360
|
+
});
|
|
361
|
+
ctx.observability = policy;
|
|
362
|
+
try {
|
|
363
|
+
yield* runTracedTurn(ctx, provider);
|
|
364
|
+
}
|
|
365
|
+
finally {
|
|
366
|
+
await writeTrace(sink, buildRecord({
|
|
367
|
+
spans: tree.collect(),
|
|
368
|
+
policy,
|
|
369
|
+
canaries: ctx.canaries,
|
|
370
|
+
...(req.metadata ? { metadata: req.metadata } : {}),
|
|
371
|
+
}), policy);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
/** A host abort ends with a cancelled `done` + `post_turn`, not a bare throw. */
|
|
375
|
+
async function* emitCancelledDoneAfterAbort(ctx) {
|
|
376
|
+
if (ctx.seen.some((e) => e.type === 'done'))
|
|
377
|
+
return;
|
|
378
|
+
const stop = { kind: 'cancelled' };
|
|
379
|
+
const done = { type: 'done', stop, traceparent: ctx.root.traceparent() };
|
|
380
|
+
const outDone = projectForObs(done, ctx.observability);
|
|
381
|
+
yield deliver(ctx, outDone);
|
|
382
|
+
const profile = ctx.known;
|
|
383
|
+
const gen = ctx.generation;
|
|
384
|
+
if (!(profile && gen && ctx.safe && ctx.trace))
|
|
385
|
+
return;
|
|
386
|
+
// The turn's own state when it got that far, so post_turn sees the history the model saw.
|
|
387
|
+
const state = ctx.state ??
|
|
388
|
+
openTurnState({
|
|
389
|
+
tools: ctx.registry.tools,
|
|
390
|
+
profile,
|
|
391
|
+
generation: gen,
|
|
392
|
+
trace: ctx.trace,
|
|
393
|
+
mediaFamily: ctx.mediaFamily,
|
|
394
|
+
allEmittedEvents: [...ctx.seen],
|
|
395
|
+
});
|
|
396
|
+
for await (const stageEv of applyTurnStage({
|
|
397
|
+
profile,
|
|
398
|
+
generation: gen,
|
|
399
|
+
state,
|
|
400
|
+
stage: 'post_turn',
|
|
401
|
+
step: Math.max(state.stepCount, 1),
|
|
402
|
+
onStage: ctx.safe.onStage,
|
|
403
|
+
stop,
|
|
404
|
+
host: gen.host,
|
|
405
|
+
})) {
|
|
406
|
+
yield* emitObservedStage(ctx, stageEv, profile);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
async function* runTurnBody(ctx, provider) {
|
|
410
|
+
const checkStart = performance.now();
|
|
411
|
+
const sanitized = sanitizeTurnRequestWithEvents(ctx.req, ctx.registry.profiles.get(ctx.req.profile));
|
|
412
|
+
const checkMs = performance.now() - checkStart;
|
|
413
|
+
ctx.safe = sanitized.request;
|
|
414
|
+
const hits = sanitized.events.filter((event) => event.type === 'guardrail');
|
|
415
|
+
// The check's time rides on its last decision, or on a pass when it found nothing.
|
|
416
|
+
if (hits.length === 0) {
|
|
417
|
+
ctx.root.event('theorem.guardrail', guardrailCheckAttributes('input', checkMs, undefined, { stage: 'input', trust: 'untrusted' }));
|
|
418
|
+
}
|
|
419
|
+
for (const event of sanitized.events) {
|
|
420
|
+
if (event.type === 'guardrail') {
|
|
421
|
+
ctx.root.event('theorem.guardrail', event === hits.at(-1)
|
|
422
|
+
? guardrailCheckAttributes('input', checkMs, event.guardrail, event.guardrail)
|
|
423
|
+
: guardrailAttributes(event.guardrail));
|
|
424
|
+
}
|
|
425
|
+
yield deliver(ctx, projectForObs(event, ctx.observability));
|
|
426
|
+
}
|
|
427
|
+
const { profile, generation: gen } = resolveTurnInRegistry(ctx.registry, ctx.safe);
|
|
428
|
+
await expandT1Policy(ctx.registry.tools, gen.tools, profile, ctx.safe);
|
|
429
|
+
if (ctx.compacting === 'self')
|
|
430
|
+
gen.tools = noTools();
|
|
431
|
+
gen.builtins = gen.tools.builtins;
|
|
432
|
+
ctx.generation = gen;
|
|
433
|
+
ctx.canary = gen.canary;
|
|
434
|
+
ctx.canaries.push(gen.canary);
|
|
435
|
+
const binding = profile.models[gen.model];
|
|
436
|
+
ctx.mediaFamily = binding ? mediaTokenFamily(binding) : undefined;
|
|
437
|
+
ctx.trace = { root: ctx.root, attempt: 0, calls: 0, binding };
|
|
438
|
+
throwIfAborted(ctx.safe.signal);
|
|
439
|
+
const compactionSpec = ctx.compacting ? undefined : getCompactionSpec(profile, gen.model);
|
|
440
|
+
const compaction = await maybeCompactBefore(ctx, profile, gen, compactionSpec, provider);
|
|
441
|
+
if (compaction)
|
|
442
|
+
yield deliver(ctx, projectForObs(compaction, ctx.observability));
|
|
443
|
+
ctx.system = bindSystem(gen.resolvedSystem, [
|
|
444
|
+
canaryNote(ctx.canary, profile.lexicon),
|
|
445
|
+
profileTypesForField('identity.system').includes(profile.type)
|
|
446
|
+
? userDataNote(profile.lexicon)
|
|
447
|
+
: '',
|
|
448
|
+
]);
|
|
449
|
+
yield* streamTurnEvents(ctx, profile, gen, provider, compactionSpec);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* The turn's provider runs the compactor only when it is the one the compactor
|
|
453
|
+
* would get: a text profile on the same protocol and provider.
|
|
454
|
+
*/
|
|
455
|
+
function compactorProvider(ctx, profile, spec, provider) {
|
|
456
|
+
if (ctx.req.compactionProvider)
|
|
457
|
+
return ctx.req.compactionProvider;
|
|
458
|
+
if (spec.profile === undefined)
|
|
459
|
+
return provider;
|
|
460
|
+
const compactor = ctx.registry.profiles.get(spec.profile);
|
|
461
|
+
const own = ctx.trace?.binding;
|
|
462
|
+
const theirs = compactor.models[compactor.defaultModel];
|
|
463
|
+
if (profile.type !== 'text' ||
|
|
464
|
+
own?.provider !== theirs?.provider ||
|
|
465
|
+
own?.protocol !== theirs?.protocol) {
|
|
466
|
+
throw new TheoremError('config', `Profile ${profile.id} compacts with '${spec.profile}', which this turn's provider cannot run; pass compactionProvider`);
|
|
467
|
+
}
|
|
468
|
+
return provider;
|
|
469
|
+
}
|
|
470
|
+
async function maybeCompactBefore(ctx, profile, gen, compactionSpec, provider) {
|
|
471
|
+
if (!(compactionSpec?.timing === 'before' && ctx.safe))
|
|
472
|
+
return undefined;
|
|
473
|
+
const compactor = compactorProvider(ctx, profile, compactionSpec, provider);
|
|
474
|
+
if (!gen.history?.length)
|
|
475
|
+
return undefined;
|
|
476
|
+
const compaction = await compactHistoryBeforeTurn({
|
|
477
|
+
registry: ctx.registry,
|
|
478
|
+
owner: profile.id,
|
|
479
|
+
model: gen.model,
|
|
480
|
+
spec: compactionSpec,
|
|
481
|
+
family: ctx.mediaFamily,
|
|
482
|
+
history: gen.history,
|
|
483
|
+
input: ctx.safe.input,
|
|
484
|
+
provider: compactor,
|
|
485
|
+
parent: ctx.root,
|
|
486
|
+
canaries: ctx.canaries,
|
|
487
|
+
signal: ctx.safe.signal,
|
|
488
|
+
});
|
|
489
|
+
if (compaction)
|
|
490
|
+
gen.history = compaction.history;
|
|
491
|
+
return compaction;
|
|
492
|
+
}
|
|
493
|
+
async function maybeAttachAfter(event, ctx, gen, compactionSpec) {
|
|
494
|
+
if (!(event.type === 'done' && compactionSpec?.timing === 'after' && ctx.safe)) {
|
|
495
|
+
return event;
|
|
496
|
+
}
|
|
497
|
+
return await attachAfterCompaction(event, {
|
|
498
|
+
spec: compactionSpec,
|
|
499
|
+
family: ctx.mediaFamily,
|
|
500
|
+
history: gen.history ?? [],
|
|
501
|
+
input: ctx.safe.input,
|
|
502
|
+
seen: ctx.seen,
|
|
503
|
+
span: ctx.root,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* `timing: 'after'`: run the compactor on the history `done.compaction` carried,
|
|
508
|
+
* with the same split, failure rules and trace as `before`. `provider` runs the
|
|
509
|
+
* compactor. `undefined` when the split leaves nothing to compact.
|
|
510
|
+
*/
|
|
511
|
+
async function compactHistoryInRegistry(registry, req, provider, sinkOverride) {
|
|
512
|
+
const profile = registry.profiles.get(req.profile);
|
|
513
|
+
const model = req.model ?? profile.defaultModel;
|
|
514
|
+
const binding = requireModelBinding(profile, model);
|
|
515
|
+
const spec = binding.compaction;
|
|
516
|
+
if (!spec) {
|
|
517
|
+
throw new TheoremError('config', `Profile ${profile.id} model '${model}' has no compaction`);
|
|
518
|
+
}
|
|
519
|
+
const family = mediaTokenFamily(binding);
|
|
520
|
+
const split = await splitForCompaction(req.history, spec, family);
|
|
521
|
+
if (split.toCompact.length === 0)
|
|
522
|
+
return undefined;
|
|
523
|
+
const turnReq = {
|
|
524
|
+
profile: req.profile,
|
|
525
|
+
input: {},
|
|
526
|
+
...(req.conversationId ? { conversationId: req.conversationId } : {}),
|
|
527
|
+
};
|
|
528
|
+
const tree = startTrace(`invoke_agent ${req.profile}`, {
|
|
529
|
+
...turnSpanOptions(turnReq),
|
|
530
|
+
...(req.traceparent ? { traceparent: req.traceparent } : {}),
|
|
531
|
+
});
|
|
532
|
+
const { sink, policy } = resolveTraceWriter({
|
|
533
|
+
override: sinkOverride,
|
|
534
|
+
observability: profile.observability,
|
|
535
|
+
});
|
|
536
|
+
const canaries = [];
|
|
537
|
+
try {
|
|
538
|
+
const run = await runCompactor({
|
|
539
|
+
registry,
|
|
540
|
+
owner: profile.id,
|
|
541
|
+
model,
|
|
542
|
+
toCompact: split.toCompact,
|
|
543
|
+
spec,
|
|
544
|
+
provider,
|
|
545
|
+
parent: tree.root,
|
|
546
|
+
canaries,
|
|
547
|
+
...(req.signal ? { signal: req.signal } : {}),
|
|
548
|
+
});
|
|
549
|
+
const result = compactionResult(split, run, req.tokens, spec);
|
|
550
|
+
tree.root.event('theorem.compaction', {
|
|
551
|
+
timing: spec.timing,
|
|
552
|
+
meter: compactionMeter(spec),
|
|
553
|
+
budget: spec.maxTokens,
|
|
554
|
+
threshold: spec.compactAt,
|
|
555
|
+
tokens_before: req.tokens,
|
|
556
|
+
...compactionOutcome(result, req.history.length),
|
|
557
|
+
});
|
|
558
|
+
tree.root.end();
|
|
559
|
+
return result;
|
|
560
|
+
}
|
|
561
|
+
catch (err) {
|
|
562
|
+
endThrownSpan(tree.root, err);
|
|
563
|
+
throw err;
|
|
564
|
+
}
|
|
565
|
+
finally {
|
|
566
|
+
await writeTrace(sink, buildRecord({
|
|
567
|
+
spans: tree.collect(),
|
|
568
|
+
policy,
|
|
569
|
+
canaries,
|
|
570
|
+
...(req.metadata ? { metadata: req.metadata } : {}),
|
|
571
|
+
}), policy);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
export { compactHistoryInRegistry, runTurnInRegistry };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ProfileValidator } from '../../types.js';
|
|
2
|
+
interface ValidationFailure {
|
|
3
|
+
path: string;
|
|
4
|
+
error: string;
|
|
5
|
+
}
|
|
6
|
+
declare function isAbsent(value: unknown): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Required vs optional comes only from the JSON Schema; host field validators run for required
|
|
9
|
+
* paths and for optional paths that are present. Throws when the root is not an object schema.
|
|
10
|
+
*/
|
|
11
|
+
declare function collectValidationFailures(jsonSchema: Record<string, unknown>, structured: unknown, fields: Record<string, ProfileValidator> | undefined, slots?: Record<string, string>): Promise<ValidationFailure[]>;
|
|
12
|
+
declare function formatValidationFailures(failures: ValidationFailure[]): string;
|
|
13
|
+
export type { ValidationFailure };
|
|
14
|
+
export { collectValidationFailures, formatValidationFailures, isAbsent };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { TheoremError } from '../../../guardrails/error.js';
|
|
2
|
+
function isAbsent(value) {
|
|
3
|
+
return value === undefined || value === null;
|
|
4
|
+
}
|
|
5
|
+
function asRecord(value) {
|
|
6
|
+
if (isAbsent(value) || typeof value !== 'object' || Array.isArray(value)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
function asObjectSchema(schema) {
|
|
12
|
+
const rec = asRecord(schema);
|
|
13
|
+
if (!rec) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (rec.type !== undefined && rec.type !== 'object') {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return rec;
|
|
20
|
+
}
|
|
21
|
+
function requiredKeys(schema) {
|
|
22
|
+
const raw = schema.required;
|
|
23
|
+
if (!Array.isArray(raw)) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
return raw.filter((k) => typeof k === 'string');
|
|
27
|
+
}
|
|
28
|
+
function propertySchemas(schema) {
|
|
29
|
+
const props = schema.properties;
|
|
30
|
+
return asRecord(props) ?? {};
|
|
31
|
+
}
|
|
32
|
+
function joinPath(prefix, key) {
|
|
33
|
+
return prefix ? `${prefix}.${key}` : key;
|
|
34
|
+
}
|
|
35
|
+
function pushMissing(path, failures) {
|
|
36
|
+
failures.push({ path, error: `required field '${path}' is missing` }); // lexicon-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
|
|
37
|
+
}
|
|
38
|
+
async function runFieldValidator(path, value, validator, slots, failures) {
|
|
39
|
+
if (!validator) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const check = await validator(value, slots);
|
|
43
|
+
if (check.isValid) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
failures.push({
|
|
47
|
+
path,
|
|
48
|
+
error: check.error || check.finding || `Validation failed for '${path}'`, // lexicon-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async function visitProperty(args) {
|
|
52
|
+
const { path, propSchema, child, isRequired, fields, slots, failures } = args;
|
|
53
|
+
if (isAbsent(child)) {
|
|
54
|
+
if (isRequired) {
|
|
55
|
+
pushMissing(path, failures);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
await runFieldValidator(path, child, fields?.[path], slots, failures);
|
|
60
|
+
const nested = asObjectSchema(propSchema);
|
|
61
|
+
const nestedValue = nested ? asRecord(child) : null;
|
|
62
|
+
if (nested && nestedValue) {
|
|
63
|
+
await walkObject(nested, nestedValue, path, fields, slots, failures);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function walkObject(schema, value, pathPrefix, fields, slots, failures) {
|
|
67
|
+
const props = propertySchemas(schema);
|
|
68
|
+
const required = new Set(requiredKeys(schema));
|
|
69
|
+
const keys = new Set([...Object.keys(props), ...required]);
|
|
70
|
+
const record = asRecord(value);
|
|
71
|
+
for (const key of keys) {
|
|
72
|
+
await visitProperty({
|
|
73
|
+
path: joinPath(pathPrefix, key),
|
|
74
|
+
propSchema: props[key],
|
|
75
|
+
child: record?.[key],
|
|
76
|
+
isRequired: required.has(key),
|
|
77
|
+
fields,
|
|
78
|
+
slots,
|
|
79
|
+
failures,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Required vs optional comes only from the JSON Schema; host field validators run for required
|
|
85
|
+
* paths and for optional paths that are present. Throws when the root is not an object schema.
|
|
86
|
+
*/
|
|
87
|
+
async function collectValidationFailures(jsonSchema, structured, fields, slots) {
|
|
88
|
+
const root = asObjectSchema(jsonSchema);
|
|
89
|
+
if (!root) {
|
|
90
|
+
throw new TheoremError('config', 'structured validation requires a JSON Schema object root'); // lexicon-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
|
|
91
|
+
}
|
|
92
|
+
const failures = [];
|
|
93
|
+
if (!asRecord(structured)) {
|
|
94
|
+
for (const key of requiredKeys(root)) {
|
|
95
|
+
pushMissing(key, failures);
|
|
96
|
+
}
|
|
97
|
+
return failures;
|
|
98
|
+
}
|
|
99
|
+
await walkObject(root, structured, '', fields, slots, failures);
|
|
100
|
+
return failures;
|
|
101
|
+
}
|
|
102
|
+
function formatValidationFailures(failures) {
|
|
103
|
+
return failures.map((f) => f.error).join('; ');
|
|
104
|
+
}
|
|
105
|
+
export { collectValidationFailures, formatValidationFailures, isAbsent };
|