code-agent 0.2.0

AI-Native Code Assistant Library
Documentation
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! AI Agent Service API and Types

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use uuid::Uuid;

/// Request to execute a task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskRequest {
    /// Task description or prompt
    pub task: String,
    /// Optional task ID (will be generated if not provided)
    pub task_id: Option<String>,
    /// Execution context
    pub context: Option<TaskContext>,
    /// Priority level
    pub priority: Option<TaskPriority>,
    /// Request metadata
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}

/// Task execution context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskContext {
    /// Working directory
    pub working_directory: Option<String>,
    /// Environment variables
    pub environment: Option<HashMap<String, String>>,
    /// Available tools
    pub tools: Option<Vec<String>>,
    /// Execution constraints
    pub constraints: Option<TaskConstraints>,
}

/// Task execution constraints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskConstraints {
    /// Maximum execution time in seconds
    pub max_execution_time: Option<u64>,
    /// Maximum number of steps
    pub max_steps: Option<u32>,
    /// Allowed file paths
    pub allowed_paths: Option<Vec<String>>,
    /// Forbidden operations
    pub forbidden_operations: Option<Vec<String>>,
}

/// Task priority levels
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TaskPriority {
    Low,
    Normal,
    High,
    Critical,
}

impl Default for TaskPriority {
    fn default() -> Self {
        TaskPriority::Normal
    }
}

/// Task execution response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskResponse {
    /// Task ID
    pub task_id: String,
    /// Execution status
    pub status: TaskStatus,
    /// Task result (if completed)
    pub result: Option<TaskResult>,
    /// Execution plan that was generated
    pub plan: Option<TaskPlan>,
    /// Execution steps taken
    pub steps: Vec<ExecutionStep>,
    /// Metrics and timing information
    pub metrics: TaskMetrics,
    /// Error information (if failed)
    pub error: Option<ServiceError>,
    /// Timestamps
    pub created_at: DateTime<Utc>,
    pub started_at: Option<DateTime<Utc>>,
    pub completed_at: Option<DateTime<Utc>>,
}

/// Task status in service context
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TaskStatus {
    Queued,
    Running,
    Completed,
    Failed,
    Cancelled,
    Timeout,
}

/// Task result from service
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TaskResult {
    /// Success status
    #[serde(default)]
    pub success: bool,
    /// Result summary
    #[serde(default)]
    pub summary: String,
    /// Detailed result
    #[serde(default)]
    pub details: Option<String>,
    /// Generated files or artifacts
    #[serde(default)]
    pub artifacts: Vec<TaskArtifact>,
    /// Execution time in seconds
    #[serde(default)]
    pub execution_time: u64,
}

/// Task artifact (generated file, output, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskArtifact {
    /// Artifact type
    pub artifact_type: ArtifactType,
    /// Artifact name/path
    pub name: String,
    /// Artifact content (for small artifacts)
    pub content: Option<String>,
    /// Artifact size in bytes
    pub size: Option<u64>,
    /// Artifact metadata
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}

/// Artifact types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ArtifactType {
    File,
    Text,
    Json,
    Image,
    Log,
    Report,
    Other(String),
}

/// Task execution plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskPlan {
    /// Understanding of the task
    pub understanding: String,
    /// Approach to solve the task
    pub approach: String,
    /// Estimated complexity
    pub complexity: TaskComplexity,
    /// Estimated number of steps
    pub estimated_steps: u32,
    /// Required tools or resources
    pub requirements: Vec<String>,
    /// Plan created timestamp
    pub created_at: DateTime<Utc>,
}

/// Task complexity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TaskComplexity {
    Simple,
    Moderate,
    Complex,
}

/// Individual execution step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionStep {
    /// Step number
    pub step_number: u32,
    /// Step type
    pub step_type: StepType,
    /// Step description
    pub description: String,
    /// Input data for the step
    pub input: Option<serde_json::Value>,
    /// Output data from the step
    pub output: Option<serde_json::Value>,
    /// Step status
    pub status: StepStatus,
    /// Error if step failed
    pub error: Option<String>,
    /// Execution time in milliseconds
    pub execution_time_ms: u64,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

/// Step types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StepType {
    Analysis,
    Planning,
    ToolUse,
    Execution,
    Verification,
    Completion,
}

/// Step status
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StepStatus {
    Pending,
    Running,
    Completed,
    Failed,
    Skipped,
}

/// Task execution metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskMetrics {
    /// Total execution time in seconds
    pub total_execution_time: u64,
    /// Time spent in planning phase
    pub planning_time_ms: u64,
    /// Time spent in execution phase
    pub execution_time_ms: u64,
    /// Number of steps executed
    pub steps_executed: u32,
    /// Number of tools used
    pub tools_used: u32,
    /// Memory usage metrics
    pub memory_usage_mb: Option<f64>,
    /// CPU usage percentage
    pub cpu_usage_percent: Option<f64>,
    /// Custom metrics
    pub custom_metrics: HashMap<String, f64>,
}

/// Service error information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceError {
    /// Error code
    pub code: String,
    /// Error message
    pub message: String,
    /// Error details
    pub details: Option<String>,
    /// Error stack trace (for debugging)
    pub stack_trace: Option<String>,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

