( provider: string, apiKey: string, baseUrl?: string, model?: string, apiFormat?: string, options?: RemoteApiOptions )
| 130 | } |
| 131 | |
| 132 | export async function validateApiKey( |
| 133 | provider: string, |
| 134 | apiKey: string, |
| 135 | baseUrl?: string, |
| 136 | model?: string, |
| 137 | apiFormat?: string, |
| 138 | options?: RemoteApiOptions |
| 139 | ): Promise<{ success: boolean; error?: string }> { |
| 140 | const providerDef = BUILTIN_PROVIDERS.find((p) => p.id === provider) |
| 141 | const defaultModel = providerDef?.modelIds?.[0] |
| 142 | |
| 143 | const config: PiModelConfig = { |
| 144 | provider, |
| 145 | model: model || defaultModel, |
| 146 | baseUrl, |
| 147 | apiFormat, |
| 148 | } |
| 149 | const piModel: PiModel<PiApi> = buildPiModel(config, { headers: options?.headers }) |
| 150 | |
| 151 | const abortController = new AbortController() |
| 152 | const timeout = setTimeout(() => abortController.abort(), 15000) |
| 153 | |
| 154 | try { |
| 155 | const result = await completeSimple( |
| 156 | piModel, |
| 157 | { messages: [{ role: 'user', content: 'Hi', timestamp: Date.now() }] as any }, |
| 158 | { apiKey, maxTokens: 1, signal: abortController.signal } |
| 159 | ) |
| 160 | if (result.stopReason === 'error' || result.stopReason === 'aborted') { |
| 161 | return { success: false, error: result.errorMessage || 'Connection failed' } |
| 162 | } |
| 163 | return { success: true } |
| 164 | } catch (error) { |
| 165 | const message = error instanceof Error ? error.message : String(error) |
| 166 | if (message.includes('aborted') || message.includes('AbortError')) { |
| 167 | return { success: false, error: 'Request timed out (15s)' } |
| 168 | } |
| 169 | return { success: false, error: message } |
| 170 | } finally { |
| 171 | clearTimeout(timeout) |
| 172 | } |
| 173 | } |
no test coverage detected