(
tester: LMM,
debugger: LMM,
chat: List[AgentMessage],
plan: str,
code: str,
tool_docs: str,
code_interpreter: CodeInterpreter,
media_list: List[Union[str, Path]],
verbose: bool,
)
| 193 | |
| 194 | |
| 195 | def test_code( |
| 196 | tester: LMM, |
| 197 | debugger: LMM, |
| 198 | chat: List[AgentMessage], |
| 199 | plan: str, |
| 200 | code: str, |
| 201 | tool_docs: str, |
| 202 | code_interpreter: CodeInterpreter, |
| 203 | media_list: List[Union[str, Path]], |
| 204 | verbose: bool, |
| 205 | ) -> CodeContext: |
| 206 | try: |
| 207 | code = strip_function_calls(code) |
| 208 | except Exception: |
| 209 | # the code may be malformatted, this will fail in the exec call and the agent |
| 210 | # will attempt to debug it |
| 211 | pass |
| 212 | test = write_test( |
| 213 | tester=tester, |
| 214 | chat=chat, |
| 215 | tool_util_docs=get_utilties_docstring(), |
| 216 | code=code, |
| 217 | media_list=media_list, |
| 218 | ) |
| 219 | if verbose: |
| 220 | print_code("Code:", code) |
| 221 | print_code("Test:", test) |
| 222 | result = code_interpreter.exec_isolation( |
| 223 | f"{DefaultImports.to_code_string()}\n{code}\n{test}" |
| 224 | ) |
| 225 | if verbose: |
| 226 | _CONSOLE.print( |
| 227 | f"[bold cyan]Code execution result:[/bold cyan] [yellow]{escape(result.text(include_logs=True))}[/yellow]" |
| 228 | ) |
| 229 | |
| 230 | count = 0 |
| 231 | debug_info = "" |
| 232 | while (not result.success or len(result.logs.stdout) == 0) and count < 3: |
| 233 | code, test, debug_info = debug_code( |
| 234 | debugger, |
| 235 | get_utilties_docstring() + "\n" + tool_docs, |
| 236 | plan, |
| 237 | code, |
| 238 | test, |
| 239 | result, |
| 240 | debug_info, |
| 241 | verbose, |
| 242 | ) |
| 243 | result = code_interpreter.exec_isolation( |
| 244 | f"{DefaultImports.to_code_string()}\n{code}\n{test}" |
| 245 | ) |
| 246 | count += 1 |
| 247 | if verbose: |
| 248 | print_code("Code and test after attempted fix:", code, test) |
| 249 | _CONSOLE.print( |
| 250 | f"[bold cyan]Code execution result after attempted fix:[/bold cyan] [yellow]{escape(result.text(include_logs=True))}[/yellow]" |
| 251 | ) |
| 252 |
no test coverage detected