Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .oxfmtrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
".pi/**",
".roo/**",
".windsurf/**",
"tools/oxlint/anti-slop/**"
"tools/oxlint/anti-slop/**",
"packages/review/app/vendor/**"
]
}
3 changes: 2 additions & 1 deletion .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
".pi/**",
".roo/**",
".windsurf/**",
"tools/oxlint/anti-slop/**"
"tools/oxlint/anti-slop/**",
"packages/review/app/vendor/**"
],
"rules": {
"sort-imports": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ export interface ICommonNativeHostService {
// Screenshots
getScreenshot(rect?: IRectangle, options?: INativeHostOptions & IScreenshotOptions): Promise<VSBuffer | undefined>;

/** Runs a step on a page in a window that is never shown, and captures what the step points at. */
renderHiddenPage(options: IHiddenPageRender): Promise<IHiddenPageResult>;

// GitHub mobile upload API (runs in main process to avoid CORS)
uploadFileViaMobileApi(token: string, repoId: string, fileName: string, fileBytes: VSBuffer, contentType: string): Promise<{ fileName: string; assetUrl: string; contentType: string }>;

Expand Down Expand Up @@ -369,3 +372,26 @@ export const INativeHostService = createDecorator<INativeHostService>('nativeHos
* hosts.
*/
export interface INativeHostService extends ICommonNativeHostService { }

/** A step on a page that is rendered out of sight. */
export interface IHiddenPageRender {
/** Renders that share a key share one loaded page. */
readonly key: string;
readonly url: string;
/** The page's width, in CSS pixels. */
readonly width: number;
readonly zoomFactor?: number;
/** A capture with a longer edge is scaled down to this many pixels. */
readonly maxEdge?: number;
/** Runs once, after the page loads. May answer `{ height }`, the height the page needs. */
readonly setup: string;
/** Runs for each render. Answers `{ rect, height }`: the part of the page to capture, and the height the page needs. */
readonly step: string;
}

export interface IHiddenPageResult {
/** What the step answered. */
readonly value: unknown;
/** The capture as a base64 PNG, when the step named a rect. */
readonly png?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) dev.fast. All rights reserved.
* Licensed under the MIT License. See LICENSE in the repository root for license information.
*--------------------------------------------------------------------------------------------*/

import { BrowserWindow } from 'electron';
import type { IHiddenPageRender, IHiddenPageResult } from '../common/native.js';

/** A page left idle this long gives its window back. */
const IDLE_MS = 15_000;

/** The tallest a page is captured, in CSS pixels: a capture at double density stays under the GPU's texture size. */
const MAX_HEIGHT = 8_000;

const START_HEIGHT = 900;

interface HiddenPage {
readonly window: BrowserWindow;
readonly ready: Promise<void>;
/** The last step asked of it, so steps on one page run in turn. */
last: Promise<unknown>;
idle: ReturnType<typeof setTimeout> | undefined;
}

interface StepValue {
readonly rect?: { x: number; y: number; width: number; height: number };
readonly height?: number;
}

/**
* Renders pages in windows that are never shown, so a capture does not
* scroll, resize or switch the window the user is working in. A page is as
* tall as its content asks, whatever the screen's height.
*/
export class HiddenPageRenderer {

private readonly pages = new Map<string, HiddenPage>();

async render(options: IHiddenPageRender): Promise<IHiddenPageResult> {
const page = this.pages.get(options.key) ?? this.open(options);
clearTimeout(page.idle);

const step = page.last.catch(() => undefined).then(() => this.step(page, options));
page.last = step;

try {
return await step;
} catch (error) {
this.close(options.key);
throw error;
} finally {
if (this.pages.get(options.key) === page) {
page.idle = setTimeout(() => this.close(options.key), IDLE_MS);
}
}
}

private open(options: IHiddenPageRender): HiddenPage {
const window = new BrowserWindow({
show: false,
width: options.width,
height: START_HEIGHT,
useContentSize: true,
enableLargerThanScreen: true,
skipTaskbar: true,
focusable: false,
webPreferences: {
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
// Never shown, it must still run its frames.
backgroundThrottling: false,
...(options.zoomFactor && { zoomFactor: options.zoomFactor }),
},
});

// The page shares its origin's zoom with the windows the user sees, so
// its window is sized in their pixels to give the page its own width.
const ready = window.loadURL(options.url)
.then(() => window.setContentSize(Math.round(options.width * window.webContents.getZoomFactor()), START_HEIGHT))
.then(() => window.webContents.executeJavaScript(options.setup, true))
.then((value: StepValue | undefined) => { this.grow(window, value?.height); });

const page: HiddenPage = { window, ready, last: Promise.resolve(), idle: undefined };
this.pages.set(options.key, page);
window.on('closed', () => {
if (this.pages.get(options.key) === page) {
this.pages.delete(options.key);
}
});

return page;
}

private async step(page: HiddenPage, options: IHiddenPageRender): Promise<IHiddenPageResult> {
await page.ready;

const { webContents } = page.window;
let value: StepValue | undefined = await webContents.executeJavaScript(options.step, true);

// A page taller than its window is shown again once the window holds all of it.
if (this.grow(page.window, value?.height)) {
value = await webContents.executeJavaScript(options.step, true);
}

if (!value?.rect || value.rect.width < 1 || value.rect.height < 1) {
return { value };
}

const zoom = webContents.getZoomFactor();
let image = await webContents.capturePage({
x: Math.round(value.rect.x * zoom),
y: Math.round(value.rect.y * zoom),
width: Math.round(value.rect.width * zoom),
height: Math.round(value.rect.height * zoom),
});

const { width, height } = image.getSize();
if (options.maxEdge && Math.max(width, height) > options.maxEdge) {
image = image.resize(width >= height ? { width: options.maxEdge, quality: 'best' } : { height: options.maxEdge, quality: 'best' });
}

return { value, png: image.isEmpty() ? undefined : image.toPNG().toString('base64') };
}

/** Makes the window as tall as the page, given in the page's pixels; true when it grew. */
private grow(window: BrowserWindow, height: number | undefined): boolean {
const wanted = Math.ceil(Math.min(MAX_HEIGHT, height ?? 0) * window.webContents.getZoomFactor());
const [width, current] = window.getContentSize();
if (wanted <= current) {
return false;
}

window.setContentSize(width, wanted);
return true;
}

private close(key: string): void {
const page = this.pages.get(key);
if (!page) {
return;
}

clearTimeout(page.idle);
this.pages.delete(key);
if (!page.window.isDestroyed()) {
page.window.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import { IEnvironmentMainService } from '../../environment/electron-main/environ
import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';
import { ILifecycleMainService, IRelaunchOptions } from '../../lifecycle/electron-main/lifecycleMainService.js';
import { ILogService } from '../../log/common/log.js';
import { FocusMode, ICommonNativeHostService, INativeHostOptions, INativeSystemWideKeybinding, INativeSystemWideKeybindingResult, IOSProperties, IOSStatistics, IStartTracingOptions, IToastOptions, IToastResult, IUninstallShellCommandOptions, PowerSaveBlockerType, SystemIdleState, ThermalState, IScreenshotOptions } from '../common/native.js';
import { FocusMode, ICommonNativeHostService, INativeHostOptions, INativeSystemWideKeybinding, INativeSystemWideKeybindingResult, IOSProperties, IOSStatistics, IStartTracingOptions, IToastOptions, IToastResult, IUninstallShellCommandOptions, PowerSaveBlockerType, SystemIdleState, ThermalState, IScreenshotOptions, IHiddenPageRender, IHiddenPageResult } from '../common/native.js';
import { IGlobalKeybindingsMainService } from '../../globalKeybindings/electron-main/globalKeybindingsMainService.js';
import { IProductService } from '../../product/common/productService.js';
import { IPartsSplash } from '../../theme/common/themeService.js';
import { IThemeMainService } from '../../theme/electron-main/themeMainService.js';
import { defaultWindowState, ICodeWindow } from '../../window/electron-main/window.js';
import { IColorScheme, IOpenedAuxiliaryWindow, IOpenedMainWindow, IOpenEmptyWindowOptions, IOpenWindowOptions, IPoint, IRectangle, IWindowOpenable } from '../../window/common/window.js';
import { defaultBrowserWindowOptions, IWindowsMainService, OpenContext } from '../../windows/electron-main/windows.js';
import { HiddenPageRenderer } from './hiddenPageRenderer.js';
import { isWorkspaceIdentifier, toWorkspaceIdentifier } from '../../workspace/common/workspace.js';
import { IWorkspacesManagementMainService } from '../../workspaces/electron-main/workspacesManagementMainService.js';
import { VSBuffer } from '../../../base/common/buffer.js';
Expand Down Expand Up @@ -927,6 +928,14 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
return VSBuffer.wrap(options?.format === 'png' ? image.toPNG() : image.toJPEG(95));
}

private hiddenPages: HiddenPageRenderer | undefined;

async renderHiddenPage(_windowId: number | undefined, options: IHiddenPageRender): Promise<IHiddenPageResult> {
this.hiddenPages ??= new HiddenPageRenderer();

return this.hiddenPages.render(options);
}

//#endregion


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { openChildWhiteboard } from "../../reviewChildWhiteboard.js";
import { reviewBottomScrollPadding } from "../../reviewScrollPadding.js";
import { getZoomFactor } from "../../../../base/browser/browser.js";
import { $, addDisposableListener, getWindow, type Dimension } from "../../../../base/browser/dom.js";
import { hash } from "../../../../base/common/hash.js";
import { FileAccess } from "../../../../base/common/network.js";
import { encodeBase64 } from "../../../../base/common/buffer.js";
import { mainWindow } from "../../../../base/browser/window.js";
import type { IHoverOptions, IHoverWidget } from "../../../../base/browser/ui/hover/hover.js";
Expand Down Expand Up @@ -168,6 +170,10 @@ const requestReviewApi: typeof fetch = (url, init) => fetch(url, init);
/** The longest edge of a rendered frame's PNG: a retina capture of a column-wide block, which agents read whole. */
const RENDER_MAX_EDGE = 1600;

/** A render out of sight shows the block at a reader's column width, and whole however tall. */
const RENDER_PAGE_WIDTH = 860;
const RENDER_MAX_EDGE_OUT_OF_SIGHT = 3200;

/** How long a render waits for a board that is still opening. */
const RENDER_READY_WAIT_MS = 10_000;

Expand Down Expand Up @@ -1525,7 +1531,10 @@ export class ReviewCanvasEditorPane extends EditorPane {
* Only the board open in this part can be rendered; the canvas measures
* CSS pixels, which the window's zoom scales to the capture's pixels.
*/
private async renderAnimation(args: { reviewId: string; targetId: string; frame: string }): Promise<ReviewVerbResponse> {
private async renderAnimation(args: { reviewId: string; targetId: string; frame: string; snapshot?: unknown }): Promise<ReviewVerbResponse> {
if (args.snapshot !== undefined) {
return this.renderOutOfSight(args.targetId, args.frame, args.snapshot);
}
if (this.apiContent?.reviewId !== args.reviewId) {
return { ok: false, error: "The whiteboard is not open in Desktop. Open it with session_open, then render again." };
}
Expand Down Expand Up @@ -1557,6 +1566,33 @@ export class ReviewCanvasEditorPane extends EditorPane {
}
}

/**
* The same look without touching this window: the block alone on a page
* in a window that is never shown, as tall as the block needs. The board
* need not be open.
*/
private async renderOutOfSight(targetId: string, frame: string, snapshot: unknown): Promise<ReviewVerbResponse> {
try {
const theme = this.colorScheme();
const input = JSON.stringify({ snapshot, theme });
const rendered = await this.hostService.renderHiddenPage({
// A page is for one version of one block in one theme.
key: `${hash(input)}`,
url: FileAccess.asBrowserUri("vs/review/canvas/render.html").toString(true),
width: RENDER_PAGE_WIDTH,
maxEdge: RENDER_MAX_EDGE_OUT_OF_SIGHT,
setup: `globalThis.reviewRender.mount(${input})`,
step: `globalThis.reviewRender.show(${JSON.stringify(targetId)}, ${JSON.stringify(frame)})`,
});
if (!rendered.png) {
throw new Error("Desktop could not capture the block.");
}
return { ok: true, result: { ...(rendered.value as object), png: rendered.png } };
} catch (error) {
return { ok: false, error: error instanceof Error ? error.message : String(error) };
}
}

/** The mounted canvas once it reports the board drawn, within a bound. */
private async whenCanvasReady(reviewId: string): Promise<ReviewCanvasHandle> {
const deadline = Date.now() + RENDER_READY_WAIT_MS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { VSBuffer } from '../../../../base/common/buffer.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { Event } from '../../../../base/common/event.js';
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
import { FocusMode, IScreenshotOptions } from '../../../../platform/native/common/native.js';
import { FocusMode, IHiddenPageRender, IHiddenPageResult, IScreenshotOptions } from '../../../../platform/native/common/native.js';
import { IWindowOpenable, IOpenWindowOptions, IOpenEmptyWindowOptions, IPoint, IRectangle, IOpenedMainWindow, IOpenedAuxiliaryWindow } from '../../../../platform/window/common/window.js';

export const IHostService = createDecorator<IHostService>('hostService');
Expand Down Expand Up @@ -163,6 +163,12 @@ export interface IHostService {
*/
getScreenshot(rect?: IRectangle, options?: IScreenshotOptions): Promise<VSBuffer | undefined>;

/**
* Runs a step on a page in a window that is never shown, and captures
* what the step points at.
*/
renderHiddenPage(options: IHiddenPageRender): Promise<IHiddenPageResult>;

//#endregion

//#region Native Handle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Emitter, Event } from '../../../../base/common/event.js';
import { IHostService, IToastOptions, IToastResult } from '../browser/host.js';
import { FocusMode, INativeHostService, IScreenshotOptions } from '../../../../platform/native/common/native.js';
import { FocusMode, IHiddenPageRender, IHiddenPageResult, INativeHostService, IScreenshotOptions } from '../../../../platform/native/common/native.js';
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
import { ILabelService, Verbosity } from '../../../../platform/label/common/label.js';
import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
Expand Down Expand Up @@ -230,6 +230,10 @@ class WorkbenchHostService extends Disposable implements IHostService {
return this.nativeHostService.getScreenshot(rect, options);
}

renderHiddenPage(options: IHiddenPageRender): Promise<IHiddenPageResult> {
return this.nativeHostService.renderHiddenPage(options);
}

//#endregion

//#region Native Handle
Expand Down
22 changes: 22 additions & 0 deletions apps/review-desktop/scripts/copy-canvas.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,31 @@ export async function copyCanvas(targets = canvasTargets([])) {
stylesheets,
}),
);
await writeFile(
path.join(targetRoot, "render.html"),
renderPageSource({ canvasFile: canvas.file, stylesheets }),
);
}
}

/** The page a hidden window loads to render one block for an agent. */
export function renderPageSource({ canvasFile, stylesheets }) {
return [
"<!doctype html>",
'<html><head><meta charset="utf-8">',
...stylesheets.map(
(file) => `<link rel="stylesheet" href=${JSON.stringify(`./${file}`)}>`,
),
"<style>html, body { margin: 0; }</style>",
'</head><body><div id="root"></div>',
'<script type="module">',
`import { reviewRenderPage } from ${JSON.stringify(`./${canvasFile}`)};`,
'globalThis.reviewRender = reviewRenderPage(document.getElementById("root"));',
"</script></body></html>",
"",
].join("\n");
}

export function canvasLoaderSource({ canvasFile, wasmFile, stylesheets }) {
return [
`export { clearReviewViewState, mountReviewCanvas, mountReviewSidebarSection } from ${JSON.stringify(`./${canvasFile}`)};`,
Expand Down
Loading
Loading