chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Multi-Agent Orchestration
//!
//! Provides patterns for orchestrating multiple agents:
//! - Sequential: Agents execute one after another
//! - Parallel: Agents execute simultaneously
//! - Loop: Agent repeats until condition met
//! - Hierarchical: Coordinator delegates to sub-agents

#![allow(dead_code)]

use crate::agency::agent::Agent;
use crate::agency::error::{AgencyError, AgencyResult};
use crate::agency::executor::{ExecutionContext, ExecutionResult, Executor};
use crate::agency::models::{AgencyEvent, EventType, TokenUsage};
use crate::agency::session::Session;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Orchestration type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OrchestrationType {
    /// Agents execute sequentially, passing state forward
    Sequential,
    /// Agents execute in parallel
    Parallel,
    /// Single agent loops until condition met
    Loop,
    /// Coordinator agent delegates to sub-agents
    Hierarchical,
}

/// A pipeline of agents
#[derive(Debug, Clone)]
pub struct Pipeline {
    /// Pipeline name
    pub name: String,
    /// Orchestration type
    pub orchestration: OrchestrationType,
    /// Agents in the pipeline
    pub agents: Vec<Arc<Agent>>,
    /// Maximum iterations for loop orchestration
    pub max_iterations: u32,
}

impl Pipeline {
    /// Create a sequential pipeline
    pub fn sequential(name: impl Into<String>, agents: Vec<Agent>) -> Self {
        Self {
            name: name.into(),
            orchestration: OrchestrationType::Sequential,
            agents: agents.into_iter().map(Arc::new).collect(),
            max_iterations: 1,
        }
    }

    /// Create a parallel pipeline
    pub fn parallel(name: impl Into<String>, agents: Vec<Agent>) -> Self {
        Self {
            name: name.into(),
            orchestration: OrchestrationType::Parallel,
            agents: agents.into_iter().map(Arc::new).collect(),
            max_iterations: 1,
        }
    }

    /// Create a loop pipeline with a single agent
    pub fn loop_agent(name: impl Into<String>, agent: Agent, max_iterations: u32) -> Self {
        Self {
            name: name.into(),
            orchestration: OrchestrationType::Loop,
            agents: vec![Arc::new(agent)],
            max_iterations,
        }
    }
}

/// A swarm of agents with a coordinator
#[derive(Debug, Clone)]
pub struct Swarm {
    /// Swarm name
    pub name: String,
    /// Description
    pub description: String,
    /// Coordinator agent
    pub coordinator: Arc<Agent>,
    /// Worker agents
    pub workers: Vec<Arc<Agent>>,
    /// Goal description
    pub goal: Option<String>,
}

impl Swarm {
    /// Create a new swarm
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        coordinator: Agent,
        workers: Vec<Agent>,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            coordinator: Arc::new(coordinator),
            workers: workers.into_iter().map(Arc::new).collect(),
            goal: None,
        }
    }

    /// Set the goal
    pub fn with_goal(mut self, goal: impl Into<String>) -> Self {
        self.goal = Some(goal.into());
        self
    }
}

/// Orchestrator handles multi-agent execution
pub struct Orchestrator {
    executor: Arc<Executor>,
}

impl Orchestrator {
    /// Create a new orchestrator
    pub fn new(executor: Arc<Executor>) -> Self {
        Self { executor }
    }

    /// Run a pipeline
    pub async fn run_pipeline(
        &self,
        pipeline: &Pipeline,
        input: &str,
        ctx: &mut ExecutionContext,
    ) -> AgencyResult<OrchestratorResult> {
        match pipeline.orchestration {
            OrchestrationType::Sequential => self.run_sequential(pipeline, input, ctx).await,
            OrchestrationType::Parallel => self.run_parallel(pipeline, input, ctx).await,
            OrchestrationType::Loop => self.run_loop(pipeline, input, ctx).await,
            OrchestrationType::Hierarchical => Err(AgencyError::OrchestrationError(
                "Use run_swarm for hierarchical orchestration".to_string(),
            )),
        }
    }

    /// Run agents sequentially
    async fn run_sequential(
        &self,
        pipeline: &Pipeline,
        input: &str,
        ctx: &mut ExecutionContext,
    ) -> AgencyResult<OrchestratorResult> {
        let start_time = std::time::Instant::now();
        let mut results = Vec::new();
        let mut events = Vec::new();
        let mut token_usage = TokenUsage::default();
        let mut current_input = input.to_string();

        for agent_arc in &pipeline.agents {
            let agent = agent_arc.as_ref();
            let mut session = Session::new(agent.name(), ctx.user_id.clone());

            let result = self
                .executor
                .execute(agent, &mut session, &current_input, ctx)
                .await?;

            // Use this agent's output as next agent's input
            current_input = result.response.clone();
            token_usage.add(&result.token_usage);
            events.extend(result.events.clone());
            results.push(result);
        }

        let final_response = results
            .last()
            .map(|r| r.response.clone())
            .unwrap_or_default();

        Ok(OrchestratorResult {
            response: final_response,
            agent_results: results,
            events,
            token_usage,
            duration_ms: start_time.elapsed().as_millis() as u64,
            iterations: 1,
        })
    }

