|
| 1 | +"""ACP handler for the lineage tutorial. |
| 2 | +
|
| 3 | +Identical harness wiring to ``00_sync/050_openai_agents``: ``Runner.run_streamed`` |
| 4 | +wrapped in an ``OpenAITurn``, delivered through ``UnifiedEmitter.yield_turn``. |
| 5 | +Lineage capture rides that surface, and the SGP tracing processor stamps |
| 6 | +``__agent_version__`` from the ``AGENT_VERSION`` env var on every span. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import os |
| 12 | +from typing import AsyncGenerator |
| 13 | + |
| 14 | +from dotenv import load_dotenv |
| 15 | + |
| 16 | +load_dotenv() |
| 17 | + |
| 18 | +from agents import Runner |
| 19 | + |
| 20 | +from agentex.lib import adk |
| 21 | +from project.agent import MODEL_NAME, create_agent |
| 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.lib.core.harness.emitter import UnifiedEmitter |
| 27 | +from agentex.types.task_message_update import TaskMessageUpdate |
| 28 | +from agentex.types.task_message_content import TaskMessageContent |
| 29 | +from agentex.lib.adk.providers._modules.openai_turn import OpenAITurn |
| 30 | +from agentex.lib.core.tracing.tracing_processor_manager import add_tracing_processor_config |
| 31 | + |
| 32 | +logger = make_logger(__name__) |
| 33 | + |
| 34 | +# LiteLLM proxy auth: copy LITELLM_API_KEY to OPENAI_API_KEY for OpenAI client |
| 35 | +# compatibility, so the same example works behind the Scale LiteLLM gateway. |
| 36 | +_litellm_key = os.environ.get("LITELLM_API_KEY") |
| 37 | +if _litellm_key and not os.environ.get("OPENAI_API_KEY"): |
| 38 | + os.environ["OPENAI_API_KEY"] = _litellm_key |
| 39 | + |
| 40 | +add_tracing_processor_config( |
| 41 | + SGPTracingProcessorConfig( |
| 42 | + sgp_api_key=os.environ.get("SGP_API_KEY", ""), |
| 43 | + sgp_account_id=os.environ.get("SGP_ACCOUNT_ID", ""), |
| 44 | + sgp_base_url=os.environ.get("SGP_CLIENT_BASE_URL", ""), |
| 45 | + ) |
| 46 | +) |
| 47 | + |
| 48 | +acp = FastACP.create(acp_type="sync") |
| 49 | + |
| 50 | +_agent = None |
| 51 | + |
| 52 | + |
| 53 | +def get_agent(): |
| 54 | + """Get or create the OpenAI Agents SDK agent instance.""" |
| 55 | + global _agent |
| 56 | + if _agent is None: |
| 57 | + _agent = create_agent() |
| 58 | + return _agent |
| 59 | + |
| 60 | + |
| 61 | +@acp.on_message_send |
| 62 | +async def handle_message_send( |
| 63 | + params: SendMessageParams, |
| 64 | +) -> TaskMessageContent | list[TaskMessageContent] | AsyncGenerator[TaskMessageUpdate, None]: |
| 65 | + """Handle incoming messages, streaming tokens and tool calls via the harness.""" |
| 66 | + agent = get_agent() |
| 67 | + task_id = params.task.id |
| 68 | + user_message = params.content.content |
| 69 | + logger.info(f"Processing message for task {task_id}") |
| 70 | + |
| 71 | + async with adk.tracing.span( |
| 72 | + trace_id=task_id, |
| 73 | + task_id=task_id, |
| 74 | + name="message", |
| 75 | + input={"message": user_message}, |
| 76 | + data={"__span_type__": "AGENT_WORKFLOW"}, |
| 77 | + ) as turn_span: |
| 78 | + result = Runner.run_streamed(starting_agent=agent, input=user_message) |
| 79 | + turn = OpenAITurn(result=result, model=MODEL_NAME) |
| 80 | + emitter = UnifiedEmitter( |
| 81 | + task_id=task_id, |
| 82 | + trace_id=task_id, |
| 83 | + parent_span_id=turn_span.id if turn_span else None, |
| 84 | + ) |
| 85 | + async for event in emitter.yield_turn(turn): |
| 86 | + yield event |
0 commit comments