lightclawbot 1.0.3 → 1.0.4
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.
- package/README.md +241 -1
- package/dist/public/data/scripts/check-update.9c4e848e.sh +138 -0
- package/dist/public/data/scripts/check-update.sh +138 -0
- package/dist/public/data/scripts/manifest.json +21 -0
- package/dist/public/data/scripts/preflight.7c5a27cd.sh +86 -0
- package/dist/public/data/scripts/preflight.sh +86 -0
- package/dist/public/data/scripts/upgrade.bd220b70.sh +214 -0
- package/dist/public/data/scripts/upgrade.sh +214 -0
- package/dist/src/channel.d.ts.map +1 -1
- package/dist/src/channel.js +34 -17
- package/dist/src/channel.js.map +1 -1
- package/dist/src/config.d.ts +34 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +104 -6
- package/dist/src/config.js.map +1 -1
- package/dist/src/download-tool.d.ts.map +1 -1
- package/dist/src/download-tool.js +16 -16
- package/dist/src/download-tool.js.map +1 -1
- package/dist/src/file-storage.d.ts.map +1 -1
- package/dist/src/file-storage.js +3 -5
- package/dist/src/file-storage.js.map +1 -1
- package/dist/src/format-urls.d.ts +26 -0
- package/dist/src/format-urls.d.ts.map +1 -0
- package/dist/src/format-urls.js +53 -0
- package/dist/src/format-urls.js.map +1 -0
- package/dist/src/gateway.d.ts.map +1 -1
- package/dist/src/gateway.js +89 -39
- package/dist/src/gateway.js.map +1 -1
- package/dist/src/history/session-store.d.ts +4 -0
- package/dist/src/history/session-store.d.ts.map +1 -1
- package/dist/src/history/session-store.js +41 -0
- package/dist/src/history/session-store.js.map +1 -1
- package/dist/src/inbound.d.ts.map +1 -1
- package/dist/src/inbound.js +38 -15
- package/dist/src/inbound.js.map +1 -1
- package/dist/src/outbound.d.ts.map +1 -1
- package/dist/src/outbound.js +5 -3
- package/dist/src/outbound.js.map +1 -1
- package/dist/src/socket-handlers.d.ts.map +1 -1
- package/dist/src/socket-handlers.js +1 -0
- package/dist/src/socket-handlers.js.map +1 -1
- package/dist/src/types.d.ts +6 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/upload-tool.d.ts.map +1 -1
- package/dist/src/upload-tool.js +14 -19
- package/dist/src/upload-tool.js.map +1 -1
- package/package.json +6 -3
- package/skills/lightclaw-media/SKILL.md +330 -0
package/README.md
CHANGED
|
@@ -1 +1,241 @@
|
|
|
1
|
-
|
|
1
|
+
# LightClaw — OpenClaw Channel 插件
|
|
2
|
+
|
|
3
|
+
对接 OpenClaw 框架的 LightClaw Bot channel 插件,支持多 apiKey(多账户)模式。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 目录
|
|
8
|
+
|
|
9
|
+
- [配置](#配置)
|
|
10
|
+
- [多账户 apiKey 体系](#多账户-apikey-体系)
|
|
11
|
+
- [整体架构](#整体架构)
|
|
12
|
+
- [数据流全景](#数据流全景)
|
|
13
|
+
- [两级映射设计](#两级映射设计)
|
|
14
|
+
- [resolveEffectiveApiKey 统一入口](#resolveeffectiveapikey-统一入口)
|
|
15
|
+
- [为什么不能只用一级映射](#为什么不能只用一级映射)
|
|
16
|
+
- [关键模块说明](#关键模块说明)
|
|
17
|
+
- [调试指南](#调试指南)
|
|
18
|
+
- [已知约束与注意事项](#已知约束与注意事项)
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 配置
|
|
23
|
+
|
|
24
|
+
`~/.openclaw/openclaw.json` 中 `channels.lightclawbot` 段:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"channels": {
|
|
29
|
+
"lightclawbot": {
|
|
30
|
+
"apiKeys": ["key-1", "key-2", "key-3"],
|
|
31
|
+
"enabled": true,
|
|
32
|
+
"dmScope": "per-channel-peer"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
| 字段 | 说明 |
|
|
39
|
+
|------|------|
|
|
40
|
+
| `apiKeys` | apiKey 数组,每个 key 对应一个 uin(用户身份)。`apiKeys[0]` 为主 key,同时作为默认 fallback |
|
|
41
|
+
| `dmScope` | 会话隔离粒度,推荐 `"per-channel-peer"`(每用户独立 session) |
|
|
42
|
+
|
|
43
|
+
> **注意**:配置中只有 `apiKeys`(复数数组),没有 `apiKey`(单数字段)。运行时 `account.apiKey` 的值取自 `apiKeys[0]`。
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 多账户 apiKey 体系
|
|
48
|
+
|
|
49
|
+
### 整体架构
|
|
50
|
+
|
|
51
|
+
多 apiKey 模式下,一个 Bot 可以持有多个 apiKey,每个 apiKey 关联到不同的 uin(用户身份)。插件需要在处理消息和执行工具时,正确选取当前用户对应的 apiKey。
|
|
52
|
+
|
|
53
|
+
核心挑战:**消息处理(inbound)** 和 **工具执行(tool)** 拿到的上下文信息不同:
|
|
54
|
+
|
|
55
|
+
| 阶段 | 可用标识 | 不可用标识 |
|
|
56
|
+
|------|---------|-----------|
|
|
57
|
+
| inbound 消息到达 | `msg.senderId`(uin) | sessionKey(需路由解析后才知道) |
|
|
58
|
+
| tool 执行 | `ctx.sessionKey` | uin(框架不传递) |
|
|
59
|
+
|
|
60
|
+
因此需要 **两级映射** 来桥接两个阶段。
|
|
61
|
+
|
|
62
|
+
### 数据流全景
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
66
|
+
│ Gateway 启动(一次性) │
|
|
67
|
+
│ │
|
|
68
|
+
│ 1. 读取配置中的 apiKeys 数组 │
|
|
69
|
+
│ 2. 对每个 apiKey 调用 /user/current 获取其 uin │
|
|
70
|
+
│ 3. 构建 uin→apiKey 映射表(apiKeyMap) │
|
|
71
|
+
│ 4. setApiKeyMap(apiKeyMap, apiKeys[0]) │
|
|
72
|
+
│ ├─ globalApiKeyMap = { uin_A→key_1, uin_B→key_2, ... } │
|
|
73
|
+
│ └─ globalDefaultApiKey = apiKeys[0] (fallback 兜底) │
|
|
74
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
75
|
+
│
|
|
76
|
+
▼
|
|
77
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
78
|
+
│ Inbound 消息处理(每条消息) │
|
|
79
|
+
│ │
|
|
80
|
+
│ 1. 收到消息,msg.senderId = uin_A │
|
|
81
|
+
│ 2. resolveEffectiveApiKey({ senderId: uin_A }) │
|
|
82
|
+
│ → 命中 globalApiKeyMap → 返回 key_1 │
|
|
83
|
+
│ 3. resolveAgentRoute(...) → 得到 route.sessionKey │
|
|
84
|
+
│ 4. setSessionApiKey(route.sessionKey, key_1) │
|
|
85
|
+
│ └─ sessionKeyToApiKey = { "agent:main:lc:direct:uin_A"→key_1 } │
|
|
86
|
+
│ 5. 处理消息、下载/上传附件(均使用 key_1) │
|
|
87
|
+
│ 6. 分发给 AI 引擎 │
|
|
88
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
89
|
+
│
|
|
90
|
+
▼
|
|
91
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
92
|
+
│ Tool 执行(AI 调用工具时) │
|
|
93
|
+
│ │
|
|
94
|
+
│ 1. 框架传入 ctx.sessionKey = "agent:main:lc:direct:uin_A" │
|
|
95
|
+
│ 2. resolveEffectiveApiKey({ sessionKey }) │
|
|
96
|
+
│ → 命中 sessionKeyToApiKey → 返回 key_1 │
|
|
97
|
+
│ 3. 使用 key_1 上传/下载文件到 COS │
|
|
98
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 两级映射设计
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
config.ts 中维护的全局状态:
|
|
105
|
+
|
|
106
|
+
┌─────────────────────────────────────────────────────────┐
|
|
107
|
+
│ 第1级:globalApiKeyMap (uin → apiKey) │
|
|
108
|
+
│ 写入时机:gateway 启动时(setApiKeyMap,一次性) │
|
|
109
|
+
│ 读取时机:inbound 处理消息时 │
|
|
110
|
+
│ 数据规模:= apiKeys 数量(固定,不随消息增长) │
|
|
111
|
+
└─────────────────────────────────────────────────────────┘
|
|
112
|
+
|
|
113
|
+
┌─────────────────────────────────────────────────────────┐
|
|
114
|
+
│ 第2级:sessionKeyToApiKey (sessionKey → apiKey) │
|
|
115
|
+
│ 写入时机:每条消息 inbound 处理时(setSessionApiKey) │
|
|
116
|
+
│ 读取时机:tool 执行时 │
|
|
117
|
+
│ 数据规模:= 活跃用户数(动态增长,但不会很大) │
|
|
118
|
+
└─────────────────────────────────────────────────────────┘
|
|
119
|
+
|
|
120
|
+
┌─────────────────────────────────────────────────────────┐
|
|
121
|
+
│ 兜底:globalDefaultApiKey │
|
|
122
|
+
│ 值 = apiKeys[0],当以上两级都未命中时使用 │
|
|
123
|
+
└─────────────────────────────────────────────────────────┘
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### resolveEffectiveApiKey 统一入口
|
|
127
|
+
|
|
128
|
+
所有需要获取 apiKey 的地方(inbound / upload-tool / download-tool)统一调用此函数:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
resolveEffectiveApiKey(params: {
|
|
132
|
+
sessionKey?: string; // tool 执行时传入
|
|
133
|
+
senderId?: string; // inbound 处理时传入
|
|
134
|
+
}): string
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**查找优先级**:
|
|
138
|
+
|
|
139
|
+
| 优先级 | 数据源 | 场景 |
|
|
140
|
+
|:------:|--------|------|
|
|
141
|
+
| 1 | `sessionKeyToApiKey[sessionKey]` | tool 执行时的主路径 |
|
|
142
|
+
| 2 | `globalApiKeyMap[senderId]` | inbound 处理时的主路径 |
|
|
143
|
+
| 3 | `globalApiKeyMap[extractUinFromSessionKey(sessionKey)]` | 兜底:从 sessionKey 解析 uin |
|
|
144
|
+
| 4 | `globalDefaultApiKey` | 最终 fallback |
|
|
145
|
+
|
|
146
|
+
### 为什么不能只用一级映射
|
|
147
|
+
|
|
148
|
+
**方案:只保留 sessionKeyToApiKey,去掉 globalApiKeyMap?**
|
|
149
|
+
|
|
150
|
+
不可行,原因是 **时序依赖**:
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
用户首条消息到达 inbound:
|
|
154
|
+
│
|
|
155
|
+
├─ 此时需要 apiKey(用于下载附件等)
|
|
156
|
+
│ → 但 sessionKey 尚未算出(要先调 resolveAgentRoute)
|
|
157
|
+
│ → 即使 sessionKey 已知,sessionKeyToApiKey 中没有记录(首条消息,从未写入过)
|
|
158
|
+
│ → ❌ 无法获取 apiKey
|
|
159
|
+
│
|
|
160
|
+
└─ 而 uin(msg.senderId)立即可用
|
|
161
|
+
→ globalApiKeyMap 在 gateway 启动时就已构建好
|
|
162
|
+
→ ✅ 可直接查到 apiKey
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**`globalApiKeyMap` 是冷启动数据源(gateway 启动时预建),`sessionKeyToApiKey` 是运行时缓存(消息处理时按需写入)。** 两者解决不同阶段的问题,缺一不可。
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## 关键模块说明
|
|
170
|
+
|
|
171
|
+
| 文件 | 职责 | 与 apiKey 体系的关系 |
|
|
172
|
+
|------|------|---------------------|
|
|
173
|
+
| `src/config.ts` | 配置解析 + apiKey 映射管理 | 定义 `setApiKeyMap`、`setSessionApiKey`、`resolveEffectiveApiKey` |
|
|
174
|
+
| `src/gateway.ts` | Socket.IO 连接生命周期管理 | 启动时调用 `resolveApiKeyIdentities` + `setApiKeyMap` 构建第1级映射 |
|
|
175
|
+
| `src/inbound.ts` | 入站消息处理 | 调用 `resolveEffectiveApiKey({ senderId })` 获取 apiKey,再调用 `setSessionApiKey` 写入第2级映射 |
|
|
176
|
+
| `src/upload-tool.ts` | 文件上传工具 | 调用 `resolveEffectiveApiKey({ sessionKey })` 获取 apiKey,传给 COS 上传 |
|
|
177
|
+
| `src/download-tool.ts` | 文件下载/转发工具 | 同上 |
|
|
178
|
+
| `src/file-storage.ts` | COS 文件存储封装 | 接收 `{ apiKey }` 参数,执行实际的 HTTP 上传/下载 |
|
|
179
|
+
|
|
180
|
+
### gateway.ts 中的 resolveApiKeyIdentities
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
对每个 apiKey 调用 /user/current,一次遍历完成:
|
|
184
|
+
- 提取每个 key 对应的 uin,构建 uin→apiKey 映射
|
|
185
|
+
- 从第一个成功的 key 中提取 botClientId(所有 key 共享同一个 bot)
|
|
186
|
+
- 容错:第一个 key 必须成功(否则无 botId,直接抛异常),后续 key 失败可降级跳过
|
|
187
|
+
- 总计 N 次 HTTP 请求,无重复调用
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### inbound.ts 中的 mainSessionKey 安全约束
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
// ⚠️ 只写入 per-channel-peer 的 sessionKey,不写入 mainSessionKey
|
|
194
|
+
// mainSessionKey(= "agent:main:main")是全局共享的,所有用户消息都会覆盖它,
|
|
195
|
+
// 导致最后一个用户的 apiKey 覆盖前一个用户,产生并发安全问题。
|
|
196
|
+
if (route?.sessionKey) {
|
|
197
|
+
setSessionApiKey(route.sessionKey, effectiveApiKey);
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## 调试指南
|
|
204
|
+
|
|
205
|
+
### 查看 apiKey 选取日志
|
|
206
|
+
|
|
207
|
+
upload-tool 和 download-tool 中保留了 `log.warn` 级别的调试日志:
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
[lightclaw_upload_file] sessionKey="agent:main:lightclawbot:direct:12345", accountId="default"
|
|
211
|
+
[lightclaw_upload_file] resolved apiKey="key1abcd..."
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
这些日志在终端始终可见(warn 级别不会被框架过滤)。
|
|
215
|
+
|
|
216
|
+
### 框架日志级别说明
|
|
217
|
+
|
|
218
|
+
| 级别 | 终端可见性 | 适用场景 |
|
|
219
|
+
|------|-----------|---------|
|
|
220
|
+
| `log.debug` | 通常不可见 | 详细流程追踪 |
|
|
221
|
+
| `log.info` | 取决于配置 | 常规信息 |
|
|
222
|
+
| `log.warn` | **始终可见** | 调试关键路径、apiKey 选取 |
|
|
223
|
+
| `log.error` | **始终可见** | 错误 |
|
|
224
|
+
|
|
225
|
+
### apiKey 日志脱敏
|
|
226
|
+
|
|
227
|
+
所有日志中 apiKey 只打印前 8 位(如 `key1abcd...`),避免泄露完整密钥。
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## 已知约束与注意事项
|
|
232
|
+
|
|
233
|
+
1. **dmScope 必须为 `per-channel-peer`**:确保每个用户有独立的 sessionKey,避免 `sessionKeyToApiKey` 并发覆盖。如果使用 `main`(默认值),所有用户共享同一个 sessionKey,多 key 模式会出问题。
|
|
234
|
+
|
|
235
|
+
2. **sessionKeyToApiKey 只增不减**:当前实现中没有过期清理机制。在活跃用户数有限的场景下不是问题。如果未来用户量极大,需考虑 LRU 或 TTL 淘汰策略。
|
|
236
|
+
|
|
237
|
+
3. **gateway 重启会重建 globalApiKeyMap**:`setApiKeyMap` 在每次 `startGateway` 时调用,会覆盖之前的映射。但 `sessionKeyToApiKey` 是累积的,重启后会被清空(模块级变量)。
|
|
238
|
+
|
|
239
|
+
4. **单 key 模式兼容**:当 `apiKeys` 只有一个元素时,`globalApiKeyMap` 只有一条记录,`resolveEffectiveApiKey` 最终都会 fallback 到 `globalDefaultApiKey`,行为与之前一致。
|
|
240
|
+
|
|
241
|
+
5. **环境变量兜底**:如果配置文件中没有 `apiKeys`,会尝试从 `LIGHTCLAW_API_KEY` 环境变量读取。
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# ========== lightclawbot 插件升级检查脚本 ==========
|
|
4
|
+
# 用途:检查 lightclawbot 插件是否需要升级(只检查,不执行安装)
|
|
5
|
+
# 输出:最后一行固定为 RESULT:{...} JSON,供前端解析
|
|
6
|
+
#
|
|
7
|
+
# 状态说明:
|
|
8
|
+
# - 已是最新: RESULT:{"status":"up_to_date","local":"x.y.z","remote":"x.y.z"}
|
|
9
|
+
# - 需要升级: RESULT:{"status":"update_available","local":"x.y.z","remote":"x.y.z"}
|
|
10
|
+
# - 未安装: RESULT:{"status":"not_installed","remote":"x.y.z"}
|
|
11
|
+
# - 无法检查: RESULT:{"status":"error","error":"...","detail":"..."}
|
|
12
|
+
#
|
|
13
|
+
# 使用方式:
|
|
14
|
+
# 本地执行: bash scripts/check-update.sh
|
|
15
|
+
# CDN 执行: bash <(curl -fsSL https://your-cdn.com/check-update.sh)
|
|
16
|
+
#
|
|
17
|
+
# 环境变量:
|
|
18
|
+
# NPM_REGISTRY — npm 源地址(默认腾讯镜像)
|
|
19
|
+
# TIMEOUT — npm view 超时秒数(默认 10)
|
|
20
|
+
|
|
21
|
+
set -euo pipefail
|
|
22
|
+
|
|
23
|
+
# ========== 配置 ==========
|
|
24
|
+
|
|
25
|
+
NPM_REGISTRY="${NPM_REGISTRY:-https://mirrors.tencent.com/npm/}"
|
|
26
|
+
TIMEOUT="${TIMEOUT:-10}"
|
|
27
|
+
|
|
28
|
+
id="lightclawbot"
|
|
29
|
+
spec="lightclawbot"
|
|
30
|
+
plugin_dir="$HOME/.openclaw/extensions/${id}"
|
|
31
|
+
|
|
32
|
+
# ========== 环境加载 ==========
|
|
33
|
+
|
|
34
|
+
load_env() {
|
|
35
|
+
export NVM_DIR="$HOME/.nvm"
|
|
36
|
+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
|
37
|
+
|
|
38
|
+
if [ -z "${PNPM_HOME:-}" ] && [ -d "$HOME/.local/share/pnpm" ]; then
|
|
39
|
+
export PNPM_HOME="$HOME/.local/share/pnpm"
|
|
40
|
+
export PATH="$PNPM_HOME:$PATH"
|
|
41
|
+
fi
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
load_env
|
|
45
|
+
|
|
46
|
+
# ========== 工具函数 ==========
|
|
47
|
+
|
|
48
|
+
# semver 比较:left < right 返回 0,否则返回 1,解析失败返回 2
|
|
49
|
+
version_lt() {
|
|
50
|
+
local left="$1"
|
|
51
|
+
local right="$2"
|
|
52
|
+
VERSION_LEFT="$left" VERSION_RIGHT="$right" node - <<'NODE'
|
|
53
|
+
function parseSemver(value) {
|
|
54
|
+
const match = String(value || "").trim().match(/^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/);
|
|
55
|
+
if (!match) return null;
|
|
56
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
57
|
+
}
|
|
58
|
+
const left = parseSemver(process.env.VERSION_LEFT);
|
|
59
|
+
const right = parseSemver(process.env.VERSION_RIGHT);
|
|
60
|
+
if (!left || !right) process.exit(2);
|
|
61
|
+
for (let i = 0; i < 3; i++) {
|
|
62
|
+
if (left[i] < right[i]) process.exit(0);
|
|
63
|
+
if (left[i] > right[i]) process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
process.exit(1);
|
|
66
|
+
NODE
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# ========== 1. 检查前置依赖 ==========
|
|
70
|
+
|
|
71
|
+
if ! command -v npm &>/dev/null; then
|
|
72
|
+
echo "RESULT:{\"status\":\"error\",\"error\":\"missing_dependency\",\"detail\":\"npm not found\"}"
|
|
73
|
+
exit 1
|
|
74
|
+
fi
|
|
75
|
+
|
|
76
|
+
# ========== 2. 获取本地版本 ==========
|
|
77
|
+
|
|
78
|
+
local_version=""
|
|
79
|
+
if [ -d "$plugin_dir" ] && [ -f "$plugin_dir/package.json" ]; then
|
|
80
|
+
if command -v jq &>/dev/null; then
|
|
81
|
+
local_version=$(jq -r '.version // ""' "$plugin_dir/package.json" 2>/dev/null || true)
|
|
82
|
+
elif command -v node &>/dev/null; then
|
|
83
|
+
local_version=$(node -e "
|
|
84
|
+
try {
|
|
85
|
+
const p = require('$plugin_dir/package.json');
|
|
86
|
+
process.stdout.write(p.version || '');
|
|
87
|
+
} catch {}
|
|
88
|
+
" 2>/dev/null || true)
|
|
89
|
+
fi
|
|
90
|
+
fi
|
|
91
|
+
|
|
92
|
+
# ========== 3. 查询远程最新版本 ==========
|
|
93
|
+
|
|
94
|
+
remote_version=""
|
|
95
|
+
|
|
96
|
+
# 尝试腾讯镜像源
|
|
97
|
+
remote_version=$(timeout "$TIMEOUT" npm view "$spec" version --registry "$NPM_REGISTRY" 2>/dev/null || true)
|
|
98
|
+
|
|
99
|
+
# 回退到官方源
|
|
100
|
+
if [ -z "$remote_version" ]; then
|
|
101
|
+
remote_version=$(timeout "$TIMEOUT" npm view "$spec" version --registry https://registry.npmjs.org 2>/dev/null || true)
|
|
102
|
+
fi
|
|
103
|
+
|
|
104
|
+
if [ -z "$remote_version" ]; then
|
|
105
|
+
echo "RESULT:{\"status\":\"error\",\"error\":\"fetch_failed\",\"detail\":\"could not fetch remote version from any registry\"}"
|
|
106
|
+
exit 1
|
|
107
|
+
fi
|
|
108
|
+
|
|
109
|
+
# ========== 4. 比较版本 ==========
|
|
110
|
+
|
|
111
|
+
# 未安装
|
|
112
|
+
if [ -z "$local_version" ]; then
|
|
113
|
+
echo "RESULT:{\"status\":\"not_installed\",\"remote\":\"${remote_version}\"}"
|
|
114
|
+
exit 0
|
|
115
|
+
fi
|
|
116
|
+
|
|
117
|
+
# 版本相同
|
|
118
|
+
if [ "$local_version" = "$remote_version" ]; then
|
|
119
|
+
echo "RESULT:{\"status\":\"up_to_date\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
120
|
+
exit 0
|
|
121
|
+
fi
|
|
122
|
+
|
|
123
|
+
# semver 比较(需要 node)
|
|
124
|
+
if command -v node &>/dev/null; then
|
|
125
|
+
if version_lt "$local_version" "$remote_version"; then
|
|
126
|
+
# local < remote → 需要升级
|
|
127
|
+
echo "RESULT:{\"status\":\"update_available\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
128
|
+
exit 0
|
|
129
|
+
else
|
|
130
|
+
# local >= remote(可能是开发版本或降级场景)
|
|
131
|
+
echo "RESULT:{\"status\":\"up_to_date\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
132
|
+
exit 0
|
|
133
|
+
fi
|
|
134
|
+
fi
|
|
135
|
+
|
|
136
|
+
# 没有 node,只能做字符串不等判断
|
|
137
|
+
echo "RESULT:{\"status\":\"update_available\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
138
|
+
exit 0
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# ========== lightclawbot 插件升级检查脚本 ==========
|
|
4
|
+
# 用途:检查 lightclawbot 插件是否需要升级(只检查,不执行安装)
|
|
5
|
+
# 输出:最后一行固定为 RESULT:{...} JSON,供前端解析
|
|
6
|
+
#
|
|
7
|
+
# 状态说明:
|
|
8
|
+
# - 已是最新: RESULT:{"status":"up_to_date","local":"x.y.z","remote":"x.y.z"}
|
|
9
|
+
# - 需要升级: RESULT:{"status":"update_available","local":"x.y.z","remote":"x.y.z"}
|
|
10
|
+
# - 未安装: RESULT:{"status":"not_installed","remote":"x.y.z"}
|
|
11
|
+
# - 无法检查: RESULT:{"status":"error","error":"...","detail":"..."}
|
|
12
|
+
#
|
|
13
|
+
# 使用方式:
|
|
14
|
+
# 本地执行: bash scripts/check-update.sh
|
|
15
|
+
# CDN 执行: bash <(curl -fsSL https://your-cdn.com/check-update.sh)
|
|
16
|
+
#
|
|
17
|
+
# 环境变量:
|
|
18
|
+
# NPM_REGISTRY — npm 源地址(默认腾讯镜像)
|
|
19
|
+
# TIMEOUT — npm view 超时秒数(默认 10)
|
|
20
|
+
|
|
21
|
+
set -euo pipefail
|
|
22
|
+
|
|
23
|
+
# ========== 配置 ==========
|
|
24
|
+
|
|
25
|
+
NPM_REGISTRY="${NPM_REGISTRY:-https://mirrors.tencent.com/npm/}"
|
|
26
|
+
TIMEOUT="${TIMEOUT:-10}"
|
|
27
|
+
|
|
28
|
+
id="lightclawbot"
|
|
29
|
+
spec="lightclawbot"
|
|
30
|
+
plugin_dir="$HOME/.openclaw/extensions/${id}"
|
|
31
|
+
|
|
32
|
+
# ========== 环境加载 ==========
|
|
33
|
+
|
|
34
|
+
load_env() {
|
|
35
|
+
export NVM_DIR="$HOME/.nvm"
|
|
36
|
+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
|
37
|
+
|
|
38
|
+
if [ -z "${PNPM_HOME:-}" ] && [ -d "$HOME/.local/share/pnpm" ]; then
|
|
39
|
+
export PNPM_HOME="$HOME/.local/share/pnpm"
|
|
40
|
+
export PATH="$PNPM_HOME:$PATH"
|
|
41
|
+
fi
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
load_env
|
|
45
|
+
|
|
46
|
+
# ========== 工具函数 ==========
|
|
47
|
+
|
|
48
|
+
# semver 比较:left < right 返回 0,否则返回 1,解析失败返回 2
|
|
49
|
+
version_lt() {
|
|
50
|
+
local left="$1"
|
|
51
|
+
local right="$2"
|
|
52
|
+
VERSION_LEFT="$left" VERSION_RIGHT="$right" node - <<'NODE'
|
|
53
|
+
function parseSemver(value) {
|
|
54
|
+
const match = String(value || "").trim().match(/^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/);
|
|
55
|
+
if (!match) return null;
|
|
56
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
57
|
+
}
|
|
58
|
+
const left = parseSemver(process.env.VERSION_LEFT);
|
|
59
|
+
const right = parseSemver(process.env.VERSION_RIGHT);
|
|
60
|
+
if (!left || !right) process.exit(2);
|
|
61
|
+
for (let i = 0; i < 3; i++) {
|
|
62
|
+
if (left[i] < right[i]) process.exit(0);
|
|
63
|
+
if (left[i] > right[i]) process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
process.exit(1);
|
|
66
|
+
NODE
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# ========== 1. 检查前置依赖 ==========
|
|
70
|
+
|
|
71
|
+
if ! command -v npm &>/dev/null; then
|
|
72
|
+
echo "RESULT:{\"status\":\"error\",\"error\":\"missing_dependency\",\"detail\":\"npm not found\"}"
|
|
73
|
+
exit 1
|
|
74
|
+
fi
|
|
75
|
+
|
|
76
|
+
# ========== 2. 获取本地版本 ==========
|
|
77
|
+
|
|
78
|
+
local_version=""
|
|
79
|
+
if [ -d "$plugin_dir" ] && [ -f "$plugin_dir/package.json" ]; then
|
|
80
|
+
if command -v jq &>/dev/null; then
|
|
81
|
+
local_version=$(jq -r '.version // ""' "$plugin_dir/package.json" 2>/dev/null || true)
|
|
82
|
+
elif command -v node &>/dev/null; then
|
|
83
|
+
local_version=$(node -e "
|
|
84
|
+
try {
|
|
85
|
+
const p = require('$plugin_dir/package.json');
|
|
86
|
+
process.stdout.write(p.version || '');
|
|
87
|
+
} catch {}
|
|
88
|
+
" 2>/dev/null || true)
|
|
89
|
+
fi
|
|
90
|
+
fi
|
|
91
|
+
|
|
92
|
+
# ========== 3. 查询远程最新版本 ==========
|
|
93
|
+
|
|
94
|
+
remote_version=""
|
|
95
|
+
|
|
96
|
+
# 尝试腾讯镜像源
|
|
97
|
+
remote_version=$(timeout "$TIMEOUT" npm view "$spec" version --registry "$NPM_REGISTRY" 2>/dev/null || true)
|
|
98
|
+
|
|
99
|
+
# 回退到官方源
|
|
100
|
+
if [ -z "$remote_version" ]; then
|
|
101
|
+
remote_version=$(timeout "$TIMEOUT" npm view "$spec" version --registry https://registry.npmjs.org 2>/dev/null || true)
|
|
102
|
+
fi
|
|
103
|
+
|
|
104
|
+
if [ -z "$remote_version" ]; then
|
|
105
|
+
echo "RESULT:{\"status\":\"error\",\"error\":\"fetch_failed\",\"detail\":\"could not fetch remote version from any registry\"}"
|
|
106
|
+
exit 1
|
|
107
|
+
fi
|
|
108
|
+
|
|
109
|
+
# ========== 4. 比较版本 ==========
|
|
110
|
+
|
|
111
|
+
# 未安装
|
|
112
|
+
if [ -z "$local_version" ]; then
|
|
113
|
+
echo "RESULT:{\"status\":\"not_installed\",\"remote\":\"${remote_version}\"}"
|
|
114
|
+
exit 0
|
|
115
|
+
fi
|
|
116
|
+
|
|
117
|
+
# 版本相同
|
|
118
|
+
if [ "$local_version" = "$remote_version" ]; then
|
|
119
|
+
echo "RESULT:{\"status\":\"up_to_date\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
120
|
+
exit 0
|
|
121
|
+
fi
|
|
122
|
+
|
|
123
|
+
# semver 比较(需要 node)
|
|
124
|
+
if command -v node &>/dev/null; then
|
|
125
|
+
if version_lt "$local_version" "$remote_version"; then
|
|
126
|
+
# local < remote → 需要升级
|
|
127
|
+
echo "RESULT:{\"status\":\"update_available\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
128
|
+
exit 0
|
|
129
|
+
else
|
|
130
|
+
# local >= remote(可能是开发版本或降级场景)
|
|
131
|
+
echo "RESULT:{\"status\":\"up_to_date\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
132
|
+
exit 0
|
|
133
|
+
fi
|
|
134
|
+
fi
|
|
135
|
+
|
|
136
|
+
# 没有 node,只能做字符串不等判断
|
|
137
|
+
echo "RESULT:{\"status\":\"update_available\",\"local\":\"${local_version}\",\"remote\":\"${remote_version}\"}"
|
|
138
|
+
exit 0
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"generatedAt": "2026-03-16T03:41:55.361Z",
|
|
3
|
+
"cdnBase": "https://cloudcache.tencent-cloud.com/qcloud/tea/app/data/scripts",
|
|
4
|
+
"files": {
|
|
5
|
+
"preflight.sh": {
|
|
6
|
+
"hashed": "preflight.7c5a27cd.sh",
|
|
7
|
+
"hash": "7c5a27cd",
|
|
8
|
+
"size": 3105
|
|
9
|
+
},
|
|
10
|
+
"check-update.sh": {
|
|
11
|
+
"hashed": "check-update.9c4e848e.sh",
|
|
12
|
+
"hash": "9c4e848e",
|
|
13
|
+
"size": 4422
|
|
14
|
+
},
|
|
15
|
+
"upgrade.sh": {
|
|
16
|
+
"hashed": "upgrade.bd220b70.sh",
|
|
17
|
+
"hash": "bd220b70",
|
|
18
|
+
"size": 8398
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# ========== lightclawbot 插件前置检查脚本 ==========
|
|
4
|
+
# 用途:快速判断 lightclawbot 插件是否可用
|
|
5
|
+
# 输出:最后一行固定为 RESULT:{...} JSON,供前端解析
|
|
6
|
+
# - 可用: RESULT:{"status":"ok","version":"x.y.z"}
|
|
7
|
+
# - 缺 openclaw: RESULT:{"status":"need_openclaw","reason":"..."}
|
|
8
|
+
# - 缺 lightclawbot:RESULT:{"status":"need_install","reason":"..."}
|
|
9
|
+
#
|
|
10
|
+
# 使用方式:
|
|
11
|
+
# 本地执行: bash scripts/preflight.sh
|
|
12
|
+
# CDN 执行: bash <(curl -fsSL https://your-cdn.com/preflight.sh)
|
|
13
|
+
|
|
14
|
+
id="lightclawbot"
|
|
15
|
+
cfg="$HOME/.openclaw/openclaw.json"
|
|
16
|
+
plugin_dir="$HOME/.openclaw/extensions/${id}"
|
|
17
|
+
|
|
18
|
+
# ---------- 1. 检查 openclaw 是否可用 ----------
|
|
19
|
+
|
|
20
|
+
# 加载 nvm 环境(openclaw 可能依赖 nvm 管理的 node)
|
|
21
|
+
export NVM_DIR="$HOME/.nvm"
|
|
22
|
+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
|
23
|
+
|
|
24
|
+
if ! command -v openclaw &>/dev/null; then
|
|
25
|
+
echo "RESULT:{\"status\":\"need_openclaw\",\"reason\":\"openclaw_not_found\"}"
|
|
26
|
+
exit 1
|
|
27
|
+
fi
|
|
28
|
+
|
|
29
|
+
# 验证 openclaw 可执行
|
|
30
|
+
# 已知输出格式:
|
|
31
|
+
# - OpenClaw 2026.3.8 (3caab92) (新版本,带前缀 + commit hash)
|
|
32
|
+
# - OpenClaw 2026.3.2 (带前缀,无 commit hash)
|
|
33
|
+
# - 2026.3.2 (旧版本,直接输出版本号,无前缀)
|
|
34
|
+
openclaw_output=$(openclaw -v 2>/dev/null || true)
|
|
35
|
+
if ! echo "$openclaw_output" | grep -qiE '(^OpenClaw[/ ])?[0-9]+\.[0-9]+'; then
|
|
36
|
+
echo "RESULT:{\"status\":\"need_openclaw\",\"reason\":\"openclaw_not_working\"}"
|
|
37
|
+
exit 1
|
|
38
|
+
fi
|
|
39
|
+
|
|
40
|
+
# ---------- 2. 检查配置文件是否存在 ----------
|
|
41
|
+
|
|
42
|
+
if [ ! -f "$cfg" ]; then
|
|
43
|
+
echo "RESULT:{\"status\":\"need_install\",\"reason\":\"config_not_found\"}"
|
|
44
|
+
exit 1
|
|
45
|
+
fi
|
|
46
|
+
|
|
47
|
+
# ---------- 3. 检查插件是否已安装(目录 + package.json) ----------
|
|
48
|
+
|
|
49
|
+
if [ ! -d "$plugin_dir" ] || [ ! -f "$plugin_dir/package.json" ]; then
|
|
50
|
+
echo "RESULT:{\"status\":\"need_install\",\"reason\":\"plugin_not_installed\"}"
|
|
51
|
+
exit 1
|
|
52
|
+
fi
|
|
53
|
+
|
|
54
|
+
# ---------- 4. 检查配置文件中插件注册是否完整 ----------
|
|
55
|
+
|
|
56
|
+
# 需要 jq 来解析 JSON
|
|
57
|
+
if ! command -v jq &>/dev/null; then
|
|
58
|
+
echo "RESULT:{\"status\":\"need_install\",\"reason\":\"jq_not_found\"}"
|
|
59
|
+
exit 1
|
|
60
|
+
fi
|
|
61
|
+
|
|
62
|
+
# 4a. plugins.entries 中是否存在且 enabled
|
|
63
|
+
plugin_enabled=$(jq -r ".plugins.entries.\"${id}\".enabled // false" "$cfg" 2>/dev/null)
|
|
64
|
+
if [ "$plugin_enabled" != "true" ]; then
|
|
65
|
+
echo "RESULT:{\"status\":\"need_install\",\"reason\":\"plugin_not_enabled\"}"
|
|
66
|
+
exit 1
|
|
67
|
+
fi
|
|
68
|
+
|
|
69
|
+
# 4b. plugins.installs 中是否有安装记录
|
|
70
|
+
if ! jq -e ".plugins.installs.\"${id}\"" "$cfg" > /dev/null 2>&1; then
|
|
71
|
+
echo "RESULT:{\"status\":\"need_install\",\"reason\":\"plugin_install_record_missing\"}"
|
|
72
|
+
exit 1
|
|
73
|
+
fi
|
|
74
|
+
|
|
75
|
+
# 4c. channels 中是否配置且 enabled
|
|
76
|
+
channel_enabled=$(jq -r ".channels.\"${id}\".enabled // false" "$cfg" 2>/dev/null)
|
|
77
|
+
if [ "$channel_enabled" != "true" ]; then
|
|
78
|
+
echo "RESULT:{\"status\":\"need_install\",\"reason\":\"channel_not_enabled\"}"
|
|
79
|
+
exit 1
|
|
80
|
+
fi
|
|
81
|
+
|
|
82
|
+
# ---------- 5. 全部通过,插件可用 ----------
|
|
83
|
+
|
|
84
|
+
version=$(jq -r '.version // "unknown"' "$plugin_dir/package.json")
|
|
85
|
+
echo "RESULT:{\"status\":\"ok\",\"version\":\"${version}\"}"
|
|
86
|
+
exit 0
|