@philomelcc/dsh-workbench 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/client.js +547 -2
- package/index.js +129 -7
- package/lib/dpapi.js +92 -0
- package/lib/tool.js +71 -0
- package/lib/tools.js +360 -0
- package/lib/vault.js +338 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
外壳本体:顶部第二行的标签切换、每个模块一整页,以及模块注册用的那套小 API。
|
|
4
4
|
|
|
5
|
+
## 0.1.17 的变化
|
|
6
|
+
|
|
7
|
+
- **新增内置功能「密钥管理」**:模块行多一个「密钥」页,粘贴存入 API Key / Token,值用 Windows DPAPI 加密落盘(`$DSH_HOME/key-vault/vault.json`),页面只显示掩码;支持编辑元数据、替换、删除与使用审计。功能开关里可以关掉。
|
|
8
|
+
- **Host 半新增三个 Agent 工具**:`key_search` / `key_annotate` / `key_use`(Host 在内存里解值后代发 HTTP,AI 永远拿不到明文)。新增依赖 `koffi`(FFI 调 `crypt32.dll`)。只支持 Windows,只做 HTTP 代发。
|
|
9
|
+
- 修复深色主题下「品牌色底 + 白字」按钮不可见的问题(改用 `bg-layer-2` 底 + 主文字色)。
|
|
10
|
+
|
|
5
11
|
## 0.1.16 的变化
|
|
6
12
|
|
|
7
13
|
- **移除内置功能「推理档位滑条」**:模型菜单的推理等级回到官方下拉。随之内联的上游源码(约 2900 行)、大肥鱼剪影素材与粒子层、对应的功能开关项与三份第三方素材声明一并删除,`client.js` 从 281 KB 降到 117 KB。
|
package/client.js
CHANGED
|
@@ -26,14 +26,15 @@
|
|
|
26
26
|
// who only wants the tab row can turn the rest off and let another plugin
|
|
27
27
|
// own balances, themes or cost estimates. Every switch defaults to on, so
|
|
28
28
|
// installing the shell keeps the previous behaviour.
|
|
29
|
-
const FEATURE_KEYS = ['theme', 'balance', 'restart', 'glow', 'cost'];
|
|
30
|
-
const FEATURE_DEFAULTS = { theme: true, balance: true, restart: true, glow: true, cost: true };
|
|
29
|
+
const FEATURE_KEYS = ['theme', 'balance', 'restart', 'glow', 'cost', 'keys'];
|
|
30
|
+
const FEATURE_DEFAULTS = { theme: true, balance: true, restart: true, glow: true, cost: true, keys: true };
|
|
31
31
|
const FEATURE_LABELS = {
|
|
32
32
|
theme: { title: '主题切换', hint: '顶部栏的深色 / 浅色 / 纸色三个色块' },
|
|
33
33
|
balance: { title: '余额显示', hint: '顶部栏的账户余额' },
|
|
34
34
|
restart: { title: '重启按钮', hint: '顶部栏的一键重启(需要外部管理器)' },
|
|
35
35
|
glow: { title: '进行中光效', hint: '会话运行中时列表行的流动光效' },
|
|
36
36
|
cost: { title: '单次消耗估计', hint: '每轮对话结束时显示的 token 与费用估算' },
|
|
37
|
+
keys: { title: '密钥管理', hint: '「密钥」页:本机加密保存密钥,AI 只能按用途检索与代用' },
|
|
37
38
|
};
|
|
38
39
|
|
|
39
40
|
function createFeaturesStore() {
|
|
@@ -371,6 +372,529 @@
|
|
|
371
372
|
}
|
|
372
373
|
|
|
373
374
|
// ---------- the public workbench API ----------
|
|
375
|
+
// 密钥页用到这些 hook(外壳自身都用 React.useXxx)
|
|
376
|
+
const { useState, useEffect, useCallback } = React;
|
|
377
|
+
|
|
378
|
+
function KeysIcon(props) {
|
|
379
|
+
const size = props.size || 20;
|
|
380
|
+
return h('svg', {
|
|
381
|
+
viewBox: '0 0 24 24', width: size, height: size, 'aria-hidden': true,
|
|
382
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.7,
|
|
383
|
+
strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
384
|
+
},
|
|
385
|
+
h('circle', { cx: 8, cy: 12, r: 4.2 }),
|
|
386
|
+
h('path', { d: 'M12.2 12h8.3' }),
|
|
387
|
+
h('path', { d: 'M17.4 12v3' }),
|
|
388
|
+
h('path', { d: 'M20.5 12v2.2' }),
|
|
389
|
+
h('circle', { cx: 8, cy: 12, r: 1.1, fill: 'currentColor', stroke: 'none' }),
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async function keysApi(path, body) {
|
|
394
|
+
const options = body === undefined
|
|
395
|
+
? { cache: 'no-store' }
|
|
396
|
+
: {
|
|
397
|
+
method: 'POST',
|
|
398
|
+
headers: { 'Content-Type': 'application/json' },
|
|
399
|
+
body: JSON.stringify(body),
|
|
400
|
+
cache: 'no-store',
|
|
401
|
+
};
|
|
402
|
+
const response = await fetch(path, options);
|
|
403
|
+
const json = await response.json().catch(() => ({ ok: false, error: '响应不是 JSON' }));
|
|
404
|
+
if (!json.ok) throw new Error(json.error || `HTTP ${response.status}`);
|
|
405
|
+
return json;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function whenText(iso) {
|
|
409
|
+
if (!iso) return '从未使用';
|
|
410
|
+
try {
|
|
411
|
+
const date = new Date(iso);
|
|
412
|
+
if (Number.isNaN(date.getTime())) return iso;
|
|
413
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
414
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
|
415
|
+
} catch {
|
|
416
|
+
return iso;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// ── 样式(三主题都由 --dsw-alias-* 跟随) ──────────────────────────
|
|
421
|
+
const KEYS_STYLE_TEXT = `
|
|
422
|
+
.wbk-page { height: 100%; width: 100%; max-width: 1040px; margin: 0 auto; box-sizing: border-box;
|
|
423
|
+
overflow-y: auto; padding: 24px 28px 48px; background: var(--dsw-alias-bg-base);
|
|
424
|
+
color: var(--dsw-alias-label-primary); }
|
|
425
|
+
.wbk-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 6px; }
|
|
426
|
+
.wbk-title { margin: 0; font-size: 20px; font-weight: 650; line-height: 1.3; }
|
|
427
|
+
.wbk-sub { margin: 0 0 16px; font-size: 12px; line-height: 1.7; color: var(--dsw-alias-label-secondary); }
|
|
428
|
+
.wbk-btn { font-size: 12px; padding: 5px 11px; border-radius: 7px; cursor: pointer;
|
|
429
|
+
border: 1px solid var(--dsw-alias-border-l1); background: transparent;
|
|
430
|
+
color: var(--dsw-alias-label-secondary); transition: background .15s, color .15s, border-color .15s; }
|
|
431
|
+
.wbk-btn:hover { background: var(--dsw-alias-bg-layer-2); border-color: var(--dsw-alias-border-l2);
|
|
432
|
+
color: var(--dsw-alias-label-primary); }
|
|
433
|
+
.wbk-btn:active { transform: scale(.96); }
|
|
434
|
+
.wbk-btn:disabled { opacity: .5; cursor: default; transform: none; }
|
|
435
|
+
.wbk-btn-primary { background: var(--dsw-alias-bg-layer-2); border-color: var(--dsw-alias-border-l2);
|
|
436
|
+
color: var(--dsw-alias-label-primary); font-weight: 600; }
|
|
437
|
+
.wbk-btn-primary:hover { background: var(--dsw-alias-bg-overlay); color: var(--dsw-alias-brand-primary); }
|
|
438
|
+
.wbk-btn-danger:hover { color: var(--dsw-alias-state-error-primary); border-color: var(--dsw-alias-state-error-primary); }
|
|
439
|
+
.wbk-banner { display: flex; gap: 8px; align-items: flex-start; padding: 10px 12px; margin-bottom: 14px;
|
|
440
|
+
border-radius: 9px; font-size: 12px; line-height: 1.6;
|
|
441
|
+
border: 1px solid var(--dsw-alias-state-error-primary); color: var(--dsw-alias-state-error-primary); }
|
|
442
|
+
.wbk-toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
|
|
443
|
+
.wbk-search { flex: 1; box-sizing: border-box; padding: 6px 10px; font-size: 12px; border-radius: 8px;
|
|
444
|
+
border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-base);
|
|
445
|
+
color: var(--dsw-alias-label-primary); outline: none; }
|
|
446
|
+
.wbk-search:focus { border-color: var(--dsw-alias-border-l2); }
|
|
447
|
+
.wbk-count { font-size: 12px; color: var(--dsw-alias-label-secondary); white-space: nowrap; }
|
|
448
|
+
.wbk-list { display: flex; flex-direction: column; gap: 8px; }
|
|
449
|
+
.wbk-row { display: flex; align-items: center; gap: 14px; padding: 12px 14px; border-radius: 10px;
|
|
450
|
+
border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-1);
|
|
451
|
+
transition: border-color .15s, background .15s; }
|
|
452
|
+
.wbk-row:hover { border-color: var(--dsw-alias-border-l2); background: var(--dsw-alias-bg-layer-2); }
|
|
453
|
+
.wbk-main { flex: 1; min-width: 0; }
|
|
454
|
+
.wbk-name { font-size: 13px; font-weight: 600; line-height: 1.4; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
|
|
455
|
+
.wbk-chip { font-size: 11px; padding: 1px 7px; border-radius: 999px; font-weight: 400;
|
|
456
|
+
border: 1px solid var(--dsw-alias-border-l1); color: var(--dsw-alias-label-secondary); }
|
|
457
|
+
.wbk-note { margin-top: 3px; font-size: 11.5px; line-height: 1.5; color: var(--dsw-alias-label-secondary);
|
|
458
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
459
|
+
.wbk-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; flex-shrink: 0; }
|
|
460
|
+
.wbk-mask { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11.5px;
|
|
461
|
+
color: var(--dsw-alias-label-secondary); letter-spacing: .3px; }
|
|
462
|
+
.wbk-field { font-size: 10.5px; text-transform: uppercase; letter-spacing: .5px;
|
|
463
|
+
color: var(--dsw-alias-label-secondary); opacity: .8; }
|
|
464
|
+
.wbk-actions { display: flex; gap: 6px; }
|
|
465
|
+
.wbk-actions .wbk-btn { padding: 3px 8px; font-size: 11px; }
|
|
466
|
+
.wbk-meta { font-size: 11px; color: var(--dsw-alias-label-secondary); }
|
|
467
|
+
.wbk-empty { display: flex; flex-direction: column; align-items: center; justify-content: center;
|
|
468
|
+
gap: 10px; padding: 56px 16px; border-radius: 12px; border: 1px dashed var(--dsw-alias-border-l1);
|
|
469
|
+
color: var(--dsw-alias-label-secondary); font-size: 12.5px; text-align: center; }
|
|
470
|
+
.wbk-audit { margin-top: 26px; border-top: 1px solid var(--dsw-alias-border-l1); padding-top: 14px; }
|
|
471
|
+
.wbk-audit summary { cursor: pointer; font-size: 12px; color: var(--dsw-alias-label-secondary); user-select: none; }
|
|
472
|
+
.wbk-audit-row { display: flex; gap: 10px; padding: 5px 0; font-size: 11.5px; line-height: 1.5;
|
|
473
|
+
color: var(--dsw-alias-label-secondary); border-bottom: 1px solid var(--dsw-alias-border-l1); }
|
|
474
|
+
.wbk-audit-row:last-child { border-bottom: none; }
|
|
475
|
+
.wbk-audit-time { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; flex-shrink: 0; }
|
|
476
|
+
.wbk-mask-modal { position: fixed; inset: 0; z-index: 80; display: flex; align-items: center;
|
|
477
|
+
justify-content: center; background: rgba(0,0,0,.45); }
|
|
478
|
+
.wbk-modal { width: 520px; max-width: 92vw; max-height: 86vh; display: flex; flex-direction: column;
|
|
479
|
+
box-sizing: border-box; padding: 20px; border-radius: 12px; background: var(--dsw-alias-bg-layer-2);
|
|
480
|
+
color: var(--dsw-alias-label-primary); box-shadow: 0 18px 50px rgba(0,0,0,.45);
|
|
481
|
+
border: 1px solid var(--dsw-alias-border-l1); }
|
|
482
|
+
.wbk-modal-title { font-size: 15px; font-weight: 620; margin-bottom: 4px; }
|
|
483
|
+
.wbk-modal-desc { font-size: 12px; line-height: 1.6; color: var(--dsw-alias-label-secondary); margin-bottom: 14px; }
|
|
484
|
+
.wbk-form { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; min-height: 0; }
|
|
485
|
+
.wbk-label { font-size: 11.5px; color: var(--dsw-alias-label-secondary); margin-bottom: 3px; display: block; }
|
|
486
|
+
.wbk-input { width: 100%; box-sizing: border-box; padding: 6px 9px; font-size: 12.5px; border-radius: 7px;
|
|
487
|
+
border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-base);
|
|
488
|
+
color: var(--dsw-alias-label-primary); outline: none; font-family: inherit; }
|
|
489
|
+
.wbk-input:focus { border-color: var(--dsw-alias-border-l2); }
|
|
490
|
+
.wbk-input-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
|
491
|
+
.wbk-value-row { display: flex; gap: 6px; }
|
|
492
|
+
.wbk-value-row .wbk-input { flex: 1; }
|
|
493
|
+
.wbk-hint { font-size: 11px; line-height: 1.5; color: var(--dsw-alias-label-secondary); opacity: .85; }
|
|
494
|
+
.wbk-error { font-size: 11.5px; color: var(--dsw-alias-state-error-primary); line-height: 1.5; }
|
|
495
|
+
.wbk-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
|
|
496
|
+
.wbk-toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); z-index: 90;
|
|
497
|
+
font-size: 12.5px; padding: 8px 16px; border-radius: 999px; background: var(--dsw-alias-bg-overlay);
|
|
498
|
+
color: var(--dsw-alias-label-primary); border: 1px solid var(--dsw-alias-border-l1);
|
|
499
|
+
box-shadow: 0 8px 28px rgba(0,0,0,.28); }
|
|
500
|
+
`;
|
|
501
|
+
|
|
502
|
+
function useKeysStyles() {
|
|
503
|
+
useEffect(() => {
|
|
504
|
+
const existing = document.querySelector('style[data-plugin="dsh-workbench-keys"]');
|
|
505
|
+
if (existing) existing.remove();
|
|
506
|
+
const style = document.createElement('style');
|
|
507
|
+
style.dataset.plugin = 'dsh-workbench-keys';
|
|
508
|
+
style.textContent = KEYS_STYLE_TEXT;
|
|
509
|
+
document.head.appendChild(style);
|
|
510
|
+
return () => {
|
|
511
|
+
try { style.remove(); } catch (_error) { /* ignore */ }
|
|
512
|
+
};
|
|
513
|
+
}, []);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// ── 弹窗外壳 ────────────────────────────────────────────────────────
|
|
517
|
+
function KeysModal(props) {
|
|
518
|
+
const { title, desc, children, footer, onClose } = props;
|
|
519
|
+
useEffect(() => {
|
|
520
|
+
const onKey = (event) => {
|
|
521
|
+
if (event.key === 'Escape') onClose();
|
|
522
|
+
};
|
|
523
|
+
window.addEventListener('keydown', onKey);
|
|
524
|
+
return () => window.removeEventListener('keydown', onKey);
|
|
525
|
+
}, [onClose]);
|
|
526
|
+
return h('div', { className: 'wbk-mask-modal', onClick: onClose },
|
|
527
|
+
h('div', { className: 'wbk-modal', onClick: (event) => event.stopPropagation() },
|
|
528
|
+
h('div', { className: 'wbk-modal-title' }, title),
|
|
529
|
+
desc ? h('div', { className: 'wbk-modal-desc' }, desc) : null,
|
|
530
|
+
children,
|
|
531
|
+
footer ? h('div', { className: 'wbk-foot' }, footer) : null,
|
|
532
|
+
),
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function KeysField(props) {
|
|
537
|
+
return h('div', null,
|
|
538
|
+
h('label', { className: 'wbk-label' }, props.label),
|
|
539
|
+
props.children,
|
|
540
|
+
props.hint ? h('div', { className: 'wbk-hint', style: { marginTop: 3 } }, props.hint) : null,
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// ── 主视图 ──────────────────────────────────────────────────────────
|
|
545
|
+
function KeysView() {
|
|
546
|
+
useKeysStyles();
|
|
547
|
+
const [items, setItems] = useState([]);
|
|
548
|
+
const [stats, setStats] = useState({ count: 0, used: 0, dir: '' });
|
|
549
|
+
const [dpapi, setDpapi] = useState({ ok: true, error: '' });
|
|
550
|
+
const [loading, setLoading] = useState(true);
|
|
551
|
+
const [error, setError] = useState('');
|
|
552
|
+
const [query, setQuery] = useState('');
|
|
553
|
+
const [audit, setAudit] = useState([]);
|
|
554
|
+
const [toast, setToast] = useState('');
|
|
555
|
+
const [dialog, setDialog] = useState(null);
|
|
556
|
+
const [busy, setBusy] = useState(false);
|
|
557
|
+
|
|
558
|
+
const flash = (message) => {
|
|
559
|
+
setToast(message);
|
|
560
|
+
window.setTimeout(() => setToast(''), 2400);
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
const load = useCallback(async () => {
|
|
564
|
+
try {
|
|
565
|
+
const json = await keysApi('/wb/workbench/keys/list');
|
|
566
|
+
setItems(json.items || []);
|
|
567
|
+
setStats(json.stats || { count: 0, used: 0, dir: '' });
|
|
568
|
+
setDpapi(json.dpapi || { ok: true, error: '' });
|
|
569
|
+
setError('');
|
|
570
|
+
} catch (err) {
|
|
571
|
+
setError(err.message);
|
|
572
|
+
} finally {
|
|
573
|
+
setLoading(false);
|
|
574
|
+
}
|
|
575
|
+
}, []);
|
|
576
|
+
|
|
577
|
+
const loadAudit = useCallback(async () => {
|
|
578
|
+
try {
|
|
579
|
+
const json = await keysApi('/wb/workbench/keys/audit?limit=40');
|
|
580
|
+
setAudit(json.records || []);
|
|
581
|
+
} catch (_error) {
|
|
582
|
+
setAudit([]);
|
|
583
|
+
}
|
|
584
|
+
}, []);
|
|
585
|
+
|
|
586
|
+
useEffect(() => {
|
|
587
|
+
load();
|
|
588
|
+
loadAudit();
|
|
589
|
+
}, [load, loadAudit]);
|
|
590
|
+
|
|
591
|
+
const filtered = query.trim()
|
|
592
|
+
? items.filter((item) => {
|
|
593
|
+
const hay = [item.name, item.purpose, item.note, item.field, ...(item.tags || [])]
|
|
594
|
+
.filter(Boolean).join(' ').toLowerCase();
|
|
595
|
+
return hay.includes(query.trim().toLowerCase());
|
|
596
|
+
})
|
|
597
|
+
: items;
|
|
598
|
+
|
|
599
|
+
const run = async (task, successText) => {
|
|
600
|
+
setBusy(true);
|
|
601
|
+
try {
|
|
602
|
+
await task();
|
|
603
|
+
if (successText) flash(successText);
|
|
604
|
+
await load();
|
|
605
|
+
await loadAudit();
|
|
606
|
+
setDialog(null);
|
|
607
|
+
} catch (err) {
|
|
608
|
+
setError(err.message);
|
|
609
|
+
} finally {
|
|
610
|
+
setBusy(false);
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
// 新增
|
|
615
|
+
const [draft, setDraft] = useState({ name: '', purpose: '', note: '', field: '', tags: '', value: '' });
|
|
616
|
+
const [revealed, setRevealed] = useState(false);
|
|
617
|
+
const openAdd = () => {
|
|
618
|
+
setDraft({ name: '', purpose: '', note: '', field: '', tags: '', value: '' });
|
|
619
|
+
setRevealed(false);
|
|
620
|
+
setError('');
|
|
621
|
+
setDialog({ kind: 'add' });
|
|
622
|
+
};
|
|
623
|
+
const submitAdd = () => run(async () => {
|
|
624
|
+
if (!draft.name.trim()) throw new Error('名称不能为空');
|
|
625
|
+
if (!draft.value.trim()) throw new Error('密钥不能为空');
|
|
626
|
+
await keysApi('/wb/workbench/keys/add', {
|
|
627
|
+
name: draft.name.trim(),
|
|
628
|
+
purpose: draft.purpose.trim(),
|
|
629
|
+
note: draft.note.trim(),
|
|
630
|
+
field: draft.field.trim(),
|
|
631
|
+
tags: draft.tags.split(/[,,\s]+/).filter(Boolean),
|
|
632
|
+
value: draft.value,
|
|
633
|
+
});
|
|
634
|
+
}, '已加密保存');
|
|
635
|
+
|
|
636
|
+
// 编辑元数据
|
|
637
|
+
const [meta, setMeta] = useState({ id: '', name: '', purpose: '', note: '', field: '', tags: '' });
|
|
638
|
+
const openEdit = (item) => {
|
|
639
|
+
setMeta({
|
|
640
|
+
id: item.id,
|
|
641
|
+
name: item.name,
|
|
642
|
+
purpose: item.purpose || '',
|
|
643
|
+
note: item.note || '',
|
|
644
|
+
field: item.field || '',
|
|
645
|
+
tags: (item.tags || []).join(', '),
|
|
646
|
+
});
|
|
647
|
+
setError('');
|
|
648
|
+
setDialog({ kind: 'edit', item });
|
|
649
|
+
};
|
|
650
|
+
const submitEdit = () => run(async () => {
|
|
651
|
+
if (!meta.name.trim()) throw new Error('名称不能为空');
|
|
652
|
+
await keysApi('/wb/workbench/keys/annotate', {
|
|
653
|
+
id: meta.id,
|
|
654
|
+
name: meta.name.trim(),
|
|
655
|
+
purpose: meta.purpose.trim(),
|
|
656
|
+
note: meta.note.trim(),
|
|
657
|
+
field: meta.field.trim(),
|
|
658
|
+
tags: meta.tags.split(/[,,\s]+/).filter(Boolean),
|
|
659
|
+
});
|
|
660
|
+
}, '已更新');
|
|
661
|
+
|
|
662
|
+
// 替换值
|
|
663
|
+
const [newValue, setNewValue] = useState('');
|
|
664
|
+
const [replaceRevealed, setReplaceRevealed] = useState(false);
|
|
665
|
+
const openReplace = (item) => {
|
|
666
|
+
setNewValue('');
|
|
667
|
+
setReplaceRevealed(false);
|
|
668
|
+
setError('');
|
|
669
|
+
setDialog({ kind: 'replace', item });
|
|
670
|
+
};
|
|
671
|
+
const submitReplace = () => run(async () => {
|
|
672
|
+
if (!newValue.trim()) throw new Error('新密钥不能为空');
|
|
673
|
+
await keysApi('/wb/workbench/keys/replace', { id: dialog.item.id, value: newValue });
|
|
674
|
+
}, '已替换');
|
|
675
|
+
|
|
676
|
+
const submitRemove = (item) => run(async () => {
|
|
677
|
+
await keysApi('/wb/workbench/keys/remove', { id: item.id });
|
|
678
|
+
}, '已删除');
|
|
679
|
+
|
|
680
|
+
const header = h('div', { className: 'wbk-head' },
|
|
681
|
+
h('div', null,
|
|
682
|
+
h('h2', { className: 'wbk-title' }, '密钥'),
|
|
683
|
+
h('p', { className: 'wbk-sub' },
|
|
684
|
+
'本机加密保存。粘贴存入后页面只显示掩码;AI 只能按用途/备注检索到条目,并通过代发请求使用它——它读不到明文,你也不需要记。'),
|
|
685
|
+
),
|
|
686
|
+
h('button', { className: 'wbk-btn dk-btn-primary', onClick: openAdd, disabled: !dpapi.ok }, '+ 新增密钥'),
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
const dpapiBanner = dpapi.ok ? null : h('div', { className: 'wbk-banner' },
|
|
690
|
+
h('span', null, '⚠'),
|
|
691
|
+
h('span', null, `加密不可用:${dpapi.error || 'DPAPI 自检失败'}。密钥无法安全保存,新增已禁用。`),
|
|
692
|
+
);
|
|
693
|
+
|
|
694
|
+
const toolbar = h('div', { className: 'wbk-toolbar' },
|
|
695
|
+
h('input', {
|
|
696
|
+
className: 'wbk-search',
|
|
697
|
+
placeholder: '按名称 / 用途 / 备注过滤…',
|
|
698
|
+
value: query,
|
|
699
|
+
onChange: (event) => setQuery(event.target.value),
|
|
700
|
+
}),
|
|
701
|
+
h('span', { className: 'wbk-count' }, `${filtered.length} / ${items.length} 条`),
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
const row = (item) => h('div', { className: 'wbk-row', key: item.id },
|
|
705
|
+
h('div', { className: 'wbk-main' },
|
|
706
|
+
h('div', { className: 'wbk-name' },
|
|
707
|
+
h('span', null, item.name),
|
|
708
|
+
item.purpose ? h('span', { className: 'wbk-chip' }, item.purpose) : null,
|
|
709
|
+
(item.tags || []).map((tag) => h('span', { className: 'wbk-chip', key: tag, style: { opacity: .75 } }, tag)),
|
|
710
|
+
),
|
|
711
|
+
item.note ? h('div', { className: 'wbk-note', title: item.note }, item.note) : null,
|
|
712
|
+
h('div', { className: 'wbk-meta', style: { marginTop: 4 } },
|
|
713
|
+
`最近使用:${whenText(item.lastUsedAt)} · 共 ${item.uses || 0} 次`),
|
|
714
|
+
),
|
|
715
|
+
h('div', { className: 'wbk-side' },
|
|
716
|
+
item.field ? h('span', { className: 'wbk-field' }, item.field) : null,
|
|
717
|
+
h('span', { className: 'wbk-mask' }, item.masked),
|
|
718
|
+
h('div', { className: 'wbk-actions' },
|
|
719
|
+
h('button', { className: 'wbk-btn', onClick: () => openEdit(item) }, '编辑'),
|
|
720
|
+
h('button', { className: 'wbk-btn', onClick: () => openReplace(item) }, '替换'),
|
|
721
|
+
h('button', {
|
|
722
|
+
className: 'wbk-btn dk-btn-danger',
|
|
723
|
+
onClick: () => setDialog({ kind: 'delete', item }),
|
|
724
|
+
}, '删除'),
|
|
725
|
+
),
|
|
726
|
+
),
|
|
727
|
+
);
|
|
728
|
+
|
|
729
|
+
const list = loading
|
|
730
|
+
? h('div', { className: 'wbk-empty' }, '读取中…')
|
|
731
|
+
: filtered.length === 0
|
|
732
|
+
? h('div', { className: 'wbk-empty' },
|
|
733
|
+
h('div', null, items.length === 0 ? '密钥库还是空的' : '没有匹配的条目'),
|
|
734
|
+
h('div', { style: { fontSize: 11.5 } },
|
|
735
|
+
items.length === 0 ? '点右上角「新增密钥」,把要用到的 API Key / Token 粘进来。' : '换个关键词试试。'),
|
|
736
|
+
)
|
|
737
|
+
: h('div', { className: 'wbk-list' }, filtered.map(row));
|
|
738
|
+
|
|
739
|
+
const auditBlock = h('details', { className: 'wbk-audit' },
|
|
740
|
+
h('summary', null, `使用审计(最近 ${audit.length} 条,不含密钥内容)`),
|
|
741
|
+
audit.length === 0
|
|
742
|
+
? h('div', { className: 'wbk-meta', style: { padding: '10px 0' } }, '还没有使用记录。')
|
|
743
|
+
: h('div', { style: { marginTop: 8 } }, audit.map((entry, index) => h('div', {
|
|
744
|
+
className: 'wbk-audit-row', key: `${entry.at}-${index}`,
|
|
745
|
+
},
|
|
746
|
+
h('span', { className: 'wbk-audit-time' }, whenText(entry.at)),
|
|
747
|
+
h('span', null, `${entry.action || 'use'} · ${entry.name || entry.id || '-'}${entry.mode ? ` · ${entry.mode}` : ''}${entry.session ? ` · ${entry.session}` : ''}${entry.error ? ` · 失败:${entry.error}` : ''}`),
|
|
748
|
+
))),
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
return h('div', { className: 'wbk-page' },
|
|
752
|
+
header,
|
|
753
|
+
dpapiBanner,
|
|
754
|
+
error ? h('div', { className: 'wbk-banner', style: { marginTop: 4 } }, h('span', null, '⚠'), h('span', null, error)) : null,
|
|
755
|
+
toolbar,
|
|
756
|
+
list,
|
|
757
|
+
auditBlock,
|
|
758
|
+
|
|
759
|
+
dialog && dialog.kind === 'add' ? h(KeysModal, {
|
|
760
|
+
title: '新增密钥',
|
|
761
|
+
desc: '密钥只在保存那一刻经过页面;保存后立即丢弃原文,只保留加密密文与掩码。',
|
|
762
|
+
onClose: () => setDialog(null),
|
|
763
|
+
footer: [
|
|
764
|
+
h('button', { key: 'cancel', className: 'wbk-btn', onClick: () => setDialog(null) }, '取消'),
|
|
765
|
+
h('button', { key: 'ok', className: 'wbk-btn dk-btn-primary', onClick: submitAdd, disabled: busy },
|
|
766
|
+
busy ? '保存中…' : '加密保存'),
|
|
767
|
+
],
|
|
768
|
+
},
|
|
769
|
+
h('div', { className: 'wbk-form' },
|
|
770
|
+
h(KeysField, { label: '名称(必填)' },
|
|
771
|
+
h('input', {
|
|
772
|
+
className: 'wbk-input', autoFocus: true, value: draft.name,
|
|
773
|
+
placeholder: '例如:阿里云 OSS',
|
|
774
|
+
onChange: (event) => setDraft({ ...draft, name: event.target.value }),
|
|
775
|
+
})),
|
|
776
|
+
h(KeysField, { label: '用途' },
|
|
777
|
+
h('input', {
|
|
778
|
+
className: 'wbk-input', value: draft.purpose,
|
|
779
|
+
placeholder: 'AI 按这个检索,例如:对象存储上传',
|
|
780
|
+
onChange: (event) => setDraft({ ...draft, purpose: event.target.value }),
|
|
781
|
+
})),
|
|
782
|
+
h(KeysField, { label: '备注' },
|
|
783
|
+
h('input', {
|
|
784
|
+
className: 'wbk-input', value: draft.note,
|
|
785
|
+
placeholder: '可选,例如:生产账号,只读',
|
|
786
|
+
onChange: (event) => setDraft({ ...draft, note: event.target.value }),
|
|
787
|
+
})),
|
|
788
|
+
h(KeysField, { label: '标签' },
|
|
789
|
+
h('input', {
|
|
790
|
+
className: 'wbk-input', value: draft.tags,
|
|
791
|
+
placeholder: '可选,逗号分隔,例如:云, 上传',
|
|
792
|
+
onChange: (event) => setDraft({ ...draft, tags: event.target.value }),
|
|
793
|
+
})),
|
|
794
|
+
h(KeysField, { label: '字段名' },
|
|
795
|
+
h('input', {
|
|
796
|
+
className: 'wbk-input dk-input-mono', value: draft.field,
|
|
797
|
+
placeholder: '可选,例如:ACCESS_KEY_ID(代发请求时作为默认头名)',
|
|
798
|
+
onChange: (event) => setDraft({ ...draft, field: event.target.value }),
|
|
799
|
+
})),
|
|
800
|
+
h(KeysField, { label: '密钥(必填)', hint: '粘贴后页面不回显已保存的值;想看这段草稿可以按住「显示」。' },
|
|
801
|
+
h('div', { className: 'wbk-value-row' },
|
|
802
|
+
h('input', {
|
|
803
|
+
className: 'wbk-input dk-input-mono', type: revealed ? 'text' : 'password',
|
|
804
|
+
value: draft.value, placeholder: '粘贴密钥 / Token',
|
|
805
|
+
autoComplete: 'off', spellCheck: false,
|
|
806
|
+
onChange: (event) => setDraft({ ...draft, value: event.target.value }),
|
|
807
|
+
}),
|
|
808
|
+
h('button', {
|
|
809
|
+
className: 'wbk-btn', onClick: () => setRevealed(!revealed), type: 'button',
|
|
810
|
+
}, revealed ? '隐藏' : '显示'),
|
|
811
|
+
)),
|
|
812
|
+
),
|
|
813
|
+
) : null,
|
|
814
|
+
|
|
815
|
+
dialog && dialog.kind === 'edit' ? h(KeysModal, {
|
|
816
|
+
title: '编辑条目',
|
|
817
|
+
desc: '只改名称 / 用途 / 备注 / 字段,密钥本身不动。',
|
|
818
|
+
onClose: () => setDialog(null),
|
|
819
|
+
footer: [
|
|
820
|
+
h('button', { key: 'cancel', className: 'wbk-btn', onClick: () => setDialog(null) }, '取消'),
|
|
821
|
+
h('button', { key: 'ok', className: 'wbk-btn dk-btn-primary', onClick: submitEdit, disabled: busy },
|
|
822
|
+
busy ? '保存中…' : '保存'),
|
|
823
|
+
],
|
|
824
|
+
},
|
|
825
|
+
h('div', { className: 'wbk-form' },
|
|
826
|
+
h(KeysField, { label: '名称' },
|
|
827
|
+
h('input', {
|
|
828
|
+
className: 'wbk-input', value: meta.name,
|
|
829
|
+
onChange: (event) => setMeta({ ...meta, name: event.target.value }),
|
|
830
|
+
})),
|
|
831
|
+
h(KeysField, { label: '用途' },
|
|
832
|
+
h('input', {
|
|
833
|
+
className: 'wbk-input', value: meta.purpose,
|
|
834
|
+
onChange: (event) => setMeta({ ...meta, purpose: event.target.value }),
|
|
835
|
+
})),
|
|
836
|
+
h(KeysField, { label: '备注' },
|
|
837
|
+
h('input', {
|
|
838
|
+
className: 'wbk-input', value: meta.note,
|
|
839
|
+
onChange: (event) => setMeta({ ...meta, note: event.target.value }),
|
|
840
|
+
})),
|
|
841
|
+
h(KeysField, { label: '标签' },
|
|
842
|
+
h('input', {
|
|
843
|
+
className: 'wbk-input', value: meta.tags,
|
|
844
|
+
onChange: (event) => setMeta({ ...meta, tags: event.target.value }),
|
|
845
|
+
})),
|
|
846
|
+
h(KeysField, { label: '字段名' },
|
|
847
|
+
h('input', {
|
|
848
|
+
className: 'wbk-input dk-input-mono', value: meta.field,
|
|
849
|
+
onChange: (event) => setMeta({ ...meta, field: event.target.value }),
|
|
850
|
+
})),
|
|
851
|
+
),
|
|
852
|
+
) : null,
|
|
853
|
+
|
|
854
|
+
dialog && dialog.kind === 'replace' ? h(KeysModal, {
|
|
855
|
+
title: `替换密钥 · ${dialog.item.name}`,
|
|
856
|
+
desc: '输入新的密钥值,旧值会被覆盖作废,页面不会回显。',
|
|
857
|
+
onClose: () => setDialog(null),
|
|
858
|
+
footer: [
|
|
859
|
+
h('button', { key: 'cancel', className: 'wbk-btn', onClick: () => setDialog(null) }, '取消'),
|
|
860
|
+
h('button', { key: 'ok', className: 'wbk-btn dk-btn-primary', onClick: submitReplace, disabled: busy },
|
|
861
|
+
busy ? '保存中…' : '加密保存'),
|
|
862
|
+
],
|
|
863
|
+
},
|
|
864
|
+
h('div', { className: 'wbk-form' },
|
|
865
|
+
h(KeysField, { label: '新密钥' },
|
|
866
|
+
h('div', { className: 'wbk-value-row' },
|
|
867
|
+
h('input', {
|
|
868
|
+
className: 'wbk-input dk-input-mono', type: replaceRevealed ? 'text' : 'password',
|
|
869
|
+
autoFocus: true, value: newValue, placeholder: '粘贴新的密钥',
|
|
870
|
+
autoComplete: 'off', spellCheck: false,
|
|
871
|
+
onChange: (event) => setNewValue(event.target.value),
|
|
872
|
+
}),
|
|
873
|
+
h('button', {
|
|
874
|
+
className: 'wbk-btn', type: 'button',
|
|
875
|
+
onClick: () => setReplaceRevealed(!replaceRevealed),
|
|
876
|
+
}, replaceRevealed ? '隐藏' : '显示'),
|
|
877
|
+
)),
|
|
878
|
+
),
|
|
879
|
+
) : null,
|
|
880
|
+
|
|
881
|
+
dialog && dialog.kind === 'delete' ? h(KeysModal, {
|
|
882
|
+
title: `删除「${dialog.item.name}」?`,
|
|
883
|
+
desc: '删除后不能恢复;如果 AI 正在用它,之后的调用会失败。',
|
|
884
|
+
onClose: () => setDialog(null),
|
|
885
|
+
footer: [
|
|
886
|
+
h('button', { key: 'cancel', className: 'wbk-btn', onClick: () => setDialog(null) }, '取消'),
|
|
887
|
+
h('button', {
|
|
888
|
+
key: 'ok', className: 'wbk-btn dk-btn-danger', onClick: () => submitRemove(dialog.item), disabled: busy,
|
|
889
|
+
}, busy ? '删除中…' : '确认删除'),
|
|
890
|
+
],
|
|
891
|
+
}, null) : null,
|
|
892
|
+
|
|
893
|
+
toast ? h('div', { className: 'wbk-toast' }, toast) : null,
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
|
|
374
898
|
function createApi(ctx) {
|
|
375
899
|
const api = {
|
|
376
900
|
/**
|
|
@@ -2427,6 +2951,27 @@ div[data-row-key^="session:"].dsh-glow-on > span:first-child > svg,
|
|
|
2427
2951
|
tab: false,
|
|
2428
2952
|
}), 'workbench: settings view');
|
|
2429
2953
|
|
|
2954
|
+
// 密钥库:跟功能开关走,关掉时把 tab 一起摘掉。
|
|
2955
|
+
ctx.effect(() => {
|
|
2956
|
+
let dispose = null;
|
|
2957
|
+
const sync = () => {
|
|
2958
|
+
if (dispose) { dispose(); dispose = null; }
|
|
2959
|
+
if (features.getSnapshot().keys) {
|
|
2960
|
+
dispose = api.registerView({
|
|
2961
|
+
id: 'keys',
|
|
2962
|
+
title: '密钥',
|
|
2963
|
+
subtitle: '本机加密密钥库',
|
|
2964
|
+
icon: KeysIcon,
|
|
2965
|
+
component: KeysView,
|
|
2966
|
+
order: 45,
|
|
2967
|
+
});
|
|
2968
|
+
}
|
|
2969
|
+
};
|
|
2970
|
+
sync();
|
|
2971
|
+
const unsubscribe = features.subscribe(sync);
|
|
2972
|
+
return () => { unsubscribe(); if (dispose) dispose(); };
|
|
2973
|
+
}, 'workbench: keys view');
|
|
2974
|
+
|
|
2430
2975
|
const balance = createBalanceStore(ctx);
|
|
2431
2976
|
try {
|
|
2432
2977
|
ctx.effect(() => { balance.start(); return () => balance.stop(); }, 'workbench: balance polling');
|