|
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 | + if agent_card_data.pop('supportsAuthenticatedExtendedCard', None): |
| 25 | + capabilities = agent_card_data.setdefault('capabilities', {}) |
| 26 | + if 'extendedAgentCard' not in capabilities: |
| 27 | + capabilities['extendedAgentCard'] = True |
| 28 | + |
| 29 | + |
| 30 | +def _handle_connection_fields_compatibility( |
| 31 | + agent_card_data: dict[str, Any], |
| 32 | +) -> None: |
| 33 | + """Map legacy connection and transport fields to supportedInterfaces.""" |
| 34 | + main_url = agent_card_data.pop('url', None) |
| 35 | + main_transport = agent_card_data.pop('preferredTransport', 'JSONRPC') |
| 36 | + version = agent_card_data.pop('protocolVersion', '0.3.0') |
| 37 | + additional_interfaces = ( |
| 38 | + agent_card_data.pop('additionalInterfaces', None) or [] |
| 39 | + ) |
| 40 | + |
| 41 | + if 'supportedInterfaces' not in agent_card_data and main_url: |
| 42 | + supported_interfaces = [] |
| 43 | + supported_interfaces.append( |
| 44 | + { |
| 45 | + 'url': main_url, |
| 46 | + 'protocolBinding': main_transport, |
| 47 | + 'protocolVersion': version, |
| 48 | + } |
| 49 | + ) |
| 50 | + supported_interfaces.extend( |
| 51 | + { |
| 52 | + 'url': iface.get('url'), |
| 53 | + 'protocolBinding': iface.get('transport'), |
| 54 | + 'protocolVersion': version, |
| 55 | + } |
| 56 | + for iface in additional_interfaces |
| 57 | + ) |
| 58 | + agent_card_data['supportedInterfaces'] = supported_interfaces |
| 59 | + |
| 60 | + |
| 61 | +def _map_legacy_security( |
| 62 | + sec_list: list[dict[str, list[str]]], |
| 63 | +) -> list[dict[str, Any]]: |
| 64 | + """Convert a legacy security requirement list into the 1.0.0 Protobuf format.""" |
| 65 | + return [ |
| 66 | + { |
| 67 | + 'schemes': { |
| 68 | + scheme_name: {'list': scopes} |
| 69 | + for scheme_name, scopes in sec_dict.items() |
| 70 | + } |
| 71 | + } |
| 72 | + for sec_dict in sec_list |
| 73 | + ] |
| 74 | + |
| 75 | + |
| 76 | +def _handle_security_compatibility(agent_card_data: dict[str, Any]) -> None: |
| 77 | + """Map legacy security requirements and schemas to their 1.0.0 Protobuf equivalents.""" |
| 78 | + legacy_security = agent_card_data.pop('security', None) |
| 79 | + if ( |
| 80 | + 'securityRequirements' not in agent_card_data |
| 81 | + and legacy_security is not None |
| 82 | + ): |
| 83 | + agent_card_data['securityRequirements'] = _map_legacy_security( |
| 84 | + legacy_security |
| 85 | + ) |
| 86 | + |
| 87 | + for skill in agent_card_data.get('skills', []): |
| 88 | + legacy_skill_sec = skill.pop('security', None) |
| 89 | + if 'securityRequirements' not in skill and legacy_skill_sec is not None: |
| 90 | + skill['securityRequirements'] = _map_legacy_security( |
| 91 | + legacy_skill_sec |
| 92 | + ) |
| 93 | + |
| 94 | + security_schemes = agent_card_data.get('securitySchemes', {}) |
| 95 | + if security_schemes: |
| 96 | + type_mapping = { |
| 97 | + 'apiKey': 'apiKeySecurityScheme', |
| 98 | + 'http': 'httpAuthSecurityScheme', |
| 99 | + 'oauth2': 'oauth2SecurityScheme', |
| 100 | + 'openIdConnect': 'openIdConnectSecurityScheme', |
| 101 | + 'mutualTLS': 'mtlsSecurityScheme', |
| 102 | + } |
| 103 | + for scheme in security_schemes.values(): |
| 104 | + scheme_type = scheme.pop('type', None) |
| 105 | + if scheme_type in type_mapping: |
| 106 | + # Map legacy 'in' to modern 'location' |
| 107 | + if scheme_type == 'apiKey' and 'in' in scheme: |
| 108 | + scheme['location'] = scheme.pop('in') |
| 109 | + |
| 110 | + mapped_name = type_mapping[scheme_type] |
| 111 | + new_scheme_wrapper = {mapped_name: scheme.copy()} |
| 112 | + scheme.clear() |
| 113 | + scheme.update(new_scheme_wrapper) |
6 | 114 |
|
7 | 115 |
|
8 | 116 | def create_text_message_object( |
|
0 commit comments