|
| 1 | +import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"; |
| 2 | +import { existsSync, readdirSync, statSync } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { app, BrowserWindow, dialog, ipcMain, shell } from "electron"; |
| 6 | + |
| 7 | +interface RunTaskInput { |
| 8 | + repo: string; |
| 9 | + task: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface RunTaskStarted { |
| 13 | + pid: number | null; |
| 14 | + command: string; |
| 15 | + args: string[]; |
| 16 | +} |
| 17 | + |
| 18 | +interface RunTaskRejected { |
| 19 | + error: string; |
| 20 | +} |
| 21 | + |
| 22 | +type RunTaskResult = RunTaskStarted | RunTaskRejected; |
| 23 | + |
| 24 | +let mainWindow: BrowserWindow | undefined; |
| 25 | +let currentTask: ChildProcessWithoutNullStreams | undefined; |
| 26 | +let currentRepo: string | undefined; |
| 27 | + |
| 28 | +const __filename = fileURLToPath(import.meta.url); |
| 29 | +const __dirname = path.dirname(__filename); |
| 30 | + |
| 31 | +async function createWindow(): Promise<void> { |
| 32 | + mainWindow = new BrowserWindow({ |
| 33 | + width: 1180, |
| 34 | + height: 780, |
| 35 | + minWidth: 960, |
| 36 | + minHeight: 620, |
| 37 | + title: "OpenCode++ Desktop", |
| 38 | + webPreferences: { |
| 39 | + preload: path.join(__dirname, "preload.js"), |
| 40 | + contextIsolation: true, |
| 41 | + nodeIntegration: false |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + const devServer = process.env.OPENCODE_PLUSPLUS_DESKTOP_RENDERER_URL; |
| 46 | + if (devServer) { |
| 47 | + await mainWindow.loadURL(devServer); |
| 48 | + } else { |
| 49 | + await mainWindow.loadFile(path.join(__dirname, "..", "renderer", "index.html")); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function commandName(): string { |
| 54 | + return process.platform === "win32" ? "opencode-plusplus.cmd" : "opencode-plusplus"; |
| 55 | +} |
| 56 | + |
| 57 | +function registerIpc(): void { |
| 58 | + ipcMain.handle("repo:select", async () => { |
| 59 | + const result = await dialog.showOpenDialog({ |
| 60 | + properties: ["openDirectory"], |
| 61 | + title: "Select repository" |
| 62 | + }); |
| 63 | + return result.canceled ? undefined : result.filePaths[0]; |
| 64 | + }); |
| 65 | + |
| 66 | + ipcMain.handle("task:run", (_event, input: RunTaskInput): RunTaskResult => { |
| 67 | + if (currentTask) return { error: "A task is already running." }; |
| 68 | + const repo = input.repo.trim(); |
| 69 | + const task = input.task.trim(); |
| 70 | + if (!repo) return { error: "Select a repository first." }; |
| 71 | + if (!task) return { error: "Enter a task first." }; |
| 72 | + if (!existsSync(repo)) return { error: `Repository does not exist: ${repo}` }; |
| 73 | + |
| 74 | + const command = commandName(); |
| 75 | + const args = ["oc", "run", task, "--repo", repo, "--max-loops", "2"]; |
| 76 | + currentRepo = repo; |
| 77 | + currentTask = spawn(command, args, { |
| 78 | + cwd: repo, |
| 79 | + shell: false, |
| 80 | + windowsHide: true |
| 81 | + }); |
| 82 | + |
| 83 | + currentTask.stdout.on("data", (chunk: Buffer) => { |
| 84 | + mainWindow?.webContents.send("task:output", { stream: "stdout", text: chunk.toString("utf8") }); |
| 85 | + }); |
| 86 | + |
| 87 | + currentTask.stderr.on("data", (chunk: Buffer) => { |
| 88 | + mainWindow?.webContents.send("task:output", { stream: "stderr", text: chunk.toString("utf8") }); |
| 89 | + }); |
| 90 | + |
| 91 | + currentTask.once("error", (error) => { |
| 92 | + mainWindow?.webContents.send("task:output", { stream: "stderr", text: `${error.message}\n` }); |
| 93 | + }); |
| 94 | + |
| 95 | + currentTask.once("close", (code, signal) => { |
| 96 | + const reportPath = currentRepo ? findLatestReport(currentRepo) : undefined; |
| 97 | + currentTask = undefined; |
| 98 | + mainWindow?.webContents.send("task:exit", { |
| 99 | + code, |
| 100 | + signal, |
| 101 | + reportPath |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + return { pid: currentTask.pid ?? null, command, args }; |
| 106 | + }); |
| 107 | + |
| 108 | + ipcMain.handle("task:stop", async () => { |
| 109 | + if (!currentTask) return { stopped: false }; |
| 110 | + const pid = currentTask.pid; |
| 111 | + if (process.platform === "win32" && pid) { |
| 112 | + spawn("taskkill", ["/pid", String(pid), "/t", "/f"], { windowsHide: true }); |
| 113 | + } else { |
| 114 | + currentTask.kill("SIGTERM"); |
| 115 | + } |
| 116 | + return { stopped: true }; |
| 117 | + }); |
| 118 | + |
| 119 | + ipcMain.handle("report:latest", (_event, repo: string) => { |
| 120 | + return findLatestReport(repo); |
| 121 | + }); |
| 122 | + |
| 123 | + ipcMain.handle("report:open", async (_event, repo: string) => { |
| 124 | + const reportPath = findLatestReport(repo); |
| 125 | + if (!reportPath) return { opened: false, error: "No orchestrator report found yet." }; |
| 126 | + const error = await shell.openPath(reportPath); |
| 127 | + return error ? { opened: false, error } : { opened: true, path: reportPath }; |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +function findLatestReport(repo: string): string | undefined { |
| 132 | + const orchestratorDir = path.join(repo, ".agent-context", "orchestrator"); |
| 133 | + if (!existsSync(orchestratorDir)) return undefined; |
| 134 | + const candidates = readdirSync(orchestratorDir, { withFileTypes: true }) |
| 135 | + .filter((entry) => entry.isDirectory()) |
| 136 | + .map((entry) => path.join(orchestratorDir, entry.name, "orchestrator.md")) |
| 137 | + .filter((file) => existsSync(file)) |
| 138 | + .map((file) => ({ file, mtimeMs: statSync(file).mtimeMs })) |
| 139 | + .sort((a, b) => b.mtimeMs - a.mtimeMs); |
| 140 | + return candidates[0]?.file; |
| 141 | +} |
| 142 | + |
| 143 | +registerIpc(); |
| 144 | + |
| 145 | +app |
| 146 | + .whenReady() |
| 147 | + .then(createWindow) |
| 148 | + .catch((error: unknown) => { |
| 149 | + console.error(error); |
| 150 | + app.quit(); |
| 151 | + }); |
| 152 | + |
| 153 | +app.on("window-all-closed", () => { |
| 154 | + if (process.platform !== "darwin") app.quit(); |
| 155 | +}); |
| 156 | + |
| 157 | +app.on("activate", () => { |
| 158 | + if (!BrowserWindow.getAllWindows().length) { |
| 159 | + createWindow().catch((error: unknown) => console.error(error)); |
| 160 | + } |
| 161 | +}); |
0 commit comments