pygmalion 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +141 -70
- package/THIRD_PARTY.md +9 -0
- package/docs/ARCHITECTURE.md +66 -108
- package/docs/DEPLOY.md +33 -137
- package/docs/DEVELOPMENT.md +63 -0
- package/docs/DEVICE.md +56 -47
- package/docs/EXTENSIONS.md +176 -0
- package/docs/PRESET.md +21 -63
- package/docs/UPGRADE-0.2.md +52 -0
- package/package.json +52 -33
- package/preset/pygmalion-director/cordis.patch.yml +234 -0
- package/preset/pygmalion-director/pygmalion-tools.js +43 -41
- package/scripts/start-local-test.mjs +45 -45
- package/skills/pygmalion-life-scripts/SKILL.md +12 -0
- package/skills/pygmalion-meme-maintenance/SKILL.md +12 -0
- package/skills/pygmalion-memory-archive/SKILL.md +24 -0
- package/skills/pygmalion-toy-control/SKILL.md +16 -0
- package/src/agent-life.js +608 -606
- package/src/archive-tools.js +9 -0
- package/src/bot-factory.js +129 -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 +36 -0
- package/src/roleplay-materials.js +113 -0
- package/src/roleplay-output.js +19 -0
- package/src/roleplay-parallel.js +239 -0
- package/src/roleplay-session.js +128 -0
- package/src/roleplay-worker.js +10 -0
- package/src/session-log.js +180 -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/session-log.js
CHANGED
|
@@ -1,172 +1,180 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pygmalion 会话日志读取:从 dsh session.jsonl.zstd 程序化提取真实消息原文。
|
|
3
|
-
*
|
|
4
|
-
* dsh 把会话日志写为「多个 zstd frame 拼接」的 .jsonl.zstd(每个 frame 一段追加)。
|
|
5
|
-
* 本模块:
|
|
6
|
-
* 1) 按 zstd 帧魔数(28 b5 2f fd)切分;
|
|
7
|
-
* 2) 每帧用 node:zlib zstdDecompressSync 解压后拼接;
|
|
8
|
-
* 3) 逐行解析 JSON 事件,供 extractMessages 提取 user/assistant 原文。
|
|
9
|
-
*
|
|
10
|
-
* 文件路径:~/.dsh/sessions/<workspace-sanitized>/<sessionId(urlencoded)>/session.jsonl.zstd
|
|
11
|
-
*/
|
|
12
|
-
import { zstdDecompressSync } from 'node:zlib';
|
|
13
|
-
import { readFile } from 'node:fs/promises';
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
.
|
|
72
|
-
.
|
|
73
|
-
.
|
|
74
|
-
.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
/
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
*
|
|
98
|
-
* source.
|
|
99
|
-
* 修 D19:旧实现是黑名单(仅排除已知注入保留字),`[sys]服务器重启…` 等漏进聊天记录。
|
|
100
|
-
*/
|
|
101
|
-
export function isRealUserMessage(ev) {
|
|
102
|
-
const src = ev?.data?.source;
|
|
103
|
-
if (src === undefined || src.kind !== 'user') return false;
|
|
104
|
-
return typeof src.id === 'string' && QQ_OPENID_RE.test(src.id)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
//
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Pygmalion 会话日志读取:从 dsh session.jsonl.zstd 程序化提取真实消息原文。
|
|
3
|
+
*
|
|
4
|
+
* dsh 把会话日志写为「多个 zstd frame 拼接」的 .jsonl.zstd(每个 frame 一段追加)。
|
|
5
|
+
* 本模块:
|
|
6
|
+
* 1) 按 zstd 帧魔数(28 b5 2f fd)切分;
|
|
7
|
+
* 2) 每帧用 node:zlib zstdDecompressSync 解压后拼接;
|
|
8
|
+
* 3) 逐行解析 JSON 事件,供 extractMessages 提取 user/assistant 原文。
|
|
9
|
+
*
|
|
10
|
+
* 文件路径:~/.dsh/sessions/<workspace-sanitized>/<sessionId(urlencoded)>/session.jsonl.zstd
|
|
11
|
+
*/
|
|
12
|
+
import { zstdDecompressSync } from 'node:zlib';
|
|
13
|
+
import { readFile } from 'node:fs/promises';
|
|
14
|
+
import { receiptOf } from './delivery-receipt.js';
|
|
15
|
+
|
|
16
|
+
const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
|
|
17
|
+
|
|
18
|
+
/** 按 zstd 帧魔数把 buffer 切成多个独立帧(每帧可独立解压)。 */
|
|
19
|
+
export function zstdFrames(buffer) {
|
|
20
|
+
const starts = [];
|
|
21
|
+
for (let i = 0; i <= buffer.length - 4; i++) {
|
|
22
|
+
if (buffer[i] === ZSTD_MAGIC[0] && buffer[i + 1] === ZSTD_MAGIC[1]
|
|
23
|
+
&& buffer[i + 2] === ZSTD_MAGIC[2] && buffer[i + 3] === ZSTD_MAGIC[3])
|
|
24
|
+
starts.push(i);
|
|
25
|
+
}
|
|
26
|
+
if (starts.length === 0) return [buffer];
|
|
27
|
+
const out = [];
|
|
28
|
+
for (let k = 0; k < starts.length; k++)
|
|
29
|
+
out.push(buffer.subarray(starts[k], k + 1 < starts.length ? starts[k + 1] : buffer.length));
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 读取并解压一个会话文件,返回完整文本。 */
|
|
34
|
+
export async function readSessionText(file) {
|
|
35
|
+
const buf = await readFile(file);
|
|
36
|
+
return Buffer.concat(zstdFrames(buf).map((f) => zstdDecompressSync(f))).toString('utf8');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 解析 jsonl 文本为事件对象数组(忽略空行/坏行)。 */
|
|
40
|
+
export function parseEvents(text) {
|
|
41
|
+
const out = [];
|
|
42
|
+
for (const line of text.split('\n')) {
|
|
43
|
+
const l = line.trim();
|
|
44
|
+
if (!l) continue;
|
|
45
|
+
try { out.push(JSON.parse(l)); } catch { /* skip */ }
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 从事件里取用户消息文本(agent/inbox/spliced → inserted[].role=user → 文本块)。 */
|
|
51
|
+
export function extractUserMessages(events) {
|
|
52
|
+
const out = [];
|
|
53
|
+
for (const ev of events) {
|
|
54
|
+
if (ev?.type === 'agent/inbox/spliced' && Array.isArray(ev.data?.inserted)) {
|
|
55
|
+
for (const m of ev.data.inserted) {
|
|
56
|
+
if (m?.role !== 'user') continue;
|
|
57
|
+
const text = (m.content || []).filter((b) => b.type === 'text').map((b) => b.text).join('');
|
|
58
|
+
if (text && text.trim()) out.push({ id: m.id, time: ev.time, text });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 模型侧上下文行(固定结构,非用户原话):[时间线] 时间戳行、[生活脚本] 非零提示行、[身份提醒] 边界提示行、[已下载附件:…] 行。 */
|
|
66
|
+
const CONTEXT_LINE = /^\[(?:时间线|生活脚本|身份提醒|设备)\]/;
|
|
67
|
+
|
|
68
|
+
/** 剥离模型侧包装([时间线]…、[生活脚本]…、[已下载附件:…]、[引用消息开始]…),只留用户原话。 */
|
|
69
|
+
export function cleanUserText(raw) {
|
|
70
|
+
let out = String(raw ?? '')
|
|
71
|
+
.replace(/\[引用消息开始\][\s\S]*?\[引用消息结束\]/g, '')
|
|
72
|
+
.split('\n')
|
|
73
|
+
.map((l) => l.trim())
|
|
74
|
+
.filter((l) => l.length > 0 && !CONTEXT_LINE.test(l) && !/^\[已下载附件:/.test(l))
|
|
75
|
+
.join('\n');
|
|
76
|
+
return out.trim();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** dsh 系统注入(非真实对话)识别:runtime context / 审批策略等。 */
|
|
80
|
+
const SYSTEM_PATTERNS = [
|
|
81
|
+
/^Current runtime context/im,
|
|
82
|
+
/Current DSH file policy/im,
|
|
83
|
+
/Approval policy: ask/im,
|
|
84
|
+
/^This snapshot supersedes/im,
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
/** 是否系统注入文本(不归档为对话原文)。 */
|
|
88
|
+
export function isSystemText(text) {
|
|
89
|
+
const t = String(text ?? '');
|
|
90
|
+
return SYSTEM_PATTERNS.some((re) => re.test(t));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** QQ 官方 C2C openid:32 位十六进制(真实用户消息的结构标识;[sys] 等系统注入无此 id)。 */
|
|
94
|
+
const QQ_OPENID_RE = /^[0-9A-F]{32}$/;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 真实 QQ 用户消息白名单:旧日志使用 QQ openid source.id,0.2.1 使用带插件命名空间
|
|
98
|
+
* 的 source.rpcId。未知来源、唤醒、管理页推送不算真人闲聊。
|
|
99
|
+
* 修 D19:旧实现是黑名单(仅排除已知注入保留字),`[sys]服务器重启…` 等漏进聊天记录。
|
|
100
|
+
*/
|
|
101
|
+
export function isRealUserMessage(ev) {
|
|
102
|
+
const src = ev?.data?.source;
|
|
103
|
+
if (src === undefined || src.kind !== 'user') return false;
|
|
104
|
+
return (typeof src.id === 'string' && QQ_OPENID_RE.test(src.id))
|
|
105
|
+
|| (typeof src.rpcId === 'string' && (src.rpcId.startsWith('pygmalion.qq:') || src.rpcId.startsWith('pygmalion.inbound:')));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 从会话事件中提取完整对话原文(按时间排序、去重):
|
|
110
|
+
* - 用户消息:`user/message`(唯一来源,规避 spliced 双计),
|
|
111
|
+
* 以 source 固定结构判定真人(排除唤醒/HTTP 推送/系统快照),并过滤系统注入;
|
|
112
|
+
* - 角色消息:`tool/call` 中 name === 'director_send' 的 arguments.messages(实际发出的消息)。
|
|
113
|
+
* 返回 [{ role: 'user'|'character', time, text }]。
|
|
114
|
+
*/
|
|
115
|
+
export function extractMessages(events) {
|
|
116
|
+
const userSeen = new Set();
|
|
117
|
+
const msgs = [];
|
|
118
|
+
// 新版工具调用只有在配对 tool/result 明确确认至少发送一条时才算真实角色消息。
|
|
119
|
+
// 旧版审查退回或参数校验失败同样会留下 tool/call,不能把未送达的候选写进长期原文。
|
|
120
|
+
const sentDirectorCalls = new Map();
|
|
121
|
+
for (const ev of events) {
|
|
122
|
+
if (ev?.type !== 'tool/result') continue;
|
|
123
|
+
const callId = ev.data?.message?.source?.callId;
|
|
124
|
+
const receipt = receiptOf(ev);
|
|
125
|
+
if (receipt !== undefined && callId != null) {
|
|
126
|
+
sentDirectorCalls.set(String(callId), { receipt });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const blocks = ev.data?.message?.content;
|
|
130
|
+
const rendered = (Array.isArray(blocks) ? blocks : [])
|
|
131
|
+
.flatMap((b) => b?.type === 'tool-result' ? (Array.isArray(b.content) ? b.content : []) : [b])
|
|
132
|
+
.filter((b) => b?.type === 'text' && typeof b.text === 'string')
|
|
133
|
+
.map((b) => b.text).join('');
|
|
134
|
+
const success = rendered.match(/^已发送\s+(\d+)\s+条消息/);
|
|
135
|
+
const partial = rendered.match(/失败前已发送\s+(\d+)\s+条消息/);
|
|
136
|
+
const count = Number(success?.[1] ?? partial?.[1] ?? 0);
|
|
137
|
+
const timeoutMatch = rendered.match(/第\s+([\d、,,\s]+)\s+条语音合成超时/);
|
|
138
|
+
const voiceTimeouts = new Set((timeoutMatch?.[1] ?? '').split(/[、,,\s]+/)
|
|
139
|
+
.map(Number).filter((n) => Number.isInteger(n) && n > 0));
|
|
140
|
+
if (callId != null && count > 0) sentDirectorCalls.set(String(callId), { count, voiceTimeouts });
|
|
141
|
+
}
|
|
142
|
+
const push = (role, time, text) => {
|
|
143
|
+
const clean = cleanUserText(text);
|
|
144
|
+
if (!clean || isSystemText(clean)) return;
|
|
145
|
+
msgs.push({ role, time: Number(time) || undefined, text: clean });
|
|
146
|
+
};
|
|
147
|
+
for (const ev of events) {
|
|
148
|
+
if (ev?.type === 'user/message' && isRealUserMessage(ev) && Array.isArray(ev.data?.content)) {
|
|
149
|
+
const t = ev.data.content.filter((b) => b.type === 'text').map((b) => b.text).join('');
|
|
150
|
+
// 按消息 id 去重(同 id 才是同一消息);无 id 时按 时间+文本 兜底。
|
|
151
|
+
// 不按文本单独去重:同一句话在不同时刻发送是真实的多条消息,必须都记(避免漏)。
|
|
152
|
+
const key = ev.data.id ?? `${ev.time}|${cleanUserText(t)}`;
|
|
153
|
+
if (userSeen.has(key)) continue;
|
|
154
|
+
userSeen.add(key);
|
|
155
|
+
push('user', ev.time, t);
|
|
156
|
+
} else if (ev?.type === 'tool/call' && ev.data?.name === 'director_send') {
|
|
157
|
+
// 旧记录可能没有 callId,继续兼容;有 callId 的新记录必须由 tool/result 证明已发送。
|
|
158
|
+
const delivery = ev.data.callId == null
|
|
159
|
+
? { count: Number.MAX_SAFE_INTEGER, voiceTimeouts: new Set() }
|
|
160
|
+
: sentDirectorCalls.get(String(ev.data.callId));
|
|
161
|
+
if (!delivery) continue;
|
|
162
|
+
try {
|
|
163
|
+
const args = JSON.parse(ev.data.arguments ?? '{}');
|
|
164
|
+
const all = Array.isArray(args.messages) ? args.messages : [];
|
|
165
|
+
const delivered = delivery.receipt
|
|
166
|
+
? delivery.receipt.messages.filter(item => item.status === 'accepted')
|
|
167
|
+
: all.slice(0, delivery.count).map((m, index) => ({ index,
|
|
168
|
+
type: m?.type === 'voice' && !delivery.voiceTimeouts.has(index + 1) ? 'voice' : 'text' }));
|
|
169
|
+
for (const item of delivered) {
|
|
170
|
+
const m = all[item.index];
|
|
171
|
+
if (typeof m === 'string') push('character', ev.time, m);
|
|
172
|
+
else if (m && typeof m.text === 'string') push('character', ev.time,
|
|
173
|
+
item.type === 'voice' ? `[语音] ${m.text}` : m.text);
|
|
174
|
+
}
|
|
175
|
+
} catch { /* skip malformed */ }
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
msgs.sort((a, b) => (a.time ?? 0) - (b.time ?? 0));
|
|
179
|
+
return msgs;
|
|
180
|
+
}
|
package/src/setup-agent.js
CHANGED
|
@@ -1,100 +1,92 @@
|
|
|
1
|
-
import { targetOfSession } from './inbound.js';
|
|
2
|
-
import {
|
|
3
|
-
import { attachSessionToCwdWorkspace } from './dsh-bookkeeping.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 构造 standing 层 pygmalion 工具执行所需的会话依赖(deps 需含
|
|
7
|
-
* api/routes/persona/roleId/httpApiToken/log)。target 为惰性闭包:调用时经
|
|
8
|
-
* routes.get(sessionId) 现取目标,因此同一 deps 可被 setup 与「已存在 agent 补
|
|
9
|
-
* 登记」两处复用。
|
|
10
|
-
*/
|
|
11
|
-
export function buildSessionDeps(deps, sessionId) {
|
|
12
|
-
const { api, routes, persona, roleId, httpApiToken, httpApiPath, log, subagents, llm, devices } = deps;
|
|
13
|
-
return {
|
|
14
|
-
api, routes,
|
|
15
|
-
target: () => routes.get(sessionId)?.target ?? targetOfSession(sessionId),
|
|
16
|
-
roleId, httpApiToken, httpApiPath, persona, log,
|
|
17
|
-
subagents, llm,
|
|
18
|
-
devices,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
//
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
log,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
outbound.registerApprovalAnswerer(agentCtx, sessionId);
|
|
96
|
-
routes.ensure(sessionId, target());
|
|
97
|
-
void attachSessionToCwdWorkspace(workspaceRegistry, defaultCwd, sessionId, log);
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
//# sourceMappingURL=setup-agent.js.map
|
|
1
|
+
import { targetOfSession } from './inbound.js';
|
|
2
|
+
import { registerStandingTools, CHAT_TOOL_NAMES } from './tools.js';
|
|
3
|
+
import { attachSessionToCwdWorkspace } from './dsh-bookkeeping.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 构造 standing 层 pygmalion 工具执行所需的会话依赖(deps 需含
|
|
7
|
+
* api/routes/persona/roleId/httpApiToken/log)。target 为惰性闭包:调用时经
|
|
8
|
+
* routes.get(sessionId) 现取目标,因此同一 deps 可被 setup 与「已存在 agent 补
|
|
9
|
+
* 登记」两处复用。
|
|
10
|
+
*/
|
|
11
|
+
export function buildSessionDeps(deps, sessionId) {
|
|
12
|
+
const { api, routes, persona, roleId, httpApiToken, httpApiPath, log, subagents, llm, devices, sessionPersistence } = deps;
|
|
13
|
+
return {
|
|
14
|
+
api, routes,
|
|
15
|
+
target: () => routes.get(sessionId)?.target ?? targetOfSession(sessionId),
|
|
16
|
+
roleId, httpApiToken, httpApiPath, persona, log,
|
|
17
|
+
subagents, llm, sessionPersistence,
|
|
18
|
+
devices,
|
|
19
|
+
identityScope: deps.identityScope,
|
|
20
|
+
beginDelivery: deps.beginDelivery,
|
|
21
|
+
voice: deps.voice,
|
|
22
|
+
workspaceOf: roleId !== undefined && persona !== undefined
|
|
23
|
+
? async () => persona.roleWorkspace(roleId)
|
|
24
|
+
: undefined,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createSetupAgent(deps) {
|
|
29
|
+
const { log, api, routes, outbound, workspaceRegistry, defaultCwd, resolvePreset, agentPresets, persona, roleId, httpApiToken, director } = deps;
|
|
30
|
+
const directorSvc = director?.get?.('pygmalion.director');
|
|
31
|
+
return async (agentCtx, agent) => {
|
|
32
|
+
const sessionId = agent.id;
|
|
33
|
+
if (typeof sessionId !== 'string') throw new Error('Pygmalion setup 需要新版 Agent 对象');
|
|
34
|
+
// 1. Join the agent's preset FIRST so its tools, prompt sections, and
|
|
35
|
+
// skill catalog are in scope before any QQ-only tool registers.
|
|
36
|
+
// `mount()` returns the preset that was actually mounted; on rejection
|
|
37
|
+
// we surface a clear error to `agents.create` so the agent never
|
|
38
|
+
// publishes half-configured.
|
|
39
|
+
let mountedPresetId;
|
|
40
|
+
if (agentPresets !== undefined && resolvePreset !== undefined) {
|
|
41
|
+
let presetId;
|
|
42
|
+
try {
|
|
43
|
+
presetId = (await resolvePreset(sessionId)).id;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
log.error('QQ setupAgent: resolving agent preset failed for %s: %o', sessionId, error);
|
|
47
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const mounted = await agentPresets.mount(agentCtx, presetId);
|
|
51
|
+
mountedPresetId = mounted.id;
|
|
52
|
+
log.info('QQ setupAgent: %s joined preset "%s"', sessionId, mounted.id);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
log.error('QQ setupAgent: mounting preset "%s" failed for %s: %o', presetId ?? '(default)', sessionId, error);
|
|
56
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
log.warn('QQ setupAgent: agentPresets service is absent; %s will run on the empty global layer '
|
|
61
|
+
+ '(only QQ-specific tools will be visible). Load an agent-presets plugin in the host composition.', sessionId);
|
|
62
|
+
}
|
|
63
|
+
// 2. 登记会话依赖到 pygmalion.director 宿主服务:preset 桥插件把 pygmalion 工具注册在
|
|
64
|
+
// standing 层(每进程一份),工具执行时按 exec.agent.id 从本表动态解析每会话依赖。
|
|
65
|
+
if (directorSvc !== undefined && typeof directorSvc.registerSession === 'function') {
|
|
66
|
+
directorSvc.registerSession(sessionId, buildSessionDeps(deps, sessionId));
|
|
67
|
+
}
|
|
68
|
+
// 3. 工具注册:导演 preset 已把 pygmalion 工具注册进 standing 层(与 fs/bash 同层,
|
|
69
|
+
// resume 恢复会话后 dispatch 依然可达)→ 跳过 own 层注册;
|
|
70
|
+
// 否则(非导演 preset / 无 agentPresets / 旧版 preset 缺桥插件行)回退 own 层注册,保持旧行为。
|
|
71
|
+
const target = () => routes.get(sessionId)?.target ?? targetOfSession(sessionId);
|
|
72
|
+
const standingReady = mountedPresetId === 'pygmalion-director'
|
|
73
|
+
&& directorSvc !== undefined && typeof directorSvc.hasStandingTools === 'function'
|
|
74
|
+
&& directorSvc.hasStandingTools();
|
|
75
|
+
if (standingReady) {
|
|
76
|
+
log.debug?.('QQ setupAgent: %s 使用 standing 层 pygmalion 工具(preset=%s),跳过 own 层注册', sessionId, mountedPresetId);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const okDir = registerStandingTools(agentCtx, async () => buildSessionDeps(deps, sessionId), CHAT_TOOL_NAMES);
|
|
80
|
+
if (okDir === true) {
|
|
81
|
+
log.debug?.('QQ setupAgent: 常驻聊天工具 registered for %s (own layer)', sessionId);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
log.warn?.(`QQ setupAgent: director tools could NOT be registered for %s (agentCtx.tools unavailable)`, sessionId);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
outbound.registerApprovalAnswerer(agentCtx, sessionId);
|
|
88
|
+
routes.ensure(sessionId, target());
|
|
89
|
+
void attachSessionToCwdWorkspace(workspaceRegistry, agent.session.header.cwd ?? defaultCwd, sessionId, log);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=setup-agent.js.map
|