pi-advisor-flow 0.12.1-dev.1 → 0.12.1-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.
@@ -1,44 +1,55 @@
1
- import { Key, matchesKey } from "@earendil-works/pi-tui";
1
+ import { Input, Key, matchesKey } from "@earendil-works/pi-tui";
2
2
  import type { Component, Focusable } from "@earendil-works/pi-tui";
3
3
 
4
+ import { normalizeJevBaseUrl } from "../jev/base-url.ts";
4
5
  import type { JevCredentials, JevTransportKind } from "../jev/transport.ts";
5
- import { redactSecrets } from "../redaction.ts";
6
- import { renderJevSetup, setupActions } from "./jev-setup-submenu-view.ts";
7
6
  import {
8
- canClearCredential,
7
+ renderJevProvider,
8
+ setupActions,
9
+ } from "./jev-provider-submenu-view.ts";
10
+ import {
9
11
  canReplaceWithEnteredKey,
10
12
  consumeSetupPlaintextWarning,
11
13
  defaultSetupDeps,
12
14
  fireAndForget,
15
+ keyEntryPrompt,
13
16
  } from "./jev-setup-support.ts";
14
17
  import type {
15
- JevSetupSubmenuOptions,
16
- JevSetupViewState,
17
- SetupAction,
18
- SetupMode,
18
+ JevProviderMode,
19
+ JevProviderSubmenuOptions,
20
+ JevProviderViewState,
21
+ JevProviderAction,
19
22
  } from "./jev-setup-support.ts";
20
23
  import { MaskedInput } from "./masked-input.ts";
21
- import type { JevSetupDeps, JevSetupSelection } from "./types.ts";
24
+ import type { JevProviderSelection, JevSetupDeps } from "./types.ts";
22
25
 
