fix: capture all intermediate text blocks from Claude tool-use responses
Switch from --output-format json to --output-format stream-json --verbose so that _run_claude() parses all assistant text blocks (not just the final result field). Discord/Telegram/WhatsApp now receive every intermediate message Claude writes between tool calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -157,7 +157,12 @@ def _save_sessions(data: dict) -> None:
|
||||
|
||||
|
||||
def _run_claude(cmd: list[str], timeout: int) -> dict:
|
||||
"""Run a Claude CLI command and return the parsed JSON output."""
|
||||
"""Run a Claude CLI command and return parsed output.
|
||||
|
||||
Expects ``--output-format stream-json --verbose``. Parses the newline-
|
||||
delimited JSON stream, collecting every text block from ``assistant``
|
||||
messages and metadata from the final ``result`` line.
|
||||
"""
|
||||
if not shutil.which(CLAUDE_BIN):
|
||||
raise FileNotFoundError(
|
||||
"Claude CLI not found. "
|
||||
@@ -184,12 +189,50 @@ def _run_claude(cmd: list[str], timeout: int) -> dict:
|
||||
f"Claude CLI error (exit {proc.returncode}): {detail}"
|
||||
)
|
||||
|
||||
try:
|
||||
data = json.loads(proc.stdout)
|
||||
except json.JSONDecodeError as exc:
|
||||
raise RuntimeError(f"Failed to parse Claude CLI output: {exc}")
|
||||
# --- Parse stream-json output ---
|
||||
text_blocks: list[str] = []
|
||||
result_obj: dict | None = None
|
||||
|
||||
return data
|
||||
for line in proc.stdout.splitlines():
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
obj = json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
msg_type = obj.get("type")
|
||||
|
||||
if msg_type == "assistant":
|
||||
# Extract text from content blocks
|
||||
message = obj.get("message", {})
|
||||
for block in message.get("content", []):
|
||||
if block.get("type") == "text":
|
||||
text = block.get("text", "").strip()
|
||||
if text:
|
||||
text_blocks.append(text)
|
||||
|
||||
elif msg_type == "result":
|
||||
result_obj = obj
|
||||
|
||||
if result_obj is None:
|
||||
raise RuntimeError(
|
||||
"Failed to parse Claude CLI output: no result line in stream"
|
||||
)
|
||||
|
||||
# Build a dict compatible with the old json output format
|
||||
combined_text = "\n\n".join(text_blocks) if text_blocks else result_obj.get("result", "")
|
||||
|
||||
return {
|
||||
"result": combined_text,
|
||||
"session_id": result_obj.get("session_id", ""),
|
||||
"usage": result_obj.get("usage", {}),
|
||||
"total_cost_usd": result_obj.get("total_cost_usd", 0),
|
||||
"cost_usd": result_obj.get("cost_usd", 0),
|
||||
"duration_ms": result_obj.get("duration_ms", 0),
|
||||
"num_turns": result_obj.get("num_turns", 0),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -248,7 +291,7 @@ def start_session(
|
||||
cmd = [
|
||||
CLAUDE_BIN, "-p", wrapped_message,
|
||||
"--model", model,
|
||||
"--output-format", "json",
|
||||
"--output-format", "stream-json", "--verbose",
|
||||
"--system-prompt", system_prompt,
|
||||
"--allowedTools", *ALLOWED_TOOLS,
|
||||
]
|
||||
@@ -258,7 +301,7 @@ def start_session(
|
||||
_elapsed_ms = int((time.monotonic() - _t0) * 1000)
|
||||
|
||||
for field in ("result", "session_id"):
|
||||
if field not in data:
|
||||
if not data.get(field):
|
||||
raise RuntimeError(
|
||||
f"Claude CLI response missing required field: {field}"
|
||||
)
|
||||
@@ -317,7 +360,7 @@ def resume_session(
|
||||
cmd = [
|
||||
CLAUDE_BIN, "-p", wrapped_message,
|
||||
"--resume", session_id,
|
||||
"--output-format", "json",
|
||||
"--output-format", "stream-json", "--verbose",
|
||||
"--allowedTools", *ALLOWED_TOOLS,
|
||||
]
|
||||
|
||||
@@ -325,7 +368,7 @@ def resume_session(
|
||||
data = _run_claude(cmd, timeout)
|
||||
_elapsed_ms = int((time.monotonic() - _t0) * 1000)
|
||||
|
||||
if "result" not in data:
|
||||
if not data.get("result"):
|
||||
raise RuntimeError(
|
||||
"Claude CLI response missing required field: result"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user