@linjz/opencode-qqbot 0.3.15 → 0.3.17

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.
Files changed (61) hide show
  1. package/README.md +303 -229
  2. package/README.zh-CN.md +313 -0
  3. package/bin/openqq.js +13 -0
  4. package/dist/notify-cli.js +92 -26
  5. package/dist/opencode-qqbot-plugin.js +1201 -67
  6. package/dist/openqq-bot.js +3963 -893
  7. package/dist/openqq-cli.js +980 -0
  8. package/dist/openqq-watchdog.js +1326 -0
  9. package/package.json +8 -5
  10. package/src/bridge.ts +1599 -150
  11. package/src/cli/notify.ts +170 -158
  12. package/src/cli/openqq-main.ts +95 -0
  13. package/src/cli/service-main.ts +302 -0
  14. package/src/cli/watchdog-main.ts +92 -0
  15. package/src/commands.ts +670 -554
  16. package/src/config.ts +122 -3
  17. package/src/core/bot-config.ts +66 -0
  18. package/src/core/bot-control.ts +166 -0
  19. package/src/core/bot-launcher.ts +110 -15
  20. package/src/core/card-approval.ts +20 -9
  21. package/src/core/cron-reminders.ts +84 -0
  22. package/src/core/delivery.ts +145 -54
  23. package/src/core/discovery.ts +207 -151
  24. package/src/core/error-rescue.ts +1 -1
  25. package/src/core/file-log.ts +50 -9
  26. package/src/core/form-bridge.ts +14 -5
  27. package/src/core/form-journal.ts +4 -4
  28. package/src/core/gateway-state.ts +3 -3
  29. package/src/core/inflight-journal.ts +215 -36
  30. package/src/core/instance-lock.ts +42 -3
  31. package/src/core/linkwatch.ts +3 -4
  32. package/src/core/log-rotate.ts +91 -0
  33. package/src/core/markdown.ts +117 -0
  34. package/src/core/message-queue.ts +242 -91
  35. package/src/core/mirror.ts +228 -182
  36. package/src/core/notices.ts +62 -0
  37. package/src/core/notify-sink.ts +93 -96
  38. package/src/core/outbound-sanitize.ts +29 -0
  39. package/src/core/outbox.ts +10 -9
  40. package/src/core/permission.ts +634 -303
  41. package/src/core/question-form.ts +230 -0
  42. package/src/core/queue-view.ts +662 -0
  43. package/src/core/send-media.ts +61 -2
  44. package/src/core/start-error.ts +26 -0
  45. package/src/core/storage.ts +34 -0
  46. package/src/core/stt.ts +55 -0
  47. package/src/core/supervisor-election.ts +53 -0
  48. package/src/core/watchdog.ts +670 -0
  49. package/src/core/zombie-reaper.ts +127 -0
  50. package/src/index.ts +262 -162
  51. package/src/opencode/client.ts +237 -27
  52. package/src/opencode/eventstream.ts +94 -12
  53. package/src/opencode/history.ts +4 -2
  54. package/src/opencode/mirror.ts +25 -5
  55. package/src/opencode/sessions.ts +135 -37
  56. package/src/plugin-entry.ts +139 -115
  57. package/src/qq/api.ts +165 -61
  58. package/src/qq/gateway.ts +114 -16
  59. package/src/qq/media.ts +21 -0
  60. package/src/qq/sender.ts +79 -18
  61. package/src/qq/types.ts +7 -0
package/src/qq/sender.ts CHANGED
@@ -7,10 +7,48 @@
7
7
  // 因此主路径直接透传 AI 的 markdown 原文,交给 QQ 渲染;仅在发送失败时回退纯文本。
8
8
  import { sendC2CMessage, sendGroupMessage, getNextMsgSeq, type MessageResponse } from "./api.js"
9
9
  import type { MessageContext } from "./types.js"
