sugars_llm 0.2.0

LLM integration and AI agent builder utilities
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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::{AsyncTask, AsyncStream};
use crate::domain::chunk::DocumentChunk;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    pub data: String,
    pub format: Option<ContentFormat>,
    pub media_type: Option<DocumentMediaType>,
    #[serde(flatten)]
    pub additional_props: HashMap<String, Value>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ContentFormat {
    Base64,
    Text,
    Html,
    Markdown,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum DocumentMediaType {
    PDF,
    DOCX,
    TXT,
    RTF,
    ODT,
}

/// Internal enum for builder data sources
enum DocumentBuilderData {
    Immediate(String),
    File(PathBuf),
    Glob(String),
    Url(String),
    GitHub(String),
}

pub struct DocumentBuilder {
    data: DocumentBuilderData,
    format: Option<ContentFormat>,
    media_type: Option<DocumentMediaType>,
    additional_props: HashMap<String, Value>,
    error_handler: Option<Box<dyn FnMut(String) + Send + 'static>>,
}

pub struct DocumentBuilderWithHandler {
    data: DocumentBuilderData,
    format: Option<ContentFormat>,
    media_type: Option<DocumentMediaType>,
    additional_props: HashMap<String, Value>,
    error_handler: Box<dyn FnMut(String) + Send + 'static>,
}

impl Document {
    // Direct semantic entry point - no new()
    pub fn from_data(data: impl Into<String>) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::Immediate(data.into()),
            format: None,
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }

    pub fn from_base64(data: impl Into<String>) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::Immediate(data.into()),
            format: Some(ContentFormat::Base64),
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }

    pub fn from_text(data: impl Into<String>) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::Immediate(data.into()),
            format: Some(ContentFormat::Text),
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }
    
    /// Create a DocumentBuilder from a file path
    pub fn from_file<P: AsRef<Path>>(path: P) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::File(path.as_ref().to_path_buf()),
            format: None,
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }
    
    /// Create a DocumentBuilder from a glob pattern
    pub fn from_glob(pattern: &str) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::Glob(pattern.to_string()),
            format: None,
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }

    /// Create a DocumentBuilder from a URL
    pub fn from_url(url: &str) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::Url(url.to_string()),
            format: None,
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }
    
    /// Create a DocumentBuilder from a GitHub file path
    /// Format: "owner/repo/path/to/file.txt"
    pub fn from_github(path: &str) -> DocumentBuilder {
        DocumentBuilder {
            data: DocumentBuilderData::GitHub(path.to_string()),
            format: None,
            media_type: None,
            additional_props: HashMap::new(),
            error_handler: None,
        }
    }

    /// Extract the text content from the document
    pub fn content(&self) -> String {
        match self.format {
            Some(ContentFormat::Base64) => "[Base64 Document]".to_string(),
            _ => self.data.clone(),
        }
    }
}

impl DocumentBuilder {
    pub fn format(mut self, format: ContentFormat) -> Self {
        self.format = Some(format);
        self
    }

    pub fn media_type(mut self, media_type: DocumentMediaType) -> Self {
        self.media_type = Some(media_type);
        self
    }

    pub fn property(mut self, key: impl Into<String>, value: Value) -> Self {
        self.additional_props.insert(key.into(), value);
        self
    }

    pub fn properties<F>(mut self, f: F) -> Self
    where
        F: FnOnce() -> hashbrown::HashMap<String, Value>
    {
        let props = f();
        for (key, value) in props {
            self.additional_props.insert(key, value);
        }
        self
    }
    
    /// Required: Provide error handler to enable async terminal methods
    pub fn on_error<F>(self, error_handler: F) -> DocumentBuilderWithHandler
    where
        F: FnMut(String) + Send + 'static,
    {
        DocumentBuilderWithHandler {
            data: self.data,
            format: self.format,
            media_type: self.media_type,
            additional_props: self.additional_props,
            error_handler: Box::new(error_handler),
        }
    }

    // Sync terminal method - only for immediate data
    pub fn load(self) -> Document {
        match self.data {
            DocumentBuilderData::Immediate(data) => Document {
                data,
                format: self.format,
                media_type: self.media_type,
                additional_props: self.additional_props,
            },
            _ => panic!("load() can only be used with immediate data. Use on_error() and load_async() for file/url/glob operations."),
        }
    }
}

impl DocumentBuilderWithHandler {
    pub fn format(mut self, format: ContentFormat) -> Self {
        self.format = Some(format);
        self
    }

    pub fn media_type(mut self, media_type: DocumentMediaType) -> Self {
        self.media_type = Some(media_type);
        self
    }

    pub fn property(mut self, key: impl Into<String>, value: Value) -> Self {
        self.additional_props.insert(key.into(), value);
        self
    }

    /// Load document asynchronously
    pub fn load_async(self) -> AsyncTask<Document> {
        match self.data {
            DocumentBuilderData::Immediate(data) => {
                AsyncTask::from_value(Document {
                    data,
                    format: self.format,
                    media_type: self.media_type,
                    additional_props: self.additional_props,
                })
            }
            DocumentBuilderData::File(path) => {
                let format = self.format;
                let media_type = self.media_type;
                let additional_props = self.additional_props;
                let mut error_handler = self.error_handler;
                
                AsyncTask::new(async move {
                    match tokio::fs::read_to_string(&path).await {
                        Ok(data) => Document {
                            data,
                            format,
                            media_type,
                            additional_props,
                        },
                        Err(e) => {
                            error_handler(format!("Failed to read file {:?}: {}", path, e));
                            Document {
                                data: String::new(),
                                format,
                                media_type,
                                additional_props,
                            }
                        }
                    }
                })
            }
            DocumentBuilderData::Url(url) => {
                let format = self.format;
                let media_type = self.media_type;
                let additional_props = self.additional_props;
                let mut error_handler = self.error_handler;
                
                AsyncTask::new(async move {
                    match reqwest::get(&url).await {
                        Ok(resp) => match resp.text().await {
                            Ok(data) => Document {
                                data,
                                format,
                                media_type,
                                additional_props,
                            },
                            Err(e) => {
                                error_handler(format!("Failed to read response from {}: {}", url, e));
                                Document {
                                    data: String::new(),
                                    format,
                                    media_type,
                                    additional_props,
                                }
                            }
                        },
                        Err(e) => {
                            error_handler(format!("Failed to fetch URL {}: {}", url, e));
                            Document {
                                data: String::new(),
                                format,
                                media_type,
                                additional_props,
                            }
                        }
                    }
                })
            }
            DocumentBuilderData::GitHub(path) => {
                let format = self.format;
                let media_type = self.media_type;
                let additional_props = self.additional_props;
                let mut error_handler = self.error_handler;
                
                // Parse owner/repo/path format
                let parts: Vec<&str> = path.split('/').collect();
                if parts.len() < 3 {
                    error_handler(format!("Invalid GitHub path format: {}", path));
                    return AsyncTask::from_value(Document {
                        data: String::new(),
                        format,
                        media_type,
                        additional_props,
                    });
                }
                
                let owner = parts[0];
                let repo = parts[1];
                let file_path = parts[2..].join("/");
                let api_url = format!(
                    "https://api.github.com/repos/{}/{}/contents/{}",
                    owner, repo, file_path
                );
                
                AsyncTask::new(async move {
                    match reqwest::get(&api_url).await {
                        Ok(resp) => match resp.json::<serde_json::Value>().await {
                            Ok(json) => {
                                if let Some(content) = json.get("content").and_then(|c| c.as_str()) {
                                    // GitHub API returns base64 encoded content
                                    match base64::Engine::decode(&base64::engine::general_purpose::STANDARD, content.replace('\n', "")) {
                                        Ok(decoded) => match String::from_utf8(decoded) {
                                            Ok(data) => Document {
                                                data,
                                                format,
                                                media_type,
                                                additional_props,
                                            },
                                            Err(e) => {
                                                error_handler(format!("Invalid UTF-8 in GitHub file: {}", e));
                                                Document {
                                                    data: String::new(),
                                                    format,
                                                    media_type,
                                                    additional_props,
                                                }
                                            }
                                        },
                                        Err(e) => {
                                            error_handler(format!("Failed to decode base64: {}", e));
                                            Document {
                                                data: String::new(),
                                                format,
                                                media_type,
                                                additional_props,
                                            }
                                        }
                                    }
                                } else {
                                    error_handler(format!("No content field in GitHub response"));
                                    Document {
                                        data: String::new(),
                                        format,
                                        media_type,
                                        additional_props,
                                    }
                                }
                            }
                            Err(e) => {
                                error_handler(format!("Failed to parse GitHub response: {}", e));
                                Document {
                                    data: String::new(),
                                    format,
                                    media_type,
                                    additional_props,
                                }
                            }
                        },
                        Err(e) => {
                            error_handler(format!("Failed to fetch from GitHub: {}", e));
                            Document {
                                data: String::new(),
                                format,
                                media_type,
                                additional_props,
                            }
                        }
                    }
                })
            }
            DocumentBuilderData::Glob(pattern) => {
                let format = self.format;
                let media_type = self.media_type;
                let additional_props = self.additional_props;
                let mut error_handler = self.error_handler;
                
                // For glob, we can't return a single Document, this should use stream()
                error_handler(format!("Use stream() for glob patterns, not load_async()"));
                AsyncTask::from_value(Document {
                    data: String::new(),
                    format,
                    media_type,
                    additional_props,
                })
            }
        }
    }
    
    /// Stream documents matching a glob pattern or load a single document as a stream
    pub fn stream(self) -> AsyncStream<Document> {
        match self.data {
            DocumentBuilderData::Glob(pattern) => {
                let format = self.format.clone();
                let media_type = self.media_type.clone();
                let additional_props = self.additional_props.clone();
                let mut error_handler = self.error_handler;
                
                let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
                
                tokio::spawn(async move {
                    match glob::glob(&pattern) {
                        Ok(paths) => {
                            for path_result in paths {
                                match path_result {
                                    Ok(path) => {
                                        match tokio::fs::read_to_string(&path).await {
                                            Ok(data) => {
                                                let doc = Document {
                                                    data,
                                                    format,
                                                    media_type,
                                                    additional_props: additional_props.clone(),
                                                };
                                                if tx.send(doc).is_err() {
                                                    break;
                                                }
                                            }
                                            Err(e) => {
                                                error_handler(format!("Failed to read {:?}: {}", path, e));
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        error_handler(format!("Glob error: {}", e));
                                    }
                                }
                            }
                        }
                        Err(e) => {
                            error_handler(format!("Invalid glob pattern: {}", e));
                        }
                    }
                });
                
                AsyncStream::new(rx)
            }
            _ => {
                // For non-glob sources, create a single-item stream
                let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
                
                tokio::spawn(async move {
                    if let Ok(doc) = self.load_async().await {
                        let _ = tx.send(doc);
                    }
                });
                
                AsyncStream::new(rx)
            }
        }
    }
    
    /// Stream document content in chunks
    pub fn stream_chunks(self, chunk_size: usize) -> AsyncStream<DocumentChunk> {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        
        tokio::spawn(async move {
            // First load the document
            let doc = match self.load_async().await {
                Ok(doc) => doc,
                Err(_) => return,
            };
            
            // Then chunk it
            let content = &doc.data;
            let mut offset = 0;
            
            while offset < content.len() {
                let end = (offset + chunk_size).min(content.len());
                let chunk = DocumentChunk::new(&content[offset..end])
                    .with_range(offset, end);
                
                if tx.send(chunk).is_err() {
                    break;
                }
                
                offset = end;
            }
        });
        
        AsyncStream::new(rx)
    }
    
    /// Stream document content line by line
    pub fn stream_lines(self) -> AsyncStream<DocumentChunk> {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        
        tokio::spawn(async move {
            // First load the document
            let doc = match self.load_async().await {
                Ok(doc) => doc,
                Err(_) => return,
            };
            
            // Then split by lines
            let mut offset = 0;
            for line in doc.data.lines() {
                let chunk = DocumentChunk::new(line)
                    .with_range(offset, offset + line.len());
                
                if tx.send(chunk).is_err() {
                    break;
                }
                
                offset += line.len() + 1; // +1 for newline
            }
        });
        
        AsyncStream::new(rx)
    }
}