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:
@@ -28,21 +28,45 @@ from src.claude_session import (
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
FAKE_CLI_RESPONSE = {
|
||||
FAKE_RESULT_LINE = {
|
||||
"type": "result",
|
||||
"subtype": "success",
|
||||
"session_id": "sess-abc-123",
|
||||
"result": "Hello from Claude!",
|
||||
"cost_usd": 0.004,
|
||||
"total_cost_usd": 0.004,
|
||||
"duration_ms": 1500,
|
||||
"num_turns": 1,
|
||||
"usage": {"input_tokens": 100, "output_tokens": 50},
|
||||
}
|
||||
|
||||
FAKE_ASSISTANT_LINE = {
|
||||
"type": "assistant",
|
||||
"message": {
|
||||
"content": [{"type": "text", "text": "Hello from Claude!"}],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _make_stream(*assistant_texts, result_override=None):
|
||||
"""Build stream-json stdout with assistant + result lines."""
|
||||
lines = []
|
||||
for text in assistant_texts:
|
||||
lines.append(json.dumps({
|
||||
"type": "assistant",
|
||||
"message": {"content": [{"type": "text", "text": text}]},
|
||||
}))
|
||||
result = dict(FAKE_RESULT_LINE)
|
||||
if result_override:
|
||||
result.update(result_override)
|
||||
lines.append(json.dumps(result))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _make_proc(stdout=None, returncode=0, stderr=""):
|
||||
"""Build a fake subprocess.CompletedProcess."""
|
||||
"""Build a fake subprocess.CompletedProcess with stream-json output."""
|
||||
if stdout is None:
|
||||
stdout = json.dumps(FAKE_CLI_RESPONSE)
|
||||
stdout = _make_stream("Hello from Claude!")
|
||||
proc = MagicMock(spec=subprocess.CompletedProcess)
|
||||
proc.stdout = stdout
|
||||
proc.stderr = stderr
|
||||
@@ -153,10 +177,20 @@ class TestSafeEnv:
|
||||
class TestRunClaude:
|
||||
@patch("shutil.which", return_value="/usr/bin/claude")
|
||||
@patch("subprocess.run")
|
||||
def test_returns_parsed_json(self, mock_run, mock_which):
|
||||
def test_returns_parsed_stream(self, mock_run, mock_which):
|
||||
mock_run.return_value = _make_proc()
|
||||
result = _run_claude(["claude", "-p", "hi"], timeout=30)
|
||||
assert result == FAKE_CLI_RESPONSE
|
||||
assert result["result"] == "Hello from Claude!"
|
||||
assert result["session_id"] == "sess-abc-123"
|
||||
assert "usage" in result
|
||||
|
||||
@patch("shutil.which", return_value="/usr/bin/claude")
|
||||
@patch("subprocess.run")
|
||||
def test_collects_multiple_text_blocks(self, mock_run, mock_which):
|
||||
stdout = _make_stream("First message", "Second message", "Third message")
|
||||
mock_run.return_value = _make_proc(stdout=stdout)
|
||||
result = _run_claude(["claude", "-p", "hi"], timeout=30)
|
||||
assert result["result"] == "First message\n\nSecond message\n\nThird message"
|
||||
|
||||
@patch("shutil.which", return_value="/usr/bin/claude")
|
||||
@patch("subprocess.run")
|
||||
@@ -176,9 +210,11 @@ class TestRunClaude:
|
||||
|
||||
@patch("shutil.which", return_value="/usr/bin/claude")
|
||||
@patch("subprocess.run")
|
||||
def test_invalid_json_raises(self, mock_run, mock_which):
|
||||
mock_run.return_value = _make_proc(stdout="not json {{{")
|
||||
with pytest.raises(RuntimeError, match="Failed to parse"):
|
||||
def test_no_result_line_raises(self, mock_run, mock_which):
|
||||
# Stream with only an assistant line but no result line
|
||||
stdout = json.dumps({"type": "assistant", "message": {"content": []}})
|
||||
mock_run.return_value = _make_proc(stdout=stdout)
|
||||
with pytest.raises(RuntimeError, match="no result line"):
|
||||
_run_claude(["claude", "-p", "hi"], timeout=30)
|
||||
|
||||
@patch("shutil.which", return_value=None)
|
||||
@@ -299,7 +335,7 @@ class TestStartSession:
|
||||
|
||||
@patch("shutil.which", return_value="/usr/bin/claude")
|
||||
@patch("subprocess.run")
|
||||
def test_missing_result_field_raises(
|
||||
def test_missing_result_line_raises(
|
||||
self, mock_run, mock_which, tmp_path, monkeypatch
|
||||
):
|
||||
sessions_dir = tmp_path / "sessions"
|
||||
@@ -308,15 +344,16 @@ class TestStartSession:
|
||||
monkeypatch.setattr(
|
||||
claude_session, "_SESSIONS_FILE", sessions_dir / "active.json"
|
||||
)
|
||||
bad_response = {"session_id": "abc"} # missing "result"
|
||||
mock_run.return_value = _make_proc(stdout=json.dumps(bad_response))
|
||||
# Stream with no result line at all
|
||||
bad_stream = json.dumps({"type": "assistant", "message": {"content": []}})
|
||||
mock_run.return_value = _make_proc(stdout=bad_stream)
|
||||
|
||||
with pytest.raises(RuntimeError, match="missing required field"):
|
||||
with pytest.raises(RuntimeError, match="no result line"):
|
||||
start_session("general", "Hello")
|
||||
|
||||
@patch("shutil.which", return_value="/usr/bin/claude")
|
||||
@patch("subprocess.run")
|
||||
def test_missing_session_id_field_raises(
|
||||
def test_missing_session_id_gives_empty_string(
|
||||
self, mock_run, mock_which, tmp_path, monkeypatch
|
||||
):
|
||||
sessions_dir = tmp_path / "sessions"
|
||||
@@ -325,8 +362,10 @@ class TestStartSession:
|
||||
monkeypatch.setattr(
|
||||
claude_session, "_SESSIONS_FILE", sessions_dir / "active.json"
|
||||
)
|
||||
bad_response = {"result": "hello"} # missing "session_id"
|
||||
mock_run.return_value = _make_proc(stdout=json.dumps(bad_response))
|
||||
# Result line without session_id → _run_claude returns "" for session_id
|
||||
# → start_session checks for empty session_id
|
||||
bad_stream = _make_stream("hello", result_override={"session_id": None})
|
||||
mock_run.return_value = _make_proc(stdout=bad_stream)
|
||||
|
||||
with pytest.raises(RuntimeError, match="missing required field"):
|
||||
start_session("general", "Hello")
|
||||
|
||||
Reference in New Issue
Block a user