|
| 1 | +import logging |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import time |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from metacoder.coders.base_coder import ( |
| 8 | + BaseCoder, |
| 9 | + CoderOutput, |
| 10 | + change_directory, |
| 11 | +) |
| 12 | +from metacoder.configuration import ConfigFileRole |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class OpencodeCoder(BaseCoder): |
| 18 | + """ |
| 19 | + OpenCode.ai AI assistant integration. |
| 20 | +
|
| 21 | + Note: Requires opencode CLI to be installed. |
| 22 | + """ |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def is_available(cls) -> bool: |
| 26 | + """Check if opencode command is available.""" |
| 27 | + return shutil.which("opencode") is not None |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def supports_mcp(cls) -> bool: |
| 31 | + return False |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def default_config_paths(cls) -> dict[Path, ConfigFileRole]: |
| 35 | + return { |
| 36 | + Path("AGENTS.md"): ConfigFileRole.PRIMARY_INSTRUCTION, |
| 37 | + } |
| 38 | + |
| 39 | + def run(self, input_text: str) -> CoderOutput: |
| 40 | + """ |
| 41 | + Run opencode with the given input text. |
| 42 | + """ |
| 43 | + self.prepare_workdir() |
| 44 | + with change_directory(self.workdir): |
| 45 | + command = ["opencode", "run", self.expand_prompt(input_text)] |
| 46 | + if self.params and self.params.get("model"): |
| 47 | + command.extend(["--model", self.params["model"]]) |
| 48 | + |
| 49 | + logger.info(f"Running command: {' '.join(command)}") |
| 50 | + start_time = time.time() |
| 51 | + |
| 52 | + try: |
| 53 | + result = self.run_process(command) |
| 54 | + except subprocess.CalledProcessError as e: |
| 55 | + return CoderOutput( |
| 56 | + stdout=e.stdout or "", |
| 57 | + stderr=str(e), |
| 58 | + result_text=f"Error: {str(e)}", |
| 59 | + success=False, |
| 60 | + ) |
| 61 | + |
| 62 | + end_time = time.time() |
| 63 | + logger.info(f"Command took {end_time - start_time:.2f} seconds") |
| 64 | + |
| 65 | + return CoderOutput( |
| 66 | + stdout=result.stdout, |
| 67 | + stderr=result.stderr, |
| 68 | + result_text=result.stdout, |
| 69 | + success=True, |
| 70 | + ) |
| 71 | + |
| 72 | + def default_config_objects(self): |
| 73 | + return [] |
0 commit comments