MCPcopy Create free account
hub / github.com/agentforce314/clawcodex / _run_sync_agent

Function _run_sync_agent

src/tool_system/tools/agent.py:379–430  ·  view source on GitHub ↗

Run an agent synchronously and return the result.

(
        *,
        run_params: RunAgentParams,
        agent_id: str,
        start_time: float,
        prompt: str,
        agent_type: str,
    )

Source from the content-addressed store, hash-verified

377 )
378
379 def _run_sync_agent(
380 *,
381 run_params: RunAgentParams,
382 agent_id: str,
383 start_time: float,
384 prompt: str,
385 agent_type: str,
386 ) -> ToolResult:
387 """Run an agent synchronously and return the result."""
388 from ..protocol import ToolResult as TR
389 from src.types.messages import Message
390
391 agent_messages: list[Message] = []
392
393 try:
394 loop = asyncio.get_event_loop()
395 if loop.is_running():
396 # We're inside an async context — use a nested run
397 import concurrent.futures
398 with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
399 future = pool.submit(_sync_collect_agent_messages, run_params)
400 agent_messages = future.result()
401 else:
402 agent_messages = loop.run_until_complete(
403 _collect_agent_messages(run_params)
404 )
405 except RuntimeError:
406 # No event loop — create one
407 agent_messages = asyncio.run(
408 _collect_agent_messages(run_params)
409 )
410
411 # Finalize result
412 metadata = {
413 "start_time": start_time,
414 "agent_type": agent_type,
415 }
416 result = finalize_agent_tool(agent_messages, agent_id, metadata)
417
418 return TR(
419 name=AGENT_TOOL_NAME,
420 output={
421 "status": "completed",
422 "prompt": prompt,
423 "agent_id": result.agent_id,
424 "agent_type": result.agent_type,
425 "content": result.content,
426 "total_duration_ms": result.total_duration_ms,
427 "total_tokens": result.total_tokens,
428 "total_tool_use_count": result.total_tool_use_count,
429 },
430 )
431
432 def _launch_async_agent(
433 *,

Callers 1

_agent_callFunction · 0.85

Calls 3

finalize_agent_toolFunction · 0.90
_collect_agent_messagesFunction · 0.85
runMethod · 0.45

Tested by

no test coverage detected