paseo-plugin-account-switcher 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "paseo-plugin-account-switcher",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "dutchakdev",
@@ -40,16 +40,16 @@
40
40
  "@getpaseo/client": "0.11.4",
41
41
  "@getpaseo/plugin": "0.11.4",
42
42
  "@getpaseo/protocol": "0.11.4",
43
- "@tanstack/react-query": "^5.90.11",
43
+ "@tanstack/react-query": "^5.107.1",
44
44
  "@types/node": "^22.0.0",
45
45
  "@types/react": "~19.2.0",
46
46
  "esbuild": "^0.28.2",
47
47
  "react": "19.1.0",
48
48
  "react-native": "0.81.5",
49
- "semver": "^7.8.5",
49
+ "semver": "^7.8.6",
50
50
  "typescript": "^7.0.2",
51
- "vite": "^8.3.5",
52
- "vitest": "^5.0.5",
51
+ "vite": "^8.3.6",
52
+ "vitest": "^5.1.0",
53
53
  "zod": "^4.4.3"
54
54
  },
55
55
  "repository": {
package/server/auth.ts CHANGED
@@ -2,11 +2,14 @@ import { execFile } from "node:child_process";
2
2
  import { lstat, mkdir, readFile, realpath } from "node:fs/promises";
3
3
  import { createHash } from "node:crypto";
4
4
  import { basename, dirname, isAbsolute, join, resolve } from "node:path";
5
+ import { homedir } from "node:os";
5
6
  import type { PaseoApi } from "./paseo-api";
6
7
  import { missingCli, type Provider } from "../shared/contracts";
7
8
  import { accountFrom, type AccountStore, type Registry, type StoredAccount } from "./store";
8
9
  import { assertManagedCommand, buildAccountEnv, prepareProfile } from "./profiles";
9
10
  import { launcherPath, writeLaunchers } from "./integration";
11
+ import { readClaudeCredentials } from "./usage/claude";
12
+ import { UsageError } from "./usage";
10
13
 
11
14
  export function accountCommand(registry: Registry, provider: Provider): string[] {
12
15
  if (!registry.available[provider]) throw new Error(missingCli(provider));
@@ -51,6 +54,18 @@ const string = (value: unknown): string | null => typeof value === "string" && v
51
54
  const identity = (provider: Provider, fields: Array<string | null>): string | null => fields.some(Boolean)
52
55
  ? `${provider}:${createHash("sha256").update(JSON.stringify(fields)).digest("hex")}` : null;
53
56
 
57
+ async function negativeSignIn(account: StoredAccount, registry: Registry, env: NodeJS.ProcessEnv): Promise<Identity> {
58
+ if (account.provider === "claude") {
59
+ // A negative CLI probe also occurs when macOS denies access to Keychain.
60
+ try {
61
+ await readClaudeCredentials({ id: account.id, provider: "claude", home: (env.CLAUDE_SECURESTORAGE_CONFIG_DIR ?? env.CLAUDE_CONFIG_DIR) || join(env.HOME || homedir(), ".claude"), command: accountCommand(registry, "claude"), env, identityKey: account.identityKey, generation: account.generation });
62
+ } catch (error) {
63
+ if (error instanceof UsageError && error.status === "unavailable") return empty("error");
64
+ }
65
+ }
66
+ return empty("needs_auth");
67
+ }
68
+
54
69
  async function status(command: string[], args: string[], env: NodeJS.ProcessEnv): Promise<{ code: number | null; stdout: string; stderr: string; failed: boolean }> {
55
70
  return new Promise(resolve => {
56
71
  execFile(command[0], [...command.slice(1), ...args], { env, timeout: 20_000, maxBuffer: 256 * 1024, encoding: "utf8", windowsHide: true }, (error, stdout, stderr) => {
@@ -74,13 +89,14 @@ export async function probeAccount(account: StoredAccount, registry: Registry):
74
89
  await assertManagedCommand(account.provider, accountCommand(registry, account.provider).slice(1));
75
90
  await prepareProfile({ provider: account.provider, home: account.home, sourceHome: registry.sourceHomes[account.provider], sourceEnvironment: registry.sourceEnvironment[account.provider] });
76
91
  }
77
- const result = await status(accountCommand(registry, account.provider), account.provider === "claude" ? ["auth", "status", "--json"] : ["login", "status"], accountEnvironment(account, registry));
92
+ const env = accountEnvironment(account, registry);
93
+ const result = await status(accountCommand(registry, account.provider), account.provider === "claude" ? ["auth", "status", "--json"] : ["login", "status"], env);
78
94
  if (result.failed) return empty("error");
79
- if (result.code !== 0) return empty("needs_auth");
95
+ if (result.code !== 0) return negativeSignIn(account, registry, env);
80
96
  if (account.provider === "claude") {
81
97
  // Only the official status JSON is parsed; stderr is never exposed to callers.
82
98
  const value = JSON.parse(result.stdout.trim());
83
- if (value.loggedIn !== true) return empty("needs_auth");
99
+ if (value.loggedIn !== true) return negativeSignIn(account, registry, env);
84
100
  if (value.authMethod !== "claude.ai" && value.authMethod !== "oauth_token") return empty("unknown");
85
101
  const email = string(value.email), orgId = string(value.orgId);
86
102
  return { identityKey: identity("claude", [email, orgId]), email, plan: string(value.subscriptionType), authStatus: "ready" };
@@ -13,7 +13,7 @@ import { installIntegration, restoreIntegration, writeLaunchers, prepareRuntime,
13
13
  import { SwitchRefusal, SwitchService } from "./switching";
14
14
  import { createUsageService, readCredentialVersion, usageChecksEnabled, type AccountReadOptions, type QuotaAccount } from "./usage";
15
15
  import { renewExpiredClaudeToken } from "./usage/claude";
16
- import { LoginSessions, type LoginDependencies } from "./login";
16
+ import { LoginRefusal, LoginSessions, type LoginDependencies } from "./login";
17
17
  import { receiptProcessRuns } from "./process-identity";
18
18
 
19
19
  const execFileAsync=promisify(execFile);
@@ -251,7 +251,7 @@ export class AccountController {
251
251
  if(account.loginToken)throw new Error("Sign-in is already open. Continue or cancel the current attempt.");
252
252
  if(account.source!=="managed")throw new Error("Add a separate account for isolated sign-in.");
253
253
  return await this.logins.start(account,registry,this.store.root,token,mode,async()=>{
254
- await this.store.change(state=>{const current=accountFrom(state,id);if(current.loginToken)throw new Error("Sign-in is already open. Continue or cancel the current attempt.");if(state.bindings.some(b=>b.currentAccountId===id||b.launchAccountId===id||b.currentAccountId===null&&b.pendingAccountId===id))throw new Error("Switch agents away from this account before signing in again.");current.loginToken=token;current.login={sessionId:token,mode:account.provider==="claude"?"browser":mode};});
254
+ await this.store.change(state=>{const current=accountFrom(state,id);if(current.loginToken)throw new LoginRefusal("Sign-in is already open. Continue or cancel the current attempt.");if(state.bindings.some(b=>b.currentAccountId===id||b.launchAccountId===id||b.currentAccountId===null&&b.pendingAccountId===id))throw new LoginRefusal("Switch agents away from this account before signing in again.");current.loginToken=token;current.login={sessionId:token,mode:account.provider==="claude"?"browser":mode};});
255
255
  this.usage.invalidate(id);
256
256
  await this.checks.get(id)?.catch(()=>{});
257
257
  });
@@ -13,8 +13,30 @@ const providers = ["claude", "codex"] as const;
13
13
  const COMMAND_DRIFT = "Provider commands changed outside the plugin. Enable integration again to apply accounts.";
14
14
  export const NO_PROVIDER_CLI = "Provider CLI not found. Install Claude Code or Codex, or configure its command in Paseo.";
15
15
  export const launcherPath = (root: string, provider: Provider): string => join(root, "bin", `${provider}-launcher`);
16
+ /** The launcher bundle that every provider's launcher runs. */
17
+ export const launcherScriptPath = (root: string): string => join(root, "bin", "launcher.js");
18
+
19
+ const shellQuote = (value: string): string => `'${value.replace(/'/g, `'\\''`)}'`;
20
+
21
+ /**
22
+ * A provider's launcher: a shell script that runs the bundle with the Node that runs this plugin, and names the
23
+ * provider. The bundle used to be the launcher itself, behind `#!/usr/bin/env node`, which takes the first `node` on the
24
+ * agent's PATH; a project that pins an old Node through mise, asdf or nvm put that one first, and it could not parse the
25
+ * bundle (#58). Under the desktop app the plugin's Node is Paseo's Electron, which acts as Node only with
26
+ * ELECTRON_RUN_AS_NODE; the launcher drops the variable again before it starts the provider CLI.
27
+ */
28
+ export function launcherWrapper(node: string, script: string, provider: Provider, electron: boolean): string {
29
+ const lines = ["#!/bin/sh", `# Account Switcher ${provider} launcher, written by the plugin at every start. Do not edit.`];
30
+ if (electron) lines.push("ELECTRON_RUN_AS_NODE=1; ACCOUNT_SWITCHER_ELECTRON=1; export ELECTRON_RUN_AS_NODE ACCOUNT_SWITCHER_ELECTRON");
31
+ lines.push(`exec ${shellQuote(node)} ${shellQuote(script)} ${provider} "$@"`);
32
+ return `${lines.join("\n")}\n`;
33
+ }
34
+
16
35
  export async function writeLaunchers(store: AccountStore): Promise<void> {
17
- for (const provider of providers) await atomicWrite(launcherPath(store.root, provider), LAUNCHER_SOURCE, 0o700);
36
+ const script = launcherScriptPath(store.root);
37
+ await atomicWrite(script, LAUNCHER_SOURCE, 0o600);
38
+ const electron = typeof process.versions.electron === "string";
39
+ for (const provider of providers) await atomicWrite(launcherPath(store.root, provider), launcherWrapper(process.execPath, script, provider, electron), 0o700);
18
40
  }
19
41
 
20
42
  function commandArray(value: unknown): string[] | null {
@@ -90,7 +112,8 @@ async function discoverCommands(paseo: PaseoApi, store: AccountStore) {
90
112
 
91
113
  async function discoverRuntime(paseo: PaseoApi, store: AccountStore) {
92
114
  const { config } = await paseo.config.get(); const registry = await store.read();
93
- const wrappers = providers.map(provider => launcherPath(store.root, provider));
115
+ // The shared bundle counts as a launcher too: `node bin/launcher.js codex` saved as the original command would recurse.
116
+ const wrappers = [...providers.map(provider => launcherPath(store.root, provider)), launcherScriptPath(store.root)];
94
117
  const commands = { ...registry.commands }, originals = { ...registry.originalCommands }, sourceHomes = { ...registry.sourceHomes };
95
118
  const sourceEnvironment: typeof registry.sourceEnvironment = { claude: {}, codex: {} };
96
119
  const available = { claude: false, codex: false };