@timmy_hu/plugin-middleware 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "插件中间件",
3
+ "code": "plugin-middleware",
4
+ "version": "1.0.0",
5
+ "description": "自动发现所有中间件类型插件,支持动态按需加载、卸载和状态查询,为智能体编排提供灵活的中间件管理能力。",
6
+ "createdBy": "fpi",
7
+ "platform": "http://10.161.48.53:3300",
8
+ "category": "middleware",
9
+ "main": "index.js"
10
+ }
package/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # 插件中间件 (Plugin Middleware)
2
+
3
+ ## 插件简介
4
+
5
+ **插件中间件** 是一个 XpertAI 平台的中间件类型插件,功能类似"技能中间件"。它能够自动发现平台上所有类型为"中间件"的插件,并提供动态按需加载、卸载、状态查询和批量管理能力,为智能体编排提供灵活的中间件管理基础设施。
6
+
7
+ ## 适用场景
8
+
9
+ - **智能体编排**:在智能体运行时动态发现和加载所需的中间件插件,无需预先配置所有中间件。
10
+ - **插件管理**:统一管理平台上所有中间件插件的生命周期,包括加载、卸载和状态监控。
11
+ - **按需加载**:根据智能体任务需求,动态加载特定中间件插件,减少资源占用。
12
+ - **批量操作**:一次性加载或管理多个中间件插件,提高运维效率。
13
+ - **缓存优化**:内置缓存机制,避免频繁调用平台 API,提升响应速度。
14
+
15
+ ## 目录结构
16
+
17
+ ```
18
+ plugin-middleware/
19
+ ├── .xpertai-plugin/
20
+ │ └── plugin.json # 插件清单
21
+ ├── index.js # 插件入口(CommonJS)
22
+ ├── package.json # 项目依赖与元信息
23
+ ├── README.md # 本文件
24
+ ├── src/
25
+ │ ├── plugin-middleware.module.ts # NestJS 模块定义
26
+ │ ├── middlewares/
27
+ │ │ └── plugin-middleware.middleware.ts # 中间件策略实现
28
+ │ └── schemas/
29
+ │ └── config.schema.ts # Zod 配置 Schema
30
+ └── examples/
31
+ └── request.example.json # 工具调用示例
32
+ ```
33
+
34
+ ## 安装与导入
35
+
36
+ ### 通过 npm 安装到 XpertAI 平台
37
+
38
+ ```bash
39
+ # 在 XpertAI 平台通过 API 安装
40
+ POST http://10.161.48.53:3300/api/plugin
41
+ {
42
+ "pluginName": "@timmy_hu/plugin-middleware",
43
+ "version": "1.0.0",
44
+ "source": "npm"
45
+ }
46
+ ```
47
+
48
+ ### 本地开发
49
+
50
+ ```bash
51
+ npm install
52
+ node -e "require('./index.js')" # 验证入口
53
+ ```
54
+
55
+ ## 配置项说明
56
+
57
+ | 配置项 | 类型 | 必填 | 默认值 | 说明 |
58
+ |--------|------|------|--------|------|
59
+ | `platformUrl` | string | 是 | `http://10.161.48.53:3300` | XpertAI 平台基础地址 |
60
+ | `organizationId` | string | 否 | - | 组织 ID,用于限定插件查询范围 |
61
+ | `autoDiscover` | boolean | 否 | `true` | 是否在启动时自动发现可用中间件插件 |
62
+ | `cacheTtlSeconds` | number | 否 | `300` | 插件发现缓存有效期(秒) |
63
+ | `loadTimeoutMs` | number | 否 | `30000` | 插件加载超时时间(毫秒) |
64
+ | `offlineMode` | boolean | 否 | `true` | 是否启用离线模式 |
65
+
66
+ ## Agent Middleware 工具列表
67
+
68
+ 本插件通过 `PluginMiddleware` provider 的 `createMiddleware()` 方法暴露以下 6 个工具:
69
+
70
+ ### 1. `list_middleware_plugins` - 列出中间件插件
71
+
72
+ 列出所有已发现的中间件类型插件,可按加载状态过滤。
73
+
74
+ **入参:**
75
+ | 参数 | 类型 | 必填 | 说明 |
76
+ |------|------|------|------|
77
+ | `filter` | enum | 否 | 过滤条件:`all`=全部, `loaded`=已加载, `available`=可用未加载 |
78
+
79
+ **返回值:**
80
+ ```json
81
+ {
82
+ "success": true,
83
+ "total": 3,
84
+ "plugins": [
85
+ {
86
+ "code": "weather-query",
87
+ "name": "天气查询",
88
+ "version": "1.0.0",
89
+ "description": "查询城市天气信息",
90
+ "status": "loaded",
91
+ "loadedAt": "2026-07-07T10:00:00.000Z"
92
+ }
93
+ ]
94
+ }
95
+ ```
96
+
97
+ ### 2. `load_middleware_plugin` - 加载中间件插件
98
+
99
+ 按插件编码动态加载指定的中间件插件。
100
+
101
+ **入参:**
102
+ | 参数 | 类型 | 必填 | 说明 |
103
+ |------|------|------|------|
104
+ | `pluginCode` | string | 是 | 要加载的中间件插件编码 |
105
+
106
+ **返回值:**
107
+ ```json
108
+ {
109
+ "success": true,
110
+ "message": "插件 pg-query-tool 加载成功",
111
+ "plugin": {
112
+ "code": "pg-query-tool",
113
+ "name": "PostgreSQL 查询工具",
114
+ "version": "1.0.0",
115
+ "loadedAt": "2026-07-07T10:05:00.000Z",
116
+ "status": "loaded"
117
+ }
118
+ }
119
+ ```
120
+
121
+ ### 3. `unload_middleware_plugin` - 卸载中间件插件
122
+
123
+ 按插件编码卸载已加载的中间件插件。
124
+
125
+ **入参:**
126
+ | 参数 | 类型 | 必填 | 说明 |
127
+ |------|------|------|------|
128
+ | `pluginCode` | string | 是 | 要卸载的中间件插件编码 |
129
+
130
+ ### 4. `get_middleware_plugin_status` - 查询插件状态
131
+
132
+ 查询指定中间件插件的详细状态信息。
133
+
134
+ **入参:**
135
+ | 参数 | 类型 | 必填 | 说明 |
136
+ |------|------|------|------|
137
+ | `pluginCode` | string | 是 | 要查询的中间件插件编码 |
138
+
139
+ ### 5. `refresh_plugin_cache` - 刷新缓存
140
+
141
+ 刷新中间件插件发现缓存,强制从平台重新获取插件列表。
142
+
143
+ **入参:** 无
144
+
145
+ ### 6. `batch_load_middleware_plugins` - 批量加载
146
+
147
+ 批量加载多个中间件插件。
148
+
149
+ **入参:**
150
+ | 参数 | 类型 | 必填 | 说明 |
151
+ |------|------|------|------|
152
+ | `pluginCodes` | string[] | 是 | 要批量加载的中间件插件编码数组 |
153
+
154
+ ## 调用示例
155
+
156
+ ```json
157
+ // 列出所有中间件插件
158
+ {
159
+ "tool": "list_middleware_plugins",
160
+ "input": { "filter": "all" }
161
+ }
162
+
163
+ // 加载指定插件
164
+ {
165
+ "tool": "load_middleware_plugin",
166
+ "input": { "pluginCode": "pg-query-tool" }
167
+ }
168
+
169
+ // 批量加载
170
+ {
171
+ "tool": "batch_load_middleware_plugins",
172
+ "input": { "pluginCodes": ["weather-query", "pg-query-tool"] }
173
+ }
174
+ ```
175
+
176
+ ## 常见错误和处理方式
177
+
178
+ | 错误 | 原因 | 处理方式 |
179
+ |------|------|----------|
180
+ | `未找到编码为 xxx 的中间件插件` | 插件编码不存在或未被发现 | 先调用 `list_middleware_plugins` 确认可用插件列表 |
181
+ | `插件 xxx 已经处于加载状态` | 重复加载 | 无需处理,插件已可用 |
182
+ | `插件 xxx 当前未加载,无需卸载` | 重复卸载 | 无需处理 |
183
+ | `Platform API returned 401` | 鉴权失败 | 检查组织 ID 和平台访问权限 |
184
+ | `Failed to discover middleware plugins` | 平台 API 不可达 | 检查 `platformUrl` 配置和网络连接 |
185
+
186
+ ## 安全注意事项
187
+
188
+ - 本插件通过平台 API 发现和加载中间件插件,需要确保平台 API 的访问权限已正确配置。
189
+ - 加载的中间件插件将继承当前智能体的权限范围,请确保只加载受信任的中间件插件。
190
+ - 缓存机制可防止频繁调用平台 API,但在插件列表变更时需要手动刷新缓存。
191
+ - 离线模式下,插件状态仅在本地维护,重启后会丢失。
192
+
193
+ ## 使用方式
194
+
195
+ 1. 在 XpertAI 平台安装本插件后,在智能体编排中添加"插件中间件管理器"中间件。
196
+ 2. 配置 `platformUrl` 和 `organizationId`(可选)。
197
+ 3. 智能体即可通过对话调用 `list_middleware_plugins`、`load_middleware_plugin` 等工具,动态管理中间件插件。
198
+
199
+ ## 下一步
200
+
201
+ 安装成功后,前往 http://10.161.48.53:3300 的智能体编排页面,添加"插件中间件管理器"中间件,即可在对话中使用插件发现和动态加载能力。
@@ -0,0 +1,119 @@
1
+ {
2
+ "tools": [
3
+ {
4
+ "name": "list_middleware_plugins",
5
+ "description": "列出所有已发现的中间件类型插件",
6
+ "input": {
7
+ "filter": "all"
8
+ },
9
+ "expectedOutput": {
10
+ "success": true,
11
+ "total": 3,
12
+ "plugins": [
13
+ {
14
+ "code": "weather-query",
15
+ "name": "天气查询",
16
+ "version": "1.0.0",
17
+ "description": "查询城市天气信息",
18
+ "status": "loaded",
19
+ "loadedAt": "2026-07-07T10:00:00.000Z"
20
+ },
21
+ {
22
+ "code": "pg-query-tool",
23
+ "name": "PostgreSQL 查询工具",
24
+ "version": "1.0.0",
25
+ "description": "执行 PostgreSQL 数据库查询",
26
+ "status": "available",
27
+ "loadedAt": null
28
+ }
29
+ ]
30
+ }
31
+ },
32
+ {
33
+ "name": "load_middleware_plugin",
34
+ "description": "动态加载指定的中间件插件",
35
+ "input": {
36
+ "pluginCode": "pg-query-tool"
37
+ },
38
+ "expectedOutput": {
39
+ "success": true,
40
+ "message": "插件 pg-query-tool 加载成功",
41
+ "plugin": {
42
+ "code": "pg-query-tool",
43
+ "name": "PostgreSQL 查询工具",
44
+ "version": "1.0.0",
45
+ "loadedAt": "2026-07-07T10:05:00.000Z",
46
+ "status": "loaded"
47
+ }
48
+ }
49
+ },
50
+ {
51
+ "name": "unload_middleware_plugin",
52
+ "description": "卸载已加载的中间件插件",
53
+ "input": {
54
+ "pluginCode": "pg-query-tool"
55
+ },
56
+ "expectedOutput": {
57
+ "success": true,
58
+ "message": "插件 pg-query-tool 已卸载",
59
+ "plugin": {
60
+ "code": "pg-query-tool",
61
+ "name": "PostgreSQL 查询工具",
62
+ "unloadedAt": "2026-07-07T10:10:00.000Z",
63
+ "status": "unloaded"
64
+ }
65
+ }
66
+ },
67
+ {
68
+ "name": "get_middleware_plugin_status",
69
+ "description": "查询指定中间件插件的详细状态",
70
+ "input": {
71
+ "pluginCode": "weather-query"
72
+ },
73
+ "expectedOutput": {
74
+ "success": true,
75
+ "plugin": {
76
+ "code": "weather-query",
77
+ "name": "天气查询",
78
+ "version": "1.0.0",
79
+ "description": "查询城市天气信息",
80
+ "category": "middleware",
81
+ "status": "loaded",
82
+ "loadedAt": "2026-07-07T10:00:00.000Z",
83
+ "tools": [],
84
+ "configRequired": []
85
+ }
86
+ }
87
+ },
88
+ {
89
+ "name": "refresh_plugin_cache",
90
+ "description": "刷新插件发现缓存",
91
+ "input": {},
92
+ "expectedOutput": {
93
+ "success": true,
94
+ "message": "缓存已刷新,发现 5 个中间件插件",
95
+ "total": 5,
96
+ "refreshedAt": "2026-07-07T10:15:00.000Z"
97
+ }
98
+ },
99
+ {
100
+ "name": "batch_load_middleware_plugins",
101
+ "description": "批量加载多个中间件插件",
102
+ "input": {
103
+ "pluginCodes": ["weather-query", "pg-query-tool", "email-sender"]
104
+ },
105
+ "expectedOutput": {
106
+ "success": true,
107
+ "total": 3,
108
+ "loaded": 2,
109
+ "alreadyLoaded": 1,
110
+ "failed": 0,
111
+ "results": [
112
+ { "code": "weather-query", "status": "already_loaded", "success": true },
113
+ { "code": "pg-query-tool", "name": "PostgreSQL 查询工具", "version": "1.0.0", "status": "loaded", "success": true },
114
+ { "code": "email-sender", "name": "邮件发送", "version": "1.0.0", "status": "loaded", "success": true }
115
+ ]
116
+ }
117
+ }
118
+ ]
119
+ }