gptengage 1.2.1

Multi-AI CLI orchestrator for Claude Code, Codex, and Gemini
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! Debate orchestration - Run multi-round debates

use crate::invokers::{get_invoker, AccessMode};
use serde::{Deserialize, Serialize};
use tokio::task;

pub struct DebateOrchestrator;

/// Full agent definition with persona, instructions, and metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentDefinition {
    pub cli: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    pub persona: String,
    pub instructions: String,
    #[serde(default)]
    pub expertise: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub communication_style: Option<String>,
}

impl AgentDefinition {
    /// Validate that required fields are non-empty
    pub fn validate(&self) -> anyhow::Result<()> {
        if self.cli.trim().is_empty() {
            return Err(anyhow::anyhow!("Agent 'cli' field cannot be empty"));
        }
        if self.persona.trim().is_empty() {
            return Err(anyhow::anyhow!(
                "Agent 'persona' field cannot be empty (required for agent definitions)"
            ));
        }
        if self.instructions.trim().is_empty() {
            return Err(anyhow::anyhow!(
                "Agent 'instructions' field cannot be empty (required for agent definitions)"
            ));
        }
        if self.instructions.len() < 10 {
            return Err(anyhow::anyhow!(
                "Agent 'instructions' must be at least 10 characters (got {})",
                self.instructions.len()
            ));
        }
        Ok(())
    }

    /// Convert to a Participant with enriched prompt building
    pub fn to_participant(&self) -> Participant {
        Participant {
            cli: self.cli.clone(),
            model: self.model.clone(),
            persona: Some(self.persona.clone()),
            agent_definition: Some(self.clone()),
        }
    }
}

/// Agent file schema
#[derive(Debug, Serialize, Deserialize)]
pub struct AgentFile {
    #[serde(default = "default_schema_version")]
    pub schema_version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub generated_by: Option<String>,
    pub participants: Vec<AgentDefinition>,
}

fn default_schema_version() -> String {
    "1.0".to_string()
}