    /// Run agents in parallel
    async fn run_parallel(
        &self,
        pipeline: &Pipeline,
        input: &str,
        ctx: &mut ExecutionContext,
    ) -> AgencyResult<OrchestratorResult> {
        let start_time = std::time::Instant::now();
        let mut handles = Vec::new();

        for agent_arc in &pipeline.agents {
            let agent = agent_arc.clone();
            let executor = self.executor.clone();
            let input = input.to_string();
            let user_id = ctx.user_id.clone();

            handles.push(tokio::spawn(async move {
                let mut session = Session::new(agent.name(), user_id.clone());
                let mut ctx = ExecutionContext::new(&session);
                ctx.user_id = user_id;

                executor
                    .execute(agent.as_ref(), &mut session, &input, &mut ctx)
                    .await
            }));
        }

        let mut results = Vec::new();
        let mut events = Vec::new();
        let mut token_usage = TokenUsage::default();
        let mut responses = Vec::new();

        for handle in handles {
            match handle.await {
                Ok(Ok(result)) => {
                    responses.push(result.response.clone());
                    token_usage.add(&result.token_usage);
                    events.extend(result.events.clone());
                    results.push(result);
                }
                Ok(Err(e)) => {
                    return Err(e);
                }
                Err(e) => {
                    return Err(AgencyError::ExecutionFailed(e.to_string()));
                }
            }
        }

        // Combine responses
        let final_response = responses.join("\n\n---\n\n");

        Ok(OrchestratorResult {
            response: final_response,
            agent_results: results,
            events,
            token_usage,
            duration_ms: start_time.elapsed().as_millis() as u64,
            iterations: 1,
        })
    }

    /// Run agent in a loop until condition met or max iterations
    async fn run_loop(
        &self,
        pipeline: &Pipeline,
        input: &str,
        ctx: &mut ExecutionContext,
    ) -> AgencyResult<OrchestratorResult> {
        let start_time = std::time::Instant::now();
        let mut results = Vec::new();
        let mut events = Vec::new();
        let mut token_usage = TokenUsage::default();
        let mut current_input = input.to_string();
        let mut iterations = 0;

        let agent_arc = pipeline.agents.first().ok_or_else(|| {
            AgencyError::OrchestrationError("Loop pipeline requires at least one agent".to_string())
        })?;

        loop {
            iterations += 1;
            if iterations > pipeline.max_iterations {
                break;
            }

            let agent = agent_arc.as_ref();
            let mut session = Session::new(agent.name(), ctx.user_id.clone());

            let result = self
                .executor
                .execute(agent, &mut session, &current_input, ctx)
                .await?;

            token_usage.add(&result.token_usage);
            events.extend(result.events.clone());
            results.push(result.clone());

            // Check for completion signal in response
            // (e.g., "DONE", "COMPLETE", or other markers)
            if result.response.contains("DONE")
                || result.response.contains("COMPLETE")
                || result.response.contains("FINISHED")
            {
                break;
            }

            // Use output as next input
            current_input = result.response;
        }

        let final_response = results
            .last()
            .map(|r| r.response.clone())
            .unwrap_or_default();

        Ok(OrchestratorResult {
            response: final_response,
            agent_results: results,
            events,
            token_usage,
            duration_ms: start_time.elapsed().as_millis() as u64,
            iterations,
        })
    }

