@theoremjs/agents 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +276 -995
- package/esm/mod.d.ts +33 -19
- package/esm/mod.js +18 -11
- 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 +23 -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 +874 -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 +29 -21
- package/esm/src/guardrails/mod.js +18 -13
- 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 +1 -0
- package/esm/src/guardrails/quota.js +1 -1
- 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 +4 -1
- package/esm/src/guardrails/theorem-error.js +3 -2
- 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 +7 -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 +173 -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 +328 -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 +128 -87
- 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 +105 -0
- package/esm/src/providers/gemini/live-protocol.d.ts +3 -0
- package/esm/src/providers/gemini/live-protocol.js +191 -0
- package/esm/src/providers/gemini/prices.d.ts +3 -0
- package/esm/src/providers/gemini/prices.js +63 -0
- package/esm/src/providers/gemini/provider.d.ts +7 -0
- package/esm/src/providers/gemini/provider.js +39 -0
- package/esm/src/providers/google/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 +3 -209
- 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 +17 -0
- package/esm/src/providers/shared/retry.js +90 -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,262 @@
|
|
|
1
|
+
import { TheoremError, throwIfAborted } from '../guardrails/error.js';
|
|
2
|
+
import { labelText } from '../guardrails/labels.js';
|
|
3
|
+
function combinedSignal(signals) {
|
|
4
|
+
const controller = new AbortController();
|
|
5
|
+
const releases = [];
|
|
6
|
+
const dispose = () => {
|
|
7
|
+
for (const release of releases.splice(0))
|
|
8
|
+
release();
|
|
9
|
+
};
|
|
10
|
+
for (const signal of signals) {
|
|
11
|
+
const abort = () => {
|
|
12
|
+
controller.abort(signal.reason);
|
|
13
|
+
dispose();
|
|
14
|
+
};
|
|
15
|
+
if (signal.aborted) {
|
|
16
|
+
abort();
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
20
|
+
releases.push(() => signal.removeEventListener('abort', abort));
|
|
21
|
+
}
|
|
22
|
+
return { signal: controller.signal, dispose };
|
|
23
|
+
}
|
|
24
|
+
export class ProviderBudget {
|
|
25
|
+
limits;
|
|
26
|
+
controller = new AbortController();
|
|
27
|
+
signal;
|
|
28
|
+
timer;
|
|
29
|
+
timerDeadline;
|
|
30
|
+
wireBytes = 0;
|
|
31
|
+
stepBytes = 0;
|
|
32
|
+
stepEvents = 0;
|
|
33
|
+
calls = new Set();
|
|
34
|
+
deadline;
|
|
35
|
+
releaseSignal;
|
|
36
|
+
constructor(limits, caller, timeoutMs = limits.operationTimeoutMs) {
|
|
37
|
+
this.limits = limits;
|
|
38
|
+
const effective = combinedSignal(caller ? [caller, this.controller.signal] : [this.controller.signal]);
|
|
39
|
+
this.signal = effective.signal;
|
|
40
|
+
this.releaseSignal = effective.dispose;
|
|
41
|
+
this.startTimer(timeoutMs);
|
|
42
|
+
this.deadline = performance.now() + timeoutMs;
|
|
43
|
+
}
|
|
44
|
+
resumeOperation() {
|
|
45
|
+
const remaining = this.deadline - performance.now();
|
|
46
|
+
if (remaining <= 0) {
|
|
47
|
+
this.controller.abort(new TheoremError('timeout', labelText('error.timeout')));
|
|
48
|
+
throwIfAborted(this.signal);
|
|
49
|
+
}
|
|
50
|
+
this.startTimer(remaining);
|
|
51
|
+
}
|
|
52
|
+
/** Milliseconds before the running timer ends the call. */
|
|
53
|
+
timeLeft() {
|
|
54
|
+
return (this.timerDeadline ?? this.deadline) - performance.now();
|
|
55
|
+
}
|
|
56
|
+
startTimer(ms) {
|
|
57
|
+
this.stopTimer();
|
|
58
|
+
this.timerDeadline = performance.now() + ms;
|
|
59
|
+
this.timer = setTimeout(() => this.controller.abort(new TheoremError('timeout', labelText('error.timeout'))), ms);
|
|
60
|
+
}
|
|
61
|
+
stopTimer() {
|
|
62
|
+
if (this.timer !== undefined)
|
|
63
|
+
clearTimeout(this.timer);
|
|
64
|
+
this.timer = undefined;
|
|
65
|
+
this.timerDeadline = undefined;
|
|
66
|
+
}
|
|
67
|
+
close() {
|
|
68
|
+
this.controller.abort();
|
|
69
|
+
this.dispose();
|
|
70
|
+
}
|
|
71
|
+
dispose() {
|
|
72
|
+
this.stopTimer();
|
|
73
|
+
this.releaseSignal();
|
|
74
|
+
}
|
|
75
|
+
startLiveStep() {
|
|
76
|
+
if (this.timer === undefined)
|
|
77
|
+
this.startTimer(this.limits.liveStepTimeoutMs);
|
|
78
|
+
}
|
|
79
|
+
fail() {
|
|
80
|
+
const error = new TheoremError('bad_response', labelText('provider.budget_exhausted'), {
|
|
81
|
+
copy: { key: 'provider.budget_exhausted' },
|
|
82
|
+
});
|
|
83
|
+
this.controller.abort(error);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
checkDeadline() {
|
|
87
|
+
if (this.timerDeadline !== undefined && performance.now() >= this.timerDeadline) {
|
|
88
|
+
this.controller.abort(new TheoremError('timeout', labelText('error.timeout')));
|
|
89
|
+
}
|
|
90
|
+
throwIfAborted(this.signal);
|
|
91
|
+
}
|
|
92
|
+
value(value) {
|
|
93
|
+
this.checkDeadline();
|
|
94
|
+
const bytes = new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
95
|
+
this.checkDeadline();
|
|
96
|
+
this.stepBytes += bytes;
|
|
97
|
+
this.stepEvents++;
|
|
98
|
+
if (bytes > this.limits.maxEventBytes ||
|
|
99
|
+
this.stepBytes > this.limits.maxStepBytes ||
|
|
100
|
+
this.stepEvents > this.limits.maxStepEvents)
|
|
101
|
+
this.fail();
|
|
102
|
+
}
|
|
103
|
+
event(event) {
|
|
104
|
+
this.value(event);
|
|
105
|
+
if (event.type === 'tool_call')
|
|
106
|
+
this.calls.add(event.call.callId);
|
|
107
|
+
if (event.type === 'tool_cancel')
|
|
108
|
+
this.calls.delete(event.callId);
|
|
109
|
+
if (this.calls.size > this.limits.maxPendingToolCalls)
|
|
110
|
+
this.fail();
|
|
111
|
+
}
|
|
112
|
+
resetStep() {
|
|
113
|
+
this.stepBytes = 0;
|
|
114
|
+
this.stepEvents = 0;
|
|
115
|
+
this.calls.clear();
|
|
116
|
+
this.wireBytes = 0;
|
|
117
|
+
this.stopTimer();
|
|
118
|
+
}
|
|
119
|
+
socket(open) {
|
|
120
|
+
return async (url, init) => {
|
|
121
|
+
throwIfAborted(this.signal);
|
|
122
|
+
const ws = await open(url, init);
|
|
123
|
+
if (this.signal.aborted) {
|
|
124
|
+
ws.close();
|
|
125
|
+
throwIfAborted(this.signal);
|
|
126
|
+
}
|
|
127
|
+
const abort = () => ws.close();
|
|
128
|
+
this.signal.addEventListener('abort', abort, { once: true });
|
|
129
|
+
ws.addEventListener('close', () => this.signal.removeEventListener('abort', abort), {
|
|
130
|
+
once: true,
|
|
131
|
+
});
|
|
132
|
+
ws.addEventListener('message', (event) => {
|
|
133
|
+
const data = event.data;
|
|
134
|
+
const bytes = typeof data === 'string'
|
|
135
|
+
? new TextEncoder().encode(data).byteLength
|
|
136
|
+
: data instanceof Blob
|
|
137
|
+
? data.size
|
|
138
|
+
: data instanceof ArrayBuffer || ArrayBuffer.isView(data)
|
|
139
|
+
? data.byteLength
|
|
140
|
+
: 0;
|
|
141
|
+
if (bytes <= this.limits.maxWireRecordBytes)
|
|
142
|
+
return;
|
|
143
|
+
event.stopImmediatePropagation();
|
|
144
|
+
try {
|
|
145
|
+
this.fail();
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
ws.close(1009, 'response limit');
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
return ws;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
fetch(upstream) {
|
|
155
|
+
return async (input, init) => {
|
|
156
|
+
const supplied = init?.signal ?? (input instanceof Request ? input.signal : undefined);
|
|
157
|
+
const effective = combinedSignal(supplied ? [this.signal, supplied] : [this.signal]);
|
|
158
|
+
const signal = effective.signal;
|
|
159
|
+
throwIfAborted(signal);
|
|
160
|
+
const response = await upstream(input, { ...init, signal }).catch((error) => {
|
|
161
|
+
effective.dispose();
|
|
162
|
+
throw error;
|
|
163
|
+
});
|
|
164
|
+
throwIfAborted(signal);
|
|
165
|
+
if (!response.body) {
|
|
166
|
+
effective.dispose();
|
|
167
|
+
return response;
|
|
168
|
+
}
|
|
169
|
+
const reader = response.body.getReader();
|
|
170
|
+
let responseBytes = 0;
|
|
171
|
+
const record = response.headers.get('content-type')?.includes('text/event-stream')
|
|
172
|
+
? new SseRecordBudget(this)
|
|
173
|
+
: undefined;
|
|
174
|
+
const abort = () => {
|
|
175
|
+
void reader.cancel(signal.reason).catch(() => undefined);
|
|
176
|
+
};
|
|
177
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
178
|
+
const body = new ReadableStream({
|
|
179
|
+
pull: async (controller) => {
|
|
180
|
+
try {
|
|
181
|
+
throwIfAborted(signal);
|
|
182
|
+
const next = await reader.read();
|
|
183
|
+
throwIfAborted(signal);
|
|
184
|
+
if (next.done) {
|
|
185
|
+
controller.close();
|
|
186
|
+
signal.removeEventListener('abort', abort);
|
|
187
|
+
effective.dispose();
|
|
188
|
+
reader.releaseLock();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
responseBytes += next.value.byteLength;
|
|
192
|
+
this.wireBytes += next.value.byteLength;
|
|
193
|
+
if (responseBytes > this.limits.maxWireResponseBytes ||
|
|
194
|
+
this.wireBytes > this.limits.maxWireResponseBytes)
|
|
195
|
+
this.fail();
|
|
196
|
+
record?.accept(next.value);
|
|
197
|
+
controller.enqueue(next.value);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
controller.error(error);
|
|
201
|
+
signal.removeEventListener('abort', abort);
|
|
202
|
+
effective.dispose();
|
|
203
|
+
void reader.cancel(error).catch(() => undefined);
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
cancel: (reason) => {
|
|
207
|
+
signal.removeEventListener('abort', abort);
|
|
208
|
+
effective.dispose();
|
|
209
|
+
return reader.cancel(reason);
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
return new Response(body, {
|
|
213
|
+
status: response.status,
|
|
214
|
+
statusText: response.statusText,
|
|
215
|
+
headers: response.headers,
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
class SseRecordBudget {
|
|
221
|
+
budget;
|
|
222
|
+
recordBytes = 0;
|
|
223
|
+
lineBytes = 0;
|
|
224
|
+
previousCR = false;
|
|
225
|
+
emptyCR = false;
|
|
226
|
+
constructor(budget) {
|
|
227
|
+
this.budget = budget;
|
|
228
|
+
}
|
|
229
|
+
accept(chunk) {
|
|
230
|
+
for (const byte of chunk) {
|
|
231
|
+
if (this.emptyCR && byte !== 10) {
|
|
232
|
+
this.recordBytes = 0;
|
|
233
|
+
this.emptyCR = false;
|
|
234
|
+
}
|
|
235
|
+
this.recordBytes++;
|
|
236
|
+
if (this.recordBytes > this.budget.limits.maxWireRecordBytes)
|
|
237
|
+
this.budget.fail();
|
|
238
|
+
if (this.emptyCR) {
|
|
239
|
+
this.recordBytes = 0;
|
|
240
|
+
this.emptyCR = false;
|
|
241
|
+
this.previousCR = false;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (byte === 10 && this.previousCR) {
|
|
245
|
+
this.previousCR = false;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (byte === 10 || byte === 13) {
|
|
249
|
+
if (this.lineBytes === 0) {
|
|
250
|
+
if (byte === 13)
|
|
251
|
+
this.emptyCR = true;
|
|
252
|
+
else
|
|
253
|
+
this.recordBytes = 0;
|
|
254
|
+
}
|
|
255
|
+
this.lineBytes = 0;
|
|
256
|
+
}
|
|
257
|
+
else
|
|
258
|
+
this.lineBytes++;
|
|
259
|
+
this.previousCR = byte === 13;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ProviderHostOptions } from './protocols/runtime.js';
|
|
2
|
+
import type { KernelRegistry } from './registry/kernel-registry.js';
|
|
3
|
+
import type { TurnModel, TurnRequest } from './types.js';
|
|
4
|
+
export interface RegistryTurnModel extends TurnModel {
|
|
5
|
+
forProfile(profileId: string, modelId?: string, request?: TurnRequest): RegistryTurnModel;
|
|
6
|
+
}
|
|
7
|
+
export declare function resolveRegisteredTurnModel(registry: KernelRegistry, profileId: string, modelId: string | undefined, options: ProviderHostOptions, request?: TurnRequest): RegistryTurnModel;
|
|
8
|
+
/**
|
|
9
|
+
* A host's provider options as the model of the agents its call runs. It
|
|
10
|
+
* completes nothing itself: a host's own call and a live session run no turn.
|
|
11
|
+
*/
|
|
12
|
+
export declare function hostAgentModel(registry: KernelRegistry, options: ProviderHostOptions): RegistryTurnModel;
|
|
13
|
+
export declare function nestedTurnModel(provider: TurnModel, profileId: string, modelId?: string, request?: TurnRequest): TurnModel;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { TheoremError } from '../guardrails/error.js';
|
|
2
|
+
import { labelText } from '../guardrails/labels.js';
|
|
3
|
+
import { assertHistoryKept, registeredTurnModel } from './provider-runtime.js';
|
|
4
|
+
export function resolveRegisteredTurnModel(registry, profileId, modelId, options, request) {
|
|
5
|
+
const profile = registry.profiles.get(profileId);
|
|
6
|
+
if (profile.type === 'host')
|
|
7
|
+
return hostAgentModel(registry, options);
|
|
8
|
+
if (profile.type === 'decision' || profile.type === 'live' || profile.type === 'embedding')
|
|
9
|
+
throw new TheoremError('unsupported', labelText('provider.turn_profile'));
|
|
10
|
+
const binding = profile.models[modelId ?? profile.defaultModel];
|
|
11
|
+
if (!binding)
|
|
12
|
+
throw new TheoremError('config', labelText('provider.model_binding_missing'));
|
|
13
|
+
assertHistoryKept(profile.providerContinuation, request?.providerState, request?.input?.history);
|
|
14
|
+
const turn = registeredTurnModel(registry.providers.require(binding.provider, binding.apiId), structuredClone(binding), profile.type, options, request?.providerState, profile.providerContinuation, request?.input?.history);
|
|
15
|
+
return {
|
|
16
|
+
complete: turn.complete,
|
|
17
|
+
forProfile: (id, model, req) => resolveRegisteredTurnModel(registry, id, model, options, req),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A host's provider options as the model of the agents its call runs. It
|
|
22
|
+
* completes nothing itself: a host's own call and a live session run no turn.
|
|
23
|
+
*/
|
|
24
|
+
export function hostAgentModel(registry, options) {
|
|
25
|
+
return {
|
|
26
|
+
complete() {
|
|
27
|
+
throw new TheoremError('unsupported', labelText('provider.host_model'));
|
|
28
|
+
},
|
|
29
|
+
forProfile: (id, model, req) => resolveRegisteredTurnModel(registry, id, model, options, req),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function nestedTurnModel(provider, profileId, modelId, request) {
|
|
33
|
+
return 'forProfile' in provider && typeof provider.forProfile === 'function'
|
|
34
|
+
? provider.forProfile(profileId, modelId, request)
|
|
35
|
+
: provider;
|
|
36
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LiveConnection } from '../providers/types.js';
|
|
2
|
+
import type { ProviderCheckpoint, ProviderContinuationPolicy } from './checkpoint.js';
|
|
3
|
+
import { type ModelBinding } from './protocols/binding.js';
|
|
4
|
+
import type { RegisteredProvider } from './protocols/provider.js';
|
|
5
|
+
import type { ProviderHostOptions } from './protocols/runtime.js';
|
|
6
|
+
import type { ModelRequest } from './types.js';
|
|
7
|
+
export declare function openRegisteredLiveSession(provider: RegisteredProvider, binding: ModelBinding, request: ModelRequest, host: ProviderHostOptions, initial?: ProviderCheckpoint, policy?: ProviderContinuationPolicy): Promise<LiveConnection>;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { TheoremError } from '../guardrails/error.js';
|
|
2
|
+
import { labelText } from '../guardrails/labels.js';
|
|
3
|
+
import { protocolOf } from './protocols/binding.js';
|
|
4
|
+
import { checkpointProviderState, guardedCall, kernelCall, protocolRequest, restoreProviderState, withProviderAbort, } from './provider-runtime.js';
|
|
5
|
+
async function liveTerminalBatch(provider, binding, keySlot, request, event, failed, pending, boundary) {
|
|
6
|
+
const success = !failed &&
|
|
7
|
+
!event.interrupted &&
|
|
8
|
+
(event.stop.kind === 'completed' || event.stop.kind === 'tool');
|
|
9
|
+
const checkpoint = success
|
|
10
|
+
? await checkpointProviderState(provider, binding, keySlot, event.state, request.history ?? [])
|
|
11
|
+
: undefined;
|
|
12
|
+
return {
|
|
13
|
+
type: 'batch',
|
|
14
|
+
row: {},
|
|
15
|
+
turnPhase: success ? (event.stop.kind === 'tool' ? 'streaming' : 'complete') : 'abort',
|
|
16
|
+
events: [
|
|
17
|
+
...(success ? pending : []),
|
|
18
|
+
...boundary,
|
|
19
|
+
...(checkpoint ? [{ type: 'provider_checkpoint', providerState: checkpoint }] : []),
|
|
20
|
+
...(event.stop.kind === 'tool'
|
|
21
|
+
? []
|
|
22
|
+
: [
|
|
23
|
+
{
|
|
24
|
+
type: 'done',
|
|
25
|
+
stop: event.stop,
|
|
26
|
+
interrupted: event.interrupted,
|
|
27
|
+
providerState: checkpoint,
|
|
28
|
+
},
|
|
29
|
+
]),
|
|
30
|
+
],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function liveClosedItem(closed) {
|
|
34
|
+
return {
|
|
35
|
+
type: 'closed',
|
|
36
|
+
code: closed?.code ?? 1000,
|
|
37
|
+
reason: closed?.reason ?? 'provider-closed',
|
|
38
|
+
...(closed?.error
|
|
39
|
+
? {
|
|
40
|
+
error: closed.error instanceof TheoremError
|
|
41
|
+
? closed.error
|
|
42
|
+
: new TheoremError('unavailable', labelText('provider.session_closed')),
|
|
43
|
+
}
|
|
44
|
+
: {}),
|
|
45
|
+
...(closed?.warning ? { goAway: closed.warning } : {}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function rememberLiveCall(call, callNames, history) {
|
|
49
|
+
if (callNames.has(call.callId) ||
|
|
50
|
+
history?.some((message) => message.role === 'tool' && message.tool_call_id === call.callId))
|
|
51
|
+
throw new TheoremError('bad_response', labelText('provider.settled_call'));
|
|
52
|
+
callNames.set(call.callId, call.name);
|
|
53
|
+
}
|
|
54
|
+
function streamingBatch(events) {
|
|
55
|
+
return { type: 'batch', row: {}, turnPhase: 'streaming', events };
|
|
56
|
+
}
|
|
57
|
+
/** A call the model made, for the step; a step's signed reasoning rides on its first call, as in a turn. */
|
|
58
|
+
function queuedCall(call, pending, reasoning) {
|
|
59
|
+
const tool = kernelCall(call);
|
|
60
|
+
const first = !pending.some((item) => item.type === 'tool');
|
|
61
|
+
return {
|
|
62
|
+
type: 'tool',
|
|
63
|
+
tool: first && reasoning.length ? { ...tool, reasoning: reasoning.splice(0) } : tool,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** The batch saying the model cancelled one of its calls. */
|
|
67
|
+
function cancelBatch(callId, callNames) {
|
|
68
|
+
const name = callNames.get(callId);
|
|
69
|
+
if (!name)
|
|
70
|
+
throw new TheoremError('bad_response', labelText('provider.tool_cancel_missing'));
|
|
71
|
+
return streamingBatch([
|
|
72
|
+
{ type: 'tool', tool: { callId, name, at: Date.now(), phase: 'cancel' } },
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
export async function openRegisteredLiveSession(provider, binding, request, host, initial, policy = {}) {
|
|
76
|
+
const restored = await restoreProviderState(provider, binding, initial, request.history ?? [], policy);
|
|
77
|
+
const call = guardedCall(provider, binding, 'live', host, { ...request, liveConnection: true });
|
|
78
|
+
const session = await call.openSession(protocolRequest(request, protocolOf(provider, binding), restored));
|
|
79
|
+
let sends = Promise.resolve();
|
|
80
|
+
const send = (fn) => {
|
|
81
|
+
sends = sends.then(() => withProviderAbort(fn, request.signal));
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
setup: {},
|
|
85
|
+
flush: () => sends,
|
|
86
|
+
sendInput(part) {
|
|
87
|
+
if (part.type === 'text')
|
|
88
|
+
send(() => session.sendText(part.text));
|
|
89
|
+
else if (part.type === 'audio' && 'data' in part)
|
|
90
|
+
send(() => session.sendAudio(part));
|
|
91
|
+
else if (part.type === 'video' && 'data' in part)
|
|
92
|
+
send(() => session.sendVideo(part));
|
|
93
|
+
else
|
|
94
|
+
throw new TheoremError('unsupported', labelText('provider.live_input'));
|
|
95
|
+
},
|
|
96
|
+
sendContext(text) {
|
|
97
|
+
send(() => session.sendContext({ server: text }));
|
|
98
|
+
},
|
|
99
|
+
sendToolResponse(callId, name, output, parts) {
|
|
100
|
+
send(() => session.sendToolResult({
|
|
101
|
+
callId,
|
|
102
|
+
name,
|
|
103
|
+
text: typeof output === 'string' ? output : JSON.stringify(output),
|
|
104
|
+
...(parts?.length ? { parts } : {}),
|
|
105
|
+
}));
|
|
106
|
+
},
|
|
107
|
+
close(_code, reason) {
|
|
108
|
+
sends = sends.then(() => session.close(reason), () => session.close(reason));
|
|
109
|
+
},
|
|
110
|
+
async *batches() {
|
|
111
|
+
if (restored.warning)
|
|
112
|
+
yield streamingBatch([{ type: 'provider_warning', warning: restored.warning }]);
|
|
113
|
+
const callNames = new Map();
|
|
114
|
+
let pending = [];
|
|
115
|
+
let boundary = [];
|
|
116
|
+
let reasoning = [];
|
|
117
|
+
let failed = false;
|
|
118
|
+
const iterator = session.events()[Symbol.asyncIterator]();
|
|
119
|
+
try {
|
|
120
|
+
while (true) {
|
|
121
|
+
const next = await withProviderAbort(() => iterator.next(), request.signal);
|
|
122
|
+
if (next.done)
|
|
123
|
+
break;
|
|
124
|
+
const raw = next.value;
|
|
125
|
+
await withProviderAbort(() => sends, request.signal);
|
|
126
|
+
const event = raw;
|
|
127
|
+
if (event.type === 'raw')
|
|
128
|
+
continue;
|
|
129
|
+
if (event.type === 'reasoning') {
|
|
130
|
+
const { type: _type, ...part } = event;
|
|
131
|
+
reasoning.push(part);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (event.type === 'session' && event.session.kind === 'turn_complete') {
|
|
135
|
+
boundary.push(event);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (event.type === 'session' &&
|
|
139
|
+
boundary.length &&
|
|
140
|
+
!pending.length &&
|
|
141
|
+
(event.session.kind === 'working' || event.session.kind === 'idle')) {
|
|
142
|
+
yield streamingBatch([...boundary, event]);
|
|
143
|
+
boundary = [];
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (event.type === 'tool_call') {
|
|
147
|
+
rememberLiveCall(event.call, callNames, request.history);
|
|
148
|
+
pending.push(queuedCall(event.call, pending, reasoning));
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (event.type === 'tool_cancel') {
|
|
152
|
+
const cancelled = cancelBatch(event.callId, callNames);
|
|
153
|
+
pending = pending.filter((item) => item.type !== 'tool' || item.tool.callId !== event.callId);
|
|
154
|
+
yield cancelled;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (event.type === 'error')
|
|
158
|
+
failed = true;
|
|
159
|
+
if (event.type === 'done') {
|
|
160
|
+
if (event.stop.kind === 'generation_complete') {
|
|
161
|
+
yield streamingBatch([{ type: 'done', stop: event.stop }]);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
yield await liveTerminalBatch(provider, binding, call.keySlot(), request, event, failed, pending, boundary);
|
|
165
|
+
pending = [];
|
|
166
|
+
boundary = [];
|
|
167
|
+
reasoning = [];
|
|
168
|
+
failed = false;
|
|
169
|
+
}
|
|
170
|
+
else
|
|
171
|
+
yield streamingBatch([event]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
const returned = iterator.return?.();
|
|
176
|
+
if (returned)
|
|
177
|
+
void returned.catch(() => { });
|
|
178
|
+
}
|
|
179
|
+
if (pending.length || boundary.length)
|
|
180
|
+
yield {
|
|
181
|
+
type: 'batch',
|
|
182
|
+
row: {},
|
|
183
|
+
turnPhase: 'abort',
|
|
184
|
+
events: [{ type: 'done', stop: { kind: 'stream_incomplete' } }],
|
|
185
|
+
};
|
|
186
|
+
yield liveClosedItem(session.closeInfo?.());
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type ProviderCheckpoint, type ProviderContinuationPolicy, type ProviderWarning } from './checkpoint.js';
|
|
2
|
+
import { type DecisionModelBinding, type ModelBinding } from './protocols/binding.js';
|
|
3
|
+
import { type DecisionCall, type EmbeddingCall, type EmbeddingResult, type ModelProfileType, type Protocol, type ProtocolEvent, type ProtocolRequest, type ProtocolSession } from './protocols/protocol.js';
|
|
4
|
+
import type { RegisteredProvider } from './protocols/provider.js';
|
|
5
|
+
import { type ProviderHostOptions } from './protocols/runtime.js';
|
|
6
|
+
import { type ToolCallRequest } from './turn-events.js';
|
|
7
|
+
import type { DecisionResult, KeySlot, ModelRequest, TurnHistoryMessage, TurnModel } from './types.js';
|
|
8
|
+
export declare function withProviderAbort<T>(operation: () => T | Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
9
|
+
/** One binding's calls with the kernel's guards: its budget, its key redaction, and event checks. */
|
|
10
|
+
export interface GuardedCall {
|
|
11
|
+
complete(request: ProtocolRequest): AsyncIterable<ProtocolEvent>;
|
|
12
|
+
openSession(request: ProtocolRequest): Promise<ProtocolSession>;
|
|
13
|
+
decide(request: DecisionCall): Promise<DecisionResult>;
|
|
14
|
+
embed(request: EmbeddingCall): Promise<EmbeddingResult>;
|
|
15
|
+
/** The slot the last attempt signed with: the fallback's after a quota refusal. */
|
|
16
|
+
keySlot(): KeySlot | undefined;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* One binding's call as the kernel runs it: the protocol runtime's keys, retries, and ids, inside
|
|
20
|
+
* the host's budgets, with every key it signs with kept out of errors and traces.
|
|
21
|
+
*/
|
|
22
|
+
export declare function guardedCall(provider: RegisteredProvider, binding: ModelBinding | DecisionModelBinding, profileType: ModelProfileType, host: ProviderHostOptions, req: {
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
tapUpstream?: (row: Record<string, unknown>) => void;
|
|
25
|
+
liveConnection?: boolean;
|
|
26
|
+
/** The call's own deadline, in place of the limits' operation timeout. */
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
}): GuardedCall;
|
|
29
|
+
/** `history: 'stateless'`: every request stands alone, so one that carries a conversation is refused. */
|
|
30
|
+
export declare function assertHistoryKept(policy: ProviderContinuationPolicy | undefined, checkpoint: ProviderCheckpoint | undefined, history: readonly TurnHistoryMessage[] | undefined): void;
|
|
31
|
+
/**
|
|
32
|
+
* The protocol's saved state when the checkpoint was written for this exact call, or a warning
|
|
33
|
+
* that it was not and the conversation goes from the transcript.
|
|
34
|
+
*/
|
|
35
|
+
export declare function restoreProviderState(provider: RegisteredProvider, binding: ModelBinding, raw: ProviderCheckpoint | undefined, history: readonly TurnHistoryMessage[], policy?: ProviderContinuationPolicy): Promise<{
|
|
36
|
+
state?: unknown;
|
|
37
|
+
covered?: number;
|
|
38
|
+
wireCallIds?: Record<string, string>;
|
|
39
|
+
warning?: ProviderWarning;
|
|
40
|
+
}>;
|
|
41
|
+
/** Saves a protocol's state with what it was written for, or nothing when it kept none. */
|
|
42
|
+
export declare function checkpointProviderState(provider: RegisteredProvider, binding: ModelBinding, keySlot: KeySlot | undefined, state: unknown, history: readonly TurnHistoryMessage[], wireCallIds?: Record<string, string>): Promise<ProviderCheckpoint | undefined>;
|
|
43
|
+
/** The engine's request as the protocol gets it, with its saved state when reused. */
|
|
44
|
+
export declare function protocolRequest(request: ModelRequest, protocol: Protocol, restored: {
|
|
45
|
+
state?: unknown;
|
|
46
|
+
covered?: number;
|
|
47
|
+
wireCallIds?: Record<string, string>;
|
|
48
|
+
}): ProtocolRequest;
|
|
49
|
+
/** A call the kernel hands on: its Theorem id, never the wire's. */
|
|
50
|
+
export declare function kernelCall(call: Extract<ProtocolEvent, {
|
|
51
|
+
type: 'tool_call';
|
|
52
|
+
}>['call']): ToolCallRequest;
|
|
53
|
+
/**
|
|
54
|
+
* A profile's model as the engine calls it: each step one guarded protocol call, with the
|
|
55
|
+
* protocol's saved state reused while it still fits and rebuilt, with a warning, when it does not.
|
|
56
|
+
*/
|
|
57
|
+
export declare function registeredTurnModel(provider: RegisteredProvider, binding: ModelBinding, profileType: ModelProfileType, host: ProviderHostOptions, initial?: ProviderCheckpoint, policy?: ProviderContinuationPolicy, hostHistory?: readonly TurnHistoryMessage[]): TurnModel;
|