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
2 changes: 1 addition & 1 deletion packages/@n8n/computer-use/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"@jitsi/robotjs": "^0.6.21",
"@modelcontextprotocol/sdk": "1.26.0",
"@n8n/mcp-browser": "workspace:*",
"@napi-rs/image": "^1.12.0",
"@vscode/ripgrep": "^1.17.1",
"eventsource": "^3.0.6",
"node-screenshots": "^0.2.8",
"picocolors": "catalog:",
"sharp": "^0.34.5",
"yargs-parser": "21.1.1",
"zod": "catalog:",
"zod-to-json-schema": "catalog:"
Expand Down
18 changes: 0 additions & 18 deletions packages/@n8n/computer-use/src/sharp.d.ts

This file was deleted.

23 changes: 12 additions & 11 deletions packages/@n8n/computer-use/src/tools/screenshot/screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { screenshotTool, screenshotRegionTool } from './screenshot';

jest.mock('node-screenshots');

const mockSharp = jest.fn<unknown, unknown[]>();
jest.mock('sharp', () => ({
const mockFromRgbaPixels = jest.fn<unknown, unknown[]>();
jest.mock('@napi-rs/image', () => ({
__esModule: true,
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
default: (...args: unknown[]) => mockSharp(...args),
// eslint-disable-next-line @typescript-eslint/naming-convention
Transformer: {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
fromRgbaPixels: (...args: unknown[]) => mockFromRgbaPixels(...args),
},
}));

const MockMonitor = Monitor as jest.MockedClass<typeof Monitor>;
Expand Down Expand Up @@ -75,13 +78,11 @@ function makeMockMonitor(opts: {
}

beforeEach(() => {
// sharp(buffer, opts)[.resize()].jpeg().toBuffer() → fake JPEG
const mockToBuffer = jest.fn().mockResolvedValue(Buffer.from('fake-jpeg'));
const mockJpeg = jest.fn().mockReturnValue({ toBuffer: mockToBuffer });
const mockJpeg = jest.fn().mockResolvedValue(Buffer.from('fake-jpeg'));
const mockResize = jest.fn();
const pipeline = { resize: mockResize, jpeg: mockJpeg };
mockResize.mockReturnValue(pipeline);
mockSharp.mockReturnValue(pipeline);
mockFromRgbaPixels.mockReturnValue(pipeline);
});

describe('screen_screenshot tool', () => {
Expand Down Expand Up @@ -136,7 +137,7 @@ describe('screen_screenshot tool', () => {

await screenshotTool.execute({}, DUMMY_CONTEXT);

const pipeline = mockSharp.mock.results[0].value as { resize: jest.Mock };
const pipeline = mockFromRgbaPixels.mock.results[0].value as { resize: jest.Mock };
expect(pipeline.resize).toHaveBeenCalledWith(1920, 1080);
});

Expand All @@ -151,7 +152,7 @@ describe('screen_screenshot tool', () => {

await screenshotTool.execute({}, DUMMY_CONTEXT);

const pipeline = mockSharp.mock.results[0].value as { resize: jest.Mock };
const pipeline = mockFromRgbaPixels.mock.results[0].value as { resize: jest.Mock };
// No HiDPI resize, but LLM downscale kicks in (1920x1080 → 1024x576)
expect(pipeline.resize).toHaveBeenCalledWith(1024, 576);
});
Expand Down Expand Up @@ -252,7 +253,7 @@ describe('screen_screenshot_region tool', () => {
await screenshotRegionTool.execute({ x: 100, y: 200, width: 400, height: 300 }, DUMMY_CONTEXT);

// Cropped image (800×600 physical) must be resized to logical 400×300
const pipeline = mockSharp.mock.results[0].value as { resize: jest.Mock };
const pipeline = mockFromRgbaPixels.mock.results[0].value as { resize: jest.Mock };
expect(pipeline.resize).toHaveBeenCalledWith(400, 300);
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/@n8n/computer-use/src/tools/screenshot/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ async function toJpeg(
logicalWidth?: number,
logicalHeight?: number,
): Promise<Buffer> {
const { default: sharp } = await import('sharp');
let pipeline = sharp(rawBuffer, { raw: { width, height, channels: 4 } });
const { Transformer } = await import('@napi-rs/image');
let pipeline = Transformer.fromRgbaPixels(rawBuffer, width, height);
if (logicalWidth && logicalHeight && (width !== logicalWidth || height !== logicalHeight)) {
pipeline = pipeline.resize(logicalWidth, logicalHeight);
}
Expand All @@ -32,7 +32,7 @@ async function toJpeg(
const scale = maxDim / Math.max(w, h);
pipeline = pipeline.resize(Math.round(w * scale), Math.round(h * scale));
}
return await pipeline.jpeg({ quality: 85 }).toBuffer();
return await pipeline.jpeg(85);
}

export const screenshotTool: ToolDefinition<typeof screenshotSchema> = {
Expand Down
155 changes: 146 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading