autoagent-cli
Version:
Run autonomous AI agents using Claude or Gemini for task execution
199 lines (198 loc) • 6.77 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProviderRateLimiter = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const logger_1 = require("../utils/logger");
const RATE_LIMIT_PATTERNS = {
gemini: [
'status 429',
'quota exceeded',
'Quota exceeded',
'rateLimitExceeded',
'rate limit exceeded',
'resource_exhausted',
'rate_limit_exceeded',
'too many requests'
],
claude: [
'rate limit',
'rate_limit',
'status 429',
'too many requests',
'quota exceeded',
'resource_exhausted'
],
mock: [
'rate limit',
'quota exceeded'
]
};
const PROVIDER_CONFIG = {
gemini: {
primaryModel: 'gemini-2.5-pro',
fallbackModel: 'gemini-2.5-flash',
cooldownPeriod: 3600000
},
claude: {
primaryModel: 'claude-sonnet-4-20250514',
fallbackModel: undefined,
cooldownPeriod: 3600000
},
mock: {
primaryModel: 'mock-model',
fallbackModel: undefined,
cooldownPeriod: 3600000
}
};
class ProviderRateLimiter {
constructor() {
this.cache = {};
this.cacheLoaded = false;
this.cachePath = path.join(ProviderRateLimiter.CONFIG_DIR, ProviderRateLimiter.CACHE_FILE);
}
async loadCache() {
if (this.cacheLoaded) {
return;
}
try {
const content = await fs_1.promises.readFile(this.cachePath, 'utf-8');
this.cache = JSON.parse(content);
this.cacheLoaded = true;
}
catch {
this.cache = {};
this.cacheLoaded = true;
}
}
async saveCache() {
await this.ensureDirectoryExists(path.dirname(this.cachePath));
await fs_1.promises.writeFile(this.cachePath, JSON.stringify(this.cache, null, 2));
}
async isProviderRateLimited(provider) {
await this.loadCache();
const rateLimitData = this.cache[provider];
if (!rateLimitData) {
return false;
}
const config = PROVIDER_CONFIG[provider];
if (!config) {
return false;
}
const timeSinceLimited = Date.now() - rateLimitData.limitedAt;
return timeSinceLimited < config.cooldownPeriod;
}
async markProviderRateLimited(provider, error) {
await this.loadCache();
const existing = this.cache[provider];
this.cache[provider] = {
provider,
limitedAt: Date.now(),
attempts: (existing?.attempts ?? 0) + 1,
lastError: error
};
await this.saveCache();
logger_1.Logger.warning(`Rate limit detected for ${provider}. Attempts: ${this.cache[provider].attempts}`);
}
async clearProviderRateLimit(provider) {
await this.loadCache();
delete this.cache[provider];
await this.saveCache();
logger_1.Logger.info(`Rate limit cleared for ${provider}`);
}
isRateLimitError(provider, text) {
const patterns = RATE_LIMIT_PATTERNS[provider] || [];
const lowerText = text.toLowerCase();
return patterns.some((pattern) => lowerText.includes(pattern.toLowerCase()));
}
async getBestGeminiModel() {
const isRateLimited = await this.isProviderRateLimited('gemini');
const config = PROVIDER_CONFIG.gemini;
if (!config) {
return 'gemini-2.5-pro';
}
if (isRateLimited && config.fallbackModel !== null && config.fallbackModel !== undefined && config.fallbackModel !== '') {
return config.fallbackModel;
}
return config.primaryModel;
}
async getRateLimitStatus(provider) {
await this.loadCache();
const rateLimitData = this.cache[provider];
if (!rateLimitData) {
return { isLimited: false };
}
const config = PROVIDER_CONFIG[provider];
if (!config) {
return { isLimited: false };
}
const timeSinceLimited = Date.now() - rateLimitData.limitedAt;
const isLimited = timeSinceLimited < config.cooldownPeriod;
return {
isLimited,
timeRemaining: isLimited ? config.cooldownPeriod - timeSinceLimited : undefined,
attempts: rateLimitData.attempts,
lastError: rateLimitData.lastError
};
}
async getRateLimitSummary() {
await this.loadCache();
const summary = {};
for (const provider of ['claude', 'gemini']) {
const status = await this.getRateLimitStatus(provider);
summary[provider] = {
isLimited: status.isLimited,
timeRemaining: status.timeRemaining,
attempts: status.attempts
};
}
return summary;
}
async ensureDirectoryExists(dirPath) {
try {
await fs_1.promises.mkdir(dirPath, { recursive: true });
}
catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
}
}
}
exports.ProviderRateLimiter = ProviderRateLimiter;
ProviderRateLimiter.CACHE_FILE = 'provider-rate-limits.json';
ProviderRateLimiter.CONFIG_DIR = path.join(os.homedir(), '.autoagent');