@jetcrab/pi-desk-sdk 1.1.16-dev → 1.1.19-dev
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 +4 -0
- package/dist/browser.d.ts +17 -0
- package/dist/config-api.d.ts +8 -0
- package/dist/config-api.js +1 -0
- package/dist/config-encryption.d.ts +10 -0
- package/dist/config-encryption.js +114 -0
- package/dist/config-paths.d.ts +9 -0
- package/dist/config-paths.js +50 -0
- package/dist/config-storage.d.ts +4 -0
- package/dist/config-storage.js +161 -0
- package/dist/config-store.d.ts +13 -0
- package/dist/config-store.js +98 -0
- package/dist/config.d.ts +8 -0
- package/dist/config.js +28 -0
- package/dist/entry.d.ts +6 -0
- package/dist/events.d.ts +35 -0
- package/dist/events.js +1 -0
- package/dist/host-runtime/base.js +34 -9
- package/dist/index.d.ts +1 -0
- package/dist/react-base.d.ts +1 -0
- package/dist/react-base.js +1 -0
- package/dist/react-status-capsule.d.ts +16 -0
- package/dist/react-status-capsule.js +69 -0
- package/dist/secrets.d.ts +15 -0
- package/dist/secrets.js +245 -0
- package/dist/session.d.ts +3 -0
- package/dist/session.js +2 -0
- package/package.json +23 -2
package/dist/index.d.ts
CHANGED
package/dist/react-base.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export type { BrowserConnectionHost, BrowserConnectionSnapshot, BrowserMessageHa
|
|
|
4
4
|
export * from './react-ui.tsx';
|
|
5
5
|
export * from './react-log.tsx';
|
|
6
6
|
export * from './react-popover.tsx';
|
|
7
|
+
export * from './react-status-capsule.tsx';
|
|
7
8
|
export declare function usePluginConnection(connection: BrowserConnectionHost): BrowserConnectionSnapshot;
|
|
8
9
|
export declare function usePluginWorkSessions(workSessions: BrowserWorkSessionHost): readonly PluginWorkSession[];
|
|
9
10
|
export declare function usePluginPush(plugin: Pick<BrowserPluginFacade, 'onPush'>, listener: (message: PluginPushMessage) => void): void;
|
package/dist/react-base.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Component, useCallback, useEffect, useRef, useState, useSyncExternalSto
|
|
|
3
3
|
export * from "./react-ui.js";
|
|
4
4
|
export * from "./react-log.js";
|
|
5
5
|
export * from "./react-popover.js";
|
|
6
|
+
export * from "./react-status-capsule.js";
|
|
6
7
|
function useExternalSnapshot(owner) {
|
|
7
8
|
const subscribe = useCallback((listener) => {
|
|
8
9
|
const dispose = owner.subscribe(listener);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Popover } from '@base-ui/react/popover';
|
|
2
|
+
import { type ButtonHTMLAttributes, type ReactNode } from 'react';
|
|
3
|
+
export interface PluginStatusCapsuleProps {
|
|
4
|
+
visible: boolean;
|
|
5
|
+
open: boolean;
|
|
6
|
+
onOpenChange: NonNullable<Popover.Root.Props['onOpenChange']>;
|
|
7
|
+
summary: string;
|
|
8
|
+
icon?: ReactNode;
|
|
9
|
+
meta?: ReactNode;
|
|
10
|
+
actions?: ReactNode;
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
triggerProps?: Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'children' | 'onClick'>;
|
|
13
|
+
openOnHover?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** 保持挂载并更新 visible,才能完成退场;卸载仍立即释放全部视图资源。 */
|
|
16
|
+
export declare function PluginStatusCapsule({ visible, open, onOpenChange, summary, icon, meta, actions, children, triggerProps, openOnHover }: PluginStatusCapsuleProps): React.JSX.Element;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Popover } from '@base-ui/react/popover';
|
|
3
|
+
import { ChevronUpIcon } from 'lucide-react';
|
|
4
|
+
import { useId, useLayoutEffect, useRef } from 'react';
|
|
5
|
+
import { PluginSurface } from "./react-ui.js";
|
|
6
|
+
const STYLES = `
|
|
7
|
+
.pi-desk-ui-capsule-slot{position:relative;min-width:0;height:0;transition:height 200ms cubic-bezier(.22,.8,.25,1)}
|
|
8
|
+
.pi-desk-ui-capsule-slot[data-visible]{height:2.75rem}
|
|
9
|
+
.pi-desk-ui-capsule-shell{position:absolute;bottom:.5rem;left:50%;height:2.25rem;max-width:100%;border-radius:2rem;background:var(--primary,#262626);color:var(--primary-foreground,#fafafa);opacity:0;transform:translate(-50%,.5rem) scale(.92);overflow:hidden;pointer-events:none;transition:width 200ms cubic-bezier(.22,.8,.25,1),opacity 150ms ease-out,transform 200ms cubic-bezier(.22,.8,.25,1)}
|
|
10
|
+
.pi-desk-ui-capsule-slot[data-visible] .pi-desk-ui-capsule-shell{opacity:1;transform:translate(-50%,0) scale(1);pointer-events:auto}
|
|
11
|
+
.pi-desk-ui-capsule-row{display:flex;align-items:center;width:max-content;max-width:min(22rem,var(--capsule-available-width));height:2.25rem}
|
|
12
|
+
.pi-desk-ui-capsule-trigger{display:flex;align-items:center;gap:.5rem;min-width:0;flex:1;height:2.25rem;padding:0 .875rem;border:0;background:transparent;color:inherit;font:inherit;font-size:.875rem;text-align:left;cursor:pointer;outline:none}
|
|
13
|
+
.pi-desk-ui-capsule-trigger:focus-visible{outline:2px solid currentColor;outline-offset:-4px;border-radius:2rem}
|
|
14
|
+
.pi-desk-ui-capsule-icon{display:flex;flex:none;align-items:center;justify-content:center}
|
|
15
|
+
.pi-desk-ui-capsule-icon>svg{width:1rem;height:1rem}
|
|
16
|
+
.pi-desk-ui-capsule-copy{min-width:0;flex:1;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;animation:pi-desk-capsule-copy 150ms ease-out}
|
|
17
|
+
.pi-desk-ui-capsule-meta{flex:none;font-size:.75rem;opacity:.75;font-variant-numeric:tabular-nums;white-space:nowrap}
|
|
18
|
+
.pi-desk-ui-capsule-chevron{flex:none;width:.875rem;height:.875rem;opacity:.75}
|
|
19
|
+
.pi-desk-ui-capsule-actions{display:flex;flex:none;align-items:center;padding-right:.375rem}
|
|
20
|
+
.pi-desk-ui-capsule-actions:empty{display:none}
|
|
21
|
+
.pi-desk-ui-capsule-positioner{z-index:1020;max-width:calc(100vw - 1rem);outline:none;filter:drop-shadow(0 .5rem 1rem rgb(0 0 0 / .16))}
|
|
22
|
+
.pi-desk-ui-capsule-popup[data-pi-desk-ui-root]{display:flex;flex-direction:column;width:26rem;max-width:var(--available-width);max-height:min(28rem,var(--available-height));overflow:hidden;background:var(--primary,#262626);color:var(--primary-foreground,#fafafa);border-radius:1.125rem;outline:none;clip-path:inset(0 0 0 0 round 1.125rem);transition:clip-path 200ms cubic-bezier(.22,.8,.25,1),opacity 150ms ease-out}
|
|
23
|
+
.pi-desk-ui-capsule-popup[data-starting-style],.pi-desk-ui-capsule-popup[data-ending-style]{clip-path:inset(max(0px,calc(100% - var(--anchor-height))) max(0px,calc((100% - var(--anchor-width))/2)) 0 round 1.125rem)}
|
|
24
|
+
.pi-desk-ui-capsule-body{display:flex;min-width:0;min-height:0;flex:1;flex-direction:column;overflow:auto;overscroll-behavior:contain;opacity:1;transition:opacity 150ms ease-out}
|
|
25
|
+
.pi-desk-ui-capsule-popup[data-starting-style] .pi-desk-ui-capsule-body,.pi-desk-ui-capsule-popup[data-ending-style] .pi-desk-ui-capsule-body{opacity:0}
|
|
26
|
+
.pi-desk-ui-capsule-footer{display:flex;flex:none;min-width:0;min-height:2.25rem;border-top:1px solid color-mix(in srgb,currentColor 18%,transparent)}
|
|
27
|
+
.pi-desk-ui-capsule-footer .pi-desk-ui-capsule-chevron{transform:rotate(180deg)}
|
|
28
|
+
@keyframes pi-desk-capsule-copy{from{opacity:0;transform:translateY(.25rem)}to{opacity:1;transform:translateY(0)}}
|
|
29
|
+
@media(prefers-reduced-motion:reduce){.pi-desk-ui-capsule-slot,.pi-desk-ui-capsule-shell,.pi-desk-ui-capsule-popup,.pi-desk-ui-capsule-body{transition:none}.pi-desk-ui-capsule-copy{animation:none}}
|
|
30
|
+
`;
|
|
31
|
+
/** 保持挂载并更新 visible,才能完成退场;卸载仍立即释放全部视图资源。 */
|
|
32
|
+
export function PluginStatusCapsule({ visible, open, onOpenChange, summary, icon, meta, actions, children, triggerProps, openOnHover = false }) {
|
|
33
|
+
const triggerId = useId();
|
|
34
|
+
const slotRef = useRef(null);
|
|
35
|
+
const shellRef = useRef(null);
|
|
36
|
+
const rowRef = useRef(null);
|
|
37
|
+
const triggerRef = useRef(null);
|
|
38
|
+
const focusInside = useRef(false);
|
|
39
|
+
const expanded = visible && open;
|
|
40
|
+
useLayoutEffect(() => {
|
|
41
|
+
const slot = slotRef.current;
|
|
42
|
+
const shell = shellRef.current;
|
|
43
|
+
const row = rowRef.current;
|
|
44
|
+
if (!slot || !shell || !row)
|
|
45
|
+
return;
|
|
46
|
+
// 只测量当前真实摘要,不复制插件视图;宽度不反向约束被观察内容。
|
|
47
|
+
const measure = () => {
|
|
48
|
+
slot.style.setProperty('--capsule-available-width', `${slot.clientWidth}px`);
|
|
49
|
+
shell.style.width = `${row.offsetWidth}px`;
|
|
50
|
+
};
|
|
51
|
+
measure();
|
|
52
|
+
const observer = new ResizeObserver(measure);
|
|
53
|
+
observer.observe(slot);
|
|
54
|
+
observer.observe(row);
|
|
55
|
+
return () => observer.disconnect();
|
|
56
|
+
}, []);
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
if (expanded)
|
|
59
|
+
focusInside.current = false;
|
|
60
|
+
}, [expanded]);
|
|
61
|
+
const copy = (_jsxs(_Fragment, { children: [icon ? _jsx("span", { className: "pi-desk-ui-capsule-icon", children: icon }) : null, _jsx("span", { className: "pi-desk-ui-capsule-copy", children: summary }, summary), meta ? _jsx("span", { className: "pi-desk-ui-capsule-meta", children: meta }) : null, _jsx(ChevronUpIcon, { className: "pi-desk-ui-capsule-chevron", "aria-hidden": "true" })] }));
|
|
62
|
+
return (_jsxs(Popover.Root, { open: expanded, onOpenChange: onOpenChange, modal: false, triggerId: triggerId, children: [_jsx("style", { href: "pi-desk-status-capsule", precedence: "pi-desk-ui", children: STYLES }), _jsx("div", { ref: slotRef, "data-slot": "status-capsule", className: "pi-desk-ui-capsule-slot", "data-visible": visible || undefined, children: _jsx("div", { ref: shellRef, "data-slot": "status-capsule-shell", className: "pi-desk-ui-capsule-shell", inert: !visible, "aria-hidden": !visible, children: _jsxs("div", { ref: rowRef, className: "pi-desk-ui-capsule-row", children: [_jsx(Popover.Trigger, { ...triggerProps, ref: triggerRef, id: triggerId, "data-slot": "status-capsule-trigger", className: "pi-desk-ui-capsule-trigger", title: summary, openOnHover: openOnHover, delay: 500, closeDelay: 160, children: copy }), actions ? _jsx("div", { className: "pi-desk-ui-capsule-actions", children: actions }) : null] }) }) }), _jsx(Popover.Portal, { children: _jsx(Popover.Positioner, { anchor: shellRef, side: "top", align: "center", sideOffset: ({ anchor }) => -anchor.height, collisionPadding: 8, collisionAvoidance: { side: 'none', align: 'shift', fallbackAxisSide: 'none' }, className: "pi-desk-ui-capsule-positioner", children: _jsxs(Popover.Popup, { render: _jsx(PluginSurface, {}), className: "pi-desk-ui-capsule-popup", "data-slot": "status-capsule-popup", "aria-labelledby": triggerId, initialFocus: false, onFocusCapture: () => {
|
|
63
|
+
focusInside.current = true;
|
|
64
|
+
}, onBlurCapture: (event) => {
|
|
65
|
+
focusInside.current =
|
|
66
|
+
event.relatedTarget instanceof Node &&
|
|
67
|
+
event.currentTarget.contains(event.relatedTarget);
|
|
68
|
+
}, finalFocus: () => visible && focusInside.current ? (triggerRef.current ?? false) : false, children: [_jsx("div", { className: "pi-desk-ui-capsule-body", children: children }), _jsxs("div", { className: "pi-desk-ui-capsule-footer", children: [_jsx(Popover.Close, { ...triggerProps, "data-slot": "status-capsule-close", className: "pi-desk-ui-capsule-trigger", children: copy }), actions ? _jsx("div", { className: "pi-desk-ui-capsule-actions", children: actions }) : null] })] }) }) })] }));
|
|
69
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface SecretStoreOptions {
|
|
2
|
+
agentDir?: string;
|
|
3
|
+
/** 仅供隔离测试或受控集成注入;生产默认使用系统钥匙串。 */
|
|
4
|
+
keyProvider?: () => Promise<Uint8Array>;
|
|
5
|
+
}
|
|
6
|
+
export interface SecretStore {
|
|
7
|
+
get(reference: string): Promise<string | null>;
|
|
8
|
+
put(value: string, reference?: string): Promise<string>;
|
|
9
|
+
/** 仅显式删除已确认不再被任何配置引用的凭据;发布过的旧引用不会自动回收。 */
|
|
10
|
+
delete(reference: string): Promise<void>;
|
|
11
|
+
export(references: readonly string[]): Promise<Record<string, string>>;
|
|
12
|
+
import(values: Readonly<Record<string, string>>): Promise<Record<string, string>>;
|
|
13
|
+
}
|
|
14
|
+
export declare function isSecretReference(value: unknown): value is string;
|
|
15
|
+
export declare function createSecretStore(options?: SecretStoreOptions): SecretStore;
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { realpathSync } from 'node:fs';
|
|
4
|
+
import { getConfigPath } from "./config.js";
|
|
5
|
+
import { ensureStorageDirectory, readStorageFile, withStorageLock, writeStorageFile } from "./config-storage.js";
|
|
6
|
+
const header = Buffer.from('PIDSECR1');
|
|
7
|
+
class KeyError extends Error {
|
|
8
|
+
}
|
|
9
|
+
const nonceLength = 12;
|
|
10
|
+
const tagLength = 16;
|
|
11
|
+
const maxVaultBytes = 8 * 1024 * 1024;
|
|
12
|
+
const maxVaultRecords = 4096;
|
|
13
|
+
function assertVaultCapacity(values) {
|
|
14
|
+
const entries = Object.entries(values);
|
|
15
|
+
const guidance = '请先导出需要保留的凭据,并仅显式删除已确认不再被任何配置引用的凭据后重试;不会自动清空保险库';
|
|
16
|
+
if (entries.length > maxVaultRecords) {
|
|
17
|
+
throw new Error(`凭据保险库最多保存 ${maxVaultRecords} 条记录;${guidance}`);
|
|
18
|
+
}
|
|
19
|
+
let bytes = header.length + nonceLength + tagLength + 2 + Math.max(entries.length - 1, 0);
|
|
20
|
+
for (const [reference, value] of entries) {
|
|
21
|
+
if (Buffer.byteLength(value, 'utf8') > maxVaultBytes) {
|
|
22
|
+
throw new Error(`凭据保险库完整密文不能超过 8 MiB;${guidance}`);
|
|
23
|
+
}
|
|
24
|
+
bytes +=
|
|
25
|
+
Buffer.byteLength(JSON.stringify(reference), 'utf8') +
|
|
26
|
+
1 +
|
|
27
|
+
Buffer.byteLength(JSON.stringify(value), 'utf8');
|
|
28
|
+
if (bytes > maxVaultBytes)
|
|
29
|
+
throw new Error(`凭据保险库完整密文不能超过 8 MiB;${guidance}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const referencePattern = /^secret:\/\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/u;
|
|
33
|
+
export function isSecretReference(value) {
|
|
34
|
+
return typeof value === 'string' && referencePattern.test(value);
|
|
35
|
+
}
|
|
36
|
+
function assertReference(reference) {
|
|
37
|
+
if (!isSecretReference(reference))
|
|
38
|
+
throw new Error('凭据引用必须为 secret://UUID');
|
|
39
|
+
}
|
|
40
|
+
function decodeVault(content, key) {
|
|
41
|
+
try {
|
|
42
|
+
if (content.length < header.length + nonceLength + tagLength ||
|
|
43
|
+
!content.subarray(0, header.length).equals(header)) {
|
|
44
|
+
throw new Error();
|
|
45
|
+
}
|
|
46
|
+
const nonceEnd = header.length + nonceLength;
|
|
47
|
+
const decipher = createDecipheriv('aes-256-gcm', key, content.subarray(header.length, nonceEnd));
|
|
48
|
+
decipher.setAAD(header);
|
|
49
|
+
decipher.setAuthTag(content.subarray(nonceEnd, nonceEnd + tagLength));
|
|
50
|
+
const plaintext = Buffer.concat([
|
|
51
|
+
decipher.update(content.subarray(nonceEnd + tagLength)),
|
|
52
|
+
decipher.final()
|
|
53
|
+
]);
|
|
54
|
+
try {
|
|
55
|
+
const values = JSON.parse(plaintext.toString('utf8'));
|
|
56
|
+
if (typeof values !== 'object' || values === null || Array.isArray(values))
|
|
57
|
+
throw new Error();
|
|
58
|
+
for (const [reference, value] of Object.entries(values)) {
|
|
59
|
+
if (!isSecretReference(reference) || typeof value !== 'string')
|
|
60
|
+
throw new Error();
|
|
61
|
+
}
|
|
62
|
+
return values;
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
plaintext.fill(0);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new Error('凭据保险库认证失败或格式无效,请确认密钥与文件完整性');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function encodeVault(values, key) {
|
|
73
|
+
const nonce = randomBytes(nonceLength);
|
|
74
|
+
const cipher = createCipheriv('aes-256-gcm', key, nonce);
|
|
75
|
+
cipher.setAAD(header);
|
|
76
|
+
const plaintext = Buffer.from(JSON.stringify(values));
|
|
77
|
+
try {
|
|
78
|
+
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
79
|
+
return Buffer.concat([header, nonce, cipher.getAuthTag(), ciphertext]);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
plaintext.fill(0);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function validateKey(key) {
|
|
86
|
+
if (!(key instanceof Uint8Array) || key.byteLength !== 32) {
|
|
87
|
+
throw new Error('凭据主密钥必须是 32 字节');
|
|
88
|
+
}
|
|
89
|
+
return Buffer.from(key);
|
|
90
|
+
}
|
|
91
|
+
async function loadKey(path, vaultExists, keyProvider, assertLocked) {
|
|
92
|
+
if (keyProvider) {
|
|
93
|
+
try {
|
|
94
|
+
return validateKey(await keyProvider());
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
throw new Error('注入的凭据密钥提供器失败或未返回 32 字节密钥');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const keyFile = process.env.PI_DESK_SECRET_KEY_FILE;
|
|
101
|
+
if (keyFile !== undefined) {
|
|
102
|
+
if (!keyFile)
|
|
103
|
+
throw new Error('PI_DESK_SECRET_KEY_FILE 必须指定外部密钥文件');
|
|
104
|
+
const content = readStorageFile(resolve(keyFile));
|
|
105
|
+
if (content === null)
|
|
106
|
+
throw new Error('外部凭据密钥文件不存在,拒绝生成或替换密钥');
|
|
107
|
+
try {
|
|
108
|
+
// 外部文件由部署方持有;SDK 只读原始 32 字节,从不创建或改写它。
|
|
109
|
+
return validateKey(content);
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
content.fill(0);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
// 延迟加载原生模块,注入测试及外部密钥文件模式不访问系统钥匙串。
|
|
117
|
+
const { Entry } = await import('@napi-rs/keyring');
|
|
118
|
+
const directory = realpathSync(dirname(path));
|
|
119
|
+
const identity = process.platform === 'win32' ? directory.toLowerCase() : directory;
|
|
120
|
+
const account = createHash('sha256').update(identity).digest('hex');
|
|
121
|
+
const entry = new Entry('pi-desk.credentials', account, { linux: { store: 'secret-service' } });
|
|
122
|
+
const password = entry.getPassword();
|
|
123
|
+
if (password !== null) {
|
|
124
|
+
if (!/^[A-Za-z0-9+/]{43}=$/u.test(password))
|
|
125
|
+
throw new KeyError('凭据主密钥格式无效');
|
|
126
|
+
const key = Buffer.from(password, 'base64');
|
|
127
|
+
if (key.length !== 32 || key.toString('base64') !== password)
|
|
128
|
+
throw new KeyError('凭据主密钥格式无效');
|
|
129
|
+
return key;
|
|
130
|
+
}
|
|
131
|
+
if (vaultExists)
|
|
132
|
+
throw new KeyError('凭据保险库已存在,但系统钥匙串中的主密钥缺失,拒绝重建');
|
|
133
|
+
assertLocked();
|
|
134
|
+
const key = randomBytes(32);
|
|
135
|
+
try {
|
|
136
|
+
entry.setPassword(key.toString('base64'));
|
|
137
|
+
if (entry.getPassword() !== key.toString('base64'))
|
|
138
|
+
throw new Error('凭据主密钥写入校验失败');
|
|
139
|
+
return key;
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
key.fill(0);
|
|
143
|
+
throw new KeyError('凭据主密钥写入失败,请检查系统钥匙串');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error instanceof KeyError)
|
|
148
|
+
throw error;
|
|
149
|
+
// 原生错误正文可能包含凭据;仅给出可定位且不泄漏秘密的操作原因。
|
|
150
|
+
throw new Error('系统钥匙串不可用,无法解锁凭据;Linux 需要 Secret Service,也可设置 PI_DESK_SECRET_KEY_FILE');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export function createSecretStore(options = {}) {
|
|
154
|
+
const path = resolve(getConfigPath('credentials.enc', options.agentDir));
|
|
155
|
+
const keyProvider = options.keyProvider;
|
|
156
|
+
async function access(action) {
|
|
157
|
+
ensureStorageDirectory(dirname(path));
|
|
158
|
+
return withStorageLock(path, async (assertLocked) => {
|
|
159
|
+
const existing = readStorageFile(path);
|
|
160
|
+
let key;
|
|
161
|
+
try {
|
|
162
|
+
let values = {};
|
|
163
|
+
if (existing !== null) {
|
|
164
|
+
key = await loadKey(path, true, keyProvider, assertLocked);
|
|
165
|
+
values = decodeVault(existing, key);
|
|
166
|
+
}
|
|
167
|
+
assertLocked();
|
|
168
|
+
const { result, changed } = action(values);
|
|
169
|
+
if (changed) {
|
|
170
|
+
assertVaultCapacity(values);
|
|
171
|
+
key ??= await loadKey(path, false, keyProvider, assertLocked);
|
|
172
|
+
const content = encodeVault(values, key);
|
|
173
|
+
// 写前先用同一解密路径验证完整格式,再原子发布并逐字节回读校验。
|
|
174
|
+
decodeVault(content, key);
|
|
175
|
+
assertLocked();
|
|
176
|
+
writeStorageFile(path, content, existing);
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
key?.fill(0);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
async get(reference) {
|
|
187
|
+
assertReference(reference);
|
|
188
|
+
return access((values) => ({ result: values[reference] ?? null, changed: false }));
|
|
189
|
+
},
|
|
190
|
+
async put(value, reference) {
|
|
191
|
+
if (typeof value !== 'string')
|
|
192
|
+
throw new Error('凭据值必须是字符串');
|
|
193
|
+
if (reference !== undefined)
|
|
194
|
+
assertReference(reference);
|
|
195
|
+
return access((values) => {
|
|
196
|
+
let next = reference ?? `secret://${randomUUID()}`;
|
|
197
|
+
while (reference === undefined && Object.hasOwn(values, next))
|
|
198
|
+
next = `secret://${randomUUID()}`;
|
|
199
|
+
values[next] = value;
|
|
200
|
+
return { result: next, changed: true };
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
async delete(reference) {
|
|
204
|
+
assertReference(reference);
|
|
205
|
+
return access((values) => {
|
|
206
|
+
const changed = Object.hasOwn(values, reference);
|
|
207
|
+
delete values[reference];
|
|
208
|
+
return { result: undefined, changed };
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
async export(references) {
|
|
212
|
+
for (const reference of references)
|
|
213
|
+
assertReference(reference);
|
|
214
|
+
return access((values) => {
|
|
215
|
+
const exported = {};
|
|
216
|
+
for (const reference of references) {
|
|
217
|
+
if (!Object.hasOwn(values, reference))
|
|
218
|
+
throw new Error('导出的凭据引用不存在');
|
|
219
|
+
exported[reference] = values[reference];
|
|
220
|
+
}
|
|
221
|
+
return { result: exported, changed: false };
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
async import(values) {
|
|
225
|
+
const entries = Object.entries(values);
|
|
226
|
+
for (const [reference, value] of entries) {
|
|
227
|
+
assertReference(reference);
|
|
228
|
+
if (typeof value !== 'string')
|
|
229
|
+
throw new Error('导入的凭据值必须是字符串');
|
|
230
|
+
}
|
|
231
|
+
return access((stored) => {
|
|
232
|
+
const mapping = {};
|
|
233
|
+
for (const [oldReference, value] of entries) {
|
|
234
|
+
let next;
|
|
235
|
+
do {
|
|
236
|
+
next = `secret://${randomUUID()}`;
|
|
237
|
+
} while (Object.hasOwn(stored, next) || Object.hasOwn(values, next));
|
|
238
|
+
stored[next] = value;
|
|
239
|
+
mapping[oldReference] = next;
|
|
240
|
+
}
|
|
241
|
+
return { result: mapping, changed: entries.length > 0 };
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
package/dist/session.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { PluginConfig } from './config-api.js';
|
|
2
|
+
export type { PluginConfig } from './config-api.js';
|
|
1
3
|
import type { AgentSession, ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';
|
|
2
4
|
import { type PluginJsonObject, type PluginSource } from './shared.ts';
|
|
3
5
|
import type { SessionCapabilityRules } from './capabilities.js';
|
|
@@ -44,6 +46,7 @@ export type SessionPluginMethodEvent = {
|
|
|
44
46
|
};
|
|
45
47
|
export interface SessionPluginFacade {
|
|
46
48
|
readonly name: string;
|
|
49
|
+
readonly config: PluginConfig;
|
|
47
50
|
setState(state: PluginJsonObject | null): void;
|
|
48
51
|
addPreviewDirectory(directory: string, options?: {
|
|
49
52
|
default?: boolean;
|
package/dist/session.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { bindConfigStore } from "./config-store.js";
|
|
2
3
|
import { normalizePluginIdentifier } from "./shared.js";
|
|
3
4
|
export { PluginMethodError, isPluginMethodError } from "./errors.js";
|
|
4
5
|
export const SESSION_PLUGIN_STATE_EVENT = 'pi-desk:session-plugins';
|
|
@@ -10,6 +11,7 @@ export function bindSessionPlugin(pi, pluginName) {
|
|
|
10
11
|
const name = normalizePluginIdentifier(pluginName, 'pluginName');
|
|
11
12
|
return {
|
|
12
13
|
name,
|
|
14
|
+
config: bindConfigStore('plugins', name),
|
|
13
15
|
setState(state) {
|
|
14
16
|
pi.events.emit(SESSION_PLUGIN_STATE_EVENT, { [name]: state });
|
|
15
17
|
},
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"url": "git+https://github.com/JetCrab/pi-desk.git",
|
|
6
6
|
"directory": "plugins/pi-desk-sdk"
|
|
7
7
|
},
|
|
8
|
-
"version": "1.1.
|
|
8
|
+
"version": "1.1.19-dev",
|
|
9
9
|
"license": "Apache-2.0",
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"registry": "https://registry.npmjs.org",
|
|
@@ -18,6 +18,20 @@
|
|
|
18
18
|
"import": "./dist/index.js",
|
|
19
19
|
"default": "./dist/index.js"
|
|
20
20
|
},
|
|
21
|
+
"./config": {
|
|
22
|
+
"types": "./dist/config.d.ts",
|
|
23
|
+
"node": {
|
|
24
|
+
"import": "./dist/config.js",
|
|
25
|
+
"default": "./dist/config.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"./secrets": {
|
|
29
|
+
"types": "./dist/secrets.d.ts",
|
|
30
|
+
"node": {
|
|
31
|
+
"import": "./dist/secrets.js",
|
|
32
|
+
"default": "./dist/secrets.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
21
35
|
"./settings": {
|
|
22
36
|
"types": "./dist/settings.d.ts",
|
|
23
37
|
"import": "./dist/settings.js",
|
|
@@ -71,6 +85,9 @@
|
|
|
71
85
|
"README.md"
|
|
72
86
|
],
|
|
73
87
|
"dependencies": {
|
|
88
|
+
"@napi-rs/keyring": "2.1.2",
|
|
89
|
+
"proper-lockfile": "^4.1.2",
|
|
90
|
+
"yaml": "^2.9.0",
|
|
74
91
|
"zod": "^4.4.3"
|
|
75
92
|
},
|
|
76
93
|
"peerDependencies": {
|
|
@@ -110,10 +127,12 @@
|
|
|
110
127
|
}
|
|
111
128
|
},
|
|
112
129
|
"devDependencies": {
|
|
130
|
+
"@types/proper-lockfile": "^4.1.4",
|
|
113
131
|
"@base-ui/react": "^1.7.0",
|
|
114
132
|
"@streamdown/cjk": "^1.0.3",
|
|
115
133
|
"@streamdown/code": "^1.1.1",
|
|
116
134
|
"esbuild": "^0.28.2",
|
|
135
|
+
"tsx": "^4.23.12",
|
|
117
136
|
"lucide-react": "^1.31.0",
|
|
118
137
|
"react": "19.2.8",
|
|
119
138
|
"react-dom": "19.2.8",
|
|
@@ -121,6 +140,8 @@
|
|
|
121
140
|
},
|
|
122
141
|
"scripts": {
|
|
123
142
|
"build": "tsc -p tsconfig.json && node scripts/build-host-runtime.mjs",
|
|
124
|
-
"
|
|
143
|
+
"typecheck:isolated": "tsc -p tsconfig.json --noEmit --isolatedModules",
|
|
144
|
+
"test": "pnpm run build && node --test tests/*.test.mjs && pnpm run test:config",
|
|
145
|
+
"test:config": "node --import tsx --test tests/unified-config.test.ts"
|
|
125
146
|
}
|
|
126
147
|
}
|