dsh-onebot-genji 0.12.2 → 0.14.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/README.md +16 -9
- package/lib/bridge.js +220 -9
- package/lib/client.js +708 -12
- package/lib/inbound.js +8 -0
- package/lib/index.js +143 -13
- package/lib/interim.js +9 -0
- package/lib/outbound.js +137 -6
- package/lib/proactive.js +635 -0
- package/lib/prompt.js +4 -3
- package/lib/registry.js +37 -1
- package/lib/settings-remote.js +347 -7
- package/lib/stickers.js +669 -0
- package/lib/tools.js +5 -62
- package/lib/types/bridge.d.ts +40 -0
- package/lib/types/client.d.ts +121 -0
- package/lib/types/inbound.d.ts +8 -0
- package/lib/types/index.d.ts +23 -3
- package/lib/types/interim.d.ts +2 -0
- package/lib/types/outbound.d.ts +31 -0
- package/lib/types/proactive.d.ts +170 -0
- package/lib/types/registry.d.ts +10 -0
- package/lib/types/settings-remote.d.ts +84 -0
- package/lib/types/stickers.d.ts +197 -0
- package/package.json +1 -1
package/lib/inbound.js
CHANGED
|
@@ -198,6 +198,14 @@ export class InboundPipeline {
|
|
|
198
198
|
if (kind === 'group' && this.ctx.config.requireMention && !mentioned) {
|
|
199
199
|
this.ctx.log('debug', mentionDropReason + ': ' + groupId);
|
|
200
200
|
currentTrace()?.emit('mention', { ok: false, reason: mentionDropReason });
|
|
201
|
+
// 群聊上下文(2026-11-19 本地加):被 @ 门挡下的消息先记进参考缓冲,
|
|
202
|
+
// 被叫到那一回合按 config.groupContextLines 注入。seam 缺席 = 零行为。
|
|
203
|
+
this.ctx.recordGroupContext?.(chatId, {
|
|
204
|
+
time: inbound.time,
|
|
205
|
+
userId,
|
|
206
|
+
nickname: inbound.nickname,
|
|
207
|
+
text: inbound.text,
|
|
208
|
+
});
|
|
201
209
|
// 群聊旁观 (#58):被 @ 门挡下的消息不派发回合,改喂旁听缓冲(nickname
|
|
202
210
|
// 在此尚未消毒,桥侧渲染回顾文本时统一 sanitize)。seam 缺席 = 零行为。
|
|
203
211
|
if (this.ctx.feedBystander !== undefined) {
|
package/lib/index.js
CHANGED
|
@@ -30,8 +30,15 @@ let InboundRecorder;
|
|
|
30
30
|
let InjectChannel;
|
|
31
31
|
let redactSnapshot;
|
|
32
32
|
let describeError;
|
|
33
|
-
let configureSendSegments;
|
|
34
33
|
let resolveUserQuestions;
|
|
34
|
+
// 2026-11-19 本地加:主动搭话(原 startup/qq-alive.mjs 合并进插件)与表情包库。
|
|
35
|
+
// 2026-11-19:会话 source 写入形态必须走 producer-owned kind,抽成常量便于静态审计。
|
|
36
|
+
const PROACTIVE_MESSAGE_SOURCE = { kind: 'plugin:dsh-onebot' };
|
|
37
|
+
let createUserMessage;
|
|
38
|
+
let BlockAssembler;
|
|
39
|
+
let ProactiveTalk;
|
|
40
|
+
let StickerStore;
|
|
41
|
+
let DEFAULT_STICKER_ROOT;
|
|
35
42
|
try {
|
|
36
43
|
;
|
|
37
44
|
({ default: z } = await import('@deepseek-ai/schemastery'));
|
|
@@ -44,7 +51,9 @@ try {
|
|
|
44
51
|
({ InjectChannel } = await import('./inject.js'));
|
|
45
52
|
({ redactSnapshot } = await import('./health.js'));
|
|
46
53
|
({ describeError } = await import('./errors.js'));
|
|
47
|
-
({
|
|
54
|
+
({ createUserMessage, BlockAssembler } = await import('@deepseek-ai/dsh-llm'));
|
|
55
|
+
({ ProactiveTalk } = await import('./proactive.js'));
|
|
56
|
+
({ StickerStore, DEFAULT_STICKER_ROOT } = await import('./stickers.js'));
|
|
48
57
|
({ resolveUserQuestions } = await import('./ask-tool.js'));
|
|
49
58
|
}
|
|
50
59
|
catch (error) {
|
|
@@ -181,13 +190,49 @@ export const Config = z.object({
|
|
|
181
190
|
.description('消息太频繁频控提示文案(每窗口至多一条):false=不发;true/缺省=默认文案;非空 string=自定义文案;空串=不发'),
|
|
182
191
|
}).default({})
|
|
183
192
|
.description('自动回执开关/自定义文案集合;缺省全部为默认文案开启(与 0.9.x 行为一致)'),
|
|
184
|
-
|
|
193
|
+
voiceReplyEnabled: z.boolean().default(true)
|
|
194
|
+
.description('回合形式掷骰(本地加 2026-11-18):true = 按概率把某些回合的回复形式指定成语音条'),
|
|
195
|
+
voiceReplyProb: z.number().min(0).max(1).default(0.1)
|
|
196
|
+
.description('语音条概率(0–1,默认 0.10):每回合独立掷骰;语音条那轮插件会在提示词里提示「这一轮用语音」'),
|
|
197
|
+
stickerReplyEnabled: z.boolean().default(true)
|
|
198
|
+
.description('表情包掷骰(本地加 2026-11-18):true = 按概率提示模型在本轮回一个表情包'),
|
|
199
|
+
stickerReplyProb: z.number().min(0).max(1).default(0.39)
|
|
200
|
+
.description('表情包概率(0–1,默认 0.39):与语音条互斥,先掷语音再掷表情包'),
|
|
201
|
+
stickerRoot: z.string().default('')
|
|
202
|
+
.description('表情包库根目录(下面分「已审核」=聊天可挑、「未审核」=待审两个子目录);留空 = <dsh-home>/dsh-onebot-genji/stickers'),
|
|
203
|
+
stickerAutoCollect: z.boolean().default(true)
|
|
204
|
+
.description('表情包自动收集(本地加 2026-11-19):每 5 分钟扫一次入站媒体目录,新图先收进「未审核」;聊天里发表情只从已审核的图里挑'),
|
|
205
|
+
proactive: z.object({
|
|
185
206
|
enabled: z.boolean().default(true)
|
|
186
|
-
.description('
|
|
187
|
-
|
|
188
|
-
.description('
|
|
207
|
+
.description('主动搭话(原 startup/qq-alive.mjs,2026-11-19 合并进插件):主人安静一阵后由机器人主动发一条私聊消息;false = 完全关闭(不建实例、不占定时器、不花 token)'),
|
|
208
|
+
targetUsers: z.array(z.string()).default([])
|
|
209
|
+
.description('主动搭话发给谁:填 QQ 号或 private:<QQ号>,可填多个;留空 = 用 adminUsers 整个列表'),
|
|
210
|
+
minIdleMinutes: z.number().min(0).default(60)
|
|
211
|
+
.description('主人至少安静这么久才考虑开口(分钟)'),
|
|
212
|
+
minGapMinutes: z.number().min(0).default(90)
|
|
213
|
+
.description('距上次主动开口至少间隔这么久(分钟)'),
|
|
214
|
+
maxPerDay: z.number().min(0).default(6)
|
|
215
|
+
.description('每天最多主动开口几次(按本地日期重置)'),
|
|
216
|
+
quietFrom: z.number().min(0).max(23).default(22)
|
|
217
|
+
.description('安静时段起点(小时,含)——这段时间内不主动开口'),
|
|
218
|
+
quietTo: z.number().min(0).max(23).default(6)
|
|
219
|
+
.description('安静时段终点(小时,不含)'),
|
|
220
|
+
attemptGapMinutes: z.number().min(0).default(30)
|
|
221
|
+
.description('模型判定「没话头」或生成失败后,至少隔这么久再试一次(分钟)'),
|
|
222
|
+
burstProb: z.number().min(0).max(1).default(0.5)
|
|
223
|
+
.description('开口时拆成 2~3 条连着发的概率(0–1)'),
|
|
224
|
+
followupEnabled: z.boolean().default(true)
|
|
225
|
+
.description('聊完补话:主人刚说完、你也回完了,隔一会儿再判断一次要不要补一句'),
|
|
226
|
+
followupMinSec: z.number().min(0).default(45)
|
|
227
|
+
.description('聊完补话的窗口起点(秒)'),
|
|
228
|
+
followupMaxSec: z.number().min(0).default(180)
|
|
229
|
+
.description('聊完补话的窗口终点(秒)'),
|
|
230
|
+
followupProb: z.number().min(0).max(1).default(0.3)
|
|
231
|
+
.description('补话窗口内掷骰通过的概率(0–1)'),
|
|
232
|
+
followupMaxPerDay: z.number().min(0).default(3)
|
|
233
|
+
.description('每天最多补话几次'),
|
|
189
234
|
}).default({})
|
|
190
|
-
.description('
|
|
235
|
+
.description('主动搭话(合并自 startup/qq-alive.mjs,2026-11-19):机器人自己判断要不要主动找主人说话'),
|
|
191
236
|
rateLimitPerMinute: z.number().default(30)
|
|
192
237
|
.description('每 chat 每分钟允许的普通消息条数上限(60 秒滑动窗口);命令消息不计入也不受限;超限时每窗口最多提示一次;0=禁用频控'),
|
|
193
238
|
restrictedMemberPrefix: z.boolean().default(true)
|
|
@@ -238,7 +283,7 @@ export const Config = z.object({
|
|
|
238
283
|
.description('权限分档:群聊会话的 agent 预设 id(粗档)——设置后群聊优先使用它,私聊仍用 agentPreset;留空 = 群私同源'),
|
|
239
284
|
permissions: z.object({
|
|
240
285
|
memberTools: z.union([z.const('none'), z.const('send'), z.const('all')]).default('none')
|
|
241
|
-
.description('成员档位(非管理员)的工具权限:none=成员回合禁用全部 qq_* 工具(默认,插件硬拦截);send=仅放行发往当前会话的内容类工具(qq_send_text /
|
|
286
|
+
.description('成员档位(非管理员)的工具权限:none=成员回合禁用全部 qq_* 工具(默认,插件硬拦截);send=仅放行发往当前会话的内容类工具(qq_send_text / qq_send_image,发图可用会话工作区图库);all=不限'),
|
|
242
287
|
}),
|
|
243
288
|
workspacePath: z.string().default('')
|
|
244
289
|
.description('QQ 会话的工作区目录(写入会话 cwd,并自动归入该工作区,不存在则创建);留空时使用宿主进程工作目录并在启动时告警提醒(宿主未提供可编程查询的默认工作区),单聊 /workspace 覆盖优先且已持久化到会话映射文件,重启/恢复失败均不丢失'),
|
|
@@ -252,7 +297,7 @@ export const Config = z.object({
|
|
|
252
297
|
.description('私聊附加设定文件路径(T4 PR 移植):支持 ~/ 展开;文件内容(剥行首尾空白)注入私聊会话 system prompt 的「本会话附加设定」段,文件编辑后即时生效(mtime 缓存失效);留空 = 不注入(默认)'),
|
|
253
298
|
groupPromptFile: z.string().default('')
|
|
254
299
|
.description('群聊附加设定文件路径(T4 PR 移植):同 dmPromptFile,只对群聊(group:)会话生效;留空 = 不注入(默认)'),
|
|
255
|
-
groupContextLines: z.number().min(0).max(200).default(
|
|
300
|
+
groupContextLines: z.number().min(0).max(200).default(3)
|
|
256
301
|
.description('群聊参考上下文条数(本地加 2026-11-18):被 @ 时把群里最近 N 条消息拼进上下文;0 = 不参考'),
|
|
257
302
|
memoryFilesEnabled: z.boolean().default(false)
|
|
258
303
|
.description('记忆文件开关(T4 PR 移植):开启后把 memoryFiles 列表里的文件内容注入私聊会话 system prompt 的「我的记忆」段(单文件 8000 字符、总量 12000 字符截断;群聊不喂记忆);默认关'),
|
|
@@ -277,7 +322,7 @@ export const Config = z.object({
|
|
|
277
322
|
dedupWindowSeconds: z.number().default(300)
|
|
278
323
|
.description('入站消息去重窗口(秒,W2-②):同一会话内相同 message_id 在该窗口内重投(断线重连重放等)只处理第一条,后续静默跳过并记 debug;仅对会触发回复处理的消息事件生效(notice/meta 类不经过此闸);0 = 禁用去重'),
|
|
279
324
|
actionRatePerMinute: z.number().default(20)
|
|
280
|
-
.description('主动写操作每分钟全桥合计上限(W2-③):约束 qq_send_image/voice/video/file
|
|
325
|
+
.description('主动写操作每分钟全桥合计上限(W2-③):约束 qq_send_image/voice/video/file 与 qq_send_forward 等工具发起的主动发送(回合内的被动回复/interim/通知不受此闸,由 rateLimitPerMinute 与离线队列上限约束);超限拒发并记 warn 与审计;0 = 禁用分钟限额'),
|
|
281
326
|
actionRatePerDay: z.number().default(500)
|
|
282
327
|
.description('主动写操作每日(本地自然日)全桥合计上限(W2-③):口径同 actionRatePerMinute;超限拒发并记 warn 与审计,次日自动恢复;0 = 禁用日限额'),
|
|
283
328
|
actionAuditEnabled: z.boolean().default(true)
|
|
@@ -362,9 +407,8 @@ export function apply(ctx, config) {
|
|
|
362
407
|
// T6 (PR port faceStyle):[CQ:face] 呈现风格是模块级解析行为(cq.ts
|
|
363
408
|
// faceToEmoji),apply 时接线一次;非法值由 setFaceStyle 回落 'name'。
|
|
364
409
|
setFaceStyle(config.faceStyle);
|
|
365
|
-
//
|
|
366
|
-
//
|
|
367
|
-
configureSendSegments(config.segments);
|
|
410
|
+
// 2026-11-19 主人要求:qq_send_segments 工具下线(分段由 outbound 的
|
|
411
|
+
// splitIntoBubbles 自动切气泡),ConfigureSendSegments 那一套也一并去掉。
|
|
368
412
|
const mediaDir = config.mediaDir !== '' ? config.mediaDir : defaultMediaDir();
|
|
369
413
|
const policy = resolvePolicy(config);
|
|
370
414
|
/** Console log line callback (level, message) — shared by the bridge deps,
|
|
@@ -380,6 +424,22 @@ export function apply(ctx, config) {
|
|
|
380
424
|
else
|
|
381
425
|
console.log(prefix + message);
|
|
382
426
|
};
|
|
427
|
+
// 表情包库(2026-11-19 本地加):根目录来自配置,自动收集只把新图丢进「未审核」,
|
|
428
|
+
// 不调模型、不占显存;审核在设置页点按钮时才走宿主模型看图打标签。
|
|
429
|
+
const stickerRoot = config.stickerRoot !== '' ? config.stickerRoot : DEFAULT_STICKER_ROOT;
|
|
430
|
+
let stickerStore;
|
|
431
|
+
const stickerStoreOf = () => {
|
|
432
|
+
if (stickerStore === undefined) {
|
|
433
|
+
stickerStore = new StickerStore({
|
|
434
|
+
root: stickerRoot,
|
|
435
|
+
log: (level, message) => {
|
|
436
|
+
const lv = level === 'warn' || level === 'error' || level === 'debug' ? level : 'info';
|
|
437
|
+
log(lv, '[stickers] ' + message);
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
return stickerStore;
|
|
442
|
+
};
|
|
383
443
|
// T3 方案 B: the host exposes no queryable default workspace —
|
|
384
444
|
// WorkspaceRegistry has no default getter and the session controller's
|
|
385
445
|
// defaultCwd is assembly-internal — so the unconfigured fallback stays the
|
|
@@ -428,6 +488,8 @@ export function apply(ctx, config) {
|
|
|
428
488
|
timeoutMs: config.sttTimeoutMs,
|
|
429
489
|
});
|
|
430
490
|
connection.onMessage = (event) => {
|
|
491
|
+
// 主动搭话(2026-11-19 合并进插件):记「主人最后说话时间」,只认目标私聊。
|
|
492
|
+
proactive?.noteInbound(event);
|
|
431
493
|
void bridge.handleInbound(event);
|
|
432
494
|
};
|
|
433
495
|
connection.onMeta = (event) => {
|
|
@@ -540,6 +602,12 @@ export function apply(ctx, config) {
|
|
|
540
602
|
sendErrorNotice: config.sendErrorNotice,
|
|
541
603
|
notices: config.notices,
|
|
542
604
|
rateLimitPerMinute: config.rateLimitPerMinute,
|
|
605
|
+
// 回合形式掷骰 + 表情包(本地加 2026-11-19):面板上的开关/概率在这里进 bridge。
|
|
606
|
+
voiceReplyEnabled: config.voiceReplyEnabled,
|
|
607
|
+
voiceReplyProb: config.voiceReplyProb,
|
|
608
|
+
stickerReplyEnabled: config.stickerReplyEnabled,
|
|
609
|
+
stickerReplyProb: config.stickerReplyProb,
|
|
610
|
+
stickerRoot,
|
|
543
611
|
restrictedMemberPrefix: config.restrictedMemberPrefix,
|
|
544
612
|
sensitivePatterns: config.sensitivePatterns,
|
|
545
613
|
mediaDir,
|
|
@@ -585,6 +653,46 @@ export function apply(ctx, config) {
|
|
|
585
653
|
// returned reference to its own log line, so host logs and qq-trace.jsonl
|
|
586
654
|
// share the same number for one fault.
|
|
587
655
|
connection.onFailure = (stage, detail) => bridge.registerConnectionFailure(stage, detail);
|
|
656
|
+
// 主动搭话(2026-11-19:原 startup/qq-alive.mjs 合并进插件)。开关在
|
|
657
|
+
// Config.proactive.enabled;关着就完全不建实例——没有定时器、不发请求。
|
|
658
|
+
/** 主动搭话的生成通道:用「当前会话的模型」(agentDefaultModel 的选择)跑一次纯文本
|
|
659
|
+
* 补全。2026-11-19 主人要求:不再配接口地址/模型名/接口 Key,一律跟随会话模型。 */
|
|
660
|
+
const proactiveGenerateText = async (prompt, options = {}) => {
|
|
661
|
+
const selection = ctx.agentDefaultModel.currentSelection();
|
|
662
|
+
if (selection === undefined)
|
|
663
|
+
throw new Error('没有可用的会话模型(agentDefaultModel.currentSelection() 为空)');
|
|
664
|
+
const call = await ctx.llm.prepareCall({ provider: selection.provider, model: selection.model, maxTokens: 1500 }, options.signal);
|
|
665
|
+
const userMessage = createUserMessage({
|
|
666
|
+
content: [{ type: 'text', text: prompt }],
|
|
667
|
+
source: PROACTIVE_MESSAGE_SOURCE,
|
|
668
|
+
});
|
|
669
|
+
const assembler = new BlockAssembler();
|
|
670
|
+
for await (const chunk of call.stream({
|
|
671
|
+
...call.config,
|
|
672
|
+
messages: [userMessage],
|
|
673
|
+
purpose: 'dsh-onebot-proactive',
|
|
674
|
+
signal: options.signal,
|
|
675
|
+
}))
|
|
676
|
+
assembler.push(chunk);
|
|
677
|
+
return assembler
|
|
678
|
+
.blocks()
|
|
679
|
+
.filter((block) => block.type === 'text')
|
|
680
|
+
.map((block) => block.text)
|
|
681
|
+
.join('')
|
|
682
|
+
.trim();
|
|
683
|
+
};
|
|
684
|
+
let proactive;
|
|
685
|
+
if (config.proactive?.enabled !== false) {
|
|
686
|
+
proactive = new ProactiveTalk({
|
|
687
|
+
config,
|
|
688
|
+
bridge: bridge,
|
|
689
|
+
log,
|
|
690
|
+
dshHome: dshHome(),
|
|
691
|
+
generateText: proactiveGenerateText,
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
/** 表情包自动收集的定时器(开关关掉时不建)。 */
|
|
695
|
+
let stickerTimer;
|
|
588
696
|
// Lifecycle: start the bridge and transport; unwind everything on unload.
|
|
589
697
|
// The effect body is async (cordis AsyncEffect): injectChannel.start() must
|
|
590
698
|
// be awaited — it skips history before arming its poll timer, and a stop
|
|
@@ -612,14 +720,36 @@ export function apply(ctx, config) {
|
|
|
612
720
|
// no log, fiber stays active). Surface the failure and unwind the
|
|
613
721
|
// already-started chain (same order as the disposer) before rethrowing.
|
|
614
722
|
log('error', 'dsh-onebot 启动失败: ' + describeError(error));
|
|
723
|
+
if (stickerTimer !== undefined)
|
|
724
|
+
clearInterval(stickerTimer);
|
|
615
725
|
await injectChannel?.stop();
|
|
616
726
|
await bridge.stop();
|
|
617
727
|
await connection.stop();
|
|
618
728
|
throw error;
|
|
619
729
|
}
|
|
620
730
|
console.log('[dsh-onebot] mounted' + mountDetails);
|
|
731
|
+
// 表情包自动收集(2026-11-19 本地加):每 5 分钟扫一次入站媒体目录,新图只进
|
|
732
|
+
// 「未审核」;不调模型、不占显存。关掉开关就连定时器都不建。
|
|
733
|
+
if (config.stickerAutoCollect !== false) {
|
|
734
|
+
const collectOnce = () => {
|
|
735
|
+
try {
|
|
736
|
+
const result = stickerStoreOf().collect(mediaDir);
|
|
737
|
+
if (result.added > 0)
|
|
738
|
+
log('info', '[stickers] 自动收集 ' + result.added + ' 张进未审核');
|
|
739
|
+
}
|
|
740
|
+
catch (error) {
|
|
741
|
+
log('warn', '[stickers] 自动收集失败: ' + describeError(error));
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
collectOnce();
|
|
745
|
+
stickerTimer = setInterval(collectOnce, 300_000);
|
|
746
|
+
stickerTimer.unref?.();
|
|
747
|
+
}
|
|
621
748
|
// QQ 平台说明与 qq_* 工具现按会话 agent 注入(见 ChatBridge.installChannelScope)
|
|
622
749
|
return async () => {
|
|
750
|
+
if (stickerTimer !== undefined)
|
|
751
|
+
clearInterval(stickerTimer);
|
|
752
|
+
proactive?.stop();
|
|
623
753
|
await injectChannel?.stop();
|
|
624
754
|
await bridge.stop();
|
|
625
755
|
await connection.stop();
|
package/lib/interim.js
CHANGED
|
@@ -146,6 +146,12 @@ export class InterimTracker {
|
|
|
146
146
|
this.ctx.log('info', 'silenced turn for ' + chatId);
|
|
147
147
|
return;
|
|
148
148
|
}
|
|
149
|
+
// 双发闸(语义版,2026-11-19 加回):工具已经用文字说过同一句时,
|
|
150
|
+
// 回合末正文只是重复就不发。
|
|
151
|
+
if (this.ctx.dropDuplicateFinal?.(chatId, final) === true) {
|
|
152
|
+
this.ctx.log('info', 'final dropped (duplicate of tool text): ' + chatId);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
149
155
|
this.ctx.sendToChat(chatId, final, { queuable: true }).catch(error => {
|
|
150
156
|
this.ctx.log('warn', 'final send failed: ' + describeError(error));
|
|
151
157
|
});
|
|
@@ -325,6 +331,9 @@ export class InterimTracker {
|
|
|
325
331
|
if (isSilenceText(final)) {
|
|
326
332
|
this.ctx.log('info', 'silenced turn for ' + chatId);
|
|
327
333
|
}
|
|
334
|
+
else if (this.ctx.dropDuplicateFinal?.(chatId, final) === true) {
|
|
335
|
+
this.ctx.log('info', 'final dropped (duplicate of tool text): ' + chatId);
|
|
336
|
+
}
|
|
328
337
|
else {
|
|
329
338
|
try {
|
|
330
339
|
await this.ctx.sendToChat(chatId, final, { queuable: true });
|
package/lib/outbound.js
CHANGED
|
@@ -206,6 +206,129 @@ export function splitAtSegments(text) {
|
|
|
206
206
|
segs.push({ type: 'text', data: { text: text.slice(last) } });
|
|
207
207
|
return segs;
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* 气泡切分(2026-11-19 按 GitHub issue #6 的核心逻辑重写,主人要求):
|
|
211
|
+
* 1. 先按换行初步切分,一行一条——**换行是硬边界,行与行之间不合并**(主人
|
|
212
|
+
* 反复强调「换行了就要分成两条」);
|
|
213
|
+
* 2. 每行内部:长度超过 sentenceCut(35) 时按句末标点断句,短行原样保留;
|
|
214
|
+
* 3. 行内吸附合并:纯标点(`\p{P}\p{S}`)并进上一条;上一条不足 6 字且合计
|
|
215
|
+
* 不超过 24 字也并进去,避免「也是哦」「哈哈」这种碎片单独成条;
|
|
216
|
+
* 4. 跨行只做一件事:整行都是纯标点时并进上一条(「真的假的」+「!」);
|
|
217
|
+
* 5. 单轮最多 4 条,多出来的并到最后一条(防刷屏)。
|
|
218
|
+
* 发送侧的呼吸停顿在 sendToChat 里做(首条立即,其后 800–1500ms 随机)。
|
|
219
|
+
*/
|
|
220
|
+
export function splitIntoBubbles(text, maxBubbles = 4, sentenceCut = 35) {
|
|
221
|
+
const raw = String(text ?? '');
|
|
222
|
+
if (raw.trim() === '')
|
|
223
|
+
return [];
|
|
224
|
+
const punctOnlyRe = /^[\p{P}\p{S}\s]+$/u;
|
|
225
|
+
// 1. 换行初切
|
|
226
|
+
const rawParts = raw.split(/\r?\n+/).map((s) => s.trim()).filter(Boolean);
|
|
227
|
+
const merged = [];
|
|
228
|
+
const shortMerge = (last, s) => punctOnlyRe.test(s)
|
|
229
|
+
|| ([...last].length < 6 && [...last].length + [...s].length <= 24);
|
|
230
|
+
for (const part of rawParts) {
|
|
231
|
+
// 2. 行内断句
|
|
232
|
+
const pieces = [...part].length <= sentenceCut
|
|
233
|
+
? [part]
|
|
234
|
+
: (() => {
|
|
235
|
+
const segs = part.split(/(?<=[。!?!?…~])\s*/).map((s) => s.trim()).filter(Boolean);
|
|
236
|
+
return segs.length > 0 ? segs : [part];
|
|
237
|
+
})();
|
|
238
|
+
// 3. 行内吸附合并(换行是硬边界,不跨行合并普通句子)
|
|
239
|
+
const line = [];
|
|
240
|
+
for (const s of pieces) {
|
|
241
|
+
const lastInLine = line[line.length - 1];
|
|
242
|
+
if (lastInLine !== undefined && shortMerge(lastInLine, s)) {
|
|
243
|
+
line[line.length - 1] = lastInLine + s;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
line.push(s);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// 4. 跨行只吸附纯标点行(「真的假的」+「!」)
|
|
250
|
+
if (line.length > 0 && merged.length > 0 && punctOnlyRe.test(line[0])) {
|
|
251
|
+
merged[merged.length - 1] += line.shift();
|
|
252
|
+
}
|
|
253
|
+
merged.push(...line);
|
|
254
|
+
}
|
|
255
|
+
if (merged.length === 0)
|
|
256
|
+
return [];
|
|
257
|
+
// 4. 上限控制
|
|
258
|
+
const limit = Math.max(1, maxBubbles);
|
|
259
|
+
if (merged.length > limit) {
|
|
260
|
+
return [...merged.slice(0, limit - 1), merged.slice(limit - 1).join(' ')];
|
|
261
|
+
}
|
|
262
|
+
return merged;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 相邻气泡之间的拟人打字停顿:800–1500ms 随机(issue #6 的生产参数)。
|
|
266
|
+
* 首条不等待,所以这里只被第 2 条及以后调用。
|
|
267
|
+
*/
|
|
268
|
+
export function bubbleGapMs(random = Math.random) {
|
|
269
|
+
const r = Number(random());
|
|
270
|
+
const base = Number.isFinite(r) ? Math.min(0.999999, Math.max(0, r)) : 0.5;
|
|
271
|
+
return 800 + Math.floor(base * 700);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 双发闸(语义版,2026-11-19 主人要回):判断「回合末正文」是不是把工具
|
|
275
|
+
* (qq_send_text / 图片配文那种)刚说过的话又重复了一遍。纯文本启发式,
|
|
276
|
+
* 不调模型:先归一化(去掉空白、标点、符号、emoji、括号里的颜文字),
|
|
277
|
+
* 再比二元组 Dice 系数。长度差超过一倍直接判不重复(一句是新内容)。
|
|
278
|
+
*/
|
|
279
|
+
export function textTopicKey(text) {
|
|
280
|
+
return String(text ?? '')
|
|
281
|
+
.replace(/\([^()]*\)|([^()]*)|\[[^\[\]]*\]|【[^】]*】/g, '')
|
|
282
|
+
.replace(/[\s\p{P}\p{S}]/gu, '')
|
|
283
|
+
.toLowerCase();
|
|
284
|
+
}
|
|
285
|
+
/** 二元组 Dice 系数(0–1):1 = 归一化后一模一样。 */
|
|
286
|
+
export function topicSimilarity(a, b) {
|
|
287
|
+
const x = textTopicKey(a);
|
|
288
|
+
const y = textTopicKey(b);
|
|
289
|
+
if (x === '' || y === '')
|
|
290
|
+
return 0;
|
|
291
|
+
if (x === y)
|
|
292
|
+
return 1;
|
|
293
|
+
const grams = (value) => {
|
|
294
|
+
const out = new Map();
|
|
295
|
+
if (value.length === 1)
|
|
296
|
+
out.set(value, 1);
|
|
297
|
+
for (let i = 0; i < value.length - 1; i += 1) {
|
|
298
|
+
const gram = value.slice(i, i + 2);
|
|
299
|
+
out.set(gram, (out.get(gram) ?? 0) + 1);
|
|
300
|
+
}
|
|
301
|
+
return out;
|
|
302
|
+
};
|
|
303
|
+
const gx = grams(x);
|
|
304
|
+
const gy = grams(y);
|
|
305
|
+
let overlap = 0;
|
|
306
|
+
let total = 0;
|
|
307
|
+
for (const [gram, count] of gx) {
|
|
308
|
+
total += count;
|
|
309
|
+
overlap += Math.min(count, gy.get(gram) ?? 0);
|
|
310
|
+
}
|
|
311
|
+
for (const count of gy.values())
|
|
312
|
+
total += count;
|
|
313
|
+
return total === 0 ? 0 : (2 * overlap) / total;
|
|
314
|
+
}
|
|
315
|
+
/** 判重阈值:0.72 能把「我先去跑,几十秒」/「我去跑,几十秒」判成同一句,
|
|
316
|
+
* 又能放过「来了」+「来了,我这就去扒谱」这种后半句有新信息的。 */
|
|
317
|
+
export const DUPLICATE_TOPIC_THRESHOLD = 0.72;
|
|
318
|
+
/** 语义判重:归一化后同串直接算重复,否则二元组 Dice ≥ 阈值算重复。 */
|
|
319
|
+
export function isDuplicateTopic(a, b, threshold = DUPLICATE_TOPIC_THRESHOLD) {
|
|
320
|
+
const x = textTopicKey(a);
|
|
321
|
+
const y = textTopicKey(b);
|
|
322
|
+
if (x === '' || y === '')
|
|
323
|
+
return false;
|
|
324
|
+
if (x === y)
|
|
325
|
+
return true;
|
|
326
|
+
const shorter = Math.min(x.length, y.length);
|
|
327
|
+
const longer = Math.max(x.length, y.length);
|
|
328
|
+
if (longer === 0 || shorter / longer < 0.5)
|
|
329
|
+
return false;
|
|
330
|
+
return topicSimilarity(a, b) >= threshold;
|
|
331
|
+
}
|
|
209
332
|
/**
|
|
210
333
|
* T9 (#2): strip a leaked <user_message> boundary. wrapUserMessage (chat.ts)
|
|
211
334
|
* wraps every inbound payload in `<user_message qq="…" nickname="…">…</user_message>`,
|
|
@@ -439,12 +562,20 @@ export class OutboundPipeline {
|
|
|
439
562
|
// T2 (PR port): 纯旁白按空正文处理(同一独立开关)。
|
|
440
563
|
const plain = this.ctx.config.asideOnlySilentEnabled === true && isAsideOnly(plainRaw) ? '' : plainRaw;
|
|
441
564
|
if (plain !== '') {
|
|
442
|
-
//
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
565
|
+
// 气泡切分(2026-11-19 按 issue #6):换行/长句 → 多条独立气泡,
|
|
566
|
+
// 最多 4 条。首条立即发,其后按 800–1500ms 随机停顿营造打字呼吸感;
|
|
567
|
+
// 引用只挂在第一条。
|
|
568
|
+
const bubbles = splitIntoBubbles(plain);
|
|
569
|
+
for (let i = 0; i < bubbles.length; i += 1) {
|
|
570
|
+
if (i > 0)
|
|
571
|
+
await new Promise((resolve) => setTimeout(resolve, bubbleGapMs()));
|
|
572
|
+
// T2 (PR port): 出站防御第三闸——正文里的 [[at:QQ]] 换成真 at 段
|
|
573
|
+
//(防御开关内;关闭时保持上游的单 text 段)。
|
|
574
|
+
const segments = defense ? splitAtSegments(bubbles[i]) : [{ type: 'text', data: { text: bubbles[i] } }];
|
|
575
|
+
const id = await this.sendMsg(chatId, segments, { ...options, replyTo: i === 0 ? replyTo : undefined });
|
|
576
|
+
if (id !== undefined)
|
|
577
|
+
ids.push(id);
|
|
578
|
+
}
|
|
448
579
|
}
|
|
449
580
|
else {
|
|
450
581
|
// pipeline-hooks.md #30: the empty-body skip used to be silent.
|