impl AgentFile {
    /// Load and validate an agent file
    pub fn load(path: &str) -> anyhow::Result<Self> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| anyhow::anyhow!("Failed to read agent file '{}': {}", path, e))?;

        let agent_file: AgentFile = serde_json::from_str(&content)
            .map_err(|e| anyhow::anyhow!("Failed to parse agent file '{}': {}", path, e))?;

        // Validate schema version
        if agent_file.schema_version != "1.0" {
            return Err(anyhow::anyhow!(
                "Unsupported schema version '{}'. Expected '1.0'",
                agent_file.schema_version
            ));
        }

        // Validate all participants
        if agent_file.participants.is_empty() {
            return Err(anyhow::anyhow!(
                "Agent file must contain at least one participant"
            ));
        }

        for (idx, agent) in agent_file.participants.iter().enumerate() {
            agent.validate().map_err(|e| {
                anyhow::anyhow!("Validation failed for participant {}: {}", idx + 1, e)
            })?;
        }

        Ok(agent_file)
    }

    /// Convert to a list of Participants
    pub fn to_participants(&self) -> Vec<Participant> {
        self.participants
            .iter()
            .map(|agent| agent.to_participant())
            .collect()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Participant {
    pub cli: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    pub persona: Option<String>,
    #[serde(skip)]
    pub agent_definition: Option<AgentDefinition>,
}

impl Participant {
    pub fn new(cli: String, persona: Option<String>) -> Self {
        Self {
            cli,
            model: None,
            persona,
            agent_definition: None,
        }
    }

    pub fn with_model(cli: String, model: Option<String>, persona: Option<String>) -> Self {
        Self {
            cli,
            model,
            persona,
            agent_definition: None,
        }
    }

    pub fn display_name(&self) -> String {
        let cli_with_model = match &self.model {
            Some(m) => format!("{}:{}", self.cli, m),
            None => self.cli.clone(),
        };
        match &self.persona {
            Some(p) => format!("{} ({})", cli_with_model, p),
            None => cli_with_model,
        }
    }

    pub fn build_prompt_with_persona(&self, base_prompt: &str) -> String {
        // If we have a full agent definition, use rich context
        if let Some(agent_def) = &self.agent_definition {
            let mut context = String::from("[AGENT CONTEXT]\n");
            context.push_str(&format!("Role: {}\n", agent_def.persona));
            context.push_str(&format!("Instructions: {}\n", agent_def.instructions));

            if !agent_def.expertise.is_empty() {
                context.push_str(&format!("Expertise: {}\n", agent_def.expertise.join(", ")));
            }

            if let Some(style) = &agent_def.communication_style {
                context.push_str(&format!("Communication Style: {}\n", style));
            }

            context.push_str("[/AGENT CONTEXT]\n\n");
            context.push_str(base_prompt);
            return context;
        }

        // Fall back to simple persona context
        match &self.persona {
            Some(persona) => {
                format!(
                    "[ROLE CONTEXT]\nYou are participating in this debate as a {}. Respond from that perspective, drawing on the expertise, priorities, and viewpoints typical of this role.\n[/ROLE CONTEXT]\n\n{}",
                    persona, base_prompt
                )
            }
            None => base_prompt.to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoundResponse {
    pub cli: String,
    pub persona: Option<String>,
    pub response: String,
}

impl RoundResponse {
    pub fn display_name(&self) -> String {
        match &self.persona {
            Some(p) => format!("{} ({})", self.cli, p),
            None => self.cli.clone(),
        }
    }
}

/// Synthesis of a debate
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Synthesis {
    /// Brief summary of the debate
    pub summary: String,
    /// Points where participants agreed
    #[serde(default)]
    pub consensus_points: Vec<String>,
    /// Points where participants disagreed
    #[serde(default)]
    pub disagreement_points: Vec<String>,
    /// Key insights that emerged
    #[serde(default)]
    pub key_insights: Vec<String>,
    /// Final recommendation (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recommendation: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DebateResult {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gptengage_version: Option<String>,
    pub topic: String,
    pub rounds: Vec<Vec<RoundResponse>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub synthesis: Option<Synthesis>,
}

impl DebateOrchestrator {
    /// Run a debate with specific participants
    pub async fn run_debate_with_participants(
        topic: &str,
        participants: Vec<Participant>,
        num_rounds: usize,
        timeout: u64,
        access_mode: AccessMode,
    ) -> anyhow::Result<DebateResult> {
        if participants.is_empty() {
            return Err(anyhow::anyhow!("At least one participant is required"));
        }

        let mut rounds: Vec<Vec<RoundResponse>> = Vec::new();

        for round in 1..=num_rounds {
            println!("Running round {} of {}...", round, num_rounds);

            // Build base context for this round
            let mut base_context = format!("Topic: {}\n\nRound {}\n\n", topic, round);

            if round > 1 {
                if let Some(prev_round) = rounds.last() {
                    base_context.push_str("Previous responses:\n");
                    for response in prev_round.iter() {
                        base_context.push_str(&format!(
                            "{}: {}\n\n",
                            response.display_name(),
                            response.response
                        ));
                    }
                }
            }

            base_context.push_str("Please provide your perspective on this topic.");

            // Spawn tasks for all participants in parallel
            let mut tasks = Vec::new();

            for participant in &participants {
                let participant_clone = participant.clone();
                let ctx = participant_clone.build_prompt_with_persona(&base_context);

                let task = task::spawn(async move {
                    let invoker = match get_invoker(&participant_clone.cli) {
                        Some(inv) => inv,
                        None => {
                            eprintln!(
                                "Unknown CLI '{}', skipping participant",
                                participant_clone.cli
                            );
                            return None;
                        }
                    };

                    if !invoker.is_available() {
                        eprintln!(
                            "{} is not available, skipping",
                            participant_clone.display_name()
                        );
                        return None;
                    }

                    match invoker
                        .invoke(
                            &ctx,
                            timeout,
                            access_mode,
                            participant_clone.model.as_deref(),
                        )
                        .await
                    {
                        Ok(response) => Some(RoundResponse {
                            cli: participant_clone.cli.clone(),
                            persona: participant_clone.persona.clone(),
                            response,
                        }),
                        Err(e) => {
                            eprintln!(
                                "{} invocation failed: {}",
                                participant_clone.display_name(),
                                e
                            );
                            None
                        }
                    }
                });

                tasks.push(task);
            }

            // Wait for all tasks to complete
            let results = futures::future::join_all(tasks).await;

            let round_responses: Vec<RoundResponse> =
                results.into_iter().flatten().flatten().collect();

            // Ensure at least one responder per round
            if round_responses.is_empty() {
                return Err(anyhow::anyhow!(
                    "No participants were able to respond in round {}. Please ensure their CLIs are installed and available.",
                    round
                ));
            }

            rounds.push(round_responses);
        }

        Ok(DebateResult {
            gptengage_version: Some(env!("CARGO_PKG_VERSION").to_string()),
            topic: topic.to_string(),
            rounds,
            synthesis: None,
        })
    }

    /// Generate a synthesis of a debate result
    pub async fn generate_synthesis(
        result: &DebateResult,
        synthesizer_cli: &str,
        timeout: u64,
        access_mode: AccessMode,
    ) -> anyhow::Result<Synthesis> {
        // Build debate transcript for synthesis
        let mut transcript = String::new();
        for (round_num, round_responses) in result.rounds.iter().enumerate() {
            transcript.push_str(&format!("ROUND {}:\n", round_num + 1));
            for response in round_responses {
                transcript.push_str(&format!(
                    "{}:\n{}\n\n",
                    response.display_name(),
                    response.response
                ));
            }
            transcript.push('\n');
        }

        let synthesis_prompt = format!(
            r#"[SYNTHESIS REQUEST]
You are synthesizing a multi-participant debate.

TOPIC: {}

DEBATE TRANSCRIPT:
{}

Generate a structured synthesis with:
1. A 2-3 sentence summary of the debate
2. Points where participants reached consensus
3. Points where participants disagreed
4. Key insights that emerged
5. A recommendation (if applicable)

Respond with JSON in this exact format:
{{
  "summary": "...",
  "consensus_points": ["...", "..."],
  "disagreement_points": ["...", "..."],
  "key_insights": ["...", "..."],
  "recommendation": "..." or null
}}
[/SYNTHESIS REQUEST]"#,
            result.topic, transcript
        );

        // Get the synthesizer invoker
        let invoker = get_invoker(synthesizer_cli).ok_or_else(|| {
            anyhow::anyhow!(
                "Synthesizer CLI '{}' not found. Use claude, codex, gemini, or an installed plugin.",
                synthesizer_cli
            )
        })?;

        if !invoker.is_available() {
            return Err(anyhow::anyhow!(
                "Synthesizer CLI '{}' is not available in PATH.",
                synthesizer_cli
            ));
        }

        eprintln!("Generating synthesis with {}...", synthesizer_cli);
        let response = invoker
            .invoke(&synthesis_prompt, timeout, access_mode, None)
            .await?;

        // Parse the JSON from the response
        Self::parse_synthesis_response(&response)
    }

    /// Parse synthesis JSON from LLM response
    fn parse_synthesis_response(response: &str) -> anyhow::Result<Synthesis> {
        // Try to extract JSON from the response (it may be wrapped in markdown or text)
        let json_start = response.find('{');
        let json_end = response.rfind('}');

        if let (Some(start), Some(end)) = (json_start, json_end) {
            if start < end {
                let json_str = &response[start..=end];
                if let Ok(synthesis) = serde_json::from_str::<Synthesis>(json_str) {
                    return Ok(synthesis);
                }
            }
        }

        // If JSON parsing fails, create a basic synthesis from the text
        Ok(Synthesis {
            summary: response.trim().to_string(),
            consensus_points: vec![],
            disagreement_points: vec![],
            key_insights: vec![],
            recommendation: None,
        })
    }

    /// Run a debate with default participants (Claude, Codex, Gemini without personas)
    pub async fn run_debate(
        topic: &str,
        num_rounds: usize,
        timeout: u64,
        access_mode: AccessMode,
    ) -> anyhow::Result<DebateResult> {
        let participants = vec![
            Participant::new("claude".to_string(), None),
            Participant::new("codex".to_string(), None),
            Participant::new("gemini".to_string(), None),
        ];

        Self::run_debate_with_participants(topic, participants, num_rounds, timeout, access_mode)
            .await
    }
}

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

    #[test]
    fn test_round_response_creation() {
        let response = RoundResponse {
            cli: "Claude".to_string(),
            persona: None,
            response: "This is Claude's perspective".to_string(),
        };

        assert_eq!(response.cli, "Claude");
        assert_eq!(response.persona, None);
        assert_eq!(response.response, "This is Claude's perspective");
        assert_eq!(response.display_name(), "Claude");
    }

    #[test]
    fn test_round_response_with_persona() {
        let response = RoundResponse {
            cli: "Claude".to_string(),
            persona: Some("CEO".to_string()),
            response: "From a CEO perspective...".to_string(),
        };

        assert_eq!(response.cli, "Claude");
        assert_eq!(response.persona, Some("CEO".to_string()));
        assert_eq!(response.display_name(), "Claude (CEO)");
    }

    #[test]
    fn test_round_response_serialization() {
        let response = RoundResponse {
            cli: "Codex".to_string(),
            persona: Some("Architect".to_string()),
            response: "This is Codex's perspective".to_string(),
        };

        let json = serde_json::to_string(&response).unwrap();
        let deserialized: RoundResponse = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.cli, "Codex");
        assert_eq!(deserialized.persona, Some("Architect".to_string()));
        assert_eq!(deserialized.response, "This is Codex's perspective");
    }

    #[test]
    fn test_debate_result_creation() {
        let result = DebateResult {
            gptengage_version: Some("0.1.0".to_string()),
            topic: "Should we use Rust?".to_string(),
            rounds: vec![vec![
                RoundResponse {
                    cli: "Claude".to_string(),
                    persona: None,
                    response: "Yes, Rust is great".to_string(),
                },
                RoundResponse {
                    cli: "Gemini".to_string(),
                    persona: None,
                    response: "Go is simpler".to_string(),
                },
            ]],
            synthesis: None,
        };

        assert_eq!(result.topic, "Should we use Rust?");
        assert_eq!(result.rounds.len(), 1);
        assert_eq!(result.rounds[0].len(), 2);
        assert_eq!(result.rounds[0][0].cli, "Claude");
    }

    #[test]
    fn test_debate_result_multiple_rounds() {
        // Round 1
        let rounds = vec![
            vec![
                RoundResponse {
                    cli: "Claude".to_string(),
                    persona: None,
                    response: "Round 1: Claude's view".to_string(),
                },
                RoundResponse {
                    cli: "Codex".to_string(),
                    persona: None,
                    response: "Round 1: Codex's view".to_string(),
                },
            ],
            // Round 2
            vec![
                RoundResponse {
                    cli: "Claude".to_string(),
                    persona: None,
                    response: "Round 2: Claude's refined view".to_string(),
                },
                RoundResponse {
                    cli: "Codex".to_string(),
                    persona: None,
                    response: "Round 2: Codex's refined view".to_string(),
                },
            ],
        ];

        let result = DebateResult {
            gptengage_version: None,
            topic: "Test Topic".to_string(),
            rounds,
            synthesis: None,
        };

        assert_eq!(result.rounds.len(), 2);
        assert_eq!(result.rounds[0].len(), 2);
        assert_eq!(result.rounds[1].len(), 2);
        assert!(result.rounds[1][0].response.contains("Round 2"));
    }

    #[test]
    fn test_debate_result_serialization() {
        let result = DebateResult {
            gptengage_version: Some("0.1.0".to_string()),
            topic: "Tabs vs Spaces".to_string(),
            rounds: vec![vec![
                RoundResponse {
                    cli: "Claude".to_string(),
                    persona: None,
                    response: "Tabs are consistent".to_string(),
                },
                RoundResponse {
                    cli: "Gemini".to_string(),
                    persona: None,
                    response: "Spaces are standard".to_string(),
                },
            ]],
            synthesis: None,
        };

        let json = serde_json::to_string(&result).unwrap();
        let deserialized: DebateResult = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.topic, "Tabs vs Spaces");
        assert_eq!(deserialized.rounds.len(), 1);
        assert_eq!(deserialized.rounds[0].len(), 2);
    }

    #[test]
    fn test_debate_result_empty_rounds() {
        let result = DebateResult {
            gptengage_version: None,
            topic: "Empty debate".to_string(),
            rounds: vec![],
            synthesis: None,
        };

        assert_eq!(result.rounds.len(), 0);
        assert_eq!(result.topic, "Empty debate");
    }

    #[test]
    fn test_round_response_clone() {
        let response1 = RoundResponse {
            cli: "Claude".to_string(),
            persona: Some("CEO".to_string()),
            response: "Test response".to_string(),
        };

        let response2 = response1.clone();

        assert_eq!(response1.cli, response2.cli);
        assert_eq!(response1.persona, response2.persona);
        assert_eq!(response1.response, response2.response);
    }

    #[test]
    fn test_round_response_with_long_content() {
        let long_response = "a".repeat(10000);
        let response = RoundResponse {
            cli: "Claude".to_string(),
            persona: None,
            response: long_response.clone(),
        };

        assert_eq!(response.response.len(), 10000);

        // Verify serialization works with large content
        let json = serde_json::to_string(&response).unwrap();
        let deserialized: RoundResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.response.len(), 10000);
    }

    #[test]
    fn test_debate_result_with_special_chars() {
        let result = DebateResult {
            gptengage_version: Some("0.1.0".to_string()),
            topic: "Test with 特殊 characters & symbols! 🚀".to_string(),
            rounds: vec![vec![RoundResponse {
                cli: "Claude".to_string(),
                persona: None,
                response: "Response with unicode: émojis: 🎉".to_string(),
            }]],
            synthesis: None,
        };

        let json = serde_json::to_string(&result).unwrap();
        let deserialized: DebateResult = serde_json::from_str(&json).unwrap();

        assert!(deserialized.topic.contains("特殊"));
        assert!(deserialized.rounds[0][0].response.contains("🎉"));
    }

    #[test]
    fn test_participant_creation() {
        let p1 = Participant::new("claude".to_string(), None);
        assert_eq!(p1.cli, "claude");
        assert_eq!(p1.persona, None);
        assert_eq!(p1.display_name(), "claude");

        let p2 = Participant::new("claude".to_string(), Some("CEO".to_string()));
        assert_eq!(p2.cli, "claude");
        assert_eq!(p2.persona, Some("CEO".to_string()));
        assert_eq!(p2.display_name(), "claude (CEO)");
    }

    #[test]
    fn test_participant_prompt_building() {
        let base = "Discuss the topic";

        let p1 = Participant::new("claude".to_string(), None);
        let prompt1 = p1.build_prompt_with_persona(base);
        assert_eq!(prompt1, base);

        let p2 = Participant::new("claude".to_string(), Some("CEO".to_string()));
        let prompt2 = p2.build_prompt_with_persona(base);
        assert!(prompt2.contains("ROLE CONTEXT"));
        assert!(prompt2.contains("CEO"));
        assert!(prompt2.contains(base));
    }
}