@cmach_socket/opencode-qq-bot 0.6.6 → 0.6.7
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 +12 -8
- package/package.json +1 -1
- package/src/access.ts +0 -5
- package/src/async.ts +25 -0
- package/src/commands.ts +8 -13
- package/src/config.ts +36 -83
- package/src/emoji/command.ts +0 -2
- package/src/emoji/label.ts +10 -22
- package/src/events/notice.ts +0 -19
- package/src/home.ts +8 -6
- package/src/index.ts +92 -81
- package/src/keepalive.ts +10 -24
- package/src/logger.ts +2 -1
- package/src/media-path.ts +2 -4
- package/src/model-view.ts +2 -2
- package/src/naplink/media.ts +4 -8
- package/src/naplink/render.ts +41 -53
- package/src/overrides.ts +6 -17
- package/src/rate-limit.ts +2 -1
- package/src/safe-fetch.ts +1 -5
- package/src/sensitive.ts +3 -5
- package/src/session/find.ts +45 -0
- package/src/session/manager.ts +70 -93
- package/src/session/proactive.ts +8 -11
- package/src/session/soul.ts +54 -31
- package/src/text.ts +7 -0
- package/src/tools/policy.ts +6 -0
- package/src/tools/qq.ts +85 -67
- package/src/tui-commands.ts +1 -1
package/src/tools/qq.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Plugin } from "@opencode/plugin"
|
|
2
2
|
import { isTargetAllowed } from "../access"
|
|
3
|
+
import { delay } from "../async"
|
|
3
4
|
import { type ChatTarget, targetKey, targetLabel } from "../chat"
|
|
4
5
|
import type { QQBotConfig } from "../config"
|
|
5
6
|
import {
|
|
@@ -101,9 +102,6 @@ interface MessageIdInput {
|
|
|
101
102
|
message_id?: number | string
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
/** QQ 单条消息的保守长度上限,超过时按段发送。 */
|
|
105
|
-
export { MAX_MESSAGE_CHARS } from "../outbound"
|
|
106
|
-
|
|
107
105
|
/** 合并转发里单个节点的字数上限,留出余量避免被 NapCat 截断。 */
|
|
108
106
|
const FORWARD_NODE_CHARS = 3000
|
|
109
107
|
|
|
@@ -113,8 +111,14 @@ function text(value: string, metadata?: Record<string, unknown>) {
|
|
|
113
111
|
return { content: value, ...(Object.keys(marks).length > 0 ? { metadata: marks } : {}) }
|
|
114
112
|
}
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
/** 工具返回值:content 是给模型看的文本,metadata 带失败标记。 */
|
|
115
|
+
type ToolResult = ReturnType<typeof text>
|
|
116
|
+
|
|
117
|
+
/** 按目标类型把消息投递出去:群聊与私聊走不同的 API。 */
|
|
118
|
+
function sendPayload(client: QQClient, target: ChatTarget, payload: string | unknown[]) {
|
|
119
|
+
return target.kind === "group"
|
|
120
|
+
? client.api.sendGroupMessage(target.id, payload)
|
|
121
|
+
: client.api.sendPrivateMessage(target.id, payload)
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
/** 从 OneBot 发送结果里取出 message_id。 */
|
|
@@ -147,11 +151,7 @@ export async function sendPlainMessage(
|
|
|
147
151
|
let sent = 0
|
|
148
152
|
for (const [index, chunk] of chunks.entries()) {
|
|
149
153
|
await options.limiter.pace(pluginSendScope(target), operation)
|
|
150
|
-
|
|
151
|
-
await client.api.sendGroupMessage(target.id, chunk)
|
|
152
|
-
} else {
|
|
153
|
-
await client.api.sendPrivateMessage(target.id, chunk)
|
|
154
|
-
}
|
|
154
|
+
await sendPayload(client, target, chunk)
|
|
155
155
|
sent += 1
|
|
156
156
|
if (index < chunks.length - 1 && (options.delayMs ?? 0) > 0) {
|
|
157
157
|
await delay(options.delayMs ?? 0)
|
|
@@ -266,6 +266,42 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
266
266
|
await tracer?.record("response_sent", traceId, attributes, targetKey(target))
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
+
/**
|
|
270
|
+
* 发送类工具的统一前置:解析目标,并确认本实例可以发送。
|
|
271
|
+
*
|
|
272
|
+
* 返回 `error` 时调用方直接把它交给模型;否则 `target` 一定存在。
|
|
273
|
+
*/
|
|
274
|
+
const prepareSend = async (
|
|
275
|
+
sessionID: string,
|
|
276
|
+
): Promise<
|
|
277
|
+
{ target: ChatTarget; error?: undefined } | { target?: undefined; error: ToolResult }
|
|
278
|
+
> => {
|
|
279
|
+
const target = requireTarget(sessionID)
|
|
280
|
+
if (!target) return { error: text("error: 当前会话没有绑定 QQ 群聊或私聊") }
|
|
281
|
+
const blocked = await checkSendable()
|
|
282
|
+
if (blocked) return { error: text(blocked) }
|
|
283
|
+
return { target }
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 只读类工具的统一前置:只解析目标,不要求连接可用。
|
|
288
|
+
*
|
|
289
|
+
* 查资料不需要发消息,连接断着也能回答。
|
|
290
|
+
*/
|
|
291
|
+
const prepareQuery = (
|
|
292
|
+
sessionID: string,
|
|
293
|
+
): { target: ChatTarget; error?: undefined } | { target?: undefined; error: ToolResult } => {
|
|
294
|
+
const target = requireTarget(sessionID)
|
|
295
|
+
if (!target) return { error: text("error: 当前会话没有绑定 QQ 群聊或私聊") }
|
|
296
|
+
return { target }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 记下机器人自己发出的消息 ID,供撤回与重复检测使用。 */
|
|
300
|
+
const trackSent = (target: ChatTarget, response: unknown): void => {
|
|
301
|
+
const messageId = messageIdOf(response)
|
|
302
|
+
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
303
|
+
}
|
|
304
|
+
|
|
269
305
|
/**
|
|
270
306
|
* 申请一次出站发送许可。
|
|
271
307
|
*
|
|
@@ -320,15 +356,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
320
356
|
return `error: ${kind}数据过大,超过 ${Math.floor(MAX_INLINE_BYTES / 1024 / 1024)}MB 上限`
|
|
321
357
|
}
|
|
322
358
|
|
|
323
|
-
const
|
|
324
|
-
target
|
|
325
|
-
? client.api.sendGroupMessage(target.id, chunk)
|
|
326
|
-
: client.api.sendPrivateMessage(target.id, chunk)
|
|
327
|
-
|
|
328
|
-
const sendSegments = (target: ChatTarget, segments: unknown[]) =>
|
|
329
|
-
target.kind === "group"
|
|
330
|
-
? client.api.sendGroupMessage(target.id, segments)
|
|
331
|
-
: client.api.sendPrivateMessage(target.id, segments)
|
|
359
|
+
const send = (target: ChatTarget, payload: string | unknown[]) =>
|
|
360
|
+
sendPayload(client, target, payload)
|
|
332
361
|
|
|
333
362
|
const sendForward = (target: ChatTarget, nodes: unknown[]) =>
|
|
334
363
|
target.kind === "group"
|
|
@@ -377,8 +406,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
377
406
|
const verdict = await permit(target, operation)
|
|
378
407
|
if (!verdict.allowed) return refuse(sessionID, verdict, 0, nodes.length)
|
|
379
408
|
const response = await sendForward(target, nodes)
|
|
380
|
-
|
|
381
|
-
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
409
|
+
trackSent(target, response)
|
|
382
410
|
manager.markMessageSent(sessionID)
|
|
383
411
|
logger.info(
|
|
384
412
|
`${label} 已发送 ${length} 字(合并转发 ${nodes.length} 个节点)`,
|
|
@@ -420,10 +448,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
420
448
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
421
449
|
execute: async (raw, context) => {
|
|
422
450
|
const input = (raw ?? {}) as SendMessageInput
|
|
423
|
-
const
|
|
424
|
-
if (
|
|
425
|
-
const
|
|
426
|
-
if (blocked) return text(blocked)
|
|
451
|
+
const prepared = await prepareSend(context.sessionID)
|
|
452
|
+
if (prepared.error) return prepared.error
|
|
453
|
+
const target = prepared.target
|
|
427
454
|
const label = targetLabel(target)
|
|
428
455
|
|
|
429
456
|
if (input.reply === false) {
|
|
@@ -477,9 +504,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
477
504
|
for (const [index, chunk] of chunks.entries()) {
|
|
478
505
|
const verdict = await permit(target, operation)
|
|
479
506
|
if (!verdict.allowed) return refuse(context.sessionID, verdict, sent, chunks.length)
|
|
480
|
-
const response = await
|
|
481
|
-
|
|
482
|
-
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
507
|
+
const response = await send(target, chunk)
|
|
508
|
+
trackSent(target, response)
|
|
483
509
|
sent += 1
|
|
484
510
|
if (index < chunks.length - 1 && wait > 0) {
|
|
485
511
|
await delay(wait)
|
|
@@ -513,10 +539,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
513
539
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
514
540
|
execute: async (raw, context) => {
|
|
515
541
|
const input = (raw ?? {}) as MediaInput
|
|
516
|
-
const
|
|
517
|
-
if (
|
|
518
|
-
const
|
|
519
|
-
if (blocked) return text(blocked)
|
|
542
|
+
const prepared = await prepareSend(context.sessionID)
|
|
543
|
+
if (prepared.error) return prepared.error
|
|
544
|
+
const target = prepared.target
|
|
520
545
|
const file = input.file ?? (input.base64 ? `base64://${input.base64}` : undefined)
|
|
521
546
|
if (!file) return text("error: 缺少 file 或 base64")
|
|
522
547
|
const tooLarge = checkInlineSize(file, "图片")
|
|
@@ -527,9 +552,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
527
552
|
const verdict = await permit(target, nextOperation())
|
|
528
553
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
529
554
|
const media = plan ? outbound.materialize(plan) : file
|
|
530
|
-
const response = await
|
|
531
|
-
|
|
532
|
-
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
555
|
+
const response = await send(target, [{ type: "image", data: { file: media } }])
|
|
556
|
+
trackSent(target, response)
|
|
533
557
|
manager.markMessageSent(context.sessionID)
|
|
534
558
|
return text("你已成功发送图片,可以选择结束对话")
|
|
535
559
|
},
|
|
@@ -566,10 +590,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
566
590
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
567
591
|
execute: async (raw, context) => {
|
|
568
592
|
const input = (raw ?? {}) as StickerInput
|
|
569
|
-
const
|
|
570
|
-
if (
|
|
571
|
-
const
|
|
572
|
-
if (blocked) return text(blocked)
|
|
593
|
+
const prepared = await prepareSend(context.sessionID)
|
|
594
|
+
if (prepared.error) return prepared.error
|
|
595
|
+
const target = prepared.target
|
|
573
596
|
const id = input.id
|
|
574
597
|
if (typeof id !== "number" || !Number.isInteger(id)) {
|
|
575
598
|
return text("error: 缺少 id(收藏表情编号)")
|
|
@@ -587,14 +610,13 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
587
610
|
const plan = await outbound.check(url)
|
|
588
611
|
const verdict = await permit(target, nextOperation())
|
|
589
612
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
590
|
-
const response = await
|
|
613
|
+
const response = await send(target, [
|
|
591
614
|
{
|
|
592
615
|
type: "image",
|
|
593
616
|
data: { file: outbound.materialize(plan), sub_type: CUSTOM_FACE_SUB_TYPE },
|
|
594
617
|
},
|
|
595
618
|
])
|
|
596
|
-
|
|
597
|
-
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
619
|
+
trackSent(target, response)
|
|
598
620
|
manager.markMessageSent(context.sessionID)
|
|
599
621
|
return text(`你已成功发送收藏表情 ${id}「${row.tag || "未标注"}」,可以选择结束对话`)
|
|
600
622
|
},
|
|
@@ -614,10 +636,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
614
636
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
615
637
|
execute: async (raw, context) => {
|
|
616
638
|
const input = (raw ?? {}) as MediaInput
|
|
617
|
-
const
|
|
618
|
-
if (
|
|
619
|
-
const
|
|
620
|
-
if (blocked) return text(blocked)
|
|
639
|
+
const prepared = await prepareSend(context.sessionID)
|
|
640
|
+
if (prepared.error) return prepared.error
|
|
641
|
+
const target = prepared.target
|
|
621
642
|
if (!input.file) return text("error: 缺少 file")
|
|
622
643
|
const tooLarge = checkInlineSize(input.file, "语音")
|
|
623
644
|
if (tooLarge) return text(tooLarge)
|
|
@@ -626,9 +647,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
626
647
|
const verdict = await permit(target, nextOperation())
|
|
627
648
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
628
649
|
const media = plan ? outbound.materialize(plan) : input.file
|
|
629
|
-
const response = await
|
|
630
|
-
|
|
631
|
-
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
650
|
+
const response = await send(target, [{ type: "record", data: { file: media } }])
|
|
651
|
+
trackSent(target, response)
|
|
632
652
|
manager.markMessageSent(context.sessionID)
|
|
633
653
|
return text("你已成功发送语音,可以选择结束对话")
|
|
634
654
|
},
|
|
@@ -662,10 +682,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
662
682
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
663
683
|
execute: async (raw, context) => {
|
|
664
684
|
const input = (raw ?? {}) as ForwardInput
|
|
665
|
-
const
|
|
666
|
-
if (
|
|
667
|
-
const
|
|
668
|
-
if (blocked) return text(blocked)
|
|
685
|
+
const prepared = await prepareSend(context.sessionID)
|
|
686
|
+
if (prepared.error) return prepared.error
|
|
687
|
+
const target = prepared.target
|
|
669
688
|
const nodes: {
|
|
670
689
|
user_id: number
|
|
671
690
|
nickname: string
|
|
@@ -690,8 +709,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
690
709
|
const verdict = await permit(target, nextOperation())
|
|
691
710
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
692
711
|
const response = await sendForward(target, nodes)
|
|
693
|
-
|
|
694
|
-
if (messageId) manager.rememberOutgoing(target, messageId)
|
|
712
|
+
trackSent(target, response)
|
|
695
713
|
manager.markMessageSent(context.sessionID)
|
|
696
714
|
await traceSent(context.sessionID, target, { mode: "forward", nodes: nodes.length })
|
|
697
715
|
return text("你已成功发送合并转发消息,可以选择结束对话")
|
|
@@ -712,10 +730,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
712
730
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
713
731
|
execute: async (raw, context) => {
|
|
714
732
|
const input = (raw ?? {}) as UserInput
|
|
715
|
-
const
|
|
716
|
-
if (
|
|
717
|
-
const
|
|
718
|
-
if (blocked) return text(blocked)
|
|
733
|
+
const prepared = await prepareSend(context.sessionID)
|
|
734
|
+
if (prepared.error) return prepared.error
|
|
735
|
+
const target = prepared.target
|
|
719
736
|
if (!input.user_id) return text("error: 缺少 user_id")
|
|
720
737
|
const verdict = await permit(target, nextOperation())
|
|
721
738
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
@@ -739,10 +756,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
739
756
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
740
757
|
execute: async (raw, context) => {
|
|
741
758
|
const input = (raw ?? {}) as MessageIdInput
|
|
742
|
-
const
|
|
743
|
-
if (
|
|
744
|
-
const
|
|
745
|
-
if (blocked) return text(blocked)
|
|
759
|
+
const prepared = await prepareSend(context.sessionID)
|
|
760
|
+
if (prepared.error) return prepared.error
|
|
761
|
+
const target = prepared.target
|
|
746
762
|
if (input.message_id === undefined) return text("error: 缺少 message_id")
|
|
747
763
|
if (!manager.isOutgoing(target, String(input.message_id))) {
|
|
748
764
|
return text("error: 只能撤回机器人自己发送过的消息")
|
|
@@ -766,8 +782,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
766
782
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
767
783
|
execute: async (raw, context) => {
|
|
768
784
|
const input = (raw ?? {}) as UserInput
|
|
769
|
-
const
|
|
770
|
-
if (
|
|
785
|
+
const prepared = prepareQuery(context.sessionID)
|
|
786
|
+
if (prepared.error) return prepared.error
|
|
787
|
+
const target = prepared.target
|
|
771
788
|
if (!input.user_id) return text("error: 缺少 user_id")
|
|
772
789
|
const info =
|
|
773
790
|
target.kind === "group"
|
|
@@ -791,8 +808,9 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
791
808
|
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
792
809
|
execute: async (raw, context) => {
|
|
793
810
|
const input = (raw ?? {}) as MessageIdInput
|
|
794
|
-
const
|
|
795
|
-
if (
|
|
811
|
+
const prepared = prepareQuery(context.sessionID)
|
|
812
|
+
if (prepared.error) return prepared.error
|
|
813
|
+
const target = prepared.target
|
|
796
814
|
if (input.message_id === undefined) return text("error: 缺少 message_id")
|
|
797
815
|
if (!manager.isKnownMessage(target, String(input.message_id))) {
|
|
798
816
|
return text("error: 只能转写当前会话最近收到的消息")
|
package/src/tui-commands.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { SessionManager } from "./session/manager"
|
|
|
9
9
|
import type { ToolPolicy } from "./tools/policy"
|
|
10
10
|
import { createPlainSender } from "./tools/qq"
|
|
11
11
|
|
|
12
|
-
export {
|
|
12
|
+
export { tuiCommandName } from "./commands"
|
|
13
13
|
|
|
14
14
|
export interface TuiCommandDeps {
|
|
15
15
|
ctx: Plugin.Context
|