UNPKG

@code-yeongyu/senpi

Version:

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

71 lines • 3.71 kB
import { getAgentDir } from "../../../../config.js"; import { SettingsManager } from "../../../settings-manager.js"; import { ANTHROPIC_SUBSCRIPTION_PROVIDER_ID } from "./account-management.js"; import { activeBoundary } from "./auth-lane-boundary.js"; import { connectionTestModel, runClaudeCliConnectionTest, } from "./claude-cli-connection-test.js"; import { defaultExecutableDeps, resolveClaudeCodeExecutable } from "./executable.js"; import { loadAnthropicSubscriptionProviderSettingsFromDisk } from "./settings.js"; /** A refusal before any turn runs; `message` is the bare code the RPC error carries. */ export class ProviderAccountTestError extends Error { constructor(code) { super(code); this.name = "ProviderAccountTestError"; this.code = code; } } export function claudeCliDefaultModel(cwd) { const settings = SettingsManager.create(cwd, getAgentDir()); return { provider: settings.getDefaultProvider(), model: settings.getDefaultModel() }; } function subscription(value) { return typeof value === "object" && value !== null && value.type === "oauth" ? value : undefined; } function checkedSlot(current, check) { return current.store === "claude-cli" ? { ...current, check } : current; } /** * Re-runs the sign-in connection test (one pinned "Reply with OK" turn, account fallback off) on one stored Claude * CLI account and stores its outcome as `check`. The turn is a probe (`queryWithAuthLane` `probe`): it runs on that * account even while it is blocked, recovers no other account, and stores or clears no block. It writes `check`, * applied to the slot as it is under the lock (blocks other sessions wrote meanwhile stay), plus the slot's `profile` * when the CLI reports a changed one (`claudeCliInitializationCheck` in auth-lane.ts, as on every turn). An account * that must sign in again is refused before any turn: no test can pass until it signs in. */ export async function testClaudeCliAccount(input) { if (input.provider !== ANTHROPIC_SUBSCRIPTION_PROVIDER_ID) throw new ProviderAccountTestError("unsupported_provider"); const store = input.store; const before = subscription(await store.read(ANTHROPIC_SUBSCRIPTION_PROVIDER_ID))?.accounts?.find((slot) => slot.name === input.name); if (before === undefined) throw new ProviderAccountTestError("unknown_account"); if (before.store !== "claude-cli") throw new ProviderAccountTestError("unsupported_account"); if (before.signInRequired !== undefined) throw new ProviderAccountTestError("needs_sign_in"); const env = activeBoundary.env(); const model = connectionTestModel(claudeCliDefaultModel(input.cwd)); const check = await (input.connectionTest ?? runClaudeCliConnectionTest)({ name: input.name, model, executable: resolveClaudeCodeExecutable({ ...defaultExecutableDeps(), env: (key) => env[key] }), cwd: input.cwd, providerSettings: loadAnthropicSubscriptionProviderSettingsFromDisk(input.cwd), ...(input.signal === undefined ? {} : { signal: input.signal }), now: activeBoundary.now, }); await store.modify(ANTHROPIC_SUBSCRIPTION_PROVIDER_ID, async (stored) => { const credential = subscription(stored); if (credential === undefined) return stored; return { ...credential, accounts: (credential.accounts ?? []).map((slot) => slot.name === input.name && slot.store === "claude-cli" && slot.dirId === before.dirId ? checkedSlot(slot, check) : slot), }; }); return check; } //# sourceMappingURL=claude-cli-account-test.js.map