@theoremjs/agents 0.0.0-stage → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +545 -2
- package/esm/_dnt.polyfills.d.ts +66 -0
- package/esm/_dnt.polyfills.js +51 -0
- package/esm/mod.d.ts +79 -0
- package/esm/mod.js +49 -0
- package/esm/package.json +3 -0
- package/esm/src/cli/bin.d.ts +7 -0
- package/esm/src/cli/bin.js +9 -0
- package/esm/src/cli/commands/bench.d.ts +6 -0
- package/esm/src/cli/commands/bench.js +480 -0
- package/esm/src/cli/commands/detect-compile.d.ts +17 -0
- package/esm/src/cli/commands/detect-compile.js +22 -0
- package/esm/src/cli/commands/eval.d.ts +37 -0
- package/esm/src/cli/commands/eval.js +190 -0
- package/esm/src/cli/commands/fuzz-canary.d.ts +15 -0
- package/esm/src/cli/commands/fuzz-canary.js +241 -0
- package/esm/src/cli/commands/fuzz-guardrails.d.ts +2 -0
- package/esm/src/cli/commands/fuzz-guardrails.js +5 -0
- package/esm/src/cli/commands/profile.d.ts +2 -0
- package/esm/src/cli/commands/profile.js +80 -0
- package/esm/src/cli/commands/run.d.ts +13 -0
- package/esm/src/cli/commands/run.js +45 -0
- package/esm/src/cli/commands/test.d.ts +26 -0
- package/esm/src/cli/commands/test.js +143 -0
- package/esm/src/cli/event-log.d.ts +15 -0
- package/esm/src/cli/event-log.js +136 -0
- package/esm/src/cli/index.d.ts +10 -0
- package/esm/src/cli/index.js +267 -0
- package/esm/src/cli/matrix/fixtures.d.ts +10 -0
- package/esm/src/cli/matrix/fixtures.js +67 -0
- package/esm/src/cli/matrix/synthesizer.d.ts +18 -0
- package/esm/src/cli/matrix/synthesizer.js +103 -0
- package/esm/src/evals/attachments.d.ts +5 -0
- package/esm/src/evals/attachments.js +55 -0
- package/esm/src/evals/breakdown.d.ts +30 -0
- package/esm/src/evals/breakdown.js +69 -0
- package/esm/src/evals/graders/answer.d.ts +16 -0
- package/esm/src/evals/graders/answer.js +134 -0
- package/esm/src/evals/graders/code.d.ts +40 -0
- package/esm/src/evals/graders/code.js +184 -0
- package/esm/src/evals/graders/judge.d.ts +24 -0
- package/esm/src/evals/graders/judge.js +367 -0
- package/esm/src/evals/graders/latency.d.ts +6 -0
- package/esm/src/evals/graders/latency.js +82 -0
- package/esm/src/evals/graders/live.d.ts +14 -0
- package/esm/src/evals/graders/live.js +42 -0
- package/esm/src/evals/graders/media.d.ts +18 -0
- package/esm/src/evals/graders/media.js +91 -0
- package/esm/src/evals/graders/shared.d.ts +11 -0
- package/esm/src/evals/graders/shared.js +62 -0
- package/esm/src/evals/graders/transcript.d.ts +19 -0
- package/esm/src/evals/graders/transcript.js +202 -0
- package/esm/src/evals/mod.d.ts +34 -0
- package/esm/src/evals/mod.js +22 -0
- package/esm/src/evals/record.d.ts +41 -0
- package/esm/src/evals/record.js +107 -0
- package/esm/src/evals/rubrics/catalog.d.ts +148 -0
- package/esm/src/evals/rubrics/catalog.js +846 -0
- package/esm/src/evals/rubrics/mod.d.ts +24 -0
- package/esm/src/evals/rubrics/mod.js +55 -0
- package/esm/src/evals/rubrics/mustache.d.ts +23 -0
- package/esm/src/evals/rubrics/mustache.js +154 -0
- package/esm/src/evals/rubrics/types.d.ts +54 -0
- package/esm/src/evals/rubrics/types.js +74 -0
- package/esm/src/evals/run.d.ts +75 -0
- package/esm/src/evals/run.js +418 -0
- package/esm/src/evals/suite.d.ts +28 -0
- package/esm/src/evals/suite.js +97 -0
- package/esm/src/evals/summary.d.ts +35 -0
- package/esm/src/evals/summary.js +31 -0
- package/esm/src/evals/trial.d.ts +14 -0
- package/esm/src/evals/trial.js +135 -0
- package/esm/src/evals/types.d.ts +169 -0
- package/esm/src/evals/types.js +113 -0
- package/esm/src/evals/verdict.d.ts +23 -0
- package/esm/src/evals/verdict.js +48 -0
- package/esm/src/guardrails/boundaries.d.ts +41 -0
- package/esm/src/guardrails/boundaries.js +103 -0
- package/esm/src/guardrails/canary-gate.d.ts +22 -0
- package/esm/src/guardrails/canary-gate.js +40 -0
- package/esm/src/guardrails/canary.d.ts +146 -0
- package/esm/src/guardrails/canary.js +974 -0
- package/esm/src/guardrails/compile-egress.d.ts +12 -0
- package/esm/src/guardrails/compile-egress.js +12 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +24 -0
- package/esm/src/guardrails/corpus/canary-egress-attacks.js +305 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.d.ts +5 -0
- package/esm/src/guardrails/corpus/fuzz-inbound.js +199 -0
- package/esm/src/guardrails/corpus/inbound-payloads.d.ts +5 -0
- package/esm/src/guardrails/corpus/inbound-payloads.js +120 -0
- package/esm/src/guardrails/corpus/live-attacks.d.ts +15 -0
- package/esm/src/guardrails/corpus/live-attacks.js +229 -0
- package/esm/src/guardrails/corpus/mod.d.ts +7 -0
- package/esm/src/guardrails/corpus/mod.js +4 -0
- package/esm/src/guardrails/corpus/secrets.d.ts +12 -0
- package/esm/src/guardrails/corpus/secrets.js +16 -0
- package/esm/src/guardrails/corpus/strings.d.ts +23 -0
- package/esm/src/guardrails/corpus/strings.js +29 -0
- package/esm/src/guardrails/corpus/types.d.ts +36 -0
- package/esm/src/guardrails/corpus/types.js +1 -0
- package/esm/src/guardrails/credential-rules.d.ts +36 -0
- package/esm/src/guardrails/credential-rules.js +3001 -0
- package/esm/src/guardrails/credential-scan.d.ts +45 -0
- package/esm/src/guardrails/credential-scan.js +172 -0
- package/esm/src/guardrails/destinations.d.ts +45 -0
- package/esm/src/guardrails/destinations.js +234 -0
- package/esm/src/guardrails/detect-at.d.ts +107 -0
- package/esm/src/guardrails/detect-at.js +434 -0
- package/esm/src/guardrails/detect-reply.d.ts +61 -0
- package/esm/src/guardrails/detect-reply.js +95 -0
- package/esm/src/guardrails/detect-stream.d.ts +33 -0
- package/esm/src/guardrails/detect-stream.js +77 -0
- package/esm/src/guardrails/detectors.d.ts +216 -0
- package/esm/src/guardrails/detectors.js +503 -0
- package/esm/src/guardrails/egress-automata.d.ts +12 -0
- package/esm/src/guardrails/egress-automata.js +25582 -0
- package/esm/src/guardrails/egress-compiler.d.ts +55 -0
- package/esm/src/guardrails/egress-compiler.js +431 -0
- package/esm/src/guardrails/egress-patterns.d.ts +26 -0
- package/esm/src/guardrails/egress-patterns.js +36 -0
- package/esm/src/guardrails/egress-stream.d.ts +123 -0
- package/esm/src/guardrails/egress-stream.js +774 -0
- package/esm/src/guardrails/egress-urls.d.ts +86 -0
- package/esm/src/guardrails/egress-urls.js +909 -0
- package/esm/src/guardrails/egress.d.ts +46 -0
- package/esm/src/guardrails/egress.js +124 -0
- package/esm/src/guardrails/error.d.ts +31 -0
- package/esm/src/guardrails/error.js +96 -0
- package/esm/src/guardrails/event-schemas.d.ts +100 -0
- package/esm/src/guardrails/event-schemas.js +48 -0
- package/esm/src/guardrails/events.d.ts +13 -0
- package/esm/src/guardrails/events.js +45 -0
- package/esm/src/guardrails/hints.d.ts +14 -0
- package/esm/src/guardrails/hints.js +57 -0
- package/esm/src/guardrails/hits.d.ts +14 -0
- package/esm/src/guardrails/hits.js +30 -0
- package/esm/src/guardrails/host-patterns.d.ts +85 -0
- package/esm/src/guardrails/host-patterns.js +173 -0
- package/esm/src/guardrails/injection-patterns.d.ts +20 -0
- package/esm/src/guardrails/injection-patterns.js +126 -0
- package/esm/src/guardrails/injection-reversed.d.ts +9 -0
- package/esm/src/guardrails/injection-reversed.js +49 -0
- package/esm/src/guardrails/injection.d.ts +30 -0
- package/esm/src/guardrails/injection.js +460 -0
- package/esm/src/guardrails/labels.d.ts +40 -0
- package/esm/src/guardrails/labels.js +874 -0
- package/esm/src/guardrails/live-outbound-gate.d.ts +55 -0
- package/esm/src/guardrails/live-outbound-gate.js +298 -0
- package/esm/src/guardrails/mod.d.ts +54 -0
- package/esm/src/guardrails/mod.js +34 -0
- package/esm/src/guardrails/network.d.ts +46 -0
- package/esm/src/guardrails/network.js +330 -0
- package/esm/src/guardrails/normalize.d.ts +20 -0
- package/esm/src/guardrails/normalize.js +165 -0
- package/esm/src/guardrails/policy.d.ts +4 -0
- package/esm/src/guardrails/policy.js +21 -0
- package/esm/src/guardrails/progressive-yield.d.ts +110 -0
- package/esm/src/guardrails/progressive-yield.js +281 -0
- package/esm/src/guardrails/prompt-echo.d.ts +40 -0
- package/esm/src/guardrails/prompt-echo.js +306 -0
- package/esm/src/guardrails/quota.d.ts +21 -0
- package/esm/src/guardrails/quota.js +80 -0
- package/esm/src/guardrails/rules.d.ts +47 -0
- package/esm/src/guardrails/rules.js +47 -0
- package/esm/src/guardrails/sanitize.d.ts +40 -0
- package/esm/src/guardrails/sanitize.js +160 -0
- package/esm/src/guardrails/sensitive-groups.d.ts +21 -0
- package/esm/src/guardrails/sensitive-groups.js +12 -0
- package/esm/src/guardrails/sensitive.d.ts +22 -0
- package/esm/src/guardrails/sensitive.js +234 -0
- package/esm/src/guardrails/serialize.d.ts +15 -0
- package/esm/src/guardrails/serialize.js +37 -0
- package/esm/src/guardrails/testing.d.ts +11 -0
- package/esm/src/guardrails/testing.js +10 -0
- package/esm/src/guardrails/theorem-error.d.ts +21 -0
- package/esm/src/guardrails/theorem-error.js +54 -0
- package/esm/src/guardrails/thought-guard.d.ts +76 -0
- package/esm/src/guardrails/thought-guard.js +664 -0
- package/esm/src/guardrails/tool-directives.d.ts +38 -0
- package/esm/src/guardrails/tool-directives.js +195 -0
- package/esm/src/guardrails/tool-leak.d.ts +26 -0
- package/esm/src/guardrails/tool-leak.js +45 -0
- package/esm/src/guardrails/tool-result.d.ts +92 -0
- package/esm/src/guardrails/tool-result.js +272 -0
- package/esm/src/guardrails/types.d.ts +250 -0
- package/esm/src/guardrails/types.js +58 -0
- package/esm/src/host/client-turn.d.ts +36 -0
- package/esm/src/host/client-turn.js +80 -0
- package/esm/src/host/mint-trace.d.ts +29 -0
- package/esm/src/host/mint-trace.js +60 -0
- package/esm/src/host/mod.d.ts +13 -0
- package/esm/src/host/mod.js +11 -0
- package/esm/src/host/readStreamingJsonStringField.d.ts +2 -0
- package/esm/src/host/readStreamingJsonStringField.js +27 -0
- package/esm/src/host/reply.d.ts +13 -0
- package/esm/src/host/reply.js +41 -0
- package/esm/src/interface/blocks.d.ts +11 -0
- package/esm/src/interface/blocks.js +283 -0
- package/esm/src/interface/composer-actions.d.ts +24 -0
- package/esm/src/interface/composer-actions.js +33 -0
- package/esm/src/interface/draft.d.ts +20 -0
- package/esm/src/interface/draft.js +39 -0
- package/esm/src/interface/from-profile.d.ts +24 -0
- package/esm/src/interface/from-profile.js +179 -0
- package/esm/src/interface/history.d.ts +46 -0
- package/esm/src/interface/history.js +278 -0
- package/esm/src/interface/inputs.d.ts +11 -0
- package/esm/src/interface/inputs.js +67 -0
- package/esm/src/interface/mod.d.ts +33 -0
- package/esm/src/interface/mod.js +22 -0
- package/esm/src/interface/models.d.ts +26 -0
- package/esm/src/interface/models.js +50 -0
- package/esm/src/interface/pending.d.ts +57 -0
- package/esm/src/interface/pending.js +152 -0
- package/esm/src/interface/profile-interface.d.ts +4 -0
- package/esm/src/interface/profile-interface.js +237 -0
- package/esm/src/interface/session.d.ts +71 -0
- package/esm/src/interface/session.js +128 -0
- package/esm/src/interface/stop.d.ts +13 -0
- package/esm/src/interface/stop.js +28 -0
- package/esm/src/interface/tool-calls.d.ts +32 -0
- package/esm/src/interface/tool-calls.js +127 -0
- package/esm/src/interface/tool-invoke.d.ts +6 -0
- package/esm/src/interface/tool-invoke.js +22 -0
- package/esm/src/interface/tool-media.d.ts +11 -0
- package/esm/src/interface/tool-media.js +113 -0
- package/esm/src/interface/types.d.ts +278 -0
- package/esm/src/interface/types.js +5 -0
- package/esm/src/kernel/api-catalog.d.ts +85 -0
- package/esm/src/kernel/api-catalog.js +229 -0
- package/esm/src/kernel/auth/credential-source.d.ts +23 -0
- package/esm/src/kernel/auth/credential-source.js +11 -0
- package/esm/src/kernel/auth/crypto.d.ts +56 -0
- package/esm/src/kernel/auth/crypto.js +163 -0
- package/esm/src/kernel/auth/mod.d.ts +5 -0
- package/esm/src/kernel/auth/mod.js +5 -0
- package/esm/src/kernel/auth/oauth.d.ts +18 -0
- package/esm/src/kernel/auth/oauth.js +439 -0
- package/esm/src/kernel/auth/scope-refusal.d.ts +9 -0
- package/esm/src/kernel/auth/scope-refusal.js +8 -0
- package/esm/src/kernel/auth/typed-secret.d.ts +13 -0
- package/esm/src/kernel/auth/typed-secret.js +23 -0
- package/esm/src/kernel/auth/types.d.ts +152 -0
- package/esm/src/kernel/auth/types.js +1 -0
- package/esm/src/kernel/checkpoint.d.ts +43 -0
- package/esm/src/kernel/checkpoint.js +58 -0
- package/esm/src/kernel/default-scope.d.ts +72 -0
- package/esm/src/kernel/default-scope.js +121 -0
- package/esm/src/kernel/engine/call-trace.d.ts +19 -0
- package/esm/src/kernel/engine/call-trace.js +29 -0
- package/esm/src/kernel/engine/compaction.d.ts +71 -0
- package/esm/src/kernel/engine/compaction.js +203 -0
- package/esm/src/kernel/engine/decision.d.ts +43 -0
- package/esm/src/kernel/engine/decision.js +439 -0
- package/esm/src/kernel/engine/embedding.d.ts +8 -0
- package/esm/src/kernel/engine/embedding.js +118 -0
- package/esm/src/kernel/engine/hash.d.ts +6 -0
- package/esm/src/kernel/engine/hash.js +22 -0
- package/esm/src/kernel/engine/live-inbound.d.ts +19 -0
- package/esm/src/kernel/engine/live-inbound.js +44 -0
- package/esm/src/kernel/engine/live-ingress.d.ts +15 -0
- package/esm/src/kernel/engine/live-ingress.js +42 -0
- package/esm/src/kernel/engine/record.d.ts +3 -0
- package/esm/src/kernel/engine/record.js +11 -0
- package/esm/src/kernel/engine/repair.d.ts +7 -0
- package/esm/src/kernel/engine/repair.js +36 -0
- package/esm/src/kernel/engine/runner/gates.d.ts +5 -0
- package/esm/src/kernel/engine/runner/gates.js +508 -0
- package/esm/src/kernel/engine/runner/mod.d.ts +29 -0
- package/esm/src/kernel/engine/runner/mod.js +625 -0
- package/esm/src/kernel/engine/runner/nested.d.ts +32 -0
- package/esm/src/kernel/engine/runner/nested.js +134 -0
- package/esm/src/kernel/engine/runner/schema-validation.d.ts +35 -0
- package/esm/src/kernel/engine/runner/schema-validation.js +179 -0
- package/esm/src/kernel/engine/runner/stages.d.ts +28 -0
- package/esm/src/kernel/engine/runner/stages.js +41 -0
- package/esm/src/kernel/engine/runner/state.d.ts +97 -0
- package/esm/src/kernel/engine/runner/state.js +50 -0
- package/esm/src/kernel/engine/runner/step-order.d.ts +27 -0
- package/esm/src/kernel/engine/runner/step-order.js +165 -0
- package/esm/src/kernel/engine/runner/steps.d.ts +14 -0
- package/esm/src/kernel/engine/runner/steps.js +519 -0
- package/esm/src/kernel/engine/runner/stream.d.ts +71 -0
- package/esm/src/kernel/engine/runner/stream.js +376 -0
- package/esm/src/kernel/engine/runner/usage.d.ts +45 -0
- package/esm/src/kernel/engine/runner/usage.js +148 -0
- package/esm/src/kernel/engine/session/mod.d.ts +33 -0
- package/esm/src/kernel/engine/session/mod.js +1182 -0
- package/esm/src/kernel/engine/session/session-trace.d.ts +115 -0
- package/esm/src/kernel/engine/session/session-trace.js +500 -0
- package/esm/src/kernel/engine/token-estimate.d.ts +23 -0
- package/esm/src/kernel/engine/token-estimate.js +38 -0
- package/esm/src/kernel/engine/tool-trace.d.ts +58 -0
- package/esm/src/kernel/engine/tool-trace.js +132 -0
- package/esm/src/kernel/engine/tree.d.ts +2 -0
- package/esm/src/kernel/engine/tree.js +17 -0
- package/esm/src/kernel/engine/turn-trace.d.ts +119 -0
- package/esm/src/kernel/engine/turn-trace.js +855 -0
- package/esm/src/kernel/engine/usage.d.ts +36 -0
- package/esm/src/kernel/engine/usage.js +151 -0
- package/esm/src/kernel/interaction-parts.d.ts +13 -0
- package/esm/src/kernel/interaction-parts.js +40 -0
- package/esm/src/kernel/json.d.ts +10 -0
- package/esm/src/kernel/json.js +3 -0
- package/esm/src/kernel/mod.d.ts +74 -0
- package/esm/src/kernel/mod.js +45 -0
- package/esm/src/kernel/profile-graph.d.ts +145 -0
- package/esm/src/kernel/profile-graph.js +158 -0
- package/esm/src/kernel/profile-presence.d.ts +18 -0
- package/esm/src/kernel/profile-presence.js +158 -0
- package/esm/src/kernel/profile-scope.d.ts +40 -0
- package/esm/src/kernel/profile-scope.js +274 -0
- package/esm/src/kernel/protocols/binding.d.ts +65 -0
- package/esm/src/kernel/protocols/binding.js +125 -0
- package/esm/src/kernel/protocols/limits.d.ts +15 -0
- package/esm/src/kernel/protocols/limits.js +28 -0
- package/esm/src/kernel/protocols/mod.d.ts +5 -0
- package/esm/src/kernel/protocols/mod.js +5 -0
- package/esm/src/kernel/protocols/prices.d.ts +36 -0
- package/esm/src/kernel/protocols/prices.js +37 -0
- package/esm/src/kernel/protocols/protocol.d.ts +253 -0
- package/esm/src/kernel/protocols/protocol.js +253 -0
- package/esm/src/kernel/protocols/provider.d.ts +108 -0
- package/esm/src/kernel/protocols/provider.js +143 -0
- package/esm/src/kernel/protocols/runtime.d.ts +55 -0
- package/esm/src/kernel/protocols/runtime.js +534 -0
- package/esm/src/kernel/provider-budget.d.ts +32 -0
- package/esm/src/kernel/provider-budget.js +262 -0
- package/esm/src/kernel/provider-dispatch.d.ts +13 -0
- package/esm/src/kernel/provider-dispatch.js +36 -0
- package/esm/src/kernel/provider-live.d.ts +7 -0
- package/esm/src/kernel/provider-live.js +189 -0
- package/esm/src/kernel/provider-runtime.d.ts +57 -0
- package/esm/src/kernel/provider-runtime.js +524 -0
- package/esm/src/kernel/registry/attachments.d.ts +46 -0
- package/esm/src/kernel/registry/attachments.js +220 -0
- package/esm/src/kernel/registry/catalog.d.ts +25 -0
- package/esm/src/kernel/registry/catalog.js +85 -0
- package/esm/src/kernel/registry/embedding-rules.d.ts +28 -0
- package/esm/src/kernel/registry/embedding-rules.js +66 -0
- package/esm/src/kernel/registry/ingress.d.ts +12 -0
- package/esm/src/kernel/registry/ingress.js +179 -0
- package/esm/src/kernel/registry/kernel-registry.d.ts +15 -0
- package/esm/src/kernel/registry/kernel-registry.js +15 -0
- package/esm/src/kernel/registry/profile-outputs.d.ts +3 -0
- package/esm/src/kernel/registry/profile-outputs.js +10 -0
- package/esm/src/kernel/registry/profiles.d.ts +116 -0
- package/esm/src/kernel/registry/profiles.js +773 -0
- package/esm/src/kernel/registry/provider-request.d.ts +6 -0
- package/esm/src/kernel/registry/provider-request.js +34 -0
- package/esm/src/kernel/registry/resolve.d.ts +17 -0
- package/esm/src/kernel/registry/resolve.js +238 -0
- package/esm/src/kernel/registry/schemas.d.ts +12 -0
- package/esm/src/kernel/registry/schemas.js +27 -0
- package/esm/src/kernel/registry/slot-fill.d.ts +7 -0
- package/esm/src/kernel/registry/slot-fill.js +22 -0
- package/esm/src/kernel/registry/sole-model.d.ts +2 -0
- package/esm/src/kernel/registry/sole-model.js +4 -0
- package/esm/src/kernel/registry/system-prompt.d.ts +8 -0
- package/esm/src/kernel/registry/system-prompt.js +32 -0
- package/esm/src/kernel/registry/system-role.d.ts +3 -0
- package/esm/src/kernel/registry/system-role.js +9 -0
- package/esm/src/kernel/registry/vault.d.ts +14 -0
- package/esm/src/kernel/registry/vault.js +1 -0
- package/esm/src/kernel/schema.d.ts +226 -0
- package/esm/src/kernel/schema.js +895 -0
- package/esm/src/kernel/scope.d.ts +29 -0
- package/esm/src/kernel/scope.js +45 -0
- package/esm/src/kernel/stages.d.ts +176 -0
- package/esm/src/kernel/stages.js +431 -0
- package/esm/src/kernel/stop.d.ts +116 -0
- package/esm/src/kernel/stop.js +137 -0
- package/esm/src/kernel/system-parts.d.ts +26 -0
- package/esm/src/kernel/system-parts.js +98 -0
- package/esm/src/kernel/tools/activity-label.d.ts +22 -0
- package/esm/src/kernel/tools/activity-label.js +435 -0
- package/esm/src/kernel/tools/agent.d.ts +66 -0
- package/esm/src/kernel/tools/agent.js +212 -0
- package/esm/src/kernel/tools/coordinate.d.ts +4 -0
- package/esm/src/kernel/tools/coordinate.js +165 -0
- package/esm/src/kernel/tools/events.d.ts +66 -0
- package/esm/src/kernel/tools/events.js +184 -0
- package/esm/src/kernel/tools/execute.d.ts +100 -0
- package/esm/src/kernel/tools/execute.js +1173 -0
- package/esm/src/kernel/tools/gate-answer.d.ts +60 -0
- package/esm/src/kernel/tools/gate-answer.js +65 -0
- package/esm/src/kernel/tools/handler-events.d.ts +45 -0
- package/esm/src/kernel/tools/handler-events.js +106 -0
- package/esm/src/kernel/tools/harness.d.ts +17 -0
- package/esm/src/kernel/tools/harness.js +43 -0
- package/esm/src/kernel/tools/http-limit.d.ts +27 -0
- package/esm/src/kernel/tools/http-limit.js +85 -0
- package/esm/src/kernel/tools/invoke.d.ts +32 -0
- package/esm/src/kernel/tools/invoke.js +282 -0
- package/esm/src/kernel/tools/mcp-sessions.d.ts +23 -0
- package/esm/src/kernel/tools/mcp-sessions.js +69 -0
- package/esm/src/kernel/tools/mod.d.ts +10 -0
- package/esm/src/kernel/tools/mod.js +7 -0
- package/esm/src/kernel/tools/model-text.d.ts +16 -0
- package/esm/src/kernel/tools/model-text.js +31 -0
- package/esm/src/kernel/tools/operation.d.ts +76 -0
- package/esm/src/kernel/tools/operation.js +100 -0
- package/esm/src/kernel/tools/page-answer.d.ts +8 -0
- package/esm/src/kernel/tools/page-answer.js +19 -0
- package/esm/src/kernel/tools/permission.d.ts +15 -0
- package/esm/src/kernel/tools/permission.js +38 -0
- package/esm/src/kernel/tools/project.d.ts +11 -0
- package/esm/src/kernel/tools/project.js +55 -0
- package/esm/src/kernel/tools/registry.d.ts +27 -0
- package/esm/src/kernel/tools/registry.js +143 -0
- package/esm/src/kernel/tools/remote.d.ts +144 -0
- package/esm/src/kernel/tools/remote.js +1125 -0
- package/esm/src/kernel/tools/resolve.d.ts +31 -0
- package/esm/src/kernel/tools/resolve.js +267 -0
- package/esm/src/kernel/tools/run.d.ts +28 -0
- package/esm/src/kernel/tools/run.js +86 -0
- package/esm/src/kernel/tools/schema.d.ts +10 -0
- package/esm/src/kernel/tools/schema.js +95 -0
- package/esm/src/kernel/tools/signed-in-fetch.d.ts +19 -0
- package/esm/src/kernel/tools/signed-in-fetch.js +45 -0
- package/esm/src/kernel/tools/stage-run.d.ts +116 -0
- package/esm/src/kernel/tools/stage-run.js +198 -0
- package/esm/src/kernel/tools/types.d.ts +497 -0
- package/esm/src/kernel/tools/types.js +1 -0
- package/esm/src/kernel/tools/unchecked-output.d.ts +14 -0
- package/esm/src/kernel/tools/unchecked-output.js +18 -0
- package/esm/src/kernel/turn-events.d.ts +776 -0
- package/esm/src/kernel/turn-events.js +543 -0
- package/esm/src/kernel/types.d.ts +1025 -0
- package/esm/src/kernel/types.js +1 -0
- package/esm/src/kernel/util/base64.d.ts +4 -0
- package/esm/src/kernel/util/base64.js +15 -0
- package/esm/src/kernel/util/exact-type.d.ts +31 -0
- package/esm/src/kernel/util/exact-type.js +1 -0
- package/esm/src/kernel/util/find-last.d.ts +3 -0
- package/esm/src/kernel/util/find-last.js +9 -0
- package/esm/src/kernel/util/mime.d.ts +2 -0
- package/esm/src/kernel/util/mime.js +4 -0
- package/esm/src/kernel/util/partial-json.d.ts +23 -0
- package/esm/src/kernel/util/partial-json.js +128 -0
- package/esm/src/kernel/util/record.d.ts +2 -0
- package/esm/src/kernel/util/record.js +4 -0
- package/esm/src/observability/destinations.d.ts +14 -0
- package/esm/src/observability/destinations.js +43 -0
- package/esm/src/observability/jsonl.d.ts +18 -0
- package/esm/src/observability/jsonl.js +110 -0
- package/esm/src/observability/mod.d.ts +22 -0
- package/esm/src/observability/mod.js +16 -0
- package/esm/src/observability/openinference.d.ts +11 -0
- package/esm/src/observability/openinference.js +205 -0
- package/esm/src/observability/otlp.d.ts +71 -0
- package/esm/src/observability/otlp.js +98 -0
- package/esm/src/observability/phoenix.d.ts +30 -0
- package/esm/src/observability/phoenix.js +71 -0
- package/esm/src/observability/policy.d.ts +17 -0
- package/esm/src/observability/policy.js +73 -0
- package/esm/src/observability/resolve-policy.d.ts +8 -0
- package/esm/src/observability/resolve-policy.js +64 -0
- package/esm/src/observability/spans.d.ts +22 -0
- package/esm/src/observability/spans.js +72 -0
- package/esm/src/observability/trace-catalog.d.ts +128 -0
- package/esm/src/observability/trace-catalog.js +906 -0
- package/esm/src/observability/trace-record.d.ts +23 -0
- package/esm/src/observability/trace-record.js +228 -0
- package/esm/src/observability/trace-schema.d.ts +57 -0
- package/esm/src/observability/trace-schema.js +59 -0
- package/esm/src/observability/trace-sink.d.ts +14 -0
- package/esm/src/observability/trace-sink.js +3 -0
- package/esm/src/observability/trace-span.d.ts +98 -0
- package/esm/src/observability/trace-span.js +201 -0
- package/esm/src/observability/trace.d.ts +13 -0
- package/esm/src/observability/trace.js +37 -0
- package/esm/src/observability/types.d.ts +109 -0
- package/esm/src/observability/types.js +3 -0
- package/esm/src/presets/google/speech-voices.d.ts +9 -0
- package/esm/src/presets/google/speech-voices.js +38 -0
- package/esm/src/presets/google-limits.d.ts +13 -0
- package/esm/src/presets/google-limits.js +18 -0
- package/esm/src/presets/google.d.ts +119 -0
- package/esm/src/presets/google.js +197 -0
- package/esm/src/presets/local.d.ts +26 -0
- package/esm/src/presets/local.js +30 -0
- package/esm/src/presets/mod.d.ts +11 -0
- package/esm/src/presets/mod.js +9 -0
- package/esm/src/presets/openrouter.d.ts +26 -0
- package/esm/src/presets/openrouter.js +31 -0
- package/esm/src/providers/embedding-limits.d.ts +7 -0
- package/esm/src/providers/embedding-limits.js +12 -0
- package/esm/src/providers/gemini/embedding-protocol.d.ts +8 -0
- package/esm/src/providers/gemini/embedding-protocol.js +125 -0
- package/esm/src/providers/gemini/interactions-protocol.d.ts +8 -0
- package/esm/src/providers/gemini/interactions-protocol.js +105 -0
- package/esm/src/providers/gemini/live-protocol.d.ts +3 -0
- package/esm/src/providers/gemini/live-protocol.js +191 -0
- package/esm/src/providers/gemini/prices.d.ts +3 -0
- package/esm/src/providers/gemini/prices.js +63 -0
- package/esm/src/providers/gemini/provider.d.ts +7 -0
- package/esm/src/providers/gemini/provider.js +39 -0
- package/esm/src/providers/google/api-error.d.ts +8 -0
- package/esm/src/providers/google/api-error.js +123 -0
- package/esm/src/providers/google/builtins.d.ts +14 -0
- package/esm/src/providers/google/builtins.js +57 -0
- package/esm/src/providers/google/grounding.d.ts +5 -0
- package/esm/src/providers/google/grounding.js +226 -0
- package/esm/src/providers/google/interactions/framing.d.ts +27 -0
- package/esm/src/providers/google/interactions/framing.js +304 -0
- package/esm/src/providers/google/interactions/steps.d.ts +12 -0
- package/esm/src/providers/google/interactions/steps.js +200 -0
- package/esm/src/providers/google/interactions/stream.d.ts +23 -0
- package/esm/src/providers/google/interactions/stream.js +230 -0
- package/esm/src/providers/google/live/framing.d.ts +61 -0
- package/esm/src/providers/google/live/framing.js +597 -0
- package/esm/src/providers/google/live/stream.d.ts +13 -0
- package/esm/src/providers/google/live/stream.js +57 -0
- package/esm/src/providers/google/thinking.d.ts +3 -0
- package/esm/src/providers/google/thinking.js +8 -0
- package/esm/src/providers/google/urls.d.ts +4 -0
- package/esm/src/providers/google/urls.js +4 -0
- package/esm/src/providers/google/usage.d.ts +7 -0
- package/esm/src/providers/google/usage.js +49 -0
- package/esm/src/providers/live.d.ts +6 -0
- package/esm/src/providers/live.js +6 -0
- package/esm/src/providers/local/embedding-protocol.d.ts +7 -0
- package/esm/src/providers/local/embedding-protocol.js +60 -0
- package/esm/src/providers/local/provider.d.ts +7 -0
- package/esm/src/providers/local/provider.js +28 -0
- package/esm/src/providers/mod.d.ts +32 -0
- package/esm/src/providers/mod.js +23 -0
- package/esm/src/providers/open-ai/protocol.d.ts +3 -0
- package/esm/src/providers/open-ai/protocol.js +189 -0
- package/esm/src/providers/openrouter/embedding-protocol.d.ts +6 -0
- package/esm/src/providers/openrouter/embedding-protocol.js +86 -0
- package/esm/src/providers/openrouter/image.d.ts +15 -0
- package/esm/src/providers/openrouter/image.js +34 -0
- package/esm/src/providers/openrouter/images-protocol.d.ts +3 -0
- package/esm/src/providers/openrouter/images-protocol.js +82 -0
- package/esm/src/providers/openrouter/openai/compat.d.ts +19 -0
- package/esm/src/providers/openrouter/openai/compat.js +162 -0
- package/esm/src/providers/openrouter/openai/image-payload.d.ts +9 -0
- package/esm/src/providers/openrouter/openai/image-payload.js +115 -0
- package/esm/src/providers/openrouter/openai/reasoning-state.d.ts +2 -0
- package/esm/src/providers/openrouter/openai/reasoning-state.js +31 -0
- package/esm/src/providers/openrouter/openai/usage.d.ts +8 -0
- package/esm/src/providers/openrouter/openai/usage.js +56 -0
- package/esm/src/providers/openrouter/protocol.d.ts +8 -0
- package/esm/src/providers/openrouter/protocol.js +171 -0
- package/esm/src/providers/openrouter/provider.d.ts +10 -0
- package/esm/src/providers/openrouter/provider.js +50 -0
- package/esm/src/providers/shared/carried-tool-files.d.ts +7 -0
- package/esm/src/providers/shared/carried-tool-files.js +43 -0
- package/esm/src/providers/shared/pcm.d.ts +14 -0
- package/esm/src/providers/shared/pcm.js +78 -0
- package/esm/src/providers/shared/protocol-events.d.ts +8 -0
- package/esm/src/providers/shared/protocol-events.js +20 -0
- package/esm/src/providers/shared/response-identity.d.ts +11 -0
- package/esm/src/providers/shared/response-identity.js +12 -0
- package/esm/src/providers/shared/retry.d.ts +17 -0
- package/esm/src/providers/shared/retry.js +90 -0
- package/esm/src/providers/shared/sse.d.ts +6 -0
- package/esm/src/providers/shared/sse.js +93 -0
- package/esm/src/providers/shared/structured-output.d.ts +4 -0
- package/esm/src/providers/shared/structured-output.js +11 -0
- package/esm/src/providers/shared/tool-args.d.ts +24 -0
- package/esm/src/providers/shared/tool-args.js +88 -0
- package/esm/src/providers/shared/tool-draft.d.ts +16 -0
- package/esm/src/providers/shared/tool-draft.js +14 -0
- package/esm/src/providers/shared/upstream-tap.d.ts +8 -0
- package/esm/src/providers/shared/upstream-tap.js +85 -0
- package/esm/src/providers/shared/upstream-tape.d.ts +6 -0
- package/esm/src/providers/shared/upstream-tape.js +88 -0
- package/esm/src/providers/system-one/protocol.d.ts +8 -0
- package/esm/src/providers/system-one/protocol.js +93 -0
- package/esm/src/providers/types.d.ts +51 -0
- package/esm/src/providers/types.js +2 -0
- package/esm/src/providers/typesafe/provider.d.ts +7 -0
- package/esm/src/providers/typesafe/provider.js +24 -0
- package/esm/src/surface/formats.d.ts +46 -0
- package/esm/src/surface/formats.js +178 -0
- package/esm/src/surface/mod.d.ts +16 -0
- package/esm/src/surface/mod.js +12 -0
- package/esm/src/surface/runtime.d.ts +44 -0
- package/esm/src/surface/runtime.js +441 -0
- package/esm/src/surface/tools.d.ts +9 -0
- package/esm/src/surface/tools.js +62 -0
- package/esm/src/surface/types.d.ts +110 -0
- package/esm/src/surface/types.js +4 -0
- package/package.json +100 -4
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { isAbortError, TheoremError, toErrorEvent, withPublicLabels, } from '../../guardrails/error.js';
|
|
2
|
+
import { projectGuardrailTurnEvent } from '../../guardrails/events.js';
|
|
3
|
+
import { resolveTraceWriter } from '../../observability/policy.js';
|
|
4
|
+
import { writeTrace } from '../../observability/trace.js';
|
|
5
|
+
import { buildRecord } from '../../observability/trace-record.js';
|
|
6
|
+
import { startTrace, } from '../../observability/trace-span.js';
|
|
7
|
+
import { invokeAgentCaller } from '../engine/runner/mod.js';
|
|
8
|
+
import { inStep, runSideBySide, stepOrder } from '../engine/runner/step-order.js';
|
|
9
|
+
import { startToolTrace, toolSpanName } from '../engine/tool-trace.js';
|
|
10
|
+
import { endThrownSpan, optional, traceLinks } from '../engine/turn-trace.js';
|
|
11
|
+
import { pickModel } from '../registry/resolve.js';
|
|
12
|
+
import { turnDoneOf } from '../turn-events.js';
|
|
13
|
+
import { coordinateTool } from './coordinate.js';
|
|
14
|
+
import { failedActivity, failureEvent, newCallId, toolCallArguments, toolCallRequestEvent, } from './events.js';
|
|
15
|
+
import { authorizeToolReplay, executeRegisteredTool, } from './execute.js';
|
|
16
|
+
import { cloneTurnToolSnapshot, prepareTurnToolSnapshot, profileToolsParallel, promoteLoadedTools, } from './resolve.js';
|
|
17
|
+
import { plainToolInput } from './schema.js';
|
|
18
|
+
function turnRequestFromInvoke(request) {
|
|
19
|
+
return {
|
|
20
|
+
profile: request.profile,
|
|
21
|
+
path: request.path,
|
|
22
|
+
sessionPermissions: request.sessionPermissions,
|
|
23
|
+
input: request.turnInput,
|
|
24
|
+
model: request.model,
|
|
25
|
+
host: request.host,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
async function prepareInvokeSnapshot(registry, request, profile) {
|
|
29
|
+
const req = turnRequestFromInvoke(request);
|
|
30
|
+
if (profile.type === 'decision' || profile.type === 'embedding') {
|
|
31
|
+
throw new TheoremError('request', `Profile ${profile.id}: type 'decision' cannot invoke tools`); // labels-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
|
|
32
|
+
}
|
|
33
|
+
const model = profile.type === 'host' ? undefined : pickModel(profile, request.model);
|
|
34
|
+
return await prepareTurnToolSnapshot(registry.tools, profile, req, model);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* One trace record around an invoke, written however it ends, including an early stop or a
|
|
38
|
+
* failure before the tool. The profile's labels and guardrail policy shape every event. An unknown
|
|
39
|
+
* profile is recorded under the standard observability policy: there is none to read.
|
|
40
|
+
*/
|
|
41
|
+
async function* invokeInTrace(registry, request, spanName, sinkOverride, run) {
|
|
42
|
+
const known = registry.profiles.find(request.profile);
|
|
43
|
+
const { sink, policy } = resolveTraceWriter({
|
|
44
|
+
override: sinkOverride,
|
|
45
|
+
observability: known?.observability,
|
|
46
|
+
guardrails: known?.guardrails,
|
|
47
|
+
});
|
|
48
|
+
const tree = startTrace(spanName, {
|
|
49
|
+
attributes: {
|
|
50
|
+
'gen_ai.agent.name': request.profile,
|
|
51
|
+
...optional('gen_ai.conversation.id', request.conversationId),
|
|
52
|
+
},
|
|
53
|
+
links: traceLinks(request.links),
|
|
54
|
+
...(request.traceparent ? { traceparent: request.traceparent } : {}),
|
|
55
|
+
});
|
|
56
|
+
// why: Agent tool calls run nested turns in this record; their canaries are scrubbed from it.
|
|
57
|
+
const canaries = [];
|
|
58
|
+
try {
|
|
59
|
+
const labels = known?.labels;
|
|
60
|
+
for await (const event of run.events(tree, canaries)) {
|
|
61
|
+
// invariant: A match is the caught text itself; it leaves only when the profile asks for it.
|
|
62
|
+
yield projectGuardrailTurnEvent(withPublicLabels(event, labels), policy.include.guardrailMatchPreview);
|
|
63
|
+
}
|
|
64
|
+
run.ended?.(tree);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
run.failed(tree, err);
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await writeTrace(sink, buildRecord({
|
|
72
|
+
spans: tree.collect(),
|
|
73
|
+
policy,
|
|
74
|
+
canaries,
|
|
75
|
+
...(request.metadata ? { metadata: request.metadata } : {}),
|
|
76
|
+
}), policy);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Close the call's span for a failure before its tool ran. */
|
|
80
|
+
function failCallBeforeTool(openSpan, invoke, callId) {
|
|
81
|
+
return (end) => startToolTrace(openSpan, {
|
|
82
|
+
name: invoke.name,
|
|
83
|
+
callId,
|
|
84
|
+
call: { arguments: toolCallArguments(plainToolInput(invoke.input)) },
|
|
85
|
+
}).end(end);
|
|
86
|
+
}
|
|
87
|
+
/** The profile an invoke runs under, and its own copy of the tool snapshot. */
|
|
88
|
+
async function invokeSnapshot(registry, request) {
|
|
89
|
+
const profile = registry.profiles.get(request.profile);
|
|
90
|
+
const snapshot = request.snapshot
|
|
91
|
+
? cloneTurnToolSnapshot(request.snapshot)
|
|
92
|
+
: await prepareInvokeSnapshot(registry, request, profile);
|
|
93
|
+
return { profile, snapshot };
|
|
94
|
+
}
|
|
95
|
+
function invokeTool(registry, request, sinkOverride) {
|
|
96
|
+
return startInvoke(registry, request, sinkOverride).events;
|
|
97
|
+
}
|
|
98
|
+
function startInvoke(registry, request, sinkOverride) {
|
|
99
|
+
const callId = request.callId ?? newCallId(request.name);
|
|
100
|
+
const invoke = {
|
|
101
|
+
...request,
|
|
102
|
+
operationId: request.operationId ?? crypto.randomUUID(),
|
|
103
|
+
};
|
|
104
|
+
const openSpan = (tree) => (_name, attributes) => {
|
|
105
|
+
tree.root.set(attributes);
|
|
106
|
+
return tree.root;
|
|
107
|
+
};
|
|
108
|
+
const events = invokeInTrace(registry, invoke, toolSpanName(invoke.name), sinkOverride, {
|
|
109
|
+
events: (tree, canaries) => invokeTraced(registry, invoke, callId, openSpan(tree), failCallBeforeTool(openSpan(tree), invoke, callId), tree.root.traceparent(), canaries),
|
|
110
|
+
failed: (tree, err) => failCallBeforeTool(openSpan(tree), invoke, callId)(isAbortError(err) ? { outcome: 'cancelled' } : { outcome: 'error', thrown: err }),
|
|
111
|
+
});
|
|
112
|
+
return { name: invoke.name, callId, events };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Runs the host's calls as one step, the way a turn runs the model's. Every call the step names
|
|
116
|
+
* itself is announced with the step's id before any runs. With `tools.parallel` they run side by
|
|
117
|
+
* side, otherwise one at a time in the order given; a call whose `pre_tool` answers `waitFor`
|
|
118
|
+
* starts once those calls in the step settle. Each call's events arrive as it gives them, and its
|
|
119
|
+
* terminal `tool` event says it settled. The calls share one snapshot. A call that gates waits for
|
|
120
|
+
* its own approval while the others run; the step then ends `gate`. One trace record holds a span
|
|
121
|
+
* per call under the step's root, rooted under the host span when one is given.
|
|
122
|
+
*/
|
|
123
|
+
function invokeToolStep(registry, request, sinkOverride) {
|
|
124
|
+
return startInvokeStep(registry, request, sinkOverride).events;
|
|
125
|
+
}
|
|
126
|
+
function startInvokeStep(registry, request, sinkOverride) {
|
|
127
|
+
const { calls: given, ...shared } = request;
|
|
128
|
+
const calls = given.map((call) => {
|
|
129
|
+
const callId = call.callId ?? newCallId(call.name);
|
|
130
|
+
return {
|
|
131
|
+
callId,
|
|
132
|
+
invoke: {
|
|
133
|
+
...shared,
|
|
134
|
+
name: call.name,
|
|
135
|
+
input: call.input,
|
|
136
|
+
callId,
|
|
137
|
+
operationId: call.operationId ?? crypto.randomUUID(),
|
|
138
|
+
},
|
|
139
|
+
// why: A model's call was already announced by its turn; only a call the step names itself is announced here.
|
|
140
|
+
announce: call.callId === undefined,
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
const events = invokeInTrace(registry, request, `tool_step ${request.profile}`, sinkOverride, {
|
|
144
|
+
events: (tree, canaries) => invokeStepTraced(registry, request, calls, (name, attributes) => tree.root.child(name, { attributes }), tree.root.traceparent(), canaries),
|
|
145
|
+
ended: (tree) => tree.root.end({ code: 'OK' }),
|
|
146
|
+
failed: (tree, err) => {
|
|
147
|
+
if (isAbortError(err))
|
|
148
|
+
tree.root.end();
|
|
149
|
+
else
|
|
150
|
+
endThrownSpan(tree.root, err);
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
return {
|
|
154
|
+
calls: calls.map(({ callId, invoke }) => ({ name: invoke.name, callId })),
|
|
155
|
+
events,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
async function* invokeStepTraced(registry, request, calls, openSpan, traceparent, canaries) {
|
|
159
|
+
const { profile, snapshot } = await invokeSnapshot(registry, request);
|
|
160
|
+
const stepId = crypto.randomUUID();
|
|
161
|
+
for (const { callId, invoke, announce } of calls) {
|
|
162
|
+
if (!announce)
|
|
163
|
+
continue;
|
|
164
|
+
yield toolCallRequestEvent({ name: invoke.name, callId, operationId: invoke.operationId }, toolCallArguments(invoke.input), { stepId, stepCallCount: calls.length });
|
|
165
|
+
}
|
|
166
|
+
const order = stepOrder(calls.map(({ callId }) => callId), profileToolsParallel(profile) === true, request.signal);
|
|
167
|
+
const step = {
|
|
168
|
+
stepCalls: calls.map(({ callId, invoke }) => ({
|
|
169
|
+
callId,
|
|
170
|
+
tool: invoke.name,
|
|
171
|
+
input: toolCallArguments(invoke.input),
|
|
172
|
+
})),
|
|
173
|
+
wait: order.wait,
|
|
174
|
+
};
|
|
175
|
+
let gated = false;
|
|
176
|
+
try {
|
|
177
|
+
// why: As in a turn's step, one call that throws ends the step; the others are closed.
|
|
178
|
+
const settlements = yield* runSideBySide(calls.map(({ callId, invoke }) => inStep(order, callId, () => runCall(registry, invoke, profile, snapshot, callId, openSpan, failCallBeforeTool(openSpan, invoke, callId), canaries, step))));
|
|
179
|
+
gated = settlements.some((settlement) => settlement?.gated !== undefined);
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
yield toErrorEvent(err);
|
|
183
|
+
}
|
|
184
|
+
yield turnDoneOf({
|
|
185
|
+
stop: { kind: gated ? 'gate' : 'completed' },
|
|
186
|
+
traceparent,
|
|
187
|
+
}, snapshot);
|
|
188
|
+
}
|
|
189
|
+
async function* invokeTraced(registry, request, callId, openSpan, failBeforeTool, traceparent, canaries) {
|
|
190
|
+
const { profile, snapshot } = await invokeSnapshot(registry, request);
|
|
191
|
+
// why: A model's call was already announced by its turn.
|
|
192
|
+
if (request.callId === undefined) {
|
|
193
|
+
yield toolCallRequestEvent({ name: request.name, callId, operationId: request.operationId }, toolCallArguments(request.input));
|
|
194
|
+
}
|
|
195
|
+
let gated = false;
|
|
196
|
+
try {
|
|
197
|
+
const settlement = yield* runCall(registry, request, profile, snapshot, callId, openSpan, failBeforeTool, canaries);
|
|
198
|
+
gated = settlement.gated !== undefined;
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
yield toErrorEvent(err);
|
|
202
|
+
}
|
|
203
|
+
yield turnDoneOf({
|
|
204
|
+
stop: { kind: gated ? 'gate' : 'completed' },
|
|
205
|
+
traceparent,
|
|
206
|
+
}, snapshot);
|
|
207
|
+
}
|
|
208
|
+
/** One call, coordinated: replayed when its operation already ran, else executed. */
|
|
209
|
+
function runCall(registry, request, profile, snapshot, callId, openSpan, failBeforeTool, canaries, step) {
|
|
210
|
+
return coordinateTool(request.execution, {
|
|
211
|
+
operationId: request.operationId ?? crypto.randomUUID(),
|
|
212
|
+
callId,
|
|
213
|
+
profileId: profile.id,
|
|
214
|
+
toolId: request.name,
|
|
215
|
+
}, {
|
|
216
|
+
input: request.input,
|
|
217
|
+
resume: request.resume,
|
|
218
|
+
snapshot,
|
|
219
|
+
context: {
|
|
220
|
+
model: request.model,
|
|
221
|
+
turnInput: request.turnInput,
|
|
222
|
+
promoted: request.promoted,
|
|
223
|
+
},
|
|
224
|
+
}, request.signal, (beginBody) => invokePrepared(registry, request, profile, snapshot, callId, openSpan, failBeforeTool, canaries, beginBody, step), (outcome) => authorizeToolReplay({
|
|
225
|
+
outcome,
|
|
226
|
+
tools: registry.tools,
|
|
227
|
+
profile,
|
|
228
|
+
name: request.name,
|
|
229
|
+
resume: request.resume,
|
|
230
|
+
snapshot,
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
async function* invokePrepared(registry, request, profile, snapshot, callId, openSpan, failBeforeTool, canaries, beginBody, step) {
|
|
234
|
+
if (request.promoted?.length) {
|
|
235
|
+
const { failure } = promoteLoadedTools(registry.tools, snapshot, request.promoted, profile);
|
|
236
|
+
if (failure) {
|
|
237
|
+
failBeforeTool({ outcome: 'error', failure });
|
|
238
|
+
yield failureEvent({
|
|
239
|
+
name: request.name,
|
|
240
|
+
callId,
|
|
241
|
+
operationId: request.operationId,
|
|
242
|
+
activityFailed: failedActivity(request.name, profile.labels),
|
|
243
|
+
}, failure);
|
|
244
|
+
return { failure, callNotStarted: true };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const handlers = request.onStage ? [request.onStage] : [];
|
|
248
|
+
return yield* executeRegisteredTool({
|
|
249
|
+
tools: registry.tools,
|
|
250
|
+
beginBody,
|
|
251
|
+
agents: invokeAgentCaller(registry, request, canaries),
|
|
252
|
+
profile,
|
|
253
|
+
name: request.name,
|
|
254
|
+
input: request.input,
|
|
255
|
+
callId,
|
|
256
|
+
ctx: {
|
|
257
|
+
operationId: request.operationId,
|
|
258
|
+
sessionPermissions: request.sessionPermissions,
|
|
259
|
+
credentials: request.credentials,
|
|
260
|
+
resolveHost: request.resolveHost,
|
|
261
|
+
path: request.path,
|
|
262
|
+
signal: request.signal,
|
|
263
|
+
resume: request.resume,
|
|
264
|
+
page: request.page,
|
|
265
|
+
pageExecution: request.pageExecution,
|
|
266
|
+
host: request.host,
|
|
267
|
+
},
|
|
268
|
+
snapshot,
|
|
269
|
+
openSpan,
|
|
270
|
+
stages: {
|
|
271
|
+
handlers,
|
|
272
|
+
profile,
|
|
273
|
+
step: 1,
|
|
274
|
+
history: () => [],
|
|
275
|
+
injectAllowed: false,
|
|
276
|
+
host: request.host,
|
|
277
|
+
signal: request.signal,
|
|
278
|
+
...step,
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
export { invokeTool, invokeToolStep, startInvoke, startInvokeStep };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sessions for MCP servers that refuse a call without one. A session is a cache, not state: it
|
|
3
|
+
* lives in memory on one tool registry, is never persisted, and losing it costs one reopen.
|
|
4
|
+
*/
|
|
5
|
+
export type McpSession = {
|
|
6
|
+
id: string;
|
|
7
|
+
protocolVersion: string;
|
|
8
|
+
};
|
|
9
|
+
export interface McpSessionCache {
|
|
10
|
+
get(key: string): McpSession | undefined;
|
|
11
|
+
/** Parallel callers for one key share a single `open`. */
|
|
12
|
+
open(key: string, open: () => Promise<McpSession>): Promise<McpSession>;
|
|
13
|
+
/** Only drops the session if it's still `id`, so a late expiry can't evict a newer one. */
|
|
14
|
+
drop(key: string, id: string): void;
|
|
15
|
+
clear(): void;
|
|
16
|
+
}
|
|
17
|
+
export declare function createMcpSessionCache(now?: () => number): McpSessionCache;
|
|
18
|
+
export declare function isMcpSessionId(value: string | null): value is string;
|
|
19
|
+
/**
|
|
20
|
+
* One session per server and credential. The URL is hashed with the headers, since a key can sit
|
|
21
|
+
* in either; the credential is never kept.
|
|
22
|
+
*/
|
|
23
|
+
export declare function mcpSessionKey(url: string, headers: Record<string, string>): Promise<string>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sessions for MCP servers that refuse a call without one. A session is a cache, not state: it
|
|
3
|
+
* lives in memory on one tool registry, is never persisted, and losing it costs one reopen.
|
|
4
|
+
*/
|
|
5
|
+
const MAX_SESSIONS = 256;
|
|
6
|
+
const IDLE_MS = 30 * 60 * 1000;
|
|
7
|
+
// why: The spec allows visible ASCII only; the cap keeps a server from bloating every later request.
|
|
8
|
+
const SESSION_ID = /^[\x21-\x7E]{1,256}$/;
|
|
9
|
+
export function createMcpSessionCache(now = Date.now) {
|
|
10
|
+
const sessions = new Map();
|
|
11
|
+
const pending = new Map();
|
|
12
|
+
return {
|
|
13
|
+
get(key) {
|
|
14
|
+
const entry = sessions.get(key);
|
|
15
|
+
if (!entry)
|
|
16
|
+
return undefined;
|
|
17
|
+
sessions.delete(key);
|
|
18
|
+
if (now() - entry.usedAt > IDLE_MS)
|
|
19
|
+
return undefined;
|
|
20
|
+
// why: Re-inserting keeps the map in least-recently-used order for eviction.
|
|
21
|
+
sessions.set(key, { ...entry, usedAt: now() });
|
|
22
|
+
return { id: entry.id, protocolVersion: entry.protocolVersion };
|
|
23
|
+
},
|
|
24
|
+
open(key, open) {
|
|
25
|
+
const inFlight = pending.get(key);
|
|
26
|
+
if (inFlight)
|
|
27
|
+
return inFlight;
|
|
28
|
+
const opening = open()
|
|
29
|
+
.then((session) => {
|
|
30
|
+
sessions.delete(key);
|
|
31
|
+
sessions.set(key, { ...session, usedAt: now() });
|
|
32
|
+
for (const oldest of sessions.keys()) {
|
|
33
|
+
if (sessions.size <= MAX_SESSIONS)
|
|
34
|
+
break;
|
|
35
|
+
sessions.delete(oldest);
|
|
36
|
+
}
|
|
37
|
+
return session;
|
|
38
|
+
})
|
|
39
|
+
.finally(() => pending.delete(key));
|
|
40
|
+
pending.set(key, opening);
|
|
41
|
+
return opening;
|
|
42
|
+
},
|
|
43
|
+
drop(key, id) {
|
|
44
|
+
if (sessions.get(key)?.id === id)
|
|
45
|
+
sessions.delete(key);
|
|
46
|
+
},
|
|
47
|
+
clear() {
|
|
48
|
+
sessions.clear();
|
|
49
|
+
pending.clear();
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function isMcpSessionId(value) {
|
|
54
|
+
return value !== null && SESSION_ID.test(value);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* One session per server and credential. The URL is hashed with the headers, since a key can sit
|
|
58
|
+
* in either; the credential is never kept.
|
|
59
|
+
*/
|
|
60
|
+
export async function mcpSessionKey(url, headers) {
|
|
61
|
+
const canonical = JSON.stringify([
|
|
62
|
+
url,
|
|
63
|
+
Object.entries(headers)
|
|
64
|
+
.map(([name, value]) => [name.toLowerCase(), value])
|
|
65
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
|
|
66
|
+
]);
|
|
67
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(canonical));
|
|
68
|
+
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
69
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { authorizeToolReplay, coerceToolResultParts, executeRegisteredTool, leanToolResultData, projectForModel, } from './execute.js';
|
|
2
|
+
export { askUserTool, registerHarnessTools } from './harness.js';
|
|
3
|
+
export { formatToolResult } from './model-text.js';
|
|
4
|
+
export type { ToolRegistry } from './registry.js';
|
|
5
|
+
export { createToolRegistry } from './registry.js';
|
|
6
|
+
export type { CredentialQuery, McpProtocolVersion, McpRpcResponse } from './remote.js';
|
|
7
|
+
export { buildHttpToolTarget, credentialPlacement, executeHttpTool, executeMcpTool, isUnsupportedMcpProtocolError, MCP_PROTOCOL_VERSIONS, parseMcpRpcResponse, resolveToolAuth, withCredentialQuery, } from './remote.js';
|
|
8
|
+
export { cloneTurnToolSnapshot, prepareTurnToolSnapshot } from './resolve.js';
|
|
9
|
+
export type * from './types.js';
|
|
10
|
+
export { UncheckedOutput, uncheckedOutput } from './unchecked-output.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { authorizeToolReplay, coerceToolResultParts, executeRegisteredTool, leanToolResultData, projectForModel, } from './execute.js';
|
|
2
|
+
export { askUserTool, registerHarnessTools } from './harness.js';
|
|
3
|
+
export { formatToolResult } from './model-text.js';
|
|
4
|
+
export { createToolRegistry } from './registry.js';
|
|
5
|
+
export { buildHttpToolTarget, credentialPlacement, executeHttpTool, executeMcpTool, isUnsupportedMcpProtocolError, MCP_PROTOCOL_VERSIONS, parseMcpRpcResponse, resolveToolAuth, withCredentialQuery, } from './remote.js';
|
|
6
|
+
export { cloneTurnToolSnapshot, prepareTurnToolSnapshot } from './resolve.js';
|
|
7
|
+
export { UncheckedOutput, uncheckedOutput } from './unchecked-output.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ModelToolResult, ToolFailure } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Every transport and replay formats through here, so the model's view of a call cannot drift.
|
|
4
|
+
* Never embeds `parts[].data`: media travels on `TurnHistoryMessage.parts`. A result without
|
|
5
|
+
* `modelText` (a host replaying a transcript) has no profile in hand, so every detector's
|
|
6
|
+
* matches are replaced.
|
|
7
|
+
*/
|
|
8
|
+
export declare function formatToolResult(result: ModelToolResult): string;
|
|
9
|
+
/** A failure as the kernel reports it to the model. The message is used as given. */
|
|
10
|
+
export declare function frameToolFailure(failure: Pick<ToolFailure, 'code' | 'message' | 'details'>): ModelToolResult;
|
|
11
|
+
/**
|
|
12
|
+
* A failure framed with no profile in hand: the message is remote-authored on HTTP and MCP
|
|
13
|
+
* tools, so every detector's matches are replaced first. A call that settles through the
|
|
14
|
+
* executor reads the message at its own boundary instead.
|
|
15
|
+
*/
|
|
16
|
+
export declare function formatToolFailureForModel(failure: Pick<ToolFailure, 'code' | 'message' | 'details'>): ModelToolResult;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { redactDetectors } from '../../guardrails/detect-at.js';
|
|
2
|
+
import { DETECTORS } from '../../guardrails/detectors.js';
|
|
3
|
+
import { composeToolText } from '../../guardrails/tool-result.js';
|
|
4
|
+
/**
|
|
5
|
+
* Every transport and replay formats through here, so the model's view of a call cannot drift.
|
|
6
|
+
* Never embeds `parts[].data`: media travels on `TurnHistoryMessage.parts`. A result without
|
|
7
|
+
* `modelText` (a host replaying a transcript) has no profile in hand, so every detector's
|
|
8
|
+
* matches are replaced.
|
|
9
|
+
*/
|
|
10
|
+
export function formatToolResult(result) {
|
|
11
|
+
if (result.modelText !== undefined) {
|
|
12
|
+
return result.modelText;
|
|
13
|
+
}
|
|
14
|
+
return redactDetectors(composeToolText(result.finding, result.data), DETECTORS);
|
|
15
|
+
}
|
|
16
|
+
/** A failure as the kernel reports it to the model. The message is used as given. */
|
|
17
|
+
export function frameToolFailure(failure) {
|
|
18
|
+
return {
|
|
19
|
+
finding: `Tool error (${failure.code}): ${failure.message}`,
|
|
20
|
+
// why: The finding already says the code and message; only details are new.
|
|
21
|
+
...(failure.details !== undefined ? { data: { details: failure.details } } : {}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A failure framed with no profile in hand: the message is remote-authored on HTTP and MCP
|
|
26
|
+
* tools, so every detector's matches are replaced first. A call that settles through the
|
|
27
|
+
* executor reads the message at its own boundary instead.
|
|
28
|
+
*/
|
|
29
|
+
export function formatToolFailureForModel(failure) {
|
|
30
|
+
return frameToolFailure({ ...failure, message: redactDetectors(failure.message, DETECTORS) });
|
|
31
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { TheoremError } from '../../guardrails/error.js';
|
|
3
|
+
import { type JsonObject, type JsonValue } from '../json.js';
|
|
4
|
+
import type { GateReference, TurnEventOf } from '../turn-events.js';
|
|
5
|
+
import type { TurnHistoryMessage } from '../types.js';
|
|
6
|
+
import type { ModelToolResult, PageAnswer, ToolFailure } from './types.js';
|
|
7
|
+
export type TerminalToolEvent = TurnEventOf<'tool'> & {
|
|
8
|
+
tool: Extract<TurnEventOf<'tool'>['tool'], {
|
|
9
|
+
phase: 'complete' | 'error';
|
|
10
|
+
}>;
|
|
11
|
+
};
|
|
12
|
+
export interface SavedToolInvocation {
|
|
13
|
+
accepted: JsonObject;
|
|
14
|
+
preparedInput: JsonValue;
|
|
15
|
+
}
|
|
16
|
+
export declare const savedToolInvocationSchema: z.ZodType<SavedToolInvocation>;
|
|
17
|
+
export interface ToolOperationIdentity extends GateReference {
|
|
18
|
+
profileId: string;
|
|
19
|
+
toolId: string;
|
|
20
|
+
invocationHash: string;
|
|
21
|
+
invocation?: SavedToolInvocation;
|
|
22
|
+
}
|
|
23
|
+
export interface ToolOperationClaim extends ToolOperationIdentity {
|
|
24
|
+
claimToken: string;
|
|
25
|
+
}
|
|
26
|
+
export interface PageToolDispatch {
|
|
27
|
+
grantId: string;
|
|
28
|
+
result: Promise<PageAnswer>;
|
|
29
|
+
}
|
|
30
|
+
export interface PageToolExecution {
|
|
31
|
+
dispatch(claim: ToolOperationClaim, signal?: AbortSignal): Promise<PageToolDispatch>;
|
|
32
|
+
}
|
|
33
|
+
export interface ToolOperationOutcome {
|
|
34
|
+
event: TerminalToolEvent;
|
|
35
|
+
effects: 'not_started' | 'completed' | 'unknown';
|
|
36
|
+
replay: {
|
|
37
|
+
modelResult?: ModelToolResult;
|
|
38
|
+
outputRaw?: unknown;
|
|
39
|
+
failure?: ToolFailure;
|
|
40
|
+
awaiting?: boolean;
|
|
41
|
+
denied?: true;
|
|
42
|
+
callNotStarted?: boolean;
|
|
43
|
+
aborted?: boolean | {
|
|
44
|
+
reason?: string;
|
|
45
|
+
};
|
|
46
|
+
pendingInject?: {
|
|
47
|
+
id?: string;
|
|
48
|
+
messages: TurnHistoryMessage[];
|
|
49
|
+
}[];
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export type ToolOperationUncertainty = 'execution_interrupted' | 'settlement_write_failed' | 'worker_lost';
|
|
53
|
+
export type BeginToolOperationResult = {
|
|
54
|
+
kind: 'acquired';
|
|
55
|
+
claimToken: string;
|
|
56
|
+
} | {
|
|
57
|
+
kind: 'settled';
|
|
58
|
+
outcome: ToolOperationOutcome;
|
|
59
|
+
} | {
|
|
60
|
+
kind: 'running';
|
|
61
|
+
} | {
|
|
62
|
+
kind: 'uncertain';
|
|
63
|
+
};
|
|
64
|
+
export interface ToolExecutionCoordinator {
|
|
65
|
+
lookup(operation: ToolOperationIdentity): Promise<Exclude<BeginToolOperationResult, {
|
|
66
|
+
kind: 'acquired';
|
|
67
|
+
}> | undefined>;
|
|
68
|
+
begin(operation: ToolOperationIdentity): Promise<BeginToolOperationResult>;
|
|
69
|
+
settle(claim: ToolOperationClaim, outcome: ToolOperationOutcome): Promise<void>;
|
|
70
|
+
settleNotStarted(operation: ToolOperationIdentity, outcome: ToolOperationOutcome): Promise<void>;
|
|
71
|
+
markUncertain(claim: ToolOperationClaim, reason: ToolOperationUncertainty): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
export declare function operationUnavailable(): TheoremError;
|
|
74
|
+
export declare function toolInvocationHash(value: unknown): Promise<string>;
|
|
75
|
+
export declare const toolOperationOutcomeSchema: z.ZodType<ToolOperationOutcome>;
|
|
76
|
+
export declare function operationWireValue(value: unknown): JsonValue;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { publicError, TheoremError } from '../../guardrails/error.js';
|
|
3
|
+
import { errorKindSchema, guardrailHit, provenance } from '../../guardrails/event-schemas.js';
|
|
4
|
+
import { jsonObjectSchema, jsonValueSchema } from '../json.js';
|
|
5
|
+
import { interactionPartSchema, TURN_EVENT_SCHEMAS, turnHistoryMessageSchema, } from '../turn-events.js';
|
|
6
|
+
export const savedToolInvocationSchema = z.strictObject({
|
|
7
|
+
accepted: jsonObjectSchema,
|
|
8
|
+
preparedInput: jsonValueSchema,
|
|
9
|
+
});
|
|
10
|
+
export function operationUnavailable() {
|
|
11
|
+
const error = new TheoremError('unavailable', '');
|
|
12
|
+
error.message = publicError(error);
|
|
13
|
+
return error;
|
|
14
|
+
}
|
|
15
|
+
export async function toolInvocationHash(value) {
|
|
16
|
+
const ordered = (item) => {
|
|
17
|
+
if (Array.isArray(item))
|
|
18
|
+
return item.map(ordered);
|
|
19
|
+
if (item && typeof item === 'object') {
|
|
20
|
+
return Object.fromEntries(Object.entries(item)
|
|
21
|
+
.filter(([, entry]) => entry !== undefined)
|
|
22
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
23
|
+
.map(([key, entry]) => [key, ordered(entry)]));
|
|
24
|
+
}
|
|
25
|
+
return item;
|
|
26
|
+
};
|
|
27
|
+
const data = jsonValueSchema.parse(ordered(operationWireValue(value)));
|
|
28
|
+
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(JSON.stringify(data)));
|
|
29
|
+
return Array.from(new Uint8Array(hash), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
30
|
+
}
|
|
31
|
+
const terminalSchema = TURN_EVENT_SCHEMAS.tool.transform((event, ctx) => {
|
|
32
|
+
if (event.tool.phase === 'complete' || event.tool.phase === 'error') {
|
|
33
|
+
return { ...event, tool: event.tool };
|
|
34
|
+
}
|
|
35
|
+
ctx.addIssue({ code: 'custom' });
|
|
36
|
+
return z.NEVER;
|
|
37
|
+
});
|
|
38
|
+
export const toolOperationOutcomeSchema = z.strictObject({
|
|
39
|
+
event: terminalSchema,
|
|
40
|
+
effects: z.enum(['not_started', 'completed', 'unknown']),
|
|
41
|
+
replay: z.strictObject({
|
|
42
|
+
modelResult: z
|
|
43
|
+
.strictObject({
|
|
44
|
+
finding: z.string(),
|
|
45
|
+
data: jsonValueSchema.optional(),
|
|
46
|
+
parts: z.array(interactionPartSchema).optional(),
|
|
47
|
+
modelText: z.string().optional(),
|
|
48
|
+
provenance: provenance.optional(),
|
|
49
|
+
suspicious: z.array(guardrailHit).optional(),
|
|
50
|
+
})
|
|
51
|
+
.optional(),
|
|
52
|
+
outputRaw: jsonValueSchema.optional(),
|
|
53
|
+
failure: z
|
|
54
|
+
.strictObject({
|
|
55
|
+
code: z.string(),
|
|
56
|
+
kind: errorKindSchema,
|
|
57
|
+
message: z.string(),
|
|
58
|
+
error: z.string().optional(),
|
|
59
|
+
details: jsonValueSchema.optional(),
|
|
60
|
+
})
|
|
61
|
+
.optional(),
|
|
62
|
+
awaiting: z.boolean().optional(),
|
|
63
|
+
denied: z.literal(true).optional(),
|
|
64
|
+
callNotStarted: z.boolean().optional(),
|
|
65
|
+
aborted: z.union([z.boolean(), z.strictObject({ reason: z.string().optional() })]).optional(),
|
|
66
|
+
pendingInject: z
|
|
67
|
+
.array(z.strictObject({ id: z.string().optional(), messages: z.array(turnHistoryMessageSchema) }))
|
|
68
|
+
.optional(),
|
|
69
|
+
}),
|
|
70
|
+
});
|
|
71
|
+
export function operationWireValue(value) {
|
|
72
|
+
const ancestors = new Set();
|
|
73
|
+
const normalize = (item) => {
|
|
74
|
+
if (!item || typeof item !== 'object')
|
|
75
|
+
return item;
|
|
76
|
+
if (ancestors.has(item))
|
|
77
|
+
throw operationUnavailable();
|
|
78
|
+
const prototype = Object.getPrototypeOf(item);
|
|
79
|
+
if (!Array.isArray(item) && prototype !== Object.prototype && prototype !== null) {
|
|
80
|
+
throw operationUnavailable();
|
|
81
|
+
}
|
|
82
|
+
if (Object.hasOwn(item, '__proto__'))
|
|
83
|
+
throw operationUnavailable();
|
|
84
|
+
ancestors.add(item);
|
|
85
|
+
try {
|
|
86
|
+
if (Array.isArray(item))
|
|
87
|
+
return item.map(normalize);
|
|
88
|
+
const entries = Object.entries(Object.getOwnPropertyDescriptors(item));
|
|
89
|
+
if (entries.some(([, descriptor]) => descriptor.get || descriptor.set))
|
|
90
|
+
throw operationUnavailable();
|
|
91
|
+
return Object.fromEntries(entries
|
|
92
|
+
.filter(([, descriptor]) => descriptor.enumerable && descriptor.value !== undefined)
|
|
93
|
+
.map(([key, descriptor]) => [key, normalize(descriptor.value)]));
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
ancestors.delete(item);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
return jsonValueSchema.parse(normalize(value));
|
|
100
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ToolHandler } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The handler of a function tool the page answers (`answeredBy: 'page'`): it returns what the
|
|
4
|
+
* page sent for the call (`ctx.page.output`), which the tool's `output` schema then checks. A
|
|
5
|
+
* call the page never answered fails to the model. A call run with no answer at all never
|
|
6
|
+
* reaches here: it is held at a `page` gate.
|
|
7
|
+
*/
|
|
8
|
+
export declare function pageAnswerHandler(name: string): ToolHandler<unknown, unknown>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { labelText } from '../../guardrails/labels.js';
|
|
2
|
+
import { uncheckedOutput } from './unchecked-output.js';
|
|
3
|
+
/**
|
|
4
|
+
* The handler of a function tool the page answers (`answeredBy: 'page'`): it returns what the
|
|
5
|
+
* page sent for the call (`ctx.page.output`), which the tool's `output` schema then checks. A
|
|
6
|
+
* call the page never answered fails to the model. A call run with no answer at all never
|
|
7
|
+
* reaches here: it is held at a `page` gate.
|
|
8
|
+
*/
|
|
9
|
+
export function pageAnswerHandler(name) {
|
|
10
|
+
return (_input, ctx) => {
|
|
11
|
+
const { page } = ctx;
|
|
12
|
+
if (page && 'output' in page)
|
|
13
|
+
return uncheckedOutput(page.output);
|
|
14
|
+
const { labels } = ctx.profile;
|
|
15
|
+
if (page?.timedOut)
|
|
16
|
+
throw new Error(labelText('tool.page_timed_out', {}, labels));
|
|
17
|
+
throw new Error(labelText('tool.page_no_answer', { tool: name }, labels));
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ToolAccess } from '../schema.js';
|
|
2
|
+
import type { ToolGateBase } from '../turn-events.js';
|
|
3
|
+
import type { InvokeToolResume, ToolGate, ToolLabels, ToolPermission } from './types.js';
|
|
4
|
+
export declare function isResumeContinuation(resume?: InvokeToolResume): boolean;
|
|
5
|
+
export declare function isGateResumeGranted(resume?: InvokeToolResume): boolean;
|
|
6
|
+
export declare function isGateResumeDenied(resume?: InvokeToolResume): boolean;
|
|
7
|
+
export declare function permissionGranted(toolName: string, sessionPermissions?: string[]): boolean;
|
|
8
|
+
/** What a gate says about the call beyond its kind: the tool's access and its filled request. */
|
|
9
|
+
export type GateDetails = Pick<ToolGateBase, 'access' | 'request'>;
|
|
10
|
+
/** What an approval card says about a call: what it would do, and what the tool can change. */
|
|
11
|
+
export declare function gateDetails(tool: {
|
|
12
|
+
access: ToolAccess;
|
|
13
|
+
labels?: ToolLabels;
|
|
14
|
+
}, input: unknown): GateDetails;
|
|
15
|
+
export declare function checkPermission(toolName: string, permission: ToolPermission, sessionPermissions?: string[], resume?: InvokeToolResume, details?: GateDetails): ToolGate | null;
|