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

102 lines
3.6 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 getInstalledSoftwareTool = tool(
async (input) => {
try {
const softwareInfo = {
platform: os.platform(),
software: []
}
// 获取Windows已安装软件
if (os.platform() === 'win32') {
try {
const { stdout } = await execPromise('powershell -Command "Get-WmiObject Win32_Product | Select-Object Name,Version,Vendor,InstallDate | ConvertTo-Json"')
const softwareData = JSON.parse(stdout)
const softwareArray = Array.isArray(softwareData) ? softwareData : [softwareData]
softwareInfo.software = softwareArray.map(soft => ({
name: soft.Name || 'N/A',
version: soft.Version || 'N/A',
vendor: soft.Vendor || 'N/A',
installDate: soft.InstallDate || 'N/A'
})).filter(item => item)
} catch (error) {
console.error('Error getting installed software:', error)
}
}
// 获取Linux已安装软件
else if (os.platform() === 'linux') {
try {
// 尝试使用dpkgDebian/Ubuntu
const { stdout: dpkgOutput } = await execPromise('dpkg -l 2>/dev/null || echo "N/A"')
if (dpkgOutput && dpkgOutput !== 'N/A') {
const lines = dpkgOutput.trim().split('\n').slice(5)
softwareInfo.software = lines.map(line => {
const parts = line.trim().split(/\s+/)
if (parts.length >= 3) {
return {
name: parts[1],
version: parts[2],
description: parts.slice(3).join(' ')
}
}
return null
}).filter(item => item)
} else {
// 尝试使用rpmRHEL/CentOS
const { stdout: rpmOutput } = await execPromise('rpm -qa 2>/dev/null || echo "N/A"')
if (rpmOutput && rpmOutput !== 'N/A') {
const lines = rpmOutput.trim().split('\n')
softwareInfo.software = lines.map(line => {
const match = line.match(/(.+)-(.+)-(.+)/)
if (match) {
return {
name: match[1],
version: `${match[2]}-${match[3]}`
}
}
return null
}).filter(item => item)
}
}
} catch (error) {
console.error('Error getting installed software:', error)
}
}
// 获取macOS已安装软件
else if (os.platform() === 'darwin') {
try {
const { stdout } = await execPromise('find /Applications -name "*.app" -type d | sort')
const apps = stdout.trim().split('\n')
softwareInfo.software = apps.map(app => ({
name: app.split('/').pop().replace('.app', ''),
path: app
})).filter(item => item)
} catch (error) {
console.error('Error getting installed software:', error)
}
}
return JSON.stringify(softwareInfo, null, 2)
} catch (error) {
console.error('Error getting installed software:', error)
return JSON.stringify({ error: error.message }, null, 2)
}
},
{
name: "get_installed_software",
description: `获取系统已安装的软件信息,包括软件名称、版本、供应商等`,
schema: z.object({})
}
)