10
+ import { normalizeOutboundMarkdown, matchFence, type FenceToken } from "../core/markdown.js"
11
+ import { stripInteractionTags } from "../core/notices.js"
12
+ import { stripScaffolding } from "../core/outbound-sanitize.js"
13
+
14
+ // QQ 按 CommonMark 语义渲染:`\`+ASCII标点 被当作转义、反斜杠被吃掉
15
+ //(2026-11-20 /status 实证:C:\Users\linjz\.openqq 被渲染成 linjz.openqq——
16
+ // 路径里 `\U`、`\l` 等「反斜杠+字母」不是标点不受影响,唯独 `\.` 中招)。
17
+ // 围栏代码块与行内代码内 QQ 按字面渲染、不做转义——故仅对围栏外、行内代码外的
18
+ // 反斜杠双写(`\.` → `\\.` → QQ 渲染还原 `\.`)。
19
+ // ASCII 标点类即 CommonMark 转义目标:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
20
+ const PUNCT_BACKSLASH = /\\(?=[!-/:-@[-`{-~])/g
21
+ const INLINE_CODE_SPLIT = /(`[^`]*`)/g
22
+
23
+ /** 单行内:行内代码段(单反引号对)原样保留,其余段做反斜杠双写。 */
24
+ function escapeLineBackslashes(line: string): string {
25
+ return line
26
+ .split(INLINE_CODE_SPLIT)
27
+ .map((seg, i) => (i % 2 === 1 ? seg : seg.replace(PUNCT_BACKSLASH, "\\\\")))
28
+ .join("")
29
+ }
10
30
 
11
- // Markdown 主路径: QQ 原生渲染标准 markdown(含表格/代码),故基本透传,仅做首尾清理。
31
+ // Markdown 主路径: QQ 原生渲染标准 markdown(含表格/代码),故基本透传,仅做首尾清理
32
+ // 与围栏归一(补未闭合 + 去空围栏对)——两者都会在 QQ 里变成空代码卡(2026-11-10 实证)。
12
33
  export function formatForQQMarkdown(text: string): string {
13
- return text.trim()
34
+ // Z 项(2026-11-23):先消毒脚手架块(thinking/system-reminder 等),再走归一——
35
+ // 顺序不可换:围栏保护在归一步,消毒须在围栏占位之前执行才刮不到代码块内。
36
+ const sanitized = stripScaffolding(text)
37
+ const normalized = normalizeOutboundMarkdown(sanitized.trim())
38
+ // 围栏状态跟踪(normalizeOutboundMarkdown 已保证围栏配对,可安全切换):
39
+ // 围栏行与其内部内容逐字保留,不做反斜杠双写。
40
+ let inFence = false
41
+ return normalized
42
+ .split("\n")
43
+ .map((line) => {
44
+ const f = matchFence(line)
45
+ if (f) {
46
+ inFence = !inFence
47
+ return line
48
+ }
49
+ return inFence ? line : escapeLineBackslashes(line)
50
+ })
51
+ .join("\n")
14
52
  }
15
53
 
16
54
  // 纯文本兜底: 去除所有 markdown 标记(当 markdown 发送失败时使用)
@@ -44,6 +82,19 @@ export function formatForQQ(text: string): string {
44
82
  processed = processed.replace(`\x00CB${i}\x00`, codeBlocks[i])
45
83
  }
46
84
 
85
+ // 交互标签兜底剥离(2026-11-22 学习吸收轮):<qqbot-cmd-input> 等仅 markdown 消息生效,
86
+ // 纯文本通道必须把标签降级为可读文本,否则尖括号原样糊给用户(AstrBot#8755 同款坑)。
87
+ // 围栏内是代码字面量,不动。
88
+ let inStripFence = false
89
+ processed = processed.split("\n").map((line) => {
90
+ const f = matchFence(line)
91
+ if (f) {
92
+ inStripFence = !inStripFence
93
+ return line
94
+ }
95
+ return inStripFence ? line : stripInteractionTags(line)
96
+ }).join("\n")
97
+
47
98
  return processed.trim()
48
99
  }
49
100
 
@@ -94,20 +145,20 @@ function utf8Bytes(s: string): number {
94
145
  return encoder.encode(s).length
95
146
  }
96
147
 
97
- // —— 结构化分条(2026-10-29 固化:设计吸收自 dsh-qqbot transport/chunker.ts,P0)——
148
+ // —— 结构化分条(2026-10-29 固化:设计吸收自 dsh-qqbot 分条器 chunker,P0)——
98
149
  // dsh 语义:GFM 表格聚成原子单元不跨片拆散 + 按行边界打包;
99
150
  // 我方在 dsh 之上补两件事:① 字节预算(QQ 按 UTF-8 字节限长,dsh 按字符数会
100
- // 踩中文 3 字节坑)② 围栏修补——在代码块中间切断时给前片补闭合 ```、后片补
101
- // 开围栏(带语言),保证每片 markdown 独立可渲染。
151
+ // 踩中文 3 字节坑)② 围栏修补——在代码块中间切断时给前片补闭合、后片补开围栏
152
+ // (带语言),保证每片 markdown 独立可渲染。围栏识别与出站兜底共用 core/markdown
153
+ // (2026-11-10:补 ~~~ 与 4+ 标记长度,避免波浪围栏被当正文切碎)。
102
154
  const GFM_TABLE_LINE = /^\s*\|.*\|\s*$/
103
- const CODE_FENCE_OPEN = /^\s*```(\S*)/
104
155
 
105
156
  interface LineUnit {
106
157
  /** 行原文(含结尾 \n,全文末行除外) */
107
158
  text: string
108
159
  bytes: number
109
- /** 本行是围栏行时记录语言,否则 null */
110
- fenceLang: string | null
160
+ /** 本行是围栏行时记录标记,否则 null */
161
+ fence: FenceToken | null
111
162
  }
112
163
 
113
164
  function toLineUnits(text: string): LineUnit[] {
@@ -117,7 +168,7 @@ function toLineUnits(text: string): LineUnit[] {
117
168
  const flushTable = (isLast: boolean): void => {
118
169
  if (tableBuf.length === 0) return
119
170
  const body = tableBuf.join("\n") + (isLast ? "" : "\n")
120
- units.push({ text: body, bytes: utf8Bytes(body), fenceLang: null })
171
+ units.push({ text: body, bytes: utf8Bytes(body), fence: null })
121
172
  tableBuf = []
122
173
  }
123
174
 
@@ -129,9 +180,8 @@ function toLineUnits(text: string): LineUnit[] {
129
180
  return
130
181
  }
131
182
  flushTable(isLast)
132
- const fence = CODE_FENCE_OPEN.exec(line)
133
183
  const body = line + (isLast ? "" : "\n")
134
- units.push({ text: body, bytes: utf8Bytes(body), fenceLang: fence ? fence[1] : null })
184
+ units.push({ text: body, bytes: utf8Bytes(body), fence: matchFence(line) })
135
185
  })
136
186
  return units
137
187
  }
@@ -187,18 +237,27 @@ export function splitMessage(text: string, maxBytes: number = MARKDOWN_MAX_BYTES
187
237
  let cur = ""
188
238
  let curBytes = 0
189
239
  let prefix = "" // 围栏内切断时,下一片需补的开围栏(带语言)
190
- let openLang: string | null = null // 原文全局围栏状态(跨片跟踪)
240
+ let open: FenceToken | null = null // 原文全局围栏状态(跨片跟踪)
191
241
 
192
242
  const flush = (): void => {
193
243
  const body = cur
194
244
  cur = ""
195
245
  curBytes = 0
196
- const inside = openLang !== null
197
- const out = prefix + body + (inside ? (body.endsWith("\n") ? "```\n" : "\n```\n") : "")
198
- prefix = inside ? `\`\`\`${openLang}\n` : ""
246
+ const f = open
247
+ const inside = f !== null
248
+ const closer = f ? f.ch.repeat(f.len) : ""
249
+ const out = prefix + body + (inside ? (body.endsWith("\n") ? `${closer}\n` : `\n${closer}\n`) : "")
250
+ prefix = f ? `${f.ch.repeat(f.len)}${f.info}\n` : ""
199
251
  if (out.trim()) chunks.push(out)
200
252
  }
201
253
 
254
+ const closesOpen = (u: LineUnit): boolean =>
255
+ open !== null &&
256
+ u.fence !== null &&
257
+ u.fence.ch === open.ch &&
258
+ u.fence.len >= open.len &&
259
+ u.fence.info === ""
260
+
202
261
  for (const u of units) {
203
262
  if (u.bytes > maxBytes) {
204
263
  // 超大单元:先封当前片,再字节级兜底切(围栏行本身不可能超大,不参与围栏语义)
@@ -210,13 +269,15 @@ export function splitMessage(text: string, maxBytes: number = MARKDOWN_MAX_BYTES
210
269
  continue
211
270
  }
212
271
  const prefixBytes = prefix ? utf8Bytes(prefix) : 0
213
- if (cur && curBytes + prefixBytes + u.bytes > maxBytes) {
272
+ // 闭栏行优先并入当前片:若在它之前切断,下一片会补出「开栏紧跟闭栏」的空代码块。
273
+ if (cur && !closesOpen(u) && curBytes + prefixBytes + u.bytes > maxBytes) {
214
274
  flush()
215
275
  }
216
276
  cur += u.text
217
277
  curBytes += u.bytes
218
- if (u.fenceLang !== null) {
219
- openLang = openLang === null ? u.fenceLang : null
278
+ if (u.fence) {
279
+ if (!open) open = u.fence
280
+ else if (u.fence.ch === open.ch && u.fence.len >= open.len && u.fence.info === "") open = null
220
281
  }
221
282
  }
222
283
  flush()
package/src/qq/types.ts CHANGED
@@ -149,6 +149,13 @@ export interface MessageContext {
149
149
  attachments?: MessageAttachment[]
150
150
  /** 排队时预下载的附件(Q1:避免 drain 时 QQ 签名 URL 过期——入队即落盘) */
151
151
  preparedAttachments?: import("./media.js").PreparedAttachments
152
+ /**
153
+ * 锚定会话(owner 2026-11-14 会话隔离裁定):入队/派发时快照的目标会话。
154
+ * 出站推送门据此校验「锚定=当前绑定」,绑定切走即不推(design §3.4 出站推送门)。
155
+ */
156
+ anchorSessionId?: string
157
+ /** 纯表情消息标记(DEF-qqbot-20261111-P2,owner 裁定=有据提示):原始内容纯表情被规范化剥空 */
158
+ emojiOnly?: boolean
152
159
  timestamp?: string
153
160
  rawEvent?: C2CMessageEvent | GroupMessageEvent
154
161
  }