Ajustes de estructura

This commit is contained in:
Jordan Diaz
2026-05-10 18:47:08 +00:00
parent 44cb956f95
commit 5e64bbdfc8
10 changed files with 170 additions and 17 deletions

View File

@@ -35,6 +35,8 @@ class ClaudeFormatEmitter:
self._tool_block_index: dict[str, dict[str, int]] = {} # session -> {tool_call_id -> index}
self._content_blocks: dict[str, list[dict[str, Any]]] = {}
self._text_accumulator: dict[str, str] = {}
self._thinking_block_open: dict[str, bool] = {}
self._thinking_block_index: dict[str, int] = {}
def _next_index(self, session_id: str) -> int:
idx = self._block_counter.get(session_id, 0)
@@ -48,6 +50,8 @@ class ClaudeFormatEmitter:
self._tool_block_index[session_id] = {}
self._content_blocks[session_id] = []
self._text_accumulator[session_id] = ""
self._thinking_block_open[session_id] = False
self._thinking_block_index[session_id] = -1
def _push(self, session_id: str, payload: dict[str, Any]) -> None:
"""Push a formatted line to all subscribers of a session."""
@@ -119,7 +123,43 @@ class ClaudeFormatEmitter:
tool_args = data.get("tool_arguments", "")
tool_call_id = data.get("tool_call_id", "")
thinking_delta = data.get("thinking_delta", "")
if thinking_delta:
# Cerrar text block abierto si lo hay
self._close_text_block(session_id)
# Abrir thinking block si no esta abierto
if not self._thinking_block_open.get(session_id):
idx = self._next_index(session_id)
self._thinking_block_index[session_id] = idx
self._thinking_block_open[session_id] = True
self._push(session_id, {
"type": "stream_event",
"event": {
"type": "content_block_start",
"index": idx,
"content_block": {"type": "thinking", "thinking": ""},
},
})
idx = self._thinking_block_index[session_id]
self._push(session_id, {
"type": "stream_event",
"event": {
"type": "content_block_delta",
"index": idx,
"delta": {"type": "thinking_delta", "thinking": thinking_delta},
},
})
return
if delta_text:
# Cerrar thinking block abierto si lo hay antes de texto normal
if self._thinking_block_open.get(session_id):
idx = self._thinking_block_index[session_id]
self._push(session_id, {
"type": "stream_event",
"event": {"type": "content_block_stop", "index": idx},
})
self._thinking_block_open[session_id] = False
# Text streaming
if not self._text_block_open.get(session_id):
self._open_text_block(session_id)
@@ -152,6 +192,15 @@ class ClaudeFormatEmitter:
tool_name = data.get("tool", "unknown")
tool_call_id = data.get("tool_call_id", "")
# Cerrar thinking block abierto si lo hay
if self._thinking_block_open.get(session_id):
idx = self._thinking_block_index[session_id]
self._push(session_id, {
"type": "stream_event",
"event": {"type": "content_block_stop", "index": idx},
})
self._thinking_block_open[session_id] = False
# Close open text block
self._close_text_block(session_id)