23
- export class JevSetupSubmenu implements Component, Focusable {
24
- private readonly options: JevSetupSubmenuOptions;
26
+ // Justified exception to the ~300-line guidance: one cohesive modal state machine.
27
+ export class JevProviderSubmenu implements Component, Focusable {
28
+ private readonly options: JevProviderSubmenuOptions;
25
29
  private readonly deps: Required<JevSetupDeps>;
26
30
  private maskedInput: MaskedInput;
31
+ private readonly textInput = new Input();
27
32
  private credentials: JevCredentials | undefined;
28
33
  private selectedTransport: JevTransportKind | undefined;
29
- private mode: SetupMode = "menu";
34
+ private mode: JevProviderMode = "menu";
30
35
  private notice: string | undefined;
36
+ private inputError: string | undefined;
31
37
  private selectedIndex = 0;
32
- private filterEnabled: boolean;
38
+ private baseUrl: string | undefined;
39
+ private providerId: string | undefined;
33
40
  private canEnterKey = false;
34
41
  private pendingSelectionRefresh = false;
35
42
  private _focused = true;
36
43
 
37
- constructor(options: JevSetupSubmenuOptions, deps: JevSetupDeps = {}) {
44
+ constructor(options: JevProviderSubmenuOptions, deps: JevSetupDeps = {}) {
38
45
  this.options = options;
39
- this.filterEnabled = options.currentValue === "On";
40
46
  this.deps = { ...defaultSetupDeps, ...options.setupDeps, ...deps };
41
- this.maskedInput = this.createMaskedInput("Paste a TypeSafe API key");
47
+ this.baseUrl = options.currentBaseUrl;
48
+ this.providerId = options.currentKeyProvider;
49
+ this.maskedInput = this.createMaskedInput(keyEntryPrompt("typesafe"));
50
+ this.textInput.focused = true;
51
+ this.textInput.onSubmit = (value) => this.submitText(value);
52
+ this.textInput.onEscape = () => this.returnToMenu();
42
53
  void fireAndForget(this.refresh(), this.reportBackgroundError);
43
54
  }
44
55
 
@@ -49,10 +60,12 @@ export class JevSetupSubmenu implements Component, Focusable {
49
60
  set focused(value: boolean) {
50
61
  this._focused = value;
51
62
  this.maskedInput.focused = value;
63
+ this.textInput.focused = value;
52
64
  }
53
65
 
54
66
  invalidate(): void {
55
67
  this.maskedInput.invalidate();
68
+ this.textInput.invalidate();
56
69
  }
57
70
 
58
71
  handleInput(keyData: string): void {
@@ -61,6 +74,15 @@ export class JevSetupSubmenu implements Component, Focusable {
61
74
  this.options.tui.requestRender();
62
75
  return;
63
76
  }
77
+ if (this.mode === "base-url" || this.mode === "provider-id") {
78
+ const before = this.textInput.getValue();
79
+ this.textInput.handleInput(keyData);
80
+ if (this.inputError && this.textInput.getValue() !== before) {
81
+ this.inputError = undefined;
82
+ }
83
+ this.options.tui.requestRender();
84
+ return;
85
+ }
64
86
  if (this.mode !== "menu") {
65
87
  return;
66
88
  }
@@ -87,21 +109,20 @@ export class JevSetupSubmenu implements Component, Focusable {
87
109
  }
88
110
 
89
111
  render(width: number): string[] {
90
- return renderJevSetup(
91
- this.options,
92
- this.viewState(),
93
- this.maskedInput,
94
- width
95
- );
112
+ const input = this.mode === "key-entry" ? this.maskedInput : this.textInput;
113
+ return renderJevProvider(this.options, this.viewState(), input, width);
96
114
  }
97
115
 
98
- private viewState(): JevSetupViewState {
116
+ private viewState(): JevProviderViewState {
99
117
  return {
118
+ baseUrl: this.baseUrl,
100
119
  canEnterKey: this.canEnterKey,
120
+ canReuseProviderLogin: Boolean(this.baseUrl),
101
121
  credentials: this.credentials,
102
- filterEnabled: this.filterEnabled,
122
+ inputError: this.inputError,
103
123
  mode: this.mode,
104
124
  notice: this.notice,
125
+ providerId: this.providerId,
105
126
  selectedIndex: this.selectedIndex,
106
127
  selectedTransport: this.selectedTransport,
107
128
  };
@@ -109,7 +130,7 @@ export class JevSetupSubmenu implements Component, Focusable {
109
130
 
110
131
  private createMaskedInput(placeholder: string): MaskedInput {
111
132
  return new MaskedInput({
112
- onEscape: () => this.cancelKeyEntry(),
133
+ onEscape: () => this.returnToMenu(),
113
134
  onSubmit: (value) => this.submitEnteredKey(value),
114
135
  placeholder,
115
136
  });
@@ -126,6 +147,9 @@ export class JevSetupSubmenu implements Component, Focusable {
126
147
  );
127
148
  this.credentials = credentials;
128
149
  this.selectedTransport = credentials?.transport;
150
+ if (credentials?.transport === "typesafe-compatible") {
151
+ this.baseUrl = credentials.baseUrl ?? this.baseUrl;
152
+ }
129
153
  this.mode = "menu";
130
154
  this.selectedIndex = 0;
131
155
  if (credentials?.source === "advisor-json") {
@@ -139,28 +163,26 @@ export class JevSetupSubmenu implements Component, Focusable {
139
163
  }
140
164
  }
141
165
 
142
- private activate(action: SetupAction): void {
166
+ private reportBackgroundError = (message: string): void => {
167
+ this.mode = "menu";
168
+ this.notice = `Setup failed: ${message}`;
169
+ this.options.tui.requestRender();
170
+ };
171
+
172
+ private returnToMenu(): void {
173
+ this.mode = "menu";
174
+ this.inputError = undefined;
175
+ this.maskedInput.setValue("");
176
+ this.options.tui.requestRender();
177
+ }
178
+
179
+ private activate(action: JevProviderAction): void {
143
180
  if (action === "done") {
144
181
  this.closeSetup();
145
182
  return;
146
183
  }
147
- if (action === "disable") {
148
- this.notice = undefined;
149
- if (
150
- this.commitSelection(
151
- {
152
- enabled: false,
153
- transport: this.options.currentTransport,
154
- },
155
- "Off"
156
- )
157
- ) {
158
- this.filterEnabled = false;
159
- }
160
- return;
161
- }
162
184
  if (action === "clear-stored-key") {
163
- void fireAndForget(this.disableAndClear(), this.reportBackgroundError);
185
+ void fireAndForget(this.clearStoredKey(), this.reportBackgroundError);
164
186
  return;
165
187
  }
166
188
  if (action === "enter-key") {
@@ -169,14 +191,113 @@ export class JevSetupSubmenu implements Component, Focusable {
169
191
  }
170
192
  return;
171
193
  }
194
+ if (action === "reuse-provider-login") {
195
+ this.openProviderIdEntry();
196
+ return;
197
+ }
198
+ if (action === "typesafe-compatible") {
199
+ this.openBaseUrlEntry();
200
+ return;
201
+ }
202
+ if (this.mode !== "menu") {
203
+ return;
204
+ }
172
205
  void fireAndForget(this.selectProvider(action), this.reportBackgroundError);
173
206
  }
174
207
 
175
- private reportBackgroundError = (message: string): void => {
176
- this.mode = "menu";
177
- this.notice = `Setup failed: ${redactSecrets(message)}`;
208
+ private openBaseUrlEntry(): void {
209
+ this.mode = "base-url";
210
+ this.inputError = undefined;
211
+ this.notice = undefined;
212
+ this.textInput.setValue(this.baseUrl ?? "");
213
+ this.textInput.focused = this._focused;
178
214
  this.options.tui.requestRender();
179
- };
215
+ }
216
+
217
+ private openProviderIdEntry(): void {
218
+ this.mode = "provider-id";
219
+ this.inputError = undefined;
220
+ this.notice = undefined;
221
+ this.textInput.setValue(this.providerId ?? "");
222
+ this.textInput.focused = this._focused;
223
+ this.options.tui.requestRender();
224
+ }
225
+
226
+ private openKeyEntry(transport: JevTransportKind): void {
227
+ this.selectedTransport = transport;
228
+ this.mode = "key-entry";
229
+ this.inputError = undefined;
230
+ this.maskedInput = this.createMaskedInput(keyEntryPrompt(transport));
231
+ this.maskedInput.focused = this._focused;
232
+ this.options.tui.requestRender();
233
+ }
234
+
235
+ private submitText(value: string): void {
236
+ if (this.mode === "base-url") {
237
+ void fireAndForget(this.submitBaseUrl(value), this.reportBackgroundError);
238
+ return;
239
+ }
240
+ if (this.mode === "provider-id") {
241
+ void fireAndForget(
242
+ this.submitProviderId(value),
243
+ this.reportBackgroundError
244
+ );
245
+ }
246
+ }
247
+
248
+ private async submitBaseUrl(value: string): Promise<void> {
249
+ const { baseUrl, error } = normalizeJevBaseUrl(value);
250
+ if (!baseUrl) {
251
+ this.inputError = error;
252
+ this.options.tui.requestRender();
253
+ return;
254
+ }
255
+ this.baseUrl = baseUrl;
256
+ this.selectedTransport = "typesafe-compatible";
257
+ this.mode = "resolving";
258
+ this.inputError = undefined;
259
+ this.options.tui.requestRender();
260
+ const options = this.providerId
261
+ ? { baseUrl, keyProvider: this.providerId }
262
+ : { baseUrl };
263
+ const credentials = await this.deps.resolveEndpoint(options);
264
+ if (!credentials) {
265
+ this.openKeyEntry("typesafe-compatible");
266
+ return;
267
+ }
268
+ await this.verifyAndSave(credentials);
269
+ }
270
+
271
+ private async submitProviderId(value: string): Promise<void> {
272
+ const providerId = value.trim();
273
+ if (!providerId || /\s/u.test(providerId)) {
274
+ this.inputError =
275
+ "Enter a single Pi provider id, for example openrouter.";
276
+ this.options.tui.requestRender();
277
+ return;
278
+ }
279
+ if (!this.baseUrl) {
280
+ this.inputError = "Enter the Base URL first.";
281
+ this.options.tui.requestRender();
282
+ return;
283
+ }
284
+ this.providerId = providerId;
285
+ this.selectedTransport = "typesafe-compatible";
286
+ this.mode = "resolving";
287
+ this.inputError = undefined;
288
+ this.options.tui.requestRender();
289
+ const credentials = await this.deps.resolveEndpoint({
290
+ baseUrl: this.baseUrl,
291
+ keyProvider: providerId,
292
+ });
293
+ if (!credentials) {
294
+ this.mode = "menu";
295
+ this.notice = `No stored login for Pi provider "${providerId}". Add one in Pi and retry.`;
296
+ this.options.tui.requestRender();
297
+ return;
298
+ }
299
+ await this.verifyAndSave(credentials);
300
+ }
180
301
 
181
302
  private async selectProvider(transport: JevTransportKind): Promise<void> {
182
303
  this.selectedTransport = transport;
@@ -189,7 +310,7 @@ export class JevSetupSubmenu implements Component, Focusable {
189
310
  credentials = await this.deps.resolveTransport(transport);
190
311
  } catch (error) {
191
312
  this.mode = "menu";
192
- this.notice = `Credential lookup failed: ${redactSecrets(error instanceof Error ? error.message : String(error))}`;
313
+ this.notice = `Credential lookup failed: ${error instanceof Error ? error.message : String(error)}`;
193
314
  this.options.tui.requestRender();
194
315
  return;
195
316
  }
@@ -211,26 +332,7 @@ export class JevSetupSubmenu implements Component, Focusable {
211
332
  this.options.tui.requestRender();
212
333
  return;
213
334
  }
214
- await this.verifyAndEnable(credentials);
215
- }
216
-
217
- private openKeyEntry(transport: JevTransportKind): void {
218
- this.selectedTransport = transport;
219
- this.mode = "key-entry";
220
- const placeholder =
221
- transport === "openai-decisions"
222
- ? "Paste an OpenAI Platform API key"
223
- : "Paste a TypeSafe API key";
224
- this.maskedInput = this.createMaskedInput(placeholder);
225
- this.maskedInput.focused = this._focused;
226
- this.options.tui.requestRender();
227
- }
228
-
229
- private cancelKeyEntry(): void {
230
- this.mode = "menu";
231
- this.selectedTransport = undefined;
232
- this.maskedInput.setValue("");
233
- this.options.tui.requestRender();
335
+ await this.verifyAndSave(credentials);
234
336
  }
235
337
 
236
338
  private async submitEnteredKey(value: string): Promise<void> {
@@ -240,10 +342,14 @@ export class JevSetupSubmenu implements Component, Focusable {
240
342
  if (!transport || !key) {
241
343
  return;
242
344
  }
243
- await this.verifyAndEnable({ apiKey: key, transport }, key);
345
+ const credentials: JevCredentials = { apiKey: key, transport };
346
+ if (transport === "typesafe-compatible" && this.baseUrl) {
347
+ credentials.baseUrl = this.baseUrl;
348
+ }
349
+ await this.verifyAndSave(credentials, key);
244
350
  }
245
351
 
246
- private async verifyAndEnable(
352
+ private async verifyAndSave(
247
353
  credentials: JevCredentials,
248
354
  enteredKey?: string
249
355
  ): Promise<void> {
@@ -258,15 +364,9 @@ export class JevSetupSubmenu implements Component, Focusable {
258
364
  this.options.tui.requestRender();
259
365
  return;
260
366
  }
261
- const keyToStore =
262
- enteredKey ??
263
- (credentials.transport === "typesafe" &&
264
- credentials.source === "advisor-json"
265
- ? credentials.apiKey
266
- : undefined);
267
- if (keyToStore) {
367
+ if (enteredKey) {
268
368
  const stored = await this.deps.writeKey(
269
- keyToStore,
369
+ enteredKey,
270
370
  credentials.transport
271
371
  );
272
372
  if (!stored.ok) {
@@ -276,99 +376,69 @@ export class JevSetupSubmenu implements Component, Focusable {
276
376
  this.options.tui.requestRender();
277
377
  return;
278
378
  }
279
- if (
280
- credentials.transport === "typesafe" &&
281
- credentials.source === "advisor-json"
282
- ) {
283
- const removed = this.deps.removePlaintextKey();
284
- this.notice = removed.ok
285
- ? "Key moved from advisor.json into the secure store."
286
- : `Stored securely, but ${removed.message}`;
287
- } else {
288
- this.notice = `${stored.message} Verification succeeded.`;
379
+ this.notice = `${stored.message} Verification succeeded.`;
380
+ // An entered key is the more explicit choice, so it clears a reused login.
381
+ if (credentials.transport === "typesafe-compatible") {
382
+ this.providerId = undefined;
289
383
  }
290
384
  }
291
385
  this.credentials = credentials;
292
- const saved = this.commitSelection(
293
- { enabled: true, transport: credentials.transport },
294
- "On"
295
- );
296
- if (!saved) {
297
- this.mode = "menu";
298
- this.notice ??= "Provider verified, but settings could not be saved.";
299
- this.options.tui.requestRender();
386
+ if (this.commitSelection(credentials)) {
387
+ this.closeSetup(true);
300
388
  return;
301
389
  }
302
- this.filterEnabled = true;
303
390
  this.mode = "menu";
304
391
  this.canEnterKey = false;
392
+ this.notice = "Provider verified, but settings could not be saved.";
305
393
  this.options.tui.requestRender();
306
394
  }
307
395
 
308
- private commitSelection(
309
- selection: JevSetupSelection,
310
- legacyValue: string
311
- ): boolean {
312
- if (this.options.onSelection) {
313
- if (!this.options.onSelection(selection)) {
314
- return false;
315
- }
316
- this.closeSetup(true);
317
- return true;
396
+ private commitSelection(credentials: JevCredentials): boolean {
397
+ if (!this.options.onSelection) {
398
+ return false;
318
399
  }
319
- this.options.done(legacyValue);
320
- return true;
400
+ return this.options.onSelection(this.selectionFor(credentials));
321
401
  }
322
402
 
323
- private closeSetup(refreshSettings = false): void {
324
- this.options.done();
325
- if (refreshSettings || this.pendingSelectionRefresh) {
326
- this.options.afterSelection?.();
403
+ private selectionFor(credentials: JevCredentials): JevProviderSelection {
404
+ if (credentials.transport !== "typesafe-compatible") {
405
+ return { transport: credentials.transport };
327
406
  }
328
- this.pendingSelectionRefresh = false;
407
+ const selection: JevProviderSelection = {
408
+ transport: credentials.transport,
409
+ };
410
+ if (this.baseUrl) {
411
+ selection.baseUrl = this.baseUrl;
412
+ }
413
+ if (this.providerId) {
414
+ selection.keyProvider = this.providerId;
415
+ }
416
+ return selection;
329
417
  }
330
418
 
331
- private async disableAndClear(): Promise<void> {
419
+ private async clearStoredKey(): Promise<void> {
332
420
  const { credentials } = this;
333
- if (!(credentials && canClearCredential(credentials))) {
421
+ if (!(credentials && credentials.source)) {
334
422
  return;
335
423
  }
336
- if (this.options.onSelection) {
337
- const saved = this.options.onSelection({
338
- enabled: false,
339
- transport: this.options.currentTransport,
340
- });
341
- if (!saved) {
342
- this.mode = "menu";
343
- this.notice =
344
- "Could not save the disabled state; the key was not cleared.";
345
- this.options.tui.requestRender();
346
- return;
347
- }
348
- this.filterEnabled = false;
349
- this.pendingSelectionRefresh = true;
350
- }
351
424
  this.mode = "clearing";
352
425
  this.notice = undefined;
353
426
  this.options.tui.requestRender();
354
427
  const result = await this.deps.clearStoredKey(credentials.transport);
355
428
  this.mode = "menu";
356
429
  this.notice = result.message;
357
- if (!result.ok) {
358
- this.options.tui.requestRender();
359
- return;
360
- }
361
- this.credentials = undefined;
362
- if (this.options.onSelection) {
363
- this.closeSetup();
364
- } else if (
365
- this.commitSelection(
366
- { enabled: false, transport: this.options.currentTransport },
367
- "Off"
368
- )
369
- ) {
370
- this.filterEnabled = false;
430
+ if (result.ok) {
431
+ this.credentials = undefined;
432
+ this.pendingSelectionRefresh = true;
371
433
  }
372
434
  this.options.tui.requestRender();
373
435
  }
436
+
437
+ private closeSetup(refreshSettings = false): void {
438
+ this.options.done();
439
+ if (refreshSettings || this.pendingSelectionRefresh) {
440
+ this.options.afterSelection?.();
441
+ }
442
+ this.pendingSelectionRefresh = false;
443
+ }
374
444
  }