/**
* 剪贴板工具
*/
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
export class ClipboardTools {
getToolDefinitions() {
return [
{
name: 'get_clipboard',
description: '获取剪贴板内容',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'set_clipboard',
description: '设置剪贴板内容',
inputSchema: {
type: 'object',
properties: {
text: { type: 'string', description: '要设置的文本内容' },
},
required: ['text'],
},
},
{
name: 'clear_clipboard',
description: '清空剪贴板',
inputSchema: {
type: 'object',
properties: {},
},
},
];
}
canHandle(toolName) {
const tools = ['get_clipboard', 'set_clipboard', 'clear_clipboard'];
return tools.includes(toolName);
}
async executeTool(name, args) {
switch (name) {
case 'get_clipboard':
return await this.getClipboard();
case 'set_clipboard':
return await this.setClipboard(args.text);
case 'clear_clipboard':
return await this.clearClipboard();
default:
throw new Error(`未知工具: ${name}`);
}
}
async getClipboard() {
try {
const { stdout } = await execAsync('powershell -Command "Get-Clipboard"', {
shell: 'powershell.exe'
});
return { success: true, content: stdout.trim() };
} catch (error) {
return { success: false, error: error.message };
}
}
async setClipboard(text) {
try {
// 转义特殊字符
const escapedText = text.replace(/"/g, '`"').replace(/\$/g, '`$');
await execAsync(`powershell -Command "Set-Clipboard -Value '${escapedText}'"`, {
shell: 'powershell.exe'
});
return { success: true, message: '剪贴板已设置', length: text.length };
} catch (error) {
return { success: false, error: error.message };
}
}
async clearClipboard() {
try {
await execAsync('powershell -Command "Set-Clipboard -Value $null"', {
shell: 'powershell.exe'
});
return { success: true, message: '剪贴板已清空' };
} catch (error) {
return { success: false, error: error.message };
}
}
}