2020DEFAULT_TIMEOUT = 300
2121
2222
23- def create_message (* , role : Role = Role .user , text : str , context_id : str | None = None ) -> Message :
23+ def create_message (
24+ * , role : Role = Role .user , text : str , context_id : str | None = None
25+ ) -> Message :
2426 return Message (
2527 kind = "message" ,
2628 role = role ,
2729 parts = [Part (TextPart (kind = "text" , text = text ))],
2830 message_id = uuid4 ().hex ,
29- context_id = context_id
31+ context_id = context_id ,
3032 )
3133
34+
3235def merge_parts (parts : list [Part ]) -> str :
3336 chunks = []
3437 for part in parts :
@@ -38,9 +41,17 @@ def merge_parts(parts: list[Part]) -> str:
3841 chunks .append (json .dumps (part .root .data , indent = 2 ))
3942 return "\n " .join (chunks )
4043
41- async def send_message (message : str , base_url : str , context_id : str | None = None , streaming = False , consumer : Consumer | None = None ):
44+
45+ async def send_message (
46+ message : str ,
47+ base_url : str ,
48+ context_id : str | None = None ,
49+ streaming : bool = False ,
50+ timeout : int = DEFAULT_TIMEOUT ,
51+ consumer : Consumer | None = None ,
52+ ):
4253 """Returns dict with context_id, response and status (if exists)"""
43- async with httpx .AsyncClient (timeout = DEFAULT_TIMEOUT ) as httpx_client :
54+ async with httpx .AsyncClient (timeout = timeout ) as httpx_client :
4455 resolver = A2ACardResolver (httpx_client = httpx_client , base_url = base_url )
4556 agent_card = await resolver .get_agent_card ()
4657 config = ClientConfig (
@@ -54,10 +65,7 @@ async def send_message(message: str, base_url: str, context_id: str | None = Non
5465
5566 outbound_msg = create_message (text = message , context_id = context_id )
5667 last_event = None
57- outputs = {
58- "response" : "" ,
59- "context_id" : None
60- }
68+ outputs = {"response" : "" , "context_id" : None }
6169
6270 # if streaming == False, only one event is generated
6371 async for event in client .send_message (outbound_msg ):
@@ -88,19 +96,31 @@ class Messenger:
8896 def __init__ (self ):
8997 self ._context_ids = {}
9098
91- async def talk_to_agent (self , message : str , url : str , new_conversation : bool = False ):
99+ async def talk_to_agent (
100+ self ,
101+ message : str ,
102+ url : str ,
103+ new_conversation : bool = False ,
104+ timeout : int = DEFAULT_TIMEOUT ,
105+ ):
92106 """
93107 Communicate with another agent by sending a message and receiving their response.
94108
95109 Args:
96110 message: The message to send to the agent
97111 url: The agent's URL endpoint
98112 new_conversation: If True, start fresh conversation; if False, continue existing conversation
113+ timeout: Timeout in seconds for the request (default: 300)
99114
100115 Returns:
101116 str: The agent's response message
102117 """
103- outputs = await send_message (message = message , base_url = url , context_id = None if new_conversation else self ._context_ids .get (url , None ))
118+ outputs = await send_message (
119+ message = message ,
120+ base_url = url ,
121+ context_id = None if new_conversation else self ._context_ids .get (url , None ),
122+ timeout = timeout ,
123+ )
104124 if outputs .get ("status" , "completed" ) != "completed" :
105125 raise RuntimeError (f"{ url } responded with: { outputs } " )
106126 self ._context_ids [url ] = outputs .get ("context_id" , None )
0 commit comments