@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
60 lines • 2.56 kB
JavaScript
import { listSlots } from "@earendil-works/pi-ai/auth/pool/slots";
import { emptyCredential, envSlotToken, isEnvSlot, listAccounts, } from "../extensions/builtin/anthropic-subscription/accounts.js";
import { accountDirFor, isValidAccountDirId, readClaudeCliAccessToken, } from "../extensions/builtin/anthropic-subscription/claude-cli-account-dir.js";
export const SIGN_IN_NEEDED = "auth stale - run /login";
export async function listPollAccounts(sources) {
const [claude, chatgpt] = await Promise.all([
sources.store.read("anthropic-subscription"),
sources.store.read("chatgpt-subscription"),
]);
return [...claudeAccounts(claude, sources), ...chatgptAccounts(chatgpt, sources.now)];
}
function claudeAccounts(stored, sources) {
const credential = stored?.type === "oauth" ? stored : emptyCredential();
return listAccounts(credential, sources.env).flatMap((slot) => {
if (isEnvSlot(slot)) {
return [
{
provider: "anthropic-subscription",
account: slot.name,
async access() {
const token = envSlotToken(sources.env, slot.name);
return token === undefined ? undefined : { access: token };
},
},
];
}
const agentDir = sources.agentDir;
if (agentDir === undefined || slot.signInRequired !== undefined || !isValidAccountDirId(slot.dirId))
return [];
const accountDir = accountDirFor(agentDir, slot.dirId);
return [
{
provider: "anthropic-subscription",
account: slot.name,
async access() {
const token = await readClaudeCliAccessToken(accountDir, sources.now());
return token === undefined ? undefined : { access: token };
},
},
];
});
}
function chatgptAccounts(stored, now) {
if (stored?.type !== "oauth")
return [];
return listSlots(stored).flatMap((slot) => slot.access === undefined || slot.access === ""
? []
: [
{
provider: "chatgpt-subscription",
account: slot.name,
async access() {
return slot.expires !== undefined && now() >= slot.expires
? undefined
: { access: slot.access ?? "" };
},
},
]);
}
//# sourceMappingURL=usage-accounts.js.map