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/CHANGELOG.md +7 -0
- package/README.md +3 -3
- package/dist/index.js +827 -1828
- package/package.json +2 -2
- package/src/commands/settings-commands.ts +8 -10
- package/src/commands/settings-persistence.ts +2 -15
- package/src/config/defaults.ts +7 -0
- package/src/config/schema.ts +10 -0
- package/src/config/state.ts +9 -0
- package/src/config/storage.ts +23 -3
- package/src/config/types.ts +2 -0
- package/src/jev/classifier-key-store.ts +203 -0
- package/src/jev/classifier.ts +421 -0
- package/src/jev/ledger.ts +3 -3
- package/src/jev/questions.ts +61 -23
- package/src/tools/jev-filter.ts +54 -34
- package/src/tools/jev-turn-gate.ts +105 -97
- package/src/tools/register-ask-advisor.ts +19 -2
- package/src/ui/jev-classifier-submenu.ts +459 -0
- package/src/ui/jev-filter-submenu.ts +67 -53
- package/src/ui/jev-provider-submenu.ts +16 -1
- package/src/ui/jev-setup-support.ts +13 -1
- package/src/ui/settings-items.ts +39 -72
- package/src/ui/settings-mutations.ts +0 -13
- package/src/ui/settings-selector.ts +21 -17
- package/src/ui/types.ts +36 -2
- package/src/usage.ts +82 -42
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import type { ClassifierModel } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { Input, Key, matchesKey } from "@earendil-works/pi-tui";
|
|
4
|
+
import type {
|
|
5
|
+
Component,
|
|
6
|
+
Focusable,
|
|
7
|
+
KeybindingsManager,
|
|
8
|
+
} from "@earendil-works/pi-tui";
|
|
9
|
+
|
|
10
|
+
import { JEV_API_KEY_CLASSIFIER } from "../config/types.ts";
|
|
11
|
+
import {
|
|
12
|
+
disableJevClassifierKeyForRuntime,
|
|
13
|
+
enableJevClassifierKeyForRuntime,
|
|
14
|
+
readJevClassifierKey,
|
|
15
|
+
restoreJevClassifierKey,
|
|
16
|
+
writeJevClassifierKey,
|
|
17
|
+
} from "../jev/classifier-key-store.ts";
|
|
18
|
+
import type { JevClassifierKeySnapshot } from "../jev/classifier-key-store.ts";
|
|
19
|
+
import { classifyWithJev } from "../jev/classifier.ts";
|
|
20
|
+
import { nativeScreeningQuestions } from "../jev/questions.ts";
|
|
21
|
+
import { MaskedInput } from "./masked-input.ts";
|
|
22
|
+
import type {
|
|
23
|
+
JevClassifierRegistryAccess,
|
|
24
|
+
JevProviderSelection,
|
|
25
|
+
RenderRequester,
|
|
26
|
+
} from "./types.ts";
|
|
27
|
+
|
|
28
|
+
interface Options {
|
|
29
|
+
currentValue: string;
|
|
30
|
+
done: (value?: string) => void;
|
|
31
|
+
keybindings: KeybindingsManager;
|
|
32
|
+
modelRegistry?: JevClassifierRegistryAccess;
|
|
33
|
+
onSelection?: (selection: JevProviderSelection) => boolean;
|
|
34
|
+
resolveStoredKey?: () => Promise<JevClassifierKeySnapshot>;
|
|
35
|
+
persistKey?: (key: string) => Promise<{ ok: boolean; message: string }>;
|
|
36
|
+
restoreKey?: (previous: JevClassifierKeySnapshot) => Promise<boolean>;
|
|
37
|
+
theme: Theme;
|
|
38
|
+
tui: RenderRequester;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface Choice {
|
|
42
|
+
action: "classifier" | "use-key" | "replace-key";
|
|
43
|
+
label: string;
|
|
44
|
+
model?: ClassifierModel<string>;
|
|
45
|
+
ref: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class JevClassifierSubmenu implements Component, Focusable {
|
|
49
|
+
private readonly options: Options;
|
|
50
|
+
private readonly search = new Input();
|
|
51
|
+
private readonly loaded = Promise.withResolvers<boolean>();
|
|
52
|
+
private keyInput: MaskedInput | undefined;
|
|
53
|
+
private choices: Choice[] = [];
|
|
54
|
+
private filtered: Choice[] = [];
|
|
55
|
+
private selectedIndex = 0;
|
|
56
|
+
private mode: "loading" | "list" | "key" | "verifying" = "loading";
|
|
57
|
+
private notice: string | undefined;
|
|
58
|
+
private failClosedKeyMode = false;
|
|
59
|
+
private _focused = true;
|
|
60
|
+
|
|
61
|
+
constructor(options: Options) {
|
|
62
|
+
this.options = options;
|
|
63
|
+
this.search.focused = true;
|
|
64
|
+
void this.loadChoices();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get focused(): boolean {
|
|
68
|
+
return this._focused;
|
|
69
|
+
}
|
|
70
|
+
set focused(value: boolean) {
|
|
71
|
+
this._focused = value;
|
|
72
|
+
this.search.focused = value;
|
|
73
|
+
}
|
|
74
|
+
invalidate(): void {
|
|
75
|
+
this.search.invalidate();
|
|
76
|
+
}
|
|
77
|
+
render(width: number): string[] {
|
|
78
|
+
const { theme } = this.options;
|
|
79
|
+
const border = theme.fg("border", "─".repeat(Math.max(1, width)));
|
|
80
|
+
|
|
81
|
+
if (this.mode === "key") {
|
|
82
|
+
return [
|
|
83
|
+
border,
|
|
84
|
+
theme.fg("accent", theme.bold("Jev API Key")),
|
|
85
|
+
"",
|
|
86
|
+
this.notice ?? "Enter a new TypeSafe Jev API key:",
|
|
87
|
+
` ${this.keyInput?.render(Math.max(1, width - 4))[0] ?? ""}`,
|
|
88
|
+
"",
|
|
89
|
+
this.failClosedKeyMode
|
|
90
|
+
? "Key restore failed. Re-enter and verify a valid Jev key."
|
|
91
|
+
: "Enter to verify · Esc to cancel",
|
|
92
|
+
border,
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const query = this.search.getValue().trim().toLowerCase();
|
|
97
|
+
|
|
98
|
+
const filteredChoices = this.choices.filter((choice) =>
|
|
99
|
+
choice.label.toLowerCase().includes(query)
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
this.filtered = filteredChoices;
|
|
103
|
+
|
|
104
|
+
const rows = this.filtered.map(
|
|
105
|
+
(choice, index) =>
|
|
106
|
+
`${index === this.selectedIndex ? "›" : " "} ${choice.label}`
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return [
|
|
110
|
+
border,
|
|
111
|
+
theme.fg("accent", theme.bold("Jev classifier")),
|
|
112
|
+
"",
|
|
113
|
+
this.mode === "loading"
|
|
114
|
+
? "Loading Pi classifier catalog…"
|
|
115
|
+
: `Search: ${this.search.render(Math.max(1, width - 10))[0]}`,
|
|
116
|
+
...(this.notice ? [this.notice] : []),
|
|
117
|
+
...rows.slice(0, 10),
|
|
118
|
+
"",
|
|
119
|
+
"↑/↓ move · Enter select · Esc cancel",
|
|
120
|
+
border,
|
|
121
|
+
];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
handleInput(data: string): void {
|
|
125
|
+
if (this.mode === "key") {
|
|
126
|
+
this.keyInput?.handleInput(data);
|
|
127
|
+
|
|
128
|
+
this.options.tui.requestRender();
|
|
129
|
+
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (this.mode !== "list") {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (data === "\u001B[B" || matchesKey(data, Key.down)) {
|
|
138
|
+
if (this.selectedIndex + 1 < this.filtered.length) {
|
|
139
|
+
this.selectedIndex += 1;
|
|
140
|
+
}
|
|
141
|
+
} else if (data === "\u001B[A" || matchesKey(data, Key.up)) {
|
|
142
|
+
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
|
143
|
+
} else if (matchesKey(data, Key.escape) || data === "\u001B") {
|
|
144
|
+
this.options.done();
|
|
145
|
+
|
|
146
|
+
return;
|
|
147
|
+
} else if (matchesKey(data, Key.enter) || data === "\r") {
|
|
148
|
+
const selected = this.filtered[this.selectedIndex];
|
|
149
|
+
|
|
150
|
+
if (selected) {
|
|
151
|
+
void this.choose(selected);
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
this.search.handleInput(data);
|
|
155
|
+
this.selectedIndex = 0;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.options.tui.requestRender();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private async loadChoices() {
|
|
162
|
+
try {
|
|
163
|
+
if (!this.options.modelRegistry) {
|
|
164
|
+
throw new Error("Pi classifier registry is unavailable.");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const models =
|
|
168
|
+
await this.options.modelRegistry.getAvailableOfType("classifier");
|
|
169
|
+
|
|
170
|
+
this.choices = (models ?? []).map((model: ClassifierModel<string>) => ({
|
|
171
|
+
action: "classifier" as const,
|
|
172
|
+
label: `${this.options.modelRegistry?.getProviderDisplayName(model.provider) ?? model.provider} / ${model.id} (${JevClassifierSubmenu.rates(model)})`,
|
|
173
|
+
model,
|
|
174
|
+
ref: `${model.provider}/${model.id}`,
|
|
175
|
+
}));
|
|
176
|
+
|
|
177
|
+
const stored = await (
|
|
178
|
+
this.options.resolveStoredKey ?? readJevClassifierKey
|
|
179
|
+
)();
|
|
180
|
+
|
|
181
|
+
if (stored.key) {
|
|
182
|
+
this.choices.push(
|
|
183
|
+
{
|
|
184
|
+
action: "use-key",
|
|
185
|
+
label: "Jev API Key (use stored key)",
|
|
186
|
+
ref: JEV_API_KEY_CLASSIFIER,
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
action: "replace-key",
|
|
190
|
+
label: "Replace Jev API Key…",
|
|
191
|
+
ref: JEV_API_KEY_CLASSIFIER,
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
} else {
|
|
195
|
+
this.choices.push({
|
|
196
|
+
action: "replace-key",
|
|
197
|
+
label: "Jev API Key… (enter TypeSafe key)",
|
|
198
|
+
ref: JEV_API_KEY_CLASSIFIER,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this.mode = "list";
|
|
203
|
+
this.loaded.resolve(true);
|
|
204
|
+
this.notice =
|
|
205
|
+
this.options.currentValue &&
|
|
206
|
+
this.options.currentValue !== "Choose classifier"
|
|
207
|
+
? `Current: ${this.options.currentValue}`
|
|
208
|
+
: undefined;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
this.mode = "list";
|
|
211
|
+
this.loaded.resolve(false);
|
|
212
|
+
this.notice = `Could not load classifiers: ${error instanceof Error ? error.message : "unknown error"}. Retry /advisor-settings.`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
this.options.tui.requestRender();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
whenLoaded() {
|
|
219
|
+
return this.loaded.promise;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private static rates(model: ClassifierModel<string>) {
|
|
223
|
+
const rates = model.cost;
|
|
224
|
+
|
|
225
|
+
return rates.input === 0 && rates.output === 0
|
|
226
|
+
? "$0/M tokens"
|
|
227
|
+
: `$${rates.input}/$${rates.output} per Mtok`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private async choose(choice: Choice) {
|
|
231
|
+
if (choice.action === "use-key") {
|
|
232
|
+
const stored = await (
|
|
233
|
+
this.options.resolveStoredKey ?? readJevClassifierKey
|
|
234
|
+
)();
|
|
235
|
+
|
|
236
|
+
if (stored.key) {
|
|
237
|
+
await this.verifyStoredKey(stored.key);
|
|
238
|
+
} else {
|
|
239
|
+
this.openKeyEntry();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (choice.action === "replace-key") {
|
|
246
|
+
this.openKeyEntry();
|
|
247
|
+
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!choice.model) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
this.mode = "verifying";
|
|
256
|
+
this.notice = `Verifying ${choice.ref}…`;
|
|
257
|
+
this.options.tui.requestRender();
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
await this.verify(choice.model);
|
|
261
|
+
const saved = this.save({ classifier: choice.ref, transport: "auto" });
|
|
262
|
+
|
|
263
|
+
if (!saved) {
|
|
264
|
+
this.mode = "list";
|
|
265
|
+
this.notice = "Verified, but settings could not be saved.";
|
|
266
|
+
}
|
|
267
|
+
} catch (error) {
|
|
268
|
+
this.notice = `Verification failed for ${choice.ref}: ${error instanceof Error ? error.message : "provider error"}`;
|
|
269
|
+
this.mode = "list";
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
this.options.tui.requestRender();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private async verify(model: ClassifierModel<string>, apiKey?: string) {
|
|
276
|
+
if (!this.options.modelRegistry) {
|
|
277
|
+
throw new Error("Pi classifier registry is unavailable.");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const state = {
|
|
281
|
+
recent_conversation: "Classifier verification check.",
|
|
282
|
+
role: "executor",
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const result = await classifyWithJev(
|
|
286
|
+
{ modelRegistry: this.options.modelRegistry, signal: undefined },
|
|
287
|
+
`${model.provider}/${model.id}`,
|
|
288
|
+
state,
|
|
289
|
+
nativeScreeningQuestions,
|
|
290
|
+
{ apiKey }
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
if (!result.answers.self_answerable || !result.answers.stakes) {
|
|
294
|
+
throw new Error("Classifier returned an incomplete verification result.");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private async verifyKey() {
|
|
299
|
+
const candidate = this.keyInput?.getValue().trim() ?? "";
|
|
300
|
+
this.keyInput?.setValue("");
|
|
301
|
+
|
|
302
|
+
if (!candidate) {
|
|
303
|
+
this.notice = "Enter a non-empty Jev API key.";
|
|
304
|
+
this.options.tui.requestRender();
|
|
305
|
+
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const model = this.options.modelRegistry?.findOfType(
|
|
310
|
+
"classifier",
|
|
311
|
+
"typesafe",
|
|
312
|
+
"jev-latest"
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
if (!model) {
|
|
316
|
+
this.notice = "TypeSafe Jev classifier is not present in Pi's catalog.";
|
|
317
|
+
this.options.tui.requestRender();
|
|
318
|
+
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const previous = await (
|
|
323
|
+
this.options.resolveStoredKey ?? readJevClassifierKey
|
|
324
|
+
)();
|
|
325
|
+
|
|
326
|
+
if (previous.failed) {
|
|
327
|
+
this.mode = "list";
|
|
328
|
+
this.notice =
|
|
329
|
+
"Could not read the stored Jev API Key state; nothing was changed. Try again later.";
|
|
330
|
+
this.options.tui.requestRender();
|
|
331
|
+
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
this.mode = "verifying";
|
|
336
|
+
this.notice = "Verifying Jev API Key…";
|
|
337
|
+
this.options.tui.requestRender();
|
|
338
|
+
|
|
339
|
+
try {
|
|
340
|
+
await this.verify(model, candidate);
|
|
341
|
+
|
|
342
|
+
const stored = await (this.options.persistKey ?? writeJevClassifierKey)(
|
|
343
|
+
candidate
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
if (!stored.ok) {
|
|
347
|
+
throw new Error(stored.message);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
enableJevClassifierKeyForRuntime();
|
|
351
|
+
|
|
352
|
+
if (
|
|
353
|
+
!this.save({ classifier: JEV_API_KEY_CLASSIFIER, transport: "auto" })
|
|
354
|
+
) {
|
|
355
|
+
if (!(await this.restorePreviousKey(previous))) {
|
|
356
|
+
this.failClosedKeyMode = true;
|
|
357
|
+
disableJevClassifierKeyForRuntime();
|
|
358
|
+
throw new Error(
|
|
359
|
+
"Settings save failed and the previous key could not be restored. Re-enter and verify a valid Jev key before continuing."
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
throw new Error(
|
|
364
|
+
"Could not save Jev classifier settings; existing key was restored."
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
this.mode = "list";
|
|
369
|
+
this.notice = "Jev API Key verified and saved securely.";
|
|
370
|
+
} catch (error) {
|
|
371
|
+
this.mode = "key";
|
|
372
|
+
this.notice = `Verification failed: ${error instanceof Error ? error.message : "provider error"}`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
this.options.tui.requestRender();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private openKeyEntry() {
|
|
379
|
+
this.mode = "key";
|
|
380
|
+
this.keyInput = new MaskedInput({
|
|
381
|
+
onEscape: () => {
|
|
382
|
+
this.mode = "list";
|
|
383
|
+
this.keyInput?.setValue("");
|
|
384
|
+
this.options.tui.requestRender();
|
|
385
|
+
},
|
|
386
|
+
onSubmit: () => this.handleKeySubmit(),
|
|
387
|
+
placeholder: "TypeSafe Jev API key",
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private restorePreviousKey(
|
|
392
|
+
previous: JevClassifierKeySnapshot
|
|
393
|
+
): Promise<boolean> {
|
|
394
|
+
if (this.options.restoreKey) {
|
|
395
|
+
return this.options.restoreKey(previous);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return restoreJevClassifierKey(previous);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private async verifyStoredKey(key: string) {
|
|
402
|
+
const model = this.options.modelRegistry?.findOfType(
|
|
403
|
+
"classifier",
|
|
404
|
+
"typesafe",
|
|
405
|
+
"jev-latest"
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
if (!model) {
|
|
409
|
+
this.notice = "TypeSafe Jev classifier is not present in Pi's catalog.";
|
|
410
|
+
this.options.tui.requestRender();
|
|
411
|
+
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
this.mode = "verifying";
|
|
416
|
+
this.notice = "Verifying stored Jev API Key…";
|
|
417
|
+
this.options.tui.requestRender();
|
|
418
|
+
|
|
419
|
+
try {
|
|
420
|
+
await this.verify(model, key);
|
|
421
|
+
|
|
422
|
+
if (
|
|
423
|
+
!this.save({ classifier: JEV_API_KEY_CLASSIFIER, transport: "auto" })
|
|
424
|
+
) {
|
|
425
|
+
throw new Error("Could not save the Jev classifier selection.");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
this.notice = "Stored Jev API Key verified.";
|
|
429
|
+
} catch (error) {
|
|
430
|
+
this.mode = "list";
|
|
431
|
+
this.notice = `Verification failed: ${error instanceof Error ? error.message : "provider error"}`;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
this.options.tui.requestRender();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
private async handleKeySubmit() {
|
|
438
|
+
try {
|
|
439
|
+
await this.verifyKey();
|
|
440
|
+
} catch (error) {
|
|
441
|
+
this.notice = `Verification failed: ${error instanceof Error ? error.message : "provider error"}`;
|
|
442
|
+
this.mode = "key";
|
|
443
|
+
this.options.tui.requestRender();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private save(selection: JevProviderSelection) {
|
|
448
|
+
const saved = this.options.onSelection?.(selection) ?? false;
|
|
449
|
+
|
|
450
|
+
if (saved) {
|
|
451
|
+
this.options.done(selection.classifier);
|
|
452
|
+
} else {
|
|
453
|
+
this.mode = "list";
|
|
454
|
+
this.notice = "Verified, but settings could not be saved.";
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return saved;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
@@ -1,22 +1,25 @@
|
|
|
1
|
+
import type { ClassifierModel } from "@earendil-works/pi-ai";
|
|
1
2
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
3
|
import { Key, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
4
|
import type { Component, Focusable } from "@earendil-works/pi-tui";
|
|
4
5
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
6
|
+
import { JEV_API_KEY_CLASSIFIER } from "../config/types.ts";
|
|
7
|
+
import { readJevClassifierKey } from "../jev/classifier-key-store.ts";
|
|
8
|
+
import type { JevClassifierKeySnapshot } from "../jev/classifier-key-store.ts";
|
|
9
|
+
import { parseJevClassifierRef } from "../jev/classifier.ts";
|
|
8
10
|
import type {
|
|
11
|
+
JevClassifierRegistryAccess,
|
|
9
12
|
JevFilterSelection,
|
|
10
|
-
JevSetupDeps,
|
|
11
13
|
RenderRequester,
|
|
12
14
|
} from "./types.ts";
|
|
13
15
|
|
|
14
16
|
export interface JevFilterSubmenuOptions {
|
|
15
|
-
|
|
17
|
+
currentClassifier?: string;
|
|
16
18
|
currentValue: string;
|
|
17
19
|
done: (selectedValue?: string) => void;
|
|
18
20
|
onSelection?: (selection: JevFilterSelection) => boolean;
|
|
19
|
-
|
|
21
|
+
modelRegistry?: JevClassifierRegistryAccess;
|
|
22
|
+
resolveClassifierKey?: () => Promise<JevClassifierKeySnapshot>;
|
|
20
23
|
theme: Theme;
|
|
21
24
|
tui: RenderRequester;
|
|
22
25
|
}
|
|
@@ -26,7 +29,7 @@ type FilterAction = "disable" | "done" | "enable";
|
|
|
26
29
|
const actionsFor = (enabled: boolean): FilterAction[] =>
|
|
27
30
|
enabled ? ["disable", "done"] : ["enable", "done"];
|
|
28
31
|
|
|
29
|
-
const labelFor = (action: FilterAction)
|
|
32
|
+
const labelFor = (action: FilterAction) => {
|
|
30
33
|
if (action === "enable") {
|
|
31
34
|
return "Enable the filter";
|
|
32
35
|
}
|
|
@@ -34,32 +37,33 @@ const labelFor = (action: FilterAction): string => {
|
|
|
34
37
|
return action === "disable" ? "Disable the filter" : "Done";
|
|
35
38
|
};
|
|
36
39
|
|
|
37
|
-
/** Owns only the on/off decision plus its credential gate. */
|
|
38
40
|
export class JevFilterSubmenu implements Component, Focusable {
|
|
39
41
|
private readonly options: JevFilterSubmenuOptions;
|
|
40
|
-
private readonly deps: Required<JevSetupDeps>;
|
|
41
|
-
private credentials: JevCredentials | undefined;
|
|
42
42
|
private mode: "menu" | "resolving" = "resolving";
|
|
43
43
|
private notice: string | undefined;
|
|
44
44
|
private selectedIndex = 0;
|
|
45
45
|
private enabled: boolean;
|
|
46
|
+
private classifierAvailable = false;
|
|
47
|
+
private readonly modelRegistry: JevClassifierRegistryAccess | undefined;
|
|
48
|
+
private readonly resolveClassifierKey:
|
|
49
|
+
| (() => Promise<JevClassifierKeySnapshot>)
|
|
50
|
+
| undefined;
|
|
46
51
|
private _focused = true;
|
|
47
52
|
|
|
48
|
-
constructor(options: JevFilterSubmenuOptions
|
|
53
|
+
constructor(options: JevFilterSubmenuOptions) {
|
|
49
54
|
this.options = options;
|
|
50
|
-
this.
|
|
51
|
-
this.
|
|
52
|
-
|
|
55
|
+
this.modelRegistry = options.modelRegistry;
|
|
56
|
+
this.resolveClassifierKey = options.resolveClassifierKey;
|
|
57
|
+
this.enabled = options.currentValue.startsWith("On");
|
|
58
|
+
void this.refresh();
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
get focused()
|
|
61
|
+
get focused() {
|
|
56
62
|
return this._focused;
|
|
57
63
|
}
|
|
58
|
-
|
|
59
64
|
set focused(value: boolean) {
|
|
60
65
|
this._focused = value;
|
|
61
66
|
}
|
|
62
|
-
|
|
63
67
|
// oxlint-disable-next-line class-methods-use-this no-empty-function
|
|
64
68
|
invalidate(): void {}
|
|
65
69
|
|
|
@@ -67,14 +71,14 @@ export class JevFilterSubmenu implements Component, Focusable {
|
|
|
67
71
|
const { theme } = this.options;
|
|
68
72
|
|
|
69
73
|
const lines = [
|
|
70
|
-
theme.fg("accent", theme.bold(" Jev
|
|
74
|
+
theme.fg("accent", theme.bold(" Jev classifier consultation filter")),
|
|
71
75
|
"",
|
|
72
76
|
` Filter: ${this.enabled ? "On" : "Off"}`,
|
|
73
|
-
`
|
|
77
|
+
` Classifier: ${this.options.currentClassifier ?? "not selected"}`,
|
|
74
78
|
];
|
|
75
79
|
|
|
76
80
|
if (this.mode === "resolving") {
|
|
77
|
-
lines.push("", " Checking
|
|
81
|
+
lines.push("", " Checking selected classifier availability…");
|
|
78
82
|
} else if (this.notice) {
|
|
79
83
|
lines.push("", theme.fg("warning", ` ${this.notice}`));
|
|
80
84
|
}
|
|
@@ -83,8 +87,9 @@ export class JevFilterSubmenu implements Component, Focusable {
|
|
|
83
87
|
lines.push("");
|
|
84
88
|
|
|
85
89
|
for (const [index, action] of actionsFor(this.enabled).entries()) {
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
lines.push(
|
|
91
|
+
`${index === this.selectedIndex ? "→ " : " "}${labelFor(action)}`
|
|
92
|
+
);
|
|
88
93
|
}
|
|
89
94
|
}
|
|
90
95
|
|
|
@@ -120,33 +125,53 @@ export class JevFilterSubmenu implements Component, Focusable {
|
|
|
120
125
|
this.options.tui.requestRender();
|
|
121
126
|
}
|
|
122
127
|
|
|
123
|
-
private async refresh()
|
|
124
|
-
const
|
|
128
|
+
private async refresh() {
|
|
129
|
+
const selected = this.options.currentClassifier;
|
|
125
130
|
|
|
126
131
|
try {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
132
|
+
if (selected === JEV_API_KEY_CLASSIFIER) {
|
|
133
|
+
const stored = await (
|
|
134
|
+
this.resolveClassifierKey ?? readJevClassifierKey
|
|
135
|
+
)();
|
|
136
|
+
|
|
137
|
+
this.classifierAvailable = Boolean(stored.key);
|
|
138
|
+
|
|
139
|
+
if (stored.failed) {
|
|
140
|
+
this.notice =
|
|
141
|
+
"Could not read the stored Jev API Key state; try again later.";
|
|
142
|
+
} else if (!this.classifierAvailable) {
|
|
143
|
+
this.notice =
|
|
144
|
+
"No Jev API Key is stored. Choose Jev API Key in Jev classifier settings.";
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
const parsed = selected ? parseJevClassifierRef(selected) : undefined;
|
|
148
|
+
const registry = this.modelRegistry;
|
|
149
|
+
|
|
150
|
+
if (parsed && registry) {
|
|
151
|
+
const available = await registry.getAvailableOfType(
|
|
152
|
+
"classifier",
|
|
153
|
+
parsed.provider
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
this.classifierAvailable = available.some(
|
|
157
|
+
(model: ClassifierModel<string>) => model.id === parsed.modelId
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!this.classifierAvailable) {
|
|
162
|
+
this.notice =
|
|
163
|
+
"Select an available authenticated classifier in Jev classifier settings first.";
|
|
164
|
+
}
|
|
165
|
+
}
|
|
130
166
|
} catch (error) {
|
|
131
|
-
this.
|
|
132
|
-
this.notice = `Credential lookup failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!this.credentials && !this.notice) {
|
|
136
|
-
this.notice = `No Jev provider resolves for "${transport}". Open Jev provider to configure one.`;
|
|
167
|
+
this.notice = `Classifier lookup failed: ${error instanceof Error ? error.message : "Pi error"}`;
|
|
137
168
|
}
|
|
138
169
|
|
|
139
170
|
this.mode = "menu";
|
|
140
171
|
this.options.tui.requestRender();
|
|
141
172
|
}
|
|
142
173
|
|
|
143
|
-
private
|
|
144
|
-
this.mode = "menu";
|
|
145
|
-
this.notice = `Setup failed: ${message}`;
|
|
146
|
-
this.options.tui.requestRender();
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
private activate(action: FilterAction): void {
|
|
174
|
+
private activate(action: FilterAction) {
|
|
150
175
|
if (action === "done") {
|
|
151
176
|
this.options.done();
|
|
152
177
|
|
|
@@ -155,16 +180,13 @@ export class JevFilterSubmenu implements Component, Focusable {
|
|
|
155
180
|
|
|
156
181
|
const enabled = action === "enable";
|
|
157
182
|
|
|
158
|
-
if (enabled && !this.
|
|
159
|
-
// refresh() already explains why nothing resolved.
|
|
183
|
+
if (enabled && !this.classifierAvailable) {
|
|
160
184
|
this.options.tui.requestRender();
|
|
161
185
|
|
|
162
186
|
return;
|
|
163
187
|
}
|
|
164
188
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (!saved) {
|
|
189
|
+
if (this.options.onSelection?.({ enabled }) === false) {
|
|
168
190
|
this.notice = "The filter state could not be saved.";
|
|
169
191
|
this.options.tui.requestRender();
|
|
170
192
|
|
|
@@ -175,12 +197,4 @@ export class JevFilterSubmenu implements Component, Focusable {
|
|
|
175
197
|
this.notice = undefined;
|
|
176
198
|
this.options.done(enabled ? "On" : "Off");
|
|
177
199
|
}
|
|
178
|
-
|
|
179
|
-
private commit(selection: JevFilterSelection): boolean {
|
|
180
|
-
if (!this.options.onSelection) {
|
|
181
|
-
return true;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return this.options.onSelection(selection);
|
|
185
|
-
}
|
|
186
200
|
}
|
|
@@ -26,7 +26,22 @@ import type { JevProviderSelection, JevSetupDeps } from "./types.ts";
|
|
|
26
26
|
// Justified exception to the ~300-line guidance: one cohesive modal state machine.
|
|
27
27
|
export class JevProviderSubmenu implements Component, Focusable {
|
|
28
28
|
private readonly options: JevProviderSubmenuOptions;
|
|
29
|
-
private readonly deps: Required<
|
|
29
|
+
private readonly deps: Required<
|
|
30
|
+
Omit<
|
|
31
|
+
JevSetupDeps,
|
|
32
|
+
| "modelRegistry"
|
|
33
|
+
| "resolveClassifierKey"
|
|
34
|
+
| "persistClassifierKey"
|
|
35
|
+
| "restoreClassifierKey"
|
|
36
|
+
>
|
|
37
|
+
> &
|
|
38
|
+
Pick<
|
|
39
|
+
JevSetupDeps,
|
|
40
|
+
| "modelRegistry"
|
|
41
|
+
| "resolveClassifierKey"
|
|
42
|
+
| "persistClassifierKey"
|
|
43
|
+
| "restoreClassifierKey"
|
|
44
|
+
>;
|
|
30
45
|
private maskedInput: MaskedInput;
|
|
31
46
|
private readonly textInput = new Input();
|
|
32
47
|
private credentials: JevCredentials | undefined;
|