forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathopencode_simple.py
More file actions
79 lines (66 loc) · 3.01 KB
/
Copy pathopencode_simple.py
File metadata and controls
79 lines (66 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2024 John Balis
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""OpenCode agent backend using the shared ACPBackend base class."""
import logging
import os
import shutil
from plugin.agent_backend.acp_backend import ACPBackend
log = logging.getLogger(__name__)
_OPENCODE_ACP_DEFAULT_ARGS = ["acp"]
class OpenCodeBackend(ACPBackend):
"""ACP-based OpenCode backend (`opencode acp`)."""
backend_id = "opencode"
def _load_config(self):
super()._load_config()
# Official CLI: `opencode acp` (subcommand appended when settings args are empty).
if self._binary_path and os.path.basename(self._binary_path).lower() == "opencode" and not self._extra_args:
self._extra_args = list(_OPENCODE_ACP_DEFAULT_ARGS)
def _find_binary(self):
"""Locate the `opencode` executable; `_load_config` adds the `acp` subcommand."""
binary_name = "opencode"
path = shutil.which(binary_name)
if path:
return path
home = os.path.expanduser("~")
for candidate in (os.path.join(home, ".local", "bin", binary_name), os.path.join(home, ".cargo", "bin", binary_name)):
if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
return candidate
return None
def get_binary_name(self) -> str:
"""Primary executable for PATH lookup (`opencode acp` is the supported install)."""
return "opencode"
def is_available(self, ctx):
"""Like ACPBackend but PATH fallback applies default `acp` args."""
self._load_config()
if self._binary_path and os.path.isfile(self._binary_path):
log.info("%s binary found: %s", self.get_display_name(), self._binary_path)
return True
path = shutil.which("opencode")
if path:
self._binary_path = path
if not self._extra_args:
self._extra_args = list(_OPENCODE_ACP_DEFAULT_ARGS)
log.info("%s found via PATH: %s", self.get_display_name(), path)
return True
log.info("%s binary not found", self.get_display_name())
return False
def get_display_name(self) -> str:
"""Return display name for UI."""
return "OpenCode (ACP)"
def get_agent_name(self) -> str:
"""Return ACP agent name."""
return "opencode"