Skip to main content

git_iris/agents/
core.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::sync::Arc;
4
5use crate::config::Config;
6use crate::git::GitRepo;
7
8/// Simplified Agent Backend that works with Rig's provider system
9#[derive(Debug, Clone)]
10pub struct AgentBackend {
11    pub provider_name: String,
12    /// Primary model for complex tasks
13    pub model: String,
14    /// Fast model for simple/bounded tasks (subagents, parsing, etc.)
15    pub fast_model: String,
16}
17
18impl AgentBackend {
19    #[must_use]
20    pub fn new(provider_name: String, model: String, fast_model: String) -> Self {
21        Self {
22            provider_name,
23            model,
24            fast_model,
25        }
26    }
27
28    /// Create backend from Git-Iris configuration
29    ///
30    /// # Errors
31    ///
32    /// Returns an error when the configured provider name is invalid or missing configuration.
33    pub fn from_config(config: &Config) -> Result<Self> {
34        let provider: crate::providers::Provider = config
35            .default_provider
36            .parse()
37            .map_err(|_| anyhow::anyhow!("Invalid provider: {}", config.default_provider))?;
38
39        let provider_config = config
40            .get_provider_config(&config.default_provider)
41            .ok_or_else(|| {
42                anyhow::anyhow!("No configuration for provider: {}", config.default_provider)
43            })?;
44
45        Ok(Self {
46            provider_name: config.default_provider.clone(),
47            model: provider_config.effective_model(provider).to_string(),
48            fast_model: provider_config.effective_fast_model(provider).to_string(),
49        })
50    }
51}
52
53/// Agent context containing Git repository and configuration
54#[derive(Debug, Clone)]
55pub struct AgentContext {
56    pub config: Config,
57    pub git_repo: Arc<GitRepo>,
58}
59
60impl AgentContext {
61    #[must_use]
62    pub fn new(config: Config, git_repo: GitRepo) -> Self {
63        Self {
64            config,
65            git_repo: Arc::new(git_repo),
66        }
67    }
68
69    #[must_use]
70    pub fn repo(&self) -> &GitRepo {
71        &self.git_repo
72    }
73
74    #[must_use]
75    pub fn config(&self) -> &Config {
76        &self.config
77    }
78}
79
80/// Task execution result
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct TaskResult {
83    pub success: bool,
84    pub message: String,
85    pub data: Option<serde_json::Value>,
86    pub confidence: f64,
87    pub execution_time: Option<std::time::Duration>,
88}
89
90impl TaskResult {
91    #[must_use]
92    pub fn success(message: String) -> Self {
93        Self {
94            success: true,
95            message,
96            data: None,
97            confidence: 1.0,
98            execution_time: None,
99        }
100    }
101
102    #[must_use]
103    pub fn success_with_data(message: String, data: serde_json::Value) -> Self {
104        Self {
105            success: true,
106            message,
107            data: Some(data),
108            confidence: 1.0,
109            execution_time: None,
110        }
111    }
112
113    #[must_use]
114    pub fn failure(message: String) -> Self {
115        Self {
116            success: false,
117            message,
118            data: None,
119            confidence: 0.0,
120            execution_time: None,
121        }
122    }
123
124    #[must_use]
125    pub fn with_confidence(mut self, confidence: f64) -> Self {
126        self.confidence = confidence;
127        self
128    }
129
130    #[must_use]
131    pub fn with_execution_time(mut self, duration: std::time::Duration) -> Self {
132        self.execution_time = Some(duration);
133        self
134    }
135}