@@ -102,7 +102,7 @@ def with_pruned_components(self, allowed_components: List[str]) -> "A2uiCatalog"
102102
103103 # Allow all components if no allowed components are specified
104104 if not allowed_components :
105- return self
105+ return self . _with_pruned_common_types ()
106106
107107 if CATALOG_COMPONENTS_KEY in schema_copy and isinstance (
108108 schema_copy [CATALOG_COMPONENTS_KEY ], dict
@@ -132,7 +132,54 @@ def with_pruned_components(self, allowed_components: List[str]) -> "A2uiCatalog"
132132
133133 any_comp ["oneOf" ] = filtered_one_of
134134
135- return replace (self , catalog_schema = schema_copy )
135+ pruned_catalog = replace (self , catalog_schema = schema_copy )
136+ return pruned_catalog ._with_pruned_common_types ()
137+
138+ def _with_pruned_common_types (self ) -> "A2uiCatalog" :
139+ """Returns a new catalog with unused common types pruned from the schema."""
140+ if not self .common_types_schema or "$defs" not in self .common_types_schema :
141+ return self
142+
143+ def _collect_refs (obj : Any ) -> set [str ]:
144+ refs = set ()
145+ if isinstance (obj , dict ):
146+ for k , v in obj .items ():
147+ if k == "$ref" and isinstance (v , str ):
148+ refs .add (v )
149+ else :
150+ refs .update (_collect_refs (v ))
151+ elif isinstance (obj , list ):
152+ for item in obj :
153+ refs .update (_collect_refs (item ))
154+ return refs
155+
156+ visited_defs = set ()
157+ queue = []
158+
159+ # Initialize queue with refs from catalog_schema and s2c_schema
160+ queue .extend (_collect_refs (self .catalog_schema ))
161+ queue .extend (_collect_refs (self .s2c_schema ))
162+
163+ while queue :
164+ ref = queue .pop (0 )
165+ if ref .startswith ("#/$defs/" ):
166+ # Note: This assumes a flat `$defs` namespace and no escaped
167+ # slashes (~1) or tildes (~0) in the definition names as per RFC 6901.
168+ def_name = ref .split ("/" )[- 1 ]
169+ if (
170+ def_name in self .common_types_schema ["$defs" ]
171+ and def_name not in visited_defs
172+ ):
173+ visited_defs .add (def_name )
174+ queue .extend (_collect_refs (self .common_types_schema ["$defs" ][def_name ]))
175+
176+ new_common_types_schema = copy .deepcopy (self .common_types_schema )
177+ all_defs = new_common_types_schema ["$defs" ]
178+ new_common_types_schema ["$defs" ] = {
179+ k : v for k , v in all_defs .items () if k in visited_defs
180+ }
181+
182+ return replace (self , common_types_schema = new_common_types_schema )
136183
137184 def render_as_llm_instructions (self ) -> str :
138185 """Renders the catalog and schema as LLM instructions."""
0 commit comments