ccswarm 0.5.0

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! Interactive mode for Piece/Movement workflows.
//!
//! Provides four variants for user interaction before workflow execution:
//! 1. **Assistant** — AI asks clarifying questions before generating task instructions
//! 2. **Persona** — Conversation with the first movement's persona
//! 3. **Quiet** — Generates task instructions without asking questions (best-effort)
//! 4. **Passthrough** — Passes user input directly as task text

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

use super::piece::Piece;

/// Interactive mode variant
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum InteractiveMode {
    /// AI asks clarifying questions before generating task instructions
    #[default]
    Assistant,
    /// Conversation with the first movement's persona (uses its system prompt and tools)
    Persona,
    /// Generates task instructions without asking questions (best-effort)
    Quiet,
    /// Passes user input directly as task text without AI processing
    Passthrough,
}

impl std::fmt::Display for InteractiveMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Assistant => write!(f, "assistant"),
            Self::Persona => write!(f, "persona"),
            Self::Quiet => write!(f, "quiet"),
            Self::Passthrough => write!(f, "passthrough"),
        }
    }
}

impl InteractiveMode {
    /// Description for display
    pub fn description(&self) -> &'static str {
        match self {
            Self::Assistant => "AI asks clarifying questions before generating task instructions",
            Self::Persona => {
                "Conversation with the first movement's persona (uses its system prompt and tools)"
            }
            Self::Quiet => "Generates task instructions without asking questions (best-effort)",
            Self::Passthrough => "Passes user input directly as task text without AI processing",
        }
    }

    /// All available modes
    pub fn all() -> Vec<Self> {
        vec![
            Self::Assistant,
            Self::Persona,
            Self::Quiet,
            Self::Passthrough,
        ]
    }
}

/// Configuration for an interactive session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InteractiveConfig {
    /// The interactive mode to use
    pub mode: InteractiveMode,
    /// Maximum number of clarification rounds (for Assistant mode)
    #[serde(default = "default_max_rounds")]
    pub max_clarification_rounds: u32,
    /// Whether to show piece selection menu
    #[serde(default = "default_true")]
    pub show_piece_selection: bool,
    /// Default piece to use (if set, skips piece selection)
    #[serde(default)]
    pub default_piece: Option<String>,
}

fn default_max_rounds() -> u32 {
    5
}

fn default_true() -> bool {
    true
}

impl Default for InteractiveConfig {
    fn default() -> Self {
        Self {
            mode: InteractiveMode::default(),
            max_clarification_rounds: default_max_rounds(),
            show_piece_selection: true,
            default_piece: None,
        }
    }
}

/// State of an interactive session
#[derive(Debug, Clone)]
pub struct InteractiveSession {
    /// Selected piece (if any)
    pub selected_piece: Option<String>,
    /// Current mode
    pub mode: InteractiveMode,
    /// Collected user inputs
    pub user_inputs: Vec<String>,
    /// Generated clarification questions (Assistant mode)
    pub clarifications: Vec<Clarification>,
    /// Final task text to execute
    pub task_text: Option<String>,
    /// Session variables
    pub variables: HashMap<String, String>,
    /// Whether the session is ready for execution
    pub ready: bool,
}

/// A clarification question and its answer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Clarification {
    /// The question asked
    pub question: String,
    /// User's answer (if provided)
    pub answer: Option<String>,
    /// Suggested options (if any)
    pub options: Vec<String>,
}

/// Result of processing user input in interactive mode
#[derive(Debug, Clone)]
pub enum InteractiveAction {
    /// Ask the user a question
    AskQuestion(Clarification),
    /// Display a message to the user
    ShowMessage(String),
    /// Ready to execute with the given task text
    Execute(String),
    /// User wants to exit
    Exit,
}

