@openllmsh/sdk 2.10.12-alpha.0 → 2.10.12-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -7
- package/dist/client/chat.d.ts +38 -0
- package/dist/client/chat.js +77 -0
- package/dist/client/devices.d.ts +70 -0
- package/dist/client/devices.js +172 -0
- package/dist/client/http.d.ts +24 -0
- package/dist/client/http.js +34 -0
- package/dist/client/index.d.ts +43 -0
- package/dist/client/index.js +57 -0
- package/dist/client/relay-mux.d.ts +71 -0
- package/dist/client/relay-mux.js +394 -0
- package/dist/client/types.d.ts +47 -0
- package/dist/client/types.js +5 -0
- package/dist/client/vault.d.ts +68 -0
- package/dist/client/vault.js +113 -0
- package/dist/connect/chat.d.ts +24 -0
- package/dist/connect/chat.js +25 -0
- package/dist/connect/client.d.ts +50 -0
- package/dist/connect/client.js +141 -0
- package/dist/connect/config.d.ts +22 -0
- package/dist/connect/config.js +22 -0
- package/dist/connect/errors.d.ts +32 -0
- package/dist/connect/errors.js +85 -0
- package/{connect/index.ts → dist/connect/index.d.ts} +7 -7
- package/dist/connect/index.js +23 -0
- package/dist/connect/oauth.d.ts +51 -0
- package/dist/connect/oauth.js +150 -0
- package/dist/connect/routes.d.ts +38 -0
- package/dist/connect/routes.js +175 -0
- package/dist/connect/store.d.ts +26 -0
- package/dist/connect/store.js +65 -0
- package/dist/mcp/handle.d.ts +6 -0
- package/dist/mcp/handle.js +6 -0
- package/dist/mcp/index.d.ts +15 -0
- package/dist/mcp/index.js +10 -0
- package/dist/mcp/server.d.ts +84 -0
- package/dist/mcp/server.js +168 -0
- package/dist/mcp/tools/marketplace.d.ts +94 -0
- package/dist/mcp/tools/marketplace.js +303 -0
- package/dist/mcp/tools/memory-client.d.ts +109 -0
- package/dist/mcp/tools/memory-client.js +200 -0
- package/dist/mcp/tools/memory-config.d.ts +29 -0
- package/dist/mcp/tools/memory-config.js +12 -0
- package/dist/mcp/tools/memory.d.ts +92 -0
- package/dist/mcp/tools/memory.js +312 -0
- package/dist/mcp/tools/model-priority.d.ts +16 -0
- package/dist/mcp/tools/model-priority.js +87 -0
- package/dist/mcp/tools/openllm.d.ts +75 -0
- package/dist/mcp/tools/openllm.js +715 -0
- package/dist/mcp/types.d.ts +25 -0
- package/dist/mcp/types.js +7 -0
- package/dist/openapi/call.d.ts +26 -0
- package/dist/openapi/call.js +56 -0
- package/dist/openapi/index.d.ts +2 -0
- package/dist/openapi/index.js +2 -0
- package/dist/openapi/openapi.json +80213 -0
- package/dist/openapi/operations.d.ts +14 -0
- package/dist/openapi/operations.js +2060 -0
- package/dist/protocol/access.d.ts +81 -0
- package/dist/protocol/access.js +125 -0
- package/dist/protocol/agent-prompt-text.d.ts +15 -0
- package/{protocol/agent-prompt-text.ts → dist/protocol/agent-prompt-text.js} +9 -14
- package/dist/protocol/agent.d.ts +1487 -0
- package/dist/protocol/agent.js +787 -0
- package/dist/protocol/api-key-syntax.d.ts +58 -0
- package/dist/protocol/api-key-syntax.js +73 -0
- package/dist/protocol/audio.d.ts +18 -0
- package/dist/protocol/audio.js +21 -0
- package/dist/protocol/auth.d.ts +126 -0
- package/dist/protocol/auth.js +119 -0
- package/dist/protocol/changelog.d.ts +69 -0
- package/dist/protocol/changelog.js +107 -0
- package/dist/protocol/chat.d.ts +789 -0
- package/dist/protocol/chat.js +394 -0
- package/dist/protocol/client-registry.d.ts +151 -0
- package/dist/protocol/client-registry.js +238 -0
- package/dist/protocol/config.d.ts +508 -0
- package/dist/protocol/config.js +289 -0
- package/dist/protocol/control-channel.d.ts +102 -0
- package/{protocol/control-channel.ts → dist/protocol/control-channel.js} +33 -61
- package/dist/protocol/cooldown-reason.d.ts +30 -0
- package/dist/protocol/cooldown-reason.js +44 -0
- package/dist/protocol/cost.d.ts +9 -0
- package/dist/protocol/cost.js +8 -0
- package/dist/protocol/daemon-port.d.ts +2 -0
- package/dist/protocol/daemon-port.js +28 -0
- package/dist/protocol/daemon.d.ts +2034 -0
- package/dist/protocol/daemon.js +1286 -0
- package/dist/protocol/default-chains.d.ts +15 -0
- package/dist/protocol/default-chains.js +21 -0
- package/dist/protocol/device-state.d.ts +223 -0
- package/dist/protocol/device-state.js +52 -0
- package/dist/protocol/doctor-report-local.d.ts +105 -0
- package/{protocol/doctor-report-local.ts → dist/protocol/doctor-report-local.js} +92 -196
- package/dist/protocol/doctor-report-policy.d.ts +62 -0
- package/{protocol/doctor-report-policy.ts → dist/protocol/doctor-report-policy.js} +23 -76
- package/dist/protocol/doctor-report.d.ts +406 -0
- package/dist/protocol/doctor-report.js +645 -0
- package/dist/protocol/errors.d.ts +17 -0
- package/dist/protocol/errors.js +10 -0
- package/dist/protocol/execution-selection.d.ts +92 -0
- package/{protocol/execution-selection.ts → dist/protocol/execution-selection.js} +38 -63
- package/dist/protocol/hop-grant.d.ts +28 -0
- package/{protocol/hop-grant.ts → dist/protocol/hop-grant.js} +7 -13
- package/dist/protocol/image-edit-parse.d.ts +25 -0
- package/dist/protocol/image-edit-parse.js +196 -0
- package/dist/protocol/images.d.ts +288 -0
- package/dist/protocol/images.js +229 -0
- package/dist/protocol/index.d.ts +69 -0
- package/dist/protocol/index.js +74 -0
- package/dist/protocol/inference-headers.d.ts +3 -0
- package/dist/protocol/keepalive.d.ts +36 -0
- package/dist/protocol/keepalive.js +51 -0
- package/dist/protocol/local-auth.d.ts +125 -0
- package/dist/protocol/local-auth.js +227 -0
- package/dist/protocol/local-device-control.d.ts +108 -0
- package/{protocol/local-device-control.ts → dist/protocol/local-device-control.js} +40 -70
- package/dist/protocol/local-provider.d.ts +105 -0
- package/dist/protocol/local-provider.js +213 -0
- package/dist/protocol/local-session.d.ts +70 -0
- package/dist/protocol/local-session.js +87 -0
- package/dist/protocol/marketplace.d.ts +190 -0
- package/dist/protocol/marketplace.js +140 -0
- package/{protocol/media-attempt.ts → dist/protocol/media-attempt.d.ts} +11 -50
- package/dist/protocol/media-attempt.js +50 -0
- package/dist/protocol/media-bytes.d.ts +8 -0
- package/dist/protocol/media-bytes.js +21 -0
- package/dist/protocol/media-default.d.ts +56 -0
- package/dist/protocol/media-default.js +302 -0
- package/{protocol/media-dimensions.ts → dist/protocol/media-dimensions.d.ts} +11 -78
- package/dist/protocol/media-dimensions.js +90 -0
- package/dist/protocol/media-input.d.ts +20 -0
- package/dist/protocol/media-input.js +29 -0
- package/dist/protocol/media-model.d.ts +77 -0
- package/dist/protocol/media-model.js +89 -0
- package/{protocol/media-option-issue.ts → dist/protocol/media-option-issue.d.ts} +25 -53
- package/dist/protocol/media-option-issue.js +50 -0
- package/dist/protocol/media-types.d.ts +14 -0
- package/dist/protocol/media-types.js +61 -0
- package/dist/protocol/model-match.d.ts +254 -0
- package/dist/protocol/model-match.js +760 -0
- package/dist/protocol/model-settings.d.ts +153 -0
- package/dist/protocol/model-settings.js +246 -0
- package/dist/protocol/models.d.ts +1065 -0
- package/dist/protocol/models.js +638 -0
- package/dist/protocol/mux.d.ts +350 -0
- package/dist/protocol/mux.js +595 -0
- package/{protocol/plan-surface.ts → dist/protocol/plan-surface.d.ts} +12 -26
- package/dist/protocol/plan-surface.js +39 -0
- package/dist/protocol/provider-models.d.ts +8 -0
- package/{protocol/provider-models.ts → dist/protocol/provider-models.js} +7 -9
- package/dist/protocol/provider-status.d.ts +59 -0
- package/dist/protocol/provider-status.js +142 -0
- package/dist/protocol/provider-tier.d.ts +34 -0
- package/{protocol/provider-tier.ts → dist/protocol/provider-tier.js} +9 -34
- package/dist/protocol/provider-usage.d.ts +230 -0
- package/dist/protocol/provider-usage.js +214 -0
- package/dist/protocol/providers.d.ts +2496 -0
- package/dist/protocol/providers.js +723 -0
- package/dist/protocol/public-usage-index.d.ts +449 -0
- package/dist/protocol/public-usage-index.js +279 -0
- package/dist/protocol/realtime-generated.d.ts +29 -0
- package/{protocol/realtime-generated.ts → dist/protocol/realtime-generated.js} +3 -5
- package/dist/protocol/realtime.d.ts +241 -0
- package/{protocol/realtime.ts → dist/protocol/realtime.js} +88 -155
- package/dist/protocol/registry-pointer.d.ts +24 -0
- package/dist/protocol/registry-pointer.js +78 -0
- package/dist/protocol/relay.d.ts +1152 -0
- package/{protocol/relay.ts → dist/protocol/relay.js} +237 -370
- package/dist/protocol/replay-session.d.ts +4 -0
- package/{protocol/replay-session.ts → dist/protocol/replay-session.js} +1 -8
- package/dist/protocol/responses.d.ts +303 -0
- package/dist/protocol/responses.js +240 -0
- package/dist/protocol/semver.d.ts +59 -0
- package/dist/protocol/semver.js +171 -0
- package/{protocol/setup-mode.ts → dist/protocol/setup-mode.d.ts} +4 -12
- package/dist/protocol/setup-mode.js +18 -0
- package/dist/protocol/sha256-hex.d.ts +2 -0
- package/dist/protocol/sha256-hex.js +93 -0
- package/dist/protocol/stats.d.ts +711 -0
- package/dist/protocol/stats.js +353 -0
- package/dist/protocol/streaming.d.ts +16 -0
- package/dist/protocol/streaming.js +15 -0
- package/{protocol/subscription-daemon-floors.ts → dist/protocol/subscription-daemon-floors.d.ts} +8 -14
- package/dist/protocol/subscription-daemon-floors.js +4 -0
- package/dist/protocol/subscription-provider-compat.d.ts +44 -0
- package/dist/protocol/subscription-provider-compat.js +67 -0
- package/dist/protocol/subscription-provider.d.ts +15 -0
- package/dist/protocol/subscription-provider.js +17 -0
- package/dist/protocol/team-grant.d.ts +14 -0
- package/{protocol/team-grant.ts → dist/protocol/team-grant.js} +0 -3
- package/dist/protocol/team-shareable.d.ts +6 -0
- package/{protocol/team-shareable.ts → dist/protocol/team-shareable.js} +29 -33
- package/dist/protocol/transcription-encoding.d.ts +25 -0
- package/dist/protocol/transcription-encoding.js +47 -0
- package/{protocol/url-locality-dns.ts → dist/protocol/url-locality-dns.d.ts} +1 -23
- package/dist/protocol/url-locality-dns.js +33 -0
- package/dist/protocol/url-locality.d.ts +45 -0
- package/dist/protocol/url-locality.js +288 -0
- package/dist/protocol/usage-snapshots.d.ts +8 -0
- package/{protocol/usage-snapshots.ts → dist/protocol/usage-snapshots.js} +1 -2
- package/dist/protocol/vendor-cli-install.d.ts +20 -0
- package/{protocol/vendor-cli-install.ts → dist/protocol/vendor-cli-install.js} +10 -15
- package/dist/protocol/video-job-plan.d.ts +18 -0
- package/dist/protocol/video-job-plan.js +44 -0
- package/dist/protocol/videos.d.ts +107 -0
- package/dist/protocol/videos.js +154 -0
- package/dist/tunnel/channel-envelope.d.ts +53 -0
- package/dist/tunnel/channel-envelope.js +100 -0
- package/dist/tunnel/codec.d.ts +71 -0
- package/dist/tunnel/codec.js +151 -0
- package/dist/tunnel/device-anchor.d.ts +62 -0
- package/dist/tunnel/device-anchor.js +168 -0
- package/dist/tunnel/device-control.d.ts +50 -0
- package/dist/tunnel/device-control.js +88 -0
- package/dist/tunnel/device-grant.d.ts +91 -0
- package/dist/tunnel/device-grant.js +105 -0
- package/dist/tunnel/fleet.d.ts +28 -0
- package/dist/tunnel/fleet.js +41 -0
- package/dist/tunnel/index.d.ts +22 -0
- package/dist/tunnel/index.js +22 -0
- package/dist/tunnel/mux.d.ts +64 -0
- package/dist/tunnel/mux.js +449 -0
- package/dist/tunnel/peer-link.d.ts +88 -0
- package/dist/tunnel/peer-link.js +271 -0
- package/dist/tunnel/rtc-auth.d.ts +159 -0
- package/dist/tunnel/rtc-auth.js +340 -0
- package/dist/tunnel/rtc-duplex.d.ts +52 -0
- package/dist/tunnel/rtc-duplex.js +148 -0
- package/dist/tunnel/rtc-ice.d.ts +55 -0
- package/dist/tunnel/rtc-ice.js +100 -0
- package/dist/tunnel/secure.d.ts +179 -0
- package/dist/tunnel/secure.js +936 -0
- package/dist/tunnel/stream-reset-error.d.ts +6 -0
- package/dist/tunnel/stream-reset-error.js +9 -0
- package/dist/tunnel/streams.d.ts +244 -0
- package/dist/tunnel/streams.js +1071 -0
- package/dist/tunnel/team-grant.d.ts +90 -0
- package/dist/tunnel/team-grant.js +226 -0
- package/dist/tunnel/wire-bytes.d.ts +32 -0
- package/dist/tunnel/wire-bytes.js +90 -0
- package/dist/vault/api-key.d.ts +71 -0
- package/dist/vault/api-key.js +130 -0
- package/{vault/argon2.ts → dist/vault/argon2.d.ts} +18 -53
- package/dist/vault/argon2.js +82 -0
- package/dist/vault/cli-auth-seal.d.ts +35 -0
- package/dist/vault/cli-auth-seal.js +78 -0
- package/dist/vault/cred-seal.d.ts +18 -0
- package/dist/vault/cred-seal.js +86 -0
- package/dist/vault/credential.d.ts +21 -0
- package/dist/vault/credential.js +26 -0
- package/dist/vault/crypto.d.ts +51 -0
- package/dist/vault/crypto.js +156 -0
- package/dist/vault/daemon-attest.d.ts +68 -0
- package/dist/vault/daemon-attest.js +147 -0
- package/dist/vault/derive.d.ts +34 -0
- package/dist/vault/derive.js +84 -0
- package/dist/vault/device-access.d.ts +17 -0
- package/dist/vault/device-access.js +77 -0
- package/dist/vault/device-anchor.d.ts +42 -0
- package/dist/vault/device-anchor.js +90 -0
- package/dist/vault/device-control.d.ts +80 -0
- package/{vault/device-control.ts → dist/vault/device-control.js} +46 -104
- package/dist/vault/handoff.d.ts +102 -0
- package/dist/vault/handoff.js +236 -0
- package/dist/vault/index.d.ts +21 -0
- package/dist/vault/index.js +21 -0
- package/dist/vault/passkey.d.ts +82 -0
- package/{vault/passkey.ts → dist/vault/passkey.js} +63 -153
- package/dist/vault/phrase.d.ts +40 -0
- package/{vault/phrase.ts → dist/vault/phrase.js} +17 -23
- package/dist/vault/pin.d.ts +46 -0
- package/dist/vault/pin.js +92 -0
- package/dist/vault/public-key-seal.d.ts +17 -0
- package/dist/vault/public-key-seal.js +59 -0
- package/dist/vault/seed.d.ts +96 -0
- package/dist/vault/seed.js +179 -0
- package/dist/vault/team.d.ts +134 -0
- package/dist/vault/team.js +217 -0
- package/dist/vault/vault-item.d.ts +45 -0
- package/dist/vault/vault-item.js +62 -0
- package/dist/vault/vault-migrate-digest.d.ts +10 -0
- package/dist/vault/vault-migrate-digest.js +22 -0
- package/dist/vault/wrapped.d.ts +19 -0
- package/dist/vault/wrapped.js +52 -0
- package/dist/wire/adapters/messages/anthropic-map.d.ts +8 -0
- package/dist/wire/adapters/messages/anthropic-map.js +32 -0
- package/dist/wire/adapters/messages/index.d.ts +7 -0
- package/dist/wire/adapters/messages/index.js +7 -0
- package/dist/wire/adapters/messages/reasoning-from-items.d.ts +14 -0
- package/dist/wire/adapters/messages/reasoning-from-items.js +39 -0
- package/dist/wire/adapters/messages/reasoning-signature.d.ts +81 -0
- package/dist/wire/adapters/messages/reasoning-signature.js +180 -0
- package/dist/wire/adapters/messages/request.d.ts +28 -0
- package/dist/wire/adapters/messages/request.js +490 -0
- package/dist/wire/adapters/messages/response.d.ts +10 -0
- package/dist/wire/adapters/messages/response.js +177 -0
- package/dist/wire/adapters/messages/streaming.d.ts +134 -0
- package/dist/wire/adapters/messages/streaming.js +791 -0
- package/dist/wire/adapters/messages/usage.d.ts +20 -0
- package/dist/wire/adapters/messages/usage.js +10 -0
- package/dist/wire/adapters/responses/index.d.ts +3 -0
- package/dist/wire/adapters/responses/index.js +3 -0
- package/dist/wire/adapters/responses/request.d.ts +2 -0
- package/dist/wire/adapters/responses/request.js +287 -0
- package/dist/wire/adapters/responses/response.d.ts +11 -0
- package/dist/wire/adapters/responses/response.js +69 -0
- package/dist/wire/adapters/responses/streaming.d.ts +2 -0
- package/dist/wire/adapters/responses/streaming.js +267 -0
- package/dist/wire/features/account-rank.d.ts +61 -0
- package/dist/wire/features/account-rank.js +109 -0
- package/dist/wire/features/compaction/compact-request.d.ts +69 -0
- package/dist/wire/features/compaction/compact-request.js +667 -0
- package/dist/wire/features/compaction/compaction-text.d.ts +17 -0
- package/dist/wire/features/compaction/compaction-text.js +29 -0
- package/dist/wire/features/context-demote.d.ts +15 -0
- package/dist/wire/features/context-demote.js +45 -0
- package/dist/wire/features/context-overflow-strategy.d.ts +12 -0
- package/dist/wire/features/context-overflow-strategy.js +8 -0
- package/dist/wire/features/context-skip.d.ts +120 -0
- package/{wire/features/context-skip.ts → dist/wire/features/context-skip.js} +23 -57
- package/dist/wire/features/conversation-affinity.d.ts +49 -0
- package/dist/wire/features/conversation-affinity.js +121 -0
- package/dist/wire/features/max-tokens-backfill.d.ts +50 -0
- package/dist/wire/features/max-tokens-backfill.js +118 -0
- package/dist/wire/features/model-caps.d.ts +7 -0
- package/dist/wire/features/model-caps.js +40 -0
- package/dist/wire/features/model-settings.d.ts +3 -0
- package/dist/wire/features/model-settings.js +13 -0
- package/dist/wire/features/output-limit-clamp.d.ts +14 -0
- package/dist/wire/features/output-limit-clamp.js +36 -0
- package/dist/wire/features/quota-gate.d.ts +16 -0
- package/dist/wire/features/quota-gate.js +48 -0
- package/dist/wire/index.d.ts +44 -0
- package/dist/wire/index.js +44 -0
- package/dist/wire/lib/attempt-outcome.d.ts +19 -0
- package/dist/wire/lib/attempt-outcome.js +18 -0
- package/{wire/lib/canonical/content-part.ts → dist/wire/lib/canonical/content-part.d.ts} +12 -18
- package/dist/wire/lib/canonical/content-part.js +11 -0
- package/dist/wire/lib/canonical/count-tokens-capability.d.ts +52 -0
- package/dist/wire/lib/canonical/count-tokens-capability.js +109 -0
- package/{wire/lib/canonical/encoding-select.ts → dist/wire/lib/canonical/encoding-select.d.ts} +7 -24
- package/dist/wire/lib/canonical/encoding-select.js +26 -0
- package/dist/wire/lib/canonical/image-signature.d.ts +20 -0
- package/dist/wire/lib/canonical/image-signature.js +184 -0
- package/dist/wire/lib/canonical/image-sse.d.ts +88 -0
- package/dist/wire/lib/canonical/image-sse.js +173 -0
- package/{wire/lib/canonical/message.ts → dist/wire/lib/canonical/message.d.ts} +2 -20
- package/dist/wire/lib/canonical/message.js +31 -0
- package/dist/wire/lib/canonical/token-estimate.d.ts +98 -0
- package/dist/wire/lib/canonical/token-estimate.js +230 -0
- package/dist/wire/lib/encrypted-content.d.ts +22 -0
- package/dist/wire/lib/encrypted-content.js +55 -0
- package/dist/wire/lib/error-class.d.ts +57 -0
- package/dist/wire/lib/error-class.js +97 -0
- package/dist/wire/lib/error-envelope.d.ts +20 -0
- package/dist/wire/lib/error-envelope.js +53 -0
- package/dist/wire/lib/forwarded-headers.d.ts +16 -0
- package/{wire/lib/forwarded-headers.ts → dist/wire/lib/forwarded-headers.js} +62 -72
- package/dist/wire/lib/provider-error-rules.d.ts +21 -0
- package/dist/wire/lib/provider-error-rules.js +20 -0
- package/{wire/lib/refusal.ts → dist/wire/lib/refusal.d.ts} +3 -13
- package/dist/wire/lib/refusal.js +27 -0
- package/dist/wire/lib/responses-apply-patch.d.ts +57 -0
- package/dist/wire/lib/responses-apply-patch.js +81 -0
- package/dist/wire/lib/responses-namespace.d.ts +27 -0
- package/{wire/lib/responses-namespace.ts → dist/wire/lib/responses-namespace.js} +16 -32
- package/dist/wire/lib/secret-scrub.d.ts +23 -0
- package/dist/wire/lib/secret-scrub.js +72 -0
- package/dist/wire/lib/streaming/accumulate.d.ts +12 -0
- package/dist/wire/lib/streaming/accumulate.js +187 -0
- package/dist/wire/lib/streaming/finish-reason.d.ts +20 -0
- package/dist/wire/lib/streaming/finish-reason.js +15 -0
- package/{wire/lib/streaming/peek.ts → dist/wire/lib/streaming/peek.d.ts} +13 -82
- package/dist/wire/lib/streaming/peek.js +100 -0
- package/dist/wire/lib/streaming/provider-decode.d.ts +57 -0
- package/dist/wire/lib/streaming/provider-decode.js +210 -0
- package/dist/wire/lib/streaming/response-stream.d.ts +16 -0
- package/dist/wire/lib/streaming/response-stream.js +64 -0
- package/dist/wire/lib/streaming/sse.d.ts +130 -0
- package/dist/wire/lib/streaming/sse.js +600 -0
- package/dist/wire/lib/streaming/strip-tool-calls.d.ts +14 -0
- package/dist/wire/lib/streaming/strip-tool-calls.js +75 -0
- package/dist/wire/lib/streaming/upstream-error.d.ts +32 -0
- package/dist/wire/lib/streaming/upstream-error.js +45 -0
- package/dist/wire/lib/strip-subagent-isolation.d.ts +16 -0
- package/dist/wire/lib/strip-subagent-isolation.js +240 -0
- package/dist/wire/lib/thinking-signature.d.ts +56 -0
- package/{wire/lib/thinking-signature.ts → dist/wire/lib/thinking-signature.js} +77 -84
- package/dist/wire/lib/token-ruler/bpe-core.d.ts +105 -0
- package/dist/wire/lib/token-ruler/bpe-core.js +398 -0
- package/dist/wire/lib/token-ruler/index.d.ts +2 -0
- package/dist/wire/lib/token-ruler/index.js +2 -0
- package/dist/wire/lib/token-ruler/ruler.d.ts +36 -0
- package/dist/wire/lib/token-ruler/ruler.js +50 -0
- package/dist/wire/lib/token-ruler/vocab/claude-ranks.d.ts +14 -0
- package/dist/wire/lib/token-ruler/vocab/claude-ranks.js +16 -0
- package/{wire/lib/token-ruler/vocab/claude.ts → dist/wire/lib/token-ruler/vocab/claude.d.ts} +2 -12
- package/dist/wire/lib/token-ruler/vocab/claude.js +8 -0
- package/dist/wire/lib/token-ruler/vocab/o200k-ranks.d.ts +3 -0
- package/dist/wire/lib/token-ruler/vocab/o200k-ranks.js +18 -0
- package/dist/wire/lib/token-ruler/vocab/o200k.d.ts +11 -0
- package/dist/wire/lib/token-ruler/vocab/o200k.js +6 -0
- package/dist/wire/lib/tool-name-map.d.ts +44 -0
- package/dist/wire/lib/tool-name-map.js +186 -0
- package/dist/wire/lib/tool-schema.d.ts +15 -0
- package/dist/wire/lib/tool-schema.js +307 -0
- package/dist/wire/lib/usage-observer.d.ts +9 -0
- package/dist/wire/lib/usage-observer.js +108 -0
- package/dist/wire/providers/anthropic/adaptive-thinking.d.ts +125 -0
- package/dist/wire/providers/anthropic/adaptive-thinking.js +221 -0
- package/dist/wire/providers/anthropic/beta-headers.d.ts +44 -0
- package/{wire/providers/anthropic/beta-headers.ts → dist/wire/providers/anthropic/beta-headers.js} +57 -79
- package/dist/wire/providers/anthropic/index.d.ts +6 -0
- package/{wire/providers/anthropic/index.ts → dist/wire/providers/anthropic/index.js} +6 -6
- package/dist/wire/providers/anthropic/request.d.ts +12 -0
- package/dist/wire/providers/anthropic/request.js +456 -0
- package/dist/wire/providers/anthropic/response.d.ts +3 -0
- package/dist/wire/providers/anthropic/response.js +126 -0
- package/dist/wire/providers/anthropic/streaming.d.ts +28 -0
- package/dist/wire/providers/anthropic/streaming.js +268 -0
- package/dist/wire/providers/anthropic/web-search-domains.d.ts +7 -0
- package/dist/wire/providers/anthropic/web-search-domains.js +36 -0
- package/dist/wire/providers/chatgpt/common.d.ts +27 -0
- package/{wire/providers/chatgpt/common.ts → dist/wire/providers/chatgpt/common.js} +69 -89
- package/dist/wire/providers/chatgpt/index.d.ts +4 -0
- package/{wire/providers/chatgpt/index.ts → dist/wire/providers/chatgpt/index.js} +4 -4
- package/dist/wire/providers/chatgpt/request.d.ts +132 -0
- package/dist/wire/providers/chatgpt/request.js +696 -0
- package/dist/wire/providers/chatgpt/streaming.d.ts +69 -0
- package/dist/wire/providers/chatgpt/streaming.js +761 -0
- package/dist/wire/providers/chatgpt/web-search.d.ts +19 -0
- package/dist/wire/providers/chatgpt/web-search.js +55 -0
- package/dist/wire/providers/grok/web-search.d.ts +23 -0
- package/dist/wire/providers/grok/web-search.js +74 -0
- package/dist/wire/providers/kimi/web-search.d.ts +48 -0
- package/dist/wire/providers/kimi/web-search.js +106 -0
- package/dist/wire/providers/upstream-deny.d.ts +61 -0
- package/dist/wire/providers/upstream-deny.js +293 -0
- package/dist/wire/providers/upstream-request.d.ts +104 -0
- package/dist/wire/providers/upstream-request.js +411 -0
- package/package.json +105 -23
- package/ARCHITECTURE.md +0 -43
- package/client/chat.ts +0 -140
- package/client/devices.ts +0 -269
- package/client/http.ts +0 -72
- package/client/index.ts +0 -129
- package/client/relay-mux.ts +0 -547
- package/client/types.ts +0 -51
- package/client/vault.ts +0 -239
- package/connect/chat.ts +0 -58
- package/connect/client.ts +0 -240
- package/connect/config.ts +0 -47
- package/connect/errors.ts +0 -121
- package/connect/oauth.ts +0 -265
- package/connect/routes.ts +0 -234
- package/connect/store.ts +0 -107
- package/mcp/handle.ts +0 -24
- package/mcp/index.ts +0 -76
- package/mcp/server.ts +0 -319
- package/mcp/tools/marketplace.ts +0 -439
- package/mcp/tools/memory-client.ts +0 -297
- package/mcp/tools/memory-config.ts +0 -32
- package/mcp/tools/memory.ts +0 -369
- package/mcp/tools/model-priority.ts +0 -88
- package/mcp/tools/openllm.ts +0 -883
- package/mcp/types.ts +0 -28
- package/openapi/call.ts +0 -90
- package/openapi/index.ts +0 -8
- package/openapi/openapi.json +0 -80213
- package/openapi/operations.ts +0 -2075
- package/protocol/CONTRIBUTING.md +0 -21
- package/protocol/README.md +0 -134
- package/protocol/access.ts +0 -186
- package/protocol/agent.ts +0 -991
- package/protocol/api-key-syntax.ts +0 -105
- package/protocol/assets/openllm-light.svg +0 -18
- package/protocol/assets/openllm.svg +0 -18
- package/protocol/audio.ts +0 -29
- package/protocol/auth.ts +0 -142
- package/protocol/changelog.ts +0 -155
- package/protocol/chat.ts +0 -458
- package/protocol/client-registry.ts +0 -356
- package/protocol/config.ts +0 -345
- package/protocol/cooldown-reason.ts +0 -78
- package/protocol/cost.ts +0 -10
- package/protocol/daemon-port.ts +0 -31
- package/protocol/daemon.ts +0 -1524
- package/protocol/default-chains.ts +0 -66
- package/protocol/device-state.ts +0 -85
- package/protocol/doctor-report.ts +0 -947
- package/protocol/errors.ts +0 -14
- package/protocol/image-edit-parse.ts +0 -263
- package/protocol/images.ts +0 -298
- package/protocol/index.ts +0 -74
- package/protocol/keepalive.ts +0 -69
- package/protocol/local-auth.ts +0 -300
- package/protocol/local-provider.ts +0 -288
- package/protocol/local-session.ts +0 -118
- package/protocol/marketplace.ts +0 -187
- package/protocol/media-bytes.ts +0 -28
- package/protocol/media-default.ts +0 -357
- package/protocol/media-input.ts +0 -61
- package/protocol/media-model.ts +0 -142
- package/protocol/media-types.ts +0 -70
- package/protocol/model-match.ts +0 -1051
- package/protocol/model-settings.ts +0 -327
- package/protocol/models.ts +0 -767
- package/protocol/mux.ts +0 -722
- package/protocol/provider-status.ts +0 -206
- package/protocol/provider-usage.ts +0 -263
- package/protocol/providers.ts +0 -901
- package/protocol/public-usage-index.ts +0 -354
- package/protocol/registry-pointer.ts +0 -118
- package/protocol/responses.ts +0 -280
- package/protocol/semver.ts +0 -182
- package/protocol/sha256-hex.ts +0 -98
- package/protocol/stats.ts +0 -406
- package/protocol/streaming.ts +0 -19
- package/protocol/subscription-provider-compat.ts +0 -104
- package/protocol/subscription-provider.ts +0 -32
- package/protocol/transcription-encoding.ts +0 -76
- package/protocol/url-locality.ts +0 -288
- package/protocol/video-job-plan.ts +0 -50
- package/protocol/videos.ts +0 -227
- package/tsconfig.json +0 -14
- package/tunnel/ARCHITECTURE.md +0 -111
- package/tunnel/assets/openllm-light.svg +0 -18
- package/tunnel/assets/openllm.svg +0 -18
- package/tunnel/channel-envelope.ts +0 -139
- package/tunnel/codec.ts +0 -191
- package/tunnel/device-anchor.ts +0 -229
- package/tunnel/device-control.ts +0 -118
- package/tunnel/device-grant.ts +0 -173
- package/tunnel/fleet.ts +0 -77
- package/tunnel/index.ts +0 -22
- package/tunnel/mux.ts +0 -580
- package/tunnel/peer-link.ts +0 -390
- package/tunnel/rtc-auth.ts +0 -415
- package/tunnel/rtc-duplex.ts +0 -187
- package/tunnel/rtc-ice.ts +0 -136
- package/tunnel/secure.ts +0 -1298
- package/tunnel/stream-reset-error.ts +0 -14
- package/tunnel/streams.ts +0 -1430
- package/tunnel/team-grant.ts +0 -302
- package/tunnel/wire-bytes.ts +0 -102
- package/vault/ARCHITECTURE.md +0 -470
- package/vault/api-key.ts +0 -190
- package/vault/cli-auth-seal.ts +0 -157
- package/vault/cred-seal.ts +0 -146
- package/vault/credential.ts +0 -55
- package/vault/crypto.ts +0 -210
- package/vault/daemon-attest.ts +0 -229
- package/vault/derive.ts +0 -113
- package/vault/device-access.ts +0 -87
- package/vault/device-anchor.ts +0 -185
- package/vault/handoff.ts +0 -396
- package/vault/index.ts +0 -21
- package/vault/pin.ts +0 -153
- package/vault/public-key-seal.ts +0 -131
- package/vault/seed.ts +0 -272
- package/vault/team.ts +0 -355
- package/vault/vault-item.ts +0 -131
- package/vault/vault-migrate-digest.ts +0 -29
- package/vault/wrapped.ts +0 -77
- package/wire/ARCHITECTURE.md +0 -90
- package/wire/CONTRIBUTING.md +0 -19
- package/wire/adapters/messages/anthropic-map.ts +0 -37
- package/wire/adapters/messages/index.ts +0 -7
- package/wire/adapters/messages/reasoning-from-items.ts +0 -38
- package/wire/adapters/messages/reasoning-signature.ts +0 -243
- package/wire/adapters/messages/request.ts +0 -574
- package/wire/adapters/messages/response.ts +0 -215
- package/wire/adapters/messages/streaming.ts +0 -1027
- package/wire/adapters/messages/usage.ts +0 -43
- package/wire/adapters/responses/index.ts +0 -3
- package/wire/adapters/responses/request.ts +0 -395
- package/wire/adapters/responses/response.ts +0 -88
- package/wire/adapters/responses/streaming.ts +0 -344
- package/wire/assets/openllm-light.svg +0 -18
- package/wire/assets/openllm.svg +0 -18
- package/wire/features/account-rank.ts +0 -166
- package/wire/features/compaction/compact-request.ts +0 -862
- package/wire/features/compaction/compaction-text.ts +0 -43
- package/wire/features/context-demote.ts +0 -47
- package/wire/features/context-overflow-strategy.ts +0 -26
- package/wire/features/conversation-affinity.ts +0 -144
- package/wire/features/max-tokens-backfill.ts +0 -141
- package/wire/features/model-caps.ts +0 -53
- package/wire/features/model-settings.ts +0 -24
- package/wire/features/output-limit-clamp.ts +0 -64
- package/wire/features/quota-gate.ts +0 -85
- package/wire/index.ts +0 -44
- package/wire/lib/attempt-outcome.ts +0 -43
- package/wire/lib/canonical/count-tokens-capability.ts +0 -138
- package/wire/lib/canonical/image-signature.ts +0 -202
- package/wire/lib/canonical/image-sse.ts +0 -216
- package/wire/lib/canonical/token-estimate.ts +0 -266
- package/wire/lib/encrypted-content.ts +0 -66
- package/wire/lib/error-class.ts +0 -154
- package/wire/lib/error-envelope.ts +0 -56
- package/wire/lib/provider-error-rules.ts +0 -48
- package/wire/lib/responses-apply-patch.ts +0 -104
- package/wire/lib/secret-scrub.ts +0 -101
- package/wire/lib/streaming/accumulate.ts +0 -221
- package/wire/lib/streaming/finish-reason.ts +0 -33
- package/wire/lib/streaming/provider-decode.ts +0 -259
- package/wire/lib/streaming/response-stream.ts +0 -71
- package/wire/lib/streaming/sse.ts +0 -699
- package/wire/lib/streaming/strip-tool-calls.ts +0 -95
- package/wire/lib/streaming/upstream-error.ts +0 -58
- package/wire/lib/strip-subagent-isolation.ts +0 -284
- package/wire/lib/token-ruler/bpe-core.ts +0 -426
- package/wire/lib/token-ruler/index.ts +0 -2
- package/wire/lib/token-ruler/ruler.ts +0 -79
- package/wire/lib/token-ruler/vocab/claude-ranks.ts +0 -16
- package/wire/lib/token-ruler/vocab/o200k-ranks.ts +0 -18
- package/wire/lib/token-ruler/vocab/o200k.ts +0 -19
- package/wire/lib/tool-name-map.ts +0 -271
- package/wire/lib/tool-schema.ts +0 -353
- package/wire/lib/usage-observer.ts +0 -125
- package/wire/providers/anthropic/adaptive-thinking.ts +0 -291
- package/wire/providers/anthropic/request.ts +0 -607
- package/wire/providers/anthropic/response.ts +0 -155
- package/wire/providers/anthropic/streaming.ts +0 -315
- package/wire/providers/anthropic/web-search-domains.ts +0 -49
- package/wire/providers/chatgpt/request.ts +0 -959
- package/wire/providers/chatgpt/streaming.ts +0 -1007
- package/wire/providers/chatgpt/web-search.ts +0 -63
- package/wire/providers/grok/web-search.ts +0 -87
- package/wire/providers/kimi/web-search.ts +0 -145
- package/wire/providers/upstream-deny.ts +0 -355
- package/wire/providers/upstream-request.ts +0 -619
- /package/{protocol/inference-headers.ts → dist/protocol/inference-headers.js} +0 -0
|
@@ -1,862 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LAST-RESORT request compaction — shrink an oversized request body just enough
|
|
3
|
-
* to fit a target context budget, so a request that is larger than EVERY hop's
|
|
4
|
-
* window is served (degraded) instead of hard-failing with a 400/502.
|
|
5
|
-
*
|
|
6
|
-
* This deliberately reverses, IN THE FAILURE CORNER ONLY, the decision recorded
|
|
7
|
-
* in `../context-skip.ts`: gateway-side request rewriting was removed because
|
|
8
|
-
* PROACTIVE, estimator-driven rewrites destabilise the prompt-cache prefix and
|
|
9
|
-
* degrade requests the vendor would have accepted. This compactor is NOT
|
|
10
|
-
* proactive — callers invoke it exclusively after the whole fallback chain is
|
|
11
|
-
* exhausted, where the alternative is a guaranteed failure and a prompt-cache
|
|
12
|
-
* miss is strictly the better outcome. It is also cache-aware: it preserves the
|
|
13
|
-
* leading `cache_control`-bearing prefix, compacting from the middle-old region
|
|
14
|
-
* forward so the cached prefix and the recent tail survive where the budget
|
|
15
|
-
* allows.
|
|
16
|
-
*
|
|
17
|
-
* Pure and immutable — never mutates the input; returns a fresh body. Uses the
|
|
18
|
-
* ruler-backed {@link estimateBodyTokensExact} for the fit check, so on a warm
|
|
19
|
-
* isolate the "does it fit now?" loop is a real tokenizer, not `chars/4`. That
|
|
20
|
-
* is the OPT-IN estimator: the per-request routing gate deliberately stays on
|
|
21
|
-
* the cheap heuristic, and compaction asks for exactness because here it
|
|
22
|
-
* changes what gets cut.
|
|
23
|
-
*/
|
|
24
|
-
import type { TTokenEncoding } from "../../lib/canonical/encoding-select";
|
|
25
|
-
import { estimateBodyTokensExact } from "../../lib/canonical/token-estimate";
|
|
26
|
-
import { COMPACTION_MIN_VISIBLE_TEXT_CHARS } from "./compaction-text";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The elision marker dropped into the middle of a truncated tool output. Kept
|
|
30
|
-
* short and unambiguous so a model reading the transcript understands the gap.
|
|
31
|
-
*/
|
|
32
|
-
const ELISION = "\n\n…[openllm: output truncated to fit context]…\n\n";
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Keep the head and tail of `text`, replacing the middle with {@link ELISION},
|
|
36
|
-
* so the result is approximately `maxChars` long. Head-heavy (2/3 head, 1/3
|
|
37
|
-
* tail): the start of a tool output — the command, the first rows — usually
|
|
38
|
-
* carries more signal than the end. Never returns something below
|
|
39
|
-
* `COMPACTION_MIN_VISIBLE_TEXT_CHARS`; a `maxChars` at or above the input length
|
|
40
|
-
* returns the input untouched.
|
|
41
|
-
*
|
|
42
|
-
* Works in characters, not tokens: the caller drives the token-fit loop and this
|
|
43
|
-
* is the cheap per-string knob. A char budget is a safe proxy — fewer chars is
|
|
44
|
-
* always fewer-or-equal tokens for any BPE.
|
|
45
|
-
*/
|
|
46
|
-
export const truncateMiddleToChars = (
|
|
47
|
-
text: string,
|
|
48
|
-
maxChars: number,
|
|
49
|
-
): string => {
|
|
50
|
-
const cap = Math.max(COMPACTION_MIN_VISIBLE_TEXT_CHARS, maxChars);
|
|
51
|
-
if (text.length <= cap) return text;
|
|
52
|
-
const budget = cap - ELISION.length;
|
|
53
|
-
if (budget <= COMPACTION_MIN_VISIBLE_TEXT_CHARS) {
|
|
54
|
-
// No room for both ends around the marker — keep a short head only, up to
|
|
55
|
-
// the (floor-widened) cap. `cap` is already >= COMPACTION_MIN_VISIBLE_TEXT_CHARS,
|
|
56
|
-
// so this never drops below the documented visible-text floor.
|
|
57
|
-
return text.slice(0, cap);
|
|
58
|
-
}
|
|
59
|
-
const headLen = Math.ceil((budget * 2) / 3);
|
|
60
|
-
const tailLen = budget - headLen;
|
|
61
|
-
const head = text.slice(0, headLen);
|
|
62
|
-
const tail = tailLen > 0 ? text.slice(text.length - tailLen) : "";
|
|
63
|
-
return `${head}${ELISION}${tail}`;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
type TRecord = Record<string, unknown>;
|
|
67
|
-
|
|
68
|
-
const isRecord = (v: unknown): v is TRecord =>
|
|
69
|
-
v !== null && typeof v === "object" && !Array.isArray(v);
|
|
70
|
-
|
|
71
|
-
/** True when a message / content block carries a `cache_control` breakpoint. */
|
|
72
|
-
const hasCacheControl = (v: unknown): boolean => {
|
|
73
|
-
if (!isRecord(v)) return false;
|
|
74
|
-
if (v.cache_control != null) return true;
|
|
75
|
-
const content = v.content;
|
|
76
|
-
if (Array.isArray(content)) return content.some(hasCacheControl);
|
|
77
|
-
return false;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The char length of a text value that is either a bare string or a block-array
|
|
82
|
-
* of `{ type, text }` parts (Anthropic tool_result / canonical tool / Responses
|
|
83
|
-
* output contents all take one of these two shapes).
|
|
84
|
-
*/
|
|
85
|
-
const textLen = (content: unknown): number => {
|
|
86
|
-
if (typeof content === "string") return content.length;
|
|
87
|
-
if (Array.isArray(content)) {
|
|
88
|
-
let n = 0;
|
|
89
|
-
for (const part of content) {
|
|
90
|
-
if (isRecord(part) && typeof part.text === "string")
|
|
91
|
-
n += part.text.length;
|
|
92
|
-
}
|
|
93
|
-
return n;
|
|
94
|
-
}
|
|
95
|
-
return 0;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Total chars of truncatable tool output across the body, per surface: Anthropic
|
|
100
|
-
* `tool_result` contents, canonical `tool` message contents, Responses
|
|
101
|
-
* `function_call_output` outputs. Used to solve the first-pass truncation cap
|
|
102
|
-
* from the actual deficit rather than a fixed ladder.
|
|
103
|
-
*/
|
|
104
|
-
const toolOutputChars = (
|
|
105
|
-
items: ReadonlyArray<unknown>,
|
|
106
|
-
surface: TCompactionSurface,
|
|
107
|
-
): number => {
|
|
108
|
-
let n = 0;
|
|
109
|
-
for (const item of items) {
|
|
110
|
-
if (!isRecord(item)) continue;
|
|
111
|
-
if (surface === "messages") {
|
|
112
|
-
if (Array.isArray(item.content)) {
|
|
113
|
-
for (const block of item.content) {
|
|
114
|
-
if (isRecord(block) && block.type === "tool_result") {
|
|
115
|
-
n += textLen(block.content);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
} else if (surface === "responses") {
|
|
120
|
-
if (item.type === "function_call_output") n += textLen(item.output);
|
|
121
|
-
} else if (item.role === "tool") {
|
|
122
|
-
n += textLen(item.content);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return n;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* The largest tool-output char cap that trims ~only what the deficit needs.
|
|
130
|
-
* Truncating every tool output to `cap` removes roughly `total − outputs×cap`
|
|
131
|
-
* chars; we want to remove `deficitChars ≈ (estimatedTokens − targetTokens) ×
|
|
132
|
-
* CHARS_PER_TOKEN`. Capping the result at 0.5× the average tool output keeps a
|
|
133
|
-
* meaningful portion of each output rather than over-shrinking on a small
|
|
134
|
-
* deficit (the live-test report: a ~9% deficit was destroying ~95% of content).
|
|
135
|
-
* Returns `null` when there is no tool output to truncate.
|
|
136
|
-
*/
|
|
137
|
-
const CHARS_PER_TOKEN = 4;
|
|
138
|
-
const firstPassCap = (
|
|
139
|
-
items: ReadonlyArray<unknown>,
|
|
140
|
-
surface: TCompactionSurface,
|
|
141
|
-
deficitTokens: number,
|
|
142
|
-
): number | null => {
|
|
143
|
-
const total = toolOutputChars(items, surface);
|
|
144
|
-
if (total === 0) return null;
|
|
145
|
-
const outputs = items.reduce<number>((count, item) => {
|
|
146
|
-
if (!isRecord(item)) return count;
|
|
147
|
-
if (surface === "messages")
|
|
148
|
-
return (
|
|
149
|
-
count +
|
|
150
|
-
(Array.isArray(item.content)
|
|
151
|
-
? (item.content as unknown[]).filter(
|
|
152
|
-
(b) => isRecord(b) && b.type === "tool_result",
|
|
153
|
-
).length
|
|
154
|
-
: 0)
|
|
155
|
-
);
|
|
156
|
-
if (surface === "responses")
|
|
157
|
-
return item.type === "function_call_output" ? count + 1 : count;
|
|
158
|
-
return item.role === "tool" ? count + 1 : count;
|
|
159
|
-
}, 0);
|
|
160
|
-
if (outputs === 0) return null;
|
|
161
|
-
const deficitChars = deficitTokens * CHARS_PER_TOKEN;
|
|
162
|
-
const keepChars = Math.max(0, total - deficitChars);
|
|
163
|
-
const cap = Math.ceil(keepChars / outputs);
|
|
164
|
-
const floorCap = Math.ceil((total / outputs) * 0.5);
|
|
165
|
-
return Math.max(cap, floorCap, COMPACTION_MIN_VISIBLE_TEXT_CHARS);
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* The result of a compaction attempt. `compacted` is true when the body fits the
|
|
170
|
-
* target budget — either it already did (returned untouched) or it was shrunk to
|
|
171
|
-
* fit. It is false ONLY when compaction could not get it under the target (the
|
|
172
|
-
* caller then surfaces the original terminal error). `estimatedTokens` is the
|
|
173
|
-
* post-attempt estimate.
|
|
174
|
-
*/
|
|
175
|
-
export type TCompactionResult = {
|
|
176
|
-
readonly body: unknown;
|
|
177
|
-
readonly compacted: boolean;
|
|
178
|
-
readonly estimatedTokens: number;
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* The surfaces a body can arrive on. `messages` is Anthropic-shaped;
|
|
183
|
-
* `chat_completions` / `responses` are canonical OpenAI-shaped (the cloud path
|
|
184
|
-
* canonicalises `responses` before dispatch).
|
|
185
|
-
*/
|
|
186
|
-
export type TCompactionSurface = "messages" | "chat_completions" | "responses";
|
|
187
|
-
|
|
188
|
-
const isAnthropic = (surface: TCompactionSurface): boolean =>
|
|
189
|
-
surface === "messages";
|
|
190
|
-
|
|
191
|
-
// ─── Anthropic-shaped truncation ──────────────────────────────────────────
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Rewrite the text inside a single Anthropic `tool_result` block, capping its
|
|
195
|
-
* serialized size. `content` is `string | block[]`; string form truncates
|
|
196
|
-
* directly, block-array form shares a single budget across `text` parts in order,
|
|
197
|
-
* leaving non-text parts untouched.
|
|
198
|
-
*/
|
|
199
|
-
const truncateAnthropicToolResult = (
|
|
200
|
-
block: TRecord,
|
|
201
|
-
maxChars: number,
|
|
202
|
-
): TRecord => {
|
|
203
|
-
const content = block.content;
|
|
204
|
-
if (typeof content === "string") {
|
|
205
|
-
return { ...block, content: truncateMiddleToChars(content, maxChars) };
|
|
206
|
-
}
|
|
207
|
-
if (Array.isArray(content)) {
|
|
208
|
-
// Share one maxChars budget across text parts, oldest→newest. When the
|
|
209
|
-
// remainder drops below COMPACTION_MIN_VISIBLE_TEXT_CHARS, hard-slice
|
|
210
|
-
// instead of calling truncateMiddleToChars (which floors up to the min
|
|
211
|
-
// and would overshoot the shared budget). Exhausted text parts are
|
|
212
|
-
// dropped (not left as empty `{type:"text", text:""}` blocks) so the
|
|
213
|
-
// wire body stays vendor-clean; non-text siblings always survive.
|
|
214
|
-
let remainingChars = maxChars;
|
|
215
|
-
const next: unknown[] = [];
|
|
216
|
-
for (const inner of content) {
|
|
217
|
-
if (
|
|
218
|
-
!isRecord(inner) ||
|
|
219
|
-
inner.type !== "text" ||
|
|
220
|
-
typeof inner.text !== "string"
|
|
221
|
-
) {
|
|
222
|
-
next.push(inner);
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
if (remainingChars <= 0) continue;
|
|
226
|
-
const text =
|
|
227
|
-
inner.text.length <= remainingChars
|
|
228
|
-
? inner.text
|
|
229
|
-
: remainingChars < COMPACTION_MIN_VISIBLE_TEXT_CHARS
|
|
230
|
-
? inner.text.slice(0, remainingChars)
|
|
231
|
-
: truncateMiddleToChars(inner.text, remainingChars);
|
|
232
|
-
remainingChars = Math.max(0, remainingChars - text.length);
|
|
233
|
-
if (text.length === 0) continue;
|
|
234
|
-
next.push(text === inner.text ? inner : { ...inner, text });
|
|
235
|
-
}
|
|
236
|
-
return { ...block, content: next };
|
|
237
|
-
}
|
|
238
|
-
return block;
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Truncate every `tool_result` block in an Anthropic messages array to
|
|
243
|
-
* `maxChars`, walking oldest→newest. Returns a fresh messages array. The last
|
|
244
|
-
* user turn is left untouched even if it carries a tool_result, so the live
|
|
245
|
-
* query is never degraded — and a `cache_control`-bearing message is left
|
|
246
|
-
* byte-identical so the cached prefix (the ~10x-cost breakpoint) survives.
|
|
247
|
-
*/
|
|
248
|
-
const truncateAnthropicToolOutputs = (
|
|
249
|
-
messages: ReadonlyArray<unknown>,
|
|
250
|
-
maxChars: number,
|
|
251
|
-
lastUserIndex: number,
|
|
252
|
-
): unknown[] =>
|
|
253
|
-
messages.map((msg, i) => {
|
|
254
|
-
if (
|
|
255
|
-
i === lastUserIndex ||
|
|
256
|
-
!isRecord(msg) ||
|
|
257
|
-
!Array.isArray(msg.content) ||
|
|
258
|
-
hasCacheControl(msg)
|
|
259
|
-
) {
|
|
260
|
-
return msg;
|
|
261
|
-
}
|
|
262
|
-
const content = msg.content.map((block) =>
|
|
263
|
-
isRecord(block) && block.type === "tool_result"
|
|
264
|
-
? truncateAnthropicToolResult(block, maxChars)
|
|
265
|
-
: block,
|
|
266
|
-
);
|
|
267
|
-
return { ...msg, content };
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// ─── Canonical (OpenAI) truncation ────────────────────────────────────────
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Truncate the output of every `role:"tool"` message to `maxChars`, oldest→
|
|
274
|
-
* newest, skipping the last user turn. `content` is `string | part[]`; string
|
|
275
|
-
* truncates directly, part-array truncates `text` parts in place. A
|
|
276
|
-
* `cache_control`-bearing message is left byte-identical so the cached prefix
|
|
277
|
-
* survives (dropping/altering a breakpoint silently ~10x's token cost).
|
|
278
|
-
*/
|
|
279
|
-
const truncateCanonicalToolOutputs = (
|
|
280
|
-
messages: ReadonlyArray<unknown>,
|
|
281
|
-
maxChars: number,
|
|
282
|
-
lastUserIndex: number,
|
|
283
|
-
): unknown[] =>
|
|
284
|
-
messages.map((msg, i) => {
|
|
285
|
-
if (
|
|
286
|
-
i === lastUserIndex ||
|
|
287
|
-
!isRecord(msg) ||
|
|
288
|
-
msg.role !== "tool" ||
|
|
289
|
-
hasCacheControl(msg)
|
|
290
|
-
)
|
|
291
|
-
return msg;
|
|
292
|
-
const content = msg.content;
|
|
293
|
-
if (typeof content === "string") {
|
|
294
|
-
return { ...msg, content: truncateMiddleToChars(content, maxChars) };
|
|
295
|
-
}
|
|
296
|
-
if (Array.isArray(content)) {
|
|
297
|
-
const next = content.map((part) =>
|
|
298
|
-
isRecord(part) && part.type === "text" && typeof part.text === "string"
|
|
299
|
-
? { ...part, text: truncateMiddleToChars(part.text, maxChars) }
|
|
300
|
-
: part,
|
|
301
|
-
);
|
|
302
|
-
return { ...msg, content: next };
|
|
303
|
-
}
|
|
304
|
-
return msg;
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
// ─── Responses (ChatGPT / Codex) truncation ───────────────────────────────
|
|
308
|
-
//
|
|
309
|
-
// A Responses body is `{ input: string | item[] }` — NOT a `messages` array.
|
|
310
|
-
// Items are `message` (role + `input_text`/`output_text`/image/file parts),
|
|
311
|
-
// `function_call` (`call_id`, `arguments`), `function_call_output` (`call_id`,
|
|
312
|
-
// bulky `output`), and `reasoning`. Tool pairing is `function_call.call_id` ↔
|
|
313
|
-
// `function_call_output.call_id`, as SEPARATE top-level items. There is no
|
|
314
|
-
// per-item `cache_control` (Responses caches via the top-level
|
|
315
|
-
// `prompt_cache_key`, which compaction never touches).
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Truncate the `output` of every `function_call_output` item to `maxChars`,
|
|
319
|
-
* oldest→newest, skipping the last user turn. `output` is `string | part[]`;
|
|
320
|
-
* string truncates directly, part-array truncates `input_text`/`output_text`
|
|
321
|
-
* parts in place.
|
|
322
|
-
*/
|
|
323
|
-
const truncateResponsesToolOutputs = (
|
|
324
|
-
input: ReadonlyArray<unknown>,
|
|
325
|
-
maxChars: number,
|
|
326
|
-
lastUserIndex: number,
|
|
327
|
-
): unknown[] =>
|
|
328
|
-
input.map((item, i) => {
|
|
329
|
-
if (
|
|
330
|
-
i === lastUserIndex ||
|
|
331
|
-
!isRecord(item) ||
|
|
332
|
-
item.type !== "function_call_output"
|
|
333
|
-
) {
|
|
334
|
-
return item;
|
|
335
|
-
}
|
|
336
|
-
const output = item.output;
|
|
337
|
-
if (typeof output === "string") {
|
|
338
|
-
return { ...item, output: truncateMiddleToChars(output, maxChars) };
|
|
339
|
-
}
|
|
340
|
-
if (Array.isArray(output)) {
|
|
341
|
-
const next = output.map((part) =>
|
|
342
|
-
isRecord(part) &&
|
|
343
|
-
(part.type === "input_text" || part.type === "output_text") &&
|
|
344
|
-
typeof part.text === "string"
|
|
345
|
-
? { ...part, text: truncateMiddleToChars(part.text, maxChars) }
|
|
346
|
-
: part,
|
|
347
|
-
);
|
|
348
|
-
return { ...item, output: next };
|
|
349
|
-
}
|
|
350
|
-
return item;
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
// ─── Turn dropping (with pairing + cache preservation) ─────────────────────
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Indices of messages that are safe to drop, oldest first. NEVER drops: the
|
|
357
|
-
* system prompt (canonical only — Anthropic `system` is a top-level field), the
|
|
358
|
-
* last user turn, any message carrying a `cache_control` breakpoint (the cached
|
|
359
|
-
* prefix), and — for canonical — a `tool` result whose paired assistant
|
|
360
|
-
* `tool_call` we'd be keeping. Pairing is preserved by dropping the assistant
|
|
361
|
-
* turn and its tool results together — canonical tool messages by
|
|
362
|
-
* `tool_call_id`, Anthropic tool_result blocks by `tool_use_id` in a following
|
|
363
|
-
* user message — so neither shape is left with an orphaned tool result.
|
|
364
|
-
*/
|
|
365
|
-
const droppableTurnStart = (messages: ReadonlyArray<unknown>): number => {
|
|
366
|
-
// Skip the leading cache_control-bearing prefix and any system messages.
|
|
367
|
-
let start = 0;
|
|
368
|
-
while (start < messages.length) {
|
|
369
|
-
const m = messages[start];
|
|
370
|
-
if (isRecord(m) && m.role === "system") {
|
|
371
|
-
start++;
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
if (hasCacheControl(m)) {
|
|
375
|
-
start++;
|
|
376
|
-
continue;
|
|
377
|
-
}
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
return start;
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* Collect the ids that a canonical `tool` message pairs with, so dropping an
|
|
385
|
-
* assistant turn also drops its orphaned tool results. For Anthropic the pairing
|
|
386
|
-
* lives inside a single message's block array, so dropping the message is
|
|
387
|
-
* self-consistent and this is unused.
|
|
388
|
-
*/
|
|
389
|
-
const canonicalToolCallIds = (msg: unknown): ReadonlySet<string> => {
|
|
390
|
-
const ids = new Set<string>();
|
|
391
|
-
if (isRecord(msg) && Array.isArray(msg.tool_calls)) {
|
|
392
|
-
for (const tc of msg.tool_calls) {
|
|
393
|
-
if (isRecord(tc) && typeof tc.id === "string") ids.add(tc.id);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return ids;
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* The `tool_use` block ids inside an Anthropic assistant message. Unlike the
|
|
401
|
-
* canonical shape (tool calls on the message), Anthropic tool_use lives in the
|
|
402
|
-
* `content` block array — and its paired `tool_result` sits in a SEPARATE later
|
|
403
|
-
* user message (`tool_use.id` ↔ `tool_result.tool_use_id`). So dropping an
|
|
404
|
-
* assistant message is NOT self-contained: its tool_results must go too, or the
|
|
405
|
-
* transcript is left with an orphaned tool_result the vendor rejects.
|
|
406
|
-
*/
|
|
407
|
-
const anthropicToolUseIds = (msg: unknown): ReadonlySet<string> => {
|
|
408
|
-
const ids = new Set<string>();
|
|
409
|
-
if (isRecord(msg) && Array.isArray(msg.content)) {
|
|
410
|
-
for (const block of msg.content) {
|
|
411
|
-
if (
|
|
412
|
-
isRecord(block) &&
|
|
413
|
-
block.type === "tool_use" &&
|
|
414
|
-
typeof block.id === "string"
|
|
415
|
-
) {
|
|
416
|
-
ids.add(block.id);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
return ids;
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* Remove the `tool_result` blocks that reference `ids` from a following user
|
|
425
|
-
* message, returning the rewritten message — or `null` when that empties the
|
|
426
|
-
* message (caller then drops it whole, mirroring the adapter's empty-turn rule).
|
|
427
|
-
* Non-tool_result blocks and unrelated tool_results are preserved in place.
|
|
428
|
-
*/
|
|
429
|
-
const stripPairedToolResults = (
|
|
430
|
-
msg: unknown,
|
|
431
|
-
ids: ReadonlySet<string>,
|
|
432
|
-
): unknown | null => {
|
|
433
|
-
if (!isRecord(msg) || !Array.isArray(msg.content)) return msg;
|
|
434
|
-
const kept = msg.content.filter(
|
|
435
|
-
(block) =>
|
|
436
|
-
!(
|
|
437
|
-
isRecord(block) &&
|
|
438
|
-
block.type === "tool_result" &&
|
|
439
|
-
typeof block.tool_use_id === "string" &&
|
|
440
|
-
ids.has(block.tool_use_id)
|
|
441
|
-
),
|
|
442
|
-
);
|
|
443
|
-
if (kept.length === 0) return null;
|
|
444
|
-
return { ...msg, content: kept };
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
/** True when `msg` carries a `tool_result` block referencing any id in `ids`. */
|
|
448
|
-
const toolResultsReference = (
|
|
449
|
-
msg: unknown,
|
|
450
|
-
ids: ReadonlySet<string>,
|
|
451
|
-
): boolean =>
|
|
452
|
-
isRecord(msg) &&
|
|
453
|
-
Array.isArray(msg.content) &&
|
|
454
|
-
msg.content.some(
|
|
455
|
-
(block) =>
|
|
456
|
-
isRecord(block) &&
|
|
457
|
-
block.type === "tool_result" &&
|
|
458
|
-
typeof block.tool_use_id === "string" &&
|
|
459
|
-
ids.has(block.tool_use_id),
|
|
460
|
-
);
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* Drop the oldest droppable messages until the body fits or nothing more can go.
|
|
464
|
-
* Preserves: system prompt, cache prefix, last user turn, and tool-call pairing
|
|
465
|
-
* (a dropped assistant turn takes its `tool` results with it; a dropped
|
|
466
|
-
* Anthropic message is self-contained). Returns the surviving messages.
|
|
467
|
-
*/
|
|
468
|
-
const dropOldestTurns = (
|
|
469
|
-
messages: ReadonlyArray<unknown>,
|
|
470
|
-
anthropic: boolean,
|
|
471
|
-
fits: (msgs: ReadonlyArray<unknown>) => boolean,
|
|
472
|
-
): unknown[] => {
|
|
473
|
-
const survivors = [...messages];
|
|
474
|
-
const start = droppableTurnStart(messages);
|
|
475
|
-
// The tool_call ids belonging to PROTECTED assistant turns (system or
|
|
476
|
-
// cache_control — kept by the loop below). A `tool` message paired to one of
|
|
477
|
-
// these must NOT be dropped, or the protected assistant is left with an
|
|
478
|
-
// orphaned tool_call. Computed once up front because protection doesn't change
|
|
479
|
-
// as we splice.
|
|
480
|
-
const protectedToolCallIds = new Set<string>();
|
|
481
|
-
if (!anthropic) {
|
|
482
|
-
for (const m of messages) {
|
|
483
|
-
if (isRecord(m) && m.role === "assistant" && hasCacheControl(m)) {
|
|
484
|
-
for (const id of canonicalToolCallIds(m)) protectedToolCallIds.add(id);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
// Recompute the protected tail index within `survivors` as we splice.
|
|
489
|
-
let i = start;
|
|
490
|
-
while (i < survivors.length) {
|
|
491
|
-
if (fits(survivors)) break;
|
|
492
|
-
// Never drop at or past the last user turn.
|
|
493
|
-
const lastUser = survivors.reduceRight<number>(
|
|
494
|
-
(acc, m, idx) =>
|
|
495
|
-
acc === -1 && isRecord(m) && m.role === "user" ? idx : acc,
|
|
496
|
-
-1,
|
|
497
|
-
);
|
|
498
|
-
if (i >= lastUser) break;
|
|
499
|
-
const msg = survivors[i];
|
|
500
|
-
if (isRecord(msg) && (msg.role === "system" || hasCacheControl(msg))) {
|
|
501
|
-
i++;
|
|
502
|
-
continue;
|
|
503
|
-
}
|
|
504
|
-
// A canonical `tool` message paired to a PROTECTED assistant must survive.
|
|
505
|
-
if (
|
|
506
|
-
!anthropic &&
|
|
507
|
-
isRecord(msg) &&
|
|
508
|
-
msg.role === "tool" &&
|
|
509
|
-
typeof msg.tool_call_id === "string" &&
|
|
510
|
-
protectedToolCallIds.has(msg.tool_call_id)
|
|
511
|
-
) {
|
|
512
|
-
i++;
|
|
513
|
-
continue;
|
|
514
|
-
}
|
|
515
|
-
if (!anthropic && isRecord(msg) && msg.role === "assistant") {
|
|
516
|
-
// Canonical: drop the assistant turn AND its paired tool results together.
|
|
517
|
-
const callIds = canonicalToolCallIds(msg);
|
|
518
|
-
survivors.splice(i, 1);
|
|
519
|
-
if (callIds.size > 0) {
|
|
520
|
-
for (let j = i; j < survivors.length; ) {
|
|
521
|
-
const t = survivors[j];
|
|
522
|
-
if (
|
|
523
|
-
isRecord(t) &&
|
|
524
|
-
t.role === "tool" &&
|
|
525
|
-
typeof t.tool_call_id === "string" &&
|
|
526
|
-
callIds.has(t.tool_call_id)
|
|
527
|
-
) {
|
|
528
|
-
survivors.splice(j, 1);
|
|
529
|
-
} else {
|
|
530
|
-
j++;
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
continue;
|
|
535
|
-
}
|
|
536
|
-
if (anthropic && isRecord(msg) && msg.role === "assistant") {
|
|
537
|
-
// Anthropic: an assistant `tool_use` pairs with a `tool_result` in a
|
|
538
|
-
// SEPARATE later user message. Drop the assistant turn AND strip its
|
|
539
|
-
// paired tool_results (dropping a now-empty user turn).
|
|
540
|
-
const useIds = anthropicToolUseIds(msg);
|
|
541
|
-
// But NOT if any of those tool_uses pair into the PROTECTED last user turn
|
|
542
|
-
// — dropping the assistant would orphan (or, via stripping, degrade) the
|
|
543
|
-
// live query. Keep this assistant and move on.
|
|
544
|
-
if (
|
|
545
|
-
useIds.size > 0 &&
|
|
546
|
-
toolResultsReference(survivors[lastUser], useIds)
|
|
547
|
-
) {
|
|
548
|
-
i++;
|
|
549
|
-
continue;
|
|
550
|
-
}
|
|
551
|
-
survivors.splice(i, 1);
|
|
552
|
-
if (useIds.size > 0) {
|
|
553
|
-
for (let j = i; j < survivors.length; ) {
|
|
554
|
-
const stripped = stripPairedToolResults(survivors[j], useIds);
|
|
555
|
-
if (stripped === null) {
|
|
556
|
-
survivors.splice(j, 1);
|
|
557
|
-
} else {
|
|
558
|
-
survivors[j] = stripped;
|
|
559
|
-
j++;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
continue;
|
|
564
|
-
}
|
|
565
|
-
survivors.splice(i, 1);
|
|
566
|
-
}
|
|
567
|
-
return survivors;
|
|
568
|
-
};
|
|
569
|
-
|
|
570
|
-
/** Responses `message` items may omit `type` (OpenAI treats it as optional). */
|
|
571
|
-
const isResponsesMessageItem = (item: Record<string, unknown>): boolean =>
|
|
572
|
-
item.type === "message" || item.type === undefined;
|
|
573
|
-
|
|
574
|
-
/** The index of the last Responses `message` item with `role:"user"`. */
|
|
575
|
-
const lastResponsesUserIndex = (input: ReadonlyArray<unknown>): number =>
|
|
576
|
-
input.reduceRight<number>(
|
|
577
|
-
(acc, item, idx) =>
|
|
578
|
-
acc === -1 &&
|
|
579
|
-
isRecord(item) &&
|
|
580
|
-
isResponsesMessageItem(item) &&
|
|
581
|
-
item.role === "user"
|
|
582
|
-
? idx
|
|
583
|
-
: acc,
|
|
584
|
-
-1,
|
|
585
|
-
);
|
|
586
|
-
|
|
587
|
-
/**
|
|
588
|
-
* Drop the oldest droppable Responses `input` items until the body fits or
|
|
589
|
-
* nothing more can go. Preserves the last user `message` (the live query) and
|
|
590
|
-
* `function_call` ↔ `function_call_output` pairing (dropping a `function_call`
|
|
591
|
-
* takes its matching `function_call_output` with it, and vice-versa), so the
|
|
592
|
-
* upstream never sees an orphaned call or output. Leading instruction `message`
|
|
593
|
-
* items and `additional_tools` declarations are never dropped.
|
|
594
|
-
*/
|
|
595
|
-
const dropOldestResponsesItems = (
|
|
596
|
-
input: ReadonlyArray<unknown>,
|
|
597
|
-
fits: (items: ReadonlyArray<unknown>) => boolean,
|
|
598
|
-
): unknown[] => {
|
|
599
|
-
const survivors = [...input];
|
|
600
|
-
// Preserve leading non-turn state: system/developer instructions and Codex's
|
|
601
|
-
// v0.147 `additional_tools` declarations must survive any retry, otherwise
|
|
602
|
-
// the remaining conversation can reference tools the upstream no longer has.
|
|
603
|
-
let start = 0;
|
|
604
|
-
while (start < survivors.length) {
|
|
605
|
-
const item = survivors[start];
|
|
606
|
-
if (
|
|
607
|
-
isRecord(item) &&
|
|
608
|
-
(item.type === "additional_tools" ||
|
|
609
|
-
(isResponsesMessageItem(item) &&
|
|
610
|
-
(item.role === "system" || item.role === "developer")))
|
|
611
|
-
) {
|
|
612
|
-
start++;
|
|
613
|
-
continue;
|
|
614
|
-
}
|
|
615
|
-
break;
|
|
616
|
-
}
|
|
617
|
-
let i = start;
|
|
618
|
-
while (i < survivors.length) {
|
|
619
|
-
if (fits(survivors)) break;
|
|
620
|
-
const lastUser = lastResponsesUserIndex(survivors);
|
|
621
|
-
if (i >= lastUser) break;
|
|
622
|
-
const item = survivors[i];
|
|
623
|
-
if (
|
|
624
|
-
isRecord(item) &&
|
|
625
|
-
isResponsesMessageItem(item) &&
|
|
626
|
-
(item.role === "system" || item.role === "developer")
|
|
627
|
-
) {
|
|
628
|
-
i++;
|
|
629
|
-
continue;
|
|
630
|
-
}
|
|
631
|
-
// Dropping a function_call / function_call_output must take its pair too.
|
|
632
|
-
if (
|
|
633
|
-
isRecord(item) &&
|
|
634
|
-
(item.type === "function_call" || item.type === "function_call_output") &&
|
|
635
|
-
typeof item.call_id === "string"
|
|
636
|
-
) {
|
|
637
|
-
const callId = item.call_id;
|
|
638
|
-
survivors.splice(i, 1);
|
|
639
|
-
for (let j = 0; j < survivors.length; ) {
|
|
640
|
-
const other = survivors[j];
|
|
641
|
-
if (
|
|
642
|
-
isRecord(other) &&
|
|
643
|
-
(other.type === "function_call" ||
|
|
644
|
-
other.type === "function_call_output") &&
|
|
645
|
-
other.call_id === callId
|
|
646
|
-
) {
|
|
647
|
-
survivors.splice(j, 1);
|
|
648
|
-
if (j < i) i--; // a removal before the cursor shifts it left
|
|
649
|
-
} else {
|
|
650
|
-
j++;
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
continue;
|
|
654
|
-
}
|
|
655
|
-
survivors.splice(i, 1);
|
|
656
|
-
}
|
|
657
|
-
return survivors;
|
|
658
|
-
};
|
|
659
|
-
|
|
660
|
-
// ─── The public entry point ────────────────────────────────────────────────
|
|
661
|
-
|
|
662
|
-
/** Char budget per tool output at each truncation pass, tightening each round. */
|
|
663
|
-
const TRUNCATION_PASSES = [8000, 2000, 500] as const;
|
|
664
|
-
|
|
665
|
-
const lastUserTurnIndex = (messages: ReadonlyArray<unknown>): number =>
|
|
666
|
-
messages.reduceRight<number>(
|
|
667
|
-
(acc, m, idx) =>
|
|
668
|
-
acc === -1 && isRecord(m) && m.role === "user" ? idx : acc,
|
|
669
|
-
-1,
|
|
670
|
-
);
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* Shrink `body` until its estimated size is `<= targetTokens`, or report that it
|
|
674
|
-
* can't. The ladder, cheapest-and-least-destructive first:
|
|
675
|
-
* 1. Truncate tool outputs (3 tightening passes) — bulky, low-signal, and NOT
|
|
676
|
-
* part of the cached prefix in practice.
|
|
677
|
-
* 2. Drop the oldest non-cached, non-system, non-last-user turns.
|
|
678
|
-
* Recomputes the ruler estimate after each step and stops the moment it fits.
|
|
679
|
-
*
|
|
680
|
-
* `encoding` selects the ruler family for the fit check (defaults inside
|
|
681
|
-
* `estimateBodyTokens`). `compacted` is true when the body fits the target
|
|
682
|
-
* (already-fit bodies are returned untouched); it is false only when the body
|
|
683
|
-
* could not be shrunk to target — the caller then surfaces its original error.
|
|
684
|
-
*/
|
|
685
|
-
export const compactRequestToFit = (
|
|
686
|
-
body: unknown,
|
|
687
|
-
surface: TCompactionSurface,
|
|
688
|
-
targetTokens: number,
|
|
689
|
-
encoding?: TTokenEncoding,
|
|
690
|
-
): TCompactionResult => {
|
|
691
|
-
const estimate = (b: unknown): number => estimateBodyTokensExact(b, encoding);
|
|
692
|
-
|
|
693
|
-
const initial = estimate(body);
|
|
694
|
-
if (initial <= targetTokens) {
|
|
695
|
-
return { body, compacted: true, estimatedTokens: initial };
|
|
696
|
-
}
|
|
697
|
-
if (!isRecord(body)) {
|
|
698
|
-
return { body, compacted: false, estimatedTokens: initial };
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
// Responses (ChatGPT / Codex): shape is `{ input: item[] }`, handled by its
|
|
702
|
-
// own item-walk rather than the `messages`-array path.
|
|
703
|
-
if (surface === "responses" && Array.isArray(body.input)) {
|
|
704
|
-
return compactResponsesBody(
|
|
705
|
-
body,
|
|
706
|
-
body.input,
|
|
707
|
-
targetTokens,
|
|
708
|
-
initial,
|
|
709
|
-
estimate,
|
|
710
|
-
);
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
if (!Array.isArray(body.messages)) {
|
|
714
|
-
// Nothing structured to shrink (e.g. a bare-string Responses `input`, or an
|
|
715
|
-
// unrecognised body).
|
|
716
|
-
return { body, compacted: false, estimatedTokens: initial };
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
const anthropic = isAnthropic(surface);
|
|
720
|
-
let messages: unknown[] = [...body.messages];
|
|
721
|
-
const withMessages = (msgs: unknown[]): TRecord => ({
|
|
722
|
-
...body,
|
|
723
|
-
messages: msgs,
|
|
724
|
-
});
|
|
725
|
-
|
|
726
|
-
// Pass 1: truncate tool outputs, trying a deficit-sized cap FIRST (trim ~only
|
|
727
|
-
// what's needed), then the fixed ladder as a coarser fallback. The deficit is
|
|
728
|
-
// solved against target × 0.95 so the per-string ceil() rounding + non-tool
|
|
729
|
-
// overhead in the estimate can't leave the result a hair over the window (the
|
|
730
|
-
// exact-deficit solve landed 128014 on a 128000 target → fell to the coarse
|
|
731
|
-
// ladder and over-shrank).
|
|
732
|
-
const firstCap = firstPassCap(
|
|
733
|
-
messages,
|
|
734
|
-
surface,
|
|
735
|
-
initial - targetTokens * 0.95,
|
|
736
|
-
);
|
|
737
|
-
const caps =
|
|
738
|
-
firstCap === null ? TRUNCATION_PASSES : [firstCap, ...TRUNCATION_PASSES];
|
|
739
|
-
for (const cap of caps) {
|
|
740
|
-
const lastUser = lastUserTurnIndex(messages);
|
|
741
|
-
messages = anthropic
|
|
742
|
-
? truncateAnthropicToolOutputs(messages, cap, lastUser)
|
|
743
|
-
: truncateCanonicalToolOutputs(messages, cap, lastUser);
|
|
744
|
-
const est = estimate(withMessages(messages));
|
|
745
|
-
if (est <= targetTokens) {
|
|
746
|
-
return {
|
|
747
|
-
body: withMessages(messages),
|
|
748
|
-
compacted: true,
|
|
749
|
-
estimatedTokens: est,
|
|
750
|
-
};
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
// Pass 2: drop oldest droppable turns.
|
|
755
|
-
//
|
|
756
|
-
// The fit check runs at the top of EVERY iteration of the drop loop, so
|
|
757
|
-
// measuring the whole body each time made this quadratic: N drops × a full
|
|
758
|
-
// re-walk (and re-tokenization) of all N surviving messages. A 400-message
|
|
759
|
-
// body cost 400+ full-body estimates and ~19 M characters of tokenizer work.
|
|
760
|
-
//
|
|
761
|
-
// Instead, cost each message ONCE (memoized by identity) and track the total
|
|
762
|
-
// as a sum, so every message's text is walked a single time across the loop.
|
|
763
|
-
//
|
|
764
|
-
// The sum of per-message estimates is NOT the estimate of the assembled body:
|
|
765
|
-
// each part is rounded up independently, so the sum OVER-states the whole by
|
|
766
|
-
// up to ~1 token per part. Used directly that would make the loop keep
|
|
767
|
-
// dropping until the inflated figure fits — over-shrinking the request, the
|
|
768
|
-
// exact failure `1336079a` (deficit-driven truncation cap) set out to stop.
|
|
769
|
-
//
|
|
770
|
-
// So the loop predicate is deliberately a LOWER bound (the sum minus that
|
|
771
|
-
// per-part rounding slack). A lower bound can only ever stop dropping EARLY,
|
|
772
|
-
// never late — so it can never over-shrink, and the exact whole-body pass
|
|
773
|
-
// below finishes the job in the rare case it stopped short. The exact
|
|
774
|
-
// estimate remains the sole authority for the reported size and `compacted`.
|
|
775
|
-
const messagelessBody: TRecord = { ...body };
|
|
776
|
-
delete messagelessBody.messages;
|
|
777
|
-
const baseCost = estimate(messagelessBody);
|
|
778
|
-
const costCache = new Map<unknown, number>();
|
|
779
|
-
const costOf = (m: unknown): number => {
|
|
780
|
-
const hit = costCache.get(m);
|
|
781
|
-
if (hit !== undefined) return hit;
|
|
782
|
-
const c = estimate(m);
|
|
783
|
-
costCache.set(m, c);
|
|
784
|
-
return c;
|
|
785
|
-
};
|
|
786
|
-
const approxFits = (msgs: ReadonlyArray<unknown>): boolean => {
|
|
787
|
-
let total = baseCost;
|
|
788
|
-
for (const m of msgs) total += costOf(m);
|
|
789
|
-
// Discount the per-part ceil() slack to keep this a lower bound.
|
|
790
|
-
return total - msgs.length <= targetTokens;
|
|
791
|
-
};
|
|
792
|
-
|
|
793
|
-
messages = dropOldestTurns(messages, anthropic, approxFits);
|
|
794
|
-
|
|
795
|
-
let finalBody = withMessages(messages);
|
|
796
|
-
let finalEst = estimate(finalBody);
|
|
797
|
-
if (finalEst > targetTokens) {
|
|
798
|
-
// The lower bound stopped us early — finish with the exact predicate. Only
|
|
799
|
-
// the remaining gap is walked, so this stays cheap.
|
|
800
|
-
messages = dropOldestTurns(
|
|
801
|
-
messages,
|
|
802
|
-
anthropic,
|
|
803
|
-
(msgs) => estimate(withMessages([...msgs])) <= targetTokens,
|
|
804
|
-
);
|
|
805
|
-
finalBody = withMessages(messages);
|
|
806
|
-
finalEst = estimate(finalBody);
|
|
807
|
-
}
|
|
808
|
-
return {
|
|
809
|
-
body: finalBody,
|
|
810
|
-
compacted: finalEst <= targetTokens,
|
|
811
|
-
estimatedTokens: finalEst,
|
|
812
|
-
};
|
|
813
|
-
};
|
|
814
|
-
|
|
815
|
-
/**
|
|
816
|
-
* The `responses`-surface arm of {@link compactRequestToFit}: same two-pass
|
|
817
|
-
* ladder (truncate `function_call_output`s → drop oldest items) over the
|
|
818
|
-
* Responses `input[]` array, preserving the last user message and
|
|
819
|
-
* function_call ↔ function_call_output pairing.
|
|
820
|
-
*/
|
|
821
|
-
const compactResponsesBody = (
|
|
822
|
-
body: TRecord,
|
|
823
|
-
inputItems: ReadonlyArray<unknown>,
|
|
824
|
-
targetTokens: number,
|
|
825
|
-
initialEstimate: number,
|
|
826
|
-
estimate: (b: unknown) => number,
|
|
827
|
-
): TCompactionResult => {
|
|
828
|
-
let input: unknown[] = [...inputItems];
|
|
829
|
-
const withInput = (items: unknown[]): TRecord => ({ ...body, input: items });
|
|
830
|
-
|
|
831
|
-
// Pass 1: truncate tool outputs, deficit-sized cap first (solved against
|
|
832
|
-
// target × 0.95 — see the messages arm), then the fixed ladder.
|
|
833
|
-
const firstCap = firstPassCap(
|
|
834
|
-
input,
|
|
835
|
-
"responses",
|
|
836
|
-
initialEstimate - targetTokens * 0.95,
|
|
837
|
-
);
|
|
838
|
-
const caps =
|
|
839
|
-
firstCap === null ? TRUNCATION_PASSES : [firstCap, ...TRUNCATION_PASSES];
|
|
840
|
-
for (const cap of caps) {
|
|
841
|
-
const lastUser = lastResponsesUserIndex(input);
|
|
842
|
-
input = truncateResponsesToolOutputs(input, cap, lastUser);
|
|
843
|
-
const est = estimate(withInput(input));
|
|
844
|
-
if (est <= targetTokens) {
|
|
845
|
-
return { body: withInput(input), compacted: true, estimatedTokens: est };
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
// Pass 2: drop oldest droppable items (pairing preserved).
|
|
850
|
-
input = dropOldestResponsesItems(
|
|
851
|
-
input,
|
|
852
|
-
(items) => estimate(withInput([...items])) <= targetTokens,
|
|
853
|
-
);
|
|
854
|
-
|
|
855
|
-
const finalBody = withInput(input);
|
|
856
|
-
const finalEst = estimate(finalBody);
|
|
857
|
-
return {
|
|
858
|
-
body: finalBody,
|
|
859
|
-
compacted: finalEst <= targetTokens,
|
|
860
|
-
estimatedTokens: finalEst,
|
|
861
|
-
};
|
|
862
|
-
};
|