aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
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
//! Tests for TUI chat functionality

#[cfg(test)]
mod tui_chat_tests {
    use aethershell::tui::agents::MultiModalAgent;
    use aethershell::tui::app::{AgentInfo, AgentStatus, ChatMessage, MessageRole};
    use aethershell::tui::chat::{
        ChatManager, ChatSession, ChatSettings, Participant, ParticipantType,
    };
    use aethershell::tui::media::{MediaFile, MediaType};
    use chrono::Utc;
    use uuid::Uuid;

    #[test]
    fn test_chat_session_creation() {
        let session = ChatSession::new("Test Session".to_string());

        assert_eq!(session.title, "Test Session");
        assert!(session.messages.is_empty());
        assert!(session.participants.is_empty());
        assert!(session.created_at <= Utc::now());

        // Allow for small timing difference between created_at and updated_at
        let time_diff = (session.updated_at - session.created_at)
            .num_milliseconds()
            .abs();
        assert!(
            time_diff < 1000,
            "Time difference between created_at and updated_at should be less than 1 second"
        );
    }

    #[test]
    fn test_chat_settings_defaults() {
        let settings = ChatSettings::default();

        assert!(!settings.auto_summarize);
        assert_eq!(settings.context_window_size, 4096);
        assert!(settings.enable_media_analysis);
        assert_eq!(settings.temperature, 0.7);
        assert_eq!(settings.max_tokens, None);
        assert_eq!(settings.system_prompt, None);
    }

    #[test]
    fn test_participant_management() {
        let mut session = ChatSession::new("Multi-participant Chat".to_string());

        let user_participant = Participant {
            id: Uuid::new_v4(),
            name: "User".to_string(),
            participant_type: ParticipantType::User,
            model: None,
            capabilities: vec![],
        };

        let agent_participant = Participant {
            id: Uuid::new_v4(),
            name: "AI Assistant".to_string(),
            participant_type: ParticipantType::Agent,
            model: Some("gpt-4".to_string()),
            capabilities: vec!["text_generation".to_string(), "image_analysis".to_string()],
        };

        session.add_participant(user_participant);
        session.add_participant(agent_participant);

        assert_eq!(session.participants.len(), 2);
        assert_eq!(
            session.participants[0].participant_type,
            ParticipantType::User
        );
        assert_eq!(
            session.participants[1].participant_type,
            ParticipantType::Agent
        );
        assert!(session.updated_at > session.created_at);
    }

    #[test]
    fn test_message_addition() {
        let mut session = ChatSession::new("Message Test".to_string());

        let message = ChatMessage {
            id: Uuid::new_v4(),
            timestamp: Utc::now(),
            role: MessageRole::User,
            content: "Hello, world!".to_string(),
            media_attachments: vec![],
            model: None,
        };

        let original_updated = session.updated_at;
        session.add_message(message);

        assert_eq!(session.messages.len(), 1);
        assert_eq!(session.messages[0].content, "Hello, world!");
        assert!(session.updated_at > original_updated);
    }

    #[test]
    fn test_multimodal_message_processing() {
        let mut session = ChatSession::new("Multimodal Test".to_string());

        let media_file = MediaFile {
            path: "test_image.jpg".to_string(),
            media_type: MediaType::Image,
            size: Some((800, 600)),
            duration: None,
            thumbnail: None,
        };

        let result = session.process_multimodal_message(
            "What's in this image?".to_string(),
            vec![media_file],
            Uuid::new_v4(),
        );

        // Skip if AI not configured
        if std::env::var("AETHER_AI").is_err() {
            return;
        }

        assert!(result.is_ok());

        let message = result.unwrap();
        assert_eq!(message.role, MessageRole::User);
        assert!(message.content.contains("What's in this image?"));
        assert_eq!(message.media_attachments.len(), 1);

        // Check that message was added to session
        assert_eq!(session.messages.len(), 1);
    }