impl InteractiveSession {
    /// Create a new interactive session
    pub fn new(mode: InteractiveMode) -> Self {
        Self {
            selected_piece: None,
            mode,
            user_inputs: Vec::new(),
            clarifications: Vec::new(),
            task_text: None,
            variables: HashMap::new(),
            ready: false,
        }
    }

    /// Select a piece for this session
    pub fn select_piece(&mut self, piece_name: &str) {
        self.selected_piece = Some(piece_name.to_string());
        info!("Selected piece: {}", piece_name);
    }

    /// Process user input according to the current mode
    pub fn process_input(
        &mut self,
        input: &str,
        piece: Option<&Piece>,
    ) -> Result<InteractiveAction> {
        // Handle commands
        if let Some(action) = self.handle_command(input)? {
            return Ok(action);
        }

        self.user_inputs.push(input.to_string());

        match self.mode {
            InteractiveMode::Assistant => self.process_assistant(input, piece),
            InteractiveMode::Persona => self.process_persona(input, piece),
            InteractiveMode::Quiet => self.process_quiet(input),
            InteractiveMode::Passthrough => self.process_passthrough(input),
        }
    }

    /// Handle special commands (/go, /play, /mode, /quit)
    fn handle_command(&mut self, input: &str) -> Result<Option<InteractiveAction>> {
        let trimmed = input.trim();

        if trimmed == "/quit" || trimmed == "/exit" {
            return Ok(Some(InteractiveAction::Exit));
        }

        if trimmed == "/go" {
            if let Some(ref task) = self.task_text {
                self.ready = true;
                return Ok(Some(InteractiveAction::Execute(task.clone())));
            }
            // No task yet - build from collected inputs
            let combined = self.user_inputs.join("\n");
            self.task_text = Some(combined.clone());
            self.ready = true;
            return Ok(Some(InteractiveAction::Execute(combined)));
        }

        if let Some(task) = trimmed.strip_prefix("/play ") {
            self.task_text = Some(task.to_string());
            self.ready = true;
            return Ok(Some(InteractiveAction::Execute(task.to_string())));
        }

        if let Some(mode_str) = trimmed.strip_prefix("/mode ") {
            match mode_str.trim() {
                "assistant" => self.mode = InteractiveMode::Assistant,
                "persona" => self.mode = InteractiveMode::Persona,
                "quiet" => self.mode = InteractiveMode::Quiet,
                "passthrough" => self.mode = InteractiveMode::Passthrough,
                other => {
                    return Ok(Some(InteractiveAction::ShowMessage(format!(
                        "Unknown mode: '{}'. Available: assistant, persona, quiet, passthrough",
                        other
                    ))));
                }
            }
            return Ok(Some(InteractiveAction::ShowMessage(format!(
                "Switched to {} mode",
                self.mode
            ))));
        }

        Ok(None)
    }

    /// Assistant mode: ask clarifying questions
    fn process_assistant(
        &mut self,
        input: &str,
        _piece: Option<&Piece>,
    ) -> Result<InteractiveAction> {
        // If we have pending clarifications, record the answer
        if let Some(last_clarification) = self.clarifications.last_mut()
            && last_clarification.answer.is_none()
        {
            last_clarification.answer = Some(input.to_string());
            debug!("Recorded answer for: {}", last_clarification.question);
        }

        // Generate next clarification based on context
        let context = self.build_context();

        // After enough context, generate task
        if self.clarifications.len() >= 3 || self.has_sufficient_context() {
            let task = self.generate_task_from_context(&context);
            self.task_text = Some(task.clone());
            return Ok(InteractiveAction::ShowMessage(format!(
                "Generated task:\n{}\n\nType /go to execute or continue refining.",
                task
            )));
        }

        // Generate next question
        let question = self.generate_clarification(&context);
        let clarification = Clarification {
            question: question.clone(),
            answer: None,
            options: vec![],
        };
        self.clarifications.push(clarification.clone());

        Ok(InteractiveAction::AskQuestion(clarification))
    }

