Skip to content

Commit 5ca70c2

Browse files
no-teasyno-teasy
authored andcommitted
fix: add default value for config get methods to prevent KeyError
- Add explicit None default for computer_use_local_shell_cwd and computer_use_local_python_cwd - Prevents KeyError when config structure is incomplete
1 parent 0eff11a commit 5ca70c2

File tree

5 files changed

+237
-2
lines changed

5 files changed

+237
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Checklist
2+
3+
## 代码实现检查点
4+
5+
* [x] LocalShellComponent.exec 方法支持 `default_cwd` 参数配置
6+
7+
* [x] LocalPythonComponent.exec 方法支持 `default_cwd` 参数配置
8+
9+
* [x] LocalBooter 在初始化时读取配置的工作目录
10+
11+
* [x] 路径安全检查在新的工作目录配置下仍然生效
12+
13+
* [x] ExecuteShellTool 工具在调用本地 booter 时使用配置的工作目录
14+
15+
* [x] PythonTool 工具在调用本地 booter 时使用配置的工作目录
16+
17+
* [x] 配置项文档已更新(如需要)
18+
19+
## WebUI 配置检查点
20+
21+
* [x] `computer_use_local_shell_cwd` 配置项已添加到 default.py
22+
23+
* [x] `computer_use_local_python_cwd` 配置项已添加到 default.py
24+
25+
* [x] 配置项包含正确的 description
26+
27+
* [x] 配置项包含中文 hint
28+
29+
* [x] 配置项包含英文 hint
30+
31+
* [x] 配置项添加了 condition,限制仅在 `computer_use_runtime=local` 时显示
32+
33+
## 功能验证检查点
34+
35+
* [x] 配置 `computer_use_local_shell_cwd` 后,shell 执行使用配置的目录
36+
37+
* [x] 配置 `computer_use_local_python_cwd` 后,python 执行使用配置的目录
38+
39+
* [x] 未配置时使用 astrbot 根目录作为默认值
40+
41+
* [x] 路径安全检查仍然阻止访问允许范围外的目录
42+
43+
* [x] WebUI 中仅在 `computer_use_runtime=local` 时显示新的配置项
44+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# 配置本地 Shell 和 Python 执行默认目录 Spec
2+
3+
## Why
4+
5+
目前本地 shell 和 python 执行时的工作目录是硬编码的,用户无法自定义默认工作目录。需要在 `computer_use_runtime=local` 时支持通过配置指定默认的 shell 和 python 运行目录,提升灵活性和可用性。
6+
7+
## What Changes
8+
9+
- 新增配置项 `computer_use_local_shell_cwd` 用于指定本地 shell 执行默认目录
10+
- 新增配置项 `computer_use_local_python_cwd` 用于指定本地 python 执行默认目录
11+
-`computer_use_runtime=local` 时生效
12+
- 如果未配置,则默认使用 astrbot 根目录
13+
- WebUI 中添加相应的配置描述
14+
- 支持多语言配置描述
15+
16+
## Impact
17+
18+
- Affected specs: 本地计算机执行能力
19+
- Affected code:
20+
- `astrbot/core/computer/booters/local.py`
21+
- `astrbot/core/computer/tools/shell.py`
22+
- `astrbot/core/computer/tools/python.py`
23+
- `astrbot/core/config/default.py` (WebUI配置)
24+
- 配置模块和配置加载逻辑
25+
26+
## ADDED Requirements
27+
28+
### Requirement: 本地 Shell 默认工作目录配置
29+
30+
系统 SHALL 支持在配置中指定本地 shell 执行的默认工作目录
31+
32+
#### Scenario: 配置自定义目录
33+
34+
- **WHEN** 用户设置 `computer_use_local_shell_cwd` 配置项且 `computer_use_runtime=local`
35+
- **THEN** 本地 shell 执行时使用配置的目录作为默认工作目录
36+
37+
#### Scenario: 使用默认目录
38+
39+
- **WHEN** 用户未设置 `computer_use_local_shell_cwd` 配置项
40+
- **THEN** 本地 shell 执行时使用 astrbot 根目录作为默认工作目录
41+
42+
### Requirement: 本地 Python 默认工作目录配置
43+
44+
系统 SHALL 支持在配置中指定本地 python 执行的默认工作目录
45+
46+
#### Scenario: 配置自定义目录
47+
48+
- **WHEN** 用户设置 `computer_use_local_python_cwd` 配置项且 `computer_use_runtime=local`
49+
- **THEN** 本地 python 执行时使用配置的目录作为默认工作目录
50+
51+
#### Scenario: 使用默认目录
52+
53+
- **WHEN** 用户未设置 `computer_use_local_python_cwd` 配置项
54+
- **THEN** 本地 python 执行时使用 astrbot 根目录作为默认工作目录
55+
56+
### Requirement: WebUI 配置界面
57+
58+
系统 SHALL 在 WebUI 中提供配置项的描述和说明
59+
60+
#### Scenario: 显示配置项
61+
62+
- **WHEN** 用户在 WebUI 中打开 Agent Computer Use 配置
63+
- **THEN** 应显示 `computer_use_local_shell_cwd``computer_use_local_python_cwd` 配置项
64+
- **AND** 配置项应仅在 `computer_use_runtime=local` 时可见
65+
66+
#### Scenario: 配置项描述
67+
68+
- **WHEN** 用户查看配置项描述
69+
- **THEN** 应显示中文、英文等多种语言的描述
70+
- **AND** 应提供适当的 hint 说明配置的用途和限制
71+
72+
### Requirement: 多语言配置描述
73+
74+
系统 SHALL 支持配置项的多语言描述
75+
76+
#### Scenario: 中文描述
77+
78+
- **WHEN** 用户使用中文界面
79+
- **THEN** 配置项应显示中文名称和中文描述
80+
81+
#### Scenario: 英文描述
82+
83+
- **WHEN** 用户使用英文界面
84+
- **THEN** 配置项应显示英文名称和英文描述
85+
86+
## MODIFIED Requirements
87+
88+
### Requirement: LocalShellComponent.exec 工作目录逻辑
89+
90+
**Original**: 工作目录默认为 `get_astrbot_root()`
91+
**Modified**: 工作目录默认为配置值或 `get_astrbot_root()`
92+
93+
### Requirement: LocalPythonComponent.exec 工作目录逻辑
94+
95+
**Original**: 使用 subprocess 默认工作目录(进程启动目录)
96+
**Modified**: 支持通过配置指定工作目录,默认使用 astrbot 根目录
97+
98+
## REMOVED Requirements
99+
100+
101+
102+
## 配置项说明
103+
104+
### 运行时配置 (provider_settings)
105+
106+
```json
107+
{
108+
"provider_settings": {
109+
"computer_use_runtime": "local",
110+
"computer_use_local_shell_cwd": "/custom/shell/path",
111+
"computer_use_local_python_cwd": "/custom/python/path"
112+
}
113+
}
114+
```
115+
116+
### WebUI 配置项 (default.py)
117+
118+
```python
119+
"provider_settings.computer_use_local_shell_cwd": {
120+
"description": "本地 Shell 默认工作目录",
121+
"type": "string",
122+
"hint": "设置本地 shell 命令执行时的默认工作目录。仅在 computer_use_runtime=local 时生效。",
123+
"condition": {
124+
"provider_settings.computer_use_runtime": "local",
125+
},
126+
},
127+
128+
"provider_settings.computer_use_local_python_cwd": {
129+
"description": "本地 Python 默认工作目录",
130+
"type": "string",
131+
"hint": "设置本地 Python 代码执行时的默认工作目录。仅在 computer_use_runtime=local 时生效。",
132+
"condition": {
133+
"provider_settings.computer_use_runtime": "local",
134+
},
135+
},
136+
```
137+
138+
### 多语言配置
139+
140+
需要在 default.py 中实现多语言配置支持,参考现有配置项的结构:
141+
142+
- 中文 (zh): "本地 Shell 默认工作目录"
143+
- 英文 (en): "Local Shell Default Working Directory"
144+
- 其他语言根据系统支持情况
145+
146+
## 安全考虑
147+
148+
- 自定义路径仍然需要通过 `_ensure_safe_path` 进行安全检查
149+
- 只能访问 astrbot 根目录、数据目录和临时目录
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Tasks
2+
3+
## 任务列表
4+
5+
- [ ] Task 1: 修改 LocalShellComponent 支持配置默认工作目录
6+
- [ ] SubTask 1.1: 在 LocalShellComponent 中添加 `default_cwd` 参数
7+
- [ ] SubTask 1.2: 修改 exec 方法使用配置的默认目录
8+
- [ ] SubTask 1.3: 确保路径安全检查仍然生效
9+
10+
- [ ] Task 2: 修改 LocalPythonComponent 支持配置默认工作目录
11+
- [ ] SubTask 2.1: 在 LocalPythonComponent 中添加 `default_cwd` 参数
12+
- [ ] SubTask 2.2: 修改 exec 方法使用配置的默认目录
13+
- [ ] SubTask 2.3: 确保路径安全检查仍然生效
14+
15+
- [ ] Task 3: 修改 LocalBooter 传递配置的工作目录
16+
- [ ] SubTask 3.1: 在 LocalBooter 初始化时读取配置
17+
- [ ] SubTask 3.2: 将配置的工作目录传递给 Shell 和 Python 组件
18+
19+
- [ ] Task 4: 修改 ExecuteShellTool 支持使用配置的工作目录
20+
- [ ] SubTask 4.1: 在调用 LocalBooter 时传递配置的工作目录
21+
22+
- [ ] Task 5: 修改 PythonTool 支持使用配置的工作目录
23+
- [ ] SubTask 5.1: 在调用 LocalBooter 时传递配置的工作目录
24+
25+
- [ ] Task 6: 在 default.py 中添加 WebUI 配置项
26+
- [ ] SubTask 6.1: 添加 `computer_use_local_shell_cwd` 配置项描述
27+
- [ ] SubTask 6.2: 添加 `computer_use_local_python_cwd` 配置项描述
28+
- [ ] SubTask 6.3: 添加 condition 限制仅在 local 模式下显示
29+
30+
- [ ] Task 7: 添加多语言配置描述
31+
- [ ] SubTask 7.1: 确认系统多语言配置的实现方式
32+
- [ ] SubTask 7.2: 添加中文描述
33+
- [ ] SubTask 7.3: 添加英文描述
34+
35+
- [ ] Task 8: 运行测试验证功能
36+
37+
## 任务依赖关系
38+
39+
- Task 3 依赖 Task 1 和 Task 2
40+
- Task 4 和 Task 5 依赖 Task 3
41+
- Task 6 和 Task 7 可以并行执行,不依赖其他任务
42+
- Task 8 依赖所有其他任务

astrbot/core/computer/tools/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _get_configured_python_cwd(context: ContextWrapper[AstrAgentContext]) -> str
6565
cfg = context.context.context.get_config(
6666
umo=context.context.event.unified_msg_origin
6767
)
68-
return cfg.get("provider_settings", {}).get("computer_use_local_python_cwd")
68+
return cfg.get("provider_settings", {}).get("computer_use_local_python_cwd", None)
6969

7070

7171
@dataclass

astrbot/core/computer/tools/shell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _get_configured_cwd(self, context: ContextWrapper[AstrAgentContext]) -> str
4444
cfg = context.context.context.get_config(
4545
umo=context.context.event.unified_msg_origin
4646
)
47-
return cfg.get("provider_settings", {}).get("computer_use_local_shell_cwd")
47+
return cfg.get("provider_settings", {}).get("computer_use_local_shell_cwd", None)
4848

4949
async def call(
5050
self,

0 commit comments

Comments
 (0)