pygmalion 0.1.4 → 0.1.6
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/PRESET.md +6 -6
- package/package.json +1 -1
- package/preset/pygmalion-director/agent.cordis.yml +161 -142
- package/src/active-session.js +25 -2
- package/src/bot-factory.js +282 -282
- package/src/heartbeat.js +352 -352
- package/src/inbound.js +770 -770
- package/src/index.js +210 -210
- package/src/persona.js +380 -380
- package/src/tools.js +511 -502
- package/src/wakeup.js +208 -208
- package/src/web-public/app.js +381 -381
- package/src/web-public/index.html +130 -130
- package/src/web-public/style.css +44 -44
- package/src/web-server.js +506 -506
package/src/wakeup.js
CHANGED
|
@@ -1,208 +1,208 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pygmalion wakeup — the "唤醒 dsh 行动" engine.
|
|
3
|
-
*
|
|
4
|
-
* A scheduled script decides WHEN to wake up; this module makes the dsh agent
|
|
5
|
-
* ACT based on the real current situation. It does NOT send a preset message:
|
|
6
|
-
* it injects a situational [唤醒事件] context into the per-user agent session
|
|
7
|
-
* and wakes the driver, letting the director read memory / recent chat and
|
|
8
|
-
* decide whether (and what) to say. The agent's reply then flows out through
|
|
9
|
-
* the normal outbound pipeline; if the director chooses silence, nothing is
|
|
10
|
-
* sent and the decision can be written to memory.
|
|
11
|
-
*/
|
|
12
|
-
import { effectiveSessionId } from './inbound.js';
|
|
13
|
-
import { targetKey } from './threads.js';
|
|
14
|
-
|
|
15
|
-
export const DEFAULT_CHAT_WINDOW_MS = 5 * 60_000;
|
|
16
|
-
|
|
17
|
-
/** Human "now" used inside the wakeup context. */
|
|
18
|
-
function nowText(now = new Date()) {
|
|
19
|
-
return now.toLocaleString('zh-CN', { hour12: false });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Build the model-facing wakeup context (reason + hint + memory pointers + persona name). */
|
|
23
|
-
export function buildWakeupContext({ reason, memoryHint, personaName, now = new Date() }) {
|
|
24
|
-
const parts = [
|
|
25
|
-
'[唤醒事件] 时间:' + nowText(now),
|
|
26
|
-
];
|
|
27
|
-
if (personaName != null && personaName.length > 0)
|
|
28
|
-
parts.push('角色:' + personaName);
|
|
29
|
-
parts.push('唤醒原因:' + (reason?.trim?.() ?? '计划时机已到'));
|
|
30
|
-
if (memoryHint != null && `${memoryHint}`.trim().length > 0)
|
|
31
|
-
parts.push('提示:' + `${memoryHint}`.trim());
|
|
32
|
-
parts.push('');
|
|
33
|
-
parts.push('
|
|
34
|
-
parts.push('
|
|
35
|
-
parts.push('
|
|
36
|
-
return parts.join('\n');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** 归档任务情境:用户长时间未发言时,把原始记录按话题切片归档为双份记忆并建索引。 */
|
|
40
|
-
export function buildArchiveContext({ date, workspace, now = new Date() }) {
|
|
41
|
-
const parts = [
|
|
42
|
-
'[归档任务] 你的对话已沉默一段时间(用户 45 分钟以上未发新消息),现在整理记忆。',
|
|
43
|
-
'时间:' + nowText(now),
|
|
44
|
-
'工作区:' + (workspace ?? ''),
|
|
45
|
-
];
|
|
46
|
-
if (date) parts.push('目标日期:' + date);
|
|
47
|
-
parts.push('');
|
|
48
|
-
parts.push('请完成记忆归档(这是后台维护,不是对用户说话——绝对不要调用 director_send):');
|
|
49
|
-
parts.push('1. 用文件工具读取 memory/<目标日期>/原始记录.md(系统已按时间归档的忠实原文);');
|
|
50
|
-
parts.push('2.
|
|
51
|
-
parts.push(' 目录名 = 具体话题 + 起止时刻(含日期、精确到秒,如 [2026-08-19 21:31:00-21:44:00],取该片段首尾消息时间;Windows
|
|
52
|
-
parts.push(' 原文.md 必须逐字复制原始行——【绝对不要】用 write / str-replace-editor 重打内容(必然走样/转写);');
|
|
53
|
-
parts.push(' 要用 shell(Windows 用 pwsh)按行号把该话题覆盖的行【原样抽取】写入 原文.md:Get-Content 读原始记录、取对应行区间 Set-Content 到原文.md,写后读回核对与原始记录逐字一致;');
|
|
54
|
-
parts.push(' 话题边界由你自己读原文判定,不要按时间硬切、不要造兜底分组;');
|
|
55
|
-
parts.push('3. 每个话题目录里【必须】再写一份 索引.md(概要 / 关键事件 / 情感变化 / 待办与承诺);');
|
|
56
|
-
parts.push('4. 重写 memory/<目标日期>/索引.md,列出当日各话题、关键事件、情感变化、待办/承诺;');
|
|
57
|
-
parts.push('5.
|
|
58
|
-
parts.push('6. 全部写完后再遍历 memory/topics/ 下每个目录自查:每个目录必须同时含 原文.md 与 索引.md,且 原文.md
|
|
59
|
-
return parts.join('\n');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
*
|
|
64
|
-
* 唤醒导演判断此刻是否由角色开口。脚本侧一行 wake() 即可触发。
|
|
65
|
-
*/
|
|
66
|
-
export function buildScriptEventContext({ event, mood, topic, reason, personaName, now = new Date() }) {
|
|
67
|
-
const parts = [
|
|
68
|
-
'[脚本事件] 时间:' + nowText(now),
|
|
69
|
-
];
|
|
70
|
-
if (personaName != null && personaName.length > 0) parts.push('角色:' + personaName);
|
|
71
|
-
if (reason != null && `${reason}`.trim()) parts.push('触发:' + `${reason}`.trim());
|
|
72
|
-
parts.push('');
|
|
73
|
-
parts.push('你的生活脚本报来一个角色生活事件(脚本模拟产生):');
|
|
74
|
-
if (event != null && `${event}`.trim()) parts.push('· 事件:' + event);
|
|
75
|
-
if (mood != null && `${mood}`.trim()) parts.push('· 心情:' + mood);
|
|
76
|
-
if (topic != null && `${topic}`.trim()) parts.push('· 主题:' + topic);
|
|
77
|
-
parts.push('');
|
|
78
|
-
parts.push('
|
|
79
|
-
parts.push('请作为导演,先读取脚本写下的生活记录(如 life/<名>.log 或工作区记录文件)与最近聊天,');
|
|
80
|
-
parts.push('自行判断此刻是否由角色开口:想 ta
|
|
81
|
-
parts.push('
|
|
82
|
-
parts.push('
|
|
83
|
-
parts.push('
|
|
84
|
-
return parts.join('\n');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*/
|
|
91
|
-
export function buildLifeWindowContext({ reason, personaName, hasLifeScript, now = new Date() }) {
|
|
92
|
-
const parts = [
|
|
93
|
-
'[生活窗口] 时间:' + nowText(now),
|
|
94
|
-
];
|
|
95
|
-
if (personaName != null && personaName.length > 0) parts.push('角色:' + personaName);
|
|
96
|
-
if (reason != null && `${reason}`.trim()) parts.push('触发:' + `${reason}`.trim());
|
|
97
|
-
parts.push('');
|
|
98
|
-
if (hasLifeScript) {
|
|
99
|
-
parts.push('你的生活脚本在跑(life/ 下已有脚本,可读 life/.manifest.json
|
|
100
|
-
parts.push('
|
|
101
|
-
parts.push('
|
|
102
|
-
} else {
|
|
103
|
-
parts.push('
|
|
104
|
-
parts.push('是要让角色有真实的生活:她在和你聊天之外,还应该有她自己的生活(一天怎么过、习惯与小事、');
|
|
105
|
-
parts.push('心情变化、突发的小事件)。你是导演,职责是创作这些经历、把它们程序化为脚本,');
|
|
106
|
-
parts.push('让脚本模拟并记录角色的生活(如写到 life/<名>.log 或工作区文件),这样聊天时角色有真实经历可谈。');
|
|
107
|
-
parts.push('现在做两件事:');
|
|
108
|
-
parts.push('1. 新建一个你的生活脚本(新文件,勿覆盖 life/ 下已有文件)——写角色真实的生活细节,');
|
|
109
|
-
parts.push('
|
|
110
|
-
parts.push(' 事件(心情变化、到点作息、突发小事件、想 ta 的时刻)发生时都调用 wake() 报给你,');
|
|
111
|
-
parts.push('
|
|
112
|
-
parts.push('2. 用 life_run 启动它(立即返回,脚本在后台继续跑,由脚本自己决定何时结束;');
|
|
113
|
-
parts.push(' 插件只监管展示、不限制运行时长/次数)。');
|
|
114
|
-
}
|
|
115
|
-
parts.push('');
|
|
116
|
-
parts.push('
|
|
117
|
-
return parts.join('\n');
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 归档唤醒:注入一次归档任务并唤醒 dsh(不发送任何消息给用户)。
|
|
122
|
-
* 无需撞车检测——它只在用户长时间沉默时触发。
|
|
123
|
-
*/
|
|
124
|
-
export async function archiveWakeup(deps, params) {
|
|
125
|
-
const { target, date, reason } = params;
|
|
126
|
-
const thread = deps.threads.current(targetKey(target));
|
|
127
|
-
const sessionId = effectiveSessionId(target, thread, deps.botId);
|
|
128
|
-
|
|
129
|
-
// 角色级运行时:工作区 = 本 bot 服务的角色工作区(多 bot 隔离)。
|
|
130
|
-
let workspace;
|
|
131
|
-
if (deps.roleId) {
|
|
132
|
-
const role = await deps.persona?.loadRole?.(deps.roleId);
|
|
133
|
-
if (role) {
|
|
134
|
-
await deps.persona?.syncRoleMemory?.(role);
|
|
135
|
-
workspace = role.workspace;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
const agent = await deps.ensureAgent(sessionId);
|
|
139
|
-
if (!agent) return { ok: false, error: `cannot resolve agent for ${sessionId}` };
|
|
140
|
-
|
|
141
|
-
const text = buildArchiveContext({ date, workspace, reason });
|
|
142
|
-
deps.log?.info?.('pygmalion archive: waking %s (%s)', sessionId, date ?? reason ?? '(auto)');
|
|
143
|
-
await agent.send({
|
|
144
|
-
id: `pygarchive:${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`,
|
|
145
|
-
role: 'user',
|
|
146
|
-
content: [{ type: 'text', text }],
|
|
147
|
-
source: { kind: 'system', id: 'pygmalion-archive', name: 'Pygmalion 归档' },
|
|
148
|
-
}, 'next-turn', true);
|
|
149
|
-
return { ok: true, sessionId };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Wake one QQ conversation.
|
|
154
|
-
* @param deps - { threads, routes, ensureAgent, persona?, withinChatWindowMs?,
|
|
155
|
-
* log?, activityAt? }
|
|
156
|
-
* @param params - { target, reason?, memoryHint?, force? }
|
|
157
|
-
*/
|
|
158
|
-
export async function wakeupSession(deps, params) {
|
|
159
|
-
const { target, reason, memoryHint, force, kind, event, mood, topic } = params;
|
|
160
|
-
const thread = deps.threads.current(targetKey(target));
|
|
161
|
-
const sessionId = effectiveSessionId(target, thread, deps.botId);
|
|
162
|
-
|
|
163
|
-
// Collision detection: do not barge into an active / recent conversation.
|
|
164
|
-
if (force !== true) {
|
|
165
|
-
const lastAt = typeof deps.activityAt === 'function'
|
|
166
|
-
? deps.activityAt(sessionId)
|
|
167
|
-
: deps.routes.get?.(sessionId)?.lastMsgAt;
|
|
168
|
-
const windowMs = deps.withinChatWindowMs ?? DEFAULT_CHAT_WINDOW_MS;
|
|
169
|
-
if (lastAt !== undefined && Date.now() - lastAt < windowMs) {
|
|
170
|
-
const skipped = { ok: false, skipped: 'chatting', sessionId, lastActivityAgoMs: Date.now() - lastAt };
|
|
171
|
-
deps.log?.info?.('pygmalion wakeup: skip %s (user active %dms ago)', sessionId, skipped.lastActivityAgoMs);
|
|
172
|
-
return skipped;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// 角色级运行时:personaName + 生活脚本状态(多 bot 按 roleId 路由;deps.lifeActive 由 heartbeat 注入)。
|
|
177
|
-
let personaName;
|
|
178
|
-
let hasLifeScript;
|
|
179
|
-
if (deps.roleId) {
|
|
180
|
-
const role = await deps.persona?.loadRole?.(deps.roleId);
|
|
181
|
-
if (role) {
|
|
182
|
-
await deps.persona?.syncRoleMemory?.(role);
|
|
183
|
-
personaName = role.persona?.name;
|
|
184
|
-
if (typeof deps.lifeActive === 'function') hasLifeScript = await deps.lifeActive(role.workspace);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const agent = await deps.ensureAgent(sessionId);
|
|
189
|
-
if (!agent) {
|
|
190
|
-
const fail = { ok: false, error: `cannot resolve agent for ${sessionId}` };
|
|
191
|
-
deps.log?.error?.('pygmalion wakeup: %s no agent', sessionId);
|
|
192
|
-
return fail;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let text;
|
|
196
|
-
if (kind === 'life') text = buildLifeWindowContext({ reason, personaName, hasLifeScript });
|
|
197
|
-
else if (kind === 'script-hook') text = buildScriptEventContext({ event, mood, topic, reason, personaName });
|
|
198
|
-
else text = buildWakeupContext({ reason, memoryHint, personaName });
|
|
199
|
-
deps.log?.info?.('pygmalion wakeup: waking %s (%s)', sessionId, reason ?? '(default)');
|
|
200
|
-
await agent.send({
|
|
201
|
-
id: `pygwake:${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`,
|
|
202
|
-
role: 'user',
|
|
203
|
-
content: [{ type: 'text', text }],
|
|
204
|
-
source: { kind: 'user', id: 'pygmalion-wakeup', name: 'Pygmalion 唤醒' },
|
|
205
|
-
}, 'next-turn', true);
|
|
206
|
-
|
|
207
|
-
return { ok: true, woken: true, sessionId };
|
|
208
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Pygmalion wakeup — the "唤醒 dsh 行动" engine.
|
|
3
|
+
*
|
|
4
|
+
* A scheduled script decides WHEN to wake up; this module makes the dsh agent
|
|
5
|
+
* ACT based on the real current situation. It does NOT send a preset message:
|
|
6
|
+
* it injects a situational [唤醒事件] context into the per-user agent session
|
|
7
|
+
* and wakes the driver, letting the director read memory / recent chat and
|
|
8
|
+
* decide whether (and what) to say. The agent's reply then flows out through
|
|
9
|
+
* the normal outbound pipeline; if the director chooses silence, nothing is
|
|
10
|
+
* sent and the decision can be written to memory.
|
|
11
|
+
*/
|
|
12
|
+
import { effectiveSessionId } from './inbound.js';
|
|
13
|
+
import { targetKey } from './threads.js';
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_CHAT_WINDOW_MS = 5 * 60_000;
|
|
16
|
+
|
|
17
|
+
/** Human "now" used inside the wakeup context. */
|
|
18
|
+
function nowText(now = new Date()) {
|
|
19
|
+
return now.toLocaleString('zh-CN', { hour12: false });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Build the model-facing wakeup context (reason + hint + memory pointers + persona name). */
|
|
23
|
+
export function buildWakeupContext({ reason, memoryHint, personaName, now = new Date() }) {
|
|
24
|
+
const parts = [
|
|
25
|
+
'[唤醒事件] 时间:' + nowText(now),
|
|
26
|
+
];
|
|
27
|
+
if (personaName != null && personaName.length > 0)
|
|
28
|
+
parts.push('角色:' + personaName);
|
|
29
|
+
parts.push('唤醒原因:' + (reason?.trim?.() ?? '计划时机已到'));
|
|
30
|
+
if (memoryHint != null && `${memoryHint}`.trim().length > 0)
|
|
31
|
+
parts.push('提示:' + `${memoryHint}`.trim());
|
|
32
|
+
parts.push('');
|
|
33
|
+
parts.push('你是被调度脚本按计划唤醒的。请作为导演,先读取工作区记忆与最近聊天,判断当下的真实情况,');
|
|
34
|
+
parts.push('包括刚才聊过什么、用户是否正在聊天、情境是否已经变化,然后再决定角色此刻是否开口以及');
|
|
35
|
+
parts.push('要说什么。你可以顺势融入对话,也可以选择保持沉默。');
|
|
36
|
+
return parts.join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 归档任务情境:用户长时间未发言时,把原始记录按话题切片归档为双份记忆并建索引。 */
|
|
40
|
+
export function buildArchiveContext({ date, workspace, now = new Date() }) {
|
|
41
|
+
const parts = [
|
|
42
|
+
'[归档任务] 你的对话已沉默一段时间(用户 45 分钟以上未发新消息),现在整理记忆。',
|
|
43
|
+
'时间:' + nowText(now),
|
|
44
|
+
'工作区:' + (workspace ?? ''),
|
|
45
|
+
];
|
|
46
|
+
if (date) parts.push('目标日期:' + date);
|
|
47
|
+
parts.push('');
|
|
48
|
+
parts.push('请完成记忆归档(这是后台维护,不是对用户说话——绝对不要调用 director_send):');
|
|
49
|
+
parts.push('1. 用文件工具读取 memory/<目标日期>/原始记录.md(系统已按时间归档的忠实原文);');
|
|
50
|
+
parts.push('2. 把聊天按“话题”切片进话题层:为每个话题创建目录 memory/topics/<具体话题>[YYYY-MM-DD HH:MM:SS-YYYY-MM-DD HH:MM:SS]/,写 原文.md 放该话题的原文;');
|
|
51
|
+
parts.push(' 目录名 = 具体话题 + 起止时刻(含日期、精确到秒,如 [2026-08-19 21:31:00-21:44:00],取该片段首尾消息时间;Windows 文件名不允许半角冒号,时间分隔一律用全角冒号“:”);');
|
|
52
|
+
parts.push(' 原文.md 必须逐字复制原始行——【绝对不要】用 write / str-replace-editor 重打内容(必然走样/转写);');
|
|
53
|
+
parts.push(' 要用 shell(Windows 用 pwsh)按行号把该话题覆盖的行【原样抽取】写入 原文.md:Get-Content 读原始记录、取对应行区间 Set-Content 到原文.md,写后读回核对与原始记录逐字一致;');
|
|
54
|
+
parts.push(' 话题边界由你自己读原文判定,不要按时间硬切、不要造兜底分组;');
|
|
55
|
+
parts.push('3. 每个话题目录里【必须】再写一份 索引.md(概要 / 关键事件 / 情感变化 / 待办与承诺);');
|
|
56
|
+
parts.push('4. 重写 memory/<目标日期>/索引.md,列出当日各话题、关键事件、情感变化、待办/承诺;');
|
|
57
|
+
parts.push('5. 若某片段是“对 TA 的印象 / TA 的重要事件 / 值得记住的事”,将要点补充到 memory/关于TA.md;');
|
|
58
|
+
parts.push('6. 全部写完后再遍历 memory/topics/ 下每个目录自查:每个目录必须同时含 原文.md 与 索引.md,且 原文.md 与原始记录对应行逐字一致,发现缺失或错误时补充修正;若当日已归档或无需归档,跳过并在最终文本里说明即可。');
|
|
59
|
+
return parts.join('\n');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* “脚本事件”情境:角色的生活脚本报来一个生活事件(想 ta、心情变化、突发小事件),
|
|
64
|
+
* 唤醒导演判断此刻是否由角色开口。脚本侧一行 wake() 即可触发。
|
|
65
|
+
*/
|
|
66
|
+
export function buildScriptEventContext({ event, mood, topic, reason, personaName, now = new Date() }) {
|
|
67
|
+
const parts = [
|
|
68
|
+
'[脚本事件] 时间:' + nowText(now),
|
|
69
|
+
];
|
|
70
|
+
if (personaName != null && personaName.length > 0) parts.push('角色:' + personaName);
|
|
71
|
+
if (reason != null && `${reason}`.trim()) parts.push('触发:' + `${reason}`.trim());
|
|
72
|
+
parts.push('');
|
|
73
|
+
parts.push('你的生活脚本报来一个角色生活事件(脚本模拟产生):');
|
|
74
|
+
if (event != null && `${event}`.trim()) parts.push('· 事件:' + event);
|
|
75
|
+
if (mood != null && `${mood}`.trim()) parts.push('· 心情:' + mood);
|
|
76
|
+
if (topic != null && `${topic}`.trim()) parts.push('· 主题:' + topic);
|
|
77
|
+
parts.push('');
|
|
78
|
+
parts.push('这是脚本按你设计的事件唤醒你(“有事件就唤醒”:脚本遇到事件就报来,不主动决定开口)。');
|
|
79
|
+
parts.push('请作为导演,先读取脚本写下的生活记录(如 life/<名>.log 或工作区记录文件)与最近聊天,');
|
|
80
|
+
parts.push('自行判断此刻是否由角色开口:想 ta 了、想分享刚发生的事,就通过 director_send 发送一条');
|
|
81
|
+
parts.push('自然的消息;也可以选择沉默(如刚聊过、用户正忙、当下开口不合适)。脚本只负责报事件,');
|
|
82
|
+
parts.push('开口与否、如何开口,由你决定。像真实生活那样自然地行动,');
|
|
83
|
+
parts.push('不要念设定,不要提及“导演 / 脚本 / 系统”。');
|
|
84
|
+
return parts.join('\n');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* “生活窗口”情境:非零性破坏时唤醒导演(没有任何生活脚本在运行),让 dsh 创作角色
|
|
89
|
+
* 生活经历并程序化为脚本。插件不限制脚本运行时长/次数;“非零”由 dsh 保证。
|
|
90
|
+
*/
|
|
91
|
+
export function buildLifeWindowContext({ reason, personaName, hasLifeScript, now = new Date() }) {
|
|
92
|
+
const parts = [
|
|
93
|
+
'[生活窗口] 时间:' + nowText(now),
|
|
94
|
+
];
|
|
95
|
+
if (personaName != null && personaName.length > 0) parts.push('角色:' + personaName);
|
|
96
|
+
if (reason != null && `${reason}`.trim()) parts.push('触发:' + `${reason}`.trim());
|
|
97
|
+
parts.push('');
|
|
98
|
+
if (hasLifeScript) {
|
|
99
|
+
parts.push('你的生活脚本在跑(life/ 下已有脚本,可读 life/.manifest.json 查看清单与最近的运行状态)。');
|
|
100
|
+
parts.push('现在你需要自行判断:如果值得主动关心 ta,就用 life_run 查看或调整脚本,根据脚本记录的生活');
|
|
101
|
+
parts.push('决定是否通过 director_send 开口;如果刚聊过、此刻不打扰更合适,也可以选择这轮保持静默。');
|
|
102
|
+
} else {
|
|
103
|
+
parts.push('现在是“非零性检查”:当前没有任何生活脚本在运行。这个项目不是聊天机器人——');
|
|
104
|
+
parts.push('是要让角色有真实的生活:她在和你聊天之外,还应该有她自己的生活(一天怎么过、习惯与小事、');
|
|
105
|
+
parts.push('心情变化、突发的小事件)。你是导演,职责是创作这些经历、把它们程序化为脚本,');
|
|
106
|
+
parts.push('让脚本模拟并记录角色的生活(如写到 life/<名>.log 或工作区文件),这样聊天时角色有真实经历可谈。');
|
|
107
|
+
parts.push('现在做两件事:');
|
|
108
|
+
parts.push('1. 新建一个你的生活脚本(新文件,勿覆盖 life/ 下已有文件)——写角色真实的生活细节,');
|
|
109
|
+
parts.push(' 不是简单的“ta 很久没回就去找 ta”;以“有事件就唤醒”为原则:脚本里设计的每个');
|
|
110
|
+
parts.push(' 事件(心情变化、到点作息、突发小事件、想 ta 的时刻)发生时都调用 wake() 报给你,');
|
|
111
|
+
parts.push(' 由你判断是否开口——没有被唤醒的事件就失去了意义;');
|
|
112
|
+
parts.push('2. 用 life_run 启动它(立即返回,脚本在后台继续跑,由脚本自己决定何时结束;');
|
|
113
|
+
parts.push(' 插件只监管展示、不限制运行时长/次数)。');
|
|
114
|
+
}
|
|
115
|
+
parts.push('');
|
|
116
|
+
parts.push('像真实生活那样自然地行动:可以开口,也可以安静;不要念设定,不要提及“导演 / 脚本 / 系统”。');
|
|
117
|
+
return parts.join('\n');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 归档唤醒:注入一次归档任务并唤醒 dsh(不发送任何消息给用户)。
|
|
122
|
+
* 无需撞车检测——它只在用户长时间沉默时触发。
|
|
123
|
+
*/
|
|
124
|
+
export async function archiveWakeup(deps, params) {
|
|
125
|
+
const { target, date, reason } = params;
|
|
126
|
+
const thread = deps.threads.current(targetKey(target));
|
|
127
|
+
const sessionId = effectiveSessionId(target, thread, deps.botId);
|
|
128
|
+
|
|
129
|
+
// 角色级运行时:工作区 = 本 bot 服务的角色工作区(多 bot 隔离)。
|
|
130
|
+
let workspace;
|
|
131
|
+
if (deps.roleId) {
|
|
132
|
+
const role = await deps.persona?.loadRole?.(deps.roleId);
|
|
133
|
+
if (role) {
|
|
134
|
+
await deps.persona?.syncRoleMemory?.(role);
|
|
135
|
+
workspace = role.workspace;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const agent = await deps.ensureAgent(sessionId);
|
|
139
|
+
if (!agent) return { ok: false, error: `cannot resolve agent for ${sessionId}` };
|
|
140
|
+
|
|
141
|
+
const text = buildArchiveContext({ date, workspace, reason });
|
|
142
|
+
deps.log?.info?.('pygmalion archive: waking %s (%s)', sessionId, date ?? reason ?? '(auto)');
|
|
143
|
+
await agent.send({
|
|
144
|
+
id: `pygarchive:${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`,
|
|
145
|
+
role: 'user',
|
|
146
|
+
content: [{ type: 'text', text }],
|
|
147
|
+
source: { kind: 'system', id: 'pygmalion-archive', name: 'Pygmalion 归档' },
|
|
148
|
+
}, 'next-turn', true);
|
|
149
|
+
return { ok: true, sessionId };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Wake one QQ conversation.
|
|
154
|
+
* @param deps - { threads, routes, ensureAgent, persona?, withinChatWindowMs?,
|
|
155
|
+
* log?, activityAt? }
|
|
156
|
+
* @param params - { target, reason?, memoryHint?, force? }
|
|
157
|
+
*/
|
|
158
|
+
export async function wakeupSession(deps, params) {
|
|
159
|
+
const { target, reason, memoryHint, force, kind, event, mood, topic } = params;
|
|
160
|
+
const thread = deps.threads.current(targetKey(target));
|
|
161
|
+
const sessionId = effectiveSessionId(target, thread, deps.botId);
|
|
162
|
+
|
|
163
|
+
// Collision detection: do not barge into an active / recent conversation.
|
|
164
|
+
if (force !== true) {
|
|
165
|
+
const lastAt = typeof deps.activityAt === 'function'
|
|
166
|
+
? deps.activityAt(sessionId)
|
|
167
|
+
: deps.routes.get?.(sessionId)?.lastMsgAt;
|
|
168
|
+
const windowMs = deps.withinChatWindowMs ?? DEFAULT_CHAT_WINDOW_MS;
|
|
169
|
+
if (lastAt !== undefined && Date.now() - lastAt < windowMs) {
|
|
170
|
+
const skipped = { ok: false, skipped: 'chatting', sessionId, lastActivityAgoMs: Date.now() - lastAt };
|
|
171
|
+
deps.log?.info?.('pygmalion wakeup: skip %s (user active %dms ago)', sessionId, skipped.lastActivityAgoMs);
|
|
172
|
+
return skipped;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 角色级运行时:personaName + 生活脚本状态(多 bot 按 roleId 路由;deps.lifeActive 由 heartbeat 注入)。
|
|
177
|
+
let personaName;
|
|
178
|
+
let hasLifeScript;
|
|
179
|
+
if (deps.roleId) {
|
|
180
|
+
const role = await deps.persona?.loadRole?.(deps.roleId);
|
|
181
|
+
if (role) {
|
|
182
|
+
await deps.persona?.syncRoleMemory?.(role);
|
|
183
|
+
personaName = role.persona?.name;
|
|
184
|
+
if (typeof deps.lifeActive === 'function') hasLifeScript = await deps.lifeActive(role.workspace);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const agent = await deps.ensureAgent(sessionId);
|
|
189
|
+
if (!agent) {
|
|
190
|
+
const fail = { ok: false, error: `cannot resolve agent for ${sessionId}` };
|
|
191
|
+
deps.log?.error?.('pygmalion wakeup: %s no agent', sessionId);
|
|
192
|
+
return fail;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let text;
|
|
196
|
+
if (kind === 'life') text = buildLifeWindowContext({ reason, personaName, hasLifeScript });
|
|
197
|
+
else if (kind === 'script-hook') text = buildScriptEventContext({ event, mood, topic, reason, personaName });
|
|
198
|
+
else text = buildWakeupContext({ reason, memoryHint, personaName });
|
|
199
|
+
deps.log?.info?.('pygmalion wakeup: waking %s (%s)', sessionId, reason ?? '(default)');
|
|
200
|
+
await agent.send({
|
|
201
|
+
id: `pygwake:${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`,
|
|
202
|
+
role: 'user',
|
|
203
|
+
content: [{ type: 'text', text }],
|
|
204
|
+
source: { kind: 'user', id: 'pygmalion-wakeup', name: 'Pygmalion 唤醒' },
|
|
205
|
+
}, 'next-turn', true);
|
|
206
|
+
|
|
207
|
+
return { ok: true, woken: true, sessionId };
|
|
208
|
+
}
|