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
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 单 bot 管线工厂:把一个 bot 记录(data/bots/<id>.json)构建成完整接入管线。
|
|
3
|
+
*
|
|
4
|
+
* 由 BotManager 在启动/运行时调用:start() 内包一个 `ctx.effect`(活跃 fiber 内注册
|
|
5
|
+
* `ctx.on('agent/disposed')` 与 dsh `webServer.register`,effect teardown 即注销),
|
|
6
|
+
* stop() 就是该 effect 返回的 disposer。store 文件名按 botId 隔离(qq-<botId>-*.json)。
|
|
7
|
+
*/
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { QQApi } from './qqapi.js';
|
|
10
|
+
import { QQGateway } from './gateway.js';
|
|
11
|
+
import { RefIndexStore } from './refindex.js';
|
|
12
|
+
import { AlwaysAllowStore } from './store.js';
|
|
13
|
+
import { InboundPipeline, normalizeMessage, SESSION_PREFIX, effectiveSessionId, targetOfSession } from './inbound.js';
|
|
14
|
+
import { OutboundPipeline } from './outbound.js';
|
|
15
|
+
import { createSlashHandler } from './slash-commands.js';
|
|
16
|
+
import { targetKey } from './threads.js';
|
|
17
|
+
import { resolveHttpApiMount, createHttpApiHandler } from './http-api.js';
|
|
18
|
+
import * as personaLib from './persona.js';
|
|
19
|
+
import { BotConfig } from './config.js';
|
|
20
|
+
import { defineChannelAdapter } from './extensions.js';
|
|
21
|
+
|
|
22
|
+
const ROUTE_TTL = 7 * 24 * 3600 * 1000; // 7 天无用户活动的会话路由修剪(修 D12)
|
|
23
|
+
|
|
24
|
+
/** Strict, narrow validator for QQ INTERACTION_CREATE payloads. */
|
|
25
|
+
function asInteractionButton(payload) {
|
|
26
|
+
if (payload === null || typeof payload !== 'object')
|
|
27
|
+
return undefined;
|
|
28
|
+
const root = payload;
|
|
29
|
+
if (typeof root.id !== 'string')
|
|
30
|
+
return undefined;
|
|
31
|
+
const data = root.data;
|
|
32
|
+
if (data === null || typeof data !== 'object')
|
|
33
|
+
return undefined;
|
|
34
|
+
const resolved = data.resolved;
|
|
35
|
+
if (resolved === null || typeof resolved !== 'object')
|
|
36
|
+
return undefined;
|
|
37
|
+
const buttonData = resolved.button_data;
|
|
38
|
+
if (typeof buttonData !== 'string')
|
|
39
|
+
return undefined;
|
|
40
|
+
return { id: root.id, buttonData };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param deps 全局依赖(index.js apply 解析):
|
|
45
|
+
* agents / sessionPersistence / workspaceRegistry / attachments / approval /
|
|
46
|
+
* agentPresets / webServer(均来自 ctx)/ persona / log / storages(目录)
|
|
47
|
+
* @param bot bot 记录(已过 BotConfig schema 校验)
|
|
48
|
+
* @param ctx 宿主 context(注册/注销 agent 生命周期监听)
|
|
49
|
+
* @returns {
|
|
50
|
+
* bot, facade, // facade 供 heartbeat:{ botId, roleId, routes, threads, inbound, withinChatWindowMs, httpApiToken }
|
|
51
|
+
* getStatus(), // 最近一次 gateway 连接状态
|
|
52
|
+
* start(), stop() // 启动/停管线(幂等)
|
|
53
|
+
* }
|
|
54
|
+
*/
|
|
55
|
+
function createQqTransport(deps, bot, ctx, host) {
|
|
56
|
+
const { log, persona, config } = deps;
|
|
57
|
+
const ns = `qq-${bot.id}`;
|
|
58
|
+
const api = new QQApi(bot, log);
|
|
59
|
+
const refIndex = new RefIndexStore(join(deps.storages, `${ns}-refindex.jsonl`));
|
|
60
|
+
const routes = host.routes;
|
|
61
|
+
const alwaysAllow = new AlwaysAllowStore(join(deps.storages, `${ns}-always-allow.json`));
|
|
62
|
+
const threads = host.threads;
|
|
63
|
+
const inbound = new InboundPipeline({
|
|
64
|
+
config: bot,
|
|
65
|
+
api,
|
|
66
|
+
log,
|
|
67
|
+
refIndex,
|
|
68
|
+
routes,
|
|
69
|
+
agents: deps.agents,
|
|
70
|
+
sessionPersistence: deps.sessionPersistence,
|
|
71
|
+
attachments: deps.attachments,
|
|
72
|
+
llm: ctx.get('llm'),
|
|
73
|
+
approval: deps.approval,
|
|
74
|
+
threads,
|
|
75
|
+
persona,
|
|
76
|
+
devices: deps.devices,
|
|
77
|
+
roleId: bot.roleId,
|
|
78
|
+
botId: bot.id,
|
|
79
|
+
botAppId: bot.qqAppId,
|
|
80
|
+
acceptIncoming: message => host.accept({ id: message.id, peerId: message.senderId }),
|
|
81
|
+
});
|
|
82
|
+
const outbound = new OutboundPipeline({
|
|
83
|
+
config: bot,
|
|
84
|
+
api,
|
|
85
|
+
log,
|
|
86
|
+
routes,
|
|
87
|
+
alwaysAllow,
|
|
88
|
+
inbound,
|
|
89
|
+
persona,
|
|
90
|
+
botId: bot.id,
|
|
91
|
+
isDirector: (sessionId) => personaLib.isRoleDirectorSession(bot.roleId, sessionId),
|
|
92
|
+
});
|
|
93
|
+
outbound.bind(ctx);
|
|
94
|
+
const onSlashCommand = createSlashHandler({
|
|
95
|
+
log, alwaysAllow, threads, agents: deps.agents, approval: deps.approval,
|
|
96
|
+
outbound, agentPresets: deps.agentPresets, botId: bot.id,
|
|
97
|
+
newParallelSession: message=>deps.director?.get?.('pygmalion.director')?.newParallelSession?.({roleId:bot.roleId,userId:message.senderId,target:message.reply,
|
|
98
|
+
identityScope:{adapterId:'qq-official',accountId:bot.qqAppId}})??{status:'unavailable'},
|
|
99
|
+
});
|
|
100
|
+
inbound.roleAgents = host.roleAgents;
|
|
101
|
+
inbound.attachHooks({ onSlashCommand, setupAgent: host.setupAgent, registerSessionContext: host.registerSessionContext });
|
|
102
|
+
|
|
103
|
+
// 可选 HTTP 推送/唤醒 API:按 botId 自动生成 path,token 由 BotManager 生成。
|
|
104
|
+
// 宿主无 webServer 时跳过挂载(status 标 unavailable),不 throw。
|
|
105
|
+
let httpApiMount;
|
|
106
|
+
let httpApiUnavailable = false;
|
|
107
|
+
if (bot.httpApiToken) {
|
|
108
|
+
httpApiMount = resolveHttpApiMount({ path: bot.httpApiPath, token: bot.httpApiToken });
|
|
109
|
+
if (deps.webServer === undefined) {
|
|
110
|
+
httpApiMount = undefined;
|
|
111
|
+
httpApiUnavailable = true;
|
|
112
|
+
log.warn('pygmalion: bot %s 配置了 httpApiToken,但宿主无 webServer(该 API 仅在 dsh web 等 Web 组合下可用),已跳过挂载', bot.id);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let status = { state: 'stopped', detail: '' };
|
|
117
|
+
const setStatus = (state, detail = '') => { status = { state, detail }; };
|
|
118
|
+
|
|
119
|
+
const gateway = new QQGateway(bot, api, {
|
|
120
|
+
onMessage: (payload, eventType) => {
|
|
121
|
+
const message = normalizeMessage(payload, eventType);
|
|
122
|
+
if (message === undefined)
|
|
123
|
+
return;
|
|
124
|
+
void inbound.handle(message).catch((error) => {
|
|
125
|
+
log.error('inbound handling failed (%s): %o', bot.id, error);
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
onInteraction: (payload) => {
|
|
129
|
+
const interaction = asInteractionButton(payload);
|
|
130
|
+
if (interaction === undefined)
|
|
131
|
+
return;
|
|
132
|
+
if (outbound.handleApprovalCallback(interaction.buttonData) !== undefined) {
|
|
133
|
+
void api.ackInteraction(interaction.id).catch((error) => {
|
|
134
|
+
log.warn('interaction ack failed: %o', error);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
onReady: () => { log.info('QQ gateway ready (bot=%s appid=%s)', bot.id, api.appId()); },
|
|
139
|
+
onStatus: setStatus,
|
|
140
|
+
}, log, join(deps.storages, `${ns}-gateway-session.json`));
|
|
141
|
+
|
|
142
|
+
let dispose = null;
|
|
143
|
+
let started = false;
|
|
144
|
+
let stopping = false;
|
|
145
|
+
|
|
146
|
+
function start() {
|
|
147
|
+
if (started || stopping)
|
|
148
|
+
return;
|
|
149
|
+
started = true;
|
|
150
|
+
// ctx.effect 返回 disposer;effect 运行时 fiber 活跃,ctx.on / webServer.register 均合法。
|
|
151
|
+
dispose = ctx.effect(() => {
|
|
152
|
+
const stops = [];
|
|
153
|
+
void refIndex.init();
|
|
154
|
+
void Promise.resolve().then(() => {
|
|
155
|
+
try {
|
|
156
|
+
routes.prune(ROUTE_TTL);
|
|
157
|
+
alwaysAllow.prune(routes.entries().map((e) => e.sessionId));
|
|
158
|
+
}
|
|
159
|
+
catch { /* ignore */ }
|
|
160
|
+
});
|
|
161
|
+
void alwaysAllow.load();
|
|
162
|
+
void gateway.start();
|
|
163
|
+
const pruneTimer = setInterval(() => {
|
|
164
|
+
try {
|
|
165
|
+
routes.prune(ROUTE_TTL);
|
|
166
|
+
alwaysAllow.prune(routes.entries().map((e) => e.sessionId));
|
|
167
|
+
}
|
|
168
|
+
catch { /* ignore */ }
|
|
169
|
+
}, 6 * 3600 * 1000);
|
|
170
|
+
pruneTimer.unref?.();
|
|
171
|
+
stops.push(() => clearInterval(pruneTimer));
|
|
172
|
+
const onDisposed = ctx.on('agent/disposed', ({ agent }) => {
|
|
173
|
+
if (!agent.id.startsWith(`${SESSION_PREFIX}:${bot.id}:`))
|
|
174
|
+
return;
|
|
175
|
+
deps.director?.get?.('pygmalion.director')?.unregisterSession?.(agent.id);
|
|
176
|
+
void outbound.closeStream(agent.id);
|
|
177
|
+
inbound.onAgentDisposed(agent.id);
|
|
178
|
+
});
|
|
179
|
+
stops.push(() => { if (typeof onDisposed === 'function') onDisposed(); });
|
|
180
|
+
if (httpApiMount !== undefined && deps.webServer !== undefined) {
|
|
181
|
+
stops.push(deps.webServer.register({
|
|
182
|
+
kind: 'prefix',
|
|
183
|
+
path: httpApiMount.path,
|
|
184
|
+
handler: createHttpApiHandler({
|
|
185
|
+
subagents: ctx.get('subagents'),
|
|
186
|
+
mountPath: httpApiMount.path,
|
|
187
|
+
token: httpApiMount.token,
|
|
188
|
+
config: bot,
|
|
189
|
+
log,
|
|
190
|
+
api,
|
|
191
|
+
routes,
|
|
192
|
+
threads,
|
|
193
|
+
ensureAgent: (sessionId) => inbound.ensureAgent(sessionId),
|
|
194
|
+
persona,
|
|
195
|
+
devices: deps.devices,
|
|
196
|
+
botId: bot.id,
|
|
197
|
+
roleId: bot.roleId,
|
|
198
|
+
activityAt: (sessionId) => inbound.userActivityAt(sessionId),
|
|
199
|
+
withinChatWindowMs: bot.withinChatWindowMs,
|
|
200
|
+
scriptWakeMinIntervalMs: config?.heartbeat?.scriptWakeMinIntervalMs ?? 600000,
|
|
201
|
+
}),
|
|
202
|
+
}));
|
|
203
|
+
log.info('pygmalion: httpApi mounted for bot %s at %s', bot.id, httpApiMount.path);
|
|
204
|
+
}
|
|
205
|
+
return () => {
|
|
206
|
+
stops.forEach((fn) => { try { fn(); } catch { /* ignore */ } });
|
|
207
|
+
outbound.disposeAllStreams();
|
|
208
|
+
inbound.dispose();
|
|
209
|
+
gateway.dispose();
|
|
210
|
+
api.dispose();
|
|
211
|
+
void outbound.dispose();
|
|
212
|
+
started = false;
|
|
213
|
+
dispose = null;
|
|
214
|
+
setStatus('stopped');
|
|
215
|
+
};
|
|
216
|
+
}, `pygmalion.bot:${bot.id}()`);
|
|
217
|
+
return dispose;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function stop() {
|
|
221
|
+
stopping = true;
|
|
222
|
+
const d = dispose;
|
|
223
|
+
dispose = null;
|
|
224
|
+
try { if (typeof d === 'function') d(); } catch { /* ignore */ }
|
|
225
|
+
stopping = false;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
api.replyContext = sessionId => {
|
|
229
|
+
const record = routes.get(sessionId);
|
|
230
|
+
return record && routes.consumePassive(sessionId, record.target.kind === 'group' ? 300_000 : 1_800_000, bot.replyPassiveLimit);
|
|
231
|
+
};
|
|
232
|
+
return { api, inbound, outbound, getStatus: () => status, start, dispose: stop, httpApiUnavailable,
|
|
233
|
+
targetForPeer: peerId => ({ kind: 'c2c', userId: peerId }),
|
|
234
|
+
targetForSession: targetOfSession,
|
|
235
|
+
sessionIdFor: target => effectiveSessionId(target, threads.current(targetKey(target)), bot.id),
|
|
236
|
+
mediaTypes: ['image', 'voice', 'video', 'file'],
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** QQ 的凭据、网关、被动回复与旧 ID 均留在实现内部;通过相同连接工厂注册。 */
|
|
241
|
+
export function createQqAdapter(deps, ctx) {
|
|
242
|
+
return defineChannelAdapter({ apiVersion: 1, id: 'qq-official', name: 'QQ 官方机器人',
|
|
243
|
+
ownsHttpApi: true,
|
|
244
|
+
storageNamespace: id => `qq-${id}`,
|
|
245
|
+
fields: [
|
|
246
|
+
{key:'qqAppId',type:'text',label:'AppID',required:true},
|
|
247
|
+
{key:'qqSecret',type:'secret',label:'AppSecret',required:true},
|
|
248
|
+
{key:'sandbox',type:'boolean',label:'沙箱环境',default:true},
|
|
249
|
+
{key:'streaming',type:'boolean',label:'流式回复',default:false},
|
|
250
|
+
],
|
|
251
|
+
accountId: config => config.qqAppId,
|
|
252
|
+
normalizeConfig(config, record) {
|
|
253
|
+
const result = BotConfig['~standard'].validate({...record,...config});
|
|
254
|
+
if (result.issues?.length) throw new Error(result.issues.map(item=>item.message).join(';'));
|
|
255
|
+
const {id,roleId,httpApiPath,httpApiToken,withinChatWindowMs,...values}=result.value;
|
|
256
|
+
return values;
|
|
257
|
+
},
|
|
258
|
+
encodeRecord: record => ({...record,...record.config}),
|
|
259
|
+
publicLegacy: record => ({qqAppId:record.config.qqAppId,sandbox:record.config.sandbox,streaming:record.config.streaming}),
|
|
260
|
+
create({ record, config, host }) { return createQqTransport(deps, {...record,...config}, ctx, host); },
|
|
261
|
+
});
|
|
262
|
+
}
|
package/src/config.js
CHANGED
|
@@ -1,68 +1,69 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pygmalion 正式版插件配置 schema。
|
|
3
|
-
*
|
|
4
|
-
* 结构:
|
|
5
|
-
* web — 管理网页(唯一公网暴露面,默认仅本机)
|
|
6
|
-
* heartbeat — 内置心跳全局默认(每角色 proactive.json 可覆盖)
|
|
7
|
-
* bots[] — 兼容旧部署格式的迁移源:一次性并入数据目录 data/bots/,
|
|
8
|
-
* 机器人凭据从此在管理网页配置(见 src/bots.js)
|
|
9
|
-
*/
|
|
10
|
-
import Schema from '@deepseek-ai/schemastery';
|
|
11
|
-
|
|
12
|
-
/** Default QQ gateway intents: public channel messages + group/C2C + interactions + DMs. */
|
|
13
|
-
export const DEFAULT_INTENTS = (1 << 30) + (1 << 25) + (1 << 26) + (1 << 12);
|
|
14
|
-
|
|
15
|
-
/** 单个机器人实例配置(继承 dsh-qqbot-community 适配器,去掉 demo 的扁平 provider/model/cwd)。 */
|
|
16
|
-
export const BotConfig = Schema.object({
|
|
17
|
-
id: Schema.string().required().description('bot 实例 id(store 文件名隔离,避免多机器人数据串)'),
|
|
18
|
-
roleId: Schema.string().required().description('该机器人服务哪个角色(对应 data/roles/<roleId>)'),
|
|
19
|
-
qqAppId: Schema.string().required().description('QQ 开放平台机器人 AppID(字符串,YAML 中加引号)'),
|
|
20
|
-
qqSecret: Schema.string().required().description('QQ 开放平台机器人 AppSecret'),
|
|
21
|
-
sandbox: Schema.boolean().default(true).description('是否使用 QQ 沙箱环境'),
|
|
22
|
-
endpoint: Schema.string().default('https://api.sgroup.qq.com').description('QQ OpenAPI 接入点'),
|
|
23
|
-
intents: Schema.number().default(DEFAULT_INTENTS).description('网关事件订阅掩码'),
|
|
24
|
-
agentPreset: Schema.string().default('standard').description('非角色会话的默认 agent preset id'),
|
|
25
|
-
debug: Schema.boolean().default(false).description('调试日志'),
|
|
26
|
-
allowFrom: Schema.array(Schema.string()).default(['*']).description("C2C 发送者 openid 白名单('*' 通配)"),
|
|
27
|
-
groupAllowFrom: Schema.array(Schema.string()).default(['*']).description('群 openid 白名单'),
|
|
28
|
-
markdown: Schema.boolean().default(false).description('以 markdown (msg_type 2) 发送回复,需开通权限'),
|
|
29
|
-
textChunkLimit: Schema.number().default(4000).description('单条静态回复的最大字符数'),
|
|
30
|
-
typing: Schema.boolean().default(true).description('C2C 输入中指示(60s 窗口自动续发)'),
|
|
31
|
-
streaming: Schema.boolean().default(false).description('C2C 流式回复;导演会话一律关闭(修 D16,原 patch-home-streaming 移除)'),
|
|
32
|
-
streamThrottleMs: Schema.number().default(1200).description('流式帧最小间隔(毫秒)'),
|
|
33
|
-
deliverWindowMs: Schema.number().default(900).description('同一轮内多条文本回复的合并窗口(毫秒)'),
|
|
34
|
-
deliverMaxWaitMs: Schema.number().default(6000).description('合并回复的最大等待(毫秒)'),
|
|
35
|
-
replyPassiveLimit: Schema.number().default(4).description('每条入站消息允许的被动回复次数'),
|
|
36
|
-
mediaDownload: Schema.boolean().default(true).description('下载非图片入站附件到工作区 .qq-media/'),
|
|
37
|
-
stt: Schema.object({
|
|
38
|
-
baseUrl: Schema.string().default('https://api.openai.com/v1').description('OpenAI 兼容 STT base URL'),
|
|
39
|
-
apiKey: Schema.string().description('STT API key(缺失时整体视为未启用)'),
|
|
40
|
-
model: Schema.string().default('whisper-1').description('STT 模型'),
|
|
41
|
-
}).description('语音转文字配置'),
|
|
42
|
-
approval: Schema.boolean().default(true).description('为 QQ 会话注册内联键盘审批 answerer'),
|
|
43
|
-
approvalTimeoutMs: Schema.number().default(300000).description('审批按钮等待超时(毫秒)'),
|
|
44
|
-
slashCommands: Schema.boolean().default(true).description('启用 /help /ping /me /new /presets /approve /always 命令'),
|
|
45
|
-
httpApiPath: Schema.string().default('/external/qq/bot-1').description('该机器人的 HTTP 推送/唤醒 API 路由前缀(多机器人必须互不相同)'),
|
|
46
|
-
httpApiToken: Schema.string().description('该机器人 HTTP API 的 Bearer 鉴权 token(至少 8 字符;缺省则不挂载,生活脚本 HTTP hook 需要它)'),
|
|
47
|
-
keyboardGroupId: Schema.string().default('dshqq').description('内联键盘按钮 action.group_id(审批键盘;配置化以免与其它应用冲突)'),
|
|
48
|
-
withinChatWindowMs: Schema.number().default(300000).description('唤醒撞车检测窗口:距最后一次用户消息不足此毫秒数则跳过唤醒'),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
export const Config = Schema.object({
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Pygmalion 正式版插件配置 schema。
|
|
3
|
+
*
|
|
4
|
+
* 结构:
|
|
5
|
+
* web — 管理网页(唯一公网暴露面,默认仅本机)
|
|
6
|
+
* heartbeat — 内置心跳全局默认(每角色 proactive.json 可覆盖)
|
|
7
|
+
* bots[] — 兼容旧部署格式的迁移源:一次性并入数据目录 data/bots/,
|
|
8
|
+
* 机器人凭据从此在管理网页配置(见 src/bots.js)
|
|
9
|
+
*/
|
|
10
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
11
|
+
|
|
12
|
+
/** Default QQ gateway intents: public channel messages + group/C2C + interactions + DMs. */
|
|
13
|
+
export const DEFAULT_INTENTS = (1 << 30) + (1 << 25) + (1 << 26) + (1 << 12);
|
|
14
|
+
|
|
15
|
+
/** 单个机器人实例配置(继承 dsh-qqbot-community 适配器,去掉 demo 的扁平 provider/model/cwd)。 */
|
|
16
|
+
export const BotConfig = Schema.object({
|
|
17
|
+
id: Schema.string().required().description('bot 实例 id(store 文件名隔离,避免多机器人数据串)'),
|
|
18
|
+
roleId: Schema.string().required().description('该机器人服务哪个角色(对应 data/roles/<roleId>)'),
|
|
19
|
+
qqAppId: Schema.string().required().description('QQ 开放平台机器人 AppID(字符串,YAML 中加引号)'),
|
|
20
|
+
qqSecret: Schema.string().required().description('QQ 开放平台机器人 AppSecret'),
|
|
21
|
+
sandbox: Schema.boolean().default(true).description('是否使用 QQ 沙箱环境'),
|
|
22
|
+
endpoint: Schema.string().default('https://api.sgroup.qq.com').description('QQ OpenAPI 接入点'),
|
|
23
|
+
intents: Schema.number().default(DEFAULT_INTENTS).description('网关事件订阅掩码'),
|
|
24
|
+
agentPreset: Schema.string().default('standard').description('非角色会话的默认 agent preset id'),
|
|
25
|
+
debug: Schema.boolean().default(false).description('调试日志'),
|
|
26
|
+
allowFrom: Schema.array(Schema.string()).default(['*']).description("C2C 发送者 openid 白名单('*' 通配)"),
|
|
27
|
+
groupAllowFrom: Schema.array(Schema.string()).default(['*']).description('群 openid 白名单'),
|
|
28
|
+
markdown: Schema.boolean().default(false).description('以 markdown (msg_type 2) 发送回复,需开通权限'),
|
|
29
|
+
textChunkLimit: Schema.number().default(4000).description('单条静态回复的最大字符数'),
|
|
30
|
+
typing: Schema.boolean().default(true).description('C2C 输入中指示(60s 窗口自动续发)'),
|
|
31
|
+
streaming: Schema.boolean().default(false).description('C2C 流式回复;导演会话一律关闭(修 D16,原 patch-home-streaming 移除)'),
|
|
32
|
+
streamThrottleMs: Schema.number().default(1200).description('流式帧最小间隔(毫秒)'),
|
|
33
|
+
deliverWindowMs: Schema.number().default(900).description('同一轮内多条文本回复的合并窗口(毫秒)'),
|
|
34
|
+
deliverMaxWaitMs: Schema.number().default(6000).description('合并回复的最大等待(毫秒)'),
|
|
35
|
+
replyPassiveLimit: Schema.number().default(4).description('每条入站消息允许的被动回复次数'),
|
|
36
|
+
mediaDownload: Schema.boolean().default(true).description('下载非图片入站附件到工作区 .qq-media/'),
|
|
37
|
+
stt: Schema.object({
|
|
38
|
+
baseUrl: Schema.string().default('https://api.openai.com/v1').description('OpenAI 兼容 STT base URL'),
|
|
39
|
+
apiKey: Schema.string().description('STT API key(缺失时整体视为未启用)'),
|
|
40
|
+
model: Schema.string().default('whisper-1').description('STT 模型'),
|
|
41
|
+
}).description('语音转文字配置'),
|
|
42
|
+
approval: Schema.boolean().default(true).description('为 QQ 会话注册内联键盘审批 answerer'),
|
|
43
|
+
approvalTimeoutMs: Schema.number().default(300000).description('审批按钮等待超时(毫秒)'),
|
|
44
|
+
slashCommands: Schema.boolean().default(true).description('启用 /help /ping /me /new /presets /approve /always 命令'),
|
|
45
|
+
httpApiPath: Schema.string().default('/external/qq/bot-1').description('该机器人的 HTTP 推送/唤醒 API 路由前缀(多机器人必须互不相同)'),
|
|
46
|
+
httpApiToken: Schema.string().description('该机器人 HTTP API 的 Bearer 鉴权 token(至少 8 字符;缺省则不挂载,生活脚本 HTTP hook 需要它)'),
|
|
47
|
+
keyboardGroupId: Schema.string().default('dshqq').description('内联键盘按钮 action.group_id(审批键盘;配置化以免与其它应用冲突)'),
|
|
48
|
+
withinChatWindowMs: Schema.number().default(300000).description('唤醒撞车检测窗口:距最后一次用户消息不足此毫秒数则跳过唤醒'),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const Config = Schema.object({
|
|
52
|
+
maintenanceMode: Schema.boolean().default(false).description('迁移维护模式:仅初始化服务,不启动 QQ、网页、心跳或恢复运行脚本'),
|
|
53
|
+
web: Schema.object({
|
|
54
|
+
host: Schema.string().default('127.0.0.1').description('管理网页监听地址(公网部署配 0.0.0.0 + HTTPS 反代)'),
|
|
55
|
+
port: Schema.number().default(4180).description('管理网页监听端口'),
|
|
56
|
+
}).description('管理网页(插件内置独立 HTTP 服务)'),
|
|
57
|
+
heartbeat: Schema.object({
|
|
58
|
+
enabled: Schema.boolean().default(true).description('内置心跳总开关'),
|
|
59
|
+
intervalMs: Schema.number().default(30000).description('心跳扫描间隔(毫秒)'),
|
|
60
|
+
archiveIdleMinutes: Schema.number().default(45).description('用户连续多少分钟未发消息则触发记忆归档(0=关,角色级可覆盖)'),
|
|
61
|
+
eveningCheck: Schema.string().default('20:00').description('「今日未联系」主动问候的起始时刻 HH:MM'),
|
|
62
|
+
lifeEnabled: Schema.boolean().default(true).description('生活脚本总开关'),
|
|
63
|
+
lifeMaxPerDay: Schema.number().default(2).description('(已废弃:插件不再限制脚本运行次数)保留字段仅为兼容旧配置,不再被消费'),
|
|
64
|
+
lifeIntervalMs: Schema.number().default(1800000).description('非零性破坏后重试提示「生活脚本未在运行」的最小间隔(毫秒,默认 30 分钟);每次 dsh 启动后首个心跳 tick 无视此间隔立刻提示一次,防跨重启陈旧节流压制'),
|
|
65
|
+
scriptReviewTime: Schema.string().default('02:00').description('每日脚本审阅时刻 HH:MM(空字符串=关闭)。到点唤醒导演审阅生活/设备脚本及日志,独立于生活脚本非零性检查'),
|
|
66
|
+
scriptWakeMinIntervalMs: Schema.number().default(600000).description('生活脚本 wake() 报事件后,同一角色再次唤醒 dsh 的最小间隔(毫秒),防脚本循环轰炸'),
|
|
67
|
+
}).description('内置心跳(主动唤醒 + 沉默归档 + 非零性检查 + rewake 到期)'),
|
|
68
|
+
bots: Schema.array(BotConfig).description('兼容旧部署格式:一次性迁移到 data/bots/,此后凭据在管理网页配置'),
|
|
69
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** 发送事实独立于展示文案;使用宿主 tool/result.meta 持久化。 */
|
|
2
|
+
export const RECEIPT_VERSION = 1;
|
|
3
|
+
|
|
4
|
+
export const deliveryReceiptSchema = {
|
|
5
|
+
type: 'object', additionalProperties: false, required: ['version', 'messages'],
|
|
6
|
+
properties: {
|
|
7
|
+
version: { type: 'number', enum: [RECEIPT_VERSION] },
|
|
8
|
+
messages: { type: 'array', items: {
|
|
9
|
+
type: 'object', additionalProperties: false, required: ['index', 'type', 'status'],
|
|
10
|
+
properties: {
|
|
11
|
+
index: { type: 'integer' },
|
|
12
|
+
type: { type: 'string', enum: ['text', 'voice'] },
|
|
13
|
+
status: { type: 'string', enum: ['accepted', 'failed', 'unknown'] },
|
|
14
|
+
},
|
|
15
|
+
} },
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** 不复制台词或凭据,日志从配对的工具参数读取原文。 */
|
|
20
|
+
export function deliveryMeta(_args, result) {
|
|
21
|
+
return { pygmalion: { delivery: result.delivery ?? { version: RECEIPT_VERSION, messages: [] } } };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 有结构结果时即使零成功也不回退文案,防止失败记录被误判。 */
|
|
25
|
+
export function receiptOf(event) {
|
|
26
|
+
const value = event?.data?.meta?.pygmalion?.delivery;
|
|
27
|
+
if (!value) return undefined;
|
|
28
|
+
if (value.version !== RECEIPT_VERSION || !Array.isArray(value.messages)) return { messages: [] };
|
|
29
|
+
const seen = new Set();
|
|
30
|
+
return { messages: value.messages.filter(item => {
|
|
31
|
+
if (!Number.isInteger(item?.index) || item.index < 0 || seen.has(item.index)
|
|
32
|
+
|| !['text', 'voice'].includes(item.type) || !['accepted', 'failed', 'unknown'].includes(item.status)) return false;
|
|
33
|
+
seen.add(item.index);
|
|
34
|
+
return true;
|
|
35
|
+
}) };
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** 一次交付绑定到当前角色及聊天关系;发送前再次确认,拒绝迟到结果。 */
|
|
2
|
+
import { bindingStamp, matchesRoleTarget } from './peer-identity.js';
|
|
3
|
+
export function createRoleDeliveryGuard({ persona, roleId, signal, identityScope }) {
|
|
4
|
+
return async function begin(target, callerSignal) {
|
|
5
|
+
const signals = [signal, callerSignal].filter(Boolean);
|
|
6
|
+
const combined = signals.length ? AbortSignal.any(signals) : undefined;
|
|
7
|
+
const role = await persona.loadRole(roleId);
|
|
8
|
+
const expected = bindingStamp(role);
|
|
9
|
+
const assertCurrent = async () => {
|
|
10
|
+
combined?.throwIfAborted();
|
|
11
|
+
const current = await persona.loadRole(roleId);
|
|
12
|
+
if (!matchesRoleTarget(current, target, identityScope) || bindingStamp(current) !== expected) {
|
|
13
|
+
throw new Error('角色或聊天绑定已改变,本次交付已取消');
|
|
14
|
+
}
|
|
15
|
+
combined?.throwIfAborted();
|
|
16
|
+
};
|
|
17
|
+
await assertCurrent();
|
|
18
|
+
return { signal: combined, assertCurrent };
|
|
19
|
+
};
|
|
20
|
+
}
|