新增本地 public 工具与适配器

Made-with: Cursor
This commit is contained in:
oliver 2026-04-30 23:18:58 +08:00
parent 4d9232f3b3
commit d8e2f4cacc
11 changed files with 582 additions and 2 deletions

View file

@ -0,0 +1,69 @@
from __future__ import annotations
from typing import Any
from oclaw.runtime.tools.base import ToolSpec
from oclaw.runtime.tools.local_sdk import get_local_adapter
def local_edit_file_tool() -> ToolSpec:
def _handler(args: dict[str, Any]) -> dict[str, Any]:
path = str(args.get("path") or "").strip()
if not path:
return {"ok": False, "error_code": "path_required", "error": "path_required"}
search = args.get("search")
replace = args.get("replace")
start_line = args.get("start_line")
end_line = args.get("end_line")
replacement = args.get("replacement")
has_search = search is not None
has_range = start_line is not None or end_line is not None or replacement is not None
if has_search and has_range:
return {
"ok": False,
"error_code": "invalid_edit_arguments",
"error": "choose search/replace or line-range replacement, not both",
}
if not has_search and not has_range:
return {
"ok": False,
"error_code": "invalid_edit_arguments",
"error": "missing edit arguments",
}
return get_local_adapter().edit_file(
path=path,
search=str(search) if has_search else None,
replace=str(replace) if replace is not None else None,
start_line=int(start_line) if start_line is not None else None,
end_line=int(end_line) if end_line is not None else None,
replacement=str(replacement) if replacement is not None else None,
)
return ToolSpec(
name="local_edit_file",
description="Edit partial file content via local backend.",
parameters={
"type": "object",
"properties": {
"path": {"type": "string", "description": "Target file path."},
"search": {"type": "string", "description": "Search text for single replace mode."},
"replace": {"type": "string", "description": "Replacement text for search mode."},
"start_line": {"type": "integer", "description": "Start line for line-range replace mode."},
"end_line": {"type": "integer", "description": "End line for line-range replace mode."},
"replacement": {"type": "string", "description": "Replacement content for line-range mode."},
},
"required": ["path"],
"additionalProperties": False,
},
handler=_handler,
tags=frozenset({"public", "local", "workspace", "write", "edit"}),
risk_level="high",
timeout_s=30.0,
read_only=False,
)
__all__ = ["local_edit_file_tool"]

View file

@ -0,0 +1,43 @@
from __future__ import annotations
from typing import Any
from oclaw.runtime.tools.base import ToolSpec
from oclaw.runtime.tools.local_sdk import get_local_adapter
def local_read_file_tool() -> ToolSpec:
def _handler(args: dict[str, Any]) -> dict[str, Any]:
path = str(args.get("path") or "").strip()
if not path:
return {"ok": False, "error_code": "path_required", "error": "path_required"}
start_line = args.get("start_line")
end_line = args.get("end_line")
return get_local_adapter().read_file(
path=path,
start_line=int(start_line) if start_line is not None else None,
end_line=int(end_line) if end_line is not None else None,
)
return ToolSpec(
name="local_read_file",
description="Read file content via local backend.",
parameters={
"type": "object",
"properties": {
"path": {"type": "string", "description": "Target file path."},
"start_line": {"type": "integer", "description": "Optional start line (1-indexed)."},
"end_line": {"type": "integer", "description": "Optional end line (1-indexed)."},
},
"required": ["path"],
"additionalProperties": False,
},
handler=_handler,
tags=frozenset({"public", "local", "workspace", "read"}),
risk_level="low",
timeout_s=20.0,
read_only=True,
)
__all__ = ["local_read_file_tool"]

View file

@ -0,0 +1,39 @@
from __future__ import annotations
from typing import Any
from oclaw.runtime.tools.base import ToolSpec
from oclaw.runtime.tools.local_sdk import get_local_adapter
def local_run_command_tool() -> ToolSpec:
def _handler(args: dict[str, Any]) -> dict[str, Any]:
command = str(args.get("command") or "").strip()
if not command:
return {"ok": False, "error_code": "command_required", "error": "command_required"}
cwd = str(args.get("cwd") or "").strip() or None
timeout = int(args.get("timeout") or 30)
return get_local_adapter().run_command(command=command, cwd=cwd, timeout=timeout)
return ToolSpec(
name="local_run_command",
description="Run a shell command via local backend.",
parameters={
"type": "object",
"properties": {
"command": {"type": "string", "description": "Shell command to execute."},
"cwd": {"type": "string", "description": "Optional working directory."},
"timeout": {"type": "integer", "description": "Timeout in seconds.", "default": 30},
},
"required": ["command"],
"additionalProperties": False,
},
handler=_handler,
tags=frozenset({"public", "local", "exec", "workspace"}),
risk_level="high",
timeout_s=90.0,
read_only=False,
)
__all__ = ["local_run_command_tool"]

View file

@ -0,0 +1,39 @@
from __future__ import annotations
from typing import Any
from oclaw.runtime.tools.base import ToolSpec
from oclaw.runtime.tools.local_sdk import get_local_adapter
def local_write_file_tool() -> ToolSpec:
def _handler(args: dict[str, Any]) -> dict[str, Any]:
path = str(args.get("path") or "").strip()
if not path:
return {"ok": False, "error_code": "path_required", "error": "path_required"}
content = str(args.get("content") or "")
mode = str(args.get("mode") or "overwrite").strip().lower()
return get_local_adapter().write_file(path=path, content=content, mode=mode)
return ToolSpec(
name="local_write_file",
description="Write or append file content via local backend.",
parameters={
"type": "object",
"properties": {
"path": {"type": "string", "description": "Target file path."},
"content": {"type": "string", "description": "Content to write."},
"mode": {"type": "string", "enum": ["overwrite", "append"], "default": "overwrite"},
},
"required": ["path", "content"],
"additionalProperties": False,
},
handler=_handler,
tags=frozenset({"public", "local", "workspace", "write"}),
risk_level="high",
timeout_s=30.0,
read_only=False,
)
__all__ = ["local_write_file_tool"]