From 4a9574980e9b497ad0508c73ed8256254a9a0b67 Mon Sep 17 00:00:00 2001 From: anor Date: Tue, 24 Mar 2026 18:15:00 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20streaming=20tool=5Fuse=20+=20=D1=81?= =?UTF-8?q?=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20=D1=81=D1=8B=D1=80=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BA=D0=BE=D0=B4=D0=B0=20=D0=BE=D1=82=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - readline вместо async for — убирает буферизацию stdout - tool events показывают статус ("Читаю файл..."), не сырой Python-код - done event как fallback для финального ответа - tab_id для изоляции сессий между вкладками Co-Authored-By: Claude Opus 4.6 (1M context) --- claude_bridge.py | 12 ++++++------ static/chat.html | 11 ++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/claude_bridge.py b/claude_bridge.py index 457bb10..af06954 100644 --- a/claude_bridge.py +++ b/claude_bridge.py @@ -70,7 +70,10 @@ async def stream_claude( ) try: - async for raw_line in proc.stdout: + while True: + raw_line = await proc.stdout.readline() + if not raw_line: + break line = raw_line.decode().strip() if not line: continue @@ -82,7 +85,6 @@ async def stream_claude( etype = event.get("type", "") if etype == "assistant": - # Extract text from content blocks msg = event.get("message", {}) for block in msg.get("content", []): if block.get("type") == "text": @@ -90,16 +92,14 @@ async def stream_claude( yield f"event: text\ndata: {json.dumps({'text': text}, ensure_ascii=False)}\n\n" elif block.get("type") == "tool_use": name = block.get("name", "") - inp = block.get("input", {}) - yield f"event: tool\ndata: {json.dumps({'tool': name, 'input': inp}, ensure_ascii=False)}\n\n" + yield f"event: tool\ndata: {json.dumps({'tool': name}, ensure_ascii=False)}\n\n" elif etype == "result": sid = event.get("session_id", "") if sid: set_session_id(tab_id, sid) result_text = event.get("result", "") - cost = event.get("total_cost_usd", 0) - yield f"event: done\ndata: {json.dumps({'result': result_text, 'cost': cost}, ensure_ascii=False)}\n\n" + yield f"event: done\ndata: {json.dumps({'result': result_text}, ensure_ascii=False)}\n\n" finally: if proc.returncode is None: diff --git a/static/chat.html b/static/chat.html index 330ab38..30de7a7 100644 --- a/static/chat.html +++ b/static/chat.html @@ -280,12 +280,17 @@ async function sendMessage() { aiDiv.textContent = aiText; msgs.scrollTop = msgs.scrollHeight; } else if (eventType === 'tool') { - addMsg('tool', '🔧 ' + data.tool + (data.input.command ? ': ' + data.input.command : data.input.file_path ? ': ' + data.input.file_path : '')); + // Show minimal status, not raw code + const toolNames = {Bash: 'Выполняю...', Read: 'Читаю файл...', Write: 'Создаю файл...', Edit: 'Редактирую...', Glob: 'Ищу файлы...', Grep: 'Ищу в файлах...'}; + const label = toolNames[data.tool] || 'Обрабатываю...'; + removeTyping(); + addTyping().textContent = label; } else if (eventType === 'done') { - if (!aiDiv && data.result) { + removeTyping(); + // Always show final result if we haven't shown text yet + if (!aiText && data.result) { aiDiv = addMsg('ai', data.result); } - // cost info hidden from user } eventType = ''; }