@linjz/opencode-qqbot 0.3.15 → 0.3.16

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 (50) hide show
  1. package/README.md +42 -5
  2. package/bin/openqq.js +13 -0
  3. package/dist/notify-cli.js +28 -14
  4. package/dist/opencode-qqbot-plugin.js +934 -64
  5. package/dist/openqq-bot.js +3095 -677
  6. package/dist/openqq-cli.js +791 -0
  7. package/dist/openqq-watchdog.js +1063 -0
  8. package/package.json +8 -5
  9. package/src/bridge.ts +1310 -150
  10. package/src/cli/notify.ts +167 -158
  11. package/src/cli/openqq-main.ts +95 -0
  12. package/src/cli/service-main.ts +302 -0
  13. package/src/cli/watchdog-main.ts +77 -0
  14. package/src/commands.ts +644 -554
  15. package/src/config.ts +85 -0
  16. package/src/core/bot-control.ts +148 -0
  17. package/src/core/bot-launcher.ts +106 -13
  18. package/src/core/card-approval.ts +20 -9
  19. package/src/core/delivery.ts +145 -54
  20. package/src/core/discovery.ts +22 -4
  21. package/src/core/file-log.ts +35 -3
  22. package/src/core/form-bridge.ts +14 -5
  23. package/src/core/form-journal.ts +4 -4
  24. package/src/core/gateway-state.ts +3 -3
  25. package/src/core/inflight-journal.ts +214 -36
  26. package/src/core/instance-lock.ts +10 -2
  27. package/src/core/linkwatch.ts +3 -4
  28. package/src/core/markdown.ts +117 -0
  29. package/src/core/message-queue.ts +242 -91
  30. package/src/core/mirror.ts +222 -182
  31. package/src/core/notify-sink.ts +93 -96
  32. package/src/core/outbox.ts +10 -9
  33. package/src/core/permission.ts +627 -303
  34. package/src/core/question-form.ts +230 -0
  35. package/src/core/queue-view.ts +661 -0
  36. package/src/core/send-media.ts +32 -0
  37. package/src/core/storage.ts +34 -0
  38. package/src/core/watchdog.ts +513 -0
  39. package/src/core/zombie-reaper.ts +127 -0
  40. package/src/index.ts +140 -52
  41. package/src/opencode/client.ts +206 -27
  42. package/src/opencode/eventstream.ts +94 -12
  43. package/src/opencode/history.ts +4 -2
  44. package/src/opencode/mirror.ts +15 -2
  45. package/src/opencode/sessions.ts +135 -37
  46. package/src/plugin-entry.ts +120 -115
  47. package/src/qq/api.ts +165 -61
  48. package/src/qq/gateway.ts +79 -16
  49. package/src/qq/sender.ts +30 -18
  50. package/src/qq/types.ts +7 -0
