pi-advisor-flow 0.17.1-dev.2 → 0.17.1-dev.3

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.
@@ -0,0 +1,35 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import {
3
+ closeSync,
4
+ openSync,
5
+ renameSync,
6
+ unlinkSync,
7
+ writeFileSync,
8
+ } from "node:fs";
9
+
10
+ const KEY_FILE_MODE = 0o600;
11
+
12
+ export const writeKeyFile = (path: string, key: string) => {
13
+ const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
14
+ let fileDescriptor: number | undefined;
15
+
16
+ try {
17
+ fileDescriptor = openSync(temporaryPath, "wx", KEY_FILE_MODE);
18
+ writeFileSync(fileDescriptor, `${key}\n`, "utf-8");
19
+ closeSync(fileDescriptor);
20
+ fileDescriptor = undefined;
21
+ renameSync(temporaryPath, path);
22
+ } catch (error) {
23
+ if (fileDescriptor !== undefined) {
24
+ closeSync(fileDescriptor);
25
+ }
26
+
27
+ try {
28
+ unlinkSync(temporaryPath);
29
+ } catch {
30
+ // The temporary file may not exist.
31
+ }
32
+
33
+ throw error;
34
+ }
35
+ };
@@ -1,18 +1,13 @@
1
- import {
2
- chmodSync,
3
- existsSync,
4
- readFileSync,
5
- rmSync,
6
- writeFileSync,
7
- } from "node:fs";
1
+ import { existsSync, readFileSync, rmSync } from "node:fs";
8
2
  import { join } from "node:path";
9
3
 
10
4
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
11
5
 
12
- import { readExistingConfig, resetConfigCache } from "../config/storage.ts";
6
+ import { readExistingConfig, removeConfigKeys } from "../config/storage.ts";
13
7
  import { isString } from "../content-utils.ts";
14
8
  import type { RecordValue } from "../content-utils.ts";
15
9
  import { redactSecrets } from "../redaction.ts";
10
+ import { writeKeyFile } from "./key-file.ts";
16
11
 
17
12
  export type JevKeySource = "bun-secrets" | "env" | "file" | "advisor-json";
18
13
 
@@ -55,8 +50,6 @@ const TYPESAFE_KEY_SERVICE = "pi-advisor";
55
50
  const TYPESAFE_KEY_NAME = "typesafe-api-key";
56
51
  const TYPESAFE_KEY_CONFIG_FIELD = "typesafe_api_key";
57
52
 
58
- const KEY_FILE_MODE = 0o600;
59
-
60
53
  const keyFilePath = () => join(getAgentDir(), "typesafe_api_key");
61
54
 
62
55
  interface BunGlobal {
@@ -84,12 +77,7 @@ const defaultReadFileStore = (): string | undefined => {
84
77
  }
85
78
  };
86
79
 
87
- const defaultWriteFileStore = (key: string) => {
88
- const path = keyFilePath();
89
- writeFileSync(path, `${key}\n`, { mode: KEY_FILE_MODE });
90
- // writeFileSync's mode only applies at creation; re-assert it on every write.
91
- chmodSync(path, KEY_FILE_MODE);
92
- };
80
+ const defaultWriteFileStore = (key: string) => writeKeyFile(keyFilePath(), key);
93
81
 
94
82
  const defaultDeleteFileStore = () => {
95
83
  rmSync(keyFilePath(), { force: true });
@@ -255,14 +243,7 @@ export const removeTypeSafeKeyFromAdvisorJson = (): JevKeyStoreResult => {
255
243
  return { message: "No plaintext key in advisor.json.", ok: true };
256
244
  }
257
245
 
258
- const retained = Object.fromEntries(
259
- Object.entries(existing).filter(
260
- ([key]) => key !== TYPESAFE_KEY_CONFIG_FIELD
261
- )
262
- );
263
-
264
- writeFileSync(path, `${JSON.stringify(retained, null, 2)}\n`);
265
- resetConfigCache();
246
+ removeConfigKeys([TYPESAFE_KEY_CONFIG_FIELD]);
266
247
 
267
248
  return { message: "Plaintext key removed from advisor.json.", ok: true };
268
249
  } catch (error) {
@@ -1,20 +1,14 @@
1
- import {
2
- chmodSync,
3
- existsSync,
4
- readFileSync,
5
- rmSync,
6
- writeFileSync,
7
- } from "node:fs";
1
+ import { existsSync, readFileSync, rmSync } from "node:fs";
8
2
  import { join } from "node:path";
9
3
 
10
4
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
11
5
 
12
6
  import { redactSecrets } from "../redaction.ts";
7
+ import { writeKeyFile } from "./key-file.ts";
13
8
  import type { JevKeyStoreResult, JevSecretsLike } from "./key-store.ts";
14
9
 
15
10
  export const OPENAI_DECISIONS_KEY_SERVICE = "pi-advisor";
16
11
  export const OPENAI_DECISIONS_KEY_NAME = "openai-decisions-api-key";
17
- const KEY_FILE_MODE = 0o600;
18
12
 
19
13
  type OpenAIDecisionsKeySource = "openai-bun-secrets" | "openai-file";
20
14
 
@@ -54,11 +48,7 @@ const readDefaultFileStore = (): string | undefined => {
54
48
  }
55
49
  };
