mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 05:50:44 +08:00
补齐本地 public 工具的 P1/P2/P3 能力。
新增目录列举、文件/目录操作、内容搜索、cwd/env 管理与进程管理工具,并在本地适配层统一实现与测试覆盖,保持风险门控策略一致。 Made-with: Cursor
This commit is contained in:
parent
d8e2f4cacc
commit
75aec2abe4
14 changed files with 684 additions and 4 deletions
30
runtime/tools/public/cd_tool.py
Normal file
30
runtime/tools/public/cd_tool.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
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 cd_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
cwd = str(args.get("cwd") or "").strip()
|
||||
return get_local_adapter().cd(cwd=cwd)
|
||||
|
||||
return ToolSpec(
|
||||
name="cd",
|
||||
description="Change current working directory for local tools (best-effort, per-process).",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {"cwd": {"type": "string", "description": "Directory to change into."}},
|
||||
"required": ["cwd"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "write"}),
|
||||
risk_level="high",
|
||||
timeout_s=5.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["cd_tool"]
|
||||
34
runtime/tools/public/delete_file_tool.py
Normal file
34
runtime/tools/public/delete_file_tool.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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 delete_file_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
path = str(args.get("path") or "").strip()
|
||||
missing_ok = bool(args.get("missing_ok", True))
|
||||
return get_local_adapter().delete_path(path=path, missing_ok=missing_ok)
|
||||
|
||||
return ToolSpec(
|
||||
name="delete_file",
|
||||
description="Delete a file or directory inside the workspace.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string", "description": "Path to delete."},
|
||||
"missing_ok": {"type": "boolean", "default": True, "description": "If true, missing path is ok."},
|
||||
},
|
||||
"required": ["path"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "workspace", "write"}),
|
||||
risk_level="high",
|
||||
timeout_s=20.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["delete_file_tool"]
|
||||
26
runtime/tools/public/get_cwd_tool.py
Normal file
26
runtime/tools/public/get_cwd_tool.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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 get_cwd_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
del args
|
||||
return get_local_adapter().get_cwd()
|
||||
|
||||
return ToolSpec(
|
||||
name="get_cwd",
|
||||
description="Get current working directory for local tools.",
|
||||
parameters={"type": "object", "properties": {}, "additionalProperties": False},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "read"}),
|
||||
risk_level="low",
|
||||
read_only=True,
|
||||
timeout_s=2.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["get_cwd_tool"]
|
||||
35
runtime/tools/public/get_env_tool.py
Normal file
35
runtime/tools/public/get_env_tool.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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 get_env_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
key = str(args.get("key") or "").strip()
|
||||
default = args.get("default")
|
||||
return get_local_adapter().get_env(key=key, default=str(default) if default is not None else None)
|
||||
|
||||
return ToolSpec(
|
||||
name="get_env",
|
||||
description="Get an environment variable value.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"key": {"type": "string", "description": "Environment variable key."},
|
||||
"default": {"type": "string", "description": "Default value if missing."},
|
||||
},
|
||||
"required": ["key"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "read"}),
|
||||
risk_level="low",
|
||||
read_only=True,
|
||||
timeout_s=2.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["get_env_tool"]
|
||||
34
runtime/tools/public/kill_process_tool.py
Normal file
34
runtime/tools/public/kill_process_tool.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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 kill_process_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
pid = int(args.get("pid") or 0)
|
||||
force = bool(args.get("force", True))
|
||||
return get_local_adapter().kill_process(pid=pid, force=force)
|
||||
|
||||
return ToolSpec(
|
||||
name="kill_process",
|
||||
description="Kill a process by PID (best-effort).",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"pid": {"type": "integer", "description": "Process id."},
|
||||
"force": {"type": "boolean", "default": True, "description": "Force kill if true."},
|
||||
},
|
||||
"required": ["pid"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "write"}),
|
||||
risk_level="high",
|
||||
timeout_s=10.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["kill_process_tool"]
|
||||
35
runtime/tools/public/list_directory_tool.py
Normal file
35
runtime/tools/public/list_directory_tool.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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 list_directory_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
path = str(args.get("path") or ".").strip() or "."
|
||||
max_entries = int(args.get("max_entries") or 500)
|
||||
return get_local_adapter().list_directory(path=path, max_entries=max_entries)
|
||||
|
||||
return ToolSpec(
|
||||
name="list_directory",
|
||||
description="List a directory with file metadata (name/size/mtime).",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string", "default": ".", "description": "Directory path."},
|
||||
"max_entries": {"type": "integer", "default": 500, "description": "Max entries to return."},
|
||||
},
|
||||
"required": [],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "workspace", "read"}),
|
||||
risk_level="low",
|
||||
read_only=True,
|
||||
timeout_s=10.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["list_directory_tool"]
|
||||
31
runtime/tools/public/list_processes_tool.py
Normal file
31
runtime/tools/public/list_processes_tool.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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 list_processes_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
max_results = int(args.get("max_results") or 200)
|
||||
return get_local_adapter().list_processes(max_results=max_results)
|
||||
|
||||
return ToolSpec(
|
||||
name="list_processes",
|
||||
description="List running processes (best-effort).",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {"max_results": {"type": "integer", "default": 200}},
|
||||
"required": [],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "read"}),
|
||||
risk_level="low",
|
||||
read_only=True,
|
||||
timeout_s=10.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["list_processes_tool"]
|
||||
36
runtime/tools/public/mkdir_tool.py
Normal file
36
runtime/tools/public/mkdir_tool.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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 mkdir_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
path = str(args.get("path") or "").strip()
|
||||
parents = bool(args.get("parents", True))
|
||||
exist_ok = bool(args.get("exist_ok", True))
|
||||
return get_local_adapter().mkdir(path=path, parents=parents, exist_ok=exist_ok)
|
||||
|
||||
return ToolSpec(
|
||||
name="mkdir",
|
||||
description="Create a directory inside the workspace.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string", "description": "Directory path to create."},
|
||||
"parents": {"type": "boolean", "default": True},
|
||||
"exist_ok": {"type": "boolean", "default": True},
|
||||
},
|
||||
"required": ["path"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "workspace", "write"}),
|
||||
risk_level="high",
|
||||
timeout_s=10.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["mkdir_tool"]
|
||||
36
runtime/tools/public/move_file_tool.py
Normal file
36
runtime/tools/public/move_file_tool.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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 move_file_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
src = str(args.get("src") or "").strip()
|
||||
dst = str(args.get("dst") or "").strip()
|
||||
overwrite = bool(args.get("overwrite", False))
|
||||
return get_local_adapter().move_path(src=src, dst=dst, overwrite=overwrite)
|
||||
|
||||
return ToolSpec(
|
||||
name="move_file",
|
||||
description="Move/rename a file or directory inside the workspace.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"src": {"type": "string", "description": "Source path."},
|
||||
"dst": {"type": "string", "description": "Destination path."},
|
||||
"overwrite": {"type": "boolean", "default": False, "description": "Overwrite destination if exists."},
|
||||
},
|
||||
"required": ["src", "dst"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "workspace", "write"}),
|
||||
risk_level="high",
|
||||
timeout_s=20.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["move_file_tool"]
|
||||
47
runtime/tools/public/search_files_tool.py
Normal file
47
runtime/tools/public/search_files_tool.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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 search_files_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
pattern = str(args.get("pattern") or "").strip()
|
||||
root = str(args.get("root") or ".").strip() or "."
|
||||
file_glob = str(args.get("file_glob") or "**/*").strip() or "**/*"
|
||||
regex = bool(args.get("regex", True))
|
||||
max_matches = int(args.get("max_matches") or 200)
|
||||
return get_local_adapter().search_files(
|
||||
root=root,
|
||||
pattern=pattern,
|
||||
file_glob=file_glob,
|
||||
regex=regex,
|
||||
max_matches=max_matches,
|
||||
)
|
||||
|
||||
return ToolSpec(
|
||||
name="search_files",
|
||||
description="Search file contents under a directory (regex or substring).",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"pattern": {"type": "string", "description": "Regex or substring pattern."},
|
||||
"root": {"type": "string", "default": ".", "description": "Root directory to search."},
|
||||
"file_glob": {"type": "string", "default": "**/*", "description": "File glob under root."},
|
||||
"regex": {"type": "boolean", "default": True, "description": "Treat pattern as regex if true."},
|
||||
"max_matches": {"type": "integer", "default": 200, "description": "Max matches to return."},
|
||||
},
|
||||
"required": ["pattern"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "workspace", "read"}),
|
||||
risk_level="low",
|
||||
read_only=True,
|
||||
timeout_s=20.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["search_files_tool"]
|
||||
34
runtime/tools/public/set_env_tool.py
Normal file
34
runtime/tools/public/set_env_tool.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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 set_env_tool() -> ToolSpec:
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
key = str(args.get("key") or "").strip()
|
||||
value = args.get("value")
|
||||
return get_local_adapter().set_env(key=key, value=str(value) if value is not None else None)
|
||||
|
||||
return ToolSpec(
|
||||
name="set_env",
|
||||
description="Set or delete an environment variable in the current process.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"key": {"type": "string", "description": "Environment variable key."},
|
||||
"value": {"type": "string", "description": "Value to set. If omitted, deletes the key."},
|
||||
},
|
||||
"required": ["key"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
handler=_handler,
|
||||
tags=frozenset({"public", "local", "write"}),
|
||||
risk_level="high",
|
||||
timeout_s=2.0,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["set_env_tool"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue