fluix 0.1.25

Rust UI components for GPUI
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
//! # Fluix AI Components
//! 
//! A comprehensive collection of AI-focused UI components for building modern AI applications.
//! 
//! ## Modules
//!
//! - [`prompt`] - AI prompt input component (PromptInput)
//! - [`message`] - Message component for displaying AI conversation messages (MessageBubble)
//! - [`model_selector`] - AI model selection component (ModelSelector)
//! 
//! ## Quick Start
//! 
//! ```rust,ignore
//! use fluix::ai::*;
//! use gpui::*;
//! 
//! // Create a simple chat interface
//! // ChatContainer::new(cx)
//! //     .with_prompt_input(
//! //         PromptInput::new(cx)
//! //             .placeholder("Ask me anything...")
//! //             .enable_attachments(true)
//! //     )
//! // ```

pub mod prompt;
pub mod message;
pub mod model_selector;

// Re-export commonly used types
pub use prompt::*;
pub use message::*;
pub use model_selector::*;

// Common types and traits used across AI components
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Unique identifier for messages
pub type MessageId = Uuid;

/// Represents the role of a message sender in AI conversations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageRole {
    /// Human user message
    User,
    /// AI assistant message
    Assistant,
    /// System message (instructions, notifications)
    System,
    /// Tool/function call result
    Tool,
}

/// Content types that can be displayed in AI messages
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessageContent {
    /// Plain text content
    Text(String),
    /// Code block with syntax highlighting
    Code { 
        language: String, 
        code: String 
    },
    /// Terminal command and output
    Terminal { 
        command: String, 
        output: String 
    },
    /// Image with metadata
    Image { 
        url: String, 
        alt: String,
        width: Option<u32>,
        height: Option<u32>,
    },
    /// File attachment
    File { 
        path: String, 
        name: String,
        size: u64,
        mime_type: Option<String>,
    },
    /// AI thinking process (internal reasoning)
    Thinking(String),
    /// Error message
    Error(String),
    /// Tool/function call
    Tool { 
        name: String, 
        input: String, 
        output: String 
    },
}

/// A message in an AI conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Unique identifier
    pub id: MessageId,
    /// Message sender role
    pub role: MessageRole,
    /// Message content (can contain multiple content blocks)
    pub content: Vec<MessageContent>,
    /// When the message was created
    pub timestamp: DateTime<Utc>,
    /// Whether the message is currently being streamed
    pub is_streaming: bool,
    /// Optional metadata
    pub metadata: Option<serde_json::Value>,
}

impl Message {
    /// Create a new user message with text content
    pub fn new_user(text: impl Into<String>) -> Self {
        Self {
            id: Uuid::new_v4(),
            role: MessageRole::User,
            content: vec![MessageContent::Text(text.into())],
            timestamp: Utc::now(),
            is_streaming: false,
            metadata: None,
        }
    }

    /// Create a new assistant message with content
    pub fn new_assistant(content: Vec<MessageContent>) -> Self {
        Self {
            id: Uuid::new_v4(),
            role: MessageRole::Assistant,
            content,
            timestamp: Utc::now(),
            is_streaming: false,
            metadata: None,
        }
    }

    /// Create a new system message
    pub fn new_system(text: impl Into<String>) -> Self {
        Self {
            id: Uuid::new_v4(),
            role: MessageRole::System,
            content: vec![MessageContent::Text(text.into())],
            timestamp: Utc::now(),
            is_streaming: false,
            metadata: None,
        }
    }

