blackboxify
Version:
BlackboxAI API client with OpenAI-compatible interface, streaming support, multiple auth accounts, and token pricing
40 lines (34 loc) • 943 B
JavaScript
class AuthManager {
constructor(authList = []) {
this.authList = authList;
this.currentIndex = 0;
}
getCurrentAuth() {
if (this.authList.length === 0) return null;
return this.authList[this.currentIndex];
}
moveToNextAuth() {
if (this.authList.length === 0) return null;
this.currentIndex = (this.currentIndex + 1) % this.authList.length;
return this.getCurrentAuth();
}
async tryWithRetry(fn) {
if (this.authList.length === 0) {
// No auth accounts, just try once
return fn(null);
}
let lastError = null;
for (let i = 0; i < this.authList.length; i++) {
const auth = this.authList[this.currentIndex];
try {
const result = await fn(auth);
return result;
} catch (error) {
lastError = error;
this.moveToNextAuth();
}
}
throw lastError;
}
}
export { AuthManager };