mixdog 1.4.3 → 1.4.5
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/package.json +1 -2
- package/src/cli.mjs +5 -1
- package/src/defaults/shipped-history.json +30 -2
- package/src/defaults/skills/browser-use/SKILL.md +148 -273
- package/src/defaults/skills/browser-use/references/devtools.md +14 -0
- package/src/defaults/skills/browser-use/references/page-flows.md +66 -0
- package/src/defaults/skills/browser-use/references/sign-in.md +70 -0
- package/src/defaults/skills/code-tidy/SKILL.md +60 -84
- package/src/defaults/skills/code-tidy/references/agent-cleanup.md +27 -2
- package/src/defaults/skills/computer-use/SKILL.md +145 -229
- package/src/defaults/skills/computer-use/references/flows.md +35 -0
- package/src/defaults/skills/computer-use/references/operations.md +22 -0
- package/src/defaults/skills/computer-use/references/platforms.md +12 -0
- package/src/defaults/skills/docx/SKILL.md +23 -31
- package/src/defaults/skills/docx/references/native-authoring.md +26 -3
- package/src/defaults/skills/docx/references/redlining.md +15 -0
- package/src/defaults/skills/image/SKILL.md +37 -77
- package/src/defaults/skills/image/references/prompt-forms.md +51 -0
- package/src/defaults/skills/image/references/text.md +6 -0
- package/src/defaults/skills/local-provider/SKILL.md +75 -105
- package/src/defaults/skills/local-provider/references/lifecycle.md +14 -0
- package/src/defaults/skills/local-provider/references/maintenance.md +19 -0
- package/src/defaults/skills/local-provider/references/recovery.md +30 -0
- package/src/defaults/skills/memory-management/SKILL.md +22 -21
- package/src/defaults/skills/pdf/SKILL.md +21 -23
- package/src/defaults/skills/pdf/references/blocks.md +20 -0
- package/src/defaults/skills/pdf/references/forms.md +12 -0
- package/src/defaults/skills/pdf/references/html.md +2 -2
- package/src/defaults/skills/pdf/references/ocr.md +7 -0
- package/src/defaults/skills/pdf/references/security.md +8 -0
- package/src/defaults/skills/pptx/SKILL.md +30 -109
- package/src/defaults/skills/pptx/references/audit.md +45 -0
- package/src/defaults/skills/pptx/references/brief-critique.md +47 -0
- package/src/defaults/skills/pptx/references/editing.md +24 -0
- package/src/defaults/skills/pptx/references/html.md +1 -1
- package/src/defaults/skills/pptx/references/script.md +33 -0
- package/src/defaults/skills/setup/SKILL.md +34 -102
- package/src/defaults/skills/setup/references/actions.md +73 -14
- package/src/defaults/skills/setup/references/surfaces.md +23 -8
- package/src/defaults/skills/setup-guide/SKILL.md +28 -115
- package/src/defaults/skills/setup-guide/references/features.md +36 -0
- package/src/defaults/skills/setup-guide/references/glossary.md +38 -0
- package/src/defaults/skills/setup-guide/references/workflow-models.md +62 -0
- package/src/defaults/skills/test-suite/SKILL.md +15 -127
- package/src/defaults/skills/test-suite/references/case-management.md +17 -9
- package/src/defaults/skills/test-suite/references/diagnose.md +23 -0
- package/src/defaults/skills/test-suite/references/prune.md +27 -0
- package/src/defaults/skills/test-suite/references/write.md +44 -0
- package/src/defaults/skills/video/SKILL.md +28 -42
- package/src/defaults/skills/video/references/prompting.md +31 -0
- package/src/defaults/skills/visualize/SKILL.md +73 -107
- package/src/defaults/skills/visualize/references/craft.md +1 -2
- package/src/defaults/skills/visualize/references/feedback.md +36 -0
- package/src/defaults/skills/visualize/references/showing.md +13 -0
- package/src/defaults/skills/xlsx/SKILL.md +23 -50
- package/src/defaults/skills/xlsx/references/editing.md +15 -0
- package/src/defaults/skills/xlsx/references/model-conventions.md +1 -1
- package/src/defaults/skills/xlsx/references/recalculation.md +37 -0
- package/src/defaults/skills/xlsx/references/report-design.md +18 -2
- package/src/lib/keychain-cjs.cjs +22 -1
- package/src/lib/rules-builder.cjs +1 -1
- package/src/rules/agent/AGENT.md +3 -0
- package/src/rules/agent-mode/MIXY.md +5 -3
- package/src/rules/lead/LEAD.md +3 -1
- package/src/runtime/agent/orchestrator/agent-runtime/cache-strategy.mjs +8 -30
- package/src/runtime/agent/orchestrator/agent-runtime/title-completion.mjs +5 -0
- package/src/runtime/agent/orchestrator/mcp/oauth-request.mjs +21 -2
- package/src/runtime/agent/orchestrator/providers/account-pool.mjs +3 -0
- package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +4 -3
- package/src/runtime/agent/orchestrator/providers/anthropic-api-request.mjs +11 -1
- package/src/runtime/agent/orchestrator/providers/anthropic-api-transport.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/anthropic-message-threads.mjs +583 -0
- package/src/runtime/agent/orchestrator/providers/anthropic-oauth-client-version.mjs +1 -1
- package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +81 -19
- package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-events/content-block-start.mjs +4 -0
- package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-outcome.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +180 -13
- package/src/runtime/agent/orchestrator/providers/anthropic-turn-result.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/anthropic.mjs +58 -6
- package/src/runtime/agent/orchestrator/providers/cursor-wire.mjs +4 -2
- package/src/runtime/agent/orchestrator/providers/custom-provider-config.mjs +121 -9
- package/src/runtime/agent/orchestrator/providers/custom-provider.mjs +4 -1
- package/src/runtime/agent/orchestrator/providers/gemini-error-contract.mjs +6 -6
- package/src/runtime/agent/orchestrator/providers/gemini-model-redirect.mjs +44 -0
- package/src/runtime/agent/orchestrator/providers/gemini-reasoning-relay.mjs +5 -0
- package/src/runtime/agent/orchestrator/providers/gemini-request-body.mjs +34 -5
- package/src/runtime/agent/orchestrator/providers/gemini-response.mjs +10 -0
- package/src/runtime/agent/orchestrator/providers/gemini-schema.mjs +11 -6
- package/src/runtime/agent/orchestrator/providers/gemini-sdk-request.mjs +4 -100
- package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +3 -227
- package/src/runtime/agent/orchestrator/providers/gemini.mjs +226 -118
- package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +11 -1
- package/src/runtime/agent/orchestrator/providers/lib/claude-bridge-schema.mjs +15 -3
- package/src/runtime/agent/orchestrator/providers/lib/gemini-model-catalog.mjs +2 -1
- package/src/runtime/agent/orchestrator/providers/lib/generation-window.mjs +9 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-chat-send.mjs +9 -1
- package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-result.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-state.mjs +5 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-options.mjs +3 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-response-normalization.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-responses.mjs +10 -1
- package/src/runtime/agent/orchestrator/providers/openai-direct-request.mjs +5 -28
- package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-events.mjs +3 -0
- package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-outcome.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +50 -28
- package/src/runtime/agent/orchestrator/providers/openai-ws-response-state.mjs +9 -2
- package/src/runtime/agent/orchestrator/providers/openai-ws-stream/socket-handlers.mjs +14 -10
- package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +1 -3
- package/src/runtime/agent/orchestrator/providers/openrouter-provider-routing.mjs +28 -0
- package/src/runtime/agent/orchestrator/providers/openrouter.mjs +4 -1
- package/src/runtime/agent/orchestrator/providers/registry.mjs +30 -25
- package/src/runtime/agent/orchestrator/providers/stream-json-pool/worker-slots/slot-spawn.mjs +8 -3
- package/src/runtime/agent/orchestrator/providers/vertex-ai-maas.mjs +2 -2
- package/src/runtime/agent/orchestrator/runtime-core/ask-user-result.mjs +66 -0
- package/src/runtime/agent/orchestrator/runtime-core/config-helpers.mjs +4 -5
- package/src/runtime/agent/orchestrator/runtime-core/goal-tool-defs.mjs +2 -2
- package/src/runtime/agent/orchestrator/runtime-core/provider-request-tools.mjs +15 -8
- package/src/runtime/agent/orchestrator/runtime-core/session-title.mjs +1 -0
- package/src/runtime/agent/orchestrator/runtime-core/todo-list.mjs +9 -32
- package/src/runtime/agent/orchestrator/runtime-core/todo-tool-defs.mjs +4 -5
- package/src/runtime/agent/orchestrator/session/compact/summary-route.mjs +1 -3
- package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +5 -2
- package/src/runtime/agent/orchestrator/session/loop/no-tool-turn/todo-check.mjs +2 -17
- package/src/runtime/agent/orchestrator/session/loop/usage.mjs +7 -3
- package/src/runtime/agent/orchestrator/session/manager/ask-turn-settle.mjs +9 -2
- package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +8 -1
- package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +26 -13
- package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +4 -5
- package/src/runtime/agent/orchestrator/session/manager/usage-metrics.mjs +16 -5
- package/src/runtime/agent/orchestrator/session/store/lifecycle-barriers.mjs +22 -1
- package/src/runtime/agent/orchestrator/session/store/live-state.mjs +4 -0
- package/src/runtime/agent/orchestrator/session/store.mjs +12 -6
- package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +11 -9
- package/src/runtime/agent/orchestrator/tools/builtin/grep-formatting.mjs +2 -8
- package/src/runtime/agent/orchestrator/tools/builtin/lib/search-input-helpers.mjs +1 -9
- package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-output-silence.mjs +64 -0
- package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-task-output.mjs +20 -6
- package/src/runtime/agent/orchestrator/tools/builtin/local-search-telemetry.mjs +2 -2
- package/src/runtime/agent/orchestrator/tools/builtin/native-search-client.mjs +1 -1
- package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.mjs +12 -3
- package/src/runtime/agent/orchestrator/tools/builtin/task-tool.mjs +12 -1
- package/src/runtime/agent/orchestrator/tools/code-graph/ast-calls.mjs +9 -2
- package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +137 -7
- package/src/runtime/agent/orchestrator/tools/code-graph/calls-cache.mjs +43 -14
- package/src/runtime/agent/orchestrator/tools/code-graph/calls-sidecar-worker.mjs +33 -0
- package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +146 -11
- package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +31 -24
- package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary/spawn-jsonl.mjs +172 -25
- package/src/runtime/agent/orchestrator/tools/code-graph/graph-model.mjs +100 -2
- package/src/runtime/agent/orchestrator/tools/code-graph/main-thread-slice.mjs +18 -0
- package/src/runtime/agent/orchestrator/tools/code-graph/mask-pool.mjs +219 -0
- package/src/runtime/agent/orchestrator/tools/code-graph/mask-worker.mjs +12 -0
- package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.mjs +6 -1
- package/src/runtime/agent/orchestrator/tools/code-graph/modes/calls.mjs +9 -2
- package/src/runtime/agent/orchestrator/tools/code-graph/modes/dependents.mjs +5 -1
- package/src/runtime/agent/orchestrator/tools/code-graph/modes/structure.mjs +8 -2
- package/src/runtime/agent/orchestrator/tools/code-graph/modes/symbols.mjs +14 -11
- package/src/runtime/agent/orchestrator/tools/code-graph/search-references.mjs +41 -18
- package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +391 -83
- package/src/runtime/agent/orchestrator/tools/code-graph/symbol-index.mjs +109 -9
- package/src/runtime/agent/orchestrator/tools/code-graph/text-mask.mjs +73 -29
- package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -5
- package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.mjs +7 -3
- package/src/runtime/channels/lib/crash-log.mjs +21 -1
- package/src/runtime/channels/lib/routines-db.mjs +35 -0
- package/src/runtime/channels/lib/schedules-db.mjs +4 -0
- package/src/runtime/channels/lib/webhooks-db.mjs +4 -0
- package/src/runtime/channels/lib/worker-main.mjs +8 -25
- package/src/runtime/computer-bridge/action-schema.mjs +2 -2
- package/src/runtime/computer-bridge/core-actions.mjs +16 -2
- package/src/runtime/computer-bridge/result-canonical.mjs +6 -0
- package/src/runtime/media/jobs.mjs +5 -0
- package/src/runtime/media/store.mjs +146 -3
- package/src/runtime/memory/tool-defs.mjs +1 -1
- package/src/runtime/office/core/office-write-ledger.mjs +2 -2
- package/src/runtime/shared/agent-reap-state.mjs +19 -0
- package/src/runtime/shared/desktop-notice-host.d.mts +13 -0
- package/src/runtime/shared/desktop-notice-host.mjs +87 -0
- package/src/runtime/shared/file-lock.mjs +2 -2
- package/src/runtime/shared/heap-cap.mjs +9 -11
- package/src/runtime/shared/llm/http-agent.mjs +11 -1
- package/src/runtime/shared/llm/system-proxy.mjs +100 -25
- package/src/runtime/shared/llm/usage-accounting.mjs +121 -19
- package/src/runtime/shared/llm/usage-context.mjs +3 -3
- package/src/runtime/shared/llm/usage-ledger-quota.mjs +2 -0
- package/src/runtime/shared/llm/usage-ledger-requests.mjs +190 -0
- package/src/runtime/shared/llm/usage-ledger-rollup.mjs +7 -3
- package/src/runtime/shared/llm/usage-ledger-worker.mjs +2 -0
- package/src/runtime/shared/llm/usage-ledger.mjs +69 -1
- package/src/runtime/shared/llm/usage-request-status.mjs +48 -0
- package/src/runtime/shared/provider-accounts.mjs +22 -0
- package/src/runtime/shared/provider-auth-binding.mjs +2 -2
- package/src/runtime/shared/resource-admission.mjs +2 -2
- package/src/runtime/shared/root-async-context.mjs +51 -0
- package/src/runtime/shared/run-child.mjs +18 -1
- package/src/runtime/shared/slow-span.mjs +2 -2
- package/src/runtime/shared/tool-execution-owner.mjs +2 -2
- package/src/runtime/shared/tool-result-summary.mjs +1 -1
- package/src/runtime/shared/tool-work-units.mjs +2 -0
- package/src/runtime/shared/turn-snapshot-store.mjs +75 -18
- package/src/runtime/shared/turn-worktree-snapshot.mjs +38 -8
- package/src/runtime/shared/user-cwd.mjs +2 -2
- package/src/runtime/shared/worker-requests.mjs +3 -1
- package/src/runtime/shared/worktrees/lock.mjs +2 -2
- package/src/runtime/visualize-bridge/client.mjs +26 -4
- package/src/runtime/visualize-bridge/tool-defs.mjs +46 -5
- package/src/session-runtime/agent-home-delivery.mjs +76 -8
- package/src/session-runtime/agent-task-sessions.mjs +63 -22
- package/src/session-runtime/boot/apis.mjs +16 -0
- package/src/session-runtime/boot/begin.mjs +2 -0
- package/src/session-runtime/boot/tools.mjs +8 -2
- package/src/session-runtime/internal-tool-executor/ask-user-tool.mjs +3 -58
- package/src/session-runtime/media-api.mjs +15 -0
- package/src/session-runtime/model-route/route-persist.mjs +12 -4
- package/src/session-runtime/provider-models/row-hydration.mjs +2 -0
- package/src/session-runtime/provider-readiness.mjs +10 -2
- package/src/session-runtime/routine-owner.mjs +9 -3
- package/src/session-runtime/runtime-core.mjs +35 -8
- package/src/session-runtime/runtime-review-api.mjs +3 -1
- package/src/session-runtime/services/agent-task-status.mjs +29 -1
- package/src/session-runtime/services/agent-tool/close-flow.mjs +6 -1
- package/src/session-runtime/services/agent-tool/execute-resume.mjs +94 -0
- package/src/session-runtime/services/agent-tool/execute-send.mjs +77 -27
- package/src/session-runtime/services/agent-tool/execute-spawn.mjs +28 -7
- package/src/session-runtime/services/agent-tool/execute.mjs +6 -1
- package/src/session-runtime/services/agent-tool/helpers.mjs +11 -0
- package/src/session-runtime/services/agent-tool/job-views/job-render.mjs +13 -10
- package/src/session-runtime/services/agent-tool/job-views/session-progress.mjs +3 -0
- package/src/session-runtime/services/agent-tool/render.mjs +8 -1
- package/src/session-runtime/services/agent-tool/send-flow.mjs +1 -0
- package/src/session-runtime/services/agent-tool/spawn-flow/spawn-prep.mjs +6 -0
- package/src/session-runtime/services/agent-tool/spawn-flow/spawn-run.mjs +2 -1
- package/src/session-runtime/services/agent-tool/tag-registry/reaped-sessions.mjs +82 -0
- package/src/session-runtime/services/agent-tool/tag-registry/record-facts-worker.mjs +36 -0
- package/src/session-runtime/services/agent-tool/tag-registry.mjs +4 -0
- package/src/session-runtime/services/agent-tool/terminal-reaper/row-helpers.mjs +29 -0
- package/src/session-runtime/services/agent-tool/terminal-reaper/scan-admission.mjs +11 -3
- package/src/session-runtime/services/agent-tool/terminal-reaper/stale-rows.mjs +19 -8
- package/src/session-runtime/services/agent-tool/terminal-reaper.mjs +5 -2
- package/src/session-runtime/services/agent-tool/worker-index.mjs +1 -0
- package/src/session-runtime/services/channel-admin.mjs +44 -3
- package/src/session-runtime/services/daemon-crash-capture/daemon-exit-record.mjs +2 -2
- package/src/session-runtime/services/plugin-admin.mjs +46 -7
- package/src/session-runtime/services/provider-admin.mjs +5 -0
- package/src/session-runtime/session-create.mjs +5 -0
- package/src/session-runtime/setup-tool/agent-settings-actions.mjs +1 -1
- package/src/session-runtime/setup-tool/executor.mjs +1 -1
- package/src/session-runtime/setup-tool/extended-actions.mjs +1 -1
- package/src/session-runtime/setup-tool/settings-contract.mjs +4 -2
- package/src/session-runtime/usage-stats-api.mjs +120 -1
- package/src/session-runtime/workflow-agents-api/agent-connectors.mjs +36 -29
- package/src/session-runtime/workflow-agents-api/agent-editor.mjs +183 -23
- package/src/session-runtime/workspace/admission-gate.mjs +25 -7
- package/src/session-runtime/workspace/transitions.mjs +4 -0
- package/src/standalone/daemon-log.mjs +28 -4
- package/src/standalone/daemon-owner.mjs +41 -7
- package/src/standalone/daemon-work-gate.mjs +139 -0
- package/src/standalone/daemon.mjs +80 -11
- package/src/standalone/desktop-service-registry.mjs +11 -4
- package/src/standalone/session-client.mjs +57 -5
- package/src/standalone/session-protocol.mjs +6 -0
- package/src/standalone/session-service/agent-tree/agent-cancel.mjs +17 -1
- package/src/standalone/session-service/agent-tree.mjs +7 -0
- package/src/standalone/session-service/retention/busy.mjs +3 -2
- package/src/standalone/session-service/session-calls/session-turns.mjs +12 -5
- package/src/standalone/session-transport/call-route.mjs +10 -4
- package/src/standalone/session-transport/routes.mjs +21 -0
- package/src/standalone/session-transport/stack.mjs +3 -1
- package/src/standalone/session-transport.mjs +5 -0
- package/src/tui/app/model-picker/model-footer.mjs +1 -1
- package/src/tui/app/model-picker/provider-models-list.mjs +7 -3
- package/src/tui/app/model-picker/route-selection/fast-parameters.mjs +23 -3
- package/src/tui/app/model-picker/route-selection.mjs +1 -0
- package/src/tui/dist/index.mjs +536 -484
- package/src/tui/session/session-api/integrations.mjs +3 -0
- package/src/tui/session/session-api/media.mjs +5 -0
- package/src/tui/session/tool-approval-settle.mjs +24 -4
- package/src/tui/session/tool-approval.mjs +35 -10
- package/src/tui/session/turn.mjs +13 -0
- package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/cancellation.mjs +0 -71
- package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/reader.mjs +0 -108
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* anthropic-message-threads.mjs — Anthropic message threads (delta sending)
|
|
3
|
+
* for both first-party Anthropic routes (API key and OAuth).
|
|
4
|
+
*
|
|
5
|
+
* The provider builds its full request exactly as before; this module then
|
|
6
|
+
* decides how that request goes on the wire:
|
|
7
|
+
* create the full request plus `thread: {type: 'create'}`
|
|
8
|
+
* continue only the messages after the server-held conversation, plus
|
|
9
|
+
* `thread: {type: 'continue', previous_message_id}`; `system` and
|
|
10
|
+
* `tools` left out (the thread keeps the create-time values)
|
|
11
|
+
* none the unchanged full request
|
|
12
|
+
*
|
|
13
|
+
* Per conversation (`opts.transportScope || opts.sessionId`, per provider) it
|
|
14
|
+
* keeps only ids and digests: the latest stored response id, a fingerprint of
|
|
15
|
+
* the create-only and fixed fields, the account identity, the digest/length of
|
|
16
|
+
* the exact wire message prefix the server holds, and the digest of the stored
|
|
17
|
+
* response's complete content. A send continues only when its own full wire
|
|
18
|
+
* messages are that prefix followed by a message whose content is exactly the
|
|
19
|
+
* stored response, nothing fixed changed and the thread is younger than
|
|
20
|
+
* THREAD_CONTINUE_MAX_AGE_MS; everything else creates. Compaction, rewind,
|
|
21
|
+
* edits, model/account/system/tool/cache-strategy changes therefore fall back
|
|
22
|
+
* to a create by construction.
|
|
23
|
+
*
|
|
24
|
+
* A send that threads owns its conversation until the next send takes it: the
|
|
25
|
+
* stored state is consumed when the send begins and written back only by that
|
|
26
|
+
* owner, only while the conversation was not torn down since (session close,
|
|
27
|
+
* delete, scope disposal), and only for a response that reached message_stop
|
|
28
|
+
* with a normal stop. Anything else leaves "unknown" and the next turn creates.
|
|
29
|
+
* A second send on a conversation whose owner is still in flight goes
|
|
30
|
+
* unthreaded.
|
|
31
|
+
*/
|
|
32
|
+
import { createHash } from 'node:crypto';
|
|
33
|
+
import {
|
|
34
|
+
anthropicFirstPartyRoute,
|
|
35
|
+
anthropicTurnScopedReminders,
|
|
36
|
+
countAnthropicCacheControls,
|
|
37
|
+
resolveAnthropicCacheTtls,
|
|
38
|
+
} from './lib/anthropic-request-utils.mjs';
|
|
39
|
+
import { splitTailReminder } from './lib/tail-reminder-split.mjs';
|
|
40
|
+
|
|
41
|
+
export const MESSAGE_THREADS_BETA = 'message-threads-2026-08-12';
|
|
42
|
+
|
|
43
|
+
// The API expires a thread 5 minutes after its latest stored response; a
|
|
44
|
+
// continue older than this margin is skipped for a direct create.
|
|
45
|
+
export const THREAD_CONTINUE_MAX_AGE_MS = 270_000;
|
|
46
|
+
|
|
47
|
+
// The request-wide limit on cache_control breakpoints with `thread` set.
|
|
48
|
+
const THREAD_MAX_BREAKPOINTS = 4;
|
|
49
|
+
|
|
50
|
+
// How long a threaded stream that ended on an early terminal frame (tool_use
|
|
51
|
+
// message_delta) is read on in the background for the message_stop that
|
|
52
|
+
// confirms storage. The turn itself is not held for it.
|
|
53
|
+
export const THREAD_MESSAGE_STOP_WAIT_MS = 5_000;
|
|
54
|
+
|
|
55
|
+
// Models the feature documents as supported (dated variants of a listed alias
|
|
56
|
+
// and the dated ids themselves match).
|
|
57
|
+
const SUPPORTED_MODELS = [
|
|
58
|
+
'claude-fable-5-1',
|
|
59
|
+
'claude-mythos-5-1',
|
|
60
|
+
'claude-fable-5',
|
|
61
|
+
'claude-mythos-5',
|
|
62
|
+
'claude-mythos-preview',
|
|
63
|
+
'claude-opus-5-5',
|
|
64
|
+
'claude-opus-5',
|
|
65
|
+
'claude-sonnet-5-5',
|
|
66
|
+
'claude-sonnet-5',
|
|
67
|
+
'claude-haiku-5-5',
|
|
68
|
+
'claude-haiku-4-5-20251001',
|
|
69
|
+
'claude-opus-4-8',
|
|
70
|
+
'claude-opus-4-7',
|
|
71
|
+
'claude-opus-4-6',
|
|
72
|
+
'claude-sonnet-4-6',
|
|
73
|
+
'claude-opus-4-5-20251101',
|
|
74
|
+
'claude-sonnet-4-5-20250929',
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
const NORMAL_STOPS = new Set(['end_turn', 'tool_use', 'stop_sequence', 'pause_turn', 'max_tokens']);
|
|
78
|
+
const REPLAY_CODES = new Set([
|
|
79
|
+
'thread_not_found',
|
|
80
|
+
'thread_already_continued',
|
|
81
|
+
'thread_continue_in_flight',
|
|
82
|
+
'thread_fingerprint_mismatch',
|
|
83
|
+
]);
|
|
84
|
+
// thread_unsupported_request values that are organization settings: no
|
|
85
|
+
// request of this account can be threaded.
|
|
86
|
+
const ACCOUNT_UNSUPPORTED = new Set(['hipaa', 'inference_hooks']);
|
|
87
|
+
// An uncoded refusal that names the `thread` parameter or the beta itself.
|
|
88
|
+
const EXPLICIT_THREAD_REJECTION = new RegExp(
|
|
89
|
+
`${MESSAGE_THREADS_BETA}|(?:^|[^\\w-])thread(?:[\`'"]|\\s*:|\\.(?:type|previous_message_id)\\b|\\s+(?:parameter|field)\\b)`,
|
|
90
|
+
'i'
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// scope -> Map(provider -> { token, inFlight, state, disabledAccount }).
|
|
94
|
+
// The inner map is the scope's generation: teardown drops it, so a send
|
|
95
|
+
// planned before the teardown can no longer write into the new one.
|
|
96
|
+
const scopes = new Map();
|
|
97
|
+
// account key -> { confirmed, disabled, logged }
|
|
98
|
+
const accounts = new Map();
|
|
99
|
+
// scope -> Set(release) of background message_stop reads still open.
|
|
100
|
+
const drains = new Map();
|
|
101
|
+
// The latest threaded plan's mode, for diagnostics.
|
|
102
|
+
let lastPlanned = null;
|
|
103
|
+
|
|
104
|
+
function sha(text) {
|
|
105
|
+
return createHash('sha256').update(text).digest('hex');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function threadsSupportModel(model) {
|
|
109
|
+
const id = String(model || '').trim();
|
|
110
|
+
if (!id) return false;
|
|
111
|
+
return SUPPORTED_MODELS.some(
|
|
112
|
+
(listed) =>
|
|
113
|
+
listed === id ||
|
|
114
|
+
(id.startsWith(`${listed}-`) && /^\d{8}$/.test(id.slice(listed.length + 1))) ||
|
|
115
|
+
(listed.startsWith(`${id}-`) && /^\d{8}$/.test(listed.slice(id.length + 1)))
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Threads are on unless the provider config, the send or the environment turns them off. */
|
|
120
|
+
export function messageThreadsEnabled(config, opts) {
|
|
121
|
+
if (process.env.MIXDOG_ANTHROPIC_MESSAGE_THREADS === '0') return false;
|
|
122
|
+
return config?.messageThreads !== false && opts?.messageThreads !== false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The account a thread belongs to, as an opaque hash. */
|
|
126
|
+
export function threadAccountKey(provider, ...parts) {
|
|
127
|
+
return sha([provider, ...parts.map((p) => String(p ?? ''))].join('\u0000'));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function conversationScope(opts) {
|
|
131
|
+
return opts?.transportScope || opts?.sessionId || '';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Context editing other than clear_thinking keep:"all", and compaction, are
|
|
135
|
+
// not available with threads.
|
|
136
|
+
function contextManagementThreadable(body) {
|
|
137
|
+
const cm = body.context_management;
|
|
138
|
+
if (cm == null) return true;
|
|
139
|
+
const edits = Array.isArray(cm.edits) ? cm.edits : [];
|
|
140
|
+
return edits.every((edit) => edit?.type === 'clear_thinking_20251015' && edit?.keep === 'all');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// A legacy tail reminder rides the last user message of this request only, so
|
|
144
|
+
// the next request rewrites what the server would hold: such a create could
|
|
145
|
+
// never be continued.
|
|
146
|
+
function carriesLegacyTailReminder(provider, config, model, requestMessages) {
|
|
147
|
+
if (!requestMessages) return false;
|
|
148
|
+
return Boolean(splitTailReminder(requestMessages).reminder) && !anthropicTurnScopedReminders(provider, model, config);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function eligible({ provider, config, opts, body, requestMessages }) {
|
|
152
|
+
if (!messageThreadsEnabled(config, opts)) return false;
|
|
153
|
+
if (!anthropicFirstPartyRoute(provider, config)) return false;
|
|
154
|
+
// One-shot forks (side questions, compaction summaries) never continue.
|
|
155
|
+
if (opts?.transportScope || opts?.sharedCachePrefixCount || opts?.requestKind === 'compaction') return false;
|
|
156
|
+
if (!opts?.sessionId) return false;
|
|
157
|
+
if (!threadsSupportModel(body?.model)) return false;
|
|
158
|
+
if (body.compaction != null) return false;
|
|
159
|
+
if (carriesLegacyTailReminder(provider, config, body.model, requestMessages)) return false;
|
|
160
|
+
return contextManagementThreadable(body);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function withoutAuthTokens(servers) {
|
|
164
|
+
if (!Array.isArray(servers)) return servers;
|
|
165
|
+
return servers.map((server) => {
|
|
166
|
+
if (!server || typeof server !== 'object') return server;
|
|
167
|
+
const { authorization_token: _token, ...rest } = server;
|
|
168
|
+
return rest;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Hash of every create-only and fixed field, the beta set and the per-layer cache TTLs. */
|
|
173
|
+
export function threadFingerprint(body, betas, opts) {
|
|
174
|
+
return sha(
|
|
175
|
+
JSON.stringify({
|
|
176
|
+
model: body.model,
|
|
177
|
+
system: body.system ?? null,
|
|
178
|
+
tools: body.tools ?? null,
|
|
179
|
+
tool_choice: body.tool_choice ?? null,
|
|
180
|
+
thinking: body.thinking ?? null,
|
|
181
|
+
mcp_servers: withoutAuthTokens(body.mcp_servers) ?? null,
|
|
182
|
+
context_management: body.context_management ?? null,
|
|
183
|
+
output_config: body.output_config ?? null,
|
|
184
|
+
container_skills: body.container?.skills ?? null,
|
|
185
|
+
cache_control: body.cache_control ?? null,
|
|
186
|
+
fallbacks: body.fallbacks !== undefined,
|
|
187
|
+
betas: [...new Set(betas)].sort(),
|
|
188
|
+
cacheTtls: resolveAnthropicCacheTtls(opts),
|
|
189
|
+
})
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// A string content is one text block on the wire (a breakpoint turns it into
|
|
194
|
+
// that shape), so both compare equal.
|
|
195
|
+
function contentBlocks(content) {
|
|
196
|
+
return typeof content === 'string' && content ? [{ type: 'text', text: content }] : content;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function withoutBlockMarker(block) {
|
|
200
|
+
if (!block || typeof block !== 'object' || !Object.hasOwn(block, 'cache_control')) return block;
|
|
201
|
+
const { cache_control: _marker, ...rest } = block;
|
|
202
|
+
return rest;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* A message's content blocks without their breakpoints. A breakpoint is the
|
|
207
|
+
* `cache_control` property of a content block itself, or of a block inside a
|
|
208
|
+
* tool_result's content list; everything else (tool_use input, tool_result
|
|
209
|
+
* payloads, any user data) is kept byte for byte, `cache_control` keys too.
|
|
210
|
+
*/
|
|
211
|
+
function blocksWithoutBreakpoints(content) {
|
|
212
|
+
const blocks = contentBlocks(content);
|
|
213
|
+
if (!Array.isArray(blocks)) return blocks;
|
|
214
|
+
return blocks.map((block) => {
|
|
215
|
+
const bare = withoutBlockMarker(block);
|
|
216
|
+
if (bare?.type !== 'tool_result' || !Array.isArray(bare.content)) return bare;
|
|
217
|
+
return { ...bare, content: bare.content.map(withoutBlockMarker) };
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// One wire message as the server stores it, minus breakpoints.
|
|
222
|
+
function messageKey(message) {
|
|
223
|
+
return JSON.stringify({ ...message, content: blocksWithoutBreakpoints(message?.content) });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function prefixHash(messages, count) {
|
|
227
|
+
const hash = createHash('sha256');
|
|
228
|
+
for (let i = 0; i < count; i++) hash.update(`${messageKey(messages[i])}\n`);
|
|
229
|
+
return hash.digest('hex');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Key-order independent serialization: a tool input the stream parsed and the
|
|
233
|
+
// same input lowered from the transcript compare equal.
|
|
234
|
+
function canonical(value) {
|
|
235
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`;
|
|
236
|
+
if (value && typeof value === 'object') {
|
|
237
|
+
return `{${Object.keys(value)
|
|
238
|
+
.filter((key) => value[key] !== undefined)
|
|
239
|
+
.sort()
|
|
240
|
+
.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`)
|
|
241
|
+
.join(',')}}`;
|
|
242
|
+
}
|
|
243
|
+
return JSON.stringify(value) ?? 'null';
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Digest of an assistant message's complete content: every block with every
|
|
248
|
+
* field (text with its exact whitespace, tool_use id/name/input, thinking and
|
|
249
|
+
* redacted_thinking with their signatures, any other block verbatim), minus
|
|
250
|
+
* breakpoints. The streamed response and the transcript's lowered copy of it
|
|
251
|
+
* digest equal only when the next request would replay exactly what the API
|
|
252
|
+
* stored.
|
|
253
|
+
*/
|
|
254
|
+
export function anthropicResponseDigest(content) {
|
|
255
|
+
const blocks = blocksWithoutBreakpoints(content);
|
|
256
|
+
return Array.isArray(blocks) ? sha(canonical(blocks.map(requestForm))) : '';
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// A streamed tool_use names its caller; `{type: 'direct'}` is what a request
|
|
260
|
+
// block without one means, so it is dropped. Any other caller stays and
|
|
261
|
+
// cannot match a transcript block.
|
|
262
|
+
function requestForm(block) {
|
|
263
|
+
if (block?.type !== 'tool_use' || block.caller?.type !== 'direct' || Object.keys(block.caller).length !== 1) {
|
|
264
|
+
return block;
|
|
265
|
+
}
|
|
266
|
+
const { caller: _direct, ...rest } = block;
|
|
267
|
+
return rest;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function accountRecord(key) {
|
|
271
|
+
let record = accounts.get(key);
|
|
272
|
+
if (!record) accounts.set(key, (record = { confirmed: false, disabled: false, logged: false }));
|
|
273
|
+
return record;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function logOnce(account, line) {
|
|
277
|
+
if (account.logged) return;
|
|
278
|
+
account.logged = true;
|
|
279
|
+
if (!process.env.MIXDOG_QUIET_PROVIDER_LOG) process.stderr.write(`[anthropic-threads] ${line}\n`);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Why a continue is not possible for `messages` (null when it is). */
|
|
283
|
+
function continueBlocker(state, { account, fingerprint, messages, now }) {
|
|
284
|
+
if (!state?.responseId) return 'no-thread';
|
|
285
|
+
if (state.account !== account) return 'account';
|
|
286
|
+
if (state.fingerprint !== fingerprint) return 'fingerprint';
|
|
287
|
+
if (now - state.storedAt > THREAD_CONTINUE_MAX_AGE_MS) return 'expired';
|
|
288
|
+
const n = state.prefixCount;
|
|
289
|
+
if (messages.length <= n) return 'prefix';
|
|
290
|
+
if (prefixHash(messages, n) !== state.prefixHash) return 'prefix';
|
|
291
|
+
const stored = messages[n];
|
|
292
|
+
if (stored?.role !== 'assistant' || anthropicResponseDigest(stored.content) !== state.responseDigest) {
|
|
293
|
+
return 'response';
|
|
294
|
+
}
|
|
295
|
+
const delta = messages.length - n - 1;
|
|
296
|
+
// A paused turn resumes with no messages and takes no new user turn; any
|
|
297
|
+
// other stop needs new messages.
|
|
298
|
+
if (state.stopReason === 'pause_turn' ? delta !== 0 : delta === 0) return 'delta';
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function continueBody(body, state) {
|
|
303
|
+
const { system: _system, tools: _tools, messages, ...rest } = body;
|
|
304
|
+
return {
|
|
305
|
+
...rest,
|
|
306
|
+
messages: messages.slice(state.prefixCount + 1),
|
|
307
|
+
thread: { type: 'continue', previous_message_id: state.responseId },
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Breakpoints a request with `thread` carries: on a continue the create-time
|
|
312
|
+
// system/tools breakpoints count again; stored messages do not.
|
|
313
|
+
function breakpointCount(wire, createBody) {
|
|
314
|
+
const top = wire.cache_control ? 1 : 0;
|
|
315
|
+
return (
|
|
316
|
+
top +
|
|
317
|
+
countAnthropicCacheControls(createBody.system || []) +
|
|
318
|
+
countAnthropicCacheControls(createBody.tools || []) +
|
|
319
|
+
countAnthropicCacheControls(wire.messages || [])
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// The streamed response the API stored: message_stop arrived, the stop was a
|
|
324
|
+
// normal one, no tool call was cut by max_tokens, no fallback model wrote it,
|
|
325
|
+
// and every block was recorded completely.
|
|
326
|
+
function storedResponse(result, receipt) {
|
|
327
|
+
if (!receipt?.id || !receipt.stoppedAt || receipt.opaque || !Array.isArray(receipt.blocks)) return false;
|
|
328
|
+
const stop = result?.stopReason;
|
|
329
|
+
if (!NORMAL_STOPS.has(stop)) return false;
|
|
330
|
+
const toolCall = receipt.blocks.some((b) => b?.type === 'tool_use' || b?.type === 'server_tool_use');
|
|
331
|
+
if (stop === 'max_tokens' && toolCall) return false;
|
|
332
|
+
if (receipt.blocks.some((b) => b?.type === 'fallback')) return false;
|
|
333
|
+
if (Array.isArray(result.contentBlockTypes) && result.contentBlockTypes.includes('fallback')) return false;
|
|
334
|
+
const iterations = result.usage?.raw?.iterations;
|
|
335
|
+
if (Array.isArray(iterations) && iterations.some((it) => it?.type === 'fallback_message')) return false;
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function unthreaded(body, betas) {
|
|
340
|
+
return {
|
|
341
|
+
mode: 'none',
|
|
342
|
+
body,
|
|
343
|
+
betas,
|
|
344
|
+
commit() {},
|
|
345
|
+
settle() {},
|
|
346
|
+
classify: () => null,
|
|
347
|
+
toCreate() {},
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* The wire plan for one send. `body` is the full request the provider built
|
|
353
|
+
* (final messages, images prepared); `betas` the beta names it sends;
|
|
354
|
+
* `requestMessages` the transcript messages the provider was given.
|
|
355
|
+
* `opts._anthropicThread` (internal, from a thread-error resend):
|
|
356
|
+
* { force: 'create'|'none', replay }.
|
|
357
|
+
* Returns { mode, body, betas, commit(result), settle(), classify(), toCreate() }.
|
|
358
|
+
*/
|
|
359
|
+
export function planAnthropicThread({
|
|
360
|
+
provider,
|
|
361
|
+
config,
|
|
362
|
+
opts,
|
|
363
|
+
body,
|
|
364
|
+
betas = [],
|
|
365
|
+
account,
|
|
366
|
+
requestMessages = null,
|
|
367
|
+
now = Date.now(),
|
|
368
|
+
}) {
|
|
369
|
+
const directive = opts?._anthropicThread || null;
|
|
370
|
+
const scope = conversationScope(opts);
|
|
371
|
+
if (directive?.force === 'none' || !eligible({ provider, config, opts, body, requestMessages })) {
|
|
372
|
+
return unthreaded(body, betas);
|
|
373
|
+
}
|
|
374
|
+
const accountState = accountRecord(account);
|
|
375
|
+
if (accountState.disabled) return unthreaded(body, betas);
|
|
376
|
+
let generation = scopes.get(scope);
|
|
377
|
+
const record = generation?.get(provider) || null;
|
|
378
|
+
if (record?.disabledAccount === account) return unthreaded(body, betas);
|
|
379
|
+
// Another send of this conversation is still in flight: it owns the thread.
|
|
380
|
+
if (record?.inFlight) return unthreaded(body, betas);
|
|
381
|
+
|
|
382
|
+
const threadBetas = betas.includes(MESSAGE_THREADS_BETA) ? betas : [...betas, MESSAGE_THREADS_BETA];
|
|
383
|
+
const fingerprint = threadFingerprint(body, threadBetas, opts);
|
|
384
|
+
const messages = Array.isArray(body.messages) ? body.messages : [];
|
|
385
|
+
const prior = record?.state || null;
|
|
386
|
+
|
|
387
|
+
let mode = 'create';
|
|
388
|
+
let wire = { ...body, thread: { type: 'create' } };
|
|
389
|
+
const blocker =
|
|
390
|
+
directive?.force === 'create' ? 'forced' : continueBlocker(prior, { account, fingerprint, messages, now });
|
|
391
|
+
if (!blocker) {
|
|
392
|
+
const candidate = continueBody(body, prior);
|
|
393
|
+
if (breakpointCount(candidate, body) <= THREAD_MAX_BREAKPOINTS) {
|
|
394
|
+
mode = 'continue';
|
|
395
|
+
wire = candidate;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (mode === 'create' && breakpointCount(wire, body) > THREAD_MAX_BREAKPOINTS) return unthreaded(body, betas);
|
|
399
|
+
|
|
400
|
+
// Take the conversation: the previous id is consumed (or abandoned) by this
|
|
401
|
+
// send either way, and only this send may store the next one.
|
|
402
|
+
if (!generation) scopes.set(scope, (generation = new Map()));
|
|
403
|
+
const token = {};
|
|
404
|
+
generation.set(provider, { token, inFlight: true, state: null, disabledAccount: null });
|
|
405
|
+
lastPlanned = { mode, blocker, messages: wire.messages?.length ?? 0 };
|
|
406
|
+
const owned = () => scopes.get(scope) === generation && generation.get(provider)?.token === token;
|
|
407
|
+
|
|
408
|
+
const commitNow = (result, receipt) => {
|
|
409
|
+
if (!owned() || !storedResponse(result, receipt)) return;
|
|
410
|
+
accountState.confirmed = true;
|
|
411
|
+
generation.get(provider).state = {
|
|
412
|
+
account,
|
|
413
|
+
fingerprint,
|
|
414
|
+
prefixCount: messages.length,
|
|
415
|
+
prefixHash: prefixHash(messages, messages.length),
|
|
416
|
+
responseId: receipt.id,
|
|
417
|
+
responseDigest: anthropicResponseDigest(receipt.blocks),
|
|
418
|
+
stopReason: result.stopReason,
|
|
419
|
+
storedAt: receipt.stoppedAt,
|
|
420
|
+
fromReplay: plan.replay,
|
|
421
|
+
};
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
const plan = {
|
|
425
|
+
mode,
|
|
426
|
+
body: wire,
|
|
427
|
+
betas: threadBetas,
|
|
428
|
+
blocker,
|
|
429
|
+
replay: directive?.replay === true,
|
|
430
|
+
// The state this continue started from was itself made by a replay.
|
|
431
|
+
afterReplay: mode === 'continue' && prior?.fromReplay === true,
|
|
432
|
+
/** A dropped continue was resent once: the next attempt replays in place. */
|
|
433
|
+
toCreate() {
|
|
434
|
+
if (plan.mode !== 'continue') return;
|
|
435
|
+
wire.messages = body.messages;
|
|
436
|
+
if (body.system !== undefined) wire.system = body.system;
|
|
437
|
+
if (body.tools !== undefined) wire.tools = body.tools;
|
|
438
|
+
wire.thread = { type: 'create' };
|
|
439
|
+
plan.mode = 'create';
|
|
440
|
+
plan.replay = true;
|
|
441
|
+
plan.afterReplay = false;
|
|
442
|
+
},
|
|
443
|
+
/** Record the streamed response once the API confirmed storing it
|
|
444
|
+
* (message_stop), now or when a background read sees it. */
|
|
445
|
+
commit(result) {
|
|
446
|
+
const receipt = result?.threadReceipt;
|
|
447
|
+
if (!receipt) return;
|
|
448
|
+
if (receipt.stoppedAt || !receipt.pending) {
|
|
449
|
+
commitNow(result, receipt);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
// Tearing the conversation down releases the background read at once.
|
|
453
|
+
const release = typeof receipt.cancel === 'function' ? receipt.cancel : null;
|
|
454
|
+
if (release) {
|
|
455
|
+
if (!drains.has(scope)) drains.set(scope, new Set());
|
|
456
|
+
drains.get(scope).add(release);
|
|
457
|
+
}
|
|
458
|
+
receipt.pending.then(() => {
|
|
459
|
+
const pending = drains.get(scope);
|
|
460
|
+
pending?.delete(release);
|
|
461
|
+
if (pending && !pending.size) drains.delete(scope);
|
|
462
|
+
commitNow(result, receipt);
|
|
463
|
+
});
|
|
464
|
+
},
|
|
465
|
+
/** The send is over: a later send may take the conversation. */
|
|
466
|
+
settle() {
|
|
467
|
+
if (owned()) generation.get(provider).inFlight = false;
|
|
468
|
+
},
|
|
469
|
+
/** The resend directive for a rejected threaded request, or null when the rejection is not about threads. */
|
|
470
|
+
classify(status, errorBody) {
|
|
471
|
+
return classifyRejection(plan, {
|
|
472
|
+
status,
|
|
473
|
+
errorBody,
|
|
474
|
+
accountState,
|
|
475
|
+
directive,
|
|
476
|
+
disableConversation: () => {
|
|
477
|
+
if (owned()) generation.get(provider).disabledAccount = account;
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
},
|
|
481
|
+
};
|
|
482
|
+
return plan;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** The thread error fields of an Anthropic error body (text or parsed). */
|
|
486
|
+
export function anthropicThreadErrorDetails(errorBody) {
|
|
487
|
+
let parsed = errorBody;
|
|
488
|
+
if (typeof errorBody === 'string') {
|
|
489
|
+
try {
|
|
490
|
+
parsed = JSON.parse(errorBody);
|
|
491
|
+
} catch {
|
|
492
|
+
parsed = null;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const error = parsed?.error && typeof parsed.error === 'object' ? parsed.error : parsed;
|
|
496
|
+
const details = error?.details || {};
|
|
497
|
+
const unsupported = Array.isArray(details.unsupported) ? details.unsupported.map(String) : [];
|
|
498
|
+
const message = String(error?.message || (typeof errorBody === 'string' ? errorBody : '') || '');
|
|
499
|
+
return { code: typeof details.error_code === 'string' ? details.error_code : '', unsupported, message };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function classifyRejection(plan, { status, errorBody, accountState, directive, disableConversation }) {
|
|
503
|
+
if (plan.mode === 'none') return null;
|
|
504
|
+
const { code, unsupported, message } = anthropicThreadErrorDetails(errorBody);
|
|
505
|
+
if (code === 'thread_unsupported_request') {
|
|
506
|
+
if (unsupported.some((value) => ACCOUNT_UNSUPPORTED.has(value))) {
|
|
507
|
+
accountState.disabled = true;
|
|
508
|
+
logOnce(accountState, `threads unavailable for this account (${unsupported.join(',')}); sending unthreaded`);
|
|
509
|
+
} else {
|
|
510
|
+
disableConversation();
|
|
511
|
+
}
|
|
512
|
+
return { force: 'none' };
|
|
513
|
+
}
|
|
514
|
+
if (REPLAY_CODES.has(code)) {
|
|
515
|
+
// A continue that still cannot be served right after a replay: the API may
|
|
516
|
+
// be unable to store this conversation.
|
|
517
|
+
if (code === 'thread_not_found' && plan.afterReplay) {
|
|
518
|
+
disableConversation();
|
|
519
|
+
return { force: 'none' };
|
|
520
|
+
}
|
|
521
|
+
if (plan.mode !== 'continue' || directive?.replay) return { force: 'none' };
|
|
522
|
+
return { force: 'create', replay: true };
|
|
523
|
+
}
|
|
524
|
+
if (code.startsWith('thread_')) return { force: 'none' };
|
|
525
|
+
// An uncoded refusal that names the thread parameter or the beta itself (a
|
|
526
|
+
// route that does not take threads). The first one for an account turns
|
|
527
|
+
// threads off for it in this process. Any other error is not about threads.
|
|
528
|
+
const s = Number(status) || 0;
|
|
529
|
+
if ((s === 400 || s === 403 || s === 422) && EXPLICIT_THREAD_REJECTION.test(message)) {
|
|
530
|
+
if (!accountState.confirmed) {
|
|
531
|
+
accountState.disabled = true;
|
|
532
|
+
logOnce(accountState, `threads rejected for this account (HTTP ${s}); sending unthreaded from now on`);
|
|
533
|
+
}
|
|
534
|
+
return { force: 'none' };
|
|
535
|
+
}
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Drop every thread record of a conversation scope (a new generation) and
|
|
540
|
+
* release its background message_stop reads. */
|
|
541
|
+
export function forgetAnthropicThreads(scope) {
|
|
542
|
+
if (!scope) return;
|
|
543
|
+
scopes.delete(scope);
|
|
544
|
+
const pending = drains.get(scope);
|
|
545
|
+
drains.delete(scope);
|
|
546
|
+
for (const release of pending || []) {
|
|
547
|
+
try {
|
|
548
|
+
release();
|
|
549
|
+
} catch {
|
|
550
|
+
// releasing a background read is best-effort
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Session close, delete/reap and transport-scope disposal reach providers
|
|
556
|
+
// through this global (session-close.mjs, transport-scope.mjs); chain the
|
|
557
|
+
// handler already installed so every transport keeps its teardown. A turn
|
|
558
|
+
// abort (`inFlightOnly`) needs nothing: the send in flight already consumed
|
|
559
|
+
// its state and records nothing without message_stop.
|
|
560
|
+
const _priorThreadCloseHook = globalThis.__mixdogCloseProviderConnectionsForSession;
|
|
561
|
+
globalThis.__mixdogCloseProviderConnectionsForSession = (scope, reason, options) => {
|
|
562
|
+
try {
|
|
563
|
+
_priorThreadCloseHook?.(scope, reason, options);
|
|
564
|
+
} finally {
|
|
565
|
+
if (!options?.inFlightOnly) forgetAnthropicThreads(scope);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
export const _threadTest = {
|
|
570
|
+
reset() {
|
|
571
|
+
scopes.clear();
|
|
572
|
+
accounts.clear();
|
|
573
|
+
drains.clear();
|
|
574
|
+
},
|
|
575
|
+
state: (scope, provider) => scopes.get(scope)?.get(provider)?.state || null,
|
|
576
|
+
record: (scope, provider) => scopes.get(scope)?.get(provider) || null,
|
|
577
|
+
setStoredAt(scope, provider, storedAt) {
|
|
578
|
+
const state = scopes.get(scope)?.get(provider)?.state;
|
|
579
|
+
if (state) state.storedAt = storedAt;
|
|
580
|
+
},
|
|
581
|
+
scopes: () => [...scopes.keys()],
|
|
582
|
+
lastPlanned: () => lastPlanned,
|
|
583
|
+
};
|
|
@@ -10,7 +10,7 @@ import { createNpmVersionSource, maxSemver } from './npm-cli-version.mjs';
|
|
|
10
10
|
// source release just to advance the user-agent version.
|
|
11
11
|
// Claude Opus 5.5 requires 2.1.280 or newer. The release workflow raises this
|
|
12
12
|
// to the published CLI version (scripts/sync-client-version-floors.mjs).
|
|
13
|
-
export const DEFAULT_CLI_VERSION = '2.1.
|
|
13
|
+
export const DEFAULT_CLI_VERSION = '2.1.317';
|
|
14
14
|
export const CLAUDE_CLI_NPM_PACKAGE = '@anthropic-ai/claude-code';
|
|
15
15
|
|
|
16
16
|
const CACHE_SCHEMA_VERSION = 1;
|