|
| 1 | +"""ACP (Agent Communication Protocol) handler for Agentex. |
| 2 | +
|
| 3 | +This is the API layer — it owns the agent lifecycle and streams tokens |
| 4 | +and tool calls from the Pydantic AI agent to the Agentex frontend. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import os |
| 10 | +from typing import AsyncGenerator |
| 11 | + |
| 12 | +from dotenv import load_dotenv |
| 13 | + |
| 14 | +load_dotenv() |
| 15 | + |
| 16 | +import agentex.lib.adk as adk |
| 17 | +from project.agent import create_agent |
| 18 | +from agentex.lib.adk import ( |
| 19 | + create_pydantic_ai_tracing_handler, |
| 20 | + convert_pydantic_ai_to_agentex_events, |
| 21 | +) |
| 22 | +from agentex.lib.types.acp import SendMessageParams |
| 23 | +from agentex.lib.types.tracing import SGPTracingProcessorConfig |
| 24 | +from agentex.lib.utils.logging import make_logger |
| 25 | +from agentex.lib.sdk.fastacp.fastacp import FastACP |
| 26 | +from agentex.types.task_message_update import TaskMessageUpdate |
| 27 | +from agentex.types.task_message_content import TaskMessageContent |
| 28 | +from agentex.lib.core.tracing.tracing_processor_manager import add_tracing_processor_config |
| 29 | + |
| 30 | +logger = make_logger(__name__) |
| 31 | + |
| 32 | +add_tracing_processor_config( |
| 33 | + SGPTracingProcessorConfig( |
| 34 | + sgp_api_key=os.environ.get("SGP_API_KEY", ""), |
| 35 | + sgp_account_id=os.environ.get("SGP_ACCOUNT_ID", ""), |
| 36 | + sgp_base_url=os.environ.get("SGP_CLIENT_BASE_URL", ""), |
| 37 | + ) |
| 38 | +) |
| 39 | + |
| 40 | +acp = FastACP.create(acp_type="sync") |
| 41 | + |
| 42 | +_agent = None |
| 43 | + |
| 44 | + |
| 45 | +def get_agent(): |
| 46 | + """Get or create the Pydantic AI agent instance.""" |
| 47 | + global _agent |
| 48 | + if _agent is None: |
| 49 | + _agent = create_agent() |
| 50 | + return _agent |
| 51 | + |
| 52 | + |
| 53 | +@acp.on_message_send |
| 54 | +async def handle_message_send( |
| 55 | + params: SendMessageParams, |
| 56 | +) -> TaskMessageContent | list[TaskMessageContent] | AsyncGenerator[TaskMessageUpdate, None]: |
| 57 | + """Handle incoming messages from Agentex, streaming tokens and tool calls.""" |
| 58 | + agent = get_agent() |
| 59 | + task_id = params.task.id |
| 60 | + |
| 61 | + user_message = params.content.content |
| 62 | + logger.info(f"Processing message for task {task_id}") |
| 63 | + |
| 64 | + async with adk.tracing.span( |
| 65 | + trace_id=task_id, |
| 66 | + task_id=task_id, |
| 67 | + name="message", |
| 68 | + input={"message": user_message}, |
| 69 | + data={"__span_type__": "AGENT_WORKFLOW"}, |
| 70 | + ) as turn_span: |
| 71 | + tracing_handler = create_pydantic_ai_tracing_handler( |
| 72 | + trace_id=task_id, |
| 73 | + parent_span_id=turn_span.id if turn_span else None, |
| 74 | + task_id=task_id, |
| 75 | + ) |
| 76 | + async with agent.run_stream_events(user_message) as stream: |
| 77 | + async for event in convert_pydantic_ai_to_agentex_events(stream, tracing_handler=tracing_handler): |
| 78 | + yield event |
0 commit comments