    #[test]
    fn test_context_generation() {
        let mut session = ChatSession::new("Context Test".to_string());
        session.settings.context_window_size = 2;

        // Add multiple messages
        let messages = vec![
            ("First message", MessageRole::User),
            ("First response", MessageRole::Assistant),
            ("Second message", MessageRole::User),
            ("Second response", MessageRole::Assistant),
            ("Third message", MessageRole::User),
        ];

        for (content, role) in messages {
            let message = ChatMessage {
                id: Uuid::new_v4(),
                timestamp: Utc::now(),
                role,
                content: content.to_string(),
                media_attachments: vec![],
                model: None,
            };
            session.add_message(message);
        }

        // Get context (should only include last 2 messages due to window size)
        let context = session.get_context_for_ai(false);
        assert!(context.contains("Second response"));
        assert!(context.contains("Third message"));
        assert!(!context.contains("First message"));
    }

    #[test]
    fn test_context_with_media() {
        let mut session = ChatSession::new("Media Context Test".to_string());

        let media_file = MediaFile {
            path: "context_image.png".to_string(),
            media_type: MediaType::Image,
            size: Some((1024, 768)),
            duration: None,
            thumbnail: None,
        };

        let message = ChatMessage {
            id: Uuid::new_v4(),
            timestamp: Utc::now(),
            role: MessageRole::User,
            content: "Analyze this image".to_string(),
            media_attachments: vec![media_file],
            model: None,
        };

        session.add_message(message);

        // Test context with media
        let context_with_media = session.get_context_for_ai(true);
        assert!(context_with_media.contains("Analyze this image"));
        assert!(context_with_media.contains("Media:"));
        assert!(context_with_media.contains("context_image.png"));

        // Test context without media
        let context_without_media = session.get_context_for_ai(false);
        assert!(context_without_media.contains("Analyze this image"));
        assert!(!context_without_media.contains("Media:"));
    }

    #[test]
    fn test_markdown_export() {
        let mut session = ChatSession::new("Export Test".to_string());

        // Add a participant
        let participant = Participant {
            id: Uuid::new_v4(),
            name: "Test User".to_string(),
            participant_type: ParticipantType::User,
            model: None,
            capabilities: vec![],
        };
        session.add_participant(participant);

        // Add messages
        let user_message = ChatMessage {
            id: Uuid::new_v4(),
            timestamp: Utc::now(),
            role: MessageRole::User,
            content: "Hello!".to_string(),
            media_attachments: vec![],
            model: None,
        };

        let assistant_message = ChatMessage {
            id: Uuid::new_v4(),
            timestamp: Utc::now(),
            role: MessageRole::Assistant,
            content: "Hi there!".to_string(),
            media_attachments: vec![],
            model: None,
        };

        session.add_message(user_message);
        session.add_message(assistant_message);

        let markdown = session.export_to_markdown();

        assert!(markdown.contains("# Export Test"));
        assert!(markdown.contains("Test User"));
        assert!(markdown.contains("👤 User"));
        assert!(markdown.contains("🤖 Assistant"));
        assert!(markdown.contains("Hello!"));
        assert!(markdown.contains("Hi there!"));
    }

    #[test]
    fn test_chat_manager() {
        let manager = ChatManager::new();

        assert!(manager.sessions.is_empty());
        assert_eq!(manager.active_session_id, None);
        assert!(manager.agents.is_empty());
    }

    #[test]
    fn test_session_management() {
        let mut manager = ChatManager::new();

        // Create a session
        let session_id = manager.create_session("Test Session".to_string());

        assert_eq!(manager.sessions.len(), 1);
        assert_eq!(manager.active_session_id, Some(session_id));
        assert!(manager.sessions.contains_key(&session_id));

        // Get active session
        let active_session = manager.get_active_session();
        assert!(active_session.is_some());
        assert_eq!(active_session.unwrap().title, "Test Session");

        // Create another session
        let session_id2 = manager.create_session("Second Session".to_string());
        assert_eq!(manager.sessions.len(), 2);
        assert_eq!(manager.active_session_id, Some(session_id2));

        // Switch back to first session
        let switch_result = manager.switch_session(session_id);
        assert!(switch_result.is_ok());
        assert_eq!(manager.active_session_id, Some(session_id));
    }

