call-ai
Version:
Lightweight library for making AI API calls with streaming support
80 lines • 3.44 kB
JavaScript
import { callAiEnv } from "./env.js";
const _keyStore = {
current: undefined,
refreshEndpoint: "https://vibecode.garden",
refreshToken: "use-vibes",
isRefreshing: false,
lastRefreshAttempt: 0,
metadata: {},
};
export function keyStore() {
return _keyStore;
}
let globalDebug = false;
function initKeyStore(env = callAiEnv) {
const store = keyStore();
store.current = typeof env.CALLAI_API_KEY === "string" ? env.CALLAI_API_KEY : undefined;
store.refreshEndpoint = typeof env.CALLAI_REFRESH_ENDPOINT === "string" ? env.CALLAI_REFRESH_ENDPOINT : "https://vibecode.garden";
store.refreshToken = typeof env.CALL_AI_REFRESH_TOKEN === "string" ? env.CALL_AI_REFRESH_TOKEN : "use-vibes";
globalDebug = !!env.CALLAI_DEBUG;
}
function isNewKeyError(ierror, debug = false) {
const error = ierror;
let status = error?.status || error?.statusCode || error?.response?.status || 450;
const errorMessage = String(error || "").toLowerCase();
if (!status && errorMessage.includes("status:")) {
const statusMatch = errorMessage.match(/status:\\s*(\\d+)/i);
if (statusMatch && statusMatch[1]) {
status = parseInt(statusMatch[1], 10);
}
}
const is4xx = status >= 400 && status < 500;
const isAuthError = status === 401 ||
status === 403 ||
errorMessage.includes("unauthorized") ||
errorMessage.includes("forbidden") ||
errorMessage.includes("authentication") ||
errorMessage.includes("api key") ||
errorMessage.includes("apikey") ||
errorMessage.includes("auth");
const isInvalidKeyError = errorMessage.includes("invalid api key") ||
errorMessage.includes("invalid key") ||
errorMessage.includes("incorrect api key") ||
errorMessage.includes("incorrect key") ||
errorMessage.includes("authentication failed") ||
errorMessage.includes("not authorized");
const isOpenAIKeyError = errorMessage.includes("openai") && (errorMessage.includes("api key") || errorMessage.includes("authentication"));
const isRateLimitError = status === 429 ||
errorMessage.includes("rate limit") ||
errorMessage.includes("too many requests") ||
errorMessage.includes("quota") ||
errorMessage.includes("exceed");
const isBillingError = errorMessage.includes("billing") ||
errorMessage.includes("payment") ||
errorMessage.includes("subscription") ||
errorMessage.includes("account");
const needsNewKey = is4xx && (isAuthError || isInvalidKeyError || isOpenAIKeyError || isRateLimitError || isBillingError);
if (debug && needsNewKey) {
console.log(`[callAi:key-refresh] Detected error requiring key refresh: ${errorMessage}`);
}
return needsNewKey;
}
function getHashFromKey(key) {
if (!key)
return null;
const metaKey = Object.keys(keyStore().metadata).find((k) => k === key);
return metaKey ? keyStore().metadata[metaKey].hash || null : null;
}
function storeKeyMetadata(data) {
if (!data || !data.key)
return;
keyStore().metadata[data.key] = {
hash: data.hash,
created: data.created || new Date(),
expires: data.expires,
remaining: data.remaining,
limit: data.limit,
};
}
export { globalDebug, initKeyStore, isNewKeyError, getHashFromKey, storeKeyMetadata };
//# sourceMappingURL=key-management.js.map