fix: streaming tool_use + скрытие сырого кода от пользователя
- readline вместо async for — убирает буферизацию stdout
- tool events показывают статус ("Читаю файл..."), не сырой Python-код
- done event как fallback для финального ответа
- tab_id для изоляции сессий между вкладками
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+6
-6
@@ -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:
|
||||
|
||||
+8
-3
@@ -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 = '';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user