Files
api_health/agent/tools/system/hardware_info.js
2026-05-30 16:41:26 +08:00

113 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 getHardwareInfoTool = tool(
async (input) => {
try {
const hardwareInfo = {
cpu: {
cores: os.cpus().length,
model: os.cpus()[0].model,
speed: os.cpus()[0].speed
},
memory: {
total: `${(os.totalmem() / (1024 ** 3)).toFixed(2)} GB`,
free: `${(os.freemem() / (1024 ** 3)).toFixed(2)} GB`
},
disks: [],
gpu: []
}
// 获取硬盘信息Windows
if (os.platform() === 'win32') {
try {
const { stdout } = await execPromise('powershell -Command "Get-WmiObject Win32_DiskDrive | Select-Object Model,InterfaceType,Size | ConvertTo-Json"')
const diskData = JSON.parse(stdout)
const diskArray = Array.isArray(diskData) ? diskData : [diskData]
hardwareInfo.disks = diskArray.map(disk => ({
model: disk.Model || 'N/A',
interfaceType: disk.InterfaceType || 'N/A',
size: disk.Size ? `${(parseInt(disk.Size) / (1024 * 1024 * 1024)).toFixed(2)} GB` : 'N/A'
})).filter(item => item)
} catch (error) {
console.error('Error getting disk info:', error)
}
// 获取显卡信息Windows
try {
const { stdout } = await execPromise('wmic path win32_videocontroller get name')
hardwareInfo.gpu = stdout.trim().split('\n').slice(1).filter(line => line.trim())
} catch (error) {
console.error('Error getting GPU info:', error)
}
}
// 获取硬盘和显卡信息Linux
else if (os.platform() === 'linux') {
try {
const { stdout } = await execPromise('lsblk -o NAME,MODEL,SIZE,TYPE | grep disk')
const diskLines = stdout.trim().split('\n')
hardwareInfo.disks = diskLines.map(line => {
const parts = line.trim().split(/\s+/)
if (parts.length >= 3) {
return {
name: parts[0],
model: parts.slice(1, -1).join(' '),
size: parts[parts.length - 1]
}
}
return null
}).filter(item => item)
} catch (error) {
console.error('Error getting disk info:', error)
}
try {
const { stdout } = await execPromise('lspci | grep VGA')
hardwareInfo.gpu = stdout.trim().split('\n').map(line => line.trim())
} catch (error) {
console.error('Error getting GPU info:', error)
}
}
// 获取硬盘和显卡信息macOS
else if (os.platform() === 'darwin') {
try {
const { stdout } = await execPromise('diskutil list')
hardwareInfo.disks = [{
info: stdout.trim()
}]
} catch (error) {
console.error('Error getting disk info:', error)
}
try {
const { stdout } = await execPromise('system_profiler SPDisplaysDataType')
hardwareInfo.gpu = [{
info: stdout.trim()
}]
} catch (error) {
console.error('Error getting GPU info:', error)
}
}
return JSON.stringify(hardwareInfo, null, 2)
} catch (error) {
console.error('Error getting hardware info:', error)
return JSON.stringify({ error: error.message }, null, 2)
}
},
{
name: "get_hardware_info",
description: `获取系统硬件配置信息包括CPU、内存、硬盘、显卡等`,
schema: z.object({})
}
)