langchainrust 0.2.10

A LangChain-inspired framework for building LLM applications in Rust. Supports OpenAI, Agents, Tools, Memory, Chains, RAG, BM25, Hybrid Retrieval, LangGraph, and native Function Calling.
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// src/vector_stores/document_store.rs
//! 文档存储模块
//!
//! 单独存储文档内容,供 BM25 和向量检索共用。
//! 支持原始文档和分割后的 chunk 文档。

use super::{Document, VectorStoreError};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;

// ============================================================================
// Chunk 文档结构
// ============================================================================

/// Chunk 文档(分割后的文档片段)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkDocument {
    /// Chunk ID
    pub chunk_id: String,
    
    /// 原始文档 ID(Parent ID)
    pub parent_id: String,
    
    /// Chunk 内容
    pub content: String,
    
    /// Chunk 序号(第几个 chunk)
    pub segment: usize,
    
    /// Chunk 元数据
    pub metadata: HashMap<String, String>,
}

impl ChunkDocument {
    /// 创建新的 Chunk 文档
    pub fn new(
        chunk_id: String,
        parent_id: String,
        content: String,
        segment: usize,
    ) -> Self {
        Self {
            chunk_id,
            parent_id,
            content,
            segment,
            metadata: HashMap::new(),
        }
    }
    
    /// 添加元数据
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
    
    /// 转换为 Document
    pub fn to_document(&self) -> Document {
        Document {
            content: self.content.clone(),
            metadata: self.metadata.clone(),
            id: Some(self.chunk_id.clone()),
        }
    }
}

// ============================================================================
// DocumentStore Trait
// ============================================================================

/// 文档存储 trait
#[async_trait]
pub trait DocumentStore: Send + Sync {
    /// 添加文档
    async fn add_document(&self, document: Document) -> Result<String, VectorStoreError>;
    
    /// 批量添加文档
    async fn add_documents(&self, documents: Vec<Document>) -> Result<Vec<String>, VectorStoreError>;
    
    /// 获取文档
    async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError>;
    
    /// 删除文档
    async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError>;
    
    /// 获取文档数量
    async fn count(&self) -> usize;
    
    /// 清空存储
    async fn clear(&self) -> Result<(), VectorStoreError>;
}

// ============================================================================
// ChunkedDocumentStore Trait(抽象接口,支持多种存储后端)
// ============================================================================

/// 支持 Parent-Child 结构的文档存储 trait
///
/// 此 trait 定义了回表存储的统一接口,支持:
/// - MongoDB(生产环境)
/// - InMemory(开发/测试)
/// - Redis(缓存层,预留)
/// - SQLite(本地存储,预留)
#[async_trait]
pub trait ChunkedDocumentStoreTrait: Send + Sync {
    /// 添加 Parent 文档并自动分割为 chunks
    ///
    /// # 参数
    /// - `document`: 原始文档
    /// - `chunk_size`: 每个 chunk 的字符大小
    ///
    /// # 返回
    /// - `(parent_id, chunk_ids)`: Parent ID 和生成的 Chunk ID 列表
    async fn add_parent_document(
        &self,
        document: Document,
        chunk_size: usize,
    ) -> Result<(String, Vec<String>), VectorStoreError>;
    
    /// 批量添加 Parent 文档
    async fn add_parent_documents(
        &self,
        documents: Vec<Document>,
        chunk_size: usize,
    ) -> Result<Vec<(String, Vec<String>)>, VectorStoreError>;
    
    /// 获取 Parent 文档
    async fn get_parent_document(&self, parent_id: &str) -> Result<Option<Document>, VectorStoreError>;
    
    /// 获取单个 Chunk
    async fn get_chunk(&self, chunk_id: &str) -> Result<Option<ChunkDocument>, VectorStoreError>;
    
    /// 获取单个 Chunk(转为 Document)
    async fn get_chunk_document(&self, chunk_id: &str) -> Result<Option<Document>, VectorStoreError>;
    
    /// 获取 Parent 的所有 Chunks
    async fn get_chunks_for_parent(&self, parent_id: &str) -> Result<Vec<ChunkDocument>, VectorStoreError>;
    
    /// 获取 Parent 的所有 Chunks(转为 Document)
    async fn get_chunk_documents_for_parent(&self, parent_id: &str) -> Result<Vec<Document>, VectorStoreError>;
    
    /// 删除 Parent 文档及其所有 Chunks
    async fn delete_parent_document(&self, parent_id: &str) -> Result<(), VectorStoreError>;
    