    #[test]
    fn test_agent_integration() {
        let mut manager = ChatManager::new();
        let session_id = manager.create_session("Agent Test".to_string());

        // Create a multimodal agent
        let base_agent = AgentInfo {
            id: Uuid::new_v4(),
            name: "TestAgent".to_string(),
            model: "stub".to_string(),
            status: AgentStatus::Idle,
            current_task: None,
            tools: vec!["print".to_string()],
            created_at: Utc::now(),
            last_activity: Utc::now(),
        };

        let agent = MultiModalAgent::new(base_agent);
        let agent_id = agent.base_agent.id;

        // Add agent to session
        let result = manager.add_agent_to_session(session_id, agent);
        assert!(result.is_ok());

        // Check that agent was added to session and manager
        let session = manager.sessions.get(&session_id).unwrap();
        assert_eq!(session.participants.len(), 1);
        assert_eq!(session.participants[0].id, agent_id);
        assert!(manager.agents.contains_key(&agent_id));
    }

    #[test]
    fn test_agent_response_processing() {
        let mut manager = ChatManager::new();
        let session_id = manager.create_session("Response Test".to_string());

        let base_agent = AgentInfo {
            id: Uuid::new_v4(),
            name: "ResponseAgent".to_string(),
            model: "stub".to_string(),
            status: AgentStatus::Idle,
            current_task: None,
            tools: vec![],
            created_at: Utc::now(),
            last_activity: Utc::now(),
        };

        let agent_id = base_agent.id;
        let agent = MultiModalAgent::new(base_agent);

        manager.add_agent_to_session(session_id, agent).unwrap();

        // Process agent response
        let result = manager.process_agent_response(
            session_id,
            agent_id,
            "Test input for agent".to_string(),
        );

        // Skip if AI not configured
        if std::env::var("AETHER_AI").is_err() {
            return;
        }

        assert!(result.is_ok());

        // Check that response was added to session
        let session = manager.sessions.get(&session_id).unwrap();
        assert_eq!(session.messages.len(), 1);
        assert_eq!(session.messages[0].role, MessageRole::Assistant);
    }

    #[test]
    fn test_auto_summarization() {
        let mut session = ChatSession::new("Summarization Test".to_string());
        session.settings.auto_summarize = true;
        session.settings.context_window_size = 2;

        // Add messages that exceed context window
        for i in 0..5 {
            let message = ChatMessage {
                id: Uuid::new_v4(),
                timestamp: Utc::now(),
                role: if i % 2 == 0 {
                    MessageRole::User
                } else {
                    MessageRole::Assistant
                },
                content: format!("Message {}", i),
                media_attachments: vec![],
                model: None,
            };
            session.add_message(message);
        }

        // Summarization requires AI, so verify behavior based on whether AI is configured
        // If no AI configured, messages just accumulate (summarization fails silently)
        if std::env::var("AETHER_AI").is_err() {
            // Without AI, all 5 messages should be present (no summarization occurred)
            assert_eq!(session.messages.len(), 5);
            return;
        }

        // Should have triggered summarization when AI is available
        // Check that first message is now a system summary
        assert!(session.messages.len() <= session.settings.context_window_size + 1);
        if session.messages.len() > 2 {
            assert_eq!(session.messages[0].role, MessageRole::System);
            assert!(session.messages[0].content.contains("Conversation Summary"));
        }
    }

    #[test]
    fn test_system_prompt_context() {
        let mut session = ChatSession::new("System Prompt Test".to_string());
        session.settings.system_prompt = Some("You are a helpful assistant.".to_string());

        let context = session.get_context_for_ai(false);
        assert!(context.contains("System: You are a helpful assistant."));
    }
}