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.
@@ -0,0 +1,177 @@
1
+ import {
2
+ chmodSync,
3
+ existsSync,
4
+ readFileSync,
5
+ rmSync,
6
+ writeFileSync,
7
+ } from "node:fs";
8
+ import { join } from "node:path";
9
+
10
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
11
+
12
+ import { redactSecrets } from "../redaction.ts";
13
+ import type { JevKeyStoreResult, JevSecretsLike } from "./key-store.ts";
14
+
15
+ export const JEV_ENDPOINT_KEY_ENV_VAR = "JEV_API_KEY";
16
+ const JEV_ENDPOINT_KEY_SERVICE = "pi-advisor";
17
+ const JEV_ENDPOINT_KEY_NAME = "jev-api-key";
18
+
19
+ const KEY_FILE_MODE = 0o600;
20
+
21
+ type JevEndpointKeySource = "jev-bun-secrets" | "jev-env" | "jev-file";
22
+
23
+ export interface JevEndpointKeyResolution {
24
+ key?: string;
25
+ source?: JevEndpointKeySource;
26
+ }
27
+
28
+ export interface JevEndpointKeyStoreDeps {
29
+ deleteFileStore?: () => void;
30
+ env?: NodeJS.ProcessEnv;
31
+ readFileStore?: () => string | undefined;
32
+ secrets?: JevSecretsLike | null;
33
+ writeFileStore?: (key: string) => void;
34
+ }
35
+
36
+ interface BunGlobal {
37
+ Bun?: { secrets?: JevSecretsLike };
38
+ }
39
+
40
+ const runtimeSecrets = (): JevSecretsLike | undefined => {
41
+ // SAFETY: only Bun runtimes expose Bun.secrets; other hosts leave it absent.
42
+ const bun = globalThis as BunGlobal;
43
+ return bun.Bun?.secrets;
44
+ };
45
+
46
+ const keyFilePath = () => join(getAgentDir(), "jev_api_key");
47
+
48
+ const normalizeKey = (value: string | null | undefined) =>
49
+ value?.trim() || undefined;
50
+
51
+ const readDefaultFileStore = (): string | undefined => {
52
+ try {
53
+ return normalizeKey(readFileSync(keyFilePath(), "utf-8"));
54
+ } catch {
55
+ return undefined;
56
+ }
57
+ };
58
+
59
+ const writeDefaultFileStore = (key: string) => {
60
+ const path = keyFilePath();
61
+ writeFileSync(path, `${key}\n`, { mode: KEY_FILE_MODE });
62
+ chmodSync(path, KEY_FILE_MODE);
63
+ };
64
+
65
+ const deleteDefaultFileStore = () => rmSync(keyFilePath(), { force: true });
66
+
67
+ const messageOf = <Error>(error: Error) =>
68
+ redactSecrets(error instanceof Error ? error.message : String(error));
69
+
70
+ /** Resolves the endpoint key: Bun.secrets → JEV_API_KEY → the 0600 key file. */
71
+ export const resolveJevEndpointKey = async (
72
+ deps: JevEndpointKeyStoreDeps = {}
73
+ ): Promise<JevEndpointKeyResolution> => {
74
+ const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
75
+ if (secrets) {
76
+ try {
77
+ const stored = normalizeKey(
78
+ await secrets.get({
79
+ name: JEV_ENDPOINT_KEY_NAME,
80
+ service: JEV_ENDPOINT_KEY_SERVICE,
81
+ })
82
+ );
83
+ if (stored) {
84
+ return { key: stored, source: "jev-bun-secrets" };
85
+ }
86
+ } catch {
87
+ // An unavailable secret store falls through to the next source.
88
+ }
89
+ }
90
+ const env = deps.env ?? process.env;
91
+ const fromEnv = normalizeKey(env[JEV_ENDPOINT_KEY_ENV_VAR]);
92
+ if (fromEnv) {
93
+ return { key: fromEnv, source: "jev-env" };
94
+ }
95
+ const fromFile = normalizeKey((deps.readFileStore ?? readDefaultFileStore)());
96
+ return fromFile ? { key: fromFile, source: "jev-file" } : {};
97
+ };
98
+
99
+ export const writeJevEndpointKey = async (
100
+ key: string,
101
+ deps: JevEndpointKeyStoreDeps = {}
102
+ ): Promise<JevKeyStoreResult> => {
103
+ const normalized = normalizeKey(key);
104
+ if (!normalized) {
105
+ return { message: "The endpoint API key is empty.", ok: false };
106
+ }
107
+ const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
108
+ if (secrets) {
109
+ try {
110
+ await secrets.set({
111
+ name: JEV_ENDPOINT_KEY_NAME,
112
+ service: JEV_ENDPOINT_KEY_SERVICE,
113
+ value: normalized,
114
+ });
115
+ return {
116
+ message: "Endpoint API key stored in Bun.secrets.",
117
+ ok: true,
118
+ };
119
+ } catch (error) {
120
+ return {
121
+ message: `Storing the endpoint API key in Bun.secrets failed: ${messageOf(error)}. Alternatively set ${JEV_ENDPOINT_KEY_ENV_VAR} in your shell profile.`,
122
+ ok: false,
123
+ };
124
+ }
125
+ }
126
+ try {
127
+ (deps.writeFileStore ?? writeDefaultFileStore)(normalized);
128
+ return {
129
+ message: "Endpoint API key stored in the Pi agent directory (mode 0600).",
130
+ ok: true,
131
+ };
132
+ } catch (error) {
133
+ return {
134
+ message: `Storing the endpoint API key failed: ${messageOf(error)}. Alternatively set ${JEV_ENDPOINT_KEY_ENV_VAR} in your shell profile.`,
135
+ ok: false,
136
+ };
137
+ }
138
+ };
139
+
140
+ export const clearJevEndpointKey = async (
141
+ deps: JevEndpointKeyStoreDeps = {}
142
+ ): Promise<JevKeyStoreResult> => {
143
+ let clearedSomething = false;
144
+ let firstError: string | undefined;
145
+ const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
146
+ if (secrets) {
147
+ try {
148
+ await secrets.delete({
149
+ name: JEV_ENDPOINT_KEY_NAME,
150
+ service: JEV_ENDPOINT_KEY_SERVICE,
151
+ });
152
+ clearedSomething = true;
153
+ } catch (error) {
154
+ firstError = messageOf(error);
155
+ }
156
+ }
157
+ try {
158
+ if (deps.deleteFileStore) {
159
+ deps.deleteFileStore();
160
+ } else if (existsSync(keyFilePath())) {
161
+ deleteDefaultFileStore();
162
+ }
163
+ clearedSomething = true;
164
+ } catch (error) {
165
+ firstError ??= messageOf(error);
166
+ }
167
+ if (firstError) {
168
+ return {
169
+ message: `Clearing the stored endpoint API key failed: ${firstError}.`,
170
+ ok: false,
171
+ };
172
+ }
173
+ return {
174
+ message: `Stored endpoint API key cleared.${clearedSomething ? "" : ` Nothing was stored; unset ${JEV_ENDPOINT_KEY_ENV_VAR} yourself if you use it.`}`,
175
+ ok: true,
176
+ };
177
+ };
@@ -1,26 +1,45 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
- import { advisorJevTransportRef } from "../config/state.ts";
3
+ import {
4
+ advisorJevBaseUrlRef,
5
+ advisorJevKeyProviderRef,
6
+ advisorJevTransportRef,
7
+ } from "../config/state.ts";
8
+ import { normalizeJevBaseUrl } from "./base-url.ts";
9
+ import { resolveJevEndpointKey } from "./endpoint-key-store.ts";
10
+ import type { JevEndpointKeyResolution } from "./endpoint-key-store.ts";
4
11
  import { resolveTypeSafeKey } from "./key-store.ts";
