@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.
- package/README.md +303 -229
- package/README.zh-CN.md +313 -0
- package/bin/openqq.js +13 -0
- package/dist/notify-cli.js +92 -26
- package/dist/opencode-qqbot-plugin.js +1201 -67
- package/dist/openqq-bot.js +3963 -893
- package/dist/openqq-cli.js +980 -0
- package/dist/openqq-watchdog.js +1326 -0
- package/package.json +8 -5
- package/src/bridge.ts +1599 -150
- package/src/cli/notify.ts +170 -158
- package/src/cli/openqq-main.ts +95 -0
- package/src/cli/service-main.ts +302 -0
- package/src/cli/watchdog-main.ts +92 -0
- package/src/commands.ts +670 -554
- package/src/config.ts +122 -3
- package/src/core/bot-config.ts +66 -0
- package/src/core/bot-control.ts +166 -0
- package/src/core/bot-launcher.ts +110 -15
- package/src/core/card-approval.ts +20 -9
- package/src/core/cron-reminders.ts +84 -0
- package/src/core/delivery.ts +145 -54
- package/src/core/discovery.ts +207 -151
- package/src/core/error-rescue.ts +1 -1
- package/src/core/file-log.ts +50 -9
- package/src/core/form-bridge.ts +14 -5
- package/src/core/form-journal.ts +4 -4
- package/src/core/gateway-state.ts +3 -3
- package/src/core/inflight-journal.ts +215 -36
- package/src/core/instance-lock.ts +42 -3
- package/src/core/linkwatch.ts +3 -4
- package/src/core/log-rotate.ts +91 -0
- package/src/core/markdown.ts +117 -0
- package/src/core/message-queue.ts +242 -91
- package/src/core/mirror.ts +228 -182
- package/src/core/notices.ts +62 -0
- package/src/core/notify-sink.ts +93 -96
- package/src/core/outbound-sanitize.ts +29 -0
- package/src/core/outbox.ts +10 -9
- package/src/core/permission.ts +634 -303
- package/src/core/question-form.ts +230 -0
- package/src/core/queue-view.ts +662 -0
- package/src/core/send-media.ts +61 -2
- package/src/core/start-error.ts +26 -0
- package/src/core/storage.ts +34 -0
- package/src/core/stt.ts +55 -0
- package/src/core/supervisor-election.ts +53 -0
- package/src/core/watchdog.ts +670 -0
- package/src/core/zombie-reaper.ts +127 -0
- package/src/index.ts +262 -162
- package/src/opencode/client.ts +237 -27
- package/src/opencode/eventstream.ts +94 -12
- package/src/opencode/history.ts +4 -2
- package/src/opencode/mirror.ts +25 -5
- package/src/opencode/sessions.ts +135 -37
- package/src/plugin-entry.ts +139 -115
- package/src/qq/api.ts +165 -61
- package/src/qq/gateway.ts +114 -16
- package/src/qq/media.ts +21 -0
- package/src/qq/sender.ts +79 -18
- package/src/qq/types.ts +7 -0
package/src/commands.ts
CHANGED
|
@@ -1,554 +1,670 @@
|
|
|
1
|
-
// @input: ./config, ./qq/types, ./opencode/* (client, sessions)
|
|
2
|
-
// @output: isCommand, handleCommand, handlePendingSelection, CommandContext, PendingSelection
|
|
3
|
-
// @pos: 根层 - 命令系统: /new /
|
|
4
|
-
import type { Config } from "./config.js"
|
|
5
|
-
import type { MessageContext } from "./qq/types.js"
|
|
6
|
-
import type { OpencodeClient, SessionMessage } from "./opencode/client.js"
|
|
7
|
-
import { renderHistory } from "./opencode/history.js"
|
|
8
|
-
import { SessionManager } from "./opencode/sessions.js"
|
|
9
|
-
import { sendLocalFile } from "./core/send-media.js"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
case "
|
|
116
|
-
return
|
|
117
|
-
case "
|
|
118
|
-
return
|
|
119
|
-
|
|
120
|
-
return
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
return
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
.
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
return
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
1
|
+
// @input: ./config, ./qq/types, ./opencode/* (client, sessions)
|
|
2
|
+
// @output: isCommand, handleCommand, handlePendingSelection, CommandContext, PendingSelection
|
|
3
|
+
// @pos: 根层 - 命令系统: /new /cancel /status /sessions /help /models /agents /rename /undo /amend
|
|
4
|
+
import type { Config } from "./config.js"
|
|
5
|
+
import type { MessageContext } from "./qq/types.js"
|
|
6
|
+
import type { OpencodeClient, SessionMessage } from "./opencode/client.js"
|
|
7
|
+
import { renderHistory } from "./opencode/history.js"
|
|
8
|
+
import { SessionManager } from "./opencode/sessions.js"
|
|
9
|
+
import { sendLocalFile } from "./core/send-media.js"
|
|
10
|
+
import { fileLogStatus } from "./core/file-log.js"
|
|
11
|
+
|
|
12
|
+
const SELECTION_TTL_MS = 60_000
|
|
13
|
+
|
|
14
|
+
// 观察模式 API(bridge 实现:SSE 订阅 + 里程碑推送)
|
|
15
|
+
export interface WatchApi {
|
|
16
|
+
start: (userId: string, sessionID: string, ctx: MessageContext) => string
|
|
17
|
+
stop: (userId: string) => string
|
|
18
|
+
status: (userId: string) => string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 摘要镜像 API(bridge 实现:订阅 + 摘要卡投递 + 持久开关)
|
|
22
|
+
export interface MirrorApi {
|
|
23
|
+
set: (enabled: boolean) => string
|
|
24
|
+
status: () => string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Q1:忙碌队列 API(bridge 实现:FIFO + drain + 控制命令)
|
|
28
|
+
export interface QueueApi {
|
|
29
|
+
list: (userId: string) => string
|
|
30
|
+
/** 全量/分页视图(/queue all [页码]) */
|
|
31
|
+
listAll: (userId: string, page?: number) => string
|
|
32
|
+
/** /status 摘要行(排队/在途/执行中/孤儿/最近拒绝) */
|
|
33
|
+
summary: (userId: string) => string
|
|
34
|
+
/** 清空队列,返回清掉的条数(/cancel 与 /queue clear 共用;同时结清台账) */
|
|
35
|
+
clear: (userId: string) => number
|
|
36
|
+
drop: (userId: string, id: number) => string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 台账诊断 API(bridge 实现:磁盘台账 + 会话历史对账)
|
|
40
|
+
export interface LedgerApi {
|
|
41
|
+
view: (userId: string, args: string) => Promise<string>
|
|
42
|
+
check: (userId: string) => Promise<string>
|
|
43
|
+
settle: (userId: string, key: string, confirm: boolean) => string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Q2:手动进度查询 API(bridge 实现:本地事件统计,零模型调用)
|
|
47
|
+
export interface ProgressApi {
|
|
48
|
+
query: (userId: string) => string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface CommandContext {
|
|
52
|
+
config: Config
|
|
53
|
+
client: OpencodeClient
|
|
54
|
+
sessions: SessionManager
|
|
55
|
+
getAccessToken: () => Promise<string>
|
|
56
|
+
pendingSelections: Map<string, PendingSelection>
|
|
57
|
+
watch: WatchApi
|
|
58
|
+
mirror: MirrorApi
|
|
59
|
+
queue: QueueApi
|
|
60
|
+
ledger: LedgerApi
|
|
61
|
+
progress: ProgressApi
|
|
62
|
+
/** P1 防线 B:/cancel 彻底复位(清队列+abort+杀后台等待+结清在途账+drain) */
|
|
63
|
+
stopAll: (userId: string) => Promise<{ cleared: number; title: string | undefined }>
|
|
64
|
+
/** steer 降级版:/amend <补充> 打断当前任务并以「原 prompt+补充」续跑(2026-11-22,参考 Hermes update_prompt) */
|
|
65
|
+
amend: (userId: string, amendment: string) => Promise<string>
|
|
66
|
+
/** 对齐官方 /undo:回滚上一回合并恢复文件,回执带被撤 prompt(2026-11-23,opencode.ai/docs/snapshots 为准) */
|
|
67
|
+
undo: (userId: string) => Promise<string>
|
|
68
|
+
/** 显性入队(/queue <文本>,owner 2026-11-23):steer 默认下的显式排队动词——排进 FIFO,当前任务跑完才执行 */
|
|
69
|
+
enqueue: (ctx: MessageContext, text: string) => Promise<string>
|
|
70
|
+
/** 官方斜杠命令直通(owner 2026-11-23):/goal /compact /redo /btw 走引擎内置实现,与桌面端同语义 */
|
|
71
|
+
engineCommand: (userId: string, name: string, text: string) => Promise<string>
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface PendingSelection {
|
|
75
|
+
type: "session" | "model"
|
|
76
|
+
items: Array<{ id: string; label: string; directory?: string }>
|
|
77
|
+
expiresAt: number
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface ParsedCommand {
|
|
81
|
+
name: string
|
|
82
|
+
args: string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface ListedSession {
|
|
86
|
+
id: string
|
|
87
|
+
title: string
|
|
88
|
+
/** location.directory(官方会话列表语义:每条会话自带 cwd,load 必须用它) */
|
|
89
|
+
directory?: string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface ListedModel {
|
|
93
|
+
id: string
|
|
94
|
+
label: string
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface ListedAgent {
|
|
98
|
+
id: string
|
|
99
|
+
label: string
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isCommand(content: string): boolean {
|
|
103
|
+
return content.trim().startsWith("/")
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function handleCommand(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
107
|
+
cmdCtx.pendingSelections.delete(ctx.userId)
|
|
108
|
+
|
|
109
|
+
const parsed = parseCommand(ctx.content)
|
|
110
|
+
if (!parsed) {
|
|
111
|
+
return "不是有效命令"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
switch (parsed.name) {
|
|
115
|
+
case "new":
|
|
116
|
+
return handleNew(ctx, cmdCtx)
|
|
117
|
+
case "cancel": // 唯一中止命令(owner 2026-11-23 裁定:不收别名减负担;UAT 实证 /cancel 是用户自然习惯)
|
|
118
|
+
return handleStop(ctx, cmdCtx)
|
|
119
|
+
case "amend": // steer 降级版:忙时插话并入当前任务(参考 Hermes update_prompt)
|
|
120
|
+
return cmdCtx.amend(ctx.userId, parsed.args)
|
|
121
|
+
case "undo": // 对齐官方 TUI /undo:回滚上一回合+恢复文件(prompt 回盒由回执承担)
|
|
122
|
+
return cmdCtx.undo(ctx.userId)
|
|
123
|
+
case "goal": // 官方引擎内置(v2.2.1+,PR#59437):目标模式直通——审查器/50轮上限在引擎侧
|
|
124
|
+
return cmdCtx.engineCommand(ctx.userId, "goal", parsed.args)
|
|
125
|
+
case "compact": // 官方引擎命令:会话压缩
|
|
126
|
+
return cmdCtx.engineCommand(ctx.userId, "compact", parsed.args)
|
|
127
|
+
case "redo": // 官方引擎命令:撤销暂存回滚的恢复
|
|
128
|
+
return cmdCtx.engineCommand(ctx.userId, "redo", parsed.args)
|
|
129
|
+
case "btw": // 官方引擎命令:侧问(不写入会话上下文)
|
|
130
|
+
return cmdCtx.engineCommand(ctx.userId, "btw", parsed.args)
|
|
131
|
+
case "queue":
|
|
132
|
+
return handleQueue(ctx, parsed.args, cmdCtx)
|
|
133
|
+
case "ledger":
|
|
134
|
+
return handleLedger(ctx, parsed.args, cmdCtx)
|
|
135
|
+
case "progress":
|
|
136
|
+
return cmdCtx.progress.query(ctx.userId)
|
|
137
|
+
case "status":
|
|
138
|
+
return handleStatus(ctx, cmdCtx)
|
|
139
|
+
case "sessions":
|
|
140
|
+
return handleSessions(ctx, cmdCtx)
|
|
141
|
+
case "help":
|
|
142
|
+
return buildHelpText()
|
|
143
|
+
case "models": // 官方 TUI 同名(owner 2026-11-23:严格对齐官方,单数别名已收编不留负担)
|
|
144
|
+
return handleModel(ctx, parsed.args, cmdCtx)
|
|
145
|
+
case "agents": // 官方 TUI 同名
|
|
146
|
+
return handleAgent(ctx, parsed.args, cmdCtx)
|
|
147
|
+
case "rename":
|
|
148
|
+
return handleRename(ctx, parsed.args, cmdCtx)
|
|
149
|
+
case "history":
|
|
150
|
+
return handleHistory(ctx, parsed.args, cmdCtx)
|
|
151
|
+
case "watch":
|
|
152
|
+
return handleWatch(ctx, parsed.args, cmdCtx)
|
|
153
|
+
case "send":
|
|
154
|
+
return handleSend(ctx, parsed.args, cmdCtx)
|
|
155
|
+
case "mirror":
|
|
156
|
+
return handleMirror(parsed.args, cmdCtx)
|
|
157
|
+
default:
|
|
158
|
+
return `不支持的命令:/${parsed.name}\n发送 /help 查看可用命令`
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export async function handlePendingSelection(
|
|
163
|
+
userId: string,
|
|
164
|
+
selection: number,
|
|
165
|
+
cmdCtx: CommandContext,
|
|
166
|
+
): Promise<string | null> {
|
|
167
|
+
const pending = cmdCtx.pendingSelections.get(userId)
|
|
168
|
+
if (!pending) {
|
|
169
|
+
return null
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (pending.expiresAt <= Date.now()) {
|
|
173
|
+
cmdCtx.pendingSelections.delete(userId)
|
|
174
|
+
return null
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const item = pending.items[selection - 1]
|
|
178
|
+
if (!item) {
|
|
179
|
+
return `序号无效,请回复 1-${pending.items.length}`
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
cmdCtx.pendingSelections.delete(userId)
|
|
183
|
+
|
|
184
|
+
if (pending.type === "session") {
|
|
185
|
+
cmdCtx.sessions.switchSession(userId, item.id, item.label, item.directory)
|
|
186
|
+
return `已切换到会话:${item.label}`
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const model = splitModelId(item.id)
|
|
190
|
+
if (!model) {
|
|
191
|
+
return `模型项无效:${item.label}`
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
await ensureSession(userId, cmdCtx)
|
|
195
|
+
await cmdCtx.sessions.setModel(userId, model.providerId, model.modelId)
|
|
196
|
+
return `已切换模型:${item.label}`
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function parseCommand(content: string): ParsedCommand | null {
|
|
200
|
+
const trimmed = content.trim()
|
|
201
|
+
if (!trimmed.startsWith("/")) {
|
|
202
|
+
return null
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const [rawName, ...rest] = trimmed.slice(1).split(/\s+/)
|
|
206
|
+
const name = rawName?.toLowerCase()
|
|
207
|
+
if (!name) {
|
|
208
|
+
return null
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
name,
|
|
213
|
+
args: rest.join(" ").trim(),
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function handleNew(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
218
|
+
const sessionId = (await cmdCtx.sessions.createNew(ctx.userId)).sessionId
|
|
219
|
+
return ["已创建新会话", `ID:${sessionId}`].join("\n")
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 缺口 4:/send <路径>——把本地文件/图片发到 QQ(路径白名单内)
|
|
223
|
+
async function handleSend(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
224
|
+
const path = args.trim()
|
|
225
|
+
if (!path) {
|
|
226
|
+
return "用法:/send <本地文件路径>(仅允许白名单目录:QQ 图片缓存与 bot 工作目录)"
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const done = await sendLocalFile(ctx, path, { getToken: cmdCtx.getAccessToken })
|
|
230
|
+
return `📎 ${done}`
|
|
231
|
+
} catch (error) {
|
|
232
|
+
return `⚠️ 发送失败:${error instanceof Error ? error.message : error}`
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function handleStop(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
237
|
+
const hadSession = !!cmdCtx.sessions.getSession(ctx.userId)
|
|
238
|
+
const { cleared, title } = await cmdCtx.stopAll(ctx.userId)
|
|
239
|
+
const suffix = cleared > 0 ? `,队列中的 ${cleared} 条已清空` : ""
|
|
240
|
+
if (!hadSession && !title) {
|
|
241
|
+
return cleared > 0 ? `当前没有会话可停止${suffix}` : "当前还没有会话可停止"
|
|
242
|
+
}
|
|
243
|
+
const target = title?.trim() ? title : cmdCtx.sessions.getSession(ctx.userId)?.sessionId ?? ""
|
|
244
|
+
return `已发送停止请求:${target}${suffix}`
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Q1:/queue 查看与管理忙碌队列(2026-11-05:默认视图 join 台账,不再只看内存)
|
|
248
|
+
function handleQueue(ctx: MessageContext, args: string, cmdCtx: CommandContext): string | Promise<string> {
|
|
249
|
+
const parts = args.trim().split(/\s+/).filter(Boolean)
|
|
250
|
+
const sub = parts[0]?.toLowerCase() ?? ""
|
|
251
|
+
if (sub === "clear") {
|
|
252
|
+
const n = cmdCtx.queue.clear(ctx.userId)
|
|
253
|
+
return n > 0 ? `已清空队列(${n} 条)` : "队列本来就是空的"
|
|
254
|
+
}
|
|
255
|
+
if (sub === "drop") {
|
|
256
|
+
const id = Number(parts[1])
|
|
257
|
+
if (!Number.isInteger(id) || id <= 0) return "用法:/queue drop <编号>"
|
|
258
|
+
return cmdCtx.queue.drop(ctx.userId, id)
|
|
259
|
+
}
|
|
260
|
+
if (sub === "all") {
|
|
261
|
+
const page = Number(parts[1] ?? "1")
|
|
262
|
+
return cmdCtx.queue.listAll(ctx.userId, Number.isInteger(page) && page > 0 ? page : 1)
|
|
263
|
+
}
|
|
264
|
+
if (/^\d+$/.test(sub)) {
|
|
265
|
+
return cmdCtx.queue.listAll(ctx.userId, Math.max(1, Number(sub)))
|
|
266
|
+
}
|
|
267
|
+
// 显性入队动词(owner 2026-11-23):steer 默认模式下,/queue <文本>=把这条文本
|
|
268
|
+
// 排进 FIFO(当前任务跑完才执行),而不是默认的并入当前任务。
|
|
269
|
+
if (args.trim()) {
|
|
270
|
+
return cmdCtx.enqueue(ctx, args.trim())
|
|
271
|
+
}
|
|
272
|
+
return cmdCtx.queue.list(ctx.userId)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 2026-11-05:/ledger——台账诊断(快照 / 对账 / 人工结清)
|
|
276
|
+
async function handleLedger(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
277
|
+
const parts = args.trim().split(/\s+/).filter(Boolean)
|
|
278
|
+
const sub = parts[0]?.toLowerCase() ?? ""
|
|
279
|
+
if (sub === "check") {
|
|
280
|
+
return cmdCtx.ledger.check(ctx.userId)
|
|
281
|
+
}
|
|
282
|
+
if (sub === "settle") {
|
|
283
|
+
const key = parts[1] ?? ""
|
|
284
|
+
const confirm = (parts[2] ?? "").toLowerCase() === "confirm"
|
|
285
|
+
if (!key) return "用法:/ledger settle <msgId尾号> confirm"
|
|
286
|
+
return cmdCtx.ledger.settle(ctx.userId, key, confirm)
|
|
287
|
+
}
|
|
288
|
+
return cmdCtx.ledger.view(ctx.userId, args)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* /status 会话行标题解析(2026-11-09 陈旧缓存显示缺陷修复):
|
|
293
|
+
* 实证「/status 显示 QQBot开发、/sessions 显示 [当前]近三日任务与设计评估」——
|
|
294
|
+
* 桌面端/TUI 改名后 sessions.json 缓存不跟随(SessionManager 只在创建与 /rename 时写 title)。
|
|
295
|
+
* 修复:/status 输出前用 session.get 取实时标题并回写缓存(同 id 不触发设置重放);
|
|
296
|
+
* OpenCode 不可达时回落缓存标题,不阻断 /status。
|
|
297
|
+
*/
|
|
298
|
+
export async function resolveStatusSessionTitle(
|
|
299
|
+
sessions: SessionManager,
|
|
300
|
+
client: OpencodeClient,
|
|
301
|
+
userId: string,
|
|
302
|
+
): Promise<{ title?: string; sessionId?: string }> {
|
|
303
|
+
const session = sessions.getSession(userId)
|
|
304
|
+
if (!session) return {}
|
|
305
|
+
try {
|
|
306
|
+
const detail = await client.session.get({ path: { id: session.sessionId } })
|
|
307
|
+
const liveTitle = detail?.data?.title
|
|
308
|
+
if (liveTitle && liveTitle !== session.title) {
|
|
309
|
+
sessions.switchSession(userId, session.sessionId, liveTitle, session.directory)
|
|
310
|
+
return { title: liveTitle, sessionId: session.sessionId }
|
|
311
|
+
}
|
|
312
|
+
} catch {
|
|
313
|
+
/* OpenCode 不可达:回落缓存标题 */
|
|
314
|
+
}
|
|
315
|
+
return { title: session.title, sessionId: session.sessionId }
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async function handleStatus(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
319
|
+
const sessionInfo = await resolveStatusSessionTitle(cmdCtx.sessions, cmdCtx.client, ctx.userId)
|
|
320
|
+
const { providerId, modelId } = cmdCtx.sessions.getModel(ctx.userId)
|
|
321
|
+
const agentId = cmdCtx.sessions.getAgent(ctx.userId)
|
|
322
|
+
|
|
323
|
+
const openCodeStatus = await getOpenCodeStatus(cmdCtx.client)
|
|
324
|
+
const qqStatus = await getQQStatus(cmdCtx)
|
|
325
|
+
const log = fileLogStatus()
|
|
326
|
+
const logLine = log
|
|
327
|
+
? (() => {
|
|
328
|
+
const ageSec = log.lastWriteAt ? Math.round((Date.now() - log.lastWriteAt) / 1000) : -1
|
|
329
|
+
const stale = ageSec > 300 ? " ⚠️ 日志停滞" : ""
|
|
330
|
+
return `日志:${log.path} · ${ageSec >= 0 ? `最后写入 ${ageSec}s 前` : "尚未写入"}${stale}`
|
|
331
|
+
})()
|
|
332
|
+
: "日志:未启用(QQ_LOG_FILE=off)"
|
|
333
|
+
|
|
334
|
+
return [
|
|
335
|
+
"OpenCode 状态",
|
|
336
|
+
`服务器:${openCodeStatus}`,
|
|
337
|
+
`QQ 鉴权:${qqStatus}`,
|
|
338
|
+
`会话:${sessionInfo.sessionId ? `${sessionInfo.title ?? "未命名会话"} (${sessionInfo.sessionId})` : "未创建"}`,
|
|
339
|
+
`模型:${providerId && modelId ? `${providerId} / ${modelId}` : "默认"}`,
|
|
340
|
+
`Agent:${agentId ?? "默认"}`,
|
|
341
|
+
cmdCtx.queue.summary(ctx.userId),
|
|
342
|
+
logLine,
|
|
343
|
+
].join("\n")
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function handleSessions(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
347
|
+
const sessions = await listSessions(cmdCtx.client)
|
|
348
|
+
if (sessions.length === 0) {
|
|
349
|
+
return "当前没有可切换的历史会话"
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const currentSessionId = cmdCtx.sessions.getSession(ctx.userId)?.sessionId
|
|
353
|
+
cmdCtx.pendingSelections.set(ctx.userId, {
|
|
354
|
+
type: "session",
|
|
355
|
+
items: sessions.map((session) => ({ id: session.id, label: session.title, directory: session.directory })),
|
|
356
|
+
expiresAt: Date.now() + SELECTION_TTL_MS,
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
const lines = sessions.map((session, index) => {
|
|
360
|
+
const prefix = session.id === currentSessionId ? "[当前] " : ""
|
|
361
|
+
return `${index + 1}. ${prefix}${session.title}`
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
return ["会话列表:", ...lines, "回复序号切换会话(60 秒内有效)"].join("\n")
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
async function handleModel(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
368
|
+
if (!args) {
|
|
369
|
+
const models = await listModels(cmdCtx.client)
|
|
370
|
+
if (models.length === 0) {
|
|
371
|
+
return "当前没有可用模型"
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const current = cmdCtx.sessions.getModel(ctx.userId)
|
|
375
|
+
cmdCtx.pendingSelections.set(ctx.userId, {
|
|
376
|
+
type: "model",
|
|
377
|
+
items: models.map((model) => ({ id: model.id, label: model.label })),
|
|
378
|
+
expiresAt: Date.now() + SELECTION_TTL_MS,
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
const lines = models.map((model, index) => {
|
|
382
|
+
const isCurrent = current.providerId && current.modelId && `${current.providerId}/${current.modelId}` === model.id
|
|
383
|
+
return `${index + 1}. ${isCurrent ? "[当前] " : ""}${model.label}`
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
return ["可用模型:", ...lines, "回复序号或 /models <provider/model> 切换(60 秒内有效)"].join("\n")
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (/^\d+$/.test(args)) {
|
|
390
|
+
const result = await handlePendingSelection(ctx.userId, Number(args), cmdCtx)
|
|
391
|
+
return result ?? "没有待选择的模型列表,请先发送 /models"
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const model = splitModelId(args)
|
|
395
|
+
if (!model) {
|
|
396
|
+
return "模型格式不对,请使用 /model <provider/model>"
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// DEF-qqbot-20261111-01:存在性校验——此前仅校格式直写映射,假模型也回「已切换成功」误导用户
|
|
400
|
+
let models: ListedModel[]
|
|
401
|
+
try {
|
|
402
|
+
models = await listModels(cmdCtx.client)
|
|
403
|
+
} catch {
|
|
404
|
+
return "无法从引擎获取模型列表(引擎不可达),本次未切换,请稍后重试"
|
|
405
|
+
}
|
|
406
|
+
const wanted = `${model.providerId}/${model.modelId}`
|
|
407
|
+
if (!models.some((m) => m.id === wanted)) {
|
|
408
|
+
return `模型不存在:${wanted}。发 /model 查看可用列表(注意区分大小写与 provider 前缀)`
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
await ensureSession(ctx.userId, cmdCtx)
|
|
412
|
+
await cmdCtx.sessions.setModel(ctx.userId, model.providerId, model.modelId)
|
|
413
|
+
return `已切换模型:${model.providerId} / ${model.modelId}`
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async function handleAgent(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
417
|
+
const agents = await listAgents(cmdCtx.client)
|
|
418
|
+
if (!args) {
|
|
419
|
+
if (agents.length === 0) {
|
|
420
|
+
return "当前没有可用 Agent"
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const currentAgent = cmdCtx.sessions.getAgent(ctx.userId)
|
|
424
|
+
const lines = agents.map((agent, index) => {
|
|
425
|
+
const isCurrent = currentAgent === agent.id
|
|
426
|
+
return `${index + 1}. ${isCurrent ? "[当前] " : ""}${agent.label}`
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
return ["可用 Agent:", ...lines, "回复 /agent <name> 切换"].join("\n")
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const normalized = args.trim().toLowerCase()
|
|
433
|
+
const matched = agents.find((agent) => agent.id.toLowerCase() === normalized)
|
|
434
|
+
if (!matched) {
|
|
435
|
+
return `未找到 Agent:${args}`
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
await ensureSession(ctx.userId, cmdCtx)
|
|
439
|
+
await cmdCtx.sessions.setAgent(ctx.userId, matched.id)
|
|
440
|
+
return `已切换 Agent:${matched.id}`
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async function handleRename(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
444
|
+
const title = args.trim()
|
|
445
|
+
if (!title) {
|
|
446
|
+
return "用法:/rename <新名称>"
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const session = cmdCtx.sessions.getSession(ctx.userId)
|
|
450
|
+
if (!session) {
|
|
451
|
+
return "当前还没有会话可重命名"
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
cmdCtx.sessions.switchSession(ctx.userId, session.sessionId, title, session.directory)
|
|
455
|
+
return `已重命名当前会话:${title}`
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
async function handleHistory(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
459
|
+
const session = cmdCtx.sessions.getSession(ctx.userId)
|
|
460
|
+
if (!session) {
|
|
461
|
+
return "当前还没有会话"
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const n = /^\d+$/.test(args.trim()) ? Math.min(Math.max(Number(args.trim()), 1), 10) : 3
|
|
465
|
+
const res = await cmdCtx.client.session.messages({ path: { id: session.sessionId } })
|
|
466
|
+
const messages: SessionMessage[] = res?.data ?? []
|
|
467
|
+
const body = renderHistory(messages, n)
|
|
468
|
+
if (!body) {
|
|
469
|
+
return "该会话暂无历史消息"
|
|
470
|
+
}
|
|
471
|
+
return `📜 ${session.title ?? session.sessionId} · 最近 ${n} 轮\n\n${body}`
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
async function handleWatch(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
475
|
+
const a = args.trim()
|
|
476
|
+
const lower = a.toLowerCase()
|
|
477
|
+
|
|
478
|
+
if (lower === "off" || lower === "stop") {
|
|
479
|
+
return cmdCtx.watch.stop(ctx.userId)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (!a) {
|
|
483
|
+
const session = await cmdCtx.sessions.getOrCreate(ctx.userId)
|
|
484
|
+
return cmdCtx.watch.start(ctx.userId, session.sessionId, ctx)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (/^ses_/i.test(a)) {
|
|
488
|
+
return cmdCtx.watch.start(ctx.userId, a, ctx)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return "用法:/watch 观察当前会话|/watch <会话ID> 观察指定会话|/watch off 停止观察"
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function handleMirror(args: string, cmdCtx: CommandContext): string {
|
|
495
|
+
const a = args.trim().toLowerCase()
|
|
496
|
+
if (!a) return cmdCtx.mirror.status()
|
|
497
|
+
if (["on", "1", "开", "打开"].includes(a)) return cmdCtx.mirror.set(true)
|
|
498
|
+
if (["off", "0", "关", "关闭"].includes(a)) return cmdCtx.mirror.set(false)
|
|
499
|
+
return "用法:/mirror 查看状态|/mirror on 开启|/mirror off 关闭"
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async function ensureSession(userId: string, cmdCtx: CommandContext): Promise<void> {
|
|
503
|
+
await cmdCtx.sessions.getOrCreate(userId)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
async function getOpenCodeStatus(client: OpencodeClient): Promise<string> {
|
|
507
|
+
try {
|
|
508
|
+
await client.session.list()
|
|
509
|
+
return "运行中"
|
|
510
|
+
} catch (error) {
|
|
511
|
+
return `异常(${toErrorMessage(error)})`
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
async function getQQStatus(cmdCtx: CommandContext): Promise<string> {
|
|
516
|
+
try {
|
|
517
|
+
await cmdCtx.getAccessToken()
|
|
518
|
+
return "正常"
|
|
519
|
+
} catch (error) {
|
|
520
|
+
return `异常(${toErrorMessage(error)})`
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
async function listSessions(client: OpencodeClient): Promise<ListedSession[]> {
|
|
525
|
+
const result = await client.session.list()
|
|
526
|
+
const rawItems = extractArray(result)
|
|
527
|
+
const items = rawItems.length > 0 ? rawItems : extractArray(extractProperty(result, "data"))
|
|
528
|
+
|
|
529
|
+
return items
|
|
530
|
+
.map((item) => {
|
|
531
|
+
const id = getString(item, "id")
|
|
532
|
+
if (!id) {
|
|
533
|
+
return null
|
|
534
|
+
}
|
|
535
|
+
const location = extractProperty(item, "location")
|
|
536
|
+
const directory = location ? getString(location, "directory") : undefined
|
|
537
|
+
return {
|
|
538
|
+
id,
|
|
539
|
+
title: getString(item, "title") ?? id,
|
|
540
|
+
...(directory !== undefined ? { directory } : {}),
|
|
541
|
+
}
|
|
542
|
+
})
|
|
543
|
+
.filter((item): item is ListedSession => item !== null)
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
async function listModels(client: OpencodeClient): Promise<ListedModel[]> {
|
|
547
|
+
const configApi = extractProperty(client, "config")
|
|
548
|
+
const providersFn = typeof configApi === "object" && configApi !== null ? Reflect.get(configApi, "providers") : undefined
|
|
549
|
+
const response = typeof providersFn === "function" ? await Promise.resolve(providersFn.call(configApi)) : []
|
|
550
|
+
|
|
551
|
+
const providers = extractArray(response)
|
|
552
|
+
const resolvedProviders = providers.length > 0 ? providers : extractArray(extractProperty(response, "data"))
|
|
553
|
+
const models: ListedModel[] = []
|
|
554
|
+
|
|
555
|
+
for (const provider of resolvedProviders) {
|
|
556
|
+
const providerId = getString(provider, "id") ?? getString(provider, "providerID")
|
|
557
|
+
const rawModels = extractArray(extractProperty(provider, "models"))
|
|
558
|
+
for (const model of rawModels) {
|
|
559
|
+
const modelId = getString(model, "id") ?? getString(model, "modelID")
|
|
560
|
+
if (!providerId || !modelId) {
|
|
561
|
+
continue
|
|
562
|
+
}
|
|
563
|
+
models.push({
|
|
564
|
+
id: `${providerId}/${modelId}`,
|
|
565
|
+
label: `${providerId} / ${modelId}`,
|
|
566
|
+
})
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return models
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async function listAgents(client: OpencodeClient): Promise<ListedAgent[]> {
|
|
574
|
+
const response = await client.app.agents()
|
|
575
|
+
const rawAgents = extractArray(response)
|
|
576
|
+
const agents = rawAgents.length > 0 ? rawAgents : extractArray(extractProperty(response, "data"))
|
|
577
|
+
|
|
578
|
+
return agents
|
|
579
|
+
.map((agent) => {
|
|
580
|
+
const id = getString(agent, "id") ?? getString(agent, "name")
|
|
581
|
+
if (!id) {
|
|
582
|
+
return null
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const description = getString(agent, "description")
|
|
586
|
+
return {
|
|
587
|
+
id,
|
|
588
|
+
label: description ? `${id} - ${description}` : id,
|
|
589
|
+
}
|
|
590
|
+
})
|
|
591
|
+
.filter((agent): agent is ListedAgent => agent !== null)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function splitModelId(value: string): { providerId: string; modelId: string } | null {
|
|
595
|
+
const trimmed = value.trim()
|
|
596
|
+
const slashIndex = trimmed.indexOf("/")
|
|
597
|
+
if (slashIndex <= 0 || slashIndex === trimmed.length - 1) {
|
|
598
|
+
return null
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const providerId = trimmed.slice(0, slashIndex).trim()
|
|
602
|
+
const modelId = trimmed.slice(slashIndex + 1).trim()
|
|
603
|
+
if (!providerId || !modelId) {
|
|
604
|
+
return null
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
return { providerId, modelId }
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
export function buildHelpText(): string {
|
|
611
|
+
return [
|
|
612
|
+
"可用命令:",
|
|
613
|
+
"/new - 创建新会话",
|
|
614
|
+
"/stop - 停止当前 AI 运行",
|
|
615
|
+
"/status - 查看服务器和当前会话状态",
|
|
616
|
+
"/sessions - 列出历史会话并回复序号切换",
|
|
617
|
+
"/help - 查看帮助",
|
|
618
|
+
"/model - 列出可用模型",
|
|
619
|
+
"/model <provider/model> - 切换模型",
|
|
620
|
+
"/agent - 列出可用 Agent",
|
|
621
|
+
"/agent <name> - 切换 Agent",
|
|
622
|
+
"/rename <name> - 重命名当前会话",
|
|
623
|
+
"/history [N] - 查看当前会话最近 N 轮历史(默认 3,最多 10)",
|
|
624
|
+
"/send <路径> - 把白名单目录内的文件/图片发到 QQ",
|
|
625
|
+
"/watch - 观察当前会话实时过程(工具调用/完成会推送摘要)",
|
|
626
|
+
"/watch <会话ID> - 观察指定会话",
|
|
627
|
+
"/watch off - 停止观察",
|
|
628
|
+
"/progress - 手动进度查询(工具调用/失败统计、在途状态,本地事件源零模型调用)",
|
|
629
|
+
"/queue - 队列与台账对账视图;/queue all [页码] 全量;/queue clear 清空;/queue drop <编号> 定点删除;/queue <文本> 显性入队(steer 默认下排进 FIFO)",
|
|
630
|
+
"/goal <目标> - 官方目标模式(引擎内置 v2.2.1+);/goal 查状态;pause/resume/clear 控制",
|
|
631
|
+
"/compact /redo /btw - 官方引擎命令直通(压缩会话 / 恢复撤销 / 侧问)",
|
|
632
|
+
"/ledger - 台账快照(在途/孤儿/最近拒绝);/ledger check 对账会话历史;/ledger settle <尾号> confirm 人工结清",
|
|
633
|
+
"/cancel - 同 /stop:中止当前 AI 运行并清空忙碌队列",
|
|
634
|
+
"/mirror [on|off] - 桌面问答摘要镜像开关(会话轮次完成推送问答摘要)",
|
|
635
|
+
"",
|
|
636
|
+
"权限审批(AI 请求权限时):",
|
|
637
|
+
"回复「允许」= 本次允许|「总是」= 总是允许|「拒绝」= 拒绝",
|
|
638
|
+
"(30 分钟无响应自动拒绝)",
|
|
639
|
+
].join("\n")
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function extractArray(value: unknown): Record<string, unknown>[] {
|
|
643
|
+
if (!Array.isArray(value)) {
|
|
644
|
+
return []
|
|
645
|
+
}
|
|
646
|
+
return value.filter(isRecord)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function extractProperty(value: unknown, key: string): unknown {
|
|
650
|
+
if (!isRecord(value)) {
|
|
651
|
+
return undefined
|
|
652
|
+
}
|
|
653
|
+
return value[key]
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function getString(value: unknown, key: string): string | undefined {
|
|
657
|
+
if (!isRecord(value)) {
|
|
658
|
+
return undefined
|
|
659
|
+
}
|
|
660
|
+
const resolved = value[key]
|
|
661
|
+
return typeof resolved === "string" && resolved.trim() ? resolved : undefined
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
665
|
+
return typeof value === "object" && value !== null
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function toErrorMessage(error: unknown): string {
|
|
669
|
+
return error instanceof Error ? error.message : String(error)
|
|
670
|
+
}
|