/** * HU-4587 (ADR 0064): the agent accounts, one list under ~/.karajan/accounts.json, written only by kj. * An account names its provider and the directory of its login; each provider has one default account. * No account added: no file, and every agent runs on the login it inherits, as before. */ import fs from "node:fs"; import path from "node:path"; import { getKarajanHome } from "../utils/paths.js"; import { ACCOUNT_VAR } from "../utils/agent-home.js"; import { exhaustedUntil } from "./exhausted.js"; const ID = /^[a-z0-9][a-z0-9_-]{0,39}$/; const fileOf = (home) => path.join(home, "accounts.json"); /** @returns {{accounts: {id: string, provider: string, dir: string}[], default: Record, projects: Record>}} */ export function readAccounts({ home = getKarajanHome() } = {}) { const file = fileOf(home); if (!fs.existsSync(file)) return { accounts: [], default: {}, projects: {} }; try { const data = JSON.parse(fs.readFileSync(file, "utf8")); return { accounts: data.accounts ?? [], default: data.default ?? {}, projects: data.projects ?? {} }; } catch (error) { throw new Error(`accounts: ${file} cannot be read (${error.message}); fix it or remove it`, { cause: error }); } } function write(data, home) { fs.mkdirSync(home, { recursive: true }); const tmp = `${fileOf(home)}.${process.pid}.tmp`; fs.writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 }); fs.renameSync(tmp, fileOf(home)); } function find(data, id) { const account = data.accounts.find((a) => a.id === id); if (!account) throw new Error(`accounts: no account "${id}"`); return account; } export function addAccount({ id, provider, dir }, { home = getKarajanHome() } = {}) { if (!ID.test(String(id))) throw new Error(`accounts: the id "${id}" must be lowercase letters, digits, - or _ (up to 40)`); if (!ACCOUNT_VAR[provider]) throw new Error(`accounts: unknown provider "${provider}" (${Object.keys(ACCOUNT_VAR).join(", ")})`); if (!path.isAbsolute(String(dir))) throw new Error(`accounts: the login dir must be an absolute path, got "${dir}"`); if (!fs.statSync(dir, { throwIfNoEntry: false })?.isDirectory()) throw new Error(`accounts: ${dir} is not a directory`); const data = readAccounts({ home }); if (data.accounts.some((a) => a.id === id)) throw new Error(`accounts: the account "${id}" is already added`); data.accounts.push({ id, provider, dir }); data.default[provider] ??= id; write(data, home); } export function removeAccount(id, { home = getKarajanHome() } = {}) { const data = readAccounts({ home }); const { provider } = find(data, id); data.accounts = data.accounts.filter((a) => a.id !== id); if (data.default[provider] === id) delete data.default[provider]; for (const order of Object.values(data.projects)) { for (const [name, ids] of Object.entries(order)) order[name] = ids.filter((x) => x !== id); } write(data, home); } /** HU-4588: the accounts a project prefers for one provider, first is used. */ export function assignAccounts(projectKey, provider, ids, { home = getKarajanHome() } = {}) { if (!Array.isArray(ids) || ids.length === 0) throw new Error("accounts: assign at least one account"); const data = readAccounts({ home }); for (const id of ids) { if (find(data, id).provider !== provider) throw new Error(`accounts: "${id}" is not a ${provider} account`); } data.projects[projectKey] = { ...data.projects[projectKey], [provider]: [...new Set(ids)] }; write(data, home); } /** HU-4588: the project goes back to the default accounts. */ export function clearProject(projectKey, { home = getKarajanHome() } = {}) { const data = readAccounts({ home }); delete data.projects[projectKey]; write(data, home); } export function setDefault(id, { home = getKarajanHome() } = {}) { const data = readAccounts({ home }); data.default[find(data, id).provider] = id; write(data, home); } /** HU-4591: the pacing of an account; every field optional. */ export function setPace(id, pace, { home = getKarajanHome() } = {}) { const { gapSeconds, maxConcurrent, limitWaitSeconds } = pace ?? {}; for (const [name, value] of Object.entries({ gapSeconds, limitWaitSeconds })) { if (value !== undefined && !(Number.isFinite(value) && value >= 0)) throw new Error(`accounts: pace ${name} must be a number >= 0, got "${value}"`); } if (maxConcurrent !== undefined && !(Number.isInteger(maxConcurrent) && maxConcurrent >= 1)) { throw new Error(`accounts: pace maxConcurrent must be an integer >= 1, got "${maxConcurrent}"`); } const data = readAccounts({ home }); find(data, id).pace = { gapSeconds, maxConcurrent, limitWaitSeconds }; write(data, home); } /** The env that moves an agent to its account; {} when none. */ export function accountEnv(agent, options) { return accountFor(agent, options).env; } /** HU-4751: the accounts to try for a provider, in order: the project's, else the default alone, else []. */ export function accountsFor(agent, { home = getKarajanHome(), projectKey } = {}) { const name = ACCOUNT_VAR[agent]; if (!name) return []; const data = readAccounts({ home }); const order = data.projects[projectKey]?.[agent]; const ids = order?.length ? order : [data.default[agent]].filter(Boolean); return ids.map((id) => { const account = data.accounts.find((a) => a.id === id); if (!account) throw new Error(`accounts: project "${path.basename(projectKey)}" orders the account "${id}", which no longer exists`); return { id, env: { [name]: account.dir }, pace: account.pace }; }); } /** * The account an agent runs on: the first of its accounts not exhausted at `now`. When every one is out, * the first plus `allExhausted: [{ id, until }]`. id null when none. */ export function accountFor(agent, { home = getKarajanHome(), projectKey, now = new Date() } = {}) { const list = accountsFor(agent, { home, projectKey }); if (list.length === 0) return { id: null, env: {}, pace: undefined }; const untils = list.map((a) => exhaustedUntil(a.id, { home, now })); const free = untils.findIndex((until) => !until); if (free >= 0) return list[free]; return { ...list[0], allExhausted: list.map((a, i) => ({ id: a.id, until: untils[i] })) }; }