/** * API Key Store - Centralized storage and hot-reload for provider API keys. * * Per Q5 strategy C: explicit /keys command + file watcher both supported. * Per Q6 strategy A: keys stored in plain text in ~/.phi/agent/models.json * with chmod 0600 on Unix and clear warnings to users. * * Storage format: models.json providers..apiKey. Resolution follows the * same convention as model-registry/resolve-config-value ("env var name or * literal value", plus "!cmd" shell commands): * 1. Value in models.json — "!cmd" runs the command (cached per process), * an env-var NAME resolves to its value, "$NAME" (legacy notation) * resolves NAME and is never returned literally, anything else is the * literal key * 2. process.env[envVar] (legacy fallback) * * The file may contain // comments and trailing commas (same tolerance as * model-registry). Note that setKey()/removeKey() rewrite the file as plain * JSON, so comments do not survive a programmatic write. * * Events emitted via the EventEmitter: * - "key_changed" { provider } : when setKey() or external edit detected * - "key_removed" { provider } : when removeKey() called * - "store_reloaded" : when reloadFromDisk() succeeds */ import { EventEmitter } from "node:events"; export interface ProviderConfigPersisted { baseUrl?: string; api?: string; apiKey?: string; headers?: Record; authHeader?: boolean; models?: unknown[]; modelOverrides?: Record; } export interface ModelsConfigPersisted { $comment?: string; providers: Record; } export interface ApiKeyStoreOptions { configPath?: string; } export declare class ApiKeyStore extends EventEmitter { readonly configPath: string; private config; private loaded; constructor(options?: ApiKeyStoreOptions); /** * Load config from disk. Creates an empty file if missing. */ load(): ModelsConfigPersisted; /** * Reload from disk and emit "store_reloaded". */ reloadFromDisk(): void; /** * Get the API key for a provider, with env-var fallback. * * The stored value is resolved through `resolveConfigValue`, the single * convention shared with the model runtime, so a config that works at * request time also works here: `$NAME` / `${NAME}` read the environment, * `!cmd` runs a shell command, anything else is a literal credential. * * (Before pi 0.84 a bare `NAME` also read the environment. That was * ambiguous — a literal key matching an env-var name was silently shadowed — * and the convention is now explicit. Configs written the old way must add * the `$`.) * * Returns undefined if no source yields a value. */ getKey(providerId: string, envVar?: string): string | undefined; /** * Get the full provider config block for a provider. */ getProvider(providerId: string): ProviderConfigPersisted | undefined; /** * List all configured provider IDs. */ listProviders(): string[]; /** * Set the API key for a provider. Creates the provider entry if missing. * Performs atomic write (tmp + rename) and chmod 0600 on Unix. * Emits "key_changed" event. */ setKey(providerId: string, key: string, providerConfig?: Partial): void; /** * Remove the API key for a provider (but keep the rest of the provider config). * Emits "key_removed" event. */ removeKey(providerId: string): void; /** * Mask an API key for display (preserves length info without exposing the secret). */ static maskKey(key: string | undefined): string; /** * Atomic write of the config to disk: * 1. Ensure parent dir exists * 2. Write to .tmp * 3. Rename to (atomic on POSIX) * 4. Apply chmod 0600 on Unix (silently skip on Windows) */ private persist; } export declare function getApiKeyStore(options?: ApiKeyStoreOptions): ApiKeyStore; export declare function _resetApiKeyStore(): void; //# sourceMappingURL=api-key-store.d.ts.map