33from __future__ import annotations
44
55import json
6- from typing import Union , Optional , Generator , AsyncGenerator
6+ from typing import Any , Union , Optional , Generator , AsyncGenerator
77from typing_extensions import Literal
88
99import httpx
@@ -359,7 +359,7 @@ def create_task(
359359 ) -> CreateTaskResponse :
360360 if agent_id is not None and agent_name is not None :
361361 raise ValueError ("Either agent_id or agent_name must be provided, but not both" )
362-
362+
363363 if agent_id is not None :
364364 raw_agent_rpc_response = self .rpc (
365365 agent_id = agent_id ,
@@ -386,7 +386,7 @@ def create_task(
386386 )
387387 else :
388388 raise ValueError ("Either agent_id or agent_name must be provided" )
389-
389+
390390 return CreateTaskResponse .model_validate (raw_agent_rpc_response , from_attributes = True )
391391
392392 def cancel_task (
@@ -453,38 +453,71 @@ def send_message(
453453 ) -> SendMessageResponse :
454454 if agent_id is not None and agent_name is not None :
455455 raise ValueError ("Either agent_id or agent_name must be provided, but not both" )
456-
456+
457457 if "stream" in params and params ["stream" ] == True :
458458 raise ValueError ("If stream is set to True, use send_message_stream() instead" )
459+
460+ if agent_id is not None :
461+ raw_agent_rpc_response = self .with_streaming_response .rpc (
462+ agent_id = agent_id ,
463+ method = "message/send" ,
464+ params = params ,
465+ id = id ,
466+ jsonrpc = jsonrpc ,
467+ extra_headers = extra_headers ,
468+ extra_query = extra_query ,
469+ extra_body = extra_body ,
470+ timeout = timeout ,
471+ )
472+ elif agent_name is not None :
473+ raw_agent_rpc_response = self .with_streaming_response .rpc_by_name (
474+ agent_name = agent_name ,
475+ method = "message/send" ,
476+ params = params ,
477+ id = id ,
478+ jsonrpc = jsonrpc ,
479+ extra_headers = extra_headers ,
480+ extra_query = extra_query ,
481+ extra_body = extra_body ,
482+ timeout = timeout ,
483+ )
459484 else :
460- if agent_id is not None :
461- raw_agent_rpc_response = self .rpc (
462- agent_id = agent_id ,
463- method = "message/send" ,
464- params = params ,
465- id = id ,
466- jsonrpc = jsonrpc ,
467- extra_headers = extra_headers ,
468- extra_query = extra_query ,
469- extra_body = extra_body ,
470- timeout = timeout ,
471- )
472- elif agent_name is not None :
473- raw_agent_rpc_response = self .rpc_by_name (
474- agent_name = agent_name ,
475- method = "message/send" ,
476- params = params ,
477- id = id ,
478- jsonrpc = jsonrpc ,
479- extra_headers = extra_headers ,
480- extra_query = extra_query ,
481- extra_body = extra_body ,
482- timeout = timeout ,
483- )
484- else :
485- raise ValueError ("Either agent_id or agent_name must be provided" )
486-
487- return SendMessageResponse .model_validate (raw_agent_rpc_response , from_attributes = True )
485+ raise ValueError ("Either agent_id or agent_name must be provided" )
486+
487+ task_messages : list [Any ] = []
488+ response_meta : dict [str , Any ] = {}
489+
490+ with raw_agent_rpc_response as response :
491+ for _line in response .iter_lines ():
492+ if not _line :
493+ continue
494+ line = _line .strip ()
495+ if line .startswith ("data:" ):
496+ line = line [len ("data:" ):].strip ()
497+ if not line :
498+ continue
499+ try :
500+ chunk = json .loads (line )
501+ if not response_meta :
502+ response_meta = {"id" : chunk .get ("id" ), "jsonrpc" : chunk .get ("jsonrpc" )}
503+ try :
504+ return SendMessageResponse .model_validate (chunk )
505+ except ValidationError :
506+ pass
507+ chunk_stream = SendMessageStreamResponse .model_validate (chunk , from_attributes = True )
508+ result = chunk_stream .result
509+ if result is not None and getattr (result , "type" , None ) == "full" :
510+ parent = getattr (result , "parent_task_message" , None )
511+ if parent is not None :
512+ task_messages .append (parent )
513+ except (json .JSONDecodeError , ValidationError ):
514+ continue
515+
516+ return SendMessageResponse (
517+ id = response_meta .get ("id" ),
518+ jsonrpc = response_meta .get ("jsonrpc" ),
519+ result = task_messages ,
520+ )
488521
489522 def send_message_stream (
490523 self ,
@@ -552,8 +585,8 @@ def send_message_stream(
552585 from_attributes = True
553586 )
554587 yield chunk_rpc_response
555- except json .JSONDecodeError :
556- # Skip invalid JSON lines
588+ except ( json .JSONDecodeError , ValidationError ) :
589+ # Skip invalid JSON lines or lines that cannot be validated
557590 continue
558591
559592 def send_event (
@@ -1021,38 +1054,71 @@ async def send_message(
10211054 ) -> SendMessageResponse :
10221055 if agent_id is not None and agent_name is not None :
10231056 raise ValueError ("Either agent_id or agent_name must be provided, but not both" )
1024-
1057+
10251058 if "stream" in params and params ["stream" ] == True :
10261059 raise ValueError ("If stream is set to True, use send_message_stream() instead" )
1060+
1061+ if agent_id is not None :
1062+ raw_agent_rpc_response = self .with_streaming_response .rpc (
1063+ agent_id = agent_id ,
1064+ method = "message/send" ,
1065+ params = params ,
1066+ id = id ,
1067+ jsonrpc = jsonrpc ,
1068+ extra_headers = extra_headers ,
1069+ extra_query = extra_query ,
1070+ extra_body = extra_body ,
1071+ timeout = timeout ,
1072+ )
1073+ elif agent_name is not None :
1074+ raw_agent_rpc_response = self .with_streaming_response .rpc_by_name (
1075+ agent_name = agent_name ,
1076+ method = "message/send" ,
1077+ params = params ,
1078+ id = id ,
1079+ jsonrpc = jsonrpc ,
1080+ extra_headers = extra_headers ,
1081+ extra_query = extra_query ,
1082+ extra_body = extra_body ,
1083+ timeout = timeout ,
1084+ )
10271085 else :
1028- if agent_id is not None :
1029- raw_agent_rpc_response = await self .rpc (
1030- agent_id = agent_id ,
1031- method = "message/send" ,
1032- params = params ,
1033- id = id ,
1034- jsonrpc = jsonrpc ,
1035- extra_headers = extra_headers ,
1036- extra_query = extra_query ,
1037- extra_body = extra_body ,
1038- timeout = timeout ,
1039- )
1040- elif agent_name is not None :
1041- raw_agent_rpc_response = await self .rpc_by_name (
1042- agent_name = agent_name ,
1043- method = "message/send" ,
1044- params = params ,
1045- id = id ,
1046- jsonrpc = jsonrpc ,
1047- extra_headers = extra_headers ,
1048- extra_query = extra_query ,
1049- extra_body = extra_body ,
1050- timeout = timeout ,
1051- )
1052- else :
1053- raise ValueError ("Either agent_id or agent_name must be provided" )
1054-
1055- return SendMessageResponse .model_validate (raw_agent_rpc_response , from_attributes = True )
1086+ raise ValueError ("Either agent_id or agent_name must be provided" )
1087+
1088+ task_messages : list [Any ] = []
1089+ response_meta : dict [str , Any ] = {}
1090+
1091+ async with raw_agent_rpc_response as response :
1092+ async for _line in response .iter_lines ():
1093+ if not _line :
1094+ continue
1095+ line = _line .strip ()
1096+ if line .startswith ("data:" ):
1097+ line = line [len ("data:" ):].strip ()
1098+ if not line :
1099+ continue
1100+ try :
1101+ chunk = json .loads (line )
1102+ if not response_meta :
1103+ response_meta = {"id" : chunk .get ("id" ), "jsonrpc" : chunk .get ("jsonrpc" )}
1104+ try :
1105+ return SendMessageResponse .model_validate (chunk )
1106+ except ValidationError :
1107+ pass
1108+ chunk_stream = SendMessageStreamResponse .model_validate (chunk , from_attributes = True )
1109+ result = chunk_stream .result
1110+ if result is not None and getattr (result , "type" , None ) == "full" :
1111+ parent = getattr (result , "parent_task_message" , None )
1112+ if parent is not None :
1113+ task_messages .append (parent )
1114+ except (json .JSONDecodeError , ValidationError ):
1115+ continue
1116+
1117+ return SendMessageResponse (
1118+ id = response_meta .get ("id" ),
1119+ jsonrpc = response_meta .get ("jsonrpc" ),
1120+ result = task_messages ,
1121+ )
10561122
10571123 async def send_message_stream (
10581124 self ,
0 commit comments