5
12
  import type { JevKeySource } from "./key-store.ts";
6
13
  import { resolveOpenAIDecisionsKey } from "./openai-key-store.ts";
7
14
  import type { OpenAIDecisionsKeyResolution } from "./openai-key-store.ts";
8
15
 
9
- export type JevTransportKind = "typesafe" | "openrouter" | "openai-decisions";
16
+ export type JevTransportKind =
17
+ | "typesafe"
18
+ | "openrouter"
19
+ | "openai-decisions"
20
+ | "typesafe-compatible";
10
21
 
11
22
  export type JevCredentialSource =
12
23
  | JevKeySource
13
24
  | "openai-env"
14
25
  | "openai-provider-credential"
15
26
  | "openai-bun-secrets"
16
- | "openai-file";
27
+ | "openai-file"
28
+ | JevEndpointKeyResolution["source"]
29
+ | "provider-credential";
17
30
 
18
31
  export interface JevCredentials {
19
32
  apiKey: string;
33
+ baseUrl?: string;
20
34
  source?: JevCredentialSource;
21
35
  transport: JevTransportKind;
22
36
  }
23
37
 
38
+ export interface JevEndpointTarget {
39
+ baseUrl: string;
40
+ keyProvider?: string;
41
+ }
42
+
24
43
  export interface JevProviderAuthResult {
25
44
  auth: { apiKey?: string };
26
45
  source?: string;
@@ -35,6 +54,8 @@ export interface JevTransportDeps {
35
54
  ) => Promise<JevProviderAuthResult | undefined>;
36
55
  /** Resolves the extension-owned OpenAI Decisions key; injectable for tests. */
37
56
  resolveOpenAIKey?: () => Promise<OpenAIDecisionsKeyResolution>;
57
+ /** Resolves the extension-owned endpoint key; injectable for tests. */
58
+ resolveEndpointKey?: () => Promise<JevEndpointKeyResolution>;
38
59
  /** Resolves the TypeSafe key chain; injectable for tests. */
39
60
  resolveTypesafe?: () => ReturnType<typeof resolveTypeSafeKey>;
40
61
  }