    /// Get the primary text content of the message
    pub fn get_text(&self) -> String {
        self.content
            .iter()
            .filter_map(|content| match content {
                MessageContent::Text(text) => Some(text.clone()),
                MessageContent::Thinking(text) => Some(text.clone()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Check if the message contains code
    pub fn has_code(&self) -> bool {
        self.content.iter().any(|content| matches!(content, MessageContent::Code { .. }))
    }

    /// Check if the message contains attachments
    pub fn has_attachments(&self) -> bool {
        self.content.iter().any(|content| {
            matches!(content, MessageContent::Image { .. } | MessageContent::File { .. })
        })
    }
}

/// Information about an AI model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
    /// Model identifier
    pub id: String,
    /// Human-readable name
    pub name: String,
    /// Model description
    pub description: Option<String>,
    /// Provider/company
    pub provider: String,
    /// Context window size
    pub context_length: Option<u32>,
    /// Model capabilities
    pub capabilities: Vec<ModelCapability>,
    /// Pricing information
    pub pricing: Option<PricingInfo>,
}

impl ModelInfo {
    /// Get default models from llm-link crate
    /// This function loads all available models from llm-link providers dynamically
    pub fn default_models_from_llm_link() -> Vec<Self> {
        use llm_link::models::ModelsConfig;
        
        let mut models = Vec::new();
        
        // Load models configuration from llm-link
        let models_config = ModelsConfig::load_with_fallback();
        
        // Get all providers dynamically from llm-link
        let provider_ids = models_config.get_all_providers();
        
        for provider_id in provider_ids {
            // Get models for each provider from ModelsConfig
            let provider_models = models_config.get_models_for_provider(&provider_id);
            
            // Format provider name for display (capitalize first letter)
            let provider_name = Self::format_provider_name(&provider_id);
            
            for model_info in provider_models {
                // Convert llm-link ModelInfo to our ModelInfo
                // Note: llm-link's ModelInfo has: id (String), name (String), description (String)
                let model = ModelInfo {
                    id: model_info.id.clone(),
                    name: model_info.name.clone(),
                    description: Some(model_info.description.clone()), // Wrap String in Some for Option<String>
                    provider: provider_name.clone(),
                    context_length: None, // llm-link doesn't provide context_length in ModelInfo
                    capabilities: Self::infer_capabilities_from_provider(&provider_id),
                    pricing: None, // llm-link doesn't provide pricing info
                };
                
                models.push(model);
            }
        }
        
        models
    }
    
    /// Format provider ID to display name
    /// Converts lowercase provider IDs (e.g., "openai") to formatted names (e.g., "OpenAI")
    fn format_provider_name(provider_id: &str) -> String {
        // Handle special cases for known providers
        match provider_id {
            "openai" => "OpenAI".to_string(),
            "anthropic" => "Anthropic".to_string(),
            "zhipu" => "Zhipu".to_string(),
            "aliyun" => "Aliyun".to_string(),
            "volcengine" => "Volcengine".to_string(),
            "tencent" => "Tencent".to_string(),
            "longcat" => "Longcat".to_string(),
            "moonshot" => "Moonshot".to_string(),
            "ollama" => "Ollama".to_string(),
            _ => {
                // Default: capitalize first letter
                let mut chars = provider_id.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
                }
            }
        }
    }
    
    /// Infer capabilities from provider name
    fn infer_capabilities_from_provider(provider: &str) -> Vec<ModelCapability> {
        // Default capabilities - most modern LLMs support these
        let mut capabilities = vec![
            ModelCapability::TextGeneration,
            ModelCapability::CodeGeneration,
        ];
        
        // Add provider-specific capabilities
        match provider {
            "openai" | "anthropic" | "zhipu" | "moonshot" => {
                capabilities.push(ModelCapability::FunctionCalling);
            }
            _ => {}
        }
        
        capabilities
    }
}

/// Model capabilities
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelCapability {
    /// Text generation and completion
    TextGeneration,
    /// Code generation and completion
    CodeGeneration,
    /// Image generation
    ImageGeneration,
    /// Image analysis and understanding
    ImageAnalysis,
    /// Function/tool calling
    FunctionCalling,
    /// Document analysis
    DocumentAnalysis,
    /// Web search
    WebSearch,
}

/// Pricing information for a model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PricingInfo {
    /// Input token price (per 1K tokens)
    pub input_price: f64,
    /// Output token price (per 1K tokens)
    pub output_price: f64,
    /// Currency code (e.g., "USD")
    pub currency: String,
}

/// Provider information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderInfo {
    /// Provider identifier
    pub id: String,
    /// Provider name
    pub name: String,
    /// Available models
    pub models: Vec<ModelInfo>,
    /// API endpoint
    pub endpoint: Option<String>,
    /// Whether authentication is required
    pub requires_auth: bool,
}

impl ProviderInfo {
    /// Get all providers from llm-link crate with their models
    /// This function loads all available providers and models dynamically
    pub fn all_from_llm_link() -> Vec<Self> {
        use llm_link::models::ModelsConfig;
        
        let mut providers = Vec::new();
        
        // Load models configuration from llm-link
        let models_config = ModelsConfig::load_with_fallback();
        
        // Get all providers dynamically from llm-link
        let provider_ids = models_config.get_all_providers();
        
        for provider_id in provider_ids {
            // Get models for this provider
            let provider_models = models_config.get_models_for_provider(&provider_id);
            
            // Convert llm-link models to our ModelInfo
            let models: Vec<ModelInfo> = provider_models
                .into_iter()
                .map(|model_info| {
                    let provider_name = ModelInfo::format_provider_name(&provider_id);
                    ModelInfo {
                        id: model_info.id.clone(),
                        name: model_info.name.clone(),
                        description: Some(model_info.description.clone()),
                        provider: provider_name.clone(),
                        context_length: None,
                        capabilities: ModelInfo::infer_capabilities_from_provider(&provider_id),
                        pricing: None,
                    }
                })
                .collect();
            
            // Format provider name for display
            let provider_name = ModelInfo::format_provider_name(&provider_id);
            
            let provider_info = ProviderInfo {
                id: provider_id.clone(),
                name: provider_name,
                models,
                endpoint: None, // llm-link doesn't provide endpoint info in ModelsConfig
                requires_auth: true, // Most providers require auth
            };
            
            providers.push(provider_info);
        }
        
        providers
    }
}

/// Attachment information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attachment {
    /// Attachment identifier
    pub id: String,
    /// File name
    pub name: String,
    /// File path or URL
    pub path: String,
    /// File size in bytes
    pub size: u64,
    /// MIME type
    pub mime_type: String,
    /// Attachment type
    pub attachment_type: AttachmentType,
}

/// Types of attachments
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AttachmentType {
    /// Image file
    Image,
    /// Document file
    Document,
    /// Code file
    Code,
    /// Audio file
    Audio,
    /// Video file
    Video,
    /// Other file type
    Other,
}

impl Attachment {
    /// Create a new attachment
    pub fn new(
        name: impl Into<String>,
        path: impl Into<String>,
        size: u64,
        mime_type: impl Into<String>,
    ) -> Self {
        let mime_type = mime_type.into();
        let attachment_type = AttachmentType::from_mime_type(&mime_type);
        
        Self {
            id: Uuid::new_v4().to_string(),
            name: name.into(),
            path: path.into(),
            size,
            mime_type,
            attachment_type,
        }
    }
}

impl AttachmentType {
    /// Determine attachment type from MIME type
    pub fn from_mime_type(mime_type: &str) -> Self {
        match mime_type.split('/').next() {
            Some("image") => Self::Image,
            Some("audio") => Self::Audio,
            Some("video") => Self::Video,
            Some("text") => Self::Code,
            Some("application") => {
                if mime_type.contains("pdf") || mime_type.contains("document") {
                    Self::Document
                } else {
                    Self::Other
                }
            }
            _ => Self::Other,
        }
    }
}