mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 14:34:32 +00:00
refactor(commands): share custom api verification request flow
This commit is contained in:
@@ -245,38 +245,39 @@ type VerificationResult = {
|
|||||||
error?: unknown;
|
error?: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
async function requestOpenAiVerification(params: {
|
function resolveVerificationEndpoint(params: {
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
apiKey: string;
|
|
||||||
modelId: string;
|
modelId: string;
|
||||||
}): Promise<VerificationResult> {
|
endpointPath: "chat/completions" | "messages";
|
||||||
// Transform Azure URLs to include the deployment path
|
}) {
|
||||||
const resolvedUrl = isAzureUrl(params.baseUrl)
|
const resolvedUrl = isAzureUrl(params.baseUrl)
|
||||||
? transformAzureUrl(params.baseUrl, params.modelId)
|
? transformAzureUrl(params.baseUrl, params.modelId)
|
||||||
: params.baseUrl;
|
: params.baseUrl;
|
||||||
const endpointUrl = new URL(
|
const endpointUrl = new URL(
|
||||||
"chat/completions",
|
params.endpointPath,
|
||||||
resolvedUrl.endsWith("/") ? resolvedUrl : `${resolvedUrl}/`,
|
resolvedUrl.endsWith("/") ? resolvedUrl : `${resolvedUrl}/`,
|
||||||
);
|
);
|
||||||
// Azure requires api-version query parameter
|
|
||||||
if (isAzureUrl(params.baseUrl)) {
|
if (isAzureUrl(params.baseUrl)) {
|
||||||
endpointUrl.searchParams.set("api-version", "2024-10-21");
|
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 {
|
try {
|
||||||
const res = await fetchWithTimeout(
|
const res = await fetchWithTimeout(
|
||||||
endpoint,
|
params.endpoint,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
...buildOpenAiHeaders(params.apiKey),
|
...params.headers,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(params.body),
|
||||||
model: params.modelId,
|
|
||||||
messages: [{ role: "user", content: "Hi" }],
|
|
||||||
max_tokens: 5,
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
VERIFY_TIMEOUT_MS,
|
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: {
|
async function requestAnthropicVerification(params: {
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
modelId: string;
|
modelId: string;
|
||||||
}): Promise<VerificationResult> {
|
}): Promise<VerificationResult> {
|
||||||
// Transform Azure URLs to include the deployment path
|
const endpoint = resolveVerificationEndpoint({
|
||||||
const resolvedUrl = isAzureUrl(params.baseUrl)
|
baseUrl: params.baseUrl,
|
||||||
? transformAzureUrl(params.baseUrl, params.modelId)
|
modelId: params.modelId,
|
||||||
: params.baseUrl;
|
endpointPath: "messages",
|
||||||
const endpointUrl = new URL(
|
});
|
||||||
"messages",
|
return await requestVerification({
|
||||||
resolvedUrl.endsWith("/") ? resolvedUrl : `${resolvedUrl}/`,
|
endpoint,
|
||||||
);
|
headers: buildAnthropicHeaders(params.apiKey),
|
||||||
// Azure requires api-version query parameter
|
body: {
|
||||||
if (isAzureUrl(params.baseUrl)) {
|
model: params.modelId,
|
||||||
endpointUrl.searchParams.set("api-version", "2024-10-21");
|
max_tokens: 16,
|
||||||
}
|
messages: [{ role: "user", content: "Hi" }],
|
||||||
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 };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function promptBaseUrlAndKey(params: {
|
async function promptBaseUrlAndKey(params: {
|
||||||
|
|||||||
Reference in New Issue
Block a user