@@ -0,0 +1,230 @@
1
+ // @input: requestSchema(elicitation.create 的 requestedSchema)
2
+ // @output: QuestionItem, parseQuestionForm, renderQuestion, parseAnswer, toFormContent,
3
+ // timeoutOutcome, TimeoutOutcome
4
+ // @pos: core层 - 表单/提问的纯逻辑:把 elicitation 的 JSON-Schema 归一成「逐题问答」,
5
+ // 并把用户文字回复映射回表单答案。不做 IO,供 bridge 与单测共用。
6
+ // 背景(2026-11-08):question 工具的字段是 options + custom,
7
+ // elicitation 落地时会拆成 q0 与 q0_custom 两个属性,必须合并成一道题。
8
+
9
+ export interface QuestionOption {
10
+ value: string
11
+ label: string
12
+ description?: string
13
+ }
14
+
15
+ export interface QuestionItem {
16
+ /** 主属性名,如 q0(写回答案时用) */
17
+ key: string
18
+ title: string
19
+ description?: string
20
+ options: QuestionOption[]
21
+ /** 多选(schema type=array,或 multiselect) */
22
+ multiple: boolean
23
+ /** 自定义答案属性名,如 q0_custom;无则为 undefined */
24
+ customKey?: string
25
+ /** 该题是否必填(schema.required 含此 key)——决定超时是「跳过」还是「中止」 */
26
+ required: boolean
27
+ /** 数字/布尔等自由输入类型 */
28
+ kind: "string" | "number" | "integer" | "boolean"
29
+ }
30
+
31
+ type JsonSchema = {
32
+ type?: string
33
+ title?: string
34
+ description?: string
35
+ enum?: unknown[]
36
+ enumNames?: unknown[]
37
+ oneOf?: Array<{ const?: unknown; title?: unknown; description?: unknown }>
38
+ anyOf?: Array<{ const?: unknown; title?: unknown; description?: unknown }>
39
+ items?: JsonSchema
40
+ }
41
+
42
+ function asArray(value: unknown): unknown[] {
43
+ return Array.isArray(value) ? value : []
44
+ }
45
+
46
+ function optionsOf(prop: JsonSchema): QuestionOption[] {
47
+ const branches = asArray(prop.oneOf).length ? asArray(prop.oneOf) : asArray(prop.anyOf)
48
+ if (branches.length) {
49
+ return branches.map((b) => {
50
+ const item = (b ?? {}) as { const?: unknown; title?: unknown; description?: unknown }
51
+ const value = item.const
52
+ return {
53
+ value: value === undefined ? String(item.title ?? "") : String(value),
54
+ label: String(item.title ?? value ?? ""),
55
+ ...(typeof item.description === "string" ? { description: item.description } : {}),
56
+ }
57
+ })
58
+ }
59
+ if (asArray(prop.enum).length) {
60
+ const names = asArray(prop.enumNames)
61
+ return asArray(prop.enum).map((v, i) => ({
62
+ value: String(v),
63
+ label: String(names[i] ?? v),
64
+ }))
65
+ }
66
+ return []
67
+ }
68
+
69
+ /** 把 elicitation 的 requestedSchema 归一成题目列表;无可用属性时返回 null。 */
70
+ export function parseQuestionForm(requestedSchema: unknown): QuestionItem[] | null {
71
+ const schema = (requestedSchema ?? {}) as { properties?: Record<string, JsonSchema> }
72
+ const props = Object.entries(schema.properties ?? {})
73
+ if (!props.length) return null
74
+
75
+ const customKeys = new Map<string, string>()
76
+ for (const [key] of props) {
77
+ if (key.endsWith("_custom")) customKeys.set(key.slice(0, -"_custom".length), key)
78
+ }
79
+ const requiredKeys = new Set(
80
+ asArray((requestedSchema as { required?: unknown[] } | null | undefined)?.required).map((k) => String(k)),
81
+ )
82
+
83
+ const items: QuestionItem[] = []
84
+ for (const [key, raw] of props) {
85
+ if (key.endsWith("_custom")) continue // 作为自定义输入随主题合并
86
+ const prop = (raw ?? {}) as JsonSchema
87
+ const type = prop.type
88
+ const multiple = type === "array"
89
+ const options = optionsOf(multiple && prop.items ? prop.items : prop)
90
+ const kind: QuestionItem["kind"] =
91
+ type === "number" || type === "integer" || type === "boolean" ? type : "string"
92
+ const customKey = customKeys.get(key)
93
+ items.push({
94
+ key,
95
+ title: (prop.title ?? key).trim() || key,
96
+ ...(prop.description ? { description: prop.description } : {}),
97
+ options,
98
+ multiple,
99
+ ...(customKey ? { customKey } : {}),
100
+ kind,
101
+ required: requiredKeys.has(key),
102
+ })
103
+ }
104
+ return items.length ? items : null
105
+ }
106
+
107
+ /** 渲染一道题给 QQ(纯文本)。total>1 时带进度。 */
108
+ export function renderQuestion(item: QuestionItem, index: number, total: number): string {
109
+ const lines: string[] = []
110
+ lines.push(total > 1 ? `📝 需要你的选择(第 ${index + 1}/${total} 题)` : "📝 需要你的选择")
111
+ lines.push(item.title)
112
+ if (item.description) lines.push(item.description)
113
+ lines.push("")
114
+ if (item.options.length) {
115
+ item.options.forEach((o, i) => {
116
+ const desc = o.description ? `(${o.description})` : ""
117
+ lines.push(`${String.fromCharCode(65 + i)}. ${o.label}${desc}`)
118
+ })
119
+ if (item.multiple) {
120
+ lines.push("", "可多选:回复字母,如 AB 或 A,B;也可以直接输入自定义答案")
121
+ } else {
122
+ lines.push("", "回复选项字母(或序号);也可以直接输入自定义答案")
123
+ }
124
+ } else if (item.kind === "boolean") {
125
+ lines.push("回复「是」或「否」")
126
+ } else {
127
+ lines.push("请直接回复内容")
128
+ }
129
+ lines.push("回复「取消」放弃;30 分钟未答:可选题自动跳过,必填题中止")
130
+ return lines.join("\n")
131
+ }
132
+
133
+ export type ParsedAnswer =
134
+ | { ok: true; value: string | string[]; custom: boolean }
135
+ | { ok: false }
136
+
137
+ /** 把用户的文字回复解析成答案;无法识别时返回 ok:false(调用方重新提问)。 */
138
+ export function parseAnswer(item: QuestionItem, content: string): ParsedAnswer {
139
+ const text = content.trim()
140
+ if (!text) return { ok: false }
141
+
142
+ if (item.options.length) {
143
+ const pick = (token: string): QuestionOption | null => {
144
+ const t = token.trim()
145
+ if (!t) return null
146
+ if (/^[A-Za-z]$/.test(t)) {
147
+ const idx = t.toUpperCase().charCodeAt(0) - 65
148
+ return item.options[idx] ?? null
149
+ }
150
+ if (/^\d+$/.test(t)) {
151
+ const idx = Number(t) - 1
152
+ return Number.isInteger(idx) ? (item.options[idx] ?? null) : null
153
+ }
154
+ const lower = t.toLowerCase()
155
+ return (
156
+ item.options.find((o) => o.label.toLowerCase() === lower) ??
157
+ item.options.find((o) => o.value.toLowerCase() === lower) ??
158
+ null
159
+ )
160
+ }
161
+
162
+ if (item.multiple) {
163
+ // "AB" 这种连写字母按单字符拆开;其余按分隔符切
164
+ const tokens = /^[A-Za-z]{2,}$/.test(text)
165
+ ? text.split("")
166
+ : text.split(/[\s,,、/|]+/).filter(Boolean)
167
+ const hits: QuestionOption[] = []
168
+ for (const token of tokens) {
169
+ const hit = pick(token)
170
+ if (!hit) {
171
+ // 任一 token 不是选项 → 视为整段自定义答案
172
+ if (item.customKey) return { ok: true, value: text, custom: true }
173
+ return { ok: false }
174
+ }
175
+ if (!hits.includes(hit)) hits.push(hit)
176
+ }
177
+ if (!hits.length) return { ok: false }
178
+ return { ok: true, value: hits.map((h) => h.value), custom: false }
179
+ }
180
+
181
+ const hit = pick(text)
182
+ if (hit) return { ok: true, value: hit.value, custom: false }
183
+ if (item.customKey) return { ok: true, value: text, custom: true }
184
+ return { ok: false }
185
+ }
186
+
187
+ if (item.kind === "boolean") {
188
+ if (/^(是|对|yes|y|true|1|要|确定|ok)$/i.test(text)) return { ok: true, value: "true", custom: false }
189
+ if (/^(否|不|no|n|false|0|不要|取消选择)$/i.test(text)) return { ok: true, value: "false", custom: false }
190
+ return { ok: false }
191
+ }
192
+
193
+ if (item.kind === "number" || item.kind === "integer") {
194
+ if (!/^-?\d+(\.\d+)?$/.test(text)) return { ok: false }
195
+ return { ok: true, value: text, custom: false }
196
+ }
197
+
198
+ return { ok: true, value: text, custom: false }
199
+ }
200
+
201
+ /** 组装写回的 content:自定义答案同时写主属性与 custom 属性(服务端优先取 custom)。 */
202
+ export function toFormContent(answers: Array<{ item: QuestionItem; value: string | string[]; custom: boolean }>): Record<string, unknown> {
203
+ const content: Record<string, unknown> = {}
204
+ for (const { item, value, custom } of answers) {
205
+ content[item.key] = value
206
+ if (custom && item.customKey) content[item.customKey] = Array.isArray(value) ? value.join(", ") : value
207
+ }
208
+ return content
209
+ }
210
+
211
+ export type TimeoutOutcome =
212
+ | { action: "accept"; content: Record<string, unknown> }
213
+ | { action: "cancel" }
214
+
215
+ /**
216
+ * 30 分钟未作答的归宿(2026-11-08 用户拍板:沉默 ≠ 拒绝,未答应跳过)。
217
+ * - 存在「未答的必填题」→ cancel(此时 cancel 才是真语义:任务缺必要输入,必须中止)
218
+ * - 否则 → accept 已收集的答案(缺的题上游会写成 Unanswered,模型按默认继续推进)
219
+ * 与上游 question 工具对齐:cancelled 分支会回给模型 "The user dismissed this question"
220
+ * (用户主动拒绝),绝不能拿来表示「用户没看见/没来得及答」。
221
+ */
222
+ export function timeoutOutcome(
223
+ questions: readonly QuestionItem[],
224
+ answers: ReadonlyArray<{ item: QuestionItem; value: string | string[]; custom: boolean }>,
225
+ ): TimeoutOutcome {
226
+ const answeredKeys = new Set(answers.map((a) => a.item.key))
227
+ const missingRequired = questions.some((q) => q.required && !answeredKeys.has(q.key))
228
+ if (missingRequired) return { action: "cancel" }
229
+ return { action: "accept", content: toFormContent([...answers]) }
230
+ }