dsh-zcode-startplan 0.4.5 → 1.0.2
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/CHANGELOG.md +1963 -405
- package/README.md +88 -405
- package/bin/cli.mjs +509 -450
- package/client.js +2459 -716
- package/cordis.patch.yml +4 -2
- package/index.js +144 -14
- package/lib/adapter.js +476 -422
- package/lib/anthropic-body.js +513 -469
- package/lib/assemble.js +808 -420
- package/lib/auto-claim.js +229 -0
- package/lib/captcha/minter.js +61 -7
- package/lib/distribution-keys.js +202 -0
- package/lib/errors.js +281 -125
- package/lib/fallback.js +120 -0
- package/lib/local-endpoint.js +367 -352
- package/lib/manage-routes.js +1245 -489
- package/lib/model-catalog.js +57 -0
- package/lib/openai-gateway.js +776 -0
- package/lib/plan-client.js +942 -532
- package/lib/pool.js +771 -478
- package/lib/sse.js +429 -317
- package/lib/upstream-api.js +948 -570
- package/lib/upstream.js +294 -134
- package/lib/version-probe.js +110 -0
- package/package.json +84 -80
- package/scripts/analyze-grant-intervals.mjs +171 -0
- package/scripts/check-compat-rc3.mjs +21 -11
- package/scripts/debug-claimable.mjs +130 -0
- package/scripts/probe-marketing.mjs +203 -0
- package/scripts/restart-dsh.ps1 +119 -76
- package/scripts/test-claimed-pending.mjs +152 -0
- package/scripts/verify_compat.mjs +75 -64
- package/scripts/watch-marketing.mjs +155 -0
- package/tests/anthropic-body.test.mjs +305 -0
- package/tests/assembler-contract.test.mjs +256 -0
- package/tests/auto-claim.test.mjs +318 -0
- package/tests/claim-routes.test.mjs +438 -0
- package/tests/client-panel.test.mjs +1141 -0
- package/tests/distribution-keys.test.mjs +123 -0
- package/tests/e2e.test.mjs +388 -0
- package/tests/fallback-scrub.test.mjs +105 -0
- package/tests/fixtures/quota-200.json +1 -0
- package/tests/launch-command.test.mjs +68 -0
- package/tests/login-channel.test.mjs +102 -0
- package/tests/openai-gateway.test.mjs +454 -0
- package/tests/pinned-models.test.mjs +505 -0
- package/tests/plan-client.test.mjs +582 -0
- package/tests/pool.test.mjs +319 -0
- package/tests/quota-blackhole.test.mjs +409 -0
- package/tests/restart-script.test.mjs +53 -0
- package/tests/routing-quota.test.mjs +251 -0
- package/tests/sse.test.mjs +86 -0
- package/tests/stream-translator.test.mjs +137 -0
- package/tests/support/hanging-stream.mjs +48 -0
- package/tests/support/resolve-profile-deps.mjs +52 -0
- package/tests/upstream-error-200.test.mjs +164 -0
- package/tests/upstream.test.mjs +330 -0
- package/tests/version-probe.test.mjs +172 -0
package/client.js
CHANGED
|
@@ -1,716 +1,2459 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Client 模块:官方插件详情页(Plugin Manager → ZCode Start Plan)里的
|
|
3
|
-
* 「账号池管理」面板。
|
|
4
|
-
*
|
|
5
|
-
* ## 挂载点(照抄 dsh-workbuddy-connect 的实证写法)
|
|
6
|
-
*
|
|
7
|
-
* `plugins.bundle.config` 是官方插件页按**包名 key 派发**的配置区块座位,
|
|
8
|
-
* 渲染在详情页「包含的组件」列表上方;宿主只在本包的详情页派发
|
|
9
|
-
* `key === 'dsh-zcode-startplan'` 的条目,列表页则以 `view === 'summary'`
|
|
10
|
-
* 请求一行简介。组件无需自己做 subject 过滤。
|
|
11
|
-
*
|
|
12
|
-
* ## 数据通道
|
|
13
|
-
*
|
|
14
|
-
* 面板与 Host 之间走同源 fetch:宿主 webserver 上的
|
|
15
|
-
* `/api/zcode-start-plan/*` 路由(`lib/manage-routes.js`,仅 loopback)。
|
|
16
|
-
* 凭据永不外发 —— Host 侧已做脱敏;设备授权的 secret 也留在 Host。
|
|
17
|
-
*
|
|
18
|
-
* ## 实现约束(与宿主一致)
|
|
19
|
-
*
|
|
20
|
-
* - React 来自浏览器模块表(`require('react')`),不自带、不走 CDN;
|
|
21
|
-
* - 样式用宿主主题 token(`--dsw-alias-*`),以组件局部 `<style>` 注入,
|
|
22
|
-
* 卸载即移除;
|
|
23
|
-
* - 工厂无副作用;slot 注册在 `ctx.slots.inject(ownerKey, cb)` 里完成,
|
|
24
|
-
* 宿主负责其生命周期。
|
|
25
|
-
*/
|
|
26
|
-
window.__ModuleLoader__.load({
|
|
27
|
-
id: 'dsh-zcode-startplan',
|
|
28
|
-
factory(require) {
|
|
29
|
-
const React = require('react')
|
|
30
|
-
const h = React.createElement
|
|
31
|
-
const { useCallback, useEffect, useRef, useState } = React
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
.zsp-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
.zsp-
|
|
97
|
-
.zsp-
|
|
98
|
-
.zsp-
|
|
99
|
-
.zsp-
|
|
100
|
-
.zsp-
|
|
101
|
-
.zsp-
|
|
102
|
-
.zsp-
|
|
103
|
-
.zsp-
|
|
104
|
-
.zsp-
|
|
105
|
-
.zsp-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
typeof
|
|
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
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
-
|
|
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
|
-
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*/
|
|
338
|
-
function
|
|
339
|
-
const {
|
|
340
|
-
const
|
|
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
|
-
const
|
|
429
|
-
const [
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
const
|
|
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
|
-
const
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
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
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
{ className: 'zsp-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
{ className: 'zsp-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Client 模块:官方插件详情页(Plugin Manager → ZCode Start Plan)里的
|
|
3
|
+
* 「账号池管理」面板。
|
|
4
|
+
*
|
|
5
|
+
* ## 挂载点(照抄 dsh-workbuddy-connect 的实证写法)
|
|
6
|
+
*
|
|
7
|
+
* `plugins.bundle.config` 是官方插件页按**包名 key 派发**的配置区块座位,
|
|
8
|
+
* 渲染在详情页「包含的组件」列表上方;宿主只在本包的详情页派发
|
|
9
|
+
* `key === 'dsh-zcode-startplan'` 的条目,列表页则以 `view === 'summary'`
|
|
10
|
+
* 请求一行简介。组件无需自己做 subject 过滤。
|
|
11
|
+
*
|
|
12
|
+
* ## 数据通道
|
|
13
|
+
*
|
|
14
|
+
* 面板与 Host 之间走同源 fetch:宿主 webserver 上的
|
|
15
|
+
* `/api/zcode-start-plan/*` 路由(`lib/manage-routes.js`,仅 loopback)。
|
|
16
|
+
* 凭据永不外发 —— Host 侧已做脱敏;设备授权的 secret 也留在 Host。
|
|
17
|
+
*
|
|
18
|
+
* ## 实现约束(与宿主一致)
|
|
19
|
+
*
|
|
20
|
+
* - React 来自浏览器模块表(`require('react')`),不自带、不走 CDN;
|
|
21
|
+
* - 样式用宿主主题 token(`--dsw-alias-*`),以组件局部 `<style>` 注入,
|
|
22
|
+
* 卸载即移除;
|
|
23
|
+
* - 工厂无副作用;slot 注册在 `ctx.slots.inject(ownerKey, cb)` 里完成,
|
|
24
|
+
* 宿主负责其生命周期。
|
|
25
|
+
*/
|
|
26
|
+
window.__ModuleLoader__.load({
|
|
27
|
+
id: 'dsh-zcode-startplan',
|
|
28
|
+
factory(require) {
|
|
29
|
+
const React = require('react')
|
|
30
|
+
const h = React.createElement
|
|
31
|
+
const { useCallback, useEffect, useRef, useState } = React
|
|
32
|
+
/** React 18 才有;宿主版本都满足,但缺失时降级成普通 useState 快照。 */
|
|
33
|
+
const useSyncExternalStore = React.useSyncExternalStore
|
|
34
|
+
?? ((subscribe, getSnapshot) => {
|
|
35
|
+
const [value, setValue] = useState(getSnapshot)
|
|
36
|
+
useEffect(() => subscribe(() => setValue(getSnapshot())), [subscribe, getSnapshot])
|
|
37
|
+
return value
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const SLOT = 'plugins.bundle.config'
|
|
41
|
+
/** 宿主按包名派发此座位(workbuddy 实证:key 必须与安装的包名一字不差)。 */
|
|
42
|
+
const BUNDLE_KEY = 'dsh-zcode-startplan'
|
|
43
|
+
/**
|
|
44
|
+
* 模型栏旁「当前使用账号」的座位(1.0.0)。
|
|
45
|
+
*
|
|
46
|
+
* ⚠ **必须是 `conversation.input.right`**:它是 composer 注册表里**已声明**的
|
|
47
|
+
* `{ kind: 'list', scope: 'session' }` 槽,且在 `standardControls` 这一行里
|
|
48
|
+
* 就渲染在模型座**正前方** ——
|
|
49
|
+
* `renderSlot("conversation.input.right", {}), renderSlot("conversation.input.model", …)`
|
|
50
|
+
* 视觉上就是「模型列表旁边」。workbuddy 的积分徽标用的也是这个座位。
|
|
51
|
+
*
|
|
52
|
+
* 反例(0.9.9 曾误用,导致整个面板白掉):模型座的 `.label` 子座位只被
|
|
53
|
+
* model-selection 包 `renderSlot()` 调用,**没有任何包声明它** ——
|
|
54
|
+
* 向未声明的槽注册会让整个 client bundle 的 `apply` 抛错,连带把配置页带走。
|
|
55
|
+
*/
|
|
56
|
+
const ROUTE_SLOT = 'conversation.input.right'
|
|
57
|
+
/**
|
|
58
|
+
* 本插件拥有的 provider id 前缀。账号栏位是 `<provider>-acct-<账号id>`,
|
|
59
|
+
* 统一栏位就是 `<provider>` —— 都以它为前缀,故用 startsWith 判定。
|
|
60
|
+
*/
|
|
61
|
+
const PROVIDER_PREFIX = 'zcode-start-plan'
|
|
62
|
+
/** 轮询间隔:路由是进程内状态,没有推送通道(对齐 workbuddy 的 60s 节奏,路由变化快所以更密)。 */
|
|
63
|
+
const ROUTING_POLL_MS = 2000
|
|
64
|
+
const API = '/api/zcode-start-plan'
|
|
65
|
+
const CONFIRM_RESET_MS = 3000
|
|
66
|
+
|
|
67
|
+
/** 降级时返回的空 disposer(slot 运行时总是期望拿到一个)。 */
|
|
68
|
+
const NOOP_DISPOSER = () => {}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 跑一个 browser-side 贡献,失败降级成一条 console.error(workbuddy 同款)。
|
|
72
|
+
*
|
|
73
|
+
* ⚠ 这是 0.9.9 事故的正面防御:任何一次 `slots.register` 抛错都会顺着
|
|
74
|
+
* `apply` 冒泡进 DSH loader,把**整个** client bundle 的 apply 打断 ——
|
|
75
|
+
* 配置页随之白掉。逐个贡献包一层,坏一个不影响其余的。
|
|
76
|
+
*/
|
|
77
|
+
function guardContribution(label, run) {
|
|
78
|
+
try {
|
|
79
|
+
return run()
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error(
|
|
82
|
+
`[dsh-zcode-startplan] client contribution failed (${label}):`,
|
|
83
|
+
error,
|
|
84
|
+
)
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const CSS = `
|
|
90
|
+
.zsp-panel{display:flex;flex-direction:column;gap:10px;width:100%;min-width:0;color:var(--dsw-alias-label-primary);font-size:12.5px;line-height:18px}
|
|
91
|
+
.zsp-head{display:flex;flex-wrap:wrap;align-items:center;gap:8px}
|
|
92
|
+
.zsp-title{margin:0;font-size:13px;font-weight:600;line-height:20px}
|
|
93
|
+
.zsp-muted{color:var(--dsw-alias-label-tertiary);font-size:12px;word-break:break-word;min-width:0}
|
|
94
|
+
.zsp-spacer{flex:1}
|
|
95
|
+
.zsp-btn{font:inherit;cursor:pointer;border:1px solid var(--dsw-alias-border-l3);border-radius:var(--dsw-radius-sm,6px);color:var(--dsw-alias-label-primary);background:transparent;padding:3px 10px;font-size:12px;line-height:18px;flex:none}
|
|
96
|
+
.zsp-btn:hover:enabled{background:var(--dsw-alias-interactive-bg-hover)}
|
|
97
|
+
.zsp-btn:disabled{cursor:default;opacity:.55}
|
|
98
|
+
.zsp-btn:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}
|
|
99
|
+
.zsp-btn[data-danger=true]{color:var(--dsw-alias-label-danger)}
|
|
100
|
+
.zsp-error{color:var(--dsw-alias-label-danger);margin:0;font-size:12px;word-break:break-word}
|
|
101
|
+
.zsp-ok{color:var(--dsw-alias-state-success-primary);font-size:12px;word-break:break-word;margin:0}
|
|
102
|
+
.zsp-card{display:flex;flex-direction:column;gap:6px;border:1px solid var(--dsw-alias-border-l1);background:var(--dsw-alias-bg-layer-1);border-radius:8px;padding:10px 12px;min-width:0}
|
|
103
|
+
.zsp-card[data-hidden=true]{opacity:.62}
|
|
104
|
+
.zsp-group{display:flex;flex-direction:column;gap:8px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;padding:10px 12px;min-width:0}
|
|
105
|
+
.zsp-row{display:flex;flex-wrap:wrap;align-items:center;gap:8px;min-width:0}
|
|
106
|
+
.zsp-dot{width:7px;height:7px;border-radius:50%;background:var(--dsw-alias-label-tertiary);flex:none}
|
|
107
|
+
.zsp-dot[data-on=true]{background:var(--dsw-alias-state-success-primary)}
|
|
108
|
+
.zsp-name{font-weight:600}
|
|
109
|
+
.zsp-name-edit{font:inherit;font-weight:600;color:inherit;background:transparent;border:0;padding:0;cursor:text;text-align:left}
|
|
110
|
+
.zsp-name-edit:hover:enabled{text-decoration:underline dotted var(--dsw-alias-label-tertiary)}
|
|
111
|
+
.zsp-name-edit:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}
|
|
112
|
+
.zsp-name-input{font:inherit;font-weight:600;color:var(--dsw-alias-label-primary);background:var(--dsw-alias-bg-layer-2);border:1px solid var(--dsw-alias-state-business-primary);border-radius:4px;padding:1px 6px;width:180px;max-width:100%}
|
|
113
|
+
.zsp-name-input:focus-visible{outline:none}
|
|
114
|
+
.zsp-chip{display:inline-flex;align-items:center;gap:4px;border:1px solid var(--dsw-alias-border-l1);border-radius:999px;padding:1px 8px;font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary)}
|
|
115
|
+
/* 探测到的 plan_id:只读展示(没有手输路径,见 ClaimSection 渲染处注释)。 */
|
|
116
|
+
.zsp-plan-id{font-family:var(--dsw-font-mono,ui-monospace,monospace);font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary);background:var(--dsw-alias-bg-layer-2);border-radius:4px;padding:1px 6px;user-select:all}
|
|
117
|
+
/* 重启指引块:说明 + 可复制的启动命令。 */
|
|
118
|
+
.zsp-restart-guide{display:flex;flex-direction:column;gap:4px;min-width:0}
|
|
119
|
+
.zsp-restart-guide .zsp-plan-id{font-size:12px;line-height:20px;align-self:flex-start;max-width:100%;white-space:pre-wrap;word-break:break-all}
|
|
120
|
+
.zsp-chip[data-limited=true]{color:var(--dsw-alias-label-danger);border-color:var(--dsw-alias-label-danger)}
|
|
121
|
+
/* 风控痕迹(1.0.2):橙黄提示,与「额度用尽」的红色刻意区分 —— 风控不是没额度。 */
|
|
122
|
+
.zsp-chip[data-risk=true]{color:var(--dsw-alias-label-warning,#b8860b);border-color:var(--dsw-alias-label-warning,#b8860b);cursor:pointer;background:transparent;font:inherit;font-size:11px;line-height:16px}
|
|
123
|
+
.zsp-chip[data-risk=true]:hover{background:var(--dsw-alias-fill-secondary,rgba(127,127,127,.18))}
|
|
124
|
+
.zsp-chip[data-risk=true]:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}
|
|
125
|
+
.zsp-chip[data-risk=true]:disabled{cursor:default;opacity:.55}
|
|
126
|
+
.zsp-chip-x{display:inline-flex;align-items:center;justify-content:center;width:14px;height:14px;margin:0 -3px 0 0;padding:0;border:0;border-radius:999px;background:transparent;color:inherit;font-size:11px;line-height:1;cursor:pointer}
|
|
127
|
+
.zsp-chip-x:hover{background:var(--dsw-alias-fill-secondary,rgba(127,127,127,.18))}
|
|
128
|
+
.zsp-chip-x:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}
|
|
129
|
+
.zsp-chip-x:disabled{cursor:default;opacity:.5}
|
|
130
|
+
.zsp-bar{height:6px;border-radius:3px;background:var(--dsw-alias-bg-layer-2);overflow:hidden;width:100%}
|
|
131
|
+
.zsp-bar>span{display:block;height:100%;background:var(--dsw-alias-state-business-primary)}
|
|
132
|
+
/**
|
|
133
|
+
* 未生效额度:同一条进度条,但**用条纹 + 半透明**区分「还没到点的钱」,
|
|
134
|
+
* 避免与已生效的满格条混淆(满格实色 = 现在就能用)。
|
|
135
|
+
*/
|
|
136
|
+
.zsp-meter{display:flex;flex-direction:column;gap:4px;min-width:0}
|
|
137
|
+
.zsp-meter-caption{margin:0}
|
|
138
|
+
.zsp-bar[data-pending=true]{background:repeating-linear-gradient(45deg,var(--dsw-alias-bg-layer-2) 0 4px,var(--dsw-alias-border-l1) 4px 8px)}
|
|
139
|
+
.zsp-bar[data-pending=true]>span{background:var(--dsw-alias-state-business-primary);opacity:.45}
|
|
140
|
+
.zsp-actions{display:flex;flex-wrap:wrap;gap:6px;margin-top:2px}
|
|
141
|
+
.zsp-add{display:flex;flex-direction:column;gap:8px;border:1px dashed var(--dsw-alias-border-l2);border-radius:8px;padding:10px 12px;min-width:0}
|
|
142
|
+
.zsp-link{color:var(--dsw-alias-state-business-primary);word-break:break-all}
|
|
143
|
+
.zsp-select{position:relative;display:inline-flex;min-width:0}
|
|
144
|
+
.zsp-select-trigger{display:inline-flex;align-items:center;gap:8px;font:inherit;cursor:pointer;border:1px solid var(--dsw-alias-border-l3);border-radius:var(--dsw-radius-sm,6px);color:var(--dsw-alias-label-primary);background:var(--dsw-alias-bg-layer-1);padding:4px 10px;font-size:12px;line-height:18px;min-width:0}
|
|
145
|
+
.zsp-select-trigger:hover{background:var(--dsw-alias-interactive-bg-hover)}
|
|
146
|
+
.zsp-select-trigger:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}
|
|
147
|
+
.zsp-select-trigger[aria-expanded=true]{border-color:var(--dsw-alias-state-business-primary)}
|
|
148
|
+
.zsp-select-label{font-weight:600;white-space:nowrap}
|
|
149
|
+
.zsp-select-desc{color:var(--dsw-alias-label-tertiary);font-size:11px;white-space:nowrap}
|
|
150
|
+
.zsp-caret{flex:none;width:0;height:0;border-left:4px solid transparent;border-right:4px solid transparent;border-top:5px solid var(--dsw-alias-label-tertiary);transition:transform .15s ease}
|
|
151
|
+
.zsp-select-trigger[aria-expanded=true] .zsp-caret{transform:rotate(180deg)}
|
|
152
|
+
.zsp-select-menu{position:absolute;top:calc(100% + 4px);left:0;z-index:30;display:flex;flex-direction:column;gap:2px;min-width:100%;max-height:260px;overflow:auto;padding:4px;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;background:var(--dsw-alias-bg-layer-2);box-shadow:0 8px 24px rgb(0 0 0 / 18%);animation:zsp-pop .14s ease}
|
|
153
|
+
@keyframes zsp-pop{from{opacity:0;transform:translateY(-3px)}to{opacity:1;transform:none}}
|
|
154
|
+
.zsp-option{display:flex;align-items:center;gap:8px;padding:6px 8px;border-radius:6px;cursor:pointer;color:var(--dsw-alias-label-primary);font-size:12px;line-height:18px;border:0;background:transparent;text-align:left;width:100%}
|
|
155
|
+
.zsp-option:hover,.zsp-option[data-active=true]{background:var(--dsw-alias-interactive-bg-hover)}
|
|
156
|
+
.zsp-option[aria-selected=true]{background:var(--dsw-alias-interactive-bg-selected,var(--dsw-alias-interactive-bg-hover))}
|
|
157
|
+
.zsp-option-check{flex:none;color:var(--dsw-alias-state-business-primary);font-size:12px;visibility:hidden}
|
|
158
|
+
.zsp-option[aria-selected=true] .zsp-option-check{visibility:visible}
|
|
159
|
+
.zsp-option-text{display:flex;flex-direction:column;min-width:0}
|
|
160
|
+
.zsp-badge{flex:none;display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;border-radius:5px;font-size:10px;font-weight:700;color:var(--dsw-alias-label-primary);background:var(--dsw-alias-interactive-bg-hover)}
|
|
161
|
+
.zsp-toggle-row{margin-left:auto;display:inline-flex;align-items:center;gap:6px;flex:none;color:var(--dsw-alias-label-tertiary);font-size:11px}
|
|
162
|
+
/**
|
|
163
|
+
* 三颗开关同列(不贴边):卡片开关留在卡片内的自然位置(距卡片边框
|
|
164
|
+
* 12px,缩进 1+12+1+12=26px);统一行比卡片少一层卡片边框+内边距
|
|
165
|
+
* (13px),补 13px 内边距对齐到同一条竖线。
|
|
166
|
+
*/
|
|
167
|
+
.zsp-unified{padding-right:13px}
|
|
168
|
+
/* ── API Keys 管理区(0.9.8 重构)────────────────────────────── */
|
|
169
|
+
.zsp-btn[data-primary=true]{background:var(--dsw-alias-state-business-primary);border-color:var(--dsw-alias-state-business-primary);color:#fff}
|
|
170
|
+
.zsp-btn[data-primary=true]:hover:enabled{opacity:.88}
|
|
171
|
+
/* 武装中的删除按钮:实底危险色,让「再点一次才删」的态势一眼可见。 */
|
|
172
|
+
.zsp-btn[data-confirm=true]{background:var(--dsw-alias-label-danger);border-color:var(--dsw-alias-label-danger);color:#fff}
|
|
173
|
+
/* 分段选择(限额预设)选中态:描边 + 主色文字,不抢实底按钮的权重。 */
|
|
174
|
+
.zsp-btn[data-selected=true]{border-color:var(--dsw-alias-state-business-primary);color:var(--dsw-alias-state-business-primary)}
|
|
175
|
+
.zsp-keycard{display:flex;flex-direction:column;gap:8px;border:1px solid var(--dsw-alias-border-l1);background:var(--dsw-alias-bg-layer-1);border-radius:8px;padding:10px 12px;min-width:0}
|
|
176
|
+
.zsp-keycard[data-disabled=true]{opacity:.62}
|
|
177
|
+
.zsp-keyhead{display:flex;flex-wrap:wrap;align-items:center;gap:8px;min-width:0}
|
|
178
|
+
.zsp-keydate{color:var(--dsw-alias-label-tertiary);font-size:11px;flex:none}
|
|
179
|
+
.zsp-meter-block{display:flex;flex-direction:column;gap:3px;min-width:0}
|
|
180
|
+
/* 用量 ≥90%:进度条转危险色 —— 「快用完」要在扫一眼就看到。 */
|
|
181
|
+
.zsp-bar[data-hot=true]>span{background:var(--dsw-alias-label-danger)}
|
|
182
|
+
/* 新 key 明文 token 的强调卡:成功色描边,明文等宽可全选。 */
|
|
183
|
+
.zsp-token-card{display:flex;flex-direction:column;gap:6px;border:1px solid var(--dsw-alias-state-success-primary);border-radius:8px;background:var(--dsw-alias-bg-layer-1);padding:10px 12px;min-width:0}
|
|
184
|
+
.zsp-token-value{font-family:var(--dsw-font-mono,ui-monospace,monospace);font-size:12px;line-height:18px;user-select:all;word-break:break-all;background:var(--dsw-alias-bg-layer-2);border-radius:4px;padding:3px 8px}
|
|
185
|
+
.zsp-empty{display:flex;flex-direction:column;gap:8px;align-items:flex-start;border:1px dashed var(--dsw-alias-border-l2);border-radius:8px;padding:14px 12px}
|
|
186
|
+
.zsp-quotafield{display:flex;flex-wrap:wrap;gap:6px;align-items:center;min-width:0}
|
|
187
|
+
.zsp-quota-input{font:inherit;color:var(--dsw-alias-label-primary);background:var(--dsw-alias-bg-layer-2);border:1px solid var(--dsw-alias-border-l2);border-radius:6px;padding:2px 8px;width:130px}
|
|
188
|
+
.zsp-quota-input:focus-visible{outline:none;border-color:var(--dsw-alias-state-business-primary)}
|
|
189
|
+
.zsp-count{display:inline-flex;align-items:center;border:1px solid var(--dsw-alias-border-l1);border-radius:999px;padding:0 8px;font-size:11px;line-height:16px;color:var(--dsw-alias-label-tertiary);flex:none}
|
|
190
|
+
.zsp-toggle{position:relative;display:inline-block;flex:none;width:30px;height:17px;margin-left:auto}
|
|
191
|
+
.zsp-toggle input{position:absolute;opacity:0;width:100%;height:100%;margin:0;cursor:pointer}
|
|
192
|
+
.zsp-toggle-track{position:absolute;inset:0;border-radius:999px;background:var(--dsw-alias-bg-layer-2);border:1px solid var(--dsw-alias-border-l2);transition:background .15s ease,border-color .15s ease;pointer-events:none}
|
|
193
|
+
.zsp-toggle-track::after{content:'';position:absolute;top:1px;left:1px;width:13px;height:13px;border-radius:50%;background:var(--dsw-alias-label-primary);transition:transform .15s ease}
|
|
194
|
+
.zsp-toggle input:checked + .zsp-toggle-track{background:var(--dsw-alias-state-business-primary);border-color:var(--dsw-alias-state-business-primary)}
|
|
195
|
+
.zsp-toggle input:checked + .zsp-toggle-track::after{transform:translateX(13px);background:#fff}
|
|
196
|
+
.zsp-toggle input:focus-visible + .zsp-toggle-track{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}
|
|
197
|
+
.zsp-toggle input:disabled{cursor:default}
|
|
198
|
+
.zsp-toggle input:disabled + .zsp-toggle-track{opacity:.5}
|
|
199
|
+
/**
|
|
200
|
+
* 模型栏旁的「当前使用账号」:样式**内联写在组件里**(照 workbuddy),
|
|
201
|
+
* 这里只留一条 hover 反馈 —— 面板 CSS 与 composer 是两套上下文,
|
|
202
|
+
* 内联样式不会因为 <style> 注入时机而闪一下默认外观。
|
|
203
|
+
*/
|
|
204
|
+
.zsp-route:hover{color:var(--dsw-alias-label-primary)}
|
|
205
|
+
.zsp-route[data-state="exhausted"]{color:var(--dsw-alias-label-danger,#d9534f)}
|
|
206
|
+
.zsp-route[data-state="exhausted"]:hover{color:var(--dsw-alias-label-danger,#d9534f)}
|
|
207
|
+
/* 风控痕迹:橙黄而不是红 —— 账号没坏、也不是没额度,只是被上游拦过一次。 */
|
|
208
|
+
.zsp-route[data-state="risk"]{color:var(--dsw-alias-label-warning,#b8860b)}
|
|
209
|
+
.zsp-route[data-state="risk"]:hover{color:var(--dsw-alias-label-warning,#b8860b)}
|
|
210
|
+
.zsp-route:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}
|
|
211
|
+
@media (max-width: 520px){
|
|
212
|
+
.zsp-head{gap:6px}
|
|
213
|
+
.zsp-title{font-size:12.5px}
|
|
214
|
+
.zsp-card{padding:8px 10px}
|
|
215
|
+
.zsp-add{padding:8px 10px}
|
|
216
|
+
.zsp-actions{gap:4px}
|
|
217
|
+
}
|
|
218
|
+
`
|
|
219
|
+
|
|
220
|
+
/** 同源 API 调用;非 2xx 抛出服务端给的可读 error。 */
|
|
221
|
+
async function api(path, body) {
|
|
222
|
+
const init =
|
|
223
|
+
body === undefined
|
|
224
|
+
? {}
|
|
225
|
+
: { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }
|
|
226
|
+
const response = await fetch(API + path, init)
|
|
227
|
+
const data = await response.json().catch(() => ({}))
|
|
228
|
+
if (!response.ok) {
|
|
229
|
+
throw new Error(typeof data?.error === 'string' ? data.error : `HTTP ${response.status}`)
|
|
230
|
+
}
|
|
231
|
+
return data
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 开关(iOS 风格):「模型列表里显示这一栏」的每栏右侧开关。
|
|
236
|
+
* checked = 显示;原生 checkbox 保证可访问性(键盘/读屏)。
|
|
237
|
+
* `label` 覆盖 aria-label(自动领取拨盘用它,不然读屏会念成「模型列表」)。
|
|
238
|
+
*/
|
|
239
|
+
function Toggle(props) {
|
|
240
|
+
const { checked, disabled, onChange, label } = props
|
|
241
|
+
return h(
|
|
242
|
+
'label',
|
|
243
|
+
{ className: 'zsp-toggle' },
|
|
244
|
+
h('input', {
|
|
245
|
+
type: 'checkbox',
|
|
246
|
+
checked: checked === true,
|
|
247
|
+
disabled,
|
|
248
|
+
'aria-label': label ?? '在模型列表中显示这一栏',
|
|
249
|
+
onChange: (event) => onChange(event.target.checked),
|
|
250
|
+
}),
|
|
251
|
+
h('span', { className: 'zsp-toggle-track', 'aria-hidden': 'true' }),
|
|
252
|
+
)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** 渠道显示名:直接用官方 provider id(zai / bigmodel),不做本地化称呼。 */
|
|
256
|
+
function channelLabel(channel) {
|
|
257
|
+
if (channel === 'zai') return 'zai'
|
|
258
|
+
if (channel === 'bigmodel') return 'bigmodel'
|
|
259
|
+
return channel === null || channel === undefined ? 'bigmodel' : String(channel)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function formatReset(resetAt) {
|
|
263
|
+
const ms = Number(resetAt)
|
|
264
|
+
if (!Number.isFinite(ms) || ms <= 0) return ''
|
|
265
|
+
return new Date(ms).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 未生效额度的到点时间:跨天要带日期(「明天 23:00」看不出来是哪天),
|
|
270
|
+
* 今天内只显示时刻,与限额 chips 的风格一致。
|
|
271
|
+
*/
|
|
272
|
+
function formatEffective(effectiveAt) {
|
|
273
|
+
const ms = Number(effectiveAt)
|
|
274
|
+
if (!Number.isFinite(ms) || ms <= 0) return ''
|
|
275
|
+
const date = new Date(ms)
|
|
276
|
+
const today = new Date()
|
|
277
|
+
const sameDay =
|
|
278
|
+
date.getFullYear() === today.getFullYear() &&
|
|
279
|
+
date.getMonth() === today.getMonth() &&
|
|
280
|
+
date.getDate() === today.getDate()
|
|
281
|
+
return sameDay
|
|
282
|
+
? date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
|
|
283
|
+
: date.toLocaleString([], { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' })
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 未生效额度的可读文案:`GLM-5.3-Flash 3亿 tokens` 之类。 */
|
|
287
|
+
function formatPendingUnits(pending) {
|
|
288
|
+
const units = Number(pending?.grantUnits)
|
|
289
|
+
if (!Number.isFinite(units) || units <= 0) return ''
|
|
290
|
+
if (units >= 100_000_000) return `${(units / 100_000_000).toFixed(units % 100_000_000 === 0 ? 0 : 1)}亿`
|
|
291
|
+
if (units >= 10_000) return `${Math.round(units / 10_000)}万`
|
|
292
|
+
return String(units)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** token 数值的紧凑显示(总额度行用):3.9亿 / 500万 / 8400。 */
|
|
296
|
+
function formatTokens(units) {
|
|
297
|
+
const n = Number(units)
|
|
298
|
+
if (!Number.isFinite(n)) return '0'
|
|
299
|
+
if (n >= 100_000_000) return `${(n / 100_000_000).toFixed(n % 100_000_000 === 0 ? 0 : 2)}亿`
|
|
300
|
+
if (n >= 10_000) return `${Math.round(n / 10_000)}万`
|
|
301
|
+
return String(Math.round(n))
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 还多久到期,如「3 小时后」「12 分钟后」「2 天后」「已过期」。
|
|
306
|
+
*
|
|
307
|
+
* 为什么还要给相对时长:额度按自然日结算,看到的永远是「今天 23:59」这种
|
|
308
|
+
* 绝对时间,用户得心算「那还剩多久」。两者并排给,判断成本最低。
|
|
309
|
+
*/
|
|
310
|
+
function formatRemaining(expiresAt, now = Date.now()) {
|
|
311
|
+
const ms = Number(expiresAt)
|
|
312
|
+
if (!Number.isFinite(ms) || ms <= 0) return ''
|
|
313
|
+
const diff = ms - now
|
|
314
|
+
if (diff <= 0) return '已过期'
|
|
315
|
+
const minutes = Math.floor(diff / 60_000)
|
|
316
|
+
if (minutes < 1) return '不到 1 分钟'
|
|
317
|
+
if (minutes < 60) return `${minutes} 分钟后`
|
|
318
|
+
const hours = Math.floor(minutes / 60)
|
|
319
|
+
if (hours < 24) {
|
|
320
|
+
const restMinutes = minutes % 60
|
|
321
|
+
return restMinutes === 0 ? `${hours} 小时后` : `${hours} 小时 ${restMinutes} 分后`
|
|
322
|
+
}
|
|
323
|
+
const days = Math.floor(hours / 24)
|
|
324
|
+
const restHours = hours % 24
|
|
325
|
+
return restHours === 0 ? `${days} 天后` : `${days} 天 ${restHours} 小时后`
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 一条额度进度条(已生效 / 未生效共用同一形状)。
|
|
330
|
+
*
|
|
331
|
+
* 为什么共用一个组件:两种额度对用户是**同一件事的两种时态**——
|
|
332
|
+
* 已生效的现在就能用,未生效的到点就能用。形状一致,差别只在
|
|
333
|
+
* 条纹(`data-pending`)与底部那句提示。
|
|
334
|
+
*
|
|
335
|
+
* `remaining`/`total` 缺失时(未生效的只有 `grantUnits`)按满格画:
|
|
336
|
+
* 尚未启用的额度必然是 100% 剩余。
|
|
337
|
+
*/
|
|
338
|
+
function QuotaMeter(props) {
|
|
339
|
+
const { remaining, total, planName, pending, effectiveAt, expiresAt } = props
|
|
340
|
+
const percent = pending === true
|
|
341
|
+
? 100
|
|
342
|
+
: typeof total === 'number' && total > 0 && typeof remaining === 'number'
|
|
343
|
+
? Math.max(0, Math.min(100, Math.round((remaining / total) * 100)))
|
|
344
|
+
: 0
|
|
345
|
+
/**
|
|
346
|
+
* 底部一句话:已生效 = 剩余 + 计划名 + 到期;未生效 = 何时启用 + 何时作废。
|
|
347
|
+
* 到期信息缺失(上游没给 / 已用光)时整段不出现,不留「undefined」。
|
|
348
|
+
*/
|
|
349
|
+
const effectiveLabel = effectiveAt ? `${formatEffective(effectiveAt)} 起可用` : '待生效'
|
|
350
|
+
const expireLabel = expiresAt
|
|
351
|
+
? `${formatEffective(expiresAt)} 到期(${formatRemaining(expiresAt)})`
|
|
352
|
+
: ''
|
|
353
|
+
return h(
|
|
354
|
+
'div',
|
|
355
|
+
{ className: 'zsp-meter' },
|
|
356
|
+
h(
|
|
357
|
+
'div',
|
|
358
|
+
{ className: 'zsp-bar', 'data-pending': pending === true },
|
|
359
|
+
h('span', { style: { width: `${percent}%` } }),
|
|
360
|
+
),
|
|
361
|
+
h(
|
|
362
|
+
'span',
|
|
363
|
+
{ className: 'zsp-muted zsp-meter-caption' },
|
|
364
|
+
pending === true
|
|
365
|
+
? `${effectiveLabel}${expireLabel ? ` · ${expireLabel}` : ''}`
|
|
366
|
+
: `剩余 ${remaining}/${total}${planName ? ` · ${planName}` : ''}${expireLabel ? ` · ${expireLabel}` : ''}`,
|
|
367
|
+
),
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 卡片标题行的可编辑昵称:点一下变输入框,Enter 提交 / Esc 或失焦取消。
|
|
373
|
+
* 什么都不填直接失焦 = 不改名。
|
|
374
|
+
*/
|
|
375
|
+
function NicknameEdit(props) {
|
|
376
|
+
const { nickname, busy, onCommit } = props
|
|
377
|
+
const [editing, setEditing] = useState(false)
|
|
378
|
+
const [draft, setDraft] = useState(nickname)
|
|
379
|
+
const inputRef = useRef(undefined)
|
|
380
|
+
|
|
381
|
+
useEffect(() => {
|
|
382
|
+
if (editing === true) {
|
|
383
|
+
setDraft(nickname)
|
|
384
|
+
/** 进入编辑态后自动聚焦并全选,方便整段替换。 */
|
|
385
|
+
inputRef.current?.focus()
|
|
386
|
+
inputRef.current?.select()
|
|
387
|
+
}
|
|
388
|
+
}, [editing, nickname])
|
|
389
|
+
|
|
390
|
+
if (editing !== true) {
|
|
391
|
+
return h(
|
|
392
|
+
'button',
|
|
393
|
+
{
|
|
394
|
+
type: 'button',
|
|
395
|
+
className: 'zsp-name zsp-name-edit',
|
|
396
|
+
title: '点击重命名',
|
|
397
|
+
disabled: busy,
|
|
398
|
+
onClick: () => setEditing(true),
|
|
399
|
+
},
|
|
400
|
+
nickname,
|
|
401
|
+
)
|
|
402
|
+
}
|
|
403
|
+
return h('input', {
|
|
404
|
+
ref: inputRef,
|
|
405
|
+
className: 'zsp-name-input',
|
|
406
|
+
value: draft,
|
|
407
|
+
disabled: busy,
|
|
408
|
+
maxLength: 40,
|
|
409
|
+
'aria-label': '账号备注',
|
|
410
|
+
onChange: (event) => setDraft(event.target.value),
|
|
411
|
+
onKeyDown: (event) => {
|
|
412
|
+
if (event.key === 'Enter') {
|
|
413
|
+
const next = draft.trim()
|
|
414
|
+
if (next.length > 0 && next !== nickname) onCommit(next)
|
|
415
|
+
setEditing(false)
|
|
416
|
+
} else if (event.key === 'Escape') {
|
|
417
|
+
setEditing(false)
|
|
418
|
+
}
|
|
419
|
+
},
|
|
420
|
+
onBlur: () => setEditing(false),
|
|
421
|
+
})
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* OpenAI 兼容端点状态行(0.9.5):网关常开,鉴权只认分发 key(面板下方生成)。
|
|
425
|
+
* 这里只显示运行状态与 baseURL;开关保留给「彻底关闭网关」的场景。
|
|
426
|
+
*/
|
|
427
|
+
function OpenAiGatewaySection(props) {
|
|
428
|
+
const { gateway, busy, onChanged } = props
|
|
429
|
+
const [toggling, setToggling] = useState(false)
|
|
430
|
+
if (gateway === undefined) return null
|
|
431
|
+
const effectiveOn = gateway.running === true
|
|
432
|
+
|
|
433
|
+
const toggle = async () => {
|
|
434
|
+
setToggling(true)
|
|
435
|
+
try {
|
|
436
|
+
await api('/openai-gateway', { enabled: gateway.enabledInSettings !== true })
|
|
437
|
+
if (typeof onChanged === 'function') await onChanged()
|
|
438
|
+
} finally {
|
|
439
|
+
setToggling(false)
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return h(
|
|
444
|
+
'div',
|
|
445
|
+
{ className: 'zsp-group' },
|
|
446
|
+
h(
|
|
447
|
+
'div',
|
|
448
|
+
{ className: 'zsp-row' },
|
|
449
|
+
h('span', { className: 'zsp-dot', 'data-on': effectiveOn }),
|
|
450
|
+
h('span', { className: 'zsp-name' }, 'OpenAI 兼容端点'),
|
|
451
|
+
h(
|
|
452
|
+
'span',
|
|
453
|
+
{ className: 'zsp-muted', style: { flex: '1 1 auto', minWidth: 0 } },
|
|
454
|
+
effectiveOn ? `运行中 · ${gateway.baseUrl ?? ''}` : '未运行(重启宿主后生效)',
|
|
455
|
+
),
|
|
456
|
+
h(
|
|
457
|
+
'span',
|
|
458
|
+
{ className: 'zsp-toggle-row' },
|
|
459
|
+
h(Toggle, { checked: gateway.enabledInSettings === true, disabled: busy || toggling, onChange: () => void toggle() }),
|
|
460
|
+
),
|
|
461
|
+
),
|
|
462
|
+
)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* 分发 key 管理区(0.9.8 重构:现代 API key 控制台形态)。
|
|
467
|
+
*
|
|
468
|
+
* 设计参照通行模式(OpenAI/Anthropic/Linear 控制台同款):
|
|
469
|
+
* - **卡片化列表**:每把 key 一张卡 —— 名字 + 状态 chip + 创建日期一行,
|
|
470
|
+
* 用量进度条一行,操作按钮一行;不再是一条扁平行塞 7 个控件;
|
|
471
|
+
* - **用量可视化**:已用/限额的进度条,≥90% 转危险色;不限额只显示已用;
|
|
472
|
+
* - **限额预设分段选择**(100万/500万/2000万/不限)+ 自定义输入,取代
|
|
473
|
+
* 旧版 ±100 万步进(要调到 4.2 亿得点 420 次);
|
|
474
|
+
* - **创建后一次性 token 强调卡**:成功色描边 + 「复制」按钮(clipboard)
|
|
475
|
+
* + 「我已保存」关闭 —— 明文只此一面,复制即忘;
|
|
476
|
+
* - **两步删除确认**:第一次点变实底危险「确认删除」,3 秒不点自动回退;
|
|
477
|
+
* - 创建表单**折叠**进「+ 新建 API Key」按钮,平时不占面板空间。
|
|
478
|
+
*
|
|
479
|
+
* ⚠ token 明文**只在创建响应里出现一次**(服务端契约:列表永不下发 token)。
|
|
480
|
+
* ⚠ CRUD 请求体必须带 `id`(0.9.6 服务端契约)且失败冒泡成错误行(0.9.7)。
|
|
481
|
+
*/
|
|
482
|
+
function DistributionKeysSection(props) {
|
|
483
|
+
const { keys, accounts, busy, onChanged } = props
|
|
484
|
+
const [creating, setCreating] = useState(false)
|
|
485
|
+
/** 新建表单展开态(平时折叠,面板不再被三个空输入框占一行)。 */
|
|
486
|
+
const [formOpen, setFormOpen] = useState(false)
|
|
487
|
+
const [newName, setNewName] = useState('')
|
|
488
|
+
const [newQuota, setNewQuota] = useState('')
|
|
489
|
+
const [newAccount, setNewAccount] = useState('')
|
|
490
|
+
const [freshToken, setFreshToken] = useState(undefined)
|
|
491
|
+
const [copied, setCopied] = useState(false)
|
|
492
|
+
/** CRUD 失败的错误行(此前 catch 后静默,按钮「没反应」无从排查)。 */
|
|
493
|
+
const [opError, setOpError] = useState(undefined)
|
|
494
|
+
/** 两步删除:武装中的 key id(3 秒不确认自动回退,CONFIRM_RESET_MS)。 */
|
|
495
|
+
const [confirmDeleteId, setConfirmDeleteId] = useState(undefined)
|
|
496
|
+
/** 每把 key 的限额编辑态(点「限额」进入,Escape/确认退出)。 */
|
|
497
|
+
const [editingQuotaId, setEditingQuotaId] = useState(undefined)
|
|
498
|
+
const [editingQuotaValue, setEditingQuotaValue] = useState('')
|
|
499
|
+
|
|
500
|
+
useEffect(() => {
|
|
501
|
+
if (confirmDeleteId === undefined) return undefined
|
|
502
|
+
const timer = setTimeout(() => setConfirmDeleteId(undefined), CONFIRM_RESET_MS)
|
|
503
|
+
return () => clearTimeout(timer)
|
|
504
|
+
}, [confirmDeleteId])
|
|
505
|
+
|
|
506
|
+
const create = async () => {
|
|
507
|
+
setCreating(true)
|
|
508
|
+
setOpError(undefined)
|
|
509
|
+
try {
|
|
510
|
+
const response = await fetch(`${API}/distribution-keys?op=create`, {
|
|
511
|
+
method: 'POST',
|
|
512
|
+
headers: { 'content-type': 'application/json' },
|
|
513
|
+
body: JSON.stringify({
|
|
514
|
+
name: newName,
|
|
515
|
+
quotaTotal: newQuota.trim().length > 0 ? Number(newQuota) : null,
|
|
516
|
+
accountId: newAccount.length > 0 ? newAccount : null,
|
|
517
|
+
}),
|
|
518
|
+
})
|
|
519
|
+
const data = await response.json().catch(() => ({}))
|
|
520
|
+
if (response.ok && typeof data?.key?.token === 'string') {
|
|
521
|
+
setFreshToken(data.key.token)
|
|
522
|
+
setCopied(false)
|
|
523
|
+
setNewName(''); setNewQuota(''); setNewAccount('')
|
|
524
|
+
setFormOpen(false)
|
|
525
|
+
if (typeof onChanged === 'function') await onChanged()
|
|
526
|
+
} else {
|
|
527
|
+
setOpError(typeof data?.error === 'string' ? data.error : `创建失败(HTTP ${response.status})`)
|
|
528
|
+
}
|
|
529
|
+
} catch (error) {
|
|
530
|
+
setOpError(error instanceof Error ? error.message : String(error))
|
|
531
|
+
} finally {
|
|
532
|
+
setCreating(false)
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* CRUD 操作(禁用/启用、限额调节、删除)。
|
|
538
|
+
*
|
|
539
|
+
* ⚠ 0.9.6 的坑:调用点都改成了 `op(key.id, …)`,但这里的序列化键名
|
|
540
|
+
* 还是 `token` —— 服务端按 `body.id` 定位(token 已不下发),发出的
|
|
541
|
+
* body 里根本没有 `id` 键 ⇒ 永远 400「需要 id」。且此前 catch 后静默,
|
|
542
|
+
* 按钮看起来就是「没反应」。键名与服务端契约绑定,错误必须冒泡。
|
|
543
|
+
*/
|
|
544
|
+
const op = async (id, opName, body = {}) => {
|
|
545
|
+
try {
|
|
546
|
+
const response = await fetch(`${API}/distribution-keys?op=${opName}`, {
|
|
547
|
+
method: 'POST',
|
|
548
|
+
headers: { 'content-type': 'application/json' },
|
|
549
|
+
body: JSON.stringify({ id, ...body }),
|
|
550
|
+
})
|
|
551
|
+
const data = await response.json().catch(() => ({}))
|
|
552
|
+
if (!response.ok || data?.ok !== true) {
|
|
553
|
+
throw new Error(typeof data?.error === 'string' ? data.error : `HTTP ${response.status}`)
|
|
554
|
+
}
|
|
555
|
+
setOpError(undefined)
|
|
556
|
+
if (typeof onChanged === 'function') await onChanged()
|
|
557
|
+
} catch (error) {
|
|
558
|
+
setOpError(error instanceof Error ? error.message : String(error))
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** 复制新 key 明文(clipboard API 失败时降级:明文本就 user-select:all 可手动选)。 */
|
|
563
|
+
const copyFreshToken = async () => {
|
|
564
|
+
try {
|
|
565
|
+
await navigator.clipboard.writeText(freshToken)
|
|
566
|
+
setCopied(true)
|
|
567
|
+
} catch {
|
|
568
|
+
setCopied(false)
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (keys === undefined) return null
|
|
573
|
+
const keyList = Array.isArray(keys) ? keys : []
|
|
574
|
+
return h(
|
|
575
|
+
'div',
|
|
576
|
+
{ className: 'zsp-group' },
|
|
577
|
+
/** 头部:标题 + 数量 chip + 新建按钮(表单折叠进它)。 */
|
|
578
|
+
h('div', { className: 'zsp-row' },
|
|
579
|
+
h('span', { className: 'zsp-name' }, 'API Keys'),
|
|
580
|
+
h('span', { className: 'zsp-count' }, `${keyList.length} 把`),
|
|
581
|
+
h('span', { className: 'zsp-muted', style: { flex: '1 1 auto' } },
|
|
582
|
+
'独立计量与限额;额度用尽即拒绝'),
|
|
583
|
+
h('button', {
|
|
584
|
+
className: 'zsp-btn',
|
|
585
|
+
'data-primary': formOpen !== true,
|
|
586
|
+
onClick: () => setFormOpen(formOpen !== true),
|
|
587
|
+
disabled: busy,
|
|
588
|
+
}, formOpen ? '收起' : '+ 新建 API Key'),
|
|
589
|
+
),
|
|
590
|
+
formOpen === true
|
|
591
|
+
? h('div', { className: 'zsp-add' },
|
|
592
|
+
h('input', { className: 'zsp-name-input', placeholder: '备注(给谁用)', value: newName, onChange: (e) => setNewName(e.target.value), style: { maxWidth: '200px' } }),
|
|
593
|
+
h('select', { className: 'zsp-name-input', value: newAccount, onChange: (e) => setNewAccount(e.target.value), style: { maxWidth: '220px' } },
|
|
594
|
+
h('option', { value: '' }, '不限账号(用整个池)'),
|
|
595
|
+
(accounts ?? []).map((a) => h('option', { key: a.id, value: a.id }, `#${a.index} ${a.nickname}`))),
|
|
596
|
+
h('div', { className: 'zsp-quotafield' },
|
|
597
|
+
h('span', { className: 'zsp-muted' }, '限额:'),
|
|
598
|
+
h('button', { className: 'zsp-btn', 'data-selected': newQuota === '', onClick: () => setNewQuota(''), disabled: busy }, '不限'),
|
|
599
|
+
[1_000_000, 5_000_000, 20_000_000].map((preset) =>
|
|
600
|
+
h('button', {
|
|
601
|
+
className: 'zsp-btn',
|
|
602
|
+
'data-selected': newQuota === String(preset),
|
|
603
|
+
key: preset,
|
|
604
|
+
onClick: () => setNewQuota(String(preset)),
|
|
605
|
+
disabled: busy,
|
|
606
|
+
}, formatTokens(preset)),
|
|
607
|
+
),
|
|
608
|
+
h('input', {
|
|
609
|
+
className: 'zsp-quota-input',
|
|
610
|
+
placeholder: '自定义 tokens',
|
|
611
|
+
value: newQuota,
|
|
612
|
+
onChange: (e) => setNewQuota(e.target.value.replace(/[^\d]/g, '')),
|
|
613
|
+
}),
|
|
614
|
+
),
|
|
615
|
+
h('button', { className: 'zsp-btn', 'data-primary': true, onClick: () => void create(), disabled: busy || creating },
|
|
616
|
+
creating ? '生成中…' : '生成'),
|
|
617
|
+
)
|
|
618
|
+
: null,
|
|
619
|
+
/** 新 key 明文强调卡:只此一面,带复制 + 已保存关闭。 */
|
|
620
|
+
freshToken !== undefined
|
|
621
|
+
? h('div', { className: 'zsp-token-card' },
|
|
622
|
+
h('span', { className: 'zsp-ok' }, '新 key 已生成 —— 现在复制保存,关闭后无法再次查看:'),
|
|
623
|
+
h('code', { className: 'zsp-token-value' }, freshToken),
|
|
624
|
+
h('div', { className: 'zsp-row' },
|
|
625
|
+
h('button', { className: 'zsp-btn', 'data-primary': copied !== true, onClick: () => void copyFreshToken() },
|
|
626
|
+
copied ? '已复制 ✓' : '复制'),
|
|
627
|
+
h('button', { className: 'zsp-btn', onClick: () => setFreshToken(undefined) }, '我已保存'),
|
|
628
|
+
),
|
|
629
|
+
)
|
|
630
|
+
: null,
|
|
631
|
+
opError !== undefined
|
|
632
|
+
? h('div', { className: 'zsp-row' },
|
|
633
|
+
h('span', { className: 'zsp-error', 'data-error': 'true',
|
|
634
|
+
title: '点此忽略', onClick: () => setOpError(undefined) }, `分发 key 操作失败:${opError}`))
|
|
635
|
+
: null,
|
|
636
|
+
keyList.length === 0 && freshToken === undefined
|
|
637
|
+
? h('div', { className: 'zsp-empty' },
|
|
638
|
+
h('span', { className: 'zsp-muted' }, '还没有 API Key —— 网关开启后,所有请求都需要一把 key 鉴权。'),
|
|
639
|
+
h('button', { className: 'zsp-btn', 'data-primary': true, onClick: () => setFormOpen(true), disabled: busy }, '生成第一把'),
|
|
640
|
+
)
|
|
641
|
+
: null,
|
|
642
|
+
keyList.map((key) => {
|
|
643
|
+
const used = (key.usage.inputTokens ?? 0) + (key.usage.outputTokens ?? 0)
|
|
644
|
+
const limited = key.quotaTotal !== null && key.quotaTotal !== undefined
|
|
645
|
+
const percent = limited && key.quotaTotal > 0 ? Math.min(100, Math.round((used / key.quotaTotal) * 100)) : 0
|
|
646
|
+
const hot = limited && percent >= 90
|
|
647
|
+
const accountName = key.accountId
|
|
648
|
+
? ((accounts ?? []).find((a) => a.id === key.accountId)?.nickname ?? `#${String(key.accountId).slice(0, 8)}`)
|
|
649
|
+
: null
|
|
650
|
+
const confirmingDelete = confirmDeleteId === key.id
|
|
651
|
+
const editingQuota = editingQuotaId === key.id
|
|
652
|
+
return h('div', { key: key.id, className: 'zsp-keycard', 'data-disabled': key.enabled !== true },
|
|
653
|
+
/** 行 1:名字 + 状态 chip + 钉定 chip + 创建日期。 */
|
|
654
|
+
h('div', { className: 'zsp-keyhead' },
|
|
655
|
+
h('span', { className: 'zsp-dot', 'data-on': key.enabled === true }),
|
|
656
|
+
h('span', { className: 'zsp-name' }, key.name),
|
|
657
|
+
h('span', { className: 'zsp-chip', 'data-limited': key.enabled !== true }, key.enabled ? '启用' : '已禁用'),
|
|
658
|
+
accountName !== null ? h('span', { className: 'zsp-chip' }, `钉定 ${accountName}`) : null,
|
|
659
|
+
h('span', { className: 'zsp-spacer' }),
|
|
660
|
+
h('span', { className: 'zsp-keydate' },
|
|
661
|
+
`创建于 ${new Date(key.createdAt).toLocaleDateString([], { month: 'numeric', day: 'numeric' })}`),
|
|
662
|
+
),
|
|
663
|
+
/** 行 2:用量进度条 + 数值。 */
|
|
664
|
+
h('div', { className: 'zsp-meter-block' },
|
|
665
|
+
h('div', { className: 'zsp-bar', 'data-hot': hot === true },
|
|
666
|
+
h('span', { style: { width: `${limited ? percent : Math.min(100, Math.round((used / 10_000_000) * 100))}%` } })),
|
|
667
|
+
h('span', { className: 'zsp-muted' },
|
|
668
|
+
limited
|
|
669
|
+
? `${formatTokens(used)} / ${formatTokens(key.quotaTotal)} tokens · ${key.usage.requests} 次请求`
|
|
670
|
+
: `${formatTokens(used)} tokens 已用 · 不限额 · ${key.usage.requests} 次请求`),
|
|
671
|
+
),
|
|
672
|
+
/** 行 3:操作。限额编辑态展开为预设分段 + 自定义输入;删除两步确认。 */
|
|
673
|
+
h('div', { className: 'zsp-actions' },
|
|
674
|
+
editingQuota
|
|
675
|
+
? h('div', { className: 'zsp-quotafield' },
|
|
676
|
+
h('button', { className: 'zsp-btn', 'data-selected': editingQuotaValue === '', onClick: () => setEditingQuotaValue(''), disabled: busy }, '不限'),
|
|
677
|
+
[1_000_000, 5_000_000, 20_000_000].map((preset) =>
|
|
678
|
+
h('button', {
|
|
679
|
+
className: 'zsp-btn',
|
|
680
|
+
'data-selected': editingQuotaValue === String(preset),
|
|
681
|
+
key: preset,
|
|
682
|
+
onClick: () => setEditingQuotaValue(String(preset)),
|
|
683
|
+
disabled: busy,
|
|
684
|
+
}, formatTokens(preset)),
|
|
685
|
+
),
|
|
686
|
+
h('input', {
|
|
687
|
+
className: 'zsp-quota-input',
|
|
688
|
+
placeholder: '自定义 tokens',
|
|
689
|
+
value: editingQuotaValue,
|
|
690
|
+
onChange: (e) => setEditingQuotaValue(e.target.value.replace(/[^\d]/g, '')),
|
|
691
|
+
}),
|
|
692
|
+
h('button', {
|
|
693
|
+
className: 'zsp-btn', 'data-primary': true, disabled: busy,
|
|
694
|
+
onClick: () => {
|
|
695
|
+
void op(key.id, 'update', { quotaTotal: editingQuotaValue.trim().length > 0 ? Number(editingQuotaValue) : null })
|
|
696
|
+
setEditingQuotaId(undefined)
|
|
697
|
+
},
|
|
698
|
+
}, '保存'),
|
|
699
|
+
h('button', { className: 'zsp-btn', onClick: () => setEditingQuotaId(undefined) }, '取消'),
|
|
700
|
+
)
|
|
701
|
+
: h('button', {
|
|
702
|
+
className: 'zsp-btn', disabled: busy,
|
|
703
|
+
onClick: () => { setEditingQuotaId(key.id); setEditingQuotaValue(key.quotaTotal === null || key.quotaTotal === undefined ? '' : String(key.quotaTotal)) },
|
|
704
|
+
}, `限额:${limited ? formatTokens(key.quotaTotal) : '不限'}`),
|
|
705
|
+
h('button', {
|
|
706
|
+
className: 'zsp-btn', disabled: busy,
|
|
707
|
+
onClick: () => void op(key.id, 'update', { enabled: key.enabled !== true }),
|
|
708
|
+
}, key.enabled ? '禁用' : '启用'),
|
|
709
|
+
h('span', { className: 'zsp-spacer' }),
|
|
710
|
+
confirmingDelete
|
|
711
|
+
? h('button', {
|
|
712
|
+
className: 'zsp-btn', 'data-confirm': true, disabled: busy,
|
|
713
|
+
onClick: () => { setConfirmDeleteId(undefined); void op(key.id, 'delete') },
|
|
714
|
+
}, '确认删除?')
|
|
715
|
+
: h('button', {
|
|
716
|
+
className: 'zsp-btn', 'data-danger': true, disabled: busy,
|
|
717
|
+
onClick: () => setConfirmDeleteId(key.id),
|
|
718
|
+
}, '删除'),
|
|
719
|
+
),
|
|
720
|
+
)
|
|
721
|
+
}),
|
|
722
|
+
)
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* 赠送额度领取区(面板底部)。
|
|
727
|
+
*
|
|
728
|
+
* ## 为什么不是「先查列表再点领取」
|
|
729
|
+
*
|
|
730
|
+
* 2026-10-28 分析官方 ZCode 客户端日志(2962 次 RPC)得出:
|
|
731
|
+
*
|
|
732
|
+
* | RPC | 全天调用次数 |
|
|
733
|
+
* |---|---|
|
|
734
|
+
* | `coding-plan-subscription.getManualClaimPlanPreviews`(`/billing/preview`) | **0** |
|
|
735
|
+
* | `coding-plan-subscription.claimManualPlan`(`/billing/claim`) | 1(用户点击时) |
|
|
736
|
+
*
|
|
737
|
+
* 官方客户端**从不预判有没有活动**:它直接发 claim,用服务端返回的
|
|
738
|
+
* `code`/`msg` 告诉用户结果。我们此前的「先查 preview,有才显示按钮」
|
|
739
|
+
* 导致面板永远显示「暂无可领取」——因为 preview 恒空,而活动实际存在。
|
|
740
|
+
*
|
|
741
|
+
* 故改为**忠实执行 + 忠实报告**:填 `planId` 直接领,业务码原样显示
|
|
742
|
+
* (`0` 成功 / `1003` 已领过 / `1005` 领完)。「有没有名额」由服务端裁决。
|
|
743
|
+
*
|
|
744
|
+
* ⚠ captcha 失败会原地重试一次(官方日志实证:一次点击里
|
|
745
|
+
* `send_preflight outcome=error` → 6 秒后 `outcome=success`,因为阿里云
|
|
746
|
+
* CDN 首轮抖动是常态)。这里是「按两次才领到」的服务端侧对策。
|
|
747
|
+
*/
|
|
748
|
+
function ClaimSection(props) {
|
|
749
|
+
const { accounts, busy, onClaimed, captcha, autoClaim, onToggleAutoClaim } = props
|
|
750
|
+
/** accountId → { planId, loading, results, error } */
|
|
751
|
+
const [state, setState] = useState({})
|
|
752
|
+
const [open, setOpen] = useState(false)
|
|
753
|
+
const [restarting, setRestarting] = useState(false)
|
|
754
|
+
const [togglingAuto, setTogglingAuto] = useState(false)
|
|
755
|
+
|
|
756
|
+
const patch = (id, next) => setState((prev) => ({ ...prev, [id]: { ...prev[id], ...next } }))
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* 探测活动:调 /claimable(官方 marketing/touch 发现端点,GET、零 captcha)。
|
|
760
|
+
* **自动执行**(展开即探),结果存进 state 供渲染与领取使用。
|
|
761
|
+
*/
|
|
762
|
+
const probe = useCallback(
|
|
763
|
+
async (id) => {
|
|
764
|
+
patch(id, { probing: true, error: undefined, results: undefined, emptyReason: undefined })
|
|
765
|
+
try {
|
|
766
|
+
/** /claimable 是 GET(id 走 query),不走 POST 版 api() 助手。 */
|
|
767
|
+
const response = await fetch(`${API}/claimable?id=${encodeURIComponent(id)}`)
|
|
768
|
+
const data = await response.json().catch(() => ({}))
|
|
769
|
+
if (!response.ok) throw new Error(typeof data?.error === 'string' ? data.error : `HTTP ${response.status}`)
|
|
770
|
+
const plans = Array.isArray(data.plans) ? data.plans : []
|
|
771
|
+
/**
|
|
772
|
+
* 三态(0.11.0):旧实现把「探测失败」与「确实没有活动」写成同一句话,
|
|
773
|
+
* 用户无法区分是服务端真没给,还是客户端拿挂了 —— 这正是
|
|
774
|
+
* 「明明有活动却显示没有」最难排查的那一种。
|
|
775
|
+
*
|
|
776
|
+
* - `ok === false` → 探测失败(上游不可达 / 异常),红字;
|
|
777
|
+
* - `sources.campaign === 0 && sources.records === 0` → 服务端确实没有投放,
|
|
778
|
+
* 也没有领取记录;
|
|
779
|
+
* - 其余空结果 → 未知(老版本宿主没带 sources)。
|
|
780
|
+
*/
|
|
781
|
+
const emptyReason =
|
|
782
|
+
plans.length > 0
|
|
783
|
+
? undefined
|
|
784
|
+
: data.ok === false
|
|
785
|
+
? 'failed'
|
|
786
|
+
: data.sources?.campaign === 0 && data.sources?.records === 0
|
|
787
|
+
? 'none'
|
|
788
|
+
: 'unknown'
|
|
789
|
+
patch(id, {
|
|
790
|
+
probing: false,
|
|
791
|
+
plans,
|
|
792
|
+
sources: data.sources,
|
|
793
|
+
emptyReason,
|
|
794
|
+
...(plans.length === 0
|
|
795
|
+
? {
|
|
796
|
+
error:
|
|
797
|
+
data.ok === false
|
|
798
|
+
? `探测失败:${data.error ?? '上游不可达'}`
|
|
799
|
+
: emptyReason === 'none'
|
|
800
|
+
? '服务端此刻没有向你投放活动(投放源与领取记录均为空)'
|
|
801
|
+
: '当前没有探测到可领取的活动',
|
|
802
|
+
}
|
|
803
|
+
: {}),
|
|
804
|
+
})
|
|
805
|
+
} catch (caught) {
|
|
806
|
+
patch(id, {
|
|
807
|
+
probing: false,
|
|
808
|
+
plans: [],
|
|
809
|
+
emptyReason: 'failed',
|
|
810
|
+
error: caught instanceof Error ? caught.message : String(caught),
|
|
811
|
+
})
|
|
812
|
+
}
|
|
813
|
+
},
|
|
814
|
+
[],
|
|
815
|
+
)
|
|
816
|
+
|
|
817
|
+
const claim = useCallback(
|
|
818
|
+
async (id) => {
|
|
819
|
+
/** plan_id 只来自探测结果 —— 没有手输路径(见渲染处注释)。 */
|
|
820
|
+
const target = state[id]?.plans?.find((plan) => plan.claimed !== true) ?? state[id]?.plans?.[0]
|
|
821
|
+
if (typeof target?.planId !== 'string' || target.planId.length === 0) {
|
|
822
|
+
patch(id, { error: '当前没有探测到可领取的活动' })
|
|
823
|
+
return
|
|
824
|
+
}
|
|
825
|
+
patch(id, { loading: true, error: undefined, results: undefined })
|
|
826
|
+
try {
|
|
827
|
+
/** campaignId:领取成功后服务端要 confirm 回执(0.9.7),探测结果里带着。 */
|
|
828
|
+
const data = await api('/claim', {
|
|
829
|
+
id,
|
|
830
|
+
planId: target.planId,
|
|
831
|
+
...(typeof target.campaignId === 'string' ? { campaignId: target.campaignId } : {}),
|
|
832
|
+
})
|
|
833
|
+
patch(id, { loading: false, results: Array.isArray(data.results) ? data.results : [] })
|
|
834
|
+
/**
|
|
835
|
+
* ⚠ 必须**重新探测**,不能只改本地快照。
|
|
836
|
+
*
|
|
837
|
+
* 旧实现只 `patch` 了 results 并刷新余额:`state[id].plans` 还是领之前
|
|
838
|
+
* 那份 `claimed:false`,于是按钮永远保持可点(用户报「领完不变灰」)。
|
|
839
|
+
*
|
|
840
|
+
* 服务端在领取成功后做两件事:撤投放(deliveries 变空)+ 写字
|
|
841
|
+
* `billing/balance` 的 plans[]。重新探测能同时反映两者:
|
|
842
|
+
* claimed 变 true、或整条从列表消失 —— 两种都让按钮失能。
|
|
843
|
+
*/
|
|
844
|
+
await probe(id)
|
|
845
|
+
/** 领取会改变余额(或生成 pending),刷新面板让进度条同步。 */
|
|
846
|
+
if (typeof onClaimed === 'function') await onClaimed()
|
|
847
|
+
} catch (caught) {
|
|
848
|
+
patch(id, { loading: false, error: caught instanceof Error ? caught.message : String(caught) })
|
|
849
|
+
/** 失败也探一次:可能在飞请求里其实成功了(幂等 1003),别让 UI 停在旧态。 */
|
|
850
|
+
void probe(id)
|
|
851
|
+
}
|
|
852
|
+
},
|
|
853
|
+
[onClaimed, probe, state],
|
|
854
|
+
)
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* 展开时自动探测一次(探测零成本:GET、不烧 captcha)。
|
|
858
|
+
* ⚠ effect 依赖 `open` 与 `accounts`;已探测过的账号跳过,避免反复请求投放端点。
|
|
859
|
+
*/
|
|
860
|
+
useEffect(() => {
|
|
861
|
+
if (open !== true) return
|
|
862
|
+
for (const account of accounts) {
|
|
863
|
+
if (state[account.id] !== undefined) continue
|
|
864
|
+
void probe(account.id)
|
|
865
|
+
}
|
|
866
|
+
}, [open, accounts, probe, state])
|
|
867
|
+
|
|
868
|
+
/** 自动领取开关(拨盘):写入宿主 settings,重启后保持。 */
|
|
869
|
+
const toggleAutoClaim = useCallback(async () => {
|
|
870
|
+
if (typeof onToggleAutoClaim !== 'function') return
|
|
871
|
+
setTogglingAuto(true)
|
|
872
|
+
try {
|
|
873
|
+
await onToggleAutoClaim(autoClaim?.enabled !== true)
|
|
874
|
+
} finally {
|
|
875
|
+
setTogglingAuto(false)
|
|
876
|
+
}
|
|
877
|
+
}, [autoClaim, onToggleAutoClaim])
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* ⚠ 所有 hooks 必须在任何早退之前调用。
|
|
881
|
+
* 0.5.0 曾把 useCallback 放在 `return null` 之后 → 两次渲染 hook 数不一致
|
|
882
|
+
* → React 抛 Rendered fewer hooks than expected → 整个面板白屏。
|
|
883
|
+
*
|
|
884
|
+
* 0.6.6 起「重启」改为**取指引**:端点不再替用户执行(杀进程 + 计划任务
|
|
885
|
+
* 被火绒判毒,且宿主 Job Object 会连带杀死脚本),只返回用户可复制的
|
|
886
|
+
* 启动命令。这里拿到后展示出来,让用户自己完成重启。
|
|
887
|
+
*
|
|
888
|
+
* @param {'plain' | 'debug'} mode `debug` 才带 `--remote-debugging-port`。
|
|
889
|
+
*/
|
|
890
|
+
const restart = useCallback(async (mode = 'debug') => {
|
|
891
|
+
setRestarting(true)
|
|
892
|
+
try {
|
|
893
|
+
const debugPort = mode === 'debug' ? captcha?.debugPort ?? 9229 : undefined
|
|
894
|
+
const data = await api('/restart-cdp', { ...(debugPort !== undefined ? { debugPort } : {}) })
|
|
895
|
+
/**
|
|
896
|
+
* ⚠ 按 mode 分槽存:普通重启指引和带端口指引**不能互相覆盖**。
|
|
897
|
+
* `__restart` 旧实现是单一槽位,点过「重启指引」再点「开调试端口重启」
|
|
898
|
+
* 会互相顶替,用户看到的那条命令与当前提示语境对不上。
|
|
899
|
+
*/
|
|
900
|
+
const slot = mode === 'debug' ? '__restartDebug' : '__restartPlain'
|
|
901
|
+
setState((prev) => ({
|
|
902
|
+
...prev,
|
|
903
|
+
[slot]: data?.command ?? '未拿到重启指引(请查看插件日志)',
|
|
904
|
+
[`${slot}Note`]: data?.note,
|
|
905
|
+
}))
|
|
906
|
+
} catch (caught) {
|
|
907
|
+
const slot = mode === 'debug' ? '__restartDebug' : '__restartPlain'
|
|
908
|
+
setState((prev) => ({
|
|
909
|
+
...prev,
|
|
910
|
+
[slot]: `获取指引失败:${caught instanceof Error ? caught.message : String(caught)}`,
|
|
911
|
+
}))
|
|
912
|
+
} finally {
|
|
913
|
+
setRestarting(false)
|
|
914
|
+
}
|
|
915
|
+
}, [captcha])
|
|
916
|
+
|
|
917
|
+
if (!Array.isArray(accounts) || accounts.length === 0) return null
|
|
918
|
+
|
|
919
|
+
const captchaReachable = captcha !== undefined && captcha.reachable === true
|
|
920
|
+
|
|
921
|
+
/**
|
|
922
|
+
* 启动命令展示块:**只在这条提示旁边**出现。
|
|
923
|
+
*
|
|
924
|
+
* ⚠ 这些命令是「一份提示配一份上下文」,不能孤零零挂在组底部:
|
|
925
|
+
* 用户看到的是「连不上调试端口」这行字,要复制的东西就得在它旁边,
|
|
926
|
+
* 否则得来回滚动、还得自己判断该用哪一条。
|
|
927
|
+
*/
|
|
928
|
+
const restartGuide = (slot) =>
|
|
929
|
+
state[slot] !== undefined
|
|
930
|
+
? h(
|
|
931
|
+
'div',
|
|
932
|
+
{ className: 'zsp-restart-guide' },
|
|
933
|
+
h('div', { className: 'zsp-muted' }, state[`${slot}Note`] ?? '请手动完成重启:'),
|
|
934
|
+
/** 命令用 code 块展示(user-select:all,点一下全选,方便复制)。 */
|
|
935
|
+
h('code', { className: 'zsp-plan-id' }, state[slot]),
|
|
936
|
+
)
|
|
937
|
+
: null
|
|
938
|
+
|
|
939
|
+
return h(
|
|
940
|
+
'div',
|
|
941
|
+
{ className: 'zsp-group' },
|
|
942
|
+
h(
|
|
943
|
+
'div',
|
|
944
|
+
{ className: 'zsp-row' },
|
|
945
|
+
h('span', { className: 'zsp-name' }, '赠送额度'),
|
|
946
|
+
h(
|
|
947
|
+
'span',
|
|
948
|
+
{ className: 'zsp-muted' },
|
|
949
|
+
captchaReachable
|
|
950
|
+
? '展开即自动探测活动(零 captcha),领取才需要 captcha'
|
|
951
|
+
: '展开即自动探测活动;日常调用模型不受影响,只有领取需要 captcha',
|
|
952
|
+
),
|
|
953
|
+
h('span', { className: 'zsp-spacer' }),
|
|
954
|
+
/**
|
|
955
|
+
* ⚠ 两件事别混成一件:
|
|
956
|
+
* - **重载插件**:任何模式下重启都行,与 `--remote-debugging-port` 无关;
|
|
957
|
+
* - **开调试端口**:只为 captcha 载体。
|
|
958
|
+
*
|
|
959
|
+
* 0.6.6 起按钮只做「取指引」:点击后展示可复制的启动命令,由**用户**
|
|
960
|
+
* 自己退出并重开 —— 插件不再杀进程(杀进程 + 计划任务被火绒判毒,
|
|
961
|
+
* 且宿主 Job Object 会连带杀死执行脚本)。
|
|
962
|
+
*/
|
|
963
|
+
captchaReachable
|
|
964
|
+
? h(
|
|
965
|
+
'button',
|
|
966
|
+
{
|
|
967
|
+
className: 'zsp-btn',
|
|
968
|
+
onClick: () => void restart('plain'),
|
|
969
|
+
disabled: busy || restarting,
|
|
970
|
+
title: '生成普通重启指引(重载插件,不带调试端口)',
|
|
971
|
+
},
|
|
972
|
+
restarting ? '获取中…' : '重启指引',
|
|
973
|
+
)
|
|
974
|
+
: h(
|
|
975
|
+
'button',
|
|
976
|
+
{
|
|
977
|
+
className: 'zsp-btn',
|
|
978
|
+
onClick: () => void restart('debug'),
|
|
979
|
+
disabled: busy || restarting,
|
|
980
|
+
title:
|
|
981
|
+
`${captcha?.reason ?? '需要宿主带调试端口启动才能产出 captcha'}。` +
|
|
982
|
+
'点击获取可直接复制的启动命令(由你手动执行)。',
|
|
983
|
+
},
|
|
984
|
+
restarting ? '获取中…' : '开调试端口重启',
|
|
985
|
+
),
|
|
986
|
+
h(
|
|
987
|
+
'button',
|
|
988
|
+
{ className: 'zsp-btn', onClick: () => setOpen((prev) => !prev), disabled: busy },
|
|
989
|
+
open ? '收起' : '领取',
|
|
990
|
+
),
|
|
991
|
+
),
|
|
992
|
+
/**
|
|
993
|
+
* 载体不可用时,**提示句 + 启动命令紧贴在一起**(一句话 + 下面一行可复制命令)。
|
|
994
|
+
* 提示句本身不重复按钮 tooltip 那串长文案:它太长,塞进行内会把按钮挤走。
|
|
995
|
+
*/
|
|
996
|
+
captchaReachable
|
|
997
|
+
? null
|
|
998
|
+
: h(
|
|
999
|
+
'div',
|
|
1000
|
+
{ className: 'zsp-row', style: { alignItems: 'flex-start', flexDirection: 'column', gap: '2px' } },
|
|
1001
|
+
h(
|
|
1002
|
+
'span',
|
|
1003
|
+
{ className: 'zsp-muted', title: captcha?.reason ?? undefined },
|
|
1004
|
+
`连不上宿主调试端口 127.0.0.1:${captcha?.debugPort ?? 9229}:` +
|
|
1005
|
+
'先彻底退出 DSH 再带端口重开(日常对话不受影响)。',
|
|
1006
|
+
),
|
|
1007
|
+
restartGuide('__restartDebug'),
|
|
1008
|
+
),
|
|
1009
|
+
/**
|
|
1010
|
+
* 自动领取拨盘:**说明在左、开关在右**(与其他行的「标签 … 控件」同构)。
|
|
1011
|
+
* ⚠ 顺序不能反过来:右对齐是一列,开关放左边会与上面的按钮错位。
|
|
1012
|
+
*/
|
|
1013
|
+
h(
|
|
1014
|
+
'div',
|
|
1015
|
+
{ className: 'zsp-row', style: { alignItems: 'center' } },
|
|
1016
|
+
h(
|
|
1017
|
+
'span',
|
|
1018
|
+
{ className: 'zsp-muted', style: { flex: '1 1 auto', minWidth: 0 } },
|
|
1019
|
+
autoClaim?.enabled === true
|
|
1020
|
+
? `自动领取已开启:定时探测,发现新活动自动领取${
|
|
1021
|
+
Number.isFinite(autoClaim?.seenCount) && autoClaim.seenCount > 0
|
|
1022
|
+
? `(已记录 ${autoClaim.seenCount} 个,不会重复领)`
|
|
1023
|
+
: ''
|
|
1024
|
+
}`
|
|
1025
|
+
: '自动领取已关闭:探测仍然自动,领取需手动点',
|
|
1026
|
+
),
|
|
1027
|
+
h(Toggle, {
|
|
1028
|
+
checked: autoClaim?.enabled === true,
|
|
1029
|
+
disabled: busy || togglingAuto,
|
|
1030
|
+
onChange: () => void toggleAutoClaim(),
|
|
1031
|
+
label: '自动领取',
|
|
1032
|
+
}),
|
|
1033
|
+
),
|
|
1034
|
+
/**
|
|
1035
|
+
* 载体已就绪时,只有**用户主动点「重启指引」**才在拨盘下方出普通重启命令;
|
|
1036
|
+
* 带端口那份永远贴着上面的提示行,两份互不顶替(按 mode 分槽)。
|
|
1037
|
+
*/
|
|
1038
|
+
restartGuide('__restartPlain'),
|
|
1039
|
+
open
|
|
1040
|
+
? accounts.map((account) => {
|
|
1041
|
+
const slot = state[account.id] ?? {}
|
|
1042
|
+
/**
|
|
1043
|
+
* ⚠ 这里**没有 plan_id 输入框**:探测是自动的(展开即探),
|
|
1044
|
+
* plan_id 只作为**只读**标识展示。手工粘贴活动 id 的路径已移除 ——
|
|
1045
|
+
* 它只会让人填错、填过期的 id,然后白烧一次 captcha。
|
|
1046
|
+
*/
|
|
1047
|
+
const plans = Array.isArray(slot.plans) ? slot.plans : []
|
|
1048
|
+
/**
|
|
1049
|
+
* ⚠ 服务端领取成功后不再投放该活动(deliveries 变空)⇒ 已领账号靠
|
|
1050
|
+
* `billing/balance` 的领取记录补显(claimed: true)。一视同仁:
|
|
1051
|
+
* 每个账号都显示全部已知活动,只是已领的没有领取按钮。
|
|
1052
|
+
*
|
|
1053
|
+
* ⚠ 刚领到的活动落在 `pending[]`(到期才进 `plans`),路由层已合并并带
|
|
1054
|
+
* `pending: true` / `effectiveAt`。这里要把它显示成「已领取 · 待生效 + 时间」,
|
|
1055
|
+
* 否则用户只会看到一行无名灰字,仍认为「没有活动」。
|
|
1056
|
+
*/
|
|
1057
|
+
const claimable = plans.find((plan) => plan.claimed !== true)
|
|
1058
|
+
const planId = claimable?.planId ?? plans[0]?.planId
|
|
1059
|
+
const allClaimed = plans.length > 0 && claimable === undefined
|
|
1060
|
+
const pendingPlan = plans.find((plan) => plan.pending === true)
|
|
1061
|
+
const formatWhen = (ms) =>
|
|
1062
|
+
typeof ms === 'number' && Number.isFinite(ms)
|
|
1063
|
+
? new Date(ms).toLocaleString([], { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' })
|
|
1064
|
+
: undefined
|
|
1065
|
+
/** 三态文案:探测失败 / 已领(待生效)/ 确实没有活动 —— 三者不可混说。 */
|
|
1066
|
+
const emptyHint =
|
|
1067
|
+
slot.emptyReason === 'failed'
|
|
1068
|
+
? slot.error
|
|
1069
|
+
: slot.emptyReason === 'none'
|
|
1070
|
+
? '服务端此刻没有向你投放活动(投放源与领取记录均为空)'
|
|
1071
|
+
: '当前没有探测到可领取的活动'
|
|
1072
|
+
return h(
|
|
1073
|
+
'div',
|
|
1074
|
+
{ key: account.id, className: 'zsp-row', style: { alignItems: 'flex-start' } },
|
|
1075
|
+
h('span', { className: 'zsp-muted', style: { minWidth: '96px' } }, `#${account.index} ${account.nickname}`),
|
|
1076
|
+
h(
|
|
1077
|
+
'div',
|
|
1078
|
+
{ className: 'zsp-meter', style: { flex: '1 1 auto' } },
|
|
1079
|
+
h('code', { className: 'zsp-plan-id' }, planId ?? '—'),
|
|
1080
|
+
typeof plans[0]?.name === 'string' && plans[0].name.length > 0
|
|
1081
|
+
? h('span', { className: 'zsp-muted' }, plans[0].name)
|
|
1082
|
+
: null,
|
|
1083
|
+
typeof plans[0]?.grantUnits === 'number'
|
|
1084
|
+
? h('span', { className: 'zsp-muted' }, `+${plans[0].grantUnits.toLocaleString()} ${plans[0].unitType ?? ''}`.trim())
|
|
1085
|
+
: null,
|
|
1086
|
+
slot.probing === true
|
|
1087
|
+
? h('span', { className: 'zsp-muted' }, '探测中…')
|
|
1088
|
+
: plans.length > 1
|
|
1089
|
+
? h('span', { className: 'zsp-muted' }, `等 ${plans.length} 个活动`)
|
|
1090
|
+
: null,
|
|
1091
|
+
allClaimed && slot.loading !== true
|
|
1092
|
+
? h(
|
|
1093
|
+
'span',
|
|
1094
|
+
{ className: 'zsp-muted' },
|
|
1095
|
+
pendingPlan === undefined
|
|
1096
|
+
? '已领取'
|
|
1097
|
+
: `已领取 · 待生效${formatWhen(pendingPlan.effectiveAt) === undefined ? '' : ` ${formatWhen(pendingPlan.effectiveAt)}`}${formatWhen(pendingPlan.expiresAt) === undefined ? '' : ` · ${formatWhen(pendingPlan.expiresAt)} 过期`}`,
|
|
1098
|
+
)
|
|
1099
|
+
: null,
|
|
1100
|
+
slot.loading === true
|
|
1101
|
+
? h('span', { className: 'zsp-muted' }, '领取中…(captcha 失败会自动重试一次)')
|
|
1102
|
+
: slot.error !== undefined
|
|
1103
|
+
? h('span', { className: slot.emptyReason === 'failed' ? 'zsp-error' : 'zsp-muted' }, slot.error)
|
|
1104
|
+
: slot.results !== undefined
|
|
1105
|
+
? h(
|
|
1106
|
+
'span',
|
|
1107
|
+
{ className: 'zsp-muted' },
|
|
1108
|
+
slot.results
|
|
1109
|
+
.map((item) => {
|
|
1110
|
+
/** 忠实回传服务端裁决,不做含糊翻译。 */
|
|
1111
|
+
if (item.ok === true) {
|
|
1112
|
+
return `✓ 领取成功${item.alreadyClaimed === true ? '(此前已领过,幂等)' : ''}`
|
|
1113
|
+
}
|
|
1114
|
+
/**
|
|
1115
|
+
* 风控(3012 → `RISK_BLOCKED`)单独一句(1.0.2)。
|
|
1116
|
+
*
|
|
1117
|
+
* 这不是普通的「失败了」:宿主在抛出 RISK_BLOCKED 前
|
|
1118
|
+
* **已自动清掉**该账号该模型的限流标记(pool.clearModelRateLimits,
|
|
1119
|
+
* 见 plan-client 的 risk 分支)—— 也就是说用户此刻就能重试。
|
|
1120
|
+
* 把「已自动清除…可立即重试」说出来,否则用户看到红字只会
|
|
1121
|
+
* 以为账号坏了、去换号或反复点;而 3012 的冷却惩罚是账号级的
|
|
1122
|
+
* (30 分钟 → 24 小时 → 停用),反复重试正是最坏的动作。
|
|
1123
|
+
*/
|
|
1124
|
+
if (item.code === 'RISK_BLOCKED') {
|
|
1125
|
+
return '⚠ 上游风控拦截(3012,非额度问题):已自动清除本账号限流标记,可立即重试'
|
|
1126
|
+
}
|
|
1127
|
+
const code = item.code !== undefined ? `code ${item.code}` : `HTTP ${item.httpStatus}`
|
|
1128
|
+
return `✗ ${code}:${item.message ?? item.error ?? '未知原因'}`
|
|
1129
|
+
})
|
|
1130
|
+
.join(';'),
|
|
1131
|
+
)
|
|
1132
|
+
: null,
|
|
1133
|
+
),
|
|
1134
|
+
h(
|
|
1135
|
+
'div',
|
|
1136
|
+
{ className: 'zsp-actions', style: { marginTop: 0 } },
|
|
1137
|
+
h(
|
|
1138
|
+
'button',
|
|
1139
|
+
{
|
|
1140
|
+
className: 'zsp-btn',
|
|
1141
|
+
onClick: () => void claim(account.id),
|
|
1142
|
+
disabled: busy || slot.loading === true || slot.probing === true || claimable === undefined,
|
|
1143
|
+
title: claimable === undefined
|
|
1144
|
+
? (allClaimed
|
|
1145
|
+
? (pendingPlan === undefined ? '该账号已领取全部已知活动' : '该账号已领取该活动,正在待生效期(到点会计入额度)')
|
|
1146
|
+
: emptyHint)
|
|
1147
|
+
: `领取 ${claimable.planId}`,
|
|
1148
|
+
},
|
|
1149
|
+
slot.loading === true ? '领取中…' : allClaimed ? (pendingPlan === undefined ? '已领取' : '已领取 · 待生效') : '领取',
|
|
1150
|
+
),
|
|
1151
|
+
),
|
|
1152
|
+
)
|
|
1153
|
+
})
|
|
1154
|
+
: null,
|
|
1155
|
+
)
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
/**
|
|
1159
|
+
* 池子**总额度**行(0.9.3 置顶,0.10.0 纳入未生效额度):把各账号已加载的
|
|
1160
|
+
* 余额桶求和,按「到期时间分层」显示 —— 快过期的一组、日重置的一组。
|
|
1161
|
+
*
|
|
1162
|
+
* 为什么未生效的(`pending`,已领没到点)也要算进总额度:用户问的是
|
|
1163
|
+
* 「池子一共有多少 token」,不是「此刻能立刻花多少」。漏掉 pending 时,
|
|
1164
|
+
* 领取成功的 3 亿包在到点前完全不计入,总额度看起来像没领到。
|
|
1165
|
+
* 但两者语义不同(一个现在就能用,一个要等到点),所以:
|
|
1166
|
+
* - 分母 = 已生效 total + 未生效 grantUnits,分子只算**现在可用**的剩余;
|
|
1167
|
+
* - 未生效部分按**生效日**单独分层(带条纹),不提前并进现有分层 ——
|
|
1168
|
+
* 否则「3 亿 / 3 亿」出现在一个今天到期的层里,读起来像马上就能花。
|
|
1169
|
+
*/
|
|
1170
|
+
function TotalQuota(props) {
|
|
1171
|
+
const { accounts, balances } = props
|
|
1172
|
+
if (!Array.isArray(accounts) || accounts.length === 0) return null
|
|
1173
|
+
const entries = accounts
|
|
1174
|
+
.map((account) => balances?.[account.id])
|
|
1175
|
+
.filter((balance) => balance?.ok === true && balance.enterprise !== true)
|
|
1176
|
+
if (entries.length === 0) return null
|
|
1177
|
+
/** 按到期日分层:同一天到期的桶合并成一组(日重置桶全部落在 23:59)。 */
|
|
1178
|
+
const byDay = new Map()
|
|
1179
|
+
/** 未生效额度按生效日分层(`expiresAt` 还是整份 plan 的作废时间)。 */
|
|
1180
|
+
const pendingByDay = new Map()
|
|
1181
|
+
let loaded = 0
|
|
1182
|
+
let pendingUnits = 0
|
|
1183
|
+
for (const balance of entries) {
|
|
1184
|
+
const buckets = Array.isArray(balance.buckets) && balance.buckets.length > 0
|
|
1185
|
+
? balance.buckets
|
|
1186
|
+
: [{ remaining: balance.remaining, total: balance.total, expiresAt: balance.expiresAt }]
|
|
1187
|
+
for (const bucket of buckets) {
|
|
1188
|
+
const remaining = typeof bucket.remaining === 'number' ? bucket.remaining : 0
|
|
1189
|
+
const total = typeof bucket.total === 'number' ? bucket.total : 0
|
|
1190
|
+
if (total <= 0) continue
|
|
1191
|
+
loaded += 1
|
|
1192
|
+
const day = typeof bucket.expiresAt === 'number' && bucket.expiresAt > 0
|
|
1193
|
+
? new Date(bucket.expiresAt).toISOString().slice(0, 10)
|
|
1194
|
+
: '—'
|
|
1195
|
+
const entry = byDay.get(day) ?? { remaining: 0, total: 0, expiresAt: undefined }
|
|
1196
|
+
entry.remaining += remaining
|
|
1197
|
+
entry.total += total
|
|
1198
|
+
if (typeof bucket.expiresAt === 'number' && bucket.expiresAt > 0) {
|
|
1199
|
+
entry.expiresAt = entry.expiresAt === undefined ? bucket.expiresAt : Math.min(entry.expiresAt, bucket.expiresAt)
|
|
1200
|
+
}
|
|
1201
|
+
byDay.set(day, entry)
|
|
1202
|
+
}
|
|
1203
|
+
for (const item of Array.isArray(balance.pending) ? balance.pending : []) {
|
|
1204
|
+
const units = Number(item?.grantUnits)
|
|
1205
|
+
if (!Number.isFinite(units) || units <= 0) continue
|
|
1206
|
+
pendingUnits += units
|
|
1207
|
+
const effectiveAt = typeof item.effectiveAt === 'number' && item.effectiveAt > 0 ? item.effectiveAt : undefined
|
|
1208
|
+
const key = effectiveAt === undefined ? '—' : new Date(effectiveAt).toISOString().slice(0, 10)
|
|
1209
|
+
const entry = pendingByDay.get(key) ?? { units: 0, effectiveAt: undefined, expiresAt: undefined }
|
|
1210
|
+
entry.units += units
|
|
1211
|
+
if (effectiveAt !== undefined) {
|
|
1212
|
+
entry.effectiveAt = entry.effectiveAt === undefined ? effectiveAt : Math.min(entry.effectiveAt, effectiveAt)
|
|
1213
|
+
}
|
|
1214
|
+
if (typeof item.expiresAt === 'number' && item.expiresAt > 0) {
|
|
1215
|
+
entry.expiresAt = entry.expiresAt === undefined ? item.expiresAt : Math.min(entry.expiresAt, item.expiresAt)
|
|
1216
|
+
}
|
|
1217
|
+
pendingByDay.set(key, entry)
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
if (loaded === 0 && pendingUnits <= 0) return null
|
|
1221
|
+
const groups = [...byDay.entries()].sort((a, b) => (a[1].expiresAt ?? Infinity) - (b[1].expiresAt ?? Infinity))
|
|
1222
|
+
const pendingGroups = [...pendingByDay.entries()]
|
|
1223
|
+
.sort((a, b) => (a[1].effectiveAt ?? Infinity) - (b[1].effectiveAt ?? Infinity))
|
|
1224
|
+
/** 分子只有已经能花的;分母含待生效 —— 否则「待生效」那几行等于没进总额度。 */
|
|
1225
|
+
const totalRemaining = groups.reduce((sum, [, g]) => sum + g.remaining, 0)
|
|
1226
|
+
const activeAll = groups.reduce((sum, [, g]) => sum + g.total, 0)
|
|
1227
|
+
const totalAll = activeAll + pendingUnits
|
|
1228
|
+
const pendingLabel = pendingUnits > 0 ? ` · 其中待生效 ${formatTokens(pendingUnits)}` : ''
|
|
1229
|
+
return h(
|
|
1230
|
+
'div',
|
|
1231
|
+
{ className: 'zsp-group zsp-total' },
|
|
1232
|
+
h(
|
|
1233
|
+
'div',
|
|
1234
|
+
{ className: 'zsp-row' },
|
|
1235
|
+
h('span', { className: 'zsp-name' }, '总额度'),
|
|
1236
|
+
h(
|
|
1237
|
+
'span',
|
|
1238
|
+
{ className: 'zsp-muted' },
|
|
1239
|
+
`${formatTokens(totalRemaining)} / ${formatTokens(totalAll)} tokens${pendingLabel} · ${entries.length}/${accounts.length} 个账号已加载`,
|
|
1240
|
+
),
|
|
1241
|
+
),
|
|
1242
|
+
groups.map(([day, group]) =>
|
|
1243
|
+
h(
|
|
1244
|
+
'div',
|
|
1245
|
+
{ key: day, className: 'zsp-row', style: { paddingLeft: '18px' } },
|
|
1246
|
+
h('span', { className: 'zsp-muted', style: { minWidth: '150px' } },
|
|
1247
|
+
`${day === '—' ? '无到期' : formatEffective(group.expiresAt)} 到期`),
|
|
1248
|
+
h(
|
|
1249
|
+
'div',
|
|
1250
|
+
{ className: 'zsp-meter', style: { flex: '1 1 auto' } },
|
|
1251
|
+
h('div', { className: 'zsp-bar' },
|
|
1252
|
+
h('span', { style: { width: `${group.total > 0 ? Math.max(0, Math.min(100, Math.round((group.remaining / group.total) * 100))) : 0}%` } })),
|
|
1253
|
+
h('span', { className: 'zsp-muted zsp-meter-caption' },
|
|
1254
|
+
`${formatTokens(group.remaining)} / ${formatTokens(group.total)}`),
|
|
1255
|
+
),
|
|
1256
|
+
),
|
|
1257
|
+
),
|
|
1258
|
+
/**
|
|
1259
|
+
* 未生效额度单独分层:条纹 + 生效时间。与上方实色分层并排的用意是
|
|
1260
|
+
* 「总额度里有多少还没到点」一眼可见 —— 合并进同一层会误导成现在可花。
|
|
1261
|
+
*/
|
|
1262
|
+
pendingGroups.map(([key, group]) =>
|
|
1263
|
+
h(
|
|
1264
|
+
'div',
|
|
1265
|
+
{ key: `pending-${key}`, className: 'zsp-row', style: { paddingLeft: '18px' } },
|
|
1266
|
+
h('span', { className: 'zsp-muted', style: { minWidth: '150px' } },
|
|
1267
|
+
`${group.effectiveAt === undefined ? '待生效' : `${formatEffective(group.effectiveAt)} 起可用`}`),
|
|
1268
|
+
h(
|
|
1269
|
+
'div',
|
|
1270
|
+
{ className: 'zsp-meter', style: { flex: '1 1 auto' } },
|
|
1271
|
+
h('div', { className: 'zsp-bar', 'data-pending': true },
|
|
1272
|
+
h('span', { style: { width: '100%' } })),
|
|
1273
|
+
h('span', { className: 'zsp-muted zsp-meter-caption' },
|
|
1274
|
+
`${formatTokens(group.units)} tokens 待生效${group.expiresAt === undefined ? '' : ` · ${formatEffective(group.expiresAt)} 到期`}`),
|
|
1275
|
+
),
|
|
1276
|
+
),
|
|
1277
|
+
),
|
|
1278
|
+
)
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
/**
|
|
1282
|
+
* 单个账号卡片:状态点、可编辑昵称、渠道、限流 chips、余额条、操作按钮。
|
|
1283
|
+
* 按钮只有两个:「使用」(置顶,下次请求即用它)与「删除」。
|
|
1284
|
+
*/
|
|
1285
|
+
function AccountCard(props) {
|
|
1286
|
+
const { account, balance, busy, modelListVisible, onToggleModelList, confirmId, setConfirmId, onRemove, onRename, onResetLimit } = props
|
|
1287
|
+
/** Host 下发的是 `modelRateLimits` 对象(model → resetAt),摊平成 chips。 */
|
|
1288
|
+
const limited = Object.entries(account.modelRateLimits ?? {})
|
|
1289
|
+
.map(([model, resetAt]) => ({ model, resetAt }))
|
|
1290
|
+
.filter((item) => Number(item.resetAt) > Date.now())
|
|
1291
|
+
/**
|
|
1292
|
+
* 风控痕迹(1.0.2):`/summary` 的账号视图带 `riskBlocked`(来源 `last_error`
|
|
1293
|
+
* 里是否出现过 3012 / unusual activity,见 manage-routes 的 riskBlockedOf)。
|
|
1294
|
+
*
|
|
1295
|
+
* 为什么值得一条 chip:3012 的惩罚是**账号级冷却**(30 分钟 → 24 小时 →
|
|
1296
|
+
* 停用),而用户从界面上看不出任何区别 —— 他会一直点领取/重试,正是最坏的
|
|
1297
|
+
* 动作。chip 兼作「清除限流」入口:它本身是按钮,点一下清掉本账号全部标记
|
|
1298
|
+
* (含 `last_error`),账号立刻恢复可用。
|
|
1299
|
+
*
|
|
1300
|
+
* 注意与上面 `limited` 并存是**正常**的:风控与额度耗尽可以同时发生。
|
|
1301
|
+
*/
|
|
1302
|
+
const riskBlocked = account.riskBlocked === true
|
|
1303
|
+
return h(
|
|
1304
|
+
'div',
|
|
1305
|
+
{ className: 'zsp-card', 'data-hidden': modelListVisible === false },
|
|
1306
|
+
h(
|
|
1307
|
+
'div',
|
|
1308
|
+
{ className: 'zsp-row' },
|
|
1309
|
+
h('span', { className: 'zsp-dot', 'data-on': account.enabled === true }),
|
|
1310
|
+
h(NicknameEdit, {
|
|
1311
|
+
nickname: account.nickname,
|
|
1312
|
+
busy,
|
|
1313
|
+
onCommit: (next) => onRename(next),
|
|
1314
|
+
}),
|
|
1315
|
+
h(
|
|
1316
|
+
'span',
|
|
1317
|
+
{ className: 'zsp-muted' },
|
|
1318
|
+
`#${account.index} · ${channelLabel(account.channel)}`,
|
|
1319
|
+
),
|
|
1320
|
+
limited.map((item) =>
|
|
1321
|
+
h(
|
|
1322
|
+
'span',
|
|
1323
|
+
{
|
|
1324
|
+
key: item.model,
|
|
1325
|
+
className: 'zsp-chip',
|
|
1326
|
+
'data-limited': 'true',
|
|
1327
|
+
title: '该账号在此模型上被上游判为额度用尽(1005/零内容),到达限时间自动解除;若模型实际可用,点 × 立即清除误判',
|
|
1328
|
+
},
|
|
1329
|
+
`${item.model} 限额至 ${formatReset(item.resetAt)}`,
|
|
1330
|
+
/** 清除误判(1.0.1):「零内容两次升级」可能仍冤枉实际可用的模型,
|
|
1331
|
+
* chip 上的 × 是唯一不依赖 CLI 的自救入口。 */
|
|
1332
|
+
h(
|
|
1333
|
+
'button',
|
|
1334
|
+
{
|
|
1335
|
+
type: 'button',
|
|
1336
|
+
className: 'zsp-chip-x',
|
|
1337
|
+
'aria-label': `清除 ${item.model} 的限流标记`,
|
|
1338
|
+
disabled: busy,
|
|
1339
|
+
onClick: () => onResetLimit?.(item.model),
|
|
1340
|
+
},
|
|
1341
|
+
'×',
|
|
1342
|
+
),
|
|
1343
|
+
),
|
|
1344
|
+
),
|
|
1345
|
+
/**
|
|
1346
|
+
* 风控 chip(1.0.2):**在模型栏这一行**(与限流 chip 同排),点它 = 清掉
|
|
1347
|
+
* 本账号全部限流标记 + `last_error`,账号立刻可重试。
|
|
1348
|
+
*
|
|
1349
|
+
* 文案写「曾触发上游风控」而不是「被风控封禁」:3012 也可能是误判
|
|
1350
|
+
* (用户实测拿到 3012 后同一请求重试即成功),把它说成封禁会让人白白换号。
|
|
1351
|
+
*/
|
|
1352
|
+
riskBlocked
|
|
1353
|
+
? h(
|
|
1354
|
+
'button',
|
|
1355
|
+
{
|
|
1356
|
+
type: 'button',
|
|
1357
|
+
key: '__risk',
|
|
1358
|
+
className: 'zsp-chip',
|
|
1359
|
+
'data-risk': 'true',
|
|
1360
|
+
disabled: busy,
|
|
1361
|
+
title: '该账号曾触发上游风控(3012 unusual activity),可点清除限流恢复(清掉本账号全部限流标记与错误文案;风控不是额度问题)',
|
|
1362
|
+
onClick: () => onResetLimit?.(undefined),
|
|
1363
|
+
},
|
|
1364
|
+
'⚠ 曾触发风控 · 点清除限流恢复',
|
|
1365
|
+
)
|
|
1366
|
+
: null,
|
|
1367
|
+
/** 每栏最右侧的开关:负 margin 拉出卡片缩进,与统一行的开关同列。 */
|
|
1368
|
+
h(Toggle, {
|
|
1369
|
+
checked: modelListVisible === true,
|
|
1370
|
+
disabled: busy,
|
|
1371
|
+
onChange: (next) => onToggleModelList(next),
|
|
1372
|
+
}),
|
|
1373
|
+
),
|
|
1374
|
+
balance === undefined
|
|
1375
|
+
? null
|
|
1376
|
+
: balance.ok === true
|
|
1377
|
+
? balance.enterprise === true
|
|
1378
|
+
? h('span', { className: 'zsp-muted' }, '企业版:服务端不下发用量数字')
|
|
1379
|
+
: h(
|
|
1380
|
+
React.Fragment,
|
|
1381
|
+
null,
|
|
1382
|
+
/**
|
|
1383
|
+
* 额度桶**逐个**画条(0.6.8):不同桶到期时间不同,
|
|
1384
|
+
* 汇总条只会显示最早的那个,其余被吞掉。
|
|
1385
|
+
* 单桶时与旧视图一致;多桶时每桶一条 + 自己的到期时间。
|
|
1386
|
+
* (buckets 缺失 = 旧宿主 → 回退汇总条。)
|
|
1387
|
+
*/
|
|
1388
|
+
Array.isArray(balance.buckets) && balance.buckets.length > 1
|
|
1389
|
+
? balance.buckets.map((bucket, index) =>
|
|
1390
|
+
h(QuotaMeter, {
|
|
1391
|
+
key: `${bucket.planId ?? 'bucket'}-${index}`,
|
|
1392
|
+
remaining: bucket.remaining,
|
|
1393
|
+
total: bucket.total,
|
|
1394
|
+
planName: bucket.showName ?? `额度桶 ${index + 1}`,
|
|
1395
|
+
expiresAt: bucket.expiresAt,
|
|
1396
|
+
}),
|
|
1397
|
+
)
|
|
1398
|
+
: h(QuotaMeter, {
|
|
1399
|
+
remaining: balance.remaining,
|
|
1400
|
+
total: balance.total,
|
|
1401
|
+
planName: balance.planName,
|
|
1402
|
+
expiresAt: balance.expiresAt,
|
|
1403
|
+
}),
|
|
1404
|
+
(balance.pending ?? []).map((item, index) =>
|
|
1405
|
+
h(
|
|
1406
|
+
React.Fragment,
|
|
1407
|
+
{ key: `${item.planId ?? 'plan'}-${index}` },
|
|
1408
|
+
h(QuotaMeter, {
|
|
1409
|
+
pending: true,
|
|
1410
|
+
effectiveAt: item.effectiveAt,
|
|
1411
|
+
expiresAt: item.expiresAt,
|
|
1412
|
+
planName: item.showName,
|
|
1413
|
+
}),
|
|
1414
|
+
h(
|
|
1415
|
+
'span',
|
|
1416
|
+
{ className: 'zsp-muted', style: { fontSize: '11px' } },
|
|
1417
|
+
`${item.showName ?? '额度'} ${formatPendingUnits(item)} tokens`,
|
|
1418
|
+
),
|
|
1419
|
+
),
|
|
1420
|
+
),
|
|
1421
|
+
)
|
|
1422
|
+
: h('span', { className: 'zsp-error' }, balance.error ?? '余额查询失败'),
|
|
1423
|
+
h(
|
|
1424
|
+
'div',
|
|
1425
|
+
{ className: 'zsp-actions' },
|
|
1426
|
+
/**
|
|
1427
|
+
* ⚠ 0.7.0 移除「使用」按钮:统一选号默认按**到期时间**取号
|
|
1428
|
+
* (快过期先用,config.selectionOrder),手动置顶不再决定顺序 ——
|
|
1429
|
+
* 按钮也就失去了意义(0.6.9 的「点了没作用」反馈即源于此)。
|
|
1430
|
+
* 钉定栏位固定吃指定账号,本就不需要置顶。
|
|
1431
|
+
*/
|
|
1432
|
+
confirmId === account.id
|
|
1433
|
+
? h(
|
|
1434
|
+
'button',
|
|
1435
|
+
{
|
|
1436
|
+
className: 'zsp-btn',
|
|
1437
|
+
'data-danger': 'true',
|
|
1438
|
+
disabled: busy,
|
|
1439
|
+
onClick: () => {
|
|
1440
|
+
setConfirmId(undefined)
|
|
1441
|
+
onRemove()
|
|
1442
|
+
},
|
|
1443
|
+
},
|
|
1444
|
+
'确认删除?',
|
|
1445
|
+
)
|
|
1446
|
+
: h(
|
|
1447
|
+
'button',
|
|
1448
|
+
{
|
|
1449
|
+
className: 'zsp-btn',
|
|
1450
|
+
'data-danger': 'true',
|
|
1451
|
+
disabled: busy,
|
|
1452
|
+
onClick: () => {
|
|
1453
|
+
setConfirmId(account.id)
|
|
1454
|
+
setTimeout(
|
|
1455
|
+
() => setConfirmId((current) => (current === account.id ? undefined : current)),
|
|
1456
|
+
CONFIRM_RESET_MS,
|
|
1457
|
+
)
|
|
1458
|
+
},
|
|
1459
|
+
},
|
|
1460
|
+
'删除',
|
|
1461
|
+
),
|
|
1462
|
+
),
|
|
1463
|
+
)
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* 渠道选项(登录渠道与 lib/upstream-api.js 的 LOGIN_CHANNELS 对齐)。
|
|
1468
|
+
*/
|
|
1469
|
+
const CHANNEL_OPTIONS = [
|
|
1470
|
+
{ value: 'bigmodel', label: 'bigmodel', desc: '智谱开放平台 · bigmodel.cn', badge: '智' },
|
|
1471
|
+
{ value: 'zai', label: 'zai', desc: 'Z.AI · z.ai', badge: 'Z' },
|
|
1472
|
+
]
|
|
1473
|
+
|
|
1474
|
+
/**
|
|
1475
|
+
* 现代化下拉(零依赖手写):渠道徽标 + 两行选项 + hover 高亮 +
|
|
1476
|
+
* 点击外部关闭;不做键盘导航(用不到,从简)。
|
|
1477
|
+
*/
|
|
1478
|
+
function ChannelSelect(props) {
|
|
1479
|
+
const { value, onChange, disabled } = props
|
|
1480
|
+
const [open, setOpen] = useState(false)
|
|
1481
|
+
const rootRef = useRef(undefined)
|
|
1482
|
+
|
|
1483
|
+
const selected = CHANNEL_OPTIONS.find((item) => item.value === value) ?? CHANNEL_OPTIONS[0]
|
|
1484
|
+
|
|
1485
|
+
useEffect(() => {
|
|
1486
|
+
if (open !== true) return undefined
|
|
1487
|
+
const onPointerDown = (event) => {
|
|
1488
|
+
if (rootRef.current !== undefined && !rootRef.current.contains(event.target)) setOpen(false)
|
|
1489
|
+
}
|
|
1490
|
+
document.addEventListener('pointerdown', onPointerDown, true)
|
|
1491
|
+
return () => document.removeEventListener('pointerdown', onPointerDown, true)
|
|
1492
|
+
}, [open])
|
|
1493
|
+
|
|
1494
|
+
const commit = useCallback(
|
|
1495
|
+
(nextValue) => {
|
|
1496
|
+
if (!CHANNEL_OPTIONS.some((item) => item.value === nextValue)) return
|
|
1497
|
+
onChange(nextValue)
|
|
1498
|
+
setOpen(false)
|
|
1499
|
+
},
|
|
1500
|
+
[onChange],
|
|
1501
|
+
)
|
|
1502
|
+
|
|
1503
|
+
const onKeyDown = useCallback(
|
|
1504
|
+
(event) => {
|
|
1505
|
+
/** 键盘只保留 Esc 关闭(一般用不到键盘选渠道,从简)。 */
|
|
1506
|
+
if (event.key === 'Escape') setOpen(false)
|
|
1507
|
+
else if (event.key === 'Tab') setOpen(false)
|
|
1508
|
+
},
|
|
1509
|
+
[],
|
|
1510
|
+
)
|
|
1511
|
+
|
|
1512
|
+
return h(
|
|
1513
|
+
'div',
|
|
1514
|
+
{ className: 'zsp-select', ref: rootRef, onKeyDown },
|
|
1515
|
+
h(
|
|
1516
|
+
'button',
|
|
1517
|
+
{
|
|
1518
|
+
type: 'button',
|
|
1519
|
+
className: 'zsp-select-trigger',
|
|
1520
|
+
'aria-haspopup': 'listbox',
|
|
1521
|
+
'aria-expanded': open === true,
|
|
1522
|
+
disabled,
|
|
1523
|
+
onClick: () => setOpen((current) => !current),
|
|
1524
|
+
},
|
|
1525
|
+
h('span', { className: 'zsp-badge' }, selected.badge),
|
|
1526
|
+
h('span', { className: 'zsp-select-label' }, selected.label),
|
|
1527
|
+
h('span', { className: 'zsp-select-desc' }, selected.desc),
|
|
1528
|
+
h('span', { className: 'zsp-caret', 'aria-hidden': 'true' }),
|
|
1529
|
+
),
|
|
1530
|
+
open === true
|
|
1531
|
+
? h(
|
|
1532
|
+
'div',
|
|
1533
|
+
{ className: 'zsp-select-menu', role: 'listbox', 'aria-label': '登录渠道' },
|
|
1534
|
+
CHANNEL_OPTIONS.map((option) =>
|
|
1535
|
+
h(
|
|
1536
|
+
'button',
|
|
1537
|
+
{
|
|
1538
|
+
key: option.value,
|
|
1539
|
+
type: 'button',
|
|
1540
|
+
role: 'option',
|
|
1541
|
+
className: 'zsp-option',
|
|
1542
|
+
'aria-selected': option.value === value,
|
|
1543
|
+
onClick: () => commit(option.value),
|
|
1544
|
+
},
|
|
1545
|
+
h('span', { className: 'zsp-badge' }, option.badge),
|
|
1546
|
+
h(
|
|
1547
|
+
'span',
|
|
1548
|
+
{ className: 'zsp-option-text' },
|
|
1549
|
+
h('span', null, option.label),
|
|
1550
|
+
h('span', { className: 'zsp-select-desc' }, option.desc),
|
|
1551
|
+
),
|
|
1552
|
+
h('span', { className: 'zsp-option-check', 'aria-hidden': 'true' }, '✓'),
|
|
1553
|
+
),
|
|
1554
|
+
),
|
|
1555
|
+
)
|
|
1556
|
+
: null,
|
|
1557
|
+
)
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
/**
|
|
1561
|
+
* 「添加账号」区块:渠道选择 → 发起授权 → 展示授权链接 → 轮询直到 ready。
|
|
1562
|
+
*
|
|
1563
|
+
* 两段式设备授权与 CLI 相同:Host 持 flowSecret,Client 只拿 authorizeUrl
|
|
1564
|
+
* 和 flowId,凭据永远不经过页面。
|
|
1565
|
+
*/
|
|
1566
|
+
function AddAccountSection(props) {
|
|
1567
|
+
const { busy, onAdded } = props
|
|
1568
|
+
const [channel, setChannel] = useState('bigmodel')
|
|
1569
|
+
const [phase, setPhase] = useState('idle') // idle | starting | waiting | ready | failed
|
|
1570
|
+
const [flow, setFlow] = useState(undefined) // { flowId, authorizeUrl, pollIntervalMs }
|
|
1571
|
+
const [message, setMessage] = useState(undefined)
|
|
1572
|
+
const [copied, setCopied] = useState(false)
|
|
1573
|
+
const timerRef = useRef(undefined)
|
|
1574
|
+
|
|
1575
|
+
useEffect(() => {
|
|
1576
|
+
/** 组件卸载/流程结束时停掉轮询定时器。 */
|
|
1577
|
+
return () => {
|
|
1578
|
+
if (timerRef.current !== undefined) clearTimeout(timerRef.current)
|
|
1579
|
+
}
|
|
1580
|
+
}, [])
|
|
1581
|
+
|
|
1582
|
+
const stopPolling = useCallback(() => {
|
|
1583
|
+
if (timerRef.current !== undefined) {
|
|
1584
|
+
clearTimeout(timerRef.current)
|
|
1585
|
+
timerRef.current = undefined
|
|
1586
|
+
}
|
|
1587
|
+
}, [])
|
|
1588
|
+
|
|
1589
|
+
const poll = useCallback(
|
|
1590
|
+
(flowId, intervalMs) => {
|
|
1591
|
+
timerRef.current = setTimeout(async () => {
|
|
1592
|
+
try {
|
|
1593
|
+
const outcome = await api('/login/poll', { flowId })
|
|
1594
|
+
if (outcome.status === 'pending') {
|
|
1595
|
+
poll(flowId, intervalMs)
|
|
1596
|
+
return
|
|
1597
|
+
}
|
|
1598
|
+
if (outcome.status === 'ready') {
|
|
1599
|
+
setPhase('ready')
|
|
1600
|
+
setMessage(
|
|
1601
|
+
`${outcome.created === false ? '已更新' : '已添加'}账号「${outcome.account?.nickname ?? ''}」`,
|
|
1602
|
+
)
|
|
1603
|
+
stopPolling()
|
|
1604
|
+
if (typeof onAdded === 'function') onAdded()
|
|
1605
|
+
return
|
|
1606
|
+
}
|
|
1607
|
+
setPhase('failed')
|
|
1608
|
+
setMessage(outcome.error ?? '授权失败')
|
|
1609
|
+
} catch (caught) {
|
|
1610
|
+
setPhase('failed')
|
|
1611
|
+
setMessage(caught instanceof Error ? caught.message : String(caught))
|
|
1612
|
+
}
|
|
1613
|
+
}, intervalMs)
|
|
1614
|
+
},
|
|
1615
|
+
[onAdded, stopPolling],
|
|
1616
|
+
)
|
|
1617
|
+
|
|
1618
|
+
const start = useCallback(async () => {
|
|
1619
|
+
stopPolling()
|
|
1620
|
+
setPhase('starting')
|
|
1621
|
+
setMessage(undefined)
|
|
1622
|
+
setCopied(false)
|
|
1623
|
+
try {
|
|
1624
|
+
const started = await api('/login/start', { channel })
|
|
1625
|
+
setFlow({ flowId: started.flowId, authorizeUrl: started.authorizeUrl, pollIntervalMs: started.pollIntervalMs ?? 2000 })
|
|
1626
|
+
setPhase('waiting')
|
|
1627
|
+
setMessage(undefined)
|
|
1628
|
+
poll(started.flowId, Math.max(1500, started.pollIntervalMs ?? 2000))
|
|
1629
|
+
} catch (caught) {
|
|
1630
|
+
setPhase('failed')
|
|
1631
|
+
setMessage(caught instanceof Error ? caught.message : String(caught))
|
|
1632
|
+
}
|
|
1633
|
+
}, [channel, poll, stopPolling])
|
|
1634
|
+
|
|
1635
|
+
const cancel = useCallback(() => {
|
|
1636
|
+
stopPolling()
|
|
1637
|
+
setPhase('idle')
|
|
1638
|
+
setFlow(undefined)
|
|
1639
|
+
setMessage(undefined)
|
|
1640
|
+
}, [stopPolling])
|
|
1641
|
+
|
|
1642
|
+
const copyLink = useCallback(async () => {
|
|
1643
|
+
if (flow?.authorizeUrl === undefined) return
|
|
1644
|
+
try {
|
|
1645
|
+
await navigator.clipboard.writeText(flow.authorizeUrl)
|
|
1646
|
+
setCopied(true)
|
|
1647
|
+
} catch {
|
|
1648
|
+
setCopied(false)
|
|
1649
|
+
}
|
|
1650
|
+
}, [flow])
|
|
1651
|
+
|
|
1652
|
+
return h(
|
|
1653
|
+
'div',
|
|
1654
|
+
{ className: 'zsp-add' },
|
|
1655
|
+
h(
|
|
1656
|
+
'div',
|
|
1657
|
+
{ className: 'zsp-row' },
|
|
1658
|
+
h('span', { className: 'zsp-name' }, '添加账号'),
|
|
1659
|
+
h(ChannelSelect, {
|
|
1660
|
+
value: channel,
|
|
1661
|
+
onChange: setChannel,
|
|
1662
|
+
disabled: busy || phase === 'starting' || phase === 'waiting',
|
|
1663
|
+
}),
|
|
1664
|
+
phase === 'waiting' || phase === 'starting'
|
|
1665
|
+
? h('button', { className: 'zsp-btn', onClick: cancel }, '取消')
|
|
1666
|
+
: h(
|
|
1667
|
+
'button',
|
|
1668
|
+
{ className: 'zsp-btn', onClick: () => void start(), disabled: busy },
|
|
1669
|
+
phase === 'starting' ? '申请中…' : '发起登录',
|
|
1670
|
+
),
|
|
1671
|
+
),
|
|
1672
|
+
phase === 'starting' ? h('span', { className: 'zsp-muted' }, '正在向 ZCode 申请设备授权…') : null,
|
|
1673
|
+
phase === 'waiting' && flow !== undefined
|
|
1674
|
+
? h(
|
|
1675
|
+
React.Fragment,
|
|
1676
|
+
null,
|
|
1677
|
+
h(
|
|
1678
|
+
'span',
|
|
1679
|
+
{ className: 'zsp-muted' },
|
|
1680
|
+
'① 在浏览器打开授权链接并用对应站点的账号登录授权(链接 10 分钟内有效):',
|
|
1681
|
+
),
|
|
1682
|
+
h('a', { className: 'zsp-link', href: flow.authorizeUrl, target: '_blank', rel: 'noreferrer' }, flow.authorizeUrl),
|
|
1683
|
+
h(
|
|
1684
|
+
'div',
|
|
1685
|
+
{ className: 'zsp-row' },
|
|
1686
|
+
h('button', { className: 'zsp-btn', onClick: () => void copyLink() }, copied ? '已复制' : '复制链接'),
|
|
1687
|
+
h('span', { className: 'zsp-muted' }, '② 授权完成后本面板会自动入池(无需其它操作)。'),
|
|
1688
|
+
),
|
|
1689
|
+
)
|
|
1690
|
+
: null,
|
|
1691
|
+
phase === 'ready' ? h('p', { className: 'zsp-ok' }, message) : null,
|
|
1692
|
+
phase === 'failed' ? h('p', { className: 'zsp-error' }, message ?? '授权失败') : null,
|
|
1693
|
+
)
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
/**
|
|
1697
|
+
* 模型栏旁的「当前使用账号」(1.0.0,座位 `conversation.input.right`)。
|
|
1698
|
+
*
|
|
1699
|
+
* ## 何时显示
|
|
1700
|
+
*
|
|
1701
|
+
* **只当选中模型的 provider 属于本插件时**才渲染(workbuddy 徽标同款判定:
|
|
1702
|
+
* 按 `selection.provider` 归属,别的 provider 直接 `return null`)。
|
|
1703
|
+
* provider id 用前缀判 —— 统一栏位是 `zcode-start-plan`,
|
|
1704
|
+
* 账号栏位是 `zcode-start-plan-acct-<账号id>`。
|
|
1705
|
+
*
|
|
1706
|
+
* ## 为什么读路由状态而不是配置
|
|
1707
|
+
*
|
|
1708
|
+
* 统一选号**每次请求都可能换号**(快过期先用 / 忙标记 / 限流切换),
|
|
1709
|
+
* 所以「当前用的是谁」是进程内事实:plan-client 在真正派发前写进
|
|
1710
|
+
* `pool.noteRouted()`,这里读 `/routing` 的 `lastRouted`。
|
|
1711
|
+
*
|
|
1712
|
+
* ⚠ 本组件**不碰**模型座下的任何子座位(那些只被 renderSlot 调用、未声明)。
|
|
1713
|
+
*/
|
|
1714
|
+
function ModelRouteBadge(props) {
|
|
1715
|
+
const { directory } = props || {}
|
|
1716
|
+
|
|
1717
|
+
/**
|
|
1718
|
+
* 订阅宿主 model directory(workbuddy 实证写法):
|
|
1719
|
+
* `directory.store` 是 `{ subscribe, getSnapshot }` 形态的外部 store。
|
|
1720
|
+
* 目录缺席(旧宿主 / 没有会话)时退化为不订阅、不显示。
|
|
1721
|
+
*/
|
|
1722
|
+
const store = directory?.store
|
|
1723
|
+
const subscribe = useCallback(
|
|
1724
|
+
(listener) => (typeof store?.subscribe === 'function' ? store.subscribe(listener) : () => {}),
|
|
1725
|
+
[store],
|
|
1726
|
+
)
|
|
1727
|
+
const snapshot = useCallback(
|
|
1728
|
+
() => (typeof store?.getSnapshot === 'function' ? store.getSnapshot() : undefined),
|
|
1729
|
+
[store],
|
|
1730
|
+
)
|
|
1731
|
+
const selection = useSyncExternalStore(subscribe, snapshot, snapshot)?.current
|
|
1732
|
+
/** 只在「存在选中项」时轮询,避免空会话的空转。 */
|
|
1733
|
+
const hasSelection = selection !== null && selection !== undefined
|
|
1734
|
+
|
|
1735
|
+
/** 只对本插件的模型出现;其它 provider 一律不渲染。 */
|
|
1736
|
+
const providerId = typeof selection?.provider === 'string' ? selection.provider : ''
|
|
1737
|
+
const mine = providerId.startsWith(PROVIDER_PREFIX)
|
|
1738
|
+
/** 账号栏位(`<provider>-acct-<账号id>`)= 钉定;统一栏位 = 自动选号。 */
|
|
1739
|
+
const pinned = providerId.includes('-acct-')
|
|
1740
|
+
const pinnedAccountId = pinned ? providerId.slice(providerId.indexOf('-acct-') + '-acct-'.length) : ''
|
|
1741
|
+
|
|
1742
|
+
const [routing, setRouting] = useState(undefined)
|
|
1743
|
+
const [refreshing, setRefreshing] = useState(false)
|
|
1744
|
+
const mounted = useRef(true)
|
|
1745
|
+
const readSeq = useRef(0)
|
|
1746
|
+
|
|
1747
|
+
/** 卸载闸:`refresh` 的迟到响应不得再写 state。 */
|
|
1748
|
+
useEffect(() => {
|
|
1749
|
+
mounted.current = true
|
|
1750
|
+
return () => {
|
|
1751
|
+
mounted.current = false
|
|
1752
|
+
}
|
|
1753
|
+
}, [])
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* 选中模型(基础 id)。必须带上:候选筛选(限流/忙)与准入判定都按
|
|
1757
|
+
* 「账号 × 模型」判,不带模型时 `/routing` 只能给出不看模型的结论。
|
|
1758
|
+
*/
|
|
1759
|
+
const selectionModel =
|
|
1760
|
+
typeof selection?.model === 'string'
|
|
1761
|
+
? selection.model
|
|
1762
|
+
: typeof selection?.modelId === 'string'
|
|
1763
|
+
? selection.modelId
|
|
1764
|
+
: ''
|
|
1765
|
+
const baseModel = selectionModel.includes('@acct:')
|
|
1766
|
+
? selectionModel.slice(0, selectionModel.indexOf('@acct:'))
|
|
1767
|
+
: selectionModel
|
|
1768
|
+
|
|
1769
|
+
const refresh = useCallback(async (signal) => {
|
|
1770
|
+
const seq = ++readSeq.current
|
|
1771
|
+
const query = baseModel.length > 0 ? `?model=${encodeURIComponent(baseModel)}` : ''
|
|
1772
|
+
const response = await fetch(`${API}/routing${query}`, {
|
|
1773
|
+
credentials: 'same-origin',
|
|
1774
|
+
cache: 'no-store',
|
|
1775
|
+
headers: { accept: 'application/json' },
|
|
1776
|
+
...(signal !== undefined ? { signal } : {}),
|
|
1777
|
+
})
|
|
1778
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
1779
|
+
const value = await response.json().catch(() => undefined)
|
|
1780
|
+
if (value?.ok !== true) throw new Error('routing response invalid')
|
|
1781
|
+
/** 旧响应不覆盖新响应(换号很快时会有并发读)。 */
|
|
1782
|
+
if (mounted.current && signal?.aborted !== true && seq === readSeq.current) setRouting(value)
|
|
1783
|
+
return value
|
|
1784
|
+
}, [baseModel])
|
|
1785
|
+
|
|
1786
|
+
/** 只在「选中本插件模型」时轮询:别的模型下一次请求不会换号,没必要读。 */
|
|
1787
|
+
useEffect(() => {
|
|
1788
|
+
if (!mine || !hasSelection) {
|
|
1789
|
+
setRouting(undefined)
|
|
1790
|
+
return undefined
|
|
1791
|
+
}
|
|
1792
|
+
const controller = new AbortController()
|
|
1793
|
+
refresh(controller.signal).catch(() => {})
|
|
1794
|
+
const timer = setInterval(() => {
|
|
1795
|
+
refresh(controller.signal).catch(() => {})
|
|
1796
|
+
}, ROUTING_POLL_MS)
|
|
1797
|
+
return () => {
|
|
1798
|
+
clearInterval(timer)
|
|
1799
|
+
controller.abort()
|
|
1800
|
+
}
|
|
1801
|
+
}, [mine, hasSelection, refresh])
|
|
1802
|
+
|
|
1803
|
+
/** 换号闪一下:`at` 变化 = 发生过一次真实路由变更。 */
|
|
1804
|
+
const [flash, setFlash] = useState(false)
|
|
1805
|
+
const lastSeenAt = useRef(undefined)
|
|
1806
|
+
useEffect(() => {
|
|
1807
|
+
const at = routing?.lastRouted?.at
|
|
1808
|
+
if (at === undefined) return undefined
|
|
1809
|
+
if (lastSeenAt.current !== undefined && lastSeenAt.current !== at) {
|
|
1810
|
+
setFlash(true)
|
|
1811
|
+
const handle = setTimeout(() => setFlash(false), 700)
|
|
1812
|
+
lastSeenAt.current = at
|
|
1813
|
+
return () => clearTimeout(handle)
|
|
1814
|
+
}
|
|
1815
|
+
lastSeenAt.current = at
|
|
1816
|
+
return undefined
|
|
1817
|
+
}, [routing?.lastRouted?.at])
|
|
1818
|
+
|
|
1819
|
+
if (!mine) return null
|
|
1820
|
+
|
|
1821
|
+
const routed = routing?.lastRouted ?? undefined
|
|
1822
|
+
|
|
1823
|
+
/**
|
|
1824
|
+
* 显示的账号,按优先级取**确定值**,不出现「待路由」这种不确定文案:
|
|
1825
|
+
*
|
|
1826
|
+
* 1. **钉定栏位** → 直接用所选账号 id 在池子里查出昵称
|
|
1827
|
+
* (选了哪个号就显示哪个号,绝不显示别的);
|
|
1828
|
+
* 2. 有真实派发记录且记录的就是这个钉定账号 → 也用记录(带 modelId 提示);
|
|
1829
|
+
* 3. **统一栏位有派发记录** → 显示真实路由到的账号;
|
|
1830
|
+
* 4. 统一栏位还没派发过 → 用 `/routing` 的 `preview`
|
|
1831
|
+
* (与 pick 同一套「快过期先用」排序的第一个 = 下一条会用谁);
|
|
1832
|
+
* 5. 以上都没有(空账号池)→ 才退回状态文案。
|
|
1833
|
+
*/
|
|
1834
|
+
const accountsById = Array.isArray(routing?.accounts) ? routing.accounts : []
|
|
1835
|
+
const pinnedEntry = pinned
|
|
1836
|
+
? accountsById.find((entry) => entry.accountId === pinnedAccountId)
|
|
1837
|
+
: undefined
|
|
1838
|
+
|
|
1839
|
+
/** 统一栏位:真实记录优先,其次 preview。`usedRatio` 一律从 accounts 表取。 */
|
|
1840
|
+
const ratioOf = (accountId) => {
|
|
1841
|
+
const hit = accountsById.find((entry) => entry.accountId === accountId)
|
|
1842
|
+
return typeof hit?.usedRatio === 'number' ? hit.usedRatio : null
|
|
1843
|
+
}
|
|
1844
|
+
const routedAccount =
|
|
1845
|
+
routed !== undefined && (!pinned || routed.accountId === pinnedAccountId)
|
|
1846
|
+
? {
|
|
1847
|
+
nickname: routed.nickname,
|
|
1848
|
+
modelId: routed.modelId,
|
|
1849
|
+
usedRatio: ratioOf(routed.accountId),
|
|
1850
|
+
source: 'routed',
|
|
1851
|
+
}
|
|
1852
|
+
: undefined
|
|
1853
|
+
const previewAccount =
|
|
1854
|
+
!pinned && routedAccount === undefined && routing?.preview !== null && routing?.preview !== undefined
|
|
1855
|
+
? {
|
|
1856
|
+
nickname: routing.preview.nickname,
|
|
1857
|
+
modelId: '',
|
|
1858
|
+
usedRatio:
|
|
1859
|
+
typeof routing.preview.usedRatio === 'number' ? routing.preview.usedRatio : null,
|
|
1860
|
+
source: 'preview',
|
|
1861
|
+
}
|
|
1862
|
+
: undefined
|
|
1863
|
+
const pinnedAccount = pinnedEntry !== undefined
|
|
1864
|
+
? {
|
|
1865
|
+
nickname: pinnedEntry.nickname,
|
|
1866
|
+
modelId: routedAccount?.modelId ?? '',
|
|
1867
|
+
usedRatio: ratioOf(pinnedAccountId),
|
|
1868
|
+
source: 'pinned',
|
|
1869
|
+
}
|
|
1870
|
+
: undefined
|
|
1871
|
+
|
|
1872
|
+
const shown = routedAccount ?? pinnedAccount ?? previewAccount
|
|
1873
|
+
|
|
1874
|
+
/**
|
|
1875
|
+
* **准入状态**(1.0.1 审计修复):面板的「有额度」来自上游余额接口的
|
|
1876
|
+
* **所有**已激活桶,对话准入看的是**当前生效**的桶 + 当日硬过滤标记。
|
|
1877
|
+
* 两者可以同时成立 —— 这正是「本地显示额度开启、对话却提示额度耗尽」。
|
|
1878
|
+
* 徽标必须把这一层显式说出来,而不是继续把该账号显示成健康账号。
|
|
1879
|
+
*/
|
|
1880
|
+
const quotaOf = (accountId) => {
|
|
1881
|
+
const hit = accountsById.find((entry) => entry.accountId === accountId)
|
|
1882
|
+
return hit?.quota ?? undefined
|
|
1883
|
+
}
|
|
1884
|
+
const shownAccountId = routedAccount !== undefined
|
|
1885
|
+
? routed.accountId
|
|
1886
|
+
: pinned
|
|
1887
|
+
? pinnedAccountId
|
|
1888
|
+
: routing?.preview?.accountId
|
|
1889
|
+
const shownQuota = shownAccountId === undefined ? undefined : quotaOf(shownAccountId)
|
|
1890
|
+
/** 已判定耗尽:上游 1005/1113/零内容标记,或缓存里的生效桶已过期。 */
|
|
1891
|
+
const exhausted = shownQuota?.limited === true || shownQuota?.expired === true
|
|
1892
|
+
/** 面板口径说「还有余额」但准入已经拒了 —— 需要显式警告。 */
|
|
1893
|
+
const quotaConflict = exhausted && shownQuota?.available === true
|
|
1894
|
+
const outcome = routed?.outcome
|
|
1895
|
+
const routedExhausted = routed !== undefined && routed.outcome === 'exhausted'
|
|
1896
|
+
/**
|
|
1897
|
+
* 风控痕迹(1.0.2):`/routing` 的 `quota.riskBlocked`(来源 `last_error` 里
|
|
1898
|
+
* 是否出现过 3012 / unusual activity)。
|
|
1899
|
+
*
|
|
1900
|
+
* 为什么必须在**模型栏**上说:3012 的惩罚是账号级冷却(30 分钟 → 24 小时
|
|
1901
|
+
* → 停用),用户在模型栏那边看到的就是「对话突然失败」,反复重问等于自己
|
|
1902
|
+
* 把冷却攒满。徽标给出「该账号曾触发风控」+「可点清除限流恢复」就够了 ——
|
|
1903
|
+
* 它**不影响** `state` / 取名逻辑(账号仍然可用,`pool.pick` 不看 last_error)。
|
|
1904
|
+
*/
|
|
1905
|
+
const riskBlocked = shownQuota?.riskBlocked === true
|
|
1906
|
+
|
|
1907
|
+
/**
|
|
1908
|
+
* 只显示账号名,不加「钉定 / 当前账号」前缀 —— 徽标本身就是「当前使用账号」,
|
|
1909
|
+
* 再写一遍是冗余;钉定栏位与统一栏位的差别放在 tooltip 里说明。
|
|
1910
|
+
* 例外:准入已拒绝时必须在名字前加 ⚠,否则「显示有额度」的错觉还在。
|
|
1911
|
+
*/
|
|
1912
|
+
const state = shown === undefined
|
|
1913
|
+
? refreshing
|
|
1914
|
+
? 'loading'
|
|
1915
|
+
: 'idle'
|
|
1916
|
+
: exhausted || routedExhausted
|
|
1917
|
+
? 'exhausted'
|
|
1918
|
+
: riskBlocked
|
|
1919
|
+
? 'risk'
|
|
1920
|
+
: 'live'
|
|
1921
|
+
/**
|
|
1922
|
+
* 前缀优先级:**耗尽 > 风控**。耗尽时已经有一个 ⚠,再加一个同类符号只会
|
|
1923
|
+
* 让两个状态混成一句看不懂的「⚠⚠」;风控的说明放在 tooltip 里逐条列出。
|
|
1924
|
+
*/
|
|
1925
|
+
const label = shown !== undefined
|
|
1926
|
+
? `${exhausted || routedExhausted ? '⚠ ' : riskBlocked ? '⚠ ' : ''}${shown.nickname}`
|
|
1927
|
+
: refreshing
|
|
1928
|
+
? '读取中…'
|
|
1929
|
+
: '无可用账号'
|
|
1930
|
+
const tooltip = shown === undefined
|
|
1931
|
+
? '账号池为空:先在插件面板登录添加账号'
|
|
1932
|
+
: [
|
|
1933
|
+
shown.source === 'pinned'
|
|
1934
|
+
? '该栏位固定使用此账号'
|
|
1935
|
+
: shown.source === 'preview'
|
|
1936
|
+
? '下一条请求将使用此账号(快过期先用)'
|
|
1937
|
+
: pinned
|
|
1938
|
+
? '该栏位固定使用此账号'
|
|
1939
|
+
: '统一选号:下次请求可能换号',
|
|
1940
|
+
shown.modelId.length > 0 ? `上次:${shown.modelId}` : '',
|
|
1941
|
+
exhausted || routedExhausted
|
|
1942
|
+
? `⚠ 该账号已被上游判定额度用尽${shownQuota?.reason !== undefined ? `(${shownQuota.reason})` : ''},下一次对话会直接换号或报错`
|
|
1943
|
+
: '',
|
|
1944
|
+
/**
|
|
1945
|
+
* 风控提示(1.0.2):说清三件事 —— 是风控不是额度、容易被误判、
|
|
1946
|
+
* 面板里能一键恢复(点账号卡片上的「⚠ 曾触发风控 · 点清除限流恢复」chip)。
|
|
1947
|
+
*/
|
|
1948
|
+
riskBlocked
|
|
1949
|
+
? '⚠ 该账号曾触发上游风控(3012 unusual activity,不是额度问题,可能是误判):到插件面板点该账号的「点清除限流恢复」即可恢复'
|
|
1950
|
+
: '',
|
|
1951
|
+
quotaConflict
|
|
1952
|
+
? '面板显示仍有余额:那是已激活但尚未生效的额度包,对话只认当前生效的那一桶'
|
|
1953
|
+
: '',
|
|
1954
|
+
outcome === 'failed' || shownQuota?.source === 'failed'
|
|
1955
|
+
? '余额接口暂不可用(面板数字可能滞后)'
|
|
1956
|
+
: '',
|
|
1957
|
+
'点击立即刷新',
|
|
1958
|
+
].filter(Boolean).join(' · ')
|
|
1959
|
+
|
|
1960
|
+
const checkNow = () => {
|
|
1961
|
+
if (refreshing) return
|
|
1962
|
+
setRefreshing(true)
|
|
1963
|
+
refresh()
|
|
1964
|
+
.catch(() => {})
|
|
1965
|
+
.finally(() => {
|
|
1966
|
+
if (mounted.current) setRefreshing(false)
|
|
1967
|
+
})
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
/**
|
|
1971
|
+
* 样式照抄 workbuddy 积分徽标(`@venshell/dsh-workbuddy-connect` 的
|
|
1972
|
+
* `wrapperStyle` / `buttonStyle$1` / `creditStyle`):
|
|
1973
|
+
*
|
|
1974
|
+
* - 外层 `inline-flex` 容器负责与相邻控件的对齐(`translateY(2px)`);
|
|
1975
|
+
* - 徽标本身是**透明、无边框、无圆角**的按钮,hover 只改文字色;
|
|
1976
|
+
* - 字号 11 / 行高 16 / `--dsw-alias-label-tertiary`,读起来像「模型名的注释」,
|
|
1977
|
+
* 而不是一个与模型选择器抢视觉重心的独立控件。
|
|
1978
|
+
*
|
|
1979
|
+
* 用内联样式而非 CSS 类:插槽贡献物在任何时刻都要和宿主 chrome 对齐,
|
|
1980
|
+
* 不依赖 <style> 的注入时机。
|
|
1981
|
+
*/
|
|
1982
|
+
const wrapperStyle = {
|
|
1983
|
+
display: 'inline-flex',
|
|
1984
|
+
position: 'relative',
|
|
1985
|
+
alignItems: 'center',
|
|
1986
|
+
transform: 'translateY(2px)',
|
|
1987
|
+
marginRight: -8,
|
|
1988
|
+
}
|
|
1989
|
+
const buttonStyle = {
|
|
1990
|
+
display: 'inline-flex',
|
|
1991
|
+
alignItems: 'center',
|
|
1992
|
+
justifyContent: 'center',
|
|
1993
|
+
gap: 2,
|
|
1994
|
+
height: 30,
|
|
1995
|
+
padding: '0 6px',
|
|
1996
|
+
border: 0,
|
|
1997
|
+
borderRadius: 8,
|
|
1998
|
+
background: 'transparent',
|
|
1999
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
2000
|
+
font: 'inherit',
|
|
2001
|
+
whiteSpace: 'nowrap',
|
|
2002
|
+
cursor: 'pointer',
|
|
2003
|
+
}
|
|
2004
|
+
const creditStyle = {
|
|
2005
|
+
fontSize: 11,
|
|
2006
|
+
lineHeight: '16px',
|
|
2007
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
2008
|
+
whiteSpace: 'nowrap',
|
|
2009
|
+
padding: '0 4px 0 0',
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* 换号一闪:同款注释级配色,只改文字色,不加背景/描边。
|
|
2013
|
+
* 风控也上色(1.0.2):橙黄,低于耗尽的红,高于正常的蓝 —— 三级一眼可分。
|
|
2014
|
+
*/
|
|
2015
|
+
const flashStyle = flash || exhausted || routedExhausted || riskBlocked
|
|
2016
|
+
? {
|
|
2017
|
+
color: exhausted || routedExhausted
|
|
2018
|
+
? 'var(--dsw-alias-label-danger,#d9534f)'
|
|
2019
|
+
: riskBlocked
|
|
2020
|
+
? 'var(--dsw-alias-label-warning,#b8860b)'
|
|
2021
|
+
: 'var(--dsw-alias-state-business-primary)',
|
|
2022
|
+
}
|
|
2023
|
+
: undefined
|
|
2024
|
+
|
|
2025
|
+
/**
|
|
2026
|
+
* 额度占比圆环:**照抄宿主的上下文使用量圆环**(composer 里
|
|
2027
|
+
* `aria-label="上下文已用 36%"` 那个):
|
|
2028
|
+
*
|
|
2029
|
+
* <svg viewBox="0 0 14 14" width="14" height="14">
|
|
2030
|
+
* <circle class="track" cx="7" cy="7" r="5.5"/>
|
|
2031
|
+
* <circle class="fill" cx="7" cy="7" r="5.5"
|
|
2032
|
+
* stroke-dasharray="<周长×占比> <周长>" transform="rotate(-90 7 7)"/>
|
|
2033
|
+
* </svg>
|
|
2034
|
+
*
|
|
2035
|
+
* `rotate(-90 7 7)` 把起点转到 12 点方向,`dasharray` 决定顺时针占比 ——
|
|
2036
|
+
* 与上下文圆环完全同一套几何参数,所以看起来就是同一个控件家族。
|
|
2037
|
+
* r=5.5 ⇒ 周长 = 2πr ≈ 34.5575。
|
|
2038
|
+
*/
|
|
2039
|
+
const RING_R = 5.5
|
|
2040
|
+
const RING_CIRC = 2 * Math.PI * RING_R
|
|
2041
|
+
const ratio = typeof shown?.usedRatio === 'number' ? shown.usedRatio : undefined
|
|
2042
|
+
const ring = ratio === undefined || ratio === null
|
|
2043
|
+
? /** 占比未知(企业版 / 还没拉到):只画灰圈底,不编造进度。 */
|
|
2044
|
+
[h('circle', {
|
|
2045
|
+
key: 'track',
|
|
2046
|
+
cx: 7,
|
|
2047
|
+
cy: 7,
|
|
2048
|
+
r: RING_R,
|
|
2049
|
+
fill: 'none',
|
|
2050
|
+
stroke: 'var(--dsw-alias-border-l2)',
|
|
2051
|
+
strokeWidth: 2,
|
|
2052
|
+
})]
|
|
2053
|
+
: [
|
|
2054
|
+
h('circle', {
|
|
2055
|
+
key: 'track',
|
|
2056
|
+
cx: 7,
|
|
2057
|
+
cy: 7,
|
|
2058
|
+
r: RING_R,
|
|
2059
|
+
fill: 'none',
|
|
2060
|
+
stroke: 'var(--dsw-alias-border-l2)',
|
|
2061
|
+
strokeWidth: 2,
|
|
2062
|
+
}),
|
|
2063
|
+
h('circle', {
|
|
2064
|
+
key: 'fill',
|
|
2065
|
+
cx: 7,
|
|
2066
|
+
cy: 7,
|
|
2067
|
+
r: RING_R,
|
|
2068
|
+
fill: 'none',
|
|
2069
|
+
stroke: ratio >= 0.9 ? 'var(--dsw-alias-label-danger)' : 'var(--dsw-alias-state-business-primary)',
|
|
2070
|
+
strokeWidth: 2,
|
|
2071
|
+
strokeLinecap: 'round',
|
|
2072
|
+
strokeDasharray: `${(RING_CIRC * Math.min(1, Math.max(0, ratio))).toFixed(3)} ${RING_CIRC.toFixed(3)}`,
|
|
2073
|
+
transform: 'rotate(-90 7 7)',
|
|
2074
|
+
}),
|
|
2075
|
+
]
|
|
2076
|
+
|
|
2077
|
+
const quotaRing = h(
|
|
2078
|
+
'svg',
|
|
2079
|
+
{
|
|
2080
|
+
viewBox: '0 0 14 14',
|
|
2081
|
+
width: 14,
|
|
2082
|
+
height: 14,
|
|
2083
|
+
'aria-hidden': 'true',
|
|
2084
|
+
style: { flex: 'none', display: 'block' },
|
|
2085
|
+
},
|
|
2086
|
+
ring,
|
|
2087
|
+
)
|
|
2088
|
+
|
|
2089
|
+
return h(
|
|
2090
|
+
'span',
|
|
2091
|
+
{ style: wrapperStyle },
|
|
2092
|
+
h(
|
|
2093
|
+
'button',
|
|
2094
|
+
{
|
|
2095
|
+
type: 'button',
|
|
2096
|
+
style: { ...buttonStyle, ...flashStyle },
|
|
2097
|
+
className: 'zsp-route',
|
|
2098
|
+
'data-state': state,
|
|
2099
|
+
title: tooltip,
|
|
2100
|
+
'aria-label': shown !== undefined ? `${shown.nickname}。${tooltip}` : tooltip,
|
|
2101
|
+
'aria-busy': refreshing,
|
|
2102
|
+
onClick: checkNow,
|
|
2103
|
+
},
|
|
2104
|
+
/** 圆环在左、账号名在右:与上下文使用量圆环同一几何与配色。 */
|
|
2105
|
+
quotaRing,
|
|
2106
|
+
h('span', { style: creditStyle }, label),
|
|
2107
|
+
),
|
|
2108
|
+
)
|
|
2109
|
+
}
|
|
2110
|
+
|
|
2111
|
+
/**
|
|
2112
|
+
* 「清除限流」结果的一句话说明(1.0.2)。
|
|
2113
|
+
*
|
|
2114
|
+
* 旧实现(以及 `act()` 的通用路径)在成功时**什么也不说** —— 用户点 ×
|
|
2115
|
+
* 只看到 chip 消失,不知道清了几条、清的是哪个模型,也不知道错误文案
|
|
2116
|
+
* 同时被清掉了。`/reset-limits` 现在回 `{ removed, clearedModels, lastErrorBefore }`,
|
|
2117
|
+
* 这里把它摊成一句能读的话。
|
|
2118
|
+
*
|
|
2119
|
+
* @param {{ removed?: number, clearedModels?: string[], lastErrorBefore?: string | null }} data
|
|
2120
|
+
* @param {string | undefined} model 点击时指定的模型(undefined = 清该账号全部)
|
|
2121
|
+
*/
|
|
2122
|
+
function describeResetLimits(data, model) {
|
|
2123
|
+
const removed = typeof data?.removed === 'number' ? data.removed : 0
|
|
2124
|
+
const cleared = Array.isArray(data?.clearedModels) ? data.clearedModels : []
|
|
2125
|
+
/** 明确「清了哪个模型」:单模型点击时优先用请求里的那个(即使它本来就没标记)。 */
|
|
2126
|
+
const scope = model !== undefined && model.length > 0
|
|
2127
|
+
? model
|
|
2128
|
+
: cleared.length > 0
|
|
2129
|
+
? cleared.join('、')
|
|
2130
|
+
: '全部模型'
|
|
2131
|
+
const suffix =
|
|
2132
|
+
typeof data?.lastErrorBefore === 'string' && data.lastErrorBefore.length > 0
|
|
2133
|
+
? `(原限流原因已清:${data.lastErrorBefore.trim().slice(0, 60)}${data.lastErrorBefore.trim().length > 60 ? '…' : ''})`
|
|
2134
|
+
: ''
|
|
2135
|
+
if (removed === 0) return `该账号在 ${scope} 上没有限流标记(可能已自动解除)`
|
|
2136
|
+
return `已清除 ${removed} 条限流标记 · ${scope}${suffix}`
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
/**
|
|
2140
|
+
* 面板主体。
|
|
2141
|
+
*
|
|
2142
|
+
* 宿主以两种 view 调用(workbuddy 实证):
|
|
2143
|
+
* - `view === 'summary'`:插件列表页的一行简介,返回纯文本;
|
|
2144
|
+
* - 其余:详情页完整面板。
|
|
2145
|
+
*
|
|
2146
|
+
* ⚠ Hooks 全部先于 summary 早退调用(React hooks 顺序约束)。
|
|
2147
|
+
*/
|
|
2148
|
+
function Panel(props) {
|
|
2149
|
+
const [data, setData] = useState(undefined)
|
|
2150
|
+
const [error, setError] = useState(undefined)
|
|
2151
|
+
/** 一次性成功提示(清除限流之类「做完要说一声」的操作,1.0.2)。 */
|
|
2152
|
+
const [notice, setNotice] = useState(undefined)
|
|
2153
|
+
const [balances, setBalances] = useState({})
|
|
2154
|
+
const [busy, setBusy] = useState(false)
|
|
2155
|
+
const [confirmId, setConfirmId] = useState(undefined)
|
|
2156
|
+
const [distKeys, setDistKeys] = useState(undefined)
|
|
2157
|
+
|
|
2158
|
+
/**
|
|
2159
|
+
* 统一刷新:summary **立即返回**账号列表(0.6.7 起不在 summary 里拉余额),
|
|
2160
|
+
* 对 `balance: null` 的账号逐个补拉 `/balance?id=` 懒加载 ——
|
|
2161
|
+
* 列表秒出,单账号余额慢/失败只影响自己那张卡,不再把整个面板挡在「加载中…」。
|
|
2162
|
+
*/
|
|
2163
|
+
const load = useCallback(async () => {
|
|
2164
|
+
try {
|
|
2165
|
+
const summary = await api('/summary')
|
|
2166
|
+
setData(summary)
|
|
2167
|
+
/** 分发 key 列表(脱敏形态)单独拉(summary 不带它,见 manage-routes 注释)。 */
|
|
2168
|
+
void fetch(`${API}/distribution-keys`).then(async (r) => {
|
|
2169
|
+
const data = await r.json().catch(() => ({}))
|
|
2170
|
+
if (r.ok) setDistKeys(Array.isArray(data.keys) ? data.keys : [])
|
|
2171
|
+
}).catch(() => {})
|
|
2172
|
+
setError(undefined)
|
|
2173
|
+
const accounts = Array.isArray(summary?.accounts) ? summary.accounts : []
|
|
2174
|
+
const seeded = {}
|
|
2175
|
+
for (const item of accounts) {
|
|
2176
|
+
if (item.balance !== null && item.balance !== undefined) {
|
|
2177
|
+
seeded[item.id] = { ok: true, ...item.balance }
|
|
2178
|
+
} else {
|
|
2179
|
+
seeded[item.id] = { ok: false, error: '余额加载中…' }
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
setBalances(seeded)
|
|
2183
|
+
/** 并行补拉(每账号独立 fetch,单账号失败/超时不拖累其他)。 */
|
|
2184
|
+
for (const item of accounts) {
|
|
2185
|
+
if (item.balance !== null && item.balance !== undefined) continue
|
|
2186
|
+
/** /balance 是 GET(id 走 query),不走 POST 版 api() 助手。 */
|
|
2187
|
+
void fetch(`${API}/balance?id=${encodeURIComponent(item.id)}`)
|
|
2188
|
+
.then(async (response) => {
|
|
2189
|
+
const data = await response.json().catch(() => ({}))
|
|
2190
|
+
if (!response.ok) throw new Error(typeof data?.error === 'string' ? data.error : `HTTP ${response.status}`)
|
|
2191
|
+
setBalances((prev) => ({
|
|
2192
|
+
...prev,
|
|
2193
|
+
[item.id]: data?.balance !== null && data?.balance !== undefined
|
|
2194
|
+
? { ok: true, ...data.balance }
|
|
2195
|
+
: { ok: false, error: '余额不可用' },
|
|
2196
|
+
}))
|
|
2197
|
+
})
|
|
2198
|
+
.catch((caught) => {
|
|
2199
|
+
setBalances((prev) => ({
|
|
2200
|
+
...prev,
|
|
2201
|
+
[item.id]: { ok: false, error: caught instanceof Error ? caught.message : String(caught) },
|
|
2202
|
+
}))
|
|
2203
|
+
})
|
|
2204
|
+
}
|
|
2205
|
+
} catch (caught) {
|
|
2206
|
+
setError(caught instanceof Error ? caught.message : String(caught))
|
|
2207
|
+
}
|
|
2208
|
+
}, [])
|
|
2209
|
+
|
|
2210
|
+
useEffect(() => {
|
|
2211
|
+
void load()
|
|
2212
|
+
}, [load])
|
|
2213
|
+
|
|
2214
|
+
const act = useCallback(
|
|
2215
|
+
async (path, body) => {
|
|
2216
|
+
setBusy(true)
|
|
2217
|
+
try {
|
|
2218
|
+
await api(path, body)
|
|
2219
|
+
await load()
|
|
2220
|
+
setError(undefined)
|
|
2221
|
+
} catch (caught) {
|
|
2222
|
+
setError(caught instanceof Error ? caught.message : String(caught))
|
|
2223
|
+
} finally {
|
|
2224
|
+
setBusy(false)
|
|
2225
|
+
}
|
|
2226
|
+
},
|
|
2227
|
+
[load],
|
|
2228
|
+
)
|
|
2229
|
+
|
|
2230
|
+
/**
|
|
2231
|
+
* 清除限流(1.0.2):**不走 `act()`**。
|
|
2232
|
+
*
|
|
2233
|
+
* `act()` 成功时什么都不说,而用户点 × / 点风控 chip 时最想知道的是
|
|
2234
|
+
* 「到底清掉了什么」。`/reset-limits` 现在回
|
|
2235
|
+
* `{ removed, clearedModels, lastErrorBefore }`,这里把它摊成一句话放进
|
|
2236
|
+
* 顶部提示位(`notice`),并保留几秒后自动消失 —— 不占长期版面。
|
|
2237
|
+
*
|
|
2238
|
+
* `model` 省略 = 清该账号全部标记(风控 chip 用这个形态)。
|
|
2239
|
+
*/
|
|
2240
|
+
const resetLimit = useCallback(
|
|
2241
|
+
async (account, model) => {
|
|
2242
|
+
setBusy(true)
|
|
2243
|
+
try {
|
|
2244
|
+
const data = await api('/reset-limits', {
|
|
2245
|
+
id: account.id,
|
|
2246
|
+
...(typeof model === 'string' && model.length > 0 ? { model } : {}),
|
|
2247
|
+
})
|
|
2248
|
+
await load()
|
|
2249
|
+
setError(undefined)
|
|
2250
|
+
setNotice(`${account.nickname}:${describeResetLimits(data, model)}`)
|
|
2251
|
+
} catch (caught) {
|
|
2252
|
+
setError(caught instanceof Error ? caught.message : String(caught))
|
|
2253
|
+
} finally {
|
|
2254
|
+
setBusy(false)
|
|
2255
|
+
}
|
|
2256
|
+
},
|
|
2257
|
+
[load],
|
|
2258
|
+
)
|
|
2259
|
+
|
|
2260
|
+
/** 提示自动消失(成功提示不该永久留在面板上)。 */
|
|
2261
|
+
useEffect(() => {
|
|
2262
|
+
if (notice === undefined) return undefined
|
|
2263
|
+
const handle = setTimeout(() => setNotice(undefined), 6000)
|
|
2264
|
+
return () => clearTimeout(handle)
|
|
2265
|
+
}, [notice])
|
|
2266
|
+
|
|
2267
|
+
/** 插件列表页的摘要视图:一行文本,不发起请求。 */
|
|
2268
|
+
if ((props || {}).view === 'summary') {
|
|
2269
|
+
return '管理 ZCode 赠送免费 plan 的账号池:每账号一个模型栏位(各吃各的额度)、统一选号开关、余额与限流、面板内登录添加账号(bigmodel / z.ai)。'
|
|
2270
|
+
}
|
|
2271
|
+
|
|
2272
|
+
const accounts = data !== undefined && Array.isArray(data.accounts) ? data.accounts : undefined
|
|
2273
|
+
/** 统一选号栏位当前是否显示(关掉 = 模型列表只留每账号一栏)。 */
|
|
2274
|
+
const unifiedVisible = data?.unifiedModelList !== false
|
|
2275
|
+
|
|
2276
|
+
return h(
|
|
2277
|
+
'section',
|
|
2278
|
+
{ className: 'zsp-panel' },
|
|
2279
|
+
h('style', null, CSS),
|
|
2280
|
+
h(
|
|
2281
|
+
'div',
|
|
2282
|
+
{ className: 'zsp-head' },
|
|
2283
|
+
h('h3', { className: 'zsp-title' }, '账号池管理'),
|
|
2284
|
+
data !== undefined
|
|
2285
|
+
? h(
|
|
2286
|
+
'span',
|
|
2287
|
+
{ className: 'zsp-muted' },
|
|
2288
|
+
`${[data.displayName ?? data.provider, `${accounts.length} 个账号`].filter(Boolean).join(' · ')}`,
|
|
2289
|
+
)
|
|
2290
|
+
: null,
|
|
2291
|
+
h('span', { className: 'zsp-spacer' }),
|
|
2292
|
+
h('button', { className: 'zsp-btn', onClick: () => void load(), disabled: busy }, '刷新'),
|
|
2293
|
+
),
|
|
2294
|
+
/**
|
|
2295
|
+
* 池子总额度(0.9.3):汇总所有账号的余额桶,置顶一行。
|
|
2296
|
+
* 数据来自懒加载的 balances —— 未加载完的账号不计入(下方注明)。
|
|
2297
|
+
*/
|
|
2298
|
+
h(TotalQuota, { accounts, balances }),
|
|
2299
|
+
error !== undefined ? h('p', { className: 'zsp-error' }, error) : null,
|
|
2300
|
+
/** 成功提示(清除限流):与红色错误分开配色,几秒后自动消失。 */
|
|
2301
|
+
notice !== undefined ? h('p', { className: 'zsp-muted', 'data-notice': 'true' }, `✓ ${notice}`) : null,
|
|
2302
|
+
data === undefined && error === undefined ? h('span', { className: 'zsp-muted' }, '加载中…') : null,
|
|
2303
|
+
h(AddAccountSection, { busy, onAdded: () => void load() }),
|
|
2304
|
+
accounts !== undefined && accounts.length === 0
|
|
2305
|
+
? h('span', { className: 'zsp-muted' }, '账号池为空:用上方「添加账号」登录,或在插件目录运行 node bin/cli.mjs login。')
|
|
2306
|
+
: null,
|
|
2307
|
+
/**
|
|
2308
|
+
* 账号区方块:最顶一行是「统一选号」+ 贴方块右上角的开关,
|
|
2309
|
+
* 下面是各账号卡片 —— 面板顺序 = 模型选择器顺序。
|
|
2310
|
+
*/
|
|
2311
|
+
h(
|
|
2312
|
+
'div',
|
|
2313
|
+
{ className: 'zsp-group' },
|
|
2314
|
+
h(
|
|
2315
|
+
'div',
|
|
2316
|
+
{ className: 'zsp-row zsp-unified' },
|
|
2317
|
+
h('span', { className: 'zsp-dot', 'data-on': unifiedVisible === true }),
|
|
2318
|
+
h('span', { className: 'zsp-name' }, '统一选号'),
|
|
2319
|
+
h(
|
|
2320
|
+
'span',
|
|
2321
|
+
{ className: 'zsp-toggle-row' },
|
|
2322
|
+
h(Toggle, {
|
|
2323
|
+
checked: unifiedVisible === true,
|
|
2324
|
+
disabled: busy,
|
|
2325
|
+
onChange: (visible) => void act('/unified-list', { visible }),
|
|
2326
|
+
}),
|
|
2327
|
+
),
|
|
2328
|
+
),
|
|
2329
|
+
accounts !== undefined
|
|
2330
|
+
? accounts.map((account) =>
|
|
2331
|
+
h(AccountCard, {
|
|
2332
|
+
key: account.id,
|
|
2333
|
+
account,
|
|
2334
|
+
balance: balances[account.id],
|
|
2335
|
+
busy,
|
|
2336
|
+
modelListVisible: account.modelListVisible !== false,
|
|
2337
|
+
onToggleModelList: (visible) => void act('/model-list-visible', { id: account.id, visible }),
|
|
2338
|
+
confirmId,
|
|
2339
|
+
setConfirmId,
|
|
2340
|
+
onRemove: () => void act('/remove', { id: account.id }),
|
|
2341
|
+
onRename: (nickname) => void act('/rename', { id: account.id, nickname }),
|
|
2342
|
+
/**
|
|
2343
|
+
* `model` 省略 = 清该账号全部标记(风控 chip 走这个形态):
|
|
2344
|
+
* 3012 的痕迹在 `last_error` 里、不在 `modelRateLimits` 表里,
|
|
2345
|
+
* 全清才能让「点清除限流恢复」真的恢复。具体清了哪几条由后端
|
|
2346
|
+
* 回在 `clearedModels` 里,这里交给 `resetLimit` 转成提示。
|
|
2347
|
+
*/
|
|
2348
|
+
onResetLimit: (model) => void resetLimit(account, model),
|
|
2349
|
+
}),
|
|
2350
|
+
)
|
|
2351
|
+
: null,
|
|
2352
|
+
),
|
|
2353
|
+
h(OpenAiGatewaySection, { gateway: data?.openAiGateway, busy, onChanged: () => void load() }),
|
|
2354
|
+
h(DistributionKeysSection, { keys: distKeys, accounts, busy, onChanged: () => void load() }),
|
|
2355
|
+
h(ClaimSection, {
|
|
2356
|
+
accounts,
|
|
2357
|
+
busy,
|
|
2358
|
+
onClaimed: () => load(),
|
|
2359
|
+
captcha: data?.captcha,
|
|
2360
|
+
autoClaim: data?.autoClaim,
|
|
2361
|
+
/**
|
|
2362
|
+
* 自动领取拨盘:写入宿主(持久化 settings.json)后重新拉 summary,
|
|
2363
|
+
* 让拨盘状态与服务端观测(seenCount)保持同源。
|
|
2364
|
+
*/
|
|
2365
|
+
onToggleAutoClaim: async (enabled) => {
|
|
2366
|
+
try {
|
|
2367
|
+
await api('/auto-claim', { enabled })
|
|
2368
|
+
} finally {
|
|
2369
|
+
await load()
|
|
2370
|
+
}
|
|
2371
|
+
},
|
|
2372
|
+
}),
|
|
2373
|
+
data !== undefined && data.storeInMemory === true
|
|
2374
|
+
? h(
|
|
2375
|
+
'span',
|
|
2376
|
+
{ className: 'zsp-muted' },
|
|
2377
|
+
'⚠ 账号库当前是内存库(数据目录不可写),重启后账号会消失。',
|
|
2378
|
+
)
|
|
2379
|
+
: null,
|
|
2380
|
+
)
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
return {
|
|
2384
|
+
/**
|
|
2385
|
+
* ⚠ 只有**必须**的服务才进硬 inject:硬 inject 里缺一个,整个 fiber 挂起、
|
|
2386
|
+
* 面板一起消失。`modelDirectories` 刻意不进这里 —— 它由下面的
|
|
2387
|
+
* `ctx.inject([...])` 延迟注入(workbuddy 同款),缺了只是徽标不出现。
|
|
2388
|
+
*/
|
|
2389
|
+
inject: ['slots'],
|
|
2390
|
+
apply(ctx) {
|
|
2391
|
+
ctx.slots.inject(SLOT, () =>
|
|
2392
|
+
/** key = 包名:宿主只在 dsh-zcode-startplan 的详情页派发本条目。 */
|
|
2393
|
+
ctx.slots.register({ name: SLOT, key: BUNDLE_KEY }, Panel),
|
|
2394
|
+
)
|
|
2395
|
+
/**
|
|
2396
|
+
* 模型栏旁的「当前使用账号」(1.0.0)。
|
|
2397
|
+
*
|
|
2398
|
+
* 照 workbuddy 的写法,**两层防护**:
|
|
2399
|
+
* 1. `ctx.inject(['modelDirectories'])` —— 服务缺席时整块跳过,
|
|
2400
|
+
* 不影响配置页(放硬 inject 里缺了就整个插件挂起);
|
|
2401
|
+
* 2. 每次贡献都包在 `guardContribution` 里 —— 任何一次注册抛错都降级成
|
|
2402
|
+
* 一条 console.error,绝不冒泡进 loader 把整个 bundle 的 apply 打断。
|
|
2403
|
+
*
|
|
2404
|
+
* ⚠ 座位必须是 `conversation.input.right`(composer 注册表里**已声明**的
|
|
2405
|
+
* list 槽,紧挨 `conversation.input.model`)。绝不可换成模型座的 `.label`
|
|
2406
|
+
* 子座位:它只被 renderSlot() 调用、没有任何包声明,向它注册会抛错。
|
|
2407
|
+
*/
|
|
2408
|
+
/**
|
|
2409
|
+
* ⚠ `remote` / `remote.session` 必须一起声明,否则**运行时必炸**:
|
|
2410
|
+
*
|
|
2411
|
+
* `modelDirectories.directoryFor()` 内部要用 `sessions`/`remote`/
|
|
2412
|
+
* `remote.session`(它自己的 `static inject`)。宿主 ctx 是沙箱 façade,
|
|
2413
|
+
* 只挂**本 fiber 声明过**的服务,所以调用 `directoryFor` 时会在
|
|
2414
|
+
* Proxy 上抛 `cannot get property "remote.session" without inject`
|
|
2415
|
+
* —— 而且这个错误发生在 `inject` props 回调里,表现为徽标永不出现。
|
|
2416
|
+
*
|
|
2417
|
+
* 这里不是多余的:声明的是「我不亲自用 remote,但被我调用的服务要用」。
|
|
2418
|
+
*/
|
|
2419
|
+
ctx.inject(['modelDirectories', 'remote', 'remote.session'], (scope) => {
|
|
2420
|
+
guardContribution('model route badge', () =>
|
|
2421
|
+
scope.slots.inject(ROUTE_SLOT, () =>
|
|
2422
|
+
guardContribution(
|
|
2423
|
+
'model route badge',
|
|
2424
|
+
() =>
|
|
2425
|
+
scope.slots.register(
|
|
2426
|
+
{
|
|
2427
|
+
name: ROUTE_SLOT,
|
|
2428
|
+
/** 与 workbuddy 的 10 同序:落在模型座左侧,两者并排。 */
|
|
2429
|
+
id: 'zcode-start-plan-route',
|
|
2430
|
+
order: 10,
|
|
2431
|
+
/**
|
|
2432
|
+
* ⚠ 这个回调**每渲染一次就调一次**(宿主拿 props 用),
|
|
2433
|
+
* 必须自己吞掉所有异常:`directoryFor` 在会话作用域尚未
|
|
2434
|
+
* 建好 / 子代理会话 / 被销毁的会话上都会抛,抛一次就整块
|
|
2435
|
+
* 渲染崩掉 —— 徽标永远不出现。返回 `{}` 让组件自己隐藏。
|
|
2436
|
+
*/
|
|
2437
|
+
inject: (sessionId) => {
|
|
2438
|
+
try {
|
|
2439
|
+
const directory = scope.modelDirectories?.directoryFor(sessionId)
|
|
2440
|
+
return directory === undefined ? {} : { directory }
|
|
2441
|
+
} catch (error) {
|
|
2442
|
+
console.error(
|
|
2443
|
+
'[dsh-zcode-startplan] model directory lookup failed:',
|
|
2444
|
+
error,
|
|
2445
|
+
)
|
|
2446
|
+
return {}
|
|
2447
|
+
}
|
|
2448
|
+
},
|
|
2449
|
+
},
|
|
2450
|
+
ModelRouteBadge,
|
|
2451
|
+
) ?? NOOP_DISPOSER,
|
|
2452
|
+
),
|
|
2453
|
+
),
|
|
2454
|
+
)
|
|
2455
|
+
})
|
|
2456
|
+
},
|
|
2457
|
+
}
|
|
2458
|
+
},
|
|
2459
|
+
})
|