@bitkyc08/opencodex 2.100.5 → 2.100.6-preview.20261119.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/ocx.mjs +2 -0
- package/gui/dist/assets/App-BI8LQ7Qk.css +1 -0
- package/gui/dist/assets/App-D5qUBOoF.js +60 -0
- package/gui/dist/assets/Tray-DMwQSAli.js +1 -0
- package/gui/dist/assets/brand-mark-ycThul7d.js +1 -0
- package/gui/dist/assets/index-Bp_GaFFu.js +138 -0
- package/gui/dist/assets/index-BtwVlpeQ.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-Bquysdn5.js → segment-thumb-features-BLUW2Fb1.js} +1 -1
- package/gui/dist/classic/assets/App-CoiVLRW3.css +1 -0
- package/gui/dist/classic/assets/App-D39mkP2w.js +59 -0
- package/gui/dist/classic/assets/Tray-nUmdL7K9.js +1 -0
- package/gui/dist/classic/assets/index-BJ4uxZk0.js +138 -0
- package/gui/dist/classic/assets/usage-companion-chart-DFoYm2RY.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +62 -0
- package/native/ocx-child/Cargo.toml +1 -0
- package/native/ocx-child/src/journal.rs +32 -10
- package/native/ocx-child/src/state.rs +2 -1
- package/native/ocx-child/src/toml_surgeon.rs +183 -72
- package/package.json +2 -2
- package/src/adapters/agy-acp/acp.ts +202 -0
- package/src/adapters/agy-acp/adapter.ts +554 -0
- package/src/adapters/agy-acp/binary.ts +79 -0
- package/src/adapters/agy-acp/options.ts +163 -0
- package/src/adapters/agy-acp/output-redaction.ts +270 -0
- package/src/adapters/agy-acp/policy.ts +105 -0
- package/src/adapters/agy-acp/process.ts +218 -0
- package/src/adapters/anthropic/account-metadata.ts +4 -1
- package/src/adapters/anthropic/advisor-tool.ts +43 -9
- package/src/adapters/anthropic/client-identity.ts +2 -0
- package/src/adapters/anthropic.ts +102 -14
- package/src/adapters/antigravity-refusal.ts +5 -1
- package/src/adapters/base.ts +25 -2
- package/src/adapters/claude-cli/adapter.ts +1 -0
- package/src/adapters/claude-cli/usage-admission.ts +12 -9
- package/src/adapters/codebuddy/adapter.ts +1 -0
- package/src/adapters/command-code.ts +2 -2
- package/src/adapters/cursor/http1-bidi.ts +7 -2
- package/src/adapters/cursor/live-transport.ts +2 -0
- package/src/adapters/cursor/request-builder.ts +4 -4
- package/src/adapters/cursor/tool-naming.ts +5 -5
- package/src/adapters/cursor/transport-retry.ts +14 -8
- package/src/adapters/cursor/transport.ts +2 -0
- package/src/adapters/cursor.ts +8 -2
- package/src/adapters/google-error-details.ts +105 -0
- package/src/adapters/google-errors.ts +183 -31
- package/src/adapters/google-http.ts +66 -22
- package/src/adapters/google-truncation.ts +48 -3
- package/src/adapters/google-vertex-endpoint.ts +12 -1
- package/src/adapters/google.ts +54 -26
- package/src/adapters/kiro/adapter.ts +39 -38
- package/src/adapters/kiro/stream.ts +59 -9
- package/src/adapters/kiro-retry.ts +5 -3
- package/src/adapters/m365-copilot/LICENSE +21 -0
- package/src/adapters/m365-copilot/fenced.ts +745 -0
- package/src/adapters/m365-copilot/images.ts +89 -0
- package/src/adapters/m365-copilot/tools.ts +284 -0
- package/src/adapters/m365-copilot/transport.ts +232 -0
- package/src/adapters/m365-copilot.ts +143 -0
- package/src/adapters/openai-chat/tool-schema.ts +2 -2
- package/src/adapters/openai-responses/passthrough.ts +1 -0
- package/src/adapters/openai-responses/reasoning.ts +23 -3
- package/src/adapters/physical-send.ts +11 -4
- package/src/adapters/qoder/adapter.ts +17 -3
- package/src/adapters/registry.ts +15 -1
- package/src/adapters/zed.ts +2 -1
- package/src/advisor/consult.ts +1 -0
- package/src/bridge/response-json.ts +6 -2
- package/src/bridge/sse.ts +21 -9
- package/src/chat/inbound.ts +11 -8
- package/src/chat/outbound.ts +28 -7
- package/src/claude/context-windows.ts +32 -8
- package/src/claude/desktop-3p.ts +18 -14
- package/src/claude/desktop-picker-platform.ts +1 -1
- package/src/claude/desktop-picker.ts +3 -3
- package/src/claude/inbound-content-options.ts +11 -1
- package/src/claude/intercept/picker-ca-cleanup.ts +17 -5
- package/src/claude/intercept/picker-ca.ts +162 -27
- package/src/claude/intercept/picker-trust-linux.ts +411 -0
- package/src/claude/intercept/picker-trust.ts +35 -3
- package/src/claude/intercept/runtime.ts +2 -2
- package/src/claude/subagent-force-status.ts +24 -5
- package/src/claude/subscription-reserve.ts +87 -0
- package/src/cli/account-api.ts +3 -0
- package/src/cli/account-auth.ts +65 -11
- package/src/cli/account-extended.ts +1 -1
- package/src/cli/account-next-actions.ts +4 -1
- package/src/cli/account-policy-dto.ts +1 -1
- package/src/cli/account-policy.ts +6 -3
- package/src/cli/account-probe.ts +66 -0
- package/src/cli/account.ts +8 -2
- package/src/cli/capabilities-accounts.ts +8 -1
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-base.ts +4 -4
- package/src/cli/capabilities-integrations.ts +44 -4
- package/src/cli/capabilities-provider-models.ts +6 -0
- package/src/cli/claude-context-settings.ts +60 -0
- package/src/cli/claude-desktop.ts +2 -2
- package/src/cli/claude.ts +15 -7
- package/src/cli/client-start-fence.ts +64 -1
- package/src/cli/combo-result.ts +1 -0
- package/src/cli/combo-stats.ts +1 -1
- package/src/cli/combo.ts +19 -2
- package/src/cli/config-command.ts +21 -8
- package/src/cli/connect.ts +16 -0
- package/src/cli/cross-home-owner.ts +65 -18
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/gateway-client-command.ts +128 -0
- package/src/cli/index.ts +98 -97
- package/src/cli/init.ts +20 -4
- package/src/cli/integrations.ts +23 -3
- package/src/cli/launcher-context.ts +12 -1
- package/src/cli/link.ts +24 -5
- package/src/cli/models-runtime-subcommands.ts +1 -0
- package/src/cli/models-runtime.ts +32 -0
- package/src/cli/start-owner-fence.ts +5 -2
- package/src/cli/status.ts +3 -3
- package/src/cli/stop-client-process.ts +51 -0
- package/src/cli/stop-restore.ts +383 -36
- package/src/cli/sync-command.ts +2 -2
- package/src/cli/uninstall-client-state.ts +7 -0
- package/src/client/connect.ts +72 -13
- package/src/client/hub-client.ts +38 -0
- package/src/client/hub-relay.ts +61 -36
- package/src/client/hub-session.ts +106 -0
- package/src/client/lifecycle-lock.ts +6 -1
- package/src/client/link-relay.ts +2 -1
- package/src/client/machine-listener.ts +60 -1
- package/src/client/runtime-restart.ts +1 -0
- package/src/client/runtime.ts +104 -34
- package/src/client/state.ts +11 -0
- package/src/client/stop-darwin-process.ts +73 -0
- package/src/client/stop-file-routing.ts +49 -0
- package/src/client/stop-native-handoff.ts +108 -0
- package/src/client/stop-routing.ts +41 -7
- package/src/client/stop-teardown-owner.ts +173 -0
- package/src/client/sync-runtime.ts +23 -4
- package/src/clients/config-export/opencode-wire.ts +24 -0
- package/src/clients/config-export.ts +28 -19
- package/src/codex/auth-api/login-flow.ts +68 -33
- package/src/codex/auth-api/main-account-probe.ts +3 -3
- package/src/codex/auth-api/pool-quota-probe.ts +8 -7
- package/src/codex/auth-api/routes.ts +1 -1
- package/src/codex/catalog/aggregation.ts +22 -10
- package/src/codex/catalog/gather-capture.ts +11 -2
- package/src/codex/catalog/m365-copilot-models.ts +52 -0
- package/src/codex/catalog/provider-models.ts +126 -53
- package/src/codex/catalog/routed-gather.ts +26 -12
- package/src/codex/catalog-admission.ts +4 -6
- package/src/codex/catalog-auto-refresh.ts +4 -2
- package/src/codex/catalog-self-heal.ts +4 -2
- package/src/codex/control-plane-models.ts +6 -0
- package/src/codex/convergence.ts +10 -0
- package/src/codex/desktop-app/picker-freshness.ts +3 -14
- package/src/codex/desktop-app/types.ts +6 -0
- package/src/codex/desktop-app/windows-command-payload.ts +9 -0
- package/src/codex/desktop-app/windows.ts +20 -12
- package/src/codex/desktop-compatibility/installed-build.ts +61 -58
- package/src/codex/desktop-compatibility/runtime-ownership.ts +11 -5
- package/src/codex/desktop-compatibility/runtime.ts +51 -14
- package/src/codex/desktop-compatibility/windows-activation-source.ts +28 -13
- package/src/codex/desktop-compatibility/windows-package-command.ts +73 -33
- package/src/codex/desktop-compatibility/windows-package-launch.ts +41 -9
- package/src/codex/desktop-compatibility/windows-package-proof.ts +104 -0
- package/src/codex/inject/config-toml.ts +67 -73
- package/src/codex/inject/plan.ts +11 -10
- package/src/codex/inject/remove.ts +4 -1
- package/src/codex/inject.ts +1 -0
- package/src/codex/journal-config-restore.ts +31 -4
- package/src/codex/journal.ts +16 -4
- package/src/codex/model-cache.ts +98 -27
- package/src/codex/native-residue.ts +14 -13
- package/src/codex/paths.ts +65 -28
- package/src/codex/quota-observation-freshness.ts +4 -0
- package/src/codex/quota.ts +71 -8
- package/src/codex/routing/selection.ts +32 -8
- package/src/combos/capability-policy.ts +31 -0
- package/src/combos/failover.ts +82 -74
- package/src/combos/identifiers.ts +2 -2
- package/src/combos/index.ts +3 -0
- package/src/combos/jev-model-backend.ts +12 -4
- package/src/combos/jev-session-pin-config.ts +22 -9
- package/src/combos/jev.ts +15 -4
- package/src/combos/request-capability.ts +214 -0
- package/src/combos/request.ts +22 -15
- package/src/combos/resolve.ts +5 -2
- package/src/combos/suggestion-channel.ts +89 -0
- package/src/combos/types.ts +14 -1
- package/src/config/account-auto-pause-revisions.ts +33 -10
- package/src/config/atomic-write.ts +1 -1
- package/src/config/codex-ws-incremental-setting.ts +34 -0
- package/src/config/diagnostics.ts +47 -8
- package/src/config/live-topology.ts +4 -2
- package/src/config/load-degrade.ts +96 -2
- package/src/config/mutation-lock.ts +7 -3
- package/src/config/paths.ts +3 -0
- package/src/config/pending-teardown.ts +19 -3
- package/src/config/process-state.ts +30 -12
- package/src/config/provider-validation.ts +10 -0
- package/src/config/salvage.ts +2 -2
- package/src/config/schema/account-auto-pause.ts +15 -1
- package/src/config/schema/agy-acp-references.ts +257 -0
- package/src/config/schema/config-schema.ts +32 -0
- package/src/config/schema/leaf-validators.ts +42 -0
- package/src/config/schema/subscription-reserve.ts +33 -0
- package/src/config/windows-process-command-line.ts +27 -9
- package/src/config.ts +6 -0
- package/src/generated/compatibility-version.json +806 -402
- package/src/images/loop.ts +79 -39
- package/src/integrations/catalog-refresh.ts +19 -1
- package/src/integrations/gateway-clients/agy.ts +340 -0
- package/src/integrations/gateway-clients/antigravity.ts +224 -0
- package/src/integrations/gateway-clients/index.ts +89 -0
- package/src/integrations/gateway-clients/io.ts +278 -0
- package/src/integrations/gateway-clients/types.ts +80 -0
- package/src/integrations/route-intent-disconnect.ts +37 -21
- package/src/lib/app-owned-memory-stores.ts +9 -0
- package/src/lib/cloud-code-appeal-link.ts +30 -0
- package/src/lib/errors.ts +32 -0
- package/src/lib/executor-physical-send-receipt.ts +42 -0
- package/src/lib/failure-detail.ts +1 -0
- package/src/lib/gcp-adc.ts +5 -0
- package/src/lib/http-date.ts +91 -0
- package/src/lib/lab-passive-linker-registration.ts +2 -1
- package/src/lib/local-chat-completion.ts +13 -3
- package/src/lib/local-internal-call-capability.ts +1 -3
- package/src/lib/messages-error-body.ts +2 -1
- package/src/lib/outbound-credential-registry.ts +40 -4
- package/src/lib/pinned-http.ts +7 -2
- package/src/lib/process-control.ts +1 -0
- package/src/lib/provider-egress.ts +16 -0
- package/src/lib/provider-outbound.ts +3 -0
- package/src/lib/redact-folding.ts +12 -3
- package/src/lib/redact.ts +37 -19
- package/src/lib/request-execution-budget.ts +241 -32
- package/src/lib/request-peer-scope.ts +64 -0
- package/src/lib/response-send-readings.ts +41 -0
- package/src/lib/state-store-registrations.ts +5 -0
- package/src/lib/tool-envelope-echo-filter.ts +33 -5
- package/src/lib/upstream-condition.ts +10 -3
- package/src/lib/upstream-retry.ts +95 -20
- package/src/lib/windows-secret-acl.ts +24 -7
- package/src/link/child-activity.ts +60 -4
- package/src/link/codex-settings.ts +2 -1
- package/src/link/relay-auth.ts +1 -0
- package/src/link/route-rounds.ts +32 -9
- package/src/link/status-projection.ts +42 -5
- package/src/link/supervisor.ts +96 -11
- package/src/link/tunnel-history.ts +5 -0
- package/src/oauth/account-auto-pause-policy.ts +72 -19
- package/src/oauth/account-auto-pause.ts +40 -16
- package/src/oauth/anthropic-account-refusal.ts +7 -0
- package/src/oauth/anthropic-continuity.ts +70 -9
- package/src/oauth/anthropic-identity.ts +75 -11
- package/src/oauth/anthropic-login-slot.ts +258 -0
- package/src/oauth/anthropic-routing.ts +24 -9
- package/src/oauth/anthropic-send-ownership.ts +4 -3
- package/src/oauth/anthropic.ts +30 -11
- package/src/oauth/health.ts +7 -2
- package/src/oauth/index.ts +50 -48
- package/src/oauth/key-providers.ts +2 -0
- package/src/oauth/login-cli.ts +11 -0
- package/src/oauth/login-flow-state.ts +10 -4
- package/src/oauth/m365-copilot-browser.ts +222 -0
- package/src/oauth/m365-copilot-web-session.ts +25 -0
- package/src/oauth/m365-copilot.ts +57 -0
- package/src/oauth/model-discovery-auth.ts +5 -0
- package/src/oauth/plan.ts +44 -0
- package/src/oauth/pool-kernel.ts +22 -8
- package/src/oauth/pool-settings-capability.ts +14 -3
- package/src/oauth/reauth-precheck.ts +27 -0
- package/src/oauth/store-anthropic-instance.ts +3 -1
- package/src/oauth/store.ts +121 -23
- package/src/oauth/types.ts +27 -2
- package/src/protocols/codecs/chat-ir.ts +7 -4
- package/src/protocols/codecs/gemini-interactions.ts +770 -0
- package/src/protocols/codecs/gemini.ts +470 -0
- package/src/protocols/contract.ts +2 -0
- package/src/protocols/encoders/adapter-events.ts +5 -3
- package/src/protocols/encoders/chat.ts +23 -7
- package/src/protocols/settings.ts +3 -0
- package/src/providers/account-quota-disk.ts +26 -8
- package/src/providers/api-key-pause.ts +2 -2
- package/src/providers/api-key-response-ownership.ts +14 -4
- package/src/providers/claude-cli-usage-state.ts +1 -0
- package/src/providers/claude-cli-usage.ts +24 -7
- package/src/providers/default-aliases.ts +11 -5
- package/src/providers/derive.ts +5 -1
- package/src/providers/fastwire.ts +6 -5
- package/src/providers/free-directory.ts +2 -2
- package/src/providers/google-vertex-location.ts +2 -1
- package/src/providers/google-vertex-model-discovery.ts +22 -0
- package/src/providers/key-failover.ts +18 -23
- package/src/providers/m365-copilot-discovery-fetch.ts +17 -0
- package/src/providers/m365-copilot-discovery.ts +68 -0
- package/src/providers/m365-copilot-models.ts +32 -0
- package/src/providers/model-rename-fields.ts +6 -0
- package/src/providers/new-model-policy-runtime.ts +27 -6
- package/src/providers/openai-forward-destination.ts +19 -0
- package/src/providers/openai-tiers-destination.ts +3 -19
- package/src/providers/quota/account-cache.ts +183 -27
- package/src/providers/quota/anthropic-account-quota.ts +21 -17
- package/src/providers/quota/anthropic-cooldown-recovery.ts +57 -27
- package/src/providers/quota/anthropic-decision-windows.ts +7 -3
- package/src/providers/quota/anthropic-usage-windows.ts +34 -4
- package/src/providers/quota/antigravity-account-probe.ts +165 -0
- package/src/providers/quota/antigravity.ts +71 -3
- package/src/providers/quota/vendor-probes-oauth.ts +52 -28
- package/src/providers/quota-decision-publication.ts +1 -1
- package/src/providers/quota-decision-snapshot.ts +4 -1
- package/src/providers/quota-types.ts +2 -0
- package/src/providers/quota.ts +8 -7
- package/src/providers/registry/agy-acp-models.ts +3 -0
- package/src/providers/registry/entries-core.ts +9 -0
- package/src/providers/registry/entries-extended.ts +60 -0
- package/src/providers/registry/model-seeds.ts +51 -0
- package/src/providers/request-pacing.ts +37 -7
- package/src/providers/service-tier.ts +2 -0
- package/src/providers/xai-privacy-mode.ts +52 -0
- package/src/remote/hub-management-relay.ts +74 -0
- package/src/responses/id-limits.ts +2 -0
- package/src/responses/input-media.ts +15 -3
- package/src/responses/parser-tools.ts +74 -11
- package/src/responses/parser.ts +16 -14
- package/src/responses/provider-opaque-metadata.ts +10 -0
- package/src/responses/reasoning-envelope.ts +3 -0
- package/src/responses/schema.ts +2 -3
- package/src/responses/spill-store.ts +5 -3
- package/src/responses/state/connection-scope.ts +232 -0
- package/src/responses/state/entry-types.ts +47 -0
- package/src/responses/state/snapshot-codec.ts +75 -14
- package/src/responses/state/snapshot-select.ts +2 -0
- package/src/responses/state/spill-codec.ts +59 -0
- package/src/responses/state/spill-queue.ts +11 -2
- package/src/responses/state.ts +178 -159
- package/src/router.ts +2 -6
- package/src/routing/compatibility/behavior.ts +6 -2
- package/src/routing/compatibility/subject.ts +17 -12
- package/src/routing/machine-weighted-routing.ts +58 -0
- package/src/server/advisor-admission-scope.ts +15 -7
- package/src/server/auth-cors.ts +27 -0
- package/src/server/chat-completions.ts +37 -17
- package/src/server/chat-native-eligibility.ts +21 -0
- package/src/server/chat-native.ts +15 -6
- package/src/server/claude-messages.ts +54 -6
- package/src/server/credential-recovery-attempt.ts +32 -0
- package/src/server/effort-policy.ts +6 -3
- package/src/server/gemini-inbound.ts +267 -0
- package/src/server/gemini-interactions-inbound.ts +229 -0
- package/src/server/gemini-interactions-store.ts +129 -0
- package/src/server/gemini-native-catalog.ts +64 -0
- package/src/server/gemini-native-log.ts +33 -0
- package/src/server/gemini-native-paths.ts +88 -0
- package/src/server/gemini-native-wire.ts +201 -0
- package/src/server/gemini-native.ts +218 -0
- package/src/server/grok-upstream-envelope-echo.ts +8 -2
- package/src/server/hub-relay-auth.ts +87 -0
- package/src/server/hub-usage-windows.ts +135 -0
- package/src/server/hub-usage.ts +16 -3
- package/src/server/inbound-body-admission.ts +2 -1
- package/src/server/index/anthropic-model-listing.ts +75 -0
- package/src/server/index/gemini-routes.ts +147 -0
- package/src/server/index/link-management.ts +3 -22
- package/src/server/index/link-relay-sessions.ts +71 -13
- package/src/server/index/link-websocket.ts +2 -0
- package/src/server/index/passive-management-read.ts +55 -0
- package/src/server/index/serve-options.ts +93 -107
- package/src/server/index/websocket-handler.ts +15 -2
- package/src/server/index.ts +10 -0
- package/src/server/inference/client-encoder-delivery.ts +2 -1
- package/src/server/inference/context.ts +33 -6
- package/src/server/management/agent-settings-routes.ts +33 -16
- package/src/server/management/claude-code-settings-patch.ts +6 -0
- package/src/server/management/combo-routes.ts +21 -1
- package/src/server/management/combo-suggestion-routes.ts +212 -0
- package/src/server/management/config-routes.ts +12 -4
- package/src/server/management/context.ts +2 -0
- package/src/server/management/gateway-client-routes.ts +62 -0
- package/src/server/management/integration-routes.ts +5 -0
- package/src/server/management/link-routes.ts +5 -1
- package/src/server/management/m365-copilot-provider-probe.ts +23 -0
- package/src/server/management/model-routes.ts +32 -2
- package/src/server/management/model-rows.ts +36 -3
- package/src/server/management/oauth-account-routes.ts +68 -15
- package/src/server/management/protocol-settings-patch.ts +1 -0
- package/src/server/management/provider-omitted-fields.ts +25 -0
- package/src/server/management/provider-overwrite-carry.ts +33 -8
- package/src/server/management/provider-routes.ts +10 -16
- package/src/server/management/request-history-routes.ts +2 -2
- package/src/server/management/route-registry.ts +10 -0
- package/src/server/management/shared.ts +118 -30
- package/src/server/management/sibling-guard.ts +2 -1
- package/src/server/management/system-routes.ts +6 -0
- package/src/server/management-auth.ts +8 -1
- package/src/server/messages-native-scope.ts +19 -0
- package/src/server/messages-native.ts +156 -59
- package/src/server/messages-shared.ts +6 -0
- package/src/server/models-capabilities.ts +24 -0
- package/src/server/native-chat-bridge.ts +259 -0
- package/src/server/ollama-inbound.ts +36 -184
- package/src/server/passive-route-linker.ts +3 -1
- package/src/server/port-reclaim.ts +48 -4
- package/src/server/proxy-liveness.ts +79 -22
- package/src/server/relay.ts +16 -9
- package/src/server/request-log-account-rotation.ts +2 -1
- package/src/server/request-log-attempt-recovery.ts +24 -2
- package/src/server/request-log-attempt-usage.ts +5 -4
- package/src/server/request-log-committed-terminal.ts +87 -0
- package/src/server/request-log-failure-attribution.ts +3 -3
- package/src/server/request-log-failure-detail.ts +34 -0
- package/src/server/request-log-key-failure.ts +27 -0
- package/src/server/request-log-terminal-status.ts +25 -12
- package/src/server/request-log.ts +59 -63
- package/src/server/responses/adapter-continuation.ts +77 -44
- package/src/server/responses/adapter-delivery.ts +47 -22
- package/src/server/responses/adapter-dispatch.ts +150 -149
- package/src/server/responses/advisor-retention.ts +12 -4
- package/src/server/responses/advisor-slot.ts +97 -2
- package/src/server/responses/agent-task-recovery.ts +44 -11
- package/src/server/responses/agy-acp-preflight.ts +41 -0
- package/src/server/responses/anthropic-image-context.ts +44 -0
- package/src/server/responses/antigravity-validation-refusal.ts +25 -1
- package/src/server/responses/buffered-sse-json.ts +18 -1
- package/src/server/responses/codex-ws-correlation.ts +5 -2
- package/src/server/responses/codex-ws-exchange.ts +91 -8
- package/src/server/responses/codex-ws-incremental.ts +136 -0
- package/src/server/responses/codex-ws-pool.ts +22 -8
- package/src/server/responses/codex-ws-request.ts +5 -0
- package/src/server/responses/codex-ws-send-accounting.ts +8 -0
- package/src/server/responses/codex-ws-session.ts +14 -0
- package/src/server/responses/codex-ws-wire.ts +44 -22
- package/src/server/responses/combo-admission-snapshot.ts +93 -0
- package/src/server/responses/combo-required-tool-stream.ts +16 -4
- package/src/server/responses/combo-selection-keepalive.ts +5 -0
- package/src/server/responses/combo-stream-preflight.ts +182 -73
- package/src/server/responses/compact-response-body.ts +27 -0
- package/src/server/responses/compact.ts +174 -121
- package/src/server/responses/compaction-recovery.ts +7 -0
- package/src/server/responses/core-codex-account.ts +88 -62
- package/src/server/responses/core-combo-failure.ts +15 -0
- package/src/server/responses/core-combo-native.ts +12 -1
- package/src/server/responses/core-combo.ts +340 -94
- package/src/server/responses/core-normalize.ts +19 -12
- package/src/server/responses/core-opaque-recovery.ts +15 -4
- package/src/server/responses/core-options.ts +50 -5
- package/src/server/responses/core-pipeline.ts +11 -0
- package/src/server/responses/core.ts +7 -1
- package/src/server/responses/empty-completion-guard.ts +21 -7
- package/src/server/responses/estimated-usage.ts +2 -1
- package/src/server/responses/fetch-helpers.ts +95 -19
- package/src/server/responses/history-slim.ts +204 -0
- package/src/server/responses/hosted-advisor-spend.ts +49 -0
- package/src/server/responses/image-context-recovery.ts +199 -0
- package/src/server/responses/input-admission.ts +36 -11
- package/src/server/responses/jev-model-invoke.ts +23 -16
- package/src/server/responses/non-replayable-error.ts +12 -2
- package/src/server/responses/passthrough-build-error.ts +5 -3
- package/src/server/responses/passthrough-delivery.ts +74 -33
- package/src/server/responses/passthrough-dispatch.ts +170 -197
- package/src/server/responses/policy-fallback.ts +7 -2
- package/src/server/responses/policy-request-scope.ts +36 -2
- package/src/server/responses/replay-owner.ts +20 -0
- package/src/server/responses/request-prepare.ts +97 -16
- package/src/server/responses/request-send-budget.ts +97 -25
- package/src/server/responses/request-sidecar-auth.ts +138 -65
- package/src/server/responses/request-spend-target.ts +13 -1
- package/src/server/responses/request-spend.ts +228 -47
- package/src/server/responses/request-transport.ts +395 -70
- package/src/server/responses/run-turn-execution.ts +40 -9
- package/src/server/responses/sidecar-execution.ts +214 -67
- package/src/server/responses/sidecar-send-budget.ts +9 -9
- package/src/server/responses/terminal-guard.ts +35 -9
- package/src/server/responses/ws-upstream.ts +27 -6
- package/src/server/responses-phase-inference.ts +32 -9
- package/src/server/responses-terminal-repair.ts +14 -35
- package/src/server/system-env.ts +8 -5
- package/src/server/ws-bridge.ts +23 -17
- package/src/service/health-sentinel-linux.ts +16 -7
- package/src/service/systemd.ts +11 -2
- package/src/service/windows-checked-stop-live.ts +60 -55
- package/src/service/windows-checked-stop.ts +29 -11
- package/src/service/windows-guarded-manager.ts +14 -26
- package/src/service/windows-process-evidence.ts +82 -74
- package/src/service/windows-process-native.ts +69 -20
- package/src/service/windows-service-stop.ts +14 -12
- package/src/service/windows-stop-binding.ts +14 -5
- package/src/service/windows-stop-confirm.ts +1 -1
- package/src/service/windows-task-instance-stop.ts +98 -0
- package/src/service/windows-taskxml.ts +30 -7
- package/src/sidecar/anthropic-binding.ts +1 -1
- package/src/stall-timeout.ts +30 -0
- package/src/telemetry/aggregate.ts +6 -3
- package/src/types/account-auto-pause.ts +19 -1
- package/src/types/accounts.ts +21 -0
- package/src/types/anthropic-account-pool.ts +1 -1
- package/src/types/combo-capability-policy.ts +19 -0
- package/src/types/config.ts +22 -2
- package/src/types/provider.ts +42 -8
- package/src/types/request.ts +15 -0
- package/src/types/tools.ts +22 -5
- package/src/types/wire.ts +1 -0
- package/src/types.ts +4 -0
- package/src/update/check-types.ts +2 -0
- package/src/update/job.ts +8 -3
- package/src/update/managed-release.ts +6 -0
- package/src/update/orchestrated-entry.ts +13 -4
- package/src/update/refresh-scheduler.ts +17 -7
- package/src/usage/attempt-delivery.ts +6 -0
- package/src/usage/cache-provenance.ts +71 -0
- package/src/usage/cost.ts +216 -27
- package/src/usage/credit-billing.ts +88 -0
- package/src/usage/jev-stats.ts +13 -9
- package/src/usage/log.ts +5 -33
- package/src/usage/summary.ts +8 -43
- package/src/usage/user-cost-overlays.ts +13 -1
- package/src/vision/index.ts +8 -3
- package/src/vision/plan.ts +28 -14
- package/src/vision/routed-describe.ts +24 -36
- package/src/web-search/header-deadline.ts +14 -3
- package/src/web-search/index.ts +6 -4
- package/src/web-search/loop.ts +240 -78
- package/src/web-search/progress-stream.ts +29 -2
- package/src/web-search/run-turn-loop.ts +5 -1
- package/gui/dist/assets/App-BGP8I0V4.css +0 -1
- package/gui/dist/assets/App-BRExzhwL.js +0 -60
- package/gui/dist/assets/Tray-DHCWAn8V.js +0 -1
- package/gui/dist/assets/brand-mark-C70PuQ3j.js +0 -1
- package/gui/dist/assets/index-DJxM-imn.css +0 -2
- package/gui/dist/assets/index-Dy1jIGty.js +0 -138
- package/gui/dist/classic/assets/App-B4j-Txmb.css +0 -1
- package/gui/dist/classic/assets/App-CaC-E6Vl.js +0 -59
- package/gui/dist/classic/assets/Tray-BG253fiL.js +0 -1
- package/gui/dist/classic/assets/index-CR_AThOp.js +0 -138
- package/gui/dist/classic/assets/quota-summary-B7KQPpEC.js +0 -1
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure Gemini Interactions API request and Chat Completions response projections.
|
|
3
|
+
*
|
|
4
|
+
* Request side: an Interactions create body becomes one Chat Completions body. Unknown fields are
|
|
5
|
+
* ignored (the API grows them often); a field this proxy cannot honor and that would change the
|
|
6
|
+
* answer (an agent, background execution, a hosted tool, non-text output, safety_settings) is a
|
|
7
|
+
* typed 400. The routed provider applies its own safety policy. Response side: a buffered Chat
|
|
8
|
+
* completion becomes an Interaction resource, and a
|
|
9
|
+
* Chat SSE chunk sequence becomes the Interactions SSE vocabulary (interaction.created, step.start,
|
|
10
|
+
* step.delta, step.stop, interaction.completed). Field names follow
|
|
11
|
+
* https://ai.google.dev/api/interactions-api; usage uses its total_* names.
|
|
12
|
+
*/
|
|
13
|
+
import { isTranslatorBudgetExceededError, type TranslatorBudget } from "../../lib/translator-budget";
|
|
14
|
+
import { GeminiCodecError, MAX_GEMINI_TOOL_CALLS, normalizeSchema } from "./gemini";
|
|
15
|
+
|
|
16
|
+
type Obj = Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
export class InteractionsCodecError extends Error {
|
|
19
|
+
readonly status = 400;
|
|
20
|
+
readonly code = "invalid_interactions_request";
|
|
21
|
+
constructor(message: string) { super(message); this.name = "InteractionsCodecError"; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function fail(message: string): never { throw new InteractionsCodecError(message); }
|
|
25
|
+
const isObj = (value: unknown): value is Obj => !!value && typeof value === "object" && !Array.isArray(value);
|
|
26
|
+
function object(value: unknown, label: string): Obj {
|
|
27
|
+
if (!isObj(value)) fail(`Invalid ${label}`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function array(value: unknown, label: string): unknown[] {
|
|
31
|
+
if (!Array.isArray(value)) fail(`Invalid ${label}`);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
function text(value: unknown, label: string, nonempty = false): string {
|
|
35
|
+
if (typeof value !== "string" || (nonempty && !value.trim())) fail(`Invalid ${label}`);
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function finiteNumber(value: unknown, label: string): number {
|
|
39
|
+
if (typeof value !== "number" || !Number.isFinite(value)) fail(`Invalid ${label}`);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Lower-case schema types with the generateContent codec's rule, reporting failures as this codec's 400. */
|
|
44
|
+
function schema(value: unknown): unknown {
|
|
45
|
+
try { return normalizeSchema(value); } catch (error) {
|
|
46
|
+
if (error instanceof GeminiCodecError) fail(error.message);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface InteractionsRequestPlan {
|
|
52
|
+
model: string;
|
|
53
|
+
stream: boolean;
|
|
54
|
+
store: boolean;
|
|
55
|
+
previousInteractionId?: string;
|
|
56
|
+
/** function_result call ids with no function_call earlier in this input. */
|
|
57
|
+
unmatchedResultIds: string[];
|
|
58
|
+
/** System messages built from system_instruction and developer/system turns, in order. */
|
|
59
|
+
systemMessages: Obj[];
|
|
60
|
+
/** The conversation messages this request adds (no system role). */
|
|
61
|
+
inputMessages: Obj[];
|
|
62
|
+
/** The input as Interactions steps, kept for a stored interaction's GET resource. */
|
|
63
|
+
inputSteps: Obj[];
|
|
64
|
+
/** The Chat body without messages; the handler sets messages. */
|
|
65
|
+
chat: Obj;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const BASE64 = /^[A-Za-z0-9+/_-]+={0,2}$/;
|
|
69
|
+
|
|
70
|
+
/** One Interactions content item as a Chat content part. Text and inline base64 images only. */
|
|
71
|
+
function contentPart(raw: unknown): Obj {
|
|
72
|
+
const item = object(raw, "content");
|
|
73
|
+
const type = item.type === undefined && typeof item.text === "string" ? "text" : text(item.type, "content type");
|
|
74
|
+
if (type === "text") return { type: "text", text: text(item.text, "text content") };
|
|
75
|
+
if (type === "image") {
|
|
76
|
+
if (item.data === undefined) fail("unsupported content: image without inline data");
|
|
77
|
+
const mime = text(item.mime_type ?? item.mimeType, "image mime_type", true).toLowerCase();
|
|
78
|
+
if (!/^image\/[a-z0-9.+-]+$/.test(mime)) fail("unsupported content: image mime_type");
|
|
79
|
+
const data = text(item.data, "image data", true);
|
|
80
|
+
if (!BASE64.test(data)) fail("Invalid image data encoding");
|
|
81
|
+
return { type: "image_url", image_url: { url: `data:${mime};base64,${data}` } };
|
|
82
|
+
}
|
|
83
|
+
return fail(`unsupported content type: ${type}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Chat user content: a string when text-only, parts when an image is present. */
|
|
87
|
+
function userContent(parts: Obj[]): string | Obj[] {
|
|
88
|
+
return parts.some(part => part.type !== "text") ? parts : parts.map(part => part.text as string).join("\n");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function contentList(value: unknown, label: string): Obj[] {
|
|
92
|
+
if (typeof value === "string") return [{ type: "text", text: value }];
|
|
93
|
+
if (isObj(value)) return [contentPart(value)];
|
|
94
|
+
return array(value, label).map(contentPart);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function contentText(value: unknown, label: string): string {
|
|
98
|
+
return contentList(value, label).map(part => {
|
|
99
|
+
if (part.type !== "text") fail(`unsupported content in ${label}`);
|
|
100
|
+
return part.text as string;
|
|
101
|
+
}).join("");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function systemInstructionText(value: unknown): string {
|
|
105
|
+
if (typeof value === "string") return value;
|
|
106
|
+
const source = object(value, "system_instruction");
|
|
107
|
+
if (typeof source.text === "string") return source.text;
|
|
108
|
+
if (source.parts !== undefined) return array(source.parts, "system_instruction parts").map(part => text(object(part, "system_instruction part").text, "system_instruction text")).join("\n");
|
|
109
|
+
return fail("Invalid system_instruction");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const STEP_TYPES = new Set(["user_input", "model_output", "thought", "function_call", "function_result"]);
|
|
113
|
+
const CONTENT_TYPES = new Set(["text", "image", "audio", "video", "document"]);
|
|
114
|
+
|
|
115
|
+
interface InputState {
|
|
116
|
+
system: Obj[];
|
|
117
|
+
messages: Obj[];
|
|
118
|
+
steps: Obj[];
|
|
119
|
+
assistant?: { content: string; calls: Obj[] };
|
|
120
|
+
pendingSignature?: string;
|
|
121
|
+
calls: Set<string>;
|
|
122
|
+
unmatched: string[];
|
|
123
|
+
callCount: number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function flushAssistant(state: InputState): void {
|
|
127
|
+
const pending = state.assistant;
|
|
128
|
+
if (!pending) return;
|
|
129
|
+
state.assistant = undefined;
|
|
130
|
+
if (!pending.content && !pending.calls.length) return;
|
|
131
|
+
state.messages.push({ role: "assistant", content: pending.content || null, ...(pending.calls.length ? { tool_calls: pending.calls } : {}) });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function assistantOf(state: InputState): { content: string; calls: Obj[] } {
|
|
135
|
+
state.assistant ??= { content: "", calls: [] };
|
|
136
|
+
return state.assistant;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function resultText(value: unknown): string {
|
|
140
|
+
if (typeof value === "string") return value;
|
|
141
|
+
if (Array.isArray(value)) return contentText(value, "function_result result");
|
|
142
|
+
if (isObj(value)) return JSON.stringify(value);
|
|
143
|
+
return fail("Invalid function_result result");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function applyStep(state: InputState, raw: unknown): void {
|
|
147
|
+
const step = object(raw, "step");
|
|
148
|
+
const role = step.role === undefined ? undefined : text(step.role, "role");
|
|
149
|
+
const type = step.type === undefined && role !== undefined ? "turn" : text(step.type, "step type");
|
|
150
|
+
if (type === "turn") {
|
|
151
|
+
const content = step.content ?? step.parts;
|
|
152
|
+
if (role === "user") {
|
|
153
|
+
flushAssistant(state);
|
|
154
|
+
state.messages.push({ role: "user", content: userContent(contentList(content, "turn content")) });
|
|
155
|
+
state.steps.push({ type: "user_input", content: contentList(content, "turn content").map(part => part.type === "text" ? part : { type: "image" }) });
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (role === "model" || role === "assistant") {
|
|
159
|
+
const text = contentText(content, "turn content");
|
|
160
|
+
assistantOf(state).content += text;
|
|
161
|
+
state.steps.push({ type: "model_output", content: [{ type: "text", text }] });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// A conversation-level instruction applies from its position on, so it stays in the sequence;
|
|
165
|
+
// only the request-level system_instruction leads the conversation.
|
|
166
|
+
if (role === "system" || role === "developer") { flushAssistant(state); state.messages.push({ role: "system", content: contentText(content, "turn content") }); return; }
|
|
167
|
+
return fail(`unsupported role: ${role}`);
|
|
168
|
+
}
|
|
169
|
+
if (!STEP_TYPES.has(type)) fail(`unsupported step type: ${type}`);
|
|
170
|
+
state.steps.push({ ...step });
|
|
171
|
+
if (type === "user_input") {
|
|
172
|
+
flushAssistant(state);
|
|
173
|
+
state.messages.push({ role: "user", content: userContent(contentList(step.content ?? [], "user_input content")) });
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (type === "model_output") { assistantOf(state).content += contentText(step.content ?? [], "model_output content"); return; }
|
|
177
|
+
if (type === "thought") {
|
|
178
|
+
// Thought summaries are not conversation text. A signature belongs to the next function call.
|
|
179
|
+
const signature = step.signature;
|
|
180
|
+
if (signature !== undefined) state.pendingSignature = text(signature, "thought signature", true);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (type === "function_call") {
|
|
184
|
+
if (++state.callCount > MAX_GEMINI_TOOL_CALLS) fail("Too many function calls");
|
|
185
|
+
const id = text(step.id, "function_call id", true);
|
|
186
|
+
if (state.calls.has(id)) fail("Duplicate function_call id");
|
|
187
|
+
const name = text(step.name, "function_call name", true);
|
|
188
|
+
const args = step.arguments === undefined ? {} : object(step.arguments, "function_call arguments");
|
|
189
|
+
const signature = state.pendingSignature;
|
|
190
|
+
state.pendingSignature = undefined;
|
|
191
|
+
state.calls.add(id);
|
|
192
|
+
assistantOf(state).calls.push({
|
|
193
|
+
id, type: "function", function: { name, arguments: JSON.stringify(args) },
|
|
194
|
+
...(signature ? { extra_content: { google: { thought_signature: signature } } } : {}),
|
|
195
|
+
});
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// function_result
|
|
199
|
+
flushAssistant(state);
|
|
200
|
+
const callId = text(step.call_id, "function_result call_id", true);
|
|
201
|
+
if (!state.calls.has(callId)) state.unmatched.push(callId);
|
|
202
|
+
const body = resultText(step.result);
|
|
203
|
+
if (step.is_error !== undefined && typeof step.is_error !== "boolean") fail("Invalid function_result is_error");
|
|
204
|
+
state.messages.push({ role: "tool", tool_call_id: callId, content: step.is_error === true ? JSON.stringify({ error: body }) : body });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function isContentItem(value: unknown): boolean {
|
|
208
|
+
return isObj(value) && ((typeof value.type === "string" && CONTENT_TYPES.has(value.type)) || (value.type === undefined && value.role === undefined && typeof value.text === "string"));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function inputMessages(input: unknown, state: InputState): void {
|
|
212
|
+
if (typeof input === "string") {
|
|
213
|
+
state.messages.push({ role: "user", content: input });
|
|
214
|
+
state.steps.push({ type: "user_input", content: [{ type: "text", text: input }] });
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (isContentItem(input)) {
|
|
218
|
+
state.messages.push({ role: "user", content: userContent([contentPart(input)]) });
|
|
219
|
+
state.steps.push({ type: "user_input", content: [{ ...(input as Obj) }] });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (isObj(input)) { applyStep(state, input); flushAssistant(state); return; }
|
|
223
|
+
const items = array(input, "input");
|
|
224
|
+
if (items.length && items.every(isContentItem)) {
|
|
225
|
+
state.messages.push({ role: "user", content: userContent(items.map(contentPart)) });
|
|
226
|
+
state.steps.push({ type: "user_input", content: items.map(item => ({ ...(item as Obj) })) });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const item of items) {
|
|
230
|
+
if (isContentItem(item)) fail("input mixes content items with steps");
|
|
231
|
+
applyStep(state, item);
|
|
232
|
+
}
|
|
233
|
+
flushAssistant(state);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function functionTools(value: unknown): Obj[] {
|
|
237
|
+
return array(value, "tools").map(raw => {
|
|
238
|
+
const tool = object(raw, "tool");
|
|
239
|
+
const type = text(tool.type, "tool type");
|
|
240
|
+
if (type !== "function") fail(`unsupported tool type: ${type}`);
|
|
241
|
+
const name = text(tool.name, "function name", true);
|
|
242
|
+
if (tool.description !== undefined) text(tool.description, "function description");
|
|
243
|
+
const parameters = tool.parameters ?? { type: "object", properties: {} };
|
|
244
|
+
object(parameters, "function parameters");
|
|
245
|
+
return { type: "function", function: { name, ...(typeof tool.description === "string" ? { description: tool.description } : {}), parameters: schema(parameters) } };
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function toolChoice(value: unknown): unknown {
|
|
250
|
+
if (typeof value === "string") {
|
|
251
|
+
if (value === "auto" || value === "validated") return undefined;
|
|
252
|
+
if (value === "any") return "required";
|
|
253
|
+
if (value === "none") return "none";
|
|
254
|
+
return fail("Unsupported tool_choice");
|
|
255
|
+
}
|
|
256
|
+
const allowed = object(object(value, "tool_choice").allowed_tools, "tool_choice allowed_tools");
|
|
257
|
+
const mode = allowed.mode === undefined ? "auto" : text(allowed.mode, "allowed_tools mode");
|
|
258
|
+
const names = allowed.tools === undefined ? [] : array(allowed.tools, "allowed_tools tools").map(name => text(name, "allowed tool name", true));
|
|
259
|
+
if (mode === "none") return "none";
|
|
260
|
+
const entries = names.map(name => ({ type: "function", function: { name } }));
|
|
261
|
+
if (mode === "auto" || mode === "validated") return entries.length ? { type: "allowed_tools", allowed_tools: { mode: "auto", tools: entries } } : undefined;
|
|
262
|
+
if (mode === "any") {
|
|
263
|
+
// Same semantics as the generateContent codec: one allowed function is a forced call.
|
|
264
|
+
if (entries.length === 1) return entries[0];
|
|
265
|
+
return entries.length ? { type: "allowed_tools", allowed_tools: { mode: "required", tools: entries } } : "required";
|
|
266
|
+
}
|
|
267
|
+
return fail("Unsupported tool_choice mode");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const THINKING_LEVELS = new Set(["minimal", "low", "medium", "high", "xhigh"]);
|
|
271
|
+
|
|
272
|
+
function generationConfig(raw: unknown, chat: Obj): unknown {
|
|
273
|
+
if (raw === undefined) return undefined;
|
|
274
|
+
const config = object(raw, "generation_config");
|
|
275
|
+
for (const [key, chatKey] of [["temperature", "temperature"], ["top_p", "top_p"]] as const) {
|
|
276
|
+
if (config[key] !== undefined) chat[chatKey] = finiteNumber(config[key], key);
|
|
277
|
+
}
|
|
278
|
+
if (config.seed !== undefined) {
|
|
279
|
+
if (typeof config.seed !== "number" || !Number.isSafeInteger(config.seed)) fail("Invalid seed");
|
|
280
|
+
chat.seed = config.seed;
|
|
281
|
+
}
|
|
282
|
+
if (config.stop_sequences !== undefined) chat.stop = array(config.stop_sequences, "stop_sequences").map(stop => text(stop, "stop sequence"));
|
|
283
|
+
if (config.max_output_tokens !== undefined) {
|
|
284
|
+
const max = config.max_output_tokens;
|
|
285
|
+
if (typeof max !== "number" || !Number.isSafeInteger(max) || max < 1) fail("Invalid max_output_tokens");
|
|
286
|
+
chat.max_tokens = max;
|
|
287
|
+
}
|
|
288
|
+
if (config.thinking_level !== undefined) {
|
|
289
|
+
const level = text(config.thinking_level, "thinking_level").toLowerCase();
|
|
290
|
+
if (!THINKING_LEVELS.has(level)) fail("Invalid thinking_level");
|
|
291
|
+
chat.reasoning_effort = level;
|
|
292
|
+
}
|
|
293
|
+
if (config.thinking_summaries !== undefined) {
|
|
294
|
+
const summaries = text(config.thinking_summaries, "thinking_summaries");
|
|
295
|
+
if (summaries !== "auto" && summaries !== "none") fail("Invalid thinking_summaries");
|
|
296
|
+
// An explicit "none" must reach the Chat conversion, whose default is to summarize.
|
|
297
|
+
chat.include_reasoning = summaries === "auto";
|
|
298
|
+
}
|
|
299
|
+
return config.tool_choice;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function responseFormat(value: unknown): Obj | undefined {
|
|
303
|
+
const formats = Array.isArray(value) ? value : [value];
|
|
304
|
+
if (formats.length !== 1) fail("response_format must name one text format");
|
|
305
|
+
const format = object(formats[0], "response_format");
|
|
306
|
+
const type = format.type === undefined ? "text" : text(format.type, "response_format type");
|
|
307
|
+
if (type !== "text") fail(`unsupported response_format type: ${type}`);
|
|
308
|
+
const mime = format.mime_type === undefined ? undefined : text(format.mime_type, "response_format mime_type");
|
|
309
|
+
if (mime !== undefined && mime !== "application/json" && mime !== "text/plain") fail("unsupported response_format mime_type");
|
|
310
|
+
if (mime === "text/plain" || (mime === undefined && format.schema === undefined)) return undefined;
|
|
311
|
+
if (format.schema === undefined) return { type: "json_object" };
|
|
312
|
+
return { type: "json_schema", json_schema: { name: "response", schema: schema(object(format.schema, "response_format schema")) } };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const UNSUPPORTED_FIELDS = ["webhook_config", "environment", "agent_config", "safety_settings"] as const;
|
|
316
|
+
|
|
317
|
+
/** Project an Interactions create body onto a Chat Completions plan. */
|
|
318
|
+
export function interactionsToChat(input: unknown): InteractionsRequestPlan {
|
|
319
|
+
const body = object(input, "request");
|
|
320
|
+
const hasModel = body.model !== undefined;
|
|
321
|
+
const hasAgent = body.agent !== undefined;
|
|
322
|
+
if (hasModel === hasAgent) fail("request requires exactly one of model or agent");
|
|
323
|
+
if (hasAgent) fail("agent interactions require a native Interactions upstream");
|
|
324
|
+
const rawModel = text(body.model, "model", true).trim();
|
|
325
|
+
const model = rawModel.startsWith("models/") ? rawModel.slice("models/".length) : rawModel;
|
|
326
|
+
if (!model) fail("Invalid model");
|
|
327
|
+
if (body.stream !== undefined && typeof body.stream !== "boolean") fail("stream must be a boolean");
|
|
328
|
+
if (body.store !== undefined && typeof body.store !== "boolean") fail("store must be a boolean");
|
|
329
|
+
if (body.background !== undefined && typeof body.background !== "boolean") fail("background must be a boolean");
|
|
330
|
+
if (body.background === true) fail("background interactions are not supported");
|
|
331
|
+
// Fields that would change where or how the interaction runs; ignoring them would change the answer.
|
|
332
|
+
for (const field of UNSUPPORTED_FIELDS) if (body[field] !== undefined) fail(`${field} is not supported`);
|
|
333
|
+
const previous = body.previous_interaction_id === undefined ? undefined : text(body.previous_interaction_id, "previous_interaction_id", true);
|
|
334
|
+
if (body.response_modalities !== undefined) {
|
|
335
|
+
const modalities = array(body.response_modalities, "response_modalities").map(value => text(value, "response modality").toLowerCase());
|
|
336
|
+
if (modalities.some(value => value !== "text")) fail("only text output is supported");
|
|
337
|
+
}
|
|
338
|
+
if (body.input === undefined) fail("Invalid input");
|
|
339
|
+
const state: InputState = { system: [], messages: [], steps: [], calls: new Set(), unmatched: [], callCount: 0 };
|
|
340
|
+
if (body.system_instruction !== undefined) {
|
|
341
|
+
const instruction = systemInstructionText(body.system_instruction);
|
|
342
|
+
if (instruction) state.system.push({ role: "system", content: instruction });
|
|
343
|
+
}
|
|
344
|
+
inputMessages(body.input, state);
|
|
345
|
+
const stream = body.stream === true;
|
|
346
|
+
const chat: Obj = { model, stream };
|
|
347
|
+
if (body.tools !== undefined) {
|
|
348
|
+
const tools = functionTools(body.tools);
|
|
349
|
+
if (tools.length) chat.tools = tools;
|
|
350
|
+
}
|
|
351
|
+
const configChoice = generationConfig(body.generation_config, chat);
|
|
352
|
+
const choiceSource = body.tool_choice ?? configChoice;
|
|
353
|
+
if (choiceSource !== undefined && chat.tools !== undefined) {
|
|
354
|
+
const choice = toolChoice(choiceSource);
|
|
355
|
+
if (choice !== undefined) chat.tool_choice = choice;
|
|
356
|
+
}
|
|
357
|
+
if (body.response_format !== undefined) {
|
|
358
|
+
const format = responseFormat(body.response_format);
|
|
359
|
+
if (format) chat.response_format = format;
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
model, stream, store: body.store !== false,
|
|
363
|
+
...(previous !== undefined ? { previousInteractionId: previous } : {}),
|
|
364
|
+
unmatchedResultIds: state.unmatched,
|
|
365
|
+
systemMessages: state.system,
|
|
366
|
+
inputMessages: state.messages,
|
|
367
|
+
inputSteps: state.steps,
|
|
368
|
+
chat,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export interface InteractionMeta { id: string; model: string; created: string; previousInteractionId?: string }
|
|
373
|
+
|
|
374
|
+
/** ocx-int- plus 128 random bits as lower-case hex. */
|
|
375
|
+
export function newInteractionId(): string {
|
|
376
|
+
return "ocx-int-" + Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString("hex");
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function interactionTimestamp(at = Date.now()): string {
|
|
380
|
+
return new Date(Math.floor(at / 1000) * 1000).toISOString().replace(".000Z", "Z");
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const STATUS_BY_FINISH: Readonly<Record<string, string>> = {
|
|
384
|
+
stop: "completed",
|
|
385
|
+
tool_calls: "requires_action",
|
|
386
|
+
function_call: "requires_action",
|
|
387
|
+
length: "incomplete",
|
|
388
|
+
content_filter: "incomplete",
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
function interactionStatus(finish: unknown): string | undefined {
|
|
392
|
+
return typeof finish === "string" && Object.hasOwn(STATUS_BY_FINISH, finish) ? STATUS_BY_FINISH[finish] : undefined;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function nonNegativeInt(value: unknown): number | undefined {
|
|
396
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Chat usage as Interactions usage (Google's total_* names; output excludes thoughts). */
|
|
400
|
+
export function interactionUsage(value: unknown): Obj | undefined {
|
|
401
|
+
if (!isObj(value)) return undefined;
|
|
402
|
+
const prompt = nonNegativeInt(value.prompt_tokens);
|
|
403
|
+
const completion = nonNegativeInt(value.completion_tokens);
|
|
404
|
+
if (prompt === undefined && completion === undefined) return undefined;
|
|
405
|
+
const details = isObj(value.completion_tokens_details) ? value.completion_tokens_details : {};
|
|
406
|
+
const promptDetails = isObj(value.prompt_tokens_details) ? value.prompt_tokens_details : {};
|
|
407
|
+
const thoughts = nonNegativeInt(details.reasoning_tokens) ?? 0;
|
|
408
|
+
return {
|
|
409
|
+
total_input_tokens: prompt ?? 0,
|
|
410
|
+
total_cached_tokens: nonNegativeInt(promptDetails.cached_tokens) ?? 0,
|
|
411
|
+
total_output_tokens: Math.max(0, (completion ?? 0) - thoughts),
|
|
412
|
+
total_thought_tokens: thoughts,
|
|
413
|
+
total_tokens: (prompt ?? 0) + (completion ?? 0),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function callArguments(raw: unknown): Obj {
|
|
418
|
+
let args: unknown = {};
|
|
419
|
+
if (typeof raw === "string") {
|
|
420
|
+
if (raw.trim()) { try { args = JSON.parse(raw); } catch { throw new Error("Invalid Chat tool arguments"); } }
|
|
421
|
+
} else if (raw !== undefined && raw !== null) args = raw;
|
|
422
|
+
if (!isObj(args)) throw new Error("Invalid Chat tool arguments");
|
|
423
|
+
return args;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function callSignature(call: Obj): string | undefined {
|
|
427
|
+
const extra = call.extra_content;
|
|
428
|
+
if (!isObj(extra) || !isObj(extra.google)) return undefined;
|
|
429
|
+
const signature = extra.google.thought_signature;
|
|
430
|
+
return typeof signature === "string" && signature ? signature : undefined;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** The output steps of one assistant answer: thought, model_output, then function calls. */
|
|
434
|
+
function outputSteps(reasoning: string, content: string, calls: { id: string; name: string; arguments: Obj; signature?: string }[]): Obj[] {
|
|
435
|
+
const steps: Obj[] = [];
|
|
436
|
+
if (reasoning) steps.push({ type: "thought", summary: [{ type: "text", text: reasoning }] });
|
|
437
|
+
if (content) steps.push({ type: "model_output", content: [{ type: "text", text: content }] });
|
|
438
|
+
for (const call of calls) {
|
|
439
|
+
if (call.signature) steps.push({ type: "thought", signature: call.signature });
|
|
440
|
+
steps.push({ type: "function_call", id: call.id, name: call.name, arguments: call.arguments });
|
|
441
|
+
}
|
|
442
|
+
return steps;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** The assistant message a stored interaction keeps for the next turn. */
|
|
446
|
+
export interface InteractionAnswer { reasoning: string; content: string; calls: { id: string; name: string; arguments: Obj; signature?: string }[]; status: string; usage?: Obj }
|
|
447
|
+
|
|
448
|
+
export function interactionAssistantMessage(answer: InteractionAnswer): Obj | undefined {
|
|
449
|
+
if (!answer.content && !answer.calls.length) return undefined;
|
|
450
|
+
return {
|
|
451
|
+
role: "assistant",
|
|
452
|
+
content: answer.content || null,
|
|
453
|
+
...(answer.calls.length ? { tool_calls: answer.calls.map(call => ({
|
|
454
|
+
id: call.id, type: "function", function: { name: call.name, arguments: JSON.stringify(call.arguments) },
|
|
455
|
+
...(call.signature ? { extra_content: { google: { thought_signature: call.signature } } } : {}),
|
|
456
|
+
})) } : {}),
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export function interactionResource(meta: InteractionMeta, answer: InteractionAnswer, updated = meta.created): Obj {
|
|
461
|
+
return {
|
|
462
|
+
id: meta.id, object: "interaction", status: answer.status, created: meta.created, updated, model: meta.model,
|
|
463
|
+
...(meta.previousInteractionId ? { previous_interaction_id: meta.previousInteractionId } : {}),
|
|
464
|
+
steps: outputSteps(answer.reasoning, answer.content, answer.calls),
|
|
465
|
+
...(answer.usage ? { usage: answer.usage } : {}),
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** A complete Chat completion as the answer of one interaction. Throws when it did not finish cleanly. */
|
|
470
|
+
export function chatCompletionAnswer(input: unknown): InteractionAnswer {
|
|
471
|
+
const body = object(input, "Chat response");
|
|
472
|
+
if (!Array.isArray(body.choices) || body.choices.length !== 1) throw new Error("Invalid Chat choices");
|
|
473
|
+
const choice = object(body.choices[0], "Chat choice");
|
|
474
|
+
const status = interactionStatus(choice.finish_reason);
|
|
475
|
+
if (!status) throw new Error("Chat completion did not finish successfully");
|
|
476
|
+
const message = object(choice.message, "Chat message");
|
|
477
|
+
const thought = message.reasoning_content ?? message.reasoning;
|
|
478
|
+
if (message.content != null && typeof message.content !== "string") throw new Error("Invalid Chat content");
|
|
479
|
+
const calls: InteractionAnswer["calls"] = [];
|
|
480
|
+
if (message.tool_calls !== undefined) {
|
|
481
|
+
if (!Array.isArray(message.tool_calls)) throw new Error("Invalid Chat tool calls");
|
|
482
|
+
message.tool_calls.forEach((raw, index) => {
|
|
483
|
+
const call = object(raw, "Chat tool call");
|
|
484
|
+
const fn = object(call.function, "Chat tool function");
|
|
485
|
+
const signature = callSignature(call);
|
|
486
|
+
calls.push({ id: typeof call.id === "string" && call.id ? call.id : `call_interaction_${index}`, name: text(fn.name, "tool name", true), arguments: callArguments(fn.arguments), ...(signature ? { signature } : {}) });
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
const usage = interactionUsage(body.usage);
|
|
490
|
+
return { reasoning: typeof thought === "string" ? thought : "", content: (message.content as string | null | undefined) ?? "", calls, status, ...(usage ? { usage } : {}) };
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export interface InteractionsSseEvent { event: string; data: Obj }
|
|
494
|
+
|
|
495
|
+
type StreamTool = { id?: string; name: string; arguments: string; signature?: string };
|
|
496
|
+
type RetainedAnswer = Pick<InteractionAnswer, "reasoning" | "content" | "calls"> & {
|
|
497
|
+
bytes: number;
|
|
498
|
+
remainingEntryBytes: number;
|
|
499
|
+
};
|
|
500
|
+
const RETAINED_SCOPE = { kind: "retained_collectors" } as const;
|
|
501
|
+
|
|
502
|
+
export interface InteractionsStreamState {
|
|
503
|
+
meta: InteractionMeta;
|
|
504
|
+
budget: TranslatorBudget;
|
|
505
|
+
created: boolean;
|
|
506
|
+
index: number;
|
|
507
|
+
open?: "thought" | "model_output";
|
|
508
|
+
retained?: RetainedAnswer;
|
|
509
|
+
finishReason?: unknown;
|
|
510
|
+
usage?: Obj;
|
|
511
|
+
tools: Map<number, StreamTool>;
|
|
512
|
+
toolMetadataBytes: number;
|
|
513
|
+
toolsEmitted: boolean;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export function createInteractionsStreamState(meta: InteractionMeta, budget: TranslatorBudget, remainingEntryBytes = 0): InteractionsStreamState {
|
|
517
|
+
const retained = remainingEntryBytes > 0
|
|
518
|
+
? { reasoning: "", content: "", calls: [], bytes: 0, remainingEntryBytes }
|
|
519
|
+
: undefined;
|
|
520
|
+
return { meta, budget, created: false, index: -1, retained, tools: new Map(), toolMetadataBytes: 0, toolsEmitted: false };
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function discardRetainedAnswer(state: InteractionsStreamState): void {
|
|
524
|
+
if (!state.retained) return;
|
|
525
|
+
state.budget.releaseRetained(state.retained.bytes, RETAINED_SCOPE);
|
|
526
|
+
state.retained = undefined;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function reserveStoredOutput(state: InteractionsStreamState, liveBytes: number, entryBytes: number): RetainedAnswer | undefined {
|
|
530
|
+
const retained = state.retained;
|
|
531
|
+
if (!retained) return undefined;
|
|
532
|
+
if (entryBytes <= retained.remainingEntryBytes) {
|
|
533
|
+
try {
|
|
534
|
+
state.budget.chargeRetained(liveBytes, RETAINED_SCOPE);
|
|
535
|
+
retained.bytes += liveBytes;
|
|
536
|
+
retained.remainingEntryBytes -= entryBytes;
|
|
537
|
+
return retained;
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (!isTranslatorBudgetExceededError(error)) throw error;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
// Storage is best effort. Drop the prefix and keep emitting the full wire response.
|
|
543
|
+
discardRetainedAnswer(state);
|
|
544
|
+
return undefined;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function retainStreamText(state: InteractionsStreamState, field: "reasoning" | "content", value: string): void {
|
|
548
|
+
if (!state.retained) return;
|
|
549
|
+
const bytes = Buffer.byteLength(value);
|
|
550
|
+
const encodedBytes = Buffer.byteLength(JSON.stringify(value)) - 2;
|
|
551
|
+
// Answer text appears in both the Chat history and the resource. Thoughts appear only in the resource.
|
|
552
|
+
const retained = reserveStoredOutput(state, bytes, field === "content" ? 2 * encodedBytes : encodedBytes);
|
|
553
|
+
if (retained) retained[field] += value;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function retainStreamCall(state: InteractionsStreamState, call: InteractionAnswer["calls"][number], args: string): void {
|
|
557
|
+
if (!state.retained) return;
|
|
558
|
+
const metadata = [call.id, call.name, call.signature ?? ""];
|
|
559
|
+
const argsBytes = Buffer.byteLength(args);
|
|
560
|
+
const bytes = argsBytes + metadata.reduce((total, value) => total + Buffer.byteLength(value), 0);
|
|
561
|
+
const entryBytes = argsBytes + Buffer.byteLength(JSON.stringify(args)) - 2
|
|
562
|
+
+ metadata.reduce((total, value) => total + 2 * (Buffer.byteLength(JSON.stringify(value)) - 2), 0);
|
|
563
|
+
reserveStoredOutput(state, bytes, entryBytes)?.calls.push(call);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/** Release the fragment buffer once the whole calls have been emitted, or when the stream stops. */
|
|
567
|
+
function releaseStreamTools(state: InteractionsStreamState): void {
|
|
568
|
+
for (const index of state.tools.keys()) state.budget.closeCall(String(index));
|
|
569
|
+
state.tools.clear();
|
|
570
|
+
state.budget.releaseRetained(state.toolMetadataBytes, { kind: "tool_args" });
|
|
571
|
+
state.toolMetadataBytes = 0;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export function disposeInteractionsStream(state: InteractionsStreamState): void {
|
|
575
|
+
discardRetainedAnswer(state);
|
|
576
|
+
releaseStreamTools(state);
|
|
577
|
+
state.budget.dispose();
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function chargeToolMetadata(state: InteractionsStreamState, value: string, previous = ""): void {
|
|
581
|
+
const delta = Buffer.byteLength(value) - Buffer.byteLength(previous);
|
|
582
|
+
if (delta > 0) state.budget.chargeRetained(delta, { kind: "tool_args" });
|
|
583
|
+
if (delta < 0) state.budget.releaseRetained(-delta, { kind: "tool_args" });
|
|
584
|
+
state.toolMetadataBytes += delta;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function sse(event: string, data: Obj): InteractionsSseEvent {
|
|
588
|
+
return { event, data: { ...data, event_type: event } };
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function createdEvent(state: InteractionsStreamState): InteractionsSseEvent[] {
|
|
592
|
+
if (state.created) return [];
|
|
593
|
+
state.created = true;
|
|
594
|
+
const { id, model, created } = state.meta;
|
|
595
|
+
return [sse("interaction.created", { interaction: { id, object: "interaction", status: "in_progress", model, created, updated: created } })];
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function closeStep(state: InteractionsStreamState, events: InteractionsSseEvent[]): void {
|
|
599
|
+
if (state.open === undefined) return;
|
|
600
|
+
events.push(sse("step.stop", { index: state.index }));
|
|
601
|
+
state.open = undefined;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function openStep(state: InteractionsStreamState, type: "thought" | "model_output", events: InteractionsSseEvent[]): void {
|
|
605
|
+
if (state.open === type) return;
|
|
606
|
+
closeStep(state, events);
|
|
607
|
+
state.index++;
|
|
608
|
+
state.open = type;
|
|
609
|
+
events.push(sse("step.start", { index: state.index, step: { type } }));
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** Consume one parsed Chat chunk. The caller owns settlement and may discard these events on failure. */
|
|
613
|
+
export function consumeInteractionsChatChunk(state: InteractionsStreamState, input: unknown): InteractionsSseEvent[] {
|
|
614
|
+
const body = object(input, "Chat chunk");
|
|
615
|
+
if (body.error !== undefined) throw new Error("Chat stream error");
|
|
616
|
+
const events = createdEvent(state);
|
|
617
|
+
if (body.usage != null) state.usage = interactionUsage(body.usage);
|
|
618
|
+
if (!Array.isArray(body.choices) || body.choices.length > 1) throw new Error("Invalid Chat chunk choices");
|
|
619
|
+
if (!body.choices.length) return events;
|
|
620
|
+
const choice = object(body.choices[0], "Chat choice");
|
|
621
|
+
const delta = object(choice.delta, "Chat delta");
|
|
622
|
+
if (state.finishReason !== undefined) throw new Error("Chat chunk after finish");
|
|
623
|
+
const thought = delta.reasoning_content ?? delta.reasoning;
|
|
624
|
+
if (thought != null) {
|
|
625
|
+
const value = text(thought, "Chat reasoning");
|
|
626
|
+
if (value) {
|
|
627
|
+
openStep(state, "thought", events);
|
|
628
|
+
retainStreamText(state, "reasoning", value);
|
|
629
|
+
events.push(sse("step.delta", { index: state.index, delta: { type: "thought_summary", content: { type: "text", text: value } } }));
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (delta.content != null) {
|
|
633
|
+
const value = text(delta.content, "Chat content");
|
|
634
|
+
if (value) {
|
|
635
|
+
openStep(state, "model_output", events);
|
|
636
|
+
retainStreamText(state, "content", value);
|
|
637
|
+
events.push(sse("step.delta", { index: state.index, delta: { type: "text", text: value } }));
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (delta.tool_calls !== undefined) {
|
|
641
|
+
if (!Array.isArray(delta.tool_calls) || state.toolsEmitted) throw new Error("Invalid Chat tool delta");
|
|
642
|
+
for (const raw of delta.tool_calls) {
|
|
643
|
+
const call = object(raw, "tool delta");
|
|
644
|
+
if (!Number.isSafeInteger(call.index) || (call.index as number) < 0) throw new Error("Invalid tool index");
|
|
645
|
+
const index = call.index as number;
|
|
646
|
+
if (!state.tools.has(index) && state.tools.size >= MAX_GEMINI_TOOL_CALLS) throw new Error("Too many tool calls");
|
|
647
|
+
const current = state.tools.get(index) ?? { name: "", arguments: "" };
|
|
648
|
+
if (call.id !== undefined) {
|
|
649
|
+
const id = text(call.id, "tool id", true);
|
|
650
|
+
if (current.id && current.id !== id) throw new Error("Conflicting tool id");
|
|
651
|
+
chargeToolMetadata(state, id, current.id);
|
|
652
|
+
current.id = id;
|
|
653
|
+
}
|
|
654
|
+
const signature = callSignature(call);
|
|
655
|
+
if (signature) {
|
|
656
|
+
chargeToolMetadata(state, signature, current.signature);
|
|
657
|
+
current.signature = signature;
|
|
658
|
+
}
|
|
659
|
+
if (call.function !== undefined) {
|
|
660
|
+
const fn = object(call.function, "tool function");
|
|
661
|
+
if (fn.name !== undefined) {
|
|
662
|
+
const name = text(fn.name, "tool name");
|
|
663
|
+
chargeToolMetadata(state, name);
|
|
664
|
+
current.name += name;
|
|
665
|
+
}
|
|
666
|
+
if (fn.arguments !== undefined) {
|
|
667
|
+
const args = text(fn.arguments, "tool arguments");
|
|
668
|
+
state.budget.chargeRetained(Buffer.byteLength(args), { kind: "tool_args", callId: String(index) });
|
|
669
|
+
current.arguments += args;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
state.tools.set(index, current);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (choice.finish_reason != null) {
|
|
676
|
+
state.finishReason = choice.finish_reason;
|
|
677
|
+
if (state.tools.size) {
|
|
678
|
+
closeStep(state, events);
|
|
679
|
+
for (const [index, tool] of [...state.tools.entries()].sort((a, b) => a[0] - b[0])) {
|
|
680
|
+
const args = callArguments(tool.arguments);
|
|
681
|
+
const id = tool.id ?? `call_interaction_${index}`;
|
|
682
|
+
const name = text(tool.name, "tool name", true);
|
|
683
|
+
if (tool.signature) {
|
|
684
|
+
state.index++;
|
|
685
|
+
events.push(sse("step.start", { index: state.index, step: { type: "thought" } }));
|
|
686
|
+
events.push(sse("step.delta", { index: state.index, delta: { type: "thought_signature", signature: tool.signature } }));
|
|
687
|
+
events.push(sse("step.stop", { index: state.index }));
|
|
688
|
+
}
|
|
689
|
+
state.index++;
|
|
690
|
+
events.push(sse("step.start", { index: state.index, step: { type: "function_call", id, name, arguments: {} } }));
|
|
691
|
+
const wireArgs = JSON.stringify(args);
|
|
692
|
+
events.push(sse("step.delta", { index: state.index, delta: { type: "arguments_delta", arguments: wireArgs } }));
|
|
693
|
+
events.push(sse("step.stop", { index: state.index }));
|
|
694
|
+
retainStreamCall(state, { id, name, arguments: args, ...(tool.signature ? { signature: tool.signature } : {}) }, wireArgs);
|
|
695
|
+
}
|
|
696
|
+
releaseStreamTools(state);
|
|
697
|
+
state.toolsEmitted = true;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return events;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/** The answer a finished stream produced, or undefined when it did not finish cleanly. */
|
|
704
|
+
export function interactionsStreamAnswer(state: InteractionsStreamState): InteractionAnswer | undefined {
|
|
705
|
+
const status = interactionStatus(state.finishReason);
|
|
706
|
+
if (!status || (state.tools.size > 0 && !state.toolsEmitted)) return undefined;
|
|
707
|
+
const { retained, usage } = state;
|
|
708
|
+
return { reasoning: retained?.reasoning ?? "", content: retained?.content ?? "", calls: retained?.calls ?? [], status, ...(usage ? { usage } : {}) };
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/** The closing events. Call only after the outer handler confirms settlement. */
|
|
712
|
+
export function interactionsStreamTerminal(state: InteractionsStreamState, answer: Pick<InteractionAnswer, "status" | "usage">, updated = state.meta.created): InteractionsSseEvent[] {
|
|
713
|
+
const events = createdEvent(state);
|
|
714
|
+
closeStep(state, events);
|
|
715
|
+
const { id, model, created } = state.meta;
|
|
716
|
+
events.push(sse("interaction.completed", { interaction: { id, object: "interaction", status: answer.status, model, created, updated, ...(answer.usage ? { usage: answer.usage } : {}) } }));
|
|
717
|
+
return events;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
const ERROR_CODES: Readonly<Record<number, string>> = {
|
|
721
|
+
400: "invalid_argument", 401: "unauthenticated", 403: "permission_denied", 404: "not_found", 413: "invalid_argument",
|
|
722
|
+
429: "resource_exhausted", 499: "cancelled", 501: "unimplemented", 503: "unavailable", 504: "deadline_exceeded",
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
/** The failure events a stream ends with after it was committed. */
|
|
726
|
+
export function interactionsStreamFailure(state: InteractionsStreamState, status: number, message: string): InteractionsSseEvent[] {
|
|
727
|
+
const events = createdEvent(state);
|
|
728
|
+
events.push(sse("error", { error: { code: ERROR_CODES[status] ?? "internal", message } }));
|
|
729
|
+
return events;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
export function encodeInteractionsEvents(events: InteractionsSseEvent[]): Uint8Array[] {
|
|
733
|
+
const encoder = new TextEncoder();
|
|
734
|
+
return events.map(event => encoder.encode(`event: ${event.event}\ndata: ${JSON.stringify(event.data)}\n\n`));
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
function toolCallIds(message: Obj | undefined): string[] {
|
|
739
|
+
if (!message || message.role !== "assistant" || !Array.isArray(message.tool_calls)) return [];
|
|
740
|
+
return message.tool_calls.flatMap(call => (isObj(call) && typeof call.id === "string" ? [call.id] : []));
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** Call ids of the history's last assistant turn that no later tool message answered. */
|
|
744
|
+
export function pendingCallIds(history: Obj[]): Set<string> {
|
|
745
|
+
let last = -1;
|
|
746
|
+
for (let i = history.length - 1; i >= 0; i--) if (history[i]!.role === "assistant") { last = i; break; }
|
|
747
|
+
const pending = new Set(toolCallIds(history[last]));
|
|
748
|
+
for (const message of history.slice(last + 1)) if (message.role === "tool" && typeof message.tool_call_id === "string") pending.delete(message.tool_call_id);
|
|
749
|
+
return pending;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Continue a stored history with a chained input. A leading assistant message that only resends the
|
|
754
|
+
* pending calls is dropped (they are already in history). Returns undefined when a function_result the
|
|
755
|
+
* input does not define is not one of the pending calls answered at the very start of the input.
|
|
756
|
+
*/
|
|
757
|
+
export function chainInput(history: Obj[], plan: Pick<InteractionsRequestPlan, "inputMessages" | "unmatchedResultIds">): Obj[] | undefined {
|
|
758
|
+
const pending = pendingCallIds(history);
|
|
759
|
+
let input = plan.inputMessages;
|
|
760
|
+
const first = input[0];
|
|
761
|
+
const resent = toolCallIds(first);
|
|
762
|
+
if (first && resent.length && !first.content && resent.every(id => pending.has(id))) input = input.slice(1);
|
|
763
|
+
if (!plan.unmatchedResultIds.length) return input;
|
|
764
|
+
const leading = new Set<string>();
|
|
765
|
+
for (const message of input) {
|
|
766
|
+
if (message.role !== "tool") break;
|
|
767
|
+
if (typeof message.tool_call_id === "string") leading.add(message.tool_call_id);
|
|
768
|
+
}
|
|
769
|
+
return plan.unmatchedResultIds.every(id => pending.has(id) && leading.has(id)) ? input : undefined;
|
|
770
|
+
}
|