pi-ai-vault 0.5.0 → 0.6.0
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 +9 -1
- package/package.json +2 -2
- package/src/bitwarden.ts +30 -0
- package/src/index.ts +85 -41
- package/src/settings.ts +59 -0
package/README.md
CHANGED
|
@@ -51,7 +51,15 @@ omp plugin install npm:pi-ai-vault # omp,同一个包
|
|
|
51
51
|
| `time` | 解锁后 N 小时(默认 8)再问一次 |
|
|
52
52
|
| `session` | 每个会话一次,会话结束失效(默认) |
|
|
53
53
|
|
|
54
|
-
`vault_get` 找不到本地条目时会去 Bitwarden
|
|
54
|
+
`vault_get` 找不到本地条目时会去 Bitwarden 搜;命中后按 `bitwarden.import` 处理:
|
|
55
|
+
|
|
56
|
+
| `bitwarden.import` | 行为 |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `ask`(默认) | 给你看这一条 → 读一次 → 问「导入 ai-vault / 只用这一次」 |
|
|
59
|
+
| `always` | 命中直接导入并返回,不开窗口 |
|
|
60
|
+
| `never` | 只读一次,永不写入本地库 |
|
|
61
|
+
|
|
62
|
+
所以默认情况下 **只有你同意导入的密码才会进 ai-vault**。Bitwarden 自己的主密码请求频率也有单独设置(`bitwarden.unlock`:`never` / `time` / `session`),用 `/vault settings` 查看和修改。
|
|
55
63
|
|
|
56
64
|
## 隔离
|
|
57
65
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-ai-vault",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "pi extension for ai-vault: one machine-wide encrypted secret store plus live Bitwarden. Works in pi and in omp (the omp fork reads the same manifest).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"security"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@xiaolikt/ai-vault": "0.
|
|
28
|
+
"@xiaolikt/ai-vault": "0.6.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@earendil-works/pi-coding-agent": "^1.5.0",
|
package/src/bitwarden.ts
CHANGED
|
@@ -55,6 +55,7 @@ import { Type } from "typebox";
|
|
|
55
55
|
import { bitwardenLogins, filterLogins, type LoginDraft } from "./import-map.js";
|
|
56
56
|
import { onSessionLeave, sessionLabel, withTier } from "./host.js";
|
|
57
57
|
import { confirmWindow, hasDisplay, promptSecret } from "./prompt.js";
|
|
58
|
+
import { getSettingsSafe, settingsCli } from "./settings.js";
|
|
58
59
|
import { scrubDeep, scrubText } from "./scrub.js";
|
|
59
60
|
|
|
60
61
|
// ---------------------------------------------------------------------------
|
|
@@ -492,6 +493,9 @@ function startAutoLock(st: BwState, ctx: ExtensionContext | undefined): void {
|
|
|
492
493
|
/** Drop this binding's session key and injected variables, and the clipboard. */
|
|
493
494
|
async function lock(st: BwState, reason: string): Promise<void> {
|
|
494
495
|
const hadSession = !!st.key;
|
|
496
|
+
// A key kept for `bitwarden.unlock = never | time` must go with an explicit
|
|
497
|
+
// lock, or the next call would silently unlock again.
|
|
498
|
+
void settingsCli().run(["bw-session", "clear"]).catch(() => undefined);
|
|
495
499
|
st.epoch++;
|
|
496
500
|
st.key = undefined;
|
|
497
501
|
st.verifiedAt = 0;
|
|
@@ -545,6 +549,27 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
|
|
|
545
549
|
|
|
546
550
|
const epoch = st.epoch;
|
|
547
551
|
const pending = (async () => {
|
|
552
|
+
const policy = (await getSettingsSafe(signal)).bitwarden;
|
|
553
|
+
// `never` / `time`: the CLI keeps the session key (0600, in the store
|
|
554
|
+
// directory), so Bitwarden costs no window at all.
|
|
555
|
+
if (policy.unlock !== "session") {
|
|
556
|
+
try {
|
|
557
|
+
const saved = await settingsCli().json<{ key: string }>(["bw-session", "get"], { signal });
|
|
558
|
+
if (saved?.key) {
|
|
559
|
+
const verify = await bwStatus(saved.key, signal);
|
|
560
|
+
if (verify?.status === "unlocked") {
|
|
561
|
+
st.key = saved.key;
|
|
562
|
+
st.verifiedAt = Date.now();
|
|
563
|
+
acquireSessionKey();
|
|
564
|
+
startAutoLock(st, ctx);
|
|
565
|
+
return saved.key;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
} catch {
|
|
569
|
+
/* absent or expired: fall through to the password */
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
548
573
|
const status = await bwStatus(undefined, signal); // no session env
|
|
549
574
|
if (status?.status === "unauthenticated") throw new Error(BW_LOGIN_HINT);
|
|
550
575
|
|
|
@@ -569,6 +594,11 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
|
|
|
569
594
|
st.verifiedAt = Date.now();
|
|
570
595
|
acquireSessionKey();
|
|
571
596
|
startAutoLock(st, ctx);
|
|
597
|
+
if (policy.unlock !== "session") {
|
|
598
|
+
// Keep the key where the CLI can hand it back; the file's expiry comes
|
|
599
|
+
// from the bitwarden.hours policy.
|
|
600
|
+
await settingsCli().run(["bw-session", "put"], { stdin: key }).catch(() => undefined);
|
|
601
|
+
}
|
|
572
602
|
await audit("unlock", "ok", st.sessionLabel);
|
|
573
603
|
// Refresh in the background: a full sync can take seconds on large vaults.
|
|
574
604
|
const lastSync = Date.parse(status?.lastSync ?? "");
|
package/src/index.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
import { CliError, VaultCli } from "./cli.js";
|
|
26
26
|
import { isOmpHost, onSessionLeave, sessionLabel, subagentId, withTier } from "./host.js";
|
|
27
27
|
import { confirmWindow, promptSecret, promptText, type PromptHost } from "./prompt.js";
|
|
28
|
+
import { forgetSettings, getSettingsSafe, type Settings } from "./settings.js";
|
|
28
29
|
|
|
29
30
|
type Ctx = ExtensionToolContext | ExtensionCommandContext;
|
|
30
31
|
|
|
@@ -309,22 +310,21 @@ async function bitwardenFallback(
|
|
|
309
310
|
}
|
|
310
311
|
if (!hit) return fail(`${missing} — not in Bitwarden either${reason ? ` (reason: ${reason})` : ""}`);
|
|
311
312
|
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
313
|
+
const asValue = (via: string): ToolOutcome => ({
|
|
314
|
+
content: [
|
|
315
|
+
{
|
|
316
|
+
type: "text" as const,
|
|
317
|
+
text: JSON.stringify(
|
|
318
|
+
{ name: hit.name, username: hit.username || undefined, password: hit.password, uris: hit.uris, via },
|
|
319
|
+
null,
|
|
320
|
+
1,
|
|
321
|
+
),
|
|
322
|
+
},
|
|
323
|
+
],
|
|
324
|
+
details: { item: hit.name, via },
|
|
325
|
+
});
|
|
320
326
|
|
|
321
|
-
const importIt =
|
|
322
|
-
dialogs(ctx),
|
|
323
|
-
"pi-ai-vault · 导入到 ai-vault",
|
|
324
|
-
`把「${hit.name}」写进 ai-vault 吗?\n\n允许 → 以后直接读本地库(按当前解锁模式,可能免密)\n拒绝 → 只用这一次,不写入本地库`,
|
|
325
|
-
signal,
|
|
326
|
-
);
|
|
327
|
-
if (importIt) {
|
|
327
|
+
const importIt = async (): Promise<ToolOutcome> => {
|
|
328
328
|
try {
|
|
329
329
|
const saved = await cli.json<SaveResult>(["save", "--yes", ...tokenArgs(scope)], {
|
|
330
330
|
stdin: JSON.stringify([
|
|
@@ -343,34 +343,32 @@ async function bitwardenFallback(
|
|
|
343
343
|
} catch (error) {
|
|
344
344
|
return fail(`found "${hit.name}" in Bitwarden but could not import it: ${describe(error)}`);
|
|
345
345
|
}
|
|
346
|
-
return
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
1,
|
|
354
|
-
),
|
|
355
|
-
},
|
|
356
|
-
],
|
|
357
|
-
details: { item: hit.name, via: "imported-from-bitwarden" },
|
|
358
|
-
};
|
|
346
|
+
return asValue("imported-from-bitwarden");
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const policy = (await getSettingsSafe(signal)).bitwarden.import;
|
|
350
|
+
if (policy === "always") {
|
|
351
|
+
// bitwarden.import = always: the user opted into pulling hits in silently.
|
|
352
|
+
return importIt();
|
|
359
353
|
}
|
|
360
354
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
355
|
+
const label = `${hit.name}${hit.username ? ` user:${hit.username}` : ""}`;
|
|
356
|
+
const allow = await confirmWindow(
|
|
357
|
+
dialogs(ctx),
|
|
358
|
+
"pi-ai-vault · Bitwarden 里找到",
|
|
359
|
+
`本地库没有「${name}」,Bitwarden 里有:\n${label}\n\n读取这一次吗?`,
|
|
360
|
+
signal,
|
|
361
|
+
);
|
|
362
|
+
if (!allow) return fail(`user denied reading "${hit.name}" from Bitwarden — do not retry`, true);
|
|
363
|
+
if (policy === "never") return asValue("bitwarden-once");
|
|
364
|
+
|
|
365
|
+
const confirmed = await confirmWindow(
|
|
366
|
+
dialogs(ctx),
|
|
367
|
+
"pi-ai-vault · 导入到 ai-vault",
|
|
368
|
+
`把「${hit.name}」写进 ai-vault 吗?\n\n允许 → 以后直接读本地库(按当前解锁模式,可能免密)\n拒绝 → 只用这一次,不写入本地库`,
|
|
369
|
+
signal,
|
|
370
|
+
);
|
|
371
|
+
return confirmed ? importIt() : asValue("bitwarden-once");
|
|
374
372
|
}
|
|
375
373
|
|
|
376
374
|
function vaultGetTool(scope: Scope, bw: BwState): ToolDefinition<typeof vaultGetParams, VaultGetDetails> {
|
|
@@ -628,6 +626,7 @@ const HELP = [
|
|
|
628
626
|
"/vault init 创建全机共享的本地加密库(弹出窗口设置主密码)",
|
|
629
627
|
"/vault unlock | lock 解锁 / 锁定(清空本会话的授权)",
|
|
630
628
|
"/vault mode [never|time|session] 查看/切换解锁模式(切换要主密码)",
|
|
629
|
+
"/vault settings [键 值] 查看/修改设置(vault.unlock、bitwarden.unlock、bitwarden.import …)",
|
|
631
630
|
"/vault status 状态与授权剩余时间",
|
|
632
631
|
"/vault list 本地条目元数据",
|
|
633
632
|
"/vault add [名称] 新增本地条目(用户名、密码在弹窗里输入)",
|
|
@@ -688,6 +687,23 @@ async function initStore(scope: Scope, ctx: Ctx, label: string): Promise<void> {
|
|
|
688
687
|
}
|
|
689
688
|
}
|
|
690
689
|
|
|
690
|
+
function settingsText(s: Settings): string {
|
|
691
|
+
const persisted = s.bitwarden.persisted
|
|
692
|
+
? s.bitwarden.expiresAt
|
|
693
|
+
? `有(${Math.max(0, Math.round((s.bitwarden.expiresAt - Date.now()) / 60_000))} 分钟后过期)`
|
|
694
|
+
: "有(不过期)"
|
|
695
|
+
: "无";
|
|
696
|
+
return [
|
|
697
|
+
`vault.unlock: ${s.vault.unlock} (never | time | session)`,
|
|
698
|
+
`vault.hours: ${s.vault.hours} (time 模式用)`,
|
|
699
|
+
`vault 机器级会话: ${s.vault.auto ? "有(免密)" : "无"}`,
|
|
700
|
+
`bitwarden.unlock: ${s.bitwarden.unlock} (never | time | session)`,
|
|
701
|
+
`bitwarden.hours: ${s.bitwarden.hours}`,
|
|
702
|
+
`bitwarden.import: ${s.bitwarden.import} (ask | always | never)`,
|
|
703
|
+
`bitwarden 已落盘的会话: ${persisted}`,
|
|
704
|
+
].join("\n");
|
|
705
|
+
}
|
|
706
|
+
|
|
691
707
|
function commandHandler(scope: Scope, bw: BwState) {
|
|
692
708
|
return async (args: string, ctx: ExtensionCommandContext): Promise<void> => {
|
|
693
709
|
const [sub = "status", ...rest] = words(args);
|
|
@@ -756,6 +772,33 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
756
772
|
}
|
|
757
773
|
return;
|
|
758
774
|
}
|
|
775
|
+
case "settings": {
|
|
776
|
+
const [key, ...valueParts] = rest;
|
|
777
|
+
const value = valueParts.join(" ").trim();
|
|
778
|
+
try {
|
|
779
|
+
if (!key) {
|
|
780
|
+
note(ctx, settingsText(await cli.json<Settings>(["settings"])));
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
if (!value) {
|
|
784
|
+
const one = await cli.json<{ key: string; value: string }>(["settings", key]);
|
|
785
|
+
note(ctx, `${one.key}: ${one.value}`);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
// `vault.unlock` opens its own password window (or answers
|
|
789
|
+
// needs-ui); the other keys do not prompt at all.
|
|
790
|
+
const run = await cli.withPassword(["settings", key, value], () =>
|
|
791
|
+
promptSecret(dialogs(ctx), "ai-vault · 主密码", "改变解锁模式需要验证"),
|
|
792
|
+
);
|
|
793
|
+
forgetSettings();
|
|
794
|
+
const updated = JSON.parse(run.stdout) as Settings;
|
|
795
|
+
if (key.startsWith("vault.")) await lockNow(scope);
|
|
796
|
+
note(ctx, settingsText(updated));
|
|
797
|
+
} catch (error) {
|
|
798
|
+
note(ctx, `ai-vault: ${error instanceof CliError && error.denied ? "已取消" : describe(error)}`, "warning");
|
|
799
|
+
}
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
759
802
|
case "mode": {
|
|
760
803
|
const [sub, hoursArg] = rest;
|
|
761
804
|
if (!sub || sub === "get" || sub === "status") {
|
|
@@ -785,6 +828,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
785
828
|
try {
|
|
786
829
|
const run = await cli.withPassword(args, () => promptSecret(dialogs(ctx), "ai-vault · 主密码", "切换解锁模式"));
|
|
787
830
|
const result = JSON.parse(run.stdout) as { mode: UnlockModeName; hours: number; auto: boolean };
|
|
831
|
+
forgetSettings();
|
|
788
832
|
await lockNow(scope);
|
|
789
833
|
scope.mode = result.mode;
|
|
790
834
|
scope.auto = result.auto;
|
package/src/settings.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Machine-wide settings, owned by the CLI (`ai-vault settings`), read here.
|
|
3
|
+
*
|
|
4
|
+
* The vault decides when to ask for its password; the Bitwarden side decides
|
|
5
|
+
* separately whether its `bw` session key may live on disk, and whether a hit
|
|
6
|
+
* may be imported into the vault without a window.
|
|
7
|
+
*/
|
|
8
|
+
import { VaultCli } from "./cli.js";
|
|
9
|
+
|
|
10
|
+
export type UnlockModeName = "never" | "time" | "session";
|
|
11
|
+
export type ImportPolicy = "ask" | "always" | "never";
|
|
12
|
+
|
|
13
|
+
export interface Settings {
|
|
14
|
+
vault: { unlock: UnlockModeName; hours: number; auto: boolean };
|
|
15
|
+
bitwarden: {
|
|
16
|
+
unlock: UnlockModeName;
|
|
17
|
+
hours: number;
|
|
18
|
+
import: ImportPolicy;
|
|
19
|
+
persisted: boolean;
|
|
20
|
+
expiresAt: number | null;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const DEFAULT_SETTINGS: Settings = {
|
|
25
|
+
vault: { unlock: "session", hours: 8, auto: false },
|
|
26
|
+
bitwarden: { unlock: "session", hours: 8, import: "ask", persisted: false, expiresAt: null },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const cli = new VaultCli();
|
|
30
|
+
let cache: { at: number; value: Settings } | undefined;
|
|
31
|
+
|
|
32
|
+
/** The shared CLI client, for the pieces that read and write the settings. */
|
|
33
|
+
export function settingsCli(): VaultCli {
|
|
34
|
+
return cli;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Settings change rarely, so a short cache keeps a Bitwarden call from paying
|
|
39
|
+
* for the read. `/vault settings ...` drops it explicitly.
|
|
40
|
+
*/
|
|
41
|
+
export async function getSettings(signal?: AbortSignal, options: { fresh?: boolean } = {}): Promise<Settings> {
|
|
42
|
+
if (!options.fresh && cache && Date.now() - cache.at < 30_000) return cache.value;
|
|
43
|
+
const value = await cli.json<Settings>(["settings"], { signal });
|
|
44
|
+
cache = { at: Date.now(), value };
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** For paths that must keep working when the CLI is missing: defaults instead. */
|
|
49
|
+
export async function getSettingsSafe(signal?: AbortSignal): Promise<Settings> {
|
|
50
|
+
try {
|
|
51
|
+
return await getSettings(signal);
|
|
52
|
+
} catch {
|
|
53
|
+
return DEFAULT_SETTINGS;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function forgetSettings(): void {
|
|
58
|
+
cache = undefined;
|
|
59
|
+
}
|