mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
refac
This commit is contained in:
1 parent
0504711411
commit
5ac52c756d
3 files changed
+37
-9
No files matched your search
@@ -13,6 +13,7 @@ from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentEvent,
|
||||
AgentReasoningDone,
|
||||
AgentReasoningDelta,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
@@ -160,7 +161,9 @@ async def run_claude_code_agent(
|
||||
usage: dict[str, Any] | None = None
|
||||
observed_session_id = session_id
|
||||
tool_calls: dict[int, AgentToolUpdate] = {}
|
||||
received_content_delta = False
|
||||
thinking_blocks: set[int] = set()
|
||||
received_text_delta = False
|
||||
received_thinking_delta = False
|
||||
|
||||
async for message in stream:
|
||||
event = getattr(message, "event", None)
|
||||
@@ -174,23 +177,33 @@ async def run_claude_code_agent(
|
||||
):
|
||||
text = delta["text"]
|
||||
if text:
|
||||
received_content_delta = True
|
||||
received_text_delta = True
|
||||
yield AgentTextDelta(text)
|
||||
elif delta.get("type") == "thinking_delta" and isinstance(
|
||||
delta.get("thinking"), str
|
||||
):
|
||||
index = event.get("index")
|
||||
if isinstance(index, int):
|
||||
thinking_blocks.add(index)
|
||||
thinking = delta["thinking"]
|
||||
yield AgentReasoningDelta(thinking)
|
||||
if thinking:
|
||||
received_content_delta = True
|
||||
yield AgentReasoningDelta(thinking)
|
||||
received_thinking_delta = True
|
||||
elif event_type == "content_block_start":
|
||||
index, tool = _tool_update_from_claude_start(event)
|
||||
if tool:
|
||||
if index is not None:
|
||||
tool_calls[index] = tool
|
||||
yield tool
|
||||
elif isinstance(index, int):
|
||||
block = event.get("content_block")
|
||||
if isinstance(block, dict) and block.get("type") == "thinking":
|
||||
thinking_blocks.add(index)
|
||||
elif event_type == "content_block_stop":
|
||||
index = event.get("index")
|
||||
if isinstance(index, int) and index in thinking_blocks:
|
||||
thinking_blocks.discard(index)
|
||||
yield AgentReasoningDone()
|
||||
tool = tool_calls.get(index) if isinstance(index, int) else None
|
||||
if tool:
|
||||
yield AgentToolUpdate(
|
||||
@@ -204,14 +217,16 @@ async def run_claude_code_agent(
|
||||
|
||||
class_name = message.__class__.__name__
|
||||
if class_name == "AssistantMessage":
|
||||
if received_content_delta:
|
||||
continue
|
||||
for block in getattr(message, "content", []) or []:
|
||||
if block.__class__.__name__ == "TextBlock":
|
||||
if received_text_delta:
|
||||
continue
|
||||
text = getattr(block, "text", "")
|
||||
if text:
|
||||
yield AgentTextDelta(text)
|
||||
elif block.__class__.__name__ == "ThinkingBlock":
|
||||
if received_thinking_delta:
|
||||
continue
|
||||
text = getattr(block, "thinking", "")
|
||||
if text:
|
||||
yield AgentReasoningDelta(text)
|
||||
|
||||
@@ -16,6 +16,11 @@ class AgentReasoningDelta:
|
||||
text: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentReasoningDone:
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentToolUpdate:
|
||||
call_id: str
|
||||
@@ -46,6 +51,7 @@ class AgentError:
|
||||
AgentEvent = (
|
||||
AgentTextDelta
|
||||
| AgentReasoningDelta
|
||||
| AgentReasoningDone
|
||||
| AgentToolUpdate
|
||||
| AgentToolOutputDelta
|
||||
| AgentDone
|
||||
|
||||
+10
-3
@@ -49,6 +49,7 @@ from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentReasoningDelta,
|
||||
AgentReasoningDone,
|
||||
AgentTextDelta,
|
||||
AgentToolOutputDelta,
|
||||
AgentToolUpdate,
|
||||
@@ -1522,13 +1523,17 @@ async def run_chat_task(
|
||||
if runner is None:
|
||||
raise RuntimeError(f"Unsupported agent type: {agent_target.agent}")
|
||||
reasoning_buffer = ""
|
||||
reasoning_item_id = f"reasoning-{message_id}"
|
||||
|
||||
async def _finish_reasoning_item():
|
||||
if not reasoning_buffer:
|
||||
existing = next(
|
||||
(item for item in output_items if item.get("id") == reasoning_item_id), None
|
||||
)
|
||||
if not existing or existing.get("status") == "completed":
|
||||
return
|
||||
item = {
|
||||
"type": "reasoning",
|
||||
"id": f"reasoning-{message_id}",
|
||||
"id": reasoning_item_id,
|
||||
"status": "completed",
|
||||
"content": [{"type": "reasoning_text", "text": reasoning_buffer}],
|
||||
}
|
||||
@@ -1555,13 +1560,15 @@ async def run_chat_task(
|
||||
reasoning_buffer += event.text
|
||||
item = {
|
||||
"type": "reasoning",
|
||||
"id": f"reasoning-{message_id}",
|
||||
"id": reasoning_item_id,
|
||||
"status": "in_progress",
|
||||
"content": [{"type": "reasoning_text", "text": reasoning_buffer}],
|
||||
}
|
||||
_upsert_output_item(output_items, item)
|
||||
await emit(output=item)
|
||||
_sync_state()
|
||||
elif isinstance(event, AgentReasoningDone):
|
||||
await _finish_reasoning_item()
|
||||
elif isinstance(event, AgentToolUpdate):
|
||||
flushed_item = _flush_text()
|
||||
if flushed_item:
|
||||
|
||||
Reference in new issue
Block a user