@cmach_socket/opencode-qq-bot 0.11.0 → 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/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
  *
@@ -158,6 +162,12 @@ 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
172
  file?: string[]
163
173
  name?: string[]
@@ -405,6 +415,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
405
415
  sensitive,
406
416
  outbound,
407
417
  outboundMember,
418
+ media,
408
419
  diary,
409
420
  tracer,
410
421
  } = deps
@@ -1227,6 +1238,36 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
1227
1238
  },
1228
1239
  })
1229
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
+
1230
1271
  editor.add({
1231
1272
  name: "get_login_info",
1232
1273
  description: "获取机器人自身的 QQ 号与昵称。",
@@ -6,6 +6,9 @@ import type { SessionManager, Turn } from "../session/manager"
6
6
  export const DUPLICATE_SEND_REFUSAL =
7
7
  "禁止连续调用发送工具:上一步已经发送过消息,再发可能重复。请先调用其它工具(例如查看消息或整理内容),或直接结束本轮。"
8
8
 
9
+ /** `/stop` 之后仍要发送时回给模型的说明。 */
10
+ export const STOPPED_SEND_REFUSAL = "这一轮已被用户停止,不要再发送消息。等用户发来新消息再继续。"
11
+
9
12
  /** 匹配 qq_send_message 与 qq_delete_message 等出站动作;钩子里可能带或不带命名空间。 */
10
13
  const SEND_TOOL = /^(?:qq_)?(?:send[_-]|delete_message$)/
11
14
 
@@ -35,7 +38,14 @@ export async function registerSendGuard(
35
38
 
36
39
  const before = await ctx.tool.hook("execute.before", async (event) => {
37
40
  const active = manager.activeTurn(event.sessionID)
38
- if (!active) return
41
+ if (!active) {
42
+ // `/stop` 之后上游那一轮可能还在跑:这时不让它再往聊天里发消息。
43
+ if (SEND_TOOL.test(event.tool) && manager.isStopped(event.sessionID)) {
44
+ logger.warn(`会话已被停止,拒绝调用 ${event.tool}`)
45
+ throw new Error(STOPPED_SEND_REFUSAL)
46
+ }
47
+ return
48
+ }
39
49
  const turn = active.turn
40
50
  const previous = last.get(event.sessionID)
41
51
  const send = SEND_TOOL.test(event.tool)