|
11 | 11 | from docker.models.containers import Container
|
12 | 12 | from docker.errors import ImageNotFound
|
13 | 13 |
|
14 |
| -from .local_commandline_code_executor import CommandLineCodeResult |
| 14 | +from .utils import _get_file_name_from_content |
| 15 | +from .base import CommandLineCodeResult |
15 | 16 |
|
16 | 17 | from ..code_utils import TIMEOUT_MSG, _cmd
|
17 | 18 | from .base import CodeBlock, CodeExecutor, CodeExtractor
|
@@ -168,25 +169,15 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandLineCodeRe
|
168 | 169 | lang = code_block.language
|
169 | 170 | code = code_block.code
|
170 | 171 |
|
171 |
| - code_hash = md5(code.encode()).hexdigest() |
172 |
| - |
173 |
| - # Check if there is a filename comment |
174 |
| - # Get first line |
175 |
| - first_line = code.split("\n")[0] |
176 |
| - if first_line.startswith("# filename:"): |
177 |
| - filename = first_line.split(":")[1].strip() |
178 |
| - |
179 |
| - # Handle relative paths in the filename |
180 |
| - path = Path(filename) |
181 |
| - if not path.is_absolute(): |
182 |
| - path = Path("/workspace") / path |
183 |
| - path = path.resolve() |
184 |
| - try: |
185 |
| - path.relative_to(Path("/workspace")) |
186 |
| - except ValueError: |
187 |
| - return CommandLineCodeResult(exit_code=1, output="Filename is not in the workspace") |
188 |
| - else: |
189 |
| - # create a file with a automatically generated name |
| 172 | + try: |
| 173 | + # Check if there is a filename comment |
| 174 | + filename = _get_file_name_from_content(code, Path("/workspace")) |
| 175 | + except ValueError: |
| 176 | + return CommandLineCodeResult(exit_code=1, output="Filename is not in the workspace") |
| 177 | + |
| 178 | + if filename is None: |
| 179 | + # create a file with an automatically generated name |
| 180 | + code_hash = md5(code.encode()).hexdigest() |
190 | 181 | filename = f"tmp_code_{code_hash}.{'py' if lang.startswith('python') else lang}"
|
191 | 182 |
|
192 | 183 | code_path = self._work_dir / filename
|
|
0 commit comments