    /// 获取 Parent 文档数量
    async fn parent_count(&self) -> usize;
    
    /// 获取 Chunk 文档数量
    async fn chunk_count(&self) -> usize;
    
    /// 获取所有 Chunks
    async fn get_all_chunks(&self) -> Result<Vec<ChunkDocument>, VectorStoreError>;
    
    /// 清空所有数据
    async fn clear(&self) -> Result<(), VectorStoreError>;
    
    /// 持久化存储(可选实现)
    async fn save(&self, path: impl AsRef<Path> + Send) -> Result<(), VectorStoreError> {
        Err(VectorStoreError::StorageError("save not implemented for this store".to_string()))
    }
    
    /// 从持久化加载(可选实现)
    async fn load(path: impl AsRef<Path> + Send) -> Result<Self, VectorStoreError> where Self: Sized {
        Err(VectorStoreError::StorageError("load not implemented for this store".to_string()))
    }
}

// ============================================================================
// InMemoryDocumentStore
// ============================================================================

/// 内存文档存储
pub struct InMemoryDocumentStore {
    /// 文档集合
    documents: Arc<RwLock<HashMap<String, Document>>>,
}

impl InMemoryDocumentStore {
    /// 创建新的内存文档存储
    pub fn new() -> Self {
        Self {
            documents: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl Default for InMemoryDocumentStore {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl DocumentStore for InMemoryDocumentStore {
    async fn add_document(&self, document: Document) -> Result<String, VectorStoreError> {
        let id = document.id.clone().unwrap_or_else(|| Uuid::new_v4().to_string());
        
        let mut store = self.documents.write().await;
        store.insert(id.clone(), document);
        
        Ok(id)
    }
    
    async fn add_documents(&self, documents: Vec<Document>) -> Result<Vec<String>, VectorStoreError> {
        let mut store = self.documents.write().await;
        let mut ids = Vec::new();
        
        for doc in documents {
            let id = doc.id.clone().unwrap_or_else(|| Uuid::new_v4().to_string());
            store.insert(id.clone(), doc);
            ids.push(id);
        }
        
        Ok(ids)
    }
    
    async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError> {
        let store = self.documents.read().await;
        Ok(store.get(id).cloned())
    }
    
    async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError> {
        let mut store = self.documents.write().await;
        store.remove(id);
        Ok(())
    }
    
    async fn count(&self) -> usize {
        let store = self.documents.read().await;
        store.len()
    }
    
    async fn clear(&self) -> Result<(), VectorStoreError> {
        let mut store = self.documents.write().await;
        store.clear();
        Ok(())
    }
}

// ============================================================================
// InMemoryChunkedDocumentStore(内存实现)
// ============================================================================

/// 内存存储实现(开发/测试用)
pub struct InMemoryChunkedDocumentStore {
    parent_docs: Arc<RwLock<HashMap<String, Document>>>,
    chunks: Arc<RwLock<HashMap<String, ChunkDocument>>>,
    parent_to_chunks: Arc<RwLock<HashMap<String, Vec<String>>>>,
}

impl InMemoryChunkedDocumentStore {
    pub fn new() -> Self {
        Self {
            parent_docs: Arc::new(RwLock::new(HashMap::new())),
            chunks: Arc::new(RwLock::new(HashMap::new())),
            parent_to_chunks: Arc::new(RwLock::new(HashMap::new())),
        }
    }
    
    pub fn add_parent_document_blocking(
        &self,
        document: Document,
        chunk_size: usize,
    ) -> Result<(String, Vec<String>), VectorStoreError> {
        let parent_id = document.id.clone().unwrap_or_else(|| Uuid::new_v4().to_string());
        
        {
            let mut parents = self.parent_docs.blocking_write();
            parents.insert(parent_id.clone(), document.clone());
        }
        
        let chunk_ids = self.split_and_store_chunks_blocking(&parent_id, &document.content, chunk_size)?;
        
        Ok((parent_id, chunk_ids))
    }
    
    pub fn get_parent_document_blocking(&self, parent_id: &str) -> Result<Option<Document>, VectorStoreError> {
        let parents = self.parent_docs.blocking_read();
        Ok(parents.get(parent_id).cloned())
    }
    
    pub fn get_chunk_blocking(&self, chunk_id: &str) -> Result<Option<ChunkDocument>, VectorStoreError> {
        let chunks = self.chunks.blocking_read();
        Ok(chunks.get(chunk_id).cloned())
    }
    
    pub fn get_chunk_document_blocking(&self, chunk_id: &str) -> Result<Option<Document>, VectorStoreError> {
        let chunks = self.chunks.blocking_read();
        Ok(chunks.get(chunk_id).map(|c| c.to_document()))
    }
    
    pub fn blocking_get_chunks_for_parent(&self, parent_id: &str) -> Result<Vec<ChunkDocument>, VectorStoreError> {
        let mapping = self.parent_to_chunks.blocking_read();
        let chunks = self.chunks.blocking_read();
        
        let chunk_ids = mapping.get(parent_id).cloned().unwrap_or_default();
        
        let result = chunk_ids
            .iter()
            .filter_map(|id| chunks.get(id).cloned())
            .collect();
        
        Ok(result)
    }
    
    fn split_and_store_chunks_blocking(
        &self,
        parent_id: &str,
        content: &str,
        chunk_size: usize,
    ) -> Result<Vec<String>, VectorStoreError> {
        let chars: Vec<char> = content.chars().collect();
        let total_len = chars.len();
        
        let mut chunk_ids = Vec::new();
        let mut segment = 0;
        let mut start = 0;
        
        while start < total_len {
            let end = std::cmp::min(start + chunk_size, total_len);
            let chunk_content: String = chars[start..end].iter().collect();
            
            let chunk_id = format!("{}_{}", parent_id, segment);
            
            let chunk = ChunkDocument::new(
                chunk_id.clone(),
                parent_id.to_string(),
                chunk_content,
                segment,
            );
            
            {
                let mut chunks = self.chunks.blocking_write();
                chunks.insert(chunk_id.clone(), chunk);
            }
            
            {
                let mut mapping = self.parent_to_chunks.blocking_write();
                mapping
                    .entry(parent_id.to_string())
                    .or_insert_with(Vec::new)
                    .push(chunk_id.clone());
            }
            
            chunk_ids.push(chunk_id);
            segment += 1;
            start = end;
        }
        
        Ok(chunk_ids)
    }
    
    async fn split_and_store_chunks_async(
        &self,
        parent_id: &str,
        content: &str,
        chunk_size: usize,
    ) -> Result<Vec<String>, VectorStoreError> {
        let chars: Vec<char> = content.chars().collect();
        let total_len = chars.len();
        
        let mut chunk_ids = Vec::new();
        let mut segment = 0;
        let mut start = 0;
        
        while start < total_len {
            let end = std::cmp::min(start + chunk_size, total_len);
            let chunk_content: String = chars[start..end].iter().collect();
            
            let chunk_id = format!("{}_{}", parent_id, segment);
            
            let chunk = ChunkDocument::new(
                chunk_id.clone(),
                parent_id.to_string(),
                chunk_content,
                segment,
            );
            
            {
                let mut chunks = self.chunks.write().await;
                chunks.insert(chunk_id.clone(), chunk);
            }
            
            {
                let mut mapping = self.parent_to_chunks.write().await;
                mapping
                    .entry(parent_id.to_string())
                    .or_insert_with(Vec::new)
                    .push(chunk_id.clone());
            }
            
            chunk_ids.push(chunk_id);
            segment += 1;
            start = end;
        }
        
        Ok(chunk_ids)
    }
}

impl Default for InMemoryChunkedDocumentStore {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl ChunkedDocumentStoreTrait for InMemoryChunkedDocumentStore {
    async fn add_parent_document(
        &self,
        document: Document,
        chunk_size: usize,
    ) -> Result<(String, Vec<String>), VectorStoreError> {
        let parent_id = document.id.clone().unwrap_or_else(|| Uuid::new_v4().to_string());
        
        {
            let mut parents = self.parent_docs.write().await;
            parents.insert(parent_id.clone(), document.clone());
        }
        
        let chunk_ids = self.split_and_store_chunks_async(&parent_id, &document.content, chunk_size).await?;
        
        Ok((parent_id, chunk_ids))
    }
    
    async fn add_parent_documents(
        &self,
        documents: Vec<Document>,
        chunk_size: usize,
    ) -> Result<Vec<(String, Vec<String>)>, VectorStoreError> {
        let mut results = Vec::new();
        for doc in documents {
            let result = self.add_parent_document(doc, chunk_size).await?;
            results.push(result);
        }
        Ok(results)
    }
    
    async fn get_parent_document(&self, parent_id: &str) -> Result<Option<Document>, VectorStoreError> {
        let parents = self.parent_docs.read().await;
        Ok(parents.get(parent_id).cloned())
    }
    
    async fn get_chunk(&self, chunk_id: &str) -> Result<Option<ChunkDocument>, VectorStoreError> {
        let chunks = self.chunks.read().await;
        Ok(chunks.get(chunk_id).cloned())
    }
    
    async fn get_chunk_document(&self, chunk_id: &str) -> Result<Option<Document>, VectorStoreError> {
        let chunks = self.chunks.read().await;
        Ok(chunks.get(chunk_id).map(|c| c.to_document()))
    }
    
    async fn get_chunks_for_parent(&self, parent_id: &str) -> Result<Vec<ChunkDocument>, VectorStoreError> {
        let mapping = self.parent_to_chunks.read().await;
        let chunks = self.chunks.read().await;
        
        let chunk_ids = mapping.get(parent_id).cloned().unwrap_or_default();
        
        let result = chunk_ids
            .iter()
            .filter_map(|id| chunks.get(id).cloned())
            .collect();
        
        Ok(result)
    }
    
    async fn get_chunk_documents_for_parent(&self, parent_id: &str) -> Result<Vec<Document>, VectorStoreError> {
        let chunks = self.get_chunks_for_parent(parent_id).await?;
        Ok(chunks.iter().map(|c| c.to_document()).collect())
    }
    
    async fn delete_parent_document(&self, parent_id: &str) -> Result<(), VectorStoreError> {
        let chunk_ids = {
            let mapping = self.parent_to_chunks.read().await;
            mapping.get(parent_id).cloned().unwrap_or_default()
        };
        
        {
            let mut chunks = self.chunks.write().await;
            for chunk_id in &chunk_ids {
                chunks.remove(chunk_id);
            }
        }
        
        {
            let mut mapping = self.parent_to_chunks.write().await;
            mapping.remove(parent_id);
        }
        
        {
            let mut parents = self.parent_docs.write().await;
            parents.remove(parent_id);
        }
        
        Ok(())
    }
    
    async fn parent_count(&self) -> usize {
        let parents = self.parent_docs.read().await;
        parents.len()
    }
    
    async fn chunk_count(&self) -> usize {
        let chunks = self.chunks.read().await;
        chunks.len()
    }
    
    async fn get_all_chunks(&self) -> Result<Vec<ChunkDocument>, VectorStoreError> {
        let chunks = self.chunks.read().await;
        Ok(chunks.values().cloned().collect())
    }
    
    async fn clear(&self) -> Result<(), VectorStoreError> {
        let mut parents = self.parent_docs.write().await;
        let mut chunks = self.chunks.write().await;
        let mut mapping = self.parent_to_chunks.write().await;
        
        parents.clear();
        chunks.clear();
        mapping.clear();
        
        Ok(())
    }
    
    async fn save(&self, path: impl AsRef<Path> + Send) -> Result<(), VectorStoreError> {
        let parents = self.parent_docs.read().await;
        let chunks = self.chunks.read().await;
        let mapping = self.parent_to_chunks.read().await;
        
        let data = ChunkedStoreData {
            parent_docs: parents.clone(),
            chunks: chunks.clone(),
            parent_to_chunks: mapping.clone(),
        };
        
        let encoded = bincode::serialize(&data)
            .map_err(|e| VectorStoreError::StorageError(e.to_string()))?;
        
        std::fs::write(path.as_ref(), encoded)
            .map_err(|e| VectorStoreError::StorageError(e.to_string()))?;
        
        Ok(())
    }
    
    async fn load(path: impl AsRef<Path> + Send) -> Result<Self, VectorStoreError> {
        let bytes = std::fs::read(path.as_ref())
            .map_err(|e| VectorStoreError::StorageError(e.to_string()))?;
        
        let data: ChunkedStoreData = bincode::deserialize(&bytes)
            .map_err(|e| VectorStoreError::StorageError(e.to_string()))?;
        
        Ok(Self {
            parent_docs: Arc::new(RwLock::new(data.parent_docs)),
            chunks: Arc::new(RwLock::new(data.chunks)),
            parent_to_chunks: Arc::new(RwLock::new(data.parent_to_chunks)),
        })
    }
}

#[async_trait]
impl DocumentStore for InMemoryChunkedDocumentStore {
    async fn add_document(&self, document: Document) -> Result<String, VectorStoreError> {
        let id = document.id.clone().unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
        
        let mut chunks = self.chunks.write().await;
        
        let chunk = ChunkDocument::new(
            id.clone(),
            id.clone(),
            document.content.clone(),
            0,
        );
        
        chunks.insert(id.clone(), chunk);
        
        Ok(id)
    }
    
    async fn add_documents(&self, documents: Vec<Document>) -> Result<Vec<String>, VectorStoreError> {
        let mut ids = Vec::new();
        for doc in documents {
            let id = self.add_document(doc).await?;
            ids.push(id);
        }
        Ok(ids)
    }
    
    async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError> {
        self.get_chunk_document(id).await
    }
    
    async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError> {
        let mut chunks = self.chunks.write().await;
        chunks.remove(id);
        Ok(())
    }
    
    async fn count(&self) -> usize {
        self.chunk_count().await
    }
    
    async fn clear(&self) -> Result<(), VectorStoreError> {
        ChunkedDocumentStoreTrait::clear(self).await
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChunkedStoreData {
    parent_docs: HashMap<String, Document>,
    chunks: HashMap<String, ChunkDocument>,
    parent_to_chunks: HashMap<String, Vec<String>>,
}

pub type ChunkedDocumentStore = InMemoryChunkedDocumentStore;

// ============================================================================
// 测试
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_in_memory_document_store() {
        let store = InMemoryDocumentStore::new();
        
        // 添加文档
        let doc = Document::new("测试内容").with_id("doc_001");
        let id = store.add_document(doc).await.unwrap();
        
        assert_eq!(id, "doc_001");
        assert_eq!(store.count().await, 1);
        
        // 获取文档
        let retrieved = store.get_document("doc_001").await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().content, "测试内容");
        
        // 删除文档
        store.delete_document("doc_001").await.unwrap();
        assert_eq!(store.count().await, 0);
    }
    
    #[tokio::test]
    async fn test_chunked_document_store() {
        let store = ChunkedDocumentStore::new();
        
        // 添加 Parent 文档(chunk_size=20)
        let doc = Document::new("这是一段很长的测试文本,用于验证文档分割功能。").with_id("parent_001");
        
        let (parent_id, chunk_ids) = store.add_parent_document(doc, 20).await.unwrap();
        
        assert_eq!(parent_id, "parent_001");
        assert!(chunk_ids.len() > 1);  // 应该分割成多个 chunk
        
        // 获取 Parent 文档
        let parent = store.get_parent_document("parent_001").await.unwrap();
        assert!(parent.is_some());
        
        // 获取所有 Chunk
        let chunks = store.get_chunks_for_parent("parent_001").await.unwrap();
        assert_eq!(chunks.len(), chunk_ids.len());
        
        // 获取单个 Chunk
        let chunk = store.get_chunk(&chunk_ids[0]).await.unwrap();
        assert!(chunk.is_some());
        assert_eq!(chunk.unwrap().parent_id, "parent_001");
        
        // 删除 Parent 及所有 Chunk
        store.delete_parent_document("parent_001").await.unwrap();
        assert_eq!(store.parent_count().await, 0);
        assert_eq!(store.chunk_count().await, 0);
    }
    
    #[tokio::test]
    async fn test_chunk_to_document() {
        let chunk = ChunkDocument::new(
            "chunk_001".to_string(),
            "parent_001".to_string(),
            "Chunk内容".to_string(),
            0,
        ).with_metadata("source", "test");
        
        let doc = chunk.to_document();
        
        assert_eq!(doc.id, Some("chunk_001".to_string()));
        assert_eq!(doc.content, "Chunk内容");
        assert_eq!(doc.metadata.get("source"), Some(&"test".to_string()));
    }
    
    #[tokio::test]
    async fn test_persistence() {
        let store = ChunkedDocumentStore::new();
        
        // 添加文档
        let doc = Document::new("测试持久化功能的内容").with_id("parent_001");
        store.add_parent_document(doc, 10).await.unwrap();
        
        // 保存
        let temp_path = tempfile::NamedTempFile::new().unwrap();
        store.save(temp_path.path()).await.unwrap();
        
        // 加载
        let loaded = ChunkedDocumentStore::load(temp_path.path()).await.unwrap();
        
        assert_eq!(loaded.parent_count().await, store.parent_count().await);
        assert_eq!(loaded.chunk_count().await, store.chunk_count().await);
        
        let parent = loaded.get_parent_document("parent_001").await.unwrap();
        assert!(parent.is_some());
    }
}