/// Service status and health information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceStatus {
    /// Service name
    pub name: String,
    /// Service version
    pub version: String,
    /// Service status
    pub status: ServiceHealth,
    /// Uptime in seconds
    pub uptime_seconds: u64,
    /// Number of active tasks
    pub active_tasks: u32,
    /// Number of completed tasks
    pub completed_tasks: u64,
    /// Number of failed tasks
    pub failed_tasks: u64,
    /// Available tools
    pub available_tools: Vec<String>,
    /// System metrics
    pub system_metrics: SystemMetrics,
    /// Last updated timestamp
    pub last_updated: DateTime<Utc>,
}

/// Service health status
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ServiceHealth {
    Healthy,
    Degraded,
    Unhealthy,
}

/// System metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemMetrics {
    /// CPU usage percentage
    pub cpu_usage_percent: f64,
    /// Memory usage in MB
    pub memory_usage_mb: f64,
    /// Disk usage in MB
    pub disk_usage_mb: f64,
    /// Network I/O
    pub network_io: NetworkMetrics,
}

/// Network metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkMetrics {
    /// Bytes received
    pub bytes_received: u64,
    /// Bytes sent
    pub bytes_sent: u64,
    /// Active connections
    pub active_connections: u32,
}

impl Default for NetworkMetrics {
    fn default() -> Self {
        Self {
            bytes_received: 0,
            bytes_sent: 0,
            active_connections: 0,
        }
    }
}

impl Default for SystemMetrics {
    fn default() -> Self {
        Self {
            cpu_usage_percent: 0.0,
            memory_usage_mb: 0.0,
            disk_usage_mb: 0.0,
            network_io: NetworkMetrics::default(),
        }
    }
}

/// Service configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceConfig {
    /// Maximum concurrent tasks
    pub max_concurrent_tasks: u32,
    /// Default task timeout in seconds
    pub default_task_timeout: u64,
    /// Maximum task timeout in seconds
    pub max_task_timeout: u64,
    /// Enable metrics collection
    pub enable_metrics: bool,
    /// Log level
    pub log_level: String,
    /// CORS settings
    pub cors: CorsConfig,
    /// Rate limiting
    pub rate_limiting: Option<RateLimitConfig>,
}

/// CORS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorsConfig {
    /// Allowed origins
    pub allowed_origins: Vec<String>,
    /// Allowed methods
    pub allowed_methods: Vec<String>,
    /// Allowed headers
    pub allowed_headers: Vec<String>,
    /// Allow credentials
    pub allow_credentials: bool,
}

/// Rate limiting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Requests per minute
    pub requests_per_minute: u32,
    /// Burst size
    pub burst_size: u32,
    /// Cleanup interval in seconds
    pub cleanup_interval_seconds: u64,
}

impl Default for ServiceConfig {
    fn default() -> Self {
        Self {
            max_concurrent_tasks: 10,
            default_task_timeout: 300, // 5 minutes
            max_task_timeout: 3600,    // 1 hour
            enable_metrics: true,
            log_level: "info".to_string(),
            cors: CorsConfig {
                allowed_origins: vec!["*".to_string()],
                allowed_methods: vec!["GET".to_string(), "POST".to_string(), "DELETE".to_string()],
                allowed_headers: vec!["*".to_string()],
                allow_credentials: false,
            },
            rate_limiting: Some(RateLimitConfig {
                requests_per_minute: 60,
                burst_size: 10,
                cleanup_interval_seconds: 300,
            }),
        }
    }
}

/// Batch task request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchTaskRequest {
    /// List of tasks to execute
    pub tasks: Vec<TaskRequest>,
    /// Execution mode
    pub mode: BatchExecutionMode,
    /// Continue on error
    pub continue_on_error: bool,
}

/// Batch execution modes
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BatchExecutionMode {
    Sequential,
    Parallel,
}

/// Batch task response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchTaskResponse {
    /// Batch ID
    pub batch_id: String,
    /// Individual task responses
    pub responses: Vec<TaskResponse>,
    /// Batch statistics
    pub statistics: BatchStatistics,
}

/// Batch execution statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchStatistics {
    /// Total tasks
    pub total_tasks: u32,
    /// Completed tasks
    pub completed_tasks: u32,
    /// Failed tasks
    pub failed_tasks: u32,
    /// Total execution time in seconds
    pub total_execution_time: u64,
    /// Average execution time per task
    pub average_execution_time: f64,
}

/// WebSocket message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum WebSocketMessage {
    /// Task status update
    TaskUpdate { task_id: String, status: TaskStatus },
    /// Task progress update
    TaskProgress { task_id: String, progress: f64, message: Option<String> },
    /// Task step update
    StepUpdate { task_id: String, step: ExecutionStep },
    /// Task completed
    TaskCompleted { task_id: String, result: TaskResult },
    /// Task failed
    TaskFailed { task_id: String, error: ServiceError },
    /// Service status update
    ServiceStatus { status: ServiceStatus },
    /// Heartbeat
    Heartbeat { timestamp: DateTime<Utc> },
}