Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(accounts): main-process account service + IPC + preload API

enesa5be6f3b1466
diff --git a/src/main/agents/hooks/claude.ts b/src/main/agents/hooks/claude.ts
index b8237327f..0f87f97a9 100644
--- a/src/main/agents/hooks/claude.ts
+++ b/src/main/agents/hooks/claude.ts
@@ -34,6 +34,16 @@ export function installClaudeHooks(): void {
})
}
+/** Install the managed hook into a specific Claude config dir (managed accounts). */
+export function installClaudeHooksInto(configDir: string): void {
+ installHooksInto({
+ agentId: 'claude',
+ scriptFileName: SCRIPT_FILE_NAME,
+ configPath: path.join(configDir, 'settings.json'),
+ events: CLAUDE_EVENTS
+ })
+}
+
export function removeClaudeHooks(): void {
removeHooksFrom({
configPath: configPath(),
diff --git a/src/main/claude-accounts.ts b/src/main/claude-accounts.ts
new file mode 100644
index 000000000..af9f38a31
--- /dev/null
+++ b/src/main/claude-accounts.ts
@@ -0,0 +1,79 @@
+// Impure lifecycle for managed Claude accounts: config-dir creation/deletion, login
+// capture (poll .claude.json), CLI version check, hook install. The account LIST lives in
+// settings.json (renderer-owned via useSettings); this module only owns the filesystem.
+import { randomUUID } from 'crypto'
+import { promises as fs } from 'fs'
+import path from 'path'
+import { execFile } from 'child_process'
+import { promisify } from 'util'
+import { app, ipcMain } from 'electron'
+import { IPC } from '../shared/ipc'
+import { accountConfigDir, isSupportedClaudeVersion, parseLoginCapture } from './claude-accounts-core'
+import { installClaudeHooksInto } from './agents/hooks/claude'
+import { findInLoginPath } from './pty-manager'
+
+const execFileP = promisify(execFile)
+const LOGIN_POLL_MS = 2000
+const LOGIN_TIMEOUT_MS = 5 * 60 * 1000
+
+export function claudeConfigDirFor(accountId: string): string {
+ return accountConfigDir(app.getPath('userData'), accountId)
+}
+
+const waiters = new Map<string, { cancelled: boolean }>()
+
+async function checkClaudeVersion(): Promise<boolean> {
+ try {
+ const claude = await findInLoginPath('claude')
+ if (!claude) return false
+ const { stdout } = await execFileP(claude, ['--version'], { timeout: 5000 })
+ return isSupportedClaudeVersion(stdout.trim())
+ } catch {
+ return false
+ }
+}
+
+export function initClaudeAccounts(): void {
+ ipcMain.handle(IPC.claudeAccountsAdd, async () => {
+ const id = randomUUID()
+ const configDir = claudeConfigDirFor(id)
+ await fs.mkdir(configDir, { recursive: true })
+ // Install the managed hook up front so the very first session in this account
+ // already reports status (badges/notifications/subagent viz).
+ installClaudeHooksInto(configDir)
+ const versionSupported = await checkClaudeVersion()
+ return { id, configDir, versionSupported }
+ })
+
+ ipcMain.handle(IPC.claudeAccountsWaitLogin, async (_e, id: string) => {
+ const configDir = claudeConfigDirFor(id) // validates the id shape
+ const w = { cancelled: false }
+ waiters.set(id, w)
+ const deadline = Date.now() + LOGIN_TIMEOUT_MS
+ try {
+ while (!w.cancelled && Date.now() < deadline) {
+ try {
+ const raw = await fs.readFile(path.join(configDir, '.claude.json'), 'utf-8')
+ const captured = parseLoginCapture(raw)
+ if (captured) return captured
+ } catch {
+ // not written yet — keep polling
+ }
+ await new Promise((r) => setTimeout(r, LOGIN_POLL_MS))
+ }
+ return null
+ } finally {
+ waiters.delete(id)
+ }
+ })
+
+ ipcMain.handle(IPC.claudeAccountsCancelWait, (_e, id: string) => {
+ const w = waiters.get(id)
+ if (w) w.cancelled = true
+ })
+
+ ipcMain.handle(IPC.claudeAccountsRemove, async (_e, id: string) => {
+ const configDir = claudeConfigDirFor(id) // id validation prevents traversal
+ await fs.rm(configDir, { recursive: true, force: true })
+ })
+}
diff --git a/src/main/index.ts b/src/main/index.ts
index 215be3c3a..27e3f3dcf 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -42,6 +42,7 @@ import { initTranscriptIndex, searchTranscripts } from './transcript-index'
import { initTelemetry } from './telemetry'
import { initClaudeUsage } from './claude-usage'
import { initLicense } from './license'
+import { initClaudeAccounts } from './claude-accounts'
import { initRemoteHost } from './remote/host-service'
import { initRemoteClient } from './remote/client-service'
import { initSshProject } from './remote-ssh/ssh-project'
@@ -734,6 +735,7 @@ app.whenReady().then(async () => {
initClaudeUsage(win)
initTelemetry(() => settingsStore.get())
initLicense(win)
+ initClaudeAccounts()
initRemoteHost(win, ptyManager)
initRemoteClient(win, { isPackaged: app.isPackaged })
sshProjectManager = initSshProject(win)
diff --git a/src/main/pty-manager.ts b/src/main/pty-manager.ts
index 27536845b..998ded85f 100644
--- a/src/main/pty-manager.ts
+++ b/src/main/pty-manager.ts
@@ -151,6 +151,27 @@ function resolveShellPath(): Promise<string | null> {
return shellPathPromise
}
+/**
+ * Resolve an executable against the user's real login-shell PATH (reusing the cached probe),
+ * returning its absolute path or null. GUI apps inherit only a minimal PATH, so a bare
+ * `execFile('claude', …)` would fail even when the tool is installed — this walks the resolved
+ * PATH entries and returns the first `bin` that exists and is accessible.
+ */
+export async function findInLoginPath(bin: string): Promise<string | null> {
+ const shellPath = (await resolveShellPath()) ?? process.env.PATH ?? ''
+ for (const dir of shellPath.split(path.delimiter)) {
+ if (!dir) continue
+ const candidate = path.join(dir, bin)
+ try {
+ await fs.promises.access(candidate, fs.constants.X_OK)
+ return candidate
+ } catch {
+ // not here — keep looking
+ }
+ }
+ return null
+}
+
interface Session {
proc: pty.IPty
/** Renderer webContents to stream output to over IPC, or null for a detached (host) session. */
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 92ccbbc34..6fe31219a 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -274,6 +274,12 @@ const api: NodeTerminalApi = {
return () => ipcRenderer.removeListener(ch, handler)
}
},
+ claudeAccounts: {
+ add: () => ipcRenderer.invoke(IPC.claudeAccountsAdd),
+ waitLogin: (id) => ipcRenderer.invoke(IPC.claudeAccountsWaitLogin, id),
+ cancelWaitLogin: (id) => ipcRenderer.invoke(IPC.claudeAccountsCancelWait, id),
+ remove: (id) => ipcRenderer.invoke(IPC.claudeAccountsRemove, id)
+ },
transcripts: {
search: (query: string) => ipcRenderer.invoke(IPC.transcriptSearch, query)
},
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index b3c8d861b..c99d51522 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -22,6 +22,10 @@ export const IPC = {
chatRemoveQueued: 'chat:remove-queued',
chatDispose: 'chat:dispose',
chatEvent: (nodeId: string) => `chat:event:${nodeId}`,
+ claudeAccountsAdd: 'claude-accounts:add',
+ claudeAccountsWaitLogin: 'claude-accounts:wait-login',
+ claudeAccountsCancelWait: 'claude-accounts:cancel-wait',
+ claudeAccountsRemove: 'claude-accounts:remove',
transcriptSearch: 'transcript:search',
appToggleMarkdown: 'app:toggle-markdown',
appCloseNode: 'app:close-node',
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 03048ffdc..4d32e0c93 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -770,6 +770,17 @@ export interface ChatApi {
onEvent(nodeId: string, listener: (e: ChatEvent) => void): () => void
}
+export interface ClaudeAccountsApi {
+ /** Mint a new managed account: create its config dir, install the hook, check the CLI version. */
+ add(): Promise<{ id: string; configDir: string; versionSupported: boolean }>
+ /** Poll the account's `.claude.json` for a completed login; null on timeout/cancel. */
+ waitLogin(id: string): Promise<{ email: string } | null>
+ /** Cancel an in-flight `waitLogin` for this account. */
+ cancelWaitLogin(id: string): Promise<void>
+ /** Delete a managed account's config dir (recursive). */
+ remove(id: string): Promise<void>
+}
+
/** One ranked search hit across all on-disk Claude session transcripts. */
export interface TranscriptHit {
sessionId: string
@@ -943,6 +954,7 @@ export interface NodeTerminalApi {
context: ContextApi
claude: ClaudeApi
chat: ChatApi
+ claudeAccounts: ClaudeAccountsApi
transcripts: TranscriptsApi
remoteHost: RemoteHostApi
remoteClient: RemoteClientApi