diff --git a/apps/ide/README.md b/apps/ide/README.md index 3e538076..bf74f69d 100644 --- a/apps/ide/README.md +++ b/apps/ide/README.md @@ -389,7 +389,7 @@ The IDE opens in the **Agent** layout: chats with the person's own coding agents - **Metric delta per turn (AF9, #274):** when the window has a project scene open, its metrics are read before each message and compared after the turn, once the scene has caught up with the files the turn changed; a short line under the turn ("Complexity max 9 -> 1, LOC -6") unfolds into the per-file detail. Without a scene, or when nothing moved, nothing is shown. - **Installing agents (AF2, #267):** a missing preset shows *Install*, one the IDE installed shows its version with *Update* and *Remove* (Agents section of the Chats view, and the first-run guide). *Install* reads the npm registry, only then, and shows inline what would be installed: package and version, licence, unpacked size plus the dependencies' size for this platform, repository, publication date, the platform-specific native binaries npm will pick (Claude Code's SDK and Codex each bring one of roughly 400 MB) and which packages run install scripts. A native dialog in the main process, which the page cannot answer, repeats it with the exact npm command; only that starts the download. `src/main/agentSessions/agentInstaller.ts` (Electron-free, tested against a local fake registry and a fake npm) runs the **system npm** (`npm install --prefix /agent-packages --no-audit --no-fund --omit=dev --save-exact --ignore-scripts=false @`, Windows through the `node.exe` + `npm-cli.js` beside `npm.cmd`, never a shell) into a private package.json: nothing global, the person's own npm configuration, proxy and CA apply, and its output streams to a progress notification. Why npm rather than an own downloader: only npm resolves the dependency tree, optional platform packages and lifecycle scripts faithfully, and bundling it would add a large dependency; a missing npm gets a message pointing to nodejs.org. Once installed, the adapter runs on the IDE's own Electron as Node, so chats need no system Node. The registry is read through Electron's `net.fetch` (system proxy settings); the smoke run has no installer and checks that the call is refused. - **First run (AF13, #278):** until the person skips it or has a chat, the editor area's welcome view carries a three-step guide (open a folder, pick an agent and install it or sign in, start the first chat), each step drawn from the window's real state and redrawn as it changes (`gettingStartedView.ts`, its model in `agentSetupModel.ts`). *Skip* or the first chat sets `codexr.agentFirst.onboarded` and it never opens on its own again; *Chat: Show Getting Started* reopens it as a tab (`guide:getting-started`). -- **Signing in (AF14, #297):** a terminal sign-in method runs the agent's login program directly, with no shell, in a terminal tab of its own (`TerminalHost.runProgram`, shown through `TerminalPanel.runTask`), so nothing is typed into the person's shell; the agent is checked again when it ends. *Check* asks the agent who is signed in with `auth/status` (an ACP draft that Claude Code's and Codex's adapters answer) and reports *Needs login* or *Signed in as ...*, because Claude Code opens sessions while signed out and only refuses the first prompt. Agents that cannot say are judged by `session/new`. +- **Signing in (AF14, #297):** a terminal sign-in method runs the agent's login program directly, with no shell, in a terminal tab of its own (`TerminalHost.runProgram`, shown through `TerminalPanel.runTask`), so nothing is typed into the person's shell; the agent is checked again when it ends. For Claude Code the login hint points at the adapter script with `--cli`; the IDE runs the native `claude` binary that ships beside the adapter (`nativeLoginProgram`, or `CLAUDE_CODE_EXECUTABLE`) instead, because on Windows a console program started through the IDE's own GUI executable gets no keyboard or output from the terminal. *Check* asks the agent who is signed in with `auth/status` (an ACP draft that Claude Code's and Codex's adapters answer) and reports *Needs login* or *Signed in as ...*, because Claude Code opens sessions while signed out and only refuses the first prompt. Agents that cannot say are judged by `session/new`. - **Importing an agent's chats (AF15, #298):** *Import chats* on a ready agent lists the chats it keeps for the open folder (`session/list`, newest first, at most 200, none already in the IDE) and adds the chosen ones; each reads its messages from the agent's `session/load` replay the first time it opens (`historyPending`), then continues the same agent session. The agent reads its own history files; the IDE never opens them. ## VS Code extensions (stream `ide-extensions`, epic #251) diff --git a/apps/ide/src/main/agentSessions/agentCatalog.ts b/apps/ide/src/main/agentSessions/agentCatalog.ts index 483adca4..1ba3c67e 100644 --- a/apps/ide/src/main/agentSessions/agentCatalog.ts +++ b/apps/ide/src/main/agentSessions/agentCatalog.ts @@ -255,6 +255,52 @@ export function loginProgram(launch: AgentLaunch, spec: Pick>; readonly exists: (file: string) => Promise }, +): Promise<{ command: string; args: string[] }> { + const cliIndex = program.args.indexOf('--cli'); + const script = program.args[0]; + if (cliIndex < 1 || !script || !/\.[cm]?js$/i.test(script)) { + return program; + } + const api = options.platform === 'win32' ? path.win32 : path.posix; + const cliArgs = program.args.slice(cliIndex + 1).filter((arg) => arg !== '--hide-claude-auth'); + const override = options.env['CLAUDE_CODE_EXECUTABLE']; + if (override && (await options.exists(override))) { + return { command: override, args: cliArgs }; + } + const ext = options.platform === 'win32' ? '.exe' : ''; + const names = options.platform === 'linux' + ? [`claude-agent-sdk-linux-${options.arch}`, `claude-agent-sdk-linux-${options.arch}-musl`] + : [`claude-agent-sdk-${options.platform}-${options.arch}`]; + // From the adapter's folder up, as Node resolves packages. + let dir = api.dirname(script); + for (let depth = 0; depth < 12; depth += 1) { + for (const name of names) { + const candidate = api.join(dir, 'node_modules', '@anthropic-ai', name, `claude${ext}`); + if (await options.exists(candidate)) { + return { command: candidate, args: cliArgs }; + } + } + const parent = api.dirname(dir); + if (parent === dir) { + break; + } + dir = parent; + } + return program; +} + export class AgentCatalog { private readonly options: CatalogOptions; private readonly probe: FileProbe; diff --git a/apps/ide/src/main/agentSessions/agentSessionService.ts b/apps/ide/src/main/agentSessions/agentSessionService.ts index 9a505b32..8faf7620 100644 --- a/apps/ide/src/main/agentSessions/agentSessionService.ts +++ b/apps/ide/src/main/agentSessions/agentSessionService.ts @@ -58,7 +58,7 @@ import { type MappingContext, type ResolvedAttachment, } from './acpMapping.ts'; -import { loginProgram, type AgentCatalog } from './agentCatalog.ts'; +import { loginProgram, nativeLoginProgram, type AgentCatalog } from './agentCatalog.ts'; import { parsePresetId, type AgentInstaller } from './agentInstaller.ts'; import { AgentTerminals } from './agentTerminals.ts'; import { isSessionId, type SessionStore } from './sessionStore.ts'; @@ -375,7 +375,12 @@ export class AgentSessionService { } if (spec.view.type === 'terminal') { const launch = resolution.launch; - const program = loginProgram(launch, spec); + const program = await nativeLoginProgram(loginProgram(launch, spec), { + platform: this.platform, + arch: process.arch, + env: launch.env, + exists: (file) => fs.promises.stat(file).then((stat) => stat.isFile(), () => false), + }); const cwd = this.options.rootOf(window) ?? this.options.homeDir; const size = (value: unknown, fallback: number): number => (typeof value === 'number' && Number.isInteger(value) && value >= 2 && value <= 1000 ? value : fallback); const opened = await this.options.openTerminal(window, { diff --git a/apps/ide/test/agentLoginImport.test.ts b/apps/ide/test/agentLoginImport.test.ts index 54d20076..8d8a8104 100644 --- a/apps/ide/test/agentLoginImport.test.ts +++ b/apps/ide/test/agentLoginImport.test.ts @@ -7,7 +7,7 @@ import { agent, methods, type AgentContext } from '@agentclientprotocol/sdk'; import { applySessionEvent, emptySession, type AgentSessionMessage } from '../src/shared/agentSession.ts'; import { JsonKeyValueStore } from '../src/main/core/jsonStore.ts'; import { AcpConnection } from '../src/main/agentSessions/acpConnection.ts'; -import { AgentCatalog, loginProgram } from '../src/main/agentSessions/agentCatalog.ts'; +import { AgentCatalog, loginProgram, nativeLoginProgram } from '../src/main/agentSessions/agentCatalog.ts'; import { AgentSessionService, type WindowPort } from '../src/main/agentSessions/agentSessionService.ts'; import { SessionStore } from '../src/main/agentSessions/sessionStore.ts'; import { importedWhen, setupActions } from '../src/renderer/agentFirst/agentSetupModel.ts'; @@ -138,6 +138,33 @@ describe('agent sign-in (#297)', () => { assert.throws(() => loginProgram({ command: 'cmd.exe', args: ['/d', '/s', '/c', '"agent.cmd"'], env: {}, verbatimArguments: true }, { args: ['--login'] }), /yourself in a terminal/); }); + it('runs Claude Code\'s native CLI directly instead of the adapter script (#297, Windows console)', async () => { + const adapter = '/pk/node_modules/@agentclientprotocol/claude-agent-acp/dist/index.js'; + const hint = { command: '/ide/electron', args: [adapter, '--cli', 'auth', 'login', '--claudeai', '--hide-claude-auth'] }; + const linuxCli = '/pk/node_modules/@anthropic-ai/claude-agent-sdk-linux-x64/claude'; + const files = (...list: string[]) => async (file: string) => list.includes(file); + assert.deepEqual( + await nativeLoginProgram(hint, { platform: 'linux', arch: 'x64', env: {}, exists: files(linuxCli) }), + { command: linuxCli, args: ['auth', 'login', '--claudeai'] }, + ); + const winAdapter = 'C:\\pk\\node_modules\\@agentclientprotocol\\claude-agent-acp\\dist\\index.js'; + const winCli = 'C:\\pk\\node_modules\\@anthropic-ai\\claude-agent-sdk-win32-x64\\claude.exe'; + assert.deepEqual( + await nativeLoginProgram({ command: 'C:\\ide\\Code-XR IDE.exe', args: [winAdapter, '--cli', 'auth', 'login', '--console'] }, { platform: 'win32', arch: 'x64', env: {}, exists: files(winCli) }), + { command: winCli, args: ['auth', 'login', '--console'] }, + ); + assert.deepEqual( + await nativeLoginProgram(hint, { platform: 'linux', arch: 'x64', env: { CLAUDE_CODE_EXECUTABLE: '/opt/claude' }, exists: files('/opt/claude', linuxCli) }), + { command: '/opt/claude', args: ['auth', 'login', '--claudeai'] }, + ); + // Not found, or not an adapter --cli hint: unchanged. + assert.deepEqual(await nativeLoginProgram(hint, { platform: 'linux', arch: 'x64', env: {}, exists: files() }), hint); + const other = { command: '/ide/electron', args: [adapter, '--login'] }; + assert.deepEqual(await nativeLoginProgram(other, { platform: 'linux', arch: 'x64', env: {}, exists: files(linuxCli) }), other); + const binary = { command: '/usr/bin/agent', args: ['--cli', 'login'] }; + assert.deepEqual(await nativeLoginProgram(binary, { platform: 'linux', arch: 'x64', env: {}, exists: files(linuxCli) }), binary); + }); + it('starts the terminal sign-in as a program, sized as the panel asked, with the agent\'s environment', async () => { const { service, window, terminals, ref, root } = await setup('login-terminal', { signedIn: false, history: [] }); await service.probeAgent(window, ref);