pi-ai-vault 0.14.0 → 0.14.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-ai-vault",
3
- "version": "0.14.0",
3
+ "version": "0.14.1",
4
4
  "description": "pi extension for ai-vault: one machine-wide encrypted secret store, live Bitwarden and Ente Auth. 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.14.0"
28
+ "@xiaolikt/ai-vault": "0.14.1"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@earendil-works/pi-coding-agent": "^1.5.0",
package/src/bitwarden.ts CHANGED
@@ -57,7 +57,7 @@ import type { Draft } from "./types.js";
57
57
  import { onSessionLeave, sessionLabel, withTier } from "./host.js";
58
58
  import { confirmWindow, hasDisplay, promptSecret } from "./prompt.js";
59
59
  import { npmShimLaunch, type CliLaunch } from "./cli.js";
60
- import { getSettingsSafe, settingsCli, settingsToken } from "./settings.js";
60
+ import { getSettingsSafe, settingsCli, settingsOpts, settingsToken } from "./settings.js";
61
61
  import { tr } from "./i18n.js";
62
62
  import { scrubDeep, scrubText } from "./scrub.js";
63
63
 
@@ -676,7 +676,7 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
676
676
  // directory), so Bitwarden costs no window at all.
677
677
  if (policy.unlock !== "session") {
678
678
  try {
679
- const saved = await settingsCli().json<{ key: string }>(["bw-session", "get"], { signal });
679
+ const saved = await settingsCli().json<{ key: string }>(["bw-session", "get"], { signal, ...settingsOpts() });
680
680
  if (saved?.key) {
681
681
  const verify = await bwStatus(saved.key, signal);
682
682
  if (verify?.status === "unlocked") {
@@ -720,7 +720,7 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
720
720
  // Keep the key where the CLI can hand it back; the file's expiry comes
721
721
  // from the bitwarden.hours policy.
722
722
  const token = settingsToken();
723
- await settingsCli().run(["bw-session", "put", ...(token ? ["--token", token] : [])], { stdin: key }).catch(() => undefined);
723
+ await settingsCli().run(["bw-session", "put", ...(token ? ["--token", token] : [])], { stdin: key, ...settingsOpts() }).catch(() => undefined);
724
724
  }
725
725
  await auditEvent("unlock", "ok", st.sessionLabel);
726
726
  // Refresh in the background: a full sync can take seconds on large vaults.
package/src/ente.ts CHANGED
@@ -26,7 +26,7 @@ import { Type } from "typebox";
26
26
  import { auditEvent } from "./bitwarden.js";
27
27
  import { confirmWindow, forDialog } from "./prompt.js";
28
28
  import { withTier } from "./host.js";
29
- import { getSettingsSafe, settingsCli } from "./settings.js";
29
+ import { getSettingsSafe, settingsCli, settingsOpts } from "./settings.js";
30
30
 
31
31
  export interface EnteCodeMeta {
32
32
  id: string;
@@ -94,7 +94,7 @@ async function enteEnv(signal?: AbortSignal): Promise<Record<string, string | un
94
94
  const settings = await getSettingsSafe(signal);
95
95
  if (settings.ente.unlock === "session") return env;
96
96
  try {
97
- const sealed = await settingsCli().json<{ key: string }>(["ente-session", "get"], { signal });
97
+ const sealed = await settingsCli().json<{ key: string }>(["ente-session", "get"], { signal, ...settingsOpts() });
98
98
  if (sealed?.key) env.ENTE_CLI_VAULT_KEY = sealed.key;
99
99
  } catch {
100
100
  // No key saved yet, or it expired: ente will fall back to the keychain,
package/src/index.ts CHANGED
@@ -29,7 +29,7 @@ import type { Draft, ItemType } from "./types.js";
29
29
  import { registerEnte } from "./ente.js";
30
30
  import { isOmpHost, onSessionLeave, sessionLabel, subagentId, withTier } from "./host.js";
31
31
  import { confirmWindow, forDialog, promptSecret, promptText, type PromptHost } from "./prompt.js";
32
- import { forgetSettings, getSettingsSafe, setSettingsToken, settingsCli, settingsToken, type Settings } from "./settings.js";
32
+ import { forgetSettings, getSettingsSafe, setEveryPassword, setSettingsToken, settingsCli, settingsOpts, settingsToken, type Settings } from "./settings.js";
33
33
 
34
34
  type Ctx = ExtensionToolContext | ExtensionCommandContext;
35
35
 
@@ -40,6 +40,8 @@ type UnlockModeName = "never" | "time" | "every";
40
40
  interface StatusResult {
41
41
  ok: boolean;
42
42
  unlocked: boolean;
43
+ /** The session the call ran under; a binding that came without a token adopts it. */
44
+ token?: string | null;
43
45
  items: number;
44
46
  requester: string;
45
47
  idleMs: number;
@@ -152,6 +154,7 @@ async function lockNow(scope: Scope): Promise<void> {
152
154
  scope.epoch++;
153
155
  scope.items = 0;
154
156
  setSettingsToken(undefined);
157
+ setEveryPassword(undefined);
155
158
  forgetSettings();
156
159
  if (!token || !cli.available) return;
157
160
  try {
@@ -215,7 +218,11 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
215
218
  scope.items = status.items;
216
219
  scope.mode = status.mode;
217
220
  scope.auto = status.auto;
218
- setSettingsToken(undefined);
221
+ // In `time` mode the CLI found an existing unlock and minted a
222
+ // session of our own under it: keep that token, so relayed
223
+ // approvals are signed for the session that will check them.
224
+ scope.token = status.token ?? undefined;
225
+ setSettingsToken(scope.token);
219
226
  return true;
220
227
  } catch (error) {
221
228
  if (error instanceof CliError && /not initialized/i.test(error.message)) {
@@ -231,6 +238,7 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
231
238
  scope.items = status.items;
232
239
  scope.mode = "every";
233
240
  scope.everyPassword = password;
241
+ setEveryPassword(password);
234
242
  return true;
235
243
  } catch (retryError) {
236
244
  if (retryError instanceof CliError && retryError.auth) {
@@ -284,7 +292,10 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
284
292
  // next call asks again.
285
293
  scope.token = result.mode === "never" || result.mode === "every" ? undefined : result.token;
286
294
  scope.items = result.items;
287
- if (result.mode === "every") scope.everyPassword = lastUnlockPassword;
295
+ if (result.mode === "every") {
296
+ scope.everyPassword = lastUnlockPassword;
297
+ setEveryPassword(lastUnlockPassword);
298
+ }
288
299
  setSettingsToken(scope.token);
289
300
  return true;
290
301
  } catch (error) {
@@ -332,6 +343,7 @@ function vaultListTool(scope: Scope): ToolDefinition<typeof vaultListParams, { o
332
343
  return failure(scope, error);
333
344
  } finally {
334
345
  scope.everyPassword = undefined;
346
+ setEveryPassword(undefined);
335
347
  }
336
348
  },
337
349
  };
@@ -489,6 +501,7 @@ function vaultGetTool(scope: Scope, bw: BwState): ToolDefinition<typeof vaultGet
489
501
  return failure(scope, error);
490
502
  } finally {
491
503
  scope.everyPassword = undefined;
504
+ setEveryPassword(undefined);
492
505
  }
493
506
  },
494
507
  };
@@ -615,7 +628,7 @@ async function saveLocalEntry(scope: Scope, ctx: Ctx, req: SaveRequest): Promise
615
628
  if (!name) return "cancelled: no entry name";
616
629
 
617
630
  const kind = req.type ?? "login";
618
- const args = ["add", "--name", name, "--type", kind, "--token", scope.token!];
631
+ const args = ["add", "--name", name, "--type", kind, ...tokenArgs(scope)];
619
632
  if (req.username !== undefined) args.push("--username", req.username);
620
633
  if (req.uri?.trim()) args.push("--uri", req.uri.trim());
621
634
  if (req.notes?.trim()) args.push("--notes", req.notes.trim());
@@ -723,9 +736,9 @@ async function importBitwarden(
723
736
  return { text: "nothing matched; nothing imported", ok: false, count: 0 };
724
737
  }
725
738
  try {
726
- const saved = await cli.json<SaveResult>(["save", "--yes", "--token", scope.token!], {
739
+ const saved = await cli.json<SaveResult>(["save", "--yes", ...tokenArgs(scope)], {
727
740
  stdin: JSON.stringify(drafts),
728
- signal,
741
+ ...callOpts(scope, signal),
729
742
  });
730
743
  scope.items += saved.count;
731
744
  setStatus(ctx, scope);
@@ -901,14 +914,15 @@ function commandHandler(scope: Scope, bw: BwState) {
901
914
  case "ente-key": {
902
915
  // Ente's unattended key: generated by `ente vault key generate`.
903
916
  // Saved sealed, so `ente.unlock = never | time` can use it.
917
+ if (!(await ensureUnlocked(scope, ctx))) return;
904
918
  const key = await promptSecret(dialogs(ctx), tr("ai-vault · Ente key", "ai-vault · Ente 密钥"), tr("the key printed by ente vault key generate", "ente vault key generate 输出的密钥"));
905
919
  if (!key) {
906
920
  note(ctx, tr("cancelled", "已取消"), "warning");
907
921
  return;
908
922
  }
909
923
  try {
910
- const args = ["ente-session", "put"];
911
- await cli.run(args, { stdin: key });
924
+ const args = ["ente-session", "put", ...tokenArgs(scope)];
925
+ await cli.run(args, { stdin: key, ...callOpts(scope) });
912
926
  forgetSettings();
913
927
  note(ctx, tr("saved (sealed). You can now set /vault settings ente.unlock never", "已保存(封存)。现在可以设置 /vault settings ente.unlock never"));
914
928
  } catch (error) {
@@ -920,6 +934,7 @@ function commandHandler(scope: Scope, bw: BwState) {
920
934
  // /vault confirm show
921
935
  // /vault confirm off|on turn the per-read window off (asks the master password) / on
922
936
  const [arg] = rest;
937
+ if (!(await ensureUnlocked(scope, ctx))) return;
923
938
  if (!arg) {
924
939
  try {
925
940
  const current = await cli.json<Settings>(["settings", ...tokenArgs(scope)], callOpts(scope));
@@ -959,6 +974,9 @@ function commandHandler(scope: Scope, bw: BwState) {
959
974
  case "settings": {
960
975
  const [key, ...valueParts] = rest;
961
976
  const value = valueParts.join(" ").trim();
977
+ // Reading the settings means opening the store: in `every` mode
978
+ // that is a password for this call, asked here.
979
+ if (!(await ensureUnlocked(scope, ctx))) return;
962
980
  try {
963
981
  if (!key) {
964
982
  note(ctx, settingsText(await cli.json<Settings>(["settings", ...tokenArgs(scope)], callOpts(scope))));
@@ -986,6 +1004,7 @@ function commandHandler(scope: Scope, bw: BwState) {
986
1004
  }
987
1005
  case "mode": {
988
1006
  const [sub, hoursArg] = rest;
1007
+ if (!(await ensureUnlocked(scope, ctx))) return;
989
1008
  if (!sub || sub === "get" || sub === "status") {
990
1009
  try {
991
1010
  const current = await cli.json<{ mode: string; hours: number; auto: boolean }>(["mode"]);
@@ -1004,8 +1023,8 @@ function commandHandler(scope: Scope, bw: BwState) {
1004
1023
  }
1005
1024
  return;
1006
1025
  }
1007
- if (!["never", "time", "session", "no-password"].includes(sub)) {
1008
- note(ctx, tr("usage: /vault mode [never|time|session] [hours]", "用法: /vault mode [never|time|session] [小时]"), "warning");
1026
+ if (!["every", "time", "never", "no-password", "every-time"].includes(sub)) {
1027
+ note(ctx, tr("usage: /vault mode [every|time|never] [hours]", "用法: /vault mode [every|time|never] [小时]"), "warning");
1009
1028
  return;
1010
1029
  }
1011
1030
  const args = ["mode", "set", sub];
@@ -1021,9 +1040,9 @@ function commandHandler(scope: Scope, bw: BwState) {
1021
1040
  const how =
1022
1041
  result.mode === "never"
1023
1042
  ? tr("no password from now on (the key is on disk: anything that can read your files can read every password in the store)", "以后免密(密钥已写盘,任何能读你文件的程序都能读到库里的密码)")
1024
- : result.mode === "time"
1025
- ? tr(`the password is asked again every ${result.hours} hours from now on`, `以后每 ${result.hours} 小时要一次密码`)
1026
- : tr("the password is asked once per session from now on", "以后每个会话要一次密码");
1043
+ : result.mode === "every"
1044
+ ? tr("the password is asked on every call from now on; nothing that can open the store is kept", "以后每次调用都要密码;不留任何能打开库的东西")
1045
+ : tr(`the password is asked again every ${result.hours} hours from now on`, `以后每 ${result.hours} 小时要一次密码`);
1027
1046
  note(ctx, `mode → ${result.mode},${how}`);
1028
1047
  } catch (error) {
1029
1048
  note(ctx, `ai-vault: ${error instanceof CliError && error.denied ? tr("cancelled", "已取消") : describe(error)}`, "warning");
@@ -1033,7 +1052,7 @@ function commandHandler(scope: Scope, bw: BwState) {
1033
1052
  case "list": {
1034
1053
  if (!(await ensureUnlocked(scope, ctx))) return;
1035
1054
  try {
1036
- const result = await cli.json<ListResult>(["list", "--token", scope.token!]);
1055
+ const result = await cli.json<ListResult>(["list", ...tokenArgs(scope)], callOpts(scope));
1037
1056
  scope.items = result.count;
1038
1057
  const rows = result.items.map((i) => `${i.name}\t${i.username ?? ""}\t${i.uris[0] ?? ""}`);
1039
1058
  note(ctx, rows.join("\n") || "(empty)");
@@ -1077,9 +1096,9 @@ function commandHandler(scope: Scope, bw: BwState) {
1077
1096
  note(ctx, tr("usage: /vault remove <name>", "用法: /vault remove <名称>"), "warning");
1078
1097
  return;
1079
1098
  }
1080
- const args = ["remove", name, "--token", scope.token!];
1099
+ const args = ["remove", name, ...tokenArgs(scope)];
1081
1100
  try {
1082
- const result = await cli.json<RemovingResult>(args);
1101
+ const result = await cli.json<RemovingResult>(args, callOpts(scope));
1083
1102
  if (result.removed) scope.items = Math.max(0, scope.items - 1);
1084
1103
  setStatus(ctx, scope);
1085
1104
  note(ctx, result.removed ? "removed" : "not found", result.removed ? "info" : "warning");
@@ -1091,7 +1110,7 @@ function commandHandler(scope: Scope, bw: BwState) {
1091
1110
  const sure = await confirmWindow(dialogs(ctx), tr("ai-vault · delete entry", "ai-vault · 删除条目"), tr(`Delete "${forDialog(name)}"? This cannot be undone.`, `删除「${forDialog(name)}」?删除后无法恢复。`));
1092
1111
  if (!sure) return;
1093
1112
  try {
1094
- const result = await cli.json<RemovingResult>([...args, "--yes"]);
1113
+ const result = await cli.json<RemovingResult>([...args, "--yes"], callOpts(scope));
1095
1114
  if (result.removed) scope.items = Math.max(0, scope.items - 1);
1096
1115
  setStatus(ctx, scope);
1097
1116
  note(ctx, result.removed ? "removed" : "not found", result.removed ? "info" : "warning");
@@ -1111,7 +1130,7 @@ function commandHandler(scope: Scope, bw: BwState) {
1111
1130
  note(ctx, tr("unlock first; usage: /vault grant <name> [minutes] (quote names with spaces)", "先解锁;用法: /vault grant <名称> [分钟](带空格的名称加引号)"), "warning");
1112
1131
  return;
1113
1132
  }
1114
- const args = ["grant", name, "--token", scope.token!];
1133
+ const args = ["grant", name, ...tokenArgs(scope)];
1115
1134
  if (minutes) args.push("--minutes", minutes);
1116
1135
  try {
1117
1136
  // A standing grant costs the master password, like a mode change.
@@ -1129,7 +1148,7 @@ function commandHandler(scope: Scope, bw: BwState) {
1129
1148
  case "revoke": {
1130
1149
  if (!(await ensureUnlocked(scope, ctx))) return;
1131
1150
  const target = rest[0] === "all" ? "all" : rest.join(" ").trim();
1132
- const args = ["revoke", "--token", scope.token!];
1151
+ const args = ["revoke", ...tokenArgs(scope)];
1133
1152
  if (target === "all") args.push("--all");
1134
1153
  else if (target) args.push(target);
1135
1154
  else {
@@ -1166,9 +1185,9 @@ function commandHandler(scope: Scope, bw: BwState) {
1166
1185
  return;
1167
1186
  }
1168
1187
  if (!(await ensureUnlocked(scope, ctx))) return;
1169
- const args = ["import", path, "--token", scope.token!];
1188
+ const args = ["import", path, ...tokenArgs(scope)];
1170
1189
  try {
1171
- const result = await cli.json<SaveResult>(args);
1190
+ const result = await cli.json<SaveResult>(args, callOpts(scope));
1172
1191
  scope.items += result.count;
1173
1192
  setStatus(ctx, scope);
1174
1193
  note(ctx, `imported ${result.count} login items`);
@@ -1184,7 +1203,7 @@ function commandHandler(scope: Scope, bw: BwState) {
1184
1203
  );
1185
1204
  if (!sure) return;
1186
1205
  try {
1187
- const result = await cli.json<SaveResult>([...args, "--yes"]);
1206
+ const result = await cli.json<SaveResult>([...args, "--yes"], callOpts(scope));
1188
1207
  scope.items += result.count;
1189
1208
  setStatus(ctx, scope);
1190
1209
  note(ctx, `imported ${result.count} login items`);
package/src/settings.ts CHANGED
@@ -50,6 +50,8 @@ let cache: { at: number; value: Settings } | undefined;
50
50
  * the binding that unlocked sets this before asking.
51
51
  */
52
52
  let sessionToken: string | undefined;
53
+ /** In `every` mode there is no token: the settings are read with the call's password. */
54
+ let everyPassword: string | undefined;
53
55
 
54
56
  export function setSettingsToken(token: string | undefined): void {
55
57
  if (token !== sessionToken) {
@@ -58,17 +60,27 @@ export function setSettingsToken(token: string | undefined): void {
58
60
  }
59
61
  }
60
62
 
63
+ /** The password an `every` call was opened with, for reading the settings under it. */
64
+ export function setEveryPassword(password: string | undefined): void {
65
+ everyPassword = password;
66
+ }
67
+
61
68
  export function settingsToken(): string | undefined {
62
69
  return sessionToken;
63
70
  }
64
71
 
72
+ /** CLI options for a settings read under whatever this binding holds. */
73
+ export function settingsOpts(): { password?: string } {
74
+ return everyPassword !== undefined ? { password: everyPassword } : {};
75
+ }
76
+
65
77
  /**
66
78
  * Settings change rarely, so a short cache keeps a Bitwarden call from paying
67
79
  * for the read. `/vault settings ...` drops it explicitly.
68
80
  */
69
81
  export async function getSettings(signal?: AbortSignal, options: { fresh?: boolean } = {}): Promise<Settings> {
70
82
  if (!options.fresh && cache && Date.now() - cache.at < 30_000) return cache.value;
71
- const value = await settingsCli().json<Settings>(["settings", ...(sessionToken ? ["--token", sessionToken] : [])], { signal });
83
+ const value = await settingsCli().json<Settings>(["settings", ...(sessionToken ? ["--token", sessionToken] : [])], { signal, ...settingsOpts() });
72
84
  cache = { at: Date.now(), value };
73
85
  return value;
74
86
  }