SSE en formato Claude Code CLI via ?format=claude

Nuevo ClaudeFormatEmitter traduce eventos nativos al formato exacto
que produce Claude Code CLI: content_block_start/delta/stop, tool_result,
assistant snapshots, result con usage/cost, done.

- streaming/claude_format.py: ClaudeFormatEmitter + DualEmitter
- base.py: enriquecer eventos con tool_call_id, raw_output, tool_arguments
- engine.py: usage/cost en EXECUTION_COMPLETED
- routes.py: ?format=claude en /sessions/{id}/stream
- main.py: DualEmitter wiring (emite a ambos formatos)

El frontend puede consumir el stream sin cambios — mismos event types
que Claude Code CLI. El formato nativo sigue disponible para el dashboard.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jordan Diaz
2026-04-03 18:48:07 +00:00
parent 6978764540
commit df7dfbc280
5 changed files with 382 additions and 24 deletions

View File

@@ -115,7 +115,7 @@ class BaseAgent:
}
await self.sse.emit(
EventType.TOOL_STARTED,
{"tool": chunk.tool_name, "step": step},
{"tool": chunk.tool_name, "tool_call_id": chunk.tool_call_id, "step": step},
session_id=session.session_id,
)
@@ -123,6 +123,17 @@ class BaseAgent:
tool = active_tools.get(chunk.tool_call_id)
if tool:
tool["arguments"] += chunk.tool_arguments
await self.sse.emit(
EventType.AGENT_DELTA,
{
"agent": self.profile.role,
"delta": "",
"tool_arguments": chunk.tool_arguments,
"tool_call_id": chunk.tool_call_id,
"step": step,
},
session_id=session.session_id,
)
if chunk.finish_reason == "tool_use" and chunk.tool_call_id:
tool = active_tools.pop(chunk.tool_call_id, None)
@@ -200,6 +211,7 @@ class BaseAgent:
tool_name=tc["name"],
arguments=tc.get("parsed_arguments", {}),
artifacts=artifacts,
tool_call_id=tc["id"],
)
tool_fingerprints[fp] = tool_exec
tool_executions.append(tool_exec)
@@ -253,6 +265,7 @@ class BaseAgent:
tool_name: str,
arguments: dict[str, Any],
artifacts: list[ArtifactSummary],
tool_call_id: str = "",
) -> ToolExecution:
"""Execute a tool and summarise the result."""
exec_id = uuid.uuid4().hex[:12]
@@ -299,6 +312,8 @@ class BaseAgent:
"tool": tool_name,
"status": "completed",
"summary": artifact.summary[:200],
"raw_output": raw_output[:4000],
"tool_call_id": tool_call_id,
},
session_id=session.session_id,
)
@@ -311,7 +326,7 @@ class BaseAgent:
await self.sse.emit(
EventType.TOOL_COMPLETED,
{"tool": tool_name, "status": "failed", "error": str(e)},
{"tool": tool_name, "status": "failed", "error": str(e), "tool_call_id": tool_call_id},
session_id=session.session_id,
)

View File

@@ -223,18 +223,6 @@ class OrchestratorEngine:
final_content = self._assemble_response(results, review_result)
status = "completed" if not failed_steps else "partial"
await self.sse.emit(
EventType.EXECUTION_COMPLETED,
{
"session_id": session.session_id,
"task_id": task.task_id,
"steps_completed": len(results),
"steps_failed": failed_steps,
"status": status,
},
session_id=session.session_id,
)
# Accumulate token usage: planner + all steps + review
total_input = planner_usage.get("input_tokens", 0)
total_output = planner_usage.get("output_tokens", 0)
@@ -250,6 +238,23 @@ class OrchestratorEngine:
+ (total_output / 1_000_000) * settings.cost_per_1m_output
)
await self.sse.emit(
EventType.EXECUTION_COMPLETED,
{
"session_id": session.session_id,
"task_id": task.task_id,
"steps_completed": len(results),
"steps_failed": failed_steps,
"status": status,
"usage": {
"input_tokens": total_input,
"output_tokens": total_output,
},
"total_cost_usd": round(cost_usd, 6),
},
session_id=session.session_id,
)
return {
"session_id": session.session_id,
"task_id": task.task_id,