pi-advisor-flow 0.13.3-dev.1 → 0.13.3-dev.2

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-advisor-flow",
3
- "version": "0.13.3-dev.1",
3
+ "version": "0.13.3-dev.2",
4
4
  "description": "Advanced Executor/Advisor flow for Pi, fully configurable and extendable.",
5
5
  "keywords": [
6
6
  "advisor",
@@ -59,7 +59,7 @@
59
59
  "@earendil-works/pi-tui": "1.2.0",
60
60
  "@oxlint/plugins": "1.88.0",
61
61
  "@types/node": "^26.6.5",
62
- "@typesafe-ai/sdk": "^0.6.0",
62
+ "@typesafe-ai/sdk": "^0.7.0",
63
63
  "bun-types": "1.4.3",
64
64
  "husky": "^9.1.7",
65
65
  "knip": "6.41.0",
@@ -9,10 +9,10 @@ import {
9
9
  } from "../config/state.ts";
10
10
  import { loadConfig, saveConfig } from "../config/storage.ts";
11
11
  import {
12
- resolveJevTransport,
13
- resolveJevTransportFor,
14
- resolveTypesafeCompatibleCredentials,
15
- } from "../jev/transport.ts";
12
+ readJevClassifierKey,
13
+ restoreJevClassifierKey,
14
+ writeJevClassifierKey,
15
+ } from "../jev/classifier-key-store.ts";
16
16
  import { AdvisorSettingsSelector } from "../ui/settings-selector.ts";
17
17
  import type { AdvisorSettings } from "../ui/types.ts";
18
18
  import { loadCommandConfig } from "./activation-preparation.ts";
