|
1 | 1 | from agentex.lib.sdk.fastacp.fastacp import FastACP |
2 | 2 | from agentex.lib.types.fastacp import AsyncACPConfig |
3 | 3 | from agentex.lib.types.acp import SendEventParams, CancelTaskParams, CreateTaskParams |
| 4 | +from agentex.lib.utils.logging import make_logger |
| 5 | +from agentex.types.text_content import TextContent |
| 6 | +from agentex.lib import adk |
| 7 | + |
| 8 | + |
| 9 | +logger = make_logger(__name__) |
4 | 10 |
|
5 | 11 |
|
6 | 12 | # Create an ACP server |
| 13 | +# This sets up the core server that will handle task creation, events, and cancellation |
| 14 | +# The `type="base"` configuration is the default configuration for the ACP server |
7 | 15 | acp = FastACP.create( |
8 | 16 | acp_type="async", |
9 | | - config=AsyncACPConfig(type="base") |
| 17 | + config=AsyncACPConfig( |
| 18 | + type="base", |
| 19 | + ), |
10 | 20 | ) |
11 | 21 |
|
12 | 22 |
|
| 23 | +# This handler is called first whenever a new task is created. |
| 24 | +# It's a good place to initialize any state or resources needed for the task. |
13 | 25 | @acp.on_task_event_send |
14 | 26 | async def handle_task_event_send(params: SendEventParams): |
15 | | - # For this tutorial, we print the parameters sent to the handler |
16 | | - # so you can see where and how messages within a task are handled |
17 | | - print(f"Hello world! I just received this message: {params}") |
| 27 | + # For this tutorial, we log the parameters sent to the handler |
| 28 | + # so you can see where and how messages within a long running task are handled |
| 29 | + logger.info(f"Received task event send rpc: {params}") |
| 30 | + |
| 31 | + # 1. Echo back the client's message to show it in the UI. This is not done by default so the agent developer has full control over what is shown to the user. |
| 32 | + await adk.messages.create(task_id=params.task.id, content=params.event.content) |
| 33 | + |
| 34 | + # 2. Send a simple response message. |
| 35 | + # In future tutorials, this is where we'll add more sophisticated response logic. |
| 36 | + await adk.messages.create( |
| 37 | + task_id=params.task.id, |
| 38 | + content=TextContent( |
| 39 | + author="agent", |
| 40 | + content=f"Hello! I've received your message. I can't respond right now, but in future tutorials we'll see how you can get me to intelligently respond to your message.", |
| 41 | + ), |
| 42 | + ) |
18 | 43 |
|
19 | 44 | @acp.on_task_cancel |
20 | 45 | async def handle_task_canceled(params: CancelTaskParams): |
21 | 46 | # For this tutorial, we print the parameters sent to the handler |
22 | 47 | # so you can see where and how task cancellation is handled |
23 | | - print(f"Hello world! Task canceled: {params.task.id}") |
| 48 | + logger.info(f"Received task cancel rpc: {params}") |
24 | 49 |
|
25 | 50 | @acp.on_task_create |
26 | 51 | async def handle_task_create(params: CreateTaskParams): |
27 | | - # For this tutorial, we print the parameters sent to the handler |
| 52 | + # For this tutorial, we log the parameters sent to the handler |
28 | 53 | # so you can see where and how task creation is handled |
29 | | - print(f"Hello world! Task created: {params.task.id}") |
| 54 | + |
| 55 | + # Here is where you can initialize any state or resources needed for the task. |
| 56 | + logger.info(f"Received task create rpc: {params}") |
0 commit comments