|
| 1 | +"""Proto → JSON Schema utilities for A2A server routes.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from google.protobuf.descriptor import Descriptor, FieldDescriptor |
| 6 | +from google.protobuf.message import Message |
| 7 | + |
| 8 | +from a2a.types.a2a_pb2 import SendMessageRequest, TaskPushNotificationConfig |
| 9 | + |
| 10 | + |
| 11 | +REST_BODY_TYPES: dict[tuple[str, str], type[Message]] = { |
| 12 | + ('/message:send', 'POST'): SendMessageRequest, |
| 13 | + ('/message:stream', 'POST'): SendMessageRequest, |
| 14 | + ('/tasks/{id}/pushNotificationConfigs', 'POST'): TaskPushNotificationConfig, |
| 15 | +} |
| 16 | + |
| 17 | +# 64-bit integer types serialize as strings in protojson. |
| 18 | +_PROTO_SCALAR_SCHEMAS: dict[int, dict[str, Any]] = { |
| 19 | + FieldDescriptor.TYPE_DOUBLE: {'type': 'number'}, |
| 20 | + FieldDescriptor.TYPE_FLOAT: {'type': 'number'}, |
| 21 | + FieldDescriptor.TYPE_INT64: {'type': 'string', 'format': 'int64'}, |
| 22 | + FieldDescriptor.TYPE_UINT64: {'type': 'string', 'format': 'uint64'}, |
| 23 | + FieldDescriptor.TYPE_INT32: {'type': 'integer', 'format': 'int32'}, |
| 24 | + FieldDescriptor.TYPE_FIXED64: {'type': 'string', 'format': 'fixed64'}, |
| 25 | + FieldDescriptor.TYPE_FIXED32: {'type': 'integer', 'format': 'fixed32'}, |
| 26 | + FieldDescriptor.TYPE_BOOL: {'type': 'boolean'}, |
| 27 | + FieldDescriptor.TYPE_STRING: {'type': 'string'}, |
| 28 | + FieldDescriptor.TYPE_BYTES: {'type': 'string', 'format': 'byte'}, |
| 29 | + FieldDescriptor.TYPE_UINT32: {'type': 'integer', 'format': 'uint32'}, |
| 30 | + FieldDescriptor.TYPE_SFIXED32: {'type': 'integer'}, |
| 31 | + FieldDescriptor.TYPE_SFIXED64: {'type': 'string'}, |
| 32 | + FieldDescriptor.TYPE_SINT32: {'type': 'integer'}, |
| 33 | + FieldDescriptor.TYPE_SINT64: {'type': 'string'}, |
| 34 | +} |
| 35 | + |
| 36 | +_WELL_KNOWN_SCHEMAS: dict[str, dict[str, Any]] = { |
| 37 | + 'google.protobuf.Timestamp': {'type': 'string', 'format': 'date-time'}, |
| 38 | + 'google.protobuf.Duration': {'type': 'string'}, |
| 39 | + 'google.protobuf.Struct': {'type': 'object'}, |
| 40 | + 'google.protobuf.Value': {}, |
| 41 | + 'google.protobuf.ListValue': {'type': 'array', 'items': {}}, |
| 42 | + 'google.protobuf.Empty': {'type': 'object'}, |
| 43 | + 'google.protobuf.Any': {'type': 'object'}, |
| 44 | + 'google.protobuf.FieldMask': {'type': 'string'}, |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +def field_schema( |
| 49 | + field: FieldDescriptor, components: dict[str, Any] |
| 50 | +) -> dict[str, Any]: |
| 51 | + if field.message_type and field.message_type.GetOptions().map_entry: |
| 52 | + value_field = field.message_type.fields_by_name['value'] |
| 53 | + return { |
| 54 | + 'type': 'object', |
| 55 | + 'additionalProperties': field_schema(value_field, components), |
| 56 | + } |
| 57 | + |
| 58 | + if field.type == FieldDescriptor.TYPE_MESSAGE: |
| 59 | + item = message_schema(field.message_type, components) |
| 60 | + elif field.type == FieldDescriptor.TYPE_ENUM: |
| 61 | + item = { |
| 62 | + 'type': 'string', |
| 63 | + 'enum': [v.name for v in field.enum_type.values], |
| 64 | + } |
| 65 | + else: |
| 66 | + item = dict(_PROTO_SCALAR_SCHEMAS.get(field.type, {'type': 'string'})) |
| 67 | + |
| 68 | + if field.is_repeated: |
| 69 | + return {'type': 'array', 'items': item} |
| 70 | + return item |
| 71 | + |
| 72 | + |
| 73 | +def message_schema( |
| 74 | + descriptor: Descriptor | Any, components: dict[str, Any] |
| 75 | +) -> dict[str, Any]: |
| 76 | + """Returns a $ref to descriptor's schema, registering it in components if needed.""" |
| 77 | + if descriptor.full_name in _WELL_KNOWN_SCHEMAS: |
| 78 | + return dict(_WELL_KNOWN_SCHEMAS[descriptor.full_name]) |
| 79 | + |
| 80 | + name = descriptor.name |
| 81 | + ref = {'$ref': f'#/components/schemas/{name}'} |
| 82 | + if name in components: |
| 83 | + return ref |
| 84 | + |
| 85 | + # Reserve the slot before recursing so cyclic types terminate. |
| 86 | + components[name] = {} |
| 87 | + |
| 88 | + real_oneofs = [o for o in descriptor.oneofs if len(o.fields) > 1] |
| 89 | + oneof_field_names = {f.name for o in real_oneofs for f in o.fields} |
| 90 | + base_properties = { |
| 91 | + f.name: field_schema(f, components) |
| 92 | + for f in descriptor.fields |
| 93 | + if f.name not in oneof_field_names |
| 94 | + } |
| 95 | + |
| 96 | + if not real_oneofs: |
| 97 | + components[name] = {'type': 'object', 'properties': base_properties} |
| 98 | + return ref |
| 99 | + |
| 100 | + oneof_constraints = [ |
| 101 | + { |
| 102 | + 'oneOf': [ |
| 103 | + { |
| 104 | + 'type': 'object', |
| 105 | + 'properties': {f.name: field_schema(f, components)}, |
| 106 | + 'required': [f.name], |
| 107 | + } |
| 108 | + for f in oneof.fields |
| 109 | + ] |
| 110 | + } |
| 111 | + for oneof in real_oneofs |
| 112 | + ] |
| 113 | + parts: list[dict[str, Any]] = [] |
| 114 | + if base_properties: |
| 115 | + parts.append({'type': 'object', 'properties': base_properties}) |
| 116 | + parts.extend(oneof_constraints) |
| 117 | + components[name] = parts[0] if len(parts) == 1 else {'allOf': parts} |
| 118 | + return ref |
0 commit comments