11import inspect
22import json
33from collections .abc import Mapping as MappingABC
4- from typing import Any , Dict , Mapping
4+ from typing import Any , Callable , Dict , Mapping
55
66from hyperbrowser .exceptions import HyperbrowserError
77from hyperbrowser .models .agents .browser_use import StartBrowserUseTaskParams
@@ -72,6 +72,27 @@ def _format_tool_param_key_for_error(key: str) -> str:
7272 return f"{ normalized_key [:available_length ]} { _TRUNCATED_KEY_DISPLAY_SUFFIX } "
7373
7474
75+ def _copy_mapping_values_by_keys (
76+ source_mapping : MappingABC [object , Any ],
77+ keys : list [str ],
78+ * ,
79+ read_error_message_builder : Callable [[str ], str ],
80+ ) -> Dict [str , Any ]:
81+ normalized_values : Dict [str , Any ] = {}
82+ for key in keys :
83+ try :
84+ normalized_values [key ] = source_mapping [key ]
85+ except HyperbrowserError :
86+ raise
87+ except Exception as exc :
88+ key_display = _format_tool_param_key_for_error (key )
89+ raise HyperbrowserError (
90+ read_error_message_builder (key_display ),
91+ original_error = exc ,
92+ ) from exc
93+ return normalized_values
94+
95+
7596def _normalize_extract_schema_mapping (
7697 schema_value : MappingABC [object , Any ],
7798) -> Dict [str , Any ]:
@@ -84,21 +105,18 @@ def _normalize_extract_schema_mapping(
84105 "Failed to read extract tool `schema` object keys" ,
85106 original_error = exc ,
86107 ) from exc
87- normalized_schema : Dict [str , Any ] = {}
108+ normalized_schema_keys : list [str ] = []
88109 for key in schema_keys :
89110 if type (key ) is not str :
90111 raise HyperbrowserError ("Extract tool `schema` object keys must be strings" )
91- try :
92- normalized_schema [key ] = schema_value [key ]
93- except HyperbrowserError :
94- raise
95- except Exception as exc :
96- key_display = _format_tool_param_key_for_error (key )
97- raise HyperbrowserError (
98- f"Failed to read extract tool `schema` value for key '{ key_display } '" ,
99- original_error = exc ,
100- ) from exc
101- return normalized_schema
112+ normalized_schema_keys .append (key )
113+ return _copy_mapping_values_by_keys (
114+ schema_value ,
115+ normalized_schema_keys ,
116+ read_error_message_builder = lambda key_display : (
117+ f"Failed to read extract tool `schema` value for key '{ key_display } '"
118+ ),
119+ )
102120
103121
104122def _prepare_extract_tool_params (params : Mapping [str , Any ]) -> Dict [str , Any ]:
@@ -188,19 +206,14 @@ def _to_param_dict(params: Mapping[str, Any]) -> Dict[str, Any]:
188206 )
189207 continue
190208 raise HyperbrowserError ("tool params keys must be strings" )
191- normalized_params : Dict [str , Any ] = {}
192- for key in param_keys :
193- try :
194- normalized_params [key ] = params [key ]
195- except HyperbrowserError :
196- raise
197- except Exception as exc :
198- key_display = _format_tool_param_key_for_error (key )
199- raise HyperbrowserError (
200- f"Failed to read tool param '{ key_display } '" ,
201- original_error = exc ,
202- ) from exc
203- return normalized_params
209+ normalized_param_keys = [key for key in param_keys if type (key ) is str ]
210+ return _copy_mapping_values_by_keys (
211+ params ,
212+ normalized_param_keys ,
213+ read_error_message_builder = lambda key_display : (
214+ f"Failed to read tool param '{ key_display } '"
215+ ),
216+ )
204217
205218
206219def _serialize_extract_tool_data (data : Any ) -> str :
0 commit comments