From 6204eaf5e1978ca5e9e22c7c0eb61eb0a460aea3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Feb 2026 21:15:13 -0800 Subject: [PATCH] Move editor definitions to contracts for strict IPC typing EDITORS config and EditorId type now live in @acme/contracts so the shell.openInEditor IPC channel is strictly typed end-to-end. The main process uses the contract's command field (null = file manager via shell.openPath). Co-Authored-By: Claude Opus 4.6 --- apps/desktop/src/main.ts | 12 +++++------- apps/renderer/src/components/ChatView.tsx | 17 +++++++++-------- packages/contracts/src/ipc.ts | 9 ++++++++- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 657705fd01..a38ed2a946 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -13,6 +13,7 @@ import { } from "electron"; import { + EDITORS, IPC_CHANNELS, type TerminalCommandInput, type TerminalCommandResult, @@ -136,16 +137,13 @@ function registerIpcHandlers(): void { ipcMain.handle( IPC_CHANNELS.shellOpenInEditor, async (_event, cwd: string, editor: string) => { - if (editor === "file-manager") { + const editorDef = EDITORS.find((e) => e.id === editor); + if (!editorDef) throw new Error(`Unknown editor: ${editor}`); + if (!editorDef.command) { await shell.openPath(cwd); return; } - const EDITOR_COMMANDS: Record string[] }> = { - cursor: { command: "cursor", args: (p) => [p] }, - }; - const entry = EDITOR_COMMANDS[editor]; - if (!entry) throw new Error(`Unknown editor: ${editor}`); - const child = spawn(entry.command, entry.args(cwd), { + const child = spawn(editorDef.command, [cwd], { detached: true, stdio: "ignore", }); diff --git a/apps/renderer/src/components/ChatView.tsx b/apps/renderer/src/components/ChatView.tsx index 2cf468ec70..ebf1e008d3 100644 --- a/apps/renderer/src/components/ChatView.tsx +++ b/apps/renderer/src/components/ChatView.tsx @@ -7,6 +7,7 @@ import { useState, } from "react"; +import { EDITORS, type EditorId } from "@acme/contracts"; import { DEFAULT_MODEL, DEFAULT_REASONING, @@ -36,10 +37,10 @@ const FILE_MANAGER_LABEL = navigator.platform.includes("Mac") ? "Explorer" : "Files"; -const EDITORS = [ - { id: "cursor", label: "Cursor" }, - { id: "file-manager", label: FILE_MANAGER_LABEL }, -] as const; +function editorLabel(editor: (typeof EDITORS)[number]): string { + return editor.command ? editor.label : FILE_MANAGER_LABEL; +} + const LAST_EDITOR_KEY = "codething:last-editor"; function statusLabel(phase: string): string { @@ -64,8 +65,8 @@ export default function ChatView() { const [isConnecting, setIsConnecting] = useState(false); const [isModelMenuOpen, setIsModelMenuOpen] = useState(false); const [isEditorMenuOpen, setIsEditorMenuOpen] = useState(false); - const [lastEditor, setLastEditor] = useState( - () => localStorage.getItem(LAST_EDITOR_KEY) ?? EDITORS[0].id, + const [lastEditor, setLastEditor] = useState( + () => (localStorage.getItem(LAST_EDITOR_KEY) as EditorId) ?? EDITORS[0].id, ); const [selectedEffort, setSelectedEffort] = useState(DEFAULT_REASONING); @@ -194,7 +195,7 @@ export default function ChatView() { return () => window.removeEventListener("keydown", handler); }, [api, activeProject, lastEditor]); - const openInEditor = (editorId: string) => { + const openInEditor = (editorId: EditorId) => { if (!api || !activeProject) return; void api.shell.openInEditor(activeProject.cwd, editorId); setLastEditor(editorId); @@ -394,7 +395,7 @@ export default function ChatView() { className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-[11px] text-[#e0e0e0] hover:bg-white/[0.06]" onClick={() => openInEditor(editor.id)} > - {editor.label} + {editorLabel(editor)} {editor.id === lastEditor && ( {navigator.platform.includes("Mac") ? "\u2318" : "Ctrl+"}O diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 5d3e1667cb..6548c7196a 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -11,6 +11,13 @@ import type { import type { TerminalCommandInput, TerminalCommandResult } from "./terminal"; import type { NewTodoInput, Todo } from "./todo"; +export const EDITORS = [ + { id: "cursor", label: "Cursor", command: "cursor" }, + { id: "file-manager", label: "File Manager", command: null }, +] as const; + +export type EditorId = (typeof EDITORS)[number]["id"]; + export const IPC_CHANNELS = { todosList: "todos:list", todosAdd: "todos:add", @@ -65,6 +72,6 @@ export interface NativeApi { onEvent: (callback: (event: ProviderEvent) => void) => () => void; }; shell: { - openInEditor: (cwd: string, editor: string) => Promise; + openInEditor: (cwd: string, editor: EditorId) => Promise; }; }