    /// Persona mode: converse with the first movement's persona
    fn process_persona(&mut self, input: &str, piece: Option<&Piece>) -> Result<InteractiveAction> {
        let persona_name = piece
            .and_then(|p| p.get_movement(&p.initial_movement))
            .and_then(|m| m.persona.as_deref())
            .unwrap_or("assistant");

        // Build response from persona perspective
        let response = format!(
            "[{}] I understand your request: \"{}\". \
             I'll incorporate this into the workflow. Type /go when ready to execute.",
            persona_name, input
        );

        // Accumulate as task text
        let current = self.task_text.clone().unwrap_or_default();
        let updated = if current.is_empty() {
            input.to_string()
        } else {
            format!("{}\n{}", current, input)
        };
        self.task_text = Some(updated);

        Ok(InteractiveAction::ShowMessage(response))
    }

    /// Quiet mode: directly generate task from input
    fn process_quiet(&mut self, input: &str) -> Result<InteractiveAction> {
        // Best-effort: take the input and enhance it into a task
        let enhanced = format!(
            "Task: {}\n\nPlease complete this task following best practices.",
            input
        );
        self.task_text = Some(enhanced.clone());
        self.ready = true;

        Ok(InteractiveAction::Execute(enhanced))
    }

    /// Passthrough mode: use input directly as task text
    fn process_passthrough(&mut self, input: &str) -> Result<InteractiveAction> {
        self.task_text = Some(input.to_string());
        self.ready = true;

        Ok(InteractiveAction::Execute(input.to_string()))
    }

    /// Build context summary from all inputs and clarifications
    fn build_context(&self) -> String {
        let mut parts = Vec::new();

        for input in &self.user_inputs {
            parts.push(format!("User: {}", input));
        }

        for clarification in &self.clarifications {
            if let Some(ref answer) = clarification.answer {
                parts.push(format!("Q: {}\nA: {}", clarification.question, answer));
            }
        }

        parts.join("\n")
    }

    /// Check if we have enough context to generate a task
    fn has_sufficient_context(&self) -> bool {
        // Heuristic: if total input length is substantial
        let total_len: usize = self.user_inputs.iter().map(|s| s.len()).sum();
        total_len > 200
    }

    /// Generate a clarification question based on context
    fn generate_clarification(&self, context: &str) -> String {
        // Generate questions based on what's missing
        let questions = [
            "What specific outcome are you looking for?",
            "Are there any constraints or requirements I should be aware of?",
            "Should I prioritize any particular aspect (speed, quality, security)?",
            "Are there any files or modules that should NOT be modified?",
            "What testing approach would you prefer?",
        ];

        let index = self.clarifications.len() % questions.len();
        let _context = context; // Used by future AI-powered question generation
        questions[index].to_string()
    }

