refactor: update checks to make it clear
This commit is contained in:
@ -1,36 +0,0 @@
|
|||||||
export function isValidAnthropicApiKey(key: string) {
|
|
||||||
const regex = /^sk-ant-[a-zA-Z0-9_-]{32,}$/;
|
|
||||||
return regex.test(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function checkAnthropicAPIKey(apiKey: string) {
|
|
||||||
const response = await fetch('https://api.anthropic.com/v1/messages', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'x-api-key': apiKey,
|
|
||||||
'anthropic-version': '2023-06-01',
|
|
||||||
'content-type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: 'claude-3-opus-20240229', // or a cheaper model
|
|
||||||
max_tokens: 1, // minimal usage
|
|
||||||
messages: [{ role: 'user', content: 'Hi' }],
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
console.log('✅ API key is valid and has credit.');
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
const data = await response.json().catch(() => ({}));
|
|
||||||
if (response.status === 401) {
|
|
||||||
console.log('❌ Invalid API key.');
|
|
||||||
} else if (response.status === 403 && data.error?.message?.includes('billing')) {
|
|
||||||
console.log('❌ API key has no credit or is not authorized.');
|
|
||||||
} else {
|
|
||||||
console.log('⚠️ Unexpected error:', data);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
57
worker/src/utils/check/checks.ts
Normal file
57
worker/src/utils/check/checks.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { isValidAnthropicApiKey } from "./anthropicCheck";
|
||||||
|
|
||||||
|
import { namespaceWrapper } from "@_koii/namespace-wrapper";
|
||||||
|
import { LogLevel } from "@_koii/namespace-wrapper/dist/types";
|
||||||
|
import { errorMessage, actionMessage, status } from "../constant";
|
||||||
|
import { checkAnthropicAPIKey } from "./anthropicCheck";
|
||||||
|
import { checkGitHub } from "./githubCheck";
|
||||||
|
export async function preRunCheck(roundNumber:string){
|
||||||
|
if (!process.env.ANTHROPIC_API_KEY) {
|
||||||
|
await namespaceWrapper.logMessage(
|
||||||
|
LogLevel.Error,
|
||||||
|
errorMessage.ANTHROPIC_API_KEY_INVALID,
|
||||||
|
actionMessage.ANTHROPIC_API_KEY_INVALID,
|
||||||
|
);
|
||||||
|
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ANTHROPIC_API_KEY_INVALID);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!isValidAnthropicApiKey(process.env.ANTHROPIC_API_KEY!)) {
|
||||||
|
await namespaceWrapper.logMessage(
|
||||||
|
LogLevel.Error,
|
||||||
|
errorMessage.ANTHROPIC_API_KEY_INVALID,
|
||||||
|
actionMessage.ANTHROPIC_API_KEY_INVALID,
|
||||||
|
);
|
||||||
|
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ANTHROPIC_API_KEY_INVALID);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const isAnthropicAPIKeyValid = await checkAnthropicAPIKey(process.env.ANTHROPIC_API_KEY!);
|
||||||
|
if (!isAnthropicAPIKeyValid) {
|
||||||
|
await namespaceWrapper.logMessage(
|
||||||
|
LogLevel.Error,
|
||||||
|
errorMessage.ANTHROPIC_API_KEY_NO_CREDIT,
|
||||||
|
actionMessage.ANTHROPIC_API_KEY_NO_CREDIT,
|
||||||
|
);
|
||||||
|
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.ANTHROPIC_API_KEY_NO_CREDIT);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!process.env.GITHUB_USERNAME || !process.env.GITHUB_TOKEN) {
|
||||||
|
await namespaceWrapper.logMessage(
|
||||||
|
LogLevel.Error,
|
||||||
|
errorMessage.GITHUB_CHECK_FAILED,
|
||||||
|
actionMessage.GITHUB_CHECK_FAILED,
|
||||||
|
);
|
||||||
|
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.GITHUB_CHECK_FAILED);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const isGitHubValid = await checkGitHub(process.env.GITHUB_USERNAME!, process.env.GITHUB_TOKEN!);
|
||||||
|
if (!isGitHubValid) {
|
||||||
|
await namespaceWrapper.logMessage(
|
||||||
|
LogLevel.Error,
|
||||||
|
errorMessage.GITHUB_CHECK_FAILED,
|
||||||
|
actionMessage.GITHUB_CHECK_FAILED,
|
||||||
|
);
|
||||||
|
await namespaceWrapper.storeSet(`result-${roundNumber}`, status.GITHUB_CHECK_FAILED);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
Reference in New Issue
Block a user