pi-advisor-flow 0.17.1-dev.1 → 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.
- package/CHANGELOG.md +20 -1
- package/README.md +3 -1
- package/dist/index.js +196 -92
- package/docs/development.md +1 -1
- package/extensions/index.ts +2 -0
- package/locales/de.json +3 -1
- package/locales/en.json +3 -1
- package/locales/fr.json +3 -1
- package/package.json +10 -10
- package/scripts/check-bun-version.mjs +1 -1
- package/src/attachments.ts +58 -40
- package/src/commands/activation.ts +35 -14
- package/src/commands/lifecycle.ts +10 -3
- package/src/commands/model-commands.ts +3 -1
- package/src/commands/settings-persistence.ts +4 -1
- package/src/config/schema-validators.ts +5 -1
- package/src/config/schema.ts +2 -1
- package/src/config/storage.ts +19 -1
- package/src/config/types.ts +1 -0
- package/src/git.ts +62 -23
- package/src/image-attachments.ts +28 -12
- package/src/images.ts +27 -17
- package/src/jev/classifier-key-store.ts +4 -7
- package/src/jev/endpoint-key-store.ts +3 -14
- package/src/jev/key-file.ts +35 -0
- package/src/jev/key-store.ts +5 -24
- package/src/jev/openai-key-store.ts +3 -13
- package/src/pi-settings.ts +30 -0
- package/src/tools/budget-feedback.ts +4 -1
- package/src/tools/consult-context.ts +1 -1
- package/src/ui/jev-provider-submenu.ts +37 -0
package/src/images.ts
CHANGED
|
@@ -52,6 +52,22 @@ export const imageFromPart = (part: RecordValue): ImageContent | undefined => {
|
|
|
52
52
|
return imageFromBytes(bytes, part.mimeType);
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
+
const IMAGE_OMITTED_MARKER =
|
|
56
|
+
"[Image omitted: unsupported format, invalid data, or over 4 MiB; pixels not reviewed]";
|
|
57
|
+
|
|
58
|
+
const imageMarkerFromImage = (
|
|
59
|
+
image: Pick<ImageContent, "data">,
|
|
60
|
+
nonce = ""
|
|
61
|
+
): string => {
|
|
62
|
+
const id = createHash("sha256")
|
|
63
|
+
.update(nonce)
|
|
64
|
+
.update(image.data)
|
|
65
|
+
.digest("hex")
|
|
66
|
+
.slice(0, 24);
|
|
67
|
+
|
|
68
|
+
return `[Image ref=img_${id}; pixels reviewed only if attached below]`;
|
|
69
|
+
};
|
|
70
|
+
|
|
55
71
|
export const imageMarker = (
|
|
56
72
|
part: RecordValue,
|
|
57
73
|
nonce = ""
|
|
@@ -62,17 +78,7 @@ export const imageMarker = (
|
|
|
62
78
|
|
|
63
79
|
const image = imageFromPart(part);
|
|
64
80
|
|
|
65
|
-
|
|
66
|
-
return "[Image omitted: unsupported format, invalid data, or over 4 MiB; pixels not reviewed]";
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const id = createHash("sha256")
|
|
70
|
-
.update(nonce)
|
|
71
|
-
.update(image.data)
|
|
72
|
-
.digest("hex")
|
|
73
|
-
.slice(0, 24);
|
|
74
|
-
|
|
75
|
-
return `[Image ref=img_${id}; pixels reviewed only if attached below]`;
|
|
81
|
+
return image ? imageMarkerFromImage(image, nonce) : IMAGE_OMITTED_MARKER;
|
|
76
82
|
};
|
|
77
83
|
|
|
78
84
|
interface SelectedImage {
|
|
@@ -111,18 +117,22 @@ const entryImageCensus = (
|
|
|
111
117
|
parts += 1;
|
|
112
118
|
}
|
|
113
119
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (!marker || seen.has(marker) || !conversation.includes(marker)) {
|
|
120
|
+
if (part.type !== "image") {
|
|
117
121
|
continue;
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
const image = imageFromPart(part);
|
|
121
125
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
126
|
+
const marker = image
|
|
127
|
+
? imageMarkerFromImage(image, nonce)
|
|
128
|
+
: IMAGE_OMITTED_MARKER;
|
|
129
|
+
|
|
130
|
+
if (!image || seen.has(marker) || !conversation.includes(marker)) {
|
|
131
|
+
continue;
|
|
125
132
|
}
|
|
133
|
+
|
|
134
|
+
seen.add(marker);
|
|
135
|
+
selected.push({ image, marker });
|
|
126
136
|
}
|
|
127
137
|
|
|
128
138
|
return { parts, selected };
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readFileSync, rmSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
|
|
4
4
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
5
5
|
|
|
6
|
+
import { writeKeyFile } from "./key-file.ts";
|
|
7
|
+
|
|
6
8
|
export interface JevClassifierSecrets {
|
|
7
9
|
delete: (options: {
|
|
8
10
|
name: string;
|
|
@@ -34,7 +36,6 @@ export interface JevClassifierKeySnapshot {
|
|
|
34
36
|
|
|
35
37
|
export const JEV_CLASSIFIER_KEY_SERVICE = "pi-advisor-jev-classifier";
|
|
36
38
|
export const JEV_CLASSIFIER_KEY_NAME = "classifier-api-key";
|
|
37
|
-
const KEY_FILE_MODE = 0o600;
|
|
38
39
|
const keyFilePath = () => join(getAgentDir(), "jev_classifier_api_key");
|
|
39
40
|
let classifierKeyFailClosed = false;
|
|
40
41
|
|
|
@@ -65,11 +66,7 @@ const defaultReadFile = () => {
|
|
|
65
66
|
}
|
|
66
67
|
};
|
|
67
68
|
|
|
68
|
-
const defaultWriteFile = (key: string) =>
|
|
69
|
-
const path = keyFilePath();
|
|
70
|
-
writeFileSync(path, `${key}\n`, { mode: KEY_FILE_MODE });
|
|
71
|
-
chmodSync(path, KEY_FILE_MODE);
|
|
72
|
-
};
|
|
69
|
+
const defaultWriteFile = (key: string) => writeKeyFile(keyFilePath(), key);
|
|
73
70
|
|
|
74
71
|
export const readJevClassifierKey = async (
|
|
75
72
|
deps: JevClassifierKeyStoreDeps = {}
|
|
@@ -1,23 +1,16 @@
|
|
|
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 JEV_ENDPOINT_KEY_ENV_VAR = "JEV_API_KEY";
|
|
16
11
|
const JEV_ENDPOINT_KEY_SERVICE = "pi-advisor";
|
|
17
12
|
const JEV_ENDPOINT_KEY_NAME = "jev-api-key";
|
|
18
13
|
|
|
19
|
-
const KEY_FILE_MODE = 0o600;
|
|
20
|
-
|
|
21
14
|
type JevEndpointKeySource = "jev-bun-secrets" | "jev-env" | "jev-file";
|
|
22
15
|
|
|
23
16
|
export interface JevEndpointKeyResolution {
|
|
@@ -57,11 +50,7 @@ const readDefaultFileStore = (): string | undefined => {
|
|
|
57
50
|
}
|
|
58
51
|
};
|
|
59
52
|
|
|
60
|
-
const writeDefaultFileStore = (key: string) =>
|
|
61
|
-
const path = keyFilePath();
|
|
62
|
-
writeFileSync(path, `${key}\n`, { mode: KEY_FILE_MODE });
|
|
63
|
-
chmodSync(path, KEY_FILE_MODE);
|
|
64
|
-
};
|
|
53
|
+
const writeDefaultFileStore = (key: string) => writeKeyFile(keyFilePath(), key);
|
|
65
54
|
|
|
66
55
|
const deleteDefaultFileStore = () => rmSync(keyFilePath(), { force: true });
|
|
67
56
|
|
|
@@ -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
|
+
};
|
package/src/jev/key-store.ts
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
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
|
|
package/src/pi-settings.ts
CHANGED
|
@@ -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)) {
|
|
@@ -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;
|