a6521f07ed
On Windows process.env/Bun.env lookups are case-insensitive, so the "env var name, else literal" resolvers turned a literal /login key like `public` (OpenCode Zen's free key) into the built-in PUBLIC=C:\Users\Public, sending `Authorization: Bearer C:\Users\Public` and 401ing every request. Added `$envExact` in pi-utils, which trusts an env lookup only when an exact-case key is enumerated (the only case-preserving signal on Windows; the getter and hasOwnProperty/getOwnPropertyDescriptor traps are all case-insensitive there). Wired it into all three resolvers: resolve-config-value.ts, model-registry.ts, and auth-storage.ts. Fixes #7361
96 lines
2.8 KiB
TypeScript
96 lines
2.8 KiB
TypeScript
/**
|
|
* Resolve configuration values that may be shell commands, environment variables, or literals.
|
|
*
|
|
* Note: command execution is async to avoid blocking the TUI.
|
|
*/
|
|
|
|
import { executeShell } from "@oh-my-pi/pi-natives";
|
|
import { $envExact } from "@oh-my-pi/pi-utils";
|
|
|
|
/** Cache for successful shell command results (persists for process lifetime). */
|
|
const commandResultCache = new Map<string, string>();
|
|
|
|
/** De-duplicates concurrent executions for the same command. */
|
|
const commandInFlight = new Map<string, Promise<string | undefined>>();
|
|
|
|
/**
|
|
* Resolve a config value (API key, header value, etc.) to an actual value.
|
|
* - If starts with "!", executes the rest as a shell command and uses stdout (cached)
|
|
* - Otherwise checks environment variable first, then treats as literal (not cached)
|
|
*/
|
|
export async function resolveConfigValue(config: string): Promise<string | undefined> {
|
|
if (config.startsWith("!")) {
|
|
return await executeCommand(config);
|
|
}
|
|
const envValue = $envExact(config);
|
|
return envValue || config;
|
|
}
|
|
|
|
async function executeCommand(commandConfig: string): Promise<string | undefined> {
|
|
const cached = commandResultCache.get(commandConfig);
|
|
if (cached !== undefined) {
|
|
return cached;
|
|
}
|
|
|
|
const existing = commandInFlight.get(commandConfig);
|
|
if (existing) {
|
|
return await existing;
|
|
}
|
|
|
|
const command = commandConfig.slice(1);
|
|
const promise = runShellCommand(command, 10_000)
|
|
.then(result => {
|
|
if (result !== undefined) {
|
|
commandResultCache.set(commandConfig, result);
|
|
}
|
|
return result;
|
|
})
|
|
.finally(() => {
|
|
commandInFlight.delete(commandConfig);
|
|
});
|
|
|
|
commandInFlight.set(commandConfig, promise);
|
|
return await promise;
|
|
}
|
|
|
|
async function runShellCommand(command: string, timeoutMs: number): Promise<string | undefined> {
|
|
try {
|
|
let output = "";
|
|
const result = await executeShell({ command, timeoutMs }, (err, chunk) => {
|
|
if (!err) {
|
|
output += chunk;
|
|
}
|
|
});
|
|
if (result.timedOut || result.exitCode !== 0) {
|
|
return undefined;
|
|
}
|
|
const trimmed = output.trim();
|
|
return trimmed.length > 0 ? trimmed : undefined;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolve all header values using the same resolution logic as API keys.
|
|
*/
|
|
export async function resolveHeaders(
|
|
headers: Record<string, string> | undefined,
|
|
): Promise<Record<string, string> | undefined> {
|
|
if (!headers) return undefined;
|
|
const resolved: Record<string, string> = {};
|
|
for (const [key, value] of Object.entries(headers)) {
|
|
const resolvedValue = await resolveConfigValue(value);
|
|
if (resolvedValue) {
|
|
resolved[key] = resolvedValue;
|
|
}
|
|
}
|
|
return Object.keys(resolved).length > 0 ? resolved : undefined;
|
|
}
|
|
|
|
/** Clear the config value command cache. Exported for testing. */
|
|
export function clearConfigValueCache(): void {
|
|
commandResultCache.clear();
|
|
commandInFlight.clear();
|
|
}
|