mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(commands): share custom api verification request flow
This commit is contained in:
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user