byok-llm 1.0.0

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,279 @@
1
+ /** Supported built-in provider IDs */
2
+ type BuiltinProviderId = "anthropic" | "openai" | "openrouter" | "google" | "groq" | "xai" | "mistral" | "deepseek";
3
+ /** Any provider ID (builtins + custom) */
4
+ type ProviderId = BuiltinProviderId | (string & {});
5
+ /** How a provider authenticates requests */
6
+ type AuthStyle = "x-api-key" | "bearer" | "query-param";
7
+ /** Definition of a provider */
8
+ interface ProviderDefinition {
9
+ /** Unique identifier, e.g. 'anthropic' */
10
+ id: ProviderId;
11
+ /** Human-readable name, e.g. 'Anthropic' */
12
+ name: string;
13
+ /** Environment variable names to check, in order */
14
+ envKeys: string[];
15
+ /** Expected key prefix for format validation (e.g. 'sk-ant-') */
16
+ keyPrefix?: string | string[];
17
+ /** Base URL for API requests */
18
+ baseUrl: string;
19
+ /** URL where users can get an API key */
20
+ docsUrl: string;
21
+ /** How this provider authenticates */
22
+ authStyle: AuthStyle;
23
+ /** Custom auth header name (default depends on authStyle) */
24
+ authHeader?: string;
25
+ /** Extra default headers to include */
26
+ defaultHeaders?: Record<string, string>;
27
+ /** Lightweight endpoint to validate a key (GET request) */
28
+ validateEndpoint?: string;
29
+ /** Whether this provider is OpenAI-SDK-compatible */
30
+ openaiCompatible?: boolean;
31
+ }
32
+ /** Where a resolved key came from */
33
+ type KeySource = "runtime" | "env" | "project-config" | "user-config";
34
+ /** A resolved API key with metadata */
35
+ interface ResolvedKey {
36
+ key: string;
37
+ source: KeySource;
38
+ providerId: ProviderId;
39
+ envVar?: string;
40
+ }
41
+ /** Result of key validation */
42
+ interface ValidationResult {
43
+ valid: boolean;
44
+ error?: string;
45
+ /** Extra metadata from the provider (e.g. org name, remaining credits) */
46
+ meta?: Record<string, unknown>;
47
+ }
48
+ /** Result of fallback resolution */
49
+ interface ResolvedProvider {
50
+ providerId: ProviderId;
51
+ key: string;
52
+ source: KeySource;
53
+ baseUrl: string;
54
+ headers: Record<string, string>;
55
+ }
56
+ /** Per-provider config in config files */
57
+ interface ProviderConfig {
58
+ apiKey?: string;
59
+ baseUrl?: string;
60
+ disabled?: boolean;
61
+ headers?: Record<string, string>;
62
+ }
63
+ /** Full byok-ai config file shape */
64
+ interface ByokConfig {
65
+ providers?: Record<string, ProviderConfig>;
66
+ defaultProvider?: ProviderId;
67
+ fallbackOrder?: ProviderId[];
68
+ }
69
+ /** Status of a single provider */
70
+ interface ProviderStatus {
71
+ id: ProviderId;
72
+ name: string;
73
+ configured: boolean;
74
+ source?: KeySource;
75
+ keyPreview?: string;
76
+ valid?: boolean;
77
+ error?: string;
78
+ }
79
+ /** Overall status report */
80
+ interface StatusReport {
81
+ providers: ProviderStatus[];
82
+ defaultProvider?: ProviderId;
83
+ }
84
+ /** Options for the setup wizard */
85
+ interface WizardOptions {
86
+ /** Which providers to offer for configuration */
87
+ providers?: ProviderId[];
88
+ /** At least one of these must be configured */
89
+ required?: ProviderId[];
90
+ /** App name shown in prompts */
91
+ appName?: string;
92
+ /** Skip providers that already have keys configured */
93
+ skipConfigured?: boolean;
94
+ }
95
+ /** OpenAI-SDK-compatible configuration */
96
+ interface OpenAICompatibleConfig {
97
+ apiKey: string;
98
+ baseURL: string;
99
+ defaultHeaders?: Record<string, string>;
100
+ }
101
+ /** Options passed to the top-level init */
102
+ interface ByokOptions {
103
+ /** Provider overrides (runtime keys) */
104
+ providers?: Record<string, ProviderConfig>;
105
+ /** Path to project config file */
106
+ configPath?: string;
107
+ /** Disable user config loading */
108
+ noUserConfig?: boolean;
109
+ }
110
+
111
+ /**
112
+ * Register a custom provider (or override a built-in one).
113
+ */
114
+ declare function registerProvider(definition: ProviderDefinition): void;
115
+ /**
116
+ * Get a provider definition by ID.
117
+ */
118
+ declare function getProvider(id: ProviderId): ProviderDefinition | undefined;
119
+ /**
120
+ * List all registered providers.
121
+ */
122
+ declare function listProviders(): ProviderDefinition[];
123
+ /**
124
+ * Check if a provider is registered.
125
+ */
126
+ declare function hasProvider(id: ProviderId): boolean;
127
+ /**
128
+ * Default fallback order (most common providers first).
129
+ */
130
+ declare const DEFAULT_FALLBACK_ORDER: ProviderId[];
131
+
132
+ /**
133
+ * Initialize / reset the config cache. Call this if you change options at runtime.
134
+ */
135
+ declare function initConfig(options?: ByokOptions): ByokConfig;
136
+ /**
137
+ * Get the current config (loads if needed).
138
+ */
139
+ declare function getConfig(): ByokConfig;
140
+ /**
141
+ * Resolve an API key for a provider, checking all layers.
142
+ *
143
+ * Resolution order:
144
+ * 1. Runtime options (passed via init or resolveKey options)
145
+ * 2. Environment variables
146
+ * 3. Project config file
147
+ * 4. User config file
148
+ * 5. Stored keys (~/.config/byok-ai/keys.json)
149
+ */
150
+ declare function resolveKey(providerId: ProviderId, runtimeKey?: string): Promise<ResolvedKey | null>;
151
+ /**
152
+ * Try to resolve a working provider from a list, in order.
153
+ * Returns the first provider that has a configured key.
154
+ */
155
+ declare function resolveWithFallback(providerIds?: ProviderId[]): Promise<ResolvedProvider | null>;
156
+ /**
157
+ * Get all providers that currently have keys configured.
158
+ */
159
+ declare function getConfiguredProviders(): Promise<ResolvedKey[]>;
160
+
161
+ /**
162
+ * Get auth headers for a provider (resolves key automatically).
163
+ */
164
+ declare function getProviderHeaders(providerId: ProviderId): Promise<Record<string, string>>;
165
+ /**
166
+ * Get the base URL for a provider.
167
+ */
168
+ declare function getProviderBaseUrl(providerId: ProviderId): string;
169
+ /**
170
+ * Get a config object compatible with the OpenAI SDK.
171
+ * Works for: openai, openrouter, groq, xai, mistral, deepseek, and any
172
+ * other provider marked as openaiCompatible.
173
+ */
174
+ declare function getOpenAICompatibleConfig(providerId: ProviderId): Promise<OpenAICompatibleConfig>;
175
+
176
+ /**
177
+ * Get the user-level config path.
178
+ */
179
+ declare function getUserConfigPath(): string;
180
+ /**
181
+ * Get the user-level keys storage path.
182
+ */
183
+ declare function getKeysStorePath(): string;
184
+
185
+ /**
186
+ * Validate a key's format (prefix check).
187
+ */
188
+ declare function validateKeyFormat(providerId: ProviderId, key: string): ValidationResult;
189
+ /**
190
+ * Validate a key by making a lightweight API call.
191
+ * This hits a read-only endpoint (like listing models) to verify the key works.
192
+ */
193
+ declare function validateKey(providerId: ProviderId, key: string): Promise<ValidationResult>;
194
+ /**
195
+ * Validate all currently configured keys.
196
+ */
197
+ declare function validateAllKeys(): Promise<Record<string, ValidationResult>>;
198
+
199
+ /**
200
+ * Store an API key for a provider.
201
+ */
202
+ declare function storeKey(providerId: ProviderId, key: string): Promise<void>;
203
+ /**
204
+ * Get a stored API key for a provider.
205
+ */
206
+ declare function getKey(providerId: ProviderId): Promise<string | null>;
207
+ /**
208
+ * Remove a stored API key for a provider.
209
+ */
210
+ declare function removeKey(providerId: ProviderId): Promise<void>;
211
+ /**
212
+ * List all stored provider IDs.
213
+ */
214
+ declare function listStoredKeys(): Promise<ProviderId[]>;
215
+ /**
216
+ * Clear all stored keys.
217
+ */
218
+ declare function clearAllKeys(): Promise<void>;
219
+
220
+ /**
221
+ * Check if a provider has its API key set via environment variable.
222
+ */
223
+ declare function hasEnvKey(providerId: ProviderId): boolean;
224
+ /**
225
+ * Get the env var value for a provider (checks all registered env var names).
226
+ */
227
+ declare function getEnvKey(providerId: ProviderId): string | null;
228
+ /**
229
+ * Get the primary env var name for a provider (for display/docs).
230
+ */
231
+ declare function getEnvVarName(providerId: ProviderId): string | null;
232
+ /**
233
+ * Generate a .env template string with all providers.
234
+ */
235
+ declare function generateEnvTemplate(providerIds?: ProviderId[]): string;
236
+
237
+ /**
238
+ * Run the interactive setup wizard.
239
+ *
240
+ * Walks the user through configuring API keys for the specified providers.
241
+ * Keys are stored in ~/.config/byok-ai/keys.json with 0600 permissions.
242
+ */
243
+ declare function setupWizard(options?: WizardOptions): Promise<void>;
244
+
245
+ /**
246
+ * Get the status of all registered providers.
247
+ */
248
+ declare function getStatus(providerIds?: ProviderId[]): Promise<StatusReport>;
249
+ /**
250
+ * Print a formatted status report to stdout.
251
+ */
252
+ declare function printStatus(providerIds?: ProviderId[]): Promise<void>;
253
+
254
+ /**
255
+ * All-in-one initialization: loads config, checks for keys,
256
+ * and optionally runs the setup wizard if no keys are found.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * import { init } from 'byok-ai';
261
+ *
262
+ * const provider = await init({
263
+ * providers: ['anthropic', 'openai', 'openrouter'],
264
+ * appName: 'my-cli-tool',
265
+ * });
266
+ *
267
+ * // provider.key, provider.baseUrl, provider.headers are ready to use
268
+ * ```
269
+ */
270
+ declare function init(options?: ByokOptions & {
271
+ /** Providers to check (in fallback order) */
272
+ providers?: ProviderId[];
273
+ /** App name for wizard prompts */
274
+ appName?: string;
275
+ /** Run wizard if no keys found (default: true) */
276
+ interactive?: boolean;
277
+ }): Promise<ResolvedProvider | null>;
278
+
279
+ export { type AuthStyle, type BuiltinProviderId, type ByokConfig, type ByokOptions, DEFAULT_FALLBACK_ORDER, type KeySource, type OpenAICompatibleConfig, type ProviderConfig, type ProviderDefinition, type ProviderId, type ProviderStatus, type ResolvedKey, type ResolvedProvider, type StatusReport, type ValidationResult, type WizardOptions, clearAllKeys, generateEnvTemplate, getConfig, getConfiguredProviders, getEnvKey, getEnvVarName, getKey, getKeysStorePath, getOpenAICompatibleConfig, getProvider, getProviderBaseUrl, getProviderHeaders, getStatus, getUserConfigPath, hasEnvKey, hasProvider, init, initConfig, listProviders, listStoredKeys, printStatus, registerProvider, removeKey, resolveKey, resolveWithFallback, setupWizard, storeKey, validateAllKeys, validateKey, validateKeyFormat };