refactor(commands): share custom api verification request flow

This commit is contained in:
Peter Steinberger
2026-02-18 18:30:13 +00:00
parent 307719abe9
commit 4f36c813a7

View File

@@ -245,38 +245,39 @@ type VerificationResult = {
error?: unknown;
};
async function requestOpenAiVerification(params: {
function resolveVerificationEndpoint(params: {
baseUrl: string;
apiKey: string;
modelId: string;
}): Promise<VerificationResult> {
// 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<string, string>;
body: Record<string, unknown>;
}): Promise<VerificationResult> {
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<VerificationResult> {
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<VerificationResult> {
// 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: {