|
| 1 | +""" |
| 2 | +ACP (Agent Communication Protocol) handler for Agentex. |
| 3 | +
|
| 4 | +This is the API layer — it manages the graph lifecycle and streams |
| 5 | +tokens and tool calls from the LangGraph graph to the Agentex frontend. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
| 11 | +from typing import AsyncGenerator |
| 12 | + |
| 13 | +from dotenv import load_dotenv |
| 14 | + |
| 15 | +load_dotenv() |
| 16 | + |
| 17 | +import agentex.lib.adk as adk |
| 18 | +from project.graph import create_graph |
| 19 | +from agentex.lib.adk import create_langgraph_tracing_handler, convert_langgraph_to_agentex_events |
| 20 | +from agentex.lib.types.acp import SendMessageParams |
| 21 | +from agentex.lib.types.tracing import SGPTracingProcessorConfig |
| 22 | +from agentex.lib.utils.logging import make_logger |
| 23 | +from agentex.lib.sdk.fastacp.fastacp import FastACP |
| 24 | +from agentex.types.task_message_delta import TextDelta |
| 25 | +from agentex.types.task_message_update import TaskMessageUpdate |
| 26 | +from agentex.types.task_message_content import TaskMessageContent |
| 27 | +from agentex.lib.core.tracing.tracing_processor_manager import add_tracing_processor_config |
| 28 | + |
| 29 | +logger = make_logger(__name__) |
| 30 | + |
| 31 | +# Register the Agentex tracing processor so spans are shipped to the backend |
| 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 | +# Create ACP server |
| 39 | +acp = FastACP.create(acp_type="sync") |
| 40 | + |
| 41 | +# Compiled graph (lazy-initialized on first request) |
| 42 | +_graph = None |
| 43 | + |
| 44 | + |
| 45 | +async def get_graph(): |
| 46 | + """Get or create the compiled graph instance.""" |
| 47 | + global _graph |
| 48 | + if _graph is None: |
| 49 | + _graph = await create_graph() |
| 50 | + return _graph |
| 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 | + graph = await get_graph() |
| 59 | + |
| 60 | + thread_id = params.task.id |
| 61 | + user_message = params.content.content |
| 62 | + |
| 63 | + logger.info(f"Processing message for thread {thread_id}") |
| 64 | + |
| 65 | + async with adk.tracing.span( |
| 66 | + trace_id=thread_id, |
| 67 | + name="message", |
| 68 | + input={"message": user_message}, |
| 69 | + data={"__span_type__": "AGENT_WORKFLOW"}, |
| 70 | + ) as turn_span: |
| 71 | + callback = create_langgraph_tracing_handler( |
| 72 | + trace_id=thread_id, |
| 73 | + parent_span_id=turn_span.id if turn_span else None, |
| 74 | + ) |
| 75 | + |
| 76 | + stream = graph.astream( |
| 77 | + {"messages": [{"role": "user", "content": user_message}]}, |
| 78 | + config={ |
| 79 | + "configurable": {"thread_id": thread_id}, |
| 80 | + "callbacks": [callback], |
| 81 | + }, |
| 82 | + stream_mode=["messages", "updates"], |
| 83 | + ) |
| 84 | + |
| 85 | + final_text = "" |
| 86 | + async for event in convert_langgraph_to_agentex_events(stream): |
| 87 | + # Accumulate text deltas for span output |
| 88 | + delta = getattr(event, "delta", None) |
| 89 | + if isinstance(delta, TextDelta) and delta.text_delta: |
| 90 | + final_text += delta.text_delta |
| 91 | + yield event |
| 92 | + |
| 93 | + if turn_span: |
| 94 | + turn_span.output = {"final_output": final_text} |
0 commit comments