99from google .protobuf .json_format import MessageToDict , ParseDict
1010
1111from a2a .compat .v0_3 import types as types_v03
12+ from a2a .compat .v0_3 .versions import is_legacy_version
1213from a2a .server .models import PushNotificationConfigModel , TaskModel
1314from a2a .types import a2a_pb2 as pb2_v10
15+ from a2a .utils import constants , errors
1416
1517
1618_COMPAT_TO_CORE_TASK_STATE : dict [types_v03 .TaskState , Any ] = {
@@ -676,7 +678,7 @@ def to_core_agent_interface(
676678 return pb2_v10 .AgentInterface (
677679 url = compat_interface .url ,
678680 protocol_binding = compat_interface .transport ,
679- protocol_version = '0.3.0' , # Defaulting for legacy
681+ protocol_version = constants . PROTOCOL_VERSION_0_3 , # Defaulting for legacy
680682 )
681683
682684
@@ -857,7 +859,8 @@ def to_core_agent_card(compat_card: types_v03.AgentCard) -> pb2_v10.AgentCard:
857859 primary_interface = pb2_v10 .AgentInterface (
858860 url = compat_card .url ,
859861 protocol_binding = compat_card .preferred_transport or 'JSONRPC' ,
860- protocol_version = compat_card .protocol_version or '0.3.0' ,
862+ protocol_version = compat_card .protocol_version
863+ or constants .PROTOCOL_VERSION_0_3 ,
861864 )
862865 core_card .supported_interfaces .append (primary_interface )
863866
@@ -918,21 +921,23 @@ def to_core_agent_card(compat_card: types_v03.AgentCard) -> pb2_v10.AgentCard:
918921def to_compat_agent_card (core_card : pb2_v10 .AgentCard ) -> types_v03 .AgentCard :
919922 # Map supported interfaces back to legacy layout
920923 """Convert agent card to v0.3 compat type."""
921- primary_interface = (
922- core_card .supported_interfaces [0 ]
923- if core_card .supported_interfaces
924- else pb2_v10 .AgentInterface (
925- url = '' , protocol_binding = 'JSONRPC' , protocol_version = '0.3.0'
924+ compat_interfaces = [
925+ interface
926+ for interface in core_card .supported_interfaces
927+ if (
928+ (not interface .protocol_version )
929+ or is_legacy_version (interface .protocol_version )
926930 )
927- )
928- additional_interfaces = (
929- [
930- to_compat_agent_interface (i )
931- for i in core_card .supported_interfaces [1 :]
932- ]
933- if len (core_card .supported_interfaces ) > 1
934- else None
935- )
931+ ]
932+ if not compat_interfaces :
933+ raise errors .VersionNotSupportedError (
934+ 'AgentCard must have at least one interface with compatible protocol version.'
935+ )
936+
937+ primary_interface = compat_interfaces [0 ]
938+ additional_interfaces = [
939+ to_compat_agent_interface (i ) for i in compat_interfaces [1 :]
940+ ]
936941
937942 compat_cap = to_compat_agent_capabilities (core_card .capabilities )
938943 supports_authenticated_extended_card = (
@@ -947,8 +952,9 @@ def to_compat_agent_card(core_card: pb2_v10.AgentCard) -> types_v03.AgentCard:
947952 version = core_card .version ,
948953 url = primary_interface .url ,
949954 preferred_transport = primary_interface .protocol_binding ,
950- protocol_version = primary_interface .protocol_version ,
951- additional_interfaces = additional_interfaces ,
955+ protocol_version = primary_interface .protocol_version
956+ or constants .PROTOCOL_VERSION_0_3 ,
957+ additional_interfaces = additional_interfaces or None ,
952958 provider = to_compat_agent_provider (core_card .provider )
953959 if core_card .HasField ('provider' )
954960 else None ,
0 commit comments