pygmalion 0.1.14 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +141 -69
- package/THIRD_PARTY.md +9 -0
- package/docs/ARCHITECTURE.md +66 -107
- package/docs/DEPLOY.md +33 -136
- package/docs/DEVELOPMENT.md +63 -0
- package/docs/DEVICE.md +56 -47
- package/docs/EXTENSIONS.md +176 -0
- package/docs/PRESET.md +21 -63
- package/docs/UPGRADE-0.2.md +52 -0
- package/package.json +52 -33
- package/preset/pygmalion-director/cordis.patch.yml +234 -0
- package/preset/pygmalion-director/pygmalion-tools.js +43 -41
- package/scripts/start-local-test.mjs +45 -45
- package/skills/pygmalion-life-scripts/SKILL.md +12 -0
- package/skills/pygmalion-meme-maintenance/SKILL.md +12 -0
- package/skills/pygmalion-memory-archive/SKILL.md +24 -0
- package/skills/pygmalion-toy-control/SKILL.md +16 -0
- package/src/agent-life.js +608 -606
- package/src/archive-tools.js +9 -0
- package/src/bot-factory.js +129 -306
- package/src/bots.js +264 -275
- package/src/channel-qq.js +262 -0
- package/src/config.js +69 -68
- package/src/delivery-receipt.js +36 -0
- package/src/delivery-scope.js +20 -0
- package/src/devices/manager.mjs +80 -525
- package/src/devices/monsterparty-controller.mjs +393 -0
- package/src/devices/{doc.mjs → monsterparty-doc.mjs} +60 -69
- package/src/devices/monsterparty.mjs +294 -294
- package/src/devices/pb.mjs +110 -110
- package/src/devices/registry.mjs +19 -0
- package/src/devices/script-helper.mjs +45 -0
- package/src/devices/scripts.mjs +109 -0
- package/src/devices/service.mjs +254 -0
- package/src/director-messages.js +35 -0
- package/src/dsh-runtime.js +17 -0
- package/src/extension-config.js +20 -0
- package/src/extensions.js +118 -0
- package/src/heartbeat.js +117 -95
- package/src/http-api.js +519 -509
- package/src/inbound.js +36 -207
- package/src/index.js +109 -37
- package/src/llm-provider.js +214 -171
- package/src/meme-worker.js +35 -39
- package/src/memebox.js +1 -1
- package/src/memory-recall.js +55 -0
- package/src/memory-write.js +136 -133
- package/src/peer-identity.js +26 -0
- package/src/persona.js +120 -41
- package/src/role-agent.js +155 -0
- package/src/role-runtime.js +65 -0
- package/src/roleplay-config.js +37 -0
- package/src/roleplay-format.js +37 -0
- package/src/roleplay-generation.js +36 -0
- package/src/roleplay-materials.js +113 -0
- package/src/roleplay-output.js +19 -0
- package/src/roleplay-parallel.js +239 -0
- package/src/roleplay-session.js +128 -0
- package/src/roleplay-worker.js +10 -0
- package/src/session-log.js +180 -143
- package/src/setup-agent.js +92 -100
- package/src/skills.js +97 -0
- package/src/slash-commands.js +155 -142
- package/src/speech-input.js +20 -0
- package/src/threads.js +2 -1
- package/src/tools.js +216 -168
- package/src/voice-defaults.js +13 -0
- package/src/voice-mimo.js +139 -95
- package/src/voice.js +200 -0
- package/src/wakeup.js +76 -35
- package/src/web-public/app.js +536 -367
- package/src/web-public/index.html +161 -156
- package/src/web-public/layout.css +7 -7
- package/src/web-public/style.css +12 -8
- package/src/web-server.js +158 -89
- package/src/worker-tools.js +18 -0
- package/templates/sample-life.mjs +59 -59
- package/templates/sample-toy.mjs +33 -33
- package/preset/pygmalion-director/agent.cordis.yml +0 -253
- package/preset/pygmalion-director/preset.yml +0 -3
package/src/bots.js
CHANGED
|
@@ -1,275 +1,264 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pygmalion 正式版动态 Bot 管理:QQ 机器人凭据在管理网页配置(角色 owner 私有)。
|
|
3
|
-
*
|
|
4
|
-
* bot 记录持久化在 data/bots/<botId>.json,插件运行时按记录动态启停接入管线。
|
|
5
|
-
*
|
|
6
|
-
* botId = 'bot-' + safeId(roleId) — 确定性派生:删了重建 store 文件与会话 id 连续
|
|
7
|
-
* httpApiPath = /external/qq/<botId> — 按 botId 唯一,避免 webServer.register 重复 path
|
|
8
|
-
* httpApiToken = randomBytes(24).hex — 自动生成;生活脚本经 PYG_TOKEN 环境变量自动获得
|
|
9
|
-
*
|
|
10
|
-
* 每 bot 一个串行操作队列(enqueue),防止并发 upsert/remove 竞态。update 若改到运行时
|
|
11
|
-
* 字段(config 部分变化)→ 先停旧管线再重建启动。qqSecret 永不回传(网页只存不读)。
|
|
12
|
-
*/
|
|
13
|
-
import { randomBytes } from 'node:crypto';
|
|
14
|
-
import { mkdir, readFile, readdir, rm } from 'node:fs/promises';
|
|
15
|
-
import { join } from 'node:path';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
function
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (!
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
httpApiPath:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
async function
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
function recordForRole(roleId) {
|
|
266
|
-
const rec = records.get(botIdForRole(roleId));
|
|
267
|
-
return rec ? publicRecord(rec) : null;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return {
|
|
271
|
-
startAll, stopAll,
|
|
272
|
-
upsertForRole, removeForRole, removeBot,
|
|
273
|
-
getBots, getPipelines, statusOf, recordForRole,
|
|
274
|
-
};
|
|
275
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Pygmalion 正式版动态 Bot 管理:QQ 机器人凭据在管理网页配置(角色 owner 私有)。
|
|
3
|
+
*
|
|
4
|
+
* bot 记录持久化在 data/bots/<botId>.json,插件运行时按记录动态启停接入管线。
|
|
5
|
+
*
|
|
6
|
+
* botId = 'bot-' + safeId(roleId) — 确定性派生:删了重建 store 文件与会话 id 连续
|
|
7
|
+
* httpApiPath = /external/qq/<botId> — 按 botId 唯一,避免 webServer.register 重复 path
|
|
8
|
+
* httpApiToken = randomBytes(24).hex — 自动生成;生活脚本经 PYG_TOKEN 环境变量自动获得
|
|
9
|
+
*
|
|
10
|
+
* 每 bot 一个串行操作队列(enqueue),防止并发 upsert/remove 竞态。update 若改到运行时
|
|
11
|
+
* 字段(config 部分变化)→ 先停旧管线再重建启动。qqSecret 永不回传(网页只存不读)。
|
|
12
|
+
*/
|
|
13
|
+
import { randomBytes } from 'node:crypto';
|
|
14
|
+
import { mkdir, readFile, readdir, rm } from 'node:fs/promises';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { createExtensionRegistry } from './extensions.js';
|
|
17
|
+
import { createQqAdapter } from './channel-qq.js';
|
|
18
|
+
import { defaults, validate } from './extension-config.js';
|
|
19
|
+
import { atomicWrite, dataRoot, safeId } from './persona.js';
|
|
20
|
+
|
|
21
|
+
async function readJson(file, fallback) {
|
|
22
|
+
try { return JSON.parse(await readFile(file, 'utf8')); } catch { return fallback; }
|
|
23
|
+
}
|
|
24
|
+
function atomicWriteJson(file, value) {
|
|
25
|
+
return atomicWrite(file, JSON.stringify(value ?? {}, null, 2));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param deps {
|
|
30
|
+
* config // 插件 Config(legacy config.bots 迁移源,可无)
|
|
31
|
+
* log // 日志
|
|
32
|
+
* persona // persona 库(dataRoot/safeId/atomicWrite)
|
|
33
|
+
* dataRoot? // 覆盖数据根(测试用)
|
|
34
|
+
* buildPipeline // (record) => { facade, getStatus(), start(), stop(), httpApiUnavailable? }
|
|
35
|
+
* // (index.js 里封装 createBotPipeline,record 已过 BotConfig schema)
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
export function createBotManager(deps) {
|
|
39
|
+
if (!deps.extensions) {
|
|
40
|
+
const extensions=createExtensionRegistry();
|
|
41
|
+
extensions.registerChannelAdapter(createQqAdapter(deps));
|
|
42
|
+
deps={...deps,extensions,defaultAdapterId:'qq-official'};
|
|
43
|
+
}
|
|
44
|
+
const { config, log, persona, buildPipeline } = deps;
|
|
45
|
+
const dataDir = deps.dataRoot ?? persona.dataRoot();
|
|
46
|
+
const dir = join(dataDir, 'bots');
|
|
47
|
+
|
|
48
|
+
const records = new Map(); // botId -> 规范化记录
|
|
49
|
+
const pipelines = new Map(); // botId -> 存活管线
|
|
50
|
+
const queues = new Map(); // botId -> 串行操作 Promise 链
|
|
51
|
+
let alive = false; // 启动期开关:stopAll 打断未完成的 startAll 时阻止后续管线再起
|
|
52
|
+
|
|
53
|
+
function botIdForRole(roleId) { return `bot-${safeId(roleId)}`; }
|
|
54
|
+
function botPath(botId) { return join(dir, `${botId}.json`); }
|
|
55
|
+
|
|
56
|
+
/** 入队串行操作:上一个完成后再执行 fn,错误不污染队列。 */
|
|
57
|
+
function enqueue(botId, fn) {
|
|
58
|
+
const prev = queues.get(botId) ?? Promise.resolve();
|
|
59
|
+
const next = prev.catch(() => {}).then(fn);
|
|
60
|
+
queues.set(botId, next.catch(() => {}));
|
|
61
|
+
return next;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 规范化记录:BotConfig 部分过 schema(校验 + 补默认值),剥离并回填 meta 字段。
|
|
66
|
+
* 非法 → null(带日志,不 throw,避免单个坏记录拖垮整批加载)。
|
|
67
|
+
*/
|
|
68
|
+
function normalizeRecord(raw) {
|
|
69
|
+
if (!raw?.id || !raw.roleId) return null;
|
|
70
|
+
const adapterId = raw.adapterId ?? deps.defaultAdapterId;
|
|
71
|
+
const descriptor = deps.extensions.getChannelAdapter(adapterId)?.descriptor;
|
|
72
|
+
let values = raw.config ?? raw;
|
|
73
|
+
try {
|
|
74
|
+
if (descriptor) {
|
|
75
|
+
values = defaults(descriptor, values); validate(descriptor, values, true);
|
|
76
|
+
values = descriptor.normalizeConfig?.(values, raw) ?? values;
|
|
77
|
+
}
|
|
78
|
+
} catch (error) { log?.warn?.('连接配置无效 %s: %s', raw.id, error.message); return null; }
|
|
79
|
+
return {...raw,adapterId,config:values,enabled:raw.enabled!==false,
|
|
80
|
+
withinChatWindowMs:raw.withinChatWindowMs??300000,createdAt:raw.createdAt??Date.now(),updatedAt:raw.updatedAt??Date.now()};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 运行时部分(不含 meta 字段)JSON 串:判断更新是否需要重建管线。 */
|
|
84
|
+
function configKey(rec) {
|
|
85
|
+
const c = { ...rec };
|
|
86
|
+
for (const k of ['owner', 'enabled', 'createdAt', 'updatedAt']) delete c[k];
|
|
87
|
+
return JSON.stringify(c);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function loadAll() {
|
|
91
|
+
await mkdir(dir, { recursive: true });
|
|
92
|
+
let names;
|
|
93
|
+
try { names = await readdir(dir); } catch { names = []; }
|
|
94
|
+
for (const name of names) {
|
|
95
|
+
if (!name.endsWith('.json')) continue;
|
|
96
|
+
const id = name.slice(0, -5);
|
|
97
|
+
const rec = normalizeRecord(await readJson(botPath(id), null));
|
|
98
|
+
if (rec) records.set(id, rec);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 一次性迁移配置里的 bots[](兼容旧部署格式)到数据目录。 */
|
|
103
|
+
async function migrateLegacy() {
|
|
104
|
+
const legacy = Array.isArray(config?.bots) ? config.bots : [];
|
|
105
|
+
for (const b of legacy) {
|
|
106
|
+
if (!b || typeof b !== 'object' || !b.roleId) continue;
|
|
107
|
+
const botId = botIdForRole(b.roleId);
|
|
108
|
+
if (records.has(botId)) continue; // 已有网页配置优先
|
|
109
|
+
const rec = normalizeRecord({ ...b, id: botId });
|
|
110
|
+
if (!rec) { log?.warn?.('pygmalion: legacy bot %s 非法,跳过迁移', b?.id); continue; }
|
|
111
|
+
if (!rec.httpApiPath) rec.httpApiPath = `/external/qq/${botId}`;
|
|
112
|
+
if (!rec.httpApiToken) rec.httpApiToken = randomBytes(24).toString('hex');
|
|
113
|
+
rec.createdAt = Date.now();
|
|
114
|
+
rec.updatedAt = Date.now();
|
|
115
|
+
records.set(botId, rec);
|
|
116
|
+
await atomicWriteJson(botPath(botId), rec);
|
|
117
|
+
log?.info?.('pygmalion: 迁移 legacy bot %s → %s', b.id, botId);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function startPipeline(botId, record) {
|
|
122
|
+
if (!alive) return;
|
|
123
|
+
try {
|
|
124
|
+
const pipeline = buildPipeline(record);
|
|
125
|
+
await pipeline.start();
|
|
126
|
+
pipelines.set(botId, pipeline);
|
|
127
|
+
log?.info?.('pygmalion: bot %s 已启动(role=%s)', botId, record.roleId);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
log?.error?.('pygmalion: bot %s 启动失败: %o', botId, error);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function stopPipeline(botId) {
|
|
135
|
+
const p = pipelines.get(botId);
|
|
136
|
+
if (!p) return;
|
|
137
|
+
pipelines.delete(botId);
|
|
138
|
+
try { p.stop(); } catch { /* ignore */ }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 启动所有记录:先加载 + 迁移配置,再启所有 enabled 管线。 */
|
|
142
|
+
async function startAll() {
|
|
143
|
+
if (alive) return;
|
|
144
|
+
alive = true;
|
|
145
|
+
await loadAll();
|
|
146
|
+
await migrateLegacy();
|
|
147
|
+
for (const [botId, rec] of records) {
|
|
148
|
+
if (!alive) return; // 已被 stopAll 打断,放弃后续启动
|
|
149
|
+
if (rec.enabled === false) continue;
|
|
150
|
+
try { await startPipeline(botId, rec); }
|
|
151
|
+
catch { /* 单 bot 失败不阻断其余 */ }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function stopAll() {
|
|
155
|
+
alive = false;
|
|
156
|
+
for (const botId of [...pipelines.keys()]) stopPipeline(botId);
|
|
157
|
+
records.clear();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function statusOf(botId) {
|
|
161
|
+
const p = pipelines.get(botId);
|
|
162
|
+
if (p) return p.getStatus();
|
|
163
|
+
const record = records.get(botId);
|
|
164
|
+
if (record && deps.extensions && !deps.extensions.getChannelAdapter(record.adapterId)) return { state:'unavailable',detail:'通信实现未安装或已卸载,配置已保留' };
|
|
165
|
+
return record ? { state: 'stopped', detail: '' } : { state: 'missing', detail: '' };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 网页可见记录(脱敏:不含 qqSecret)。 */
|
|
169
|
+
function publicRecord(rec) {
|
|
170
|
+
const pipeline = pipelines.get(rec.id);
|
|
171
|
+
const descriptor = deps.extensions?.getChannelAdapter(rec.adapterId)?.descriptor;
|
|
172
|
+
const values = {}, secrets = {};
|
|
173
|
+
for (const field of descriptor?.fields ?? []) {
|
|
174
|
+
if (field.type==='secret') secrets[field.key]=Boolean(rec.config?.[field.key]);
|
|
175
|
+
else if (rec.config?.[field.key]!==undefined) values[field.key]=rec.config[field.key];
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
botId:rec.id,roleId:rec.roleId,adapterId:rec.adapterId,config:values,secrets,
|
|
179
|
+
...descriptor?.publicLegacy?.(rec),withinChatWindowMs:rec.withinChatWindowMs,
|
|
180
|
+
httpApiPath:rec.httpApiPath,httpApiToken:rec.httpApiToken,enabled:rec.enabled,
|
|
181
|
+
httpApiUnavailable:pipeline?.httpApiUnavailable===true,status:statusOf(rec.id),
|
|
182
|
+
owner:rec.owner,createdAt:rec.createdAt,updatedAt:rec.updatedAt,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 创建/更新某角色的机器人(botId 由 roleId 派生,天然一角色一机器人)。
|
|
188
|
+
* input.qqSecret 为空 = 保持原值;首次配置必须提供。运行时字段变化 → 停旧管线重建启动。
|
|
189
|
+
*/
|
|
190
|
+
async function upsertForRole(roleId, input) {
|
|
191
|
+
const rid = safeId(String(roleId ?? '').trim());
|
|
192
|
+
if (!rid) throw new Error('roleId 非法');
|
|
193
|
+
const botId = botIdForRole(rid);
|
|
194
|
+
return enqueue(botId, async () => {
|
|
195
|
+
const existing = records.get(botId);
|
|
196
|
+
const adapterId=input.adapterId??existing?.adapterId??deps.defaultAdapterId;
|
|
197
|
+
const descriptor=deps.extensions.getChannelAdapter(adapterId)?.descriptor;
|
|
198
|
+
if(!descriptor)throw new Error('所选通信实现不可用,原配置已保留');
|
|
199
|
+
const values=adapterId===existing?.adapterId?{...existing.config}:{};
|
|
200
|
+
const changes=input.config??Object.fromEntries(descriptor.fields.filter(field=>input[field.key]!==undefined).map(field=>[field.key,input[field.key]]));
|
|
201
|
+
const fields=new Map(descriptor.fields.map(field=>[field.key,field]));
|
|
202
|
+
for(const [key,value]of Object.entries(changes)){
|
|
203
|
+
if(!fields.has(key))throw new Error('通信配置包含未声明字段');
|
|
204
|
+
if(fields.get(key).type==='secret'&&value==='')continue;
|
|
205
|
+
if(value===null)delete values[key];else values[key]=value;
|
|
206
|
+
}
|
|
207
|
+
const metadata=existing?Object.fromEntries(['owner','enabled','withinChatWindowMs','httpApiPath','httpApiToken','createdAt','updatedAt'].map(key=>[key,existing[key]])):{};
|
|
208
|
+
const base={...metadata,id:botId,roleId:rid,adapterId,config:values};
|
|
209
|
+
for(const key of ['enabled','withinChatWindowMs','owner'])if(input[key]!==undefined)base[key]=input[key];
|
|
210
|
+
base.httpApiPath??=`/external/pygmalion/${botId}`;base.httpApiToken??=randomBytes(24).toString('hex');
|
|
211
|
+
const rec=normalizeRecord(base);if(!rec)throw new Error('通信配置无效');
|
|
212
|
+
rec.updatedAt=Date.now();
|
|
213
|
+
await atomicWriteJson(botPath(botId),descriptor.encodeRecord?.(rec)??rec);
|
|
214
|
+
const rebuild=!existing||configKey(rec)!==configKey(existing);
|
|
215
|
+
records.set(botId,rec);
|
|
216
|
+
if(!rec.enabled)stopPipeline(botId);
|
|
217
|
+
else if(rebuild||!pipelines.has(botId)){stopPipeline(botId);await startPipeline(botId,rec);}
|
|
218
|
+
return publicRecord(rec);
|
|
219
|
+
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function removeForRole(roleId) {
|
|
224
|
+
const botId = botIdForRole(roleId);
|
|
225
|
+
return enqueue(botId, async () => {
|
|
226
|
+
stopPipeline(botId);
|
|
227
|
+
records.delete(botId);
|
|
228
|
+
await rm(botPath(botId), { force: true });
|
|
229
|
+
return { ok: true, botId };
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async function removeBot(botId) {
|
|
233
|
+
return enqueue(botId, async () => {
|
|
234
|
+
stopPipeline(botId);
|
|
235
|
+
records.delete(botId);
|
|
236
|
+
await rm(botPath(botId), { force: true });
|
|
237
|
+
return { ok: true, botId };
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** 全部记录(网页列表,脱敏)。 */
|
|
242
|
+
function getBots() { return [...records.values()].map(publicRecord); }
|
|
243
|
+
/** 存活管线 facade(heartbeat 消费:routes/threads/inbound/withinChatWindowMs/httpApiToken)。 */
|
|
244
|
+
function getPipelines() { return [...pipelines.values()].map((p) => p.facade).filter(Boolean); }
|
|
245
|
+
/** 某角色的机器人记录(脱敏)或 null。 */
|
|
246
|
+
function recordForRole(roleId) {
|
|
247
|
+
const rec = records.get(botIdForRole(roleId));
|
|
248
|
+
return rec ? publicRecord(rec) : null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function refreshAdapters() {
|
|
252
|
+
for(const [id,record] of records) {
|
|
253
|
+
if(!alive||record.enabled===false)continue;
|
|
254
|
+
const entry=deps.extensions?.getChannelAdapter(record.adapterId);
|
|
255
|
+
if(pipelines.get(id)?.adapterEntry===entry)continue;
|
|
256
|
+
await enqueue(id,async()=>{if(!alive)return;stopPipeline(id);if(entry)await startPipeline(id,record);});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
startAll, stopAll, refreshAdapters,
|
|
261
|
+
upsertForRole, removeForRole, removeBot,
|
|
262
|
+
getBots, getPipelines, statusOf, recordForRole,
|
|
263
|
+
};
|
|
264
|
+
}
|