pygmalion 0.1.8 → 0.1.9
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/docs/DEVICE.md +47 -0
- package/package.json +1 -1
- package/preset/pygmalion-director/agent.cordis.yml +2 -1
- package/src/bot-factory.js +31 -2
- package/src/devices/doc.mjs +67 -0
- package/src/devices/manager.mjs +498 -0
- package/src/devices/monsterparty.mjs +294 -0
- package/src/devices/pb.mjs +110 -0
- package/src/http-api.js +45 -0
- package/src/inbound.js +27 -1
- package/src/index.js +7 -1
- package/src/memory-write.js +1 -1
- package/src/persona.js +87 -2
- package/src/session-log.js +1 -1
- package/src/setup-agent.js +3 -1
- package/src/tools.js +202 -0
- package/src/wakeup.js +5 -2
- package/src/web-public/app.js +96 -0
- package/src/web-public/index.html +28 -0
- package/src/web-public/style.css +5 -0
- package/src/web-server.js +109 -0
- package/templates/sample-toy.mjs +42 -0
package/src/web-server.js
CHANGED
|
@@ -52,6 +52,8 @@ const MIME = {
|
|
|
52
52
|
|
|
53
53
|
export function createWebServer(deps) {
|
|
54
54
|
const { persona, log } = deps;
|
|
55
|
+
// DeviceManager(index.js 注入;独立运行/无宿主时可缺省,设备路由照常、运行态清理跳过)。
|
|
56
|
+
const devices = deps.devices;
|
|
55
57
|
// 自定义 LLM provider 注册服务(index.js 注入;缺省时保存模型不注册,功能照旧)。
|
|
56
58
|
const llmProviders = deps.llmProviders;
|
|
57
59
|
const host = deps.web?.host ?? '127.0.0.1';
|
|
@@ -192,6 +194,18 @@ export function createWebServer(deps) {
|
|
|
192
194
|
const backups = await readdir(join(lifeDir, '.backup')).then((a) => a.sort().reverse()).catch(() => []);
|
|
193
195
|
return { manifest, logs: logs.slice(0, 50), backups: backups.slice(0, 50), running: listRunningLifeScripts(role.workspace) };
|
|
194
196
|
}
|
|
197
|
+
/** 玩具操控脚本监管:脚本清单 + .logs + 运行中(与 life 同级,完全透明)。 */
|
|
198
|
+
async function toysStatus(roleId) {
|
|
199
|
+
const role = await persona.loadRole(roleId);
|
|
200
|
+
if (!role) return null;
|
|
201
|
+
const toysDir = join(role.workspace, 'toys');
|
|
202
|
+
const scripts = await readdir(toysDir).then((a) => a.filter((n) => /\.(mjs|js|ts)$/.test(n)).sort()).catch(() => []);
|
|
203
|
+
const logs = await readdir(join(toysDir, '.logs')).then((a) => a.sort().reverse()).catch(() => []);
|
|
204
|
+
const running = (typeof devices?.listScriptRuns === 'function' ? devices.listScriptRuns() : [])
|
|
205
|
+
.filter((r) => r.roleId === roleId)
|
|
206
|
+
.map((r) => ({ runId: r.runId, scriptName: r.scriptName, pid: r.pid, startedAt: r.startedAt }));
|
|
207
|
+
return { scripts, logs: logs.slice(0, 50), running };
|
|
208
|
+
}
|
|
195
209
|
/** 安全化角色 id 参数(防路径穿越)。 */
|
|
196
210
|
function roleParam(raw) {
|
|
197
211
|
const rid = String(raw ?? '');
|
|
@@ -230,9 +244,21 @@ export function createWebServer(deps) {
|
|
|
230
244
|
speakingStyle: String(p.speakingStyle ?? '').trim() || undefined,
|
|
231
245
|
relationship: String(p.relationship ?? '').trim() || undefined,
|
|
232
246
|
schedule,
|
|
247
|
+
devices: (p.devices !== undefined && Array.isArray(p.devices))
|
|
248
|
+
? [...new Set(p.devices.map((d) => String(d)).filter((d) => /^[A-Za-z0-9._-]+$/.test(d)))]
|
|
249
|
+
: undefined,
|
|
233
250
|
};
|
|
234
251
|
}
|
|
235
252
|
|
|
253
|
+
/** 校验 persona.devices 每项都是 me 名下的设备;返回非法 deviceId 或 null。 */
|
|
254
|
+
async function checkDevicesOwned(p, me) {
|
|
255
|
+
for (const id of Array.isArray(p?.devices) ? p.devices : []) {
|
|
256
|
+
const dev = await persona.loadDevice(id).catch(() => null);
|
|
257
|
+
if (!dev || dev.owner !== me.qqNumber) return id;
|
|
258
|
+
}
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
|
|
236
262
|
/**
|
|
237
263
|
* 应用 model/proactive patch 并同步自定义 provider(POST 新建与 PUT 编辑共用)。
|
|
238
264
|
* model 保存时去掉空值键(清空的字段等同不配置);provider 留空由 providerRoute
|
|
@@ -357,6 +383,8 @@ export function createWebServer(deps) {
|
|
|
357
383
|
const b = await readBody(req);
|
|
358
384
|
const personaFields = normalizePersona(b);
|
|
359
385
|
if (!personaFields.name) { sendJson(res, 400, { ok: false, error: '请填写角色名' }); return; }
|
|
386
|
+
const bad = await checkDevicesOwned(personaFields, me);
|
|
387
|
+
if (bad) { sendJson(res, 400, { ok: false, error: `设备 ${bad} 不归你所有` }); return; }
|
|
360
388
|
const roleId = await persona.createRole({ accountId: me.qqNumber, persona: personaFields });
|
|
361
389
|
// 首次保存同样落 model/proactive(修:新建角色一次保存即生效,而非只有再编辑才存)。
|
|
362
390
|
const role = await applyModelProactive(roleId, b ?? {});
|
|
@@ -364,6 +392,41 @@ export function createWebServer(deps) {
|
|
|
364
392
|
return;
|
|
365
393
|
}
|
|
366
394
|
|
|
395
|
+
// ---- 我的设备库(用户级,角色经 persona.devices 勾选)----
|
|
396
|
+
if (pathname === '/api/me/devices' && req.method === 'GET') {
|
|
397
|
+
const list = await persona.listDevices(me.qqNumber);
|
|
398
|
+
sendJson(res, 200, { ok: true, devices: list });
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (pathname === '/api/me/devices' && req.method === 'POST') {
|
|
402
|
+
const b = await readBody(req);
|
|
403
|
+
const alias = String(b?.alias ?? '').trim();
|
|
404
|
+
if (!alias) { sendJson(res, 400, { ok: false, error: '请填写设备别名' }); return; }
|
|
405
|
+
const kind = b?.kind === 'monsterparty' ? 'monsterparty' : 'monsterparty';
|
|
406
|
+
const device = await persona.createDevice({ owner: me.qqNumber, alias, kind });
|
|
407
|
+
sendJson(res, 200, { ok: true, device });
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const pDev = /^\/api\/me\/devices\/([^/]+)$/.exec(pathname);
|
|
411
|
+
if (pDev && req.method === 'DELETE') {
|
|
412
|
+
const did = /^[A-Za-z0-9._-]+$/.test(pDev[1]) ? pDev[1] : null;
|
|
413
|
+
if (!did) { sendJson(res, 400, { ok: false, error: '设备 ID 非法' }); return; }
|
|
414
|
+
const dev = await persona.loadDevice(did);
|
|
415
|
+
if (!dev || dev.owner !== me.qqNumber) { sendJson(res, 404, { ok: false, error: '设备不存在' }); return; }
|
|
416
|
+
const { removed, roleIds } = await persona.deleteDevice(did);
|
|
417
|
+
if (removed) {
|
|
418
|
+
for (const rid of roleIds) {
|
|
419
|
+
try { await devices?.disposeRole?.(rid); } catch { /* ignore */ }
|
|
420
|
+
try {
|
|
421
|
+
const r = await persona.loadRole(rid);
|
|
422
|
+
if (r) await persona.syncRoleMemory(r);
|
|
423
|
+
} catch { /* ignore */ }
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
sendJson(res, 200, { ok: true, removed });
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
|
|
367
430
|
const pm = /^\/api\/me\/roles\/([^/]+)$/.exec(pathname);
|
|
368
431
|
if (pm) {
|
|
369
432
|
const rid = roleParam(pm[1]);
|
|
@@ -376,6 +439,8 @@ export function createWebServer(deps) {
|
|
|
376
439
|
const patch = b ?? {};
|
|
377
440
|
if (patch.persona !== undefined) {
|
|
378
441
|
const p = normalizePersona({ persona: patch.persona });
|
|
442
|
+
const bad = await checkDevicesOwned(p, me);
|
|
443
|
+
if (bad) { sendJson(res, 400, { ok: false, error: `设备 ${bad} 不归你所有` }); return; }
|
|
379
444
|
p.owner = me.qqNumber;
|
|
380
445
|
await persona.saveRoleField(rid, 'persona', p);
|
|
381
446
|
}
|
|
@@ -388,6 +453,7 @@ export function createWebServer(deps) {
|
|
|
388
453
|
try { await llmProviders.unregister(role); } catch (e) { log?.warn?.('pygmalion: 注销自定义 provider %s 失败 %o', rid, e); }
|
|
389
454
|
}
|
|
390
455
|
await botApi.removeForRole(rid);
|
|
456
|
+
try { await devices?.disposeRole?.(rid); } catch { /* ignore */ }
|
|
391
457
|
await persona.deleteRole(rid);
|
|
392
458
|
sendJson(res, 200, { ok: true, deleted: rid });
|
|
393
459
|
return;
|
|
@@ -548,6 +614,49 @@ export function createWebServer(deps) {
|
|
|
548
614
|
return;
|
|
549
615
|
}
|
|
550
616
|
|
|
617
|
+
// 玩具操控脚本监管(与生活脚本同级的透明监控)
|
|
618
|
+
const pToysStop = /^\/api\/me\/roles\/([^/]+)\/toys\/stop$/.exec(pathname);
|
|
619
|
+
if (pToysStop && req.method === 'POST') {
|
|
620
|
+
const rid = roleParam(pToysStop[1]);
|
|
621
|
+
if (!rid) { sendJson(res, 400, { ok: false, error: '角色 ID 非法' }); return; }
|
|
622
|
+
const role = await persona.loadRole(rid);
|
|
623
|
+
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
624
|
+
if (!devices || typeof devices.stopScript !== 'function') { sendJson(res, 503, { ok: false, error: '设备服务不可用' }); return; }
|
|
625
|
+
const b = await readBody(req);
|
|
626
|
+
const runId = String(b?.runId ?? '').trim();
|
|
627
|
+
if (!runId) { sendJson(res, 400, { ok: false, error: '缺少 runId' }); return; }
|
|
628
|
+
const r = await devices.stopScript(runId);
|
|
629
|
+
sendJson(res, 200, r);
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
const pToys = /^\/api\/me\/roles\/([^/]+)\/toys(?:\/(script|log)\/([^/]+))?$/.exec(pathname);
|
|
633
|
+
if (pToys && req.method === 'GET') {
|
|
634
|
+
const rid = roleParam(pToys[1]);
|
|
635
|
+
if (!rid) { sendJson(res, 400, { ok: false, error: '角色 ID 非法' }); return; }
|
|
636
|
+
const role = await persona.loadRole(rid);
|
|
637
|
+
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
638
|
+
if (pToys[2]) {
|
|
639
|
+
let rawName = '';
|
|
640
|
+
try { rawName = decodeURIComponent(pToys[3]); } catch { /* 非法 % 序列 */ }
|
|
641
|
+
const name = pToys[2] === 'log' ? logFileNameParam(rawName) : scriptFileNameParam(rawName);
|
|
642
|
+
if (!name) { sendJson(res, 400, { ok: false, error: pToys[2] === 'log' ? '日志名非法' : '脚本名非法' }); return; }
|
|
643
|
+
const file = pToys[2] === 'log'
|
|
644
|
+
? join(role.workspace, 'toys', '.logs', name)
|
|
645
|
+
: join(role.workspace, 'toys', name);
|
|
646
|
+
const base = pToys[2] === 'log'
|
|
647
|
+
? join(role.workspace, 'toys', '.logs')
|
|
648
|
+
: join(role.workspace, 'toys');
|
|
649
|
+
if (!file.startsWith(base)) { sendJson(res, 400, { ok: false, error: '路径非法' }); return; }
|
|
650
|
+
const text = await readFile(file, 'utf8').catch(() => null);
|
|
651
|
+
if (text === null) { sendJson(res, 404, { ok: false, error: pToys[2] === 'log' ? '日志不存在' : '脚本不存在' }); return; }
|
|
652
|
+
res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' });
|
|
653
|
+
res.end(text);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
sendJson(res, 200, { ok: true, ...(await toysStatus(rid)) });
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
551
660
|
sendJson(res, 404, { ok: false, error: `not found: ${pathname}` });
|
|
552
661
|
} catch (e) {
|
|
553
662
|
log?.error?.('web-server error: %o', e);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// toy 脚本示例 —— 角色操控玩具设备的参考模板(dsh 自由改编)。
|
|
2
|
+
//
|
|
3
|
+
// 脚本只需关心两个参数,不接触任何驱动/通信细节:
|
|
4
|
+
// await toy.set({ intensity, stroke }) 改变强度 / 行程范围
|
|
5
|
+
// await toy.sleep(ms) 控制节奏
|
|
6
|
+
// toy.stop() 停止
|
|
7
|
+
// await toy.wake({ mood, topic, reason }) 合适时机请导演开口(如调情/回应)——可说可静默
|
|
8
|
+
//
|
|
9
|
+
// 参数语义(雷电):intensity 0-100(70~100 是 ta 常用的强区间);stroke 是一对行程边界
|
|
10
|
+
// "A,B",取值 0/33/66/100(0=L1 最深、100=L3 几乎拔出)。体感:0,100 大开大合、
|
|
11
|
+
// 0,66 幅度适宜有包裹感、0,33 快而轻、66,100 顶端附近。
|
|
12
|
+
//
|
|
13
|
+
// 脚本自然结束即终止,并保持最后一步的参数继续运行(不中断体验),系统会提醒导演可续可停;
|
|
14
|
+
// 若脚本想主动停设备,就在结束前调 toy.stop()。也可被 toy_run_script stop / toy_control stop 中途接管。
|
|
15
|
+
// 把本文件另存为 workspace/toys/ 下的新文件(勿覆盖已存在的),再用 toy_run_script 运行。
|
|
16
|
+
|
|
17
|
+
// 下面只是一个展示语法的简单例子——脚本的运作方式远不止这一种(可循环、可随 ta 的反应与对话气氛应变等),由你自由设计。
|
|
18
|
+
// 这段示范"渐进加深→起伏→收尾",雷电常用强度偏高,取 60~90。
|
|
19
|
+
const STEPS = [
|
|
20
|
+
{ intensity: 60, stroke: '0,66', ms: 1800 }, // 舒适区、有包裹感
|
|
21
|
+
{ intensity: 75, stroke: '0,66', ms: 2000 }, // 加强
|
|
22
|
+
{ intensity: 90, stroke: '0,100', ms: 2400 }, // 大开大合
|
|
23
|
+
{ wake: { mood: '俏皮', topic: '刚来了一段猛的', reason: '适合说一句撩人的话' } }, // 请导演开口
|
|
24
|
+
// 起伏:固定行程内反复调强度
|
|
25
|
+
{ intensity: 70, stroke: '33,100', ms: 1100 },
|
|
26
|
+
{ intensity: 90, stroke: '33,100', ms: 1100 },
|
|
27
|
+
{ intensity: 70, stroke: '33,100', ms: 1100 },
|
|
28
|
+
{ intensity: 95, stroke: '0,100', ms: 1500 },
|
|
29
|
+
// 收尾
|
|
30
|
+
{ intensity: 80, stroke: '0,66', ms: 1300 },
|
|
31
|
+
{ intensity: 65, stroke: '0,33', ms: 1200 },
|
|
32
|
+
// 脚本在此自然结束:设备保持上面这步的参数继续运行(体验不中断),系统会提醒导演可续可停;
|
|
33
|
+
// 若想让脚本主动停设备,把上面换成 { intensity: 0, ms: 500 } 或结束前 await toy.stop()
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
for (const step of STEPS) {
|
|
37
|
+
if (step.wake) { await toy.wake(step.wake); continue; } // 合适时机请导演开口(可说可静默)
|
|
38
|
+
const p = { intensity: step.intensity };
|
|
39
|
+
if (step.stroke) p.stroke = step.stroke;
|
|
40
|
+
await toy.set(p); // toy.set 是异步的(内部经 HTTP 下发),务必 await
|
|
41
|
+
if (step.ms) await toy.sleep(step.ms);
|
|
42
|
+
}
|