Ajustes de estructura
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user