pygmalion 0.1.15 → 0.2.1
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 -70
- package/THIRD_PARTY.md +9 -0
- package/docs/ARCHITECTURE.md +66 -108
- package/docs/DEPLOY.md +33 -137
- package/docs/DEVELOPMENT.md +67 -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 -311
- 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 -570
- package/src/devices/monsterparty-controller.mjs +393 -0
- package/src/devices/{doc.mjs → monsterparty-doc.mjs} +60 -71
- 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 +118 -97
- 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 +132 -69
- 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 +46 -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 +134 -0
- package/src/roleplay-worker.js +10 -0
- package/src/session-log.js +180 -172
- 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 +226 -254
- package/src/voice-defaults.js +13 -0
- package/src/voice-mimo.js +139 -125
- package/src/voice.js +200 -0
- package/src/wakeup.js +85 -66
- package/src/web-public/app.js +542 -382
- package/src/web-public/index.html +161 -158
- package/src/web-public/layout.css +7 -7
- package/src/web-public/style.css +12 -8
- package/src/web-server.js +161 -102
- 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 -262
- package/preset/pygmalion-director/preset.yml +0 -3
- package/src/message-review.js +0 -276
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 雷电既有控制器及 toy_* 兼容行为;不用于描述其他设备。
|
|
3
|
+
* 进程内持有每个角色(roleId)的雷电运行时:
|
|
4
|
+
* - activation(token/session,仅内存,单次有效)来自 QQ 转发的分享链接;
|
|
5
|
+
* - conn(MonsterPartyConn)懒建:首次 apply 才连 WS;
|
|
6
|
+
* - 工具模式:applyFromTool 设置参数 + 可选时长,到点保持参数并提醒 dsh;
|
|
7
|
+
* - 脚本由通用运行器监管;本模块只处理雷电的控制阶段与结束通知。
|
|
8
|
+
* - 提醒 dsh 走 registerRole 注入的 wake 闭包(由 bot-factory 提供,force 唤醒,导演内部信号)。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { mkdirSync, writeFileSync, existsSync } from 'node:fs';
|
|
12
|
+
|
|
13
|
+
import { join, basename } from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { parseLink as parseMp, resolveSession as resolveMp, MonsterPartyConn, MONSTERPARTY_CAPABILITY } from './monsterparty.mjs';
|
|
16
|
+
|
|
17
|
+
const DEFAULT_BASE = 'http://127.0.0.1:3080';
|
|
18
|
+
const REMIND_COALESCE_MS = 10_000; // 同类提醒 10s 合并,防堆积
|
|
19
|
+
export const TOY_REFERENCE_NAME = '玩具脚本示例.mjs';
|
|
20
|
+
|
|
21
|
+
/** 角色工作区中的可读参考。它只示范“连续波形 + 脚本内唤醒”的接口位置;具体波形由导演自行创作。 */
|
|
22
|
+
const TOY_REFERENCE_SOURCE = `// 玩具脚本参考:先理解接口和 wake 的位置,再自行创作实际波形。
|
|
23
|
+
const clamp = (value, low, high) => Math.max(low, Math.min(high, value));
|
|
24
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
25
|
+
|
|
26
|
+
const startedAt = Date.now();
|
|
27
|
+
const durationMs = 90_000;
|
|
28
|
+
let woke = false;
|
|
29
|
+
|
|
30
|
+
while (Date.now() - startedAt < durationMs) {
|
|
31
|
+
const seconds = (Date.now() - startedAt) / 1000;
|
|
32
|
+
const intensity = Math.round(clamp(65 + 20 * Math.sin(seconds * 0.7), 0, 100));
|
|
33
|
+
const stroke = Math.sin(seconds * 0.7) >= 0 ? '0,66' : '0,100';
|
|
34
|
+
await toy.set({ intensity, stroke });
|
|
35
|
+
|
|
36
|
+
if (!woke && seconds >= 35) {
|
|
37
|
+
woke = true;
|
|
38
|
+
await toy.wake({ reason: '脚本中的玩具运动产生了值得导演感知的变化' });
|
|
39
|
+
}
|
|
40
|
+
await toy.sleep(700);
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
function ensureToyReference(workspace) {
|
|
45
|
+
if (!workspace) return;
|
|
46
|
+
try {
|
|
47
|
+
const toysDir = join(workspace, 'toys');
|
|
48
|
+
mkdirSync(toysDir, { recursive: true });
|
|
49
|
+
const file = join(toysDir, TOY_REFERENCE_NAME);
|
|
50
|
+
if (!existsSync(file)) writeFileSync(file, TOY_REFERENCE_SOURCE, 'utf8');
|
|
51
|
+
} catch { /* 参考文件写入失败不阻塞设备激活或脚本运行 */ }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 驱动注册表(v1 仅 monsterparty)。kind → { capability, parseLink, resolveSession, Conn } */
|
|
55
|
+
const DRIVERS = {
|
|
56
|
+
monsterparty: {
|
|
57
|
+
capability: MONSTERPARTY_CAPABILITY,
|
|
58
|
+
parseLink: parseMp,
|
|
59
|
+
resolveSession: resolveMp,
|
|
60
|
+
Conn: MonsterPartyConn,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** 行程参数归一:接受 "0,66" / "33,100" / "L1,L2" / "L1 L2" / "L2"(区间) 等。返回 [a,b] 或 null。 */
|
|
65
|
+
export function normalizeStroke(value, cap) {
|
|
66
|
+
const zones = cap?.stroke?.zones;
|
|
67
|
+
const tok = (s) => {
|
|
68
|
+
const t = String(s ?? '').trim().toUpperCase();
|
|
69
|
+
if (zones && zones[t]) return zones[t]; // L1/L2/L3 区间
|
|
70
|
+
const n = Number(t);
|
|
71
|
+
return Number.isFinite(n) ? [n, n] : null;
|
|
72
|
+
};
|
|
73
|
+
const raw = String(value ?? '').trim();
|
|
74
|
+
if (!raw) return null;
|
|
75
|
+
const parts = raw.split(/[,,\s~至\-]+/).filter(Boolean).map(tok);
|
|
76
|
+
if (!parts.length || parts.some((p) => !p)) return null;
|
|
77
|
+
const a = Math.min(...parts.map((p) => p[0]));
|
|
78
|
+
const b = Math.max(...parts.map((p) => p[1]));
|
|
79
|
+
if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 || b > 100 || a > b) return null;
|
|
80
|
+
return [a, b];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 建 DeviceManager。
|
|
85
|
+
* @param {object} deps { persona, log?, baseUrl? }(getWake 可选由 registerRole 提供)
|
|
86
|
+
*/
|
|
87
|
+
export function createMonsterPartyController(deps) {
|
|
88
|
+
const persona = deps.persona;
|
|
89
|
+
const log = deps.log;
|
|
90
|
+
const baseUrl = deps.baseUrl || DEFAULT_BASE;
|
|
91
|
+
const drivers = deps.drivers || DRIVERS;
|
|
92
|
+
|
|
93
|
+
/** roleId → { deviceId, alias, token, session }(内存激活态,token 不落盘) */
|
|
94
|
+
const activations = new Map();
|
|
95
|
+
/** roleId → DeviceRuntime */
|
|
96
|
+
const runtimes = new Map();
|
|
97
|
+
/** roleId → wake 闭包(bot-factory 注入):async (contextText, { priority }) => wakeupSession(...) */
|
|
98
|
+
const wakes = new Map();
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
function info(...args) { log?.info?.(...args); }
|
|
102
|
+
function warn(...args) { log?.warn?.(...args); }
|
|
103
|
+
|
|
104
|
+
async function roleOf(roleId) {
|
|
105
|
+
return persona?.loadRole ? await persona.loadRole(roleId).catch(() => null) : null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 该角色授权的 monsterparty 设备(未指定 deviceId 且有多个 → 需指定)。 */
|
|
109
|
+
async function authorizedDevice(role, kind, deviceId) {
|
|
110
|
+
if (!role?.persona) return null;
|
|
111
|
+
const owner = role.persona.owner;
|
|
112
|
+
const ids = Array.isArray(role.persona.devices) ? role.persona.devices : [];
|
|
113
|
+
const matching = [];
|
|
114
|
+
for (const id of ids) { const dev = await persona.loadDevice?.(id); if (dev?.owner === owner && dev.kind === kind) matching.push(id); }
|
|
115
|
+
const wanted = deviceId || (matching.length === 1 ? matching[0] : null);
|
|
116
|
+
if (!wanted || !ids.includes(wanted)) return null;
|
|
117
|
+
const dev = await persona.loadDevice?.(wanted).catch(() => null);
|
|
118
|
+
return dev && dev.owner === owner && dev.kind === kind ? dev : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ---------------- 状态机 ----------------
|
|
122
|
+
function runtime(roleId) {
|
|
123
|
+
let rt = runtimes.get(roleId);
|
|
124
|
+
if (!rt) {
|
|
125
|
+
rt = {
|
|
126
|
+
roleId,
|
|
127
|
+
deviceId: null,
|
|
128
|
+
activation: null,
|
|
129
|
+
conn: null,
|
|
130
|
+
controller: 'idle', // idle | tool | script
|
|
131
|
+
last: { intensity: 0, stroke: null },
|
|
132
|
+
phaseTimer: null,
|
|
133
|
+
phaseGeneration: 0,
|
|
134
|
+
script: null,
|
|
135
|
+
lastRemindAt: 0,
|
|
136
|
+
};
|
|
137
|
+
runtimes.set(roleId, rt);
|
|
138
|
+
}
|
|
139
|
+
return rt;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function clearPhase(rt) {
|
|
143
|
+
rt.phaseGeneration++;
|
|
144
|
+
if (rt.phaseTimer) { clearTimeout(rt.phaseTimer); rt.phaseTimer = null; }
|
|
145
|
+
}
|
|
146
|
+
async function stopConn(rt) {
|
|
147
|
+
const conn = rt.conn;
|
|
148
|
+
if (conn) {
|
|
149
|
+
try { conn.stop(); } catch { /* ignore */ }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function closeConn(rt) {
|
|
153
|
+
if (rt.conn) { try { rt.conn.close(); } catch { /* ignore */ } rt.conn = null; }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 建立连接(懒建)。失败返回错误对象。 */
|
|
157
|
+
async function ensureConn(rt) {
|
|
158
|
+
if (rt.conn) return null;
|
|
159
|
+
const act = rt.activation;
|
|
160
|
+
if (!act) return { error: '设备未激活', code: 'not-activated' };
|
|
161
|
+
const drv = drivers[act.kind];
|
|
162
|
+
if (!drv) return { error: `未知设备类型 ${act.kind}`, code: 'no-driver' };
|
|
163
|
+
const conn = new drv.Conn(act.session, { log });
|
|
164
|
+
try {
|
|
165
|
+
await conn.connect();
|
|
166
|
+
} catch (e) {
|
|
167
|
+
// 连接/加入失败(如 duplicate receiver / 链接已失效)→ 清 activation,要求重发链接
|
|
168
|
+
warn('mp connect fail role=%s: %s', rt.roleId, e.message);
|
|
169
|
+
activations.delete(rt.roleId);
|
|
170
|
+
rt.activation = null;
|
|
171
|
+
rt.deviceId = null;
|
|
172
|
+
return { error: `设备连接失败: ${e.message}(分享链接可能已失效,请重新发一条)`, code: 'expired' };
|
|
173
|
+
}
|
|
174
|
+
rt.conn = conn;
|
|
175
|
+
info('device conn ready role=%s device=%s', rt.roleId, act.alias);
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function rtState(rt) {
|
|
180
|
+
return {
|
|
181
|
+
deviceId: rt.deviceId,
|
|
182
|
+
alias: rt.activation?.alias ?? null,
|
|
183
|
+
activated: Boolean(rt.activation),
|
|
184
|
+
controller: rt.controller,
|
|
185
|
+
last: rt.last.intensity !== 0 || rt.last.stroke ? { ...rt.last } : null,
|
|
186
|
+
runId: rt.script?.runId ?? null,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** 提醒 dsh(10s 合并同类)。contextText 已是 [设备事件] 文本。 */
|
|
191
|
+
async function notify(rt, text, { priority = false } = {}) {
|
|
192
|
+
const wake = wakes.get(rt.roleId);
|
|
193
|
+
if (typeof wake !== 'function') return { ok: false, error: 'no-wake' };
|
|
194
|
+
const now = Date.now();
|
|
195
|
+
if (!priority && now - rt.lastRemindAt < REMIND_COALESCE_MS) return { ok: false, skipped: 'coalesced' };
|
|
196
|
+
rt.lastRemindAt = now;
|
|
197
|
+
try { await wake(text, { priority }); return { ok: true }; }
|
|
198
|
+
catch (e) { warn('device notify role=%s fail: %s', rt.roleId, e?.message ?? e); return { ok: false, error: String(e) }; }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 结束一段(工具计时到点 / 脚本结束):保持当前参数,提醒 dsh。 */
|
|
202
|
+
async function finishPhase(rt, { kind, detail, phaseGeneration }) {
|
|
203
|
+
// 被后续脚本/工具清除的旧计时,即使回调已经进入任务队列也不能再唤醒导演。
|
|
204
|
+
if (kind === 'tool' && phaseGeneration !== rt.phaseGeneration) return;
|
|
205
|
+
rt.phaseTimer = null;
|
|
206
|
+
rt.controller = 'idle'; // 设备仍在跑(conn 持续刷新),dsh 决定再调或停
|
|
207
|
+
const lastText = rt.last.stroke
|
|
208
|
+
? `强度 ${rt.last.intensity}、行程 ${rt.last.stroke.join(',')}`
|
|
209
|
+
: rt.last.intensity
|
|
210
|
+
? `强度 ${rt.last.intensity}`
|
|
211
|
+
: '已停';
|
|
212
|
+
const text = kind === 'script'
|
|
213
|
+
? `[设备事件] 你的操控脚本「${detail}」已结束,当前设备保持最后参数(${lastText})继续运行。若需要可再 toy_run_script 或 toy_control;要停用 toy_control 停。`
|
|
214
|
+
: `[设备事件] 本次 toy_control 计时结束,设备保持当前参数(${lastText})继续运行。可再次调用 toy_control 调整或停止。`;
|
|
215
|
+
await notify(rt, text, { priority: kind === 'script' });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ---------------- 对外 API ----------------
|
|
219
|
+
return {
|
|
220
|
+
registerRole(roleId, wake) { if (roleId && typeof wake === 'function') wakes.set(roleId, wake); },
|
|
221
|
+
unregisterRole(roleId) { wakes.delete(roleId); },
|
|
222
|
+
|
|
223
|
+
/** 文本里找设备分享链接。@returns { token, raw, kind } | null */
|
|
224
|
+
detectLink(text) {
|
|
225
|
+
const s = String(text ?? '');
|
|
226
|
+
for (const drv of Object.values(drivers)) {
|
|
227
|
+
const hit = drv.parseLink(s);
|
|
228
|
+
if (hit) return hit;
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* QQ 转发链接激活。@returns { ok:true, deviceId, alias } | { ok:false, error, code }
|
|
235
|
+
* code: no-device(未授权) / invalid(解析失败) / not-owner / ok
|
|
236
|
+
*/
|
|
237
|
+
async activate(roleId, token, { openid, deviceId } = {}) {
|
|
238
|
+
const role = await roleOf(roleId);
|
|
239
|
+
if (!role?.persona) return { ok: false, error: '角色不存在', code: 'no-role' };
|
|
240
|
+
ensureToyReference(role.workspace);
|
|
241
|
+
if (role.binding?.openid && openid && role.binding.openid !== openid)
|
|
242
|
+
return { ok: false, error: '非设备拥有者', code: 'not-owner' };
|
|
243
|
+
// 匹配该角色授权中的 monsterparty 设备(链接可能来自任一授权设备,单台场景直接命中)
|
|
244
|
+
const dev = await authorizedDevice(role, 'monsterparty', deviceId);
|
|
245
|
+
if (!dev) return { ok: false, error: '该角色未配置可用设备(请在管理台「我的设备」登记并为角色勾选)', code: 'no-device' };
|
|
246
|
+
const res = await drivers.monsterparty.resolveSession(token);
|
|
247
|
+
if (!res.ok) return { ok: false, error: res.error, code: 'invalid' };
|
|
248
|
+
// 若已激活/已连旧会话 → 断开旧 conn(等新 token 用于下次 apply)
|
|
249
|
+
const old = runtimes.get(roleId);
|
|
250
|
+
if (old) { clearPhase(old); old.script = null; await closeConn(old); }
|
|
251
|
+
activations.set(roleId, { deviceId: dev.id, alias: dev.alias, kind: dev.kind, token, session: res.session });
|
|
252
|
+
info('device activated role=%s device=%s', roleId, dev.alias);
|
|
253
|
+
return { ok: true, deviceId: dev.id, alias: dev.alias };
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
/** 当前角色授权设备 + 激活/运行状态(供 toy_status)。 */
|
|
257
|
+
async status(roleId) {
|
|
258
|
+
const role = await roleOf(roleId);
|
|
259
|
+
if (!role?.persona) return { ok: false, error: '角色不存在' };
|
|
260
|
+
const ids = Array.isArray(role.persona.devices) ? role.persona.devices : [];
|
|
261
|
+
const out = [];
|
|
262
|
+
for (const id of ids) {
|
|
263
|
+
const dev = await persona.loadDevice?.(id).catch(() => null);
|
|
264
|
+
if (!dev || dev.owner !== role.persona.owner || dev.kind !== 'monsterparty') continue;
|
|
265
|
+
const act = activations.get(roleId);
|
|
266
|
+
const rt = runtimes.get(roleId);
|
|
267
|
+
out.push({
|
|
268
|
+
deviceId: id, alias: dev.alias, kind: dev.kind,
|
|
269
|
+
activated: act?.deviceId === id,
|
|
270
|
+
controller: rt?.controller ?? 'idle',
|
|
271
|
+
last: rt && (rt.last.intensity !== 0 || rt.last.stroke) ? { ...rt.last } : null,
|
|
272
|
+
runId: rt?.script?.runId ?? null,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return { ok: true, devices: out };
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 统一参数入口。params: { roleId, deviceId?, intensity?, stroke?, seconds?, stop?, fromScript? }。
|
|
280
|
+
* fromScript=true 表示调用来自运行中的 toy 脚本自身(经 /toycmd):不得杀自己/设计时,只落参数。
|
|
281
|
+
* seconds>0:定时,到点保持参数并提醒;stop:停(清计时/脚本,强度归零)。
|
|
282
|
+
*/
|
|
283
|
+
async applyFromTool(params) {
|
|
284
|
+
const { roleId, fromScript } = params;
|
|
285
|
+
if (!roleId) return { ok: false, error: '缺少 roleId' };
|
|
286
|
+
const role = await roleOf(roleId);
|
|
287
|
+
if (!role?.persona) return { ok: false, error: '角色不存在', code: 'no-role' };
|
|
288
|
+
const dev = await authorizedDevice(role, 'monsterparty', params.deviceId);
|
|
289
|
+
if (!dev) return { ok: false, error: '该角色未配置可用设备', code: 'no-device' };
|
|
290
|
+
const rt = runtime(roleId);
|
|
291
|
+
|
|
292
|
+
// stop:停当前(脚本自身 stop 不杀自己,只停设备;dsh 工具 stop 清计时+杀脚本)
|
|
293
|
+
if (params.stop === true) {
|
|
294
|
+
const stoppedScript = !fromScript ? rt.script : null;
|
|
295
|
+
if (!fromScript) { clearPhase(rt); rt.script = null; }
|
|
296
|
+
await stopConn(rt);
|
|
297
|
+
rt.last = { intensity: 0, stroke: null };
|
|
298
|
+
if (!fromScript) rt.controller = 'idle';
|
|
299
|
+
if (stoppedScript) {
|
|
300
|
+
await notify(rt, `[设备事件] 玩具操控脚本「${basename(stoppedScript.script)}」已被 toy_control stop 终止,设备也已停止。`,
|
|
301
|
+
{ priority: true });
|
|
302
|
+
}
|
|
303
|
+
return { ok: true, state: rtState(rt) };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 玩具脚本拥有排他的控制权。普通工具调用不得暗中杀掉脚本;导演必须先显式终止脚本。
|
|
307
|
+
if (!fromScript && rt.script) {
|
|
308
|
+
return { ok: false, code: 'script-active',
|
|
309
|
+
error: `玩具脚本「${basename(rt.script.script)}」正在运行(runId=${rt.script.runId});请先用 toy_run_script stop=${rt.script.runId} 终止脚本,再调用 toy_control。` };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// 激活检查:必须是这台授权设备
|
|
313
|
+
const act = activations.get(roleId);
|
|
314
|
+
if (!act || act.deviceId !== dev.id) {
|
|
315
|
+
return {
|
|
316
|
+
ok: false, code: 'not-activated', error: `设备「${dev.alias}」尚未激活——请让 ta 在 QQ 发一条新鲜的 MonsterParty 分享链接。`,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
rt.activation = act;
|
|
320
|
+
rt.deviceId = dev.id;
|
|
321
|
+
|
|
322
|
+
// 普通工具的新计时可替换旧计时;脚本自身调用始终保持 script 控制者。
|
|
323
|
+
if (!fromScript && rt.phaseTimer) clearPhase(rt);
|
|
324
|
+
const err = await ensureConn(rt);
|
|
325
|
+
if (err) { rt.activation = null; activations.delete(roleId); return { ok: false, ...err }; }
|
|
326
|
+
|
|
327
|
+
const cap = drivers.monsterparty.capability;
|
|
328
|
+
// 参数归一
|
|
329
|
+
if (params.intensity !== undefined) {
|
|
330
|
+
const v = Math.max(cap.intensity.min, Math.min(cap.intensity.max, Math.round(Number(params.intensity))));
|
|
331
|
+
if (!Number.isFinite(v)) return { ok: false, error: '强度须为数字', code: 'bad-params' };
|
|
332
|
+
rt.last.intensity = v;
|
|
333
|
+
}
|
|
334
|
+
if (params.stroke !== undefined) {
|
|
335
|
+
const ab = normalizeStroke(params.stroke, cap);
|
|
336
|
+
if (!ab) return { ok: false, error: `行程格式无效(如 "0,66"/"L1 L2"/"0,0"): ${params.stroke}`, code: 'bad-params' };
|
|
337
|
+
rt.last.stroke = ab;
|
|
338
|
+
}
|
|
339
|
+
// 落设备:行程变更发 segment;强度>0 持续 slider;强度=0 停止
|
|
340
|
+
if (rt.last.stroke) rt.conn.sendStroke(rt.last.stroke[0], rt.last.stroke[1]);
|
|
341
|
+
rt.conn.setIntensity(rt.last.intensity);
|
|
342
|
+
|
|
343
|
+
// 脚本自身调用只落参数(节奏由脚本 toy.sleep 控制),dsh 定时模式才设计时
|
|
344
|
+
if (fromScript) return { ok: true, state: rtState(rt) };
|
|
345
|
+
if (Number(params.seconds) > 0) {
|
|
346
|
+
clearPhase(rt);
|
|
347
|
+
const ms = Math.round(Number(params.seconds) * 1000);
|
|
348
|
+
rt.controller = 'tool';
|
|
349
|
+
const phaseGeneration = rt.phaseGeneration;
|
|
350
|
+
rt.phaseTimer = setTimeout(() => { void finishPhase(rt, { kind: 'tool', phaseGeneration }); }, ms);
|
|
351
|
+
rt.phaseTimer.unref?.();
|
|
352
|
+
} else {
|
|
353
|
+
rt.controller = 'tool';
|
|
354
|
+
}
|
|
355
|
+
return { ok: true, state: rtState(rt) };
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
/** 通用脚本运行器的雷电专属生命周期处理。 */
|
|
359
|
+
beginScript({ roleId, workspace, runId, script }) {
|
|
360
|
+
ensureToyReference(workspace);
|
|
361
|
+
const rt = runtime(roleId); clearPhase(rt);
|
|
362
|
+
rt.script = { runId, script }; rt.controller = 'script';
|
|
363
|
+
},
|
|
364
|
+
async endScript({ roleId, script, reason }) {
|
|
365
|
+
const rt = runtime(roleId); rt.script = null; rt.controller = 'idle';
|
|
366
|
+
if (reason !== 'control-stop' && reason !== 'disposed') await finishPhase(rt, { kind: 'script', detail: basename(script) });
|
|
367
|
+
},
|
|
368
|
+
async controlStoppedScript({ roleId, script }) {
|
|
369
|
+
return notify(runtime(roleId), `[设备事件] 玩具操控脚本「${basename(script)}」已被 toy_control stop 终止,设备也已停止。`, { priority: true });
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
/** 角色删除/设备摘除时清理该角色的运行态。 */
|
|
373
|
+
disposeRole: async (roleId) => {
|
|
374
|
+
const rt = runtimes.get(roleId);
|
|
375
|
+
if (rt) { clearPhase(rt); rt.script = null; await closeConn(rt); }
|
|
376
|
+
activations.delete(roleId);
|
|
377
|
+
runtimes.delete(roleId);
|
|
378
|
+
wakes.delete(roleId);
|
|
379
|
+
},
|
|
380
|
+
|
|
381
|
+
/** 插件 teardown:断开全部 WS、终止脚本。 */
|
|
382
|
+
async disposeAll() {
|
|
383
|
+
for (const roleId of [...runtimes.keys()]) {
|
|
384
|
+
const rt = runtimes.get(roleId);
|
|
385
|
+
if (rt) { clearPhase(rt); rt.script = null; await closeConn(rt); }
|
|
386
|
+
activations.delete(roleId);
|
|
387
|
+
runtimes.delete(roleId);
|
|
388
|
+
wakes.delete(roleId);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
}
|
|
@@ -1,71 +1,60 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
lines.push('-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
lines.push('
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
lines.push('');
|
|
54
|
-
lines.push('
|
|
55
|
-
lines.push('-
|
|
56
|
-
lines.push('');
|
|
57
|
-
lines.push('
|
|
58
|
-
lines.push('
|
|
59
|
-
lines.
|
|
60
|
-
|
|
61
|
-
lines.push('');
|
|
62
|
-
lines.push('### 脚本 toy_run_script');
|
|
63
|
-
lines.push('- 在编写或运行前,先读取 workspace/toys/玩具脚本示例.mjs;设备激活时插件会将此参考文件放入 toys/。再在 workspace/toys/ 写自己的 .mjs 脚本,并用 toy_run_script 运行。');
|
|
64
|
-
lines.push('- 脚本里只关心参数:await toy.set({ intensity, stroke }) 改变强度/行程、toy.sleep(毫秒) 控制节奏、toy.stop() 停止;');
|
|
65
|
-
lines.push('- 启动脚本会清除尚未到期的 toy_control 计时提醒,避免旧提醒与脚本撞车;');
|
|
66
|
-
lines.push('- 脚本自然结束即终止,**结束时设备保持最后的参数继续运行、不中断**,系统会提醒你可续可停;');
|
|
67
|
-
lines.push('- 每个玩具脚本都必须在其自行判断的合适位置调用 await toy.wake({ mood, topic, reason }),让你感知脚本中的变化并决定是否开口;');
|
|
68
|
-
lines.push('- 脚本无需也不可接触任何通信细节。');
|
|
69
|
-
lines.push('');
|
|
70
|
-
return lines.join('\n');
|
|
71
|
-
}
|
|
1
|
+
/** MonsterParty 雷电专属说明;由对应设备类型选择,内容保持原样。 */
|
|
2
|
+
import { MONSTERPARTY_CAPABILITY } from './monsterparty.mjs';
|
|
3
|
+
|
|
4
|
+
export function monsterpartyDocMarkdown(device) {
|
|
5
|
+
const alias = device?.alias || '未命名设备';
|
|
6
|
+
const cap = device?.kind === 'monsterparty' ? MONSTERPARTY_CAPABILITY : null;
|
|
7
|
+
const lines = [];
|
|
8
|
+
if (!cap) {
|
|
9
|
+
lines.push(`# 设备:${alias}`, '', '> 未知设备类型,暂不可操控。');
|
|
10
|
+
return lines.join('\n');
|
|
11
|
+
}
|
|
12
|
+
lines.push(`# 可用设备:${alias}`);
|
|
13
|
+
lines.push('');
|
|
14
|
+
lines.push(`型号:${cap.label}(${cap.kind})`);
|
|
15
|
+
if (cap.noAutoStop) lines.push('特性:无自动停止——何时停由你判断;用户随时可以要求停(届时你应停止)或自己手动停。');
|
|
16
|
+
lines.push('');
|
|
17
|
+
lines.push('## 如何开始');
|
|
18
|
+
lines.push('- 让用户在 QQ 把 MonsterParty 的**新鲜分享链接**发给角色,即可自动激活本设备;');
|
|
19
|
+
lines.push('- **每次想使用都需要用户发一条新的分享链接**(旧链接单次有效,连一次即失效);未激活时操控工具会返回提示。');
|
|
20
|
+
lines.push('');
|
|
21
|
+
lines.push('## 能力范围');
|
|
22
|
+
if (cap.intensity) lines.push(`- 强度:intensity 0-${cap.intensity.max}(0=停,数值越大越强/越快)`);
|
|
23
|
+
if (cap.stroke) {
|
|
24
|
+
lines.push('- 行程:stroke 为两个边界 A,B,取值 0/33/66/100(每个代表行程轴 1/4 的位置):');
|
|
25
|
+
for (const [zone, [lo, hi]] of Object.entries(cap.stroke.zones ?? {})) {
|
|
26
|
+
lines.push(` - ${zone} = ${lo}~${hi}(${zone === 'L1' ? '最深' : zone === 'L3' ? '几乎拔出' : '中段'})`);
|
|
27
|
+
}
|
|
28
|
+
lines.push(' 例:"0,100"=L1~L3 全行程往复;"0,66"=L1~L2 区间;"0,0"=停住不动。');
|
|
29
|
+
}
|
|
30
|
+
if (cap.kind === 'monsterparty') {
|
|
31
|
+
lines.push('', '## 体感(MonsterParty 雷电实测)');
|
|
32
|
+
lines.push('- 强度:intensity 0~40 偏弱、30 以下动作明显变慢;40~70 属中等偏强;**70~100 是用户常用的区间(偏好强而快)**;');
|
|
33
|
+
lines.push(' 越往上越强越快,100 最强。**设备本身安全、100 不会造成伤害**——强弱是"感受"差异,应按当下情境与用户的反馈选;');
|
|
34
|
+
lines.push(' 用户说轻一点/停,就必须轻/停。');
|
|
35
|
+
lines.push('- 行程区间体感(同一强度下,区间越短频率越快、幅度越小):');
|
|
36
|
+
lines.push(' - "0,66":幅度适宜、有包裹感,行程较短,套弄频率比 "0,100" 快——常用、舒服');
|
|
37
|
+
lines.push(' - "0,100":几乎完全拔出再大力坐下,大开大合;幅度最大,频率受行程限制、没法很快——猛/尽兴/要强烈时用');
|
|
38
|
+
lines.push(' - "0,33":行程很短、频率快但幅度小,快感不明显(试探/轻柔可用)');
|
|
39
|
+
lines.push(' - "66,100":在顶端(接近拔出)附近来回套弄');
|
|
40
|
+
lines.push(' - 其余按此推,如 "33,100"=中段偏上到拔出、"0,66" 附近舒适区;可结合强度调出 快而轻/缓而深/快而猛 等质感。');
|
|
41
|
+
}
|
|
42
|
+
lines.push('');
|
|
43
|
+
lines.push('## 玩具操控');
|
|
44
|
+
lines.push('- 能使用则使用脚本控制玩具。脚本通过构建玩具运动的“波形”来控制体验:不是随机抽取参数,也不是把若干参数和固定时长简单串成阶段表。具体波形由你根据当下情境自行设计。');
|
|
45
|
+
lines.push('');
|
|
46
|
+
lines.push('### 定时调用工具 toy_control');
|
|
47
|
+
lines.push('- 参数:intensity(强度)、stroke(行程范围)、seconds(本次时长的秒数)。');
|
|
48
|
+
lines.push('- 到点后设备保持当前参数继续运行,系统会提醒你计时结束,可再次调用调整或 toy_control 停。');
|
|
49
|
+
lines.push('- 玩具脚本运行时,toy_control 不会直接接管;先用 toy_run_script 的 stop 明确终止脚本,再调用 toy_control。');
|
|
50
|
+
lines.push('');
|
|
51
|
+
lines.push('### 脚本 toy_run_script');
|
|
52
|
+
lines.push('- 在编写或运行前,先读取 workspace/toys/玩具脚本示例.mjs;设备激活时插件会将此参考文件放入 toys/。再在 workspace/toys/ 写自己的 .mjs 脚本,并用 toy_run_script 运行。');
|
|
53
|
+
lines.push('- 脚本里只关心参数:await toy.set({ intensity, stroke }) 改变强度/行程、toy.sleep(毫秒) 控制节奏、toy.stop() 停止;');
|
|
54
|
+
lines.push('- 启动脚本会清除尚未到期的 toy_control 计时提醒,避免旧提醒与脚本撞车;');
|
|
55
|
+
lines.push('- 脚本自然结束即终止,**结束时设备保持最后的参数继续运行、不中断**,系统会提醒你可续可停;');
|
|
56
|
+
lines.push('- 每个玩具脚本都必须在其自行判断的合适位置调用 await toy.wake({ mood, topic, reason }),让你感知脚本中的变化并决定是否开口;');
|
|
57
|
+
lines.push('- 脚本无需也不可接触任何通信细节。');
|
|
58
|
+
lines.push('');
|
|
59
|
+
return lines.join('\n');
|
|
60
|
+
}
|