@@ -8,10 +8,22 @@ use async_trait::async_trait;
88use sea_orm:: DatabaseConnection ;
99
1010use crate :: error:: { AQBotError , Result } ;
11- use crate :: types:: { RagContextResult , RagRetrievedItem , RagSourceResult } ;
11+ use crate :: types:: { RagContextResult , RagRetrievedItem , RagSourceError , RagSourceResult } ;
1212use crate :: vector_store:: { EmbeddingRecord , VectorSearchResult , VectorStore } ;
1313use crate :: { document_parser, text_chunker} ;
1414
15+ fn format_rag_failure_message ( reason : impl AsRef < str > ) -> String {
16+ const PREFIX : & str = "检索失败" ;
17+ let reason = reason. as_ref ( ) . trim ( ) ;
18+ if reason. is_empty ( ) {
19+ return PREFIX . to_string ( ) ;
20+ }
21+ if reason. starts_with ( PREFIX ) {
22+ return reason. to_string ( ) ;
23+ }
24+ format ! ( "{PREFIX}:{reason}" )
25+ }
26+
1527// ── Trait ────────────────────────────────────────────────────────────────────
1628
1729/// A source of RAG content that can be searched and indexed.
@@ -314,6 +326,7 @@ pub async fn collect_rag_context(
314326
315327 let mut context_parts = Vec :: new ( ) ;
316328 let mut source_results = Vec :: new ( ) ;
329+ let mut errors = Vec :: new ( ) ;
317330
318331 for src_ref in & sources {
319332 let source = src_ref. source ( ) ;
@@ -426,6 +439,16 @@ pub async fn collect_rag_context(
426439 src_ref. container_id,
427440 e
428441 ) ;
442+ let source_type_str = match src_ref. source_type {
443+ RAGSourceType :: Knowledge => "knowledge" ,
444+ RAGSourceType :: Memory => "memory" ,
445+ } ;
446+ errors. push ( RagSourceError {
447+ source_type : source_type_str. to_string ( ) ,
448+ container_id : src_ref. container_id . clone ( ) ,
449+ message : format_rag_failure_message ( format ! ( "重排序失败:{e}" ) ) ,
450+ } ) ;
451+ continue ;
429452 }
430453 }
431454 }
@@ -475,6 +498,15 @@ pub async fn collect_rag_context(
475498 src_ref. container_id,
476499 e
477500 ) ;
501+ let source_type_str = match src_ref. source_type {
502+ RAGSourceType :: Knowledge => "knowledge" ,
503+ RAGSourceType :: Memory => "memory" ,
504+ } ;
505+ errors. push ( RagSourceError {
506+ source_type : source_type_str. to_string ( ) ,
507+ container_id : src_ref. container_id . clone ( ) ,
508+ message : format_rag_failure_message ( format ! ( "向量检索失败:{e}" ) ) ,
509+ } ) ;
478510 }
479511 }
480512 }
@@ -511,6 +543,7 @@ pub async fn collect_rag_context(
511543 RagContextResult {
512544 context_parts,
513545 source_results,
546+ errors,
514547 }
515548}
516549
0 commit comments