    /// Run a swarm with coordinator
    pub async fn run_swarm(
        &self,
        swarm: &Swarm,
        input: &str,
        ctx: &mut ExecutionContext,
    ) -> AgencyResult<OrchestratorResult> {
        let start_time = std::time::Instant::now();
        let mut results = Vec::new();
        let mut events = Vec::new();
        let mut token_usage = TokenUsage::default();

        // First, have coordinator analyze and delegate
        let coordinator = swarm.coordinator.as_ref();
        let mut coord_session = Session::new(coordinator.name(), ctx.user_id.clone());

        // Build context for coordinator including available workers
        let worker_info: Vec<_> = swarm
            .workers
            .iter()
            .map(|w| format!("- {}: {}", w.name(), w.description()))
            .collect();

        let coordinator_input = format!(
            "Task: {}\n\nAvailable workers:\n{}\n\nAnalyze the task and delegate to appropriate workers.",
            input,
            worker_info.join("\n")
        );

        let coord_result = self
            .executor
            .execute(coordinator, &mut coord_session, &coordinator_input, ctx)
            .await?;

        token_usage.add(&coord_result.token_usage);
        events.extend(coord_result.events.clone());
        results.push(coord_result.clone());

        // Emit handoff event
        let handoff_event = AgencyEvent {
            event_type: EventType::Handoff,
            agent_name: coordinator.name().to_string(),
            data: serde_json::json!({
                "from": coordinator.name(),
                "task": input
            }),
            timestamp: Utc::now(),
            session_id: Some(coord_session.id.clone()),
        };
        events.push(handoff_event.clone());
        ctx.emit(handoff_event).await;

        // TODO: Parse coordinator response to determine which workers to call
        // For now, run all workers in parallel with the original input
        for worker_arc in &swarm.workers {
            let worker = worker_arc.as_ref();
            let mut worker_session = Session::new(worker.name(), ctx.user_id.clone());

            let worker_result = self
                .executor
                .execute(worker, &mut worker_session, input, ctx)
                .await?;

            token_usage.add(&worker_result.token_usage);
            events.extend(worker_result.events.clone());
            results.push(worker_result);
        }

        // Have coordinator synthesize results
        let worker_results: Vec<_> = results
            .iter()
            .skip(1) // Skip coordinator's initial result
            .map(|r| format!("Result: {}", r.response))
            .collect();

        let synthesis_input = format!(
            "Original task: {}\n\nWorker results:\n{}\n\nSynthesize these results into a final response.",
            input,
            worker_results.join("\n\n")
        );

        let final_result = self
            .executor
            .execute(coordinator, &mut coord_session, &synthesis_input, ctx)
            .await?;

        token_usage.add(&final_result.token_usage);
        events.extend(final_result.events.clone());
        results.push(final_result.clone());

        Ok(OrchestratorResult {
            response: final_result.response,
            agent_results: results,
            events,
            token_usage,
            duration_ms: start_time.elapsed().as_millis() as u64,
            iterations: 1,
        })
    }
}

/// Result from orchestrator execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrchestratorResult {
    /// Final combined response
    pub response: String,
    /// Individual agent results
    pub agent_results: Vec<ExecutionResult>,
    /// All events emitted
    pub events: Vec<AgencyEvent>,
    /// Total token usage
    pub token_usage: TokenUsage,
    /// Execution duration in milliseconds
    pub duration_ms: u64,
    /// Number of iterations (for loop orchestration)
    pub iterations: u32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agency::agent::AgentBuilder;
    use crate::agency::tools::ToolRegistry;

    fn create_test_agent(name: &str) -> Agent {
        AgentBuilder::new(name)
            .description(format!("{} agent", name))
            .instruction("You are a helpful assistant.")
            .model("gemini-2.5-flash")
            .build()
    }

    #[tokio::test]
    #[ignore = "Integration test - requires API credentials"]
    async fn test_sequential_pipeline() {
        let tool_registry = Arc::new(ToolRegistry::new());
        let executor = Arc::new(Executor::new(tool_registry));
        let orchestrator = Orchestrator::new(executor);

        let agents = vec![create_test_agent("researcher"), create_test_agent("writer")];
        let pipeline = Pipeline::sequential("research_pipeline", agents);

        let session = Session::new("test", None);
        let mut ctx = ExecutionContext::new(&session);

        let result = orchestrator
            .run_pipeline(&pipeline, "Tell me about Rust", &mut ctx)
            .await
            .unwrap();

        assert!(!result.response.is_empty());
        assert_eq!(result.agent_results.len(), 2);
    }

    #[tokio::test]
    #[ignore = "Integration test - requires API credentials"]
    async fn test_parallel_pipeline() {
        let tool_registry = Arc::new(ToolRegistry::new());
        let executor = Arc::new(Executor::new(tool_registry));
        let orchestrator = Orchestrator::new(executor);

        let agents = vec![create_test_agent("analyst1"), create_test_agent("analyst2")];
        let pipeline = Pipeline::parallel("analysis_pipeline", agents);

        let session = Session::new("test", None);
        let mut ctx = ExecutionContext::new(&session);

        let result = orchestrator
            .run_pipeline(&pipeline, "Analyze this data", &mut ctx)
            .await
            .unwrap();

        assert!(!result.response.is_empty());
        assert_eq!(result.agent_results.len(), 2);
    }
}