|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Assemble stream-json JSONL into a single json-verbose response. |
| 3 | +
|
| 4 | +Reads JSONL from stdin (claude --output-format stream-json --verbose), |
| 5 | +collects all events into a turns array, and outputs a single JSON object |
| 6 | +that combines the final result with the full conversation history. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +def _extract_tool_uses(content): |
| 14 | + """Extract tool_use entries from assistant message content.""" |
| 15 | + out = [] |
| 16 | + for block in content: |
| 17 | + if block.get("type") != "tool_use": |
| 18 | + continue |
| 19 | + entry = { |
| 20 | + "type": "tool_use", |
| 21 | + "id": block["id"], |
| 22 | + "name": block["name"], |
| 23 | + "input": block.get("input", {}), |
| 24 | + } |
| 25 | + out.append(entry) |
| 26 | + return out |
| 27 | + |
| 28 | + |
| 29 | +def _extract_text(content): |
| 30 | + """Extract text blocks from assistant message content.""" |
| 31 | + out = [] |
| 32 | + for block in content: |
| 33 | + if block.get("type") != "text": |
| 34 | + continue |
| 35 | + out.append({"type": "text", "text": block["text"]}) |
| 36 | + return out |
| 37 | + |
| 38 | + |
| 39 | +def _extract_tool_results(content): |
| 40 | + """Extract tool_result entries from user message content.""" |
| 41 | + out = [] |
| 42 | + for block in content: |
| 43 | + if block.get("type") != "tool_result": |
| 44 | + continue |
| 45 | + entry = { |
| 46 | + "type": "tool_result", |
| 47 | + "tool_use_id": block.get("tool_use_id", ""), |
| 48 | + "is_error": block.get("is_error", False), |
| 49 | + } |
| 50 | + raw = block.get("content", "") |
| 51 | + if isinstance(raw, str): |
| 52 | + entry["content"] = raw |
| 53 | + elif isinstance(raw, list): |
| 54 | + # content can be a list of blocks |
| 55 | + texts = [b.get("text", "") for b in raw if b.get("type") == "text"] |
| 56 | + entry["content"] = "\n".join(texts) if texts else str(raw) |
| 57 | + else: |
| 58 | + entry["content"] = str(raw) |
| 59 | + out.append(entry) |
| 60 | + return out |
| 61 | + |
| 62 | + |
| 63 | +def assemble(lines): |
| 64 | + """Parse JSONL lines and return assembled json-verbose dict.""" |
| 65 | + turns = [] |
| 66 | + result = None |
| 67 | + system_init = None |
| 68 | + |
| 69 | + for line in lines: |
| 70 | + line = line.strip() |
| 71 | + if not line: |
| 72 | + continue |
| 73 | + try: |
| 74 | + event = json.loads(line) |
| 75 | + except json.JSONDecodeError: |
| 76 | + continue |
| 77 | + |
| 78 | + etype = event.get("type", "") |
| 79 | + |
| 80 | + if etype == "system" and event.get("subtype") == "init": |
| 81 | + system_init = { |
| 82 | + "session_id": event.get("session_id", ""), |
| 83 | + "model": event.get("model", ""), |
| 84 | + "cwd": event.get("cwd", ""), |
| 85 | + "tools": event.get("tools", []), |
| 86 | + } |
| 87 | + continue |
| 88 | + |
| 89 | + if etype == "assistant": |
| 90 | + msg = event.get("message", {}) |
| 91 | + content = msg.get("content", []) |
| 92 | + texts = _extract_text(content) |
| 93 | + tool_uses = _extract_tool_uses(content) |
| 94 | + parts = texts + tool_uses |
| 95 | + if not parts: |
| 96 | + continue |
| 97 | + turns.append({"role": "assistant", "content": parts}) |
| 98 | + continue |
| 99 | + |
| 100 | + if etype == "user": |
| 101 | + msg = event.get("message", {}) |
| 102 | + content = msg.get("content", []) |
| 103 | + tool_results = _extract_tool_results(content) |
| 104 | + if not tool_results: |
| 105 | + continue |
| 106 | + turns.append({"role": "tool_result", "content": tool_results}) |
| 107 | + continue |
| 108 | + |
| 109 | + if etype == "result": |
| 110 | + result = event |
| 111 | + continue |
| 112 | + |
| 113 | + if not result: |
| 114 | + return { |
| 115 | + "type": "result", |
| 116 | + "subtype": "error", |
| 117 | + "is_error": True, |
| 118 | + "result": "no result event found in stream", |
| 119 | + "turns": turns, |
| 120 | + } |
| 121 | + |
| 122 | + result["turns"] = turns |
| 123 | + if system_init: |
| 124 | + result["system"] = system_init |
| 125 | + |
| 126 | + return result |
| 127 | + |
| 128 | + |
| 129 | +def main(): |
| 130 | + lines = sys.stdin.readlines() |
| 131 | + output = assemble(lines) |
| 132 | + json.dump(output, sys.stdout) |
| 133 | + sys.stdout.write("\n") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments