lightclawbot 1.2.0-beta.3 → 1.2.5
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/dist/src/channel.js +160 -39
- package/dist/src/config.js +4 -0
- package/dist/src/gateway.js +17 -5
- package/dist/src/history/index.js +1 -1
- package/dist/src/history/session-reader.js +57 -2
- package/dist/src/history/session-store.js +43 -0
- package/dist/src/history/text-processing.js +7 -0
- package/dist/src/inbound.js +34 -20
- package/dist/src/socket/chat.js +257 -0
- package/dist/src/socket/handlers.js +271 -29
- package/dist/src/streaming/stream-reply-sink.js +3 -3
- package/dist/src/utils/common.js +153 -1
- package/node_modules/ws/lib/sender.js +6 -1
- package/node_modules/ws/package.json +1 -1
- package/openclaw.plugin.json +77 -3
- package/package.json +1 -1
package/dist/src/utils/common.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
1
3
|
import { X_PRODUCT } from '../config.js';
|
|
4
|
+
import { resolveOpenClawHome } from '../history/session-store.js';
|
|
2
5
|
/**
|
|
3
6
|
* 将长文本按指定长度拆分为多个文本块。
|
|
4
7
|
*
|
|
@@ -53,7 +56,7 @@ export function buildAuthHeaders(apiKey) {
|
|
|
53
56
|
* - extra 用于透传 kind 相关字段(如 tool_start 的 toolName/toolPhase、tool_end 的 toolIsError)。
|
|
54
57
|
*/
|
|
55
58
|
export function emitSignal(ctx, kind, content = '', extra) {
|
|
56
|
-
const { emitter, targetId, replyMsgId, originalMsgId, agentId } = ctx;
|
|
59
|
+
const { emitter, targetId, replyMsgId, originalMsgId, agentId, chatId } = ctx;
|
|
57
60
|
return emitter.emit({
|
|
58
61
|
msgId: replyMsgId,
|
|
59
62
|
from: emitter.botClientId,
|
|
@@ -64,5 +67,154 @@ export function emitSignal(ctx, kind, content = '', extra) {
|
|
|
64
67
|
...(kind !== 'typing_start' ? { replyToMsgId: originalMsgId } : {}),
|
|
65
68
|
...extra,
|
|
66
69
|
agentId,
|
|
70
|
+
extra: { chatId: chatId ?? '' },
|
|
67
71
|
});
|
|
68
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 构建 chats.json 的绝对路径:
|
|
75
|
+
* `~/.openclaw/agents/<agentId>/chats/<userId>/chats.json`
|
|
76
|
+
*/
|
|
77
|
+
export function resolveChatsFilePath(agentId, userId) {
|
|
78
|
+
const home = resolveOpenClawHome();
|
|
79
|
+
return path.join(home, 'agents', agentId, 'chats', userId, 'chats.json');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 读取 chats.json 并返回 chats 列表(**纯只读**,无任何文件落盘副作用)。
|
|
83
|
+
*
|
|
84
|
+
* - 文件不存在 → 返回空数组
|
|
85
|
+
* - JSON 解析失败或结构不合法 → 返回空数组
|
|
86
|
+
* - 合法记录按 `updatedAt` 倒序排序,未提供则按 `createdAt` 兜底
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
89
|
+
export function readChatsFile(filePath) {
|
|
90
|
+
if (!fs.existsSync(filePath))
|
|
91
|
+
return [];
|
|
92
|
+
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
93
|
+
if (!raw.trim())
|
|
94
|
+
return [];
|
|
95
|
+
const parsed = JSON.parse(raw);
|
|
96
|
+
if (!parsed || !Array.isArray(parsed.chats))
|
|
97
|
+
return [];
|
|
98
|
+
// 过滤出字段合法的记录
|
|
99
|
+
const valid = parsed.chats.filter((c) => !!c &&
|
|
100
|
+
typeof c.chatId === 'string' &&
|
|
101
|
+
typeof c.title === 'string' &&
|
|
102
|
+
typeof c.createdAt === 'number' &&
|
|
103
|
+
typeof c.updatedAt === 'number');
|
|
104
|
+
// 按 updatedAt 倒序,便于前端直接渲染
|
|
105
|
+
return valid.sort((a, b) => (b.updatedAt ?? b.createdAt) - (a.updatedAt ?? a.createdAt));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 读取 chats.json;若文件不存在则**兜底创建一条 chatId='' 的默认对话**并落盘。
|
|
109
|
+
*
|
|
110
|
+
* 与 {@link readChatsFile} 的差异:本函数有写盘副作用,专供"必须保证存在默认对话
|
|
111
|
+
* 兜底条目"的调用方(典型场景:sessionId 登记入口 ensureSessionInHistory)。
|
|
112
|
+
*
|
|
113
|
+
* 拆分原因:原 readChatsFile 兼任只读与兜底创建两种语义,导致 history:request 等
|
|
114
|
+
* 纯查询路径在用户尚未发任何消息前就把 chats.json 写到磁盘上,违反单一职责。
|
|
115
|
+
*
|
|
116
|
+
* 行为:
|
|
117
|
+
* - 文件不存在 → 落盘 [{ chatId: '', title: '默认会话', ... }] 并返回;
|
|
118
|
+
* - 文件存在但解析失败 / 结构不合法 → 沿用 readChatsFile 的容错(返回 [],不创建);
|
|
119
|
+
* - 文件存在但内容合法 → 等价于 readChatsFile。
|
|
120
|
+
*/
|
|
121
|
+
export function readChatsFileOrInitDefault(filePath) {
|
|
122
|
+
if (!fs.existsSync(filePath)) {
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
const defaultChat = {
|
|
125
|
+
chatId: '',
|
|
126
|
+
title: '默认会话',
|
|
127
|
+
createdAt: now,
|
|
128
|
+
updatedAt: now,
|
|
129
|
+
titleLocked: false,
|
|
130
|
+
pinned: false,
|
|
131
|
+
sessionIdHistory: [],
|
|
132
|
+
};
|
|
133
|
+
writeChatsFile(filePath, [defaultChat]);
|
|
134
|
+
return [defaultChat];
|
|
135
|
+
}
|
|
136
|
+
return readChatsFile(filePath);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* 将 chats 列表原子写入 `chats.json`。
|
|
140
|
+
*
|
|
141
|
+
* 写入策略:
|
|
142
|
+
* 1. 目录不存在时逐级创建(recursive: true)
|
|
143
|
+
* 2. 先写入同目录下的 `.tmp` 文件,再 rename 覆盖目标,
|
|
144
|
+
* 避免写入过程中进程崩溃导致 json 文件损坏
|
|
145
|
+
* 3. schema 固定 `version: 1`,为未来平滑升级预留字段
|
|
146
|
+
*
|
|
147
|
+
* @param filePath 目标 chats.json 绝对路径
|
|
148
|
+
* @param chats 完整的 ChatMeta 列表(调用方自行保证顺序与合法性)
|
|
149
|
+
*/
|
|
150
|
+
export function writeChatsFile(filePath, chats) {
|
|
151
|
+
const dir = path.dirname(filePath);
|
|
152
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
153
|
+
const payload = { version: 1, chats };
|
|
154
|
+
const tmpPath = `${filePath}.tmp`;
|
|
155
|
+
fs.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), 'utf-8');
|
|
156
|
+
fs.renameSync(tmpPath, filePath);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 把当前生效的 sessionId 幂等地登记到 `chats.json` 中对应 chatId 的 `sessionIdHistory` 数组。
|
|
160
|
+
*
|
|
161
|
+
* 语义说明:
|
|
162
|
+
* - sessionIdHistory 记录该 chat 见过的所有 sessionId(含当前在用),按时间序末尾追加;
|
|
163
|
+
* - 同一 sessionId 若已存在则跳过(幂等,不会重复 push);
|
|
164
|
+
* - 列表最后一项 ≈ 该 chat 当前在用的 sessionId(前提是调用方在每次消息进入时都调用本函数)。
|
|
165
|
+
*
|
|
166
|
+
* 关于 chatId 为空('')的处理:
|
|
167
|
+
* - 空 chatId 是**合法**的"默认对话"会话标识——`readChatsFile` 在 chats.json
|
|
168
|
+
* 不存在时会主动写入一条 `chatId: ''` 的兜底记录;
|
|
169
|
+
* - 因此本函数对空 chatId 同样登记,目标条目就是 `chatId === ''` 那一条;
|
|
170
|
+
* - 若 chats.json 文件不存在 → 通过 readChatsFile 自动创建兜底记录;
|
|
171
|
+
* - 若 chats.json 已存在但缺失目标 chatId 条目 → 跳过登记并告警(避免污染他人数据)。
|
|
172
|
+
*
|
|
173
|
+
* 容错策略:
|
|
174
|
+
* - chatId 为 undefined/null → 跳过;空字符串 '' 视为合法(默认会话);
|
|
175
|
+
* - sessionId 为空 → 跳过;
|
|
176
|
+
* - 任意 IO/解析异常 → 静默吞错 + 日志,绝不抛出影响主流程。
|
|
177
|
+
*
|
|
178
|
+
* @param agentId - Agent ID(chats/<agentId> 目录段)
|
|
179
|
+
* @param userId - 用户 ID(chats/<agentId>/chats/<userId> 目录段)
|
|
180
|
+
* @param chatId - 会话 ID(ChatMeta.chatId);undefined/null 时跳过;空字符串视为默认会话
|
|
181
|
+
* @param sessionId - 当前生效的 sessionId(来自 sessions.json 索引);为空时直接 return
|
|
182
|
+
* @returns 是否实际写盘(true=本次新增了一个 sessionId;false=跳过 / 已存在 / 出错)
|
|
183
|
+
*/
|
|
184
|
+
export function ensureSessionInHistory(agentId, userId, chatId, sessionId) {
|
|
185
|
+
// chatId 必须是 string(含空字符串 '' —— 默认会话的合法标识);undefined/null 跳过
|
|
186
|
+
if (typeof chatId !== 'string')
|
|
187
|
+
return false;
|
|
188
|
+
if (!sessionId || !sessionId.trim())
|
|
189
|
+
return false;
|
|
190
|
+
const chatsPath = resolveChatsFilePath(agentId, userId);
|
|
191
|
+
try {
|
|
192
|
+
// 登记入口需要兜底:默认对话首次消息时 chats.json 尚不存在,
|
|
193
|
+
// 走 readChatsFileOrInitDefault 让其落盘一条 chatId='' 的默认对话,
|
|
194
|
+
// 然后正常走"查找+追加"流程。
|
|
195
|
+
const chats = readChatsFileOrInitDefault(chatsPath);
|
|
196
|
+
const idx = chats.findIndex((c) => c.chatId === chatId);
|
|
197
|
+
if (idx < 0) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
const target = chats[idx];
|
|
201
|
+
const history = Array.isArray(target.sessionIdHistory) ? target.sessionIdHistory : [];
|
|
202
|
+
if (history.includes(sessionId)) {
|
|
203
|
+
// 已存在,幂等跳过,不写盘
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
// 末尾追加,写回。
|
|
207
|
+
// 把默认对话/具名对话无端"置顶",扰乱前端按 updatedAt 倒序的列表排序。
|
|
208
|
+
const updated = {
|
|
209
|
+
...target,
|
|
210
|
+
sessionIdHistory: [...history, sessionId],
|
|
211
|
+
};
|
|
212
|
+
const nextChats = [...chats];
|
|
213
|
+
nextChats[idx] = updated;
|
|
214
|
+
writeChatsFile(chatsPath, nextChats);
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
const { Duplex } = require('stream');
|
|
6
6
|
const { randomFillSync } = require('crypto');
|
|
7
|
+
const {
|
|
8
|
+
types: { isUint8Array }
|
|
9
|
+
} = require('util');
|
|
7
10
|
|
|
8
11
|
const PerMessageDeflate = require('./permessage-deflate');
|
|
9
12
|
const { EMPTY_BUFFER, kWebSocket, NOOP } = require('./constants');
|
|
@@ -200,8 +203,10 @@ class Sender {
|
|
|
200
203
|
|
|
201
204
|
if (typeof data === 'string') {
|
|
202
205
|
buf.write(data, 2);
|
|
203
|
-
} else {
|
|
206
|
+
} else if (isUint8Array(data)) {
|
|
204
207
|
buf.set(data, 2);
|
|
208
|
+
} else {
|
|
209
|
+
throw new TypeError('Second argument must be a string or a Uint8Array');
|
|
205
210
|
}
|
|
206
211
|
}
|
|
207
212
|
|
package/openclaw.plugin.json
CHANGED
|
@@ -1,10 +1,84 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "lightclawbot",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"name": "LightClawBot",
|
|
4
|
+
"description": "Connect OpenClaw to your LightClawBot via WebSocket long-connection",
|
|
5
|
+
"kind": "channel",
|
|
6
|
+
"channels": [
|
|
7
|
+
"lightclawbot"
|
|
8
|
+
],
|
|
9
|
+
"skills": [
|
|
10
|
+
"./skills"
|
|
11
|
+
],
|
|
5
12
|
"configSchema": {
|
|
6
13
|
"type": "object",
|
|
7
14
|
"additionalProperties": false,
|
|
8
15
|
"properties": {}
|
|
16
|
+
},
|
|
17
|
+
"channelConfigs": {
|
|
18
|
+
"lightclawbot": {
|
|
19
|
+
"label": "LightClawBot",
|
|
20
|
+
"description": "Connect OpenClaw to your LightClawBot via WebSocket long-connection",
|
|
21
|
+
"schema": {
|
|
22
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
23
|
+
"type": "object",
|
|
24
|
+
"additionalProperties": true,
|
|
25
|
+
"properties": {
|
|
26
|
+
"enabled": {
|
|
27
|
+
"type": "boolean",
|
|
28
|
+
"description": "Whether this channel is enabled."
|
|
29
|
+
},
|
|
30
|
+
"name": {
|
|
31
|
+
"type": "string",
|
|
32
|
+
"description": "Human-readable account/channel name."
|
|
33
|
+
},
|
|
34
|
+
"dmPolicy": {
|
|
35
|
+
"type": "string",
|
|
36
|
+
"enum": ["open", "allowlist", "disabled"],
|
|
37
|
+
"description": "Policy that controls who can DM the bot."
|
|
38
|
+
},
|
|
39
|
+
"allowFrom": {
|
|
40
|
+
"type": "array",
|
|
41
|
+
"items": { "type": "string" },
|
|
42
|
+
"description": "Allowlist of user IDs ('*' means everyone)."
|
|
43
|
+
},
|
|
44
|
+
"systemPrompt": {
|
|
45
|
+
"type": "string",
|
|
46
|
+
"description": "Custom system prompt applied to this account."
|
|
47
|
+
},
|
|
48
|
+
"defaultAccount": {
|
|
49
|
+
"type": "string",
|
|
50
|
+
"description": "Default account id to use when none is specified."
|
|
51
|
+
},
|
|
52
|
+
"accounts": {
|
|
53
|
+
"type": "object",
|
|
54
|
+
"description": "Per-account configuration keyed by account id (typically the uin).",
|
|
55
|
+
"additionalProperties": {
|
|
56
|
+
"type": "object",
|
|
57
|
+
"additionalProperties": false,
|
|
58
|
+
"properties": {
|
|
59
|
+
"enabled": { "type": "boolean" },
|
|
60
|
+
"name": { "type": "string" },
|
|
61
|
+
"apiKey": { "type": "string" },
|
|
62
|
+
"apiBaseUrl": { "type": "string" },
|
|
63
|
+
"dmPolicy": {
|
|
64
|
+
"type": "string",
|
|
65
|
+
"enum": ["open", "allowlist", "disabled"]
|
|
66
|
+
},
|
|
67
|
+
"allowFrom": {
|
|
68
|
+
"type": "array",
|
|
69
|
+
"items": { "type": "string" }
|
|
70
|
+
},
|
|
71
|
+
"systemPrompt": { "type": "string" }
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"contracts": {
|
|
80
|
+
"tools": [
|
|
81
|
+
"lightclaw_upload_file"
|
|
82
|
+
]
|
|
9
83
|
}
|
|
10
|
-
}
|
|
84
|
+
}
|