From 976c2ed517da1989cf6da7f5718c4d1f934429b7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Feb 2026 21:54:04 +0000 Subject: [PATCH] feat: remove Electron desktop app, clean up contracts and docs - Delete apps/desktop/ entirely (replaced by apps/server) - Remove IPC_CHANNELS constant from contracts (no longer needed) - Keep NativeApi interface (still the renderer's API contract) - Keep EDITORS constant (used by both server and renderer) - Update README.md with new architecture diagram and scripts - Update AGENTS.md with new package roles and architecture Co-authored-by: Theo Browne --- AGENTS.md | 17 +- README.md | 111 ++- apps/desktop/package.json | 31 - apps/desktop/scripts/dev-electron.mjs | 28 - apps/desktop/scripts/smoke-test.mjs | 84 -- .../desktop/src/codexAppServerManager.test.ts | 81 -- apps/desktop/src/codexAppServerManager.ts | 847 ------------------ apps/desktop/src/fixPath.ts | 18 - apps/desktop/src/ipcHelpers.test.ts | 104 --- apps/desktop/src/ipcHelpers.ts | 24 - apps/desktop/src/main.ts | 349 -------- apps/desktop/src/preload.ts | 55 -- apps/desktop/src/processManager.test.ts | 48 - apps/desktop/src/processManager.ts | 144 --- apps/desktop/src/providerManager.test.ts | 65 -- apps/desktop/src/providerManager.ts | 209 ----- apps/desktop/src/todoStore.ts | 106 --- apps/desktop/tsconfig.json | 9 - apps/desktop/tsup.config.ts | 10 - packages/contracts/src/ipc.ts | 22 - 20 files changed, 75 insertions(+), 2287 deletions(-) delete mode 100644 apps/desktop/package.json delete mode 100644 apps/desktop/scripts/dev-electron.mjs delete mode 100644 apps/desktop/scripts/smoke-test.mjs delete mode 100644 apps/desktop/src/codexAppServerManager.test.ts delete mode 100644 apps/desktop/src/codexAppServerManager.ts delete mode 100644 apps/desktop/src/fixPath.ts delete mode 100644 apps/desktop/src/ipcHelpers.test.ts delete mode 100644 apps/desktop/src/ipcHelpers.ts delete mode 100644 apps/desktop/src/main.ts delete mode 100644 apps/desktop/src/preload.ts delete mode 100644 apps/desktop/src/processManager.test.ts delete mode 100644 apps/desktop/src/processManager.ts delete mode 100644 apps/desktop/src/providerManager.test.ts delete mode 100644 apps/desktop/src/providerManager.ts delete mode 100644 apps/desktop/src/todoStore.ts delete mode 100644 apps/desktop/tsconfig.json delete mode 100644 apps/desktop/tsup.config.ts diff --git a/AGENTS.md b/AGENTS.md index 7c84e207ce..69b5400d9b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,7 +1,7 @@ # AGENTS.md ## Project Snapshot -CodeThing is a minimal GUI for using code agents like Codex and Claude Code (coming soon). +CodeThing is a minimal web GUI for using code agents like Codex and Claude Code (coming soon). This repository is a VERY EARLY WIP. Proposing sweeping changes that improve long-term maintainability is encouraged. @@ -13,17 +13,18 @@ This repository is a VERY EARLY WIP. Proposing sweeping changes that improve lon If a tradeoff is required, choose correctness and robustness over short-term convenience. ## Package Roles -- `apps/desktop`: Electron main/preload runtime. Owns provider orchestration, process/session lifecycle, and native IPC boundaries. -- `apps/renderer`: React/Vite UI. Owns session UX, conversation/event rendering, and client-side state. -- `packages/contracts`: Shared Zod schemas and TypeScript contracts for provider events, IPC payloads, and model/session types. +- `apps/server`: Node.js WebSocket server. Wraps Codex app-server (JSON-RPC over stdio), serves the React web app, and manages provider sessions. +- `apps/renderer`: React/Vite UI. Owns session UX, conversation/event rendering, and client-side state. Connects to the server via WebSocket. +- `packages/contracts`: Shared Zod schemas and TypeScript contracts for provider events, WebSocket protocol, and model/session types. ## Codex App Server (Important) -CodeThing is currently Codex-first. The desktop app starts `codex app-server` (JSON-RPC over stdio) per provider session, then streams structured events into the renderer through the provider APIs. +CodeThing is currently Codex-first. The server starts `codex app-server` (JSON-RPC over stdio) per provider session, then streams structured events to the browser through WebSocket push messages. How we use it in this codebase: -- Session startup/resume and turn lifecycle are brokered in `apps/desktop/src/codexAppServerManager.ts`. -- Provider dispatch and thread event logging are coordinated in `apps/desktop/src/providerManager.ts`. -- Renderer consumes provider event streams via `nativeApi.providers.onEvent`. +- Session startup/resume and turn lifecycle are brokered in `apps/server/src/codexAppServerManager.ts`. +- Provider dispatch and thread event logging are coordinated in `apps/server/src/providerManager.ts`. +- WebSocket server routes NativeApi methods in `apps/server/src/wsServer.ts`. +- Renderer consumes provider event streams via WebSocket push on channel `providers.event`. Docs: - Codex App Server docs: https://developers.openai.com/codex/sdk/#app-server diff --git a/README.md b/README.md index 567c2659a0..e939888eae 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,36 @@ -# CodeThing (Electron + Vite + Bun) +# CodeThing -CodeThing is a desktop shell for coding agents. This first implementation is: +CodeThing is a minimal web GUI for coding agents. Currently Codex-first, with Claude Code support coming soon. -1. Codex-first: connects to `codex app-server` and streams turn/item events. -2. Provider-ready: renderer speaks a provider abstraction so Claude Code can plug in later. -3. Typed end-to-end: contracts validate payloads at preload/main boundaries. +Run `npx t3` in any project directory to launch the web interface. + +## Architecture + +CodeThing runs as a **Node.js WebSocket server** that wraps `codex app-server` (JSON-RPC over stdio) and serves a React web app. + +``` +┌─────────────────────────────────┐ +│ Browser (React + Vite) │ +│ Connected via WebSocket │ +└──────────┬──────────────────────┘ + │ ws://localhost:3773 +┌──────────▼──────────────────────┐ +│ apps/server (Node.js) │ +│ WebSocket + HTTP static server │ +│ ProviderManager │ +│ CodexAppServerManager │ +└──────────┬──────────────────────┘ + │ JSON-RPC over stdio +┌──────────▼──────────────────────┐ +│ codex app-server │ +└─────────────────────────────────┘ +``` ## Workspace layout -- `/apps/desktop`: Electron main + preload process, includes provider and Codex session managers. -- `/apps/renderer`: React + Vite UI for session control, conversation, and protocol event stream. -- `/packages/contracts`: shared Zod schemas + TypeScript types for IPC and provider events. +- `/apps/server`: Node.js WebSocket server. Wraps Codex app-server, serves the built renderer, and opens the browser on start. +- `/apps/renderer`: React + Vite UI. Session control, conversation, and provider event rendering. Connects to the server via WebSocket. +- `/packages/contracts`: Shared Zod schemas and TypeScript contracts for provider events, WebSocket protocol, and model/session types. ## Codex prerequisites @@ -18,50 +38,51 @@ CodeThing is a desktop shell for coding agents. This first implementation is: - Authenticate Codex before running CodeThing (for example via API key or ChatGPT auth supported by Codex). - CodeThing starts the server via `codex app-server` per session. -## Security and boundary model +## Quick start -- `nodeIntegration: false` -- `contextIsolation: true` -- `sandbox: true` -- Renderer talks only to `window.nativeApi` exposed by preload. -- Preload and main both validate inputs using shared Zod schemas. +```bash +# Development (with hot reload) +bun run dev -`sandbox: true` above is Electron renderer sandboxing. It is separate from Codex execution sandbox policy (`read-only`, `workspace-write`, `danger-full-access`) used when starting provider sessions. +# Production +bun run build +bun run start -## Runtime modes - -CodeThing has a global runtime mode switch in the sidebar: - -- `Full access` (default): starts new sessions with `approvalPolicy: never` and `sandboxMode: danger-full-access`. -- `Approval required`: starts new sessions with `approvalPolicy: on-request` and `sandboxMode: workspace-write`, then prompts in-app for command/file approvals. - -Mode changes apply across all threads. Existing live sessions are restarted so old and new threads use the selected mode. +# Or from any project directory after publishing: +npx t3 +``` ## Scripts -- `bun run dev`: starts contract build/watch, renderer dev server, and Electron process. -- `bun run build`: builds contracts, renderer, and desktop bundles through Turbo. -- `bun run typecheck`: strict TypeScript checks for all packages. -- `bun run test`: runs workspace tests. +- `bun run dev` — Starts both the WebSocket server and Vite dev server in parallel with hot reload. +- `bun run dev:server` — Starts just the WebSocket server (uses tsx for TS execution). +- `bun run dev:web` — Starts just the Vite dev server for the renderer. +- `bun run start` — Runs the production server (serves built renderer as static files). +- `bun run build` — Builds contracts, renderer, and server through Turbo. +- `bun run typecheck` — Strict TypeScript checks for all packages. +- `bun run test` — Runs workspace tests. + +## Runtime modes + +CodeThing has a global runtime mode switch in the chat toolbar: + +- **Full access** (default): starts sessions with `approvalPolicy: never` and `sandboxMode: danger-full-access`. +- **Supervised**: starts sessions with `approvalPolicy: on-request` and `sandboxMode: workspace-write`, then prompts in-app for command/file approvals. + +## Provider architecture + +The renderer communicates with the server via WebSocket using a simple JSON-RPC-style protocol: + +- **Request/Response**: `{ id, method, params }` → `{ id, result }` or `{ id, error }` +- **Push events**: `{ type: "push", channel, data }` for streaming provider events + +Methods mirror the `NativeApi` interface defined in `@acme/contracts`: +- `providers.startSession`, `providers.sendTurn`, `providers.interruptTurn` +- `providers.respondToRequest`, `providers.stopSession`, `providers.listSessions` +- `shell.openInEditor`, `server.getConfig` + +Codex is the only implemented provider. `claudeCode` is reserved in contracts/UI. ## CI quality gates - `.github/workflows/ci.yml` runs `bun run lint`, `bun run typecheck`, and `bun run test` on pull requests and pushes to `main`. - -Optional: - -- `ELECTRON_RENDERER_PORT=5180 bun run dev` if `5173` is already in use. - -## Provider architecture - -The renderer now depends on `nativeApi.providers.*`: - -1. `startSession` -2. `sendTurn` -3. `interruptTurn` -4. `respondToRequest` -5. `stopSession` -6. `listSessions` -7. `onEvent` - -Codex is the only implemented provider right now. `claudeCode` is reserved in contracts/UI but returns a not-implemented error in main-process dispatch. diff --git a/apps/desktop/package.json b/apps/desktop/package.json deleted file mode 100644 index b86b9c007f..0000000000 --- a/apps/desktop/package.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "@acme/desktop", - "version": "0.0.0", - "private": true, - "main": "dist-electron/main.js", - "scripts": { - "dev": "concurrently -k -n BUNDLE,ELECTRON \"bun run dev:bundle\" \"bun run dev:electron\"", - "dev:bundle": "tsup --watch", - "dev:electron": "bun run scripts/dev-electron.mjs", - "build": "tsup", - "start": "electron dist-electron/main.js", - "postinstall": "electron-rebuild", - "typecheck": "tsc --noEmit", - "test": "vitest run", - "smoke-test": "node scripts/smoke-test.mjs" - }, - "dependencies": { - "@acme/contracts": "workspace:*", - "electron": "33.4.11", - "node-pty": "^1.0.0" - }, - "devDependencies": { - "concurrently": "^9.1.2", - "@electron/rebuild": "^3.7.0", - "@types/node": "^22.10.2", - "electronmon": "^2.0.2", - "tsup": "^8.3.5", - "typescript": "^5.7.3", - "wait-on": "^8.0.2" - } -} diff --git a/apps/desktop/scripts/dev-electron.mjs b/apps/desktop/scripts/dev-electron.mjs deleted file mode 100644 index d0522bb4e7..0000000000 --- a/apps/desktop/scripts/dev-electron.mjs +++ /dev/null @@ -1,28 +0,0 @@ -import { spawn } from "node:child_process"; - -import waitOn from "wait-on"; - -const port = Number(process.env.ELECTRON_RENDERER_PORT ?? 5173); -const devServerUrl = `http://localhost:${port}`; - -await waitOn({ - resources: [ - `tcp:${port}`, - "file:dist-electron/main.js", - "file:dist-electron/preload.js", - ], -}); - -const command = - process.platform === "win32" ? "electronmon.cmd" : "electronmon"; -const child = spawn(command, ["dist-electron/main.js"], { - stdio: "inherit", - env: { - ...process.env, - VITE_DEV_SERVER_URL: devServerUrl, - }, -}); - -child.on("exit", (code) => { - process.exit(code ?? 0); -}); diff --git a/apps/desktop/scripts/smoke-test.mjs b/apps/desktop/scripts/smoke-test.mjs deleted file mode 100644 index 62884bcde3..0000000000 --- a/apps/desktop/scripts/smoke-test.mjs +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Smoke test: builds desktop + renderer, launches Electron against the - * production bundle, waits for the renderer to confirm it loaded, then exits. - * - * Catches the two categories of regression we've hit: - * 1. Module resolution failures (preload can't find @acme/contracts, etc.) - * 2. CSP / script blocking (React fails to mount) - * - * The test works by injecting a tiny check via ELECTRON_ENABLE_LOGGING — - * Electron forwards renderer console.log to the main process stdout when - * that env var is set. We look for React's "Download the React DevTools" - * message as proof that React successfully mounted (it only fires after - * the first render). For production mode (no React DevTools message), we - * instead check that no fatal errors appeared. - */ -import { spawn, execSync } from "node:child_process"; -import { dirname, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; - -const __dirname = dirname(fileURLToPath(import.meta.url)); -const root = resolve(__dirname, "../../.."); -const desktopDir = resolve(__dirname, ".."); -const electronBin = resolve(desktopDir, "node_modules/.bin/electron"); -const mainJs = resolve(desktopDir, "dist-electron/main.js"); - -// ── Build first ────────────────────────────────────────────────────── -console.log("Building contracts + renderer + desktop..."); -execSync("bun run build", { cwd: root, stdio: "inherit" }); - -// ── Launch Electron (production mode — no VITE_DEV_SERVER_URL) ────── -console.log("\nLaunching Electron (production mode)..."); - -const child = spawn(electronBin, [mainJs], { - stdio: ["pipe", "pipe", "pipe"], - env: { - ...process.env, - VITE_DEV_SERVER_URL: "", // ensure production path - ELECTRON_ENABLE_LOGGING: "1", - }, -}); - -let output = ""; - -child.stdout.on("data", (d) => { - output += d.toString(); -}); -child.stderr.on("data", (d) => { - output += d.toString(); -}); - -const TIMEOUT_MS = 8_000; - -const timer = setTimeout(() => { - child.kill(); -}, TIMEOUT_MS); - -child.on("exit", () => { - clearTimeout(timer); - - // Fatal patterns that indicate broken builds - const fatalPatterns = [ - "Cannot find module", - "MODULE_NOT_FOUND", - "Refused to execute", // CSP blocking scripts - "can't detect preamble", // @vitejs/plugin-react failure - "Uncaught Error", - "Uncaught TypeError", - "Uncaught ReferenceError", - ]; - - const failures = fatalPatterns.filter((p) => output.includes(p)); - - if (failures.length > 0) { - console.error("\n❌ Smoke test FAILED. Matched fatal patterns:"); - for (const f of failures) { - console.error(` • ${f}`); - } - console.error("\nFull output:\n" + output); - process.exit(1); - } - - console.log("✅ Smoke test passed — no fatal errors detected"); - process.exit(0); -}); diff --git a/apps/desktop/src/codexAppServerManager.test.ts b/apps/desktop/src/codexAppServerManager.test.ts deleted file mode 100644 index b611e4d4de..0000000000 --- a/apps/desktop/src/codexAppServerManager.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { - classifyCodexStderrLine, - isRecoverableThreadResumeError, - normalizeCodexModelSlug, -} from "./codexAppServerManager"; - -describe("classifyCodexStderrLine", () => { - it("ignores empty lines", () => { - expect(classifyCodexStderrLine(" ")).toBeNull(); - }); - - it("ignores non-error structured codex logs", () => { - const line = - "2026-02-08T04:24:19.241256Z WARN codex_core::features: unknown feature key in config: skills"; - expect(classifyCodexStderrLine(line)).toBeNull(); - }); - - it("ignores known benign rollout path errors", () => { - const line = - "\u001b[2m2026-02-08T04:24:20.085687Z\u001b[0m \u001b[31mERROR\u001b[0m \u001b[2mcodex_core::rollout::list\u001b[0m: state db missing rollout path for thread 019c3b6c-46b8-7b70-ad23-82f824d161fb"; - expect(classifyCodexStderrLine(line)).toBeNull(); - }); - - it("keeps unknown structured errors", () => { - const line = "2026-02-08T04:24:20.085687Z ERROR codex_core::runtime: unrecoverable failure"; - expect(classifyCodexStderrLine(line)).toEqual({ - message: line, - }); - }); - - it("keeps plain stderr messages", () => { - const line = "fatal: permission denied"; - expect(classifyCodexStderrLine(line)).toEqual({ - message: line, - }); - }); -}); - -describe("normalizeCodexModelSlug", () => { - it("maps 5.3 aliases to gpt-5.3-codex", () => { - expect(normalizeCodexModelSlug("5.3")).toBe("gpt-5.3-codex"); - expect(normalizeCodexModelSlug("gpt-5.3")).toBe("gpt-5.3-codex"); - }); - - it("prefers codex id when model differs", () => { - expect(normalizeCodexModelSlug("gpt-5.3", "gpt-5.3-codex")).toBe("gpt-5.3-codex"); - }); - - it("keeps non-aliased models as-is", () => { - expect(normalizeCodexModelSlug("gpt-5.2-codex")).toBe("gpt-5.2-codex"); - expect(normalizeCodexModelSlug("gpt-5.2")).toBe("gpt-5.2"); - }); -}); - -describe("isRecoverableThreadResumeError", () => { - it("matches not-found resume errors", () => { - expect( - isRecoverableThreadResumeError( - new Error("thread/resume failed: thread not found"), - ), - ).toBe(true); - }); - - it("ignores non-resume errors", () => { - expect( - isRecoverableThreadResumeError( - new Error("thread/start failed: permission denied"), - ), - ).toBe(false); - }); - - it("ignores non-recoverable resume errors", () => { - expect( - isRecoverableThreadResumeError( - new Error("thread/resume failed: timed out waiting for server"), - ), - ).toBe(false); - }); -}); diff --git a/apps/desktop/src/codexAppServerManager.ts b/apps/desktop/src/codexAppServerManager.ts deleted file mode 100644 index 1564883e65..0000000000 --- a/apps/desktop/src/codexAppServerManager.ts +++ /dev/null @@ -1,847 +0,0 @@ -import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process"; -import { randomUUID } from "node:crypto"; -import { EventEmitter } from "node:events"; -import readline from "node:readline"; - -import type { - ProviderApprovalDecision, - ProviderEvent, - ProviderRequestKind, - ProviderSendTurnInput, - ProviderSession, - ProviderSessionStartInput, - ProviderTurnStartResult, -} from "@acme/contracts"; -import { normalizeModelSlug } from "@acme/contracts"; - -type PendingRequestKey = string; - -interface PendingRequest { - method: string; - timeout: ReturnType; - resolve: (value: unknown) => void; - reject: (error: Error) => void; -} - -interface PendingApprovalRequest { - requestId: string; - jsonRpcId: string | number; - method: - | "item/commandExecution/requestApproval" - | "item/fileChange/requestApproval"; - requestKind: ProviderRequestKind; - threadId?: string; - turnId?: string; - itemId?: string; -} - -interface CodexSessionContext { - session: ProviderSession; - child: ChildProcessWithoutNullStreams; - output: readline.Interface; - pending: Map; - pendingApprovals: Map; - nextRequestId: number; - stopping: boolean; -} - -interface JsonRpcError { - code?: number; - message?: string; -} - -interface JsonRpcRequest { - id: string | number; - method: string; - params?: unknown; -} - -interface JsonRpcResponse { - id: string | number; - result?: unknown; - error?: JsonRpcError; -} - -interface JsonRpcNotification { - method: string; - params?: unknown; -} - -const ANSI_ESCAPE_CHAR = String.fromCharCode(27); -const ANSI_ESCAPE_REGEX = new RegExp(`${ANSI_ESCAPE_CHAR}\\[[0-9;]*m`, "g"); -const CODEX_STDERR_LOG_REGEX = - /^\d{4}-\d{2}-\d{2}T\S+\s+(TRACE|DEBUG|INFO|WARN|ERROR)\s+\S+:\s+(.*)$/; -const BENIGN_ERROR_LOG_SNIPPETS = [ - "state db missing rollout path for thread", - "state db record_discrepancy: find_thread_path_by_id_str_in_subdir, falling_back", -]; -const RECOVERABLE_THREAD_RESUME_ERROR_SNIPPETS = [ - "not found", - "missing thread", - "no such thread", - "unknown thread", - "does not exist", -]; - -export function normalizeCodexModelSlug( - model: string | undefined | null, - preferredId?: string, -): string | undefined { - const normalized = normalizeModelSlug(model); - if (!normalized) { - return undefined; - } - - if (preferredId?.endsWith("-codex") && preferredId !== normalized) { - return preferredId; - } - - return normalized; -} - -export function classifyCodexStderrLine(rawLine: string): { message: string } | null { - const line = rawLine.replaceAll(ANSI_ESCAPE_REGEX, "").trim(); - if (!line) { - return null; - } - - const match = line.match(CODEX_STDERR_LOG_REGEX); - if (match) { - const level = match[1]; - if (level && level !== "ERROR") { - return null; - } - - const isBenignError = BENIGN_ERROR_LOG_SNIPPETS.some((snippet) => line.includes(snippet)); - if (isBenignError) { - return null; - } - } - - return { message: line }; -} - -export function isRecoverableThreadResumeError(error: unknown): boolean { - const message = ( - error instanceof Error ? error.message : String(error) - ).toLowerCase(); - if (!message.includes("thread/resume")) { - return false; - } - - return RECOVERABLE_THREAD_RESUME_ERROR_SNIPPETS.some((snippet) => - message.includes(snippet), - ); -} - -export interface CodexAppServerManagerEvents { - event: [event: ProviderEvent]; -} - -export class CodexAppServerManager extends EventEmitter { - private readonly sessions = new Map(); - - async startSession(input: ProviderSessionStartInput): Promise { - const sessionId = randomUUID(); - const now = new Date().toISOString(); - - const session: ProviderSession = { - sessionId, - provider: "codex", - status: "connecting", - model: normalizeCodexModelSlug(input.model), - cwd: input.cwd, - createdAt: now, - updatedAt: now, - }; - - const child = spawn("codex", ["app-server"], { - cwd: input.cwd, - env: process.env, - stdio: ["pipe", "pipe", "pipe"], - }); - const output = readline.createInterface({ input: child.stdout }); - - const context: CodexSessionContext = { - session, - child, - output, - pending: new Map(), - pendingApprovals: new Map(), - nextRequestId: 1, - stopping: false, - }; - - this.sessions.set(sessionId, context); - this.attachProcessListeners(context); - - this.emitLifecycleEvent(context, "session/connecting", "Starting codex app-server"); - - try { - await this.sendRequest(context, "initialize", { - clientInfo: { - name: "codething_desktop", - title: "CodeThing Desktop", - version: "0.1.0", - }, - capabilities: { - experimentalApi: false, - }, - }); - - this.writeMessage(context, { method: "initialized" }); - - const normalizedModel = normalizeCodexModelSlug(input.model); - const sessionOverrides = { - model: normalizedModel ?? null, - cwd: input.cwd ?? null, - approvalPolicy: input.approvalPolicy, - sandbox: input.sandboxMode, - }; - - const threadStartParams = { - ...sessionOverrides, - experimentalRawEvents: false, - }; - - let threadOpenMethod: "thread/start" | "thread/resume" = "thread/start"; - let threadOpenResponse: unknown; - if (input.resumeThreadId) { - try { - threadOpenMethod = "thread/resume"; - threadOpenResponse = await this.sendRequest( - context, - "thread/resume", - { - ...sessionOverrides, - threadId: input.resumeThreadId, - }, - ); - } catch (error) { - if (!isRecoverableThreadResumeError(error)) { - throw error; - } - - threadOpenMethod = "thread/start"; - this.emitLifecycleEvent( - context, - "session/threadResumeFallback", - `Could not resume thread ${input.resumeThreadId}; started a new thread instead.`, - ); - threadOpenResponse = await this.sendRequest( - context, - "thread/start", - threadStartParams, - ); - } - } else { - threadOpenMethod = "thread/start"; - threadOpenResponse = await this.sendRequest( - context, - "thread/start", - threadStartParams, - ); - } - - const threadOpenRecord = this.readObject(threadOpenResponse); - const threadId = - this.readString(this.readObject(threadOpenRecord, "thread"), "id") ?? - this.readString(threadOpenRecord, "threadId"); - if (!threadId) { - throw new Error( - `${threadOpenMethod} response did not include a thread id.`, - ); - } - - this.updateSession(context, { - status: "ready", - threadId, - }); - this.emitLifecycleEvent(context, "session/ready", `Connected to thread ${threadId}`); - return { ...context.session }; - } catch (error) { - const message = error instanceof Error ? error.message : "Failed to start Codex session."; - this.updateSession(context, { - status: "error", - lastError: message, - }); - this.emitErrorEvent(context, "session/startFailed", message); - this.stopSession(sessionId); - throw new Error(message, { cause: error }); - } - } - - async sendTurn(input: ProviderSendTurnInput): Promise { - const context = this.requireSession(input.sessionId); - if (!context.session.threadId) { - throw new Error("Session is missing a thread id."); - } - - const turnStartParams: { - threadId: string; - input: Array<{ type: "text"; text: string; text_elements: [] }>; - model?: string; - effort?: string; - } = { - threadId: context.session.threadId, - input: [ - { - type: "text", - text: input.input, - text_elements: [], - }, - ], - }; - const normalizedModel = normalizeCodexModelSlug(input.model); - if (normalizedModel) { - turnStartParams.model = normalizedModel; - } - if (input.effort) { - turnStartParams.effort = input.effort; - } - - const response = await this.sendRequest(context, "turn/start", turnStartParams); - - const turn = this.readObject(this.readObject(response), "turn"); - const turnId = this.readString(turn, "id"); - if (!turnId) { - throw new Error("turn/start response did not include a turn id."); - } - - this.updateSession(context, { - status: "running", - activeTurnId: turnId, - }); - - return { - threadId: context.session.threadId, - turnId, - }; - } - - async interruptTurn(sessionId: string, turnId?: string): Promise { - const context = this.requireSession(sessionId); - const effectiveTurnId = turnId ?? context.session.activeTurnId; - - if (!effectiveTurnId || !context.session.threadId) { - return; - } - - await this.sendRequest(context, "turn/interrupt", { - threadId: context.session.threadId, - turnId: effectiveTurnId, - }); - } - - async respondToRequest( - sessionId: string, - requestId: string, - decision: ProviderApprovalDecision, - ): Promise { - const context = this.requireSession(sessionId); - const pendingRequest = context.pendingApprovals.get(requestId); - if (!pendingRequest) { - throw new Error(`Unknown pending approval request: ${requestId}`); - } - - context.pendingApprovals.delete(requestId); - this.writeMessage(context, { - id: pendingRequest.jsonRpcId, - result: { - decision, - }, - }); - - this.emitEvent({ - id: randomUUID(), - kind: "notification", - provider: "codex", - sessionId: context.session.sessionId, - createdAt: new Date().toISOString(), - method: "item/requestApproval/decision", - threadId: pendingRequest.threadId, - turnId: pendingRequest.turnId, - itemId: pendingRequest.itemId, - requestId: pendingRequest.requestId, - requestKind: pendingRequest.requestKind, - payload: { - requestId: pendingRequest.requestId, - requestKind: pendingRequest.requestKind, - decision, - }, - }); - } - - stopSession(sessionId: string): void { - const context = this.sessions.get(sessionId); - if (!context) { - return; - } - - context.stopping = true; - - for (const pending of context.pending.values()) { - clearTimeout(pending.timeout); - pending.reject(new Error("Session stopped before request completed.")); - } - context.pending.clear(); - context.pendingApprovals.clear(); - - context.output.close(); - - if (!context.child.killed) { - context.child.kill(); - } - - this.updateSession(context, { - status: "closed", - activeTurnId: undefined, - }); - this.emitLifecycleEvent(context, "session/closed", "Session stopped"); - this.sessions.delete(sessionId); - } - - listSessions(): ProviderSession[] { - return Array.from(this.sessions.values(), ({ session }) => ({ - ...session, - })); - } - - hasSession(sessionId: string): boolean { - return this.sessions.has(sessionId); - } - - stopAll(): void { - for (const sessionId of this.sessions.keys()) { - this.stopSession(sessionId); - } - } - - private requireSession(sessionId: string): CodexSessionContext { - const context = this.sessions.get(sessionId); - if (!context) { - throw new Error(`Unknown session: ${sessionId}`); - } - - if (context.session.status === "closed") { - throw new Error(`Session is closed: ${sessionId}`); - } - - return context; - } - - private attachProcessListeners(context: CodexSessionContext): void { - context.output.on("line", (line) => { - this.handleStdoutLine(context, line); - }); - - context.child.stderr.on("data", (chunk: Buffer) => { - const raw = chunk.toString(); - const lines = raw.split(/\r?\n/g); - for (const rawLine of lines) { - const classified = classifyCodexStderrLine(rawLine); - if (!classified) { - continue; - } - - this.emitErrorEvent(context, "process/stderr", classified.message); - } - }); - - context.child.on("error", (error) => { - const message = error.message || "codex app-server process errored."; - this.updateSession(context, { - status: "error", - lastError: message, - }); - this.emitErrorEvent(context, "process/error", message); - }); - - context.child.on("exit", (code, signal) => { - if (context.stopping) { - return; - } - - const message = `codex app-server exited (code=${code ?? "null"}, signal=${signal ?? "null"}).`; - this.updateSession(context, { - status: "closed", - activeTurnId: undefined, - lastError: code === 0 ? context.session.lastError : message, - }); - this.emitLifecycleEvent(context, "session/exited", message); - this.sessions.delete(context.session.sessionId); - }); - } - - private handleStdoutLine(context: CodexSessionContext, line: string): void { - let parsed: unknown; - try { - parsed = JSON.parse(line); - } catch { - this.emitErrorEvent( - context, - "protocol/parseError", - "Received invalid JSON from codex app-server.", - ); - return; - } - - if (!parsed || typeof parsed !== "object") { - this.emitErrorEvent( - context, - "protocol/invalidMessage", - "Received non-object protocol message.", - ); - return; - } - - if (this.isServerRequest(parsed)) { - this.handleServerRequest(context, parsed); - return; - } - - if (this.isServerNotification(parsed)) { - this.handleServerNotification(context, parsed); - return; - } - - if (this.isResponse(parsed)) { - this.handleResponse(context, parsed); - return; - } - - this.emitErrorEvent( - context, - "protocol/unrecognizedMessage", - "Received protocol message in an unknown shape.", - ); - } - - private handleServerNotification( - context: CodexSessionContext, - notification: JsonRpcNotification, - ): void { - const route = this.readRouteFields(notification.params); - const textDelta = - notification.method === "item/agentMessage/delta" - ? this.readString(notification.params, "delta") - : undefined; - - this.emitEvent({ - id: randomUUID(), - kind: "notification", - provider: "codex", - sessionId: context.session.sessionId, - createdAt: new Date().toISOString(), - method: notification.method, - threadId: route.threadId, - turnId: route.turnId, - itemId: route.itemId, - textDelta, - payload: notification.params, - }); - - if (notification.method === "thread/started") { - const threadId = this.readString(this.readObject(notification.params)?.thread, "id"); - if (threadId) { - this.updateSession(context, { threadId }); - } - return; - } - - if (notification.method === "turn/started") { - const turnId = this.readString(this.readObject(notification.params)?.turn, "id"); - this.updateSession(context, { - status: "running", - activeTurnId: turnId, - }); - return; - } - - if (notification.method === "turn/completed") { - const turn = this.readObject(notification.params, "turn"); - const status = this.readString(turn, "status"); - const errorMessage = this.readString(this.readObject(turn, "error"), "message"); - this.updateSession(context, { - status: status === "failed" ? "error" : "ready", - activeTurnId: undefined, - lastError: errorMessage ?? context.session.lastError, - }); - return; - } - - if (notification.method === "error") { - const message = this.readString(this.readObject(notification.params)?.error, "message"); - const willRetry = this.readBoolean(notification.params, "willRetry"); - - this.updateSession(context, { - status: willRetry ? "running" : "error", - lastError: message ?? context.session.lastError, - }); - } - } - - private handleServerRequest(context: CodexSessionContext, request: JsonRpcRequest): void { - const route = this.readRouteFields(request.params); - const requestKind = this.requestKindForMethod(request.method); - let requestId: string | undefined; - if (requestKind) { - requestId = randomUUID(); - const pendingRequest: PendingApprovalRequest = { - requestId, - jsonRpcId: request.id, - method: - requestKind === "command" - ? "item/commandExecution/requestApproval" - : "item/fileChange/requestApproval", - requestKind, - ...(route.threadId ? { threadId: route.threadId } : {}), - ...(route.turnId ? { turnId: route.turnId } : {}), - ...(route.itemId ? { itemId: route.itemId } : {}), - }; - context.pendingApprovals.set(requestId, pendingRequest); - } - - this.emitEvent({ - id: randomUUID(), - kind: "request", - provider: "codex", - sessionId: context.session.sessionId, - createdAt: new Date().toISOString(), - method: request.method, - threadId: route.threadId, - turnId: route.turnId, - itemId: route.itemId, - requestId, - requestKind, - payload: request.params, - }); - - if (requestKind) { - return; - } - - if (request.method === "item/tool/requestUserInput") { - this.writeMessage(context, { - id: request.id, - result: { answers: {} }, - }); - return; - } - - this.writeMessage(context, { - id: request.id, - error: { - code: -32601, - message: `Unsupported server request: ${request.method}`, - }, - }); - } - - private handleResponse(context: CodexSessionContext, response: JsonRpcResponse): void { - const key = String(response.id); - const pending = context.pending.get(key); - if (!pending) { - return; - } - - clearTimeout(pending.timeout); - context.pending.delete(key); - - if (response.error?.message) { - pending.reject(new Error(`${pending.method} failed: ${String(response.error.message)}`)); - return; - } - - pending.resolve(response.result); - } - - private async sendRequest( - context: CodexSessionContext, - method: string, - params: unknown, - timeoutMs = 20_000, - ): Promise { - const id = context.nextRequestId; - context.nextRequestId += 1; - - const result = await new Promise((resolve, reject) => { - const timeout = setTimeout(() => { - context.pending.delete(String(id)); - reject(new Error(`Timed out waiting for ${method}.`)); - }, timeoutMs); - - context.pending.set(String(id), { - method, - timeout, - resolve, - reject, - }); - this.writeMessage(context, { - method, - id, - params, - }); - }); - - return result as TResponse; - } - - private writeMessage(context: CodexSessionContext, message: unknown): void { - const encoded = JSON.stringify(message); - if (!context.child.stdin.writable) { - throw new Error("Cannot write to codex app-server stdin."); - } - - context.child.stdin.write(`${encoded}\n`); - } - - private emitLifecycleEvent(context: CodexSessionContext, method: string, message: string): void { - this.emitEvent({ - id: randomUUID(), - kind: "session", - provider: "codex", - sessionId: context.session.sessionId, - createdAt: new Date().toISOString(), - method, - message, - }); - } - - private emitErrorEvent(context: CodexSessionContext, method: string, message: string): void { - this.emitEvent({ - id: randomUUID(), - kind: "error", - provider: "codex", - sessionId: context.session.sessionId, - createdAt: new Date().toISOString(), - method, - message, - }); - } - - private emitEvent(event: ProviderEvent): void { - this.emit("event", event); - } - - private updateSession(context: CodexSessionContext, updates: Partial): void { - context.session = { - ...context.session, - ...updates, - updatedAt: new Date().toISOString(), - }; - } - - private requestKindForMethod( - method: string, - ): ProviderRequestKind | undefined { - if (method === "item/commandExecution/requestApproval") { - return "command"; - } - - if (method === "item/fileChange/requestApproval") { - return "file-change"; - } - - return undefined; - } - - private isServerRequest(value: unknown): value is JsonRpcRequest { - if (!value || typeof value !== "object") { - return false; - } - - const candidate = value as Record; - return ( - typeof candidate.method === "string" && - (typeof candidate.id === "string" || typeof candidate.id === "number") - ); - } - - private isServerNotification(value: unknown): value is JsonRpcNotification { - if (!value || typeof value !== "object") { - return false; - } - - const candidate = value as Record; - return typeof candidate.method === "string" && !("id" in candidate); - } - - private isResponse(value: unknown): value is JsonRpcResponse { - if (!value || typeof value !== "object") { - return false; - } - - const candidate = value as Record; - const hasId = typeof candidate.id === "string" || typeof candidate.id === "number"; - const hasMethod = typeof candidate.method === "string"; - return hasId && !hasMethod; - } - - private readRouteFields(params: unknown): { - threadId?: string; - turnId?: string; - itemId?: string; - } { - const route: { - threadId?: string; - turnId?: string; - itemId?: string; - } = {}; - - const threadId = - this.readString(params, "threadId") ?? - this.readString(this.readObject(params, "thread"), "id"); - const turnId = - this.readString(params, "turnId") ?? this.readString(this.readObject(params, "turn"), "id"); - const itemId = - this.readString(params, "itemId") ?? this.readString(this.readObject(params, "item"), "id"); - - if (threadId) { - route.threadId = threadId; - } - - if (turnId) { - route.turnId = turnId; - } - - if (itemId) { - route.itemId = itemId; - } - - return route; - } - - private readObject(value: unknown, key?: string): Record | undefined { - const target = - key === undefined - ? value - : value && typeof value === "object" - ? (value as Record)[key] - : undefined; - - if (!target || typeof target !== "object") { - return undefined; - } - - return target as Record; - } - - private readString(value: unknown, key: string): string | undefined { - if (!value || typeof value !== "object") { - return undefined; - } - - const candidate = (value as Record)[key]; - return typeof candidate === "string" ? candidate : undefined; - } - - private readBoolean(value: unknown, key: string): boolean | undefined { - if (!value || typeof value !== "object") { - return undefined; - } - - const candidate = (value as Record)[key]; - return typeof candidate === "boolean" ? candidate : undefined; - } -} diff --git a/apps/desktop/src/fixPath.ts b/apps/desktop/src/fixPath.ts deleted file mode 100644 index be7ea9447c..0000000000 --- a/apps/desktop/src/fixPath.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { execFileSync } from "node:child_process"; - -export function fixPath(): void { - if (process.platform !== "darwin") return; - - try { - const shell = process.env.SHELL ?? "/bin/zsh"; - const result = execFileSync(shell, ["-ilc", "echo -n $PATH"], { - encoding: "utf8", - timeout: 5000, - }); - if (result) { - process.env.PATH = result; - } - } catch { - // Silently ignore — keep default PATH - } -} diff --git a/apps/desktop/src/ipcHelpers.test.ts b/apps/desktop/src/ipcHelpers.test.ts deleted file mode 100644 index ce3318f061..0000000000 --- a/apps/desktop/src/ipcHelpers.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; - -import { withParsedArgs, withParsedPayload } from "./ipcHelpers"; - -describe("withParsedPayload", () => { - it("parses payload and passes typed input to handler", async () => { - const handler = vi.fn(async (_event: unknown, payload: { value: string }) => - payload.value.toUpperCase(), - ); - const wrapped = withParsedPayload( - { - parse(payload: unknown): { value: string } { - if ( - !payload || - typeof payload !== "object" || - typeof (payload as { value?: unknown }).value !== "string" - ) { - throw new Error("Invalid payload"); - } - - return { value: (payload as { value: string }).value }; - }, - }, - handler, - ); - - const result = await wrapped({}, { value: "hello" }); - expect(result).toBe("HELLO"); - expect(handler).toHaveBeenCalledWith({}, { value: "hello" }); - }); - - it("throws and does not call handler on invalid payload", async () => { - const handler = vi.fn(async () => "ok"); - const wrapped = withParsedPayload( - { - parse(payload: unknown): { value: string } { - if ( - !payload || - typeof payload !== "object" || - typeof (payload as { value?: unknown }).value !== "string" - ) { - throw new Error("Invalid payload"); - } - - return { value: (payload as { value: string }).value }; - }, - }, - handler, - ); - - expect(() => wrapped({}, { value: 123 })).toThrow(); - expect(handler).not.toHaveBeenCalled(); - }); -}); - -describe("withParsedArgs", () => { - it("parses tuple arguments before invoking handler", () => { - const handler = vi.fn((_event: unknown, sessionId: string, data: string) => { - return `${sessionId}:${data}`; - }); - const wrapped = withParsedArgs( - { - parse(args: unknown[]): [string, string] { - const [sessionId, data] = args; - if (typeof sessionId !== "string" || sessionId.length === 0) { - throw new Error("Invalid sessionId"); - } - if (typeof data !== "string") { - throw new Error("Invalid data"); - } - - return [sessionId, data]; - }, - }, - handler, - ); - - expect(wrapped({}, "abc", "input")).toBe("abc:input"); - expect(handler).toHaveBeenCalledWith({}, "abc", "input"); - }); - - it("throws and does not call handler when args are invalid", () => { - const handler = vi.fn(); - const wrapped = withParsedArgs( - { - parse(args: unknown[]): [string, string] { - const [sessionId, data] = args; - if (typeof sessionId !== "string" || sessionId.length === 0) { - throw new Error("Invalid sessionId"); - } - if (typeof data !== "string") { - throw new Error("Invalid data"); - } - - return [sessionId, data]; - }, - }, - handler, - ); - - expect(() => wrapped({}, 123, "input")).toThrow(); - expect(handler).not.toHaveBeenCalled(); - }); -}); diff --git a/apps/desktop/src/ipcHelpers.ts b/apps/desktop/src/ipcHelpers.ts deleted file mode 100644 index 6383cbfef2..0000000000 --- a/apps/desktop/src/ipcHelpers.ts +++ /dev/null @@ -1,24 +0,0 @@ -type MaybePromise = T | Promise; -type Parser = { - parse: (value: unknown) => T; -}; -type ArgsParser = { - parse: (value: unknown[]) => T; -}; - -export function withParsedPayload( - schema: Parser, - handler: (event: unknown, payload: TPayload) => MaybePromise, -): (event: unknown, payload: unknown) => MaybePromise { - return (event, payload) => handler(event, schema.parse(payload)); -} - -export function withParsedArgs( - schema: ArgsParser, - handler: (event: unknown, ...args: TArgs) => MaybePromise, -): (event: unknown, ...args: unknown[]) => MaybePromise { - return (event, ...args) => { - const parsedArgs = schema.parse(args); - return handler(event, ...parsedArgs); - }; -} diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts deleted file mode 100644 index 454cbe65f1..0000000000 --- a/apps/desktop/src/main.ts +++ /dev/null @@ -1,349 +0,0 @@ -import { fixPath } from "./fixPath"; -fixPath(); - -import { spawn } from "node:child_process"; -import path from "node:path"; -import { BrowserWindow, app, dialog, ipcMain, session, shell } from "electron"; - -import { - EDITORS, - IPC_CHANNELS, - type TerminalCommandInput, - type TerminalCommandResult, - agentConfigSchema, - agentSessionIdSchema, - newTodoInputSchema, - providerInterruptTurnInputSchema, - providerRespondToRequestInputSchema, - providerSendTurnInputSchema, - providerSessionStartInputSchema, - providerStopSessionInputSchema, - terminalCommandInputSchema, - todoIdSchema, -} from "@acme/contracts"; -import { withParsedArgs, withParsedPayload } from "./ipcHelpers"; -import { ProcessManager } from "./processManager"; -import { ProviderManager } from "./providerManager"; -import { TodoStore } from "./todoStore"; - -const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL); - -let todoStore: TodoStore; -const processManager = new ProcessManager(); -const providerManager = new ProviderManager(); -const agentWriteArgsParser = { - parse(args: unknown[]): [string, string] { - const [sessionId, data] = args; - if (typeof data !== "string") { - throw new Error("agent:write data must be a string"); - } - - return [agentSessionIdSchema.parse(sessionId), data]; - }, -}; - -function createWindow(): BrowserWindow { - const window = new BrowserWindow({ - width: 1100, - height: 780, - minWidth: 840, - minHeight: 620, - show: false, - autoHideMenuBar: true, - titleBarStyle: "hiddenInset", - trafficLightPosition: { x: 16, y: 18 }, - webPreferences: { - preload: path.join(__dirname, "preload.js"), - contextIsolation: true, - nodeIntegration: false, - sandbox: true, - }, - }); - - window.webContents.setWindowOpenHandler(() => ({ action: "deny" })); - - window.once("ready-to-show", () => { - window.show(); - }); - - setupEventForwarding(window); - - if (isDevelopment) { - void window.loadURL(process.env.VITE_DEV_SERVER_URL as string); - return window; - } - - void window.loadFile(path.join(__dirname, "../../renderer/dist/index.html")); - return window; -} - -function registerIpcHandlers(): void { - // Todo handlers - ipcMain.handle(IPC_CHANNELS.todosList, async () => { - return todoStore.list(); - }); - - ipcMain.handle( - IPC_CHANNELS.todosAdd, - withParsedPayload(newTodoInputSchema, async (_event, payload) => { - return todoStore.add(payload); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.todosToggle, - withParsedPayload(todoIdSchema, async (_event, id) => { - return todoStore.toggle(id); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.todosRemove, - withParsedPayload(todoIdSchema, async (_event, id) => { - return todoStore.remove(id); - }), - ); - - ipcMain.handle(IPC_CHANNELS.dialogPickFolder, async () => { - const owner = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]; - const result = owner - ? await dialog.showOpenDialog(owner, { - properties: ["openDirectory", "createDirectory"], - }) - : await dialog.showOpenDialog({ - properties: ["openDirectory", "createDirectory"], - }); - - if (result.canceled) return null; - return result.filePaths[0] ?? null; - }); - - // Terminal handlers - ipcMain.handle( - IPC_CHANNELS.terminalRun, - withParsedPayload(terminalCommandInputSchema, async (_event, payload) => { - return runTerminalCommand(payload); - }), - ); - - // Shell handlers - ipcMain.handle( - IPC_CHANNELS.shellOpenInEditor, - async (_event, cwd: string, editor: string) => { - if (!cwd) throw new Error("cwd is required"); - const editorDef = EDITORS.find((e) => e.id === editor); - if (!editorDef) throw new Error(`Unknown editor: ${editor}`); - if (!editorDef.command) { - const error = await shell.openPath(cwd); - if (error) throw new Error(error); - return; - } - const child = spawn(editorDef.command, [cwd], { - detached: true, - stdio: "ignore", - }); - child.on("error", () => { - /* ignore spawn failures for detached editors */ - }); - child.unref(); - }, - ); - - // Agent handlers - ipcMain.handle( - IPC_CHANNELS.agentSpawn, - withParsedPayload(agentConfigSchema, async (_event, config) => { - return processManager.spawn(config); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.agentKill, - withParsedPayload(agentSessionIdSchema, async (_event, sessionId) => { - processManager.kill(sessionId); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.agentWrite, - withParsedArgs(agentWriteArgsParser, async (_event, sessionId, data) => { - processManager.write(sessionId, data); - }), - ); - - // Provider handlers - ipcMain.handle( - IPC_CHANNELS.providerSessionStart, - withParsedPayload(providerSessionStartInputSchema, async (_event, payload) => { - return providerManager.startSession(payload); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.providerTurnStart, - withParsedPayload(providerSendTurnInputSchema, async (_event, payload) => { - return providerManager.sendTurn(payload); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.providerTurnInterrupt, - withParsedPayload(providerInterruptTurnInputSchema, async (_event, payload) => { - await providerManager.interruptTurn(payload); - }), - ); - - ipcMain.handle( - IPC_CHANNELS.providerRequestRespond, - withParsedPayload( - providerRespondToRequestInputSchema, - async (_event, payload) => { - await providerManager.respondToRequest(payload); - }, - ), - ); - - ipcMain.handle( - IPC_CHANNELS.providerSessionStop, - withParsedPayload(providerStopSessionInputSchema, async (_event, payload) => { - providerManager.stopSession(payload); - }), - ); - - ipcMain.handle(IPC_CHANNELS.providerSessionList, async () => { - return providerManager.listSessions(); - }); -} - -async function runTerminalCommand(input: TerminalCommandInput): Promise { - const shellPath = - process.platform === "win32" - ? (process.env.ComSpec ?? "cmd.exe") - : (process.env.SHELL ?? "/bin/sh"); - - const args = - process.platform === "win32" ? ["/d", "/s", "/c", input.command] : ["-lc", input.command]; - - return new Promise((resolve, reject) => { - const child = spawn(shellPath, args, { - cwd: input.cwd, - env: process.env, - stdio: ["ignore", "pipe", "pipe"], - }); - - let stdout = ""; - let stderr = ""; - let timedOut = false; - - const timeout = setTimeout(() => { - timedOut = true; - child.kill("SIGTERM"); - setTimeout(() => { - if (!child.killed) { - child.kill("SIGKILL"); - } - }, 1_000).unref(); - }, input.timeoutMs ?? 30_000); - - child.stdout?.on("data", (chunk: Buffer) => { - stdout += chunk.toString(); - }); - - child.stderr?.on("data", (chunk: Buffer) => { - stderr += chunk.toString(); - }); - - child.on("error", (error) => { - clearTimeout(timeout); - reject(error); - }); - - child.on("close", (code, signal) => { - clearTimeout(timeout); - resolve({ - stdout, - stderr, - code: code ?? null, - signal: signal ?? null, - timedOut, - }); - }); - }); -} - -function setupEventForwarding(window: BrowserWindow): void { - const onOutput = (chunk: unknown) => { - if (!window.isDestroyed()) { - window.webContents.send(IPC_CHANNELS.agentOutput, chunk); - } - }; - - const onExit = (exit: unknown) => { - if (!window.isDestroyed()) { - window.webContents.send(IPC_CHANNELS.agentExit, exit); - } - }; - - const onProviderEvent = (event: unknown) => { - if (!window.isDestroyed()) { - window.webContents.send(IPC_CHANNELS.providerEvent, event); - } - }; - - processManager.on("output", onOutput); - processManager.on("exit", onExit); - providerManager.on("event", onProviderEvent); - - window.on("closed", () => { - processManager.off("output", onOutput); - processManager.off("exit", onExit); - providerManager.off("event", onProviderEvent); - }); -} - -function setupCSP(): void { - session.defaultSession.webRequest.onHeadersReceived((details, callback) => { - const csp = isDevelopment - ? "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; connect-src 'self' ws://localhost:* http://localhost:*" - : "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'"; - - callback({ - responseHeaders: { - ...details.responseHeaders, - "Content-Security-Policy": [csp], - }, - }); - }); -} - -async function bootstrap(): Promise { - setupCSP(); - - todoStore = new TodoStore(path.join(app.getPath("userData"), "todos.json")); - await todoStore.init(); - - registerIpcHandlers(); - createWindow(); - - app.on("activate", () => { - if (BrowserWindow.getAllWindows().length === 0) { - createWindow(); - } - }); -} - -app.on("before-quit", () => { - processManager.killAll(); - providerManager.stopAll(); - providerManager.dispose(); -}); - -app.whenReady().then(() => { - void bootstrap(); -}); - -app.on("window-all-closed", () => { - if (process.platform !== "darwin") { - app.quit(); - } -}); diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts deleted file mode 100644 index 11fdfe4e77..0000000000 --- a/apps/desktop/src/preload.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { contextBridge, ipcRenderer } from "electron"; - -import { IPC_CHANNELS, type NativeApi } from "@acme/contracts"; - -const nativeApi: NativeApi = { - todos: { - list: () => ipcRenderer.invoke(IPC_CHANNELS.todosList), - add: (input) => ipcRenderer.invoke(IPC_CHANNELS.todosAdd, input), - toggle: (id) => ipcRenderer.invoke(IPC_CHANNELS.todosToggle, id), - remove: (id) => ipcRenderer.invoke(IPC_CHANNELS.todosRemove, id), - }, - dialogs: { - pickFolder: () => ipcRenderer.invoke(IPC_CHANNELS.dialogPickFolder), - }, - terminal: { - run: (input) => ipcRenderer.invoke(IPC_CHANNELS.terminalRun, input), - }, - agent: { - spawn: (config) => ipcRenderer.invoke(IPC_CHANNELS.agentSpawn, config), - kill: (sessionId) => ipcRenderer.invoke(IPC_CHANNELS.agentKill, sessionId), - write: (sessionId, data) => ipcRenderer.invoke(IPC_CHANNELS.agentWrite, sessionId, data), - onOutput: (callback) => { - const listener = (_event: Electron.IpcRendererEvent, chunk: unknown) => - callback(chunk as Parameters[0]); - ipcRenderer.on(IPC_CHANNELS.agentOutput, listener); - return () => ipcRenderer.removeListener(IPC_CHANNELS.agentOutput, listener); - }, - onExit: (callback) => { - const listener = (_event: Electron.IpcRendererEvent, exit: unknown) => - callback(exit as Parameters[0]); - ipcRenderer.on(IPC_CHANNELS.agentExit, listener); - return () => ipcRenderer.removeListener(IPC_CHANNELS.agentExit, listener); - }, - }, - providers: { - startSession: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerSessionStart, input), - sendTurn: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerTurnStart, input), - interruptTurn: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerTurnInterrupt, input), - respondToRequest: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerRequestRespond, input), - stopSession: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerSessionStop, input), - listSessions: () => ipcRenderer.invoke(IPC_CHANNELS.providerSessionList), - onEvent: (callback) => { - const listener = (_event: Electron.IpcRendererEvent, payload: unknown) => - callback(payload as Parameters[0]); - ipcRenderer.on(IPC_CHANNELS.providerEvent, listener); - return () => ipcRenderer.removeListener(IPC_CHANNELS.providerEvent, listener); - }, - }, - shell: { - openInEditor: (cwd: string, editor: string) => - ipcRenderer.invoke(IPC_CHANNELS.shellOpenInEditor, cwd, editor), - }, -}; - -contextBridge.exposeInMainWorld("nativeApi", nativeApi); diff --git a/apps/desktop/src/processManager.test.ts b/apps/desktop/src/processManager.test.ts deleted file mode 100644 index ad231ef849..0000000000 --- a/apps/desktop/src/processManager.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { ProcessManager } from "./processManager"; - -describe("ProcessManager", () => { - it("can be instantiated", () => { - const pm = new ProcessManager(); - expect(pm).toBeInstanceOf(ProcessManager); - }); - - it("spawns a process and receives output", async () => { - const pm = new ProcessManager(); - const chunks: string[] = []; - - const done = new Promise((resolve) => { - pm.on("output", (chunk) => { - chunks.push(chunk.data); - }); - pm.on("exit", () => { - resolve(); - }); - }); - - const sessionId = pm.spawn({ command: "echo", args: ["hello"] }); - expect(typeof sessionId).toBe("string"); - expect(sessionId.length).toBeGreaterThan(0); - - await done; - - expect(chunks.join("").trim()).toBe("hello"); - }); - - it("can kill a spawned process", async () => { - const pm = new ProcessManager(); - - const exited = new Promise((resolve) => { - pm.on("exit", (exit) => { - resolve(exit.code); - }); - }); - - const sessionId = pm.spawn({ command: "sleep", args: ["10"] }); - pm.kill(sessionId); - - const code = await exited; - expect(code).not.toBe(0); - }); -}); diff --git a/apps/desktop/src/processManager.ts b/apps/desktop/src/processManager.ts deleted file mode 100644 index 7297615a90..0000000000 --- a/apps/desktop/src/processManager.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { type ChildProcess, spawn } from "node:child_process"; -import { randomUUID } from "node:crypto"; -import { EventEmitter } from "node:events"; - -import { - type AgentConfig, - type AgentExit, - type OutputChunk, - agentConfigSchema, -} from "@acme/contracts"; -import type { IPty } from "node-pty"; - -export interface ProcessManagerEvents { - output: [chunk: OutputChunk]; - exit: [exit: AgentExit]; -} - -export class ProcessManager extends EventEmitter { - private sessions = new Map(); - private ptySessions = new Map(); - - spawn(raw: AgentConfig): string { - const config = agentConfigSchema.parse(raw); - const sessionId = randomUUID(); - - if (config.usePty) { - return this.spawnPty(sessionId, config); - } - - return this.spawnProcess(sessionId, config); - } - - private spawnProcess(sessionId: string, config: AgentConfig): string { - const child = spawn(config.command, config.args, { - cwd: config.cwd, - env: config.env ? { ...process.env, ...config.env } : process.env, - stdio: ["pipe", "pipe", "pipe"], - }); - - this.sessions.set(sessionId, child); - - child.stdout?.on("data", (data: Buffer) => { - this.emit("output", { - sessionId, - stream: "stdout", - data: data.toString(), - }); - }); - - child.stderr?.on("data", (data: Buffer) => { - this.emit("output", { - sessionId, - stream: "stderr", - data: data.toString(), - }); - }); - - child.on("exit", (code, signal) => { - this.sessions.delete(sessionId); - this.emit("exit", { - sessionId, - code: code ?? null, - signal: signal ?? null, - }); - }); - - return sessionId; - } - - private spawnPty(sessionId: string, config: AgentConfig): string { - const pty = require("node-pty") as typeof import("node-pty"); - - const ptyProcess = pty.spawn(config.command, config.args, { - name: "xterm-256color", - cols: 120, - rows: 30, - cwd: config.cwd ?? process.cwd(), - env: (config.env ? { ...process.env, ...config.env } : process.env) as Record, - }); - - this.ptySessions.set(sessionId, ptyProcess); - - ptyProcess.onData((data) => { - this.emit("output", { - sessionId, - stream: "stdout", - data, - }); - }); - - ptyProcess.onExit(({ exitCode, signal }) => { - this.ptySessions.delete(sessionId); - this.emit("exit", { - sessionId, - code: exitCode, - signal: signal !== undefined ? String(signal) : null, - }); - }); - - return sessionId; - } - - write(sessionId: string, data: string): void { - const child = this.sessions.get(sessionId); - if (child) { - child.stdin?.write(data); - return; - } - - const pty = this.ptySessions.get(sessionId); - if (pty) { - pty.write(data); - return; - } - - throw new Error(`No session: ${sessionId}`); - } - - kill(sessionId: string): void { - const child = this.sessions.get(sessionId); - if (child) { - child.kill(); - return; - } - - const pty = this.ptySessions.get(sessionId); - if (pty) { - pty.kill(); - return; - } - } - - killAll(): void { - for (const child of this.sessions.values()) { - child.kill(); - } - this.sessions.clear(); - - for (const pty of this.ptySessions.values()) { - pty.kill(); - } - this.ptySessions.clear(); - } -} diff --git a/apps/desktop/src/providerManager.test.ts b/apps/desktop/src/providerManager.test.ts deleted file mode 100644 index c159bb296c..0000000000 --- a/apps/desktop/src/providerManager.test.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { ProviderManager } from "./providerManager"; - -describe("ProviderManager", () => { - it("detaches provider event listener and ends thread log streams on dispose", () => { - const manager = new ProviderManager(); - const internals = manager as unknown as { - codex: { listenerCount: (event: string) => number }; - onCodexEvent: (event: { - id: string; - kind: "notification"; - provider: "codex"; - sessionId: string; - createdAt: string; - method: string; - threadId: string; - }) => void; - threadLogStreams: Map< - string, - { writableEnded: boolean; destroyed: boolean } - >; - }; - internals.onCodexEvent({ - id: "evt-1", - kind: "notification", - provider: "codex", - sessionId: "sess-1", - createdAt: new Date().toISOString(), - method: "thread/started", - threadId: "thread-1", - }); - const stream = internals.threadLogStreams.get("thread-1"); - expect(stream).toBeDefined(); - if (!stream) return; - - expect(internals.codex.listenerCount("event")).toBe(1); - manager.dispose(); - - expect(internals.codex.listenerCount("event")).toBe(0); - expect(stream.writableEnded || stream.destroyed).toBe(true); - expect(internals.threadLogStreams.size).toBe(0); - }); - - it("allows multiple dispose calls", () => { - const manager = new ProviderManager(); - - manager.dispose(); - expect(() => manager.dispose()).not.toThrow(); - }); - - it("rejects request responses for unknown sessions", async () => { - const manager = new ProviderManager(); - - await expect( - manager.respondToRequest({ - sessionId: "missing-session", - requestId: "req-1", - decision: "accept", - }), - ).rejects.toThrow("Unknown provider session: missing-session"); - - manager.dispose(); - }); -}); diff --git a/apps/desktop/src/providerManager.ts b/apps/desktop/src/providerManager.ts deleted file mode 100644 index ebba31b20d..0000000000 --- a/apps/desktop/src/providerManager.ts +++ /dev/null @@ -1,209 +0,0 @@ -import { EventEmitter } from "node:events"; -import fs from "node:fs"; -import path from "node:path"; - -import { - type ProviderEvent, - type ProviderInterruptTurnInput, - type ProviderRespondToRequestInput, - type ProviderSendTurnInput, - type ProviderSession, - type ProviderSessionStartInput, - type ProviderStopSessionInput, - type ProviderTurnStartResult, - providerInterruptTurnInputSchema, - providerRespondToRequestInputSchema, - providerSendTurnInputSchema, - providerSessionStartInputSchema, - providerStopSessionInputSchema, -} from "@acme/contracts"; -import { CodexAppServerManager } from "./codexAppServerManager"; - -export interface ProviderManagerEvents { - event: [event: ProviderEvent]; -} - -export class ProviderManager extends EventEmitter { - private readonly codex = new CodexAppServerManager(); - private readonly threadLogsDir: string; - private readonly threadLogStreams = new Map(); - private readonly sessionThreadIds = new Map(); - private readonly pendingEventsBySession = new Map(); - private disposed = false; - private readonly onCodexEvent = (event: ProviderEvent) => { - if (this.disposed) { - return; - } - - this.routeEventToThreadLog(event); - this.emit("event", event); - }; - - constructor() { - super(); - - const logsDir = path.resolve(process.cwd(), ".logs"); - this.threadLogsDir = path.join(logsDir, "threads"); - fs.mkdirSync(this.threadLogsDir, { recursive: true }); - - this.codex.on("event", this.onCodexEvent); - } - - async startSession(raw: ProviderSessionStartInput): Promise { - const input = providerSessionStartInputSchema.parse(raw); - if (input.provider !== "codex") { - throw new Error(`Provider '${input.provider}' is not implemented yet.`); - } - - const session = await this.codex.startSession(input); - if (session.threadId) { - this.sessionThreadIds.set(session.sessionId, session.threadId); - this.flushPendingSessionEvents(session.sessionId, session.threadId); - } - return session; - } - - async sendTurn(raw: ProviderSendTurnInput): Promise { - const input = providerSendTurnInputSchema.parse(raw); - if (!this.codex.hasSession(input.sessionId)) { - throw new Error(`Unknown provider session: ${input.sessionId}`); - } - - return this.codex.sendTurn(input); - } - - async interruptTurn(raw: ProviderInterruptTurnInput): Promise { - const input = providerInterruptTurnInputSchema.parse(raw); - if (!this.codex.hasSession(input.sessionId)) { - throw new Error(`Unknown provider session: ${input.sessionId}`); - } - - await this.codex.interruptTurn(input.sessionId, input.turnId); - } - - async respondToRequest(raw: ProviderRespondToRequestInput): Promise { - const input = providerRespondToRequestInputSchema.parse(raw); - if (!this.codex.hasSession(input.sessionId)) { - throw new Error(`Unknown provider session: ${input.sessionId}`); - } - - await this.codex.respondToRequest( - input.sessionId, - input.requestId, - input.decision, - ); - } - - stopSession(raw: ProviderStopSessionInput): void { - const input = providerStopSessionInputSchema.parse(raw); - this.codex.stopSession(input.sessionId); - this.sessionThreadIds.delete(input.sessionId); - this.pendingEventsBySession.delete(input.sessionId); - } - - listSessions(): ProviderSession[] { - return this.codex.listSessions(); - } - - stopAll(): void { - this.codex.stopAll(); - } - - dispose(): void { - if (this.disposed) { - return; - } - - this.disposed = true; - this.codex.off("event", this.onCodexEvent); - for (const stream of this.threadLogStreams.values()) { - stream.end(); - } - this.threadLogStreams.clear(); - this.sessionThreadIds.clear(); - this.pendingEventsBySession.clear(); - } - - private routeEventToThreadLog(event: ProviderEvent): void { - const threadId = this.resolveThreadId(event); - if (!threadId) { - const pending = this.pendingEventsBySession.get(event.sessionId) ?? []; - pending.push(event); - this.pendingEventsBySession.set(event.sessionId, pending.slice(-100)); - return; - } - - this.flushPendingSessionEvents(event.sessionId, threadId); - this.writeThreadEvent(threadId, event); - } - - private flushPendingSessionEvents(sessionId: string, threadId: string): void { - const pending = this.pendingEventsBySession.get(sessionId); - if (!pending || pending.length === 0) { - return; - } - - for (const event of pending) { - this.writeThreadEvent(threadId, event); - } - this.pendingEventsBySession.delete(sessionId); - } - - private resolveThreadId(event: ProviderEvent): string | undefined { - const fromPayload = this.readThreadIdFromPayload(event.payload); - const threadId = - event.threadId ?? - fromPayload ?? - this.sessionThreadIds.get(event.sessionId); - - if (threadId) { - this.sessionThreadIds.set(event.sessionId, threadId); - } - - return threadId; - } - - private readThreadIdFromPayload(payload: unknown): string | undefined { - if (!payload || typeof payload !== "object") { - return undefined; - } - - const record = payload as Record; - const direct = record.threadId; - if (typeof direct === "string" && direct.length > 0) { - return direct; - } - - const thread = record.thread; - if (!thread || typeof thread !== "object") { - return undefined; - } - - const nested = (thread as Record).id; - if (typeof nested === "string" && nested.length > 0) { - return nested; - } - - return undefined; - } - - private writeThreadEvent(threadId: string, event: ProviderEvent): void { - const stream = this.getOrCreateThreadLogStream(threadId); - stream.write(`${JSON.stringify(event)}\n`); - } - - private getOrCreateThreadLogStream(threadId: string): fs.WriteStream { - const cached = this.threadLogStreams.get(threadId); - if (cached) { - return cached; - } - - const safeThreadId = threadId.replace(/[^a-zA-Z0-9._-]/g, "_"); - const stream = fs.createWriteStream( - path.join(this.threadLogsDir, `${safeThreadId}.events.ndjson`), - { flags: "a" }, - ); - this.threadLogStreams.set(threadId, stream); - return stream; - } -} diff --git a/apps/desktop/src/todoStore.ts b/apps/desktop/src/todoStore.ts deleted file mode 100644 index a27ec06020..0000000000 --- a/apps/desktop/src/todoStore.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { randomUUID } from "node:crypto"; -import { promises as fs } from "node:fs"; -import path from "node:path"; - -import { - type NewTodoInput, - type Todo, - newTodoInputSchema, - todoIdSchema, - todoListSchema, -} from "@acme/contracts"; - -export class TodoStore { - private todos: Todo[] = []; - private queue: Promise = Promise.resolve(); - - public constructor(private readonly filePath: string) {} - - public async init(): Promise { - return this.runExclusive(async () => { - await fs.mkdir(path.dirname(this.filePath), { recursive: true }); - - try { - const raw = await fs.readFile(this.filePath, "utf8"); - const parsed = todoListSchema.safeParse(JSON.parse(raw)); - - if (parsed.success) { - this.todos = parsed.data; - return; - } - } catch (error) { - if (!isNotFoundError(error)) { - throw error; - } - } - - this.todos = []; - await this.persist(); - }); - } - - public async list(): Promise { - return this.runExclusive(async () => [...this.todos]); - } - - public async add(input: NewTodoInput): Promise { - return this.runExclusive(async () => { - const { title } = newTodoInputSchema.parse(input); - const todo: Todo = { - id: randomUUID(), - title, - completed: false, - createdAt: new Date().toISOString(), - }; - - this.todos = [todo, ...this.todos]; - await this.persist(); - return [...this.todos]; - }); - } - - public async toggle(id: string): Promise { - return this.runExclusive(async () => { - const parsedId = todoIdSchema.parse(id); - this.todos = this.todos.map((todo) => - todo.id === parsedId ? { ...todo, completed: !todo.completed } : todo, - ); - - await this.persist(); - return [...this.todos]; - }); - } - - public async remove(id: string): Promise { - return this.runExclusive(async () => { - const parsedId = todoIdSchema.parse(id); - this.todos = this.todos.filter((todo) => todo.id !== parsedId); - - await this.persist(); - return [...this.todos]; - }); - } - - private async persist(): Promise { - await fs.writeFile(this.filePath, JSON.stringify(this.todos, null, 2), "utf8"); - } - - private async runExclusive(operation: () => Promise): Promise { - const next = this.queue.then(operation, operation); - this.queue = next.then( - () => undefined, - () => undefined, - ); - - return next; - } -} - -function isNotFoundError(error: unknown): boolean { - return ( - typeof error === "object" && - error !== null && - "code" in error && - (error as { code?: string }).code === "ENOENT" - ); -} diff --git a/apps/desktop/tsconfig.json b/apps/desktop/tsconfig.json deleted file mode 100644 index 893a95a419..0000000000 --- a/apps/desktop/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "composite": true, - "types": ["node", "electron"], - "lib": ["ES2023", "DOM"] - }, - "include": ["src", "tsup.config.ts"] -} diff --git a/apps/desktop/tsup.config.ts b/apps/desktop/tsup.config.ts deleted file mode 100644 index 0d630c2805..0000000000 --- a/apps/desktop/tsup.config.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { defineConfig } from "tsup"; - -export default defineConfig({ - entry: ["src/main.ts", "src/preload.ts"], - format: "cjs", - outDir: "dist-electron", - sourcemap: true, - clean: true, - noExternal: ["@acme/contracts"], -}); diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 59166a0161..d0b895c230 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -19,28 +19,6 @@ export const EDITORS = [ export type EditorId = (typeof EDITORS)[number]["id"]; -export const IPC_CHANNELS = { - todosList: "todos:list", - todosAdd: "todos:add", - todosToggle: "todos:toggle", - todosRemove: "todos:remove", - dialogPickFolder: "dialog:pick-folder", - terminalRun: "terminal:run", - agentSpawn: "agent:spawn", - agentKill: "agent:kill", - agentWrite: "agent:write", - agentOutput: "agent:output", - agentExit: "agent:exit", - providerSessionStart: "provider:session:start", - providerTurnStart: "provider:turn:start", - providerTurnInterrupt: "provider:turn:interrupt", - providerSessionStop: "provider:session:stop", - providerSessionList: "provider:session:list", - providerRequestRespond: "provider:request:respond", - providerEvent: "provider:event", - shellOpenInEditor: "shell:open-in-editor", -} as const; - export interface NativeApi { todos: { list: () => Promise;