feat add multi-platform support

This commit is contained in:
LaZzyMan 2026-02-10 14:38:01 +08:00
parent 56030f9291
commit b488126830
6 changed files with 64 additions and 25 deletions

View file

@ -33,6 +33,13 @@ const external = [
'@lydell/node-pty-linux-x64',
'@lydell/node-pty-win32-arm64',
'@lydell/node-pty-win32-x64',
'@teddyzhu/clipboard',
'@teddyzhu/clipboard-darwin-arm64',
'@teddyzhu/clipboard-darwin-x64',
'@teddyzhu/clipboard-linux-x64-gnu',
'@teddyzhu/clipboard-linux-arm64-gnu',
'@teddyzhu/clipboard-win32-x64-msvc',
'@teddyzhu/clipboard-win32-arm64-msvc',
];
esbuild

View file

@ -41,7 +41,6 @@
"@iarna/toml": "^2.2.5",
"@modelcontextprotocol/sdk": "^1.25.1",
"@qwen-code/qwen-code-core": "file:../core",
"@teddyzhu/clipboard": "^0.0.5",
"@types/update-notifier": "^6.0.8",
"ansi-regex": "^6.2.2",
"command-exists": "^1.2.9",
@ -96,6 +95,15 @@
"typescript": "^5.3.3",
"vitest": "^3.1.1"
},
"optionalDependencies": {
"@teddyzhu/clipboard": "^0.0.5",
"@teddyzhu/clipboard-darwin-arm64": "0.0.5",
"@teddyzhu/clipboard-darwin-x64": "0.0.5",
"@teddyzhu/clipboard-linux-x64-gnu": "0.0.5",
"@teddyzhu/clipboard-linux-arm64-gnu": "0.0.5",
"@teddyzhu/clipboard-win32-x64-msvc": "0.0.5",
"@teddyzhu/clipboard-win32-arm64-msvc": "0.0.5"
},
"engines": {
"node": ">=20"
}

View file

@ -16,6 +16,12 @@ const mockHasFormat = vi.fn();
const mockGetImageData = vi.fn();
vi.mock('@teddyzhu/clipboard', () => ({
default: {
ClipboardManager: vi.fn().mockImplementation(() => ({
hasFormat: mockHasFormat,
getImageData: mockGetImageData,
})),
},
ClipboardManager: vi.fn().mockImplementation(() => ({
hasFormat: mockHasFormat,
getImageData: mockGetImageData,
@ -53,26 +59,19 @@ describe('clipboardUtils', () => {
expect(result).toBe(false);
});
it('should log errors in DEBUG mode', async () => {
it('should return false and not throw when error occurs in DEBUG mode', async () => {
const originalEnv = process.env;
vi.stubGlobal('process', {
...process,
env: { ...originalEnv, DEBUG: '1' },
});
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
mockHasFormat.mockImplementation(() => {
throw new Error('Test error');
});
await clipboardHasImage();
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error checking clipboard for image:',
expect.any(Error),
);
consoleErrorSpy.mockRestore();
const result = await clipboardHasImage();
expect(result).toBe(false);
});
});
@ -101,26 +100,19 @@ describe('clipboardUtils', () => {
expect(result).toBe(null);
});
it('should log errors in DEBUG mode', async () => {
it('should return null and not throw when error occurs in DEBUG mode', async () => {
const originalEnv = process.env;
vi.stubGlobal('process', {
...process,
env: { ...originalEnv, DEBUG: '1' },
});
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
mockHasFormat.mockImplementation(() => {
throw new Error('Test error');
});
await saveClipboardImage('/tmp/test');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error saving clipboard image:',
expect.any(Error),
);
consoleErrorSpy.mockRestore();
const result = await saveClipboardImage('/tmp/test');
expect(result).toBe(null);
});
});

View file

@ -6,18 +6,41 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { ClipboardManager } from '@teddyzhu/clipboard';
import { createDebugLogger } from '@qwen-code/qwen-code-core';
const debugLogger = createDebugLogger('CLIPBOARD_UTILS');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ClipboardModule = any;
let cachedClipboardModule: ClipboardModule | null = null;
let clipboardLoadAttempted = false;
async function getClipboardModule(): Promise<ClipboardModule | null> {
if (clipboardLoadAttempted) return cachedClipboardModule;
clipboardLoadAttempted = true;
try {
const modName = '@teddyzhu/clipboard';
cachedClipboardModule = await import(modName);
return cachedClipboardModule;
} catch (_e) {
debugLogger.error(
'Failed to load @teddyzhu/clipboard native module. Clipboard image features will be unavailable.',
);
return null;
}
}
/**
* Checks if the system clipboard contains an image
* @returns true if clipboard contains an image
*/
export async function clipboardHasImage(): Promise<boolean> {
try {
const clipboard = new ClipboardManager();
const mod = await getClipboardModule();
if (!mod) return false;
const clipboard = new mod.ClipboardManager();
return clipboard.hasFormat('image');
} catch (error) {
debugLogger.error('Error checking clipboard for image:', error);
@ -34,7 +57,9 @@ export async function saveClipboardImage(
targetDir?: string,
): Promise<string | null> {
try {
const clipboard = new ClipboardManager();
const mod = await getClipboardModule();
if (!mod) return null;
const clipboard = new mod.ClipboardManager();
if (!clipboard.hasFormat('image')) {
return null;

View file

@ -100,7 +100,7 @@ const CLAUDE_TOOLS_MAPPING: Record<string, string | string[]> = {
Grep: 'Grep',
KillShell: 'None',
NotebookEdit: 'None',
Read: ['ReadFile', 'ReadManyFiles'],
Read: 'ReadFile',
Skill: 'Skill',
Task: 'Task',
TodoWrite: 'TodoWrite',

View file

@ -161,6 +161,13 @@ const distPackageJson = {
'@lydell/node-pty-linux-x64': '1.1.0',
'@lydell/node-pty-win32-arm64': '1.1.0',
'@lydell/node-pty-win32-x64': '1.1.0',
'@teddyzhu/clipboard': '0.0.5',
'@teddyzhu/clipboard-darwin-arm64': '0.0.5',
'@teddyzhu/clipboard-darwin-x64': '0.0.5',
'@teddyzhu/clipboard-linux-x64-gnu': '0.0.5',
'@teddyzhu/clipboard-linux-arm64-gnu': '0.0.5',
'@teddyzhu/clipboard-win32-x64-msvc': '0.0.5',
'@teddyzhu/clipboard-win32-arm64-msvc': '0.0.5',
},
engines: rootPackageJson.engines,
};