Skip to main content

tavo_ai/
ai_analysis.rs

1//! AI Analysis operations
2
3use crate::{Result, TavoClient};
4use serde::Deserialize;
5use std::collections::HashMap;
6
7/// AI Analysis operations for the Tavo AI API
8pub struct AIAnalysisOperations<'a> {
9    client: &'a TavoClient,
10}
11
12impl<'a> AIAnalysisOperations<'a> {
13    /// Create a new AIAnalysisOperations instance
14    pub fn new(client: &'a TavoClient) -> Self {
15        Self { client }
16    }
17
18    /// Analyze AI model for security risks
19    pub async fn analyze_model(
20        &self,
21        model_config: serde_json::Value,
22    ) -> Result<ModelAnalysisResult> {
23        let mut data = HashMap::new();
24        data.insert("model_config".to_string(), model_config);
25        self.client.post("/ai/analysis/model", &data).await
26    }
27
28    /// Analyze code with AI
29    pub async fn analyze_code(
30        &self,
31        code: &str,
32        language: &str,
33        analysis_type: Option<&str>,
34    ) -> Result<CodeAnalysisResult> {
35        let mut data = HashMap::new();
36        data.insert(
37            "code".to_string(),
38            serde_json::Value::String(code.to_string()),
39        );
40        data.insert(
41            "language".to_string(),
42            serde_json::Value::String(language.to_string()),
43        );
44
45        if let Some(analysis_type) = analysis_type {
46            data.insert(
47                "analysis_type".to_string(),
48                serde_json::Value::String(analysis_type.to_string()),
49            );
50        }
51
52        self.client.post("/ai/analysis/code", &data).await
53    }
54
55    /// Get AI analysis history
56    pub async fn get_history(
57        &self,
58        params: Option<HashMap<String, serde_json::Value>>,
59    ) -> Result<Vec<AIAnalysis>> {
60        match params {
61            Some(p) => {
62                self.client
63                    .get_with_params("/ai/analysis/history", &p)
64                    .await
65            }
66            None => self.client.get("/ai/analysis/history").await,
67        }
68    }
69
70    /// Get specific AI analysis
71    pub async fn get(&self, analysis_id: &str) -> Result<AIAnalysis> {
72        let url = format!("/ai/analysis/{}", analysis_id);
73        self.client.get(&url).await
74    }
75
76    /// Delete AI analysis
77    pub async fn delete(&self, analysis_id: &str) -> Result<()> {
78        let url = format!("/ai/analysis/{}", analysis_id);
79        self.client.delete(&url).await
80    }
81}
82
83/// AI model analysis result
84#[derive(Deserialize, Debug, Clone)]
85pub struct ModelAnalysisResult {
86    pub safe: bool,
87    pub risks: Vec<String>,
88    pub recommendations: HashMap<String, serde_json::Value>,
89    pub confidence_score: f32,
90}
91
92/// Code analysis result
93#[derive(Deserialize, Debug, Clone)]
94pub struct CodeAnalysisResult {
95    pub analysis_id: String,
96    pub language: String,
97    pub issues: Vec<CodeIssue>,
98    pub suggestions: Vec<String>,
99    pub complexity_score: Option<f32>,
100    pub maintainability_index: Option<f32>,
101}
102
103/// Code issue found by AI analysis
104#[derive(Deserialize, Debug, Clone)]
105pub struct CodeIssue {
106    pub id: String,
107    pub title: String,
108    pub description: String,
109    pub severity: String,
110    pub category: String,
111    pub line: u32,
112    pub column: u32,
113    pub suggestion: Option<String>,
114}
115
116/// AI analysis information
117#[derive(Deserialize, Debug, Clone)]
118pub struct AIAnalysis {
119    pub id: String,
120    pub analysis_type: String,
121    pub target: String,
122    pub status: String,
123    pub created_at: String,
124    pub completed_at: Option<String>,
125    pub result: Option<serde_json::Value>,
126}