Skip to content

Commit 0d939db

Browse files
committed
check against server hash
1 parent 66c3959 commit 0d939db

File tree

4 files changed

+11
-27
lines changed

4 files changed

+11
-27
lines changed

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

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

5-
65
from agentex.lib.types.task_message_updates import TaskMessageUpdate
76
from agentex.types.task_message import TaskMessageContent
87
from agentex.types.task_message_content import TextContent
98
from agentex.lib.utils.logging import make_logger
10-
from agentex.lib.utils.model_utils import BaseModel
119

1210
logger = make_logger(__name__)
1311

@@ -18,27 +16,12 @@
1816
)
1917

2018

21-
class StateModel(BaseModel):
22-
system_prompt: str
23-
model: str
24-
2519
@acp.on_message_send
2620
async def handle_message_send(
2721
params: SendMessageParams
2822
) -> Union[TaskMessageContent, AsyncGenerator[TaskMessageUpdate, None]]:
29-
from agentex.lib import adk
3023
"""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)
4024
return TextContent(
4125
author="agent",
4226
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}",
4327
)
44-

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

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

4-
4+
from agentex.lib import adk
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,7 +26,6 @@ class StateModel(BaseModel):
2626
async def handle_message_send(
2727
params: SendMessageParams
2828
) -> Union[TaskMessageContent, AsyncGenerator[TaskMessageUpdate, None]]:
29-
from agentex.lib import adk
3029
"""
3130
In this tutorial, we'll see how to handle a basic multi-turn conversation without streaming.
3231
"""

src/agentex/lib/environment_variables.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class EnvVarKeys(str, Enum):
2020
AGENT_NAME = "AGENT_NAME"
2121
AGENT_DESCRIPTION = "AGENT_DESCRIPTION"
2222
AGENT_ID = "AGENT_ID"
23+
AGENT_API_KEY = "AGENT_API_KEY"
24+
AGENT_API_KEY_HASH = "AGENT_API_KEY_HASH"
2325
# ACP Configuration
2426
ACP_URL = "ACP_URL"
2527
ACP_PORT = "ACP_PORT"
@@ -49,6 +51,8 @@ class EnvironmentVariables(BaseModel):
4951
AGENT_NAME: str
5052
AGENT_DESCRIPTION: str | None = None
5153
AGENT_ID: str | None = None
54+
AGENT_API_KEY: str | None = None
55+
AGENT_API_KEY_HASH: str | None = None
5256
ACP_TYPE: str | None = "agentic"
5357
# ACP Configuration
5458
ACP_URL: str

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import base64
3+
import hashlib
34
import inspect
45
import json
56
import os
@@ -102,14 +103,9 @@ async def _handle_jsonrpc(self, request: Request):
102103
rpc_request = JSONRPCRequest(**data)
103104

104105
# Check if the request is authenticated
105-
if os.environ.get("AGENT_API_KEY") is not None:
106+
if refreshed_environment_variables.AGENT_API_KEY_HASH:
106107
authorization_header = request.headers.get("Authorization")
107-
if authorization_header is None:
108-
return JSONRPCResponse(
109-
id=rpc_request.id,
110-
error=JSONRPCError(code=-32601, message="Unauthorized"),
111-
)
112-
if authorization_header != f"Bearer {os.environ.get("AGENT_API_KEY")}":
108+
if authorization_header != f"Bearer {refreshed_environment_variables.AGENT_API_KEY_HASH}":
113109
return JSONRPCResponse(
114110
id=rpc_request.id,
115111
error=JSONRPCError(code=-32601, message="Unauthorized"),
@@ -409,15 +405,17 @@ async def _register_agent(self, env_vars: EnvironmentVariables):
409405
if response.status_code == 200:
410406
agent = response.json()
411407
agent_id, agent_name = agent["id"], agent["name"]
412-
print(agent)
413408
agent_api_key = agent["agent_api_key"]
409+
agent_api_key_hash = hashlib.sha256(agent_api_key.encode()).hexdigest()
414410

415411
os.environ["AGENT_ID"] = agent_id
416412
os.environ["AGENT_NAME"] = agent_name
417413
os.environ["AGENT_API_KEY"] = agent_api_key
414+
os.environ["AGENT_API_KEY_HASH"] = agent_api_key_hash
418415
refreshed_environment_variables.AGENT_ID = agent_id
419416
refreshed_environment_variables.AGENT_NAME = agent_name
420417
refreshed_environment_variables.AGENT_API_KEY = agent_api_key
418+
refreshed_environment_variables.AGENT_API_KEY_HASH = agent_api_key_hash
421419
get_async_agentex_client() # refresh cache
422420
logger.info(
423421
f"Successfully registered agent '{env_vars.AGENT_NAME}' with Agentex server with acp_url: {full_acp_url}. Registration data: {registration_data}"

0 commit comments

Comments
 (0)