llm 1.3.8

A Rust library unifying multiple LLM backends.
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
//! Dialogue controller methods for multi-LLM conversations.

use crate::dialogue::{
    DialogueConfig, DialogueController, DialogueMode, DialogueParticipant, StopReason, TurnMode,
};
use crate::provider::{ProviderFactory, ProviderId};
use crate::runtime::overlay::{DialogueBuilderResult, DialogueBuilderState};
use crate::runtime::{AppStatus, OverlayState};

use super::AppController;

impl AppController {
    /// Opens the dialogue builder overlay.
    pub fn open_dialogue_builder(&mut self) -> bool {
        self.state.overlay = OverlayState::DialogueBuilder(DialogueBuilderState::new());
        true
    }

    /// Starts a dialogue with the specified participants.
    pub fn start_dialogue(&mut self, participants: Vec<&str>) -> bool {
        if participants.len() < 2 {
            self.set_status(AppStatus::Error(
                "Multi-LLM dialogue requires at least 2 participants".to_string(),
            ));
            return false;
        }

        let factory = ProviderFactory::new(&self.state.config, &self.state.provider_registry);

        let mut dialogue_participants = Vec::new();
        let mut participant_providers = Vec::new();

        for (index, provider_model) in participants.iter().enumerate() {
            let Some(participant) = DialogueParticipant::from_provider_model(provider_model, index)
            else {
                self.set_status(AppStatus::Error(format!(
                    "Invalid participant format: {}. Use provider:model",
                    provider_model
                )));
                return false;
            };

            // Build provider for this participant
            let provider_id = ProviderId::new(&participant.provider_id);
            let selection = crate::provider::ProviderSelection {
                provider_id,
                model: Some(participant.model_id.clone()),
            };
            let overrides = crate::provider::ProviderOverrides::default();

            match factory.build(&selection, overrides) {
                Ok(instance) => {
                    participant_providers.push((participant.clone(), instance.provider));
                    dialogue_participants.push(participant);
                }
                Err(_) => {
                    self.set_status(AppStatus::Error(format!(
                        "Failed to create provider for {}",
                        provider_model
                    )));
                    return false;
                }
            }
        }

        // Initialize dialogue in conversation state
        let config = DialogueConfig {
            mode: DialogueMode::FreeForm,
            initial_prompt: String::new(),
            turn_mode: crate::dialogue::TurnMode::RoundRobin,
        };

        if let Some(conv) = self.state.conversations.active_mut() {
            conv.start_dialogue(
                DialogueMode::FreeForm,
                config.clone(),
                dialogue_participants.clone(),
            );
        }

        // Initialize the dialogue controller with participants
        let mut controller = DialogueController::new(config);
        for (participant, provider) in participant_providers {
            controller.add_participant(participant, provider);
        }
        self.state.dialogue_controller = Some(controller);

        self.push_notice(format!(
            "Dialogue started with {} participants. Type /continue to begin or send a message.",
            dialogue_participants.len()
        ));
        self.set_status(AppStatus::Idle);
        true
    }

    /// Invites a new participant to the active dialogue.
    pub fn invite_dialogue_participant(&mut self, provider_model: &str) -> bool {
        let Some(ref mut controller) = self.state.dialogue_controller else {
            self.set_status(AppStatus::Error("No active dialogue".to_string()));
            return false;
        };

        let index = controller.participants().len();
        let Some(participant) = DialogueParticipant::from_provider_model(provider_model, index)
        else {
            self.set_status(AppStatus::Error(format!(
                "Invalid participant format: {}. Use provider:model",
                provider_model
            )));
            return false;
        };

        // Verify provider exists
        let provider_id = ProviderId::new(&participant.provider_id);
        if self.state.provider_registry.get(&provider_id).is_none() {
            self.set_status(AppStatus::Error(format!(
                "Unknown provider: {}",
                participant.provider_id
            )));
            return false;
        }

        // Build provider for the participant
        let factory = ProviderFactory::new(&self.state.config, &self.state.provider_registry);
        let selection = crate::provider::ProviderSelection {
            provider_id,
            model: Some(participant.model_id.clone()),
        };
        let overrides = crate::provider::ProviderOverrides::default();

        let Ok(instance) = factory.build(&selection, overrides) else {
            self.set_status(AppStatus::Error(format!(
                "Failed to create provider for {}",
                provider_model
            )));
            return false;
        };

        controller.add_participant(participant.clone(), instance.provider);

        // Also add to conversation state
        if let Some(conv) = self.state.conversations.active_mut() {
            conv.participants.push(participant.clone());
        }

        self.push_notice(format!("Invited {} to dialogue", participant.display_name));
        true
    }

