Skip to main content

ai_session/agent/
mod.rs

1//! Agent-specific session management for ccswarm integration
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Agent types supported by ai-session
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum AgentType {
9    /// Claude Code agent
10    ClaudeCode,
11    /// Aider agent
12    Aider,
13    /// OpenAI Codex
14    Codex,
15    /// Custom LLM agent
16    Custom,
17    /// Standard shell (no AI)
18    Shell,
19}
20
21/// Agent-specific configuration
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct AgentConfig {
24    /// Agent type
25    pub agent_type: AgentType,
26    /// Startup command (e.g., "claude", "aider")
27    pub startup_command: Option<String>,
28    /// Prompt prefix for commands
29    pub prompt_prefix: Option<String>,
30    /// Prompt suffix for commands
31    pub prompt_suffix: Option<String>,
32    /// Response parsing pattern
33    pub response_pattern: Option<String>,
34    /// Timeout for responses
35    pub response_timeout_ms: u64,
36    /// Enable streaming output
37    pub streaming: bool,
38    /// Custom settings
39    pub custom_settings: HashMap<String, serde_json::Value>,
40}
41
42impl Default for AgentConfig {
43    fn default() -> Self {
44        Self {
45            agent_type: AgentType::Shell,
46            startup_command: None,
47            prompt_prefix: None,
48            prompt_suffix: None,
49            response_pattern: None,
50            response_timeout_ms: 30000,
51            streaming: false,
52            custom_settings: HashMap::new(),
53        }
54    }
55}
56
57impl AgentConfig {
58    /// Create Claude Code agent configuration
59    pub fn claude_code() -> Self {
60        Self {
61            agent_type: AgentType::ClaudeCode,
62            startup_command: Some("claude".to_string()),
63            prompt_prefix: None,
64            prompt_suffix: Some("\n".to_string()),
65            response_pattern: Some(r"(?s).*\n\n(.+)$".to_string()),
66            response_timeout_ms: 60000,
67            streaming: true,
68            custom_settings: HashMap::new(),
69        }
70    }
71
72    /// Create Aider agent configuration
73    pub fn aider() -> Self {
74        Self {
75            agent_type: AgentType::Aider,
76            startup_command: Some("aider".to_string()),
77            prompt_prefix: None,
78            prompt_suffix: Some("\n".to_string()),
79            response_pattern: Some(r"(?s).*\n\n(.+)$".to_string()),
80            response_timeout_ms: 60000,
81            streaming: true,
82            custom_settings: HashMap::new(),
83        }
84    }
85}
86
87/// Agent session wrapper with enhanced capabilities
88pub struct AgentSession {
89    /// Base session ID
90    pub session_id: String,
91    /// Agent configuration
92    pub config: AgentConfig,
93    /// Conversation history
94    pub history: Vec<ConversationTurn>,
95    /// Current context tokens
96    pub context_tokens: usize,
97    /// Active task
98    pub current_task: Option<AgentTask>,
99}
100
101/// Single conversation turn
102#[derive(Debug, Clone, Serialize, Deserialize)]
103pub struct ConversationTurn {
104    /// Timestamp
105    pub timestamp: chrono::DateTime<chrono::Utc>,
106    /// User input
107    pub input: String,
108    /// Agent response
109    pub response: String,
110    /// Token count
111    pub tokens: usize,
112    /// Execution time in ms
113    pub execution_time_ms: u64,
114}
115
116/// Task assigned to an agent
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct AgentTask {
119    /// Task ID
120    pub id: String,
121    /// Task type
122    pub task_type: TaskType,
123    /// Task description
124    pub description: String,
125    /// Task parameters
126    pub parameters: HashMap<String, serde_json::Value>,
127    /// Task status
128    pub status: TaskStatus,
129    /// Parent task (for subtasks)
130    pub parent_task_id: Option<String>,
131    /// Dependencies
132    pub depends_on: Vec<String>,
133}
134
135/// Task types for agents
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
137pub enum TaskType {
138    /// Code generation
139    CodeGeneration,
140    /// Code review
141    CodeReview,
142    /// Debugging
143    Debugging,
144    /// Documentation
145    Documentation,
146    /// Testing
147    Testing,
148    /// Refactoring
149    Refactoring,
150    /// Research
151    Research,
152    /// Custom task
153    Custom,
154}
155
156/// Task status
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
158pub enum TaskStatus {
159    /// Pending execution
160    Pending,
161    /// Currently executing
162    InProgress,
163    /// Completed successfully
164    Completed,
165    /// Failed
166    Failed,
167    /// Cancelled
168    Cancelled,
169    /// Blocked by dependencies
170    Blocked,
171}
172
173/// Message for inter-agent communication
174#[derive(Debug, Clone, Serialize, Deserialize)]
175pub struct AgentMessage {
176    /// Message ID
177    pub id: String,
178    /// Source agent
179    pub from: String,
180    /// Target agent
181    pub to: String,
182    /// Message type
183    pub message_type: MessageType,
184    /// Message content
185    pub content: serde_json::Value,
186    /// Timestamp
187    pub timestamp: chrono::DateTime<chrono::Utc>,
188    /// Priority
189    pub priority: MessagePriority,
190}
191
192/// Message types
193#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
194pub enum MessageType {
195    /// Task assignment
196    TaskAssignment,
197    /// Task result
198    TaskResult,
199    /// Information sharing
200    Information,
201    /// Query
202    Query,
203    /// Response
204    Response,
205    /// Error
206    Error,
207    /// Status update
208    StatusUpdate,
209}
210
211/// Message priority
212#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
213pub enum MessagePriority {
214    Low,
215    Normal,
216    High,
217    Critical,
218}
219
220/// Agent workflow definition
221#[derive(Debug, Clone, Serialize, Deserialize)]
222pub struct Workflow {
223    /// Workflow ID
224    pub id: String,
225    /// Workflow name
226    pub name: String,
227    /// Workflow steps
228    pub steps: Vec<WorkflowStep>,
229    /// Current step index
230    pub current_step: usize,
231    /// Workflow status
232    pub status: WorkflowStatus,
233    /// Workflow context
234    pub context: HashMap<String, serde_json::Value>,
235}
236
237/// Single workflow step
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct WorkflowStep {
240    /// Step name
241    pub name: String,
242    /// Agent to execute
243    pub agent: String,
244    /// Task to execute
245    pub task: AgentTask,
246    /// Success condition
247    pub success_condition: Option<String>,
248    /// Retry policy
249    pub retry_policy: RetryPolicy,
250    /// Next steps based on outcome
251    pub next_steps: HashMap<String, usize>,
252}
253
254/// Retry policy for workflow steps
255#[derive(Debug, Clone, Serialize, Deserialize)]
256pub struct RetryPolicy {
257    /// Maximum retry attempts
258    pub max_attempts: u32,
259    /// Delay between retries in ms
260    pub retry_delay_ms: u64,
261    /// Exponential backoff
262    pub exponential_backoff: bool,
263}
264
265/// Workflow status
266#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
267pub enum WorkflowStatus {
268    /// Not started
269    NotStarted,
270    /// Running
271    Running,
272    /// Paused
273    Paused,
274    /// Completed
275    Completed,
276    /// Failed
277    Failed,
278    /// Cancelled
279    Cancelled,
280}