@theoremjs/agents 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +277 -995
- package/esm/mod.d.ts +34 -20
- package/esm/mod.js +19 -12
- package/esm/src/cli/commands/bench.js +36 -14
- 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 +3 -3
- package/esm/src/cli/commands/fuzz-canary.js +55 -16
- package/esm/src/cli/commands/profile.js +4 -4
- package/esm/src/cli/commands/run.d.ts +2 -2
- package/esm/src/cli/commands/run.js +1 -1
- package/esm/src/cli/commands/test.d.ts +4 -3
- package/esm/src/cli/commands/test.js +2 -2
- package/esm/src/cli/index.js +9 -9
- package/esm/src/cli/matrix/fixtures.js +25 -30
- package/esm/src/evals/attachments.d.ts +1 -0
- package/esm/src/evals/attachments.js +4 -2
- package/esm/src/evals/breakdown.d.ts +2 -0
- package/esm/src/evals/breakdown.js +1 -0
- package/esm/src/evals/graders/answer.d.ts +3 -0
- package/esm/src/evals/graders/answer.js +1 -0
- package/esm/src/evals/graders/code.d.ts +7 -31
- package/esm/src/evals/graders/code.js +4 -25
- package/esm/src/evals/graders/judge.d.ts +4 -0
- package/esm/src/evals/graders/judge.js +7 -5
- package/esm/src/evals/graders/latency.d.ts +1 -0
- package/esm/src/evals/graders/latency.js +3 -2
- package/esm/src/evals/graders/live.d.ts +3 -0
- package/esm/src/evals/graders/live.js +2 -0
- package/esm/src/evals/graders/media.js +1 -2
- package/esm/src/evals/graders/shared.js +1 -1
- package/esm/src/evals/graders/transcript.d.ts +2 -0
- package/esm/src/evals/graders/transcript.js +5 -3
- package/esm/src/evals/mod.d.ts +1 -9
- package/esm/src/evals/mod.js +1 -9
- package/esm/src/evals/record.d.ts +6 -0
- package/esm/src/evals/record.js +4 -2
- package/esm/src/evals/rubrics/mod.d.ts +1 -1
- package/esm/src/evals/rubrics/mod.js +1 -1
- package/esm/src/evals/rubrics/mustache.js +1 -1
- package/esm/src/evals/rubrics/types.js +1 -1
- package/esm/src/evals/run.d.ts +9 -52
- package/esm/src/evals/run.js +12 -46
- package/esm/src/evals/suite.d.ts +12 -3
- package/esm/src/evals/suite.js +9 -4
- package/esm/src/evals/summary.d.ts +3 -0
- package/esm/src/evals/summary.js +1 -0
- package/esm/src/evals/trial.d.ts +3 -16
- package/esm/src/evals/trial.js +7 -30
- package/esm/src/evals/types.d.ts +30 -121
- package/esm/src/evals/types.js +3 -16
- package/esm/src/evals/verdict.d.ts +5 -0
- package/esm/src/evals/verdict.js +4 -1
- 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 +1 -0
- package/esm/src/guardrails/canary.d.ts +49 -15
- package/esm/src/guardrails/canary.js +103 -30
- package/esm/src/guardrails/compile-egress.d.ts +6 -5
- package/esm/src/guardrails/compile-egress.js +6 -5
- package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +3 -1
- package/esm/src/guardrails/corpus/canary-egress-attacks.js +4 -2
- package/esm/src/guardrails/corpus/fuzz-inbound.js +14 -17
- package/esm/src/guardrails/corpus/inbound-payloads.d.ts +2 -0
- package/esm/src/guardrails/corpus/inbound-payloads.js +4 -2
- package/esm/src/guardrails/corpus/live-attacks.d.ts +2 -0
- package/esm/src/guardrails/corpus/live-attacks.js +6 -1
- package/esm/src/guardrails/corpus/mod.d.ts +1 -1
- package/esm/src/guardrails/corpus/secrets.d.ts +3 -3
- package/esm/src/guardrails/corpus/secrets.js +2 -2
- package/esm/src/guardrails/corpus/strings.d.ts +1 -1
- package/esm/src/guardrails/corpus/strings.js +1 -1
- package/esm/src/guardrails/corpus/types.d.ts +5 -1
- 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 +4 -6
- package/esm/src/guardrails/egress-automata.js +24095 -4572
- package/esm/src/guardrails/egress-compiler.d.ts +38 -16
- package/esm/src/guardrails/egress-compiler.js +254 -60
- package/esm/src/guardrails/egress-patterns.d.ts +4 -2
- package/esm/src/guardrails/egress-patterns.js +6 -2
- package/esm/src/guardrails/egress-stream.d.ts +54 -36
- package/esm/src/guardrails/egress-stream.js +253 -124
- package/esm/src/guardrails/egress-urls.d.ts +6 -3
- package/esm/src/guardrails/egress-urls.js +14 -22
- package/esm/src/guardrails/egress.d.ts +26 -86
- package/esm/src/guardrails/egress.js +59 -309
- package/esm/src/guardrails/error.d.ts +13 -8
- package/esm/src/guardrails/error.js +25 -18
- package/esm/src/guardrails/event-schemas.d.ts +46 -3
- package/esm/src/guardrails/event-schemas.js +12 -5
- package/esm/src/guardrails/events.d.ts +5 -1
- package/esm/src/guardrails/events.js +4 -1
- package/esm/src/guardrails/hints.d.ts +14 -0
- package/esm/src/guardrails/hints.js +57 -0
- package/esm/src/guardrails/hits.d.ts +7 -1
- package/esm/src/guardrails/hits.js +12 -1
- 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 +6 -1
- package/esm/src/guardrails/injection-patterns.js +33 -1
- 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 +17 -1
- package/esm/src/guardrails/injection.js +259 -36
- package/esm/src/guardrails/labels.d.ts +40 -0
- package/esm/src/guardrails/labels.js +881 -0
- package/esm/src/guardrails/live-outbound-gate.d.ts +9 -8
- package/esm/src/guardrails/live-outbound-gate.js +100 -107
- package/esm/src/guardrails/mod.d.ts +30 -22
- package/esm/src/guardrails/mod.js +19 -14
- package/esm/src/guardrails/network.d.ts +4 -0
- package/esm/src/guardrails/network.js +21 -19
- package/esm/src/guardrails/normalize.d.ts +15 -1
- package/esm/src/guardrails/normalize.js +34 -1
- package/esm/src/guardrails/policy.d.ts +2 -17
- package/esm/src/guardrails/policy.js +12 -41
- package/esm/src/guardrails/progressive-yield.d.ts +65 -24
- package/esm/src/guardrails/progressive-yield.js +191 -147
- package/esm/src/guardrails/prompt-echo.d.ts +4 -2
- package/esm/src/guardrails/prompt-echo.js +87 -6
- package/esm/src/guardrails/quota.d.ts +4 -1
- package/esm/src/guardrails/quota.js +7 -2
- package/esm/src/guardrails/rules.d.ts +20 -29
- package/esm/src/guardrails/rules.js +23 -30
- package/esm/src/guardrails/sanitize.d.ts +32 -15
- package/esm/src/guardrails/sanitize.js +100 -124
- 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 +16 -23
- package/esm/src/guardrails/sensitive.js +168 -47
- package/esm/src/guardrails/serialize.d.ts +1 -0
- package/esm/src/guardrails/testing.d.ts +4 -2
- package/esm/src/guardrails/testing.js +4 -2
- package/esm/src/guardrails/theorem-error.d.ts +5 -2
- package/esm/src/guardrails/theorem-error.js +6 -3
- package/esm/src/guardrails/thought-guard.d.ts +34 -13
- package/esm/src/guardrails/thought-guard.js +264 -64
- package/esm/src/guardrails/tool-directives.d.ts +29 -10
- package/esm/src/guardrails/tool-directives.js +133 -47
- 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 +45 -14
- package/esm/src/guardrails/tool-result.js +94 -70
- package/esm/src/guardrails/types.d.ts +79 -71
- package/esm/src/guardrails/types.js +11 -7
- package/esm/src/host/client-turn.d.ts +29 -2
- package/esm/src/host/client-turn.js +46 -4
- package/esm/src/host/mint-trace.d.ts +1 -0
- package/esm/src/host/mint-trace.js +3 -2
- package/esm/src/host/mod.d.ts +2 -2
- package/esm/src/host/mod.js +1 -1
- package/esm/src/host/readStreamingJsonStringField.js +2 -43
- package/esm/src/host/reply.d.ts +6 -0
- package/esm/src/host/reply.js +8 -1
- package/esm/src/interface/blocks.d.ts +3 -0
- package/esm/src/interface/blocks.js +84 -14
- package/esm/src/interface/composer-actions.d.ts +4 -0
- package/esm/src/interface/composer-actions.js +5 -3
- package/esm/src/interface/draft.d.ts +6 -0
- package/esm/src/interface/draft.js +22 -11
- package/esm/src/interface/from-profile.d.ts +13 -1
- package/esm/src/interface/from-profile.js +66 -31
- package/esm/src/interface/history.d.ts +9 -3
- package/esm/src/interface/history.js +33 -24
- package/esm/src/interface/inputs.d.ts +1 -0
- package/esm/src/interface/inputs.js +3 -0
- package/esm/src/interface/mod.d.ts +4 -2
- package/esm/src/interface/mod.js +3 -2
- package/esm/src/interface/models.d.ts +7 -0
- package/esm/src/interface/models.js +5 -0
- package/esm/src/interface/pending.d.ts +9 -0
- package/esm/src/interface/pending.js +9 -3
- package/esm/src/interface/profile-interface.js +54 -40
- package/esm/src/interface/session.d.ts +12 -5
- package/esm/src/interface/session.js +17 -10
- 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 +14 -2
- package/esm/src/interface/tool-calls.js +64 -10
- package/esm/src/interface/tool-invoke.d.ts +2 -0
- package/esm/src/interface/tool-invoke.js +3 -1
- package/esm/src/interface/tool-media.d.ts +2 -0
- package/esm/src/interface/tool-media.js +2 -1
- package/esm/src/interface/types.d.ts +105 -40
- package/esm/src/interface/types.js +1 -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/crypto.d.ts +7 -2
- package/esm/src/kernel/auth/crypto.js +12 -10
- package/esm/src/kernel/auth/oauth.d.ts +2 -1
- package/esm/src/kernel/auth/oauth.js +23 -22
- package/esm/src/kernel/auth/typed-secret.js +2 -2
- package/esm/src/kernel/auth/types.d.ts +13 -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 +43 -5
- package/esm/src/kernel/default-scope.js +54 -5
- 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 +6 -5
- package/esm/src/kernel/engine/compaction.js +12 -11
- package/esm/src/kernel/engine/decision.d.ts +10 -8
- package/esm/src/kernel/engine/decision.js +174 -146
- package/esm/src/kernel/engine/embedding.d.ts +8 -0
- package/esm/src/kernel/engine/embedding.js +118 -0
- package/esm/src/kernel/engine/live-inbound.d.ts +14 -4
- package/esm/src/kernel/engine/live-inbound.js +36 -11
- package/esm/src/kernel/engine/live-ingress.d.ts +6 -0
- package/esm/src/kernel/engine/live-ingress.js +6 -1
- package/esm/src/kernel/engine/repair.js +12 -12
- package/esm/src/kernel/engine/runner/gates.d.ts +2 -2
- package/esm/src/kernel/engine/runner/gates.js +287 -101
- package/esm/src/kernel/engine/runner/mod.d.ts +19 -5
- package/esm/src/kernel/engine/runner/mod.js +107 -56
- 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 +24 -3
- package/esm/src/kernel/engine/runner/schema-validation.js +78 -4
- package/esm/src/kernel/engine/runner/stages.d.ts +1 -1
- package/esm/src/kernel/engine/runner/stages.js +1 -8
- package/esm/src/kernel/engine/runner/state.d.ts +33 -12
- package/esm/src/kernel/engine/runner/state.js +11 -1
- 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 +2 -2
- package/esm/src/kernel/engine/runner/steps.js +329 -169
- package/esm/src/kernel/engine/runner/stream.d.ts +42 -11
- package/esm/src/kernel/engine/runner/stream.js +197 -46
- package/esm/src/kernel/engine/runner/usage.d.ts +5 -5
- package/esm/src/kernel/engine/runner/usage.js +12 -14
- package/esm/src/kernel/engine/session/mod.d.ts +8 -6
- package/esm/src/kernel/engine/session/mod.js +360 -129
- package/esm/src/kernel/engine/session/session-trace.d.ts +10 -10
- package/esm/src/kernel/engine/session/session-trace.js +41 -30
- package/esm/src/kernel/engine/token-estimate.d.ts +10 -64
- package/esm/src/kernel/engine/token-estimate.js +14 -194
- package/esm/src/kernel/engine/tool-trace.d.ts +3 -3
- package/esm/src/kernel/engine/tool-trace.js +2 -1
- package/esm/src/kernel/engine/turn-trace.d.ts +8 -7
- package/esm/src/kernel/engine/turn-trace.js +28 -36
- package/esm/src/kernel/engine/usage.d.ts +6 -0
- package/esm/src/kernel/engine/usage.js +17 -0
- package/esm/src/kernel/interaction-parts.d.ts +3 -0
- package/esm/src/kernel/interaction-parts.js +18 -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 +38 -11
- package/esm/src/kernel/mod.js +22 -7
- package/esm/src/kernel/profile-graph.d.ts +11 -6
- package/esm/src/kernel/profile-graph.js +14 -7
- package/esm/src/kernel/profile-presence.d.ts +10 -1
- package/esm/src/kernel/profile-presence.js +90 -75
- package/esm/src/kernel/profile-scope.d.ts +7 -3
- package/esm/src/kernel/profile-scope.js +67 -55
- 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 +15 -4
- package/esm/src/kernel/registry/attachments.js +31 -22
- package/esm/src/kernel/registry/catalog.d.ts +6 -0
- package/esm/src/kernel/registry/catalog.js +9 -2
- 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 +6 -2
- package/esm/src/kernel/registry/ingress.js +55 -10
- package/esm/src/kernel/registry/kernel-registry.d.ts +3 -0
- package/esm/src/kernel/registry/kernel-registry.js +9 -2
- package/esm/src/kernel/registry/profile-outputs.js +4 -1
- package/esm/src/kernel/registry/profiles.d.ts +38 -14
- package/esm/src/kernel/registry/profiles.js +280 -205
- package/esm/src/kernel/registry/provider-request.d.ts +3 -3
- package/esm/src/kernel/registry/provider-request.js +5 -13
- package/esm/src/kernel/registry/resolve.d.ts +4 -1
- package/esm/src/kernel/registry/resolve.js +59 -58
- package/esm/src/kernel/registry/schemas.d.ts +3 -0
- package/esm/src/kernel/registry/schemas.js +3 -1
- 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/system-prompt.js +7 -8
- package/esm/src/kernel/registry/vault.d.ts +14 -13
- package/esm/src/kernel/registry/vault.js +1 -14
- package/esm/src/kernel/schema.d.ts +100 -46
- package/esm/src/kernel/schema.js +499 -257
- package/esm/src/kernel/scope.d.ts +12 -5
- package/esm/src/kernel/scope.js +23 -3
- package/esm/src/kernel/stages.d.ts +38 -3
- package/esm/src/kernel/stages.js +82 -39
- package/esm/src/kernel/stop.d.ts +29 -6
- package/esm/src/kernel/stop.js +16 -4
- package/esm/src/kernel/system-parts.js +6 -6
- package/esm/src/kernel/tools/activity-label.d.ts +12 -6
- package/esm/src/kernel/tools/activity-label.js +258 -32
- 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 +21 -6
- package/esm/src/kernel/tools/events.js +53 -18
- package/esm/src/kernel/tools/execute.d.ts +47 -11
- package/esm/src/kernel/tools/execute.js +508 -136
- package/esm/src/kernel/tools/gate-answer.d.ts +12 -2
- package/esm/src/kernel/tools/gate-answer.js +17 -6
- 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 +1 -0
- package/esm/src/kernel/tools/harness.js +2 -1
- 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 +27 -5
- package/esm/src/kernel/tools/invoke.js +223 -70
- package/esm/src/kernel/tools/mcp-sessions.d.ts +4 -1
- package/esm/src/kernel/tools/mcp-sessions.js +13 -8
- package/esm/src/kernel/tools/mod.d.ts +4 -3
- package/esm/src/kernel/tools/mod.js +3 -2
- package/esm/src/kernel/tools/model-text.d.ts +10 -5
- package/esm/src/kernel/tools/model-text.js +18 -12
- 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 +11 -2
- package/esm/src/kernel/tools/permission.js +12 -12
- package/esm/src/kernel/tools/project.d.ts +4 -1
- package/esm/src/kernel/tools/project.js +32 -2
- package/esm/src/kernel/tools/registry.d.ts +9 -2
- package/esm/src/kernel/tools/registry.js +69 -10
- package/esm/src/kernel/tools/remote.d.ts +40 -6
- package/esm/src/kernel/tools/remote.js +219 -92
- package/esm/src/kernel/tools/resolve.d.ts +3 -0
- package/esm/src/kernel/tools/resolve.js +12 -4
- 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 +3 -3
- package/esm/src/kernel/tools/schema.js +21 -100
- package/esm/src/kernel/tools/signed-in-fetch.d.ts +2 -1
- package/esm/src/kernel/tools/signed-in-fetch.js +3 -3
- package/esm/src/kernel/tools/stage-run.d.ts +27 -4
- package/esm/src/kernel/tools/stage-run.js +75 -17
- package/esm/src/kernel/tools/types.d.ts +235 -25
- 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 +208 -31
- package/esm/src/kernel/turn-events.js +127 -9
- package/esm/src/kernel/types.d.ts +302 -161
- package/esm/src/kernel/util/base64.d.ts +2 -0
- package/esm/src/kernel/util/base64.js +2 -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 +1 -0
- package/esm/src/kernel/util/record.js +1 -0
- package/esm/src/observability/destinations.d.ts +5 -0
- package/esm/src/observability/destinations.js +7 -2
- package/esm/src/observability/jsonl.d.ts +2 -2
- package/esm/src/observability/jsonl.js +15 -22
- package/esm/src/observability/mod.d.ts +3 -3
- package/esm/src/observability/mod.js +2 -2
- package/esm/src/observability/openinference.d.ts +1 -1
- package/esm/src/observability/openinference.js +3 -3
- package/esm/src/observability/otlp.d.ts +3 -0
- package/esm/src/observability/otlp.js +3 -3
- package/esm/src/observability/phoenix.d.ts +1 -0
- package/esm/src/observability/policy.d.ts +3 -0
- package/esm/src/observability/policy.js +5 -1
- package/esm/src/observability/resolve-policy.js +11 -5
- package/esm/src/observability/spans.d.ts +8 -2
- package/esm/src/observability/spans.js +21 -5
- package/esm/src/observability/trace-catalog.d.ts +19 -2
- package/esm/src/observability/trace-catalog.js +130 -88
- package/esm/src/observability/trace-record.js +22 -17
- package/esm/src/observability/trace-schema.d.ts +3 -0
- package/esm/src/observability/trace-schema.js +1 -1
- package/esm/src/observability/trace-sink.d.ts +1 -0
- package/esm/src/observability/trace-sink.js +1 -1
- package/esm/src/observability/trace-span.d.ts +7 -1
- package/esm/src/observability/trace-span.js +3 -1
- package/esm/src/observability/trace.d.ts +2 -0
- package/esm/src/observability/trace.js +4 -2
- package/esm/src/observability/types.d.ts +35 -11
- package/esm/src/observability/types.js +1 -1
- package/esm/src/presets/google/speech-voices.d.ts +1 -0
- package/esm/src/presets/google-limits.d.ts +1 -0
- package/esm/src/presets/google.d.ts +27 -12
- package/esm/src/presets/google.js +34 -25
- package/esm/src/presets/local.d.ts +26 -0
- package/esm/src/presets/local.js +30 -0
- package/esm/src/presets/mod.d.ts +5 -4
- package/esm/src/presets/mod.js +4 -4
- package/esm/src/presets/openrouter.d.ts +14 -1
- package/esm/src/presets/openrouter.js +19 -1
- 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 +102 -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 +2 -0
- package/esm/src/providers/google/api-error.js +26 -18
- 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 +4 -4
- package/esm/src/providers/google/interactions/framing.d.ts +20 -8
- package/esm/src/providers/google/interactions/framing.js +85 -44
- package/esm/src/providers/google/interactions/steps.d.ts +5 -5
- package/esm/src/providers/google/interactions/steps.js +4 -4
- package/esm/src/providers/google/interactions/stream.d.ts +10 -11
- package/esm/src/providers/google/interactions/stream.js +33 -100
- package/esm/src/providers/google/live/framing.d.ts +15 -5
- package/esm/src/providers/google/live/framing.js +25 -25
- package/esm/src/providers/google/live/stream.d.ts +6 -44
- package/esm/src/providers/google/live/stream.js +4 -210
- package/esm/src/providers/google/urls.d.ts +1 -3
- package/esm/src/providers/google/urls.js +1 -3
- package/esm/src/providers/google/usage.js +2 -2
- 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 +28 -7
- package/esm/src/providers/mod.js +19 -3
- 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 +0 -9
- package/esm/src/providers/openrouter/image.js +0 -153
- 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 +2 -2
- package/esm/src/providers/openrouter/openai/compat.js +6 -6
- package/esm/src/providers/openrouter/openai/image-payload.d.ts +2 -2
- 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/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.js +2 -2
- 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 +2 -2
- package/esm/src/providers/shared/retry.d.ts +18 -0
- package/esm/src/providers/shared/retry.js +89 -0
- package/esm/src/providers/shared/sse.d.ts +0 -4
- package/esm/src/providers/shared/sse.js +50 -36
- package/esm/src/providers/shared/tool-args.d.ts +6 -7
- package/esm/src/providers/shared/tool-args.js +10 -6
- 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 +2 -2
- package/esm/src/providers/shared/upstream-tape.js +1 -1
- 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 +48 -16
- package/esm/src/providers/types.js +2 -1
- 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 +19 -1
- package/esm/src/surface/formats.js +68 -29
- package/esm/src/surface/mod.d.ts +4 -2
- package/esm/src/surface/mod.js +4 -2
- package/esm/src/surface/runtime.d.ts +4 -0
- package/esm/src/surface/runtime.js +2 -0
- package/esm/src/surface/tools.d.ts +2 -8
- package/esm/src/surface/tools.js +4 -22
- package/esm/src/surface/types.d.ts +6 -0
- package/package.json +5 -13
- package/esm/src/cli/commands/egress-compile.d.ts +0 -17
- package/esm/src/cli/commands/egress-compile.js +0 -25
- package/esm/src/guardrails/egress-policy.d.ts +0 -27
- package/esm/src/guardrails/egress-policy.js +0 -76
- package/esm/src/guardrails/egress-rules.d.ts +0 -54
- package/esm/src/guardrails/egress-rules.js +0 -49
- package/esm/src/guardrails/lexicon.d.ts +0 -30
- package/esm/src/guardrails/lexicon.js +0 -478
- package/esm/src/kernel/engine/media-probe/audio.d.ts +0 -7
- package/esm/src/kernel/engine/media-probe/audio.js +0 -342
- package/esm/src/kernel/engine/media-probe/bytes.d.ts +0 -6
- package/esm/src/kernel/engine/media-probe/bytes.js +0 -26
- package/esm/src/kernel/engine/media-probe/image.d.ts +0 -5
- package/esm/src/kernel/engine/media-probe/image.js +0 -171
- package/esm/src/kernel/engine/media-probe/iso-bmff.d.ts +0 -19
- package/esm/src/kernel/engine/media-probe/iso-bmff.js +0 -48
- package/esm/src/kernel/engine/media-probe/matroska.d.ts +0 -35
- package/esm/src/kernel/engine/media-probe/matroska.js +0 -226
- package/esm/src/kernel/engine/media-probe/mp4.d.ts +0 -22
- package/esm/src/kernel/engine/media-probe/mp4.js +0 -279
- package/esm/src/kernel/engine/media-probe/opus.d.ts +0 -4
- package/esm/src/kernel/engine/media-probe/opus.js +0 -23
- package/esm/src/kernel/engine/media-probe/pdf.d.ts +0 -5
- package/esm/src/kernel/engine/media-probe/pdf.js +0 -117
- package/esm/src/kernel/engine/media-probe/video.d.ts +0 -15
- package/esm/src/kernel/engine/media-probe/video.js +0 -49
- package/esm/src/presets/typesafe.d.ts +0 -2
- package/esm/src/presets/typesafe.js +0 -2
- package/esm/src/providers/create-provider.d.ts +0 -21
- package/esm/src/providers/create-provider.js +0 -90
- package/esm/src/providers/decision/usage.d.ts +0 -3
- package/esm/src/providers/decision/usage.js +0 -25
- package/esm/src/providers/google/interactions/mod.d.ts +0 -2
- package/esm/src/providers/google/interactions/mod.js +0 -2
- package/esm/src/providers/google/keys.d.ts +0 -20
- package/esm/src/providers/google/keys.js +0 -87
- package/esm/src/providers/google/live/mod.d.ts +0 -7
- package/esm/src/providers/google/live/mod.js +0 -7
- package/esm/src/providers/google/live/openapi-schema.d.ts +0 -1
- package/esm/src/providers/google/live/openapi-schema.js +0 -42
- package/esm/src/providers/google/live/session.d.ts +0 -14
- package/esm/src/providers/google/live/session.js +0 -150
- package/esm/src/providers/local/local.d.ts +0 -12
- package/esm/src/providers/local/local.js +0 -186
- package/esm/src/providers/local/mod.d.ts +0 -8
- package/esm/src/providers/local/mod.js +0 -8
- package/esm/src/providers/openrouter/cache-control.d.ts +0 -5
- package/esm/src/providers/openrouter/cache-control.js +0 -4
- package/esm/src/providers/openrouter/chat.d.ts +0 -64
- package/esm/src/providers/openrouter/chat.js +0 -490
- package/esm/src/providers/openrouter/openai/sdk-messages.d.ts +0 -10
- package/esm/src/providers/openrouter/openai/sdk-messages.js +0 -99
- package/esm/src/providers/openrouter/resolve-api-key.d.ts +0 -3
- package/esm/src/providers/openrouter/resolve-api-key.js +0 -8
- package/esm/src/providers/openrouter/speech.d.ts +0 -18
- package/esm/src/providers/openrouter/speech.js +0 -109
- package/esm/src/providers/probe.d.ts +0 -5
- package/esm/src/providers/probe.js +0 -15
- package/esm/src/providers/shared/builtin-wire.d.ts +0 -4
- package/esm/src/providers/shared/builtin-wire.js +0 -9
- package/esm/src/providers/shared/vault.d.ts +0 -13
- package/esm/src/providers/shared/vault.js +0 -41
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isAbortError, TheoremError, throwIfAborted, } from '../guardrails/error.js';
|
|
3
|
+
import { labelText } from '../guardrails/labels.js';
|
|
4
|
+
import { historyHash, providerCheckpointSchema, } from './checkpoint.js';
|
|
5
|
+
import { historyToolCalls } from './interaction-parts.js';
|
|
6
|
+
import { jsonObjectSchema, jsonValueSchema } from './json.js';
|
|
7
|
+
import { keySlotsOf, protocolOf, } from './protocols/binding.js';
|
|
8
|
+
import { resolveProviderLimits } from './protocols/limits.js';
|
|
9
|
+
import { carriesTo, } from './protocols/protocol.js';
|
|
10
|
+
import { callProtocol, plainWebSocket } from './protocols/runtime.js';
|
|
11
|
+
import { ProviderBudget } from './provider-budget.js';
|
|
12
|
+
import { reasoningPartSchema, turnEventSchema, turnStopSchema, } from './turn-events.js';
|
|
13
|
+
import { isRecord } from './util/record.js';
|
|
14
|
+
const toolCallSchema = z.strictObject({
|
|
15
|
+
name: z.string().min(1),
|
|
16
|
+
callId: z.string().min(1),
|
|
17
|
+
wireCallId: z.string().min(1).optional(),
|
|
18
|
+
arguments: z.record(z.string(), jsonValueSchema),
|
|
19
|
+
stepId: z.string().min(1).optional(),
|
|
20
|
+
stepCallCount: z.number().int().min(1).optional(),
|
|
21
|
+
});
|
|
22
|
+
const reasoningEventSchema = reasoningPartSchema.extend({ type: z.literal('reasoning') });
|
|
23
|
+
const contentTypes = new Set([
|
|
24
|
+
'text',
|
|
25
|
+
'thought',
|
|
26
|
+
'structured',
|
|
27
|
+
'media',
|
|
28
|
+
'grounding',
|
|
29
|
+
'citation',
|
|
30
|
+
'evidence',
|
|
31
|
+
'tokens',
|
|
32
|
+
'session',
|
|
33
|
+
'tool_draft',
|
|
34
|
+
'error',
|
|
35
|
+
'custom',
|
|
36
|
+
]);
|
|
37
|
+
function providerStop(raw) {
|
|
38
|
+
const stop = turnStopSchema.parse(raw);
|
|
39
|
+
if (stop.kind === 'gate')
|
|
40
|
+
throw new TheoremError('bad_response', labelText('provider.kernel_gate'));
|
|
41
|
+
return stop;
|
|
42
|
+
}
|
|
43
|
+
/** Checks one event a protocol streamed has the shape its type promises. */
|
|
44
|
+
function validateProtocolEvent(raw) {
|
|
45
|
+
if (!isRecord(raw) || typeof raw.type !== 'string')
|
|
46
|
+
throw new TheoremError('bad_response', labelText('provider.event_invalid'));
|
|
47
|
+
if (contentTypes.has(raw.type))
|
|
48
|
+
return turnEventSchema.parse(raw);
|
|
49
|
+
switch (raw.type) {
|
|
50
|
+
case 'tool_call':
|
|
51
|
+
return { type: 'tool_call', call: toolCallSchema.parse(raw.call) };
|
|
52
|
+
case 'tool_cancel':
|
|
53
|
+
return { type: 'tool_cancel', callId: z.string().min(1).parse(raw.callId) };
|
|
54
|
+
case 'reasoning':
|
|
55
|
+
return reasoningEventSchema.parse(raw);
|
|
56
|
+
case 'raw':
|
|
57
|
+
return { type: 'raw', row: jsonObjectSchema.parse(raw.row) };
|
|
58
|
+
case 'done':
|
|
59
|
+
return {
|
|
60
|
+
type: 'done',
|
|
61
|
+
stop: providerStop(raw.stop),
|
|
62
|
+
interrupted: z.boolean().optional().parse(raw.interrupted),
|
|
63
|
+
state: raw.state,
|
|
64
|
+
};
|
|
65
|
+
case 'response':
|
|
66
|
+
if (!isRecord(raw.response))
|
|
67
|
+
throw new TheoremError('bad_response', labelText('provider.response_identity'));
|
|
68
|
+
return {
|
|
69
|
+
type: 'response',
|
|
70
|
+
response: z
|
|
71
|
+
.strictObject({ id: z.string().optional(), model: z.string().optional() })
|
|
72
|
+
.parse(raw.response),
|
|
73
|
+
};
|
|
74
|
+
default:
|
|
75
|
+
throw new TheoremError('bad_response', labelText('provider.event_type', { type: raw.type }));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export async function withProviderAbort(operation, signal) {
|
|
79
|
+
throwIfAborted(signal);
|
|
80
|
+
if (!signal)
|
|
81
|
+
return await operation();
|
|
82
|
+
let abort = () => { };
|
|
83
|
+
const stopped = new Promise((_resolve, reject) => {
|
|
84
|
+
abort = () => {
|
|
85
|
+
try {
|
|
86
|
+
throwIfAborted(signal);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
reject(error);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
93
|
+
if (signal.aborted)
|
|
94
|
+
abort();
|
|
95
|
+
});
|
|
96
|
+
try {
|
|
97
|
+
return await Promise.race([
|
|
98
|
+
Promise.resolve().then(() => {
|
|
99
|
+
throwIfAborted(signal);
|
|
100
|
+
return operation();
|
|
101
|
+
}),
|
|
102
|
+
stopped,
|
|
103
|
+
]);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
signal.removeEventListener('abort', abort);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function diagnosticCopy(copy, clean) {
|
|
110
|
+
if (!copy)
|
|
111
|
+
return undefined;
|
|
112
|
+
const line = (value) => ({
|
|
113
|
+
...value,
|
|
114
|
+
...(value.params
|
|
115
|
+
? {
|
|
116
|
+
params: Object.fromEntries(Object.entries(value.params).map(([key, parameter]) => [
|
|
117
|
+
key,
|
|
118
|
+
typeof parameter === 'string' ? clean(parameter) : parameter,
|
|
119
|
+
])),
|
|
120
|
+
}
|
|
121
|
+
: {}),
|
|
122
|
+
});
|
|
123
|
+
return 'key' in copy ? line(copy) : copy.map(line);
|
|
124
|
+
}
|
|
125
|
+
function diagnosticError(error, clean) {
|
|
126
|
+
const message = clean(error instanceof Error ? error.message : String(error));
|
|
127
|
+
if (error instanceof DOMException)
|
|
128
|
+
return new DOMException(message, error.name);
|
|
129
|
+
if (isAbortError(error))
|
|
130
|
+
return new DOMException(message, 'AbortError');
|
|
131
|
+
const result = error instanceof TheoremError
|
|
132
|
+
? new TheoremError(error.kind, message, { copy: diagnosticCopy(error.copy, clean) })
|
|
133
|
+
: error instanceof TypeError
|
|
134
|
+
? new TypeError(message)
|
|
135
|
+
: new Error(message);
|
|
136
|
+
if (isRecord(error) && typeof error.status === 'number')
|
|
137
|
+
Object.assign(result, { status: error.status });
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
async function safeProviderCall(operation, clean) {
|
|
141
|
+
try {
|
|
142
|
+
return await operation();
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
throw diagnosticError(error, clean);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async function* guardedEvents(events, clean, budget, live = false) {
|
|
149
|
+
if (!live)
|
|
150
|
+
budget.resumeOperation();
|
|
151
|
+
let iterator;
|
|
152
|
+
try {
|
|
153
|
+
iterator = events()[Symbol.asyncIterator]();
|
|
154
|
+
for (;;) {
|
|
155
|
+
const current = iterator;
|
|
156
|
+
const next = await withProviderAbort(() => current.next(), budget.signal);
|
|
157
|
+
if (next.done)
|
|
158
|
+
break;
|
|
159
|
+
const event = validateProtocolEvent(next.value);
|
|
160
|
+
if (live && (event.type !== 'session' || event.session.kind === 'working'))
|
|
161
|
+
budget.startLiveStep();
|
|
162
|
+
budget.event(event);
|
|
163
|
+
if (event.type === 'error')
|
|
164
|
+
yield {
|
|
165
|
+
...event,
|
|
166
|
+
...(event.errorCopy ? { errorCopy: diagnosticCopy(event.errorCopy, clean) } : {}),
|
|
167
|
+
...(event.errorInternal ? { errorInternal: clean(event.errorInternal) } : {}),
|
|
168
|
+
...(event.error ? { error: clean(event.error) } : {}),
|
|
169
|
+
};
|
|
170
|
+
else
|
|
171
|
+
yield event;
|
|
172
|
+
if (live && event.type === 'done')
|
|
173
|
+
budget.resetStep();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
throw diagnosticError(error, clean);
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
budget.close();
|
|
181
|
+
if (iterator?.return)
|
|
182
|
+
void Promise.resolve(iterator.return()).catch(() => undefined);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const SECRET_FIELD = /^(?:authorization|proxy-authorization|cookie|set-cookie|api[_-]?key|x-goog-api-key|secret|token|access[_-]?token|refresh[_-]?token|password)$/i;
|
|
186
|
+
/**
|
|
187
|
+
* One binding's call as the kernel runs it: the protocol runtime's keys, retries, and ids, inside
|
|
188
|
+
* the host's budgets, with every key it signs with kept out of errors and traces.
|
|
189
|
+
*/
|
|
190
|
+
export function guardedCall(provider, binding, profileType, host, req) {
|
|
191
|
+
const limits = resolveProviderLimits(host.limits);
|
|
192
|
+
const budget = new ProviderBudget(limits, req.signal, req.timeoutMs ?? (req.liveConnection ? limits.connectTimeoutMs : limits.operationTimeoutMs));
|
|
193
|
+
const secrets = new Set();
|
|
194
|
+
const remember = (value) => {
|
|
195
|
+
if (typeof value === 'string')
|
|
196
|
+
secrets.add(value);
|
|
197
|
+
else if (value && typeof value === 'object')
|
|
198
|
+
for (const item of Object.values(value))
|
|
199
|
+
remember(item);
|
|
200
|
+
};
|
|
201
|
+
const scrub = (value) => {
|
|
202
|
+
if (typeof value === 'string') {
|
|
203
|
+
let clean = value;
|
|
204
|
+
for (const secret of secrets) {
|
|
205
|
+
if (secret && (secret.length >= 4 || clean === secret))
|
|
206
|
+
clean = clean.split(secret).join('[redacted]');
|
|
207
|
+
}
|
|
208
|
+
return clean;
|
|
209
|
+
}
|
|
210
|
+
if (Array.isArray(value))
|
|
211
|
+
return value.map(scrub);
|
|
212
|
+
if (value && typeof value === 'object')
|
|
213
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
|
214
|
+
String(scrub(key)),
|
|
215
|
+
SECRET_FIELD.test(key) && typeof item === 'string' ? '[redacted]' : scrub(item),
|
|
216
|
+
]));
|
|
217
|
+
return value;
|
|
218
|
+
};
|
|
219
|
+
const clean = (text) => String(scrub(text));
|
|
220
|
+
let slot = keySlotsOf(provider, binding).keySlot;
|
|
221
|
+
const call = callProtocol({
|
|
222
|
+
provider,
|
|
223
|
+
binding,
|
|
224
|
+
profileType,
|
|
225
|
+
limits,
|
|
226
|
+
signal: budget.signal,
|
|
227
|
+
fetch: budget.fetch(host.fetch ?? globalThis.fetch),
|
|
228
|
+
openWebSocket: budget.socket(host.openWebSocket ?? plainWebSocket(provider.id)),
|
|
229
|
+
...(host.vault ? { vault: host.vault } : {}),
|
|
230
|
+
...(host.wait ? { wait: host.wait } : {}),
|
|
231
|
+
timeLeft: () => budget.timeLeft(),
|
|
232
|
+
...(req.tapUpstream
|
|
233
|
+
? {
|
|
234
|
+
tapUpstream: (row) => req.tapUpstream?.(jsonObjectSchema.parse(scrub(row))),
|
|
235
|
+
}
|
|
236
|
+
: {}),
|
|
237
|
+
onKey(used, key) {
|
|
238
|
+
slot = used;
|
|
239
|
+
remember(key);
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
budget.stopTimer();
|
|
243
|
+
/** One request with one answer, inside the budget and its abort. */
|
|
244
|
+
const once = async (run) => {
|
|
245
|
+
try {
|
|
246
|
+
budget.resumeOperation();
|
|
247
|
+
const result = await withProviderAbort(() => safeProviderCall(run, clean), budget.signal);
|
|
248
|
+
budget.value(result);
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
budget.close();
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
keySlot: () => slot,
|
|
257
|
+
complete: (request) => guardedEvents(() => call.complete({ ...request, signal: budget.signal }), clean, budget),
|
|
258
|
+
decide: (request) => once(() => call.decide({ ...request, signal: budget.signal })),
|
|
259
|
+
embed: (request) => once(() => call.embed({ ...request, signal: budget.signal })),
|
|
260
|
+
async openSession(request) {
|
|
261
|
+
budget.resumeOperation();
|
|
262
|
+
const session = await withProviderAbort(() => safeProviderCall(() => call.openSession({ ...request, signal: budget.signal }), clean), budget.signal)
|
|
263
|
+
.catch((error) => {
|
|
264
|
+
budget.close();
|
|
265
|
+
throw error;
|
|
266
|
+
})
|
|
267
|
+
.finally(() => budget.stopTimer());
|
|
268
|
+
return {
|
|
269
|
+
events: () => guardedEvents(() => session.events(), clean, budget, true),
|
|
270
|
+
closeInfo: () => {
|
|
271
|
+
const closed = session.closeInfo?.();
|
|
272
|
+
return (closed && {
|
|
273
|
+
...closed,
|
|
274
|
+
...(closed.error ? { error: diagnosticError(closed.error, clean) } : {}),
|
|
275
|
+
});
|
|
276
|
+
},
|
|
277
|
+
sendText: (text) => {
|
|
278
|
+
budget.startLiveStep();
|
|
279
|
+
return safeProviderCall(() => session.sendText(text), clean);
|
|
280
|
+
},
|
|
281
|
+
sendAudio: (media) => safeProviderCall(() => session.sendAudio(media), clean),
|
|
282
|
+
sendVideo: (media) => safeProviderCall(() => session.sendVideo(media), clean),
|
|
283
|
+
sendContext: (context) => safeProviderCall(() => session.sendContext(context), clean),
|
|
284
|
+
sendToolResult: (result) => {
|
|
285
|
+
budget.startLiveStep();
|
|
286
|
+
return safeProviderCall(() => session.sendToolResult(result), clean);
|
|
287
|
+
},
|
|
288
|
+
close: (reason) => {
|
|
289
|
+
budget.stopTimer();
|
|
290
|
+
return safeProviderCall(() => session.close(reason), clean).finally(() => budget.dispose());
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
/** What saved state was written for besides the model: the connection and the keyed options. */
|
|
297
|
+
function compatibilityKey(provider, binding, protocol) {
|
|
298
|
+
const options = binding.options ?? {};
|
|
299
|
+
const keyed = [...(protocol.continuation?.keyOptions ?? [])]
|
|
300
|
+
.sort()
|
|
301
|
+
.map((name) => [name, options[name] ?? null]);
|
|
302
|
+
return JSON.stringify({ connection: provider.connection ?? null, options: keyed });
|
|
303
|
+
}
|
|
304
|
+
/** `history: 'stateless'`: every request stands alone, so one that carries a conversation is refused. */
|
|
305
|
+
export function assertHistoryKept(policy, checkpoint, history) {
|
|
306
|
+
if (policy?.history === 'stateless' && (checkpoint || history?.length))
|
|
307
|
+
throw new TheoremError('request', labelText('provider.stateless_history'));
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The protocol's saved state when the checkpoint was written for this exact call, or a warning
|
|
311
|
+
* that it was not and the conversation goes from the transcript.
|
|
312
|
+
*/
|
|
313
|
+
export async function restoreProviderState(provider, binding, raw, history, policy = {}) {
|
|
314
|
+
if (!raw)
|
|
315
|
+
return {};
|
|
316
|
+
const checkpoint = providerCheckpointSchema.parse(raw);
|
|
317
|
+
const protocol = protocolOf(provider, binding);
|
|
318
|
+
const contract = protocol.continuation;
|
|
319
|
+
let reason;
|
|
320
|
+
if (checkpoint.protocol !== protocol.id || checkpoint.provider !== provider.id)
|
|
321
|
+
reason = 'provider_changed';
|
|
322
|
+
else if (checkpoint.apiId !== binding.apiId)
|
|
323
|
+
reason = 'model_changed';
|
|
324
|
+
else if (!contract || checkpoint.version !== contract.version)
|
|
325
|
+
reason = 'version_changed';
|
|
326
|
+
else if (checkpoint.compatibilityKey !== compatibilityKey(provider, binding, protocol))
|
|
327
|
+
reason = 'connection_changed';
|
|
328
|
+
else if (checkpoint.keySlot !== keySlotsOf(provider, binding).keySlot)
|
|
329
|
+
reason = 'key_changed';
|
|
330
|
+
else if (policy.history === 'provider'
|
|
331
|
+
? history.length > 0
|
|
332
|
+
: checkpoint.coveredHistoryLength > history.length ||
|
|
333
|
+
checkpoint.coveredHistoryHash !==
|
|
334
|
+
(await historyHash(history.slice(0, checkpoint.coveredHistoryLength))))
|
|
335
|
+
reason = 'history_changed';
|
|
336
|
+
if (reason)
|
|
337
|
+
return { warning: { code: 'provider_state_rebuilt', reason } };
|
|
338
|
+
return {
|
|
339
|
+
state: contract?.schema.parse(checkpoint.data),
|
|
340
|
+
covered: policy.history === 'provider' ? 0 : checkpoint.coveredHistoryLength,
|
|
341
|
+
...(checkpoint.wireCallIds ? { wireCallIds: checkpoint.wireCallIds } : {}),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/** Saves a protocol's state with what it was written for, or nothing when it kept none. */
|
|
345
|
+
export async function checkpointProviderState(provider, binding, keySlot, state, history, wireCallIds = {}) {
|
|
346
|
+
if (state === undefined)
|
|
347
|
+
return undefined;
|
|
348
|
+
const protocol = protocolOf(provider, binding);
|
|
349
|
+
const contract = protocol.continuation;
|
|
350
|
+
if (!contract)
|
|
351
|
+
throw new TheoremError('bad_response', labelText('provider.continuation_contract'));
|
|
352
|
+
return {
|
|
353
|
+
protocol: protocol.id,
|
|
354
|
+
provider: provider.id,
|
|
355
|
+
version: contract.version,
|
|
356
|
+
apiId: binding.apiId,
|
|
357
|
+
...(keySlot ? { keySlot } : {}),
|
|
358
|
+
compatibilityKey: compatibilityKey(provider, binding, protocol),
|
|
359
|
+
coveredHistoryLength: history.length,
|
|
360
|
+
coveredHistoryHash: await historyHash(history),
|
|
361
|
+
data: jsonValueSchema.parse(contract.schema.parse(state)),
|
|
362
|
+
...(Object.keys(wireCallIds).length ? { wireCallIds } : {}),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/** Each message's reasoning, kept only where the target protocol can read it back. */
|
|
366
|
+
function carriedHistory(history, protocol) {
|
|
367
|
+
return history?.map((message) => {
|
|
368
|
+
if (!message.reasoning)
|
|
369
|
+
return message;
|
|
370
|
+
const { reasoning, ...rest } = message;
|
|
371
|
+
const carried = reasoning.filter((part) => carriesTo(part.origin.protocol, protocol));
|
|
372
|
+
return carried.length ? { ...rest, reasoning: carried } : rest;
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
/** History with each call the saved state holds under the wire's own id, as the wire knows it. */
|
|
376
|
+
function wireHistory(history, wireCallIds) {
|
|
377
|
+
if (!history || !wireCallIds)
|
|
378
|
+
return history;
|
|
379
|
+
const wire = (id) => wireCallIds[id] ?? id;
|
|
380
|
+
return history.map((message) => ({
|
|
381
|
+
...message,
|
|
382
|
+
...(message.tool_calls
|
|
383
|
+
? { tool_calls: message.tool_calls.map((call) => ({ ...call, id: wire(call.id) })) }
|
|
384
|
+
: {}),
|
|
385
|
+
...(message.tool_call_id ? { tool_call_id: wire(message.tool_call_id) } : {}),
|
|
386
|
+
}));
|
|
387
|
+
}
|
|
388
|
+
/** The engine's request as the protocol gets it, with its saved state when reused. */
|
|
389
|
+
export function protocolRequest(request, protocol, restored) {
|
|
390
|
+
const { providerState: _checkpoint, tapUpstream: _tap, history, ...portable } = request;
|
|
391
|
+
const carried = wireHistory(carriedHistory(history, protocol), restored.wireCallIds);
|
|
392
|
+
return {
|
|
393
|
+
...portable,
|
|
394
|
+
...(carried ? { history: carried } : {}),
|
|
395
|
+
...(restored.state !== undefined
|
|
396
|
+
? { state: restored.state, stateHistoryLength: restored.covered }
|
|
397
|
+
: {}),
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/** A call the kernel hands on: its Theorem id, never the wire's. */
|
|
401
|
+
export function kernelCall(call) {
|
|
402
|
+
const { wireCallId: _wire, ...rest } = call;
|
|
403
|
+
return rest;
|
|
404
|
+
}
|
|
405
|
+
async function* consumeModelStep(events, signal) {
|
|
406
|
+
const wireCallIds = {};
|
|
407
|
+
let terminal;
|
|
408
|
+
let failed = false;
|
|
409
|
+
const pending = [];
|
|
410
|
+
const reasoning = [];
|
|
411
|
+
let text = '';
|
|
412
|
+
const outputs = [];
|
|
413
|
+
const flushText = () => {
|
|
414
|
+
if (text)
|
|
415
|
+
outputs.push({ role: 'assistant', content: text });
|
|
416
|
+
text = '';
|
|
417
|
+
};
|
|
418
|
+
const iterator = events[Symbol.asyncIterator]();
|
|
419
|
+
let exhausted = false;
|
|
420
|
+
try {
|
|
421
|
+
for (;;) {
|
|
422
|
+
const next = await withProviderAbort(() => iterator.next(), signal);
|
|
423
|
+
if (next.done) {
|
|
424
|
+
exhausted = true;
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
throwIfAborted(signal);
|
|
428
|
+
const event = next.value;
|
|
429
|
+
if (terminal)
|
|
430
|
+
throw new TheoremError('bad_response', labelText('provider.events_after_completion'));
|
|
431
|
+
switch (event.type) {
|
|
432
|
+
case 'raw':
|
|
433
|
+
continue;
|
|
434
|
+
case 'text':
|
|
435
|
+
text += event.text;
|
|
436
|
+
break;
|
|
437
|
+
case 'structured':
|
|
438
|
+
flushText();
|
|
439
|
+
outputs.push({ role: 'assistant', content: JSON.stringify(event.structured) });
|
|
440
|
+
break;
|
|
441
|
+
case 'reasoning': {
|
|
442
|
+
const { type: _type, ...part } = event;
|
|
443
|
+
reasoning.push(part);
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
case 'tool_call':
|
|
447
|
+
if (pending.some((call) => call.callId === event.call.callId))
|
|
448
|
+
throw new TheoremError('bad_response', labelText('provider.duplicate_call'));
|
|
449
|
+
flushText();
|
|
450
|
+
if (event.call.wireCallId)
|
|
451
|
+
wireCallIds[event.call.callId] = event.call.wireCallId;
|
|
452
|
+
pending.push(kernelCall(event.call));
|
|
453
|
+
continue;
|
|
454
|
+
case 'tool_cancel':
|
|
455
|
+
throw new TheoremError('bad_response', labelText('provider.tool_cancel_live'));
|
|
456
|
+
case 'error':
|
|
457
|
+
failed = true;
|
|
458
|
+
break;
|
|
459
|
+
case 'done':
|
|
460
|
+
terminal = event;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
yield event;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
finally {
|
|
467
|
+
if (!exhausted && iterator.return)
|
|
468
|
+
void Promise.resolve(iterator.return()).catch(() => undefined);
|
|
469
|
+
}
|
|
470
|
+
flushText();
|
|
471
|
+
return { terminal, failed, pending, reasoning, outputs, wireCallIds };
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* A profile's model as the engine calls it: each step one guarded protocol call, with the
|
|
475
|
+
* protocol's saved state reused while it still fits and rebuilt, with a warning, when it does not.
|
|
476
|
+
*/
|
|
477
|
+
export function registeredTurnModel(provider, binding, profileType, host, initial, policy = {}, hostHistory = []) {
|
|
478
|
+
const protocol = protocolOf(provider, binding);
|
|
479
|
+
let active = initial;
|
|
480
|
+
return {
|
|
481
|
+
async *complete(req) {
|
|
482
|
+
// invariant: Only the checkpoint the host handed in follows the profile's history holder,
|
|
483
|
+
// judged against the history the host passed. A checkpoint from an earlier step of this
|
|
484
|
+
// turn always covers this turn's own history.
|
|
485
|
+
const providerHeld = active === initial && policy.history === 'provider';
|
|
486
|
+
const restored = await restoreProviderState(provider, binding, active, providerHeld ? hostHistory : (req.history ?? []), providerHeld ? policy : {});
|
|
487
|
+
if (restored.warning) {
|
|
488
|
+
active = undefined;
|
|
489
|
+
yield { type: 'provider_warning', warning: restored.warning };
|
|
490
|
+
}
|
|
491
|
+
const call = guardedCall(provider, binding, profileType, host, req);
|
|
492
|
+
const { terminal, failed, pending, reasoning, outputs, wireCallIds } = yield* consumeModelStep(call.complete(protocolRequest(req, protocol, restored)), req.signal);
|
|
493
|
+
throwIfAborted(req.signal);
|
|
494
|
+
if (!terminal) {
|
|
495
|
+
yield { type: 'done', stop: { kind: failed ? 'provider_error' : 'stream_incomplete' } };
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (failed ||
|
|
499
|
+
terminal.interrupted ||
|
|
500
|
+
(terminal.stop.kind !== 'tool' && terminal.stop.kind !== 'completed')) {
|
|
501
|
+
yield { type: 'done', stop: terminal.stop };
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (pending.length)
|
|
505
|
+
outputs.push(historyToolCalls(pending.map((entry, index) => (index === 0 ? { ...entry, reasoning } : entry))));
|
|
506
|
+
const history = [...(req.history ?? [])];
|
|
507
|
+
if (req.input.length)
|
|
508
|
+
history.push(req.input.every((part) => part.type === 'text')
|
|
509
|
+
? {
|
|
510
|
+
role: 'user',
|
|
511
|
+
content: req.input.map((part) => (part.type === 'text' ? part.text : '')).join(''),
|
|
512
|
+
}
|
|
513
|
+
: { role: 'user', parts: req.input });
|
|
514
|
+
const checkpoint = await checkpointProviderState(provider, binding, call.keySlot(), terminal.state, [
|
|
515
|
+
...history,
|
|
516
|
+
...(pending.length ? outputs.filter((message) => message.tool_calls) : outputs),
|
|
517
|
+
], { ...restored.wireCallIds, ...wireCallIds });
|
|
518
|
+
active = checkpoint;
|
|
519
|
+
for (const entry of pending)
|
|
520
|
+
yield { type: 'tool', tool: entry };
|
|
521
|
+
yield { type: 'done', stop: terminal.stop, providerState: checkpoint };
|
|
522
|
+
},
|
|
523
|
+
};
|
|
524
|
+
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
import type { BoundaryReader } from '../../guardrails/detect-at.js';
|
|
1
2
|
import { type ErrorCopy, TheoremError } from '../../guardrails/error.js';
|
|
2
|
-
import { type
|
|
3
|
+
import { type LabelOverrides } from '../../guardrails/labels.js';
|
|
3
4
|
import type { AttachmentValidationIssue, MediaLimits, MimeInputs, Profile, TurnBlob, TurnMediaRef } from '../types.js';
|
|
5
|
+
/** The media limits an inputs spec sets, or `undefined` unless it sets `maxFiles`, `maxBytes`, and `maxTurnBytes`. */
|
|
4
6
|
declare function resolveMediaLimits(inputs: MimeInputs): MediaLimits | undefined;
|
|
5
7
|
/** A MIME-specific cap, then its category wildcard, then the global cap. */
|
|
6
8
|
declare function maxBytesForMime(mimeType: string, limits: MediaLimits): number;
|
|
7
9
|
declare function isTurnMediaRef(item: TurnBlob | TurnMediaRef): item is TurnMediaRef;
|
|
10
|
+
/** The profile's media limits; throws when it takes no media. */
|
|
8
11
|
declare function requireMediaLimits(profile: Profile): MediaLimits;
|
|
9
12
|
/** CSV formula injection: formula-like cells get an apostrophe before they reach a model. */
|
|
10
13
|
declare function sanitizeCsvText(text: string): string;
|
|
@@ -22,14 +25,22 @@ export interface AttachmentRules {
|
|
|
22
25
|
}
|
|
23
26
|
/** The one attachment check: the kernel throws on it at ingress and the headless interface runs it before a send. */
|
|
24
27
|
declare function attachmentIssues(rules: AttachmentRules, files: AttachmentFacts[], clips: AttachmentFacts[]): AttachmentValidationIssue[];
|
|
28
|
+
/** The user-facing wording key and params for an attachment issue. */
|
|
25
29
|
declare function attachmentIssueCopy(issue: AttachmentValidationIssue): ErrorCopy;
|
|
26
|
-
|
|
30
|
+
/** The user-facing text for an attachment issue. */
|
|
31
|
+
declare function attachmentIssueText(issue: AttachmentValidationIssue, labels?: LabelOverrides): string;
|
|
32
|
+
/** The error for a turn whose attachments were refused. */
|
|
27
33
|
declare function attachmentsRefused(issues: readonly AttachmentValidationIssue[]): TheoremError;
|
|
34
|
+
/** The decoded size of inline base64 data; throws when it is not base64. */
|
|
35
|
+
declare function base64Bytes(data: string): number;
|
|
28
36
|
/** Provider references are checked for MIME and count; byte limits apply to inline blobs only. */
|
|
29
37
|
declare function assertTurnAttachments(profile: Profile, attachments: Array<TurnBlob | TurnMediaRef> | undefined, voice: TurnBlob[] | undefined): void;
|
|
30
38
|
/** Provider file references pass through unsanitized: the kernel has no bytes to scan. */
|
|
31
|
-
declare function sanitizeTurnBlobs(profile: Profile, attachments: Array<TurnBlob | TurnMediaRef> | undefined, voice: TurnBlob[] | undefined
|
|
39
|
+
declare function sanitizeTurnBlobs(profile: Profile, attachments: Array<TurnBlob | TurnMediaRef> | undefined, voice: TurnBlob[] | undefined, at: {
|
|
40
|
+
attachment: BoundaryReader;
|
|
41
|
+
voice: BoundaryReader;
|
|
42
|
+
}): {
|
|
32
43
|
attachments?: Array<TurnBlob | TurnMediaRef>;
|
|
33
44
|
voice?: TurnBlob[];
|
|
34
45
|
};
|
|
35
|
-
export { assertTurnAttachments, attachmentIssueCopy, attachmentIssues, attachmentIssueText, attachmentsRefused, isTurnMediaRef, maxBytesForMime, requireMediaLimits, resolveMediaLimits, sanitizeCsvText, sanitizeTurnBlobs, };
|
|
46
|
+
export { assertTurnAttachments, attachmentIssueCopy, attachmentIssues, attachmentIssueText, attachmentsRefused, base64Bytes, isTurnMediaRef, maxBytesForMime, requireMediaLimits, resolveMediaLimits, sanitizeCsvText, sanitizeTurnBlobs, };
|