@@ -112,6 +133,67 @@ const typeSafeCredentials = async (
112
133
  return credentials;
113
134
  };
114
135
 
136
+ export const resolveTypesafeCompatibleCredentials = async (
137
+ target: JevEndpointTarget,
138
+ ctx?: ExtensionContext,
139
+ deps: JevTransportDeps = {}
140
+ ): Promise<JevCredentials | undefined> => {
141
+ const { baseUrl } = target;
142
+ const provider = target.keyProvider;
143
+ if (provider) {
144
+ let providerKey: string | undefined;
145
+ try {
146
+ providerKey = deps.getProviderKey
147
+ ? await deps.getProviderKey(provider)
148
+ : await ctx?.modelRegistry?.getApiKeyForProvider(provider);
149
+ } catch {
150
+ providerKey = undefined;
151
+ }
152
+ const trimmed = providerKey?.trim();
153
+ if (!trimmed) {
154
+ // A declared Pi login wins outright; a stale store key must never stand in.
155
+ return undefined;
156
+ }
157
+ return {
158
+ apiKey: trimmed,
159
+ baseUrl,
160
+ source: "provider-credential",
161
+ transport: "typesafe-compatible",
162
+ };
163
+ }
164
+ const resolution = await (deps.resolveEndpointKey ?? resolveJevEndpointKey)();
165
+ if (!resolution.key) {
166
+ return undefined;
167
+ }
168
+ const credentials: JevCredentials = {
169
+ apiKey: resolution.key,
170
+ baseUrl,
171
+ transport: "typesafe-compatible",
172
+ };
173
+ if (resolution.source) {
174
+ credentials.source = resolution.source;
175
+ }
176
+ return credentials;
177
+ };
178
+
179
+ const typeSafeCompatibleCredentials = (
180
+ ctx: ExtensionContext | undefined,
181
+ deps: JevTransportDeps
182
+ ): Promise<JevCredentials | undefined> => {
183
+ const configured = advisorJevBaseUrlRef;
184
+ if (!configured) {
185
+ return Promise.resolve(undefined);
186
+ }
187
+ const { baseUrl } = normalizeJevBaseUrl(configured);
188
+ if (!baseUrl) {
189
+ return Promise.resolve(undefined);
190
+ }
191
+ const options = advisorJevKeyProviderRef
192
+ ? { baseUrl, keyProvider: advisorJevKeyProviderRef }
193
+ : { baseUrl };
194
+ return resolveTypesafeCompatibleCredentials(options, ctx, deps);
195
+ };
196
+
115
197
  export const resolveJevTransportFor = async (
116
198
  transport: JevTransportKind,
117
199
  ctx?: ExtensionContext,
@@ -120,6 +202,9 @@ export const resolveJevTransportFor = async (
120
202
  if (transport === "typesafe") {
121
203
  return typeSafeCredentials(deps);
122
204
  }
205
+ if (transport === "typesafe-compatible") {
206
+ return typeSafeCompatibleCredentials(ctx, deps);
207
+ }
123
208
  if (transport === "openrouter") {
124
209
  const key = await openRouterKey(ctx, deps);
125
210
  return key ? { apiKey: key, transport } : undefined;
@@ -0,0 +1,165 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { Key, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
3
+ import type { Component, Focusable } from "@earendil-works/pi-tui";
4
+
5
+ import type { JevTransport } from "../config/types.ts";
6
+ import type { JevCredentials } from "../jev/transport.ts";
7
+ import { defaultSetupDeps, fireAndForget } from "./jev-setup-support.ts";
8
+ import type {
9
+ JevFilterSelection,
10
+ JevSetupDeps,
11
+ RenderRequester,
12
+ } from "./types.ts";
13
+
14
+ export interface JevFilterSubmenuOptions {
15
+ currentTransport: JevTransport;
16
+ currentValue: string;
17
+ done: (selectedValue?: string) => void;
18
+ onSelection?: (selection: JevFilterSelection) => boolean;
19
+ setupDeps?: JevSetupDeps;
20
+ theme: Theme;
21
+ tui: RenderRequester;
22
+ }
23
+
24
+ type FilterAction = "disable" | "done" | "enable";
25
+
26
+ const actionsFor = (enabled: boolean): FilterAction[] =>
27
+ enabled ? ["disable", "done"] : ["enable", "done"];
28
+
29
+ const labelFor = (action: FilterAction): string => {
30
+ if (action === "enable") {
31
+ return "Enable the filter";
32
+ }
33
+ return action === "disable" ? "Disable the filter" : "Done";
34
+ };
35
+
36
+ /** Owns only the on/off decision plus its credential gate. */
37
+ export class JevFilterSubmenu implements Component, Focusable {
38
+ private readonly options: JevFilterSubmenuOptions;
39
+ private readonly deps: Required<JevSetupDeps>;
40
+ private credentials: JevCredentials | undefined;
41
+ private mode: "menu" | "resolving" = "resolving";
42
+ private notice: string | undefined;
43
+ private selectedIndex = 0;
44
+ private enabled: boolean;
45
+ private _focused = true;
46
+
47
+ constructor(options: JevFilterSubmenuOptions, deps: JevSetupDeps = {}) {
48
+ this.options = options;
49
+ this.enabled = options.currentValue === "On";
50
+ this.deps = { ...defaultSetupDeps, ...options.setupDeps, ...deps };
51
+ void fireAndForget(this.refresh(), this.reportBackgroundError);
52
+ }
53
+
54
+ get focused(): boolean {
55
+ return this._focused;
56
+ }
57
+
58
+ set focused(value: boolean) {
59
+ this._focused = value;
60
+ }
61
+
62
+ // oxlint-disable-next-line class-methods-use-this no-empty-function
63
+ invalidate(): void {}
64
+
65
+ render(width: number): string[] {
66
+ const { theme } = this.options;
67
+ const lines = [
68
+ theme.fg("accent", theme.bold(" Jev/Decisions consultation filter")),
69
+ "",
70
+ ` Filter: ${this.enabled ? "On" : "Off"}`,
71
+ ` Provider: ${this.options.currentTransport}`,
72
+ ];
73
+ if (this.mode === "resolving") {
74
+ lines.push("", " Checking the selected provider credentials…");
75
+ } else if (this.notice) {
76
+ lines.push("", theme.fg("warning", ` ${this.notice}`));
77
+ }
78
+ if (this.mode === "menu") {
79
+ lines.push("");
80
+ for (const [index, action] of actionsFor(this.enabled).entries()) {
81
+ const prefix = index === this.selectedIndex ? "→ " : " ";
82
+ lines.push(`${prefix}${labelFor(action)}`);
83
+ }
84
+ }
85
+ return lines.map((line) => truncateToWidth(line, width));
86
+ }
87
+
88
+ handleInput(keyData: string): void {
89
+ if (this.mode !== "menu") {
90
+ return;
91
+ }
92
+ const actions = actionsFor(this.enabled);
93
+ if (
94
+ matchesKey(keyData, Key.down) ||
95
+ keyData === "\u001B[B" ||
96
+ keyData === "\u001BOB"
97
+ ) {
98
+ this.selectedIndex = (this.selectedIndex + 1) % actions.length;
99
+ } else if (
100
+ matchesKey(keyData, Key.up) ||
101
+ keyData === "\u001B[A" ||
102
+ keyData === "\u001BOA"
103
+ ) {
104
+ this.selectedIndex =
105
+ (this.selectedIndex - 1 + actions.length) % actions.length;
106
+ } else if (matchesKey(keyData, Key.enter) || keyData === "\r") {
107
+ this.activate(actions[this.selectedIndex]);
108
+ } else {
109
+ return;
110
+ }
111
+ this.options.tui.requestRender();
112
+ }
113
+
114
+ private async refresh(): Promise<void> {
115
+ const transport = this.options.currentTransport;
116
+ try {
117
+ this.credentials = await this.deps.resolveTransport(
118
+ transport === "auto" ? undefined : transport
119
+ );
120
+ } catch (error) {
121
+ this.credentials = undefined;
122
+ this.notice = `Credential lookup failed: ${error instanceof Error ? error.message : String(error)}`;
123
+ }
124
+ if (!this.credentials && !this.notice) {
125
+ this.notice = `No Jev provider resolves for "${transport}". Open Jev provider to configure one.`;
126
+ }
127
+ this.mode = "menu";
128
+ this.options.tui.requestRender();
129
+ }
130
+
131
+ private reportBackgroundError = (message: string): void => {
132
+ this.mode = "menu";
133
+ this.notice = `Setup failed: ${message}`;
134
+ this.options.tui.requestRender();
135
+ };
136
+
137
+ private activate(action: FilterAction): void {
138
+ if (action === "done") {
139
+ this.options.done();
140
+ return;
141
+ }
142
+ const enabled = action === "enable";
143
+ if (enabled && !this.credentials) {
144
+ // refresh() already explains why nothing resolved.
145
+ this.options.tui.requestRender();
146
+ return;
147
+ }
148
+ const saved = this.commit({ enabled });
149
+ if (!saved) {
150
+ this.notice = "The filter state could not be saved.";
151
+ this.options.tui.requestRender();
152
+ return;
153
+ }
154
+ this.enabled = enabled;
155
+ this.notice = undefined;
156
+ this.options.done(enabled ? "On" : "Off");
157
+ }
158
+
159
+ private commit(selection: JevFilterSelection): boolean {
160
+ if (!this.options.onSelection) {
161
+ return true;
162
+ }
163
+ return this.options.onSelection(selection);
164
+ }
165
+ }
@@ -0,0 +1,137 @@
1
+ import { truncateToWidth } from "@earendil-works/pi-tui";
2
+
3
+ import {
4
+ canClearCredential,
5
+ providerName,
6
+ transportLabel,
7
+ } from "./jev-setup-support.ts";
8
+ import type {
9
+ JevProviderAction,
10
+ JevProviderSubmenuOptions,
11
+ JevProviderViewState,
12
+ } from "./jev-setup-support.ts";
13
+
14
+ export interface JevProviderInput {
15
+ render: (width: number) => string[];
16
+ }
17
+
18
+ const providerActions = (): JevProviderAction[] => [
19
+ "typesafe",
20
+ "openrouter",
21
+ "openai-decisions",
22
+ "typesafe-compatible",
23
+ ];
24
+
25
+ export const setupActions = (
26
+ state: JevProviderViewState
27
+ ): JevProviderAction[] => {
28
+ const actions = providerActions();
29
+ if (state.canEnterKey) {
30
+ actions.push("enter-key");
31
+ }
32
+ if (state.canReuseProviderLogin) {
33
+ actions.push("reuse-provider-login");
34
+ }
35
+ if (state.credentials && canClearCredential(state.credentials)) {
36
+ actions.push("clear-stored-key");
37
+ }
38
+ actions.push("done");
39
+ return actions;
40
+ };
41
+
42
+ const setupLabels = (state: JevProviderViewState): string[] =>
43
+ setupActions(state).map((action) => {
44
+ if (action === "clear-stored-key") {
45
+ return "Clear stored key";
46
+ }
47
+ if (action === "done") {
48
+ return "Done";
49
+ }
50
+ if (action === "enter-key") {
51
+ return `Enter an API key for ${providerName(state.selectedTransport ?? "typesafe")}`;
52
+ }
53
+ if (action === "reuse-provider-login") {
54
+ return "Reuse a Pi provider login";
55
+ }
56
+ if (action === "typesafe-compatible") {
57
+ return `${providerName(action)}…`;
58
+ }
59
+ return providerName(action);
60
+ });
61
+
62
+ const inputLines = (
63
+ key: "base-url" | "key-entry" | "provider-id",
64
+ state: JevProviderViewState,
65
+ input: JevProviderInput,
66
+ width: number
67
+ ): string[] => {
68
+ const rendered = input.render(Math.max(10, width - 4))[0] ?? "";
69
+ const titles = {
70
+ "base-url": [
71
+ "Base URL of a System One–compatible endpoint",
72
+ "Origin, for example https://api.codiv.ai. /v1/systemone is appended; plain http works on localhost.",
73
+ ],
74
+ "key-entry": [
75
+ providerName(state.selectedTransport ?? "typesafe"),
76
+ state.selectedTransport === "openai-decisions"
77
+ ? "No OpenAI Platform API key found. ChatGPT subscription OAuth is not accepted."
78
+ : `No API key found for ${providerName(state.selectedTransport ?? "typesafe")}.`,
79
+ ],
80
+ "provider-id": [
81
+ "Pi provider id",
82
+ "The Pi provider whose stored login supplies this endpoint's key, for example openrouter.",
83
+ ],
84
+ } as const;
85
+ const [title, description] = titles[key];
86
+ return [` ${title}`, ` ${description}`, ` ${rendered}`];
87
+ };
88
+
89
+ export const renderJevProvider = (
90
+ options: JevProviderSubmenuOptions,
91
+ state: JevProviderViewState,
92
+ input: JevProviderInput,
93
+ width: number
94
+ ): string[] => {
95
+ const { theme } = options;
96
+ const lines = [theme.fg("accent", theme.bold(" Jev provider")), ""];
97
+ if (state.credentials) {
98
+ lines.push(` Credential: ${transportLabel(state.credentials)}`);
99
+ } else if (state.selectedTransport) {
100
+ lines.push(` Selected provider: ${providerName(state.selectedTransport)}`);
101
+ } else {
102
+ lines.push(` Saved transport: ${options.currentTransport}`);
103
+ }
104
+ if (state.baseUrl) {
105
+ lines.push(` Base URL: ${state.baseUrl}`);
106
+ }
107
+ if (state.providerId) {
108
+ lines.push(` Pi provider: ${state.providerId}`);
109
+ }
110
+ if (state.notice) {
111
+ lines.push("", theme.fg("warning", ` ${state.notice}`));
112
+ }
113
+ lines.push("");
114
+ if (state.mode === "resolving") {
115
+ lines.push(" Checking the selected provider credentials…");
116
+ } else if (state.mode === "verifying") {
117
+ lines.push(" Verifying with a live Jev call…");
118
+ } else if (state.mode === "clearing") {
119
+ lines.push(" Clearing the selected provider's stored key…");
120
+ } else if (
121
+ state.mode === "base-url" ||
122
+ state.mode === "key-entry" ||
123
+ state.mode === "provider-id"
124
+ ) {
125
+ lines.push(...inputLines(state.mode, state, input, width));
126
+ if (state.inputError) {
127
+ lines.push(theme.fg("error", ` ${state.inputError}`));
128
+ }
129
+ lines.push(theme.fg("dim", " Enter: continue · Esc: return"));
130
+ } else {
131
+ for (const [index, label] of setupLabels(state).entries()) {
132
+ const prefix = index === state.selectedIndex ? "→ " : " ";
133
+ lines.push(`${prefix}${label}`);
134
+ }
135
+ }
136
+ return lines.map((line) => truncateToWidth(line, width));
137
+ };