fix(coding-agent): guard clipboard native load

This commit is contained in:
Mario Zechner 2026-02-02 00:23:12 +01:00
parent ad8026f821
commit 287a0b606d
3 changed files with 28 additions and 21 deletions

View file

@ -1,22 +1,8 @@
import { spawnSync } from "child_process";
import { createRequire } from "module";
import { clipboard } from "./clipboard-native.js";
import { loadPhoton } from "./photon.js";
type ClipboardModule = {
hasImage: () => boolean;
getImageBinary: () => Promise<Array<number>>;
};
const require = createRequire(import.meta.url);
let Clipboard: ClipboardModule | null = null;
try {
Clipboard = require("@mariozechner/clipboard") as ClipboardModule;
} catch {
Clipboard = null;
}
export type ClipboardImage = {
bytes: Uint8Array;
mimeType: string;
@ -191,11 +177,11 @@ export async function readClipboardImage(options?: {
if (platform === "linux" && isWaylandSession(env)) {
image = readClipboardImageViaWlPaste() ?? readClipboardImageViaXclip();
} else {
if (!Clipboard || !Clipboard.hasImage()) {
if (!clipboard || !clipboard.hasImage()) {
return null;
}
const imageData = await Clipboard.getImageBinary();
const imageData = await clipboard.getImageBinary();
if (!imageData || imageData.length === 0) {
return null;
}

View file

@ -0,0 +1,21 @@
import { createRequire } from "module";
export type ClipboardModule = {
hasImage: () => boolean;
getImageBinary: () => Promise<Array<number>>;
};
const require = createRequire(import.meta.url);
let clipboard: ClipboardModule | null = null;
const hasDisplay = process.platform !== "linux" || Boolean(process.env.DISPLAY || process.env.WAYLAND_DISPLAY);
if (!process.env.TERMUX_VERSION && hasDisplay) {
try {
clipboard = require("@mariozechner/clipboard") as ClipboardModule;
} catch {
clipboard = null;
}
}
export { clipboard };

View file

@ -17,9 +17,9 @@ vi.mock("child_process", () => {
};
});
vi.mock("@mariozechner/clipboard", () => {
vi.mock("../src/utils/clipboard-native.js", () => {
return {
default: mocks.clipboard,
clipboard: mocks.clipboard,
};
});
@ -54,7 +54,7 @@ describe("readClipboardImage", () => {
mocks.clipboard.getImageBinary.mockReset();
});
test("Wayland: uses wl-paste and never calls @mariozechner/clipboard", async () => {
test("Wayland: uses wl-paste and never calls clipboard", async () => {
mocks.clipboard.hasImage.mockImplementation(() => {
throw new Error("clipboard.hasImage should not be called on Wayland");
});
@ -107,7 +107,7 @@ describe("readClipboardImage", () => {
expect(Array.from(result?.bytes ?? [])).toEqual([9, 8]);
});
test("Non-Wayland: uses @mariozechner/clipboard", async () => {
test("Non-Wayland: uses clipboard", async () => {
mocks.spawnSync.mockImplementation(() => {
throw new Error("spawnSync should not be called for non-Wayland sessions");
});