/* * testKey.ts * Zyquo Cloud Web * * Author: Simon-Pierre Boucher * Mail: contact@spboucher.ai * * Key validation, ported from the native ProviderClient extension: the * cheapest authenticated call available — /models where supported, otherwise * (Perplexity) a minimal non-streaming completion against the provider's * cheapest catalog model. Returns round-trip latency in ms. */ import type { Provider } from '../types' import { cheapestModel } from './catalog' import { clientFor, PROVIDER_META } from './registry' import { ProviderError } from './types' export async function testKey( provider: Provider, apiKey: string, baseURLOverride?: string ): Promise { const client = clientFor(provider) const start = performance.now() if (PROVIDER_META[provider].supportsModelListing) { await client.listModelIDs(apiKey, baseURLOverride) } else { const model = cheapestModel(provider) if (!model) throw ProviderError.noModelAvailable(provider) await client.complete( { model, messages: [ { id: 'key-test', role: 'user', text: 'Reply with exactly: OK', createdAt: 0 }, ], // Perplexity requires max_tokens ≥ 16 (probe-verified, docs/CORS-MATRIX.md). parameters: { maxTokens: 16 }, stream: false, ...(baseURLOverride !== undefined ? { baseURLOverride } : {}), }, apiKey ) } return performance.now() - start }