From 4f36c813a7725054b0e7dfb7f6e21271f65cd000 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 18 Feb 2026 18:30:13 +0000 Subject: [PATCH] refactor(commands): share custom api verification request flow --- src/commands/onboard-custom.ts | 98 +++++++++++++++++----------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/src/commands/onboard-custom.ts b/src/commands/onboard-custom.ts index 5b631631ca4..2ab5de416a4 100644 --- a/src/commands/onboard-custom.ts +++ b/src/commands/onboard-custom.ts @@ -245,38 +245,39 @@ type VerificationResult = { error?: unknown; }; -async function requestOpenAiVerification(params: { +function resolveVerificationEndpoint(params: { baseUrl: string; - apiKey: string; modelId: string; -}): Promise { - // Transform Azure URLs to include the deployment path + endpointPath: "chat/completions" | "messages"; +}) { const resolvedUrl = isAzureUrl(params.baseUrl) ? transformAzureUrl(params.baseUrl, params.modelId) : params.baseUrl; const endpointUrl = new URL( - "chat/completions", + params.endpointPath, resolvedUrl.endsWith("/") ? resolvedUrl : `${resolvedUrl}/`, ); - // Azure requires api-version query parameter if (isAzureUrl(params.baseUrl)) { endpointUrl.searchParams.set("api-version", "2024-10-21"); } - const endpoint = endpointUrl.href; + return endpointUrl.href; +} + +async function requestVerification(params: { + endpoint: string; + headers: Record; + body: Record; +}): Promise { try { const res = await fetchWithTimeout( - endpoint, + params.endpoint, { method: "POST", headers: { "Content-Type": "application/json", - ...buildOpenAiHeaders(params.apiKey), + ...params.headers, }, - body: JSON.stringify({ - model: params.modelId, - messages: [{ role: "user", content: "Hi" }], - max_tokens: 5, - }), + body: JSON.stringify(params.body), }, VERIFY_TIMEOUT_MS, ); @@ -286,45 +287,46 @@ async function requestOpenAiVerification(params: { } } +async function requestOpenAiVerification(params: { + baseUrl: string; + apiKey: string; + modelId: string; +}): Promise { + const endpoint = resolveVerificationEndpoint({ + baseUrl: params.baseUrl, + modelId: params.modelId, + endpointPath: "chat/completions", + }); + return await requestVerification({ + endpoint, + headers: buildOpenAiHeaders(params.apiKey), + body: { + model: params.modelId, + messages: [{ role: "user", content: "Hi" }], + max_tokens: 5, + }, + }); +} + async function requestAnthropicVerification(params: { baseUrl: string; apiKey: string; modelId: string; }): Promise { - // Transform Azure URLs to include the deployment path - const resolvedUrl = isAzureUrl(params.baseUrl) - ? transformAzureUrl(params.baseUrl, params.modelId) - : params.baseUrl; - const endpointUrl = new URL( - "messages", - resolvedUrl.endsWith("/") ? resolvedUrl : `${resolvedUrl}/`, - ); - // Azure requires api-version query parameter - if (isAzureUrl(params.baseUrl)) { - endpointUrl.searchParams.set("api-version", "2024-10-21"); - } - const endpoint = endpointUrl.href; - try { - const res = await fetchWithTimeout( - endpoint, - { - method: "POST", - headers: { - "Content-Type": "application/json", - ...buildAnthropicHeaders(params.apiKey), - }, - body: JSON.stringify({ - model: params.modelId, - max_tokens: 16, - messages: [{ role: "user", content: "Hi" }], - }), - }, - VERIFY_TIMEOUT_MS, - ); - return { ok: res.ok, status: res.status }; - } catch (error) { - return { ok: false, error }; - } + const endpoint = resolveVerificationEndpoint({ + baseUrl: params.baseUrl, + modelId: params.modelId, + endpointPath: "messages", + }); + return await requestVerification({ + endpoint, + headers: buildAnthropicHeaders(params.apiKey), + body: { + model: params.modelId, + max_tokens: 16, + messages: [{ role: "user", content: "Hi" }], + }, + }); } async function promptBaseUrlAndKey(params: {