@jini-ai/agent-runtime 0.7.2 → 0.7.3-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -3
- 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 +6 -1
- package/dist/agent-protocol/acp/constants.js +7 -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 +161 -128
- 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 +231 -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 +28 -16
- 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,480 @@
|
|
|
1
|
+
/** @module claude-stream-handler
|
|
2
|
+
* Stateful Claude stream-json handler behind `createClaudeStreamHandler`.
|
|
3
|
+
* One `StreamCtx` carries the per-stream scratch state (message ids, streamed
|
|
4
|
+
* channels, artifact-echo suppression, role-marker guards) and every handler is
|
|
5
|
+
* a small function over it. The pure frame helpers live in `./claude-stream-frames.js`.
|
|
6
|
+
*/
|
|
7
|
+
import { createBoundedLineStream } from './core/bounded-line-stream.js';
|
|
8
|
+
import { createRoleMarkerGuard } from './role-marker-guard.js';
|
|
9
|
+
import { createClaudeSegmentUsage } from './claude-segment-usage.js';
|
|
10
|
+
import { applyInputJsonDelta, artifactOpenCandidateLength, emitAssistantErrorIfPresent, emitCanonicalTaskSnapshot, fileWriteContent, handleSystemMessage, handleUserMessage, isFileWriteToolUse, isHtmlWriteToolInput, isRedundantWrittenArtifact, isUnstreamedTextBlock, isUnstreamedThinkingBlock, normalizeArtifactEchoContent, } from './claude-stream-frames.js';
|
|
11
|
+
function isRecord(value) {
|
|
12
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
function createStreamCtx({ onEvent, options }) {
|
|
15
|
+
const s = {
|
|
16
|
+
onEvent,
|
|
17
|
+
options,
|
|
18
|
+
lines: createBoundedLineStream({
|
|
19
|
+
onLine: line => { const trimmed = line.trim(); if (trimmed)
|
|
20
|
+
feedLine(s, { line: trimmed }); },
|
|
21
|
+
onOverflow: error => onEvent({ type: 'error', message: error.message, code: 'STREAM_LINE_TOO_LARGE' }),
|
|
22
|
+
}),
|
|
23
|
+
segmentUsage: createClaudeSegmentUsage({}, {
|
|
24
|
+
...(options.nowMs === undefined ? {} : { nowMs: options.nowMs }),
|
|
25
|
+
...(options.costBasis === undefined ? {} : { costBasis: options.costBasis }),
|
|
26
|
+
}),
|
|
27
|
+
blocks: new Map(),
|
|
28
|
+
streamedToolUseIds: new Set(),
|
|
29
|
+
currentMessageId: null,
|
|
30
|
+
textStreamed: new Set(),
|
|
31
|
+
thinkingStreamed: new Set(),
|
|
32
|
+
currentMessageStreamedText: false,
|
|
33
|
+
currentMessageStreamedThinking: false,
|
|
34
|
+
turnEndSignature: null,
|
|
35
|
+
anonymousMessageEpoch: 0,
|
|
36
|
+
roleGuards: new Map(),
|
|
37
|
+
taskRegistry: {
|
|
38
|
+
tasks: new Map(),
|
|
39
|
+
counter: { next: 1 },
|
|
40
|
+
seenToolUseIds: new Set(),
|
|
41
|
+
},
|
|
42
|
+
suppressNextArtifactText: false,
|
|
43
|
+
suppressDuplicateArtifactText: false,
|
|
44
|
+
artifactOpenCandidate: '',
|
|
45
|
+
pendingArtifactText: '',
|
|
46
|
+
duplicateArtifactCandidate: '',
|
|
47
|
+
recentWriteContents: [],
|
|
48
|
+
wroteHtmlFileThisTurn: false,
|
|
49
|
+
};
|
|
50
|
+
return s;
|
|
51
|
+
}
|
|
52
|
+
/** Builds the `{ feed, flush }` handler `createClaudeStreamHandler` returns. */
|
|
53
|
+
export function createStreamHandlerImpl({ onEvent, options }, _optional = {}) {
|
|
54
|
+
const s = createStreamCtx({ onEvent, options });
|
|
55
|
+
return {
|
|
56
|
+
feed: ({ chunk }) => feed(s, { chunk }),
|
|
57
|
+
flush: () => flush(s),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function emitTurnEnd(s, { stopReason }) {
|
|
61
|
+
// `\u0001` prefixes the synthetic key so it can never collide with a real
|
|
62
|
+
// message id, which is why this is safe to compare in the same slot.
|
|
63
|
+
const messageKey = s.currentMessageId ?? `\u0001anon${s.anonymousMessageEpoch}`;
|
|
64
|
+
const signature = `${messageKey}\u0000${stopReason}`;
|
|
65
|
+
if (s.turnEndSignature === signature)
|
|
66
|
+
return;
|
|
67
|
+
s.onEvent({ type: 'turn_end', stopReason });
|
|
68
|
+
s.turnEndSignature = signature;
|
|
69
|
+
if (stopReason !== 'tool_use') {
|
|
70
|
+
s.recentWriteContents.length = 0;
|
|
71
|
+
s.wroteHtmlFileThisTurn = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function emitToolUse(s, { id, name, input }) {
|
|
75
|
+
if (typeof id === 'string') {
|
|
76
|
+
if (s.streamedToolUseIds.has(id))
|
|
77
|
+
return;
|
|
78
|
+
s.streamedToolUseIds.add(id);
|
|
79
|
+
}
|
|
80
|
+
if (emitCanonicalTaskSnapshot({ toolUseId: id, name, input, registry: s.taskRegistry, onEvent: s.onEvent }))
|
|
81
|
+
return;
|
|
82
|
+
if (isFileWriteToolUse({ name, input })) {
|
|
83
|
+
s.suppressNextArtifactText = true;
|
|
84
|
+
const content = fileWriteContent({ input });
|
|
85
|
+
if (content) {
|
|
86
|
+
s.wroteHtmlFileThisTurn = s.wroteHtmlFileThisTurn || isHtmlWriteToolInput({ input });
|
|
87
|
+
s.recentWriteContents.push(normalizeArtifactEchoContent({ value: content }));
|
|
88
|
+
if (s.recentWriteContents.length > 5)
|
|
89
|
+
s.recentWriteContents.shift();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
s.onEvent({
|
|
93
|
+
type: 'tool_use',
|
|
94
|
+
id,
|
|
95
|
+
name,
|
|
96
|
+
input,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function blockKey(s, { index }) {
|
|
100
|
+
return `${s.currentMessageId ?? 'anon'}:${index}`;
|
|
101
|
+
}
|
|
102
|
+
// Per-message role-marker guard (#3247). Covers text_delta ONLY.
|
|
103
|
+
//
|
|
104
|
+
// Why not thinking_delta: extended thinking is rendered to a
|
|
105
|
+
// separate `kind: 'thinking'` payload and is never folded into
|
|
106
|
+
// `m.content` by `buildDaemonTranscript` (apps/web/src/providers/daemon.ts),
|
|
107
|
+
// so it cannot be re-serialized as a turn boundary on the next
|
|
108
|
+
// round-trip — it is not a #3247 re-injection vector. Models
|
|
109
|
+
// routinely emit literal `## user` / `## assistant` lines in
|
|
110
|
+
// chain-of-thought when reasoning about conversation structure,
|
|
111
|
+
// and with kill-on-detection wired in server.ts a guard on the
|
|
112
|
+
// thinking channel would abort otherwise-legitimate runs without
|
|
113
|
+
// any compensating security benefit. See PR #3303 review
|
|
114
|
+
// r3324xxxxxx. Thinking is passed through unguarded; only the
|
|
115
|
+
// user-visible text channel is policed.
|
|
116
|
+
function getOrCreateRoleGuard(s, { msgId }) {
|
|
117
|
+
let guard = s.roleGuards.get(msgId);
|
|
118
|
+
if (!guard) {
|
|
119
|
+
guard = createRoleMarkerGuard({ messageId: msgId });
|
|
120
|
+
s.roleGuards.set(msgId, guard);
|
|
121
|
+
}
|
|
122
|
+
return guard;
|
|
123
|
+
}
|
|
124
|
+
function emitSafeText({ s, msgId, text, eventType }) {
|
|
125
|
+
if (eventType === 'text_delta') {
|
|
126
|
+
text = stripDuplicateArtifactText(s, { text });
|
|
127
|
+
if (!text)
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (eventType !== 'text_delta' || !msgId) {
|
|
131
|
+
s.onEvent({ type: eventType, delta: text });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const guard = getOrCreateRoleGuard(s, { msgId });
|
|
135
|
+
if (guard.contaminated)
|
|
136
|
+
return;
|
|
137
|
+
const safe = guard.feedText({ text: text });
|
|
138
|
+
if (safe.length > 0) {
|
|
139
|
+
s.onEvent({ type: eventType, delta: safe });
|
|
140
|
+
}
|
|
141
|
+
if (guard.contaminated) {
|
|
142
|
+
const warn = guard.warningEvent();
|
|
143
|
+
if (warn)
|
|
144
|
+
s.onEvent(warn);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Resolves an in-progress `<artifact>...</artifact>` echo once its close
|
|
148
|
+
// tag has arrived: drops it if it duplicates a recent file write, then
|
|
149
|
+
// resumes stripping on whatever text followed the close tag.
|
|
150
|
+
function resolveDuplicateArtifactCandidate(s, { current }) {
|
|
151
|
+
s.duplicateArtifactCandidate += current;
|
|
152
|
+
const closeIndex = s.duplicateArtifactCandidate.indexOf('</artifact>');
|
|
153
|
+
if (closeIndex === -1)
|
|
154
|
+
return '';
|
|
155
|
+
const closeEnd = closeIndex + '</artifact>'.length;
|
|
156
|
+
const candidate = s.duplicateArtifactCandidate.slice(0, closeEnd);
|
|
157
|
+
const rest = s.duplicateArtifactCandidate.slice(closeEnd);
|
|
158
|
+
s.duplicateArtifactCandidate = '';
|
|
159
|
+
s.suppressDuplicateArtifactText = false;
|
|
160
|
+
s.suppressNextArtifactText = false;
|
|
161
|
+
const duplicate = isRedundantWrittenArtifact({
|
|
162
|
+
candidate,
|
|
163
|
+
ctx: {
|
|
164
|
+
suppressHtmlArtifactsAfterFileWrite: s.options.suppressHtmlArtifactsAfterFileWrite === true,
|
|
165
|
+
wroteHtmlFileThisTurn: s.wroteHtmlFileThisTurn,
|
|
166
|
+
recentWriteContents: s.recentWriteContents,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
if (s.options.suppressHtmlArtifactsAfterFileWrite !== true) {
|
|
170
|
+
s.recentWriteContents.length = 0;
|
|
171
|
+
}
|
|
172
|
+
return `${duplicate ? '' : candidate}${stripDuplicateArtifactText(s, { text: rest })}`;
|
|
173
|
+
}
|
|
174
|
+
// No open `<artifact` tag has been seen yet: either buffer a partial tag
|
|
175
|
+
// prefix at the end of `current` so it can match across chunk boundaries,
|
|
176
|
+
// or pass the text through untouched.
|
|
177
|
+
function bufferPartialArtifactOpenTag(s, { current, openTag }) {
|
|
178
|
+
const candidateLength = artifactOpenCandidateLength({ text: current, openTag });
|
|
179
|
+
if ((s.suppressNextArtifactText || s.recentWriteContents.length > 0) && candidateLength > 0) {
|
|
180
|
+
s.artifactOpenCandidate = current.slice(-candidateLength);
|
|
181
|
+
return current.slice(0, -candidateLength);
|
|
182
|
+
}
|
|
183
|
+
return current;
|
|
184
|
+
}
|
|
185
|
+
// An `<artifact` open tag just arrived: start accumulating it as a
|
|
186
|
+
// duplicate-write candidate and flush whatever text preceded it.
|
|
187
|
+
function startDuplicateArtifactCandidate(s, { current, openIndex }) {
|
|
188
|
+
s.suppressDuplicateArtifactText = true;
|
|
189
|
+
s.suppressNextArtifactText = false;
|
|
190
|
+
s.duplicateArtifactCandidate = current.slice(openIndex);
|
|
191
|
+
const prefix = `${s.pendingArtifactText}${current.slice(0, openIndex)}`;
|
|
192
|
+
s.pendingArtifactText = '';
|
|
193
|
+
return `${prefix}${stripDuplicateArtifactText(s, { text: '' })}`;
|
|
194
|
+
}
|
|
195
|
+
function stripDuplicateArtifactText(s, { text }) {
|
|
196
|
+
if (!s.suppressNextArtifactText &&
|
|
197
|
+
!s.suppressDuplicateArtifactText &&
|
|
198
|
+
s.artifactOpenCandidate.length === 0 &&
|
|
199
|
+
s.recentWriteContents.length === 0) {
|
|
200
|
+
return text;
|
|
201
|
+
}
|
|
202
|
+
const openTag = '<artifact';
|
|
203
|
+
const current = `${s.artifactOpenCandidate}${text}`;
|
|
204
|
+
s.artifactOpenCandidate = '';
|
|
205
|
+
if (s.suppressDuplicateArtifactText) {
|
|
206
|
+
return resolveDuplicateArtifactCandidate(s, { current });
|
|
207
|
+
}
|
|
208
|
+
const openIndex = current.indexOf(openTag);
|
|
209
|
+
if (openIndex === -1) {
|
|
210
|
+
return bufferPartialArtifactOpenTag(s, { current, openTag });
|
|
211
|
+
}
|
|
212
|
+
return startDuplicateArtifactCandidate(s, { current, openIndex });
|
|
213
|
+
}
|
|
214
|
+
function feed(s, { chunk }) {
|
|
215
|
+
s.lines.feed({ chunk: chunk });
|
|
216
|
+
}
|
|
217
|
+
/** One complete, non-blank line: a JSON object is handled, anything else passes through raw. */
|
|
218
|
+
function feedLine(s, { line }) {
|
|
219
|
+
let obj;
|
|
220
|
+
try {
|
|
221
|
+
obj = JSON.parse(line);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
s.onEvent({ type: 'raw', line });
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
handleObject(s, { obj });
|
|
228
|
+
}
|
|
229
|
+
function flush(s) {
|
|
230
|
+
s.lines.flush();
|
|
231
|
+
flushPendingArtifactText(s);
|
|
232
|
+
// A hard cancel has no result frame. Flush only counters actually observed, once.
|
|
233
|
+
if (s.segmentUsage.hasObserved())
|
|
234
|
+
s.onEvent({ type: 'usage', ...s.segmentUsage.finish({ result: {}, interrupted: true }), stopReason: null });
|
|
235
|
+
}
|
|
236
|
+
function handleToolUseBlock(s, { block }) {
|
|
237
|
+
if (typeof block.id === 'string' && s.streamedToolUseIds.has(block.id))
|
|
238
|
+
return;
|
|
239
|
+
emitToolUse(s, { id: block.id, name: block.name, input: block.input ?? null });
|
|
240
|
+
}
|
|
241
|
+
function handleAssistantContentBlock(s, { block, ctx }) {
|
|
242
|
+
if (!isRecord(block))
|
|
243
|
+
return;
|
|
244
|
+
if (block.type === 'tool_use') {
|
|
245
|
+
handleToolUseBlock(s, { block });
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (isUnstreamedTextBlock(block, { alreadyStreamed: ctx.textAlreadyStreamed })) {
|
|
249
|
+
emitSafeText({ s, msgId: ctx.textMsgId, text: block.text, eventType: 'text_delta' });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (isUnstreamedThinkingBlock(block, { alreadyStreamed: ctx.thinkingAlreadyStreamed })) {
|
|
253
|
+
emitSafeText({ s, msgId: ctx.thinkingMsgId, text: block.thinking, eventType: 'thinking_delta' });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// Resolves the message id assistant text/thinking blocks should be
|
|
257
|
+
// attributed to (an explicit id on the wrapper, or the currently-tracked
|
|
258
|
+
// streaming id), and whether that channel already streamed via deltas.
|
|
259
|
+
// Also owns the message-id/epoch bookkeeping side effects that must
|
|
260
|
+
// happen exactly once per assistant wrapper.
|
|
261
|
+
function resolveAssistantMessageIds(s, { message }) {
|
|
262
|
+
const explicitMsgId = typeof message.id === 'string' ? message.id : null;
|
|
263
|
+
const textMsgId = explicitMsgId ?? (s.currentMessageStreamedText ? s.currentMessageId : null);
|
|
264
|
+
const thinkingMsgId = explicitMsgId ?? (s.currentMessageStreamedThinking ? s.currentMessageId : null);
|
|
265
|
+
if (explicitMsgId)
|
|
266
|
+
s.currentMessageId = explicitMsgId;
|
|
267
|
+
// An id-less assistant wrapper is itself a new message boundary: no
|
|
268
|
+
// `message_start` announced it, so this is the only place the epoch can
|
|
269
|
+
// advance before its own `turn_end` is emitted below. See
|
|
270
|
+
// `anonymousMessageEpoch`'s declaration for why message_delta/result must
|
|
271
|
+
// NOT advance it.
|
|
272
|
+
if (s.currentMessageId === null)
|
|
273
|
+
s.anonymousMessageEpoch += 1;
|
|
274
|
+
return {
|
|
275
|
+
textMsgId,
|
|
276
|
+
thinkingMsgId,
|
|
277
|
+
textAlreadyStreamed: textMsgId ? s.textStreamed.has(textMsgId) : false,
|
|
278
|
+
thinkingAlreadyStreamed: thinkingMsgId ? s.thinkingStreamed.has(thinkingMsgId) : false,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
// `assistant` messages are the "block finished" signal for the current
|
|
282
|
+
// content block. For tool_use blocks whose input finished assembling,
|
|
283
|
+
// emit tool_use now with the final parsed input. For text blocks, emit
|
|
284
|
+
// the text as a single delta — but only if no streaming deltas already
|
|
285
|
+
// covered it (older Claude Code without --include-partial-messages
|
|
286
|
+
// delivers text only here; newer builds stream it and would duplicate).
|
|
287
|
+
function handleAssistantMessage(s, { obj }) {
|
|
288
|
+
const message = obj.message;
|
|
289
|
+
if (!isRecord(message) || !Array.isArray(message.content))
|
|
290
|
+
return;
|
|
291
|
+
if (typeof message.model === 'string')
|
|
292
|
+
s.onEvent({ type: 'status', label: 'model', model: message.model });
|
|
293
|
+
const { textMsgId, thinkingMsgId, textAlreadyStreamed, thinkingAlreadyStreamed } = resolveAssistantMessageIds(s, { message });
|
|
294
|
+
s.segmentUsage.observe({ messageId: usageMessageKey(s), usage: message.usage });
|
|
295
|
+
// Per-turn `stop_reason` is emitted as `turn_end` AFTER the content
|
|
296
|
+
// blocks have been processed (see below). When `--include-partial-
|
|
297
|
+
// messages` is unsupported, tool_use events surface only from the
|
|
298
|
+
// assistant wrapper here — emitting `turn_end` before that loop would
|
|
299
|
+
// let the daemon's stdin-close handler act on the turn before its
|
|
300
|
+
// tool_use blocks were seen, closing stdin mid-tool. Read the
|
|
301
|
+
// stop_reason now, emit after.
|
|
302
|
+
const stopReason = typeof message.stop_reason === 'string' ? message.stop_reason : null;
|
|
303
|
+
for (const block of message.content) {
|
|
304
|
+
handleAssistantContentBlock(s, { block, ctx: { textMsgId, thinkingMsgId, textAlreadyStreamed, thinkingAlreadyStreamed } });
|
|
305
|
+
}
|
|
306
|
+
// Surface the turn_end signal now that every tool_use in this
|
|
307
|
+
// assistant message has been emitted, so the daemon's stdin-close
|
|
308
|
+
// handler sees the final `stop_reason` before deciding whether to
|
|
309
|
+
// close stream-json input stdin.
|
|
310
|
+
if (stopReason) {
|
|
311
|
+
emitTurnEnd(s, { stopReason });
|
|
312
|
+
}
|
|
313
|
+
emitAssistantErrorIfPresent({ obj, content: message.content, onEvent: s.onEvent });
|
|
314
|
+
s.currentMessageStreamedText = false;
|
|
315
|
+
s.currentMessageStreamedThinking = false;
|
|
316
|
+
}
|
|
317
|
+
function handleResultMessage(s, { obj }) {
|
|
318
|
+
// The first non-empty string of `stop_reason`, then `terminal_reason`; otherwise null.
|
|
319
|
+
const stopReason = [obj.stop_reason, obj.terminal_reason].find((reason) => typeof reason === 'string' && reason !== '') ?? null;
|
|
320
|
+
// Claude Code 2.1.201 does not always emit a terminal
|
|
321
|
+
// stream_event/message_delta. In that wire shape, the top-level result
|
|
322
|
+
// is the only frame carrying `end_turn`. Surface it as a turn boundary
|
|
323
|
+
// so AgentExecutor can close stream-json stdin and reap the process.
|
|
324
|
+
if (stopReason) {
|
|
325
|
+
emitTurnEnd(s, { stopReason });
|
|
326
|
+
}
|
|
327
|
+
s.onEvent({
|
|
328
|
+
type: 'usage',
|
|
329
|
+
...s.segmentUsage.finish({ result: obj, interrupted: stopReason?.startsWith('aborted') === true }),
|
|
330
|
+
stopReason,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
function handleObject(s, { obj }) {
|
|
334
|
+
if (!isRecord(obj))
|
|
335
|
+
return;
|
|
336
|
+
s.segmentUsage.setSession({ id: obj.session_id });
|
|
337
|
+
switch (obj.type) {
|
|
338
|
+
case 'system':
|
|
339
|
+
handleSystemMessage({ obj, onEvent: s.onEvent });
|
|
340
|
+
return;
|
|
341
|
+
case 'stream_event':
|
|
342
|
+
if (isRecord(obj.event))
|
|
343
|
+
handleStreamEvent(s, { ev: obj.event });
|
|
344
|
+
return;
|
|
345
|
+
case 'assistant':
|
|
346
|
+
handleAssistantMessage(s, { obj });
|
|
347
|
+
return;
|
|
348
|
+
case 'user':
|
|
349
|
+
handleUserMessage({ obj, onEvent: s.onEvent });
|
|
350
|
+
return;
|
|
351
|
+
case 'result':
|
|
352
|
+
handleResultMessage(s, { obj });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function handleMessageStart(s, { ev }) {
|
|
357
|
+
if (isRecord(ev.message) && typeof ev.message.model === 'string')
|
|
358
|
+
s.onEvent({ type: 'status', label: 'model', model: ev.message.model });
|
|
359
|
+
flushPendingArtifactText(s);
|
|
360
|
+
// Clean up per-message role-marker guard from the previous message.
|
|
361
|
+
if (s.currentMessageId)
|
|
362
|
+
s.roleGuards.delete(s.currentMessageId);
|
|
363
|
+
s.currentMessageId = isRecord(ev.message) && typeof ev.message.id === 'string' ? ev.message.id : null;
|
|
364
|
+
// New message boundary — see `anonymousMessageEpoch`'s declaration.
|
|
365
|
+
if (s.currentMessageId === null)
|
|
366
|
+
s.anonymousMessageEpoch += 1;
|
|
367
|
+
s.segmentUsage.observe({ messageId: usageMessageKey(s), usage: isRecord(ev.message) ? ev.message.usage : undefined });
|
|
368
|
+
s.currentMessageStreamedText = false;
|
|
369
|
+
s.currentMessageStreamedThinking = false;
|
|
370
|
+
if (typeof ev.ttft_ms === 'number') {
|
|
371
|
+
s.onEvent({ type: 'status', label: 'streaming', ttftMs: ev.ttft_ms });
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
function handleContentBlockStart(s, { index, block }) {
|
|
375
|
+
const key = blockKey(s, { index });
|
|
376
|
+
s.blocks.set(key, {
|
|
377
|
+
type: block.type,
|
|
378
|
+
name: block.name,
|
|
379
|
+
id: block.id,
|
|
380
|
+
input: '',
|
|
381
|
+
inputValue: 'input' in block ? block.input : undefined,
|
|
382
|
+
});
|
|
383
|
+
if (block.type === 'thinking') {
|
|
384
|
+
s.onEvent({ type: 'thinking_start' });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
function handleContentBlockDelta(s, { index, delta }) {
|
|
388
|
+
const state = s.blocks.get(blockKey(s, { index }));
|
|
389
|
+
if (delta.type === 'text_delta' && typeof delta.text === 'string') {
|
|
390
|
+
if (s.currentMessageId)
|
|
391
|
+
s.textStreamed.add(s.currentMessageId);
|
|
392
|
+
s.currentMessageStreamedText = true;
|
|
393
|
+
emitSafeText({ s, msgId: s.currentMessageId, text: delta.text, eventType: 'text_delta' });
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (delta.type === 'thinking_delta' && typeof delta.thinking === 'string') {
|
|
397
|
+
if (s.currentMessageId)
|
|
398
|
+
s.thinkingStreamed.add(s.currentMessageId);
|
|
399
|
+
s.currentMessageStreamedThinking = true;
|
|
400
|
+
emitSafeText({ s, msgId: s.currentMessageId, text: delta.thinking, eventType: 'thinking_delta' });
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (delta.type === 'input_json_delta' && typeof delta.partial_json === 'string') {
|
|
404
|
+
applyInputJsonDelta({ state, partialJson: delta.partial_json, onEvent: s.onEvent });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/** Emits a finished tool_use block from its streamed JSON text, else from its complete input value. */
|
|
408
|
+
function emitFinishedToolBlock(s, { state, id }) {
|
|
409
|
+
if (state.input.trim()) {
|
|
410
|
+
try {
|
|
411
|
+
emitToolUse(s, { id, name: state.name, input: JSON.parse(state.input) });
|
|
412
|
+
s.streamedToolUseIds.add(id);
|
|
413
|
+
}
|
|
414
|
+
catch {
|
|
415
|
+
// Fall through to the final assistant wrapper's input if the
|
|
416
|
+
// streamed JSON is malformed or incomplete.
|
|
417
|
+
}
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (state.inputValue === undefined)
|
|
421
|
+
return;
|
|
422
|
+
emitToolUse(s, { id, name: state.name, input: state.inputValue });
|
|
423
|
+
s.streamedToolUseIds.add(id);
|
|
424
|
+
}
|
|
425
|
+
function handleContentBlockStop(s, { ev }) {
|
|
426
|
+
const key = blockKey(s, { index: ev.index });
|
|
427
|
+
const state = s.blocks.get(key);
|
|
428
|
+
if (state?.type === 'tool_use' && typeof state.id === 'string')
|
|
429
|
+
emitFinishedToolBlock(s, { state, id: state.id });
|
|
430
|
+
s.blocks.delete(key);
|
|
431
|
+
}
|
|
432
|
+
// Usage is keyed per message; an id-less message is keyed by its epoch.
|
|
433
|
+
function usageMessageKey(s) {
|
|
434
|
+
return s.currentMessageId ?? `anon${s.anonymousMessageEpoch}`;
|
|
435
|
+
}
|
|
436
|
+
function handleMessageDeltaStreamEvent(s, { ev }) {
|
|
437
|
+
s.segmentUsage.observe({ messageId: usageMessageKey(s), usage: ev.usage ?? (isRecord(ev.delta) ? ev.delta.usage : undefined) });
|
|
438
|
+
if (isRecord(ev.delta))
|
|
439
|
+
handleMessageDeltaEvent(s, { delta: ev.delta });
|
|
440
|
+
}
|
|
441
|
+
function handleMessageDeltaEvent(s, { delta }) {
|
|
442
|
+
const stopReason = typeof delta.stop_reason === 'string' ? delta.stop_reason : null;
|
|
443
|
+
if (stopReason)
|
|
444
|
+
emitTurnEnd(s, { stopReason });
|
|
445
|
+
}
|
|
446
|
+
function handleStreamEvent(s, { ev }) {
|
|
447
|
+
switch (ev.type) {
|
|
448
|
+
case 'message_start':
|
|
449
|
+
handleMessageStart(s, { ev });
|
|
450
|
+
return;
|
|
451
|
+
case 'content_block_start':
|
|
452
|
+
if (isRecord(ev.content_block))
|
|
453
|
+
handleContentBlockStart(s, { index: ev.index, block: ev.content_block });
|
|
454
|
+
return;
|
|
455
|
+
case 'content_block_delta':
|
|
456
|
+
if (isRecord(ev.delta))
|
|
457
|
+
handleContentBlockDelta(s, { index: ev.index, delta: ev.delta });
|
|
458
|
+
return;
|
|
459
|
+
case 'content_block_stop':
|
|
460
|
+
handleContentBlockStop(s, { ev });
|
|
461
|
+
return;
|
|
462
|
+
case 'message_delta':
|
|
463
|
+
handleMessageDeltaStreamEvent(s, { ev });
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function flushPendingArtifactText(s) {
|
|
468
|
+
const text = `${s.pendingArtifactText}${s.artifactOpenCandidate}${s.duplicateArtifactCandidate}`;
|
|
469
|
+
if (!text)
|
|
470
|
+
return;
|
|
471
|
+
s.pendingArtifactText = '';
|
|
472
|
+
s.artifactOpenCandidate = '';
|
|
473
|
+
s.duplicateArtifactCandidate = '';
|
|
474
|
+
s.suppressNextArtifactText = false;
|
|
475
|
+
s.suppressDuplicateArtifactText = false;
|
|
476
|
+
s.recentWriteContents.length = 0;
|
|
477
|
+
s.wroteHtmlFileThisTurn = false;
|
|
478
|
+
emitSafeText({ s, msgId: s.currentMessageId, text, eventType: 'text_delta' });
|
|
479
|
+
}
|
|
480
|
+
//# sourceMappingURL=claude-stream-handler.js.map
|
package/dist/claude-stream.d.ts
CHANGED
|
@@ -36,7 +36,9 @@
|
|
|
36
36
|
* a larger, riskier change than this pass makes. They stay as closures,
|
|
37
37
|
* exercised only through `feed`/`flush` in the test suite.
|
|
38
38
|
*/
|
|
39
|
-
import {
|
|
39
|
+
import type { RoleMarkerWarningEvent } from './role-marker-guard.js';
|
|
40
|
+
import type { EventSink, McpServerStatus } from './claude-stream-frames.js';
|
|
41
|
+
import { type ClaudeStreamHandlerOptions } from './claude-stream-handler.js';
|
|
40
42
|
/**
|
|
41
43
|
* Every event `createClaudeStreamHandler` can emit, discriminated on `type`. Exported (via
|
|
42
44
|
* `index.ts`) precisely because it wasn't before: a consumer outside this package previously had
|
|
@@ -96,76 +98,6 @@ export type ClaudeStreamEvent = {
|
|
|
96
98
|
type: 'raw';
|
|
97
99
|
line: string;
|
|
98
100
|
} | RoleMarkerWarningEvent;
|
|
99
|
-
export type EventSink = (event: ClaudeStreamEvent) => void;
|
|
100
|
-
export type BlockState = {
|
|
101
|
-
type?: unknown;
|
|
102
|
-
name?: unknown;
|
|
103
|
-
id?: unknown;
|
|
104
|
-
input: string;
|
|
105
|
-
inputValue?: unknown;
|
|
106
|
-
};
|
|
107
|
-
export type RuntimeTask = {
|
|
108
|
-
id: string;
|
|
109
|
-
content: string;
|
|
110
|
-
status: 'pending' | 'in_progress' | 'completed' | 'stopped';
|
|
111
|
-
activeForm?: string;
|
|
112
|
-
};
|
|
113
|
-
export type RuntimeTaskIdCounter = {
|
|
114
|
-
next: number;
|
|
115
|
-
};
|
|
116
|
-
export interface TaskRegistry {
|
|
117
|
-
tasks: Map<string, RuntimeTask>;
|
|
118
|
-
counter: RuntimeTaskIdCounter;
|
|
119
|
-
seenToolUseIds: Set<string>;
|
|
120
|
-
}
|
|
121
|
-
export interface ArtifactEchoContext {
|
|
122
|
-
suppressHtmlArtifactsAfterFileWrite: boolean;
|
|
123
|
-
wroteHtmlFileThisTurn: boolean;
|
|
124
|
-
recentWriteContents: readonly string[];
|
|
125
|
-
}
|
|
126
|
-
export declare function toolInputPath(input: Record<string, unknown>): string;
|
|
127
|
-
export declare function normalizeTaskStatus(value: unknown): RuntimeTask['status'];
|
|
128
|
-
export declare function runtimeTaskIdFromCreate(input: Record<string, unknown>, counter: RuntimeTaskIdCounter): string;
|
|
129
|
-
export declare function applyTaskCreate(input: Record<string, unknown>, tasks: Map<string, RuntimeTask>, counter: RuntimeTaskIdCounter): boolean;
|
|
130
|
-
export declare function applyTaskUpdate(input: Record<string, unknown>, tasks: Map<string, RuntimeTask>): boolean;
|
|
131
|
-
export declare function emitCanonicalTaskSnapshot(toolUseId: unknown, name: unknown, input: unknown, registry: TaskRegistry, onEvent: EventSink): boolean;
|
|
132
|
-
export declare function isFileWriteToolUse(name: unknown, input: unknown): boolean;
|
|
133
|
-
export declare function fileWriteContent(input: unknown): string | null;
|
|
134
|
-
export declare function isHtmlWriteToolInput(input: unknown): boolean;
|
|
135
|
-
export declare function artifactOpenCandidateLength(text: string, openTag: string): number;
|
|
136
|
-
export declare function isRedundantWrittenArtifact(candidate: string, ctx: ArtifactEchoContext): boolean;
|
|
137
|
-
export declare function assistantText(content: unknown[]): string;
|
|
138
|
-
export declare function isUnstreamedTextBlock(block: Record<string, unknown>, alreadyStreamed: boolean): block is Record<string, unknown> & {
|
|
139
|
-
text: string;
|
|
140
|
-
};
|
|
141
|
-
export declare function isUnstreamedThinkingBlock(block: Record<string, unknown>, alreadyStreamed: boolean): block is Record<string, unknown> & {
|
|
142
|
-
thinking: string;
|
|
143
|
-
};
|
|
144
|
-
/** One MCP server's state from an init frame's `mcp_servers`, e.g. `{ name: 'jini', status: 'failed' }`. */
|
|
145
|
-
export interface McpServerStatus {
|
|
146
|
-
readonly name: string;
|
|
147
|
-
readonly status: string;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* The init frame's `mcp_servers`, reduced to `{name, status}`, or `undefined` when the frame has no
|
|
151
|
-
* such list. Kept on the event so a host can tell "its bridge did not connect" from "no bridge was
|
|
152
|
-
* configured": Claude Code times a dead MCP server out, reports it `failed` here, and carries on
|
|
153
|
-
* without its tools (the 2026-10-01 desktop incident). Entries without a string name and status are
|
|
154
|
-
* dropped rather than guessed at.
|
|
155
|
-
*/
|
|
156
|
-
export declare function initMcpServers(obj: Record<string, unknown>): McpServerStatus[] | undefined;
|
|
157
|
-
export declare function handleSystemMessage(obj: Record<string, unknown>, onEvent: EventSink): void;
|
|
158
|
-
export declare function stringifyToolResult(content: unknown): string;
|
|
159
|
-
export declare function handleUserMessage(obj: Record<string, unknown>, onEvent: EventSink): void;
|
|
160
|
-
export declare function applyInputJsonDelta(state: BlockState | undefined, partialJson: string, onEvent: EventSink): void;
|
|
161
|
-
export declare function emitAssistantErrorIfPresent(obj: Record<string, unknown>, content: unknown[], onEvent: EventSink): void;
|
|
162
|
-
interface ClaudeStreamHandlerOptions {
|
|
163
|
-
suppressHtmlArtifactsAfterFileWrite?: boolean;
|
|
164
|
-
/** Clock used only when an interrupted segment omits its duration. */
|
|
165
|
-
nowMs?: () => number;
|
|
166
|
-
/** Claude's results span a session; compatible CLIs can retain per-run cost semantics. */
|
|
167
|
-
costBasis?: 'session' | 'run';
|
|
168
|
-
}
|
|
169
101
|
export declare function createClaudeStreamHandler({ onEvent }: {
|
|
170
102
|
onEvent: EventSink;
|
|
171
103
|
}, options?: ClaudeStreamHandlerOptions): {
|
|
@@ -174,5 +106,4 @@ export declare function createClaudeStreamHandler({ onEvent }: {
|
|
|
174
106
|
}) => void;
|
|
175
107
|
flush: () => void;
|
|
176
108
|
};
|
|
177
|
-
export {};
|
|
178
109
|
//# sourceMappingURL=claude-stream.d.ts.map
|