    /// Kicks a participant from the active dialogue.
    pub fn kick_dialogue_participant(&mut self, name: &str) -> bool {
        let Some(ref mut controller) = self.state.dialogue_controller else {
            self.set_status(AppStatus::Error("No active dialogue".to_string()));
            return false;
        };

        let Some(participant) = controller.find_participant_by_name(name) else {
            self.set_status(AppStatus::Error(format!(
                "Participant '{}' not found",
                name
            )));
            return false;
        };

        let id = participant.config.id;
        let display_name = participant.config.display_name.clone();

        if controller.kick_participant(id) {
            // Also update conversation state
            if let Some(conv) = self.state.conversations.active_mut() {
                if let Some(pos) = conv.participants.iter().position(|p| p.id == id) {
                    conv.participants[pos].active = false;
                }
            }

            self.push_notice(format!("Kicked {} from dialogue", display_name));
            true
        } else {
            self.set_status(AppStatus::Error(format!("Failed to kick {}", display_name)));
            false
        }
    }

    /// Stops the active dialogue.
    pub fn stop_dialogue(&mut self) -> bool {
        let Some(ref mut controller) = self.state.dialogue_controller else {
            self.set_status(AppStatus::Error("No active dialogue".to_string()));
            return false;
        };

        controller.stop(StopReason::UserRequested);

        // Clear dialogue state from conversation
        if let Some(conv) = self.state.conversations.active_mut() {
            conv.end_dialogue();
        }

        self.state.dialogue_controller = None;
        self.push_notice("Dialogue stopped".to_string());
        self.set_status(AppStatus::Idle);
        true
    }

    /// Starts a dialogue from the builder result.
    pub fn start_dialogue_from_builder(&mut self, result: DialogueBuilderResult) -> bool {
        if result.participants.len() < 2 {
            self.set_status(AppStatus::Error(
                "Multi-LLM dialogue requires at least 2 participants".to_string(),
            ));
            return false;
        }

        let factory = ProviderFactory::new(&self.state.config, &self.state.provider_registry);

        // Convert draft participants to DialogueParticipant and build providers
        let mut dialogue_participants: Vec<DialogueParticipant> = Vec::new();
        let mut participant_providers = Vec::new();

        for draft in &result.participants {
            let participant = DialogueParticipant {
                id: crate::dialogue::ParticipantId::new(),
                provider_id: draft.provider_id.clone(),
                model_id: draft.model_id.clone(),
                display_name: draft.display_name.clone(),
                system_prompt: draft.system_prompt.clone(),
                params: crate::dialogue::ParticipantParams::default(),
                color: draft.color,
                active: true,
            };

            // Build provider for this participant
            let provider_id = ProviderId::new(&draft.provider_id);
            let selection = crate::provider::ProviderSelection {
                provider_id,
                model: Some(draft.model_id.clone()),
            };
            let overrides = crate::provider::ProviderOverrides::default();

            match factory.build(&selection, overrides) {
                Ok(instance) => {
                    participant_providers.push((participant.clone(), instance.provider));
                    dialogue_participants.push(participant);
                }
                Err(_) => {
                    self.set_status(AppStatus::Error(format!(
                        "Failed to create provider for {}:{}",
                        draft.provider_id, draft.model_id
                    )));
                    return false;
                }
            }
        }

        // Initialize dialogue in conversation state
        let config = DialogueConfig {
            mode: result.mode,
            initial_prompt: result.initial_prompt.clone(),
            turn_mode: TurnMode::RoundRobin,
        };

        if let Some(conv) = self.state.conversations.active_mut() {
            conv.start_dialogue(result.mode, config.clone(), dialogue_participants.clone());
        }

        // Initialize the dialogue controller with participants
        let mut controller = DialogueController::new(config);
        for (participant, provider) in participant_providers {
            controller.add_participant(participant, provider);
        }
        self.state.dialogue_controller = Some(controller);

        self.push_notice(format!(
            "Dialogue started with {} participants. Type /continue to begin or send a message.",
            dialogue_participants.len()
        ));
        self.set_status(AppStatus::Idle);
        true
    }

    /// Returns whether a dialogue is currently active.
    pub fn is_dialogue_active(&self) -> bool {
        self.state.dialogue_controller.is_some()
    }

    /// Gets the active dialogue controller.
    #[allow(dead_code)]
    pub fn dialogue_controller(&self) -> Option<&DialogueController> {
        self.state.dialogue_controller.as_ref()
    }

    /// Gets a mutable reference to the active dialogue controller.
    #[allow(dead_code)]
    pub fn dialogue_controller_mut(&mut self) -> Option<&mut DialogueController> {
        self.state.dialogue_controller.as_mut()
    }