@@ -70,12 +70,10 @@ export const registerSettingsCommands = (runtime: CommandRuntime) => {
70
70
  effortLevels: EFFORT_LEVELS,
71
71
  initial,
72
72
  jevSetupDeps: {
73
- resolveEndpoint: (options) =>
74
- resolveTypesafeCompatibleCredentials(options, ctx),
75
- resolveTransport: (transport) =>
76
- transport
77
- ? resolveJevTransportFor(transport, ctx)
78
- : resolveJevTransport(ctx),
73
+ modelRegistry: ctx.modelRegistry,
74
+ persistClassifierKey: writeJevClassifierKey,
75
+ resolveClassifierKey: readJevClassifierKey,
76
+ restoreClassifierKey: restoreJevClassifierKey,
79
77
  },
80
78
  keybindings,
81
79
  modelRefs: getConfiguredModelRefs(ctx),
@@ -18,16 +18,12 @@ import {
18
18
  setAdvisorGitContextRef,
19
19
  setAdvisorHerdrIntegrationRef,
20
20
  setAdvisorJevDigestMaxCharsRef,
21
- setAdvisorJevBaseUrlRef,
21
+ setAdvisorJevClassifierRef,
22
22
  setAdvisorJevFilterEnabledRef,
23
23
  setAdvisorJevFilterNoulMarginRef,
24
24
  setAdvisorJevFilterOverrideWindowRef,
25
25
  setAdvisorJevFilterSkipConfidenceRef,
26
- setAdvisorJevKeyProviderRef,
27
- setAdvisorJevModelRef,
28
- setAdvisorJevPricePerMtokRef,
29
26
  setAdvisorJevTimeoutMsRef,
30
- setAdvisorJevTransportRef,
31
27
  setAdvisorJevTurnGateEveryTurnsRef,
32
28
  setAdvisorJevTurnGateNoulThresholdRef,
33
29
  setAdvisorLoopThresholdRef,
@@ -57,10 +53,7 @@ import {
57
53
  DEFAULT_JEV_FILTER_NOUL_MARGIN,
58
54
  DEFAULT_JEV_FILTER_OVERRIDE_WINDOW,
59
55
  DEFAULT_JEV_FILTER_SKIP_CONFIDENCE,
60
- DEFAULT_JEV_MODEL,
61
- DEFAULT_JEV_PRICE_PER_MTOK,
62
56
  DEFAULT_JEV_TIMEOUT_MS,
63
- DEFAULT_JEV_TRANSPORT,
64
57
  DEFAULT_JEV_TURN_GATE_EVERY_TURNS,
65
58
  DEFAULT_JEV_TURN_GATE_NOUL_THRESHOLD,
66
59
  DEFAULT_SCOUT_TIMEOUT_MS,
@@ -100,8 +93,7 @@ const applySessionSettings = (settings: AdvisorSettings) => {
100
93
 
101
94
  const applyJevSettings = (settings: AdvisorSettings) => {
102
95
  setAdvisorJevFilterEnabledRef(settings.jevFilterEnabled ?? false);
103
- setAdvisorJevBaseUrlRef(settings.jevBaseUrl);
104
- setAdvisorJevKeyProviderRef(settings.jevKeyProvider);
96
+ setAdvisorJevClassifierRef(settings.jevClassifier);
105
97
  setAdvisorJevFilterSkipConfidenceRef(
106
98
  settings.jevFilterSkipConfidence ?? DEFAULT_JEV_FILTER_SKIP_CONFIDENCE
107
99
  );
@@ -111,15 +103,10 @@ const applyJevSettings = (settings: AdvisorSettings) => {
111
103
  setAdvisorJevFilterOverrideWindowRef(
112
104
  settings.jevFilterOverrideWindow ?? DEFAULT_JEV_FILTER_OVERRIDE_WINDOW
113
105
  );
114
- setAdvisorJevModelRef(settings.jevModel ?? DEFAULT_JEV_MODEL);
115
106
  setAdvisorJevTimeoutMsRef(settings.jevTimeoutMs ?? DEFAULT_JEV_TIMEOUT_MS);
116
107
  setAdvisorJevDigestMaxCharsRef(
117
108
  settings.jevDigestMaxChars ?? DEFAULT_JEV_DIGEST_MAX_CHARS
118
109
  );
119
- setAdvisorJevPricePerMtokRef(
120
- settings.jevPricePerMtok ?? DEFAULT_JEV_PRICE_PER_MTOK
121
- );
122
- setAdvisorJevTransportRef(settings.jevTransport ?? DEFAULT_JEV_TRANSPORT);
123
110
  setAdvisorJevTurnGateEveryTurnsRef(
124
111
  settings.jevTurnGateEveryTurns ?? DEFAULT_JEV_TURN_GATE_EVERY_TURNS
125
112
  );
@@ -14,6 +14,7 @@ import {
14
14
  setAdvisorHerdrIntegrationRef,
15
15
  setAdvisorJevDigestMaxCharsRef,
16
16
  setAdvisorJevBaseUrlRef,
17
+ setAdvisorJevClassifierRef,
17
18
  setAdvisorJevFilterEnabledRef,
18
19
  setAdvisorJevFilterNoulMarginRef,
19
20
  setAdvisorJevFilterOverrideWindowRef,
@@ -97,6 +98,7 @@ export const resetDefaults = () => {
97
98
  setAdvisorFailureModeRef("block-session");
98
99
  setAdvisorHerdrIntegrationRef(true);
99
100
  setAdvisorJevModelRef(DEFAULT_JEV_MODEL);
101
+ setAdvisorJevClassifierRef(undefined);
100
102
  setAdvisorJevBaseUrlRef(undefined);
101
103
  setAdvisorJevKeyProviderRef(undefined);
102
104
  setAdvisorJevFilterEnabledRef(false);
@@ -246,6 +248,11 @@ export const applyConfig = (config: AdvisorConfig) => {
246
248
  setAdvisorJevFilterOverrideWindowRef
247
249
  );
248
250
  applyNonEmptyStringConfig(config.advisorJevModel, setAdvisorJevModelRef);
251
+ applyOptionalConfig(
252
+ config,
253
+ "advisorJevClassifier",
254
+ setAdvisorJevClassifierRef
255
+ );
249
256
  applyOptionalConfig(config, "advisorJevBaseUrl", setAdvisorJevBaseUrlRef);
250
257
  applyOptionalConfig(
251
258
  config,
@@ -18,6 +18,7 @@ import {
18
18
  advisorGitContextRef,
19
19
  advisorHerdrIntegrationRef,
20
20
  advisorJevBaseUrlRef,
21
+ advisorJevClassifierRef,
21
22
  advisorJevDigestMaxCharsRef,
22
23
  advisorJevFilterEnabledRef,
23
24
  advisorJevFilterNoulMarginRef,
@@ -274,6 +275,15 @@ export const CONFIG_SCHEMA = {
274
275
  type: "string",
275
276
  validate: isValidJevBaseUrl,
276
277
  },
278
+ advisorJevClassifier: {
279
+ accepted: "a concrete provider/model classifier reference or jev-api-key",
280
+ current: () => advisorJevClassifierRef,
281
+ persisted: true,
282
+ type: "string",
283
+ validate: (value: unknown): value is JsonValue =>
284
+ isString(value) &&
285
+ (value === "jev-api-key" || /^[^/]+\/.+$/u.test(value.trim())),
286
+ },
277
287
  advisorJevDigestMaxChars: {
278
288
  accepted: "a non-negative safe integer",
279
289
  current: () => advisorJevDigestMaxCharsRef,
@@ -60,6 +60,8 @@ export let advisorJevFilterOverrideWindowRef =
60
60
  DEFAULT_JEV_FILTER_OVERRIDE_WINDOW;
61
61
 
62
62
  export let advisorJevModelRef = DEFAULT_JEV_MODEL;
63
+ export let advisorJevClassifierRef: string | undefined;
64
+ export let advisorJevClassifierModeRef: "pi" | "api-key" = "pi";
63
65
  export let advisorJevTimeoutMsRef = DEFAULT_JEV_TIMEOUT_MS;
64
66
  export let advisorJevDigestMaxCharsRef = DEFAULT_JEV_DIGEST_MAX_CHARS;
65
67
  export let advisorJevPricePerMtokRef = DEFAULT_JEV_PRICE_PER_MTOK;
@@ -207,6 +209,12 @@ export const setAdvisorHerdrIntegrationRef = (enabled: boolean) => {
207
209
  advisorHerdrIntegrationRef = enabled;
208
210
  };
209
211
 
212
+ export const setAdvisorJevClassifierRef = (ref: string | undefined) => {
213
+ advisorJevClassifierRef = ref?.trim() || undefined;
214
+ advisorJevClassifierModeRef =
215
+ advisorJevClassifierRef === "jev-api-key" ? "api-key" : "pi";
216
+ };
217
+
210
218
  export const setAdvisorJevFilterEnabledRef = (enabled: boolean) => {
211
219
  advisorJevFilterEnabledRef = enabled;
212
220
  };
@@ -334,6 +342,7 @@ export const getAdvisorSettings = () => ({
334
342
  gitContextMaxChars: advisorGitContextMaxCharsRef,
335
343
  herdrIntegration: advisorHerdrIntegrationRef,
336
344
  jevBaseUrl: advisorJevBaseUrlRef,
345
+ jevClassifier: advisorJevClassifierRef,
337
346
  jevDigestMaxChars: advisorJevDigestMaxCharsRef,
338
347
  jevFilterEnabled: advisorJevFilterEnabledRef,
339
348
  jevFilterNoulMargin: advisorJevFilterNoulMarginRef,
@@ -13,7 +13,12 @@ import {
13
13
  SAVED_CONFIG_KEYS,
14
14
  } from "./schema.ts";
15
15
  import type { PersistedConfigKey } from "./schema.ts";
16
- import { setAdvisorOutcomeLoggingRef, setPersistedModelRefs } from "./state.ts";
16
+ import {
17
+ advisorJevClassifierRef,
18
+ advisorJevFilterEnabledRef,
19
+ setAdvisorOutcomeLoggingRef,
20
+ setPersistedModelRefs,
21
+ } from "./state.ts";
17
22
  import type { AdvisorConfig } from "./types.ts";
18
23
  import { unknownConfigKeys, validateConfig } from "./validation.ts";
19
24
 
@@ -146,11 +151,13 @@ const configCache = new Map<
146
151
  >();
147
152
 
148
153
  const warnedUnknownConfigIdentities = new Set<string>();
154
+ const warnedFilterWithoutClassifierIdentities = new Set<string>();
149
155
 
150
156
  /** Drops the parsed-configuration cache; the next load re-reads from disk. */
151
157
  export const resetConfigCache = () => {
152
158
  configCache.clear();
153
159
  warnedUnknownConfigIdentities.clear();
160
+ warnedFilterWithoutClassifierIdentities.clear();
154
161
  };
155
162
 
156
163
  /**
@@ -198,12 +205,25 @@ export const loadConfig = (_ctx: ExtensionContext) => {
198
205
  if (globalConfig) {
199
206
  applyConfig(globalConfig);
200
207
 
208
+ const warningIdentity = `${global}:${configIdentity(global)}`;
209
+
210
+ if (
211
+ advisorJevFilterEnabledRef &&
212
+ !advisorJevClassifierRef &&
213
+ _ctx.hasUI &&
214
+ !warnedFilterWithoutClassifierIdentities.has(warningIdentity)
215
+ ) {
216
+ warnedFilterWithoutClassifierIdentities.add(warningIdentity);
217
+ _ctx.ui.notify(
218
+ "The Jev consultation filter is enabled but no Jev classifier is selected; consultations are not screened until you pick one in /advisor-settings.",
219
+ "warning"
220
+ );
221
+ }
222
+
201
223
  const unknownKeys = unknownConfigKeys(globalConfig).filter(
202
224
  (key) => !RESERVED_ADVISOR_JSON_KEYS.has(key)
203
225
  );
204
226
 
205
- const warningIdentity = `${global}:${configIdentity(global)}`;
206
-
207
227
  if (
208
228
  unknownKeys.length > 0 &&
209
229
  _ctx.hasUI &&
@@ -22,6 +22,7 @@ export {
22
22
 
23
23
  export const DEFAULT_ADVISOR_GIT_CONTEXT_MAX_CHARS = 20_000;
24
24
  export const DEFAULT_JEV_MODEL = "jev-latest";
25
+ export const JEV_API_KEY_CLASSIFIER = "jev-api-key";
25
26
 
26
27
  // Provisional: the latency benchmark was dropped from scope; revisit if live
27
28
  // measurements suggest a different bound.
@@ -90,6 +91,7 @@ export interface AdvisorConfig {
90
91
  advisorGitContextMaxChars?: number;
91
92
  advisorHerdrIntegration?: boolean;
92
93
  advisorJevBaseUrl?: string;
94
+ advisorJevClassifier?: string;
93
95
  advisorJevDigestMaxChars?: number;
94
96
  advisorJevFilterEnabled?: boolean;
95
97
  advisorJevFilterNoulMargin?: number;
@@ -0,0 +1,203 @@
1
+ import { chmodSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+
4
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
5
+
6
+ export interface JevClassifierSecrets {
7
+ delete: (options: {
8
+ name: string;
9
+ service: string;
10
+ }) => Promise<boolean | undefined>;
11
+ get: (options: {
12
+ name: string;
13
+ service: string;
14
+ }) => Promise<string | null | undefined>;
15
+ set: (options: {
16
+ name: string;
17
+ service: string;
18
+ value: string;
19
+ }) => Promise<void>;
20
+ }
21
+
22
+ export interface JevClassifierKeyStoreDeps {
23
+ readFile?: () => string | undefined;
24
+ secrets?: JevClassifierSecrets | null;
25
+ writeFile?: (key: string) => void;
26
+ }
27
+
28
+ export interface JevClassifierKeySnapshot {
29
+ /** The secure store could not be read; key presence is unknown. */
30
+ failed?: boolean;
31
+ key?: string;
32
+ source?: "bun-secrets" | "file";
33
+ }
34
+
35
+ export const JEV_CLASSIFIER_KEY_SERVICE = "pi-advisor-jev-classifier";
36
+ export const JEV_CLASSIFIER_KEY_NAME = "classifier-api-key";
37
+ const KEY_FILE_MODE = 0o600;
38
+ const keyFilePath = () => join(getAgentDir(), "jev_classifier_api_key");
39
+ let classifierKeyFailClosed = false;
40
+
41
+ export const disableJevClassifierKeyForRuntime = () => {
42
+ classifierKeyFailClosed = true;
43
+ };
44
+
45
+ export const enableJevClassifierKeyForRuntime = () => {
46
+ classifierKeyFailClosed = false;
47
+ };
48
+
49
+ interface BunGlobal {
50
+ Bun?: { secrets?: JevClassifierSecrets };
51
+ }
52
+
53
+ const runtimeSecrets = () => {
54
+ // SAFETY: Bun.secrets is only present on Bun runtimes; Node loaders use the 0600 file fallback.
55
+ const bun = globalThis as BunGlobal;
56
+
57
+ return bun.Bun?.secrets;
58
+ };
59
+
60
+ const defaultReadFile = () => {
61
+ try {
62
+ return readFileSync(keyFilePath(), "utf-8").trim() || undefined;
63
+ } catch {
64
+ return undefined;
65
+ }
66
+ };
67
+
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
+ };
73
+
74
+ export const readJevClassifierKey = async (
75
+ deps: JevClassifierKeyStoreDeps = {}
76
+ ): Promise<JevClassifierKeySnapshot> => {
77
+ const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
78
+
79
+ if (secrets) {
80
+ try {
81
+ const storedValue = await secrets.get({
82
+ name: JEV_CLASSIFIER_KEY_NAME,
83
+ service: JEV_CLASSIFIER_KEY_SERVICE,
84
+ });
85
+
86
+ const key = storedValue?.trim();
87
+
88
+ if (key) {
89
+ return { key, source: "bun-secrets" };
90
+ }
91
+ } catch {
92
+ return { failed: true };
93
+ }
94
+ }
95
+
96
+ const key = (deps.readFile ?? defaultReadFile)();
97
+
98
+ return key ? { key, source: "file" } : {};
99
+ };
100
+
101
+ export const writeJevClassifierKey = async (
102
+ key: string,
103
+ deps: JevClassifierKeyStoreDeps = {}
104
+ ): Promise<{ ok: boolean; message: string }> => {
105
+ const normalized = key.trim();
106
+
107
+ if (!normalized) {
108
+ return { message: "The key is empty.", ok: false };
109
+ }
110
+
111
+ const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
112
+
113
+ if (secrets) {
114
+ try {
115
+ await secrets.set({
116
+ name: JEV_CLASSIFIER_KEY_NAME,
117
+ service: JEV_CLASSIFIER_KEY_SERVICE,
118
+ value: normalized,
119
+ });
120
+
121
+ return { message: "Jev classifier key stored securely.", ok: true };
122
+ } catch {
123
+ return {
124
+ message: "Could not store the Jev classifier key securely.",
125
+ ok: false,
126
+ };
127
+ }
128
+ }
129
+
130
+ try {
131
+ (deps.writeFile ?? defaultWriteFile)(normalized);
132
+
133
+ return {
134
+ message: "Jev classifier key stored in a 0600-mode file.",
135
+ ok: true,
136
+ };
137
+ } catch {
138
+ return {
139
+ message: "Could not store the Jev classifier key securely.",
140
+ ok: false,
141
+ };
142
+ }
143
+ };
144
+
145
+ export const restoreJevClassifierKey = async (
146
+ previous: JevClassifierKeySnapshot,
147
+ deps: JevClassifierKeyStoreDeps = {}
148
+ ): Promise<boolean> => {
149
+ if (previous.failed) {
150
+ return false;
151
+ }
152
+
153
+ if (previous.key) {
154
+ const result = await writeJevClassifierKey(previous.key, deps);
155
+
156
+ return result.ok;
157
+ }
158
+
159
+ if (deps.secrets === null && deps.writeFile) {
160
+ return true;
161
+ }
162
+
163
+ const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
164
+ let secretCleared = true;
165
+
166
+ if (secrets) {
167
+ try {
168
+ await secrets.delete({
169
+ name: JEV_CLASSIFIER_KEY_NAME,
170
+ service: JEV_CLASSIFIER_KEY_SERVICE,
171
+ });
172
+ } catch {
173
+ secretCleared = false;
174
+ }
175
+ }
176
+
177
+ try {
178
+ if (deps.secrets === undefined && !secrets) {
179
+ rmSync(keyFilePath(), { force: true });
180
+ }
181
+ } catch {
182
+ return false;
183
+ }
184
+
185
+ return secretCleared;
186
+ };
187
+
188
+ export const consumeJevClassifierKey = async () => {
189
+ if (classifierKeyFailClosed) {
190
+ return undefined;
191
+ }
192
+
193
+ const stored = await readJevClassifierKey();
194
+
195
+ return stored.key;
196
+ };
197
+
198
+ export const writeJevClassifierKeyCandidate = async (key: string) => {
199
+ const previous = await readJevClassifierKey();
200
+ const written = await writeJevClassifierKey(key);
201
+
202
+ return { previous, written };
203
+ };