@sayknow-cli/coding-agent 0.3.16 → 0.4.0
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/CHANGELOG.md +52 -0
- package/package.json +127 -110
- package/scripts/build-sdk-package-smoke.ts +132 -0
- package/scripts/capture-notifications-settings-showcase.ts +435 -0
- package/scripts/compile-args.ts +5 -2
- package/scripts/g005-daemon-smoke.ts +4 -5
- package/scripts/g011-daemon-path-smoke.ts +5 -5
- package/scripts/generate-hotkeys-docs.ts +74 -0
- package/scripts/generate-sdk-adapter-parity-manifest.ts +163 -0
- package/scripts/generate-sdk-operation-inventory.ts +914 -0
- package/scripts/generate-telegram-baseline-manifest.ts +128 -0
- package/scripts/manifest-schema.ts +124 -0
- package/scripts/run-test-manifest.ts +153 -0
- package/scripts/run-threat-model-check.ts +217 -0
- package/scripts/verify-skc-sdk-canonicalization.ts +3647 -0
- package/scripts/verify-skc-sdk-rename.ts +90 -0
- package/src/append-only-mode.ts +1 -1
- package/src/async/job-manager.ts +455 -69
- package/src/cli/args.ts +43 -12
- package/src/cli/auth-gateway-cli.ts +2 -2
- package/src/cli/config-cli.ts +77 -3
- package/src/cli/daemon-cli.ts +63 -14
- package/src/cli/launch-disposition.ts +42 -0
- package/src/cli/list-models.ts +1 -1
- package/src/cli/notify-cli.ts +373 -283
- package/src/cli/session-picker.ts +20 -3
- package/src/cli/setup-cli.ts +1 -1
- package/src/cli/worktree-cli.ts +9 -287
- package/src/cli.ts +31 -11
- package/src/commands/config.ts +1 -1
- package/src/commands/daemon.ts +50 -6
- package/src/commands/gc.ts +11 -1
- package/src/commands/harness.ts +127 -142
- package/src/commands/launch.ts +11 -6
- package/src/commands/mcp-serve.ts +97 -13
- package/src/commands/mcp.ts +2 -2
- package/src/commands/notify.ts +44 -7
- package/src/commands/sdk.ts +453 -0
- package/src/commands/ultragoal.ts +4 -1
- package/src/commands/worktree.ts +10 -53
- package/src/commit/agentic/agent.ts +1 -0
- package/src/config/atomic-yaml-patch.ts +490 -0
- package/src/config/file-lock.ts +105 -43
- package/src/config/keybindings.ts +64 -20
- package/src/config/model-auth.ts +9 -0
- package/src/config/model-bindings-applier.ts +139 -0
- package/src/config/model-discovery-manager.ts +224 -0
- package/src/config/model-equivalence.ts +66 -0
- package/src/config/model-profile-activation.ts +233 -86
- package/src/config/model-profiles.ts +52 -49
- package/src/config/model-registry.ts +5 -4
- package/src/config/model-resolver.ts +487 -410
- package/src/config/model-selector-value.ts +50 -0
- package/src/config/models-config-schema.ts +17 -23
- package/src/config/settings-schema.ts +372 -21
- package/src/config/settings.ts +1120 -182
- package/src/coordinator-mcp/model-preset.ts +74 -20
- package/src/coordinator-mcp/policy.ts +76 -11
- package/src/coordinator-mcp/question-gate-codec.ts +274 -0
- package/src/coordinator-mcp/question-state.ts +778 -0
- package/src/coordinator-mcp/server.ts +3120 -3111
- package/src/daemon/builtin.ts +8 -5
- package/src/daemon/control-types.ts +1 -1
- package/src/daemon/runtime.ts +2 -2
- package/src/debug/index.ts +1 -1
- package/src/defaults/skc/skills/team/SKILL.md +27 -13
- package/src/defaults/skc/skills/ultragoal/SKILL.md +0 -2
- package/src/discovery/helpers.ts +1 -50
- package/src/discovery/mcp-json.ts +327 -20
- package/src/edit/index.ts +64 -0
- package/src/eval/js/shared/runtime.ts +1 -1
- package/src/eval/py/env.ts +26 -0
- package/src/eval/py/index.ts +2 -2
- package/src/eval/py/kernel.ts +5 -3
- package/src/export/html/template.generated.ts +1 -1
- package/src/export/html/template.js +52 -18
- package/src/extensibility/custom-tools/types.ts +1 -1
- package/src/extensibility/extensions/compact-handler.ts +3 -3
- package/src/extensibility/extensions/get-commands-handler.ts +2 -2
- package/src/extensibility/extensions/loader.ts +77 -5
- package/src/extensibility/extensions/runner.ts +124 -7
- package/src/extensibility/extensions/types.ts +215 -18
- package/src/extensibility/hooks/runner.ts +2 -2
- package/src/extensibility/hooks/types.ts +1 -1
- package/src/extensibility/plugins/marketplace/manager.ts +94 -10
- package/src/extensibility/runtime-skill-discovery.ts +33 -13
- package/src/extensibility/shared-events.ts +2 -0
- package/src/extensibility/skc-plugins/installer.ts +16 -16
- package/src/extensibility/slash-commands.ts +3 -3
- package/src/goals/continuation-timeout-guard.ts +79 -0
- package/src/goals/runtime.ts +7 -5
- package/src/goals/state.ts +18 -0
- package/src/harness-control-plane/lifecycle-reconciliation.ts +64 -0
- package/src/harness-control-plane/operate.ts +9 -9
- package/src/harness-control-plane/owner.ts +341 -41
- package/src/harness-control-plane/phase-rollup.ts +4 -7
- package/src/harness-control-plane/receipt-ingest.ts +5 -0
- package/src/harness-control-plane/receipt-spool.ts +51 -7
- package/src/harness-control-plane/receipts.ts +0 -1
- package/src/harness-control-plane/sdk-transport.ts +395 -0
- package/src/harness-control-plane/session-lease.ts +12 -130
- package/src/harness-control-plane/session-transport.ts +94 -0
- package/src/harness-control-plane/types.ts +1 -1
- package/src/hindsight/backend.ts +26 -27
- package/src/hindsight/client.ts +65 -2
- package/src/hindsight/state.ts +101 -32
- package/src/hooks/skill-state.ts +17 -11
- package/src/i18n/messages/en.ts +1 -0
- package/src/internal-urls/agent-protocol.ts +76 -5
- package/src/internal-urls/docs-index.generated.ts +44 -37
- package/src/internal-urls/local-protocol.ts +420 -18
- package/src/lsp/client.ts +43 -7
- package/src/lsp/config.ts +145 -26
- package/src/lsp/index.ts +7 -91
- package/src/lsp/lspmux.ts +39 -10
- package/src/lsp/path-trust.ts +81 -0
- package/src/main.ts +451 -236
- package/src/memories/index.ts +1 -0
- package/src/memories/storage.ts +6 -2
- package/src/modes/DESIGN.md +258 -0
- package/src/modes/acp/acp-agent.ts +1236 -2612
- package/src/modes/acp/acp-client-bridge.ts +2 -20
- package/src/modes/acp/acp-event-mapper.ts +18 -2
- package/src/modes/acp/acp-mode.ts +8 -36
- package/src/modes/acp/permission-mode.ts +25 -0
- package/src/modes/acp/startup-options.ts +6 -0
- package/src/modes/action-registry.ts +165 -0
- package/src/modes/bridge/bridge-mode.ts +6 -4
- package/src/modes/components/agent-dashboard.ts +37 -17
- package/src/modes/components/ansi-display-validator.ts +54 -0
- package/src/modes/components/chrome.ts +40 -0
- package/src/modes/components/command-palette.ts +168 -0
- package/src/modes/components/custom-editor.ts +105 -33
- package/src/modes/components/custom-provider-wizard.ts +77 -15
- package/src/modes/components/index.ts +2 -1
- package/src/modes/components/model-selector.ts +93 -35
- package/src/modes/components/notifications-settings-editor.ts +1403 -0
- package/src/modes/components/pet-capability.ts +115 -0
- package/src/modes/components/pet-selector.ts +15 -23
- package/src/modes/components/plan-preview-overlay.ts +295 -0
- package/src/modes/components/queue-mode-selector.ts +10 -27
- package/src/modes/components/queue-pane.ts +79 -0
- package/src/modes/components/read-tool-group.ts +13 -0
- package/src/modes/components/sayknow-pet-widget.ts +169 -20
- package/src/modes/components/session-observer-overlay.ts +213 -775
- package/src/modes/components/sessions-dashboard.ts +181 -0
- package/src/modes/components/settings-selector.ts +89 -12
- package/src/modes/components/show-images-selector.ts +10 -24
- package/src/modes/components/status-line/types.ts +1 -1
- package/src/modes/components/status-line.ts +1 -969
- package/src/modes/components/tasks-pane.ts +73 -0
- package/src/modes/components/theme-selector.ts +11 -31
- package/src/modes/components/tool-execution.ts +22 -0
- package/src/modes/components/tool-status-header.ts +1056 -0
- package/src/modes/components/tool-transcript-format.ts +393 -0
- package/src/modes/components/transcript-viewer-overlay.ts +446 -0
- package/src/modes/components/welcome.ts +4 -4
- package/src/modes/controllers/btw-controller.ts +1 -0
- package/src/modes/controllers/command-controller.ts +25 -17
- package/src/modes/controllers/event-controller.ts +14 -18
- package/src/modes/controllers/extension-ui-controller.ts +441 -37
- package/src/modes/controllers/goal-mode-controller.ts +493 -0
- package/src/modes/controllers/input-controller.ts +889 -263
- package/src/modes/controllers/mode-gate.ts +24 -0
- package/src/modes/controllers/plan-mode-controller.ts +519 -0
- package/src/modes/controllers/runtime-mcp-command-controller.ts +3 -0
- package/src/modes/controllers/selector-controller.ts +957 -54
- package/src/modes/controllers/stt-controller.ts +125 -0
- package/src/modes/index.ts +0 -22
- package/src/modes/interactive-mode.ts +376 -1130
- package/src/modes/jobs-observer.ts +3 -0
- package/src/modes/prompt-action-autocomplete.ts +7 -0
- package/src/modes/rpc/rpc-mode.ts +3 -1
- package/src/modes/runtime-init.ts +320 -6
- package/src/modes/shared/agent-wire/approval-gate.ts +9 -11
- package/src/modes/shared/agent-wire/command-dispatch.ts +3 -1
- package/src/modes/shared/agent-wire/deep-interview-gate.ts +65 -111
- package/src/modes/shared/agent-wire/event-contract.ts +7 -8
- package/src/modes/shared/agent-wire/event-envelope.ts +9 -17
- package/src/modes/shared/agent-wire/event-observation.ts +16 -7
- package/src/modes/shared/agent-wire/workflow-gate-broker.ts +1331 -89
- package/src/modes/shared/agent-wire/workflow-gate-schema.ts +19 -17
- package/src/modes/shared/agent-wire/workflow-gate-types.ts +407 -0
- package/src/modes/tasks-aggregator.ts +199 -0
- package/src/modes/transcript-item-registry.ts +229 -0
- package/src/modes/types.ts +63 -21
- package/src/modes/utils/ui-helpers.ts +17 -9
- package/src/notifications/index.ts +8 -2
- package/src/plan-mode/approved-plan.ts +3 -1
- package/src/prompts/agent-fragments/ralplan-persistence.md +6 -0
- package/src/prompts/agent-fragments/restricted-bash.md +1 -0
- package/src/prompts/agents/architect.md +3 -8
- package/src/prompts/agents/critic.md +3 -8
- package/src/prompts/agents/executor.md +3 -0
- package/src/prompts/agents/planner.md +4 -12
- package/src/prompts/system/custom-system-prompt.md +2 -24
- package/src/prompts/system/plan-mode-approved.md +8 -0
- package/src/prompts/system/system-prompt.md +18 -94
- package/src/prompts/tools/bash.md +4 -4
- package/src/prompts/tools/browser.md +15 -52
- package/src/prompts/tools/find.md +2 -2
- package/src/prompts/tools/hashline.md +13 -55
- package/src/prompts/tools/lsp.md +3 -3
- package/src/prompts/tools/read.md +3 -6
- package/src/prompts/tools/search-tool-bm25.md +0 -2
- package/src/prompts/tools/search.md +2 -4
- package/src/prompts/tools/skill-discovery.md +1 -2
- package/src/prompts/tools/task.md +1 -1
- package/src/runtime-mcp/client.ts +94 -19
- package/src/runtime-mcp/config.ts +27 -15
- package/src/runtime-mcp/discoverable-tool-metadata.ts +16 -13
- package/src/runtime-mcp/manager.ts +359 -106
- package/src/runtime-mcp/oauth-discovery.ts +116 -26
- package/src/runtime-mcp/oauth-flow.ts +42 -12
- package/src/runtime-mcp/transports/http.ts +49 -19
- package/src/runtime-mcp/transports/stdio.ts +47 -28
- package/src/runtime-mcp/types.ts +10 -0
- package/src/sdk/acp/adapter.ts +418 -0
- package/src/sdk/acp/index.ts +2 -0
- package/src/sdk/broker/broker.ts +877 -0
- package/src/sdk/broker/discovery.ts +238 -0
- package/src/sdk/broker/ensure.ts +393 -0
- package/src/sdk/broker/identity.ts +42 -0
- package/src/sdk/broker/index.ts +8 -0
- package/src/sdk/broker/internal-source-marker-2178.txt +1 -0
- package/src/sdk/broker/internal-source.bunfig.toml +1 -0
- package/src/sdk/broker/internal.ts +10 -0
- package/src/sdk/broker/lifecycle-codec.ts +11 -0
- package/src/sdk/broker/lifecycle-ledger.ts +583 -0
- package/src/sdk/broker/lifecycle.ts +3413 -0
- package/src/sdk/broker/process-incarnation.ts +151 -0
- package/src/sdk/broker/runtime.ts +164 -0
- package/src/sdk/broker/session-index.ts +678 -0
- package/src/sdk/broker/state-version.ts +47 -0
- package/src/sdk/broker/transport.ts +125 -0
- package/src/sdk/bus/attachment-registry.ts +23 -0
- package/src/sdk/bus/chat-adapters.ts +147 -0
- package/src/sdk/bus/chat-command-policy.ts +107 -0
- package/src/sdk/bus/chat-daemon-cli.ts +278 -0
- package/src/sdk/bus/chat-daemon-control.ts +1101 -0
- package/src/sdk/bus/chat-daemon-runtime.ts +527 -0
- package/src/sdk/bus/chat-effect-journal.ts +363 -0
- package/src/sdk/bus/config-commands.ts +192 -0
- package/src/sdk/bus/config.ts +568 -0
- package/src/sdk/bus/conversation-store.ts +367 -0
- package/src/sdk/bus/daemon-paths.ts +34 -0
- package/src/sdk/bus/discord-conversation.ts +85 -0
- package/src/sdk/bus/discord-daemon.ts +2292 -0
- package/src/sdk/bus/discord-live-provider.ts +521 -0
- package/src/sdk/bus/discord-provider.ts +56 -0
- package/src/sdk/bus/engine.ts +100 -0
- package/src/sdk/bus/helpers.ts +135 -0
- package/src/sdk/bus/html-format.ts +518 -0
- package/src/sdk/bus/index.ts +4961 -0
- package/src/sdk/bus/lifecycle-commands.ts +291 -0
- package/src/sdk/bus/lifecycle-control-runtime.ts +1409 -0
- package/src/sdk/bus/lifecycle-orchestrator.ts +458 -0
- package/src/sdk/bus/managed-daemon.ts +163 -0
- package/src/sdk/bus/native-runtime-compatibility.ts +42 -0
- package/src/sdk/bus/natives-augment.d.ts +63 -0
- package/src/sdk/bus/notification-orchestration.ts +535 -0
- package/src/sdk/bus/notification-service.ts +1174 -0
- package/src/sdk/bus/operator-runtime.ts +171 -0
- package/src/sdk/bus/rate-limit-pool.ts +276 -0
- package/src/sdk/bus/recent-activity.ts +279 -0
- package/src/sdk/bus/reply-sent-store.ts +134 -0
- package/src/sdk/bus/rich-document.ts +258 -0
- package/src/sdk/bus/rich-draft.ts +107 -0
- package/src/sdk/bus/rich-render.ts +135 -0
- package/src/sdk/bus/session-control.ts +439 -0
- package/src/sdk/bus/slack-conversation.ts +74 -0
- package/src/sdk/bus/slack-daemon.ts +2144 -0
- package/src/sdk/bus/slack-live-provider.ts +380 -0
- package/src/sdk/bus/slack-provider.ts +76 -0
- package/src/sdk/bus/telegram-cli.ts +194 -0
- package/src/sdk/bus/telegram-daemon-cli.ts +292 -0
- package/src/sdk/bus/telegram-daemon-contract.ts +27 -0
- package/src/sdk/bus/telegram-daemon-control.ts +642 -0
- package/src/sdk/bus/telegram-daemon.ts +6675 -0
- package/src/sdk/bus/telegram-reference.ts +436 -0
- package/src/sdk/bus/telegram-setup.ts +663 -0
- package/src/sdk/bus/threaded-inbound.ts +141 -0
- package/src/sdk/bus/threaded-render.ts +294 -0
- package/src/sdk/bus/topic-registry.ts +364 -0
- package/src/sdk/cli/index.ts +1 -0
- package/src/sdk/cli/session-cli.ts +275 -0
- package/src/sdk/client/client.ts +1 -0
- package/src/sdk/client/discovery.ts +108 -0
- package/src/sdk/client/index.ts +2 -0
- package/src/sdk/host/control/dispatch.ts +388 -0
- package/src/sdk/host/control/index.ts +10 -0
- package/src/sdk/host/control/operations.ts +68 -0
- package/src/sdk/host/events.ts +91 -0
- package/src/sdk/host/host.ts +338 -0
- package/src/sdk/host/index.ts +4 -0
- package/src/sdk/host/query/cursor.ts +189 -0
- package/src/sdk/host/query/handlers.ts +531 -0
- package/src/sdk/host/query/index.ts +3 -0
- package/src/sdk/host/query/revision-store.ts +1149 -0
- package/src/sdk/host/reverse-leases.ts +325 -0
- package/src/sdk/host/types.ts +29 -0
- package/src/sdk/index.ts +14 -0
- package/src/sdk/lifecycle-session.ts +39 -0
- package/src/sdk/mcp/index.ts +1 -0
- package/src/sdk/mcp/server.ts +332 -0
- package/src/sdk/models.ts +168 -0
- package/src/sdk/protocol/adapter-validation.ts +48 -0
- package/src/sdk/protocol/operation-inventory.generated.json +4719 -0
- package/src/sdk/protocol/operation-registry.ts +312 -0
- package/src/sdk/session-directory.ts +141 -0
- package/src/sdk/session.ts +2869 -0
- package/src/sdk/startup-capability.ts +189 -0
- package/src/secrets/index.ts +53 -10
- package/src/secrets/obfuscator.ts +21 -18
- package/src/secrets/regex.ts +2 -0
- package/src/session/agent-session.ts +5602 -2138
- package/src/session/agent-storage.ts +18 -0
- package/src/session/artifacts.ts +188 -23
- package/src/session/cache-economics.ts +25 -8
- package/src/session/context-estimation.ts +7 -2
- package/src/session/default-model-selection.ts +93 -0
- package/src/session/fallback-chain-controller.ts +186 -0
- package/src/session/internal/managed-session-scope.ts +2402 -0
- package/src/session/internal/managed-session-storage.ts +1559 -0
- package/src/session/messages.ts +91 -2
- package/src/session/session-manager-internal.ts +33 -0
- package/src/session/session-manager.ts +2578 -392
- package/src/session/session-storage.ts +550 -40
- package/src/session/streaming-output.ts +6 -0
- package/src/session/volatile-context-pruning.ts +78 -0
- package/src/session/yield-queue.ts +20 -0
- package/src/setup/credential-auto-import.ts +291 -35
- package/src/setup/hermes/templates/operator-instructions.v1.md +4 -6
- package/src/setup/provider-onboarding.ts +36 -61
- package/src/setup/provider-presets.json +5 -0
- package/src/skc-runtime/deep-interview-recorder.ts +70 -3
- package/src/skc-runtime/deep-interview-state.ts +441 -5
- package/src/skc-runtime/gc-render.ts +19 -1
- package/src/skc-runtime/gc-runtime.ts +68 -3
- package/src/skc-runtime/goal-mode-request.ts +55 -8
- package/src/skc-runtime/launch-worktree.ts +5 -5
- package/src/skc-runtime/linux-proc.ts +71 -0
- package/src/skc-runtime/managed-owner-admission.ts +352 -0
- package/src/skc-runtime/managed-owner-supervisor.ts +162 -0
- package/src/skc-runtime/psmux-detect.ts +82 -4
- package/src/skc-runtime/ralplan-runtime.ts +74 -90
- package/src/skc-runtime/session-resolution.ts +18 -3
- package/src/skc-runtime/session-state-sidecar.ts +47 -138
- package/src/skc-runtime/state-argv.ts +292 -0
- package/src/skc-runtime/state-migrations.ts +1 -1
- package/src/skc-runtime/state-runtime.ts +387 -451
- package/src/skc-runtime/state-writer.ts +36 -40
- package/src/skc-runtime/team-launch.ts +319 -0
- package/src/skc-runtime/team-notify.ts +326 -0
- package/src/skc-runtime/team-runtime.ts +8 -2
- package/src/skc-runtime/team-store.ts +828 -0
- package/src/skc-runtime/team-workers.ts +962 -0
- package/src/skc-runtime/tmux-owner-isolation-cli.ts +55 -28
- package/src/skc-runtime/tmux-owner-isolation.ts +136 -11
- package/src/skc-runtime/tmux-sessions.ts +23 -34
- package/src/skc-runtime/ultragoal-change-set.ts +172 -0
- package/src/skc-runtime/ultragoal-evidence.ts +874 -0
- package/src/skc-runtime/ultragoal-guard.ts +136 -18
- package/src/skc-runtime/ultragoal-owner-loss-recovery.ts +328 -0
- package/src/skc-runtime/ultragoal-pipeline.ts +306 -0
- package/src/skc-runtime/ultragoal-receipt-freshness.ts +102 -1
- package/src/skc-runtime/ultragoal-runtime.ts +325 -1322
- package/src/skc-runtime/windows-powershell-command.ts +58 -0
- package/src/skc-runtime/workflow-cli-common.ts +30 -0
- package/src/skc-runtime/workflow-manifest.generated.json +58 -0
- package/src/skc-runtime/workflow-manifest.ts +18 -0
- package/src/skill-state/active-state.ts +3 -13
- package/src/skill-state/workflow-mutation-guard.ts +661 -0
- package/src/skill-state/workflow-state-contract.ts +6 -41
- package/src/slash-commands/builtin-registry.ts +115 -41
- package/src/system-prompt.ts +37 -44
- package/src/task/agents.ts +19 -1
- package/src/task/executor.ts +223 -45
- package/src/task/index.ts +124 -56
- package/src/thinking.ts +21 -6
- package/src/tool-discovery/tool-index.ts +20 -134
- package/src/tools/ask-answer-registry.ts +16 -2
- package/src/tools/ast-edit.ts +1 -1
- package/src/tools/bash-allowed-prefixes.ts +79 -29
- package/src/tools/bash-skill-urls.ts +14 -3
- package/src/tools/bash.ts +40 -2
- package/src/tools/browser/STEALTH-THIRD-PARTY-NOTICES.md +26 -0
- package/src/tools/browser/benchmark/detector-report.ts +157 -0
- package/src/tools/browser/benchmark/run-suite.ts +39 -0
- package/src/tools/browser/dead-tab-recovery.ts +78 -0
- package/src/tools/browser/launch.ts +127 -20
- package/src/tools/browser/profile-discovery.ts +63 -0
- package/src/tools/browser/profile-posture.ts +59 -0
- package/src/tools/browser/profile-reuse.ts +102 -0
- package/src/tools/browser/profile-warmup.ts +109 -0
- package/src/tools/browser/readable.ts +1 -1
- package/src/tools/browser/registry.ts +87 -11
- package/src/tools/browser/screenshot-format.ts +34 -0
- package/src/tools/browser/tab-protocol.ts +4 -0
- package/src/tools/browser/tab-supervisor.ts +392 -142
- package/src/tools/browser/tab-worker-entry.ts +33 -14
- package/src/tools/browser/tab-worker-path-resolver.ts +67 -0
- package/src/tools/browser/tab-worker-smoke.ts +87 -0
- package/src/tools/browser/tab-worker.ts +8 -6
- package/src/tools/browser.ts +6 -0
- package/src/tools/computer.ts +10 -3
- package/src/tools/cron.ts +30 -16
- package/src/tools/fetch.ts +11 -4
- package/src/tools/index.ts +88 -32
- package/src/tools/output-meta.ts +19 -7
- package/src/tools/path-utils.ts +42 -35
- package/src/tools/plan-mode-guard.ts +1 -0
- package/src/tools/puppeteer/14_stealth_permissions.txt +57 -0
- package/src/tools/puppeteer/15_stealth_webrtc.txt +60 -0
- package/src/tools/read.ts +226 -65
- package/src/tools/resource-gc.ts +125 -36
- package/src/tools/search-tool-bm25.ts +11 -74
- package/src/tools/sqlite-reader.ts +3 -2
- package/src/tools/subagent-render.ts +19 -8
- package/src/tools/subagent.ts +129 -34
- package/src/tools/telegram-send.ts +2 -2
- package/src/utils/event-bus.ts +9 -4
- package/src/utils/file-mentions.ts +4 -4
- package/src/utils/image-loading.ts +85 -34
- package/src/utils/image-resize.ts +16 -9
- package/src/utils/pasted-image-loading.ts +514 -0
- package/src/utils/pasted-image-path.ts +223 -98
- package/src/web/scrapers/html-to-markdown.ts +68 -0
- package/src/web/scrapers/readthedocs.ts +1 -1
- package/src/web/scrapers/types.ts +1 -68
- package/src/web/search/index.ts +3 -3
- package/src/web/search/providers/openai-compatible.ts +76 -23
- package/dist/types/append-only-mode.d.ts +0 -31
- package/dist/types/async/index.d.ts +0 -2
- package/dist/types/async/job-manager.d.ts +0 -328
- package/dist/types/async/support.d.ts +0 -2
- package/dist/types/autoresearch/dashboard.d.ts +0 -4
- package/dist/types/autoresearch/git.d.ts +0 -36
- package/dist/types/autoresearch/helpers.d.ts +0 -24
- package/dist/types/autoresearch/index.d.ts +0 -2
- package/dist/types/autoresearch/state.d.ts +0 -17
- package/dist/types/autoresearch/storage.d.ts +0 -142
- package/dist/types/autoresearch/tools/init-experiment.d.ts +0 -31
- package/dist/types/autoresearch/tools/log-experiment.d.ts +0 -23
- package/dist/types/autoresearch/tools/run-experiment.d.ts +0 -8
- package/dist/types/autoresearch/tools/update-notes.d.ts +0 -12
- package/dist/types/autoresearch/types.d.ts +0 -154
- package/dist/types/build-metadata.d.ts +0 -7
- package/dist/types/capability/context-file.d.ts +0 -17
- package/dist/types/capability/extension-module.d.ts +0 -15
- package/dist/types/capability/extension.d.ts +0 -28
- package/dist/types/capability/fs.d.ts +0 -20
- package/dist/types/capability/hook.d.ts +0 -19
- package/dist/types/capability/index.d.ts +0 -80
- package/dist/types/capability/instruction.d.ts +0 -17
- package/dist/types/capability/mcp.d.ts +0 -49
- package/dist/types/capability/prompt.d.ts +0 -15
- package/dist/types/capability/rule.d.ts +0 -69
- package/dist/types/capability/settings.d.ts +0 -15
- package/dist/types/capability/skill.d.ts +0 -36
- package/dist/types/capability/slash-command.d.ts +0 -17
- package/dist/types/capability/ssh.d.ts +0 -23
- package/dist/types/capability/system-prompt.d.ts +0 -15
- package/dist/types/capability/tool.d.ts +0 -19
- package/dist/types/capability/types.d.ts +0 -154
- package/dist/types/cli/agents-cli.d.ts +0 -12
- package/dist/types/cli/args.d.ts +0 -57
- package/dist/types/cli/auth-broker-cli.d.ts +0 -25
- package/dist/types/cli/auth-gateway-cli.d.ts +0 -18
- package/dist/types/cli/classify-install-target.d.ts +0 -8
- package/dist/types/cli/commands/init-xdg.d.ts +0 -1
- package/dist/types/cli/completion-cli.d.ts +0 -41
- package/dist/types/cli/config-cli.d.ts +0 -23
- package/dist/types/cli/daemon-cli.d.ts +0 -27
- package/dist/types/cli/fast-help.d.ts +0 -1
- package/dist/types/cli/file-processor.d.ts +0 -11
- package/dist/types/cli/fixture-report.d.ts +0 -20
- package/dist/types/cli/fixture-report.test.d.ts +0 -1
- package/dist/types/cli/grep-cli.d.ts +0 -17
- package/dist/types/cli/initial-message.d.ts +0 -17
- package/dist/types/cli/list-models.d.ts +0 -36
- package/dist/types/cli/local-provider-smoke.d.ts +0 -59
- package/dist/types/cli/malloc-env-guard.d.ts +0 -65
- package/dist/types/cli/malloc-env-guard.test.d.ts +0 -1
- package/dist/types/cli/mcp-cli.d.ts +0 -25
- package/dist/types/cli/migrate-cli.d.ts +0 -20
- package/dist/types/cli/nofile-limit.d.ts +0 -11
- package/dist/types/cli/notify-cli.d.ts +0 -39
- package/dist/types/cli/plugin-cli.d.ts +0 -34
- package/dist/types/cli/read-cli.d.ts +0 -4
- package/dist/types/cli/session-picker.d.ts +0 -4
- package/dist/types/cli/setup-cli.d.ts +0 -73
- package/dist/types/cli/shell-cli.d.ts +0 -8
- package/dist/types/cli/skills-cli.d.ts +0 -9
- package/dist/types/cli/ssh-cli.d.ts +0 -21
- package/dist/types/cli/stats-cli.d.ts +0 -17
- package/dist/types/cli/telegram-cli.d.ts +0 -13
- package/dist/types/cli/update-cli.d.ts +0 -108
- package/dist/types/cli/web-search-cli.d.ts +0 -32
- package/dist/types/cli/worktree-cli.d.ts +0 -26
- package/dist/types/cli.d.ts +0 -140
- package/dist/types/commands/acp.d.ts +0 -12
- package/dist/types/commands/agents.d.ts +0 -34
- package/dist/types/commands/auth-broker.d.ts +0 -54
- package/dist/types/commands/auth-gateway.d.ts +0 -32
- package/dist/types/commands/codex-native-hook.d.ts +0 -6
- package/dist/types/commands/commit.d.ts +0 -27
- package/dist/types/commands/completion.d.ts +0 -29
- package/dist/types/commands/config.d.ts +0 -33
- package/dist/types/commands/contribution-prep.d.ts +0 -18
- package/dist/types/commands/coordinator.d.ts +0 -19
- package/dist/types/commands/daemon.d.ts +0 -45
- package/dist/types/commands/deep-interview.d.ts +0 -54
- package/dist/types/commands/gc.d.ts +0 -26
- package/dist/types/commands/grep.d.ts +0 -42
- package/dist/types/commands/harness.d.ts +0 -43
- package/dist/types/commands/launch.d.ts +0 -131
- package/dist/types/commands/local-provider.d.ts +0 -34
- package/dist/types/commands/mcp-serve.d.ts +0 -24
- package/dist/types/commands/mcp.d.ts +0 -70
- package/dist/types/commands/migrate.d.ts +0 -33
- package/dist/types/commands/notify.d.ts +0 -47
- package/dist/types/commands/plugin.d.ts +0 -58
- package/dist/types/commands/ralplan.d.ts +0 -7
- package/dist/types/commands/read.d.ts +0 -15
- package/dist/types/commands/rlm.d.ts +0 -10
- package/dist/types/commands/session.d.ts +0 -30
- package/dist/types/commands/setup.d.ts +0 -105
- package/dist/types/commands/shell.d.ts +0 -21
- package/dist/types/commands/skills.d.ts +0 -26
- package/dist/types/commands/ssh.d.ts +0 -48
- package/dist/types/commands/state.d.ts +0 -7
- package/dist/types/commands/stats.d.ts +0 -25
- package/dist/types/commands/team.d.ts +0 -24
- package/dist/types/commands/telegram.d.ts +0 -22
- package/dist/types/commands/ultragoal.d.ts +0 -8
- package/dist/types/commands/update.d.ts +0 -20
- package/dist/types/commands/web-search.d.ts +0 -87
- package/dist/types/commands/worktree.d.ts +0 -34
- package/dist/types/commit/agentic/agent.d.ts +0 -31
- package/dist/types/commit/agentic/fallback.d.ts +0 -5
- package/dist/types/commit/agentic/index.d.ts +0 -2
- package/dist/types/commit/agentic/state.d.ts +0 -58
- package/dist/types/commit/agentic/tools/analyze-file.d.ts +0 -19
- package/dist/types/commit/agentic/tools/git-file-diff.d.ts +0 -10
- package/dist/types/commit/agentic/tools/git-hunk.d.ts +0 -9
- package/dist/types/commit/agentic/tools/git-overview.d.ts +0 -9
- package/dist/types/commit/agentic/tools/index.d.ts +0 -16
- package/dist/types/commit/agentic/tools/propose-changelog.d.ts +0 -28
- package/dist/types/commit/agentic/tools/propose-commit.d.ts +0 -36
- package/dist/types/commit/agentic/tools/recent-commits.d.ts +0 -7
- package/dist/types/commit/agentic/tools/schemas.d.ts +0 -27
- package/dist/types/commit/agentic/tools/split-commit.d.ts +0 -53
- package/dist/types/commit/agentic/topo-sort.d.ts +0 -4
- package/dist/types/commit/agentic/trivial.d.ts +0 -7
- package/dist/types/commit/agentic/validation.d.ts +0 -20
- package/dist/types/commit/analysis/conventional.d.ts +0 -22
- package/dist/types/commit/analysis/index.d.ts +0 -4
- package/dist/types/commit/analysis/scope.d.ts +0 -6
- package/dist/types/commit/analysis/summary.d.ts +0 -19
- package/dist/types/commit/analysis/validation.d.ts +0 -8
- package/dist/types/commit/changelog/detect.d.ts +0 -2
- package/dist/types/commit/changelog/generate.d.ts +0 -30
- package/dist/types/commit/changelog/index.d.ts +0 -30
- package/dist/types/commit/changelog/parse.d.ts +0 -2
- package/dist/types/commit/cli.d.ts +0 -3
- package/dist/types/commit/git/diff.d.ts +0 -5
- package/dist/types/commit/index.d.ts +0 -4
- package/dist/types/commit/map-reduce/index.d.ts +0 -28
- package/dist/types/commit/map-reduce/map-phase.d.ts +0 -17
- package/dist/types/commit/map-reduce/reduce-phase.d.ts +0 -13
- package/dist/types/commit/map-reduce/utils.d.ts +0 -2
- package/dist/types/commit/message.d.ts +0 -2
- package/dist/types/commit/model-selection.d.ts +0 -15
- package/dist/types/commit/pipeline.d.ts +0 -5
- package/dist/types/commit/shared-llm.d.ts +0 -54
- package/dist/types/commit/types.d.ts +0 -78
- package/dist/types/commit/utils/exclusions.d.ts +0 -4
- package/dist/types/commit/utils.d.ts +0 -20
- package/dist/types/config/config-file.d.ts +0 -58
- package/dist/types/config/file-lock-gc.d.ts +0 -5
- package/dist/types/config/file-lock.d.ts +0 -36
- package/dist/types/config/keybindings.d.ts +0 -364
- package/dist/types/config/model-equivalence.d.ts +0 -24
- package/dist/types/config/model-profile-activation.d.ts +0 -65
- package/dist/types/config/model-profiles.d.ts +0 -39
- package/dist/types/config/model-registry.d.ts +0 -473
- package/dist/types/config/model-resolver.d.ts +0 -217
- package/dist/types/config/models-config-schema.d.ts +0 -700
- package/dist/types/config/prompt-templates.d.ts +0 -32
- package/dist/types/config/resolve-config-value.d.ts +0 -17
- package/dist/types/config/settings-schema.d.ts +0 -4223
- package/dist/types/config/settings.d.ts +0 -158
- package/dist/types/config/skill-settings-defaults.d.ts +0 -14
- package/dist/types/config/telegram-autostart.d.ts +0 -14
- package/dist/types/config/telegram-env-bridge.d.ts +0 -18
- package/dist/types/config.d.ts +0 -66
- package/dist/types/coordinator/contract.d.ts +0 -4
- package/dist/types/coordinator-mcp/model-preset.d.ts +0 -45
- package/dist/types/coordinator-mcp/policy.d.ts +0 -27
- package/dist/types/coordinator-mcp/safety.d.ts +0 -26
- package/dist/types/coordinator-mcp/server.d.ts +0 -139
- package/dist/types/coordinator-mcp/session-reaper.d.ts +0 -53
- package/dist/types/cursor.d.ts +0 -24
- package/dist/types/daemon/builtin.d.ts +0 -20
- package/dist/types/daemon/control-types.d.ts +0 -67
- package/dist/types/daemon/operator-contract.d.ts +0 -47
- package/dist/types/daemon/runtime.d.ts +0 -25
- package/dist/types/dap/client.d.ts +0 -39
- package/dist/types/dap/config.d.ts +0 -6
- package/dist/types/dap/index.d.ts +0 -4
- package/dist/types/dap/session.d.ts +0 -108
- package/dist/types/dap/types.d.ts +0 -524
- package/dist/types/debug/crash-diagnostics.d.ts +0 -45
- package/dist/types/debug/index.d.ts +0 -15
- package/dist/types/debug/log-formatting.d.ts +0 -4
- package/dist/types/debug/log-viewer.d.ts +0 -68
- package/dist/types/debug/profiler.d.ts +0 -24
- package/dist/types/debug/raw-sse-buffer.d.ts +0 -44
- package/dist/types/debug/raw-sse.d.ts +0 -16
- package/dist/types/debug/report-bundle.d.ts +0 -55
- package/dist/types/debug/runtime-gauges.d.ts +0 -6
- package/dist/types/debug/system-info.d.ts +0 -26
- package/dist/types/deep-interview/plaintext-gate-guard.d.ts +0 -11
- package/dist/types/deep-interview/render-middleware.d.ts +0 -6
- package/dist/types/defaults/skc/extensions/grok-build/index.d.ts +0 -1
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/index.d.ts +0 -1
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/models/catalog.d.ts +0 -29
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/payload/sanitize.d.ts +0 -27
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/provider/billing.d.ts +0 -8
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/provider/register.d.ts +0 -5
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/provider/stream.d.ts +0 -10
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/provider/usage.d.ts +0 -2
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/shared/base-url.d.ts +0 -2
- package/dist/types/defaults/skc/extensions/grok-cli-vendor/src/shared/errors.d.ts +0 -38
- package/dist/types/defaults/skc-defaults.d.ts +0 -64
- package/dist/types/defaults/skc-grok-cli.d.ts +0 -5
- package/dist/types/discovery/agents-md.d.ts +0 -1
- package/dist/types/discovery/agents.d.ts +0 -12
- package/dist/types/discovery/builtin.d.ts +0 -1
- package/dist/types/discovery/claude-plugins.d.ts +0 -1
- package/dist/types/discovery/claude.d.ts +0 -1
- package/dist/types/discovery/cline.d.ts +0 -1
- package/dist/types/discovery/codex.d.ts +0 -1
- package/dist/types/discovery/cursor.d.ts +0 -19
- package/dist/types/discovery/gemini.d.ts +0 -1
- package/dist/types/discovery/github.d.ts +0 -1
- package/dist/types/discovery/helpers.d.ts +0 -271
- package/dist/types/discovery/index.d.ts +0 -48
- package/dist/types/discovery/mcp-json.d.ts +0 -1
- package/dist/types/discovery/opencode.d.ts +0 -1
- package/dist/types/discovery/plugin-dir-roots.d.ts +0 -15
- package/dist/types/discovery/ssh.d.ts +0 -1
- package/dist/types/discovery/substitute-plugin-root.d.ts +0 -1
- package/dist/types/discovery/vscode.d.ts +0 -1
- package/dist/types/discovery/windsurf.d.ts +0 -15
- package/dist/types/edit/apply-patch/index.d.ts +0 -35
- package/dist/types/edit/apply-patch/parser.d.ts +0 -34
- package/dist/types/edit/diff.d.ts +0 -75
- package/dist/types/edit/file-read-cache.d.ts +0 -25
- package/dist/types/edit/index.d.ts +0 -58
- package/dist/types/edit/modes/apply-patch.d.ts +0 -24
- package/dist/types/edit/modes/patch.d.ts +0 -99
- package/dist/types/edit/modes/replace.d.ts +0 -149
- package/dist/types/edit/normalize.d.ts +0 -43
- package/dist/types/edit/notebook.d.ts +0 -26
- package/dist/types/edit/read-file.d.ts +0 -8
- package/dist/types/edit/renderer.d.ts +0 -110
- package/dist/types/edit/streaming.d.ts +0 -66
- package/dist/types/eval/backend.d.ts +0 -40
- package/dist/types/eval/index.d.ts +0 -4
- package/dist/types/eval/js/context-manager.d.ts +0 -27
- package/dist/types/eval/js/executor.d.ts +0 -29
- package/dist/types/eval/js/index.d.ts +0 -10
- package/dist/types/eval/js/shared/helpers.d.ts +0 -38
- package/dist/types/eval/js/shared/indirect-eval.d.ts +0 -14
- package/dist/types/eval/js/shared/prelude.d.ts +0 -1
- package/dist/types/eval/js/shared/rewrite-imports.d.ts +0 -6
- package/dist/types/eval/js/shared/runtime.d.ts +0 -47
- package/dist/types/eval/js/shared/types.d.ts +0 -24
- package/dist/types/eval/js/tool-bridge.d.ts +0 -18
- package/dist/types/eval/js/worker-core.d.ts +0 -5
- package/dist/types/eval/js/worker-entry.d.ts +0 -1
- package/dist/types/eval/js/worker-protocol.d.ts +0 -77
- package/dist/types/eval/py/display.d.ts +0 -25
- package/dist/types/eval/py/executor.d.ts +0 -88
- package/dist/types/eval/py/index.d.ts +0 -10
- package/dist/types/eval/py/kernel.d.ts +0 -65
- package/dist/types/eval/py/prelude.d.ts +0 -1
- package/dist/types/eval/py/runtime.d.ts +0 -29
- package/dist/types/eval/py/tool-bridge.d.ts +0 -20
- package/dist/types/eval/types.d.ts +0 -52
- package/dist/types/exa/factory.d.ts +0 -13
- package/dist/types/exa/index.d.ts +0 -20
- package/dist/types/exa/mcp-client.d.ts +0 -45
- package/dist/types/exa/render.d.ts +0 -19
- package/dist/types/exa/researcher.d.ts +0 -9
- package/dist/types/exa/search.d.ts +0 -9
- package/dist/types/exa/types.d.ts +0 -155
- package/dist/types/exa/websets.d.ts +0 -9
- package/dist/types/exec/bash-executor.d.ts +0 -64
- package/dist/types/exec/exec.d.ts +0 -25
- package/dist/types/exec/idle-timeout-watchdog.d.ts +0 -18
- package/dist/types/exec/non-interactive-env.d.ts +0 -1
- package/dist/types/export/custom-share.d.ts +0 -20
- package/dist/types/export/html/index.d.ts +0 -10
- package/dist/types/export/html/template.generated.d.ts +0 -1
- package/dist/types/export/html/template.macro.d.ts +0 -5
- package/dist/types/export/ttsr.d.ts +0 -55
- package/dist/types/extensibility/custom-commands/bundled/ci-green/index.d.ts +0 -9
- package/dist/types/extensibility/custom-commands/index.d.ts +0 -2
- package/dist/types/extensibility/custom-commands/loader.d.ts +0 -29
- package/dist/types/extensibility/custom-commands/types.d.ts +0 -106
- package/dist/types/extensibility/custom-tools/index.d.ts +0 -6
- package/dist/types/extensibility/custom-tools/loader.d.ts +0 -69
- package/dist/types/extensibility/custom-tools/types.d.ts +0 -229
- package/dist/types/extensibility/custom-tools/wrapper.d.ts +0 -24
- package/dist/types/extensibility/extensions/compact-handler.d.ts +0 -26
- package/dist/types/extensibility/extensions/get-commands-handler.d.ts +0 -29
- package/dist/types/extensibility/extensions/index.d.ts +0 -9
- package/dist/types/extensibility/extensions/loader.d.ts +0 -43
- package/dist/types/extensibility/extensions/prefix-command-bridge.d.ts +0 -35
- package/dist/types/extensibility/extensions/runner.d.ts +0 -135
- package/dist/types/extensibility/extensions/types.d.ts +0 -920
- package/dist/types/extensibility/extensions/wrapper.d.ts +0 -55
- package/dist/types/extensibility/hooks/index.d.ts +0 -5
- package/dist/types/extensibility/hooks/loader.d.ts +0 -89
- package/dist/types/extensibility/hooks/runner.d.ts +0 -126
- package/dist/types/extensibility/hooks/tool-wrapper.d.ts +0 -25
- package/dist/types/extensibility/hooks/types.d.ts +0 -431
- package/dist/types/extensibility/plugins/doctor.d.ts +0 -3
- package/dist/types/extensibility/plugins/git-url.d.ts +0 -34
- package/dist/types/extensibility/plugins/index.d.ts +0 -8
- package/dist/types/extensibility/plugins/installer.d.ts +0 -5
- package/dist/types/extensibility/plugins/legacy-pi-compat.d.ts +0 -2
- package/dist/types/extensibility/plugins/loader.d.ts +0 -31
- package/dist/types/extensibility/plugins/manager.d.ts +0 -65
- package/dist/types/extensibility/plugins/marketplace/cache.d.ts +0 -41
- package/dist/types/extensibility/plugins/marketplace/fetcher.d.ts +0 -48
- package/dist/types/extensibility/plugins/marketplace/index.d.ts +0 -6
- package/dist/types/extensibility/plugins/marketplace/manager.d.ts +0 -57
- package/dist/types/extensibility/plugins/marketplace/registry.d.ts +0 -34
- package/dist/types/extensibility/plugins/marketplace/source-resolver.d.ts +0 -28
- package/dist/types/extensibility/plugins/marketplace/types.d.ts +0 -130
- package/dist/types/extensibility/plugins/parser.d.ts +0 -52
- package/dist/types/extensibility/plugins/security-scanner.d.ts +0 -31
- package/dist/types/extensibility/plugins/types.d.ts +0 -149
- package/dist/types/extensibility/runtime-skill-discovery.d.ts +0 -20
- package/dist/types/extensibility/shared-events.d.ts +0 -281
- package/dist/types/extensibility/skc-plugins/activation.d.ts +0 -14
- package/dist/types/extensibility/skc-plugins/compiler.d.ts +0 -19
- package/dist/types/extensibility/skc-plugins/constrained-hooks.d.ts +0 -29
- package/dist/types/extensibility/skc-plugins/index.d.ts +0 -18
- package/dist/types/extensibility/skc-plugins/injection.d.ts +0 -40
- package/dist/types/extensibility/skc-plugins/installer.d.ts +0 -13
- package/dist/types/extensibility/skc-plugins/loader.d.ts +0 -3
- package/dist/types/extensibility/skc-plugins/mcp-policy.d.ts +0 -26
- package/dist/types/extensibility/skc-plugins/observability.d.ts +0 -27
- package/dist/types/extensibility/skc-plugins/paths.d.ts +0 -8
- package/dist/types/extensibility/skc-plugins/prompt-appendix.d.ts +0 -16
- package/dist/types/extensibility/skc-plugins/registry.d.ts +0 -32
- package/dist/types/extensibility/skc-plugins/runtime-adapters.d.ts +0 -64
- package/dist/types/extensibility/skc-plugins/schema.d.ts +0 -3
- package/dist/types/extensibility/skc-plugins/session-validation.d.ts +0 -42
- package/dist/types/extensibility/skc-plugins/state.d.ts +0 -9
- package/dist/types/extensibility/skc-plugins/tools.d.ts +0 -8
- package/dist/types/extensibility/skc-plugins/types.d.ts +0 -220
- package/dist/types/extensibility/skc-plugins/validation.d.ts +0 -11
- package/dist/types/extensibility/skills.d.ts +0 -82
- package/dist/types/extensibility/slash-commands.d.ts +0 -48
- package/dist/types/extensibility/tool-proxy.d.ts +0 -4
- package/dist/types/extensibility/typebox.d.ts +0 -155
- package/dist/types/extensibility/utils.d.ts +0 -12
- package/dist/types/goals/index.d.ts +0 -3
- package/dist/types/goals/runtime.d.ts +0 -63
- package/dist/types/goals/state.d.ts +0 -32
- package/dist/types/goals/tools/goal-tool.d.ts +0 -62
- package/dist/types/harness-control-plane/classifier.d.ts +0 -13
- package/dist/types/harness-control-plane/control-endpoint.d.ts +0 -31
- package/dist/types/harness-control-plane/finalize.d.ts +0 -60
- package/dist/types/harness-control-plane/gc-adapter.d.ts +0 -3
- package/dist/types/harness-control-plane/operate.d.ts +0 -35
- package/dist/types/harness-control-plane/owner.d.ts +0 -53
- package/dist/types/harness-control-plane/phase-rollup.d.ts +0 -23
- package/dist/types/harness-control-plane/preserve.d.ts +0 -19
- package/dist/types/harness-control-plane/receipt-ingest.d.ts +0 -19
- package/dist/types/harness-control-plane/receipt-spool.d.ts +0 -19
- package/dist/types/harness-control-plane/receipts.d.ts +0 -149
- package/dist/types/harness-control-plane/rpc-adapter.d.ts +0 -69
- package/dist/types/harness-control-plane/seams.d.ts +0 -21
- package/dist/types/harness-control-plane/session-lease.d.ts +0 -65
- package/dist/types/harness-control-plane/state-machine.d.ts +0 -24
- package/dist/types/harness-control-plane/storage.d.ts +0 -81
- package/dist/types/harness-control-plane/types.d.ts +0 -187
- package/dist/types/hashline/anchors.d.ts +0 -20
- package/dist/types/hashline/apply.d.ts +0 -14
- package/dist/types/hashline/constants.d.ts +0 -19
- package/dist/types/hashline/diff-preview.d.ts +0 -2
- package/dist/types/hashline/diff.d.ts +0 -17
- package/dist/types/hashline/execute.d.ts +0 -4
- package/dist/types/hashline/hash.d.ts +0 -123
- package/dist/types/hashline/index.d.ts +0 -13
- package/dist/types/hashline/input.d.ts +0 -11
- package/dist/types/hashline/parser.d.ts +0 -7
- package/dist/types/hashline/prefixes.d.ts +0 -7
- package/dist/types/hashline/recovery.d.ts +0 -27
- package/dist/types/hashline/stream.d.ts +0 -2
- package/dist/types/hashline/types.d.ts +0 -77
- package/dist/types/hindsight/backend.d.ts +0 -13
- package/dist/types/hindsight/bank.d.ts +0 -54
- package/dist/types/hindsight/client.d.ts +0 -224
- package/dist/types/hindsight/config.d.ts +0 -51
- package/dist/types/hindsight/content.d.ts +0 -70
- package/dist/types/hindsight/index.d.ts +0 -8
- package/dist/types/hindsight/mental-models.d.ts +0 -125
- package/dist/types/hindsight/state.d.ts +0 -99
- package/dist/types/hindsight/transcript.d.ts +0 -28
- package/dist/types/hooks/codex-native-hooks-config.d.ts +0 -29
- package/dist/types/hooks/native-skill-hook.d.ts +0 -28
- package/dist/types/hooks/skill-keywords.d.ts +0 -18
- package/dist/types/hooks/skill-state.d.ts +0 -93
- package/dist/types/i18n/index.d.ts +0 -38
- package/dist/types/i18n/messages/de.d.ts +0 -2
- package/dist/types/i18n/messages/de.messages.d.ts +0 -1
- package/dist/types/i18n/messages/de.settings.d.ts +0 -1
- package/dist/types/i18n/messages/en.d.ts +0 -81
- package/dist/types/i18n/messages/es.d.ts +0 -2
- package/dist/types/i18n/messages/es.messages.d.ts +0 -1
- package/dist/types/i18n/messages/es.settings.d.ts +0 -1
- package/dist/types/i18n/messages/fr.d.ts +0 -2
- package/dist/types/i18n/messages/fr.messages.d.ts +0 -1
- package/dist/types/i18n/messages/fr.settings.d.ts +0 -1
- package/dist/types/i18n/messages/ja.d.ts +0 -2
- package/dist/types/i18n/messages/ja.messages.d.ts +0 -1
- package/dist/types/i18n/messages/ja.settings.d.ts +0 -1
- package/dist/types/i18n/messages/ko.d.ts +0 -2
- package/dist/types/i18n/messages/ko.messages.d.ts +0 -1
- package/dist/types/i18n/messages/ko.settings.d.ts +0 -1
- package/dist/types/i18n/messages/zh.d.ts +0 -2
- package/dist/types/i18n/messages/zh.messages.d.ts +0 -1
- package/dist/types/i18n/messages/zh.settings.d.ts +0 -1
- package/dist/types/index.d.ts +0 -33
- package/dist/types/internal-urls/agent-protocol.d.ts +0 -12
- package/dist/types/internal-urls/artifact-protocol.d.ts +0 -6
- package/dist/types/internal-urls/docs-index.generated.d.ts +0 -2
- package/dist/types/internal-urls/index.d.ts +0 -20
- package/dist/types/internal-urls/issue-pr-protocol.d.ts +0 -17
- package/dist/types/internal-urls/json-query.d.ts +0 -30
- package/dist/types/internal-urls/local-protocol.d.ts +0 -43
- package/dist/types/internal-urls/mcp-protocol.d.ts +0 -12
- package/dist/types/internal-urls/memory-protocol.d.ts +0 -23
- package/dist/types/internal-urls/parse.d.ts +0 -19
- package/dist/types/internal-urls/registry-helpers.d.ts +0 -11
- package/dist/types/internal-urls/router.d.ts +0 -14
- package/dist/types/internal-urls/rule-protocol.d.ts +0 -6
- package/dist/types/internal-urls/skc-protocol.d.ts +0 -12
- package/dist/types/internal-urls/skill-protocol.d.ts +0 -13
- package/dist/types/internal-urls/types.d.ts +0 -109
- package/dist/types/lsp/client.d.ts +0 -66
- package/dist/types/lsp/clients/biome-client.d.ts +0 -16
- package/dist/types/lsp/clients/index.d.ts +0 -19
- package/dist/types/lsp/clients/lsp-linter-client.d.ts +0 -16
- package/dist/types/lsp/clients/swiftlint-client.d.ts +0 -20
- package/dist/types/lsp/config.d.ts +0 -65
- package/dist/types/lsp/edits.d.ts +0 -23
- package/dist/types/lsp/index.d.ts +0 -130
- package/dist/types/lsp/lspmux.d.ts +0 -58
- package/dist/types/lsp/render.d.ts +0 -32
- package/dist/types/lsp/startup-events.d.ts +0 -12
- package/dist/types/lsp/types.d.ts +0 -306
- package/dist/types/lsp/utils.d.ts +0 -108
- package/dist/types/main.d.ts +0 -139
- package/dist/types/memories/index.d.ts +0 -28
- package/dist/types/memories/storage.d.ts +0 -110
- package/dist/types/memory-backend/index.d.ts +0 -4
- package/dist/types/memory-backend/local-backend.d.ts +0 -9
- package/dist/types/memory-backend/off-backend.d.ts +0 -7
- package/dist/types/memory-backend/resolve.d.ts +0 -15
- package/dist/types/memory-backend/types.d.ts +0 -61
- package/dist/types/migrate/action-planner.d.ts +0 -11
- package/dist/types/migrate/adapters/claude-code.d.ts +0 -2
- package/dist/types/migrate/adapters/codex.d.ts +0 -5
- package/dist/types/migrate/adapters/index.d.ts +0 -45
- package/dist/types/migrate/adapters/opencode.d.ts +0 -2
- package/dist/types/migrate/executor.d.ts +0 -2
- package/dist/types/migrate/mcp-mapper.d.ts +0 -20
- package/dist/types/migrate/report.d.ts +0 -18
- package/dist/types/migrate/skill-normalizer.d.ts +0 -27
- package/dist/types/migrate/types.d.ts +0 -126
- package/dist/types/modes/acp/acp-agent.d.ts +0 -78
- package/dist/types/modes/acp/acp-client-bridge.d.ts +0 -9
- package/dist/types/modes/acp/acp-event-mapper.d.ts +0 -36
- package/dist/types/modes/acp/acp-mode.d.ts +0 -17
- package/dist/types/modes/acp/index.d.ts +0 -2
- package/dist/types/modes/acp/terminal-auth.d.ts +0 -6
- package/dist/types/modes/bridge/auth.d.ts +0 -12
- package/dist/types/modes/bridge/bridge-client-bridge.d.ts +0 -9
- package/dist/types/modes/bridge/bridge-mode.d.ts +0 -47
- package/dist/types/modes/bridge/bridge-ui-context.d.ts +0 -88
- package/dist/types/modes/bridge/event-stream.d.ts +0 -8
- package/dist/types/modes/components/agent-dashboard.d.ts +0 -21
- package/dist/types/modes/components/assistant-message.d.ts +0 -20
- package/dist/types/modes/components/bash-execution.d.ts +0 -29
- package/dist/types/modes/components/bordered-loader.d.ts +0 -11
- package/dist/types/modes/components/branch-summary-message.d.ts +0 -13
- package/dist/types/modes/components/btw-panel.d.ts +0 -16
- package/dist/types/modes/components/compaction-summary-message.d.ts +0 -13
- package/dist/types/modes/components/countdown-timer.d.ts +0 -14
- package/dist/types/modes/components/custom-editor.d.ts +0 -69
- package/dist/types/modes/components/custom-message.d.ts +0 -15
- package/dist/types/modes/components/custom-model-preset-wizard.d.ts +0 -12
- package/dist/types/modes/components/custom-provider-wizard.d.ts +0 -10
- package/dist/types/modes/components/diff.d.ts +0 -11
- package/dist/types/modes/components/dynamic-border.d.ts +0 -14
- package/dist/types/modes/components/eval-execution.d.ts +0 -23
- package/dist/types/modes/components/execution-shared.d.ts +0 -46
- package/dist/types/modes/components/extensions/extension-dashboard.d.ts +0 -26
- package/dist/types/modes/components/extensions/extension-list.d.ts +0 -39
- package/dist/types/modes/components/extensions/index.d.ts +0 -8
- package/dist/types/modes/components/extensions/inspector-panel.d.ts +0 -8
- package/dist/types/modes/components/extensions/state-manager.d.ts +0 -50
- package/dist/types/modes/components/extensions/types.d.ts +0 -151
- package/dist/types/modes/components/footer.d.ts +0 -30
- package/dist/types/modes/components/history-search.d.ts +0 -7
- package/dist/types/modes/components/hook-editor.d.ts +0 -18
- package/dist/types/modes/components/hook-input.d.ts +0 -15
- package/dist/types/modes/components/hook-message.d.ts +0 -15
- package/dist/types/modes/components/hook-selector.d.ts +0 -59
- package/dist/types/modes/components/index.d.ts +0 -35
- package/dist/types/modes/components/irc-sidebar.d.ts +0 -32
- package/dist/types/modes/components/jobs-overlay-model.d.ts +0 -31
- package/dist/types/modes/components/jobs-overlay.d.ts +0 -30
- package/dist/types/modes/components/keybinding-hints.d.ts +0 -40
- package/dist/types/modes/components/login-dialog.d.ts +0 -32
- package/dist/types/modes/components/message-frame.d.ts +0 -42
- package/dist/types/modes/components/model-selector.d.ts +0 -60
- package/dist/types/modes/components/oauth-selector.d.ts +0 -17
- package/dist/types/modes/components/pet-selector.d.ts +0 -11
- package/dist/types/modes/components/plugin-selector.d.ts +0 -26
- package/dist/types/modes/components/plugin-settings.d.ts +0 -66
- package/dist/types/modes/components/provider-onboarding-selector.d.ts +0 -7
- package/dist/types/modes/components/queue-mode-selector.d.ts +0 -9
- package/dist/types/modes/components/queued-message-selector.d.ts +0 -12
- package/dist/types/modes/components/read-tool-group.d.ts +0 -30
- package/dist/types/modes/components/runtime-mcp-add-wizard.d.ts +0 -26
- package/dist/types/modes/components/sayknow-pet-widget.d.ts +0 -59
- package/dist/types/modes/components/session-observer-overlay.d.ts +0 -11
- package/dist/types/modes/components/session-selector.d.ts +0 -43
- package/dist/types/modes/components/settings-defs.d.ts +0 -50
- package/dist/types/modes/components/settings-selector.d.ts +0 -64
- package/dist/types/modes/components/show-images-selector.d.ts +0 -9
- package/dist/types/modes/components/skill-hud/render.d.ts +0 -2
- package/dist/types/modes/components/skill-message.d.ts +0 -10
- package/dist/types/modes/components/status-line/context-thresholds.d.ts +0 -4
- package/dist/types/modes/components/status-line/git-utils.d.ts +0 -28
- package/dist/types/modes/components/status-line/index.d.ts +0 -4
- package/dist/types/modes/components/status-line/presets.d.ts +0 -3
- package/dist/types/modes/components/status-line/segments.d.ts +0 -5
- package/dist/types/modes/components/status-line/separators.d.ts +0 -3
- package/dist/types/modes/components/status-line/token-rate.d.ts +0 -10
- package/dist/types/modes/components/status-line/types.d.ts +0 -83
- package/dist/types/modes/components/status-line.d.ts +0 -94
- package/dist/types/modes/components/theme-selector.d.ts +0 -10
- package/dist/types/modes/components/thinking-selector.d.ts +0 -15
- package/dist/types/modes/components/todo-reminder.d.ts +0 -13
- package/dist/types/modes/components/tool-execution.d.ts +0 -55
- package/dist/types/modes/components/tree-selector.d.ts +0 -31
- package/dist/types/modes/components/ttsr-notification.d.ts +0 -13
- package/dist/types/modes/components/two-column-body.d.ts +0 -9
- package/dist/types/modes/components/user-message-selector.d.ts +0 -28
- package/dist/types/modes/components/user-message.d.ts +0 -8
- package/dist/types/modes/components/visual-truncate.d.ts +0 -19
- package/dist/types/modes/components/welcome.d.ts +0 -46
- package/dist/types/modes/controllers/btw-controller.d.ts +0 -10
- package/dist/types/modes/controllers/command-controller-shared.d.ts +0 -47
- package/dist/types/modes/controllers/command-controller.d.ts +0 -40
- package/dist/types/modes/controllers/event-controller.d.ts +0 -21
- package/dist/types/modes/controllers/extension-ui-controller.d.ts +0 -80
- package/dist/types/modes/controllers/input-controller.d.ts +0 -42
- package/dist/types/modes/controllers/runtime-mcp-command-controller.d.ts +0 -10
- package/dist/types/modes/controllers/selector-controller.d.ts +0 -68
- package/dist/types/modes/controllers/ssh-command-controller.d.ts +0 -10
- package/dist/types/modes/controllers/todo-command-controller.d.ts +0 -7
- package/dist/types/modes/emoji-autocomplete.d.ts +0 -16
- package/dist/types/modes/index.d.ts +0 -10
- package/dist/types/modes/interactive-mode.d.ts +0 -297
- package/dist/types/modes/irc-observation-ledger.d.ts +0 -22
- package/dist/types/modes/jobs-observer.d.ts +0 -57
- package/dist/types/modes/oauth-manual-input.d.ts +0 -8
- package/dist/types/modes/print-mode.d.ts +0 -44
- package/dist/types/modes/prompt-action-autocomplete.d.ts +0 -50
- package/dist/types/modes/rpc/host-tools.d.ts +0 -1
- package/dist/types/modes/rpc/host-uris.d.ts +0 -1
- package/dist/types/modes/rpc/rpc-client.d.ts +0 -299
- package/dist/types/modes/rpc/rpc-mode.d.ts +0 -86
- package/dist/types/modes/rpc/rpc-socket-security.d.ts +0 -10
- package/dist/types/modes/rpc/rpc-types.d.ts +0 -810
- package/dist/types/modes/runtime-init.d.ts +0 -21
- package/dist/types/modes/session-observer-registry.d.ts +0 -26
- package/dist/types/modes/shared/agent-wire/approval-gate.d.ts +0 -57
- package/dist/types/modes/shared/agent-wire/command-contract.d.ts +0 -18
- package/dist/types/modes/shared/agent-wire/command-dispatch.d.ts +0 -37
- package/dist/types/modes/shared/agent-wire/command-validation.d.ts +0 -2
- package/dist/types/modes/shared/agent-wire/deep-interview-gate.d.ts +0 -102
- package/dist/types/modes/shared/agent-wire/event-contract.d.ts +0 -103
- package/dist/types/modes/shared/agent-wire/event-envelope.d.ts +0 -60
- package/dist/types/modes/shared/agent-wire/event-observation.d.ts +0 -37
- package/dist/types/modes/shared/agent-wire/handshake.d.ts +0 -56
- package/dist/types/modes/shared/agent-wire/host-tool-bridge.d.ts +0 -16
- package/dist/types/modes/shared/agent-wire/host-uri-bridge.d.ts +0 -17
- package/dist/types/modes/shared/agent-wire/protocol.d.ts +0 -25
- package/dist/types/modes/shared/agent-wire/responses.d.ts +0 -4
- package/dist/types/modes/shared/agent-wire/scopes.d.ts +0 -18
- package/dist/types/modes/shared/agent-wire/session-registry.d.ts +0 -25
- package/dist/types/modes/shared/agent-wire/ui-request-broker.d.ts +0 -42
- package/dist/types/modes/shared/agent-wire/ui-result.d.ts +0 -27
- package/dist/types/modes/shared/agent-wire/unattended-action-policy.d.ts +0 -29
- package/dist/types/modes/shared/agent-wire/unattended-audit.d.ts +0 -68
- package/dist/types/modes/shared/agent-wire/unattended-run-controller.d.ts +0 -166
- package/dist/types/modes/shared/agent-wire/unattended-session.d.ts +0 -127
- package/dist/types/modes/shared/agent-wire/workflow-gate-broker.d.ts +0 -154
- package/dist/types/modes/shared/agent-wire/workflow-gate-schema.d.ts +0 -39
- package/dist/types/modes/shared.d.ts +0 -15
- package/dist/types/modes/slash-command-visibility.d.ts +0 -1
- package/dist/types/modes/theme/defaults/index.d.ts +0 -652
- package/dist/types/modes/theme/mermaid-cache.d.ts +0 -9
- package/dist/types/modes/theme/shimmer.d.ts +0 -38
- package/dist/types/modes/theme/theme.d.ts +0 -275
- package/dist/types/modes/tmux-scroll.d.ts +0 -18
- package/dist/types/modes/types.d.ts +0 -318
- package/dist/types/modes/utils/abort-message.d.ts +0 -4
- package/dist/types/modes/utils/context-usage.d.ts +0 -36
- package/dist/types/modes/utils/hotkeys-markdown.d.ts +0 -5
- package/dist/types/modes/utils/injected-user-submission.d.ts +0 -52
- package/dist/types/modes/utils/irc-message.d.ts +0 -38
- package/dist/types/modes/utils/keybinding-matchers.d.ts +0 -10
- package/dist/types/modes/utils/terminal-bell.d.ts +0 -3
- package/dist/types/modes/utils/tools-markdown.d.ts +0 -5
- package/dist/types/modes/utils/ui-helpers.d.ts +0 -75
- package/dist/types/notifications/attachment-registry.d.ts +0 -17
- package/dist/types/notifications/chat-adapters.d.ts +0 -9
- package/dist/types/notifications/config-commands.d.ts +0 -64
- package/dist/types/notifications/config.d.ts +0 -132
- package/dist/types/notifications/daemon-paths.d.ts +0 -17
- package/dist/types/notifications/engine.d.ts +0 -59
- package/dist/types/notifications/helpers.d.ts +0 -55
- package/dist/types/notifications/html-format.d.ts +0 -75
- package/dist/types/notifications/index.d.ts +0 -219
- package/dist/types/notifications/lifecycle-commands.d.ts +0 -69
- package/dist/types/notifications/lifecycle-control-runtime.d.ts +0 -107
- package/dist/types/notifications/lifecycle-orchestrator.d.ts +0 -144
- package/dist/types/notifications/managed-daemon.d.ts +0 -48
- package/dist/types/notifications/notification-service.d.ts +0 -144
- package/dist/types/notifications/operator-runtime.d.ts +0 -52
- package/dist/types/notifications/rate-limit-pool.d.ts +0 -113
- package/dist/types/notifications/recent-activity.d.ts +0 -39
- package/dist/types/notifications/reply-sent-store.d.ts +0 -53
- package/dist/types/notifications/rich-draft.d.ts +0 -68
- package/dist/types/notifications/rich-render.d.ts +0 -90
- package/dist/types/notifications/telegram-cli.d.ts +0 -19
- package/dist/types/notifications/telegram-daemon-cli.d.ts +0 -28
- package/dist/types/notifications/telegram-daemon-control.d.ts +0 -70
- package/dist/types/notifications/telegram-daemon.d.ts +0 -558
- package/dist/types/notifications/telegram-reference.d.ts +0 -136
- package/dist/types/notifications/threaded-inbound.d.ts +0 -77
- package/dist/types/notifications/threaded-render.d.ts +0 -88
- package/dist/types/notifications/topic-registry.d.ts +0 -89
- package/dist/types/plan-mode/approved-plan.d.ts +0 -49
- package/dist/types/plan-mode/state.d.ts +0 -6
- package/dist/types/registry/agent-registry.d.ts +0 -64
- package/dist/types/reminders/star-reminder.d.ts +0 -115
- package/dist/types/research-plan/index.d.ts +0 -1
- package/dist/types/research-plan/ledger.d.ts +0 -33
- package/dist/types/rlm/artifacts.d.ts +0 -9
- package/dist/types/rlm/complete-research-tool.d.ts +0 -35
- package/dist/types/rlm/data-context.d.ts +0 -6
- package/dist/types/rlm/index.d.ts +0 -47
- package/dist/types/rlm/notebook.d.ts +0 -12
- package/dist/types/rlm/preset.d.ts +0 -23
- package/dist/types/rlm/python-tool.d.ts +0 -16
- package/dist/types/rlm/report.d.ts +0 -14
- package/dist/types/rlm/types.d.ts +0 -37
- package/dist/types/runtime/process-lifecycle.d.ts +0 -108
- package/dist/types/runtime-api-key.d.ts +0 -7
- package/dist/types/runtime-credential-selector.d.ts +0 -7
- package/dist/types/runtime-mcp/client.d.ts +0 -74
- package/dist/types/runtime-mcp/config-writer.d.ts +0 -89
- package/dist/types/runtime-mcp/config.d.ts +0 -77
- package/dist/types/runtime-mcp/discoverable-tool-metadata.d.ts +0 -7
- package/dist/types/runtime-mcp/index.d.ts +0 -18
- package/dist/types/runtime-mcp/json-rpc.d.ts +0 -22
- package/dist/types/runtime-mcp/loader.d.ts +0 -45
- package/dist/types/runtime-mcp/manager.d.ts +0 -195
- package/dist/types/runtime-mcp/oauth-discovery.d.ts +0 -40
- package/dist/types/runtime-mcp/oauth-flow.d.ts +0 -59
- package/dist/types/runtime-mcp/render.d.ts +0 -25
- package/dist/types/runtime-mcp/smithery-auth.d.ts +0 -16
- package/dist/types/runtime-mcp/smithery-connect.d.ts +0 -38
- package/dist/types/runtime-mcp/smithery-registry.d.ts +0 -51
- package/dist/types/runtime-mcp/tool-bridge.d.ts +0 -86
- package/dist/types/runtime-mcp/tool-cache.d.ts +0 -8
- package/dist/types/runtime-mcp/transports/http.d.ts +0 -36
- package/dist/types/runtime-mcp/transports/index.d.ts +0 -5
- package/dist/types/runtime-mcp/transports/stdio.d.ts +0 -29
- package/dist/types/runtime-mcp/types.d.ts +0 -349
- package/dist/types/sdk.d.ts +0 -256
- package/dist/types/secrets/index.d.ts +0 -9
- package/dist/types/secrets/obfuscator.d.ts +0 -23
- package/dist/types/secrets/regex.d.ts +0 -2
- package/dist/types/session/agent-session.d.ts +0 -1238
- package/dist/types/session/agent-storage.d.ts +0 -101
- package/dist/types/session/artifacts.d.ts +0 -64
- package/dist/types/session/auth-broker-config.d.ts +0 -13
- package/dist/types/session/auth-storage.d.ts +0 -6
- package/dist/types/session/blob-store.d.ts +0 -160
- package/dist/types/session/cache-economics.d.ts +0 -79
- package/dist/types/session/client-bridge.d.ts +0 -88
- package/dist/types/session/context-estimation.d.ts +0 -31
- package/dist/types/session/contribution-prep.d.ts +0 -47
- package/dist/types/session/file-mention-pruning.d.ts +0 -8
- package/dist/types/session/history-storage.d.ts +0 -16
- package/dist/types/session/messages.d.ts +0 -169
- package/dist/types/session/provider-safety-stop.d.ts +0 -5
- package/dist/types/session/session-dump-format.d.ts +0 -22
- package/dist/types/session/session-manager.d.ts +0 -781
- package/dist/types/session/session-storage.d.ts +0 -230
- package/dist/types/session/streaming-output.d.ts +0 -209
- package/dist/types/session/tool-choice-queue.d.ts +0 -84
- package/dist/types/session/yield-queue.d.ts +0 -24
- package/dist/types/setup/credential-auto-import.d.ts +0 -63
- package/dist/types/setup/credential-import.d.ts +0 -82
- package/dist/types/setup/hermes-setup.d.ts +0 -78
- package/dist/types/setup/host-plugin-setup.d.ts +0 -39
- package/dist/types/setup/model-onboarding-guidance.d.ts +0 -9
- package/dist/types/setup/provider-onboarding.d.ts +0 -52
- package/dist/types/skc-runtime/cli-write-receipt.d.ts +0 -24
- package/dist/types/skc-runtime/deep-interview-ambiguity.d.ts +0 -39
- package/dist/types/skc-runtime/deep-interview-recorder.d.ts +0 -114
- package/dist/types/skc-runtime/deep-interview-runtime.d.ts +0 -36
- package/dist/types/skc-runtime/deep-interview-state.d.ts +0 -127
- package/dist/types/skc-runtime/gc-render.d.ts +0 -6
- package/dist/types/skc-runtime/gc-runtime.d.ts +0 -134
- package/dist/types/skc-runtime/goal-mode-request.d.ts +0 -53
- package/dist/types/skc-runtime/launch-tmux.d.ts +0 -78
- package/dist/types/skc-runtime/launch-worktree.d.ts +0 -46
- package/dist/types/skc-runtime/ledger-event-renderer.d.ts +0 -68
- package/dist/types/skc-runtime/psmux-detect.d.ts +0 -78
- package/dist/types/skc-runtime/ralplan-runtime.d.ts +0 -26
- package/dist/types/skc-runtime/restricted-role-agent-bash.d.ts +0 -3
- package/dist/types/skc-runtime/session-layout.d.ts +0 -59
- package/dist/types/skc-runtime/session-resolution.d.ts +0 -47
- package/dist/types/skc-runtime/session-state-sidecar.d.ts +0 -70
- package/dist/types/skc-runtime/state-graph.d.ts +0 -4
- package/dist/types/skc-runtime/state-migrations.d.ts +0 -33
- package/dist/types/skc-runtime/state-renderer.d.ts +0 -70
- package/dist/types/skc-runtime/state-runtime.d.ts +0 -38
- package/dist/types/skc-runtime/state-schema.d.ts +0 -319
- package/dist/types/skc-runtime/state-validation.d.ts +0 -6
- package/dist/types/skc-runtime/state-writer.d.ts +0 -244
- package/dist/types/skc-runtime/team-gc.d.ts +0 -7
- package/dist/types/skc-runtime/team-runtime.d.ts +0 -355
- package/dist/types/skc-runtime/tmux-common.d.ts +0 -111
- package/dist/types/skc-runtime/tmux-gc.d.ts +0 -7
- package/dist/types/skc-runtime/tmux-owner-isolation-cli.d.ts +0 -5
- package/dist/types/skc-runtime/tmux-owner-isolation.d.ts +0 -309
- package/dist/types/skc-runtime/tmux-sessions.d.ts +0 -94
- package/dist/types/skc-runtime/ultragoal-guard.d.ts +0 -93
- package/dist/types/skc-runtime/ultragoal-receipt-freshness.d.ts +0 -42
- package/dist/types/skc-runtime/ultragoal-runtime.d.ts +0 -410
- package/dist/types/skc-runtime/workflow-command-ref.d.ts +0 -43
- package/dist/types/skc-runtime/workflow-manifest.d.ts +0 -54
- package/dist/types/skill-state/active-state.d.ts +0 -126
- package/dist/types/skill-state/canonical-skills.d.ts +0 -3
- package/dist/types/skill-state/deep-interview-mutation-guard.d.ts +0 -38
- package/dist/types/skill-state/initial-phase.d.ts +0 -12
- package/dist/types/skill-state/workflow-hud.d.ts +0 -83
- package/dist/types/skill-state/workflow-state-contract.d.ts +0 -57
- package/dist/types/skill-state/workflow-state-version.d.ts +0 -3
- package/dist/types/slash-commands/acp-builtins.d.ts +0 -18
- package/dist/types/slash-commands/builtin-registry.d.ts +0 -24
- package/dist/types/slash-commands/helpers/context-report.d.ts +0 -7
- package/dist/types/slash-commands/helpers/fast-status-report.d.ts +0 -82
- package/dist/types/slash-commands/helpers/format.d.ts +0 -10
- package/dist/types/slash-commands/helpers/mcp.d.ts +0 -3
- package/dist/types/slash-commands/helpers/parse.d.ts +0 -33
- package/dist/types/slash-commands/helpers/ssh.d.ts +0 -3
- package/dist/types/slash-commands/helpers/todo.d.ts +0 -3
- package/dist/types/slash-commands/helpers/usage-report.d.ts +0 -7
- package/dist/types/slash-commands/types.d.ts +0 -120
- package/dist/types/ssh/config-writer.d.ts +0 -49
- package/dist/types/ssh/connection-manager.d.ts +0 -33
- package/dist/types/ssh/ssh-executor.d.ts +0 -37
- package/dist/types/ssh/sshfs-mount.d.ts +0 -6
- package/dist/types/ssh/utils.d.ts +0 -2
- package/dist/types/stt/downloader.d.ts +0 -9
- package/dist/types/stt/index.d.ts +0 -3
- package/dist/types/stt/recorder.d.ts +0 -13
- package/dist/types/stt/setup.d.ts +0 -18
- package/dist/types/stt/stt-controller.d.ts +0 -16
- package/dist/types/stt/transcriber.d.ts +0 -16
- package/dist/types/system-prompt.d.ts +0 -118
- package/dist/types/task/agents.d.ts +0 -28
- package/dist/types/task/commands.d.ts +0 -35
- package/dist/types/task/discovery.d.ts +0 -19
- package/dist/types/task/executor.d.ts +0 -127
- package/dist/types/task/fork-context-advisory.d.ts +0 -13
- package/dist/types/task/fork-context-budget.d.ts +0 -7
- package/dist/types/task/id.d.ts +0 -7
- package/dist/types/task/index.d.ts +0 -43
- package/dist/types/task/name-generator.d.ts +0 -16
- package/dist/types/task/output-manager.d.ts +0 -36
- package/dist/types/task/parallel.d.ts +0 -34
- package/dist/types/task/receipt.d.ts +0 -89
- package/dist/types/task/render.d.ts +0 -28
- package/dist/types/task/roi-reconciliation.d.ts +0 -27
- package/dist/types/task/simple-mode.d.ts +0 -8
- package/dist/types/task/skc-command.d.ts +0 -7
- package/dist/types/task/spawn-gate.d.ts +0 -29
- package/dist/types/task/subprocess-tool-registry.d.ts +0 -72
- package/dist/types/task/token-log.d.ts +0 -30
- package/dist/types/task/token-log.test.d.ts +0 -1
- package/dist/types/task/types.d.ts +0 -516
- package/dist/types/task/worktree.d.ts +0 -94
- package/dist/types/thinking-metadata.d.ts +0 -16
- package/dist/types/thinking.d.ts +0 -22
- package/dist/types/tool-discovery/tool-index.d.ts +0 -112
- package/dist/types/tools/archive-reader.d.ts +0 -40
- package/dist/types/tools/ask-answer-registry.d.ts +0 -18
- package/dist/types/tools/ask.d.ts +0 -123
- package/dist/types/tools/ast-edit.d.ts +0 -77
- package/dist/types/tools/ast-grep.d.ts +0 -69
- package/dist/types/tools/auto-generated-guard.d.ts +0 -17
- package/dist/types/tools/bash-allowed-prefixes.d.ts +0 -10
- package/dist/types/tools/bash-command-fixup.d.ts +0 -11
- package/dist/types/tools/bash-interactive.d.ts +0 -16
- package/dist/types/tools/bash-interceptor.d.ts +0 -24
- package/dist/types/tools/bash-pty-selection.d.ts +0 -7
- package/dist/types/tools/bash-skill-urls.d.ts +0 -31
- package/dist/types/tools/bash.d.ts +0 -157
- package/dist/types/tools/bisect.d.ts +0 -81
- package/dist/types/tools/browser/actions.d.ts +0 -54
- package/dist/types/tools/browser/attach.d.ts +0 -50
- package/dist/types/tools/browser/launch.d.ts +0 -62
- package/dist/types/tools/browser/readable.d.ts +0 -17
- package/dist/types/tools/browser/registry.d.ts +0 -57
- package/dist/types/tools/browser/render.d.ts +0 -53
- package/dist/types/tools/browser/tab-protocol.d.ts +0 -144
- package/dist/types/tools/browser/tab-supervisor.d.ts +0 -109
- package/dist/types/tools/browser/tab-worker-entry.d.ts +0 -1
- package/dist/types/tools/browser/tab-worker.d.ts +0 -19
- package/dist/types/tools/browser.d.ts +0 -211
- package/dist/types/tools/calculator.d.ts +0 -76
- package/dist/types/tools/checkpoint.d.ts +0 -63
- package/dist/types/tools/composer-bash-policy.d.ts +0 -14
- package/dist/types/tools/computer/render.d.ts +0 -17
- package/dist/types/tools/computer-gc.d.ts +0 -23
- package/dist/types/tools/computer.d.ts +0 -465
- package/dist/types/tools/conflict-detect.d.ts +0 -205
- package/dist/types/tools/context.d.ts +0 -19
- package/dist/types/tools/cron.d.ts +0 -91
- package/dist/types/tools/debug.d.ts +0 -209
- package/dist/types/tools/eval.d.ts +0 -108
- package/dist/types/tools/fetch.d.ts +0 -84
- package/dist/types/tools/file-recorder.d.ts +0 -13
- package/dist/types/tools/find.d.ts +0 -95
- package/dist/types/tools/fs-cache-invalidation.d.ts +0 -15
- package/dist/types/tools/gh-format.d.ts +0 -6
- package/dist/types/tools/gh-renderer.d.ts +0 -25
- package/dist/types/tools/gh.d.ts +0 -356
- package/dist/types/tools/github-cache.d.ts +0 -103
- package/dist/types/tools/grouped-file-output.d.ts +0 -36
- package/dist/types/tools/hindsight-recall.d.ts +0 -21
- package/dist/types/tools/hindsight-reflect.d.ts +0 -23
- package/dist/types/tools/hindsight-retain.d.ts +0 -27
- package/dist/types/tools/image-gen.d.ts +0 -79
- package/dist/types/tools/index.d.ts +0 -371
- package/dist/types/tools/irc.d.ts +0 -79
- package/dist/types/tools/job.d.ts +0 -78
- package/dist/types/tools/json-tree.d.ts +0 -23
- package/dist/types/tools/jtd-to-json-schema.d.ts +0 -29
- package/dist/types/tools/jtd-to-typescript.d.ts +0 -26
- package/dist/types/tools/jtd-utils.d.ts +0 -42
- package/dist/types/tools/list-limit.d.ts +0 -12
- package/dist/types/tools/match-line-format.d.ts +0 -11
- package/dist/types/tools/monitor.d.ts +0 -54
- package/dist/types/tools/output-meta.d.ts +0 -204
- package/dist/types/tools/path-utils.d.ts +0 -147
- package/dist/types/tools/plan-mode-guard.d.ts +0 -18
- package/dist/types/tools/read.d.ts +0 -85
- package/dist/types/tools/recipe/index.d.ts +0 -45
- package/dist/types/tools/recipe/render.d.ts +0 -36
- package/dist/types/tools/recipe/runner.d.ts +0 -60
- package/dist/types/tools/recipe/runners/cargo.d.ts +0 -16
- package/dist/types/tools/recipe/runners/index.d.ts +0 -2
- package/dist/types/tools/recipe/runners/just.d.ts +0 -2
- package/dist/types/tools/recipe/runners/make.d.ts +0 -2
- package/dist/types/tools/recipe/runners/pkg.d.ts +0 -2
- package/dist/types/tools/recipe/runners/task.d.ts +0 -2
- package/dist/types/tools/render-mermaid.d.ts +0 -37
- package/dist/types/tools/render-utils.d.ts +0 -158
- package/dist/types/tools/renderers.d.ts +0 -26
- package/dist/types/tools/resolve.d.ts +0 -80
- package/dist/types/tools/resource-gc.d.ts +0 -54
- package/dist/types/tools/review.d.ts +0 -63
- package/dist/types/tools/search-tool-bm25.d.ts +0 -65
- package/dist/types/tools/search.d.ts +0 -93
- package/dist/types/tools/skill-discovery.d.ts +0 -40
- package/dist/types/tools/skill.d.ts +0 -48
- package/dist/types/tools/sqlite-reader.d.ts +0 -94
- package/dist/types/tools/ssh.d.ts +0 -67
- package/dist/types/tools/subagent-render.d.ts +0 -31
- package/dist/types/tools/subagent.d.ts +0 -117
- package/dist/types/tools/telegram-send.d.ts +0 -39
- package/dist/types/tools/todo-write.d.ts +0 -120
- package/dist/types/tools/tool-errors.d.ts +0 -33
- package/dist/types/tools/tool-result.d.ts +0 -30
- package/dist/types/tools/tool-timeouts.d.ts +0 -56
- package/dist/types/tools/ultragoal-ask-guard.d.ts +0 -12
- package/dist/types/tools/vim.d.ts +0 -58
- package/dist/types/tools/write.d.ts +0 -59
- package/dist/types/tools/yield.d.ts +0 -25
- package/dist/types/tui/code-cell.d.ts +0 -32
- package/dist/types/tui/file-list.d.ts +0 -22
- package/dist/types/tui/hyperlink.d.ts +0 -42
- package/dist/types/tui/index.d.ts +0 -11
- package/dist/types/tui/output-block.d.ts +0 -28
- package/dist/types/tui/status-line.d.ts +0 -18
- package/dist/types/tui/tree-list.d.ts +0 -19
- package/dist/types/tui/types.d.ts +0 -13
- package/dist/types/tui/utils.d.ts +0 -36
- package/dist/types/utils/changelog.d.ts +0 -37
- package/dist/types/utils/clipboard.d.ts +0 -22
- package/dist/types/utils/cmux-workspace.d.ts +0 -52
- package/dist/types/utils/command-args.d.ts +0 -9
- package/dist/types/utils/commit-message-generator.d.ts +0 -7
- package/dist/types/utils/edit-mode.d.ts +0 -13
- package/dist/types/utils/event-bus.d.ts +0 -6
- package/dist/types/utils/external-editor.d.ts +0 -19
- package/dist/types/utils/file-display-mode.d.ts +0 -27
- package/dist/types/utils/file-mentions.d.ts +0 -25
- package/dist/types/utils/git.d.ts +0 -393
- package/dist/types/utils/image-loading.d.ts +0 -26
- package/dist/types/utils/image-resize.d.ts +0 -39
- package/dist/types/utils/lang-from-path.d.ts +0 -8
- package/dist/types/utils/linkedom.d.ts +0 -4
- package/dist/types/utils/markit.d.ts +0 -7
- package/dist/types/utils/open.d.ts +0 -2
- package/dist/types/utils/pasted-image-path.d.ts +0 -32
- package/dist/types/utils/session-color.d.ts +0 -10
- package/dist/types/utils/shell-snapshot.d.ts +0 -5
- package/dist/types/utils/sixel.d.ts +0 -21
- package/dist/types/utils/title-generator.d.ts +0 -31
- package/dist/types/utils/tool-choice.d.ts +0 -20
- package/dist/types/utils/tools-manager.d.ts +0 -9
- package/dist/types/vim/buffer.d.ts +0 -41
- package/dist/types/vim/commands.d.ts +0 -6
- package/dist/types/vim/engine.d.ts +0 -47
- package/dist/types/vim/parser.d.ts +0 -3
- package/dist/types/vim/render.d.ts +0 -25
- package/dist/types/vim/types.d.ts +0 -182
- package/dist/types/web/insane/bridge.d.ts +0 -103
- package/dist/types/web/insane/url-guard.d.ts +0 -25
- package/dist/types/web/kagi.d.ts +0 -23
- package/dist/types/web/parallel.d.ts +0 -58
- package/dist/types/web/scrapers/artifacthub.d.ts +0 -6
- package/dist/types/web/scrapers/arxiv.d.ts +0 -5
- package/dist/types/web/scrapers/aur.d.ts +0 -5
- package/dist/types/web/scrapers/biorxiv.d.ts +0 -5
- package/dist/types/web/scrapers/bluesky.d.ts +0 -5
- package/dist/types/web/scrapers/brew.d.ts +0 -5
- package/dist/types/web/scrapers/cheatsh.d.ts +0 -8
- package/dist/types/web/scrapers/chocolatey.d.ts +0 -5
- package/dist/types/web/scrapers/choosealicense.d.ts +0 -2
- package/dist/types/web/scrapers/cisa-kev.d.ts +0 -5
- package/dist/types/web/scrapers/clojars.d.ts +0 -5
- package/dist/types/web/scrapers/coingecko.d.ts +0 -5
- package/dist/types/web/scrapers/crates-io.d.ts +0 -5
- package/dist/types/web/scrapers/crossref.d.ts +0 -2
- package/dist/types/web/scrapers/devto.d.ts +0 -5
- package/dist/types/web/scrapers/discogs.d.ts +0 -8
- package/dist/types/web/scrapers/discourse.d.ts +0 -5
- package/dist/types/web/scrapers/dockerhub.d.ts +0 -5
- package/dist/types/web/scrapers/docs-rs.d.ts +0 -2
- package/dist/types/web/scrapers/fdroid.d.ts +0 -5
- package/dist/types/web/scrapers/firefox-addons.d.ts +0 -2
- package/dist/types/web/scrapers/flathub.d.ts +0 -2
- package/dist/types/web/scrapers/github-gist.d.ts +0 -5
- package/dist/types/web/scrapers/github.d.ts +0 -12
- package/dist/types/web/scrapers/gitlab.d.ts +0 -5
- package/dist/types/web/scrapers/go-pkg.d.ts +0 -5
- package/dist/types/web/scrapers/hackage.d.ts +0 -5
- package/dist/types/web/scrapers/hackernews.d.ts +0 -2
- package/dist/types/web/scrapers/hex.d.ts +0 -5
- package/dist/types/web/scrapers/huggingface.d.ts +0 -2
- package/dist/types/web/scrapers/iacr.d.ts +0 -5
- package/dist/types/web/scrapers/index.d.ts +0 -84
- package/dist/types/web/scrapers/jetbrains-marketplace.d.ts +0 -2
- package/dist/types/web/scrapers/lemmy.d.ts +0 -2
- package/dist/types/web/scrapers/lobsters.d.ts +0 -5
- package/dist/types/web/scrapers/mastodon.d.ts +0 -5
- package/dist/types/web/scrapers/maven.d.ts +0 -6
- package/dist/types/web/scrapers/mdn.d.ts +0 -2
- package/dist/types/web/scrapers/metacpan.d.ts +0 -5
- package/dist/types/web/scrapers/musicbrainz.d.ts +0 -5
- package/dist/types/web/scrapers/npm.d.ts +0 -5
- package/dist/types/web/scrapers/nuget.d.ts +0 -5
- package/dist/types/web/scrapers/nvd.d.ts +0 -5
- package/dist/types/web/scrapers/ollama.d.ts +0 -2
- package/dist/types/web/scrapers/open-vsx.d.ts +0 -5
- package/dist/types/web/scrapers/opencorporates.d.ts +0 -5
- package/dist/types/web/scrapers/openlibrary.d.ts +0 -5
- package/dist/types/web/scrapers/orcid.d.ts +0 -5
- package/dist/types/web/scrapers/osv.d.ts +0 -5
- package/dist/types/web/scrapers/packagist.d.ts +0 -5
- package/dist/types/web/scrapers/pub-dev.d.ts +0 -5
- package/dist/types/web/scrapers/pubmed.d.ts +0 -5
- package/dist/types/web/scrapers/pypi.d.ts +0 -5
- package/dist/types/web/scrapers/rawg.d.ts +0 -2
- package/dist/types/web/scrapers/readthedocs.d.ts +0 -2
- package/dist/types/web/scrapers/reddit.d.ts +0 -5
- package/dist/types/web/scrapers/repology.d.ts +0 -5
- package/dist/types/web/scrapers/rfc.d.ts +0 -5
- package/dist/types/web/scrapers/rubygems.d.ts +0 -5
- package/dist/types/web/scrapers/searchcode.d.ts +0 -2
- package/dist/types/web/scrapers/sec-edgar.d.ts +0 -5
- package/dist/types/web/scrapers/semantic-scholar.d.ts +0 -2
- package/dist/types/web/scrapers/snapcraft.d.ts +0 -2
- package/dist/types/web/scrapers/sourcegraph.d.ts +0 -2
- package/dist/types/web/scrapers/spdx.d.ts +0 -5
- package/dist/types/web/scrapers/spotify.d.ts +0 -8
- package/dist/types/web/scrapers/stackoverflow.d.ts +0 -6
- package/dist/types/web/scrapers/terraform.d.ts +0 -5
- package/dist/types/web/scrapers/tldr.d.ts +0 -7
- package/dist/types/web/scrapers/twitter.d.ts +0 -5
- package/dist/types/web/scrapers/types.d.ts +0 -83
- package/dist/types/web/scrapers/utils.d.ts +0 -32
- package/dist/types/web/scrapers/vimeo.d.ts +0 -5
- package/dist/types/web/scrapers/vscode-marketplace.d.ts +0 -5
- package/dist/types/web/scrapers/w3c.d.ts +0 -2
- package/dist/types/web/scrapers/wikidata.d.ts +0 -5
- package/dist/types/web/scrapers/wikipedia.d.ts +0 -5
- package/dist/types/web/scrapers/youtube.d.ts +0 -13
- package/dist/types/web/search/index.d.ts +0 -118
- package/dist/types/web/search/provider.d.ts +0 -55
- package/dist/types/web/search/providers/anthropic.d.ts +0 -32
- package/dist/types/web/search/providers/base.d.ts +0 -90
- package/dist/types/web/search/providers/brave.d.ts +0 -27
- package/dist/types/web/search/providers/codex.d.ts +0 -35
- package/dist/types/web/search/providers/duckduckgo.d.ts +0 -57
- package/dist/types/web/search/providers/exa.d.ts +0 -52
- package/dist/types/web/search/providers/gemini.d.ts +0 -57
- package/dist/types/web/search/providers/insane.d.ts +0 -53
- package/dist/types/web/search/providers/jina.d.ts +0 -26
- package/dist/types/web/search/providers/kagi.d.ts +0 -24
- package/dist/types/web/search/providers/kimi.d.ts +0 -27
- package/dist/types/web/search/providers/openai-compatible.d.ts +0 -9
- package/dist/types/web/search/providers/parallel.d.ts +0 -15
- package/dist/types/web/search/providers/perplexity.d.ts +0 -38
- package/dist/types/web/search/providers/searxng.d.ts +0 -44
- package/dist/types/web/search/providers/synthetic.d.ts +0 -21
- package/dist/types/web/search/providers/tavily.d.ts +0 -29
- package/dist/types/web/search/providers/text-citations.d.ts +0 -23
- package/dist/types/web/search/providers/utils.d.ts +0 -88
- package/dist/types/web/search/providers/xai.d.ts +0 -64
- package/dist/types/web/search/providers/zai.d.ts +0 -28
- package/dist/types/web/search/render.d.ts +0 -36
- package/dist/types/web/search/types.d.ts +0 -372
- package/dist/types/web/search/utils.d.ts +0 -4
- package/dist/types/workflow/workflow-intent-diff.d.ts +0 -25
- package/dist/types/workflow/workflow-intent-report-consumer.d.ts +0 -50
- package/dist/types/workflow/workflow-intent-report-schema.d.ts +0 -2
- package/dist/types/workflow/workflow-intent-report.d.ts +0 -48
- package/dist/types/workspace-tree.d.ts +0 -42
- package/src/sdk.ts +0 -2623
|
@@ -0,0 +1,2292 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { SdkClientError } from "../client/client";
|
|
3
|
+
import type { ChatDeliveryError } from "./chat-daemon-runtime";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type ChatEffect,
|
|
7
|
+
ChatEffectJournal,
|
|
8
|
+
type ChatEffectLease,
|
|
9
|
+
type ChatEffectReceipt,
|
|
10
|
+
} from "./chat-effect-journal";
|
|
11
|
+
import { ConversationStore } from "./conversation-store";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type DiscordConversation,
|
|
15
|
+
type DiscordInboundDispatchReceipt,
|
|
16
|
+
discordConversationKey,
|
|
17
|
+
normalizeDiscordConversation,
|
|
18
|
+
} from "./discord-conversation";
|
|
19
|
+
import type { DiscordInboundEvent, DiscordMessageComponent, DiscordProvider, DiscordThread } from "./discord-provider";
|
|
20
|
+
|
|
21
|
+
const FAILURE = "This conversation is no longer available.";
|
|
22
|
+
|
|
23
|
+
export class DiscordEndpointBindingError extends Error {
|
|
24
|
+
constructor(message = "Discord session endpoint changed before outbound publication.") {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "DiscordEndpointBindingError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type DiscordClosingIntent = Readonly<{ nonce: string; at: number }>;
|
|
31
|
+
|
|
32
|
+
function closingIntent(record: DiscordConversation | undefined): DiscordClosingIntent | undefined {
|
|
33
|
+
const candidate = record as { state?: unknown; closingNonce?: unknown; closingAt?: unknown } | undefined;
|
|
34
|
+
return candidate?.state === "closing" &&
|
|
35
|
+
typeof candidate.closingNonce === "string" &&
|
|
36
|
+
typeof candidate.closingAt === "number"
|
|
37
|
+
? { nonce: candidate.closingNonce, at: candidate.closingAt }
|
|
38
|
+
: undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function withClosingIntent(record: DiscordConversation, nonce: string, at: number): DiscordConversation {
|
|
42
|
+
return {
|
|
43
|
+
...record,
|
|
44
|
+
generation: record.generation + 1,
|
|
45
|
+
state: "closing",
|
|
46
|
+
closingNonce: nonce,
|
|
47
|
+
closingAt: at,
|
|
48
|
+
pendingActionId: undefined,
|
|
49
|
+
pendingActionNonce: undefined,
|
|
50
|
+
pendingActionEffectId: undefined,
|
|
51
|
+
} as unknown as DiscordConversation;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function withoutClosingIntent(record: DiscordConversation, closedAt: number): DiscordConversation {
|
|
55
|
+
const {
|
|
56
|
+
closingNonce: _closingNonce,
|
|
57
|
+
closingAt: _closingAt,
|
|
58
|
+
...rest
|
|
59
|
+
} = record as DiscordConversation & {
|
|
60
|
+
closingNonce?: unknown;
|
|
61
|
+
closingAt?: unknown;
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
...rest,
|
|
65
|
+
generation: record.generation + 1,
|
|
66
|
+
state: "closed",
|
|
67
|
+
closedAt,
|
|
68
|
+
pendingActionId: undefined,
|
|
69
|
+
pendingActionNonce: undefined,
|
|
70
|
+
pendingActionEffectId: undefined,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A captured live SDK transport. `isCurrent` must fail closed after replacement or removal. */
|
|
75
|
+
export interface DiscordEndpointBinding {
|
|
76
|
+
generation: number;
|
|
77
|
+
isCurrent(): boolean;
|
|
78
|
+
send(frame: Record<string, unknown>): void | Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface DiscordLeaseRecoveryScheduler {
|
|
82
|
+
setTimeout(callback: () => void | Promise<void>, delayMs: number): unknown;
|
|
83
|
+
clearTimeout(handle: unknown): void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface DiscordNotificationDaemonOptions {
|
|
87
|
+
agentDir: string;
|
|
88
|
+
repo: string;
|
|
89
|
+
guildId: string;
|
|
90
|
+
parentChannelId: string;
|
|
91
|
+
provider: DiscordProvider;
|
|
92
|
+
now?: () => number;
|
|
93
|
+
leaseRecoveryScheduler?: DiscordLeaseRecoveryScheduler;
|
|
94
|
+
resolveEndpoint: (sessionId: string, expectedGeneration?: number) => Promise<DiscordEndpointBinding | null>;
|
|
95
|
+
onCommand?: (
|
|
96
|
+
sessionId: string,
|
|
97
|
+
content: string,
|
|
98
|
+
endpoint: DiscordEndpointBinding,
|
|
99
|
+
idempotencyKey: string,
|
|
100
|
+
) => Promise<boolean>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface DiscordNotificationInput {
|
|
104
|
+
sessionId: string;
|
|
105
|
+
endpointGeneration: number;
|
|
106
|
+
content: string;
|
|
107
|
+
threadName?: string;
|
|
108
|
+
actionId?: string;
|
|
109
|
+
options?: string[];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
type DiscordInboundEffectPayload =
|
|
113
|
+
| {
|
|
114
|
+
type: "command";
|
|
115
|
+
content: string;
|
|
116
|
+
idempotencyKey: string;
|
|
117
|
+
routing: DiscordInboundRouting;
|
|
118
|
+
}
|
|
119
|
+
| {
|
|
120
|
+
type: "reply";
|
|
121
|
+
id: string;
|
|
122
|
+
answer: string | number;
|
|
123
|
+
idempotencyKey: string;
|
|
124
|
+
routing: DiscordInboundRouting;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
type DiscordInboundRouting = {
|
|
128
|
+
guildId: string;
|
|
129
|
+
parentId: string;
|
|
130
|
+
threadId: string;
|
|
131
|
+
eventId: string;
|
|
132
|
+
interactionId?: string;
|
|
133
|
+
kind: "command" | "action";
|
|
134
|
+
actionId?: string;
|
|
135
|
+
actionNonce?: string;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
function backfilledEffectIncarnationId(record: DiscordConversation): string {
|
|
139
|
+
if (record.effectIncarnationId) return record.effectIncarnationId;
|
|
140
|
+
if (record.createNonce) return record.createNonce;
|
|
141
|
+
return `legacy-${createHash("sha256")
|
|
142
|
+
.update(
|
|
143
|
+
[record.appId, record.guildId, record.parentChannelId, record.threadId ?? "", record.sessionId ?? ""].join(
|
|
144
|
+
"\u0000",
|
|
145
|
+
),
|
|
146
|
+
)
|
|
147
|
+
.digest("hex")
|
|
148
|
+
.slice(0, 32)}`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
type DiscordInboundClaim = {
|
|
152
|
+
receipt: DiscordInboundDispatchReceipt;
|
|
153
|
+
liveCallbackEffect?: ChatEffect<DiscordInboundEffectPayload>;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** SDK-only Discord threaded notification daemon. It owns no AgentSession and never retains endpoint credentials. */
|
|
157
|
+
export class DiscordNotificationDaemon {
|
|
158
|
+
readonly #store: ConversationStore<DiscordConversation>;
|
|
159
|
+
readonly #now: () => number;
|
|
160
|
+
readonly #leaseRecoveryScheduler: DiscordLeaseRecoveryScheduler;
|
|
161
|
+
readonly #creates = new Map<string, Promise<DiscordConversation>>();
|
|
162
|
+
readonly #resumes = new Map<string, Promise<DiscordConversation | undefined>>();
|
|
163
|
+
readonly #archives = new Map<string, Promise<void>>();
|
|
164
|
+
readonly #resolveEndpoint: (
|
|
165
|
+
sessionId: string,
|
|
166
|
+
expectedGeneration?: number,
|
|
167
|
+
) => Promise<DiscordEndpointBinding | null>;
|
|
168
|
+
readonly #effects: ChatEffectJournal;
|
|
169
|
+
readonly #activeWork = new Set<Promise<unknown>>();
|
|
170
|
+
readonly #inflightInbound = new Set<string>();
|
|
171
|
+
#started = false;
|
|
172
|
+
#lifecycleGeneration = 0;
|
|
173
|
+
#startTask: Promise<void> | undefined;
|
|
174
|
+
#stopTask: Promise<void> | undefined;
|
|
175
|
+
#providerStarting = false;
|
|
176
|
+
|
|
177
|
+
#leaseRecoveryTimer: unknown;
|
|
178
|
+
#leaseRecoveryAt: number | undefined;
|
|
179
|
+
#leaseRecoveryFailures = 0;
|
|
180
|
+
|
|
181
|
+
readonly #dispatchOwner = randomUUID();
|
|
182
|
+
readonly #dispatchLeaseMs = 60_000;
|
|
183
|
+
readonly #providerOwner = randomUUID();
|
|
184
|
+
readonly #providerLeaseMs = 60_000;
|
|
185
|
+
constructor(private readonly options: DiscordNotificationDaemonOptions) {
|
|
186
|
+
this.#store = new ConversationStore({ agentDir: options.agentDir, kind: "discord", now: options.now });
|
|
187
|
+
this.#effects = new ChatEffectJournal({ agentDir: options.agentDir, transport: "discord", now: options.now });
|
|
188
|
+
this.#now = options.now ?? Date.now;
|
|
189
|
+
this.#leaseRecoveryScheduler = options.leaseRecoveryScheduler ?? {
|
|
190
|
+
setTimeout: (callback, delayMs) => setTimeout(() => void callback(), delayMs),
|
|
191
|
+
clearTimeout: handle => clearTimeout(handle as ReturnType<typeof setTimeout>),
|
|
192
|
+
};
|
|
193
|
+
this.#resolveEndpoint = options.resolveEndpoint;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async start(): Promise<void> {
|
|
197
|
+
if (this.#started && !this.#stopTask) return;
|
|
198
|
+
if (this.#stopTask) {
|
|
199
|
+
await this.#stopTask;
|
|
200
|
+
return await this.start();
|
|
201
|
+
}
|
|
202
|
+
if (this.#startTask) return await this.#startTask;
|
|
203
|
+
|
|
204
|
+
const lifecycleGeneration = ++this.#lifecycleGeneration;
|
|
205
|
+
const task = this.#start(lifecycleGeneration);
|
|
206
|
+
this.#startTask = task;
|
|
207
|
+
try {
|
|
208
|
+
await task;
|
|
209
|
+
} finally {
|
|
210
|
+
if (this.#startTask === task) this.#startTask = undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async #start(lifecycleGeneration: number): Promise<void> {
|
|
215
|
+
try {
|
|
216
|
+
// Provider start is the delivery boundary; complete crash recovery first.
|
|
217
|
+
await this.#reconcileTerminalInboundReceipts();
|
|
218
|
+
const closingBeforeProviderRecoveryFailed = await this.#recoverClosingConversations();
|
|
219
|
+
const providerRecoveryFailed = await this.#drainProviderEffects();
|
|
220
|
+
await this.#reconcileTerminalInboundReceipts();
|
|
221
|
+
const closingAfterProviderRecoveryFailed = await this.#recoverClosingConversations();
|
|
222
|
+
const inboundRecoveryFailed = await this.#drainPendingDispatches();
|
|
223
|
+
const recoveryFailed =
|
|
224
|
+
closingBeforeProviderRecoveryFailed ||
|
|
225
|
+
providerRecoveryFailed ||
|
|
226
|
+
closingAfterProviderRecoveryFailed ||
|
|
227
|
+
inboundRecoveryFailed;
|
|
228
|
+
if (lifecycleGeneration !== this.#lifecycleGeneration) return;
|
|
229
|
+
|
|
230
|
+
this.#started = true;
|
|
231
|
+
await this.#scheduleLeaseRecovery(recoveryFailed);
|
|
232
|
+
// stop() invalidates this generation before awaiting recovery or start. This
|
|
233
|
+
// guard is immediately adjacent to the Gateway-open boundary.
|
|
234
|
+
if (lifecycleGeneration !== this.#lifecycleGeneration) {
|
|
235
|
+
this.#started = false;
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.#providerStarting = true;
|
|
239
|
+
try {
|
|
240
|
+
await this.options.provider.start(
|
|
241
|
+
event => this.#track(this.handleInbound(event)),
|
|
242
|
+
() => {},
|
|
243
|
+
);
|
|
244
|
+
} finally {
|
|
245
|
+
this.#providerStarting = false;
|
|
246
|
+
}
|
|
247
|
+
if (lifecycleGeneration !== this.#lifecycleGeneration) {
|
|
248
|
+
this.#started = false;
|
|
249
|
+
await this.options.provider.stop();
|
|
250
|
+
}
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (lifecycleGeneration === this.#lifecycleGeneration) {
|
|
253
|
+
this.#started = false;
|
|
254
|
+
if (this.#leaseRecoveryTimer !== undefined)
|
|
255
|
+
this.#leaseRecoveryScheduler.clearTimeout(this.#leaseRecoveryTimer);
|
|
256
|
+
this.#leaseRecoveryTimer = undefined;
|
|
257
|
+
this.#leaseRecoveryAt = undefined;
|
|
258
|
+
}
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async stop(): Promise<void> {
|
|
264
|
+
if (this.#stopTask) return await this.#stopTask;
|
|
265
|
+
|
|
266
|
+
const starting = this.#startTask;
|
|
267
|
+
const stopProvider = this.#started || this.#providerStarting;
|
|
268
|
+
++this.#lifecycleGeneration;
|
|
269
|
+
this.#started = false;
|
|
270
|
+
if (this.#leaseRecoveryTimer !== undefined) this.#leaseRecoveryScheduler.clearTimeout(this.#leaseRecoveryTimer);
|
|
271
|
+
this.#leaseRecoveryTimer = undefined;
|
|
272
|
+
this.#leaseRecoveryAt = undefined;
|
|
273
|
+
|
|
274
|
+
const task = this.#stop(starting, stopProvider);
|
|
275
|
+
this.#stopTask = task;
|
|
276
|
+
try {
|
|
277
|
+
await task;
|
|
278
|
+
} finally {
|
|
279
|
+
if (this.#stopTask === task) this.#stopTask = undefined;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async #stop(starting: Promise<void> | undefined, stopProvider: boolean): Promise<void> {
|
|
284
|
+
// Calling provider.stop() before awaiting start lets a provider cancel a
|
|
285
|
+
// Gateway open that is already in flight. #start rechecks the generation
|
|
286
|
+
// after it resolves and closes any late open.
|
|
287
|
+
if (stopProvider) await this.options.provider.stop();
|
|
288
|
+
try {
|
|
289
|
+
await starting;
|
|
290
|
+
} catch {
|
|
291
|
+
// The caller of start() owns its recovery/start error; stop still drains it.
|
|
292
|
+
}
|
|
293
|
+
if (this.#providerStarting || this.#started) {
|
|
294
|
+
this.#started = false;
|
|
295
|
+
await this.options.provider.stop();
|
|
296
|
+
}
|
|
297
|
+
// Drain until quiescent: a tracked task can schedule further tracked work while
|
|
298
|
+
// we await, and any that outlives stop() would bleed timing pressure into the
|
|
299
|
+
// next daemon/test. #started is false, so no new recovery timers arm and the
|
|
300
|
+
// loop terminates.
|
|
301
|
+
while (this.#activeWork.size > 0) await Promise.all([...this.#activeWork]);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async notify(input: DiscordNotificationInput): Promise<DiscordConversation> {
|
|
305
|
+
await this.#requireLiveBinding(input.sessionId, input.endpointGeneration);
|
|
306
|
+
const conversation = await this.#ensureConversation(input);
|
|
307
|
+
await this.#requireLiveBinding(input.sessionId, input.endpointGeneration);
|
|
308
|
+
if (conversation.state !== "active" || !conversation.threadId) throw new Error("Discord thread is unavailable");
|
|
309
|
+
const pendingActionId = input.actionId;
|
|
310
|
+
const authoritative = pendingActionId
|
|
311
|
+
? await this.#ensureActionPublication(conversation, pendingActionId)
|
|
312
|
+
: conversation;
|
|
313
|
+
await this.#requireLiveBinding(input.sessionId, input.endpointGeneration);
|
|
314
|
+
const components =
|
|
315
|
+
pendingActionId && authoritative.pendingActionNonce && input.options && input.options.length > 0
|
|
316
|
+
? actionComponents(
|
|
317
|
+
authoritative.endpointGeneration!,
|
|
318
|
+
pendingActionId,
|
|
319
|
+
authoritative.pendingActionNonce,
|
|
320
|
+
input.options,
|
|
321
|
+
)
|
|
322
|
+
: undefined;
|
|
323
|
+
|
|
324
|
+
// The durable action publication intent, rather than this notification call,
|
|
325
|
+
// owns the provider-visible effect identity. This makes retries reconcile the
|
|
326
|
+
// original post and preserves the original component route.
|
|
327
|
+
const effectId = pendingActionId
|
|
328
|
+
? authoritative.pendingActionEffectId!
|
|
329
|
+
: `notification:${authoritative.threadId}:${randomUUID()}`;
|
|
330
|
+
await this.#postEffect(effectId, authoritative, input.content, components, pendingActionId !== undefined);
|
|
331
|
+
|
|
332
|
+
return authoritative;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Posts a safe command outcome to the active mapped conversation. */
|
|
336
|
+
async postCommandResult(sessionId: string, content: string): Promise<boolean> {
|
|
337
|
+
const record = await this.#bySession(sessionId);
|
|
338
|
+
if (record?.state !== "active" || !record.threadId) return false;
|
|
339
|
+
await this.#postEffect(`command-result:${record.threadId}:${randomUUID()}`, record, content);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async close(sessionId: string): Promise<void> {
|
|
344
|
+
const closing = await this.#markClosing(sessionId);
|
|
345
|
+
if (closing) await this.#driveClose(closing);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async archive(sessionId: string): Promise<void> {
|
|
349
|
+
const running = this.#archives.get(sessionId);
|
|
350
|
+
if (running) return await running;
|
|
351
|
+
const task = this.#archive(sessionId);
|
|
352
|
+
this.#archives.set(sessionId, task);
|
|
353
|
+
try {
|
|
354
|
+
await task;
|
|
355
|
+
} finally {
|
|
356
|
+
this.#archives.delete(sessionId);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async #archive(sessionId: string): Promise<void> {
|
|
361
|
+
const record = await this.#bySession(sessionId);
|
|
362
|
+
if (!record?.threadId || record.state !== "active") return;
|
|
363
|
+
await this.#requireLiveBinding(record.sessionId!, record.endpointGeneration!);
|
|
364
|
+
const archiving = await this.#beginArchive(record);
|
|
365
|
+
const occurrenceId = archiving.archiveOccurrenceId;
|
|
366
|
+
if (!occurrenceId || !archiving.archiveEffectId) throw new Error("Discord archive occurrence is unavailable");
|
|
367
|
+
await this.#threadEffect(archiving.archiveEffectId, archiving, "archive", false, false, occurrenceId);
|
|
368
|
+
await this.#requireLiveBinding(archiving.sessionId!, archiving.endpointGeneration!);
|
|
369
|
+
await this.#completeArchive(archiving, occurrenceId);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async resume(sessionId: string, endpointGeneration: number): Promise<DiscordConversation | undefined> {
|
|
373
|
+
const running = this.#resumes.get(sessionId);
|
|
374
|
+
if (running) return await running;
|
|
375
|
+
const task = this.#resume(sessionId, endpointGeneration);
|
|
376
|
+
this.#resumes.set(sessionId, task);
|
|
377
|
+
try {
|
|
378
|
+
return await task;
|
|
379
|
+
} finally {
|
|
380
|
+
this.#resumes.delete(sessionId);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async #resume(sessionId: string, endpointGeneration: number): Promise<DiscordConversation | undefined> {
|
|
385
|
+
const record = await this.#bySession(sessionId);
|
|
386
|
+
if (!record?.threadId || record.state === "closed") return undefined;
|
|
387
|
+
if (closingIntent(record)) {
|
|
388
|
+
await this.#driveClose(record);
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
391
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
392
|
+
const resumeOccurrenceId = randomUUID();
|
|
393
|
+
const effectIncarnationId = backfilledEffectIncarnationId(record);
|
|
394
|
+
const resuming = await this.#replace(record, {
|
|
395
|
+
...record,
|
|
396
|
+
state: "resuming",
|
|
397
|
+
endpointGeneration,
|
|
398
|
+
effectIncarnationId,
|
|
399
|
+
resumeOccurrenceId,
|
|
400
|
+
resumeEffectId: `unarchive:${record.threadId}:${effectIncarnationId}:${resumeOccurrenceId}`,
|
|
401
|
+
pendingActionId: undefined,
|
|
402
|
+
pendingActionNonce: undefined,
|
|
403
|
+
pendingActionEffectId: undefined,
|
|
404
|
+
});
|
|
405
|
+
try {
|
|
406
|
+
const occurrenceId = resuming.resumeOccurrenceId;
|
|
407
|
+
if (!occurrenceId || !resuming.resumeEffectId) throw new Error("Discord resume occurrence is unavailable");
|
|
408
|
+
await this.#threadEffect(resuming.resumeEffectId, resuming, "unarchive", false, false, occurrenceId);
|
|
409
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
410
|
+
return await this.#completeResume(resuming, occurrenceId);
|
|
411
|
+
} catch {
|
|
412
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
413
|
+
const superseded = await this.#replace(resuming, {
|
|
414
|
+
...resuming,
|
|
415
|
+
state: "archived",
|
|
416
|
+
resumeOccurrenceId: undefined,
|
|
417
|
+
resumeEffectId: undefined,
|
|
418
|
+
pendingActionId: undefined,
|
|
419
|
+
pendingActionNonce: undefined,
|
|
420
|
+
pendingActionEffectId: undefined,
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const replacement = await this.#create(sessionId, endpointGeneration, `resume-${randomUUID()}`);
|
|
424
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
425
|
+
await this.#replace(superseded, { ...superseded, supersededByThreadId: replacement.threadId });
|
|
426
|
+
return replacement;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
async resolveAction(sessionId: string, actionId: string): Promise<void> {
|
|
431
|
+
const record = await this.#bySession(sessionId);
|
|
432
|
+
if (record?.pendingActionId === actionId && record.pendingActionNonce)
|
|
433
|
+
await this.#clearPending(record, actionId, record.pendingActionNonce);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async handleInbound(event: DiscordInboundEvent): Promise<void> {
|
|
437
|
+
if (event.bot || event.authorId === this.options.provider.botUserId) return;
|
|
438
|
+
await this.#reconcileTerminalInboundReceipts();
|
|
439
|
+
const record = await this.#byThread(event.guildId, event.parentId, event.threadId);
|
|
440
|
+
if (!record?.sessionId) {
|
|
441
|
+
await this.#fail(event.threadId);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const claim = await this.#claimInbound(record, event);
|
|
445
|
+
if (claim === "invalid") {
|
|
446
|
+
await this.#fail(event.threadId);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (!claim || this.#inflightInbound.has(claim.receipt.effectId)) return;
|
|
450
|
+
this.#inflightInbound.add(claim.receipt.effectId);
|
|
451
|
+
try {
|
|
452
|
+
// Interaction callbacks have a short provider deadline. The mapping and
|
|
453
|
+
// journal claim are sufficient to acknowledge; endpoint discovery waits.
|
|
454
|
+
const endpoint = event.interaction
|
|
455
|
+
? null
|
|
456
|
+
: await this.#resolveEndpoint(record.sessionId, record.endpointGeneration);
|
|
457
|
+
if (!event.interaction && !this.#matches(record, endpoint)) {
|
|
458
|
+
await this.#fail(event.threadId);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
await this.#dispatchInbound(record, endpoint, claim.receipt, event.interaction, claim.liveCallbackEffect);
|
|
462
|
+
} finally {
|
|
463
|
+
this.#inflightInbound.delete(claim.receipt.effectId);
|
|
464
|
+
await this.#scheduleLeaseRecovery();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
#track<T>(work: Promise<T>): Promise<T> {
|
|
469
|
+
this.#activeWork.add(work);
|
|
470
|
+
return work.finally(() => this.#activeWork.delete(work));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async #claimInbound(
|
|
474
|
+
record: DiscordConversation,
|
|
475
|
+
event: DiscordInboundEvent,
|
|
476
|
+
): Promise<DiscordInboundClaim | "invalid" | undefined> {
|
|
477
|
+
if (record.state !== "active" || closingIntent(record)) return "invalid";
|
|
478
|
+
const route = event.interaction ? decodeCustomId(event.interaction.customId) : undefined;
|
|
479
|
+
const command = !event.interaction && event.content?.startsWith("/sdk ");
|
|
480
|
+
if (!command && (!route || !event.interaction || route.generation !== record.endpointGeneration))
|
|
481
|
+
return "invalid";
|
|
482
|
+
const key = discordConversationKey({
|
|
483
|
+
appId: record.appId,
|
|
484
|
+
guildId: record.guildId,
|
|
485
|
+
parentChannelId: record.parentChannelId,
|
|
486
|
+
threadId: record.threadId!,
|
|
487
|
+
});
|
|
488
|
+
let receipt: DiscordInboundDispatchReceipt | undefined;
|
|
489
|
+
let valid = false;
|
|
490
|
+
const effectId = `discord:${record.appId}:${record.guildId}:${record.parentChannelId}:${record.threadId}:${event.id}`;
|
|
491
|
+
const idempotencyKey = effectId;
|
|
492
|
+
const routing: DiscordInboundRouting = {
|
|
493
|
+
guildId: event.guildId,
|
|
494
|
+
parentId: event.parentId,
|
|
495
|
+
threadId: event.threadId,
|
|
496
|
+
eventId: event.id,
|
|
497
|
+
...(event.interaction ? { interactionId: event.interaction.id } : {}),
|
|
498
|
+
kind: command ? "command" : "action",
|
|
499
|
+
...(!command ? { actionId: route!.actionId, actionNonce: route!.actionNonce } : {}),
|
|
500
|
+
};
|
|
501
|
+
const payload: DiscordInboundEffectPayload = command
|
|
502
|
+
? { type: "command", content: event.content!, idempotencyKey, routing }
|
|
503
|
+
: {
|
|
504
|
+
type: "reply",
|
|
505
|
+
id: route!.actionId,
|
|
506
|
+
answer: componentAnswer(event.interaction!.value ?? ""),
|
|
507
|
+
idempotencyKey,
|
|
508
|
+
routing,
|
|
509
|
+
};
|
|
510
|
+
let liveCallbackEffect: ChatEffect<DiscordInboundEffectPayload> | undefined;
|
|
511
|
+
if (event.interaction) {
|
|
512
|
+
liveCallbackEffect = await this.#rescheduleAfterEffectTransition(
|
|
513
|
+
this.#effects.enqueueAndClaim(
|
|
514
|
+
{
|
|
515
|
+
id: effectId,
|
|
516
|
+
kind: "discord.inbound.action",
|
|
517
|
+
transport: "discord",
|
|
518
|
+
sessionId: record.sessionId,
|
|
519
|
+
endpointGeneration: record.endpointGeneration!,
|
|
520
|
+
payload,
|
|
521
|
+
},
|
|
522
|
+
this.#dispatchOwner,
|
|
523
|
+
this.#dispatchLeaseMs,
|
|
524
|
+
),
|
|
525
|
+
);
|
|
526
|
+
} else {
|
|
527
|
+
await this.#rescheduleAfterEffectTransition(
|
|
528
|
+
this.#effects.enqueue({
|
|
529
|
+
id: effectId,
|
|
530
|
+
kind: "discord.inbound.command",
|
|
531
|
+
transport: "discord",
|
|
532
|
+
sessionId: record.sessionId,
|
|
533
|
+
endpointGeneration: record.endpointGeneration!,
|
|
534
|
+
payload,
|
|
535
|
+
}),
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
await this.#store.transact(key, current => {
|
|
540
|
+
if (current?.state !== "active" || current.endpointGeneration !== record.endpointGeneration) return current;
|
|
541
|
+
const interactionId = event.interaction?.id;
|
|
542
|
+
const existing = (current.inboundDispatches ?? []).find(
|
|
543
|
+
item => item.eventId === event.id || (interactionId !== undefined && item.interactionId === interactionId),
|
|
544
|
+
);
|
|
545
|
+
if (existing) {
|
|
546
|
+
valid = true;
|
|
547
|
+
receipt = existing;
|
|
548
|
+
return current;
|
|
549
|
+
}
|
|
550
|
+
if (
|
|
551
|
+
!command &&
|
|
552
|
+
(current.pendingActionId !== route!.actionId || current.pendingActionNonce !== route!.actionNonce)
|
|
553
|
+
)
|
|
554
|
+
return current;
|
|
555
|
+
if (
|
|
556
|
+
!command &&
|
|
557
|
+
(current.inboundDispatches ?? []).some(
|
|
558
|
+
item =>
|
|
559
|
+
item.kind === "action" &&
|
|
560
|
+
item.actionId === route!.actionId &&
|
|
561
|
+
item.actionNonce === route!.actionNonce,
|
|
562
|
+
)
|
|
563
|
+
)
|
|
564
|
+
return current;
|
|
565
|
+
valid = true;
|
|
566
|
+
receipt = command
|
|
567
|
+
? {
|
|
568
|
+
key: event.id,
|
|
569
|
+
eventId: event.id,
|
|
570
|
+
kind: "command",
|
|
571
|
+
endpointGeneration: record.endpointGeneration!,
|
|
572
|
+
effectId,
|
|
573
|
+
idempotencyKey,
|
|
574
|
+
}
|
|
575
|
+
: {
|
|
576
|
+
key: event.id,
|
|
577
|
+
eventId: event.id,
|
|
578
|
+
interactionId: interactionId!,
|
|
579
|
+
kind: "action",
|
|
580
|
+
actionId: route!.actionId,
|
|
581
|
+
actionNonce: route!.actionNonce,
|
|
582
|
+
endpointGeneration: record.endpointGeneration!,
|
|
583
|
+
effectId,
|
|
584
|
+
idempotencyKey,
|
|
585
|
+
};
|
|
586
|
+
return normalizeDiscordConversation({
|
|
587
|
+
...current,
|
|
588
|
+
generation: current.generation + 1,
|
|
589
|
+
updatedAt: this.#now(),
|
|
590
|
+
inboundDispatches: [...(current.inboundDispatches ?? []), receipt!],
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
if (!valid || !receipt) {
|
|
594
|
+
await this.#terminalizeRejectedInbound(
|
|
595
|
+
effectId,
|
|
596
|
+
liveCallbackEffect ? { owner: this.#dispatchOwner, epoch: liveCallbackEffect.epoch } : undefined,
|
|
597
|
+
);
|
|
598
|
+
return "invalid";
|
|
599
|
+
}
|
|
600
|
+
if (liveCallbackEffect && liveCallbackEffect.id !== receipt.effectId) {
|
|
601
|
+
await this.#terminalizeRejectedInbound(liveCallbackEffect.id, {
|
|
602
|
+
owner: this.#dispatchOwner,
|
|
603
|
+
epoch: liveCallbackEffect.epoch,
|
|
604
|
+
});
|
|
605
|
+
liveCallbackEffect = undefined;
|
|
606
|
+
}
|
|
607
|
+
return { receipt, liveCallbackEffect };
|
|
608
|
+
}
|
|
609
|
+
async #dispatchInbound(
|
|
610
|
+
record: DiscordConversation,
|
|
611
|
+
initialEndpoint: DiscordEndpointBinding | null,
|
|
612
|
+
receipt: DiscordInboundDispatchReceipt,
|
|
613
|
+
interaction?: DiscordInboundEvent["interaction"],
|
|
614
|
+
liveCallbackEffect?: ChatEffect<DiscordInboundEffectPayload>,
|
|
615
|
+
): Promise<void> {
|
|
616
|
+
const claimedEffect =
|
|
617
|
+
liveCallbackEffect ??
|
|
618
|
+
(await this.#rescheduleAfterEffectTransition(
|
|
619
|
+
this.#effects.claim<DiscordInboundEffectPayload>(
|
|
620
|
+
receipt.effectId,
|
|
621
|
+
this.#dispatchOwner,
|
|
622
|
+
this.#dispatchLeaseMs,
|
|
623
|
+
),
|
|
624
|
+
));
|
|
625
|
+
if (!claimedEffect) return;
|
|
626
|
+
let effect: ChatEffect<DiscordInboundEffectPayload> = claimedEffect;
|
|
627
|
+
let lease = { owner: this.#dispatchOwner, epoch: effect.epoch };
|
|
628
|
+
|
|
629
|
+
const current = await this.#currentInboundRecord(record, receipt);
|
|
630
|
+
if (!current) {
|
|
631
|
+
await this.#terminalizeInbound(record, receipt, "rejected", lease);
|
|
632
|
+
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
record = current;
|
|
636
|
+
if (receipt.kind === "action" && !this.#hasDurableDeferIntent(effect)) {
|
|
637
|
+
// Callback tokens cannot survive a restart. Persist the intent before remote
|
|
638
|
+
// I/O so recovery can safely resume SDK delivery whether defer reached Discord
|
|
639
|
+
// or the process stopped first.
|
|
640
|
+
if (!interaction) {
|
|
641
|
+
await this.#terminalizeInbound(record, receipt, "callback_token_unavailable", lease);
|
|
642
|
+
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
let callbackLeaseLost = false;
|
|
647
|
+
let callbackRenewal: Promise<boolean> | undefined;
|
|
648
|
+
const renewCallbackLease = async (): Promise<boolean> => {
|
|
649
|
+
if (callbackLeaseLost) return false;
|
|
650
|
+
if (callbackRenewal) return await callbackRenewal;
|
|
651
|
+
const renewal = (async (): Promise<boolean> => {
|
|
652
|
+
if (
|
|
653
|
+
!(await this.#rescheduleAfterEffectTransition(
|
|
654
|
+
this.#effects.renew(effect.id, lease, this.#dispatchLeaseMs),
|
|
655
|
+
))
|
|
656
|
+
)
|
|
657
|
+
callbackLeaseLost = true;
|
|
658
|
+
return !callbackLeaseLost;
|
|
659
|
+
})();
|
|
660
|
+
callbackRenewal = renewal;
|
|
661
|
+
try {
|
|
662
|
+
return await renewal;
|
|
663
|
+
} finally {
|
|
664
|
+
if (callbackRenewal === renewal) callbackRenewal = undefined;
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
const timer = setInterval(
|
|
668
|
+
() => {
|
|
669
|
+
void renewCallbackLease().catch(() => {});
|
|
670
|
+
},
|
|
671
|
+
Math.max(1, Math.floor(this.#dispatchLeaseMs / 3)),
|
|
672
|
+
);
|
|
673
|
+
try {
|
|
674
|
+
if (!(await renewCallbackLease())) return;
|
|
675
|
+
const prepared = await this.#rescheduleAfterEffectTransition(
|
|
676
|
+
this.#effects.recordReceipt<DiscordInboundEffectPayload>(effect.id, lease, { status: "defer_intent" }),
|
|
677
|
+
);
|
|
678
|
+
if (!prepared) return;
|
|
679
|
+
effect = prepared;
|
|
680
|
+
if (!(await renewCallbackLease())) return;
|
|
681
|
+
await this.options.provider.deferInteraction({ id: interaction.id, token: interaction.token });
|
|
682
|
+
if (!(await renewCallbackLease())) return;
|
|
683
|
+
const deferred = await this.#rescheduleAfterEffectTransition(
|
|
684
|
+
this.#effects.record(effect.id, lease, "accepted", { status: "deferred" }),
|
|
685
|
+
);
|
|
686
|
+
if (!deferred) return;
|
|
687
|
+
const reclaimed = await this.#rescheduleAfterEffectTransition(
|
|
688
|
+
this.#effects.claim<DiscordInboundEffectPayload>(effect.id, this.#dispatchOwner, this.#dispatchLeaseMs),
|
|
689
|
+
);
|
|
690
|
+
if (!reclaimed) return;
|
|
691
|
+
effect = reclaimed;
|
|
692
|
+
lease = { owner: this.#dispatchOwner, epoch: effect.epoch };
|
|
693
|
+
} catch (error) {
|
|
694
|
+
const definitelyUnsent = this.#isDefiniteCallbackPreSendFailure(error);
|
|
695
|
+
await this.#rescheduleAfterEffectTransition(
|
|
696
|
+
this.#effects.record(effect.id, lease, "accepted", {
|
|
697
|
+
status: definitelyUnsent ? "callback_pre_send_failure" : "defer_intent",
|
|
698
|
+
}),
|
|
699
|
+
);
|
|
700
|
+
throw new Error("Discord interaction callback failed");
|
|
701
|
+
} finally {
|
|
702
|
+
clearInterval(timer);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
const dispatchable = await this.#currentInboundRecord(record, receipt);
|
|
707
|
+
if (!dispatchable) {
|
|
708
|
+
await this.#terminalizeInbound(record, receipt, "stale_binding", lease);
|
|
709
|
+
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
record = dispatchable;
|
|
713
|
+
const endpoint = initialEndpoint ?? (await this.#resolveEndpoint(record.sessionId!, receipt.endpointGeneration));
|
|
714
|
+
if (!this.#matches(record, endpoint) || receipt.endpointGeneration !== endpoint.generation) {
|
|
715
|
+
await this.#rescheduleAfterEffectTransition(
|
|
716
|
+
this.#effects.record(effect.id, lease, "accepted", {
|
|
717
|
+
status: this.#inboundAcceptedStatus(effect, "pre_send_binding_changed"),
|
|
718
|
+
}),
|
|
719
|
+
);
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
let leaseLost = false;
|
|
724
|
+
let renewal: Promise<boolean> | undefined;
|
|
725
|
+
const renew = async (): Promise<boolean> => {
|
|
726
|
+
if (leaseLost) return false;
|
|
727
|
+
if (renewal) return await renewal;
|
|
728
|
+
const currentRenewal = (async (): Promise<boolean> => {
|
|
729
|
+
if (
|
|
730
|
+
!(await this.#rescheduleAfterEffectTransition(
|
|
731
|
+
this.#effects.renew(effect.id, lease, this.#dispatchLeaseMs),
|
|
732
|
+
))
|
|
733
|
+
)
|
|
734
|
+
leaseLost = true;
|
|
735
|
+
return !leaseLost;
|
|
736
|
+
})();
|
|
737
|
+
renewal = currentRenewal;
|
|
738
|
+
try {
|
|
739
|
+
return await currentRenewal;
|
|
740
|
+
} finally {
|
|
741
|
+
if (renewal === currentRenewal) renewal = undefined;
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
const timer = setInterval(
|
|
745
|
+
() => {
|
|
746
|
+
void renew().catch(() => {});
|
|
747
|
+
},
|
|
748
|
+
Math.max(1, Math.floor(this.#dispatchLeaseMs / 3)),
|
|
749
|
+
);
|
|
750
|
+
try {
|
|
751
|
+
if (!(await renew())) return;
|
|
752
|
+
if (!this.#matches(record, endpoint) || receipt.endpointGeneration !== endpoint.generation) {
|
|
753
|
+
await this.#rescheduleAfterEffectTransition(
|
|
754
|
+
this.#effects.record(effect.id, lease, "accepted", {
|
|
755
|
+
status: this.#inboundAcceptedStatus(effect, "pre_send_binding_changed"),
|
|
756
|
+
}),
|
|
757
|
+
);
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
if (!(await renew())) return;
|
|
762
|
+
const beforeSend = await this.#currentInboundRecord(record, receipt);
|
|
763
|
+
if (!beforeSend) {
|
|
764
|
+
await this.#terminalizeInbound(record, receipt, "stale_binding", lease);
|
|
765
|
+
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
record = beforeSend;
|
|
769
|
+
if (!this.#matches(record, endpoint) || receipt.endpointGeneration !== endpoint.generation) {
|
|
770
|
+
await this.#rescheduleAfterEffectTransition(
|
|
771
|
+
this.#effects.record(effect.id, lease, "accepted", {
|
|
772
|
+
status: this.#inboundAcceptedStatus(effect, "pre_send_binding_changed"),
|
|
773
|
+
}),
|
|
774
|
+
);
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
if (effect.payload.type === "command")
|
|
779
|
+
await this.options.onCommand?.(
|
|
780
|
+
record.sessionId!,
|
|
781
|
+
effect.payload.content,
|
|
782
|
+
endpoint,
|
|
783
|
+
effect.payload.idempotencyKey,
|
|
784
|
+
);
|
|
785
|
+
else await endpoint.send(effect.payload);
|
|
786
|
+
if (!leaseLost && (await renew()) && (await this.#effects.record(effect.id, lease, "terminal")))
|
|
787
|
+
await this.#finishInbound(record, receipt);
|
|
788
|
+
} catch (error) {
|
|
789
|
+
if (!leaseLost) {
|
|
790
|
+
const state = this.#isDefiniteSdkPreSendFailure(error) ? "accepted" : "uncertain";
|
|
791
|
+
await this.#rescheduleAfterEffectTransition(
|
|
792
|
+
this.#effects.record(effect.id, lease, state, {
|
|
793
|
+
status: state === "accepted" ? this.#inboundAcceptedStatus(effect, "pre_send_failure") : "uncertain",
|
|
794
|
+
}),
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
} finally {
|
|
798
|
+
clearInterval(timer);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
#hasDurableDeferIntent(effect: ChatEffect<DiscordInboundEffectPayload>): boolean {
|
|
802
|
+
return (
|
|
803
|
+
effect.kind === "discord.inbound.action" &&
|
|
804
|
+
(effect.receipt?.status === "defer_intent" || effect.receipt?.status === "deferred")
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
#inboundAcceptedStatus(effect: ChatEffect<DiscordInboundEffectPayload>, fallback: string): string {
|
|
808
|
+
return this.#hasDurableDeferIntent(effect) ? (effect.receipt?.status ?? fallback) : fallback;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
#hasLiveCallbackLease(effect: ChatEffect | undefined): boolean {
|
|
812
|
+
return (
|
|
813
|
+
effect?.kind === "discord.inbound.action" &&
|
|
814
|
+
effect.state === "leased" &&
|
|
815
|
+
typeof effect.owner === "string" &&
|
|
816
|
+
(effect.leaseExpiresAt ?? 0) > this.#now()
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
async #drainPendingDispatches(): Promise<boolean> {
|
|
821
|
+
await this.#reconcileTerminalInboundReceipts();
|
|
822
|
+
let recoveryFailed = false;
|
|
823
|
+
const dispatched = new Set<string>();
|
|
824
|
+
for (const record of Object.values((await this.#store.load()).conversations)) {
|
|
825
|
+
if (!record.threadId || !record.sessionId || record.state !== "active") continue;
|
|
826
|
+
let endpoint: DiscordEndpointBinding | null = null;
|
|
827
|
+
let endpointResolved = false;
|
|
828
|
+
for (const [index, receipt] of (record.inboundDispatches ?? []).entries()) {
|
|
829
|
+
// Never race a foreground handleInbound() dispatch: while an effect is
|
|
830
|
+
// in-flight it briefly unowns itself (record "accepted"/"deferred" before
|
|
831
|
+
// reclaiming), a window in which recovery could otherwise claim it and
|
|
832
|
+
// deliver its reply on the recovery timer instead of the awaited path.
|
|
833
|
+
if (this.#inflightInbound.has(receipt.effectId)) {
|
|
834
|
+
dispatched.add(receipt.effectId);
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (
|
|
838
|
+
receipt.kind === "action" &&
|
|
839
|
+
(record.inboundDispatches ?? [])
|
|
840
|
+
.slice(0, index)
|
|
841
|
+
.some(
|
|
842
|
+
candidate =>
|
|
843
|
+
candidate.kind === "action" &&
|
|
844
|
+
candidate.actionId === receipt.actionId &&
|
|
845
|
+
candidate.actionNonce === receipt.actionNonce,
|
|
846
|
+
)
|
|
847
|
+
) {
|
|
848
|
+
await this.#terminalizeInbound(record, receipt, "duplicate_action");
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
const effect = await this.#effects.read(receipt.effectId);
|
|
852
|
+
if (!effect) {
|
|
853
|
+
// Referenced effects are retained by terminal pruning. Preserve the
|
|
854
|
+
// receipt and retry recovery rather than acknowledging lost authority.
|
|
855
|
+
recoveryFailed = true;
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (effect?.state === "terminal") {
|
|
859
|
+
await this.#finishInbound(record, receipt);
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
if (this.#hasLiveCallbackLease(effect)) {
|
|
863
|
+
dispatched.add(receipt.effectId);
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
866
|
+
if (!endpointResolved) {
|
|
867
|
+
endpoint = await this.#resolveEndpoint(record.sessionId, receipt.endpointGeneration);
|
|
868
|
+
endpointResolved = true;
|
|
869
|
+
}
|
|
870
|
+
if (!this.#matches(record, endpoint) || endpoint.generation !== receipt.endpointGeneration) {
|
|
871
|
+
await this.#terminalizeInbound(record, receipt, "stale_binding");
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
dispatched.add(receipt.effectId);
|
|
875
|
+
await this.#dispatchInbound(record, endpoint, receipt);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// Effects are the authority: a crash between enqueue and mapping receipt
|
|
880
|
+
// publication must not strand a command at restart. Adoption revalidates
|
|
881
|
+
// the current mapping before an orphan can reach the SDK.
|
|
882
|
+
for (const effect of await this.#effects.list()) {
|
|
883
|
+
if (
|
|
884
|
+
effect.transport !== "discord" ||
|
|
885
|
+
effect.state === "terminal" ||
|
|
886
|
+
!effect.kind.startsWith("discord.inbound.") ||
|
|
887
|
+
dispatched.has(effect.id) ||
|
|
888
|
+
this.#inflightInbound.has(effect.id) ||
|
|
889
|
+
this.#hasLiveCallbackLease(effect)
|
|
890
|
+
)
|
|
891
|
+
continue;
|
|
892
|
+
const payload = effect.payload as DiscordInboundEffectPayload;
|
|
893
|
+
const routing = payload.routing;
|
|
894
|
+
if (!routing) continue;
|
|
895
|
+
const adopted = await this.#adoptOrphanInbound(
|
|
896
|
+
effect.id,
|
|
897
|
+
effect.sessionId,
|
|
898
|
+
effect.endpointGeneration,
|
|
899
|
+
payload,
|
|
900
|
+
routing,
|
|
901
|
+
);
|
|
902
|
+
if (!adopted) continue;
|
|
903
|
+
const endpoint = await this.#resolveEndpoint(adopted.record.sessionId!, effect.endpointGeneration);
|
|
904
|
+
if (!this.#matches(adopted.record, endpoint) || endpoint.generation !== effect.endpointGeneration) {
|
|
905
|
+
await this.#terminalizeInbound(adopted.record, adopted.receipt, "stale_binding");
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
await this.#dispatchInbound(adopted.record, endpoint, adopted.receipt);
|
|
909
|
+
}
|
|
910
|
+
return recoveryFailed;
|
|
911
|
+
}
|
|
912
|
+
async #adoptOrphanInbound(
|
|
913
|
+
effectId: string,
|
|
914
|
+
sessionId: string | undefined,
|
|
915
|
+
endpointGeneration: number,
|
|
916
|
+
payload: DiscordInboundEffectPayload,
|
|
917
|
+
routing: DiscordInboundRouting,
|
|
918
|
+
): Promise<{ record: DiscordConversation; receipt: DiscordInboundDispatchReceipt } | undefined> {
|
|
919
|
+
const key = discordConversationKey({
|
|
920
|
+
appId: this.options.provider.applicationId,
|
|
921
|
+
guildId: routing.guildId,
|
|
922
|
+
parentChannelId: routing.parentId,
|
|
923
|
+
threadId: routing.threadId,
|
|
924
|
+
});
|
|
925
|
+
const expectedId = `discord:${this.options.provider.applicationId}:${routing.guildId}:${routing.parentId}:${routing.threadId}:${routing.eventId}`;
|
|
926
|
+
let record: DiscordConversation | undefined;
|
|
927
|
+
let receipt: DiscordInboundDispatchReceipt | undefined;
|
|
928
|
+
const structurallyValid =
|
|
929
|
+
effectId === expectedId &&
|
|
930
|
+
payload.idempotencyKey === effectId &&
|
|
931
|
+
(payload.type === "command"
|
|
932
|
+
? routing.kind === "command"
|
|
933
|
+
: routing.kind === "action" && payload.id === routing.actionId && typeof routing.actionNonce === "string");
|
|
934
|
+
await this.#store.transact(key, current => {
|
|
935
|
+
if (
|
|
936
|
+
!structurallyValid ||
|
|
937
|
+
!current ||
|
|
938
|
+
current.state !== "active" ||
|
|
939
|
+
current.sessionId !== sessionId ||
|
|
940
|
+
current.endpointGeneration !== endpointGeneration
|
|
941
|
+
)
|
|
942
|
+
return current;
|
|
943
|
+
const existing = (current.inboundDispatches ?? []).find(
|
|
944
|
+
candidate =>
|
|
945
|
+
candidate.eventId === routing.eventId ||
|
|
946
|
+
(routing.interactionId !== undefined && candidate.interactionId === routing.interactionId),
|
|
947
|
+
);
|
|
948
|
+
if (existing) {
|
|
949
|
+
if (
|
|
950
|
+
existing.effectId !== effectId ||
|
|
951
|
+
existing.idempotencyKey !== payload.idempotencyKey ||
|
|
952
|
+
existing.endpointGeneration !== endpointGeneration ||
|
|
953
|
+
existing.kind !== routing.kind ||
|
|
954
|
+
(existing.kind === "action" &&
|
|
955
|
+
(existing.actionId !== routing.actionId ||
|
|
956
|
+
existing.actionNonce !== routing.actionNonce ||
|
|
957
|
+
existing.interactionId !== routing.interactionId))
|
|
958
|
+
)
|
|
959
|
+
return current;
|
|
960
|
+
|
|
961
|
+
record = current;
|
|
962
|
+
receipt = existing;
|
|
963
|
+
return current;
|
|
964
|
+
}
|
|
965
|
+
if (
|
|
966
|
+
current.seenEventIds.includes(routing.eventId) ||
|
|
967
|
+
(routing.interactionId !== undefined && current.seenInteractionIds.includes(routing.interactionId)) ||
|
|
968
|
+
(routing.kind === "action" &&
|
|
969
|
+
(current.pendingActionId !== routing.actionId || current.pendingActionNonce !== routing.actionNonce))
|
|
970
|
+
)
|
|
971
|
+
return current;
|
|
972
|
+
if (
|
|
973
|
+
routing.kind === "action" &&
|
|
974
|
+
(current.inboundDispatches ?? []).some(
|
|
975
|
+
candidate =>
|
|
976
|
+
candidate.kind === "action" &&
|
|
977
|
+
candidate.actionId === routing.actionId &&
|
|
978
|
+
candidate.actionNonce === routing.actionNonce,
|
|
979
|
+
)
|
|
980
|
+
)
|
|
981
|
+
return current;
|
|
982
|
+
|
|
983
|
+
receipt = this.#receiptFromRouting(effectId, endpointGeneration, payload.idempotencyKey, routing);
|
|
984
|
+
record = normalizeDiscordConversation({
|
|
985
|
+
...current,
|
|
986
|
+
generation: current.generation + 1,
|
|
987
|
+
updatedAt: this.#now(),
|
|
988
|
+
inboundDispatches: [...(current.inboundDispatches ?? []), receipt],
|
|
989
|
+
});
|
|
990
|
+
return record;
|
|
991
|
+
});
|
|
992
|
+
if (record && receipt) return { record, receipt };
|
|
993
|
+
await this.#terminalizeRejectedInbound(effectId);
|
|
994
|
+
return undefined;
|
|
995
|
+
}
|
|
996
|
+
#sameInboundReceipt(left: DiscordInboundDispatchReceipt, right: DiscordInboundDispatchReceipt): boolean {
|
|
997
|
+
return (
|
|
998
|
+
left.key === right.key &&
|
|
999
|
+
left.eventId === right.eventId &&
|
|
1000
|
+
left.interactionId === right.interactionId &&
|
|
1001
|
+
left.kind === right.kind &&
|
|
1002
|
+
left.actionId === right.actionId &&
|
|
1003
|
+
left.actionNonce === right.actionNonce &&
|
|
1004
|
+
left.endpointGeneration === right.endpointGeneration &&
|
|
1005
|
+
left.effectId === right.effectId &&
|
|
1006
|
+
left.idempotencyKey === right.idempotencyKey
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
#completeInbound(record: DiscordConversation, receipt: DiscordInboundDispatchReceipt): DiscordConversation {
|
|
1010
|
+
const clearsAction =
|
|
1011
|
+
receipt.kind === "action" &&
|
|
1012
|
+
record.pendingActionId === receipt.actionId &&
|
|
1013
|
+
record.pendingActionNonce === receipt.actionNonce;
|
|
1014
|
+
return normalizeDiscordConversation({
|
|
1015
|
+
...record,
|
|
1016
|
+
generation: record.generation + 1,
|
|
1017
|
+
updatedAt: this.#now(),
|
|
1018
|
+
pendingActionId: clearsAction ? undefined : record.pendingActionId,
|
|
1019
|
+
pendingActionNonce: clearsAction ? undefined : record.pendingActionNonce,
|
|
1020
|
+
pendingActionEffectId: clearsAction ? undefined : record.pendingActionEffectId,
|
|
1021
|
+
seenEventIds: [...record.seenEventIds, receipt.eventId],
|
|
1022
|
+
seenInteractionIds:
|
|
1023
|
+
receipt.interactionId === undefined
|
|
1024
|
+
? record.seenInteractionIds
|
|
1025
|
+
: [...record.seenInteractionIds, receipt.interactionId],
|
|
1026
|
+
inboundDispatches: (record.inboundDispatches ?? []).filter(
|
|
1027
|
+
candidate => !this.#sameInboundReceipt(candidate, receipt),
|
|
1028
|
+
),
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
#hasLiveEffectLease(effect: ChatEffect | undefined): boolean {
|
|
1032
|
+
return (
|
|
1033
|
+
effect?.state === "leased" && typeof effect.owner === "string" && (effect.leaseExpiresAt ?? 0) > this.#now()
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
async #terminalizeEffect(id: string, status: string, lease?: ChatEffectLease): Promise<boolean> {
|
|
1037
|
+
if (lease) {
|
|
1038
|
+
const terminalized = await this.#effects.record(id, lease, "terminal", { status });
|
|
1039
|
+
return terminalized?.state === "terminal";
|
|
1040
|
+
}
|
|
1041
|
+
const effect = await this.#effects.read(id);
|
|
1042
|
+
if (!effect || effect.state === "terminal") return true;
|
|
1043
|
+
if (effect.state === "uncertain" || this.#hasLiveEffectLease(effect)) return false;
|
|
1044
|
+
if (effect.state === "leased") {
|
|
1045
|
+
const claimed = await this.#effects.claim(id, this.#providerOwner, this.#providerLeaseMs);
|
|
1046
|
+
if (!claimed) return false;
|
|
1047
|
+
await this.#effects.record(id, { owner: this.#providerOwner, epoch: claimed.epoch }, "uncertain", {
|
|
1048
|
+
status: "stale_lease_expired",
|
|
1049
|
+
});
|
|
1050
|
+
return false;
|
|
1051
|
+
}
|
|
1052
|
+
return (await this.#effects.terminalize(id, { status }))?.state === "terminal";
|
|
1053
|
+
}
|
|
1054
|
+
async #terminalizeRejectedInbound(effectId: string, lease?: ChatEffectLease): Promise<void> {
|
|
1055
|
+
await this.#terminalizeEffect(effectId, "rejected", lease);
|
|
1056
|
+
}
|
|
1057
|
+
async #terminalizeInbound(
|
|
1058
|
+
record: DiscordConversation,
|
|
1059
|
+
receipt: DiscordInboundDispatchReceipt,
|
|
1060
|
+
status: string,
|
|
1061
|
+
lease?: ChatEffectLease,
|
|
1062
|
+
): Promise<void> {
|
|
1063
|
+
if (!(await this.#terminalizeEffect(receipt.effectId, status, lease))) return;
|
|
1064
|
+
const key = discordConversationKey({
|
|
1065
|
+
appId: record.appId,
|
|
1066
|
+
guildId: record.guildId,
|
|
1067
|
+
parentChannelId: record.parentChannelId,
|
|
1068
|
+
threadId: record.threadId!,
|
|
1069
|
+
});
|
|
1070
|
+
await this.#store.transact(key, current => {
|
|
1071
|
+
const matching = current?.inboundDispatches?.find(candidate => this.#sameInboundReceipt(candidate, receipt));
|
|
1072
|
+
return !current || !matching ? current : this.#completeInbound(current, matching);
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
async #currentInboundRecord(
|
|
1076
|
+
record: DiscordConversation,
|
|
1077
|
+
receipt: DiscordInboundDispatchReceipt,
|
|
1078
|
+
): Promise<DiscordConversation | undefined> {
|
|
1079
|
+
const current = await this.#byThread(record.guildId, record.parentChannelId, record.threadId!);
|
|
1080
|
+
const claimed = current?.inboundDispatches?.find(candidate => this.#sameInboundReceipt(candidate, receipt));
|
|
1081
|
+
if (
|
|
1082
|
+
current?.state !== "active" ||
|
|
1083
|
+
current.sessionId !== record.sessionId ||
|
|
1084
|
+
current.endpointGeneration !== receipt.endpointGeneration ||
|
|
1085
|
+
!claimed
|
|
1086
|
+
)
|
|
1087
|
+
return undefined;
|
|
1088
|
+
if (
|
|
1089
|
+
receipt.kind === "action" &&
|
|
1090
|
+
(current.pendingActionId !== receipt.actionId || current.pendingActionNonce !== receipt.actionNonce)
|
|
1091
|
+
)
|
|
1092
|
+
return undefined;
|
|
1093
|
+
|
|
1094
|
+
return current;
|
|
1095
|
+
}
|
|
1096
|
+
#receiptFromRouting(
|
|
1097
|
+
effectId: string,
|
|
1098
|
+
endpointGeneration: number,
|
|
1099
|
+
idempotencyKey: string,
|
|
1100
|
+
routing: DiscordInboundRouting,
|
|
1101
|
+
): DiscordInboundDispatchReceipt {
|
|
1102
|
+
return routing.kind === "command"
|
|
1103
|
+
? {
|
|
1104
|
+
key: routing.eventId,
|
|
1105
|
+
eventId: routing.eventId,
|
|
1106
|
+
kind: "command",
|
|
1107
|
+
endpointGeneration,
|
|
1108
|
+
effectId,
|
|
1109
|
+
idempotencyKey,
|
|
1110
|
+
}
|
|
1111
|
+
: {
|
|
1112
|
+
key: routing.eventId,
|
|
1113
|
+
eventId: routing.eventId,
|
|
1114
|
+
interactionId: routing.interactionId!,
|
|
1115
|
+
kind: "action",
|
|
1116
|
+
actionId: routing.actionId!,
|
|
1117
|
+
actionNonce: routing.actionNonce!,
|
|
1118
|
+
endpointGeneration,
|
|
1119
|
+
effectId,
|
|
1120
|
+
idempotencyKey,
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
async #finishInbound(record: DiscordConversation, receipt: DiscordInboundDispatchReceipt): Promise<void> {
|
|
1125
|
+
const key = discordConversationKey({
|
|
1126
|
+
appId: record.appId,
|
|
1127
|
+
guildId: record.guildId,
|
|
1128
|
+
parentChannelId: record.parentChannelId,
|
|
1129
|
+
threadId: record.threadId!,
|
|
1130
|
+
});
|
|
1131
|
+
await this.#store.transact(key, current => {
|
|
1132
|
+
const matching = current?.inboundDispatches?.find(candidate => this.#sameInboundReceipt(candidate, receipt));
|
|
1133
|
+
return !current || !matching ? current : this.#completeInbound(current, matching);
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
async #reconcileTerminalInboundReceipts(): Promise<void> {
|
|
1137
|
+
for (const effect of await this.#effects.list()) {
|
|
1138
|
+
if (
|
|
1139
|
+
effect.transport !== "discord" ||
|
|
1140
|
+
effect.state !== "terminal" ||
|
|
1141
|
+
(effect.kind !== "discord.inbound.command" && effect.kind !== "discord.inbound.action")
|
|
1142
|
+
)
|
|
1143
|
+
continue;
|
|
1144
|
+
const payload = effect.payload as DiscordInboundEffectPayload;
|
|
1145
|
+
const routing = payload?.routing;
|
|
1146
|
+
if (
|
|
1147
|
+
!routing ||
|
|
1148
|
+
!effect.sessionId ||
|
|
1149
|
+
!routing.guildId ||
|
|
1150
|
+
!routing.parentId ||
|
|
1151
|
+
!routing.threadId ||
|
|
1152
|
+
!routing.eventId ||
|
|
1153
|
+
payload.idempotencyKey !== effect.id ||
|
|
1154
|
+
effect.id !==
|
|
1155
|
+
`discord:${this.options.provider.applicationId}:${routing.guildId}:${routing.parentId}:${routing.threadId}:${routing.eventId}` ||
|
|
1156
|
+
(payload.type === "command"
|
|
1157
|
+
? routing.kind !== "command" || effect.kind !== "discord.inbound.command"
|
|
1158
|
+
: routing.kind !== "action" ||
|
|
1159
|
+
effect.kind !== "discord.inbound.action" ||
|
|
1160
|
+
!routing.interactionId ||
|
|
1161
|
+
!routing.actionId ||
|
|
1162
|
+
!routing.actionNonce ||
|
|
1163
|
+
payload.id !== routing.actionId)
|
|
1164
|
+
)
|
|
1165
|
+
continue;
|
|
1166
|
+
const receipt = this.#receiptFromRouting(
|
|
1167
|
+
effect.id,
|
|
1168
|
+
effect.endpointGeneration,
|
|
1169
|
+
payload.idempotencyKey,
|
|
1170
|
+
routing,
|
|
1171
|
+
);
|
|
1172
|
+
const record = await this.#byThread(routing.guildId, routing.parentId, routing.threadId);
|
|
1173
|
+
if (record?.inboundDispatches?.some(candidate => this.#sameInboundReceipt(candidate, receipt)))
|
|
1174
|
+
await this.#finishInbound(record, receipt);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
#matches(record: DiscordConversation, endpoint: DiscordEndpointBinding | null): endpoint is DiscordEndpointBinding {
|
|
1179
|
+
if (!endpoint?.isCurrent()) return false;
|
|
1180
|
+
return record.state === "active" && record.endpointGeneration === endpoint.generation;
|
|
1181
|
+
}
|
|
1182
|
+
async #bindingCurrent(sessionId: string, endpointGeneration: number): Promise<boolean> {
|
|
1183
|
+
try {
|
|
1184
|
+
const endpoint = await this.#resolveEndpoint(sessionId, endpointGeneration);
|
|
1185
|
+
return !!endpoint && endpoint.isCurrent() && endpoint.generation === endpointGeneration;
|
|
1186
|
+
} catch {
|
|
1187
|
+
return false;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
async #requireLiveBinding(sessionId: string, endpointGeneration: number): Promise<void> {
|
|
1191
|
+
if (!(await this.#bindingCurrent(sessionId, endpointGeneration))) throw new DiscordEndpointBindingError();
|
|
1192
|
+
}
|
|
1193
|
+
#isDefiniteSdkPreSendFailure(error: unknown): boolean {
|
|
1194
|
+
if (error instanceof DiscordEndpointBindingError) return true;
|
|
1195
|
+
if (error instanceof SdkClientError) return error.code === "connection_closed";
|
|
1196
|
+
return (
|
|
1197
|
+
error instanceof Error &&
|
|
1198
|
+
error.name === "ChatDeliveryError" &&
|
|
1199
|
+
(error as ChatDeliveryError).phase === "pre_send"
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
#isDefiniteCallbackPreSendFailure(error: unknown): boolean {
|
|
1203
|
+
// This is intentionally limited to Discord's received-and-rejected callback
|
|
1204
|
+
// response. Transport errors, connection loss, and arbitrary provider errors
|
|
1205
|
+
// leave the persisted defer intent authoritative because the callback may have
|
|
1206
|
+
// been accepted before its HTTP response was lost.
|
|
1207
|
+
return error instanceof Error && /^Discord API request failed \(4\d\d\)$/.test(error.message);
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
async #ensureConversation(input: DiscordNotificationInput): Promise<DiscordConversation> {
|
|
1211
|
+
const existing = await this.#bySession(input.sessionId);
|
|
1212
|
+
if (existing && closingIntent(existing)) {
|
|
1213
|
+
await this.#driveClose(existing);
|
|
1214
|
+
throw new Error("Discord thread is closing");
|
|
1215
|
+
}
|
|
1216
|
+
if (existing?.state === "active" && existing.threadId) {
|
|
1217
|
+
if (existing.endpointGeneration === input.endpointGeneration) return existing;
|
|
1218
|
+
await this.#requireLiveBinding(input.sessionId, input.endpointGeneration);
|
|
1219
|
+
return await this.#replace(existing, { ...existing, endpointGeneration: input.endpointGeneration });
|
|
1220
|
+
}
|
|
1221
|
+
const inFlight = this.#creates.get(input.sessionId);
|
|
1222
|
+
if (inFlight) {
|
|
1223
|
+
let created: DiscordConversation;
|
|
1224
|
+
try {
|
|
1225
|
+
created = await inFlight;
|
|
1226
|
+
} catch {
|
|
1227
|
+
await this.#requireLiveBinding(input.sessionId, input.endpointGeneration);
|
|
1228
|
+
return await this.#ensureConversation(input);
|
|
1229
|
+
}
|
|
1230
|
+
if (created.endpointGeneration === input.endpointGeneration) return created;
|
|
1231
|
+
await this.#requireLiveBinding(input.sessionId, input.endpointGeneration);
|
|
1232
|
+
return await this.#replace(created, { ...created, endpointGeneration: input.endpointGeneration });
|
|
1233
|
+
}
|
|
1234
|
+
const pending = this.#create(input.sessionId, input.endpointGeneration, randomUUID(), input.threadName);
|
|
1235
|
+
this.#creates.set(input.sessionId, pending);
|
|
1236
|
+
try {
|
|
1237
|
+
return await pending;
|
|
1238
|
+
} finally {
|
|
1239
|
+
this.#creates.delete(input.sessionId);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
async #create(
|
|
1244
|
+
sessionId: string,
|
|
1245
|
+
endpointGeneration: number,
|
|
1246
|
+
nonce: string,
|
|
1247
|
+
name = "SKC session",
|
|
1248
|
+
): Promise<DiscordConversation> {
|
|
1249
|
+
const intentKey = this.#intentKey(sessionId);
|
|
1250
|
+
const owner = randomUUID();
|
|
1251
|
+
let intent: DiscordConversation | undefined;
|
|
1252
|
+
for (;;) {
|
|
1253
|
+
const active = await this.#bySession(sessionId);
|
|
1254
|
+
if (active && closingIntent(active)) {
|
|
1255
|
+
await this.#driveClose(active);
|
|
1256
|
+
throw new Error("Discord thread is closing");
|
|
1257
|
+
}
|
|
1258
|
+
if (active?.state === "active" && active.threadId) {
|
|
1259
|
+
if (active.endpointGeneration === endpointGeneration) return active;
|
|
1260
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
1261
|
+
return await this.#replace(active, { ...active, endpointGeneration });
|
|
1262
|
+
}
|
|
1263
|
+
const now = this.#now();
|
|
1264
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
1265
|
+
intent = await this.#store.transact(intentKey, old => {
|
|
1266
|
+
if (old?.state === "creating" && old.createOwner && (old.createLeaseExpiresAt ?? 0) > now) return old;
|
|
1267
|
+
return {
|
|
1268
|
+
generation: (old?.generation ?? 0) + 1,
|
|
1269
|
+
state: "creating",
|
|
1270
|
+
appId: this.options.provider.applicationId,
|
|
1271
|
+
guildId: this.options.guildId,
|
|
1272
|
+
parentChannelId: this.options.parentChannelId,
|
|
1273
|
+
sessionId,
|
|
1274
|
+
endpointGeneration,
|
|
1275
|
+
createNonce: old?.createNonce ?? nonce,
|
|
1276
|
+
createOwner: owner,
|
|
1277
|
+
createLeaseExpiresAt: now + 60_000,
|
|
1278
|
+
updatedAt: now,
|
|
1279
|
+
seenEventIds: [],
|
|
1280
|
+
seenInteractionIds: [],
|
|
1281
|
+
};
|
|
1282
|
+
});
|
|
1283
|
+
if (!intent) throw new Error("Unable to persist Discord create intent");
|
|
1284
|
+
if (intent.createOwner === owner) break;
|
|
1285
|
+
await Bun.sleep(Math.min(25, Math.max(1, (intent.createLeaseExpiresAt ?? now) - now)));
|
|
1286
|
+
}
|
|
1287
|
+
const active = await this.#bySession(sessionId);
|
|
1288
|
+
if (active?.state === "active" && active.threadId) {
|
|
1289
|
+
if (active.endpointGeneration === endpointGeneration) return active;
|
|
1290
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
1291
|
+
return await this.#replace(active, { ...active, endpointGeneration });
|
|
1292
|
+
}
|
|
1293
|
+
let thread: DiscordThread | null;
|
|
1294
|
+
try {
|
|
1295
|
+
thread = await this.#withCreateIntentLease(intent, () => this.#createThreadEffect(intent, name));
|
|
1296
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
1297
|
+
} catch (error) {
|
|
1298
|
+
await this.#abandonCreator(intentKey, intent);
|
|
1299
|
+
throw error;
|
|
1300
|
+
}
|
|
1301
|
+
const currentIntent = await this.#store.read(intentKey);
|
|
1302
|
+
if (
|
|
1303
|
+
currentIntent?.state !== "creating" ||
|
|
1304
|
+
currentIntent.createOwner !== intent.createOwner ||
|
|
1305
|
+
currentIntent.generation !== intent.generation ||
|
|
1306
|
+
(currentIntent.createLeaseExpiresAt ?? 0) <= this.#now()
|
|
1307
|
+
) {
|
|
1308
|
+
throw new Error("Discord create intent lost its fence before mapping commit");
|
|
1309
|
+
}
|
|
1310
|
+
await this.#requireLiveBinding(sessionId, endpointGeneration);
|
|
1311
|
+
const key = discordConversationKey({
|
|
1312
|
+
appId: intent.appId,
|
|
1313
|
+
guildId: intent.guildId,
|
|
1314
|
+
parentChannelId: intent.parentChannelId,
|
|
1315
|
+
threadId: thread.id,
|
|
1316
|
+
});
|
|
1317
|
+
const record = await this.#store.transact(key, old =>
|
|
1318
|
+
normalizeDiscordConversation({
|
|
1319
|
+
generation: (old?.generation ?? 0) + 1,
|
|
1320
|
+
state: "active",
|
|
1321
|
+
appId: intent.appId,
|
|
1322
|
+
guildId: intent.guildId,
|
|
1323
|
+
parentChannelId: intent.parentChannelId,
|
|
1324
|
+
threadId: thread.id,
|
|
1325
|
+
sessionId,
|
|
1326
|
+
endpointGeneration,
|
|
1327
|
+
createNonce: intent.createNonce,
|
|
1328
|
+
effectIncarnationId: old?.effectIncarnationId ?? intent.effectIncarnationId ?? randomUUID(),
|
|
1329
|
+
updatedAt: this.#now(),
|
|
1330
|
+
seenEventIds: old?.seenEventIds ?? [],
|
|
1331
|
+
seenInteractionIds: old?.seenInteractionIds ?? [],
|
|
1332
|
+
inboundDispatches: old?.inboundDispatches,
|
|
1333
|
+
}),
|
|
1334
|
+
);
|
|
1335
|
+
if (!record) throw new Error("Unable to persist Discord thread mapping");
|
|
1336
|
+
await this.#store.delete(intentKey, intent.generation);
|
|
1337
|
+
return record;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
async #sessionMappings(sessionId: string): Promise<DiscordConversation[]> {
|
|
1341
|
+
return Object.values((await this.#store.load()).conversations).filter(
|
|
1342
|
+
record => record.sessionId === sessionId && record.state !== "creating",
|
|
1343
|
+
);
|
|
1344
|
+
}
|
|
1345
|
+
async #bySession(sessionId: string): Promise<DiscordConversation | undefined> {
|
|
1346
|
+
const document = await this.#store.load();
|
|
1347
|
+
return Object.values(document.conversations)
|
|
1348
|
+
.filter(record => record.sessionId === sessionId && record.state !== "creating")
|
|
1349
|
+
.sort(
|
|
1350
|
+
(left, right) =>
|
|
1351
|
+
stateRank(left.state) - stateRank(right.state) ||
|
|
1352
|
+
right.generation - left.generation ||
|
|
1353
|
+
right.updatedAt - left.updatedAt,
|
|
1354
|
+
)[0];
|
|
1355
|
+
}
|
|
1356
|
+
async #byThread(
|
|
1357
|
+
guildId: string,
|
|
1358
|
+
parentChannelId: string,
|
|
1359
|
+
threadId: string,
|
|
1360
|
+
): Promise<DiscordConversation | undefined> {
|
|
1361
|
+
return await this.#store.read(
|
|
1362
|
+
discordConversationKey({ appId: this.options.provider.applicationId, guildId, parentChannelId, threadId }),
|
|
1363
|
+
);
|
|
1364
|
+
}
|
|
1365
|
+
#closeMarkerEffectId(record: DiscordConversation): string {
|
|
1366
|
+
const intent = closingIntent(record);
|
|
1367
|
+
if (!record.threadId || !intent) throw new Error("Discord close intent is unavailable.");
|
|
1368
|
+
return `close-marker:${record.threadId}:${intent.nonce}`;
|
|
1369
|
+
}
|
|
1370
|
+
#closeArchiveEffectId(record: DiscordConversation): string {
|
|
1371
|
+
const intent = closingIntent(record);
|
|
1372
|
+
if (!record.threadId || !intent) throw new Error("Discord close intent is unavailable.");
|
|
1373
|
+
return `close-archive:${record.threadId}:${intent.nonce}`;
|
|
1374
|
+
}
|
|
1375
|
+
async #markClosing(sessionId: string): Promise<DiscordConversation | undefined> {
|
|
1376
|
+
const record = await this.#bySession(sessionId);
|
|
1377
|
+
if (!record?.threadId || record.state === "closed") return undefined;
|
|
1378
|
+
const key = discordConversationKey({
|
|
1379
|
+
appId: record.appId,
|
|
1380
|
+
guildId: record.guildId,
|
|
1381
|
+
parentChannelId: record.parentChannelId,
|
|
1382
|
+
threadId: record.threadId,
|
|
1383
|
+
});
|
|
1384
|
+
let closing: DiscordConversation | undefined;
|
|
1385
|
+
await this.#store.transact(key, current => {
|
|
1386
|
+
if (!current || current.sessionId !== sessionId || !current.threadId || current.state === "closed")
|
|
1387
|
+
return current;
|
|
1388
|
+
if (closingIntent(current)) {
|
|
1389
|
+
closing = current;
|
|
1390
|
+
return current;
|
|
1391
|
+
}
|
|
1392
|
+
closing = normalizeDiscordConversation(withClosingIntent(current, randomUUID(), this.#now()));
|
|
1393
|
+
return closing;
|
|
1394
|
+
});
|
|
1395
|
+
return closing;
|
|
1396
|
+
}
|
|
1397
|
+
async #driveClose(record: DiscordConversation): Promise<void> {
|
|
1398
|
+
if (!record.threadId) return;
|
|
1399
|
+
const key = discordConversationKey({
|
|
1400
|
+
appId: record.appId,
|
|
1401
|
+
guildId: record.guildId,
|
|
1402
|
+
parentChannelId: record.parentChannelId,
|
|
1403
|
+
threadId: record.threadId,
|
|
1404
|
+
});
|
|
1405
|
+
let current = await this.#store.read(key);
|
|
1406
|
+
if (!current || !closingIntent(current)) return;
|
|
1407
|
+
for (const receipt of current.inboundDispatches ?? [])
|
|
1408
|
+
await this.#terminalizeInbound(current, receipt, "closing");
|
|
1409
|
+
current = await this.#store.read(key);
|
|
1410
|
+
if (!current || !closingIntent(current)) return;
|
|
1411
|
+
const closingRecord = current;
|
|
1412
|
+
const intent = closingIntent(closingRecord)!;
|
|
1413
|
+
await this.#postEffect(
|
|
1414
|
+
this.#closeMarkerEffectId(closingRecord),
|
|
1415
|
+
closingRecord,
|
|
1416
|
+
"This conversation is closed.",
|
|
1417
|
+
undefined,
|
|
1418
|
+
false,
|
|
1419
|
+
true,
|
|
1420
|
+
);
|
|
1421
|
+
await this.#threadEffect(this.#closeArchiveEffectId(closingRecord), closingRecord, "archive", true, true);
|
|
1422
|
+
await this.#store.transact(key, candidate => {
|
|
1423
|
+
const candidateIntent = closingIntent(candidate);
|
|
1424
|
+
return candidate && candidate.sessionId === closingRecord.sessionId && candidateIntent?.nonce === intent.nonce
|
|
1425
|
+
? normalizeDiscordConversation(withoutClosingIntent(candidate, this.#now()))
|
|
1426
|
+
: candidate;
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
async #recoverClosingConversations(): Promise<boolean> {
|
|
1430
|
+
let failed = false;
|
|
1431
|
+
for (const record of Object.values((await this.#store.load()).conversations)) {
|
|
1432
|
+
if (!closingIntent(record)) continue;
|
|
1433
|
+
try {
|
|
1434
|
+
await this.#driveClose(record);
|
|
1435
|
+
} catch {
|
|
1436
|
+
failed = true;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
return failed;
|
|
1440
|
+
}
|
|
1441
|
+
async #replace(
|
|
1442
|
+
current: DiscordConversation,
|
|
1443
|
+
next: Omit<DiscordConversation, "generation"> & { generation?: number },
|
|
1444
|
+
): Promise<DiscordConversation> {
|
|
1445
|
+
const key = current.threadId
|
|
1446
|
+
? discordConversationKey({
|
|
1447
|
+
appId: current.appId,
|
|
1448
|
+
guildId: current.guildId,
|
|
1449
|
+
parentChannelId: current.parentChannelId,
|
|
1450
|
+
threadId: current.threadId,
|
|
1451
|
+
})
|
|
1452
|
+
: this.#intentKey(current.sessionId!);
|
|
1453
|
+
const result = await this.#store.write(key, current.generation, {
|
|
1454
|
+
...next,
|
|
1455
|
+
generation: current.generation + 1,
|
|
1456
|
+
updatedAt: this.#now(),
|
|
1457
|
+
});
|
|
1458
|
+
if (!result) {
|
|
1459
|
+
const stored = await this.#store.read(key);
|
|
1460
|
+
throw new Error(
|
|
1461
|
+
`Discord conversation changed concurrently (key=${key}, expected=${current.generation}, actual=${stored?.generation ?? "missing"})`,
|
|
1462
|
+
);
|
|
1463
|
+
}
|
|
1464
|
+
return (await this.#store.read(key))!;
|
|
1465
|
+
}
|
|
1466
|
+
async #beginArchive(record: DiscordConversation): Promise<DiscordConversation> {
|
|
1467
|
+
const key = discordConversationKey({
|
|
1468
|
+
appId: record.appId,
|
|
1469
|
+
guildId: record.guildId,
|
|
1470
|
+
parentChannelId: record.parentChannelId,
|
|
1471
|
+
threadId: record.threadId!,
|
|
1472
|
+
});
|
|
1473
|
+
let archiving: DiscordConversation | undefined;
|
|
1474
|
+
await this.#store.transact(key, current => {
|
|
1475
|
+
if (
|
|
1476
|
+
current?.state !== "active" ||
|
|
1477
|
+
current.sessionId !== record.sessionId ||
|
|
1478
|
+
current.endpointGeneration !== record.endpointGeneration
|
|
1479
|
+
)
|
|
1480
|
+
return current;
|
|
1481
|
+
if (current.archiveOccurrenceId) {
|
|
1482
|
+
archiving = current;
|
|
1483
|
+
return current;
|
|
1484
|
+
}
|
|
1485
|
+
const archiveOccurrenceId = randomUUID();
|
|
1486
|
+
const effectIncarnationId = backfilledEffectIncarnationId(current);
|
|
1487
|
+
archiving = normalizeDiscordConversation({
|
|
1488
|
+
...current,
|
|
1489
|
+
generation: current.generation + 1,
|
|
1490
|
+
updatedAt: this.#now(),
|
|
1491
|
+
effectIncarnationId,
|
|
1492
|
+
archiveOccurrenceId,
|
|
1493
|
+
archiveEffectId: `archive:${current.threadId}:${effectIncarnationId}:${archiveOccurrenceId}`,
|
|
1494
|
+
});
|
|
1495
|
+
|
|
1496
|
+
return archiving;
|
|
1497
|
+
});
|
|
1498
|
+
if (!archiving) throw new Error("Discord archive intent lost its authority");
|
|
1499
|
+
return archiving;
|
|
1500
|
+
}
|
|
1501
|
+
async #completeArchive(record: DiscordConversation, occurrenceId: string): Promise<void> {
|
|
1502
|
+
const key = discordConversationKey({
|
|
1503
|
+
appId: record.appId,
|
|
1504
|
+
guildId: record.guildId,
|
|
1505
|
+
parentChannelId: record.parentChannelId,
|
|
1506
|
+
threadId: record.threadId!,
|
|
1507
|
+
});
|
|
1508
|
+
await this.#store.transact(key, current => {
|
|
1509
|
+
if (
|
|
1510
|
+
current?.state !== "active" ||
|
|
1511
|
+
current.sessionId !== record.sessionId ||
|
|
1512
|
+
current.archiveOccurrenceId !== occurrenceId
|
|
1513
|
+
)
|
|
1514
|
+
return current;
|
|
1515
|
+
return normalizeDiscordConversation({
|
|
1516
|
+
...current,
|
|
1517
|
+
generation: current.generation + 1,
|
|
1518
|
+
updatedAt: this.#now(),
|
|
1519
|
+
state: "archived",
|
|
1520
|
+
archivedAt: this.#now(),
|
|
1521
|
+
archiveOccurrenceId: undefined,
|
|
1522
|
+
archiveEffectId: undefined,
|
|
1523
|
+
});
|
|
1524
|
+
});
|
|
1525
|
+
}
|
|
1526
|
+
async #completeResume(record: DiscordConversation, occurrenceId: string): Promise<DiscordConversation> {
|
|
1527
|
+
const key = discordConversationKey({
|
|
1528
|
+
appId: record.appId,
|
|
1529
|
+
guildId: record.guildId,
|
|
1530
|
+
parentChannelId: record.parentChannelId,
|
|
1531
|
+
threadId: record.threadId!,
|
|
1532
|
+
});
|
|
1533
|
+
const completed = await this.#store.transact(key, current => {
|
|
1534
|
+
if (
|
|
1535
|
+
!current ||
|
|
1536
|
+
current.sessionId !== record.sessionId ||
|
|
1537
|
+
current.endpointGeneration !== record.endpointGeneration
|
|
1538
|
+
)
|
|
1539
|
+
return current;
|
|
1540
|
+
if (current.state === "active" && current.resumeOccurrenceId === undefined) return current;
|
|
1541
|
+
if (current.state !== "resuming" || current.resumeOccurrenceId !== occurrenceId) return current;
|
|
1542
|
+
return normalizeDiscordConversation({
|
|
1543
|
+
...current,
|
|
1544
|
+
generation: current.generation + 1,
|
|
1545
|
+
updatedAt: this.#now(),
|
|
1546
|
+
state: "active",
|
|
1547
|
+
archivedAt: undefined,
|
|
1548
|
+
resumeOccurrenceId: undefined,
|
|
1549
|
+
resumeEffectId: undefined,
|
|
1550
|
+
});
|
|
1551
|
+
});
|
|
1552
|
+
if (completed?.state !== "active" || completed.sessionId !== record.sessionId)
|
|
1553
|
+
throw new Error("Discord resume occurrence lost its authority");
|
|
1554
|
+
return completed;
|
|
1555
|
+
}
|
|
1556
|
+
async #abandonCreator(intentKey: string, intent: DiscordConversation): Promise<void> {
|
|
1557
|
+
await this.#store.transact(intentKey, current => {
|
|
1558
|
+
if (!current || current.generation !== intent.generation || current.createOwner !== intent.createOwner)
|
|
1559
|
+
return current;
|
|
1560
|
+
return normalizeDiscordConversation({
|
|
1561
|
+
...current,
|
|
1562
|
+
generation: current.generation + 1,
|
|
1563
|
+
updatedAt: this.#now(),
|
|
1564
|
+
createOwner: undefined,
|
|
1565
|
+
createLeaseExpiresAt: undefined,
|
|
1566
|
+
});
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
#intentKey(sessionId: string): string {
|
|
1570
|
+
return `${this.options.provider.applicationId}:${this.options.guildId}:${this.options.parentChannelId}:creating:${sessionId}`;
|
|
1571
|
+
}
|
|
1572
|
+
async #withCreateIntentLease<T>(intent: DiscordConversation, work: () => Promise<T>): Promise<T> {
|
|
1573
|
+
let lost = false;
|
|
1574
|
+
let renewal: Promise<boolean> | undefined;
|
|
1575
|
+
let expectedGeneration = intent.generation;
|
|
1576
|
+
const renew = async (): Promise<boolean> => {
|
|
1577
|
+
if (lost) return false;
|
|
1578
|
+
if (renewal) return await renewal;
|
|
1579
|
+
const currentRenewal = (async (): Promise<boolean> => {
|
|
1580
|
+
const now = this.#now();
|
|
1581
|
+
const current = await this.#store.transact(this.#intentKey(intent.sessionId!), candidate => {
|
|
1582
|
+
if (
|
|
1583
|
+
candidate?.state !== "creating" ||
|
|
1584
|
+
candidate.createOwner !== intent.createOwner ||
|
|
1585
|
+
candidate.generation !== expectedGeneration ||
|
|
1586
|
+
(candidate.createLeaseExpiresAt ?? 0) <= now
|
|
1587
|
+
)
|
|
1588
|
+
return candidate;
|
|
1589
|
+
return {
|
|
1590
|
+
...candidate,
|
|
1591
|
+
generation: candidate.generation + 1,
|
|
1592
|
+
createLeaseExpiresAt: now + this.#providerLeaseMs,
|
|
1593
|
+
updatedAt: now,
|
|
1594
|
+
};
|
|
1595
|
+
});
|
|
1596
|
+
if (
|
|
1597
|
+
current?.state !== "creating" ||
|
|
1598
|
+
current.createOwner !== intent.createOwner ||
|
|
1599
|
+
current.generation !== expectedGeneration + 1 ||
|
|
1600
|
+
(current.createLeaseExpiresAt ?? 0) <= now
|
|
1601
|
+
) {
|
|
1602
|
+
lost = true;
|
|
1603
|
+
} else {
|
|
1604
|
+
expectedGeneration = current.generation;
|
|
1605
|
+
intent.generation = current.generation;
|
|
1606
|
+
intent.createLeaseExpiresAt = current.createLeaseExpiresAt;
|
|
1607
|
+
}
|
|
1608
|
+
return !lost;
|
|
1609
|
+
})();
|
|
1610
|
+
renewal = currentRenewal;
|
|
1611
|
+
try {
|
|
1612
|
+
return await currentRenewal;
|
|
1613
|
+
} finally {
|
|
1614
|
+
if (renewal === currentRenewal) renewal = undefined;
|
|
1615
|
+
}
|
|
1616
|
+
};
|
|
1617
|
+
if (!(await renew())) throw new Error("Discord create intent lost its fence");
|
|
1618
|
+
const timer = setInterval(
|
|
1619
|
+
() => {
|
|
1620
|
+
void renew().catch(() => {});
|
|
1621
|
+
},
|
|
1622
|
+
Math.max(1, Math.floor(this.#providerLeaseMs / 3)),
|
|
1623
|
+
);
|
|
1624
|
+
try {
|
|
1625
|
+
const result = await work();
|
|
1626
|
+
if (!(await renew())) throw new Error("Discord create intent lost its fence");
|
|
1627
|
+
return result;
|
|
1628
|
+
} finally {
|
|
1629
|
+
clearInterval(timer);
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
async #clearPending(record: DiscordConversation, actionId: string, actionNonce: string): Promise<void> {
|
|
1633
|
+
const key = record.threadId
|
|
1634
|
+
? discordConversationKey({
|
|
1635
|
+
appId: record.appId,
|
|
1636
|
+
guildId: record.guildId,
|
|
1637
|
+
parentChannelId: record.parentChannelId,
|
|
1638
|
+
threadId: record.threadId,
|
|
1639
|
+
})
|
|
1640
|
+
: this.#intentKey(record.sessionId!);
|
|
1641
|
+
await this.#store.transact(key, current => {
|
|
1642
|
+
if (current?.pendingActionId !== actionId || current.pendingActionNonce !== actionNonce) return current;
|
|
1643
|
+
return normalizeDiscordConversation({
|
|
1644
|
+
...current,
|
|
1645
|
+
generation: current.generation + 1,
|
|
1646
|
+
updatedAt: this.#now(),
|
|
1647
|
+
pendingActionId: undefined,
|
|
1648
|
+
pendingActionNonce: undefined,
|
|
1649
|
+
pendingActionEffectId: undefined,
|
|
1650
|
+
});
|
|
1651
|
+
});
|
|
1652
|
+
}
|
|
1653
|
+
async #ensureActionPublication(record: DiscordConversation, actionId: string): Promise<DiscordConversation> {
|
|
1654
|
+
const key = discordConversationKey({
|
|
1655
|
+
appId: record.appId,
|
|
1656
|
+
guildId: record.guildId,
|
|
1657
|
+
parentChannelId: record.parentChannelId,
|
|
1658
|
+
threadId: record.threadId!,
|
|
1659
|
+
});
|
|
1660
|
+
await this.#requireLiveBinding(record.sessionId!, record.endpointGeneration!);
|
|
1661
|
+
const result = await this.#store.transact(key, current => {
|
|
1662
|
+
if (
|
|
1663
|
+
current?.state !== "active" ||
|
|
1664
|
+
current.sessionId !== record.sessionId ||
|
|
1665
|
+
current.endpointGeneration !== record.endpointGeneration
|
|
1666
|
+
)
|
|
1667
|
+
return current;
|
|
1668
|
+
if (current.pendingActionId === actionId && current.pendingActionNonce && current.pendingActionEffectId)
|
|
1669
|
+
return current;
|
|
1670
|
+
const actionNonce =
|
|
1671
|
+
current.pendingActionId === actionId && current.pendingActionNonce
|
|
1672
|
+
? current.pendingActionNonce
|
|
1673
|
+
: randomUUID();
|
|
1674
|
+
return normalizeDiscordConversation({
|
|
1675
|
+
...current,
|
|
1676
|
+
generation: current.generation + 1,
|
|
1677
|
+
updatedAt: this.#now(),
|
|
1678
|
+
pendingActionId: actionId,
|
|
1679
|
+
pendingActionNonce: actionNonce,
|
|
1680
|
+
pendingActionEffectId: `action-publication:${current.threadId}:${actionId}:${actionNonce}`,
|
|
1681
|
+
});
|
|
1682
|
+
});
|
|
1683
|
+
if (
|
|
1684
|
+
result?.state !== "active" ||
|
|
1685
|
+
result.sessionId !== record.sessionId ||
|
|
1686
|
+
result.endpointGeneration !== record.endpointGeneration ||
|
|
1687
|
+
result.pendingActionId !== actionId ||
|
|
1688
|
+
!result.pendingActionNonce ||
|
|
1689
|
+
!result.pendingActionEffectId
|
|
1690
|
+
) {
|
|
1691
|
+
throw new Error("Discord action publication intent lost its authority");
|
|
1692
|
+
}
|
|
1693
|
+
return result;
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
async #runEffect<TPayload>(
|
|
1697
|
+
id: string,
|
|
1698
|
+
kind: string,
|
|
1699
|
+
sessionId: string | undefined,
|
|
1700
|
+
endpointGeneration: number,
|
|
1701
|
+
payload: TPayload,
|
|
1702
|
+
operation: (ensure: () => Promise<void>, beforeProvider: () => void) => Promise<ChatEffectReceipt>,
|
|
1703
|
+
revalidate: () => boolean | Promise<boolean>,
|
|
1704
|
+
terminalizeStaleBeforeProvider = false,
|
|
1705
|
+
): Promise<ChatEffectReceipt> {
|
|
1706
|
+
const claimed = await this.#rescheduleAfterEffectTransition(
|
|
1707
|
+
this.#effects.enqueueAndClaim<TPayload>(
|
|
1708
|
+
{ id, kind, transport: "discord", sessionId, endpointGeneration, payload },
|
|
1709
|
+
this.#providerOwner,
|
|
1710
|
+
this.#providerLeaseMs,
|
|
1711
|
+
),
|
|
1712
|
+
);
|
|
1713
|
+
let effect: ChatEffect<TPayload>;
|
|
1714
|
+
if (claimed) {
|
|
1715
|
+
// Fresh effect atomically inserted into a live lease. This closes the
|
|
1716
|
+
// enqueue→claim window in which the same-process lease-recovery timer could
|
|
1717
|
+
// claim the still-"pending" effect first and make this foreground claim fail
|
|
1718
|
+
// with "owned by another worker" (mirrors the inbound enqueueAndClaim path).
|
|
1719
|
+
effect = claimed;
|
|
1720
|
+
} else {
|
|
1721
|
+
const initial = await this.#effects.read<TPayload>(id);
|
|
1722
|
+
if (initial?.state === "terminal") {
|
|
1723
|
+
if (!initial.receipt) throw new Error(`Discord effect ${id} has no receipt`);
|
|
1724
|
+
if (terminalizeStaleBeforeProvider && initial.receipt.status === "stale_noop")
|
|
1725
|
+
throw new DiscordEndpointBindingError("Discord thread effect is no longer current.");
|
|
1726
|
+
return initial.receipt;
|
|
1727
|
+
}
|
|
1728
|
+
const reclaimed = await this.#rescheduleAfterEffectTransition(
|
|
1729
|
+
this.#effects.claim<TPayload>(id, this.#providerOwner, this.#providerLeaseMs),
|
|
1730
|
+
);
|
|
1731
|
+
if (!reclaimed) throw new Error(`Discord effect ${id} is owned by another worker`);
|
|
1732
|
+
effect = reclaimed;
|
|
1733
|
+
}
|
|
1734
|
+
const lease: ChatEffectLease = { owner: this.#providerOwner, epoch: effect.epoch };
|
|
1735
|
+
let renewalLost = false;
|
|
1736
|
+
let revalidationFailed = false;
|
|
1737
|
+
let providerEffectStarted = false;
|
|
1738
|
+
let renewal: Promise<boolean> | undefined;
|
|
1739
|
+
const renew = async (): Promise<boolean> => {
|
|
1740
|
+
if (renewalLost) return false;
|
|
1741
|
+
if (renewal) return await renewal;
|
|
1742
|
+
const currentRenewal = (async (): Promise<boolean> => {
|
|
1743
|
+
const renewed = await this.#rescheduleAfterEffectTransition(
|
|
1744
|
+
this.#effects.renew(id, lease, this.#providerLeaseMs),
|
|
1745
|
+
);
|
|
1746
|
+
if (!renewed) renewalLost = true;
|
|
1747
|
+
else if (!(await revalidate())) {
|
|
1748
|
+
revalidationFailed = true;
|
|
1749
|
+
renewalLost = true;
|
|
1750
|
+
}
|
|
1751
|
+
return !renewalLost;
|
|
1752
|
+
})();
|
|
1753
|
+
renewal = currentRenewal;
|
|
1754
|
+
try {
|
|
1755
|
+
return await currentRenewal;
|
|
1756
|
+
} finally {
|
|
1757
|
+
if (renewal === currentRenewal) renewal = undefined;
|
|
1758
|
+
}
|
|
1759
|
+
};
|
|
1760
|
+
const timer = setInterval(
|
|
1761
|
+
() => {
|
|
1762
|
+
void renew().catch(() => {});
|
|
1763
|
+
},
|
|
1764
|
+
Math.max(1, Math.floor(this.#providerLeaseMs / 3)),
|
|
1765
|
+
);
|
|
1766
|
+
const ensure = async (): Promise<void> => {
|
|
1767
|
+
if (!(await renew())) throw new Error(`Discord effect ${id} lost its fence`);
|
|
1768
|
+
};
|
|
1769
|
+
try {
|
|
1770
|
+
await ensure();
|
|
1771
|
+
const receipt = await operation(ensure, () => {
|
|
1772
|
+
providerEffectStarted = true;
|
|
1773
|
+
});
|
|
1774
|
+
await ensure();
|
|
1775
|
+
const committed = await this.#effects.record(id, lease, "terminal", receipt);
|
|
1776
|
+
if (!committed) throw new Error(`Discord effect ${id} lost its fence before commit`);
|
|
1777
|
+
return receipt;
|
|
1778
|
+
} catch (error) {
|
|
1779
|
+
if (terminalizeStaleBeforeProvider && revalidationFailed && !providerEffectStarted)
|
|
1780
|
+
await this.#rescheduleAfterEffectTransition(
|
|
1781
|
+
this.#effects.record(id, lease, "terminal", { status: "stale_noop" }),
|
|
1782
|
+
);
|
|
1783
|
+
else if (!renewalLost)
|
|
1784
|
+
await this.#rescheduleAfterEffectTransition(
|
|
1785
|
+
this.#effects.record(id, lease, "uncertain", { status: "uncertain" }),
|
|
1786
|
+
);
|
|
1787
|
+
throw error;
|
|
1788
|
+
} finally {
|
|
1789
|
+
clearInterval(timer);
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
async #postEffect(
|
|
1794
|
+
id: string,
|
|
1795
|
+
record: DiscordConversation,
|
|
1796
|
+
content: string,
|
|
1797
|
+
components?: DiscordMessageComponent[],
|
|
1798
|
+
actionPublication = false,
|
|
1799
|
+
closing = false,
|
|
1800
|
+
allowInactive = false,
|
|
1801
|
+
): Promise<void> {
|
|
1802
|
+
const nonce = discordEffectNonce(id);
|
|
1803
|
+
const existingEffect = await this.#effects.read<{
|
|
1804
|
+
threadId: string;
|
|
1805
|
+
content: string;
|
|
1806
|
+
nonce: string;
|
|
1807
|
+
components?: DiscordMessageComponent[];
|
|
1808
|
+
}>(id);
|
|
1809
|
+
const payload = existingEffect?.payload ?? {
|
|
1810
|
+
threadId: record.threadId!,
|
|
1811
|
+
content,
|
|
1812
|
+
nonce,
|
|
1813
|
+
...(components ? { components } : {}),
|
|
1814
|
+
};
|
|
1815
|
+
await this.#runEffect(
|
|
1816
|
+
id,
|
|
1817
|
+
"post-message",
|
|
1818
|
+
record.sessionId,
|
|
1819
|
+
record.endpointGeneration!,
|
|
1820
|
+
payload,
|
|
1821
|
+
async ensure => {
|
|
1822
|
+
await ensure();
|
|
1823
|
+
const reconciled = await this.options.provider.findMessageByNonce({
|
|
1824
|
+
threadId: payload.threadId,
|
|
1825
|
+
nonce: payload.nonce,
|
|
1826
|
+
});
|
|
1827
|
+
if (reconciled)
|
|
1828
|
+
return {
|
|
1829
|
+
provider: "discord",
|
|
1830
|
+
messageId: reconciled.id,
|
|
1831
|
+
threadId: record.threadId,
|
|
1832
|
+
status: "reconciled",
|
|
1833
|
+
};
|
|
1834
|
+
await ensure();
|
|
1835
|
+
const posted = await this.options.provider.postMessage({
|
|
1836
|
+
threadId: payload.threadId,
|
|
1837
|
+
content: payload.content,
|
|
1838
|
+
nonce: payload.nonce,
|
|
1839
|
+
...(payload.components ? { components: payload.components } : {}),
|
|
1840
|
+
});
|
|
1841
|
+
return { provider: "discord", messageId: posted.id, threadId: record.threadId, status: "posted" };
|
|
1842
|
+
},
|
|
1843
|
+
async () => {
|
|
1844
|
+
const current = await this.#byThread(record.guildId, record.parentChannelId, record.threadId!);
|
|
1845
|
+
const intent = closingIntent(record);
|
|
1846
|
+
const mappingCurrent = closing
|
|
1847
|
+
? !!current &&
|
|
1848
|
+
intent?.nonce === closingIntent(current)?.nonce &&
|
|
1849
|
+
current.sessionId === record.sessionId &&
|
|
1850
|
+
current.endpointGeneration === record.endpointGeneration
|
|
1851
|
+
: !!current &&
|
|
1852
|
+
(allowInactive || current.state === "active") &&
|
|
1853
|
+
current.generation === record.generation &&
|
|
1854
|
+
current.endpointGeneration === record.endpointGeneration &&
|
|
1855
|
+
(!actionPublication || current.pendingActionEffectId === id);
|
|
1856
|
+
return (
|
|
1857
|
+
mappingCurrent &&
|
|
1858
|
+
(closing || allowInactive || (await this.#bindingCurrent(record.sessionId!, record.endpointGeneration!)))
|
|
1859
|
+
);
|
|
1860
|
+
},
|
|
1861
|
+
);
|
|
1862
|
+
}
|
|
1863
|
+
async #threadEffect(
|
|
1864
|
+
id: string,
|
|
1865
|
+
record: DiscordConversation,
|
|
1866
|
+
operation: "archive" | "unarchive",
|
|
1867
|
+
locked = false,
|
|
1868
|
+
closing = false,
|
|
1869
|
+
occurrenceId?: string,
|
|
1870
|
+
): Promise<void> {
|
|
1871
|
+
await this.#runEffect(
|
|
1872
|
+
id,
|
|
1873
|
+
operation,
|
|
1874
|
+
record.sessionId,
|
|
1875
|
+
record.endpointGeneration!,
|
|
1876
|
+
{ threadId: record.threadId!, locked, ...(occurrenceId === undefined ? {} : { occurrenceId }) },
|
|
1877
|
+
|
|
1878
|
+
async (ensure, beforeProvider) => {
|
|
1879
|
+
await ensure();
|
|
1880
|
+
beforeProvider();
|
|
1881
|
+
if (operation === "archive")
|
|
1882
|
+
await this.options.provider.archiveThread({
|
|
1883
|
+
threadId: record.threadId!,
|
|
1884
|
+
...(locked ? { locked: true } : {}),
|
|
1885
|
+
});
|
|
1886
|
+
else await this.options.provider.unarchiveThread({ threadId: record.threadId! });
|
|
1887
|
+
return { provider: "discord", threadId: record.threadId, status: operation };
|
|
1888
|
+
},
|
|
1889
|
+
async () => {
|
|
1890
|
+
const current = await this.#byThread(record.guildId, record.parentChannelId, record.threadId!);
|
|
1891
|
+
const intent = closingIntent(record);
|
|
1892
|
+
const mappingCurrent = closing
|
|
1893
|
+
? !!current &&
|
|
1894
|
+
intent?.nonce === closingIntent(current)?.nonce &&
|
|
1895
|
+
current.sessionId === record.sessionId &&
|
|
1896
|
+
current.endpointGeneration === record.endpointGeneration
|
|
1897
|
+
: !!current &&
|
|
1898
|
+
current.state === (operation === "archive" ? "active" : "resuming") &&
|
|
1899
|
+
current.generation === record.generation &&
|
|
1900
|
+
current.endpointGeneration === record.endpointGeneration &&
|
|
1901
|
+
(occurrenceId === undefined ||
|
|
1902
|
+
(operation === "archive"
|
|
1903
|
+
? current.archiveEffectId === id && current.archiveOccurrenceId === occurrenceId
|
|
1904
|
+
: current.resumeEffectId === id && current.resumeOccurrenceId === occurrenceId));
|
|
1905
|
+
return (
|
|
1906
|
+
mappingCurrent &&
|
|
1907
|
+
(closing ||
|
|
1908
|
+
(!!record.sessionId && (await this.#bindingCurrent(record.sessionId, record.endpointGeneration!))))
|
|
1909
|
+
);
|
|
1910
|
+
},
|
|
1911
|
+
!closing,
|
|
1912
|
+
);
|
|
1913
|
+
}
|
|
1914
|
+
async #createThreadEffect(intent: DiscordConversation, name: string): Promise<DiscordThread> {
|
|
1915
|
+
const effectId = `create:${intent.sessionId}:${intent.createNonce}`;
|
|
1916
|
+
const nonce = discordEffectNonce(effectId);
|
|
1917
|
+
const receipt = await this.#runEffect(
|
|
1918
|
+
effectId,
|
|
1919
|
+
"create-thread",
|
|
1920
|
+
intent.sessionId,
|
|
1921
|
+
intent.endpointGeneration!,
|
|
1922
|
+
{ guildId: intent.guildId, parentId: intent.parentChannelId, name, nonce },
|
|
1923
|
+
async ensure => {
|
|
1924
|
+
await ensure();
|
|
1925
|
+
const existing = await this.options.provider.findThreadByNonce({
|
|
1926
|
+
guildId: intent.guildId,
|
|
1927
|
+
parentId: intent.parentChannelId,
|
|
1928
|
+
nonce,
|
|
1929
|
+
});
|
|
1930
|
+
await ensure();
|
|
1931
|
+
const thread =
|
|
1932
|
+
existing ??
|
|
1933
|
+
(await this.options.provider.createThread({
|
|
1934
|
+
guildId: intent.guildId,
|
|
1935
|
+
parentId: intent.parentChannelId,
|
|
1936
|
+
name,
|
|
1937
|
+
nonce,
|
|
1938
|
+
}));
|
|
1939
|
+
return {
|
|
1940
|
+
provider: "discord",
|
|
1941
|
+
threadId: thread.id,
|
|
1942
|
+
channelId: thread.parentId,
|
|
1943
|
+
status: existing ? "reconciled" : "created",
|
|
1944
|
+
};
|
|
1945
|
+
},
|
|
1946
|
+
async () => {
|
|
1947
|
+
const current = await this.#store.read(this.#intentKey(intent.sessionId!));
|
|
1948
|
+
return (
|
|
1949
|
+
current?.state === "creating" &&
|
|
1950
|
+
current.createOwner === intent.createOwner &&
|
|
1951
|
+
current.generation === intent.generation &&
|
|
1952
|
+
(current.createLeaseExpiresAt ?? 0) > this.#now() &&
|
|
1953
|
+
(await this.#bindingCurrent(intent.sessionId!, intent.endpointGeneration!))
|
|
1954
|
+
);
|
|
1955
|
+
},
|
|
1956
|
+
);
|
|
1957
|
+
if (!receipt.threadId) throw new Error("Discord create effect has no thread receipt");
|
|
1958
|
+
return { id: receipt.threadId, guildId: intent.guildId, parentId: intent.parentChannelId, archived: false };
|
|
1959
|
+
}
|
|
1960
|
+
async #recoverCreateThread(
|
|
1961
|
+
effect: ChatEffect,
|
|
1962
|
+
payload: { guildId?: string; parentId?: string; name?: string; nonce?: string },
|
|
1963
|
+
): Promise<void> {
|
|
1964
|
+
if (!effect.sessionId || !payload.nonce) return;
|
|
1965
|
+
const intentKey = this.#intentKey(effect.sessionId);
|
|
1966
|
+
const intent = await this.#store.read(intentKey);
|
|
1967
|
+
const matchesIntent =
|
|
1968
|
+
intent?.state === "creating" &&
|
|
1969
|
+
intent.sessionId === effect.sessionId &&
|
|
1970
|
+
intent.guildId === payload.guildId &&
|
|
1971
|
+
intent.parentChannelId === payload.parentId &&
|
|
1972
|
+
discordEffectNonce(`create:${intent.sessionId}:${intent.createNonce}`) === payload.nonce;
|
|
1973
|
+
if (!matchesIntent || !intent) {
|
|
1974
|
+
if (effect.state !== "terminal") await this.#terminalizeEffect(effect.id, "rejected");
|
|
1975
|
+
|
|
1976
|
+
return;
|
|
1977
|
+
}
|
|
1978
|
+
if (!(await this.#bindingCurrent(effect.sessionId, effect.endpointGeneration))) {
|
|
1979
|
+
await this.#store.delete(intentKey, intent.generation);
|
|
1980
|
+
return;
|
|
1981
|
+
}
|
|
1982
|
+
if (effect.state !== "terminal") {
|
|
1983
|
+
await this.#create(effect.sessionId, effect.endpointGeneration, intent.createNonce!, payload.name);
|
|
1984
|
+
return;
|
|
1985
|
+
}
|
|
1986
|
+
const threadId = effect.receipt?.threadId;
|
|
1987
|
+
if (!threadId) return;
|
|
1988
|
+
// Any durable mapping for this session is already session-level authority.
|
|
1989
|
+
// Do not let a terminal receipt reactivate an older remote thread merely
|
|
1990
|
+
// because its exact thread key is absent. Delete only this generation so a
|
|
1991
|
+
// later notification must mint a fresh nonce and provider effect.
|
|
1992
|
+
if ((await this.#sessionMappings(effect.sessionId)).length > 0) {
|
|
1993
|
+
await this.#store.delete(intentKey, intent.generation);
|
|
1994
|
+
return;
|
|
1995
|
+
}
|
|
1996
|
+
await this.#requireLiveBinding(effect.sessionId, effect.endpointGeneration);
|
|
1997
|
+
const key = discordConversationKey({
|
|
1998
|
+
appId: intent.appId,
|
|
1999
|
+
guildId: intent.guildId,
|
|
2000
|
+
parentChannelId: intent.parentChannelId,
|
|
2001
|
+
threadId,
|
|
2002
|
+
});
|
|
2003
|
+
const committed = await this.#store.transact(
|
|
2004
|
+
key,
|
|
2005
|
+
old =>
|
|
2006
|
+
old ??
|
|
2007
|
+
normalizeDiscordConversation({
|
|
2008
|
+
generation: 1,
|
|
2009
|
+
state: "active",
|
|
2010
|
+
appId: intent.appId,
|
|
2011
|
+
guildId: intent.guildId,
|
|
2012
|
+
parentChannelId: intent.parentChannelId,
|
|
2013
|
+
threadId,
|
|
2014
|
+
sessionId: intent.sessionId,
|
|
2015
|
+
endpointGeneration: intent.endpointGeneration,
|
|
2016
|
+
createNonce: intent.createNonce,
|
|
2017
|
+
effectIncarnationId: intent.createNonce,
|
|
2018
|
+
updatedAt: this.#now(),
|
|
2019
|
+
seenEventIds: [],
|
|
2020
|
+
seenInteractionIds: [],
|
|
2021
|
+
}),
|
|
2022
|
+
);
|
|
2023
|
+
if (committed) await this.#store.delete(intentKey, intent.generation);
|
|
2024
|
+
}
|
|
2025
|
+
async #drainProviderEffects(): Promise<boolean> {
|
|
2026
|
+
let failed = false;
|
|
2027
|
+
for (const effect of await this.#effects.list()) {
|
|
2028
|
+
if (
|
|
2029
|
+
effect.transport !== "discord" ||
|
|
2030
|
+
(effect.state === "terminal" &&
|
|
2031
|
+
effect.kind !== "create-thread" &&
|
|
2032
|
+
effect.kind !== "archive" &&
|
|
2033
|
+
effect.kind !== "unarchive")
|
|
2034
|
+
)
|
|
2035
|
+
continue;
|
|
2036
|
+
if (this.#hasLiveEffectLease(effect)) continue;
|
|
2037
|
+
|
|
2038
|
+
const payload = effect.payload as {
|
|
2039
|
+
guildId?: string;
|
|
2040
|
+
parentId?: string;
|
|
2041
|
+
name?: string;
|
|
2042
|
+
nonce?: string;
|
|
2043
|
+
threadId?: string;
|
|
2044
|
+
content?: string;
|
|
2045
|
+
components?: DiscordMessageComponent[];
|
|
2046
|
+
locked?: boolean;
|
|
2047
|
+
occurrenceId?: string;
|
|
2048
|
+
};
|
|
2049
|
+
const providerEffect =
|
|
2050
|
+
effect.kind === "post-message" || effect.kind === "archive" || effect.kind === "unarchive";
|
|
2051
|
+
if (
|
|
2052
|
+
providerEffect &&
|
|
2053
|
+
(!effect.sessionId ||
|
|
2054
|
+
!payload.threadId ||
|
|
2055
|
+
(effect.kind === "post-message" && payload.content === undefined))
|
|
2056
|
+
) {
|
|
2057
|
+
await this.#terminalizeEffect(effect.id, "stale_noop");
|
|
2058
|
+
|
|
2059
|
+
continue;
|
|
2060
|
+
}
|
|
2061
|
+
try {
|
|
2062
|
+
if (effect.kind === "create-thread" && effect.sessionId && payload.nonce) {
|
|
2063
|
+
await this.#recoverCreateThread(effect, payload);
|
|
2064
|
+
}
|
|
2065
|
+
if (effect.kind === "post-message" && payload.threadId && payload.content !== undefined) {
|
|
2066
|
+
const record = await this.#byThread(
|
|
2067
|
+
this.options.guildId,
|
|
2068
|
+
this.options.parentChannelId,
|
|
2069
|
+
payload.threadId,
|
|
2070
|
+
);
|
|
2071
|
+
const closing = closingIntent(record);
|
|
2072
|
+
const closeMarker = !!closing && effect.id === this.#closeMarkerEffectId(record!);
|
|
2073
|
+
const inactiveFailure = effect.id.startsWith("failure:");
|
|
2074
|
+
if (
|
|
2075
|
+
!record ||
|
|
2076
|
+
(record.state !== "active" && !closeMarker && !inactiveFailure) ||
|
|
2077
|
+
record.endpointGeneration !== effect.endpointGeneration ||
|
|
2078
|
+
(effect.id.startsWith("action-publication:") && record.pendingActionEffectId !== effect.id)
|
|
2079
|
+
) {
|
|
2080
|
+
await this.#terminalizeEffect(effect.id, "stale_noop");
|
|
2081
|
+
} else {
|
|
2082
|
+
await this.#postEffect(
|
|
2083
|
+
effect.id,
|
|
2084
|
+
record,
|
|
2085
|
+
payload.content,
|
|
2086
|
+
payload.components,
|
|
2087
|
+
effect.id.startsWith("action-publication:"),
|
|
2088
|
+
closeMarker,
|
|
2089
|
+
inactiveFailure,
|
|
2090
|
+
);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
if ((effect.kind === "archive" || effect.kind === "unarchive") && payload.threadId) {
|
|
2094
|
+
const record = await this.#byThread(
|
|
2095
|
+
this.options.guildId,
|
|
2096
|
+
this.options.parentChannelId,
|
|
2097
|
+
payload.threadId,
|
|
2098
|
+
);
|
|
2099
|
+
const closing = closingIntent(record);
|
|
2100
|
+
const closeArchive =
|
|
2101
|
+
!!closing &&
|
|
2102
|
+
effect.kind === "archive" &&
|
|
2103
|
+
payload.locked === true &&
|
|
2104
|
+
effect.id === this.#closeArchiveEffectId(record!);
|
|
2105
|
+
const occurrenceId = payload.occurrenceId;
|
|
2106
|
+
const occurrenceMatches =
|
|
2107
|
+
closeArchive ||
|
|
2108
|
+
(!!record &&
|
|
2109
|
+
typeof occurrenceId === "string" &&
|
|
2110
|
+
effect.id === (effect.kind === "archive" ? record.archiveEffectId : record.resumeEffectId) &&
|
|
2111
|
+
(effect.kind === "archive"
|
|
2112
|
+
? record.archiveOccurrenceId === occurrenceId
|
|
2113
|
+
: record.resumeOccurrenceId === occurrenceId));
|
|
2114
|
+
if (
|
|
2115
|
+
!record ||
|
|
2116
|
+
(!closeArchive &&
|
|
2117
|
+
(effect.kind === "archive" ? record.state !== "active" : record.state !== "resuming")) ||
|
|
2118
|
+
record.endpointGeneration !== effect.endpointGeneration ||
|
|
2119
|
+
!occurrenceMatches
|
|
2120
|
+
) {
|
|
2121
|
+
await this.#terminalizeEffect(effect.id, "stale_noop");
|
|
2122
|
+
} else {
|
|
2123
|
+
await this.#threadEffect(effect.id, record, effect.kind, payload.locked, closeArchive, occurrenceId);
|
|
2124
|
+
if (!closeArchive && occurrenceId) {
|
|
2125
|
+
if (effect.kind === "archive") await this.#completeArchive(record, occurrenceId);
|
|
2126
|
+
else await this.#completeResume(record, occurrenceId);
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
} catch {
|
|
2131
|
+
failed = true; /* retained for a later journal-authoritative replay */
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
return failed;
|
|
2135
|
+
}
|
|
2136
|
+
async #rescheduleAfterEffectTransition<T extends ChatEffect | undefined>(transition: Promise<T>): Promise<T> {
|
|
2137
|
+
const effect = await transition;
|
|
2138
|
+
if (effect?.state !== "terminal") await this.#scheduleLeaseRecovery();
|
|
2139
|
+
return effect;
|
|
2140
|
+
}
|
|
2141
|
+
async #scheduleLeaseRecovery(recoveryFailed = false): Promise<void> {
|
|
2142
|
+
if (!this.#started) return;
|
|
2143
|
+
const lifecycleGeneration = this.#lifecycleGeneration;
|
|
2144
|
+
const effects = await this.#effects.list();
|
|
2145
|
+
if (!this.#started || lifecycleGeneration !== this.#lifecycleGeneration) return;
|
|
2146
|
+
const now = this.#now();
|
|
2147
|
+
const recoveryAt = effects
|
|
2148
|
+
.filter(effect => effect.transport === "discord")
|
|
2149
|
+
.reduce<number | undefined>(
|
|
2150
|
+
(earliest, effect) => {
|
|
2151
|
+
const claimAt =
|
|
2152
|
+
effect.state === "leased" && Number.isFinite(effect.leaseExpiresAt)
|
|
2153
|
+
? effect.leaseExpiresAt
|
|
2154
|
+
: effect.state === "pending" ||
|
|
2155
|
+
effect.state === "accepted" ||
|
|
2156
|
+
(effect.state === "uncertain" && !effect.kind.includes(".inbound."))
|
|
2157
|
+
? now
|
|
2158
|
+
: undefined;
|
|
2159
|
+
return claimAt === undefined || (earliest !== undefined && earliest <= claimAt) ? earliest : claimAt;
|
|
2160
|
+
},
|
|
2161
|
+
recoveryFailed ? now : undefined,
|
|
2162
|
+
);
|
|
2163
|
+
if (recoveryAt === undefined) {
|
|
2164
|
+
if (this.#leaseRecoveryTimer !== undefined)
|
|
2165
|
+
this.#leaseRecoveryScheduler.clearTimeout(this.#leaseRecoveryTimer);
|
|
2166
|
+
this.#leaseRecoveryTimer = undefined;
|
|
2167
|
+
this.#leaseRecoveryAt = undefined;
|
|
2168
|
+
this.#leaseRecoveryFailures = 0;
|
|
2169
|
+
return;
|
|
2170
|
+
}
|
|
2171
|
+
if (this.#leaseRecoveryAt !== undefined && this.#leaseRecoveryAt <= recoveryAt) return;
|
|
2172
|
+
if (this.#leaseRecoveryTimer !== undefined) this.#leaseRecoveryScheduler.clearTimeout(this.#leaseRecoveryTimer);
|
|
2173
|
+
this.#leaseRecoveryAt = recoveryAt;
|
|
2174
|
+
const delay =
|
|
2175
|
+
recoveryAt <= now
|
|
2176
|
+
? Math.min(1_000, 25 * 2 ** Math.min(this.#leaseRecoveryFailures, 5))
|
|
2177
|
+
: Math.min(recoveryAt - now, 2_147_483_647);
|
|
2178
|
+
this.#leaseRecoveryTimer = this.#leaseRecoveryScheduler.setTimeout(() => {
|
|
2179
|
+
if (lifecycleGeneration !== this.#lifecycleGeneration) return;
|
|
2180
|
+
this.#leaseRecoveryTimer = undefined;
|
|
2181
|
+
this.#leaseRecoveryAt = undefined;
|
|
2182
|
+
return this.#track(this.#recoverLeasedEffects()).catch(() => {});
|
|
2183
|
+
}, delay);
|
|
2184
|
+
}
|
|
2185
|
+
async #recoverLeasedEffects(): Promise<void> {
|
|
2186
|
+
if (!this.#started) return;
|
|
2187
|
+
let failed = false;
|
|
2188
|
+
try {
|
|
2189
|
+
try {
|
|
2190
|
+
await this.#reconcileTerminalInboundReceipts();
|
|
2191
|
+
failed ||= await this.#recoverClosingConversations();
|
|
2192
|
+
failed ||= await this.#drainProviderEffects();
|
|
2193
|
+
await this.#reconcileTerminalInboundReceipts();
|
|
2194
|
+
failed ||= await this.#recoverClosingConversations();
|
|
2195
|
+
} catch {
|
|
2196
|
+
failed = true;
|
|
2197
|
+
}
|
|
2198
|
+
try {
|
|
2199
|
+
failed = (await this.#drainPendingDispatches()) || failed;
|
|
2200
|
+
} catch {
|
|
2201
|
+
failed = true;
|
|
2202
|
+
}
|
|
2203
|
+
} finally {
|
|
2204
|
+
if (failed) this.#leaseRecoveryFailures = Math.min(this.#leaseRecoveryFailures + 1, 5);
|
|
2205
|
+
else this.#leaseRecoveryFailures = 0;
|
|
2206
|
+
try {
|
|
2207
|
+
await this.#scheduleLeaseRecovery(failed);
|
|
2208
|
+
} catch {
|
|
2209
|
+
/* retained effects are retried by the next trigger */
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
|
|
2214
|
+
async #fail(threadId: string): Promise<void> {
|
|
2215
|
+
try {
|
|
2216
|
+
const record = await this.#byThread(this.options.guildId, this.options.parentChannelId, threadId);
|
|
2217
|
+
if (record)
|
|
2218
|
+
await this.#postEffect(
|
|
2219
|
+
`failure:${threadId}:${randomUUID()}`,
|
|
2220
|
+
record,
|
|
2221
|
+
FAILURE,
|
|
2222
|
+
undefined,
|
|
2223
|
+
false,
|
|
2224
|
+
false,
|
|
2225
|
+
true,
|
|
2226
|
+
);
|
|
2227
|
+
} catch {}
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
2230
|
+
|
|
2231
|
+
function decodeCustomId(value: string): { generation: number; actionId: string; actionNonce: string } | undefined {
|
|
2232
|
+
const match = /^skc:(\d+):([^:]+):([0-9a-f-]{36})$/.exec(value);
|
|
2233
|
+
if (!match) return undefined;
|
|
2234
|
+
const generation = Number(match[1]);
|
|
2235
|
+
return Number.isSafeInteger(generation) && generation >= 0
|
|
2236
|
+
? { generation, actionId: match[2]!, actionNonce: match[3]! }
|
|
2237
|
+
: undefined;
|
|
2238
|
+
}
|
|
2239
|
+
|
|
2240
|
+
function stateRank(state: string): number {
|
|
2241
|
+
return state === "closing"
|
|
2242
|
+
? -1
|
|
2243
|
+
: state === "active"
|
|
2244
|
+
? 0
|
|
2245
|
+
: state === "resuming"
|
|
2246
|
+
? 1
|
|
2247
|
+
: state === "archived"
|
|
2248
|
+
? 2
|
|
2249
|
+
: state === "closed"
|
|
2250
|
+
? 3
|
|
2251
|
+
: 4;
|
|
2252
|
+
}
|
|
2253
|
+
|
|
2254
|
+
function actionComponents(
|
|
2255
|
+
generation: number,
|
|
2256
|
+
actionId: string,
|
|
2257
|
+
actionNonce: string,
|
|
2258
|
+
options: string[],
|
|
2259
|
+
): DiscordMessageComponent[] {
|
|
2260
|
+
return [
|
|
2261
|
+
{
|
|
2262
|
+
type: 1,
|
|
2263
|
+
components: [
|
|
2264
|
+
{
|
|
2265
|
+
type: 3,
|
|
2266
|
+
customId: `skc:${generation}:${actionId}:${actionNonce}`,
|
|
2267
|
+
placeholder: "Choose an option",
|
|
2268
|
+
minValues: 1,
|
|
2269
|
+
maxValues: 1,
|
|
2270
|
+
options: options.slice(0, 25).map((option, index) => ({
|
|
2271
|
+
label: option.slice(0, 100) || `Option ${index + 1}`,
|
|
2272
|
+
value: String(index),
|
|
2273
|
+
})),
|
|
2274
|
+
},
|
|
2275
|
+
],
|
|
2276
|
+
},
|
|
2277
|
+
];
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
function componentAnswer(value: string | number): string | number {
|
|
2281
|
+
if (typeof value === "string" && /^\d+$/.test(value)) {
|
|
2282
|
+
const index = Number(value);
|
|
2283
|
+
if (Number.isSafeInteger(index)) return index;
|
|
2284
|
+
}
|
|
2285
|
+
return value;
|
|
2286
|
+
}
|
|
2287
|
+
|
|
2288
|
+
function discordEffectNonce(effectId: string): string {
|
|
2289
|
+
// Discord nonces are bounded; hash the durable effect identifier rather than
|
|
2290
|
+
// truncating its potentially shared prefix.
|
|
2291
|
+
return `skc-${createHash("sha256").update(effectId).digest("hex").slice(0, 21)}`;
|
|
2292
|
+
}
|