hippox-drivers 0.3.5

🦛All indivisible atomic driver units in Hippox.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! GPU information driver
//!
//! This driver provides functionality to get detailed GPU information including
//! model, vendor, driver, and memory specifications.
use crate::{
    DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
    types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
/// Driver for getting GPU information
#[derive(Debug)]
pub struct GpuInfoDriver;
#[async_trait::async_trait]
impl Driver for GpuInfoDriver {
    /// Returns the unique name of this driver
    fn name(&self) -> &str {
        "gpu_info"
    }
    /// Returns a brief description of the driver's functionality
    fn description(&self) -> &str {
        "Get detailed GPU information including model, vendor, driver, and memory"
    }
    /// Returns detailed usage guidance for LLMs
    fn usage_hint(&self) -> &str {
        "Use this skill to get GPU specifications and capabilities"
    }
    /// Returns the parameter definitions for this driver
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![];
    }
    /// Returns an example call for this driver
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "gpu_info",
            "parameters": {}
        }));
    }
    /// Returns an example output from this driver
    fn example_output(&self) -> String {
        return r#"GPU Information:
Name: NVIDIA GeForce RTX 3080
Vendor: NVIDIA Corporation
Driver Version: 525.125.06
Total Memory: 10240 MB
Memory Type: GDDR6X
PCIe Speed: 16 GT/s
PCIe Width: x16"#
            .to_string();
    }
    /// Returns the category of this driver
    fn category(&self) -> DriverCategory {
        return DriverCategory::OperatingSystemGpu;
    }
    /// Executes the driver with the given parameters
    async fn execute(
        &self,
        _parameters: &HashMap<String, Value>,
        _callback: Option<&dyn DriverCallback>,
        _context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing gpu_info driver");
        let gpus = detect_gpus()?;
        if gpus.is_empty() {
            info!("No GPU detected");
            return Ok("No GPU detected".to_string());
        }
        info!("Detected {} GPU(s)", gpus.len());
        let mut output = String::from("GPU Information:\n");
        for (i, gpu) in gpus.iter().enumerate() {
            if i > 0 {
                output.push_str("\n");
            }
            output.push_str(&format!("Name: {}\n", gpu.name));
            output.push_str(&format!("Vendor: {}\n", gpu.vendor));
            output.push_str(&format!("Driver Version: {}\n", gpu.driver_version));
            output.push_str(&format!("Total Memory: {} MB\n", gpu.total_memory_mb));
            output.push_str(&format!("Memory Type: {}\n", gpu.memory_type));
            output.push_str(&format!("PCIe Speed: {}\n", gpu.pcie_speed));
            output.push_str(&format!("PCIe Width: x{}\n", gpu.pcie_width));
            if let Some(bios) = &gpu.bios_version {
                output.push_str(&format!("BIOS Version: {}\n", bios));
            }
            if let Some(serial) = &gpu.serial_number {
                output.push_str(&format!("Serial: {}\n", serial));
            }
        }
        return Ok(output);
    }
}
/// Internal GPU information structure
#[derive(Debug, Clone)]
struct GpuInfo {
    pub name: String,
    pub vendor: String,
    pub driver_version: String,
    pub total_memory_mb: u64,
    pub memory_type: String,
    pub pcie_speed: String,
    pub pcie_width: u8,
    pub bios_version: Option<String>,
    pub serial_number: Option<String>,
}
/// Detects GPUs on the system
fn detect_gpus() -> DriverResult<Vec<GpuInfo>> {
    #[cfg(target_os = "linux")]
    {
        debug!("Detecting GPUs on Linux");
        let mut gpus = Vec::new();
        // Try NVIDIA
        debug!("Trying NVIDIA nvidia-smi for GPU detection");
        if let Ok(output) = crate::common::hidden_cmd("nvidia-smi")
            .args(&["--query-gpu", "name,driver_version,memory.total,memory.type,pcie.link.gen.current,pcie.link.width.current,bios_version,serial"])
            .args(&["--format", "csv,noheader"])
            .output()
        {
            if output.status.success() {
                if let Ok(output_str) = String::from_utf8(output.stdout) {
                    for line in output_str.lines() {
                        let parts: Vec<&str> = line.split(',').collect();
                        if parts.len() >= 6 {
                            gpus.push(GpuInfo {
                                name: parts[0].trim().to_string(),
                                vendor: "NVIDIA Corporation".to_string(),
                                driver_version: parts[1].trim().to_string(),
                                total_memory_mb: parts[2].trim().split(' ').next().map(|s| s.parse::<u64>().unwrap_or(0)).unwrap_or(0),
                                memory_type: parts[3].trim().to_string(),
                                pcie_speed: format!("{} GT/s", parts[4].trim()),
                                pcie_width: parts[5].trim().parse::<u8>().unwrap_or(16),
                                bios_version: parts.get(6).map(|s| s.trim().to_string()),
                                serial_number: parts.get(7).map(|s| s.trim().to_string()),
                            });
                        }
                    }
                    info!("Detected {} NVIDIA GPU(s)", gpus.len());
                }
            }
        }
        // Try AMD via rocm-smi
        if gpus.is_empty() {
            debug!("Trying AMD rocm-smi for GPU detection");
            if let Ok(output) =
                crate::common::hidden_cmd("rocm-smi").args(&["--showproductname", "--showdriverversion", "--showmeminfo", "vram"]).output()
            {
                if output.status.success() {
                    if let Ok(output_str) = String::from_utf8(output.stdout) {
                        let mut name = "Unknown".to_string();
                        let mut driver = "Unknown".to_string();
                        let mut memory = 0;
                        for line in output_str.lines() {
                            if line.contains("GPU") && line.contains("Product Name") {
                                if let Some(n) = line.split(':').nth(1) {
                                    name = n.trim().to_string();
                                }
                            }
                            if line.contains("Driver Version") {
                                if let Some(d) = line.split(':').nth(1) {
                                    driver = d.trim().to_string();
                                }
                            }
                            if line.contains("VRAM") && line.contains("Total") {
                                if let Some(m) =
                                    line.split_whitespace().find(|s| s.ends_with("MB")).and_then(|s| s.trim_end_matches("MB").parse::<u64>().ok())
                                {
                                    memory = m;
                                }
                            }
                        }
                        if !name.is_empty() && name != "Unknown" {
                            gpus.push(GpuInfo {
                                name,
                                vendor: "AMD".to_string(),
                                driver_version: driver,
                                total_memory_mb: memory,
                                memory_type: "GDDR6".to_string(),
                                pcie_speed: "16 GT/s".to_string(),
                                pcie_width: 16,
                                bios_version: None,
                                serial_number: None,
                            });
                            info!("Detected AMD GPU via rocm-smi");
                        }
                    }
                }
            }
        }
        // Try AMD via lspci
        if gpus.is_empty() {
            debug!("Trying lspci for AMD GPU detection");
            if let Ok(output) = crate::common::hidden_cmd("lspci")
                .args(&["-v", "-nn", "-d", "1002:"]) // AMD PCI vendor ID
                .output()
            {
                if output.status.success() {
                    if let Ok(output_str) = String::from_utf8(output.stdout) {
                        for line in output_str.lines() {
                            if line.contains("VGA") || line.contains("Display") {
                                if let Some(name) = line.split('(').next().map(|s| s.trim()) {
                                    gpus.push(GpuInfo {
                                        name: name.to_string(),
                                        vendor: "AMD".to_string(),
                                        driver_version: "Unknown".to_string(),
                                        total_memory_mb: 0,
                                        memory_type: "Unknown".to_string(),
                                        pcie_speed: "Unknown".to_string(),
                                        pcie_width: 16,
                                        bios_version: None,
                                        serial_number: None,
                                    });
                                    info!("Detected AMD GPU via lspci");
                                }
                            }
                        }
                    }
                }
            }
        }
        // Try Intel via lspci
        if gpus.is_empty() {
            debug!("Trying lspci for Intel GPU detection");
            if let Ok(output) = crate::common::hidden_cmd("lspci")
                .args(&["-v", "-nn", "-d", "8086:"]) // Intel PCI vendor ID
                .output()
            {
                if output.status.success() {
                    if let Ok(output_str) = String::from_utf8(output.stdout) {
                        for line in output_str.lines() {
                            if line.contains("VGA") || line.contains("Display") {
                                if let Some(name) = line.split('(').next().map(|s| s.trim()) {
                                    gpus.push(GpuInfo {
                                        name: name.to_string(),
                                        vendor: "Intel Corporation".to_string(),
                                        driver_version: "Unknown".to_string(),
                                        total_memory_mb: 0,
                                        memory_type: "Shared".to_string(),
                                        pcie_speed: "Unknown".to_string(),
                                        pcie_width: 16,
                                        bios_version: None,
                                        serial_number: None,
                                    });
                                    info!("Detected Intel GPU via lspci");
                                }
                            }
                        }
                    }
                }
            }
        }
        if gpus.is_empty() {
            info!("No GPU detected on Linux");
            gpus.push(GpuInfo {
                name: "Unknown GPU".to_string(),
                vendor: "Unknown".to_string(),
                driver_version: "Unknown".to_string(),
                total_memory_mb: 0,
                memory_type: "Unknown".to_string(),
                pcie_speed: "Unknown".to_string(),
                pcie_width: 16,
                bios_version: None,
                serial_number: None,
            });
        }
        return Ok(gpus);
    }
    #[cfg(target_os = "windows")]
    {
        debug!("Detecting GPUs on Windows");
        return get_windows_gpus();
    }
    #[cfg(target_os = "macos")]
    {
        debug!("Detecting GPUs on macOS");
        let mut gpus = Vec::new();
        if let Ok(output) = crate::common::hidden_cmd("system_profiler").args(&["SPDisplaysDataType", "-json"]).output() {
            if output.status.success() {
                if let Ok(output_str) = String::from_utf8(output.stdout) {
                    if let Ok(json) = serde_json::from_str::<serde_json::Value>(&output_str) {
                        if let Some(displays) = json.get("SPDisplaysDataType").and_then(|v| v.as_array()) {
                            for display in displays {
                                if let (Some(name), Some(vendor)) =
                                    (display.get("sppci_model").and_then(|v| v.as_str()), display.get("sppci_vendor").and_then(|v| v.as_str()))
                                {
                                    let memory = display
                                        .get("spdisplays_vram")
                                        .and_then(|v| v.as_str())
                                        .map(|s| {
                                            let bytes = s.split(' ').next().unwrap_or("0");
                                            bytes.parse::<u64>().unwrap_or(0) / 1024 / 1024
                                        })
                                        .unwrap_or(0);
                                    gpus.push(GpuInfo {
                                        name: name.to_string(),
                                        vendor: vendor.to_string(),
                                        driver_version: display
                                            .get("spdisplays_metalfamily")
                                            .and_then(|v| v.as_str())
                                            .unwrap_or("Unknown")
                                            .to_string(),
                                        total_memory_mb: memory,
                                        memory_type: "Unknown".to_string(),
                                        pcie_speed: "Unknown".to_string(),
                                        pcie_width: 16,
                                        bios_version: None,
                                        serial_number: None,
                                    });
                                }
                            }
                            info!("Detected {} GPU(s) on macOS", gpus.len());
                        }
                    }
                }
            }
        }
        if gpus.is_empty() {
            info!("No GPU detected on macOS");
            gpus.push(GpuInfo {
                name: "Unknown GPU (macOS)".to_string(),
                vendor: "Unknown".to_string(),
                driver_version: "Unknown".to_string(),
                total_memory_mb: 0,
                memory_type: "Unknown".to_string(),
                pcie_speed: "Unknown".to_string(),
                pcie_width: 16,
                bios_version: None,
                serial_number: None,
            });
        }
        return Ok(gpus);
    }
    #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
    {
        debug!("GPU detection not supported on this platform");
        return Ok(vec![GpuInfo {
            name: format!("Unknown GPU ({})", std::env::consts::OS),
            vendor: "Unknown".to_string(),
            driver_version: "Unknown".to_string(),
            total_memory_mb: 0,
            memory_type: "Unknown".to_string(),
            pcie_speed: "Unknown".to_string(),
            pcie_width: 16,
            bios_version: None,
            serial_number: None,
        }]);
    }
}
/// Gets GPU information on Windows
#[cfg(target_os = "windows")]
fn get_windows_gpus() -> DriverResult<Vec<GpuInfo>> {
    use std::process::Command;
    debug!("Getting GPU info on Windows via PowerShell WMI");
    let mut gpus = Vec::new();
    let output = crate::common::hidden_cmd("powershell")
        .args(&[
            "-Command",
            "Get-CimInstance -Namespace root/cimv2 -ClassName Win32_VideoController | Select-Object Name, DriverVersion, AdapterRAM, VideoProcessor, VideoModeDescription"
        ])
        .output();
    if let Ok(output) = output {
        if output.status.success() {
            if let Ok(output_str) = String::from_utf8(output.stdout) {
                let mut current_gpu = GpuInfo {
                    name: "Unknown".to_string(),
                    vendor: "Unknown".to_string(),
                    driver_version: "Unknown".to_string(),
                    total_memory_mb: 0,
                    memory_type: "Unknown".to_string(),
                    pcie_speed: "Unknown".to_string(),
                    pcie_width: 16,
                    bios_version: None,
                    serial_number: None,
                };
                for line in output_str.lines() {
                    if line.contains(":") {
                        let parts: Vec<&str> = line.split(':').collect();
                        if parts.len() >= 2 {
                            let key = parts[0].trim();
                            let value = parts[1].trim();
                            if key == "Name" {
                                if !current_gpu.name.is_empty() && current_gpu.name != "Unknown" {
                                    gpus.push(current_gpu.clone());
                                }
                                current_gpu.name = value.to_string();
                            } else if key == "DriverVersion" {
                                current_gpu.driver_version = value.to_string();
                            } else if key == "AdapterRAM" {
                                if let Ok(ram) = value.parse::<u64>() {
                                    current_gpu.total_memory_mb = ram / (1024 * 1024);
                                }
                            } else if key == "VideoProcessor" {
                                if value.contains("NVIDIA") {
                                    current_gpu.vendor = "NVIDIA Corporation".to_string();
                                    current_gpu.memory_type = "GDDR6".to_string();
                                } else if value.contains("AMD") || value.contains("Radeon") {
                                    current_gpu.vendor = "AMD".to_string();
                                    current_gpu.memory_type = "GDDR6".to_string();
                                } else if value.contains("Intel") {
                                    current_gpu.vendor = "Intel Corporation".to_string();
                                    current_gpu.memory_type = "Shared".to_string();
                                }
                            }
                        }
                    }
                }
                if !current_gpu.name.is_empty() && current_gpu.name != "Unknown" {
                    gpus.push(current_gpu);
                }
                info!("Detected {} GPU(s) on Windows", gpus.len());
            }
        }
    }
    if gpus.is_empty() {
        info!("No GPU detected on Windows");
        gpus.push(GpuInfo {
            name: "Unknown GPU (Windows)".to_string(),
            vendor: "Unknown".to_string(),
            driver_version: "Unknown".to_string(),
            total_memory_mb: 0,
            memory_type: "Unknown".to_string(),
            pcie_speed: "Unknown".to_string(),
            pcie_width: 16,
            bios_version: None,
            serial_number: None,
        });
    }
    return Ok(gpus);
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_gpu_info_metadata() {
        let driver = GpuInfoDriver;
        assert_eq!(driver.name(), "gpu_info");
        assert_eq!(driver.category(), DriverCategory::OperatingSystemGpu);
    }
}