56
50
 
57
- const writeDefaultFileStore = (key: string) => {
58
- const path = keyFilePath();
59
- writeFileSync(path, `${key}\n`, { mode: KEY_FILE_MODE });
60
- chmodSync(path, KEY_FILE_MODE);
61
- };
51
+ const writeDefaultFileStore = (key: string) => writeKeyFile(keyFilePath(), key);
62
52
 
63
53
  const deleteDefaultFileStore = () => rmSync(keyFilePath(), { force: true });
64
54
 
@@ -11,6 +11,20 @@ interface CachedSetting {
11
11
  readAt: number;
12
12
  }
13
13
 
14
+ interface EffectivePiSettings {
15
+ hideThinkingBlock?: boolean;
16
+ }
17
+
18
+ let liveSettingsReader: (() => EffectivePiSettings | undefined) | undefined;
19
+ let liveCache: { hidden: boolean | undefined; readAt: number } | undefined;
20
+
21
+ export const setLivePiSettings = (
22
+ reader?: () => EffectivePiSettings | undefined
23
+ ) => {
24
+ liveSettingsReader = reader;
25
+ liveCache = undefined;
26
+ };
27
+
14
28
  // Cached per file and re-read on identity change; stat calls are rate-limited because renderers run per frame.
15
29
  const SETTING_RECHECK_INTERVAL_MS = 2000;
16
30
  const cache = new Map<string, CachedSetting>();
@@ -37,6 +51,22 @@ const readHideThinking = (path: string): boolean => {
37
51
 
38
52
  /** Whether Pi's `hide_thinking` setting hides rendered thinking blocks. */
39
53
  export const piHideThinkingEnabled = (): boolean => {
54
+ if (liveSettingsReader) {
55
+ const now = Date.now();
56
+
57
+ if (!liveCache || now - liveCache.readAt >= SETTING_RECHECK_INTERVAL_MS) {
58
+ const effective = liveSettingsReader();
59
+ liveCache = {
60
+ hidden: effective?.hideThinkingBlock,
61
+ readAt: now,
62
+ };
63
+ }
64
+
65
+ if (liveCache.hidden !== undefined) {
66
+ return liveCache.hidden;
67
+ }
68
+ }
69
+
40
70
  const path = join(getAgentDir(), "settings.json");
41
71
 
42
72
  if (!existsSync(path)) {
@@ -93,7 +93,7 @@ export const assembleConsultationContext = async (
93
93
  advisorGitContextMaxCharsRef
94
94
  );
95
95
 
96
- const changes = collectGitContext(
96
+ const changes = await collectGitContext(
97
97
  ctx.cwd,
98
98
  level,
99
99
  gitBudget,
@@ -463,7 +463,16 @@ export class JevProviderSubmenu implements Component, Focusable {
463
463
  return;
464
464
  }
465
465
 
466
+ let previousKey: { apiKey?: string } | undefined;
467
+ let previousKeyKnown = true;
468
+
466
469
  if (enteredKey) {
470
+ try {
471
+ previousKey = await this.deps.resolveTransport(credentials.transport);
472
+ } catch {
473
+ previousKeyKnown = false;
474
+ }
475
+
467
476
  const stored = await this.deps.writeKey(
468
477
  enteredKey,
469
478
  credentials.transport
@@ -503,9 +512,37 @@ export class JevProviderSubmenu implements Component, Focusable {
503
512
  "jevSetup.providerVerifiedNotSaved",
504
513
  "Provider verified, but settings could not be saved."
505
514
  );
515
+
516
+ if (enteredKey) {
517
+ const restored =
518
+ previousKeyKnown &&
519
+ (await this.restorePreviousKey(credentials.transport, previousKey));
520
+
521
+ this.notice = restored
522
+ ? t(
523
+ "jevSetup.providerKeyRestored",
524
+ "Provider verified, but settings could not be saved; the previous key was restored."
525
+ )
526
+ : t(
527
+ "jevSetup.providerKeyNotRestored",
528
+ "Provider verified, but settings could not be saved and the previous key could not be restored. Re-enter a valid key before retrying."
529
+ );
530
+ }
531
+
506
532
  this.options.tui.requestRender();
507
533
  }
508
534
 
535
+ private async restorePreviousKey(
536
+ transport: JevTransportKind,
537
+ previous: { apiKey?: string } | undefined
538
+ ): Promise<boolean> {
539
+ const result = previous?.apiKey
540
+ ? await this.deps.writeKey(previous.apiKey, transport)
541
+ : await this.deps.clearStoredKey(transport);
542
+
543
+ return result.ok;
544
+ }
545
+
509
546
  private commitSelection(credentials: JevCredentials): boolean {
510
547
  if (!this.options.onSelection) {
511
548
  return false;