Skip to content

Commit 1fc6c59

Browse files
committed
.
1 parent b934090 commit 1fc6c59

File tree

4 files changed

+941
-909
lines changed

4 files changed

+941
-909
lines changed

examples/tutorials/00_sync/000_hello_acp/project/acp.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
from agentex.lib.sdk.fastacp.fastacp import FastACP
33
from agentex.lib.types.acp import SendMessageParams
44

5+
56
from agentex.lib.types.task_message_updates import TaskMessageUpdate
67
from agentex.types.task_message import TaskMessageContent
78
from agentex.types.task_message_content import TextContent
89
from agentex.lib.utils.logging import make_logger
10+
from agentex.lib.utils.model_utils import BaseModel
911

1012
logger = make_logger(__name__)
1113

@@ -16,11 +18,25 @@
1618
)
1719

1820

21+
class StateModel(BaseModel):
22+
system_prompt: str
23+
model: str
24+
1925
@acp.on_message_send
2026
async def handle_message_send(
2127
params: SendMessageParams
2228
) -> Union[TaskMessageContent, AsyncGenerator[TaskMessageUpdate, None]]:
29+
from agentex.lib import adk
2330
"""Default message handler with streaming support"""
31+
# Try to retrieve the state. If it doesn't exist, create it.
32+
task_state = await adk.state.get_by_task_and_agent(task_id=params.task.id, agent_id=params.agent.id)
33+
34+
if not task_state:
35+
# If the state doesn't exist, create it.
36+
state = StateModel(system_prompt="You are a helpful assistant that can answer questions.", model="gpt-4o-mini")
37+
task_state = await adk.state.create(task_id=params.task.id, agent_id=params.agent.id, state=state)
38+
else:
39+
state = StateModel.model_validate(task_state.state)
2440
return TextContent(
2541
author="agent",
2642
content=f"Hello! I've received your message. Here's a generic response, but in future tutorials we'll see how you can get me to intelligently respond to your message. This is what I heard you say: {params.content.content}",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from typing import AsyncGenerator, Union
33

4-
from agentex.lib import adk
4+
55
from agentex.lib.sdk.fastacp.fastacp import FastACP
66
from agentex.lib.types.acp import SendMessageParams
77
from agentex.lib.types.llm_messages import AssistantMessage, LLMConfig, SystemMessage, UserMessage
@@ -26,6 +26,7 @@ class StateModel(BaseModel):
2626
async def handle_message_send(
2727
params: SendMessageParams
2828
) -> Union[TaskMessageContent, AsyncGenerator[TaskMessageUpdate, None]]:
29+
from agentex.lib import adk
2930
"""
3031
In this tutorial, we'll see how to handle a basic multi-turn conversation without streaming.
3132
"""

src/agentex/lib/sdk/fastacp/base/base_acp_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ async def _handle_jsonrpc(self, request: Request):
100100
data = await request.json()
101101
rpc_request = JSONRPCRequest(**data)
102102

103+
# Check if the request is authenticated
104+
if os.environ.get("AGENT_API_KEY") is not None:
105+
authorization_header = request.headers.get("Authorization")
106+
if authorization_header is None:
107+
return JSONRPCResponse(
108+
id=rpc_request.id,
109+
error=JSONRPCError(code=-32601, message="Unauthorized"),
110+
)
111+
if authorization_header != f"Bearer {os.environ.get("AGENT_API_KEY")}":
112+
return JSONRPCResponse(
113+
id=rpc_request.id,
114+
error=JSONRPCError(code=-32601, message="Unauthorized"),
115+
)
116+
117+
103118
# Check if method is valid first
104119
try:
105120
method = RPCMethod(rpc_request.method)

0 commit comments

Comments
 (0)