MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / validateApiKey

Function validateApiKey

packages/node-runtime/src/ai/remote-api.ts:132–173  ·  view source on GitHub ↗
(
  provider: string,
  apiKey: string,
  baseUrl?: string,
  model?: string,
  apiFormat?: string,
  options?: RemoteApiOptions
)

Source from the content-addressed store, hash-verified

130}
131
132export 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}

Callers 1

registerAiLlmRoutesFunction · 0.90

Calls 5

buildPiModelFunction · 0.90
setTimeoutFunction · 0.85
clearTimeoutFunction · 0.85
abortMethod · 0.80
nowMethod · 0.45

Tested by

no test coverage detected