import * as z from "zod" import { tool } from "langchain" import os from 'os' import { promisify } from 'util' import { exec } from 'child_process' const execPromise = promisify(exec) /** * 获取操作系统信息工具 * @returns {string} - 返回操作系统信息的 JSON 字符串 */ export const getOsInfoTool = tool( async (input) => { try { const osInfo = { platform: os.platform(), arch: os.arch(), release: os.release(), hostname: os.hostname(), uptime: `${Math.round(os.uptime() / 3600)} hours`, nodeVersion: process.version, homeDir: os.homedir(), tempDir: os.tmpdir(), userInfo: os.userInfo() } // 获取Windows详细信息 if (os.platform() === 'win32') { try { const { stdout } = await execPromise('powershell -Command "Get-WmiObject Win32_OperatingSystem | Select-Object Caption,Version,BuildNumber,OSArchitecture | ConvertTo-Json"') const winInfo = JSON.parse(stdout) osInfo.windows = { caption: winInfo.Caption || 'N/A', version: winInfo.Version || 'N/A', buildNumber: winInfo.BuildNumber || 'N/A', architecture: winInfo.OSArchitecture || 'N/A' } } catch (error) { console.error('Error getting Windows info:', error) } } // 获取Linux详细信息 else if (os.platform() === 'linux') { try { const { stdout } = await execPromise('cat /etc/os-release') const lines = stdout.trim().split('\n') const linuxInfo = {} lines.forEach(line => { const match = line.match(/(.+?)=(.+)/) if (match) { linuxInfo[match[1]] = match[2].replace(/"/g, '') } }) osInfo.linux = linuxInfo } catch (error) { console.error('Error getting Linux info:', error) } } // 获取macOS详细信息 else if (os.platform() === 'darwin') { try { const { stdout } = await execPromise('sw_vers') const lines = stdout.trim().split('\n') const macInfo = {} lines.forEach(line => { const match = line.match(/(.+?):\s+(.+)/) if (match) { macInfo[match[1]] = match[2] } }) osInfo.macOS = macInfo } catch (error) { console.error('Error getting macOS info:', error) } } return JSON.stringify(osInfo, null, 2) } catch (error) { console.error('Error getting OS info:', error) return JSON.stringify({ error: error.message }, null, 2) } }, { name: "get_os_info", description: `获取操作系统信息,包括平台、架构、版本、主机名、运行时间等`, schema: z.object({}) } )