diff --git a/README.md b/README.md index 48280bb0e8..4347bf43c1 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,22 @@ openscreen export demo.openscreen -o demo.mp4 --json See the [CLI reference](https://getopenscreen.com/docs/cli/). +### Let your coding agent make the demo + +Give Claude Code, Codex or Cursor OpenScreen's MCP tools, and the skill that +teaches the whole job: + +```bash +claude mcp add openscreen -- "/Applications/Openscreen.app/Contents/MacOS/Openscreen" mcp +npx skills add getopenscreen/openscreen --skill openscreen-demo +``` + +Then ask: *"Make a demo video of this feature with OpenScreen."* The agent plans +the storyboard, records the app, edits the take with OpenScreen's own editing +tools, exports the MP4, checks it frame by frame, and drafts the post for X, +which it publishes only once you say yes. The executable on Windows and Linux, +and the other clients, are in [AI editing](https://getopenscreen.com/docs/ai-editing/#letting-an-agent-make-the-whole-video). + ## Installation Every platform has a recommended route below. On Windows that is the Microsoft Store; everywhere else it is the installer from the [GitHub Releases](https://github.com/getopenscreen/openscreen/releases) page. diff --git a/electron/ai-edition/document-service.ts b/electron/ai-edition/document-service.ts index 1228e728d2..c1e0ea20b5 100644 --- a/electron/ai-edition/document-service.ts +++ b/electron/ai-edition/document-service.ts @@ -243,6 +243,11 @@ export class DocumentService { return path.join(this.projectsRoot, `${safe}${PROJECT_FILE_EXTENSION}`); } + /** The project's own file, for a reader that takes a path (the export runner). */ + filePathFor(projectId: string): string { + return this.fileFor(projectId); + } + private legacyFileFor(projectId: string): string { const safe = safeProjectId(projectId); return path.join(this.projectsRoot, `${safe}${LEGACY_PROJECT_FILE_EXTENSION}`); diff --git a/electron/cli/args.test.ts b/electron/cli/args.test.ts index ddf012321b..dd4a373cc5 100644 --- a/electron/cli/args.test.ts +++ b/electron/cli/args.test.ts @@ -230,6 +230,11 @@ describe("parseCliArgs", () => { }); }); + it("parses mcp, which takes no argument", () => { + expect(parse(["mcp"])).toEqual({ kind: "mcp" }); + expect(parse(["mcp", "--port", "1"])).toMatchObject({ kind: "error" }); + }); + it("parses info and help", () => { expect(parse(["info", "demo.openscreen", "--json"])).toMatchObject({ kind: "info", diff --git a/electron/cli/args.ts b/electron/cli/args.ts index 23926da479..9c6532d9d7 100644 --- a/electron/cli/args.ts +++ b/electron/cli/args.ts @@ -22,10 +22,16 @@ export interface CliErrorCommand { message: string; } +/** Serve the MCP tools on stdin/stdout, to the agent that started this process. */ +export interface CliMcpCommand { + kind: "mcp"; +} + export type CliCommand = ( | CliRequest | CliInfoCommand | CliPackCommand + | CliMcpCommand | CliHelpCommand | CliErrorCommand ) & { @@ -60,6 +66,7 @@ const SUBCOMMANDS = new Set([ "pack", "captions", "info", + "mcp", "help", "--help", "-h", @@ -75,6 +82,7 @@ Usage: openscreen captions Add auto-captions (on-device Whisper) to a project [--min-words <1-12>] [--max-words <1-12>] openscreen info [--json] Inspect a project file + openscreen mcp Serve the agent tools over MCP on stdio openscreen help Show this help Export options: @@ -165,6 +173,10 @@ export function parseCliArgs( if (sub === "sources") return parseSources(args.slice(1), cwd); if (sub === "pack") return parsePack(args.slice(1), cwd); if (sub === "captions") return parseCaptions(args.slice(1), cwd); + if (sub === "mcp") { + if (args.length > 1) throw new Error(`Unexpected argument for mcp: ${args[1]}`); + return { kind: "mcp" }; + } return parseInfo(args.slice(1), cwd); } catch (error) { return { kind: "error", message: error instanceof Error ? error.message : String(error) }; diff --git a/electron/cli/cliMain.ts b/electron/cli/cliMain.ts index d64b27e5dd..1aeffd0793 100644 --- a/electron/cli/cliMain.ts +++ b/electron/cli/cliMain.ts @@ -7,18 +7,17 @@ import path from "node:path"; import readline from "node:readline"; import { fileURLToPath } from "node:url"; import { app, BrowserWindow, ipcMain, session, systemPreferences } from "electron"; -import type { - CliDoneResult, - CliProgressEvent, - CliRequest, - CliSourcesResult, -} from "../../src/lib/cliContracts"; +import type { CliProgressEvent, CliSourcesResult } from "../../src/lib/cliContracts"; import { isDiagnosticModeEnabled } from "../diagnostics/main-log-buffer"; import { getSelectedDesktopSource, registerIpcHandlers } from "../ipc/handlers"; +import { openVideoInHiddenWindow } from "../mcp/hidden-video"; +import { serveMcpOverStdio } from "../mcp/stdio-server"; import { registerSttIpc } from "../stt"; import { ASSET_BASE_URL_ARG } from "../windows"; import { CLI_USAGE, type CliCommand } from "./args"; import { runInfoCommand, runPackCommand } from "./projectCommands"; +import { createRunnerHost } from "./runnerHost"; +import { openStdin } from "./stdin"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"]; @@ -157,7 +156,7 @@ function registerAppHandlersForCli(cliWindowRef: () => BrowserWindow | null) { const notAvailable = () => { throw new Error("Window not available in CLI mode"); }; - registerIpcHandlers( + return registerIpcHandlers( noop, // createEditorWindow: recording finished; runner drives completion notAvailable, // createSourceSelectorWindow notAvailable, // createCountdownOverlayWindow @@ -213,7 +212,7 @@ function setupRecordStopSignals(stop: (reason: string) => void): void { try { // Touching process.stdin can throw on Windows GUI-subsystem builds when // spawned with stdio "ignore"; signals and --duration still work then. - const rl = readline.createInterface({ input: process.stdin }); + const rl = readline.createInterface({ input: openStdin() }); rl.on("line", (line) => { const trimmed = line.trim().toLowerCase(); if (trimmed === "stop" || trimmed === "q" || trimmed === "quit") { @@ -299,8 +298,8 @@ export function runCli(command: CliCommand): void { app.on("window-all-closed", () => { // Completion is signalled via cli-done; a vanished window is a failure - // only while the run is still in flight. - if (finished) return; + // only while the run is still in flight. The MCP server outlives its runners. + if (finished || command.kind === "mcp") return; output.error("Renderer window closed unexpectedly"); app.exit(1); }); @@ -382,7 +381,7 @@ export function runCli(command: CliCommand): void { milestone("media access checked"); let cliWindow: BrowserWindow | null = null; - registerAppHandlersForCli(() => cliWindow); + const { agentServices } = registerAppHandlersForCli(() => cliWindow); milestone("app handlers registered"); // Speech-to-text backs the captions command; registered by the GUI boot @@ -397,107 +396,24 @@ export function runCli(command: CliCommand): void { }); ipcMain.handle("update-global-shortcut", () => ({ success: false })); - const request: CliRequest = command; - ipcMain.handle("cli-get-request", () => { - milestone("renderer asked for the request"); - return request; + // The latest runner is the app's "main window" for the recording handlers, as the + // HUD is in the GUI. + const runners = createRunnerHost(ipcMain, (windowType): BrowserWindow => { + const runnerWindow = loadRunnerWindow(windowType); + cliWindow = runnerWindow; + return runnerWindow; }); - ipcMain.on("cli-log", (_event, level: string, message: string) => { - if (level === "error") { - output.error(message); - } else { - output.info(message); - output.event("log", { message }); - } - }); - ipcMain.on("cli-progress", (_event, progress: CliProgressEvent) => { - output.progress(progress); - }); - - ipcMain.handle("cli-done", async (_event, result: CliDoneResult) => { - milestone(`renderer reported done (success=${result.success})`); - if (finished) return; - finished = true; - - try { - if (result.success && command.kind === "record" && command.projectOut) { - if (result.projectData !== undefined) { - await writeProjectFile(command.projectOut, result.projectData); - result.projectPath = command.projectOut; - } - } - if (result.success && command.kind === "captions" && result.projectData !== undefined) { - await writeProjectFile(command.projectPath, result.projectData); - } - // -o writes where no wrapper can redirect. stdout itself is fine -- - // Chromium logs to stderr -- but a caller does not control what wraps - // this process, and xvfb-run on Ubuntu merges the two, which is enough - // to make `sources --json | jq` fail under the very tool used to run a - // GUI binary headlessly. - if ( - result.success && - command.kind === "sources" && - command.jsonOutPath && - result.sources - ) { - // Same as writeProjectFile above: `record --project out/x` creates - // `out/`, so `sources -o out/x.json` has to as well. Without it a - // fully successful enumeration is lost on every channel at once -- - // the ENOENT lands in the catch below, which clears result.success - // and so skips printSources and the `done` payload alike. - await fs.mkdir(path.dirname(command.jsonOutPath), { recursive: true }); - // Write beside the target and rename over it. writeFile truncates - // first, so a failure part-way through -- a full disk is the easy - // case -- would leave a half-written file where website/docs/cli.md promises - // an earlier run's result is untouched by a later failure. rename is - // atomic within a directory, so the reader sees the old file or the - // new one and never a torn one. - const tmpOutPath = `${command.jsonOutPath}.${process.pid}.tmp`; - try { - await fs.writeFile( - tmpOutPath, - `${JSON.stringify(result.sources, null, 2)}\n`, - "utf8", - ); - await fs.rename(tmpOutPath, command.jsonOutPath); - } catch (writeError) { - // Best-effort: the original error is what the caller needs to see. - await fs.rm(tmpOutPath, { force: true }).catch(() => undefined); - throw writeError; - } - } - } catch (error) { - result.success = false; - result.error = `Run succeeded but writing the output file failed: ${String(error)}`; - } - - if (result.success) { - for (const warning of result.warnings ?? []) { - output.info(`Warning: ${warning}`); - output.event("warning", { message: warning }); - } - if (command.kind === "sources" && result.sources) { - printSources(output, result.sources); - } else if (command.kind === "captions") { - output.info( - `Added ${result.captionCount ?? 0} caption annotation(s) → ${result.projectPath}`, - ); - } else if (command.kind === "export") { - output.info(`Exported ${result.format ?? ""} → ${result.outputPath}`); - } else { - output.info(`Recording saved → ${result.screenVideoPath}`); - if (result.cursorDataPath) output.info(`Cursor data → ${result.cursorDataPath}`); - if (result.projectPath) output.info(`Project → ${result.projectPath}`); - } - output.event("done", { ...result }); - } else { - output.error(result.error ?? "Unknown failure"); - output.event("done", { success: false, error: result.error }); - } - // Give the renderer a beat to resolve the invoke before exiting. - setTimeout(() => app.exit(result.success ? 0 : 1), 50); - }); + if (command.kind === "mcp") { + await serveMcpOverStdio({ + ...agentServices, + runners, + openVideo: openVideoInHiddenWindow, + onClose: () => app.exit(0), + }); + milestone("mcp server listening on stdio"); + return; + } if (command.kind === "record") { const stop = (reason: string) => { @@ -508,39 +424,112 @@ export function runCli(command: CliCommand): void { setupRecordStopSignals(stop); } - milestone("cli ipc registered; resolving window type"); - const windowType = { - export: "cli-export", - record: "cli-record", - sources: "cli-sources", - captions: "cli-captions", - }[command.kind]; - cliWindow = loadRunnerWindow(windowType); - milestone(`runner window created (${windowType})`); - cliWindow.webContents.on("did-finish-load", () => milestone("renderer did-finish-load")); - cliWindow.webContents.on("dom-ready", () => milestone("renderer dom-ready")); + milestone("cli ipc registered; starting the runner"); + const run = runners.start(command, { + onRequest: () => milestone("renderer asked for the request"), + onLog: (level, message) => { + if (level === "error") { + output.error(message); + } else { + output.info(message); + output.event("log", { message }); + } + }, + onProgress: (progress) => output.progress(progress), + }); + const runnerWindow = run.window; + milestone(`runner window created (${command.kind})`); + runnerWindow.webContents.on("did-finish-load", () => milestone("renderer did-finish-load")); + runnerWindow.webContents.on("dom-ready", () => milestone("renderer dom-ready")); // Surface renderer console errors/warnings on stderr — the hidden window // has no other way to show what went wrong (toasts are invisible). - cliWindow.webContents.on("console-message", (details) => { + runnerWindow.webContents.on("console-message", (details) => { if (details.level === "error" || details.level === "warning") { safeWrite(process.stderr, `[renderer] ${details.message}\n`); } }); - cliWindow.webContents.on("did-fail-load", (_e, code, description) => { - output.error(`Failed to load runner window: ${description} (${code})`); - app.exit(1); - }); + // A crash or a failed load ends the run through `run.done`, reported below. app.on("child-process-gone", (_e, details) => { milestone(`child-process-gone: ${details.type} (${details.reason})`); }); - cliWindow.webContents.on("render-process-gone", (_e, details) => { - output.error(`Renderer crashed: ${details.reason}`); - app.exit(1); - }); output.event("started", { command: command.kind }); + + const result = await run.done; + milestone(`renderer reported done (success=${result.success})`); + finished = true; + + try { + if (result.success && command.kind === "record" && command.projectOut) { + if (result.projectData !== undefined) { + await writeProjectFile(command.projectOut, result.projectData); + result.projectPath = command.projectOut; + } + } + if (result.success && command.kind === "captions" && result.projectData !== undefined) { + await writeProjectFile(command.projectPath, result.projectData); + } + // -o writes where no wrapper can redirect. stdout itself is fine -- + // Chromium logs to stderr -- but a caller does not control what wraps + // this process, and xvfb-run on Ubuntu merges the two, which is enough + // to make `sources --json | jq` fail under the very tool used to run a + // GUI binary headlessly. + if (result.success && command.kind === "sources" && command.jsonOutPath && result.sources) { + // Same as writeProjectFile above: `record --project out/x` creates + // `out/`, so `sources -o out/x.json` has to as well. Without it a + // fully successful enumeration is lost on every channel at once -- + // the ENOENT lands in the catch below, which clears result.success + // and so skips printSources and the `done` payload alike. + await fs.mkdir(path.dirname(command.jsonOutPath), { recursive: true }); + // Write beside the target and rename over it. writeFile truncates + // first, so a failure part-way through -- a full disk is the easy + // case -- would leave a half-written file where website/docs/cli.md promises + // an earlier run's result is untouched by a later failure. rename is + // atomic within a directory, so the reader sees the old file or the + // new one and never a torn one. + const tmpOutPath = `${command.jsonOutPath}.${process.pid}.tmp`; + try { + await fs.writeFile(tmpOutPath, `${JSON.stringify(result.sources, null, 2)}\n`, "utf8"); + await fs.rename(tmpOutPath, command.jsonOutPath); + } catch (writeError) { + // Best-effort: the original error is what the caller needs to see. + await fs.rm(tmpOutPath, { force: true }).catch(() => undefined); + throw writeError; + } + } + } catch (error) { + result.success = false; + result.error = `Run succeeded but writing the output file failed: ${String(error)}`; + } + + if (result.success) { + for (const warning of result.warnings ?? []) { + output.info(`Warning: ${warning}`); + output.event("warning", { message: warning }); + } + if (command.kind === "sources" && result.sources) { + printSources(output, result.sources); + } else if (command.kind === "captions") { + output.info( + `Added ${result.captionCount ?? 0} caption annotation(s) → ${result.projectPath}`, + ); + } else if (command.kind === "export") { + output.info(`Exported ${result.format ?? ""} → ${result.outputPath}`); + } else { + output.info(`Recording saved → ${result.screenVideoPath}`); + if (result.cursorDataPath) output.info(`Cursor data → ${result.cursorDataPath}`); + if (result.projectPath) output.info(`Project → ${result.projectPath}`); + } + output.event("done", { ...result }); + } else { + output.error(result.error ?? "Unknown failure"); + output.event("done", { success: false, error: result.error }); + } + + // Give the renderer a beat to resolve the invoke before exiting. + setTimeout(() => app.exit(result.success ? 0 : 1), 50); }) .catch((error) => { output.error(error instanceof Error ? (error.stack ?? error.message) : String(error)); diff --git a/electron/cli/runnerHost.test.ts b/electron/cli/runnerHost.test.ts new file mode 100644 index 0000000000..3f0abde775 --- /dev/null +++ b/electron/cli/runnerHost.test.ts @@ -0,0 +1,140 @@ +import { EventEmitter } from "node:events"; +import { describe, expect, it } from "vitest"; +import type { CliRequest } from "../../src/lib/cliContracts"; +import { createRunnerHost, type RunnerIpc } from "./runnerHost"; + +type Listener = (event: { sender: { id: number } }, ...args: unknown[]) => unknown; + +/** `ipcMain`, reduced to what a runner window can call on it. */ +class FakeIpc { + readonly listeners = new Map(); + handle(channel: string, listener: Listener) { + this.listeners.set(channel, listener); + } + on(channel: string, listener: Listener) { + this.listeners.set(channel, listener); + } + /** What the renderer of window `id` does when it calls `channel`. */ + call(id: number, channel: string, ...args: unknown[]) { + return this.listeners.get(channel)?.({ sender: { id } }, ...args); + } +} + +/** Whatever a runner window event passes its listener. */ +type WindowListener = (...args: never[]) => void; + +class FakeWindow { + readonly events = new EventEmitter(); + readonly webContents; + destroyed = false; + constructor(id: number) { + const listen = (listener: WindowListener) => listener as (...args: unknown[]) => void; + this.webContents = { + id, + once: (event: string, listener: WindowListener) => this.events.once(event, listen(listener)), + on: (event: string, listener: WindowListener) => this.events.on(event, listen(listener)), + }; + } + isDestroyed() { + return this.destroyed; + } + destroy() { + this.destroyed = true; + this.events.emit("destroyed"); + } +} + +function setup() { + const ipc = new FakeIpc(); + const opened: { windowType: string; window: FakeWindow }[] = []; + let nextId = 1; + const host = createRunnerHost(ipc as unknown as RunnerIpc, (windowType) => { + const window = new FakeWindow(nextId++); + opened.push({ windowType, window }); + return window; + }); + return { ipc, host, opened }; +} + +const sources: CliRequest = { kind: "sources" }; + +describe("createRunnerHost", () => { + it("hands each runner its own request and result, several at once", async () => { + const { ipc, host, opened } = setup(); + const first = host.start(sources); + const second = host.start({ + kind: "captions", + projectPath: "/p.openscreen", + minWordsPerCaption: 2, + maxWordsPerCaption: 7, + }); + expect(opened.map((o) => o.windowType)).toEqual(["cli-sources", "cli-captions"]); + + expect(ipc.call(2, "cli-get-request")).toMatchObject({ kind: "captions" }); + expect(ipc.call(1, "cli-get-request")).toEqual(sources); + + ipc.call(2, "cli-done", { success: true, captionCount: 3 }); + ipc.call(1, "cli-done", { success: false, error: "no display" }); + await expect(second.done).resolves.toEqual({ success: true, captionCount: 3 }); + await expect(first.done).resolves.toEqual({ success: false, error: "no display" }); + }); + + it("routes logs and progress to the run they came from", () => { + const { ipc, host } = setup(); + const logs: string[] = []; + const progress: number[] = []; + let asked = 0; + host.start(sources, { + onRequest: () => asked++, + onLog: (level, message) => logs.push(`${level}:${message}`), + onProgress: (p) => progress.push(p.percentage), + }); + host.start(sources, { onLog: () => logs.push("wrong run") }); + + ipc.call(1, "cli-get-request"); + ipc.call(1, "cli-log", "info", "Recording started"); + ipc.call(1, "cli-progress", { percentage: 40 }); + expect({ asked, logs, progress }).toEqual({ + asked: 1, + logs: ["info:Recording started"], + progress: [40], + }); + }); + + it("fails a run whose renderer crashes or whose page does not load, instead of hanging", async () => { + const { host, opened } = setup(); + const crashed = host.start(sources); + opened[0].window.events.emit("render-process-gone", {}, { reason: "oom" }); + await expect(crashed.done).resolves.toEqual({ + success: false, + error: "Renderer crashed: oom", + }); + + const unloaded = host.start(sources); + // A subframe failing is not the runner failing. + opened[1].window.events.emit("did-fail-load", {}, -3, "ABORTED", "x", false); + opened[1].window.events.emit("did-fail-load", {}, -6, "FILE_NOT_FOUND", "x", true); + await expect(unloaded.done).resolves.toMatchObject({ + success: false, + error: expect.stringContaining("FILE_NOT_FOUND (-6)"), + }); + }); + + it("closes a finished runner's window once, when asked", () => { + const { host, opened } = setup(); + const run = host.start(sources); + run.close(); + run.close(); + expect(opened[0].window.isDestroyed()).toBe(true); + }); + + it("fails a run whose window goes away, and ignores a second result", async () => { + const { ipc, host, opened } = setup(); + const run = host.start(sources); + opened[0].window.events.emit("destroyed"); + await expect(run.done).resolves.toMatchObject({ success: false }); + // Too late: the run is over, and nothing answers for an unknown window. + expect(ipc.call(1, "cli-done", { success: true })).toBeUndefined(); + expect(ipc.call(1, "cli-get-request")).toBeNull(); + }); +}); diff --git a/electron/cli/runnerHost.ts b/electron/cli/runnerHost.ts new file mode 100644 index 0000000000..7475aa2dc8 --- /dev/null +++ b/electron/cli/runnerHost.ts @@ -0,0 +1,129 @@ +// Runs the hidden renderer runners (record, export, sources, captions) and +// routes their IPC by window, so one process can run any number of them: the +// one-shot CLI runs one, the MCP server one per tool call. +// +// No `electron` import: the IPC surface and the window factory are handed in, +// so the routing stays unit-testable. + +import type { IpcMain } from "electron"; +import type { CliDoneResult, CliProgressEvent, CliRequest } from "../../src/lib/cliContracts"; + +/** The slice of a BrowserWindow this needs. */ +export interface RunnerWindow { + isDestroyed(): boolean; + destroy(): void; + webContents: { + id: number; + once(event: "destroyed", listener: () => void): unknown; + on( + event: "render-process-gone", + listener: (event: unknown, details: { reason: string }) => void, + ): unknown; + on( + event: "did-fail-load", + listener: ( + event: unknown, + errorCode: number, + errorDescription: string, + validatedURL: string, + isMainFrame: boolean, + ) => void, + ): unknown; + }; +} + +interface RunnerIpcEvent { + sender: { id: number }; +} + +/** The slice of `ipcMain` this needs. */ +export type RunnerIpc = Pick; + +export interface RunnerHandlers { + /** The renderer asked for its request: it booted. */ + onRequest?(): void; + onLog?(level: string, message: string): void; + onProgress?(progress: CliProgressEvent): void; +} + +export interface RunnerRun { + window: W; + /** The runner's own result. A window that closes, crashes or fails to load fails the run. */ + done: Promise; + /** Closes the runner's window: a long-lived caller does once it has the result. */ + close(): void; +} + +interface PendingRun { + request: CliRequest; + handlers: RunnerHandlers; + resolve(result: CliDoneResult): void; +} + +export const RUNNER_WINDOW_TYPES = { + export: "cli-export", + record: "cli-record", + sources: "cli-sources", + captions: "cli-captions", +} as const satisfies Record; + +export function createRunnerHost( + ipc: RunnerIpc, + openWindow: (windowType: string) => W, +) { + const runs = new Map(); + const runFor = (event: RunnerIpcEvent) => runs.get(event.sender.id); + + ipc.handle("cli-get-request", (event) => { + const run = runFor(event); + run?.handlers.onRequest?.(); + return run?.request ?? null; + }); + ipc.on("cli-log", (event, level: string, message: string) => { + runFor(event)?.handlers.onLog?.(level, message); + }); + ipc.on("cli-progress", (event, progress: CliProgressEvent) => { + runFor(event)?.handlers.onProgress?.(progress); + }); + ipc.handle("cli-done", (event, result: CliDoneResult) => { + const run = runFor(event); + if (!run) return; + runs.delete(event.sender.id); + run.resolve(result); + }); + + return { + start(request: CliRequest, handlers: RunnerHandlers = {}): RunnerRun { + const window = openWindow(RUNNER_WINDOW_TYPES[request.kind]); + const id = window.webContents.id; + // Registered in the same tick the load starts in: the renderer cannot ask + // for its request before this line has run. + const done = new Promise((resolve) => { + runs.set(id, { request, handlers, resolve }); + }); + // A runner that never reports would otherwise hold its caller forever. + const fail = (error: string) => { + const run = runs.get(id); + if (!run) return; + runs.delete(id); + run.resolve({ success: false, error }); + }; + window.webContents.once("destroyed", () => + fail("The runner window closed before it finished"), + ); + window.webContents.on("render-process-gone", (_event, details) => + fail(`Renderer crashed: ${details.reason}`), + ); + window.webContents.on("did-fail-load", (_event, code, description, _url, isMainFrame) => { + if (isMainFrame) fail(`Failed to load runner window: ${description} (${code})`); + }); + return { + window, + done, + close: () => { + if (!window.isDestroyed()) window.destroy(); + }, + }; + }, + }; +} diff --git a/electron/cli/stdin.test.ts b/electron/cli/stdin.test.ts new file mode 100644 index 0000000000..23e2479e27 --- /dev/null +++ b/electron/cli/stdin.test.ts @@ -0,0 +1,13 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { openStdin } from "./stdin"; + +describe("openStdin", () => { + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("keeps Node's own stdin outside Windows", () => { + vi.stubGlobal("process", { ...process, platform: "linux", stdin: "the-stdin" }); + expect(openStdin()).toBe("the-stdin"); + }); +}); diff --git a/electron/cli/stdin.ts b/electron/cli/stdin.ts new file mode 100644 index 0000000000..6e8d886b17 --- /dev/null +++ b/electron/cli/stdin.ts @@ -0,0 +1,16 @@ +// The process's real stdin. On Windows, Electron's main process gets a +// `process.stdin` that ends at once whatever the parent piped in (measured with +// Electron 41), so a pipe has to be read from descriptor 0 directly. + +import net from "node:net"; +import type { Readable } from "node:stream"; + +export function openStdin(): Readable { + if (process.platform !== "win32") return process.stdin; + try { + return new net.Socket({ fd: 0, readable: true, writable: false }); + } catch { + // Not a pipe (a console, or nothing at all): no one can write to us anyway. + return process.stdin; + } +} diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts index fdc79c32a0..c71996c1cd 100644 --- a/electron/ipc/handlers.ts +++ b/electron/ipc/handlers.ts @@ -79,6 +79,7 @@ import { import { EditorDocumentHost } from "../mcp/editor-document-host"; import { McpController } from "../mcp/mcp-controller"; import { McpSettingsStore } from "../mcp/mcp-settings-store"; +import type { McpToolDeps } from "../mcp/openscreen-mcp-server"; import { type AudioPeaksResult, getAudioPeaks } from "../media/audioPeaks"; import { readCursorRecordingFile as readCursorRecordingFileFrom, @@ -5030,13 +5031,14 @@ export function registerIpcHandlers( // read on every call — separate from the in-app agent's "Project edits", so // turning the server on grants a client read access and nothing more. const mcpSettings = new McpSettingsStore(app.getPath("userData"), safeStorage); - const mcpController = new McpController(mcpSettings, { + const mcpToolDeps: McpToolDeps = { host: new EditorDocumentHost(ipcMain), projects: aiEditionDocuments, editsAllowed: () => mcpSettings.getSettings().allowEdits, cursor: agentCursorTelemetryReader, version: app.getVersion(), - }); + }; + const mcpController = new McpController(mcpSettings, mcpToolDeps); registerNativeBridgeHandlers({ getPlatform: () => process.platform, @@ -5096,5 +5098,15 @@ export function registerIpcHandlers( getMcpController: () => mcpController, }); - return { mcpController }; + // `openscreen mcp` serves the same tools on stdio, from the CLI process. + return { + mcpController, + agentServices: { + mcpToolDeps, + documents: aiEditionDocuments, + // The reader the export runner's auto zoom reads through. + readCursorTelemetry: async (videoPath: string) => + (await readCursorTelemetryFile(videoPath)).samples, + }, + }; } diff --git a/electron/mcp/hidden-video.ts b/electron/mcp/hidden-video.ts new file mode 100644 index 0000000000..a2f94e6c89 --- /dev/null +++ b/electron/mcp/hidden-video.ts @@ -0,0 +1,106 @@ +// A video decoded by Chromium's own