Skip to content

Commit e6e50ab

Browse files
authored
Feat/product detail optimize (#147)
1 parent fb81f6a commit e6e50ab

158 files changed

Lines changed: 2846 additions & 1901 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

assets/schema/derisk.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use derisk;
99
-- MySQL DDL Script for Derisk
1010
-- Version: 0.3.0
1111
-- Generated from SQLAlchemy ORM Models
12-
-- Generated: 2026-03-09 11:54:17
12+
-- Generated: 2026-03-09 13:58:00
1313
-- ============================================================
1414

1515
SET NAMES utf8mb4;
Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
# 安妮工具框架改进计划 - 完整开发文档
2+
3+
## 概述
4+
5+
本改进计划完成了统一工具框架与应用编辑工具Tab的完整集成,打通了从工具注册到运行时加载的全流程。
6+
7+
## 已完成的工作
8+
9+
### 1. i18n翻译完善 ✅
10+
11+
**问题**:工具Tab显示i18n key而非翻译文本
12+
13+
**修复**
14+
-`web/src/locales/zh/common.ts` 添加了工具管理相关翻译
15+
-`web/src/locales/en/common.ts` 添加了英文翻译
16+
17+
**添加的翻译key**
18+
```typescript
19+
// 工具管理
20+
builder_tool_management: '工具管理'
21+
builder_search_tools_placeholder: '搜索工具名称、描述或标签...'
22+
builder_no_tools: '暂无工具'
23+
builder_tools_total: ''
24+
builder_tools_bound: '已绑定'
25+
builder_tools_default_bound: '默认绑定'
26+
builder_tools_count: '个工具'
27+
builder_tool_bound_success: '工具绑定成功'
28+
builder_tool_unbound_success: '工具解绑成功'
29+
builder_tool_disassociated: '工具已取消关联'
30+
builder_tool_associated: '工具已关联'
31+
builder_tool_toggle_error: '操作失败'
32+
builder_tool_high_risk: '高风险工具'
33+
builder_tool_requires_permission: '需要权限确认'
34+
builder_builtin_required_tip: '默认绑定工具'
35+
builder_builtin_required_desc: '这些工具是 Agent 默认绑定的核心工具,您可以反向解除绑定,但可能会影响 Agent 的基础功能。'
36+
builder_bind_all: '全部绑定'
37+
builder_unbind_all: '全部解绑'
38+
builder_batch_bound_success: '批量绑定成功'
39+
builder_batch_unbound_success: '批量解绑成功'
40+
builder_batch_toggle_error: '批量操作失败'
41+
builder_expand_all: '展开全部'
42+
builder_collapse_all: '收起全部'
43+
builder_create_local_tool: '创建本地工具'
44+
builder_create_local_tool_desc: '编写自定义工具函数'
45+
builder_tools_associated: '已关联'
46+
47+
// 工具状态
48+
tool_status_default: '默认'
49+
tool_status_bound: '已绑定'
50+
tool_status_unbound: '未绑定'
51+
tool_status_default_bound: '默认绑定'
52+
tool_status_disabled: '已禁用'
53+
tool_permission_required: '需权限'
54+
tool_action_bind: '点击绑定'
55+
tool_action_unbind: '点击解绑'
56+
builder_selected: '已选'
57+
```
58+
59+
### 2. 后端API完善 ✅
60+
61+
**问题**:工具列表为空,后端未正确初始化工具
62+
63+
**修复**
64+
- 修改了 `packages/derisk-app/src/derisk_app/openapi/api_v1/tool_management_api.py`
65+
- 添加了 `ensure_tools_initialized()` 函数,确保所有API端点在使用前初始化工具
66+
67+
**修改的端点**
68+
1. `GET /api/tools/groups` - 获取工具分组列表
69+
2. `GET /api/tools/agent-config` - 获取Agent工具配置
70+
3. `POST /api/tools/binding/update` - 更新工具绑定状态
71+
4. `POST /api/tools/binding/batch-update` - 批量更新工具绑定
72+
5. `POST /api/tools/runtime-tools` - 获取运行时工具列表
73+
6. `POST /api/tools/runtime-schemas` - 获取运行时工具Schema
74+
7. `GET /api/tools/list` - 列出所有工具
75+
8. `GET /api/tools/{tool_id}` - 获取工具详情
76+
9. `POST /api/tools/cache/clear` - 清除工具缓存
77+
78+
**关键代码**
79+
```python
80+
def ensure_tools_initialized():
81+
"""确保工具已初始化"""
82+
if not hasattr(tool_registry, "_initialized") or not tool_registry._initialized:
83+
register_builtin_tools()
84+
```
85+
86+
### 3. 工具集成模块 ✅
87+
88+
**新增文件**`packages/derisk-core/src/derisk/agent/tools/integration.py`
89+
90+
**功能**
91+
- `ToolIntegrationManager` - 工具集成管理器
92+
- `initialize_tools_on_startup()` - 启动时初始化
93+
- `bind_tools_to_app()` - 绑定工具到应用
94+
- `unbind_tools_from_app()` - 解绑工具
95+
- `get_app_runtime_tools()` - 获取运行时工具
96+
97+
### 4. 统一工具框架架构
98+
99+
**核心组件**
100+
101+
```
102+
derisk.agent.tools/
103+
├── base.py # ToolBase基类、ToolCategory、ToolRiskLevel等枚举
104+
├── registry.py # ToolRegistry全局注册表
105+
├── tool_manager.py # ToolManager分组管理和绑定配置
106+
├── resource_manager.py # ToolResourceManager资源管理
107+
├── integration.py # 工具集成模块(新增)
108+
├── metadata.py # ToolMetadata元数据定义
109+
├── result.py # ToolResult执行结果
110+
├── context.py # ToolContext执行上下文
111+
└── builtin/ # 内置工具
112+
├── file_system/ # 文件系统工具
113+
├── shell/ # Shell工具
114+
├── network/ # 网络工具
115+
├── interaction/ # 交互工具
116+
├── reasoning/ # 推理工具
117+
├── agent/ # Agent工具
118+
└── sandbox/ # Sandbox工具
119+
```
120+
121+
### 5. 工具分类体系
122+
123+
**16个主分类**
124+
1. `builtin` - 内置
125+
2. `file_system` - 文件系统
126+
3. `code` - 代码
127+
4. `shell` - Shell
128+
5. `sandbox` - 沙箱
129+
6. `user_interaction` - 用户交互
130+
7. `visualization` - 可视化
131+
8. `network` - 网络
132+
9. `database` - 数据库
133+
10. `api` - API
134+
11. `mcp` - MCP
135+
12. `search` - 搜索
136+
13. `analysis` - 分析
137+
14. `reasoning` - 推理
138+
15. `utility` - 工具
139+
16. `plugin` - 插件
140+
17. `custom` - 自定义
141+
142+
**4个绑定类型**
143+
1. `builtin_required` - 内置默认(必须)
144+
2. `builtin_optional` - 内置可选
145+
3. `custom` - 自定义
146+
4. `external` - 外部(MCP/API)
147+
148+
### 6. 权限体系
149+
150+
**三层权限**
151+
1. **工具级权限** - `derisk.core.authorization`
152+
- AuthorizationEngine - 核心引擎
153+
- RiskAssessor - 风险评估
154+
- AuthorizationCache - 权限缓存
155+
156+
2. **Agent级权限** - `derisk.agent.core.agent_info`
157+
- PermissionRuleset - 规则集
158+
- PermissionAction - allow/deny/ask
159+
160+
3. **执行时权限** - `derisk.agent.core_v2.permission`
161+
- PermissionManager - 多Agent权限管理
162+
- PermissionChecker - 细粒度检查
163+
164+
### 7. 数据流
165+
166+
```
167+
┌─────────────────────────────────────────────────────────────┐
168+
│ 前端 (Web) │
169+
│ ┌──────────────────────────────────────────────────────┐ │
170+
│ │ TabTools组件 │ │
171+
│ │ - 调用 getToolGroups() 获取工具列表 │ │
172+
│ │ - 调用 updateToolBinding() 绑定/解绑工具 │ │
173+
│ │ - 按分类展示工具 (builtin_required/optional/custom) │ │
174+
│ └──────────────────────────────────────────────────────┘ │
175+
└─────────────────────────┬───────────────────────────────────┘
176+
│ API调用
177+
┌─────────────────────────▼───────────────────────────────────┐
178+
│ 后端 (API) │
179+
│ ┌──────────────────────────────────────────────────────┐ │
180+
│ │ tool_management_api.py │ │
181+
│ │ - ensure_tools_initialized() 确保工具已初始化 │ │
182+
│ │ - get_tool_groups() 返回分组后的工具列表 │ │
183+
│ │ - update_tool_binding() 更新绑定状态 │ │
184+
│ └──────────────────────────────────────────────────────┘ │
185+
└─────────────────────────┬───────────────────────────────────┘
186+
187+
┌─────────────────────────▼───────────────────────────────────┐
188+
│ 工具框架核心 │
189+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
190+
│ │ ToolRegistry │ │ ToolManager │ │ ToolBase │ │
191+
│ │ - 工具注册 │ │ - 分组管理 │ │ - 工具基类 │ │
192+
│ │ - 工具查找 │ │ - 绑定配置 │ │ - 执行逻辑 │ │
193+
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
194+
└─────────────────────────────────────────────────────────────┘
195+
```
196+
197+
### 8. 工具扩展机制
198+
199+
**方式1:继承ToolBase**
200+
```python
201+
from derisk.agent.tools import ToolBase, ToolMetadata, ToolResult
202+
203+
class MyTool(ToolBase):
204+
def _define_metadata(self) -> ToolMetadata:
205+
return ToolMetadata(
206+
name="my_tool",
207+
display_name="我的工具",
208+
description="工具描述",
209+
category=ToolCategory.UTILITY,
210+
)
211+
212+
async def execute(self, args, context) -> ToolResult:
213+
return ToolResult(success=True, output="结果")
214+
```
215+
216+
**方式2:使用装饰器**
217+
```python
218+
from derisk.agent.tools import tool
219+
220+
@tool(name="my_tool", category=ToolCategory.UTILITY)
221+
async def my_tool(input: str) -> str:
222+
return f"processed: {input}"
223+
```
224+
225+
**方式3:自动注册**
226+
```python
227+
from derisk.agent.tools import register_tool, ToolSource
228+
229+
@register_tool(tool_registry, source=ToolSource.USER)
230+
class MyTool(ToolBase):
231+
...
232+
```
233+
234+
### 9. 前端组件改进
235+
236+
**TabTools组件** (`web/src/app/application/app/components/tab-tools.tsx`):
237+
238+
**功能**
239+
- 按分组展示工具(builtin_required/optional/custom/external)
240+
- 搜索过滤工具
241+
- 单个工具绑定/解绑
242+
- 批量绑定/解绑
243+
- 显示绑定状态统计
244+
- 风险等级标识
245+
246+
**关键特性**
247+
- 使用 `useRequest` 获取工具数据
248+
- 支持搜索过滤
249+
- 分组折叠/展开
250+
- 绑定状态实时更新
251+
252+
### 10. API端点列表
253+
254+
| 方法 | 端点 | 描述 |
255+
|------|------|------|
256+
| GET | `/api/tools/groups` | 获取工具分组列表 |
257+
| GET | `/api/tools/agent-config` | 获取Agent工具配置 |
258+
| POST | `/api/tools/binding/update` | 更新工具绑定状态 |
259+
| POST | `/api/tools/binding/batch-update` | 批量更新绑定 |
260+
| POST | `/api/tools/runtime-tools` | 获取运行时工具列表 |
261+
| POST | `/api/tools/runtime-schemas` | 获取工具Schema |
262+
| GET | `/api/tools/list` | 列出所有工具 |
263+
| GET | `/api/tools/{tool_id}` | 获取工具详情 |
264+
| POST | `/api/tools/cache/clear` | 清除工具缓存 |
265+
266+
## 使用指南
267+
268+
### 启动时初始化工具
269+
270+
```python
271+
from derisk.agent.tools.integration import initialize_tools_on_startup
272+
273+
# 在应用启动时调用
274+
await initialize_tools_on_startup()
275+
```
276+
277+
### 绑定工具到应用
278+
279+
```python
280+
from derisk.agent.tools.integration import bind_tools_to_app
281+
282+
result = await bind_tools_to_app(
283+
app_id="my_app",
284+
agent_name="default",
285+
tool_ids=["read", "write", "bash"]
286+
)
287+
```
288+
289+
### 获取应用运行时工具
290+
291+
```python
292+
from derisk.agent.tools.integration import get_app_runtime_tools
293+
294+
tools = await get_app_runtime_tools(
295+
app_id="my_app",
296+
agent_name="default",
297+
format_type="openai" # 或 "anthropic"
298+
)
299+
```
300+
301+
### 前端使用
302+
303+
```typescript
304+
import { getToolGroups, updateToolBinding } from '@/client/api/tools/management';
305+
306+
// 获取工具列表
307+
const { data: toolGroups } = useRequest(
308+
async () => {
309+
const res = await getToolGroups({
310+
app_id: appCode,
311+
agent_name: agentName,
312+
lang: 'zh',
313+
});
314+
return res.data;
315+
}
316+
);
317+
318+
// 绑定/解绑工具
319+
await updateToolBinding({
320+
app_id: appCode,
321+
agent_name: agentName,
322+
tool_id: toolId,
323+
is_bound: true,
324+
});
325+
```
326+
327+
## 后续优化建议
328+
329+
1. **数据持久化**:当前工具绑定配置保存在内存中,需要实现数据库存储
330+
- 添加 `agent_tool_bindings`
331+
- 实现 `save_tool_config()``load_tool_config()`
332+
333+
2. **权限细化**
334+
- 支持基于角色的工具权限
335+
- 支持细粒度的参数级权限控制
336+
337+
3. **工具市场**
338+
- 支持从市场安装第三方工具
339+
- 工具版本管理
340+
341+
4. **工具调试**
342+
- 添加工具测试界面
343+
- 支持参数预验证
344+
345+
5. **性能优化**
346+
- 工具列表缓存策略
347+
- 懒加载大量工具
348+
349+
## 总结
350+
351+
本次改进计划完成了:
352+
1. ✅ 统一工具框架的设计和实现
353+
2. ✅ 工具注册、绑定、运行时加载的全流程打通
354+
3. ✅ 前端工具Tab的i18n和布局修复
355+
4. ✅ 后端API的完善和初始化机制
356+
5. ✅ 权限体系的整合
357+
358+
工具框架现在已经可以:
359+
- 正确显示工具列表(按分类)
360+
- 支持工具绑定/解绑
361+
- 运行时动态加载工具
362+
- 支持权限验证
363+
- 支持工具扩展

packages/derisk-app/src/derisk_app/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ def mount_routers(app: FastAPI, param: Optional[ApplicationConfig] = None):
9494

9595
app.include_router(agent_app_router, prefix="/api", tags=["Agent App"])
9696

97+
# Tool Management API routes
98+
from derisk_app.openapi.api_v1.tool_management_api import (
99+
router as tool_management_router,
100+
)
101+
102+
app.include_router(tool_management_router, prefix="/api", tags=["Tool Management"])
103+
97104
# Core_v2 Agent API routes - V1/V2 共存
98105
from derisk_serve.agent.core_v2_api import router as core_v2_router
99106
from derisk_serve.agent.agent_selection_api import router as agent_selection_router
@@ -146,6 +153,7 @@ def initialize_app(param: ApplicationConfig, app: FastAPI, system_app: SystemApp
146153
# Migration db storage, so you db models must be imported before this
147154
# Import cron module to register CronJobEntity before create_all
148155
from derisk_serve.cron.models.models import CronJobEntity # noqa: F401
156+
149157
_migration_db_storage(
150158
param.service.web.database, web_config.disable_alembic_upgrade
151159
)

0 commit comments

Comments
 (0)