This commit is contained in:
lik
2026-09-09 17:36:25 +08:00
parent 1f2da22cae
commit d6efb180dd
54 changed files with 0 additions and 2000 deletions
+112
View File
@@ -0,0 +1,112 @@
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({})
}
)