|
1 | 1 | """Helper functions for the A2A client.""" |
2 | 2 |
|
| 3 | +from typing import Any |
3 | 4 | from uuid import uuid4 |
4 | 5 |
|
5 | | -from a2a.types.a2a_pb2 import Message, Part, Role |
| 6 | +from google.protobuf.json_format import ParseDict |
| 7 | + |
| 8 | +from a2a.types.a2a_pb2 import AgentCard, Message, Part, Role |
| 9 | + |
| 10 | + |
| 11 | +def parse_agent_card(agent_card_data: dict[str, Any]) -> AgentCard: |
| 12 | + """Parse AgentCard JSON dictionary and handle backward compatibility.""" |
| 13 | + _handle_extended_card_compatibility(agent_card_data) |
| 14 | + _handle_connection_fields_compatibility(agent_card_data) |
| 15 | + _handle_security_compatibility(agent_card_data) |
| 16 | + |
| 17 | + return ParseDict(agent_card_data, AgentCard(), ignore_unknown_fields=True) |
| 18 | + |
| 19 | + |
| 20 | +def _handle_extended_card_compatibility( |
| 21 | + agent_card_data: dict[str, Any], |
| 22 | +) -> None: |
| 23 | + """Map legacy supportsAuthenticatedExtendedCard to capabilities.""" |
| 24 | + supports_extended_card = agent_card_data.pop( |
| 25 | + 'supportsAuthenticatedExtendedCard', None |
| 26 | + ) |
| 27 | + if supports_extended_card is None: |
| 28 | + supports_extended_card = agent_card_data.pop( |
| 29 | + 'supports_authenticated_extended_card', None |
| 30 | + ) |
| 31 | + |
| 32 | + if supports_extended_card: |
| 33 | + capabilities = agent_card_data.setdefault('capabilities', {}) |
| 34 | + if ( |
| 35 | + 'extendedAgentCard' not in capabilities |
| 36 | + and 'extended_agent_card' not in capabilities |
| 37 | + ): |
| 38 | + capabilities['extendedAgentCard'] = True |
| 39 | + |
| 40 | + |
| 41 | +def _handle_connection_fields_compatibility( |
| 42 | + agent_card_data: dict[str, Any], |
| 43 | +) -> None: |
| 44 | + """Map legacy connection and transport fields to supportedInterfaces.""" |
| 45 | + main_url = agent_card_data.pop('url', None) |
| 46 | + |
| 47 | + main_transport = agent_card_data.pop('preferredTransport', None) |
| 48 | + if main_transport is None: |
| 49 | + main_transport = agent_card_data.pop('preferred_transport', 'JSONRPC') |
| 50 | + |
| 51 | + version = agent_card_data.pop('protocolVersion', None) |
| 52 | + if version is None: |
| 53 | + version = agent_card_data.pop('protocol_version', '0.3.0') |
| 54 | + |
| 55 | + additional_interfaces = agent_card_data.pop('additionalInterfaces', None) |
| 56 | + if additional_interfaces is None: |
| 57 | + additional_interfaces = agent_card_data.pop('additional_interfaces', []) |
| 58 | + |
| 59 | + if ( |
| 60 | + 'supportedInterfaces' not in agent_card_data |
| 61 | + and 'supported_interfaces' not in agent_card_data |
| 62 | + and main_url |
| 63 | + ): |
| 64 | + supported_interfaces = [] |
| 65 | + supported_interfaces.append( |
| 66 | + { |
| 67 | + 'url': main_url, |
| 68 | + 'protocolBinding': main_transport, |
| 69 | + 'protocolVersion': version, |
| 70 | + } |
| 71 | + ) |
| 72 | + supported_interfaces.extend( |
| 73 | + { |
| 74 | + 'url': iface.get('url'), |
| 75 | + 'protocolBinding': iface.get('transport'), |
| 76 | + 'protocolVersion': version, |
| 77 | + } |
| 78 | + for iface in additional_interfaces |
| 79 | + ) |
| 80 | + agent_card_data['supportedInterfaces'] = supported_interfaces |
| 81 | + |
| 82 | + |
| 83 | +def _map_legacy_security(sec_list: list[dict[str, list[str]]]) -> list[dict[str, Any]]: |
| 84 | + """Convert a legacy security requirement list into the 1.0.0 Protobuf format.""" |
| 85 | + return [ |
| 86 | + { |
| 87 | + 'schemes': { |
| 88 | + scheme_name: {'list': scopes} |
| 89 | + for scheme_name, scopes in sec_dict.items() |
| 90 | + } |
| 91 | + } |
| 92 | + for sec_dict in sec_list |
| 93 | + ] |
| 94 | + |
| 95 | + |
| 96 | +def _handle_security_compatibility(agent_card_data: dict[str, Any]) -> None: |
| 97 | + """Map legacy security requirements and schemas to their 1.0.0 Protobuf equivalents.""" |
| 98 | + legacy_security = agent_card_data.pop('security', None) |
| 99 | + if ( |
| 100 | + 'securityRequirements' not in agent_card_data |
| 101 | + and legacy_security is not None |
| 102 | + ): |
| 103 | + agent_card_data['securityRequirements'] = _map_legacy_security( |
| 104 | + legacy_security |
| 105 | + ) |
| 106 | + |
| 107 | + for skill in agent_card_data.get('skills', []): |
| 108 | + legacy_skill_sec = skill.pop('security', None) |
| 109 | + if 'securityRequirements' not in skill and legacy_skill_sec is not None: |
| 110 | + skill['securityRequirements'] = _map_legacy_security( |
| 111 | + legacy_skill_sec |
| 112 | + ) |
| 113 | + |
| 114 | + if 'securitySchemes' in agent_card_data: |
| 115 | + type_mapping = { |
| 116 | + 'apiKey': 'apiKeySecurityScheme', |
| 117 | + 'http': 'httpAuthSecurityScheme', |
| 118 | + 'oauth2': 'oauth2SecurityScheme', |
| 119 | + 'openIdConnect': 'openIdConnectSecurityScheme', |
| 120 | + 'mutualTLS': 'mtlsSecurityScheme', |
| 121 | + } |
| 122 | + for scheme in agent_card_data['securitySchemes'].values(): |
| 123 | + scheme_type = scheme.pop('type', None) |
| 124 | + if scheme_type in type_mapping: |
| 125 | + # Map legacy 'in' to modern 'location' |
| 126 | + if scheme_type == 'apiKey' and 'in' in scheme: |
| 127 | + scheme['location'] = scheme.pop('in') |
| 128 | + |
| 129 | + mapped_name = type_mapping[scheme_type] |
| 130 | + new_scheme_wrapper = {mapped_name: scheme.copy()} |
| 131 | + scheme.clear() |
| 132 | + scheme.update(new_scheme_wrapper) |
6 | 133 |
|
7 | 134 |
|
8 | 135 | def create_text_message_object( |
|
0 commit comments