UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

53 lines • 2.35 kB
import { runClaudeCliToExit } from "./claude-cli-process.js"; function stringField(record, name) { const value = record[name]; return typeof value === "string" && value.length > 0 ? value : undefined; } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } /** * Reads `claude auth status --json` (2.1.306: `{loggedIn, authMethod, apiProvider, email, orgName, * subscriptionType, ...}`, exit 0 when logged in, 1 when not). Only a first-party claude.ai login with exit 0 is * signed in; an exit code and a `loggedIn` that disagree are unreadable, never trusted. */ export function parseClaudeCliAuthStatus(stdout, exitCode) { let parsed; try { parsed = JSON.parse(stdout); } catch { return { state: "unreadable" }; } if (!isRecord(parsed) || typeof parsed.loggedIn !== "boolean") return { state: "unreadable" }; if (!parsed.loggedIn) return exitCode === 0 ? { state: "unreadable" } : { state: "signed_out" }; if (exitCode !== 0) return { state: "unreadable" }; if (parsed.authMethod !== "claude.ai") return { state: "rejected", reason: "auth_method" }; if (parsed.apiProvider !== "firstParty") return { state: "rejected", reason: "api_provider" }; const email = stringField(parsed, "email"); const orgName = stringField(parsed, "orgName"); const subscriptionType = stringField(parsed, "subscriptionType"); return { state: "signed_in", profile: { ...(email === undefined ? {} : { email }), ...(orgName === undefined ? {} : { orgName }), ...(subscriptionType === undefined ? {} : { subscriptionType }), }, }; } export async function readClaudeCliAuthStatus(run, timeoutMs) { const exit = await runClaudeCliToExit(run, ["auth", "status", "--json"], timeoutMs); return exit === undefined ? { state: "unreadable" } : parseClaudeCliAuthStatus(exit.stdout, exit.exitCode); } /** `claude auth logout` for one account dir: deletes that dir's credential store entry. */ export async function logoutClaudeCli(run, timeoutMs, signal) { const exit = await runClaudeCliToExit(run, ["auth", "logout"], timeoutMs, signal); return exit?.exitCode === 0; } //# sourceMappingURL=claude-cli-auth-status.js.map