Skip to content

Commit feaccd7

Browse files
author
no-teasy
committed
refactor(computer): 提取公共配置目录获取逻辑到工具函数#7281 (review)
将重复的配置目录获取逻辑提取到 permissions.py 中的 get_configured_cwd 函数 移除 LocalPythonComponent 中不再需要的 default_cwd 字段
1 parent c84bbc2 commit feaccd7

4 files changed

Lines changed: 14 additions & 14 deletions

File tree

astrbot/core/computer/booters/local.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ def _run() -> dict[str, Any]:
185185

186186
@dataclass
187187
class LocalPythonComponent(PythonComponent):
188-
default_cwd: str | None = None
189-
190188
async def exec(
191189
self,
192190
code: str,
@@ -197,8 +195,7 @@ async def exec(
197195
) -> dict[str, Any]:
198196
def _run() -> dict[str, Any]:
199197
try:
200-
effective_cwd = cwd if cwd else self.default_cwd
201-
working_dir, _ = _resolve_working_dir(effective_cwd, get_astrbot_root)
198+
working_dir, _ = _resolve_working_dir(cwd, get_astrbot_root)
202199
result = subprocess.run(
203200
[os.environ.get("PYTHON", sys.executable), "-c", code],
204201
timeout=timeout,

astrbot/core/computer/tools/permissions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
from astrbot.core.astr_agent_context import AstrAgentContext
33

44

5+
def get_configured_cwd(
6+
context: ContextWrapper[AstrAgentContext], config_key: str
7+
) -> str | None:
8+
cfg = context.context.context.get_config(
9+
umo=context.context.event.unified_msg_origin
10+
)
11+
return cfg.get("provider_settings", {}).get(config_key, None)
12+
13+
514
def check_admin_permission(
615
context: ContextWrapper[AstrAgentContext], operation_name: str
716
) -> str | None:

astrbot/core/computer/tools/python.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from astrbot.core.agent.tool import ToolExecResult
99
from astrbot.core.astr_agent_context import AstrAgentContext, AstrMessageEvent
1010
from astrbot.core.computer.computer_client import get_booter, get_local_booter
11-
from astrbot.core.computer.tools.permissions import check_admin_permission
11+
from astrbot.core.computer.tools.permissions import check_admin_permission, get_configured_cwd
1212
from astrbot.core.message.message_event_result import MessageChain
1313

1414
_OS_NAME = platform.system()
@@ -62,10 +62,7 @@ async def handle_result(result: dict, event: AstrMessageEvent) -> ToolExecResult
6262

6363

6464
def _get_configured_python_cwd(context: ContextWrapper[AstrAgentContext]) -> str | None:
65-
cfg = context.context.context.get_config(
66-
umo=context.context.event.unified_msg_origin
67-
)
68-
return cfg.get("provider_settings", {}).get("computer_use_local_python_cwd", None)
65+
return get_configured_cwd(context, "computer_use_local_python_cwd")
6966

7067

7168
@dataclass

astrbot/core/computer/tools/shell.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from astrbot.core.astr_agent_context import AstrAgentContext
88

99
from ..computer_client import get_booter, get_local_booter
10-
from .permissions import check_admin_permission
10+
from .permissions import check_admin_permission, get_configured_cwd
1111

1212

1313
@dataclass
@@ -41,10 +41,7 @@ class ExecuteShellTool(FunctionTool):
4141
is_local: bool = False
4242

4343
def _get_configured_cwd(self, context: ContextWrapper[AstrAgentContext]) -> str | None:
44-
cfg = context.context.context.get_config(
45-
umo=context.context.event.unified_msg_origin
46-
)
47-
return cfg.get("provider_settings", {}).get("computer_use_local_shell_cwd", None)
44+
return get_configured_cwd(context, "computer_use_local_shell_cwd")
4845

4946
async def call(
5047
self,

0 commit comments

Comments
 (0)