    /// Continues the dialogue with the next participant's turn.
    pub fn continue_dialogue(&mut self) -> bool {
        let Some(ref dialogue) = self.state.dialogue_controller else {
            self.set_status(AppStatus::Error("No active dialogue".to_string()));
            return false;
        };

        let Some(next_participant) = dialogue.next_participant() else {
            self.set_status(AppStatus::Error("No active participants".to_string()));
            return false;
        };

        let participant_name = next_participant.config.display_name.clone();
        let participant_color = next_participant.config.color;
        let participant_system_prompt = next_participant.config.system_prompt.clone();
        let provider_id = ProviderId::new(&next_participant.config.provider_id);

        // Build provider for the next participant with their system prompt
        let factory = ProviderFactory::new(&self.state.config, &self.state.provider_registry);
        let selection = crate::provider::ProviderSelection {
            provider_id,
            model: Some(next_participant.config.model_id.clone()),
        };
        let overrides = crate::provider::ProviderOverrides {
            system: participant_system_prompt,
            ..Default::default()
        };

        let Ok(instance) = factory.build(&selection, overrides) else {
            self.set_status(AppStatus::Error(format!(
                "Failed to create provider for {}",
                participant_name
            )));
            return false;
        };

        // Create a placeholder message for the next participant
        let Some(conv_id) = self.state.active_conversation_id() else {
            return false;
        };

        // Add a placeholder assistant message with participant metadata
        let assistant_id =
            self.append_dialogue_placeholder(conv_id, &participant_name, participant_color);

        // Build context messages from dialogue history
        let Some(conversation) = self.state.active_conversation().cloned() else {
            return false;
        };

        // Get messages for context, passing current participant name
        let messages = self.build_dialogue_messages(&conversation, &participant_name);

        // Start stream for this participant
        let request = crate::runtime::streaming::StreamRequest {
            conversation_id: conv_id,
            message_id: assistant_id,
            provider: instance.provider,
            messages,
            capabilities: instance.capabilities,
        };

        self.stream_manager.start(request);
        self.set_status(AppStatus::Streaming);
        self.push_notice(format!("{} is responding...", participant_name));
        true
    }

    /// Appends a placeholder message for a dialogue participant.
    fn append_dialogue_placeholder(
        &mut self,
        conv_id: crate::conversation::ConversationId,
        participant_name: &str,
        color: crate::dialogue::ParticipantColor,
    ) -> crate::conversation::MessageId {
        use crate::conversation::{ConversationMessage, MessageKind, MessageRole, MessageState};

        let mut message =
            ConversationMessage::new(MessageRole::Assistant, MessageKind::Text(String::new()));
        message.state = MessageState::Streaming;
        message.metadata.participant_name = Some(participant_name.to_string());
        message.metadata.participant_color = Some(color);

        let id = message.id;
        if let Some(conv) = self.state.conversations.active_mut() {
            if conv.id == conv_id {
                conv.push_message(message);
            }
        }
        id
    }

    /// Builds chat messages from dialogue history for context.
    ///
    /// For dialogue mode, messages from OTHER participants are sent as User role
    /// so the current participant sees them as input to respond to.
    fn build_dialogue_messages(
        &self,
        conversation: &crate::conversation::Conversation,
        current_participant: &str,
    ) -> Vec<llm::chat::ChatMessage> {
        use crate::conversation::{MessageKind, MessageRole};
        use llm::chat::ChatMessage;

        let mut messages = Vec::new();

        // Add dialogue initial prompt as user message if present
        if let Some(ref config) = conversation.dialogue_config {
            if !config.initial_prompt.is_empty() {
                messages.push(
                    ChatMessage::user()
                        .content(config.initial_prompt.clone())
                        .build(),
                );
            }
        }

        // Add conversation messages
        for msg in &conversation.messages {
            if let MessageKind::Text(ref content) = msg.kind {
                if content.is_empty() {
                    continue;
                }

                // Determine the role based on who sent the message
                let role = match msg.role {
                    MessageRole::User => llm::chat::ChatRole::User,
                    MessageRole::Assistant => {
                        // Check if this message is from the current participant
                        if let Some(ref name) = msg.metadata.participant_name {
                            if name == current_participant {
                                // This participant's own previous messages = Assistant
                                llm::chat::ChatRole::Assistant
                            } else {
                                // Other participants' messages = User (so we respond to them)
                                llm::chat::ChatRole::User
                            }
                        } else {
                            // No participant name (shouldn't happen in dialogue)
                            llm::chat::ChatRole::Assistant
                        }
                    }
                    MessageRole::Tool | MessageRole::Error => continue,
                };

                // Add participant prefix for clarity
                let content = if let Some(ref name) = msg.metadata.participant_name {
                    format!("[{}] {}", name, content)
                } else {
                    content.clone()
                };

                messages.push(ChatMessage {
                    role,
                    message_type: llm::chat::MessageType::Text,
                    content,
                });
            }
        }

        messages
    }
}