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
package/src/web-server.js
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* GET /api/me/roles/<roleId> → 完整角色(persona/model/proactive/binding/bot/脚本状态)
|
|
21
21
|
* PUT /api/me/roles/<roleId> { persona?, model?, proactive? }
|
|
22
22
|
* DELETE /api/me/roles/<roleId> → 级联删除该角色的机器人
|
|
23
|
-
* PUT /api/me/roles/<roleId>/bot { qqAppId, qqSecret?, sandbox?, withinChatWindowMs?, streaming?, enabled? }
|
|
23
|
+
* PUT /api/me/roles/<roleId>/bot { qqAppId, qqSecret?, sandbox?, withinChatWindowMs?, streaming?, enabled? }
|
|
24
24
|
* DELETE /api/me/roles/<roleId>/bot
|
|
25
25
|
* POST /api/me/roles/<roleId>/reset-binding
|
|
26
26
|
* GET /api/me/roles/<roleId>/life → 脚本监管:manifest + logs + backups + running 清单
|
|
@@ -29,20 +29,21 @@
|
|
|
29
29
|
* POST /api/me/roles/<roleId>/life/stop → 终止一个运行中脚本(body: { runId })
|
|
30
30
|
*/
|
|
31
31
|
import { createServer } from 'node:http';
|
|
32
|
-
import { readFile, readdir } from 'node:fs/promises';
|
|
32
|
+
import { readFile, readdir } from 'node:fs/promises';
|
|
33
33
|
import { existsSync } from 'node:fs';
|
|
34
34
|
import { join, extname } from 'node:path';
|
|
35
35
|
import { fileURLToPath } from 'node:url';
|
|
36
36
|
import path from 'node:path';
|
|
37
|
-
import { listLifeScripts, listRunningLifeScripts, stopLifeScript, stopLifeScripts, recoverLifeScripts
|
|
38
|
-
import { roleUsesCustomProvider, roleUsesCustomSubagentApi } from './llm-provider.js';
|
|
39
|
-
import {
|
|
40
|
-
import {
|
|
37
|
+
import { listLifeScripts, listRunningLifeScripts, stopLifeScript, stopLifeScripts, recoverLifeScripts } from './agent-life.js';
|
|
38
|
+
import { roleUsesCustomProvider, roleUsesCustomSubagentApi, roleUsesCustomRoleplayApi } from './llm-provider.js';
|
|
39
|
+
import { normalizeRoleplayConfig, publicRoleplayConfig, validateEnabledRoleplay, RoleplayConfigError, roleplayConfigSignature } from './roleplay-config.js';
|
|
40
|
+
import { listMemes, saveMeme, updateMeme, deleteMeme, memeFileFor } from './memebox.js';
|
|
41
|
+
import { createBundledVoiceServices } from './voice-defaults.js';
|
|
41
42
|
|
|
42
43
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
43
44
|
const PUBLIC_DIR = join(__dirname, 'web-public');
|
|
44
45
|
const COOKIE_NAME = 'pygmalion_sid';
|
|
45
|
-
const BODY_LIMIT = 12 * 1024 * 1024;
|
|
46
|
+
const BODY_LIMIT = 12 * 1024 * 1024;
|
|
46
47
|
const LOGIN_MAX_FAILS = 5;
|
|
47
48
|
const LOGIN_LOCK_MS = 15 * 60 * 1000;
|
|
48
49
|
const MIME = {
|
|
@@ -53,6 +54,9 @@ const MIME = {
|
|
|
53
54
|
|
|
54
55
|
export function createWebServer(deps) {
|
|
55
56
|
const { persona, log } = deps;
|
|
57
|
+
const bundledVoice = deps.voice ? null : createBundledVoiceServices({ persona });
|
|
58
|
+
const voice = deps.voice ?? bundledVoice.voice;
|
|
59
|
+
const extensions = deps.extensions ?? bundledVoice?.extensions;
|
|
56
60
|
// DeviceManager(index.js 注入;独立运行/无宿主时可缺省,设备路由照常、运行态清理跳过)。
|
|
57
61
|
const devices = deps.devices;
|
|
58
62
|
// 自定义 LLM provider 注册服务(index.js 注入;缺省时保存模型不注册,功能照旧)。
|
|
@@ -156,6 +160,7 @@ export function createWebServer(deps) {
|
|
|
156
160
|
return {
|
|
157
161
|
configured: true,
|
|
158
162
|
botId: bot.botId,
|
|
163
|
+
adapterId: bot.adapterId, config: bot.config, secrets: bot.secrets,
|
|
159
164
|
qqAppId: bot.qqAppId,
|
|
160
165
|
sandbox: bot.sandbox,
|
|
161
166
|
withinChatWindowMs: bot.withinChatWindowMs,
|
|
@@ -167,19 +172,26 @@ export function createWebServer(deps) {
|
|
|
167
172
|
status: bot.status,
|
|
168
173
|
};
|
|
169
174
|
}
|
|
170
|
-
async function publicRole(role) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
+
async function publicRole(role) {
|
|
176
|
+
const bot = roleBot(role.roleId);
|
|
177
|
+
let generationStatus = {status:role.roleplay?.enabled?'normal':'disabled'};
|
|
178
|
+
if (role.roleplay?.enabled) {
|
|
179
|
+
try {
|
|
180
|
+
const saved=JSON.parse(await readFile(join(persona.roleDir(role.roleId),'roleplay-runtime.json'),'utf8'));
|
|
181
|
+
if(saved.signature===roleplayConfigSignature(role.roleplay)&&['normal','retrying','fallback','retryable_error'].includes(saved.status))
|
|
182
|
+
generationStatus={status:saved.status,...(/^[A-Z_]+$/.test(saved.code??'')?{code:saved.code}:{})};
|
|
183
|
+
} catch { /* 尚未生成,无运行记录 */ }
|
|
184
|
+
}
|
|
175
185
|
return {
|
|
176
186
|
roleId: role.roleId,
|
|
177
187
|
persona: role.persona,
|
|
178
188
|
model: role.model,
|
|
189
|
+
roleplay: publicRoleplayConfig(role.roleplay),
|
|
190
|
+
roleplayStatus: generationStatus,
|
|
179
191
|
proactive: role.proactive,
|
|
180
|
-
binding: role.binding,
|
|
181
|
-
voice:
|
|
182
|
-
bot,
|
|
192
|
+
binding: role.binding,
|
|
193
|
+
voice: await voice.view(role.roleId),
|
|
194
|
+
bot,
|
|
183
195
|
life: {
|
|
184
196
|
hasScript: (await listLifeScripts(role.workspace)).length > 0,
|
|
185
197
|
runningCount: listRunningLifeScripts(role.workspace).length,
|
|
@@ -189,8 +201,6 @@ export function createWebServer(deps) {
|
|
|
189
201
|
async function lifeStatus(roleId) {
|
|
190
202
|
const role = await persona.loadRole(roleId);
|
|
191
203
|
if (!role) return null;
|
|
192
|
-
await recoverLifeScripts(role.workspace);
|
|
193
|
-
await pruneDeadLifeScripts(role.workspace);
|
|
194
204
|
const lifeDir = join(role.workspace, 'life');
|
|
195
205
|
const manifest = await readFile(join(lifeDir, '.manifest.json'), 'utf8').then(JSON.parse).catch(() => ({}));
|
|
196
206
|
const logs = await readdir(join(lifeDir, '.logs')).then((a) => a.sort().reverse()).catch(() => []);
|
|
@@ -202,17 +212,22 @@ export function createWebServer(deps) {
|
|
|
202
212
|
const role = await persona.loadRole(roleId);
|
|
203
213
|
if (!role) return null;
|
|
204
214
|
const toysDir = join(role.workspace, 'toys');
|
|
205
|
-
const scripts =
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
.filter((
|
|
209
|
-
.map((
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
const scripts = [], logs = [];
|
|
216
|
+
for (const directory of ['toys', 'device-scripts']) {
|
|
217
|
+
const prefix = directory === 'toys' ? '' : '@devices/';
|
|
218
|
+
scripts.push(...await readdir(join(role.workspace, directory)).then(a => a.filter(n => /\.(mjs|js|ts)$/.test(n)).sort().map(n => prefix + n)).catch(() => []));
|
|
219
|
+
logs.push(...await readdir(join(role.workspace, directory, '.logs')).then(a => a.sort().reverse().map(n => prefix + n)).catch(() => []));
|
|
220
|
+
}
|
|
221
|
+
const running = (typeof devices?.listScriptRuns === 'function' ? devices.listScriptRuns() : [])
|
|
222
|
+
.filter((r) => r.roleId === roleId)
|
|
223
|
+
.map((r) => ({
|
|
224
|
+
runId: r.runId,
|
|
225
|
+
deviceId: r.deviceId, driverId: r.driverId,
|
|
226
|
+
scriptName: (r.directory === 'device-scripts' ? '@devices/' : '') + r.scriptName,
|
|
227
|
+
pid: r.pid,
|
|
228
|
+
startedAt: r.startedAt,
|
|
229
|
+
logName: (r.directory === 'device-scripts' ? '@devices/' : '') + r.logName,
|
|
230
|
+
}));
|
|
216
231
|
return { scripts, logs: logs.slice(0, 50), running };
|
|
217
232
|
}
|
|
218
233
|
/** 安全化角色 id 参数(防路径穿越)。 */
|
|
@@ -220,12 +235,12 @@ export function createWebServer(deps) {
|
|
|
220
235
|
const rid = String(raw ?? '');
|
|
221
236
|
return /^[A-Za-z0-9._-]+$/.test(rid) ? rid : null;
|
|
222
237
|
}
|
|
223
|
-
function logFileNameParam(raw) {
|
|
224
|
-
const n = String(raw ?? '');
|
|
225
|
-
// 日志以“脚本名-时间戳.log”命名;脚本名允许中文,故不能只接受 ASCII \w。
|
|
226
|
-
// 仍拒绝路径分隔、控制字符和 Windows 非法文件名字符,防止路径穿越。
|
|
227
|
-
return /^[^/\\\x00-\x1f<>:"|?*]+\.log$/.test(n) && n !== '..' ? n : null;
|
|
228
|
-
}
|
|
238
|
+
function logFileNameParam(raw) {
|
|
239
|
+
const n = String(raw ?? '');
|
|
240
|
+
// 日志以“脚本名-时间戳.log”命名;脚本名允许中文,故不能只接受 ASCII \w。
|
|
241
|
+
// 仍拒绝路径分隔、控制字符和 Windows 非法文件名字符,防止路径穿越。
|
|
242
|
+
return /^[^/\\\x00-\x1f<>:"|?*]+\.log$/.test(n) && n !== '..' ? n : null;
|
|
243
|
+
}
|
|
229
244
|
function scriptFileNameParam(raw) {
|
|
230
245
|
const n = String(raw ?? '');
|
|
231
246
|
// 排除法:允许中文等任意文件名,只禁路径分隔与 Windows 非法字符(防穿越)
|
|
@@ -275,30 +290,43 @@ export function createWebServer(deps) {
|
|
|
275
290
|
* model 保存时去掉空值键(清空的字段等同不配置);provider 留空由 providerRoute
|
|
276
291
|
* 自动按 apiUrl 派生。返回聚合后的最新 role。
|
|
277
292
|
*/
|
|
293
|
+
async function resolveRoleplayPatch(rid, patch) {
|
|
294
|
+
if (patch === undefined) return undefined;
|
|
295
|
+
if (!patch || typeof patch !== 'object' || Array.isArray(patch)) throw new RoleplayConfigError('角色生成配置无效');
|
|
296
|
+
const previous = rid ? (await persona.loadRole(rid))?.roleplay ?? {} : {};
|
|
297
|
+
const requested = {...patch};
|
|
298
|
+
if (requested.apiKey === '') delete requested.apiKey;
|
|
299
|
+
if (requested.apiKey === null) requested.apiKey = '';
|
|
300
|
+
const roleplay = normalizeRoleplayConfig({...previous,...requested});
|
|
301
|
+
validateEnabledRoleplay(roleplay); return roleplay;
|
|
302
|
+
}
|
|
278
303
|
async function applyModelProactive(rid, patch) {
|
|
304
|
+
// 在写入其他字段前校验;空密钥输入保留既有配置,null 显式清除。
|
|
305
|
+
const roleplay = await resolveRoleplayPatch(rid,patch.roleplay);
|
|
279
306
|
if (patch.model !== undefined) {
|
|
280
307
|
await persona.saveRoleField(rid, 'model', Object.fromEntries(Object.entries(patch.model ?? {}).filter(([, v]) => String(v ?? '').trim())));
|
|
281
|
-
}
|
|
282
|
-
if (patch.proactive !== undefined) {
|
|
283
|
-
const proactive = patch.proactive ?? {};
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
...
|
|
287
|
-
|
|
288
|
-
memeCollection: {
|
|
289
|
-
...persona.DEFAULT_PROACTIVE.memeCollection,
|
|
290
|
-
...(proactive.memeCollection ?? {}),
|
|
291
|
-
vision: { ...persona.DEFAULT_PROACTIVE.memeCollection.vision, ...(proactive.memeCollection?.vision ?? {}) },
|
|
292
|
-
},
|
|
293
|
-
});
|
|
294
|
-
}
|
|
308
|
+
}
|
|
309
|
+
if (patch.proactive !== undefined) {
|
|
310
|
+
const proactive = { ...(patch.proactive ?? {}) };
|
|
311
|
+
delete proactive.messageReview; // 忽略旧版客户端提交的已移除配置。
|
|
312
|
+
await persona.saveRoleField(rid, 'proactive', {
|
|
313
|
+
...persona.DEFAULT_PROACTIVE,
|
|
314
|
+
...proactive,
|
|
315
|
+
memeCollection: {
|
|
316
|
+
...persona.DEFAULT_PROACTIVE.memeCollection,
|
|
317
|
+
...(proactive.memeCollection ?? {}),
|
|
318
|
+
vision: { ...persona.DEFAULT_PROACTIVE.memeCollection.vision, ...(proactive.memeCollection?.vision ?? {}) },
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
if (roleplay) await persona.saveRoleField(rid,'roleplay',roleplay);
|
|
295
323
|
const updated = await persona.loadRole(rid);
|
|
296
|
-
await persona.syncRoleMemory(updated);
|
|
324
|
+
await persona.syncRoleMemory(updated, { renderDevice: devices?.document });
|
|
297
325
|
if (llmProviders) {
|
|
298
326
|
try {
|
|
299
327
|
// 主 provider 与 subagent 视觉 API 任一为自定义都要注册(视觉 route 独立,
|
|
300
328
|
// 只配视觉 API 的角色主 model 走内置,但 pygmalion-meme-<roleId> 必须注册)。
|
|
301
|
-
if (roleUsesCustomProvider(updated) || roleUsesCustomSubagentApi(updated)) await llmProviders.register(updated);
|
|
329
|
+
if (roleUsesCustomProvider(updated) || roleUsesCustomSubagentApi(updated) || roleUsesCustomRoleplayApi(updated)) await llmProviders.register(updated);
|
|
302
330
|
else await llmProviders.unregister(updated);
|
|
303
331
|
} catch (e) { log?.warn?.('pygmalion: 同步自定义 provider %s 失败 %o', rid, e); }
|
|
304
332
|
}
|
|
@@ -364,6 +392,9 @@ export function createWebServer(deps) {
|
|
|
364
392
|
// ---- 业务(需登录)----
|
|
365
393
|
const me = await requireAccount(req, res);
|
|
366
394
|
if (!me) return;
|
|
395
|
+
if (pathname === '/api/me/tts-providers' && req.method === 'GET') {
|
|
396
|
+
sendJson(res, 200, { ok: true, providers: extensions?.listTtsProviders() ?? [] }); return;
|
|
397
|
+
}
|
|
367
398
|
|
|
368
399
|
function publicMe(a) {
|
|
369
400
|
return {
|
|
@@ -406,6 +437,7 @@ export function createWebServer(deps) {
|
|
|
406
437
|
if (!personaFields.name) { sendJson(res, 400, { ok: false, error: '请填写角色名' }); return; }
|
|
407
438
|
const bad = await checkDevicesOwned(personaFields, me);
|
|
408
439
|
if (bad) { sendJson(res, 400, { ok: false, error: `设备 ${bad} 不归你所有` }); return; }
|
|
440
|
+
await resolveRoleplayPatch(undefined,b?.roleplay);
|
|
409
441
|
const roleId = await persona.createRole({ accountId: me.qqNumber, persona: personaFields });
|
|
410
442
|
// 首次保存同样落 model/proactive(修:新建角色一次保存即生效,而非只有再编辑才存)。
|
|
411
443
|
const role = await applyModelProactive(roleId, b ?? {});
|
|
@@ -414,21 +446,38 @@ export function createWebServer(deps) {
|
|
|
414
446
|
}
|
|
415
447
|
|
|
416
448
|
// ---- 我的设备库(用户级,角色经 persona.devices 勾选)----
|
|
449
|
+
if (pathname === '/api/me/channel-adapters' && req.method === 'GET') {
|
|
450
|
+
sendJson(res, 200, { ok:true, adapters:extensions?.listChannelAdapters()??[] }); return;
|
|
451
|
+
}
|
|
452
|
+
if (pathname === '/api/me/device-drivers' && req.method === 'GET') {
|
|
453
|
+
sendJson(res, 200, { ok: true, drivers: devices?.listDrivers() ?? [] }); return;
|
|
454
|
+
}
|
|
417
455
|
if (pathname === '/api/me/devices' && req.method === 'GET') {
|
|
418
456
|
const list = await persona.listDevices(me.qqNumber);
|
|
419
|
-
sendJson(res, 200, { ok: true, devices: list });
|
|
457
|
+
sendJson(res, 200, { ok: true, devices: list.map(item => devices?.publicDevice(item) ?? { ...item, config: {} }) });
|
|
420
458
|
return;
|
|
421
459
|
}
|
|
422
460
|
if (pathname === '/api/me/devices' && req.method === 'POST') {
|
|
423
461
|
const b = await readBody(req);
|
|
424
462
|
const alias = String(b?.alias ?? '').trim();
|
|
425
463
|
if (!alias) { sendJson(res, 400, { ok: false, error: '请填写设备别名' }); return; }
|
|
426
|
-
const
|
|
427
|
-
const
|
|
428
|
-
|
|
464
|
+
const drivers = devices?.listDrivers() ?? [];
|
|
465
|
+
const kind = b?.kind ?? (drivers.length === 1 ? drivers[0].id : undefined);
|
|
466
|
+
let config;
|
|
467
|
+
try { config = devices.deviceConfig(kind, b?.config ?? {}); }
|
|
468
|
+
catch (error) { sendJson(res, 400, { ok: false, error: error.message }); return; }
|
|
469
|
+
const device = await persona.createDevice({ owner: me.qqNumber, alias, kind, config });
|
|
470
|
+
sendJson(res, 200, { ok: true, device: devices.publicDevice(device) });
|
|
429
471
|
return;
|
|
430
472
|
}
|
|
431
473
|
const pDev = /^\/api\/me\/devices\/([^/]+)$/.exec(pathname);
|
|
474
|
+
if (pDev && req.method === 'PUT') {
|
|
475
|
+
const dev = await persona.loadDevice(pDev[1]);
|
|
476
|
+
if (!dev || dev.owner !== me.qqNumber) { sendJson(res, 404, { ok: false, error: '设备不存在' }); return; }
|
|
477
|
+
try { const device = await devices.updateDevice(me.qqNumber, dev.id, await readBody(req)); sendJson(res, 200, { ok: true, device }); }
|
|
478
|
+
catch (error) { sendJson(res, 400, { ok: false, error: error.message }); }
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
432
481
|
if (pDev && req.method === 'DELETE') {
|
|
433
482
|
const did = /^[A-Za-z0-9._-]+$/.test(pDev[1]) ? pDev[1] : null;
|
|
434
483
|
if (!did) { sendJson(res, 400, { ok: false, error: '设备 ID 非法' }); return; }
|
|
@@ -440,7 +489,7 @@ export function createWebServer(deps) {
|
|
|
440
489
|
try { await devices?.disposeRole?.(rid); } catch { /* ignore */ }
|
|
441
490
|
try {
|
|
442
491
|
const r = await persona.loadRole(rid);
|
|
443
|
-
if (r) await persona.syncRoleMemory(r);
|
|
492
|
+
if (r) await persona.syncRoleMemory(r, { renderDevice: devices?.document });
|
|
444
493
|
} catch { /* ignore */ }
|
|
445
494
|
}
|
|
446
495
|
}
|
|
@@ -458,24 +507,28 @@ export function createWebServer(deps) {
|
|
|
458
507
|
if (req.method === 'PUT') {
|
|
459
508
|
const b = await readBody(req);
|
|
460
509
|
const patch = b ?? {};
|
|
510
|
+
await resolveRoleplayPatch(rid,patch.roleplay);
|
|
461
511
|
if (patch.persona !== undefined) {
|
|
462
512
|
const p = normalizePersona({ persona: patch.persona });
|
|
463
513
|
const bad = await checkDevicesOwned(p, me);
|
|
464
514
|
if (bad) { sendJson(res, 400, { ok: false, error: `设备 ${bad} 不归你所有` }); return; }
|
|
465
515
|
p.owner = me.qqNumber;
|
|
466
516
|
await persona.saveRoleField(rid, 'persona', p);
|
|
517
|
+
if (JSON.stringify(role.persona?.devices ?? []) !== JSON.stringify(p.devices ?? [])) await devices?.disposeRole?.(rid);
|
|
467
518
|
}
|
|
468
519
|
const updated = await applyModelProactive(rid, patch);
|
|
469
520
|
sendJson(res, 200, { ok: true, role: await publicRole(updated) });
|
|
470
521
|
return;
|
|
471
522
|
}
|
|
472
523
|
if (req.method === 'DELETE') {
|
|
524
|
+
await deps.beforeRoleDelete?.(rid);
|
|
473
525
|
if (llmProviders) {
|
|
474
526
|
try { await llmProviders.unregister(role); } catch (e) { log?.warn?.('pygmalion: 注销自定义 provider %s 失败 %o', rid, e); }
|
|
475
527
|
}
|
|
476
528
|
await botApi.removeForRole(rid);
|
|
477
529
|
try { await devices?.disposeRole?.(rid); } catch { /* ignore */ }
|
|
478
530
|
await persona.deleteRole(rid);
|
|
531
|
+
deps.afterRoleDelete?.(rid);
|
|
479
532
|
sendJson(res, 200, { ok: true, deleted: rid });
|
|
480
533
|
return;
|
|
481
534
|
}
|
|
@@ -483,53 +536,55 @@ export function createWebServer(deps) {
|
|
|
483
536
|
return;
|
|
484
537
|
}
|
|
485
538
|
|
|
486
|
-
//
|
|
487
|
-
const
|
|
488
|
-
if (
|
|
489
|
-
const rid = roleParam(
|
|
490
|
-
const role = rid && await persona.loadRole(rid);
|
|
491
|
-
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
492
|
-
try {
|
|
493
|
-
const b = await readBody(req);
|
|
494
|
-
sendJson(res, 200, { ok: true, voice: await
|
|
495
|
-
} catch (e) { sendJson(res, 400, { ok: false, error: String(e?.message ?? e) }); }
|
|
496
|
-
return;
|
|
497
|
-
}
|
|
498
|
-
const pVoice = /^\/api\/me\/roles\/([^/]+)\/voice$/.exec(pathname);
|
|
499
|
-
if (pVoice) {
|
|
500
|
-
const rid = roleParam(pVoice[1]);
|
|
501
|
-
const role = rid && await persona.loadRole(rid);
|
|
502
|
-
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
503
|
-
if (req.method === 'GET') { sendJson(res, 200, { ok: true, voice:
|
|
504
|
-
if (req.method === 'PUT') {
|
|
505
|
-
try { sendJson(res, 200, { ok: true, voice: await
|
|
506
|
-
catch (e) { sendJson(res, 400, { ok: false, error: String(e?.message ?? e) }); }
|
|
507
|
-
return;
|
|
508
|
-
}
|
|
509
|
-
sendJson(res, 405, { ok: false, error: 'method not allowed' }); return;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
// 我的角色的 QQ 机器人(凭据仅角色 owner 可配;qqSecret 不回传,空留=保持原值)。
|
|
539
|
+
// 语音素材按实现字段上传;保留旧 reference 地址兼容已有客户端。
|
|
540
|
+
const pVoiceUpload = /^\/api\/me\/roles\/([^/]+)\/voice\/(?:assets|reference)$/.exec(pathname);
|
|
541
|
+
if (pVoiceUpload && req.method === 'POST') {
|
|
542
|
+
const rid = roleParam(pVoiceUpload[1]);
|
|
543
|
+
const role = rid && await persona.loadRole(rid);
|
|
544
|
+
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
545
|
+
try {
|
|
546
|
+
const b = await readBody(req);
|
|
547
|
+
sendJson(res, 200, { ok: true, voice: await voice.upload(rid, b ?? {}) });
|
|
548
|
+
} catch (e) { sendJson(res, 400, { ok: false, error: String(e?.message ?? e) }); }
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
const pVoice = /^\/api\/me\/roles\/([^/]+)\/voice$/.exec(pathname);
|
|
552
|
+
if (pVoice) {
|
|
553
|
+
const rid = roleParam(pVoice[1]);
|
|
554
|
+
const role = rid && await persona.loadRole(rid);
|
|
555
|
+
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
556
|
+
if (req.method === 'GET') { sendJson(res, 200, { ok: true, voice: await voice.view(rid, url.searchParams.get('providerId')) }); return; }
|
|
557
|
+
if (req.method === 'PUT') {
|
|
558
|
+
try { sendJson(res, 200, { ok: true, voice: await voice.save(rid, await readBody(req)) }); }
|
|
559
|
+
catch (e) { sendJson(res, 400, { ok: false, error: String(e?.message ?? e) }); }
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
sendJson(res, 405, { ok: false, error: 'method not allowed' }); return;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// 我的角色的 QQ 机器人(凭据仅角色 owner 可配;qqSecret 不回传,空留=保持原值)。
|
|
513
566
|
const pBot = /^\/api\/me\/roles\/([^/]+)\/bot$/.exec(pathname);
|
|
514
567
|
if (pBot) {
|
|
515
568
|
const rid = roleParam(pBot[1]);
|
|
516
569
|
if (!rid) { sendJson(res, 400, { ok: false, error: '角色 ID 非法' }); return; }
|
|
517
570
|
const role = await persona.loadRole(rid);
|
|
518
571
|
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
519
|
-
if (req.method === 'PUT') {
|
|
520
|
-
const b = await readBody(req);
|
|
521
|
-
try {
|
|
522
|
-
await botApi.upsertForRole(rid, { ...(b ?? {}), owner: me.qqNumber });
|
|
523
|
-
// 休眠不仅断开 QQ 网关,也须停止角色已在运行的主动脚本和设备脚本。
|
|
524
|
-
if (b?.enabled === false) {
|
|
525
|
-
await stopLifeScripts(role.workspace);
|
|
526
|
-
try {
|
|
527
|
-
const toyRuns = devices?.listScriptRuns?.() ?? [];
|
|
528
|
-
await Promise.all(toyRuns.filter((run) => run.roleId === rid).map((run) => devices.stopScript(run.runId)));
|
|
529
|
-
await devices?.disposeRole?.(rid);
|
|
530
|
-
} catch { /* 设备清理失败不影响休眠 */ }
|
|
531
|
-
}
|
|
532
|
-
|
|
572
|
+
if (req.method === 'PUT') {
|
|
573
|
+
const b = await readBody(req);
|
|
574
|
+
try {
|
|
575
|
+
await botApi.upsertForRole(rid, { ...(b ?? {}), owner: me.qqNumber });
|
|
576
|
+
// 休眠不仅断开 QQ 网关,也须停止角色已在运行的主动脚本和设备脚本。
|
|
577
|
+
if (b?.enabled === false) {
|
|
578
|
+
await stopLifeScripts(role.workspace);
|
|
579
|
+
try {
|
|
580
|
+
const toyRuns = devices?.listScriptRuns?.() ?? [];
|
|
581
|
+
await Promise.all(toyRuns.filter((run) => run.roleId === rid).map((run) => devices.stopScript(run.runId, rid)));
|
|
582
|
+
await devices?.disposeRole?.(rid);
|
|
583
|
+
} catch { /* 设备清理失败不影响休眠 */ }
|
|
584
|
+
} else if (b?.enabled === true) {
|
|
585
|
+
await recoverLifeScripts(role.workspace);
|
|
586
|
+
}
|
|
587
|
+
sendJson(res, 200, { ok: true, bot: roleBot(rid) });
|
|
533
588
|
} catch (e) {
|
|
534
589
|
sendJson(res, 400, { ok: false, error: String(e?.message ?? e) });
|
|
535
590
|
}
|
|
@@ -623,6 +678,7 @@ export function createWebServer(deps) {
|
|
|
623
678
|
const role = await persona.loadRole(rid);
|
|
624
679
|
if (!role || !ownedBy(role, me.qqNumber)) { sendJson(res, 404, { ok: false, error: '角色不存在' }); return; }
|
|
625
680
|
const updated = await persona.resetRoleBinding(rid);
|
|
681
|
+
await devices?.disposeRole?.(rid);
|
|
626
682
|
sendJson(res, 200, { ok: true, role: await publicRole(updated) });
|
|
627
683
|
return;
|
|
628
684
|
}
|
|
@@ -681,7 +737,7 @@ export function createWebServer(deps) {
|
|
|
681
737
|
const b = await readBody(req);
|
|
682
738
|
const runId = String(b?.runId ?? '').trim();
|
|
683
739
|
if (!runId) { sendJson(res, 400, { ok: false, error: '缺少 runId' }); return; }
|
|
684
|
-
const r = await devices.stopScript(runId);
|
|
740
|
+
const r = await devices.stopScript(runId, rid);
|
|
685
741
|
sendJson(res, 200, r);
|
|
686
742
|
return;
|
|
687
743
|
}
|
|
@@ -694,14 +750,16 @@ export function createWebServer(deps) {
|
|
|
694
750
|
if (pToys[2]) {
|
|
695
751
|
let rawName = '';
|
|
696
752
|
try { rawName = decodeURIComponent(pToys[3]); } catch { /* 非法 % 序列 */ }
|
|
753
|
+
const directory = rawName.startsWith('@devices/') ? 'device-scripts' : 'toys';
|
|
754
|
+
if (directory === 'device-scripts') rawName = rawName.slice('@devices/'.length);
|
|
697
755
|
const name = pToys[2] === 'log' ? logFileNameParam(rawName) : scriptFileNameParam(rawName);
|
|
698
756
|
if (!name) { sendJson(res, 400, { ok: false, error: pToys[2] === 'log' ? '日志名非法' : '脚本名非法' }); return; }
|
|
699
757
|
const file = pToys[2] === 'log'
|
|
700
|
-
? join(role.workspace,
|
|
701
|
-
: join(role.workspace,
|
|
758
|
+
? join(role.workspace, directory, '.logs', name)
|
|
759
|
+
: join(role.workspace, directory, name);
|
|
702
760
|
const base = pToys[2] === 'log'
|
|
703
|
-
? join(role.workspace,
|
|
704
|
-
: join(role.workspace,
|
|
761
|
+
? join(role.workspace, directory, '.logs')
|
|
762
|
+
: join(role.workspace, directory);
|
|
705
763
|
if (!file.startsWith(base)) { sendJson(res, 400, { ok: false, error: '路径非法' }); return; }
|
|
706
764
|
const text = await readFile(file, 'utf8').catch(() => null);
|
|
707
765
|
if (text === null) { sendJson(res, 404, { ok: false, error: pToys[2] === 'log' ? '日志不存在' : '脚本不存在' }); return; }
|
|
@@ -716,7 +774,7 @@ export function createWebServer(deps) {
|
|
|
716
774
|
sendJson(res, 404, { ok: false, error: `not found: ${pathname}` });
|
|
717
775
|
} catch (e) {
|
|
718
776
|
log?.error?.('web-server error: %o', e);
|
|
719
|
-
if (!res.headersSent) sendJson(res, 500, { ok: false, error: String(e?.message ?? e) });
|
|
777
|
+
if (!res.headersSent) sendJson(res, e instanceof RoleplayConfigError?400:500, { ok: false, error: String(e?.message ?? e) });
|
|
720
778
|
}
|
|
721
779
|
};
|
|
722
780
|
|
|
@@ -735,6 +793,7 @@ export function createWebServer(deps) {
|
|
|
735
793
|
return started;
|
|
736
794
|
},
|
|
737
795
|
async stop() {
|
|
796
|
+
if (bundledVoice) { bundledVoice.voice.dispose(); bundledVoice.extensions.dispose(); }
|
|
738
797
|
if (!server) return;
|
|
739
798
|
const s = server; server = null;
|
|
740
799
|
await new Promise((resolve) => s.close(() => resolve()));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** 后台表情包任务只拿到读取、下载、入库能力,不继承角色的发送/设备工具。 */
|
|
2
|
+
export const name = 'pygmalion-worker-tools';
|
|
3
|
+
export const inject = ['tools', 'pygmalion.director'];
|
|
4
|
+
export function restrictWorkerTools(ctx, names) {
|
|
5
|
+
const allowed = new Set(names);
|
|
6
|
+
ctx.tools.guard(exec => allowed.has(exec.name) ? undefined : '后台工作会话没有此能力');
|
|
7
|
+
ctx.on('agent/pre-step', async ({ agent }, next) => {
|
|
8
|
+
// 在 agent 视图中限制继承的能力,避免 standing 层 allow 掩掉另一个 standing 工具。
|
|
9
|
+
const denied = ctx.tools.schemas(agent).map(tool => tool.name).filter(name => !allowed.has(name));
|
|
10
|
+
if (denied.length) agent.ctx.get('tools').restrict({ deny: denied });
|
|
11
|
+
return next();
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export function apply(ctx) {
|
|
15
|
+
const allowed = ['read', 'read_image', 'web_search', 'web_fetch', 'meme_download', 'meme_archive', 'meme_save', 'meme_update', 'meme_read'];
|
|
16
|
+
ctx.get('pygmalion.director').registerCapabilityTools(ctx, allowed);
|
|
17
|
+
restrictWorkerTools(ctx, allowed);
|
|
18
|
+
}
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pygmalion 生活脚本参考模板(不再自动布设;dsh 自行创建并运行自己的脚本)。
|
|
3
|
-
*
|
|
4
|
-
* 新模型:脚本 = 角色独立生活的程序化模拟器。何时运行/怎么写/何时结束完全由 dsh 决定,
|
|
5
|
-
* 插件只做监管展示(life/.logs/ + life/.manifest.json + life/.backup/),不限制运行时长/次数。
|
|
6
|
-
* 脚本可以长驻循环(本模板即循环),模拟角色一天的生活节奏、习惯、心情、突发小事件,
|
|
7
|
-
* 并把「生活经历」写成可读记录(life/生活记录.log),这样聊天时角色有真实生活可谈——
|
|
8
|
-
* 而不是简单的「ta 很久没回就去找 ta」。
|
|
9
|
-
*
|
|
10
|
-
* 注入环境变量:
|
|
11
|
-
* PYG_QQ 角色 id
|
|
12
|
-
* PYG_WORKSPACE 本角色工作区(life 的上级目录)
|
|
13
|
-
* PYG_STATE_FILE state.json 路径(可读 rewake/日程)
|
|
14
|
-
* PYG_LAST_USER_AT 最后用户活动时间(epoch ms;未知则不设)
|
|
15
|
-
* PYG_DATA_ROOT Pygmalion 数据根
|
|
16
|
-
* PYG_TOKEN / PYG_BASE HTTP hook 用(本模板未使用)
|
|
17
|
-
*
|
|
18
|
-
* 约定:脚本 stdout/stderr 全部进 life/.logs/ 监管日志;以「有事件就唤醒」为原则——脚本里
|
|
19
|
-
* 设计的每个事件(心情变化、到点作息、突发小事件)发生时都调用 wake({ mood, topic, reason, force }),
|
|
20
|
-
* node 脚本启动时已自动预加载全局 wake(),直接调用即可(无需 import/fetch)。插件会节流唤醒
|
|
21
|
-
* dsh 注入 [脚本事件] 情境;是否开口由导演判断(脚本只负责报事件,不决定开口)。
|
|
22
|
-
*/
|
|
23
|
-
import { appendFileSync, readFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
24
|
-
import { join, dirname } from 'node:path';
|
|
25
|
-
|
|
26
|
-
const WORKSPACE = process.env.PYG_WORKSPACE || '.';
|
|
27
|
-
const LIFE_DIR = join(WORKSPACE, 'life');
|
|
28
|
-
const LOG_FILE = join(LIFE_DIR, '生活记录.log');
|
|
29
|
-
mkdirSync(LIFE_DIR, { recursive: true });
|
|
30
|
-
|
|
31
|
-
// 简单的时间标签,让生活记录读起来像角色的真实一天。
|
|
32
|
-
const hm = (d = new Date()) => `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
|
33
|
-
const date = (d = new Date()) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
34
|
-
|
|
35
|
-
/** 追加一行生活记录(脚本模拟出的经历,供导演之后读取/引用)。 */
|
|
36
|
-
function live(text) {
|
|
37
|
-
const line = `[${date()} ${hm()}] ${text}`;
|
|
38
|
-
console.log(line);
|
|
39
|
-
appendFileSync(LOG_FILE, line + '\n', 'utf8');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 内存级状态:所有需要跨重启保留的可变状态必须写在 pyg.state 上(如计数/进度/心情/已发生的事件),
|
|
43
|
-
// 插件会自动 checkpoint 到 life/.state-<脚本名>.json 并在重启后重新拉起时恢复 ——
|
|
44
|
-
// 重启前 pyg.state.ticks 是几,重启后还是几。不要用脚本顶层变量存重要状态(重启会丢)。
|
|
45
|
-
const ticks = (pyg.state.ticks ?? 0) + 1;
|
|
46
|
-
pyg.state.ticks = ticks;
|
|
47
|
-
|
|
48
|
-
// 角色的一天:按当前时刻推进生活(这是演示骨架,dsh 应改成具体角色的真实生活)。
|
|
49
|
-
function tick() {
|
|
50
|
-
const h = new Date().getHours();
|
|
51
|
-
if (h === 8) live('起床,泡了杯热茶,坐在窗边发了会儿呆。');
|
|
52
|
-
if (h === 12) live('午饭时间,今天随便煮了碗面。');
|
|
53
|
-
if (h === 19) { live('晚饭后出门散步,吹了会儿晚风。'); wake({ mood: '软', topic: '天黑了,想和 ta 说说话', reason: '散步时特别想 ta,想轻轻问一句在干嘛' }); }
|
|
54
|
-
if (h === 23) live('要睡了,把今天的心情写在日记里。');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// 长驻循环:每 60 秒推进一次生活节奏,直到进程被终止(插件 stop 或 dsh 自行结束)。
|
|
58
|
-
tick();
|
|
59
|
-
setInterval(tick, 60_000);
|
|
1
|
+
/**
|
|
2
|
+
* Pygmalion 生活脚本参考模板(不再自动布设;dsh 自行创建并运行自己的脚本)。
|
|
3
|
+
*
|
|
4
|
+
* 新模型:脚本 = 角色独立生活的程序化模拟器。何时运行/怎么写/何时结束完全由 dsh 决定,
|
|
5
|
+
* 插件只做监管展示(life/.logs/ + life/.manifest.json + life/.backup/),不限制运行时长/次数。
|
|
6
|
+
* 脚本可以长驻循环(本模板即循环),模拟角色一天的生活节奏、习惯、心情、突发小事件,
|
|
7
|
+
* 并把「生活经历」写成可读记录(life/生活记录.log),这样聊天时角色有真实生活可谈——
|
|
8
|
+
* 而不是简单的「ta 很久没回就去找 ta」。
|
|
9
|
+
*
|
|
10
|
+
* 注入环境变量:
|
|
11
|
+
* PYG_QQ 角色 id
|
|
12
|
+
* PYG_WORKSPACE 本角色工作区(life 的上级目录)
|
|
13
|
+
* PYG_STATE_FILE state.json 路径(可读 rewake/日程)
|
|
14
|
+
* PYG_LAST_USER_AT 最后用户活动时间(epoch ms;未知则不设)
|
|
15
|
+
* PYG_DATA_ROOT Pygmalion 数据根
|
|
16
|
+
* PYG_TOKEN / PYG_BASE HTTP hook 用(本模板未使用)
|
|
17
|
+
*
|
|
18
|
+
* 约定:脚本 stdout/stderr 全部进 life/.logs/ 监管日志;以「有事件就唤醒」为原则——脚本里
|
|
19
|
+
* 设计的每个事件(心情变化、到点作息、突发小事件)发生时都调用 wake({ mood, topic, reason, force }),
|
|
20
|
+
* node 脚本启动时已自动预加载全局 wake(),直接调用即可(无需 import/fetch)。插件会节流唤醒
|
|
21
|
+
* dsh 注入 [脚本事件] 情境;是否开口由导演判断(脚本只负责报事件,不决定开口)。
|
|
22
|
+
*/
|
|
23
|
+
import { appendFileSync, readFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
24
|
+
import { join, dirname } from 'node:path';
|
|
25
|
+
|
|
26
|
+
const WORKSPACE = process.env.PYG_WORKSPACE || '.';
|
|
27
|
+
const LIFE_DIR = join(WORKSPACE, 'life');
|
|
28
|
+
const LOG_FILE = join(LIFE_DIR, '生活记录.log');
|
|
29
|
+
mkdirSync(LIFE_DIR, { recursive: true });
|
|
30
|
+
|
|
31
|
+
// 简单的时间标签,让生活记录读起来像角色的真实一天。
|
|
32
|
+
const hm = (d = new Date()) => `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
|
33
|
+
const date = (d = new Date()) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
34
|
+
|
|
35
|
+
/** 追加一行生活记录(脚本模拟出的经历,供导演之后读取/引用)。 */
|
|
36
|
+
function live(text) {
|
|
37
|
+
const line = `[${date()} ${hm()}] ${text}`;
|
|
38
|
+
console.log(line);
|
|
39
|
+
appendFileSync(LOG_FILE, line + '\n', 'utf8');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 内存级状态:所有需要跨重启保留的可变状态必须写在 pyg.state 上(如计数/进度/心情/已发生的事件),
|
|
43
|
+
// 插件会自动 checkpoint 到 life/.state-<脚本名>.json 并在重启后重新拉起时恢复 ——
|
|
44
|
+
// 重启前 pyg.state.ticks 是几,重启后还是几。不要用脚本顶层变量存重要状态(重启会丢)。
|
|
45
|
+
const ticks = (pyg.state.ticks ?? 0) + 1;
|
|
46
|
+
pyg.state.ticks = ticks;
|
|
47
|
+
|
|
48
|
+
// 角色的一天:按当前时刻推进生活(这是演示骨架,dsh 应改成具体角色的真实生活)。
|
|
49
|
+
function tick() {
|
|
50
|
+
const h = new Date().getHours();
|
|
51
|
+
if (h === 8) live('起床,泡了杯热茶,坐在窗边发了会儿呆。');
|
|
52
|
+
if (h === 12) live('午饭时间,今天随便煮了碗面。');
|
|
53
|
+
if (h === 19) { live('晚饭后出门散步,吹了会儿晚风。'); wake({ mood: '软', topic: '天黑了,想和 ta 说说话', reason: '散步时特别想 ta,想轻轻问一句在干嘛' }); }
|
|
54
|
+
if (h === 23) live('要睡了,把今天的心情写在日记里。');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 长驻循环:每 60 秒推进一次生活节奏,直到进程被终止(插件 stop 或 dsh 自行结束)。
|
|
58
|
+
tick();
|
|
59
|
+
setInterval(tick, 60_000);
|