| 400 | * Add account via API key. |
| 401 | */ |
| 402 | export function addAccountByKey(apiKey, label = '', apiServerUrl = '') { |
| 403 | const existing = accounts.find(a => a.apiKey === apiKey); |
| 404 | if (existing) { |
| 405 | if (apiServerUrl && !existing.apiServerUrl) { |
| 406 | existing.apiServerUrl = apiServerUrl; |
| 407 | saveAccounts(); |
| 408 | } |
| 409 | return existing; |
| 410 | } |
| 411 | |
| 412 | const account = { |
| 413 | id: randomUUID().slice(0, 8), |
| 414 | email: label || `key-${apiKey.slice(0, 8)}`, |
| 415 | apiKey, |
| 416 | apiServerUrl: apiServerUrl || '', |
| 417 | method: 'api_key', |
| 418 | status: 'active', |
| 419 | lastUsed: 0, |
| 420 | errorCount: 0, |
| 421 | refreshToken: '', |
| 422 | expiresAt: 0, |
| 423 | refreshTimer: null, |
| 424 | addedAt: Date.now(), |
| 425 | tier: 'unknown', |
| 426 | capabilities: {}, |
| 427 | lastProbed: 0, |
| 428 | blockedModels: [], |
| 429 | }; |
| 430 | account.credits = null; |
| 431 | accounts.push(account); |
| 432 | saveAccounts(); |
| 433 | log.info(`Account added: ${safeAccountRef(account)} [api_key]`); |
| 434 | return account; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Add account via auth token. |