mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 06:04:15 +00:00
fix(coding-agent): guard clipboard native load
This commit is contained in:
parent
ad8026f821
commit
287a0b606d
3 changed files with 28 additions and 21 deletions
|
|
@ -1,22 +1,8 @@
|
||||||
import { spawnSync } from "child_process";
|
import { spawnSync } from "child_process";
|
||||||
import { createRequire } from "module";
|
|
||||||
|
|
||||||
|
import { clipboard } from "./clipboard-native.js";
|
||||||
import { loadPhoton } from "./photon.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 = {
|
export type ClipboardImage = {
|
||||||
bytes: Uint8Array;
|
bytes: Uint8Array;
|
||||||
mimeType: string;
|
mimeType: string;
|
||||||
|
|
@ -191,11 +177,11 @@ export async function readClipboardImage(options?: {
|
||||||
if (platform === "linux" && isWaylandSession(env)) {
|
if (platform === "linux" && isWaylandSession(env)) {
|
||||||
image = readClipboardImageViaWlPaste() ?? readClipboardImageViaXclip();
|
image = readClipboardImageViaWlPaste() ?? readClipboardImageViaXclip();
|
||||||
} else {
|
} else {
|
||||||
if (!Clipboard || !Clipboard.hasImage()) {
|
if (!clipboard || !clipboard.hasImage()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageData = await Clipboard.getImageBinary();
|
const imageData = await clipboard.getImageBinary();
|
||||||
if (!imageData || imageData.length === 0) {
|
if (!imageData || imageData.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21
packages/coding-agent/src/utils/clipboard-native.ts
Normal file
21
packages/coding-agent/src/utils/clipboard-native.ts
Normal 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 };
|
||||||
|
|
@ -17,9 +17,9 @@ vi.mock("child_process", () => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
vi.mock("@mariozechner/clipboard", () => {
|
vi.mock("../src/utils/clipboard-native.js", () => {
|
||||||
return {
|
return {
|
||||||
default: mocks.clipboard,
|
clipboard: mocks.clipboard,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ describe("readClipboardImage", () => {
|
||||||
mocks.clipboard.getImageBinary.mockReset();
|
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(() => {
|
mocks.clipboard.hasImage.mockImplementation(() => {
|
||||||
throw new Error("clipboard.hasImage should not be called on Wayland");
|
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]);
|
expect(Array.from(result?.bytes ?? [])).toEqual([9, 8]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Non-Wayland: uses @mariozechner/clipboard", async () => {
|
test("Non-Wayland: uses clipboard", async () => {
|
||||||
mocks.spawnSync.mockImplementation(() => {
|
mocks.spawnSync.mockImplementation(() => {
|
||||||
throw new Error("spawnSync should not be called for non-Wayland sessions");
|
throw new Error("spawnSync should not be called for non-Wayland sessions");
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue