@cmach_socket/opencode-qq-bot 0.10.1 → 1.0.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 +35 -32
- package/package.json +1 -1
- package/src/bot-lock.ts +47 -0
- package/src/config/store.ts +1 -1
- package/src/diary/summary.ts +84 -28
- package/src/emoji/collection.ts +2 -0
- package/src/index.ts +47 -15
- package/src/media-path.ts +5 -7
- package/src/model-view.ts +20 -1
- package/src/naplink/client.ts +2 -1
- package/src/naplink/history.ts +3 -3
- package/src/naplink/media.ts +178 -43
- package/src/naplink/render.ts +205 -55
- package/src/panel/documents.ts +21 -6
- package/src/restartable.ts +14 -0
- package/src/session/manager.ts +158 -30
- package/src/session/prompt.ts +2 -0
- package/src/session/soul-guard.ts +36 -5
- package/src/session/soul.ts +24 -11
- package/src/tools/policy.ts +5 -4
- package/src/tools/qq.ts +380 -161
- package/src/tools/send-guard.ts +11 -1
package/src/tools/qq.ts
CHANGED
|
@@ -25,6 +25,7 @@ import type { Logger } from "../logger"
|
|
|
25
25
|
import type { OutboundMedia } from "../media-path"
|
|
26
26
|
import { neutralizeStructureTags, sanitizeUntrustedText } from "../model-view"
|
|
27
27
|
import type { QQClient } from "../naplink/client"
|
|
28
|
+
import type { MediaStore } from "../naplink/media"
|
|
28
29
|
import { splitGraphemes, splitMessage, segmentDelayMs, toPlainText } from "../outbound"
|
|
29
30
|
import type { RateLimitVerdict, SendRateLimiter } from "../rate-limit"
|
|
30
31
|
import { modelSendScope, pluginSendScope } from "../rate-limit"
|
|
@@ -63,6 +64,7 @@ export const QQ_TOOL_NAMES = [
|
|
|
63
64
|
"qq_collect_sticker",
|
|
64
65
|
"qq_get_group_member_info",
|
|
65
66
|
"qq_fetch_ptt_text",
|
|
67
|
+
"qq_download_file",
|
|
66
68
|
"qq_get_login_info",
|
|
67
69
|
"qq_send_file",
|
|
68
70
|
"qq_diary_write",
|
|
@@ -90,6 +92,8 @@ export interface ToolDeps {
|
|
|
90
92
|
sensitive: SensitiveChecker
|
|
91
93
|
/** 出站媒体的路径与地址把关。 */
|
|
92
94
|
outbound: OutboundMedia
|
|
95
|
+
/** 入站媒体存储,供 qq_download_file 定位落盘文件。 */
|
|
96
|
+
media: MediaStore
|
|
93
97
|
/**
|
|
94
98
|
* 普通成员可用的出站媒体把关,只放开媒体目录。
|
|
95
99
|
*
|
|
@@ -110,14 +114,14 @@ export interface ToolDeps {
|
|
|
110
114
|
}
|
|
111
115
|
|
|
112
116
|
interface SendMessageInput {
|
|
113
|
-
content?: string
|
|
117
|
+
content?: string[]
|
|
114
118
|
reply?: boolean
|
|
115
119
|
unlimit_length?: boolean
|
|
116
120
|
}
|
|
117
121
|
|
|
118
122
|
interface MediaInput {
|
|
119
|
-
file?: string
|
|
120
|
-
base64?: string
|
|
123
|
+
file?: string[]
|
|
124
|
+
base64?: string[]
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
interface ForwardNodeInput {
|
|
@@ -133,7 +137,7 @@ interface UserInput {
|
|
|
133
137
|
}
|
|
134
138
|
|
|
135
139
|
interface StickerInput {
|
|
136
|
-
id?: number
|
|
140
|
+
id?: number[]
|
|
137
141
|
}
|
|
138
142
|
|
|
139
143
|
interface CollectStickerInput {
|
|
@@ -158,9 +162,36 @@ interface DiaryListInput {
|
|
|
158
162
|
days?: number
|
|
159
163
|
}
|
|
160
164
|
|
|
165
|
+
interface DownloadFileInput {
|
|
166
|
+
id?: string
|
|
167
|
+
offset?: number
|
|
168
|
+
limit?: number
|
|
169
|
+
}
|
|
170
|
+
|
|
161
171
|
interface FileInput {
|
|
162
|
-
file?: string
|
|
163
|
-
name?: string
|
|
172
|
+
file?: string[]
|
|
173
|
+
name?: string[]
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 合并转发里的一个节点:发送者固定为机器人自己,正文是纯文本。 */
|
|
177
|
+
interface ForwardNode {
|
|
178
|
+
user_id: number
|
|
179
|
+
nickname: string
|
|
180
|
+
content: { type: "text"; data: { text: string } }[]
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 一条待发消息:纯文本、媒体段,或要合并转发出去的节点。 */
|
|
184
|
+
type Outgoing =
|
|
185
|
+
| { kind: "text"; text: string }
|
|
186
|
+
| { kind: "segments"; payload: unknown[] }
|
|
187
|
+
| { kind: "forward"; nodes: ForwardNode[] }
|
|
188
|
+
|
|
189
|
+
/** 一批发送的文案口径:名目与量词。 */
|
|
190
|
+
interface SendWording {
|
|
191
|
+
/** 名目,如「消息」「图片」。 */
|
|
192
|
+
noun: string
|
|
193
|
+
/** 量词,如「条」「张」。 */
|
|
194
|
+
unit: string
|
|
164
195
|
}
|
|
165
196
|
|
|
166
197
|
/** 合并转发里单个节点的字数上限,留出余量避免被 NapCat 截断。 */
|
|
@@ -175,6 +206,64 @@ function text(value: string, metadata?: Record<string, unknown>) {
|
|
|
175
206
|
/** 工具返回值:content 是给模型看的文本,metadata 带失败标记。 */
|
|
176
207
|
type ToolResult = ReturnType<typeof text>
|
|
177
208
|
|
|
209
|
+
/**
|
|
210
|
+
* 把模型给的入参归一成字符串列表。
|
|
211
|
+
*
|
|
212
|
+
* 单个字符串按一条处理:模型偶尔不按 schema 传数组。
|
|
213
|
+
* 空串与空白丢掉:它们不构成一条消息。
|
|
214
|
+
*/
|
|
215
|
+
function toStringList(value: unknown): string[] {
|
|
216
|
+
const raw = Array.isArray(value) ? value : [value]
|
|
217
|
+
return raw
|
|
218
|
+
.filter((item): item is string => typeof item === "string")
|
|
219
|
+
.map((item) => item.trim())
|
|
220
|
+
.filter((item) => item.length > 0)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 把模型给的入参归一成字符串列表,保留原有下标。
|
|
225
|
+
*
|
|
226
|
+
* 用于与 `file` 一一对应的 `name`:空串占位,后面的名字才不会错位。
|
|
227
|
+
*/
|
|
228
|
+
function toStringSlots(value: unknown): string[] {
|
|
229
|
+
const raw = Array.isArray(value) ? value : [value]
|
|
230
|
+
return raw.map((item) => (typeof item === "string" ? item.trim() : ""))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** 把模型给的入参归一成整数列表:非整数丢掉。 */
|
|
234
|
+
function toNumberList(value: unknown): number[] {
|
|
235
|
+
const raw = Array.isArray(value) ? value : [value]
|
|
236
|
+
return raw.filter((item): item is number => typeof item === "number" && Number.isInteger(item))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 消息发送成功的文案:一条按单数说,多条报出条数。 */
|
|
240
|
+
function sentMessageText(count: number): string {
|
|
241
|
+
return count === 1
|
|
242
|
+
? "你已成功发送消息,可以选择结束对话"
|
|
243
|
+
: `你已成功发送 ${count} 条消息,可以选择结束对话`
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** 图片发送成功的文案:一张按单数说,多张报出数量。 */
|
|
247
|
+
function sentImageText(count: number): string {
|
|
248
|
+
return count === 1
|
|
249
|
+
? "你已成功发送图片,可以选择结束对话"
|
|
250
|
+
: `你已成功发送 ${count} 张图片,可以选择结束对话`
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** 语音发送成功的文案:一条按单数说,多条报出数量。 */
|
|
254
|
+
function sentRecordText(count: number): string {
|
|
255
|
+
return count === 1
|
|
256
|
+
? "你已成功发送语音,可以选择结束对话"
|
|
257
|
+
: `你已成功发送 ${count} 条语音,可以选择结束对话`
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** 文件发送成功的文案:一个按单数说,多个报出数量。 */
|
|
261
|
+
function sentFileText(count: number): string {
|
|
262
|
+
return count === 1
|
|
263
|
+
? "你已成功发送文件,可以选择结束对话"
|
|
264
|
+
: `你已成功发送 ${count} 个文件,可以选择结束对话`
|
|
265
|
+
}
|
|
266
|
+
|
|
178
267
|
/** 日记正文进模型上下文的固定上限,超出部分截断。 */
|
|
179
268
|
const DIARY_READ_MAX_CHARS = 4000
|
|
180
269
|
|
|
@@ -326,6 +415,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
326
415
|
sensitive,
|
|
327
416
|
outbound,
|
|
328
417
|
outboundMember,
|
|
418
|
+
media,
|
|
329
419
|
diary,
|
|
330
420
|
tracer,
|
|
331
421
|
} = deps
|
|
@@ -458,7 +548,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
458
548
|
/** 限流拒绝时的工具返回值:本轮算已回复,避免模型被反复提醒补发。 */
|
|
459
549
|
const refuse = (sessionID: string, verdict: RateLimitVerdict, sent = 0, total = 0) => {
|
|
460
550
|
manager.markMessageSent(sessionID)
|
|
461
|
-
const progress = total > 0 ? `(已发送 ${sent}/${total}
|
|
551
|
+
const progress = total > 0 ? `(已发送 ${sent}/${total} 条)` : ""
|
|
462
552
|
// 限流拒绝是“请停止发送”,不要因为它允许模型立刻重试。
|
|
463
553
|
return text(`error: ${verdict.reason}${progress}`, { retryable: false })
|
|
464
554
|
}
|
|
@@ -525,40 +615,99 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
525
615
|
}
|
|
526
616
|
|
|
527
617
|
/**
|
|
528
|
-
*
|
|
618
|
+
* 合并转发里的节点:发送者固定为机器人自己,正文按节点字数上限拆开。
|
|
529
619
|
*
|
|
530
620
|
* 一口气刷十几条会把群刷爆,合并转发只占一条消息。
|
|
531
621
|
*/
|
|
532
|
-
const
|
|
533
|
-
sessionID: string,
|
|
534
|
-
target: ChatTarget,
|
|
535
|
-
plain: string,
|
|
536
|
-
operation: string,
|
|
537
|
-
length: number,
|
|
538
|
-
) => {
|
|
539
|
-
const label = targetLabel(target)
|
|
622
|
+
const buildForwardNodes = async (plain: string): Promise<ForwardNode[]> => {
|
|
540
623
|
const identity = await botIdentity()
|
|
541
|
-
|
|
624
|
+
return splitMessage(plain, FORWARD_NODE_CHARS).map((chunk) => ({
|
|
542
625
|
user_id: identity.userId,
|
|
543
626
|
nickname: identity.nickname,
|
|
544
627
|
content: [{ type: "text", data: { text: chunk } }],
|
|
545
628
|
}))
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** 按消息类型投递出去。 */
|
|
632
|
+
const deliver = async (target: ChatTarget, message: Outgoing) => {
|
|
633
|
+
if (message.kind === "text") return send(target, message.text)
|
|
634
|
+
if (message.kind === "segments") return send(target, message.payload)
|
|
635
|
+
return sendForward(target, message.nodes)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/** 一批发送的参数。 */
|
|
639
|
+
interface BatchSend {
|
|
640
|
+
/** 这批消息的出站形式,按发送顺序排列。 */
|
|
641
|
+
messages: Outgoing[]
|
|
642
|
+
/** 名目与量词,用于报错文案。 */
|
|
643
|
+
wording: SendWording
|
|
644
|
+
/** 成功文案;参数是实际发出的条数。 */
|
|
645
|
+
successText: (count: number) => string
|
|
646
|
+
/** 记进追踪的字数;媒体类工具没有字数,不传就不记。 */
|
|
647
|
+
chars?: number
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* 按顺序发一批消息。
|
|
652
|
+
*
|
|
653
|
+
* 一条一次出站许可,条与条之间按节奏等待,看起来像人在分条打字;
|
|
654
|
+
* 中途被限流或失败时如实报出已发出几条。
|
|
655
|
+
*/
|
|
656
|
+
const sendOutgoing = async (
|
|
657
|
+
sessionID: string,
|
|
658
|
+
target: ChatTarget,
|
|
659
|
+
operation: string,
|
|
660
|
+
batch: BatchSend,
|
|
661
|
+
): Promise<ToolResult> => {
|
|
662
|
+
const label = targetLabel(target)
|
|
663
|
+
const { messages, wording } = batch
|
|
664
|
+
// 段与段之间的间隔:配了随机区间就在区间里等。
|
|
665
|
+
const wait = segmentDelayMs({
|
|
666
|
+
sendDelayMs: config.sendDelayMs,
|
|
667
|
+
minDelayMs: config.reply.minDelayMs,
|
|
668
|
+
maxDelayMs: config.reply.maxDelayMs,
|
|
669
|
+
})
|
|
670
|
+
let sent = 0
|
|
546
671
|
try {
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
672
|
+
for (const [index, message] of messages.entries()) {
|
|
673
|
+
const verdict = await permit(target, operation)
|
|
674
|
+
if (!verdict.allowed) return refuse(sessionID, verdict, sent, messages.length)
|
|
675
|
+
const response = await deliver(target, message)
|
|
676
|
+
trackSent(target, response)
|
|
677
|
+
sent += 1
|
|
678
|
+
if (index < messages.length - 1 && wait > 0) {
|
|
679
|
+
await delay(wait)
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
} catch (error) {
|
|
683
|
+
if (sent > 0) manager.markMessageSent(sessionID)
|
|
684
|
+
logger.error(`${label} 发送${wording.noun}失败,已发送 ${sent}/${messages.length}`, error)
|
|
685
|
+
// 已经发出部分消息时不要允许整条重试,否则会重复发送。
|
|
686
|
+
return text(`error: ${wording.noun}只发送了 ${sent}/${messages.length} ${wording.unit}`, {
|
|
687
|
+
retryable: sent === 0,
|
|
688
|
+
})
|
|
689
|
+
}
|
|
690
|
+
manager.markMessageSent(sessionID)
|
|
691
|
+
if (batch.chars !== undefined) {
|
|
692
|
+
const nodes = messages.reduce(
|
|
693
|
+
(sum, message) => sum + (message.kind === "forward" ? message.nodes.length : 0),
|
|
694
|
+
0,
|
|
695
|
+
)
|
|
552
696
|
logger.info(
|
|
553
|
-
|
|
697
|
+
nodes > 0
|
|
698
|
+
? `${label} 已发送 ${batch.chars} 字(合并转发 ${nodes} 个节点)`
|
|
699
|
+
: `${label} 已发送 ${batch.chars} 字`,
|
|
554
700
|
traceMeta(sessionID),
|
|
555
701
|
)
|
|
556
|
-
await traceSent(
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
702
|
+
await traceSent(
|
|
703
|
+
sessionID,
|
|
704
|
+
target,
|
|
705
|
+
nodes > 0
|
|
706
|
+
? { chars: batch.chars, mode: "forward", nodes }
|
|
707
|
+
: { chars: batch.chars, mode: "plain", segments: sent },
|
|
708
|
+
)
|
|
561
709
|
}
|
|
710
|
+
return text(batch.successText(sent))
|
|
562
711
|
}
|
|
563
712
|
|
|
564
713
|
const result = await ctx.tool.transform((editor) => {
|
|
@@ -611,11 +760,15 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
611
760
|
editor.add({
|
|
612
761
|
name: "send_message",
|
|
613
762
|
description:
|
|
614
|
-
"向当前 QQ
|
|
763
|
+
"向当前 QQ 群聊或私聊发送消息。content 是文本列表:一条内容发一条消息,要分多条说就一次给全。reply: true 表示发送,false 表示选择不发送(已读不回);unlimit_length: 设为 true 可解除默认的长度告警限制。",
|
|
615
764
|
input: {
|
|
616
765
|
type: "object",
|
|
617
766
|
properties: {
|
|
618
|
-
content: {
|
|
767
|
+
content: {
|
|
768
|
+
type: "array",
|
|
769
|
+
description: "要发送的文本内容列表,一条内容发一条消息;reply=false 时可省略",
|
|
770
|
+
items: { type: "string" },
|
|
771
|
+
},
|
|
619
772
|
reply: { type: "boolean", description: "true 表示发送,false 表示本轮不回复" },
|
|
620
773
|
unlimit_length: { type: "boolean", description: "解除默认的消息长度限制" },
|
|
621
774
|
},
|
|
@@ -634,83 +787,78 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
634
787
|
manager.markMessageSent(context.sessionID)
|
|
635
788
|
return text("skipped")
|
|
636
789
|
}
|
|
637
|
-
|
|
638
|
-
|
|
790
|
+
// 模型偶尔不听话写 markdown,剥成纯文本再发出去。
|
|
791
|
+
const plains = toStringList(input.content)
|
|
792
|
+
.map((item) => toPlainText(item).trim())
|
|
793
|
+
.filter((item) => item.length > 0)
|
|
794
|
+
if (plains.length === 0) {
|
|
639
795
|
logger.info(`${label} 回复内容为空`)
|
|
640
796
|
manager.markMessageSent(context.sessionID)
|
|
641
797
|
return text("skipped")
|
|
642
798
|
}
|
|
643
799
|
|
|
644
|
-
// 模型偶尔不听话写 markdown,剥成纯文本再发出去。
|
|
645
|
-
const plain = toPlainText(content).trim()
|
|
646
|
-
if (!plain) {
|
|
647
|
-
logger.info(`${label} 回复内容剥离后为空`)
|
|
648
|
-
manager.markMessageSent(context.sessionID)
|
|
649
|
-
return text("skipped")
|
|
650
|
-
}
|
|
651
800
|
const denied = checkReplyPermission(context.sessionID, target)
|
|
652
801
|
if (denied) return text(denied)
|
|
653
|
-
|
|
802
|
+
// 整批先过出站检查:有一条命中敏感信息就整批不发。
|
|
803
|
+
for (const plain of plains) guardOutbound(target, plain)
|
|
654
804
|
|
|
655
805
|
// 长度按字素簇算,和拆段的口径一致:一个 emoji 只算一个。
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
806
|
+
const chars = plains.reduce((sum, plain) => sum + splitGraphemes(plain).length, 0)
|
|
807
|
+
for (const plain of plains) {
|
|
808
|
+
const length = splitGraphemes(plain).length
|
|
809
|
+
if (length > config.warnLength && input.unlimit_length !== true) {
|
|
810
|
+
logger.warn(`send_message 内容长度 ${length} 超过 ${config.warnLength}`)
|
|
811
|
+
}
|
|
659
812
|
}
|
|
660
813
|
|
|
661
814
|
const operation = nextOperation()
|
|
662
815
|
// 太长的回复改成合并转发,别一口气刷屏。
|
|
663
816
|
const forwardThreshold = config.reply.forwardThresholdChars
|
|
664
|
-
|
|
665
|
-
|
|
817
|
+
const messages: Outgoing[] = []
|
|
818
|
+
for (const plain of plains) {
|
|
819
|
+
const length = splitGraphemes(plain).length
|
|
820
|
+
if (forwardThreshold > 0 && length > forwardThreshold) {
|
|
821
|
+
messages.push({ kind: "forward", nodes: await buildForwardNodes(plain) })
|
|
822
|
+
continue
|
|
823
|
+
}
|
|
824
|
+
// 单条超过配置的字数就拆成多段发。
|
|
825
|
+
for (const chunk of splitMessage(plain, config.reply.segmentChars)) {
|
|
826
|
+
messages.push({ kind: "text", text: chunk })
|
|
827
|
+
}
|
|
666
828
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
829
|
+
// 剩余额度不够时,宁可把整批合成一条合并转发,也不要把回复截成半截。
|
|
830
|
+
if (forwardThreshold > 0 && messages.length > limiter.remaining(modelSendScope(target))) {
|
|
831
|
+
const nodes: ForwardNode[] = []
|
|
832
|
+
for (const plain of plains) nodes.push(...(await buildForwardNodes(plain)))
|
|
833
|
+
messages.length = 0
|
|
834
|
+
messages.push({ kind: "forward", nodes })
|
|
673
835
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
836
|
+
return await sendOutgoing(context.sessionID, target, operation, {
|
|
837
|
+
messages,
|
|
838
|
+
wording: { noun: "消息", unit: "条" },
|
|
839
|
+
successText: sentMessageText,
|
|
840
|
+
chars,
|
|
679
841
|
})
|
|
680
|
-
let sent = 0
|
|
681
|
-
try {
|
|
682
|
-
for (const [index, chunk] of chunks.entries()) {
|
|
683
|
-
const verdict = await permit(target, operation)
|
|
684
|
-
if (!verdict.allowed) return refuse(context.sessionID, verdict, sent, chunks.length)
|
|
685
|
-
const response = await send(target, chunk)
|
|
686
|
-
trackSent(target, response)
|
|
687
|
-
sent += 1
|
|
688
|
-
if (index < chunks.length - 1 && wait > 0) {
|
|
689
|
-
await delay(wait)
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
} catch (error) {
|
|
693
|
-
if (sent > 0) manager.markMessageSent(context.sessionID)
|
|
694
|
-
logger.error(`${label} 发送消息失败,已发送 ${sent}/${chunks.length} 段`, error)
|
|
695
|
-
// 已经发出部分段落时不要允许整条重试,否则会重复发送。
|
|
696
|
-
return text(`error: 消息只发送了 ${sent}/${chunks.length} 段`, { retryable: sent === 0 })
|
|
697
|
-
}
|
|
698
|
-
manager.markMessageSent(context.sessionID)
|
|
699
|
-
logger.info(`${label} 已发送 ${length} 字`, traceMeta(context.sessionID))
|
|
700
|
-
await traceSent(context.sessionID, target, { chars: length, mode: "plain", segments: sent })
|
|
701
|
-
return text("你已成功发送消息,可以选择结束对话")
|
|
702
842
|
},
|
|
703
843
|
})
|
|
704
844
|
|
|
705
845
|
editor.add({
|
|
706
846
|
name: "send_image",
|
|
707
847
|
description:
|
|
708
|
-
"向当前 QQ
|
|
848
|
+
"向当前 QQ 群聊或私聊发送图片。file 是图片来源列表(本地路径或 http(s) 地址),base64 是 base64 数据列表;两个列表合成一批图片,一条一个。本地路径:owner/admin 可取工作目录、媒体目录和配置放开的目录,普通成员只能取媒体目录;http(s) 地址只允许公网地址。",
|
|
709
849
|
input: {
|
|
710
850
|
type: "object",
|
|
711
851
|
properties: {
|
|
712
|
-
file: {
|
|
713
|
-
|
|
852
|
+
file: {
|
|
853
|
+
type: "array",
|
|
854
|
+
description: "本地文件路径或图片 URL 列表,一条一个",
|
|
855
|
+
items: { type: "string" },
|
|
856
|
+
},
|
|
857
|
+
base64: {
|
|
858
|
+
type: "array",
|
|
859
|
+
description: "base64 编码的图片数据列表,一条一个",
|
|
860
|
+
items: { type: "string" },
|
|
861
|
+
},
|
|
714
862
|
},
|
|
715
863
|
additionalProperties: false,
|
|
716
864
|
},
|
|
@@ -722,21 +870,30 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
722
870
|
const target = prepared.target
|
|
723
871
|
const denied = checkReplyPermission(context.sessionID, target)
|
|
724
872
|
if (denied) return text(denied)
|
|
725
|
-
const
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
873
|
+
const sources = [
|
|
874
|
+
...toStringList(input.file),
|
|
875
|
+
...toStringList(input.base64).map((value) =>
|
|
876
|
+
isInlineData(value) ? value : `base64://${value}`,
|
|
877
|
+
),
|
|
878
|
+
]
|
|
879
|
+
if (sources.length === 0) return text("error: 缺少 file 或 base64")
|
|
880
|
+
// 整批先校验:一条不过关就整批不发。
|
|
731
881
|
const mediaCheck = mediaFor(context.sessionID)
|
|
732
|
-
const
|
|
733
|
-
const
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
882
|
+
const payloads: unknown[][] = []
|
|
883
|
+
for (const source of sources) {
|
|
884
|
+
const tooLarge = checkInlineSize(source, "图片")
|
|
885
|
+
if (tooLarge) return text(tooLarge)
|
|
886
|
+
if (!isInlineData(source)) guardOutbound(target, source)
|
|
887
|
+
// 路径白名单在这里就判定:不过就不占发送额度。内联数据不涉及路径与网络。
|
|
888
|
+
const plan = isInlineData(source) ? undefined : await mediaCheck.check(source)
|
|
889
|
+
const media = plan ? mediaCheck.materialize(plan) : source
|
|
890
|
+
payloads.push([{ type: "image", data: { file: media } }])
|
|
891
|
+
}
|
|
892
|
+
return await sendOutgoing(context.sessionID, target, nextOperation(), {
|
|
893
|
+
messages: payloads.map((payload) => ({ kind: "segments", payload })),
|
|
894
|
+
wording: { noun: "图片", unit: "张" },
|
|
895
|
+
successText: sentImageText,
|
|
896
|
+
})
|
|
740
897
|
},
|
|
741
898
|
})
|
|
742
899
|
|
|
@@ -813,11 +970,15 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
813
970
|
editor.add({
|
|
814
971
|
name: "send_sticker",
|
|
815
972
|
description:
|
|
816
|
-
"
|
|
973
|
+
"发送收藏表情(表情包)。id 是收藏表情编号列表,一条编号发一张;先用 qq_favorite_list 查看编号与标签的对应关系。",
|
|
817
974
|
input: {
|
|
818
975
|
type: "object",
|
|
819
976
|
properties: {
|
|
820
|
-
id: {
|
|
977
|
+
id: {
|
|
978
|
+
type: "array",
|
|
979
|
+
description: "收藏表情编号列表,一条编号发一张",
|
|
980
|
+
items: { type: "number" },
|
|
981
|
+
},
|
|
821
982
|
},
|
|
822
983
|
required: ["id"],
|
|
823
984
|
additionalProperties: false,
|
|
@@ -830,46 +991,59 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
830
991
|
const target = prepared.target
|
|
831
992
|
const denied = checkReplyPermission(context.sessionID, target)
|
|
832
993
|
if (denied) return text(denied)
|
|
833
|
-
const
|
|
834
|
-
if (
|
|
994
|
+
const ids = toNumberList(input.id)
|
|
995
|
+
if (ids.length === 0) {
|
|
835
996
|
return text("error: 缺少 id(收藏表情编号)")
|
|
836
997
|
}
|
|
837
998
|
const faces = await favorites.list()
|
|
838
999
|
const rows = await favoriteIndex.sync(faces, await collection.list())
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
1000
|
+
// 整批先校验:编号有一个对不上就整批不发。
|
|
1001
|
+
const stickers: { id: number; tag: string; payload: unknown[] }[] = []
|
|
1002
|
+
for (const id of ids) {
|
|
1003
|
+
const row = rows.find((item) => item.id === id)
|
|
1004
|
+
if (!row) {
|
|
1005
|
+
return text(
|
|
1006
|
+
`error: 没有找到编号 ${id} 的收藏表情,可用编号:${formatFavoriteIds(rows)}`,
|
|
1007
|
+
)
|
|
1008
|
+
}
|
|
1009
|
+
const url = typeof row.face.url === "string" ? row.face.url : undefined
|
|
1010
|
+
if (!url) return text(`error: 编号 ${id} 的收藏表情缺少可用地址`)
|
|
1011
|
+
guardOutbound(target, url)
|
|
1012
|
+
// 收藏表情的地址同样来自外部,要走和图片一致的路径与地址白名单。
|
|
1013
|
+
let plan
|
|
1014
|
+
try {
|
|
1015
|
+
plan = await outbound.check(url)
|
|
1016
|
+
} catch (error) {
|
|
1017
|
+
return text(`error: ${error instanceof Error ? error.message : String(error)}`)
|
|
1018
|
+
}
|
|
1019
|
+
// QQ 收藏表情带 sub_type=1(KCUSTOM);本机收集的表情不在 QQ 收藏里,按普通图片发。
|
|
1020
|
+
const data: { file: string; sub_type?: number } = { file: outbound.materialize(plan) }
|
|
1021
|
+
if (!row.collected) data.sub_type = CUSTOM_FACE_SUB_TYPE
|
|
1022
|
+
stickers.push({ id, tag: row.tag || "未标注", payload: [{ type: "image", data }] })
|
|
852
1023
|
}
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
return text(`你已成功发送收藏表情 ${id}「${row.tag || "未标注"}」,可以选择结束对话`)
|
|
1024
|
+
return await sendOutgoing(context.sessionID, target, nextOperation(), {
|
|
1025
|
+
messages: stickers.map((sticker) => ({ kind: "segments", payload: sticker.payload })),
|
|
1026
|
+
wording: { noun: "收藏表情", unit: "个" },
|
|
1027
|
+
successText: (count) =>
|
|
1028
|
+
count === 1
|
|
1029
|
+
? `你已成功发送收藏表情 ${stickers[0]?.id}「${stickers[0]?.tag}」,可以选择结束对话`
|
|
1030
|
+
: `你已成功发送 ${count} 个收藏表情,可以选择结束对话`,
|
|
1031
|
+
})
|
|
862
1032
|
},
|
|
863
1033
|
})
|
|
864
1034
|
|
|
865
1035
|
editor.add({
|
|
866
1036
|
name: "send_record",
|
|
867
1037
|
description:
|
|
868
|
-
"向当前 QQ
|
|
1038
|
+
"向当前 QQ 群聊或私聊发送语音。file 是语音来源列表(本地路径、http(s) 音频地址或 base64:// 数据),一条一个。本地路径:owner/admin 可取工作目录、媒体目录和配置放开的目录,普通成员只能取媒体目录;http(s) 地址只允许公网地址。",
|
|
869
1039
|
input: {
|
|
870
1040
|
type: "object",
|
|
871
1041
|
properties: {
|
|
872
|
-
file: {
|
|
1042
|
+
file: {
|
|
1043
|
+
type: "array",
|
|
1044
|
+
description: "本地文件路径、音频 URL 或 base64:// 数据列表,一条一个",
|
|
1045
|
+
items: { type: "string" },
|
|
1046
|
+
},
|
|
873
1047
|
},
|
|
874
1048
|
additionalProperties: false,
|
|
875
1049
|
},
|
|
@@ -881,19 +1055,24 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
881
1055
|
const target = prepared.target
|
|
882
1056
|
const denied = checkReplyPermission(context.sessionID, target)
|
|
883
1057
|
if (denied) return text(denied)
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
if (!isInlineData(input.file)) guardOutbound(target, input.file)
|
|
1058
|
+
const sources = toStringList(input.file)
|
|
1059
|
+
if (sources.length === 0) return text("error: 缺少 file")
|
|
1060
|
+
// 整批先校验:一条不过关就整批不发。
|
|
888
1061
|
const mediaCheck = mediaFor(context.sessionID)
|
|
889
|
-
const
|
|
890
|
-
const
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1062
|
+
const payloads: unknown[][] = []
|
|
1063
|
+
for (const source of sources) {
|
|
1064
|
+
const tooLarge = checkInlineSize(source, "语音")
|
|
1065
|
+
if (tooLarge) return text(tooLarge)
|
|
1066
|
+
if (!isInlineData(source)) guardOutbound(target, source)
|
|
1067
|
+
const plan = isInlineData(source) ? undefined : await mediaCheck.check(source)
|
|
1068
|
+
const media = plan ? mediaCheck.materialize(plan) : source
|
|
1069
|
+
payloads.push([{ type: "record", data: { file: media } }])
|
|
1070
|
+
}
|
|
1071
|
+
return await sendOutgoing(context.sessionID, target, nextOperation(), {
|
|
1072
|
+
messages: payloads.map((payload) => ({ kind: "segments", payload })),
|
|
1073
|
+
wording: { noun: "语音", unit: "条" },
|
|
1074
|
+
successText: sentRecordText,
|
|
1075
|
+
})
|
|
897
1076
|
},
|
|
898
1077
|
})
|
|
899
1078
|
|
|
@@ -929,11 +1108,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
929
1108
|
const denied = checkReplyPermission(context.sessionID, target)
|
|
930
1109
|
if (denied) return text(denied)
|
|
931
1110
|
const identity = await botIdentity()
|
|
932
|
-
const nodes:
|
|
933
|
-
user_id: number
|
|
934
|
-
nickname: string
|
|
935
|
-
content: { type: string; data: { text: string } }[]
|
|
936
|
-
}[] = []
|
|
1111
|
+
const nodes: ForwardNode[] = []
|
|
937
1112
|
for (const node of input.nodes ?? []) {
|
|
938
1113
|
const body = toPlainText(node.content ?? "").trim()
|
|
939
1114
|
// 合并转发里每段都要扫:昵称由插件固定,模型只能控制正文。
|
|
@@ -1063,6 +1238,36 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
1063
1238
|
},
|
|
1064
1239
|
})
|
|
1065
1240
|
|
|
1241
|
+
editor.add({
|
|
1242
|
+
name: "download_file",
|
|
1243
|
+
description: [
|
|
1244
|
+
"定位聊天里收到的文件并返回本地路径;文件在收到时已经落盘,这里不会再下载一次。",
|
|
1245
|
+
"id 取文件占位文本里的「文件 id」;文本与二进制文件都能取,取到后用 read 工具读取。",
|
|
1246
|
+
"文件保留 7 天,过期或找不到会返回错误。",
|
|
1247
|
+
].join(""),
|
|
1248
|
+
input: {
|
|
1249
|
+
type: "object",
|
|
1250
|
+
properties: {
|
|
1251
|
+
id: { type: "string", description: "文件 id(占位文本里的十六进制串)" },
|
|
1252
|
+
},
|
|
1253
|
+
required: ["id"],
|
|
1254
|
+
additionalProperties: false,
|
|
1255
|
+
},
|
|
1256
|
+
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
1257
|
+
execute: async (raw, context) => {
|
|
1258
|
+
const input = (raw ?? {}) as DownloadFileInput
|
|
1259
|
+
const prepared = prepareQuery(context.sessionID)
|
|
1260
|
+
if (prepared.error) return prepared.error
|
|
1261
|
+
const id = typeof input.id === "string" ? input.id.trim().toLowerCase() : ""
|
|
1262
|
+
if (!/^[0-9a-f]{8,64}$/u.test(id)) return text("error: 文件 id 无效")
|
|
1263
|
+
const found = await media.locate(id)
|
|
1264
|
+
if (!found) return text(`error: 没有找到文件 id=${id};可能已过期(文件保留 7 天)`)
|
|
1265
|
+
return text(
|
|
1266
|
+
`文件已保存到 ${found.path}(${found.ext},${found.size} 字节),可以直接用 read 工具读取。`,
|
|
1267
|
+
)
|
|
1268
|
+
},
|
|
1269
|
+
})
|
|
1270
|
+
|
|
1066
1271
|
editor.add({
|
|
1067
1272
|
name: "get_login_info",
|
|
1068
1273
|
description: "获取机器人自身的 QQ 号与昵称。",
|
|
@@ -1205,12 +1410,20 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
1205
1410
|
editor.add({
|
|
1206
1411
|
name: "send_file",
|
|
1207
1412
|
description:
|
|
1208
|
-
"向当前 QQ
|
|
1413
|
+
"向当前 QQ 群聊或私聊发送本地文件。file 是文件路径列表,只能是机器人允许发送的目录里的普通文件;name 是对方看到的文件名列表,与 file 一一对应,省略时用原文件名。本地路径:owner/admin 可取工作目录、媒体目录和配置放开的目录,普通成员只能取媒体目录。",
|
|
1209
1414
|
input: {
|
|
1210
1415
|
type: "object",
|
|
1211
1416
|
properties: {
|
|
1212
|
-
file: {
|
|
1213
|
-
|
|
1417
|
+
file: {
|
|
1418
|
+
type: "array",
|
|
1419
|
+
description: "本地文件路径列表(必填),一条一个",
|
|
1420
|
+
items: { type: "string" },
|
|
1421
|
+
},
|
|
1422
|
+
name: {
|
|
1423
|
+
type: "array",
|
|
1424
|
+
description: "对方看到的文件名列表,与 file 一一对应;省略时用原文件名",
|
|
1425
|
+
items: { type: "string" },
|
|
1426
|
+
},
|
|
1214
1427
|
},
|
|
1215
1428
|
required: ["file"],
|
|
1216
1429
|
additionalProperties: false,
|
|
@@ -1223,25 +1436,31 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
1223
1436
|
const target = prepared.target
|
|
1224
1437
|
const denied = checkReplyPermission(context.sessionID, target)
|
|
1225
1438
|
if (denied) return text(denied)
|
|
1226
|
-
const
|
|
1227
|
-
if (
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1439
|
+
const files = toStringList(input.file)
|
|
1440
|
+
if (files.length === 0) return text("error: 缺少 file")
|
|
1441
|
+
const names = toStringSlots(input.name)
|
|
1442
|
+
const mediaCheck = mediaFor(context.sessionID)
|
|
1443
|
+
// 整批先校验:一条不过关就整批不发。
|
|
1444
|
+
const payloads: unknown[][] = []
|
|
1445
|
+
for (const [index, file] of files.entries()) {
|
|
1446
|
+
let plan
|
|
1447
|
+
try {
|
|
1448
|
+
plan = await mediaCheck.check(file)
|
|
1449
|
+
} catch (error) {
|
|
1450
|
+
return text(`error: ${error instanceof Error ? error.message : String(error)}`)
|
|
1451
|
+
}
|
|
1452
|
+
if (plan.kind !== "local") return text("error: 只能发送本地文件")
|
|
1453
|
+
const path = plan.path
|
|
1454
|
+
const name = names[index] || basename(path)
|
|
1455
|
+
// 文件内容没法扫描,路径与显示名同样过一遍出站检查。
|
|
1456
|
+
guardOutbound(target, `${name}\n${path}`)
|
|
1457
|
+
payloads.push([{ type: "file", data: { file: path, name } }])
|
|
1233
1458
|
}
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
const verdict = await permit(target, nextOperation())
|
|
1240
|
-
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
1241
|
-
const response = await send(target, [{ type: "file", data: { file: path, name } }])
|
|
1242
|
-
trackSent(target, response)
|
|
1243
|
-
manager.markMessageSent(context.sessionID)
|
|
1244
|
-
return text("你已成功发送文件,可以选择结束对话")
|
|
1459
|
+
return await sendOutgoing(context.sessionID, target, nextOperation(), {
|
|
1460
|
+
messages: payloads.map((payload) => ({ kind: "segments", payload })),
|
|
1461
|
+
wording: { noun: "文件", unit: "个" },
|
|
1462
|
+
successText: sentFileText,
|
|
1463
|
+
})
|
|
1245
1464
|
},
|
|
1246
1465
|
})
|
|
1247
1466
|
})
|