@cmach_socket/opencode-qq-bot 0.6.4 → 0.6.6
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/package.json +1 -1
- package/src/cleanup.ts +74 -0
- package/src/index.ts +635 -574
- package/src/naplink/client.ts +38 -0
- package/src/tools/qq.ts +40 -0
package/src/naplink/client.ts
CHANGED
|
@@ -29,6 +29,17 @@ function isMediaSegmentType(type: string): type is MediaSegmentType {
|
|
|
29
29
|
return (MEDIA_SEGMENT_TYPES as readonly string[]).includes(type)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/** 把 SDK 事件里的关键字段摘出来,便于日志排查。 */
|
|
33
|
+
function describeEvent(event: unknown): Record<string, unknown> {
|
|
34
|
+
if (!event || typeof event !== "object") return {}
|
|
35
|
+
const record = event as Record<string, unknown>
|
|
36
|
+
const result: Record<string, unknown> = {}
|
|
37
|
+
for (const key of ["code", "reason", "attempt", "delay", "url"]) {
|
|
38
|
+
if (record[key] !== undefined) result[key] = record[key]
|
|
39
|
+
}
|
|
40
|
+
return result
|
|
41
|
+
}
|
|
42
|
+
|
|
32
43
|
/** 取第一个可下载的 http(s) 直链,忽略本地路径与 base64。 */
|
|
33
44
|
export function pickRemoteUrl(...values: unknown[]): string | undefined {
|
|
34
45
|
for (const value of values) {
|
|
@@ -110,6 +121,19 @@ export class QQClient {
|
|
|
110
121
|
)
|
|
111
122
|
})
|
|
112
123
|
})
|
|
124
|
+
// 断线与重连要能看到:发送失败往往是因为连接在那一刻正好断了。
|
|
125
|
+
this.link.on("disconnect", (event: unknown) => {
|
|
126
|
+
this.logger.warn("NapCat 连接断开", describeEvent(event))
|
|
127
|
+
})
|
|
128
|
+
this.link.on("reconnecting", (event: unknown) => {
|
|
129
|
+
this.logger.info("NapCat 正在重连", describeEvent(event))
|
|
130
|
+
})
|
|
131
|
+
this.link.on("connection:restored", (event: unknown) => {
|
|
132
|
+
this.logger.info("NapCat 连接已恢复", describeEvent(event))
|
|
133
|
+
})
|
|
134
|
+
this.link.on("connection:lost", (event: unknown) => {
|
|
135
|
+
this.logger.error("NapCat 连接丢失", describeEvent(event))
|
|
136
|
+
})
|
|
113
137
|
}
|
|
114
138
|
|
|
115
139
|
get api(): OneBotApi {
|
|
@@ -135,6 +159,20 @@ export class QQClient {
|
|
|
135
159
|
await this.refreshBotId()
|
|
136
160
|
}
|
|
137
161
|
|
|
162
|
+
/**
|
|
163
|
+
* 发送前确保连接可用。
|
|
164
|
+
*
|
|
165
|
+
* NapLink 在正常关闭(code 1000)后不会自动重连,这时发送会报
|
|
166
|
+
* 「连接未建立,请先调用 connect()」。这里补一次连接。
|
|
167
|
+
*
|
|
168
|
+
* @returns 连接是否可用。
|
|
169
|
+
*/
|
|
170
|
+
async ensureConnected(): Promise<boolean> {
|
|
171
|
+
if (this.connected) return true
|
|
172
|
+
this.logger.info("NapCat 未连接,发送前先重连")
|
|
173
|
+
return await this.connectSafe()
|
|
174
|
+
}
|
|
175
|
+
|
|
138
176
|
/** 读取机器人自己的 QQ 号,并发调用时复用同一个请求。 */
|
|
139
177
|
private refreshBotId(): Promise<void> {
|
|
140
178
|
this.botIdRequest ??= this.loadBotId().finally(() => {
|
package/src/tools/qq.ts
CHANGED
|
@@ -60,6 +60,12 @@ export interface ToolDeps {
|
|
|
60
60
|
outbound: OutboundMedia
|
|
61
61
|
/** 调用链追踪器;不传就不记追踪。 */
|
|
62
62
|
tracer?: Tracer
|
|
63
|
+
/**
|
|
64
|
+
* 本实例是否持有机器人连接锁。
|
|
65
|
+
*
|
|
66
|
+
* 没有锁说明机器上另一个实例在消费消息,这个实例不能发送。
|
|
67
|
+
*/
|
|
68
|
+
canSend?: () => boolean
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
interface SendMessageInput {
|
|
@@ -215,6 +221,26 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
215
221
|
return target
|
|
216
222
|
}
|
|
217
223
|
|
|
224
|
+
/**
|
|
225
|
+
* 发送前的检查:没锁、没连接都不发。
|
|
226
|
+
*
|
|
227
|
+
* 没锁说明这台机器上另一个实例在消费消息,本实例的通道并没有连上 NapCat,
|
|
228
|
+
* 硬发只会得到「连接未建立」。连接断掉时先重连一次,重连不上就明确告诉模型。
|
|
229
|
+
*
|
|
230
|
+
* @returns 可以发送时返回 undefined,否则返回给模型的错误文案。
|
|
231
|
+
*/
|
|
232
|
+
const checkSendable = async (): Promise<string | undefined> => {
|
|
233
|
+
if (deps.canSend && !deps.canSend()) {
|
|
234
|
+
logger.warn("本实例没有机器人连接锁,拒绝发送")
|
|
235
|
+
return "error: 本实例没有机器人连接锁(这台机器上已有另一个实例在运行),消息未发送"
|
|
236
|
+
}
|
|
237
|
+
if (!(await client.ensureConnected())) {
|
|
238
|
+
logger.error("NapCat 未连接,放弃发送")
|
|
239
|
+
return "error: NapCat 未连接,消息未发送;请稍后重试"
|
|
240
|
+
}
|
|
241
|
+
return undefined
|
|
242
|
+
}
|
|
243
|
+
|
|
218
244
|
// 同一次工具调用拆出的多段共用一个操作 id:段与段之间不再额外等间隔。
|
|
219
245
|
let operationSeq = 0
|
|
220
246
|
const nextOperation = (): string => `send-${++operationSeq}`
|
|
@@ -396,6 +422,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
396
422
|
const input = (raw ?? {}) as SendMessageInput
|
|
397
423
|
const target = requireTarget(context.sessionID)
|
|
398
424
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
425
|
+
const blocked = await checkSendable()
|
|
426
|
+
if (blocked) return text(blocked)
|
|
399
427
|
const label = targetLabel(target)
|
|
400
428
|
|
|
401
429
|
if (input.reply === false) {
|
|
@@ -487,6 +515,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
487
515
|
const input = (raw ?? {}) as MediaInput
|
|
488
516
|
const target = requireTarget(context.sessionID)
|
|
489
517
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
518
|
+
const blocked = await checkSendable()
|
|
519
|
+
if (blocked) return text(blocked)
|
|
490
520
|
const file = input.file ?? (input.base64 ? `base64://${input.base64}` : undefined)
|
|
491
521
|
if (!file) return text("error: 缺少 file 或 base64")
|
|
492
522
|
const tooLarge = checkInlineSize(file, "图片")
|
|
@@ -538,6 +568,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
538
568
|
const input = (raw ?? {}) as StickerInput
|
|
539
569
|
const target = requireTarget(context.sessionID)
|
|
540
570
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
571
|
+
const blocked = await checkSendable()
|
|
572
|
+
if (blocked) return text(blocked)
|
|
541
573
|
const id = input.id
|
|
542
574
|
if (typeof id !== "number" || !Number.isInteger(id)) {
|
|
543
575
|
return text("error: 缺少 id(收藏表情编号)")
|
|
@@ -584,6 +616,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
584
616
|
const input = (raw ?? {}) as MediaInput
|
|
585
617
|
const target = requireTarget(context.sessionID)
|
|
586
618
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
619
|
+
const blocked = await checkSendable()
|
|
620
|
+
if (blocked) return text(blocked)
|
|
587
621
|
if (!input.file) return text("error: 缺少 file")
|
|
588
622
|
const tooLarge = checkInlineSize(input.file, "语音")
|
|
589
623
|
if (tooLarge) return text(tooLarge)
|
|
@@ -630,6 +664,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
630
664
|
const input = (raw ?? {}) as ForwardInput
|
|
631
665
|
const target = requireTarget(context.sessionID)
|
|
632
666
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
667
|
+
const blocked = await checkSendable()
|
|
668
|
+
if (blocked) return text(blocked)
|
|
633
669
|
const nodes: {
|
|
634
670
|
user_id: number
|
|
635
671
|
nickname: string
|
|
@@ -678,6 +714,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
678
714
|
const input = (raw ?? {}) as UserInput
|
|
679
715
|
const target = requireTarget(context.sessionID)
|
|
680
716
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
717
|
+
const blocked = await checkSendable()
|
|
718
|
+
if (blocked) return text(blocked)
|
|
681
719
|
if (!input.user_id) return text("error: 缺少 user_id")
|
|
682
720
|
const verdict = await permit(target, nextOperation())
|
|
683
721
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
@@ -703,6 +741,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
703
741
|
const input = (raw ?? {}) as MessageIdInput
|
|
704
742
|
const target = requireTarget(context.sessionID)
|
|
705
743
|
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
744
|
+
const blocked = await checkSendable()
|
|
745
|
+
if (blocked) return text(blocked)
|
|
706
746
|
if (input.message_id === undefined) return text("error: 缺少 message_id")
|
|
707
747
|
if (!manager.isOutgoing(target, String(input.message_id))) {
|
|
708
748
|
return text("error: 只能撤回机器人自己发送过的消息")
|