AgentSkillsCN

fine-grained-tool-streaming

修补 Claude Code cli.js,以支持细粒度的工具流式传输(input_json_delta)。适用于工具调用参数在最后才一次性涌入,而非逐步流式传输时,也适用于 sse_lines.jsonl 显示的 input_json_delta 事件极少或无事件时,或当用户提出“启用工具流式传输”、“修复部分 JSON”、“修补细粒度流式传输”时使用。

SKILL.md
--- frontmatter
name: fine-grained-tool-streaming
description: Patch Claude Code cli.js to enable fine-grained tool streaming (input_json_delta). Use when tool call arguments arrive as a single burst at the end instead of streaming incrementally, when sse_lines.jsonl shows few/no input_json_delta events, or when asked to "enable tool streaming", "fix partial json", or "patch fine-grained streaming".

Fine-Grained Tool Streaming Patch

Enables incremental streaming of tool call arguments (input_json_delta) in Claude Code cli.js.

Problem

Pristine Claude Code cli.js does NOT enable the fine-grained-tool-streaming-2025-05-14 beta by default. Tool arguments arrive as a single burst at the end instead of streaming character-by-character.

Solution

Add Q.push("fine-grained-tool-streaming-2025-05-14") to the betas builder function.

Patch Procedure

1. Find the patch point

bash
grep -n "process.env.ANTHROPIC_BETAS" cli.js

Look for the betas builder function (typically in module RR, function pp1).

2. Identify the insertion point

Find these consecutive lines:

javascript
if (G === "vertex" && ...) Q.push(...);
if (G === "foundry") Q.push(...);
if (process.env.ANTHROPIC_BETAS && !B)

3. Insert the patch

Add these 2 lines BEFORE if (process.env.ANTHROPIC_BETAS:

javascript
if (G === "firstParty" && !a1(process.env.CLAUDE_CODE_DISABLE_FINE_GRAINED_TOOL_STREAMING))
  Q.push("fine-grained-tool-streaming-2025-05-14");

Note: a1 is the env-var truthy checker (varies by version: a1, B0, etc.). Find it by searching !a1(process.env.DISABLE_ nearby.

4. Run the patch script

bash
python scripts/patch_fine_grained_streaming.py <path-to-cli.js>

Verification

bash
node cli.js --dangerously-skip-permissions --log-dir /tmp/test-fgts \
  -p "Write a 50 word story. Save to /tmp/test-fgts/story.txt"

# Check for multiple input_json_delta events
grep -c "input_json_delta" /tmp/test-fgts/sse_lines.jsonl
# Should be >10 (not 0-2)

# Check timestamps are spread out (not burst)
grep "input_json_delta" /tmp/test-fgts/sse_lines.jsonl | head -5 | jq -r '.t'

Disable (if needed)

Set env var to disable:

bash
CLAUDE_CODE_DISABLE_FINE_GRAINED_TOOL_STREAMING=1 node cli.js ...