dsh-qqbot-plus 0.0.0-stage → 0.1.2
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/CHANGELOG.md +188 -0
- package/CHANGELOG_EN.md +188 -0
- package/LICENSE +21 -0
- package/README.md +281 -2
- package/README_EN.md +248 -0
- package/cordis.patch.yml +163 -0
- package/cordis.yml +112 -0
- package/install.sh +108 -0
- package/lib/alarms.js +371 -0
- package/lib/client.js +2411 -0
- package/lib/commands/auth.d.ts +18 -0
- package/lib/commands/auth.d.ts.map +1 -0
- package/lib/commands/auth.js +15 -0
- package/lib/commands/auth.js.map +1 -0
- package/lib/commands/help.d.ts +10 -0
- package/lib/commands/help.d.ts.map +1 -0
- package/lib/commands/help.js +39 -0
- package/lib/commands/help.js.map +1 -0
- package/lib/commands/index.d.ts +15 -0
- package/lib/commands/index.d.ts.map +1 -0
- package/lib/commands/index.js +30 -0
- package/lib/commands/index.js.map +1 -0
- package/lib/commands/misc.d.ts +21 -0
- package/lib/commands/misc.d.ts.map +1 -0
- package/lib/commands/misc.js +93 -0
- package/lib/commands/misc.js.map +1 -0
- package/lib/commands/model.d.ts +6 -0
- package/lib/commands/model.d.ts.map +1 -0
- package/lib/commands/model.js +63 -0
- package/lib/commands/model.js.map +1 -0
- package/lib/commands/new.js +17 -0
- package/lib/commands/preset.d.ts +8 -0
- package/lib/commands/preset.d.ts.map +1 -0
- package/lib/commands/preset.js +56 -0
- package/lib/commands/preset.js.map +1 -0
- package/lib/commands/session.d.ts +14 -0
- package/lib/commands/session.d.ts.map +1 -0
- package/lib/commands/session.js +34 -0
- package/lib/commands/session.js.map +1 -0
- package/lib/commands/status.d.ts +7 -0
- package/lib/commands/status.d.ts.map +1 -0
- package/lib/commands/status.js +26 -0
- package/lib/commands/status.js.map +1 -0
- package/lib/commands/types.d.ts +21 -0
- package/lib/commands/types.d.ts.map +1 -0
- package/lib/commands/types.js +2 -0
- package/lib/commands/types.js.map +1 -0
- package/lib/config.d.ts +95 -0
- package/lib/config.js +88 -0
- package/lib/features/answer-parser.d.ts +9 -0
- package/lib/features/answer-parser.d.ts.map +1 -0
- package/lib/features/answer-parser.js +23 -0
- package/lib/features/answer-parser.js.map +1 -0
- package/lib/features/approval-channel.d.ts +94 -0
- package/lib/features/approval-channel.d.ts.map +1 -0
- package/lib/features/approval-channel.js +158 -0
- package/lib/features/approval-channel.js.map +1 -0
- package/lib/features/approval-renderer.d.ts +18 -0
- package/lib/features/approval-renderer.d.ts.map +1 -0
- package/lib/features/approval-renderer.js +52 -0
- package/lib/features/approval-renderer.js.map +1 -0
- package/lib/features/button-utils.d.ts +34 -0
- package/lib/features/button-utils.d.ts.map +1 -0
- package/lib/features/button-utils.js +27 -0
- package/lib/features/button-utils.js.map +1 -0
- package/lib/features/history-store.d.ts +8 -0
- package/lib/features/history-store.d.ts.map +1 -0
- package/lib/features/history-store.js +23 -0
- package/lib/features/history-store.js.map +1 -0
- package/lib/features/question-channel.d.ts +96 -0
- package/lib/features/question-channel.d.ts.map +1 -0
- package/lib/features/question-channel.js +234 -0
- package/lib/features/question-channel.js.map +1 -0
- package/lib/features/question-renderer.d.ts +20 -0
- package/lib/features/question-renderer.d.ts.map +1 -0
- package/lib/features/question-renderer.js +70 -0
- package/lib/features/question-renderer.js.map +1 -0
- package/lib/fishcheck/measure.js +230 -0
- package/lib/fishcheck/rules.js +99 -0
- package/lib/fishmeme/plugin.js +605 -0
- package/lib/fishmeme/routes.js +141 -0
- package/lib/fishmemory/plugin.js +1052 -0
- package/lib/gateway/bootstrap.d.ts +12 -0
- package/lib/gateway/bootstrap.d.ts.map +1 -0
- package/lib/gateway/bootstrap.js +200 -0
- package/lib/gateway/bootstrap.js.map +1 -0
- package/lib/gateway/index.d.ts +3 -0
- package/lib/gateway/index.d.ts.map +1 -0
- package/lib/gateway/index.js +3 -0
- package/lib/gateway/index.js.map +1 -0
- package/lib/gateway/middleware-setup.d.ts +12 -0
- package/lib/gateway/middleware-setup.d.ts.map +1 -0
- package/lib/gateway/middleware-setup.js +82 -0
- package/lib/gateway/middleware-setup.js.map +1 -0
- package/lib/host/adapter.d.ts +48 -0
- package/lib/host/adapter.d.ts.map +1 -0
- package/lib/host/adapter.js +127 -0
- package/lib/host/adapter.js.map +1 -0
- package/lib/humanlike/ENV_PROMPT.md +28 -0
- package/lib/humanlike/affinity.js +316 -0
- package/lib/humanlike/learn.js +343 -0
- package/lib/humanlike/life.js +899 -0
- package/lib/humanlike/mind.js +323 -0
- package/lib/humanlike/plugin.js +587 -0
- package/lib/humanlike/reach.js +119 -0
- package/lib/humanlike/real.js +276 -0
- package/lib/humanlike/rules.js +67 -0
- package/lib/humanlike/settings-api.js +159 -0
- package/lib/humanlike/settings.js +370 -0
- package/lib/humanlike/together.js +251 -0
- package/lib/identity/paths.js +27 -0
- package/lib/identity/persona.js +75 -0
- package/lib/identity/portrait.js +25 -0
- package/lib/identity/runtime.js +78 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +117 -0
- package/lib/media/draft-tool.js +442 -0
- package/lib/media/draft-tool.js.bak-to-182027 +389 -0
- package/lib/media/media-cleaner.d.ts +19 -0
- package/lib/media/media-cleaner.d.ts.map +1 -0
- package/lib/media/media-cleaner.js +81 -0
- package/lib/media/media-cleaner.js.map +1 -0
- package/lib/media/say-tool.js +72 -0
- package/lib/media/send-file-tool.d.ts +12 -0
- package/lib/media/send-file-tool.d.ts.map +1 -0
- package/lib/media/send-file-tool.js +122 -0
- package/lib/media/send-file-tool.js.map +1 -0
- package/lib/media/send-media.d.ts +61 -0
- package/lib/media/send-media.d.ts.map +1 -0
- package/lib/media/send-media.js +104 -0
- package/lib/media/send-media.js.map +1 -0
- package/lib/media/vision-tool.d.ts +22 -0
- package/lib/media/vision-tool.d.ts.map +1 -0
- package/lib/media/vision-tool.js +288 -0
- package/lib/media/vision-tool.js.map +1 -0
- package/lib/middleware/attachment.d.ts +11 -0
- package/lib/middleware/attachment.d.ts.map +1 -0
- package/lib/middleware/attachment.js +30 -0
- package/lib/middleware/attachment.js.map +1 -0
- package/lib/middleware/question-answer.d.ts +10 -0
- package/lib/middleware/question-answer.d.ts.map +1 -0
- package/lib/middleware/question-answer.js +19 -0
- package/lib/middleware/question-answer.js.map +1 -0
- package/lib/model/index.d.ts +10 -0
- package/lib/model/index.d.ts.map +1 -0
- package/lib/model/index.js +9 -0
- package/lib/model/index.js.map +1 -0
- package/lib/model/model-resolver.d.ts +102 -0
- package/lib/model/model-resolver.d.ts.map +1 -0
- package/lib/model/model-resolver.js +206 -0
- package/lib/model/model-resolver.js.map +1 -0
- package/lib/model/prefs-store.d.ts +50 -0
- package/lib/model/prefs-store.d.ts.map +1 -0
- package/lib/model/prefs-store.js +215 -0
- package/lib/model/prefs-store.js.map +1 -0
- package/lib/model/settings-reader.d.ts +19 -0
- package/lib/model/settings-reader.d.ts.map +1 -0
- package/lib/model/settings-reader.js +76 -0
- package/lib/model/settings-reader.js.map +1 -0
- package/lib/model/types.d.ts +15 -0
- package/lib/model/types.d.ts.map +1 -0
- package/lib/model/types.js +5 -0
- package/lib/model/types.js.map +1 -0
- package/lib/resume-check.js +122 -0
- package/lib/resume-tool.js +29 -0
- package/lib/rules.js +57 -0
- package/lib/session/idle-evictor.d.ts +18 -0
- package/lib/session/idle-evictor.d.ts.map +1 -0
- package/lib/session/idle-evictor.js +31 -0
- package/lib/session/idle-evictor.js.map +1 -0
- package/lib/session/index.d.ts +9 -0
- package/lib/session/index.d.ts.map +1 -0
- package/lib/session/index.js +8 -0
- package/lib/session/index.js.map +1 -0
- package/lib/session/pick-agent.js +116 -0
- package/lib/session/session-id.d.ts +5 -0
- package/lib/session/session-id.d.ts.map +1 -0
- package/lib/session/session-id.js +15 -0
- package/lib/session/session-id.js.map +1 -0
- package/lib/session/session-manager.d.ts +138 -0
- package/lib/session/session-manager.d.ts.map +1 -0
- package/lib/session/session-manager.js +1157 -0
- package/lib/session/session-manager.js.bak-target +1081 -0
- package/lib/session/session-manager.js.bak-title-20261118 +1119 -0
- package/lib/session/session-manager.js.map +1 -0
- package/lib/session/types.d.ts +162 -0
- package/lib/session/types.d.ts.map +1 -0
- package/lib/session/types.js +2 -0
- package/lib/session/types.js.map +1 -0
- package/lib/setup/bind-routes.d.ts +78 -0
- package/lib/setup/bind-routes.d.ts.map +1 -0
- package/lib/setup/bind-routes.js +588 -0
- package/lib/setup/bind-routes.js.bak-join-163944 +761 -0
- package/lib/setup/bind-routes.js.map +1 -0
- package/lib/setup/binding-service.d.ts +86 -0
- package/lib/setup/binding-service.d.ts.map +1 -0
- package/lib/setup/binding-service.js +175 -0
- package/lib/setup/binding-service.js.map +1 -0
- package/lib/setup/index.d.ts +50 -0
- package/lib/setup/index.d.ts.map +1 -0
- package/lib/setup/index.js +163 -0
- package/lib/setup/index.js.map +1 -0
- package/lib/setup/persist.d.ts +42 -0
- package/lib/setup/persist.d.ts.map +1 -0
- package/lib/setup/persist.js +344 -0
- package/lib/setup/persist.js.map +1 -0
- package/lib/setup/presenters.d.ts +40 -0
- package/lib/setup/presenters.d.ts.map +1 -0
- package/lib/setup/presenters.js +84 -0
- package/lib/setup/presenters.js.map +1 -0
- package/lib/setup/qr-svg.d.ts +9 -0
- package/lib/setup/qr-svg.d.ts.map +1 -0
- package/lib/setup/qr-svg.js +33 -0
- package/lib/setup/qr-svg.js.map +1 -0
- package/lib/shared/bind-protocol.d.ts +42 -0
- package/lib/shared/bind-protocol.d.ts.map +1 -0
- package/lib/shared/bind-protocol.js +33 -0
- package/lib/shared/bind-protocol.js.map +1 -0
- package/lib/shared/dirs.d.ts +10 -0
- package/lib/shared/dirs.d.ts.map +1 -0
- package/lib/shared/dirs.js +20 -0
- package/lib/shared/dirs.js.map +1 -0
- package/lib/shared/http-body.js +43 -0
- package/lib/shared/index.d.ts +8 -0
- package/lib/shared/index.d.ts.map +1 -0
- package/lib/shared/index.js +8 -0
- package/lib/shared/index.js.map +1 -0
- package/lib/shared/json-file.js +34 -0
- package/lib/shared/orders.js +26 -0
- package/lib/shared/scope.d.ts +14 -0
- package/lib/shared/scope.d.ts.map +1 -0
- package/lib/shared/scope.js +12 -0
- package/lib/shared/scope.js.map +1 -0
- package/lib/shared/send-helper.d.ts +11 -0
- package/lib/shared/send-helper.d.ts.map +1 -0
- package/lib/shared/send-helper.js +13 -0
- package/lib/shared/send-helper.js.map +1 -0
- package/lib/shared/utils.d.ts +31 -0
- package/lib/shared/utils.d.ts.map +1 -0
- package/lib/shared/utils.js +228 -0
- package/lib/shared/utils.js.map +1 -0
- package/lib/tool-guard.js +74 -0
- package/lib/transport/attachment.d.ts +41 -0
- package/lib/transport/attachment.d.ts.map +1 -0
- package/lib/transport/attachment.js +191 -0
- package/lib/transport/attachment.js.map +1 -0
- package/lib/transport/chunker.d.ts +14 -0
- package/lib/transport/chunker.d.ts.map +1 -0
- package/lib/transport/chunker.js +78 -0
- package/lib/transport/chunker.js.map +1 -0
- package/lib/transport/deliverables.d.ts +20 -0
- package/lib/transport/deliverables.d.ts.map +1 -0
- package/lib/transport/deliverables.js +72 -0
- package/lib/transport/deliverables.js.map +1 -0
- package/lib/transport/events.d.ts +117 -0
- package/lib/transport/events.d.ts.map +1 -0
- package/lib/transport/events.js +112 -0
- package/lib/transport/events.js.map +1 -0
- package/lib/transport/inbound.d.ts +11 -0
- package/lib/transport/inbound.d.ts.map +1 -0
- package/lib/transport/inbound.js +511 -0
- package/lib/transport/inbound.js.map +1 -0
- package/lib/transport/index.d.ts +10 -0
- package/lib/transport/index.d.ts.map +1 -0
- package/lib/transport/index.js +10 -0
- package/lib/transport/index.js.map +1 -0
- package/lib/transport/msgid-cache.d.ts +23 -0
- package/lib/transport/msgid-cache.d.ts.map +1 -0
- package/lib/transport/msgid-cache.js +57 -0
- package/lib/transport/msgid-cache.js.map +1 -0
- package/lib/transport/notify.js +147 -0
- package/lib/transport/outbound-buffer.d.ts +42 -0
- package/lib/transport/outbound-buffer.d.ts.map +1 -0
- package/lib/transport/outbound-buffer.js +68 -0
- package/lib/transport/outbound-buffer.js.map +1 -0
- package/lib/transport/outbound.d.ts +42 -0
- package/lib/transport/outbound.d.ts.map +1 -0
- package/lib/transport/outbound.js +183 -0
- package/lib/transport/outbound.js.map +1 -0
- package/lib/transport/reply-limiter.d.ts +31 -0
- package/lib/transport/reply-limiter.d.ts.map +1 -0
- package/lib/transport/reply-limiter.js +50 -0
- package/lib/transport/reply-limiter.js.map +1 -0
- package/lib/transport/reply-target.d.ts +32 -0
- package/lib/transport/reply-target.d.ts.map +1 -0
- package/lib/transport/reply-target.js +64 -0
- package/lib/transport/reply-target.js.map +1 -0
- package/lib/transport/streaming-writer.d.ts +55 -0
- package/lib/transport/streaming-writer.d.ts.map +1 -0
- package/lib/transport/streaming-writer.js +122 -0
- package/lib/transport/streaming-writer.js.map +1 -0
- package/lib/transport/tool-presenter.d.ts +37 -0
- package/lib/transport/tool-presenter.d.ts.map +1 -0
- package/lib/transport/tool-presenter.js +116 -0
- package/lib/transport/tool-presenter.js.map +1 -0
- package/lib/turn-check.js +85 -0
- package/lib/types.d.ts +39 -0
- package/lib/types.js +5 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +127 -4
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 起草—确认:让模型先 qq_draft 起稿,再 qq_confirm 发出去。
|
|
3
|
+
*
|
|
4
|
+
* 和 dsh-qq 那套一致:回复正文永远不自动发;想发几条就重复 draft + confirm,
|
|
5
|
+
* 每条是独立的一条 QQ 消息;本轮结束还没确认的草稿直接作废。
|
|
6
|
+
* 这一轮不想说话就 qq_skip(只写日志,不发任何东西)。
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-qqbot-plus/media/draft-tool
|
|
9
|
+
*/
|
|
10
|
+
import { statSync, readFileSync } from 'node:fs'
|
|
11
|
+
import { homedir } from 'node:os'
|
|
12
|
+
import { isAbsolute, join, resolve } from 'node:path'
|
|
13
|
+
import { fileURLToPath } from 'node:url'
|
|
14
|
+
import { chunkMarkdownText } from '../transport/chunker.js'
|
|
15
|
+
import { notifySendFailure, classifySendError } from '../transport/notify.js'
|
|
16
|
+
import { isPathAllowed, resolveAgentCwd } from './send-media.js'
|
|
17
|
+
import { markSpoke, endTurn } from '../turn-check.js'
|
|
18
|
+
import { markOutreachSent, markOutreachSkipped } from '../outreach.js'
|
|
19
|
+
|
|
20
|
+
const textResult = (value) => [{ type: 'text', text: String(value) }]
|
|
21
|
+
|
|
22
|
+
/** 草稿/发送回执里给人看的去处标签:私聊 QQ <id>,群 QQ 群 <id>。 */
|
|
23
|
+
function targetLabel(target) {
|
|
24
|
+
if (!target || !target.targetId) return 'QQ'
|
|
25
|
+
return target.scope === 'group' ? `QQ 群 ${target.targetId}` : `QQ ${target.targetId}`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 这一轮的来处:'qq' = QQ 发来的,'auto' = 插件心跳叫醒的,'console' = 控制台/面板里敲的。
|
|
30
|
+
* 判据两样:消息正文有没有投递前缀([私聊 …] / [群「…」 …]),以及消息的 source.kind。
|
|
31
|
+
*/
|
|
32
|
+
const AUTO_KINDS = new Set(['qq-outreach', 'bili-autonomy', 'xhs-autonomy', 'bili-duty-scan', 'dsh-day', 'day-wake'])
|
|
33
|
+
/** 注入消息的识别:这些不是他说的,判来处时要跳过。 */
|
|
34
|
+
const INJECTED_RE = /^(Current runtime context|\[Reference attachments\]|Time sampled|\[Compressed conversation section\]|<system-reminder|<skill|<available_skills>|The available skill catalog changed|Use only names in this replacement catalog|(宿主刚重启过)/m
|
|
35
|
+
|
|
36
|
+
/** 最后一条 QQ 入站是不是刚刚。事件里翻不到人话时的最后一道判据。
|
|
37
|
+
* 窗口给到 10 分钟:一个回合里可能先跑了几分钟工具,收尾时才起草;
|
|
38
|
+
* 90 秒会把「他刚在群里问我」误判成 console,宁可多发一条。 */
|
|
39
|
+
function justInbound(ms = 10 * 60_000) {
|
|
40
|
+
try {
|
|
41
|
+
const raw = readFileSync(join(homedir(), '.dsh', 'dsh-qqbot-plus', 'runtime.json'), 'utf8')
|
|
42
|
+
const at = Date.parse(String(JSON.parse(raw)?.lastInbound?.at ?? ''))
|
|
43
|
+
if (!Number.isFinite(at)) return false
|
|
44
|
+
return Date.now() - at <= ms
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 这条消息是不是通道刚收到的那条入站(runtime.json 的 lastInbound.text)。 */
|
|
52
|
+
function isInboundText(text) {
|
|
53
|
+
try {
|
|
54
|
+
const raw = readFileSync(join(homedir(), '.dsh', 'dsh-qqbot-plus', 'runtime.json'), 'utf8')
|
|
55
|
+
const want = String(JSON.parse(raw)?.lastInbound?.text ?? '').trim()
|
|
56
|
+
if (want === '' || text === '') return false
|
|
57
|
+
return text.includes(want) || want.includes(text.slice(0, 40))
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function originOf(agent) {
|
|
65
|
+
try {
|
|
66
|
+
const session = agent?.session
|
|
67
|
+
const events = typeof session?.snapshotEvents === 'function' ? session.snapshotEvents() : session?.events
|
|
68
|
+
if (!Array.isArray(events)) return 'console'
|
|
69
|
+
// 往上最多看 8 条真人消息:这一轮里还夹着宿主注入的快照、重启提示、技能目录,
|
|
70
|
+
// 只看「最近一条」会被它们挡住,只看「第一条命中的」会先撞到 console。
|
|
71
|
+
const seen = []
|
|
72
|
+
for (let i = events.length - 1; i >= 0 && seen.length < 8; i -= 1) {
|
|
73
|
+
const event = events[i]
|
|
74
|
+
if (event?.type !== 'user/message' && event?.type !== 'user') continue
|
|
75
|
+
const text = String(event.text ?? event.content?.[0]?.text ?? '')
|
|
76
|
+
if (text === '') continue
|
|
77
|
+
if (INJECTED_RE.test(text)) continue
|
|
78
|
+
seen.push({ text, kind: String(event.source?.kind ?? '') })
|
|
79
|
+
}
|
|
80
|
+
for (const item of seen) {
|
|
81
|
+
if (/^\[(私聊|群「)/.test(item.text)) return 'qq'
|
|
82
|
+
if (isInboundText(item.text)) return 'qq'
|
|
83
|
+
}
|
|
84
|
+
for (const item of seen) {
|
|
85
|
+
if (AUTO_KINDS.has(item.kind)) return 'auto'
|
|
86
|
+
}
|
|
87
|
+
// 事件里翻不到这一轮的人话(有时候当前回合的消息还没进快照):
|
|
88
|
+
// 通道刚收到过 QQ 消息就按 QQ 算——宁可多发一条,不要让他等着。
|
|
89
|
+
return justInbound() ? 'qq' : 'console'
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return 'console'
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 人话体检:起草时顺手量一遍,只提示不改字、不阻断(源码在 lib/fishcheck/,抄自 dsh-fishcheck)。 */
|
|
97
|
+
let __fishcheck = null
|
|
98
|
+
async function fishHint(text) {
|
|
99
|
+
if (typeof text !== 'string' || text.trim().length < 20) return ''
|
|
100
|
+
try {
|
|
101
|
+
if (__fishcheck === null) __fishcheck = await import('../fishcheck/measure.js')
|
|
102
|
+
const r = __fishcheck.analyze(text)
|
|
103
|
+
if (r.score < 22) return ''
|
|
104
|
+
const tops = r.problems.slice(0, 3).map((p) => p.text)
|
|
105
|
+
return `体检 ${r.score} 分(${r.verdict}):${tops.join(' · ')}`
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return ''
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 语音文件允许的根目录(breeze_voice 落盘的地方)。 */
|
|
113
|
+
const VOICE_ROOT = join(homedir(), '.dsh', 'dsh-breeze-voice', 'audio')
|
|
114
|
+
/** 读回来的媒体上限,与 SDK 那侧一致。 */
|
|
115
|
+
const MAX_INLINE_BYTES = 50 * 1024 * 1024
|
|
116
|
+
|
|
117
|
+
/** data: URL → Buffer(只认 base64 段)。 */
|
|
118
|
+
function readDataUrl(text, field) {
|
|
119
|
+
const comma = text.indexOf(',')
|
|
120
|
+
if (comma < 0 || !/;base64/i.test(text.slice(0, comma))) {
|
|
121
|
+
throw new Error(`qq_draft: ${field} 的 data URL 需要 base64 编码`)
|
|
122
|
+
}
|
|
123
|
+
const buffer = Buffer.from(text.slice(comma + 1), 'base64')
|
|
124
|
+
if (buffer.byteLength === 0) throw new Error(`qq_draft: ${field} 的 data URL 解出来是空的`)
|
|
125
|
+
if (buffer.byteLength > MAX_INLINE_BYTES) throw new Error(`qq_draft: ${field} 太大(${buffer.byteLength} 字节)`)
|
|
126
|
+
return buffer
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 本机/局域网地址:腾讯那侧拉不到,得自己下成 buffer。 */
|
|
130
|
+
function isLocalHost(hostname) {
|
|
131
|
+
const host = String(hostname ?? '').toLowerCase().replace(/^\[|\]$/g, '')
|
|
132
|
+
return host === 'localhost' || host === '127.0.0.1' || host === '0.0.0.0' || host === '::1' || host.endsWith('.local')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function readLocalUrl(url, field) {
|
|
136
|
+
let response
|
|
137
|
+
try {
|
|
138
|
+
response = await fetch(url)
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
throw new Error(`qq_draft: ${field} 的本机地址读不到(${url}):${error?.message ?? error}`)
|
|
142
|
+
}
|
|
143
|
+
if (!response.ok) throw new Error(`qq_draft: ${field} 的本机地址返回 ${response.status}:${url}`)
|
|
144
|
+
const buffer = Buffer.from(await response.arrayBuffer())
|
|
145
|
+
if (buffer.byteLength === 0) throw new Error(`qq_draft: ${field} 的本机地址内容是空的:${url}`)
|
|
146
|
+
if (buffer.byteLength > MAX_INLINE_BYTES) throw new Error(`qq_draft: ${field} 太大(${buffer.byteLength} 字节)`)
|
|
147
|
+
return buffer
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function registerDraftTools(ctx, sender, mediaSender, manager, config, logger) {
|
|
151
|
+
const tools = ctx.get('tools')
|
|
152
|
+
if (!tools?.register) {
|
|
153
|
+
logger.warn('im-qqbot: tools 服务不可用,qq_draft/qq_confirm 未注册')
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
/** sessionId → 草稿(同一会话只留最近一份) */
|
|
157
|
+
const drafts = new Map()
|
|
158
|
+
const chunkLimit = config.textChunkLimit ?? 4500
|
|
159
|
+
const cwd = resolveAgentCwd(config.cwd)
|
|
160
|
+
const mediaPolicy = config.sendFile ?? {}
|
|
161
|
+
const recordOf = (agent) => (agent === undefined ? undefined : manager.findByAgent(agent))
|
|
162
|
+
const sessionKeyOf = (record) => record.agent?.session?.id ?? record.sessionId
|
|
163
|
+
/**
|
|
164
|
+
* 解析一个媒体参数:
|
|
165
|
+
* - http(s) 公网 URL:原样交给 SDK 拉取
|
|
166
|
+
* - 本机 / 局域网 URL:自己读成 buffer(腾讯服务器拉不到会报「上传URL错误」)
|
|
167
|
+
* - data: URL:解成 buffer
|
|
168
|
+
* - file:// URL 与本地路径:校验存在性与白名单后给 localPath,相对路径按会话 cwd 解析
|
|
169
|
+
*/
|
|
170
|
+
const sourceOf = async (raw, field, record) => {
|
|
171
|
+
const text = String(raw ?? '').trim()
|
|
172
|
+
if (text === '') return null
|
|
173
|
+
const sessionCwd = typeof record.agent?.session?.header?.cwd === 'string' ? record.agent.session.header.cwd : ''
|
|
174
|
+
if (text.startsWith('data:')) return { buffer: readDataUrl(text, field) }
|
|
175
|
+
if (/^https?:\/\//i.test(text)) {
|
|
176
|
+
let parsed
|
|
177
|
+
try {
|
|
178
|
+
parsed = new URL(text)
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
throw new Error(`qq_draft: ${field} 不是合法 URL:${text}`)
|
|
182
|
+
}
|
|
183
|
+
if (isLocalHost(parsed.hostname)) return await readLocalUrl(text, field)
|
|
184
|
+
return { url: text }
|
|
185
|
+
}
|
|
186
|
+
let file = text
|
|
187
|
+
if (/^file:\/\//i.test(text)) {
|
|
188
|
+
try {
|
|
189
|
+
file = fileURLToPath(text)
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
throw new Error(`qq_draft: ${field} 不是合法的 file:// 路径:${text}`)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// 相对路径按会话实际工作目录解析;共享会话时那是 dsh-qq 的工作区。
|
|
196
|
+
if (!isAbsolute(file)) file = resolve(sessionCwd === '' ? cwd : sessionCwd, file)
|
|
197
|
+
let info
|
|
198
|
+
try {
|
|
199
|
+
info = statSync(file)
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
throw new Error(`qq_draft: ${field} 指向的本地文件不存在:${file}`)
|
|
203
|
+
}
|
|
204
|
+
if (!info.isFile()) throw new Error(`qq_draft: ${field} 不是一个文件:${file}`)
|
|
205
|
+
const extraRoots = [
|
|
206
|
+
cwd,
|
|
207
|
+
...(sessionCwd === '' ? [] : [sessionCwd]),
|
|
208
|
+
...(Array.isArray(mediaPolicy.extraRoots) ? mediaPolicy.extraRoots : []),
|
|
209
|
+
...(field === 'voice' ? [VOICE_ROOT] : []),
|
|
210
|
+
]
|
|
211
|
+
const allowed = isPathAllowed(file, {
|
|
212
|
+
restrictPaths: mediaPolicy.restrictPaths !== false,
|
|
213
|
+
extraRoots,
|
|
214
|
+
}, sessionCwd === '' ? cwd : sessionCwd)
|
|
215
|
+
if (!allowed) throw new Error(`qq_draft: ${field} 的路径不在允许范围(media / agent 工作目录)内:${file}`)
|
|
216
|
+
return { localPath: file }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
tools.register({
|
|
220
|
+
name: 'qq_draft',
|
|
221
|
+
description: '先起一份 QQ 消息草稿(文本 / 图片 / 视频 / 文件 / 语音),一个字都不发。要发得再调 qq_confirm(decision="send");本轮结束前没确认的草稿会直接作废。想发几条就重复 draft + confirm。',
|
|
222
|
+
parameters: {
|
|
223
|
+
type: 'object',
|
|
224
|
+
properties: {
|
|
225
|
+
text: { type: 'string', description: '要发送的文本;QQ 里没有 Markdown 渲染,尽量简短' },
|
|
226
|
+
image: { type: 'string', description: '图片:http(s) 链接、file:// 或本地路径(相对路径按会话工作目录解析)' },
|
|
227
|
+
video: { type: 'string', description: '视频:http(s) 链接、file:// 或本地路径' },
|
|
228
|
+
file: { type: 'string', description: '普通文件:http(s) 链接、file:// 或本地路径' },
|
|
229
|
+
voice: { type: 'string', description: '语音:本地路径或 http(s) 链接;一次草稿只放一条语音' },
|
|
230
|
+
},
|
|
231
|
+
required: [],
|
|
232
|
+
additionalProperties: false,
|
|
233
|
+
},
|
|
234
|
+
output: { schema: { type: 'string' }, render: (_args, value) => textResult(value) },
|
|
235
|
+
async execute(args, exec) {
|
|
236
|
+
const origin = originOf(exec.agent)
|
|
237
|
+
if (origin === 'console') {
|
|
238
|
+
throw new Error('qq_draft: 这一轮是在控制台说的,正文就是回复,不要往 QQ 发')
|
|
239
|
+
}
|
|
240
|
+
const record = recordOf(exec.agent)
|
|
241
|
+
if (record === undefined) {
|
|
242
|
+
throw new Error(origin === 'qq'
|
|
243
|
+
? 'qq_draft: 这条是从 QQ 来的,但会话表里没有它的收件人记录——先别发,把情况写进正文'
|
|
244
|
+
: origin === 'auto'
|
|
245
|
+
? 'qq_draft: 这一轮是心跳叫醒的,没有收件人记录——把情况写进正文,别硬发'
|
|
246
|
+
: 'qq_draft: 这一轮是在控制台说的,正文就是回复,不要往 QQ 发')
|
|
247
|
+
}
|
|
248
|
+
const text = String(args.text ?? '').trim()
|
|
249
|
+
const image = await sourceOf(args.image, 'image', record)
|
|
250
|
+
const video = await sourceOf(args.video, 'video', record)
|
|
251
|
+
const file = await sourceOf(args.file, 'file', record)
|
|
252
|
+
const voice = await sourceOf(args.voice, 'voice', record)
|
|
253
|
+
if (text === '' && image === null && video === null && file === null && voice === null) {
|
|
254
|
+
throw new Error('qq_draft: 草稿是空的,至少给 text / image / video / file / voice 之一')
|
|
255
|
+
}
|
|
256
|
+
const key = sessionKeyOf(record)
|
|
257
|
+
drafts.set(key, { text, image, video, file, voice, replyTarget: record.replyTarget, createdAt: Date.now() })
|
|
258
|
+
const lines = [`[草稿已备好 → ${targetLabel(record.replyTarget)}]`]
|
|
259
|
+
lines.push(text === '' ? '正文:(没有文字,只有附件)' : `正文:${text}`)
|
|
260
|
+
const extras = [
|
|
261
|
+
image === null ? null : '图片',
|
|
262
|
+
video === null ? null : '视频',
|
|
263
|
+
file === null ? null : '文件',
|
|
264
|
+
voice === null ? null : '语音',
|
|
265
|
+
].filter(Boolean)
|
|
266
|
+
if (extras.length > 0) lines.push(`附件:${extras.join(' / ')}`)
|
|
267
|
+
const hint = await fishHint(text)
|
|
268
|
+
if (hint !== '') lines.push('', hint)
|
|
269
|
+
lines.push(
|
|
270
|
+
'',
|
|
271
|
+
'还一个字都没发出去。要发 → qq_confirm(decision="send");不发了 → qq_confirm(decision="cancel")。',
|
|
272
|
+
'本轮结束前没确认的草稿会直接作废。',
|
|
273
|
+
)
|
|
274
|
+
return lines.join('\n')
|
|
275
|
+
},
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
tools.register({
|
|
279
|
+
name: 'qq_confirm',
|
|
280
|
+
description: '把最近这份草稿发出去(decision="send"),或者作废它(decision="cancel")。只有最近一份草稿能被确认;想发下一条就再 qq_draft。',
|
|
281
|
+
parameters: {
|
|
282
|
+
type: 'object',
|
|
283
|
+
properties: {
|
|
284
|
+
decision: { type: 'string', enum: ['send', 'cancel'], description: 'send 发出去;cancel 作废' },
|
|
285
|
+
},
|
|
286
|
+
required: ['decision'],
|
|
287
|
+
additionalProperties: false,
|
|
288
|
+
},
|
|
289
|
+
output: { schema: { type: 'string' }, render: (_args, value) => textResult(value) },
|
|
290
|
+
async execute(args, exec) {
|
|
291
|
+
const record = recordOf(exec.agent)
|
|
292
|
+
if (record === undefined) throw new Error('qq_confirm: 这一轮不是从 QQ 来的,没有收件人——正文就是回复')
|
|
293
|
+
const key = sessionKeyOf(record)
|
|
294
|
+
const draft = drafts.get(key)
|
|
295
|
+
if (draft === undefined) throw new Error('qq_confirm: 现在没有待确认的草稿(先用 qq_draft 起草)')
|
|
296
|
+
const decision = String(args.decision ?? '').trim()
|
|
297
|
+
if (decision === 'cancel') {
|
|
298
|
+
drafts.delete(key)
|
|
299
|
+
return `草稿作废了,没有发到 ${targetLabel(record.replyTarget)}。`
|
|
300
|
+
}
|
|
301
|
+
if (decision !== 'send') throw new Error('qq_confirm: decision 只能是 send 或 cancel')
|
|
302
|
+
const target = draft.replyTarget ?? record.replyTarget
|
|
303
|
+
// 每成功发掉一块就从草稿里清掉:中途某个附件失败时,重试不会把已经发过的内容再发一遍。
|
|
304
|
+
const sentText = []
|
|
305
|
+
const chunks = chunkMarkdownText(draft.text ?? '', chunkLimit).filter((chunk) => chunk.trim() !== '')
|
|
306
|
+
for (let index = 0; index < chunks.length; index += 1) {
|
|
307
|
+
const chunk = chunks[index]
|
|
308
|
+
try {
|
|
309
|
+
await sender.sendMarkdown(target, chunk)
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
// 剩下的留在草稿里给重试;已经发出去的记进情节,别丢
|
|
313
|
+
draft.text = chunks.slice(index).join('\n')
|
|
314
|
+
if (sentText.length > 0) manager.memory?.noteAssistant?.(record, sentText.join('\n'))
|
|
315
|
+
notifySendFailure(record, error, { kind: 'text', sent: sentText.length, total: chunks.length })
|
|
316
|
+
throw new Error(`qq_confirm: 第 ${sentText.length + 1} 块(共 ${chunks.length} 块)没发出去:${classifySendError(error)}`)
|
|
317
|
+
}
|
|
318
|
+
sentText.push(chunk)
|
|
319
|
+
draft.text = chunks.slice(index + 1).join('\n')
|
|
320
|
+
}
|
|
321
|
+
// 我自己说的话也算情节:只记对方的话,翻回来就是我一个人的独白。
|
|
322
|
+
if (sentText.length > 0) manager.memory?.noteAssistant?.(record, sentText.join('\n'))
|
|
323
|
+
// 媒体:发失败就留在草稿里,并且让模型当场知道(工具也会抛,但注入能保证下一个 step 看见)
|
|
324
|
+
const sendMedia = async (field, label, run) => {
|
|
325
|
+
if (draft[field] == null) return
|
|
326
|
+
try {
|
|
327
|
+
await run(draft[field])
|
|
328
|
+
}
|
|
329
|
+
catch (error) {
|
|
330
|
+
notifySendFailure(record, error, { kind: 'file', name: label, dedup: field })
|
|
331
|
+
throw new Error(`qq_confirm: ${label}没发出去:${classifySendError(error)}`)
|
|
332
|
+
}
|
|
333
|
+
draft[field] = null
|
|
334
|
+
}
|
|
335
|
+
await sendMedia('image', '图片', (source) => mediaSender.sendImage(target, source))
|
|
336
|
+
await sendMedia('video', '视频', (source) => mediaSender.sendVideo(target, source))
|
|
337
|
+
await sendMedia('voice', '语音', (source) => mediaSender.sendVoice(target, source))
|
|
338
|
+
await sendMedia('file', '文件', (source) => {
|
|
339
|
+
const { fileName, ...rest } = source
|
|
340
|
+
return mediaSender.sendFile(target, rest, fileName === undefined ? undefined : { fileName })
|
|
341
|
+
})
|
|
342
|
+
drafts.delete(key)
|
|
343
|
+
markSpoke(key)
|
|
344
|
+
try { markOutreachSent() } catch { /* 记账失败不影响发送 */ }
|
|
345
|
+
return `已发送到 ${targetLabel(target)}。还有下一句就继续 qq_draft + qq_confirm;不要只写在回复正文里。`
|
|
346
|
+
},
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
tools.register({
|
|
350
|
+
name: 'qq_skip',
|
|
351
|
+
description: '这一轮不出声:不往 QQ 发任何东西。对方只是随口一句、话还没说完、或者你不想接,就用它——聊天里不是每句都要回。要说话用 qq_draft + qq_confirm。',
|
|
352
|
+
parameters: {
|
|
353
|
+
type: 'object',
|
|
354
|
+
properties: {
|
|
355
|
+
reason: { type: 'string', description: '为什么这轮不说话(只写进日志,不发给对方)' },
|
|
356
|
+
},
|
|
357
|
+
additionalProperties: false,
|
|
358
|
+
},
|
|
359
|
+
output: { schema: { type: 'string' }, render: (_args, value) => textResult(value) },
|
|
360
|
+
async execute(args, exec) {
|
|
361
|
+
const record = recordOf(exec.agent)
|
|
362
|
+
if (record === undefined) throw new Error('qq_skip: 这一轮不是从 QQ 来的,安静着就好')
|
|
363
|
+
if (config.allowSilence === false) throw new Error('qq_skip: 这里设成了每轮都要回,不能跳过(用 qq_draft + qq_confirm 回一句)')
|
|
364
|
+
const reason = typeof args.reason === 'string' ? args.reason.trim() : ''
|
|
365
|
+
logger.info(`im-qqbot: 这一轮不出声 ${record.sessionKey ?? ''}${reason === '' ? '' : ' · ' + reason}`)
|
|
366
|
+
markSpoke(sessionKeyOf(record))
|
|
367
|
+
try { markOutreachSkipped() } catch { /* 同理 */ }
|
|
368
|
+
return '好,这一轮不发了。'
|
|
369
|
+
},
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
// 本轮结束还没确认的草稿作废 —— 桥绝不替模型补发。
|
|
373
|
+
ctx.on('session/event', (session, event) => {
|
|
374
|
+
if (event?.type !== 'turn/end') return
|
|
375
|
+
try {
|
|
376
|
+
const result = endTurn(session?.id)
|
|
377
|
+
if (result.missed) logger.warn?.(`im-qqbot: 本轮没有发任何消息(累计 ${result.misses} 次)`)
|
|
378
|
+
} catch { /* 记账失败不影响回合结束 */ }
|
|
379
|
+
const key = session?.id
|
|
380
|
+
if (typeof key !== 'string') return
|
|
381
|
+
const draft = drafts.get(key)
|
|
382
|
+
if (draft === undefined) return
|
|
383
|
+
drafts.delete(key)
|
|
384
|
+
const preview = String(draft.text ?? '').slice(0, 40)
|
|
385
|
+
logger.info(`im-qqbot: 草稿没确认,作废 ${preview}`)
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
logger.info('im-qqbot: qq_draft / qq_confirm / qq_skip 工具已注册')
|
|
389
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { Logger } from '../types.ts';
|
|
3
|
+
/** 富媒体下载根目录(attachment.ts 复用) */
|
|
4
|
+
export declare const MEDIA_ROOT: string;
|
|
5
|
+
/**
|
|
6
|
+
* 清理过期的富媒体文件(按文件 mtime 判断,异步并发执行,不阻塞事件循环)
|
|
7
|
+
*
|
|
8
|
+
* @param ttlHours 保留时长(小时),<=0 表示永不过期(跳过清理)
|
|
9
|
+
* @returns 删除的文件数
|
|
10
|
+
*/
|
|
11
|
+
export declare function cleanupExpiredMedia(ttlHours: number, logger?: Logger): Promise<number>;
|
|
12
|
+
/**
|
|
13
|
+
* 启动富媒体过期清理:启动时清理一次 + 定期清理(每 1 小时)
|
|
14
|
+
*
|
|
15
|
+
* 用 ctx.effect 挂载定时器,插件卸载时自动 clearInterval。
|
|
16
|
+
* ttlHours <= 0 时不启动(永不过期)。
|
|
17
|
+
*/
|
|
18
|
+
export declare function startMediaCleanup(ctx: Context, ttlHours: number, logger: Logger): void;
|
|
19
|
+
//# sourceMappingURL=media-cleaner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-cleaner.d.ts","sourceRoot":"","sources":["../../src/media/media-cleaner.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C,iCAAiC;AACjC,eAAO,MAAM,UAAU,QAAgC,CAAC;AAKxD;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAyB5F;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CA4BtF"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 富媒体过期清理 — 避免下载文件持续膨胀
|
|
3
|
+
*
|
|
4
|
+
* 下载目录 ~/.dsh/dsh-qqbot-plus/media,文件写入后不再变更,故文件 mtime 即「下载时间」。
|
|
5
|
+
* 清理时按文件 mtime 判断是否超过保留时长。
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync } from 'node:fs';
|
|
8
|
+
import { readdir, rm, stat } from 'node:fs/promises';
|
|
9
|
+
import { join, resolve } from 'node:path';
|
|
10
|
+
import { PLUGIN_HOME } from "../shared/dirs.js";
|
|
11
|
+
/** 富媒体下载根目录(attachment.ts 复用) */
|
|
12
|
+
export const MEDIA_ROOT = resolve(PLUGIN_HOME, 'media');
|
|
13
|
+
/** 定期清理间隔(毫秒),固定 1 小时 */
|
|
14
|
+
const CLEANUP_INTERVAL_MS = 60 * 60 * 1000;
|
|
15
|
+
/**
|
|
16
|
+
* 清理过期的富媒体文件(按文件 mtime 判断,异步并发执行,不阻塞事件循环)
|
|
17
|
+
*
|
|
18
|
+
* @param ttlHours 保留时长(小时),<=0 表示永不过期(跳过清理)
|
|
19
|
+
* @returns 删除的文件数
|
|
20
|
+
*/
|
|
21
|
+
export async function cleanupExpiredMedia(ttlHours, logger) {
|
|
22
|
+
if (ttlHours <= 0)
|
|
23
|
+
return 0;
|
|
24
|
+
if (!existsSync(MEDIA_ROOT))
|
|
25
|
+
return 0;
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const ttlMs = ttlHours * 60 * 60 * 1000;
|
|
28
|
+
const entries = await readdir(MEDIA_ROOT, { withFileTypes: true });
|
|
29
|
+
const files = entries.filter(entry => entry.isFile()).map(entry => entry.name);
|
|
30
|
+
const results = await Promise.all(files.map(async (name) => {
|
|
31
|
+
const filePath = join(MEDIA_ROOT, name);
|
|
32
|
+
try {
|
|
33
|
+
const info = await stat(filePath);
|
|
34
|
+
if (now - info.mtimeMs <= ttlMs)
|
|
35
|
+
return false;
|
|
36
|
+
await rm(filePath, { force: true });
|
|
37
|
+
logger?.debug(`im-qqbot: cleaned expired media: ${name}`);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
logger?.warn(`im-qqbot: media cleanup failed for ${name}: ${err instanceof Error ? err.message : String(err)}`);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
return results.filter(Boolean).length;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 启动富媒体过期清理:启动时清理一次 + 定期清理(每 1 小时)
|
|
49
|
+
*
|
|
50
|
+
* 用 ctx.effect 挂载定时器,插件卸载时自动 clearInterval。
|
|
51
|
+
* ttlHours <= 0 时不启动(永不过期)。
|
|
52
|
+
*/
|
|
53
|
+
export function startMediaCleanup(ctx, ttlHours, logger) {
|
|
54
|
+
if (ttlHours <= 0)
|
|
55
|
+
return;
|
|
56
|
+
logger.info(`im-qqbot: media cleanup started (ttl=${ttlHours}h, interval=1h, root=${MEDIA_ROOT})`);
|
|
57
|
+
// 启动时立即清理一次(异步,不阻塞启动流程)
|
|
58
|
+
void cleanupExpiredMedia(ttlHours, logger)
|
|
59
|
+
.then((cleaned) => {
|
|
60
|
+
if (cleaned > 0)
|
|
61
|
+
logger.info(`im-qqbot: cleaned ${cleaned} expired media files`);
|
|
62
|
+
})
|
|
63
|
+
.catch((err) => {
|
|
64
|
+
logger.warn(`im-qqbot: media cleanup failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
65
|
+
});
|
|
66
|
+
ctx
|
|
67
|
+
.effect(() => {
|
|
68
|
+
const timer = setInterval(() => {
|
|
69
|
+
void cleanupExpiredMedia(ttlHours, logger)
|
|
70
|
+
.then((n) => {
|
|
71
|
+
if (n > 0)
|
|
72
|
+
logger.info(`im-qqbot: cleaned ${n} expired media files`);
|
|
73
|
+
})
|
|
74
|
+
.catch((err) => {
|
|
75
|
+
logger.warn(`im-qqbot: media cleanup failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
76
|
+
});
|
|
77
|
+
}, CLEANUP_INTERVAL_MS);
|
|
78
|
+
return async () => { clearInterval(timer); };
|
|
79
|
+
}, 'im-qqbot.media-cleanup');
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=media-cleaner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-cleaner.js","sourceRoot":"","sources":["../../src/media/media-cleaner.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内置 qqbot_say 工具 —— 让模型自己决定「这一轮回哪几句」。
|
|
3
|
+
*
|
|
4
|
+
* 官方插件默认把 assistant 的整段正文直接发出去(流式或整块),像客服。这里改成
|
|
5
|
+
* 由模型按人设主动说话:想回就调 1–3 次 qqbot_say,每句短;调过之后本轮正文
|
|
6
|
+
* 不再自动发送(outbound 看 record.saidViaTool 抑制),没调用才退回原来的
|
|
7
|
+
* 「轮末发一条正文」的兜底行为。
|
|
8
|
+
*/
|
|
9
|
+
import { chunkMarkdownText } from "../transport/chunker.js";
|
|
10
|
+
/** 工具名(qqbot 前缀避免与其他通道的同名工具冲突) */
|
|
11
|
+
export const SAY_TOOL_NAME = 'qqbot_say';
|
|
12
|
+
const DESCRIPTION = 'Send one short chat message to the QQ user or group of the current conversation. '
|
|
13
|
+
+ 'Call this 1-3 times per turn to say what you want to say, one sentence per call. '
|
|
14
|
+
+ 'Keep each message under 25 Chinese characters. '
|
|
15
|
+
+ 'If you call this at least once, the assistant text of this turn will NOT be sent again.';
|
|
16
|
+
/** 注册 qqbot_say 工具(tools 服务缺失时优雅降级,不阻断插件启动) */
|
|
17
|
+
export function registerSayTool(ctx, sender, manager, config, logger) {
|
|
18
|
+
const tools = ctx.get('tools');
|
|
19
|
+
if (!tools?.register) {
|
|
20
|
+
logger.warn('im-qqbot: tools 服务不可用,qqbot_say 工具未注册');
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const limit = Math.max(1, Math.min(config.textChunkLimit ?? 4500, 2000));
|
|
24
|
+
const definition = {
|
|
25
|
+
name: SAY_TOOL_NAME,
|
|
26
|
+
description: DESCRIPTION,
|
|
27
|
+
parameters: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
text: {
|
|
31
|
+
type: 'string',
|
|
32
|
+
description: '一条要发出去的聊天消息(尽量 25 字以内)',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
required: ['text'],
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
},
|
|
38
|
+
output: {
|
|
39
|
+
schema: {
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
ok: { type: 'boolean' },
|
|
43
|
+
chars: { type: 'integer' },
|
|
44
|
+
},
|
|
45
|
+
required: ['ok', 'chars'],
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
},
|
|
48
|
+
render: (_args, value) => [{ type: 'text', text: `已发送(${value.chars} 字)` }],
|
|
49
|
+
},
|
|
50
|
+
async execute(args, exec) {
|
|
51
|
+
const text = String(args?.text ?? '').trim();
|
|
52
|
+
if (!text) {
|
|
53
|
+
throw new Error('qqbot_say: text 不能为空');
|
|
54
|
+
}
|
|
55
|
+
if (text.length > limit) {
|
|
56
|
+
throw new Error(`qqbot_say: 单条太长(${text.length} > ${limit}),请拆成更短的几句`);
|
|
57
|
+
}
|
|
58
|
+
const record = manager.findByAgent(exec.agent);
|
|
59
|
+
if (record === undefined) {
|
|
60
|
+
throw new Error('qqbot_say: 当前会话不是 QQ 会话,无法发送');
|
|
61
|
+
}
|
|
62
|
+
const chunks = chunkMarkdownText(text, config.textChunkLimit ?? 4500);
|
|
63
|
+
for (const chunk of chunks) {
|
|
64
|
+
await sender.sendMarkdown(record.replyTarget, chunk);
|
|
65
|
+
}
|
|
66
|
+
record.saidViaTool = true;
|
|
67
|
+
return { ok: true, chars: text.length };
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
tools.register(definition);
|
|
71
|
+
logger.info('im-qqbot: qqbot_say 工具已注册');
|
|
72
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { ImQQBotConfig } from '../config.ts';
|
|
3
|
+
import type { SessionManager } from '../session/index.ts';
|
|
4
|
+
import type { Logger } from '../types.ts';
|
|
5
|
+
import { type MediaSenderLike } from './send-media.ts';
|
|
6
|
+
/** 工具名(qqbot 前缀避免与其他通道的同名工具冲突) */
|
|
7
|
+
export declare const SEND_FILE_TOOL_NAME = "qqbot_send_file";
|
|
8
|
+
/** 发送器与分类发送的共享实现见 send-media.ts(扩展名分发 + 白名单为单一事实源) */
|
|
9
|
+
export type { MediaSenderLike, MediaSendResult } from './send-media.ts';
|
|
10
|
+
/** 注册 qqbot_send_file 工具(tools 服务缺失时优雅降级,不阻断插件启动) */
|
|
11
|
+
export declare function registerSendFileTool(ctx: Context, bot: MediaSenderLike, manager: SessionManager, config: ImQQBotConfig, logger: Logger): void;
|
|
12
|
+
//# sourceMappingURL=send-file-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send-file-tool.d.ts","sourceRoot":"","sources":["../../src/media/send-file-tool.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAY,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,KAAK,EAAE,MAAM,EAAe,MAAM,aAAa,CAAC;AACvD,OAAO,EAAiD,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEtG,kCAAkC;AAClC,eAAO,MAAM,mBAAmB,oBAAoB,CAAC;AAQrD,sDAAsD;AACtD,YAAY,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAuBxE,qDAAqD;AACrD,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,eAAe,EACpB,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,MAAM,GACb,IAAI,CAkGN"}
|