|
31 | 31 |
|
32 | 32 | STANDARD_CATALOG_ID = "https://github.com/google/A2UI/blob/main/specification/0.8/json/standard_catalog_definition.json" |
33 | 33 |
|
| 34 | + |
34 | 35 | def create_a2ui_part(a2ui_data: dict[str, Any]) -> Part: |
35 | | - """Creates an A2A Part containing A2UI data. |
36 | | -
|
37 | | - Args: |
38 | | - a2ui_data: The A2UI data dictionary. |
39 | | -
|
40 | | - Returns: |
41 | | - An A2A Part with a DataPart containing the A2UI data. |
42 | | - """ |
43 | | - return Part( |
44 | | - root=DataPart( |
45 | | - data=a2ui_data, |
46 | | - metadata={ |
47 | | - MIME_TYPE_KEY: A2UI_MIME_TYPE, |
48 | | - }, |
49 | | - ) |
50 | | - ) |
| 36 | + """Creates an A2A Part containing A2UI data. |
| 37 | +
|
| 38 | + Args: |
| 39 | + a2ui_data: The A2UI data dictionary. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + An A2A Part with a DataPart containing the A2UI data. |
| 43 | + """ |
| 44 | + return Part( |
| 45 | + root=DataPart( |
| 46 | + data=a2ui_data, |
| 47 | + metadata={ |
| 48 | + MIME_TYPE_KEY: A2UI_MIME_TYPE, |
| 49 | + }, |
| 50 | + ) |
| 51 | + ) |
51 | 52 |
|
52 | 53 |
|
53 | 54 | def is_a2ui_part(part: Part) -> bool: |
54 | | - """Checks if an A2A Part contains A2UI data. |
55 | | -
|
56 | | - Args: |
57 | | - part: The A2A Part to check. |
58 | | -
|
59 | | - Returns: |
60 | | - True if the part contains A2UI data, False otherwise. |
61 | | - """ |
62 | | - return ( |
63 | | - isinstance(part.root, DataPart) |
64 | | - and part.root.metadata |
65 | | - and part.root.metadata.get(MIME_TYPE_KEY) == A2UI_MIME_TYPE |
66 | | - ) |
| 55 | + """Checks if an A2A Part contains A2UI data. |
| 56 | +
|
| 57 | + Args: |
| 58 | + part: The A2A Part to check. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + True if the part contains A2UI data, False otherwise. |
| 62 | + """ |
| 63 | + return ( |
| 64 | + isinstance(part.root, DataPart) |
| 65 | + and part.root.metadata |
| 66 | + and part.root.metadata.get(MIME_TYPE_KEY) == A2UI_MIME_TYPE |
| 67 | + ) |
67 | 68 |
|
68 | 69 |
|
69 | 70 | def get_a2ui_datapart(part: Part) -> Optional[DataPart]: |
70 | | - """Extracts the DataPart containing A2UI data from an A2A Part, if present. |
| 71 | + """Extracts the DataPart containing A2UI data from an A2A Part, if present. |
71 | 72 |
|
72 | | - Args: |
73 | | - part: The A2A Part to extract A2UI data from. |
| 73 | + Args: |
| 74 | + part: The A2A Part to extract A2UI data from. |
74 | 75 |
|
75 | | - Returns: |
76 | | - The DataPart containing A2UI data if present, None otherwise. |
77 | | - """ |
78 | | - if is_a2ui_part(part): |
79 | | - return part.root |
80 | | - return None |
| 76 | + Returns: |
| 77 | + The DataPart containing A2UI data if present, None otherwise. |
| 78 | + """ |
| 79 | + if is_a2ui_part(part): |
| 80 | + return part.root |
| 81 | + return None |
81 | 82 |
|
82 | 83 |
|
83 | 84 | AGENT_EXTENSION_SUPPORTED_CATALOG_IDS_KEY = "supportedCatalogIds" |
84 | 85 | AGENT_EXTENSION_ACCEPTS_INLINE_CATALOGS_KEY = "acceptsInlineCatalogs" |
85 | 86 |
|
| 87 | + |
86 | 88 | def get_a2ui_agent_extension( |
87 | 89 | accepts_inline_catalogs: bool = False, |
88 | 90 | supported_catalog_ids: List[str] = [], |
89 | 91 | ) -> AgentExtension: |
90 | | - """Creates the A2UI AgentExtension configuration. |
91 | | -
|
92 | | - Args: |
93 | | - accepts_inline_catalogs: Whether the agent accepts inline custom catalogs. |
94 | | - supported_catalog_ids: All pre-defined catalogs the agent is known to support. |
95 | | -
|
96 | | - Returns: |
97 | | - The configured A2UI AgentExtension. |
98 | | - """ |
99 | | - params = {} |
100 | | - if accepts_inline_catalogs: |
101 | | - params[AGENT_EXTENSION_ACCEPTS_INLINE_CATALOGS_KEY] = True # Only set if not default of False |
102 | | - |
103 | | - if supported_catalog_ids: |
104 | | - params[AGENT_EXTENSION_SUPPORTED_CATALOG_IDS_KEY] = supported_catalog_ids |
105 | | - |
106 | | - return AgentExtension( |
107 | | - uri=A2UI_EXTENSION_URI, |
108 | | - description="Provides agent driven UI using the A2UI JSON format.", |
109 | | - params=params if params else None, |
| 92 | + """Creates the A2UI AgentExtension configuration. |
| 93 | +
|
| 94 | + Args: |
| 95 | + accepts_inline_catalogs: Whether the agent accepts inline custom catalogs. |
| 96 | + supported_catalog_ids: All pre-defined catalogs the agent is known to support. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + The configured A2UI AgentExtension. |
| 100 | + """ |
| 101 | + params = {} |
| 102 | + if accepts_inline_catalogs: |
| 103 | + params[AGENT_EXTENSION_ACCEPTS_INLINE_CATALOGS_KEY] = ( |
| 104 | + True # Only set if not default of False |
110 | 105 | ) |
111 | 106 |
|
| 107 | + if supported_catalog_ids: |
| 108 | + params[AGENT_EXTENSION_SUPPORTED_CATALOG_IDS_KEY] = supported_catalog_ids |
| 109 | + |
| 110 | + return AgentExtension( |
| 111 | + uri=A2UI_EXTENSION_URI, |
| 112 | + description="Provides agent driven UI using the A2UI JSON format.", |
| 113 | + params=params if params else None, |
| 114 | + ) |
| 115 | + |
112 | 116 |
|
113 | 117 | def try_activate_a2ui_extension(context: RequestContext) -> bool: |
114 | | - """Activates the A2UI extension if requested. |
115 | | -
|
116 | | - Args: |
117 | | - context: The request context to check. |
118 | | -
|
119 | | - Returns: |
120 | | - True if activated, False otherwise. |
121 | | - """ |
122 | | - if A2UI_EXTENSION_URI in context.requested_extensions: |
123 | | - context.add_activated_extension(A2UI_EXTENSION_URI) |
124 | | - return True |
125 | | - return False |
| 118 | + """Activates the A2UI extension if requested. |
| 119 | +
|
| 120 | + Args: |
| 121 | + context: The request context to check. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + True if activated, False otherwise. |
| 125 | + """ |
| 126 | + if A2UI_EXTENSION_URI in context.requested_extensions: |
| 127 | + context.add_activated_extension(A2UI_EXTENSION_URI) |
| 128 | + return True |
| 129 | + return False |
0 commit comments