@jini-ai/agent-runtime 0.7.2 → 0.7.3-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -13
- package/README.md +1 -0
- package/dist/acp-model-probe.d.ts +2 -3
- package/dist/acp-model-probe.js +1 -1
- package/dist/agent-protocol/acp/account-failure.d.ts +3 -1
- package/dist/agent-protocol/acp/account-failure.js +1 -1
- package/dist/agent-protocol/acp/constants.d.ts +3 -1
- package/dist/agent-protocol/acp/constants.js +2 -0
- package/dist/agent-protocol/acp/index.d.ts +2 -1
- package/dist/agent-protocol/acp/index.js +2 -1
- package/dist/agent-protocol/acp/json.d.ts +26 -7
- package/dist/agent-protocol/acp/json.js +11 -11
- package/dist/agent-protocol/acp/models.d.ts +26 -6
- package/dist/agent-protocol/acp/models.js +157 -129
- package/dist/agent-protocol/acp/rpc.d.ts +37 -9
- package/dist/agent-protocol/acp/rpc.js +19 -16
- package/dist/agent-protocol/acp/session-params.d.ts +4 -1
- package/dist/agent-protocol/acp/session-params.js +1 -1
- package/dist/agent-protocol/acp/session-permission.d.ts +13 -0
- package/dist/agent-protocol/acp/session-permission.js +110 -0
- package/dist/agent-protocol/acp/session-results.d.ts +61 -0
- package/dist/agent-protocol/acp/session-results.js +226 -0
- package/dist/agent-protocol/acp/session-run.d.ts +92 -0
- package/dist/agent-protocol/acp/session-run.js +555 -0
- package/dist/agent-protocol/acp/session-state.d.ts +9 -0
- package/dist/agent-protocol/acp/session-state.js +29 -0
- package/dist/agent-protocol/acp/session-text-updates.d.ts +56 -0
- package/dist/agent-protocol/acp/session-text-updates.js +119 -0
- package/dist/agent-protocol/acp/session-tool-updates.d.ts +21 -0
- package/dist/agent-protocol/acp/session-tool-updates.js +90 -0
- package/dist/agent-protocol/acp/session-types.d.ts +166 -0
- package/dist/agent-protocol/acp/session-types.js +4 -0
- package/dist/agent-protocol/acp/session.d.ts +4 -280
- package/dist/agent-protocol/acp/session.js +65 -998
- package/dist/agent-protocol/acp/text-suppression.d.ts +5 -1
- package/dist/agent-protocol/acp/text-suppression.js +1 -1
- package/dist/agent-protocol/acp/updates.d.ts +43 -12
- package/dist/agent-protocol/acp/updates.js +34 -34
- package/dist/agent-protocol/core/json-line-stream.d.ts +133 -36
- package/dist/agent-protocol/core/json-line-stream.js +62 -62
- package/dist/agent-protocol/pi-rpc/events.d.ts +14 -4
- package/dist/agent-protocol/pi-rpc/events.js +14 -14
- package/dist/agent-protocol/pi-rpc/internal.d.ts +9 -3
- package/dist/agent-protocol/pi-rpc/internal.js +3 -3
- package/dist/agent-protocol/pi-rpc/models.d.ts +3 -17
- package/dist/agent-protocol/pi-rpc/models.js +1 -45
- package/dist/agent-protocol/pi-rpc/session.d.ts +36 -12
- package/dist/agent-protocol/pi-rpc/session.js +134 -122
- package/dist/amr-model-cache.d.ts +1 -1
- package/dist/anthropic-live-models.d.ts +8 -3
- package/dist/anthropic-live-models.js +0 -0
- package/dist/auth-common.d.ts +2 -0
- package/dist/auth-common.js +2 -0
- package/dist/auth.d.ts +7 -34
- package/dist/auth.js +25 -139
- package/dist/capabilities.d.ts +1 -1
- package/dist/claude-code-models.d.ts +13 -5
- package/dist/claude-code-models.js +12 -10
- package/dist/claude-filesystem-policy.d.ts +1 -1
- package/dist/claude-stream-frames.d.ts +125 -0
- package/dist/claude-stream-frames.js +327 -0
- package/dist/claude-stream-handler.d.ts +19 -0
- package/dist/claude-stream-handler.js +480 -0
- package/dist/claude-stream.d.ts +3 -72
- package/dist/claude-stream.js +2 -801
- package/dist/core/model-discovery-shared.d.ts +14 -8
- package/dist/core/model-discovery-shared.js +6 -9
- package/dist/core/omit-keys.d.ts +4 -1
- package/dist/core/omit-keys.js +1 -1
- package/dist/defs/aider/executable.d.ts +2 -0
- package/dist/defs/aider/executable.js +2 -0
- package/dist/defs/aider/model-discovery.d.ts +3 -0
- package/dist/defs/aider/model-discovery.js +6 -0
- package/dist/defs/aider.d.ts +10 -10
- package/dist/defs/amp/index.d.ts +35 -0
- package/dist/defs/amp/index.js +75 -0
- package/dist/defs/amp.d.ts +1 -34
- package/dist/defs/amp.js +1 -74
- package/dist/defs/amr/executable-resolution.d.ts +7 -0
- package/dist/defs/amr/executable-resolution.js +50 -0
- package/dist/defs/amr/executable.d.ts +2 -0
- package/dist/defs/amr/executable.js +2 -0
- package/dist/defs/amr/index.d.ts +69 -0
- package/dist/defs/amr/index.js +279 -0
- package/dist/defs/amr/model-discovery.d.ts +3 -0
- package/dist/defs/amr/model-discovery.js +4 -0
- package/dist/defs/amr.d.ts +1 -68
- package/dist/defs/amr.js +1 -291
- package/dist/defs/antigravity/auth.d.ts +14 -0
- package/dist/defs/antigravity/auth.js +47 -0
- package/dist/defs/antigravity/index.d.ts +9 -9
- package/dist/defs/antigravity/index.js +3 -3
- package/dist/defs/antigravity/model-discovery.d.ts +3 -0
- package/dist/defs/antigravity/model-discovery.js +5 -0
- package/dist/defs/auggie/executable.d.ts +2 -0
- package/dist/defs/auggie/executable.js +2 -0
- package/dist/defs/auggie.d.ts +13 -13
- package/dist/defs/auggie.js +2 -9
- package/dist/defs/claude/auth.d.ts +15 -0
- package/dist/defs/claude/auth.js +47 -0
- package/dist/defs/claude/executable.d.ts +2 -0
- package/dist/defs/claude/executable.js +2 -0
- package/dist/defs/claude/model-discovery.d.ts +3 -0
- package/dist/defs/claude/model-discovery.js +20 -0
- package/dist/defs/claude-compatible-args.d.ts +10 -6
- package/dist/defs/claude-compatible-args.js +5 -5
- package/dist/defs/claude.d.ts +11 -11
- package/dist/defs/claude.js +5 -4
- package/dist/defs/cline/executable.d.ts +2 -0
- package/dist/defs/cline/executable.js +2 -0
- package/dist/defs/cline.d.ts +13 -13
- package/dist/defs/cline.js +2 -9
- package/dist/defs/codebuddy/executable.d.ts +2 -0
- package/dist/defs/codebuddy/executable.js +2 -0
- package/dist/defs/codebuddy/index.d.ts +48 -0
- package/dist/defs/codebuddy/index.js +121 -0
- package/dist/defs/codebuddy/model-discovery.d.ts +3 -0
- package/dist/defs/codebuddy/model-discovery.js +5 -0
- package/dist/defs/codebuddy.d.ts +1 -47
- package/dist/defs/codebuddy.js +1 -120
- package/dist/defs/codex/auth.d.ts +4 -0
- package/dist/defs/codex/auth.js +2 -0
- package/dist/defs/codex/event-stream.d.ts +10 -0
- package/dist/defs/codex/event-stream.js +353 -0
- package/dist/defs/codex/executable-resolution.d.ts +7 -0
- package/dist/defs/codex/executable-resolution.js +37 -0
- package/dist/defs/codex/executable.d.ts +2 -0
- package/dist/defs/codex/executable.js +2 -0
- package/dist/defs/codex/launch.d.ts +3 -0
- package/dist/defs/codex/launch.js +155 -0
- package/dist/defs/codex/model-discovery.d.ts +3 -0
- package/dist/defs/codex/model-discovery.js +11 -0
- package/dist/defs/codex/options.d.ts +11 -0
- package/dist/defs/codex/options.js +5 -0
- package/dist/defs/codex.d.ts +18 -11
- package/dist/defs/codex.js +32 -29
- package/dist/defs/crush/executable.d.ts +2 -0
- package/dist/defs/crush/executable.js +2 -0
- package/dist/defs/crush/model-discovery.d.ts +3 -0
- package/dist/defs/crush/model-discovery.js +6 -0
- package/dist/defs/crush.d.ts +10 -10
- package/dist/defs/cursor-agent/auth.d.ts +13 -0
- package/dist/defs/cursor-agent/auth.js +22 -0
- package/dist/defs/cursor-agent/event-stream.d.ts +8 -0
- package/dist/defs/cursor-agent/event-stream.js +104 -0
- package/dist/defs/cursor-agent/executable.d.ts +2 -0
- package/dist/defs/cursor-agent/executable.js +2 -0
- package/dist/defs/cursor-agent/model-discovery.d.ts +3 -0
- package/dist/defs/cursor-agent/model-discovery.js +5 -0
- package/dist/defs/cursor-agent.d.ts +11 -13
- package/dist/defs/cursor-agent.js +1 -1
- package/dist/defs/deepseek/auth.d.ts +13 -0
- package/dist/defs/deepseek/auth.js +18 -0
- package/dist/defs/deepseek/executable.d.ts +2 -0
- package/dist/defs/deepseek/executable.js +2 -0
- package/dist/defs/deepseek/model-discovery.d.ts +3 -0
- package/dist/defs/deepseek/model-discovery.js +5 -0
- package/dist/defs/deepseek.d.ts +10 -10
- package/dist/defs/devin/executable.d.ts +2 -0
- package/dist/defs/devin/executable.js +2 -0
- package/dist/defs/devin.d.ts +15 -15
- package/dist/defs/devin.js +3 -10
- package/dist/defs/droid/executable.d.ts +2 -0
- package/dist/defs/droid/executable.js +2 -0
- package/dist/defs/droid/model-discovery.d.ts +3 -0
- package/dist/defs/droid/model-discovery.js +5 -0
- package/dist/defs/droid.d.ts +10 -10
- package/dist/defs/gemini/event-stream.d.ts +14 -0
- package/dist/defs/gemini/event-stream.js +193 -0
- package/dist/defs/goose/executable.d.ts +2 -0
- package/dist/defs/goose/executable.js +2 -0
- package/dist/defs/goose.d.ts +13 -13
- package/dist/defs/goose.js +2 -9
- package/dist/defs/grok-build/model-discovery.d.ts +3 -0
- package/dist/defs/grok-build/model-discovery.js +5 -0
- package/dist/defs/grok-build.d.ts +10 -10
- package/dist/defs/hermes/executable.d.ts +2 -0
- package/dist/defs/hermes/executable.js +2 -0
- package/dist/defs/hermes/index.d.ts +34 -0
- package/dist/defs/hermes/index.js +57 -0
- package/dist/defs/hermes.d.ts +1 -33
- package/dist/defs/hermes.js +1 -56
- package/dist/defs/index.d.ts +1 -0
- package/dist/defs/index.js +1 -0
- package/dist/defs/kilo/executable.d.ts +2 -0
- package/dist/defs/kilo/executable.js +2 -0
- package/dist/defs/kilo.d.ts +13 -13
- package/dist/defs/kilo.js +2 -9
- package/dist/defs/kimi/event-stream.d.ts +3 -0
- package/dist/defs/kimi/event-stream.js +46 -0
- package/dist/defs/kimi/executable.d.ts +2 -0
- package/dist/defs/kimi/executable.js +2 -0
- package/dist/defs/kimi.d.ts +13 -13
- package/dist/defs/kimi.js +2 -9
- package/dist/defs/kiro/executable.d.ts +2 -0
- package/dist/defs/kiro/executable.js +2 -0
- package/dist/defs/kiro.d.ts +13 -13
- package/dist/defs/kiro.js +2 -9
- package/dist/defs/mimo/executable.d.ts +2 -0
- package/dist/defs/mimo/executable.js +2 -0
- package/dist/defs/mimo/model-discovery.d.ts +3 -0
- package/dist/defs/mimo/model-discovery.js +7 -0
- package/dist/defs/mimo.d.ts +10 -10
- package/dist/defs/opencode/event-stream.d.ts +8 -0
- package/dist/defs/opencode/event-stream.js +108 -0
- package/dist/defs/opencode/executable.d.ts +2 -0
- package/dist/defs/opencode/executable.js +2 -0
- package/dist/defs/opencode/model-discovery.d.ts +4 -0
- package/dist/defs/opencode/model-discovery.js +14 -0
- package/dist/defs/opencode-acp.d.ts +42 -0
- package/dist/defs/opencode-acp.js +23 -0
- package/dist/defs/opencode.d.ts +9 -9
- package/dist/defs/pi/executable.d.ts +2 -0
- package/dist/defs/pi/executable.js +2 -0
- package/dist/defs/pi/model-discovery.d.ts +4 -0
- package/dist/defs/pi/model-discovery.js +16 -0
- package/dist/defs/pi/options.d.ts +11 -0
- package/dist/defs/pi/options.js +5 -0
- package/dist/defs/pi.d.ts +10 -10
- package/dist/defs/qoder/executable.d.ts +2 -0
- package/dist/defs/qoder/executable.js +2 -0
- package/dist/defs/qoder/model-discovery.d.ts +3 -0
- package/dist/defs/qoder/model-discovery.js +7 -0
- package/dist/defs/qoder.d.ts +9 -9
- package/dist/defs/qwen/executable.d.ts +2 -0
- package/dist/defs/qwen/executable.js +2 -0
- package/dist/defs/qwen/model-discovery.d.ts +3 -0
- package/dist/defs/qwen/model-discovery.js +7 -0
- package/dist/defs/qwen.d.ts +10 -10
- package/dist/defs/reasonix/auth.d.ts +13 -0
- package/dist/defs/reasonix/auth.js +18 -0
- package/dist/defs/reasonix/executable.d.ts +2 -0
- package/dist/defs/reasonix/executable.js +2 -0
- package/dist/defs/reasonix.d.ts +13 -13
- package/dist/defs/reasonix.js +2 -9
- package/dist/defs/shared.d.ts +10 -2
- package/dist/defs/shared.js +5 -1
- package/dist/defs/trae-cli/executable.d.ts +2 -0
- package/dist/defs/trae-cli/executable.js +2 -0
- package/dist/defs/trae-cli.d.ts +15 -15
- package/dist/defs/trae-cli.js +2 -9
- package/dist/defs/vibe/executable.d.ts +2 -0
- package/dist/defs/vibe/executable.js +2 -0
- package/dist/defs/vibe.d.ts +13 -13
- package/dist/defs/vibe.js +2 -9
- package/dist/detection.d.ts +4 -3
- package/dist/detection.js +22 -22
- package/dist/diagnostics.d.ts +4 -4
- package/dist/diagnostics.js +2 -2
- package/dist/env.js +2 -1
- package/dist/executable-helpers.d.ts +21 -0
- package/dist/executable-helpers.js +2 -0
- package/dist/executables.d.ts +2 -2
- package/dist/executables.js +68 -114
- package/dist/index.d.ts +6 -2
- package/dist/index.js +5 -1
- package/dist/invocation.d.ts +22 -6
- package/dist/invocation.js +19 -17
- package/dist/json-event-stream-shared.d.ts +81 -0
- package/dist/json-event-stream-shared.js +147 -0
- package/dist/json-event-stream.d.ts +2 -4
- package/dist/json-event-stream.js +15 -940
- package/dist/launch.d.ts +1 -9
- package/dist/launch.js +6 -155
- package/dist/log-file.d.ts +2 -8
- package/dist/log-file.js +1 -1
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +1 -1
- package/dist/mmd-routes.d.ts +7 -7
- package/dist/mmd-routes.js +19 -27
- package/dist/model-discovery-adapters.d.ts +47 -11
- package/dist/model-discovery-adapters.js +47 -41
- package/dist/model-discovery-config-types.d.ts +44 -0
- package/dist/model-discovery-config-types.js +20 -0
- package/dist/model-discovery-config.d.ts +19 -11
- package/dist/model-discovery-config.js +64 -87
- package/dist/model-discovery-native.d.ts +2 -1
- package/dist/model-discovery-native.js +3 -3
- package/dist/model-discovery-sdk.d.ts +6 -2
- package/dist/model-discovery-sdk.js +2 -2
- package/dist/model-discovery-types.d.ts +1 -138
- package/dist/model-discovery-yaml.d.ts +3 -1
- package/dist/model-discovery-yaml.js +1 -1
- package/dist/model-discovery.d.ts +15 -5
- package/dist/model-discovery.js +119 -105
- package/dist/model-loading-state.d.ts +1 -1
- package/dist/model-loading-state.js +1 -1
- package/dist/model-registry.d.ts +5 -5
- package/dist/model-registry.js +7 -6
- package/dist/models.d.ts +4 -3
- package/dist/models.js +2 -2
- package/dist/native-permissions.d.ts +47 -0
- package/dist/native-permissions.js +80 -0
- package/dist/native-runtime.d.ts +11 -8
- package/dist/native-runtime.js +8 -4
- package/dist/native-tool-policy.d.ts +1 -40
- package/dist/opencode-log.d.ts +3 -3
- package/dist/opencode-log.js +5 -3
- package/dist/paths.d.ts +1 -1
- package/dist/paths.js +1 -1
- package/dist/pi-models.d.ts +12 -2
- package/dist/pi-models.js +11 -1
- package/dist/prompt-augmenter.d.ts +1 -37
- package/dist/prompt-augmenter.js +0 -6
- package/dist/prompt-budget.d.ts +3 -3
- package/dist/prompt-budget.js +2 -2
- package/dist/prompt-file.d.ts +2 -6
- package/dist/prompt-file.js +1 -1
- package/dist/providers/aihubmix.d.ts +8 -8
- package/dist/providers/aihubmix.js +8 -8
- package/dist/providers/anthropic-messages.d.ts +22 -23
- package/dist/providers/anthropic-messages.js +26 -53
- package/dist/providers/azure-chat.d.ts +7 -6
- package/dist/providers/azure-chat.js +9 -6
- package/dist/providers/connection-guard.d.ts +3 -9
- package/dist/providers/connection-guard.js +6 -14
- package/dist/providers/connection-test.d.ts +1 -2
- package/dist/providers/connection-test.js +51 -51
- package/dist/providers/credentials.d.ts +59 -0
- package/dist/providers/credentials.js +80 -0
- package/dist/providers/elevenlabs.d.ts +2 -6
- package/dist/providers/elevenlabs.js +2 -31
- package/dist/providers/google-messages.d.ts +11 -10
- package/dist/providers/google-messages.js +16 -35
- package/dist/providers/google-schema.d.ts +1 -1
- package/dist/providers/google-schema.js +1 -1
- package/dist/providers/google.d.ts +6 -6
- package/dist/providers/google.js +6 -6
- package/dist/providers/index.d.ts +5 -2
- package/dist/providers/index.js +5 -2
- package/dist/providers/model-catalog.d.ts +4 -9
- package/dist/providers/model-catalog.js +3 -11
- package/dist/providers/oauth-callback-server.js +93 -87
- package/dist/providers/oauth-provider.d.ts +1 -1
- package/dist/providers/oauth-provider.js +1 -1
- package/dist/providers/oauth-tokens.d.ts +5 -5
- package/dist/providers/oauth-tokens.js +8 -9
- package/dist/providers/ollama-chat.d.ts +13 -12
- package/dist/providers/ollama-chat.js +15 -24
- package/dist/providers/openai-chat-request.d.ts +34 -0
- package/dist/providers/openai-chat-request.js +152 -0
- package/dist/providers/openai-chat-stream.d.ts +48 -0
- package/dist/providers/openai-chat-stream.js +89 -0
- package/dist/providers/openai-chat-tool-results.d.ts +81 -0
- package/dist/providers/openai-chat-tool-results.js +124 -0
- package/dist/providers/openai-chat-types.d.ts +180 -0
- package/dist/providers/openai-chat-types.js +3 -0
- package/dist/providers/openai-chat.d.ts +5 -405
- package/dist/providers/openai-chat.js +9 -388
- package/dist/providers/pkce.d.ts +1 -1
- package/dist/providers/pkce.js +1 -1
- package/dist/providers/provider-url.d.ts +6 -0
- package/dist/providers/provider-url.js +9 -0
- package/dist/providers/response-text.d.ts +4 -0
- package/dist/providers/response-text.js +16 -0
- package/dist/providers/sse-decode.d.ts +4 -0
- package/dist/providers/sse-decode.js +11 -0
- package/dist/providers/token-params.d.ts +5 -5
- package/dist/providers/token-params.js +5 -5
- package/dist/providers/tool-loop-verdict.d.ts +1 -1
- package/dist/providers/tool-loop-verdict.js +1 -1
- package/dist/providers/tool-turn-types.d.ts +2 -1
- package/dist/providers/tool-turn.js +30 -50
- package/dist/providers/turn-end-guard.d.ts +4 -1
- package/dist/providers/turn-end-guard.js +1 -1
- package/dist/qoder-stream.d.ts +1 -1
- package/dist/qoder-stream.js +1 -1
- package/dist/registry.d.ts +5 -5
- package/dist/registry.js +4 -4
- package/dist/role-marker-guard.d.ts +13 -1
- package/dist/role-marker-guard.js +15 -1
- package/dist/runtime-def-binding.d.ts +15 -0
- package/dist/runtime-def-binding.js +19 -0
- package/dist/runtime-port-bindings.d.ts +181 -0
- package/dist/runtime-port-bindings.js +95 -0
- package/dist/runtime-state.d.ts +2 -11
- package/dist/runtime-state.js +0 -13
- package/dist/types.d.ts +1 -454
- package/package.json +27 -15
- package/dist/ai-trace/capture.d.ts +0 -17
- package/dist/ai-trace/capture.js +0 -43
- package/dist/ai-trace/classify.d.ts +0 -12
- package/dist/ai-trace/classify.js +0 -16
- package/dist/ai-trace/index.d.ts +0 -13
- package/dist/ai-trace/index.js +0 -6
- package/dist/ai-trace/recorder.d.ts +0 -64
- package/dist/ai-trace/recorder.js +0 -258
- package/dist/ai-trace/sinks.d.ts +0 -51
- package/dist/ai-trace/sinks.js +0 -153
- package/dist/ai-trace/types.d.ts +0 -123
- package/dist/ai-trace/types.js +0 -17
- package/dist/ai-trace/usage.d.ts +0 -13
- package/dist/ai-trace/usage.js +0 -45
- package/dist/copilot-stream.d.ts +0 -65
- package/dist/copilot-stream.js +0 -131
- package/dist/defs/antigravity.d.ts +0 -94
- package/dist/defs/antigravity.js +0 -320
- package/dist/defs/copilot.d.ts +0 -34
- package/dist/defs/copilot.js +0 -96
- package/dist/defs/gemini.d.ts +0 -24
- package/dist/defs/gemini.js +0 -81
- package/dist/model-contracts.d.ts +0 -29
- package/dist/model-contracts.js +0 -2
- package/dist/providers/types.d.ts +0 -36
- package/dist/providers/types.js +0 -13
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
/** @module agent-protocol/acp/session-run
|
|
2
|
+
* Per-session run context and lifecycle helpers for `attachAcpSession`.
|
|
3
|
+
* Holds the mutable session flags (finished/fatal/aborted, stage watchdog,
|
|
4
|
+
* suppression diagnostics, pending permission requests) as one `AcpRun`
|
|
5
|
+
* object so each concern is a small function instead of a closure inside one
|
|
6
|
+
* giant function. Only types are imported from `./session.js`, so there is no
|
|
7
|
+
* runtime import cycle.
|
|
8
|
+
*/
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { createToolCallTextSuppressor, } from './text-suppression.js';
|
|
11
|
+
import { ACP_RAW_EVENT_SHAPE_DIAGNOSTIC_LIMIT, ACP_PROTOCOL_VERSION, AMR_STDERR_RETRY_TAIL_LIMIT, } from './constants.js';
|
|
12
|
+
import { errorMessage, asObject } from './json.js';
|
|
13
|
+
import { sendRpc, sendRpcResult, formatUsage, } from './rpc.js';
|
|
14
|
+
import { acpRawEventShape, promotedAmrStderrPayload } from './updates.js';
|
|
15
|
+
import { buildPromptBlocks } from './session-params.js';
|
|
16
|
+
import { createAcpSessionState } from './session-state.js';
|
|
17
|
+
/** `source` tag on every diagnostic this session reports about the ACP JSON-RPC stream. */
|
|
18
|
+
const ACP_JSON_RPC_SOURCE = 'acp-json-rpc';
|
|
19
|
+
function emptySuppressionSummary() {
|
|
20
|
+
return { suppressedChars: 0, suppressedChunks: 0, openedBlocks: 0, closedBlocks: 0 };
|
|
21
|
+
}
|
|
22
|
+
/** Builds the run context, rejecting a child without piped stdin/stdout. */
|
|
23
|
+
export function createAcpRun({ options, resolved }, _optional = {}) {
|
|
24
|
+
const { child, cwd, resumeSessionId } = options;
|
|
25
|
+
if (!child.stdin || !child.stdout) {
|
|
26
|
+
const error = new Error('ACP child process must expose stdin and stdout streams');
|
|
27
|
+
if (resumeSessionId)
|
|
28
|
+
Object.assign(error, {
|
|
29
|
+
details: { kind: 'resume_failed', sessionId: resumeSessionId },
|
|
30
|
+
});
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
options, resolved, child, stdin: child.stdin,
|
|
35
|
+
state: createAcpSessionState(),
|
|
36
|
+
runStartedAt: Date.now(),
|
|
37
|
+
effectiveCwd: path.resolve(cwd || process.cwd()),
|
|
38
|
+
emittedFirstTokenStatus: false,
|
|
39
|
+
rawAcpShapeDiagnosticCount: 0,
|
|
40
|
+
artifactSuppressionDiagnosticCount: 0,
|
|
41
|
+
amrStderrRetryTail: '',
|
|
42
|
+
finished: false,
|
|
43
|
+
fatal: false,
|
|
44
|
+
aborted: false,
|
|
45
|
+
stageTimer: null,
|
|
46
|
+
pendingPermissionRequests: new Map(),
|
|
47
|
+
toolCallTextSuppressor: createToolCallTextSuppressor(),
|
|
48
|
+
toolCallTextSuppressorLastSuppressedChars: 0,
|
|
49
|
+
artifactTextSuppressionSummary: emptySuppressionSummary(),
|
|
50
|
+
toolCallTextSuppressionSummary: emptySuppressionSummary(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function sendAgent({ run, payload }, _optional = {}) {
|
|
54
|
+
run.options.send({ event: 'agent', payload });
|
|
55
|
+
}
|
|
56
|
+
export function clearStageTimer({ run }, _optional = {}) {
|
|
57
|
+
if (run.stageTimer)
|
|
58
|
+
clearTimeout(run.stageTimer);
|
|
59
|
+
run.stageTimer = null;
|
|
60
|
+
}
|
|
61
|
+
export function resetStageTimer({ run, label }, _optional = {}) {
|
|
62
|
+
if (run.stageTimer)
|
|
63
|
+
clearTimeout(run.stageTimer);
|
|
64
|
+
const { stageTimeoutMs } = run.resolved;
|
|
65
|
+
// `stageTimeoutMs <= 0` disables the watchdog. Mirrors a typical outer
|
|
66
|
+
// run-level inactivity watchdog's escape hatch. Without this, a caller
|
|
67
|
+
// passing a zero stage timeout would schedule a 0ms timeout that fires on
|
|
68
|
+
// the next tick and kills the session immediately.
|
|
69
|
+
if (stageTimeoutMs <= 0 || run.pendingPermissionRequests.size > 0)
|
|
70
|
+
return;
|
|
71
|
+
run.stageTimer = setTimeout(() => {
|
|
72
|
+
failRun({ run, message: `ACP ${label} timed out after ${stageTimeoutMs}ms` });
|
|
73
|
+
}, stageTimeoutMs);
|
|
74
|
+
}
|
|
75
|
+
function amrModelUnavailablePayload(message) {
|
|
76
|
+
return {
|
|
77
|
+
message,
|
|
78
|
+
error: {
|
|
79
|
+
code: 'AMR_MODEL_UNAVAILABLE',
|
|
80
|
+
message,
|
|
81
|
+
retryable: false,
|
|
82
|
+
details: { kind: 'amr_model', action: 'choose_model' },
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function isModelUnavailableError(message) {
|
|
87
|
+
const value = message.toLowerCase();
|
|
88
|
+
return (value.includes('model not found') ||
|
|
89
|
+
value.includes('providermodelnotfounderror') ||
|
|
90
|
+
value.includes('unknown model') ||
|
|
91
|
+
value.includes('invalid model'));
|
|
92
|
+
}
|
|
93
|
+
/** Emits a failure, tagging an unacknowledged resume without losing its cause.
|
|
94
|
+
* The session id is only set by a valid load acknowledgement, so the same
|
|
95
|
+
* boundary covers initialize/load RPC errors, watchdogs, and transport loss.
|
|
96
|
+
* Fresh sessions and errors after load keep their existing payloads.
|
|
97
|
+
* @complexity O(1), excluding the host's event sink.
|
|
98
|
+
*/
|
|
99
|
+
function sendFailure(run, payload) {
|
|
100
|
+
const { resumeSessionId, send } = run.options;
|
|
101
|
+
if (!resumeSessionId || run.state.sessionId) {
|
|
102
|
+
send({ event: 'error', payload });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const originalError = asObject({ value: payload.error });
|
|
106
|
+
send({ event: 'error', payload: {
|
|
107
|
+
...payload,
|
|
108
|
+
error: {
|
|
109
|
+
code: 'AGENT_EXECUTION_FAILED',
|
|
110
|
+
message: payload.message,
|
|
111
|
+
retryable: false,
|
|
112
|
+
...originalError,
|
|
113
|
+
details: {
|
|
114
|
+
kind: 'resume_failed',
|
|
115
|
+
sessionId: resumeSessionId,
|
|
116
|
+
...(originalError?.details === undefined ? {} : { cause: originalError.details }),
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
} });
|
|
120
|
+
}
|
|
121
|
+
// Both call sites of `failWithPayload` (the session/update retry-status promotion and the
|
|
122
|
+
// stderr promotion) are already nested inside a scope gated by `finished`
|
|
123
|
+
// at their own entry point (the parser callback's own `if (aborted ||
|
|
124
|
+
// finished) return;`, and the stderr handler's `if (!modelUnavailableErrorCode
|
|
125
|
+
// || finished) return;`), and neither is re-entrant within a single
|
|
126
|
+
// synchronous call — so `failWithPayload` can never actually be invoked a
|
|
127
|
+
// second time once `finished` is true. The origin file carried its own
|
|
128
|
+
// `if (finished) return;` re-entry guard here anyway; removed per this
|
|
129
|
+
// package's coverage-driven dead-branch discipline (verified unreachable
|
|
130
|
+
// by tracing every call site — see archived provenance ledger), not suppressed.
|
|
131
|
+
export function failWithPayload({ run, payload }, _optional = {}) {
|
|
132
|
+
run.finished = true;
|
|
133
|
+
run.fatal = true;
|
|
134
|
+
clearStageTimer({ run });
|
|
135
|
+
sendFailure(run, payload);
|
|
136
|
+
releaseChild({ run, release: () => { if (!run.child.killed)
|
|
137
|
+
run.child.kill('SIGTERM'); } });
|
|
138
|
+
}
|
|
139
|
+
function releaseChild({ run, release }) {
|
|
140
|
+
if (!run.options.beforeRelease)
|
|
141
|
+
release();
|
|
142
|
+
else
|
|
143
|
+
void run.options.beforeRelease().then(release, release);
|
|
144
|
+
}
|
|
145
|
+
function buildFailurePayload({ message, options }) {
|
|
146
|
+
const bareFailure = options.details === undefined && options.retryable === undefined;
|
|
147
|
+
if (bareFailure)
|
|
148
|
+
return { message };
|
|
149
|
+
return {
|
|
150
|
+
message,
|
|
151
|
+
error: {
|
|
152
|
+
code: 'AGENT_EXECUTION_FAILED',
|
|
153
|
+
message,
|
|
154
|
+
retryable: options.retryable ?? false,
|
|
155
|
+
// Every real call site that reaches this branch (the
|
|
156
|
+
// RPC-error path and the no-visible-output path) always
|
|
157
|
+
// supplies `details` whenever it supplies `retryable`, and
|
|
158
|
+
// `retryable` is itself derived from the same `details`
|
|
159
|
+
// source at the RPC-error call site — so `options.details`
|
|
160
|
+
// is never actually undefined here. Spreading it
|
|
161
|
+
// unconditionally (rather than conditionally omitting the
|
|
162
|
+
// key) is behaviorally identical for every real caller;
|
|
163
|
+
// simplified per this package's coverage-driven
|
|
164
|
+
// dead-branch discipline. See archived provenance ledger.
|
|
165
|
+
details: options.details,
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
export function failRun({ run, message }, options = {}) {
|
|
170
|
+
if (run.finished)
|
|
171
|
+
return;
|
|
172
|
+
run.finished = true;
|
|
173
|
+
run.fatal = true;
|
|
174
|
+
clearStageTimer({ run });
|
|
175
|
+
const useModelUnavailable = run.options.modelUnavailableErrorCode &&
|
|
176
|
+
(options.forceModelUnavailable || isModelUnavailableError(message));
|
|
177
|
+
sendFailure(run, useModelUnavailable
|
|
178
|
+
? amrModelUnavailablePayload(message)
|
|
179
|
+
: buildFailurePayload({ message, options }));
|
|
180
|
+
releaseChild({ run, release: () => { if (!run.child.killed)
|
|
181
|
+
run.child.kill('SIGTERM'); } });
|
|
182
|
+
}
|
|
183
|
+
export function writeRpc({ run, ...rpc }, _optional = {}) {
|
|
184
|
+
resetStageTimer({ run, label: rpc.timeoutLabel });
|
|
185
|
+
try {
|
|
186
|
+
sendRpc({ writable: run.stdin, id: rpc.id, method: rpc.method, params: rpc.params });
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
failRun({ run, message: `stdin write failed: ${errorMessage({ err })}` });
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function emitAcpRawShapeDiagnostic(run, update) {
|
|
193
|
+
if (!run.options.modelUnavailableErrorCode)
|
|
194
|
+
return;
|
|
195
|
+
if (run.rawAcpShapeDiagnosticCount >= ACP_RAW_EVENT_SHAPE_DIAGNOSTIC_LIMIT)
|
|
196
|
+
return;
|
|
197
|
+
run.rawAcpShapeDiagnosticCount += 1;
|
|
198
|
+
sendAgent({ run,
|
|
199
|
+
payload: {
|
|
200
|
+
type: 'diagnostic',
|
|
201
|
+
name: 'acp_raw_event_shape',
|
|
202
|
+
source: ACP_JSON_RPC_SOURCE,
|
|
203
|
+
elapsedMs: Date.now() - run.runStartedAt,
|
|
204
|
+
shape: acpRawEventShape({ update }),
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
// All three call sites guard against an empty `delta` before calling this
|
|
209
|
+
// (the finishCleanPrompt flush, the `if (outputDelta)` branch, and the
|
|
210
|
+
// plain-else branch which is only reached after an earlier `if
|
|
211
|
+
// (!toolCallStrippedDelta) return;` already ruled out emptiness) — so the
|
|
212
|
+
// origin file's own `if (!delta) return;` guard here can never actually
|
|
213
|
+
// fire. Removed per this package's coverage-driven dead-branch
|
|
214
|
+
// discipline (verified unreachable by tracing every call site — see
|
|
215
|
+
// archived provenance ledger), not suppressed.
|
|
216
|
+
function emitVisibleTextDelta(run, { delta }) {
|
|
217
|
+
run.state.emittedVisibleTextChunk = true;
|
|
218
|
+
if (!run.emittedFirstTokenStatus) {
|
|
219
|
+
run.emittedFirstTokenStatus = true;
|
|
220
|
+
sendAgent({ run,
|
|
221
|
+
payload: {
|
|
222
|
+
type: 'status',
|
|
223
|
+
label: 'streaming',
|
|
224
|
+
ttftMs: Date.now() - run.runStartedAt,
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
sendAgent({ run, payload: { type: 'text_delta', delta } });
|
|
229
|
+
}
|
|
230
|
+
function applySuppressionStats({ summary, stats, delta }) {
|
|
231
|
+
summary.suppressedChars += delta;
|
|
232
|
+
summary.suppressedChunks = stats.suppressedChunks;
|
|
233
|
+
summary.openedBlocks = stats.openedBlocks;
|
|
234
|
+
summary.closedBlocks = stats.closedBlocks;
|
|
235
|
+
}
|
|
236
|
+
function sendSuppressionDiagnostic(run, diagnostic) {
|
|
237
|
+
if (run.artifactSuppressionDiagnosticCount >= ACP_RAW_EVENT_SHAPE_DIAGNOSTIC_LIMIT)
|
|
238
|
+
return;
|
|
239
|
+
run.artifactSuppressionDiagnosticCount += 1;
|
|
240
|
+
const { summary, stats } = diagnostic;
|
|
241
|
+
sendAgent({ run,
|
|
242
|
+
payload: {
|
|
243
|
+
type: 'diagnostic',
|
|
244
|
+
name: diagnostic.name,
|
|
245
|
+
source: ACP_JSON_RPC_SOURCE,
|
|
246
|
+
elapsedMs: Date.now() - run.runStartedAt,
|
|
247
|
+
reason: diagnostic.reason,
|
|
248
|
+
suppressedChars: summary.suppressedChars,
|
|
249
|
+
suppressedChunks: summary.suppressedChunks,
|
|
250
|
+
openedBlocks: summary.openedBlocks,
|
|
251
|
+
closedBlocks: summary.closedBlocks,
|
|
252
|
+
pendingCandidateChars: stats.pendingCandidateChars,
|
|
253
|
+
suppressing: stats.suppressing,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
function noteArtifactTextSuppression(run, { reason }) {
|
|
258
|
+
const { state } = run;
|
|
259
|
+
if (!state.dsmlArtifactSuppressor)
|
|
260
|
+
return;
|
|
261
|
+
const stats = state.dsmlArtifactSuppressor.stats();
|
|
262
|
+
const suppressedDelta = stats.suppressedChars - state.dsmlArtifactSuppressorLastSuppressedChars;
|
|
263
|
+
if (suppressedDelta <= 0)
|
|
264
|
+
return;
|
|
265
|
+
state.dsmlArtifactSuppressorLastSuppressedChars = stats.suppressedChars;
|
|
266
|
+
applySuppressionStats({ summary: run.artifactTextSuppressionSummary, stats, delta: suppressedDelta });
|
|
267
|
+
sendSuppressionDiagnostic(run, {
|
|
268
|
+
name: 'acp_artifact_text_suppression', reason, summary: run.artifactTextSuppressionSummary, stats,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
function emitArtifactTextSuppressionSummary(run) {
|
|
272
|
+
const summary = run.artifactTextSuppressionSummary;
|
|
273
|
+
if (summary.suppressedChars <= 0)
|
|
274
|
+
return;
|
|
275
|
+
if (run.resolved.executionProfile === 'filesystem') {
|
|
276
|
+
sendAgent({ run,
|
|
277
|
+
payload: {
|
|
278
|
+
type: 'diagnostic',
|
|
279
|
+
name: 'unexpected_text_artifact_in_filesystem_run',
|
|
280
|
+
source: ACP_JSON_RPC_SOURCE,
|
|
281
|
+
elapsedMs: Date.now() - run.runStartedAt,
|
|
282
|
+
suppressedChars: summary.suppressedChars,
|
|
283
|
+
suppressedChunks: summary.suppressedChunks,
|
|
284
|
+
openedBlocks: summary.openedBlocks,
|
|
285
|
+
closedBlocks: summary.closedBlocks,
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
sendAgent({ run,
|
|
290
|
+
payload: {
|
|
291
|
+
type: 'diagnostic',
|
|
292
|
+
name: 'acp_artifact_text_suppression_summary',
|
|
293
|
+
source: ACP_JSON_RPC_SOURCE,
|
|
294
|
+
elapsedMs: Date.now() - run.runStartedAt,
|
|
295
|
+
...summary,
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function noteToolCallTextSuppression(run, { reason }) {
|
|
300
|
+
const stats = run.toolCallTextSuppressor.stats();
|
|
301
|
+
const suppressedDelta = stats.suppressedChars - run.toolCallTextSuppressorLastSuppressedChars;
|
|
302
|
+
if (suppressedDelta <= 0)
|
|
303
|
+
return;
|
|
304
|
+
run.toolCallTextSuppressorLastSuppressedChars = stats.suppressedChars;
|
|
305
|
+
applySuppressionStats({ summary: run.toolCallTextSuppressionSummary, stats, delta: suppressedDelta });
|
|
306
|
+
sendSuppressionDiagnostic(run, {
|
|
307
|
+
name: 'acp_tool_call_text_suppression', reason, summary: run.toolCallTextSuppressionSummary, stats,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
function emitToolCallTextSuppressionSummary(run) {
|
|
311
|
+
if (run.toolCallTextSuppressionSummary.suppressedChars <= 0)
|
|
312
|
+
return;
|
|
313
|
+
sendAgent({ run,
|
|
314
|
+
payload: {
|
|
315
|
+
type: 'diagnostic',
|
|
316
|
+
name: 'acp_tool_call_text_suppression_summary',
|
|
317
|
+
source: ACP_JSON_RPC_SOURCE,
|
|
318
|
+
elapsedMs: Date.now() - run.runStartedAt,
|
|
319
|
+
...run.toolCallTextSuppressionSummary,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
function sendPrompt(run) {
|
|
324
|
+
const { state } = run;
|
|
325
|
+
state.promptRequestId = state.nextId;
|
|
326
|
+
state.expectedId = state.promptRequestId;
|
|
327
|
+
writeRpc({ run,
|
|
328
|
+
id: state.promptRequestId,
|
|
329
|
+
method: 'session/prompt',
|
|
330
|
+
params: {
|
|
331
|
+
sessionId: state.sessionId,
|
|
332
|
+
prompt: buildPromptBlocks({ prompt: run.options.prompt, imagePaths: run.resolved.imagePaths }),
|
|
333
|
+
},
|
|
334
|
+
timeoutLabel: 'session/prompt',
|
|
335
|
+
});
|
|
336
|
+
sendAgent({ run,
|
|
337
|
+
payload: {
|
|
338
|
+
type: 'status',
|
|
339
|
+
label: 'waiting_for_first_output',
|
|
340
|
+
elapsedMs: Date.now() - run.runStartedAt,
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
state.nextId += 1;
|
|
344
|
+
}
|
|
345
|
+
function flushPendingVisibleText(run) {
|
|
346
|
+
const flushedToolText = run.toolCallTextSuppressor.flush();
|
|
347
|
+
noteToolCallTextSuppression(run, { reason: 'tool_call_xml_flush' });
|
|
348
|
+
const flushedText = flushedToolText
|
|
349
|
+
? (run.state.dsmlArtifactSuppressor?.strip(flushedToolText) ?? flushedToolText)
|
|
350
|
+
: '';
|
|
351
|
+
if (flushedText) {
|
|
352
|
+
emitVisibleTextDelta(run, { delta: flushedText });
|
|
353
|
+
}
|
|
354
|
+
noteArtifactTextSuppression(run, { reason: 'artifact_flush' });
|
|
355
|
+
emitToolCallTextSuppressionSummary(run);
|
|
356
|
+
emitArtifactTextSuppressionSummary(run);
|
|
357
|
+
}
|
|
358
|
+
// `finishCleanPrompt` has exactly one call site (`handlePromptResult`,
|
|
359
|
+
// called only from `routeResultById`'s promptRequestId-match branch),
|
|
360
|
+
// which is itself only ever reached from the parser callback's own `if
|
|
361
|
+
// (aborted || finished) return;` guard at entry — so this function can
|
|
362
|
+
// never actually be invoked once `finished` is true. The origin file
|
|
363
|
+
// carried its own re-entry guard here anyway; removed per this package's
|
|
364
|
+
// coverage-driven dead-branch discipline. See archived provenance ledger.
|
|
365
|
+
function finishCleanPrompt(run, { usageSource } = {}) {
|
|
366
|
+
flushPendingVisibleText(run);
|
|
367
|
+
const usage = formatUsage({ usage: usageSource });
|
|
368
|
+
if (usage) {
|
|
369
|
+
sendAgent({ run,
|
|
370
|
+
payload: {
|
|
371
|
+
type: 'usage',
|
|
372
|
+
usage,
|
|
373
|
+
durationMs: Date.now() - run.runStartedAt,
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
run.finished = true;
|
|
378
|
+
clearStageTimer({ run });
|
|
379
|
+
releaseChild({ run, release: () => {
|
|
380
|
+
run.stdin.end();
|
|
381
|
+
// Some ACP agents keep the child process alive after stdin closes,
|
|
382
|
+
// waiting for another prompt. Each run owns one process per turn, so
|
|
383
|
+
// close it once this prompt is cleanly complete.
|
|
384
|
+
const { child } = run;
|
|
385
|
+
const cleanExitTimer = setTimeout(() => {
|
|
386
|
+
if (!child.killed)
|
|
387
|
+
child.kill('SIGTERM');
|
|
388
|
+
}, 500);
|
|
389
|
+
child.once('close', () => clearTimeout(cleanExitTimer));
|
|
390
|
+
} });
|
|
391
|
+
}
|
|
392
|
+
function recoverFromModelSelectionError(run) {
|
|
393
|
+
const { state } = run;
|
|
394
|
+
state.setModelRequestId = null;
|
|
395
|
+
state.activeModel = state.activeModel || 'default';
|
|
396
|
+
sendAgent({ run, payload: { type: 'status', label: 'model', model: state.activeModel } });
|
|
397
|
+
sendPrompt(run);
|
|
398
|
+
}
|
|
399
|
+
/** Binds the run to the side-effect bundle the exported ACP handlers consume. */
|
|
400
|
+
export function buildAcpEffects({ run }, _optional = {}) {
|
|
401
|
+
const { options, resolved } = run;
|
|
402
|
+
return {
|
|
403
|
+
send: options.send,
|
|
404
|
+
fail: (message, failOptions) => failRun({ run, message }, failOptions),
|
|
405
|
+
failWithPayload: (payload) => failWithPayload({ run, payload }),
|
|
406
|
+
writeRpc: (id, method, params, timeoutLabel) => writeRpc({ run, id, method, params, timeoutLabel }),
|
|
407
|
+
sendPrompt: () => sendPrompt(run),
|
|
408
|
+
recoverFromModelSelectionError: () => recoverFromModelSelectionError(run),
|
|
409
|
+
finishCleanPrompt: (usageSource) => finishCleanPrompt(run, { usageSource }),
|
|
410
|
+
emitVisibleTextDelta: (delta) => emitVisibleTextDelta(run, { delta }),
|
|
411
|
+
noteArtifactTextSuppression: (reason) => noteArtifactTextSuppression(run, { reason }),
|
|
412
|
+
noteToolCallTextSuppression: (reason) => noteToolCallTextSuppression(run, { reason }),
|
|
413
|
+
emitAcpRawShapeDiagnostic: (update) => emitAcpRawShapeDiagnostic(run, update),
|
|
414
|
+
toolCallTextSuppressor: run.toolCallTextSuppressor,
|
|
415
|
+
runStartedAt: run.runStartedAt,
|
|
416
|
+
modelUnavailableErrorCode: options.modelUnavailableErrorCode,
|
|
417
|
+
accountFailureClassifier: resolved.accountFailureClassifier,
|
|
418
|
+
model: options.model,
|
|
419
|
+
effort: options.effort,
|
|
420
|
+
onSessionInit: options.onSessionInit,
|
|
421
|
+
resumeSessionId: options.resumeSessionId,
|
|
422
|
+
effectiveCwd: run.effectiveCwd,
|
|
423
|
+
mcpServers: options.mcpServers,
|
|
424
|
+
envFormat: resolved.envFormat,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function handleStderrChunk(run, { chunk }) {
|
|
428
|
+
if (!run.options.modelUnavailableErrorCode || run.finished)
|
|
429
|
+
return;
|
|
430
|
+
run.amrStderrRetryTail = `${run.amrStderrRetryTail}${String(chunk)}`.slice(-AMR_STDERR_RETRY_TAIL_LIMIT);
|
|
431
|
+
const promotedPayload = promotedAmrStderrPayload({ chunk: run.amrStderrRetryTail }, { classifier: run.resolved.accountFailureClassifier });
|
|
432
|
+
if (promotedPayload)
|
|
433
|
+
failWithPayload({ run, payload: promotedPayload });
|
|
434
|
+
}
|
|
435
|
+
function handleChildClose(run, { parser, exit }) {
|
|
436
|
+
clearStageTimer({ run });
|
|
437
|
+
parser.flush();
|
|
438
|
+
if (!run.finished && !run.aborted && !run.fatal) {
|
|
439
|
+
failRun({
|
|
440
|
+
run,
|
|
441
|
+
message: `ACP session exited before completion (code=${exit.code ?? 'null'}, signal=${exit.signal ?? 'none'})`,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
/** Connects the child's streams and lifecycle events to the parser and failure paths. */
|
|
446
|
+
export function wireAcpProcessListeners({ run, parser }, _optional = {}) {
|
|
447
|
+
const { child, stdin } = run;
|
|
448
|
+
child.stdout?.on('data', (chunk) => parser.feed({ chunk: chunk }));
|
|
449
|
+
child.stderr?.setEncoding('utf8');
|
|
450
|
+
child.stderr?.on('data', (chunk) => handleStderrChunk(run, { chunk }));
|
|
451
|
+
child.on('close', (code, signal) => handleChildClose(run, { parser, exit: { code, signal } }));
|
|
452
|
+
child.on('error', (err) => failRun({ run, message: err.message }));
|
|
453
|
+
stdin.on('error', (err) => failRun({ run, message: `stdin error: ${err.message}` }));
|
|
454
|
+
}
|
|
455
|
+
/** Sends the `initialize` request that opens the ACP handshake. */
|
|
456
|
+
export function sendInitialize({ run }, _optional = {}) {
|
|
457
|
+
writeRpc({ run,
|
|
458
|
+
id: 1,
|
|
459
|
+
method: 'initialize',
|
|
460
|
+
params: {
|
|
461
|
+
protocolVersion: ACP_PROTOCOL_VERSION,
|
|
462
|
+
clientCapabilities: { terminal: false },
|
|
463
|
+
clientInfo: { name: run.resolved.clientName, version: run.resolved.clientVersion },
|
|
464
|
+
},
|
|
465
|
+
timeoutLabel: 'initialize',
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
// ACP requires pending permission requests to receive a terminal
|
|
469
|
+
// `cancelled` response when the prompt turn is cancelled. Called before
|
|
470
|
+
// closing stdin, which may otherwise make the response impossible to
|
|
471
|
+
// deliver.
|
|
472
|
+
function cancelPendingPermissionRequests(run) {
|
|
473
|
+
for (const [requestId, logCancellation] of run.pendingPermissionRequests) {
|
|
474
|
+
try {
|
|
475
|
+
sendRpcResult({ writable: run.stdin, id: requestId, result: { outcome: { outcome: 'cancelled' } } });
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
// Closing stdin/process signalling below remains the fallback.
|
|
479
|
+
}
|
|
480
|
+
logCancellation();
|
|
481
|
+
}
|
|
482
|
+
run.pendingPermissionRequests.clear();
|
|
483
|
+
}
|
|
484
|
+
// Only cancel an established session; before session/new resolves there is
|
|
485
|
+
// no sessionId to cancel, but the caller still closes stdin below.
|
|
486
|
+
function sendSessionCancelIfEstablished(run, activeStdin) {
|
|
487
|
+
const { state } = run;
|
|
488
|
+
if (!state.sessionId)
|
|
489
|
+
return;
|
|
490
|
+
try {
|
|
491
|
+
activeStdin.write(`${JSON.stringify({ jsonrpc: '2.0', method: 'session/cancel', params: { sessionId: state.sessionId } })}\n`);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
// The caller owns process-signal fallback if the ACP transport is gone.
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
function abortRun(run) {
|
|
498
|
+
if (run.aborted || run.finished)
|
|
499
|
+
return;
|
|
500
|
+
run.aborted = true;
|
|
501
|
+
run.finished = true;
|
|
502
|
+
clearStageTimer({ run });
|
|
503
|
+
cancelPendingPermissionRequests(run);
|
|
504
|
+
const { stdin } = run.child;
|
|
505
|
+
if (!stdin || stdin.destroyed || stdin.writableEnded)
|
|
506
|
+
return;
|
|
507
|
+
sendSessionCancelIfEstablished(run, stdin);
|
|
508
|
+
// Always close stdin so the agent receives EOF and shuts down its own
|
|
509
|
+
// runtime — some ACP bridges tear down a private backing server on EOF —
|
|
510
|
+
// instead of lingering (and leaking that server) until the caller's
|
|
511
|
+
// SIGTERM fallback fires. This also covers aborts during ACP startup,
|
|
512
|
+
// before session/new returns. Mirrors the clean-completion path above.
|
|
513
|
+
releaseChild({ run, release: () => {
|
|
514
|
+
try {
|
|
515
|
+
stdin.end();
|
|
516
|
+
}
|
|
517
|
+
catch {
|
|
518
|
+
// Best effort; the caller still owns the SIGTERM/SIGKILL fallback.
|
|
519
|
+
}
|
|
520
|
+
} });
|
|
521
|
+
}
|
|
522
|
+
/** The public handle `attachAcpSession` returns for one run. */
|
|
523
|
+
export function buildAcpController({ run }, _optional = {}) {
|
|
524
|
+
return {
|
|
525
|
+
/** Returns `true` when the session ended with a fatal protocol or transport error, allowing the caller to surface the failure. */
|
|
526
|
+
hasFatalError() {
|
|
527
|
+
return run.fatal;
|
|
528
|
+
},
|
|
529
|
+
// The durable upstream session handle to persist for resume, or null when
|
|
530
|
+
// none was reported (older agents, or a handshake that never established a
|
|
531
|
+
// session). Mirrors pi-rpc's getLastSessionPath().
|
|
532
|
+
/** Returns the durable upstream session id (e.g. a vendor bridge's own session id) to persist for next-turn resume, or `null` when the agent did not report one. */
|
|
533
|
+
getDurableSessionId() {
|
|
534
|
+
return run.state.durableSessionId;
|
|
535
|
+
},
|
|
536
|
+
/** Returns `true` when the prompt request resolved cleanly without a fatal error and without an abort, even if the child process later exited via SIGTERM. */
|
|
537
|
+
completedSuccessfully() {
|
|
538
|
+
// Returns true when the prompt request resolved without a fatal error
|
|
539
|
+
// and was not aborted. The chat consumer treats this as a successful
|
|
540
|
+
// run even if the child process subsequently exited via SIGTERM
|
|
541
|
+
// (which is expected for agents that don't shut down on stdin.end()).
|
|
542
|
+
return run.finished && !run.fatal && !run.aborted;
|
|
543
|
+
},
|
|
544
|
+
/**
|
|
545
|
+
* Aborts an in-progress ACP session. Sends `session/cancel` when a session
|
|
546
|
+
* id has already been established, then always closes stdin so the agent
|
|
547
|
+
* receives EOF and can tear down its own runtime (e.g. a vendor bridge's
|
|
548
|
+
* private backing server). Idempotent — subsequent calls are no-ops.
|
|
549
|
+
*/
|
|
550
|
+
abort() {
|
|
551
|
+
abortRun(run);
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
//# sourceMappingURL=session-run.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** @module agent-protocol/acp/session-state
|
|
2
|
+
* The initial value of an ACP session's mutable state. A leaf module: `session-run.ts` builds a run
|
|
3
|
+
* from it, and `session.ts` imports `session-run.ts`, so the factory cannot live in `session.ts`
|
|
4
|
+
* without a runtime import cycle. The `AcpSessionState` shape stays in `session.ts` (type-only import).
|
|
5
|
+
*/
|
|
6
|
+
import type { AcpSessionState } from './session-types.js';
|
|
7
|
+
/** Builds a fresh {@link AcpSessionState} with every field at its initial value. */
|
|
8
|
+
export declare function createAcpSessionState(): AcpSessionState;
|
|
9
|
+
//# sourceMappingURL=session-state.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Builds a fresh {@link AcpSessionState} with every field at its initial value. */
|
|
2
|
+
export function createAcpSessionState() {
|
|
3
|
+
return {
|
|
4
|
+
expectedId: 1,
|
|
5
|
+
nextId: 2,
|
|
6
|
+
promptRequestId: null,
|
|
7
|
+
setModelRequestId: null,
|
|
8
|
+
effortRequestId: null,
|
|
9
|
+
configOptions: null,
|
|
10
|
+
sessionId: null,
|
|
11
|
+
durableSessionId: null,
|
|
12
|
+
activeModel: null,
|
|
13
|
+
modelConfigId: null,
|
|
14
|
+
emittedThinkingStart: false,
|
|
15
|
+
emittedTextChunk: false,
|
|
16
|
+
emittedVisibleTextChunk: false,
|
|
17
|
+
emittedTextBuffer: '',
|
|
18
|
+
emittedToolCall: false,
|
|
19
|
+
emittedConcreteToolEvent: false,
|
|
20
|
+
dsmlArtifactSuppressor: null,
|
|
21
|
+
dsmlArtifactSuppressorLastSuppressedChars: 0,
|
|
22
|
+
dsmlArtifactSuppressorToolCallId: null,
|
|
23
|
+
dsmlArtifactSuppressorArmedAfterText: false,
|
|
24
|
+
dsmlArtifactSuppressorSawIncrementalProse: false,
|
|
25
|
+
acpArtifactWriteToolCallIds: new Set(),
|
|
26
|
+
acpArtifactRunEventState: new Map(),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=session-state.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type ArtifactTextSuppressor } from './text-suppression.js';
|
|
2
|
+
import type { UnknownRecord } from './types.js';
|
|
3
|
+
import { type AcpSessionState, type AcpSessionEffects } from './session-types.js';
|
|
4
|
+
/** Translates an `agent_thought_chunk` update into `thinking_start` (once) / `thinking_delta` events. */
|
|
5
|
+
export declare function handleThoughtChunkUpdate({ state, effects, update, }: {
|
|
6
|
+
state: AcpSessionState;
|
|
7
|
+
effects: AcpSessionEffects;
|
|
8
|
+
update: UnknownRecord;
|
|
9
|
+
}, _optional?: Record<string, never>): void;
|
|
10
|
+
/** Strips tool-call XML from a raw message delta via the session's tool-call text suppressor, noting the suppression reason. */
|
|
11
|
+
export declare function stripToolCallText({ effects, delta }: {
|
|
12
|
+
effects: AcpSessionEffects;
|
|
13
|
+
delta: string;
|
|
14
|
+
}, _optional?: Record<string, never>): string;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a non-cumulative message delta should be emitted as-is (prose)
|
|
17
|
+
* rather than the DSML artifact suppressor's stripped output. True when this
|
|
18
|
+
* delta isn't itself part of an artifact echo/candidate, AND either nothing
|
|
19
|
+
* was actually stripped, or the suppressor was armed mid-stream (after
|
|
20
|
+
* visible text) and prior deltas already established this is ordinary prose
|
|
21
|
+
* rather than the start of another artifact echo.
|
|
22
|
+
*/
|
|
23
|
+
export declare function shouldPreserveIncrementalProse(args: {
|
|
24
|
+
isCumulativeSnapshot: boolean;
|
|
25
|
+
wasSuppressingArtifact: boolean;
|
|
26
|
+
hadPendingArtifactCandidate: boolean;
|
|
27
|
+
hasOpenArtifactCandidate: boolean;
|
|
28
|
+
strippedDelta: string;
|
|
29
|
+
toolCallStrippedDelta: string;
|
|
30
|
+
dsmlArtifactSuppressorArmedAfterText: boolean;
|
|
31
|
+
dsmlArtifactSuppressorSawIncrementalProse: boolean;
|
|
32
|
+
}, _optional?: Record<string, never>): boolean;
|
|
33
|
+
/** Whether a delta passed through the DSML suppressor completely unchanged and there is no artifact suppression in progress. */
|
|
34
|
+
export declare function isPlainIncrementalDelta(args: {
|
|
35
|
+
strippedDelta: string;
|
|
36
|
+
toolCallStrippedDelta: string;
|
|
37
|
+
wasSuppressingArtifact: boolean;
|
|
38
|
+
hadPendingArtifactCandidate: boolean;
|
|
39
|
+
hasOpenArtifactCandidate: boolean;
|
|
40
|
+
}, _optional?: Record<string, never>): boolean;
|
|
41
|
+
/** Runs a tool-call-stripped delta through the DSML artifact suppressor, emits whatever should stay visible, and arms/disarms the "saw incremental prose" and suppressor-cleared state. */
|
|
42
|
+
export declare function applyDsmlArtifactSuppression({ state, effects, suppressor, toolCallStrippedDelta, isCumulativeSnapshot, }: {
|
|
43
|
+
state: AcpSessionState;
|
|
44
|
+
effects: AcpSessionEffects;
|
|
45
|
+
suppressor: ArtifactTextSuppressor;
|
|
46
|
+
toolCallStrippedDelta: string;
|
|
47
|
+
delta: string;
|
|
48
|
+
isCumulativeSnapshot: boolean;
|
|
49
|
+
}, _optional?: Record<string, never>): void;
|
|
50
|
+
/** Translates an `agent_message_chunk` update into `text_delta` events, applying tool-call and DSML-artifact text suppression. */
|
|
51
|
+
export declare function handleMessageChunkUpdate({ state, effects, update, }: {
|
|
52
|
+
state: AcpSessionState;
|
|
53
|
+
effects: AcpSessionEffects;
|
|
54
|
+
update: UnknownRecord;
|
|
55
|
+
}, _optional?: Record<string, never>): void;
|
|
56
|
+
//# sourceMappingURL=session-text-updates.d.ts.map
|