Skip to content

Commit a14b2db

Browse files
committed
Update acp.py
1 parent c27c827 commit a14b2db

File tree

1 file changed

+55
-1
lines changed
  • examples/tutorials/00_sync/010_multiturn/project

1 file changed

+55
-1
lines changed

examples/tutorials/00_sync/010_multiturn/project/acp.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
11
import os
2+
import json
23
from typing import AsyncGenerator, Union
34

45
from agentex.lib import adk
6+
from agentex.lib.adk.utils._modules.client import create_async_agentex_client
57
from agentex.lib.sdk.fastacp.fastacp import FastACP
68
from agentex.lib.types.acp import SendMessageParams
79
from agentex.lib.types.llm_messages import AssistantMessage, LLMConfig, SystemMessage, UserMessage
810
from agentex.lib.types.task_message_updates import TaskMessageUpdate
911
from agentex.types.task_message import TaskMessageContent
1012
from agentex.types.task_message_content import TextContent
1113
from agentex.lib.utils.model_utils import BaseModel
14+
from fastapi.responses import JSONResponse
15+
from fastapi import Request
16+
from agentex.lib.environment_variables import EnvironmentVariables
1217

1318
# Create an ACP server
1419
acp = FastACP.create(
1520
acp_type="sync",
1621
)
17-
22+
agentex_client = create_async_agentex_client()
1823

1924
class StateModel(BaseModel):
2025
system_prompt: str
2126
model: str
2227

2328

29+
@acp.get("/webhook")
30+
@acp.post("/webhook")
31+
async def handle_webhook(request: Request) -> JSONResponse:
32+
agent_id = None
33+
env_vars = EnvironmentVariables.refresh()
34+
if env_vars:
35+
agent_id = env_vars.AGENT_ID
36+
if not agent_id:
37+
# No agent ID found, this agent is not registered with the Agentex Server.
38+
return JSONResponse(content={"message": "This agent is not registered with the Agentex Server."})
39+
40+
task = None
41+
task_name = f"{agent_id}_webhook_test"
42+
try:
43+
task = await adk.tasks.get(task_name=task_name)
44+
except Exception:
45+
# If the task doesn't exist, create it.
46+
task = await agentex_client.agents.rpc(
47+
agent_id=agent_id,
48+
method="task/create",
49+
params={
50+
"name": task_name,
51+
}
52+
)
53+
54+
#print(task)
55+
56+
if not task:
57+
return JSONResponse(content={"message": "Failed to get named task."})
58+
59+
message_data = {
60+
"message": "Incoming webhook request received.",
61+
"request": {
62+
"method": request.method,
63+
"headers": dict(request.headers),
64+
"body": await request.json(),
65+
}
66+
}
67+
68+
# Use TextContent to see the full message in UI
69+
await adk.messages.create(
70+
task_id=task.id,
71+
content=TextContent(
72+
author="agent",
73+
content=json.dumps(message_data, indent=4),
74+
)
75+
)
76+
return JSONResponse(content={"message": "Success"})
77+
2478
# Note: The return of this handler is required to be persisted by the Agentex Server
2579
@acp.on_message_send
2680
async def handle_message_send(

0 commit comments

Comments
 (0)