@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
46 lines • 2.38 kB
JavaScript
import { assertValidAccountName, credentialPoolRepository, getCredentialAccounts, summarizeCredentialAccounts, } from "./credential-accounts.js";
import { emitProviderAccountsChanged } from "./extensions/builtin/anthropic-subscription/account-events.js";
function storedOrder(credential) {
return credential?.order ?? [];
}
export async function listCredentialAccounts(storage, provider, env = process.env, repository, activeName) {
const accounts = await getCredentialAccounts(storage, provider, env, repository, activeName);
const order = storedOrder(storage.get(provider));
return { accounts, ordered: accounts.some((account) => order.includes(account.name)) };
}
function assertOrderNames(names, known) {
if (names.length === 0)
throw new Error("Account order must name at least one account");
const seen = new Set();
for (const name of names) {
assertValidAccountName(name);
if (seen.has(name))
throw new Error(`Duplicate provider account in order: ${name}`);
seen.add(name);
if (!known.some((account) => account.name === name))
throw new Error(`Provider account not found: ${name}`);
}
}
function withOrder(credential, names) {
const { order: _previous, ...rest } = credential;
return names === null ? rest : { ...rest, order: [...names] };
}
/**
* Sets the provider's account order, or clears it when `names` is null (senpi#3591).
* The order lives beside the pin in the stored credential, so it has the pin's scope:
* a provider with nothing stored has no order to keep.
*/
export async function orderCredentialAccounts(storage, provider, names, env = process.env, repository) {
const repo = repository ?? credentialPoolRepository(storage);
await storage.modify(provider, async (current) => {
if (current === undefined)
throw new Error(`No stored credential for provider: ${provider}`);
// Validated against the credential this write replaces, under the store's lock,
// so an account removed concurrently cannot leave its name in the order.
if (names !== null)
assertOrderNames(names, await summarizeCredentialAccounts(provider, current, env, repo));
return withOrder(current, names);
});
emitProviderAccountsChanged(provider);
}
//# sourceMappingURL=credential-account-order.js.map