@linjz/opencode-qqbot 0.3.5 → 0.3.7
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 +74 -39
- package/bin/openqq-setup.mjs +50 -50
- package/bin/openqq.js +47 -47
- package/dist/opencode-qqbot-plugin.js +10 -1
- package/dist/openqq-bot.js +365 -70
- package/package.json +69 -61
- package/src/_FOLDER.md +19 -19
- package/src/bridge.ts +936 -796
- package/src/commands.ts +554 -514
- package/src/config.ts +121 -121
- package/src/core/bot-launcher.ts +146 -134
- package/src/core/card-approval.ts +146 -146
- package/src/core/context-buffer.ts +78 -78
- package/src/core/delivery.ts +284 -284
- package/src/core/discovery.ts +151 -151
- package/src/core/error-rescue.ts +33 -0
- package/src/core/file-log.ts +45 -45
- package/src/core/form-bridge.ts +304 -304
- package/src/core/inflight-journal.ts +217 -151
- package/src/core/instance-lock.ts +122 -122
- package/src/core/linkwatch.ts +195 -195
- package/src/core/message-queue.ts +80 -0
- package/src/core/mirror.ts +173 -173
- package/src/core/permission.ts +301 -301
- package/src/core/reply-limiter.ts +106 -106
- package/src/core/send-media.ts +90 -90
- package/src/core/util.ts +8 -8
- package/src/index.ts +162 -162
- package/src/opencode/_FOLDER.md +11 -11
- package/src/opencode/client.ts +387 -386
- package/src/opencode/eventstream.ts +138 -138
- package/src/opencode/history.ts +143 -143
- package/src/opencode/mirror.ts +65 -65
- package/src/opencode/sessions.ts +158 -158
- package/src/plugin-entry.ts +112 -112
- package/src/qq/_FOLDER.md +13 -13
- package/src/qq/api.ts +539 -539
- package/src/qq/gateway.ts +492 -492
- package/src/qq/media.ts +219 -204
- package/src/qq/sender.ts +300 -300
- package/src/qq/types.ts +154 -152
- package/tsconfig.json +24 -24
package/src/commands.ts
CHANGED
|
@@ -1,514 +1,554 @@
|
|
|
1
|
-
// @input: ./config, ./qq/types, ./opencode/* (client, sessions)
|
|
2
|
-
// @output: isCommand, handleCommand, handlePendingSelection, CommandContext, PendingSelection
|
|
3
|
-
// @pos: 根层 - 命令系统: /new /stop /status /sessions /help /model /agent /rename
|
|
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
|
-
const SELECTION_TTL_MS = 60_000
|
|
12
|
-
|
|
13
|
-
// 观察模式 API(bridge 实现:SSE 订阅 + 里程碑推送)
|
|
14
|
-
export interface WatchApi {
|
|
15
|
-
start: (userId: string, sessionID: string, ctx: MessageContext) => string
|
|
16
|
-
stop: (userId: string) => string
|
|
17
|
-
status: (userId: string) => string
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// 摘要镜像 API(bridge 实现:订阅 + 摘要卡投递 + 持久开关)
|
|
21
|
-
export interface MirrorApi {
|
|
22
|
-
set: (enabled: boolean) => string
|
|
23
|
-
status: () => string
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
id: string
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface
|
|
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
|
-
case "
|
|
94
|
-
return
|
|
95
|
-
case "
|
|
96
|
-
return
|
|
97
|
-
case "
|
|
98
|
-
return
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
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
|
-
return
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
return
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async function
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (
|
|
272
|
-
return "
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
return
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
cmdCtx.
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (!
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if (
|
|
340
|
-
return
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (
|
|
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
|
-
return
|
|
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
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
1
|
+
// @input: ./config, ./qq/types, ./opencode/* (client, sessions)
|
|
2
|
+
// @output: isCommand, handleCommand, handlePendingSelection, CommandContext, PendingSelection
|
|
3
|
+
// @pos: 根层 - 命令系统: /new /stop /status /sessions /help /model /agent /rename
|
|
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
|
+
const SELECTION_TTL_MS = 60_000
|
|
12
|
+
|
|
13
|
+
// 观察模式 API(bridge 实现:SSE 订阅 + 里程碑推送)
|
|
14
|
+
export interface WatchApi {
|
|
15
|
+
start: (userId: string, sessionID: string, ctx: MessageContext) => string
|
|
16
|
+
stop: (userId: string) => string
|
|
17
|
+
status: (userId: string) => string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 摘要镜像 API(bridge 实现:订阅 + 摘要卡投递 + 持久开关)
|
|
21
|
+
export interface MirrorApi {
|
|
22
|
+
set: (enabled: boolean) => string
|
|
23
|
+
status: () => string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Q1:忙碌队列 API(bridge 实现:FIFO + drain + 控制命令)
|
|
27
|
+
export interface QueueApi {
|
|
28
|
+
list: (userId: string) => string
|
|
29
|
+
/** 清空队列,返回清掉的条数(/cancel 与 /queue clear 共用) */
|
|
30
|
+
clear: (userId: string) => number
|
|
31
|
+
drop: (userId: string, id: number) => string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Q2:手动进度查询 API(bridge 实现:本地事件统计,零模型调用)
|
|
35
|
+
export interface ProgressApi {
|
|
36
|
+
query: (userId: string) => string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface CommandContext {
|
|
40
|
+
config: Config
|
|
41
|
+
client: OpencodeClient
|
|
42
|
+
sessions: SessionManager
|
|
43
|
+
getAccessToken: () => Promise<string>
|
|
44
|
+
pendingSelections: Map<string, PendingSelection>
|
|
45
|
+
watch: WatchApi
|
|
46
|
+
mirror: MirrorApi
|
|
47
|
+
queue: QueueApi
|
|
48
|
+
progress: ProgressApi
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface PendingSelection {
|
|
52
|
+
type: "session" | "model"
|
|
53
|
+
items: Array<{ id: string; label: string }>
|
|
54
|
+
expiresAt: number
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface ParsedCommand {
|
|
58
|
+
name: string
|
|
59
|
+
args: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface ListedSession {
|
|
63
|
+
id: string
|
|
64
|
+
title: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface ListedModel {
|
|
68
|
+
id: string
|
|
69
|
+
label: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface ListedAgent {
|
|
73
|
+
id: string
|
|
74
|
+
label: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isCommand(content: string): boolean {
|
|
78
|
+
return content.trim().startsWith("/")
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function handleCommand(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
82
|
+
cmdCtx.pendingSelections.delete(ctx.userId)
|
|
83
|
+
|
|
84
|
+
const parsed = parseCommand(ctx.content)
|
|
85
|
+
if (!parsed) {
|
|
86
|
+
return "不是有效命令"
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
switch (parsed.name) {
|
|
90
|
+
case "new":
|
|
91
|
+
return handleNew(ctx, cmdCtx)
|
|
92
|
+
case "stop":
|
|
93
|
+
case "cancel": // /cancel 与 /stop 同义(用户自然习惯 + pi-qqbot 同款,UAT S3 实证缺口)
|
|
94
|
+
return handleStop(ctx, cmdCtx)
|
|
95
|
+
case "queue":
|
|
96
|
+
return handleQueue(ctx, parsed.args, cmdCtx)
|
|
97
|
+
case "progress":
|
|
98
|
+
return cmdCtx.progress.query(ctx.userId)
|
|
99
|
+
case "status":
|
|
100
|
+
return handleStatus(ctx, cmdCtx)
|
|
101
|
+
case "sessions":
|
|
102
|
+
return handleSessions(ctx, cmdCtx)
|
|
103
|
+
case "help":
|
|
104
|
+
return buildHelpText()
|
|
105
|
+
case "model":
|
|
106
|
+
return handleModel(ctx, parsed.args, cmdCtx)
|
|
107
|
+
case "agent":
|
|
108
|
+
return handleAgent(ctx, parsed.args, cmdCtx)
|
|
109
|
+
case "rename":
|
|
110
|
+
return handleRename(ctx, parsed.args, cmdCtx)
|
|
111
|
+
case "history":
|
|
112
|
+
return handleHistory(ctx, parsed.args, cmdCtx)
|
|
113
|
+
case "watch":
|
|
114
|
+
return handleWatch(ctx, parsed.args, cmdCtx)
|
|
115
|
+
case "send":
|
|
116
|
+
return handleSend(ctx, parsed.args, cmdCtx)
|
|
117
|
+
case "mirror":
|
|
118
|
+
return handleMirror(parsed.args, cmdCtx)
|
|
119
|
+
default:
|
|
120
|
+
return `不支持的命令:/${parsed.name}\n发送 /help 查看可用命令`
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function handlePendingSelection(
|
|
125
|
+
userId: string,
|
|
126
|
+
selection: number,
|
|
127
|
+
cmdCtx: CommandContext,
|
|
128
|
+
): Promise<string | null> {
|
|
129
|
+
const pending = cmdCtx.pendingSelections.get(userId)
|
|
130
|
+
if (!pending) {
|
|
131
|
+
return null
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (pending.expiresAt <= Date.now()) {
|
|
135
|
+
cmdCtx.pendingSelections.delete(userId)
|
|
136
|
+
return null
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const item = pending.items[selection - 1]
|
|
140
|
+
if (!item) {
|
|
141
|
+
return `序号无效,请回复 1-${pending.items.length}`
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
cmdCtx.pendingSelections.delete(userId)
|
|
145
|
+
|
|
146
|
+
if (pending.type === "session") {
|
|
147
|
+
cmdCtx.sessions.switchSession(userId, item.id, item.label)
|
|
148
|
+
return `已切换到会话:${item.label}`
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const model = splitModelId(item.id)
|
|
152
|
+
if (!model) {
|
|
153
|
+
return `模型项无效:${item.label}`
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
await ensureSession(userId, cmdCtx)
|
|
157
|
+
await cmdCtx.sessions.setModel(userId, model.providerId, model.modelId)
|
|
158
|
+
return `已切换模型:${item.label}`
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function parseCommand(content: string): ParsedCommand | null {
|
|
162
|
+
const trimmed = content.trim()
|
|
163
|
+
if (!trimmed.startsWith("/")) {
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const [rawName, ...rest] = trimmed.slice(1).split(/\s+/)
|
|
168
|
+
const name = rawName?.toLowerCase()
|
|
169
|
+
if (!name) {
|
|
170
|
+
return null
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
name,
|
|
175
|
+
args: rest.join(" ").trim(),
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function handleNew(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
180
|
+
const session = await cmdCtx.sessions.createNew(ctx.userId)
|
|
181
|
+
return [
|
|
182
|
+
"已创建新会话",
|
|
183
|
+
`标题:${session.title ?? "未命名会话"}`,
|
|
184
|
+
`ID:${session.sessionId}`,
|
|
185
|
+
].join("\n")
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 缺口 4:/send <路径>——把本地文件/图片发到 QQ(路径白名单内)
|
|
189
|
+
async function handleSend(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
190
|
+
const path = args.trim()
|
|
191
|
+
if (!path) {
|
|
192
|
+
return "用法:/send <本地文件路径>(仅允许白名单目录:QQ 图片缓存与 bot 工作目录)"
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const done = await sendLocalFile(ctx, path, { getToken: cmdCtx.getAccessToken })
|
|
196
|
+
return `📎 ${done}`
|
|
197
|
+
} catch (error) {
|
|
198
|
+
return `⚠️ 发送失败:${error instanceof Error ? error.message : error}`
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function handleStop(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
203
|
+
const session = cmdCtx.sessions.getSession(ctx.userId)
|
|
204
|
+
const cleared = cmdCtx.queue.clear(ctx.userId) // Q1:/cancel 同时清队("停"=全停)
|
|
205
|
+
const suffix = cleared > 0 ? `,队列中的 ${cleared} 条已清空` : ""
|
|
206
|
+
if (!session) {
|
|
207
|
+
return cleared > 0 ? `当前没有会话可停止${suffix}` : "当前还没有会话可停止"
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
await cmdCtx.client.session.abort({ path: { id: session.sessionId } })
|
|
211
|
+
return `已发送停止请求:${session.title ?? session.sessionId}${suffix}`
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Q1:/queue 查看与管理忙碌队列
|
|
215
|
+
function handleQueue(ctx: MessageContext, args: string, cmdCtx: CommandContext): string {
|
|
216
|
+
const sub = args.trim().split(/\s+/)[0]?.toLowerCase() ?? ""
|
|
217
|
+
if (sub === "clear") {
|
|
218
|
+
const n = cmdCtx.queue.clear(ctx.userId)
|
|
219
|
+
return n > 0 ? `已清空队列(${n} 条)` : "队列本来就是空的"
|
|
220
|
+
}
|
|
221
|
+
if (sub === "drop") {
|
|
222
|
+
const id = Number(args.trim().split(/\s+/)[1])
|
|
223
|
+
if (!Number.isInteger(id) || id <= 0) return "用法:/queue drop <编号>"
|
|
224
|
+
return cmdCtx.queue.drop(ctx.userId, id)
|
|
225
|
+
}
|
|
226
|
+
return cmdCtx.queue.list(ctx.userId)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function handleStatus(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
230
|
+
const session = cmdCtx.sessions.getSession(ctx.userId)
|
|
231
|
+
const { providerId, modelId } = cmdCtx.sessions.getModel(ctx.userId)
|
|
232
|
+
const agentId = cmdCtx.sessions.getAgent(ctx.userId)
|
|
233
|
+
|
|
234
|
+
const openCodeStatus = await getOpenCodeStatus(cmdCtx.client)
|
|
235
|
+
const qqStatus = await getQQStatus(cmdCtx)
|
|
236
|
+
|
|
237
|
+
return [
|
|
238
|
+
"OpenCode 状态",
|
|
239
|
+
`服务器:${openCodeStatus}`,
|
|
240
|
+
`QQ 鉴权:${qqStatus}`,
|
|
241
|
+
`会话:${session ? `${session.title ?? "未命名会话"} (${session.sessionId})` : "未创建"}`,
|
|
242
|
+
`模型:${providerId && modelId ? `${providerId} / ${modelId}` : "默认"}`,
|
|
243
|
+
`Agent:${agentId ?? "默认"}`,
|
|
244
|
+
].join("\n")
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function handleSessions(ctx: MessageContext, cmdCtx: CommandContext): Promise<string> {
|
|
248
|
+
const sessions = await listSessions(cmdCtx.client)
|
|
249
|
+
if (sessions.length === 0) {
|
|
250
|
+
return "当前没有可切换的历史会话"
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const currentSessionId = cmdCtx.sessions.getSession(ctx.userId)?.sessionId
|
|
254
|
+
cmdCtx.pendingSelections.set(ctx.userId, {
|
|
255
|
+
type: "session",
|
|
256
|
+
items: sessions.map((session) => ({ id: session.id, label: session.title })),
|
|
257
|
+
expiresAt: Date.now() + SELECTION_TTL_MS,
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
const lines = sessions.map((session, index) => {
|
|
261
|
+
const prefix = session.id === currentSessionId ? "[当前] " : ""
|
|
262
|
+
return `${index + 1}. ${prefix}${session.title}`
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
return ["会话列表:", ...lines, "回复序号切换会话(60 秒内有效)"].join("\n")
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function handleModel(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
269
|
+
if (!args) {
|
|
270
|
+
const models = await listModels(cmdCtx.client)
|
|
271
|
+
if (models.length === 0) {
|
|
272
|
+
return "当前没有可用模型"
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const current = cmdCtx.sessions.getModel(ctx.userId)
|
|
276
|
+
cmdCtx.pendingSelections.set(ctx.userId, {
|
|
277
|
+
type: "model",
|
|
278
|
+
items: models.map((model) => ({ id: model.id, label: model.label })),
|
|
279
|
+
expiresAt: Date.now() + SELECTION_TTL_MS,
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
const lines = models.map((model, index) => {
|
|
283
|
+
const isCurrent = current.providerId && current.modelId && `${current.providerId}/${current.modelId}` === model.id
|
|
284
|
+
return `${index + 1}. ${isCurrent ? "[当前] " : ""}${model.label}`
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
return ["可用模型:", ...lines, "回复序号或 /model <provider/model> 切换(60 秒内有效)"].join("\n")
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (/^\d+$/.test(args)) {
|
|
291
|
+
const result = await handlePendingSelection(ctx.userId, Number(args), cmdCtx)
|
|
292
|
+
return result ?? "没有待选择的模型列表,请先发送 /model"
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const model = splitModelId(args)
|
|
296
|
+
if (!model) {
|
|
297
|
+
return "模型格式不对,请使用 /model <provider/model>"
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
await ensureSession(ctx.userId, cmdCtx)
|
|
301
|
+
await cmdCtx.sessions.setModel(ctx.userId, model.providerId, model.modelId)
|
|
302
|
+
return `已切换模型:${model.providerId} / ${model.modelId}`
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async function handleAgent(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
306
|
+
const agents = await listAgents(cmdCtx.client)
|
|
307
|
+
if (!args) {
|
|
308
|
+
if (agents.length === 0) {
|
|
309
|
+
return "当前没有可用 Agent"
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const currentAgent = cmdCtx.sessions.getAgent(ctx.userId)
|
|
313
|
+
const lines = agents.map((agent, index) => {
|
|
314
|
+
const isCurrent = currentAgent === agent.id
|
|
315
|
+
return `${index + 1}. ${isCurrent ? "[当前] " : ""}${agent.label}`
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
return ["可用 Agent:", ...lines, "回复 /agent <name> 切换"].join("\n")
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const normalized = args.trim().toLowerCase()
|
|
322
|
+
const matched = agents.find((agent) => agent.id.toLowerCase() === normalized)
|
|
323
|
+
if (!matched) {
|
|
324
|
+
return `未找到 Agent:${args}`
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
await ensureSession(ctx.userId, cmdCtx)
|
|
328
|
+
await cmdCtx.sessions.setAgent(ctx.userId, matched.id)
|
|
329
|
+
return `已切换 Agent:${matched.id}`
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async function handleRename(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
333
|
+
const title = args.trim()
|
|
334
|
+
if (!title) {
|
|
335
|
+
return "用法:/rename <新名称>"
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const session = cmdCtx.sessions.getSession(ctx.userId)
|
|
339
|
+
if (!session) {
|
|
340
|
+
return "当前还没有会话可重命名"
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
cmdCtx.sessions.switchSession(ctx.userId, session.sessionId, title)
|
|
344
|
+
return `已重命名当前会话:${title}`
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async function handleHistory(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
348
|
+
const session = cmdCtx.sessions.getSession(ctx.userId)
|
|
349
|
+
if (!session) {
|
|
350
|
+
return "当前还没有会话"
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const n = /^\d+$/.test(args.trim()) ? Math.min(Math.max(Number(args.trim()), 1), 10) : 3
|
|
354
|
+
const res = await cmdCtx.client.session.messages({ path: { id: session.sessionId } })
|
|
355
|
+
const messages: SessionMessage[] = res?.data ?? []
|
|
356
|
+
const body = renderHistory(messages, n)
|
|
357
|
+
if (!body) {
|
|
358
|
+
return "该会话暂无历史消息"
|
|
359
|
+
}
|
|
360
|
+
return `📜 ${session.title ?? session.sessionId} · 最近 ${n} 轮\n\n${body}`
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function handleWatch(ctx: MessageContext, args: string, cmdCtx: CommandContext): Promise<string> {
|
|
364
|
+
const a = args.trim()
|
|
365
|
+
const lower = a.toLowerCase()
|
|
366
|
+
|
|
367
|
+
if (lower === "off" || lower === "stop") {
|
|
368
|
+
return cmdCtx.watch.stop(ctx.userId)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (!a) {
|
|
372
|
+
const session = await cmdCtx.sessions.getOrCreate(ctx.userId)
|
|
373
|
+
return cmdCtx.watch.start(ctx.userId, session.sessionId, ctx)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (/^ses_/i.test(a)) {
|
|
377
|
+
return cmdCtx.watch.start(ctx.userId, a, ctx)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return "用法:/watch 观察当前会话|/watch <会话ID> 观察指定会话|/watch off 停止观察"
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function handleMirror(args: string, cmdCtx: CommandContext): string {
|
|
384
|
+
const a = args.trim().toLowerCase()
|
|
385
|
+
if (!a) return cmdCtx.mirror.status()
|
|
386
|
+
if (["on", "1", "开", "打开"].includes(a)) return cmdCtx.mirror.set(true)
|
|
387
|
+
if (["off", "0", "关", "关闭"].includes(a)) return cmdCtx.mirror.set(false)
|
|
388
|
+
return "用法:/mirror 查看状态|/mirror on 开启|/mirror off 关闭"
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
async function ensureSession(userId: string, cmdCtx: CommandContext): Promise<void> {
|
|
392
|
+
await cmdCtx.sessions.getOrCreate(userId)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async function getOpenCodeStatus(client: OpencodeClient): Promise<string> {
|
|
396
|
+
try {
|
|
397
|
+
await client.session.list()
|
|
398
|
+
return "运行中"
|
|
399
|
+
} catch (error) {
|
|
400
|
+
return `异常(${toErrorMessage(error)})`
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async function getQQStatus(cmdCtx: CommandContext): Promise<string> {
|
|
405
|
+
try {
|
|
406
|
+
await cmdCtx.getAccessToken()
|
|
407
|
+
return "正常"
|
|
408
|
+
} catch (error) {
|
|
409
|
+
return `异常(${toErrorMessage(error)})`
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async function listSessions(client: OpencodeClient): Promise<ListedSession[]> {
|
|
414
|
+
const result = await client.session.list()
|
|
415
|
+
const rawItems = extractArray(result)
|
|
416
|
+
const items = rawItems.length > 0 ? rawItems : extractArray(extractProperty(result, "data"))
|
|
417
|
+
|
|
418
|
+
return items
|
|
419
|
+
.map((item) => {
|
|
420
|
+
const id = getString(item, "id")
|
|
421
|
+
if (!id) {
|
|
422
|
+
return null
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
id,
|
|
427
|
+
title: getString(item, "title") ?? id,
|
|
428
|
+
}
|
|
429
|
+
})
|
|
430
|
+
.filter((item): item is ListedSession => item !== null)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async function listModels(client: OpencodeClient): Promise<ListedModel[]> {
|
|
434
|
+
const configApi = extractProperty(client, "config")
|
|
435
|
+
const providersFn = typeof configApi === "object" && configApi !== null ? Reflect.get(configApi, "providers") : undefined
|
|
436
|
+
const response = typeof providersFn === "function" ? await Promise.resolve(providersFn.call(configApi)) : []
|
|
437
|
+
|
|
438
|
+
const providers = extractArray(response)
|
|
439
|
+
const resolvedProviders = providers.length > 0 ? providers : extractArray(extractProperty(response, "data"))
|
|
440
|
+
const models: ListedModel[] = []
|
|
441
|
+
|
|
442
|
+
for (const provider of resolvedProviders) {
|
|
443
|
+
const providerId = getString(provider, "id") ?? getString(provider, "providerID")
|
|
444
|
+
const rawModels = extractArray(extractProperty(provider, "models"))
|
|
445
|
+
for (const model of rawModels) {
|
|
446
|
+
const modelId = getString(model, "id") ?? getString(model, "modelID")
|
|
447
|
+
if (!providerId || !modelId) {
|
|
448
|
+
continue
|
|
449
|
+
}
|
|
450
|
+
models.push({
|
|
451
|
+
id: `${providerId}/${modelId}`,
|
|
452
|
+
label: `${providerId} / ${modelId}`,
|
|
453
|
+
})
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return models
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async function listAgents(client: OpencodeClient): Promise<ListedAgent[]> {
|
|
461
|
+
const response = await client.app.agents()
|
|
462
|
+
const rawAgents = extractArray(response)
|
|
463
|
+
const agents = rawAgents.length > 0 ? rawAgents : extractArray(extractProperty(response, "data"))
|
|
464
|
+
|
|
465
|
+
return agents
|
|
466
|
+
.map((agent) => {
|
|
467
|
+
const id = getString(agent, "id") ?? getString(agent, "name")
|
|
468
|
+
if (!id) {
|
|
469
|
+
return null
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const description = getString(agent, "description")
|
|
473
|
+
return {
|
|
474
|
+
id,
|
|
475
|
+
label: description ? `${id} - ${description}` : id,
|
|
476
|
+
}
|
|
477
|
+
})
|
|
478
|
+
.filter((agent): agent is ListedAgent => agent !== null)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function splitModelId(value: string): { providerId: string; modelId: string } | null {
|
|
482
|
+
const trimmed = value.trim()
|
|
483
|
+
const slashIndex = trimmed.indexOf("/")
|
|
484
|
+
if (slashIndex <= 0 || slashIndex === trimmed.length - 1) {
|
|
485
|
+
return null
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const providerId = trimmed.slice(0, slashIndex).trim()
|
|
489
|
+
const modelId = trimmed.slice(slashIndex + 1).trim()
|
|
490
|
+
if (!providerId || !modelId) {
|
|
491
|
+
return null
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return { providerId, modelId }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export function buildHelpText(): string {
|
|
498
|
+
return [
|
|
499
|
+
"可用命令:",
|
|
500
|
+
"/new - 创建新会话",
|
|
501
|
+
"/stop - 停止当前 AI 运行",
|
|
502
|
+
"/status - 查看服务器和当前会话状态",
|
|
503
|
+
"/sessions - 列出历史会话并回复序号切换",
|
|
504
|
+
"/help - 查看帮助",
|
|
505
|
+
"/model - 列出可用模型",
|
|
506
|
+
"/model <provider/model> - 切换模型",
|
|
507
|
+
"/agent - 列出可用 Agent",
|
|
508
|
+
"/agent <name> - 切换 Agent",
|
|
509
|
+
"/rename <name> - 重命名当前会话",
|
|
510
|
+
"/history [N] - 查看当前会话最近 N 轮历史(默认 3,最多 10)",
|
|
511
|
+
"/send <路径> - 把白名单目录内的文件/图片发到 QQ",
|
|
512
|
+
"/watch - 观察当前会话实时过程(工具调用/完成会推送摘要)",
|
|
513
|
+
"/watch <会话ID> - 观察指定会话",
|
|
514
|
+
"/watch off - 停止观察",
|
|
515
|
+
"/progress - 手动进度查询(工具调用/失败统计、在途状态,本地事件源零模型调用)",
|
|
516
|
+
"/queue - 忙碌队列查看;/queue clear 清空;/queue drop <编号> 定点删除",
|
|
517
|
+
"/cancel - 同 /stop:中止当前 AI 运行并清空忙碌队列",
|
|
518
|
+
"/mirror [on|off] - 桌面问答摘要镜像开关(会话轮次完成推送问答摘要)",
|
|
519
|
+
"",
|
|
520
|
+
"权限审批(AI 请求权限时):",
|
|
521
|
+
"回复「允许」= 本次允许|「总是」= 总是允许|「拒绝」= 拒绝",
|
|
522
|
+
"也可用 /allow /always /deny(30 分钟无响应自动拒绝)",
|
|
523
|
+
].join("\n")
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function extractArray(value: unknown): Record<string, unknown>[] {
|
|
527
|
+
if (!Array.isArray(value)) {
|
|
528
|
+
return []
|
|
529
|
+
}
|
|
530
|
+
return value.filter(isRecord)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function extractProperty(value: unknown, key: string): unknown {
|
|
534
|
+
if (!isRecord(value)) {
|
|
535
|
+
return undefined
|
|
536
|
+
}
|
|
537
|
+
return value[key]
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function getString(value: unknown, key: string): string | undefined {
|
|
541
|
+
if (!isRecord(value)) {
|
|
542
|
+
return undefined
|
|
543
|
+
}
|
|
544
|
+
const resolved = value[key]
|
|
545
|
+
return typeof resolved === "string" && resolved.trim() ? resolved : undefined
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
549
|
+
return typeof value === "object" && value !== null
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function toErrorMessage(error: unknown): string {
|
|
553
|
+
return error instanceof Error ? error.message : String(error)
|
|
554
|
+
}
|