pi-advisor-flow 0.12.1-dev.1 → 0.12.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 +7 -0
- package/README.md +2 -2
- package/dist/index.js +945 -251
- package/package.json +8 -5
- package/scripts/check-bun-version.mjs +67 -0
- package/src/commands/settings-commands.ts +35 -33
- package/src/commands/settings-persistence.ts +4 -0
- package/src/config/defaults.ts +10 -0
- package/src/config/schema.ts +27 -1
- package/src/config/state.ts +10 -0
- package/src/config/types.ts +5 -1
- package/src/config/validation.ts +24 -71
- package/src/herdr-shared.ts +137 -7
- package/src/jev/base-url.ts +68 -0
- package/src/jev/client.ts +23 -5
- package/src/jev/endpoint-key-store.ts +177 -0
- package/src/jev/transport.ts +88 -3
- package/src/ui/jev-filter-submenu.ts +165 -0
- package/src/ui/jev-provider-submenu-view.ts +137 -0
- package/src/ui/{jev-setup-submenu.ts → jev-provider-submenu.ts} +214 -144
- package/src/ui/jev-setup-support.ts +92 -29
- package/src/ui/settings-items.ts +57 -10
- package/src/ui/settings-selector.ts +65 -7
- package/src/ui/types.ts +31 -2
- package/src/ui/jev-setup-submenu-view.ts +0 -96
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const JEV_SYSTEM_ONE_PATH = "/v1/systemone";
|
|
2
|
+
|
|
3
|
+
export interface JevBaseUrlResult {
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
error?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const VERSION_PATH = "/v1";
|
|
9
|
+
|
|
10
|
+
const IPV4_LOOPBACK_HOST = /^127(?:\.\d{1,3}){3}$/u;
|
|
11
|
+
|
|
12
|
+
const isLoopbackHost = (hostname: string): boolean => {
|
|
13
|
+
const host = hostname.toLowerCase();
|
|
14
|
+
return (
|
|
15
|
+
host === "localhost" ||
|
|
16
|
+
host === "[::1]" ||
|
|
17
|
+
host === "::1" ||
|
|
18
|
+
IPV4_LOOPBACK_HOST.test(host)
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const stripContractPath = (pathname: string): string => {
|
|
23
|
+
let path = pathname.replace(/\/+$/u, "");
|
|
24
|
+
if (path.endsWith(JEV_SYSTEM_ONE_PATH)) {
|
|
25
|
+
path = path.slice(0, -JEV_SYSTEM_ONE_PATH.length);
|
|
26
|
+
}
|
|
27
|
+
path = path.replace(/\/+$/u, "");
|
|
28
|
+
if (path.endsWith(VERSION_PATH)) {
|
|
29
|
+
path = path.slice(0, -VERSION_PATH.length);
|
|
30
|
+
}
|
|
31
|
+
return path.replace(/\/+$/u, "");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Returns the origin plus path prefix of any accepted Base URL spelling. */
|
|
35
|
+
export const normalizeJevBaseUrl = (input: string): JevBaseUrlResult => {
|
|
36
|
+
const trimmed = input.trim();
|
|
37
|
+
if (!trimmed) {
|
|
38
|
+
return { error: "Enter the Base URL of a System One–compatible endpoint." };
|
|
39
|
+
}
|
|
40
|
+
let parsed: URL;
|
|
41
|
+
try {
|
|
42
|
+
parsed = new URL(trimmed);
|
|
43
|
+
} catch {
|
|
44
|
+
return { error: "That is not a valid URL." };
|
|
45
|
+
}
|
|
46
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
47
|
+
return { error: "The Base URL must use https." };
|
|
48
|
+
}
|
|
49
|
+
if (parsed.protocol === "http:" && !isLoopbackHost(parsed.hostname)) {
|
|
50
|
+
return {
|
|
51
|
+
error:
|
|
52
|
+
"Plain http is allowed only for localhost, ::1, and 127.x addresses.",
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (parsed.username || parsed.password) {
|
|
56
|
+
return { error: "Remove the credentials embedded in the URL." };
|
|
57
|
+
}
|
|
58
|
+
if (parsed.search) {
|
|
59
|
+
return { error: "Remove the query string from the Base URL." };
|
|
60
|
+
}
|
|
61
|
+
if (parsed.hash) {
|
|
62
|
+
return { error: "Remove the fragment from the Base URL." };
|
|
63
|
+
}
|
|
64
|
+
return { baseUrl: `${parsed.origin}${stripContractPath(parsed.pathname)}` };
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const jevSystemOneEndpoint = (baseUrl: string): string =>
|
|
68
|
+
`${baseUrl}${JEV_SYSTEM_ONE_PATH}`;
|
package/src/jev/client.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import { isNumber, isRecord, isRecordOf, isString } from "../content-utils.ts";
|
|
9
9
|
import type { JsonValue, RecordValue } from "../content-utils.ts";
|
|
10
10
|
import { redactSecrets } from "../redaction.ts";
|
|
11
|
+
import { jevSystemOneEndpoint } from "./base-url.ts";
|
|
11
12
|
import {
|
|
12
13
|
buildDecisionsRequest,
|
|
13
14
|
OPENAI_DECISIONS_ENDPOINT,
|
|
@@ -39,6 +40,7 @@ export interface JevAskResult {
|
|
|
39
40
|
|
|
40
41
|
export interface JevClientOptions {
|
|
41
42
|
apiKey: string;
|
|
43
|
+
baseUrl?: string;
|
|
42
44
|
fetch?: Fetch;
|
|
43
45
|
model: string;
|
|
44
46
|
pricePerMtok?: number;
|
|
@@ -46,7 +48,10 @@ export interface JevClientOptions {
|
|
|
46
48
|
transport: JevTransportKind;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
const
|
|
51
|
+
const FIXED_ENDPOINTS: Record<
|
|
52
|
+
Exclude<JevTransportKind, "typesafe-compatible">,
|
|
53
|
+
string
|
|
54
|
+
> = {
|
|
50
55
|
"openai-decisions": OPENAI_DECISIONS_ENDPOINT,
|
|
51
56
|
openrouter: "https://openrouter.ai/api/alpha/decisions",
|
|
52
57
|
typesafe: "https://api.typesafe.ai/v1/systemone",
|
|
@@ -159,6 +164,7 @@ export class JevClient {
|
|
|
159
164
|
|
|
160
165
|
constructor({
|
|
161
166
|
apiKey,
|
|
167
|
+
baseUrl,
|
|
162
168
|
fetch,
|
|
163
169
|
model,
|
|
164
170
|
pricePerMtok,
|
|
@@ -166,7 +172,17 @@ export class JevClient {
|
|
|
166
172
|
transport,
|
|
167
173
|
}: JevClientOptions) {
|
|
168
174
|
this.#apiKey = apiKey;
|
|
169
|
-
|
|
175
|
+
if (transport === "typesafe-compatible") {
|
|
176
|
+
if (!baseUrl) {
|
|
177
|
+
throw new JevFailureError(
|
|
178
|
+
"malformed",
|
|
179
|
+
"The System One–compatible endpoint has no Base URL."
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
this.#endpoint = jevSystemOneEndpoint(baseUrl);
|
|
183
|
+
} else {
|
|
184
|
+
this.#endpoint = FIXED_ENDPOINTS[transport];
|
|
185
|
+
}
|
|
170
186
|
// SAFETY: the bound global fetch satisfies the SDK Fetch signature; binding keeps the receiver correct.
|
|
171
187
|
this.#fetch = fetch ?? (globalThis.fetch.bind(globalThis) as Fetch);
|
|
172
188
|
if (transport === "openai-decisions") {
|
|
@@ -324,9 +340,10 @@ export class JevClient {
|
|
|
324
340
|
if (this.#transport === "openrouter") {
|
|
325
341
|
return "OpenRouter";
|
|
326
342
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
343
|
+
if (this.#transport === "openai-decisions") {
|
|
344
|
+
return "OpenAI Decisions";
|
|
345
|
+
}
|
|
346
|
+
return this.#transport === "typesafe" ? "TypeSafe" : "endpoint";
|
|
330
347
|
}
|
|
331
348
|
|
|
332
349
|
async #parseSuccess(
|
|
@@ -405,6 +422,7 @@ export const jevClientFromCredentials = (
|
|
|
405
422
|
): JevClient =>
|
|
406
423
|
new JevClient({
|
|
407
424
|
apiKey: credentials.apiKey,
|
|
425
|
+
baseUrl: credentials.baseUrl,
|
|
408
426
|
fetch,
|
|
409
427
|
model: advisorJevModelRef,
|
|
410
428
|
timeoutMs: advisorJevTimeoutMsRef,
|
|
@@ -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
|
+
};
|
package/src/jev/transport.ts
CHANGED
|
@@ -1,26 +1,45 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
|
|
3
|
-
import {
|
|
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 =
|
|
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
|
+
}
|