@johnny-joster/n9e-plugin 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,8 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(npx tsc:*)",
5
+ "Bash(npm install)"
6
+ ]
7
+ }
8
+ }
package/README.md ADDED
@@ -0,0 +1,191 @@
1
+ # N9e Plugin for OpenClaw
2
+
3
+ 夜莺(Nightingale)告警平台查询插件,支持通过自然语言查询告警事件、历史告警、告警规则和告警屏蔽配置。
4
+
5
+ ## 功能特性
6
+
7
+ - ✅ 查询当前活跃告警
8
+ - ✅ 查询历史告警记录
9
+ - ✅ 查询告警规则配置
10
+ - ✅ 查询告警屏蔽规则
11
+ - ✅ 自然语言交互
12
+ - ✅ 全局查询(跨所有业务组)
13
+ - ✅ 完善的错误处理
14
+
15
+ ## 快速开始
16
+
17
+ ### 1. 安装插件
18
+
19
+ ```bash
20
+ # 从本地目录安装(开发模式)
21
+ openclaw plugins install -l ./n9e-plugin
22
+
23
+ # 或从npm安装
24
+ openclaw plugins install @youdao/n9e-plugin
25
+ ```
26
+
27
+ ### 2. 配置插件
28
+
29
+ 编辑OpenClaw配置文件,添加以下配置:
30
+
31
+ ```json
32
+ {
33
+ "plugins": {
34
+ "entries": {
35
+ "n9e-plugin": {
36
+ "enabled": true,
37
+ "config": {
38
+ "apiBaseUrl": "https://n9e-dev.inner.youdao.com/api/n9e",
39
+ "apiKey": "your-api-key-here",
40
+ "timeout": 30000
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ ```
47
+
48
+ ### 3. 重启Gateway
49
+
50
+ ```bash
51
+ openclaw gateway restart
52
+ ```
53
+
54
+ ### 4. 验证安装
55
+
56
+ ```bash
57
+ openclaw plugins list
58
+ ```
59
+
60
+ ## 使用示例
61
+
62
+ 与Agent对话即可使用:
63
+
64
+ ```
65
+ 用户: 查一下最近的告警
66
+ → Agent调用 n9e_query_active_alerts
67
+ → 返回: 当前有5条活跃告警...
68
+
69
+ 用户: 昨天有哪些告警
70
+ → Agent调用 n9e_query_historical_alerts
71
+ → 返回: 昨天共有15条告警...
72
+
73
+ 用户: CPU相关的告警规则
74
+ → Agent调用 n9e_query_alert_rules
75
+ → 返回: 找到3条CPU相关规则...
76
+
77
+ 用户: 哪些告警被屏蔽了
78
+ → Agent调用 n9e_query_alert_mutes
79
+ → 返回: 当前有2条屏蔽规则...
80
+ ```
81
+
82
+ ## API Tools
83
+
84
+ ### 1. n9e_query_active_alerts
85
+
86
+ 查询当前活跃告警。
87
+
88
+ **参数:**
89
+ - `severity`: 严重级别(1=严重,2=警告,3=提示)
90
+ - `rule_name`: 规则名称(支持模糊匹配)
91
+ - `limit`: 返回数量(默认50)
92
+ - `query`: 搜索关键词
93
+
94
+ ### 2. n9e_query_historical_alerts
95
+
96
+ 查询历史告警记录。
97
+
98
+ **参数:**
99
+ - `severity`: 严重级别
100
+ - `rule_name`: 规则名称
101
+ - `stime`: 开始时间戳(秒)
102
+ - `etime`: 结束时间戳(秒)
103
+ - `limit`: 返回数量(默认50)
104
+
105
+ ### 3. n9e_query_alert_rules
106
+
107
+ 查询告警规则配置。
108
+
109
+ **参数:**
110
+ - `rule_name`: 规则名称
111
+ - `datasource_ids`: 数据源ID列表
112
+ - `disabled`: 规则状态(0=启用,1=禁用)
113
+ - `limit`: 返回数量(默认50)
114
+
115
+ ### 4. n9e_query_alert_mutes
116
+
117
+ 查询告警屏蔽规则。
118
+
119
+ **参数:**
120
+ - `query`: 搜索关键词
121
+ - `limit`: 返回数量(默认50)
122
+
123
+ ## 项目结构
124
+
125
+ ```
126
+ n9e-plugin/
127
+ ├── openclaw.plugin.json # 插件清单
128
+ ├── package.json
129
+ ├── index.ts # 插件入口
130
+ ├── src/
131
+ │ ├── config-schema.ts # Zod配置Schema
132
+ │ ├── n9e-client.ts # 夜莺API客户端
133
+ │ ├── types.ts # TypeScript类型定义
134
+ │ └── tools/ # 工具实现
135
+ │ ├── active-alerts.ts
136
+ │ ├── historical-alerts.ts
137
+ │ ├── alert-rules.ts
138
+ │ └── alert-mutes.ts
139
+ └── skills/ # Agent技能
140
+ ├── query-active-alerts/
141
+ ├── query-historical-alerts/
142
+ ├── query-alert-rules/
143
+ └── query-alert-mutes/
144
+ ```
145
+
146
+ ## 开发
147
+
148
+ ### 安装依赖
149
+
150
+ ```bash
151
+ npm install
152
+ ```
153
+
154
+ ### 链接模式开发
155
+
156
+ ```bash
157
+ npm run dev
158
+ # 等同于: openclaw plugins install -l .
159
+ ```
160
+
161
+ ### 类型检查
162
+
163
+ ```bash
164
+ npx tsc --noEmit
165
+ ```
166
+
167
+ ## 配置说明
168
+
169
+ | 字段 | 类型 | 必需 | 说明 |
170
+ |------|------|------|------|
171
+ | `apiBaseUrl` | string | ✅ | 夜莺API地址 |
172
+ | `apiKey` | string | ✅ | API访问密钥 |
173
+ | `timeout` | number | ❌ | 请求超时(毫秒),默认30000 |
174
+
175
+ ## 错误处理
176
+
177
+ | 错误类型 | 提示信息 |
178
+ |---------|---------|
179
+ | 网络错误 | 无法连接到夜莺平台,请检查网络或配置 |
180
+ | 401/403 | 认证失败,请检查API Key配置 |
181
+ | 404 | 未找到指定的资源 |
182
+ | 500 | 夜莺服务器错误,请稍后重试 |
183
+ | 超时 | 请求超时,请检查网络连接 |
184
+
185
+ ## 许可证
186
+
187
+ MIT
188
+
189
+ ## 作者
190
+
191
+ Youdao DevOps Team
@@ -0,0 +1,377 @@
1
+ # 夜莺(Nightingale) OpenClaw 插件设计文档
2
+
3
+ **日期**: 2026-02-12
4
+ **版本**: 1.0
5
+ **状态**: 已批准
6
+
7
+ ## 概述
8
+
9
+ 为 OpenClaw 平台开发一个夜莺告警平台查询插件,支持通过自然语言查询告警事件、历史告警、告警规则和告警屏蔽配置。
10
+
11
+ ## 设计目标
12
+
13
+ - **自然语言交互**: 用户使用自然语言提问,AI agent 自动选择合适的工具调用
14
+ - **全面覆盖**: 支持活跃告警、历史告警、告警规则、告警屏蔽四大数据类型
15
+ - **全局查询**: 跨所有业务组查询,无需配置特定业务组
16
+ - **简单认证**: 使用静态 API Key (X-API-Key header) 认证
17
+
18
+ ## 架构设计
19
+
20
+ ### 项目结构
21
+
22
+ ```
23
+ n9e-plugin/
24
+ ├── openclaw.plugin.json # 插件清单
25
+ ├── package.json
26
+ ├── index.ts # 插件入口
27
+ ├── src/
28
+ │ ├── config-schema.ts # Zod 配置 Schema
29
+ │ ├── n9e-client.ts # 夜莺 API 客户端
30
+ │ ├── types.ts # TypeScript 类型定义
31
+ │ └── tools/ # 工具实现
32
+ │ ├── active-alerts.ts
33
+ │ ├── historical-alerts.ts
34
+ │ ├── alert-rules.ts
35
+ │ └── alert-mutes.ts
36
+ └── skills/ # Agent 技能
37
+ ├── query-active-alerts/
38
+ │ └── SKILL.md
39
+ ├── query-historical-alerts/
40
+ │ └── SKILL.md
41
+ ├── query-alert-rules/
42
+ │ └── SKILL.md
43
+ └── query-alert-mutes/
44
+ └── SKILL.md
45
+ ```
46
+
47
+ ### 配置 Schema
48
+
49
+ ```typescript
50
+ const n9ePluginConfigSchema = z.object({
51
+ apiBaseUrl: z.string().url().describe("夜莺API地址"),
52
+ apiKey: z.string().min(1).describe("API访问密钥"),
53
+ timeout: z.number().min(1000).max(120000).default(30000).describe("请求超时(毫秒)")
54
+ });
55
+ ```
56
+
57
+ **配置示例**:
58
+ ```json
59
+ {
60
+ "plugins": {
61
+ "entries": {
62
+ "n9e-plugin": {
63
+ "enabled": true,
64
+ "config": {
65
+ "apiBaseUrl": "https://n9e-dev.inner.youdao.com/api/n9e",
66
+ "apiKey": "your-api-key-here",
67
+ "timeout": 30000
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ ```
74
+
75
+ ## Agent Tools 设计
76
+
77
+ ### 1. n9e_query_active_alerts
78
+
79
+ **功能**: 查询当前活跃的告警事件
80
+
81
+ **参数**:
82
+ - `severity` (optional, number): 严重级别 (1=严重, 2=警告, 3=提示)
83
+ - `rule_name` (optional, string): 按规则名称筛选
84
+ - `limit` (optional, number): 返回数量限制 (默认: 50)
85
+ - `query` (optional, string): 搜索查询字符串
86
+
87
+ **API 端点**: `GET /api/n9e/alert-cur-events/list`
88
+
89
+ **返回格式**:
90
+ ```json
91
+ {
92
+ "total": 5,
93
+ "alerts": [
94
+ {
95
+ "id": 12345,
96
+ "rule_name": "CPU使用率过高",
97
+ "severity": 1,
98
+ "severity_label": "严重",
99
+ "trigger_time": "2026-02-12 14:30:00",
100
+ "status": "triggered",
101
+ "tags": {"host": "server-01"}
102
+ }
103
+ ]
104
+ }
105
+ ```
106
+
107
+ **使用场景**:
108
+ - "查一下最近的告警"
109
+ - "有严重级别的告警吗"
110
+ - "CPU相关的告警有哪些"
111
+
112
+ ### 2. n9e_query_historical_alerts
113
+
114
+ **功能**: 查询历史告警事件
115
+
116
+ **参数**:
117
+ - `severity` (optional, number): 严重级别
118
+ - `rule_name` (optional, string): 规则名称
119
+ - `stime` (optional, number): 开始时间戳(秒)
120
+ - `etime` (optional, number): 结束时间戳(秒)
121
+ - `limit` (optional, number): 返回数量限制 (默认: 50)
122
+
123
+ **API 端点**: `GET /api/n9e/alert-his-events/list`
124
+
125
+ **使用场景**:
126
+ - "昨天有哪些告警"
127
+ - "过去一周的告警趋势"
128
+ - "查看历史告警记录"
129
+
130
+ ### 3. n9e_query_alert_rules
131
+
132
+ **功能**: 查询告警规则配置
133
+
134
+ **参数**:
135
+ - `rule_name` (optional, string): 规则名称搜索
136
+ - `datasource_ids` (optional, array): 数据源ID列表
137
+ - `disabled` (optional, number): 0=启用, 1=禁用
138
+
139
+ **API 端点**: `GET /api/n9e/alert-rules`
140
+
141
+ **使用场景**:
142
+ - "有哪些告警规则"
143
+ - "CPU相关的规则配置"
144
+ - "查看内存监控规则"
145
+
146
+ ### 4. n9e_query_alert_mutes
147
+
148
+ **功能**: 查询告警屏蔽配置
149
+
150
+ **参数**:
151
+ - `query` (optional, string): 搜索查询
152
+
153
+ **API 端点**: `GET /api/n9e/v1/n9e/alert-mutes`
154
+
155
+ **使用场景**:
156
+ - "哪些告警被屏蔽了"
157
+ - "查看告警屏蔽规则"
158
+ - "为什么没有收到告警"
159
+
160
+ ## API 客户端设计
161
+
162
+ ### N9eClient 类
163
+
164
+ ```typescript
165
+ class N9eClient {
166
+ constructor(private config: N9ePluginConfig) {}
167
+
168
+ private async request<T>(
169
+ method: string,
170
+ path: string,
171
+ params?: Record<string, any>
172
+ ): Promise<T> {
173
+ // 构建 URL 和查询参数
174
+ // 添加 X-API-Key header
175
+ // 处理超时和错误
176
+ // 返回解析后的 JSON
177
+ }
178
+
179
+ // 活跃告警
180
+ async getActiveAlerts(params: ActiveAlertQuery): Promise<AlertCurEvent[]>
181
+ async getActiveAlertById(eid: number): Promise<AlertCurEvent>
182
+
183
+ // 历史告警
184
+ async getHistoricalAlerts(params: HistoricalAlertQuery): Promise<AlertHisEvent[]>
185
+ async getHistoricalAlertById(eid: number): Promise<AlertHisEvent>
186
+
187
+ // 告警规则
188
+ async getAlertRules(params: AlertRuleQuery): Promise<AlertRule[]>
189
+ async getAlertRuleById(arid: number): Promise<AlertRule>
190
+
191
+ // 告警屏蔽
192
+ async getAlertMutes(params: AlertMuteQuery): Promise<AlertMute[]>
193
+ }
194
+ ```
195
+
196
+ ### 错误处理策略
197
+
198
+ | 错误类型 | 用户提示 |
199
+ |---------|---------|
200
+ | 网络错误 | "无法连接到夜莺平台,请检查网络或配置" |
201
+ | 401/403 | "认证失败,请检查API Key配置" |
202
+ | 404 | "未找到指定的资源" |
203
+ | 500 | "夜莺服务器错误,请稍后重试" |
204
+ | 超时 | "请求超时,请检查网络连接" |
205
+
206
+ ## Skills 设计
207
+
208
+ ### Skill 1: query-active-alerts
209
+
210
+ ```markdown
211
+ ---
212
+ name: query-active-alerts
213
+ description: 查询夜莺平台当前活跃的告警事件
214
+ ---
215
+
216
+ # 查询活跃告警
217
+
218
+ ## 何时使用
219
+
220
+ - 用户询问"当前有哪些告警"、"最近的告警"、"帮我查一下告警"
221
+ - 用户询问特定严重级别的告警:"有没有严重告警"、"warning级别的告警"
222
+ - 用户想了解当前系统状态
223
+
224
+ ## 使用方法
225
+
226
+ 调用 `n9e_query_active_alerts` 工具,根据用户意图填充参数:
227
+ - severity: 1(严重), 2(警告), 3(提示)
228
+ - rule_name: 从用户消息中提取规则名称
229
+ - limit: 默认50条
230
+
231
+ ## 返回后如何回答
232
+
233
+ - 总结告警数量和严重程度分布
234
+ - 列出最重要的几条告警(ID、规则、触发时间)
235
+ - 提供进一步查询建议
236
+
237
+ ## 示例
238
+
239
+ 用户: "查一下最近的告警"
240
+ → 调用 `n9e_query_active_alerts({ limit: 10 })`
241
+ → 总结: "当前有5条活跃告警,其中2条严重、3条警告..."
242
+ ```
243
+
244
+ ### Skill 2: query-historical-alerts
245
+
246
+ 用于查询历史告警、分析趋势,支持时间范围过滤。
247
+
248
+ ### Skill 3: query-alert-rules
249
+
250
+ 用于查看配置的告警规则、搜索特定规则配置。
251
+
252
+ ### Skill 4: query-alert-mutes
253
+
254
+ 用于查看告警屏蔽配置,了解为什么某些告警被抑制。
255
+
256
+ ## 数据格式与响应
257
+
258
+ ### 响应格式化原则
259
+
260
+ 1. **结构化 JSON**: 便于 AI agent 解析和理解
261
+ 2. **包含摘要**: 总数、分布统计
262
+ 3. **时间戳转换**: Unix 时间戳 → "2026-02-12 14:30:00"
263
+ 4. **严重级别标签化**: 1 → "严重", 2 → "警告", 3 → "提示"
264
+
265
+ ### 大数据集处理
266
+
267
+ - 默认限制: 50 条
268
+ - 超出提示: "显示前50条,共有120条结果"
269
+ - 建议筛选: "可以通过严重级别或规则名称筛选"
270
+
271
+ ### 时间戳处理
272
+
273
+ - 支持相对时间: "last 1 hour", "yesterday"
274
+ - 转换为可读格式: "2026-02-12 14:30:00"
275
+ - 使用服务器时区
276
+
277
+ ## 验证与安全
278
+
279
+ ### 输入验证
280
+
281
+ - 严重级别: 仅允许 1-3
282
+ - 时间范围: stime < etime
283
+ - 限制参数: 1-1000
284
+ - 查询字符串: 防注入清理
285
+
286
+ ### 日志策略
287
+
288
+ - DEBUG: 所有 API 请求 (方法、路径、参数)
289
+ - INFO: 成功响应
290
+ - ERROR: 错误详情和上下文
291
+ - **禁止**: 记录敏感数据 (API Key)
292
+
293
+ ### 错误响应格式
294
+
295
+ ```typescript
296
+ {
297
+ content: [{
298
+ type: "text",
299
+ text: "❌ 查询失败: 认证错误,请检查API Key配置"
300
+ }],
301
+ isError: true
302
+ }
303
+ ```
304
+
305
+ ## 测试清单
306
+
307
+ - [ ] 有效 API 凭证测试
308
+ - [ ] 无效/过期 API Key 测试
309
+ - [ ] 网络超时场景测试
310
+ - [ ] 空结果集测试
311
+ - [ ] 大数据集测试
312
+ - [ ] 各种查询参数组合测试
313
+ - [ ] Skills 正确加载测试
314
+ - [ ] Agent 理解和调用工具测试
315
+
316
+ ## 部署流程
317
+
318
+ 1. 安装插件: `openclaw plugins install -l ./n9e-plugin`
319
+ 2. 配置文件中添加配置 (apiBaseUrl, apiKey)
320
+ 3. 重启 Gateway: `openclaw gateway restart`
321
+ 4. 验证加载: `openclaw plugins list`
322
+ 5. 测试自然语言查询
323
+
324
+ ## 开发检查清单
325
+
326
+ - [ ] 插件清单 (`openclaw.plugin.json`) 完整
327
+ - [ ] 每个工具都有配套 Skill
328
+ - [ ] 配置 Schema 使用 Zod 定义
329
+ - [ ] API Key 标记为 sensitive
330
+ - [ ] 工具的 description 清晰准确
331
+ - [ ] 错误处理完善
332
+ - [ ] 日志记录完整
333
+ - [ ] 所有功能测试通过
334
+
335
+ ## 示例用户交互
336
+
337
+ **用户**: "查一下最近的告警"
338
+ **Agent**: 调用 `n9e_query_active_alerts({ limit: 10 })`
339
+ **回复**: "当前有5条活跃告警,其中2条严重级别、3条警告级别。最近的告警是..."
340
+
341
+ **用户**: "有严重级别的告警吗"
342
+ **Agent**: 调用 `n9e_query_active_alerts({ severity: 1 })`
343
+ **回复**: "有2条严重告警: 1) CPU使用率过高 (server-01) ..."
344
+
345
+ **用户**: "昨天有哪些告警"
346
+ **Agent**: 调用 `n9e_query_historical_alerts({ stime: yesterday_ts, etime: today_ts })`
347
+ **回复**: "昨天共有15条告警事件,包括..."
348
+
349
+ **用户**: "CPU相关的告警规则"
350
+ **Agent**: 调用 `n9e_query_alert_rules({ rule_name: "CPU" })`
351
+ **回复**: "找到3条CPU相关的告警规则: 1) CPU使用率 > 80% ..."
352
+
353
+ **用户**: "哪些告警被屏蔽了"
354
+ **Agent**: 调用 `n9e_query_alert_mutes()`
355
+ **回复**: "当前有2条告警屏蔽规则: 1) 维护窗口期屏蔽 ..."
356
+
357
+ ## 技术栈
358
+
359
+ - **语言**: TypeScript (ESM)
360
+ - **运行时**: jiti (OpenClaw Gateway 进程内)
361
+ - **HTTP 客户端**: node-fetch 或 内置 fetch
362
+ - **验证**: Zod
363
+ - **API 文档**: Swagger JSON from n9e
364
+
365
+ ## 后续扩展可能性
366
+
367
+ - 支持告警事件操作 (确认、屏蔽、删除)
368
+ - 支持告警统计和聚合查询
369
+ - 支持订阅规则查询
370
+ - 添加告警趋势分析功能
371
+ - 集成告警通知功能
372
+
373
+ ## 参考资料
374
+
375
+ - OpenClaw 插件开发指南: `/Users/yaobinze/workarea/youdao/ts/clawdbot_popo_plugin/docs/plugin-development-guide.md`
376
+ - 夜莺 API 文档: `/Users/yaobinze/workarea/youdao/go/n9e/center/docs/swagger.json`
377
+ - OpenClaw 插件 SDK: `openclaw/plugin-sdk`