(
*args,
func=func,
inner_func=inner_func,
threshold=threshold,
**kwargs,
)
| 129 | # Create a wrapper that explicitly binds `func` |
| 130 | @wraps(inner_func) |
| 131 | def wrapper( |
| 132 | *args, |
| 133 | func=func, |
| 134 | inner_func=inner_func, |
| 135 | threshold=threshold, |
| 136 | **kwargs, |
| 137 | ): |
| 138 | function_name = func.get_function_name() |
| 139 | if function_name in self.ignore_toolkit.ignored_risks: |
| 140 | reason = self.ignore_toolkit.ignored_risks.pop( |
| 141 | function_name |
| 142 | ) |
| 143 | logger.info( |
| 144 | f"Ignored risk for function {function_name}: {reason}" |
| 145 | ) |
| 146 | return inner_func(*args, **kwargs) |
| 147 | self.agent.init_messages() |
| 148 | resp = self.agent.step( |
| 149 | f""" |
| 150 | Function is: {function_name} |
| 151 | Function description: {func.get_function_description()} |
| 152 | Args: {args} |
| 153 | Kwargs: {kwargs} |
| 154 | """ |
| 155 | ) |
| 156 | tool_call = resp.info.get("external_tool_request", None) |
| 157 | if not tool_call: |
| 158 | logger.error("No tool call found in response.") |
| 159 | return { |
| 160 | "error": "Risk assessment failed. Disabling function." |
| 161 | } |
| 162 | data = tool_call.function.arguments |
| 163 | data = json.loads(data) |
| 164 | if threshold < data["score"]: |
| 165 | message = ( |
| 166 | f"Risk assessment not passed for {function_name}." |
| 167 | f"Score: {data['score']} > Threshold: {threshold}" |
| 168 | f"\nReason: {data['reason']}" |
| 169 | ) |
| 170 | logger.warning(message) |
| 171 | return {"error": message} |
| 172 | |
| 173 | logger.info( |
| 174 | ( |
| 175 | f"Function {function_name} passed risk assessment." |
| 176 | f"Score: {data['score']}, Reason: {data['reason']}" |
| 177 | ) |
| 178 | ) |
| 179 | if self.verbose: |
| 180 | print( |
| 181 | ( |
| 182 | f"Function {function_name} passed risk assessment." |
| 183 | f"Score: {data['score']}, Reason: {data['reason']}" |
| 184 | ) |
| 185 | ) |
| 186 | return inner_func(*args, **kwargs) |
| 187 | |
| 188 | func.func = wrapper |
nothing calls this directly
no test coverage detected