    /// Generate a task description from collected context
    fn generate_task_from_context(&self, context: &str) -> String {
        let mut task_parts = Vec::new();
        task_parts.push("Task Summary:".to_string());

        // Include all user inputs as requirements
        for (i, input) in self.user_inputs.iter().enumerate() {
            if i == 0 {
                task_parts.push(format!("Primary goal: {}", input));
            } else {
                task_parts.push(format!("- {}", input));
            }
        }

        // Include clarification answers as constraints
        let answered: Vec<&Clarification> = self
            .clarifications
            .iter()
            .filter(|c| c.answer.is_some())
            .collect();
        if !answered.is_empty() {
            task_parts.push("\nAdditional context:".to_string());
            for c in answered {
                if let Some(ref answer) = c.answer {
                    task_parts.push(format!("- {}{}", c.question, answer));
                }
            }
        }

        let _context = context; // Used by future AI-powered task generation
        task_parts.join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_interactive_modes() {
        let modes = InteractiveMode::all();
        assert_eq!(modes.len(), 4);
        assert_eq!(InteractiveMode::default(), InteractiveMode::Assistant);
    }

    #[test]
    fn test_mode_display() {
        assert_eq!(InteractiveMode::Assistant.to_string(), "assistant");
        assert_eq!(InteractiveMode::Persona.to_string(), "persona");
        assert_eq!(InteractiveMode::Quiet.to_string(), "quiet");
        assert_eq!(InteractiveMode::Passthrough.to_string(), "passthrough");
    }

    #[test]
    fn test_passthrough_mode() {
        let mut session = InteractiveSession::new(InteractiveMode::Passthrough);
        let result = session.process_input("Fix the login bug", None).unwrap();

        match result {
            InteractiveAction::Execute(task) => {
                assert_eq!(task, "Fix the login bug");
            }
            _ => panic!("Expected Execute action"),
        }
        assert!(session.ready);
    }

    #[test]
    fn test_quiet_mode() {
        let mut session = InteractiveSession::new(InteractiveMode::Quiet);
        let result = session.process_input("Add unit tests", None).unwrap();

        match result {
            InteractiveAction::Execute(task) => {
                assert!(task.contains("Add unit tests"));
            }
            _ => panic!("Expected Execute action"),
        }
        assert!(session.ready);
    }

    #[test]
    fn test_assistant_mode_asks_questions() {
        let mut session = InteractiveSession::new(InteractiveMode::Assistant);
        let result = session
            .process_input("Refactor the auth module", None)
            .unwrap();

        match result {
            InteractiveAction::AskQuestion(q) => {
                assert!(!q.question.is_empty());
            }
            _ => panic!("Expected AskQuestion action"),
        }
        assert!(!session.ready);
    }

    #[test]
    fn test_go_command() {
        let mut session = InteractiveSession::new(InteractiveMode::Assistant);
        session.process_input("Build a REST API", None).unwrap();

        let result = session.process_input("/go", None).unwrap();
        match result {
            InteractiveAction::Execute(task) => {
                assert!(task.contains("Build a REST API"));
            }
            _ => panic!("Expected Execute action"),
        }
        assert!(session.ready);
    }

    #[test]
    fn test_play_command() {
        let mut session = InteractiveSession::new(InteractiveMode::Assistant);
        let result = session
            .process_input("/play Create a login form", None)
            .unwrap();

        match result {
            InteractiveAction::Execute(task) => {
                assert_eq!(task, "Create a login form");
            }
            _ => panic!("Expected Execute action"),
        }
    }

    #[test]
    fn test_mode_switch() {
        let mut session = InteractiveSession::new(InteractiveMode::Assistant);
        let result = session.process_input("/mode quiet", None).unwrap();

        match result {
            InteractiveAction::ShowMessage(msg) => {
                assert!(msg.contains("quiet"));
            }
            _ => panic!("Expected ShowMessage action"),
        }
        assert_eq!(session.mode, InteractiveMode::Quiet);
    }

    #[test]
    fn test_quit_command() {
        let mut session = InteractiveSession::new(InteractiveMode::Assistant);
        let result = session.process_input("/quit", None).unwrap();

        match result {
            InteractiveAction::Exit => {}
            _ => panic!("Expected Exit action"),
        }
    }

    #[test]
    fn test_persona_mode() {
        let mut session = InteractiveSession::new(InteractiveMode::Persona);
        let result = session
            .process_input("Optimize the database queries", None)
            .unwrap();

        match result {
            InteractiveAction::ShowMessage(msg) => {
                assert!(msg.contains("Optimize the database queries"));
            }
            _ => panic!("Expected ShowMessage action"),
        }
        assert!(session.task_text.is_some());
    }

    #[test]
    fn test_interactive_config_default() {
        let config = InteractiveConfig::default();
        assert_eq!(config.mode, InteractiveMode::Assistant);
        assert_eq!(config.max_clarification_rounds, 5);
        assert!(config.show_piece_selection);
        assert!(config.default_piece.is_none());
    }
}