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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! Streaming response generation for ROGRAG system
//!
//! Provides robust response generation with streaming capabilities
//! to improve user experience and system resilience.
#[cfg(feature = "rograg")]
use crate::rograg::{
IntentResult, ProcessingStats, RogragResponse, SubqueryResult, SubqueryResultType,
};
#[cfg(feature = "rograg")]
use crate::Result;
#[cfg(feature = "rograg")]
use itertools::Itertools;
#[cfg(feature = "rograg")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "rograg")]
use std::collections::HashMap;
#[cfg(feature = "rograg")]
use strum::{Display as StrumDisplay, EnumString};
#[cfg(feature = "rograg")]
use thiserror::Error;
/// Error types for streaming response generation
#[cfg(feature = "rograg")]
#[derive(Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum StreamingError {
/// Response generation failed during template or content creation.
#[error("Failed to generate response: {reason}")]
GenerationFailed {
/// Description of what failed during generation.
reason: String,
},
/// Not enough subquery results to synthesize a response.
#[error("Insufficient subquery results: got {got}, needed {needed}")]
InsufficientResults {
/// Number of results available.
got: usize,
/// Minimum number of results required.
needed: usize,
},
/// Synthesis operation failed to combine subquery results.
#[error("Response synthesis failed: {reason}")]
SynthesisFailed {
/// Description of the synthesis failure.
reason: String,
},
/// Generic streaming error occurred.
#[error("Streaming error: {message}")]
StreamingError {
/// Error message describing the streaming issue.
message: String,
},
}
/// Configuration for streaming response builder
#[cfg(feature = "rograg")]
#[derive(Debug, Clone)]
pub struct StreamingConfig {
/// Whether to enable streaming mode for responses
pub enable_streaming: bool,
/// Size of each response chunk in words when streaming
pub chunk_size: usize,
/// Maximum length of the generated response in characters
pub max_response_length: usize,
/// Strategy to use for synthesizing multiple subquery results
pub synthesis_strategy: SynthesisStrategy,
/// Whether to weight results by confidence scores
pub confidence_weighting: bool,
/// Whether to include source attribution in responses
pub source_attribution: bool,
/// Whether to add citation markers to sources
pub enable_citations: bool,
}
#[cfg(feature = "rograg")]
impl Default for StreamingConfig {
fn default() -> Self {
Self {
enable_streaming: true,
chunk_size: 256,
max_response_length: 2048,
synthesis_strategy: SynthesisStrategy::Weighted,
confidence_weighting: true,
source_attribution: true,
enable_citations: true,
}
}
}
/// Strategy for synthesizing multiple subquery results
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, StrumDisplay, EnumString, Serialize, Deserialize)]
pub enum SynthesisStrategy {
/// Concatenate results in order
Sequential,
/// Weight results by confidence
Weighted,
/// Select best result only
BestOnly,
/// Merge results intelligently
SmartMerge,
/// Hierarchical combination
Hierarchical,
}
/// Streaming response chunk
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseChunk {
/// Sequential identifier for this chunk
pub chunk_id: usize,
/// Text content of this chunk
pub content: String,
/// Whether this is the final chunk in the stream
pub is_final: bool,
/// Confidence score for the information in this chunk
pub confidence: f32,
/// Source documents referenced in this chunk
pub sources: Vec<String>,
/// Additional metadata for this chunk
pub metadata: HashMap<String, String>,
}
/// Template for response generation
#[cfg(feature = "rograg")]
#[derive(Debug, Clone)]
pub struct ResponseTemplate {
/// Type of response this template is designed for
pub template_type: TemplateType,
/// Template pattern with placeholder markers like {entity}
pub pattern: String,
/// List of placeholder names that can be substituted
pub placeholders: Vec<String>,
/// Minimum confidence required to use this template
pub confidence_threshold: f32,
}
/// Type of response template for different query intents
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, StrumDisplay, EnumString, PartialEq, Eq, Hash)]
pub enum TemplateType {
/// Template for factual information queries
Factual,
/// Template for definition queries
Definitional,
/// Template for relationship queries between entities
Relational,
/// Template for comparison queries
Comparative,
/// Template for summary or overview queries
Summary,
/// Template for cause-and-effect queries
Causal,
/// Template for time-based queries
Temporal,
/// Generic fallback template when no specific type matches
Fallback,
}
/// Streaming response builder implementation
#[cfg(feature = "rograg")]
pub struct StreamingResponseBuilder {
/// Configuration controlling streaming behavior
config: StreamingConfig,
/// Template registry organized by template type
templates: HashMap<TemplateType, Vec<ResponseTemplate>>,
/// Engine for synthesizing multiple subquery results
synthesis_engine: SynthesisEngine,
}
#[cfg(feature = "rograg")]
impl Default for StreamingResponseBuilder {
fn default() -> Self {
Self::new()
}
}
impl StreamingResponseBuilder {
/// Create a new streaming response builder
pub fn new() -> Self {
Self::with_config(StreamingConfig::default())
}
/// Create a new streaming response builder with custom configuration
pub fn with_config(config: StreamingConfig) -> Self {
let mut builder = Self {
config,
templates: HashMap::new(),
synthesis_engine: SynthesisEngine::new(),
};
builder.initialize_templates();
builder
}
/// Initialize response templates
fn initialize_templates(&mut self) {
// Factual templates
self.add_template(
TemplateType::Factual,
ResponseTemplate {
template_type: TemplateType::Factual,
pattern: "Based on the available information, {content}. {confidence_indicator}"
.to_string(),
placeholders: vec!["content".to_string(), "confidence_indicator".to_string()],
confidence_threshold: 0.7,
},
);
// Definitional templates
self.add_template(
TemplateType::Definitional,
ResponseTemplate {
template_type: TemplateType::Definitional,
pattern: "{entity} is {definition}. {additional_context}".to_string(),
placeholders: vec![
"entity".to_string(),
"definition".to_string(),
"additional_context".to_string(),
],
confidence_threshold: 0.6,
},
);
// Relational templates
self.add_template(
TemplateType::Relational,
ResponseTemplate {
template_type: TemplateType::Relational,
pattern: "{entity1} and {entity2} are related through {relationship}. {details}"
.to_string(),
placeholders: vec![
"entity1".to_string(),
"entity2".to_string(),
"relationship".to_string(),
"details".to_string(),
],
confidence_threshold: 0.5,
},
);
// Comparative templates
self.add_template(
TemplateType::Comparative,
ResponseTemplate {
template_type: TemplateType::Comparative,
pattern: "Comparing {entity1} and {entity2}: {comparison}. {conclusion}"
.to_string(),
placeholders: vec![
"entity1".to_string(),
"entity2".to_string(),
"comparison".to_string(),
"conclusion".to_string(),
],
confidence_threshold: 0.6,
},
);
// Summary templates
self.add_template(
TemplateType::Summary,
ResponseTemplate {
template_type: TemplateType::Summary,
pattern: "Here's what I found about {topic}: {summary}. {key_points}".to_string(),
placeholders: vec![
"topic".to_string(),
"summary".to_string(),
"key_points".to_string(),
],
confidence_threshold: 0.5,
},
);
// Fallback template
self.add_template(
TemplateType::Fallback,
ResponseTemplate {
template_type: TemplateType::Fallback,
pattern: "Based on the available information: {content}".to_string(),
placeholders: vec!["content".to_string()],
confidence_threshold: 0.3,
},
);
}
/// Add a response template
fn add_template(&mut self, template_type: TemplateType, template: ResponseTemplate) {
self.templates
.entry(template_type)
.or_default()
.push(template);
}
/// Build a streaming response from subquery results
///
/// Synthesizes multiple subquery results into a coherent response using the configured
/// synthesis strategy and appropriate response templates.
///
/// # Arguments
///
/// * `query` - The original user query
/// * `subquery_results` - Results from decomposed subqueries
/// * `intent_result` - Classified intent of the query
///
/// # Returns
///
/// A complete ROGRAG response with synthesized content, confidence scores, and sources
pub async fn build_streaming_response(
&self,
query: String,
subquery_results: Vec<SubqueryResult>,
intent_result: IntentResult,
) -> Result<RogragResponse> {
let start_time = std::time::Instant::now();
// Synthesize subquery results
let synthesis_result = self
.synthesis_engine
.synthesize(&subquery_results, &self.config.synthesis_strategy)?;
// Select appropriate template
let template_type = self.determine_template_type(&intent_result, &subquery_results);
let template = self.select_template(&template_type, synthesis_result.confidence)?;
// Generate response content
let content = self.generate_content(template, &synthesis_result, &subquery_results)?;
// Add source attribution if enabled
let final_content = if self.config.source_attribution {
self.add_source_attribution(content, &synthesis_result.sources)
} else {
content
};
// Calculate final confidence
let confidence = self.calculate_final_confidence(&synthesis_result, &intent_result);
// Extract sources
let sources = self.extract_all_sources(&subquery_results);
let processing_time = start_time.elapsed();
Ok(RogragResponse {
query,
content: final_content,
confidence,
sources,
subquery_results: subquery_results.clone(),
intent_result,
processing_stats: ProcessingStats {
total_time_ms: processing_time.as_millis() as u64,
decomposition_time_ms: 0, // Set by caller
retrieval_time_ms: 0, // Set by caller
synthesis_time_ms: processing_time.as_millis() as u64,
intent_classification_time_ms: 0,
validation_time_ms: 0,
subqueries_processed: subquery_results.len(),
fallback_used: subquery_results
.iter()
.filter(|r| matches!(r.result_type, SubqueryResultType::Fallback))
.count()
> 0,
},
is_streaming: self.config.enable_streaming,
is_refusal: false,
})
}
/// Build a complete (non-streaming) response
///
/// Generates a full response without streaming, using the same synthesis logic
/// as streaming responses but delivering the complete result at once.
///
/// # Arguments
///
/// * `query` - The original user query
/// * `subquery_results` - Results from decomposed subqueries
/// * `intent_result` - Classified intent of the query
///
/// # Returns
///
/// A complete ROGRAG response marked as non-streaming
pub async fn build_complete_response(
&self,
query: String,
subquery_results: Vec<SubqueryResult>,
intent_result: IntentResult,
) -> Result<RogragResponse> {
// For now, use the same logic as streaming but mark as non-streaming
let mut response = self
.build_streaming_response(query, subquery_results, intent_result)
.await?;
response.is_streaming = false;
Ok(response)
}
/// Determine the appropriate template type based on query intent
///
/// Maps the classified query intent to the most suitable response template type,
/// with fallback logic based on subquery result patterns.
fn determine_template_type(
&self,
intent_result: &IntentResult,
subquery_results: &[SubqueryResult],
) -> TemplateType {
use crate::rograg::QueryIntent;
// First, check intent classification
match intent_result.primary_intent {
QueryIntent::Factual => TemplateType::Factual,
QueryIntent::Definitional => TemplateType::Definitional,
QueryIntent::Relational => TemplateType::Relational,
QueryIntent::Comparative => TemplateType::Comparative,
QueryIntent::Summary | QueryIntent::Exploratory => TemplateType::Summary,
QueryIntent::Causal => TemplateType::Causal,
QueryIntent::Temporal => TemplateType::Temporal,
_ => {
// Fallback: determine from subquery results
if subquery_results.len() > 1 {
TemplateType::Summary
} else {
TemplateType::Fallback
}
},
}
}
/// Select the best template for the given type and confidence level
///
/// Finds a template matching the type with an appropriate confidence threshold.
/// Falls back to generic templates if no exact match is found.
fn select_template(
&self,
template_type: &TemplateType,
confidence: f32,
) -> Result<&ResponseTemplate> {
let templates = self
.templates
.get(template_type)
.or_else(|| self.templates.get(&TemplateType::Fallback))
.ok_or_else(|| StreamingError::GenerationFailed {
reason: "No suitable template found".to_string(),
})?;
// Find template with appropriate confidence threshold
templates
.iter()
.find(|t| confidence >= t.confidence_threshold)
.or_else(|| templates.last()) // Fallback to last template
.ok_or_else(|| crate::GraphRAGError::TextProcessing {
message: "No template matches confidence level".to_string(),
})
}
/// Generate content using template and synthesis result
///
/// Replaces template placeholders with actual content from synthesis results
/// and subquery data, then cleans up the final output.
fn generate_content(
&self,
template: &ResponseTemplate,
synthesis_result: &SynthesisResult,
subquery_results: &[SubqueryResult],
) -> Result<String> {
let mut content = template.pattern.clone();
// Replace placeholders
for placeholder in &template.placeholders {
let replacement = match placeholder.as_str() {
"content" => synthesis_result.content.clone(),
"confidence_indicator" => {
self.generate_confidence_indicator(synthesis_result.confidence)
},
"entity" => self.extract_primary_entity(subquery_results),
"entity1" => self.extract_entity_by_index(subquery_results, 0),
"entity2" => self.extract_entity_by_index(subquery_results, 1),
"definition" => synthesis_result.content.clone(),
"relationship" => self.extract_relationship(subquery_results),
"comparison" => synthesis_result.content.clone(),
"topic" => self.extract_primary_entity(subquery_results),
"summary" => synthesis_result.content.clone(),
"additional_context" => self.generate_additional_context(subquery_results),
"details" => self.generate_details(subquery_results),
"conclusion" => self.generate_conclusion(synthesis_result),
"key_points" => self.generate_key_points(subquery_results),
_ => format!("[{placeholder}]"), // Placeholder not found
};
content = content.replace(&format!("{{{placeholder}}}"), &replacement);
}
// Clean up the content
content = self.clean_content(content);
Ok(content)
}
/// Generate confidence indicator text based on confidence score
///
/// Returns human-readable text describing the confidence level of the response.
fn generate_confidence_indicator(&self, confidence: f32) -> String {
if confidence >= 0.9 {
"I'm very confident in this information.".to_string()
} else if confidence >= 0.7 {
"This information appears to be reliable.".to_string()
} else if confidence >= 0.5 {
"This information has moderate confidence.".to_string()
} else {
"Please note that this information has limited confidence.".to_string()
}
}
/// Extract primary entity from results
///
/// Attempts to identify and extract the main entity being discussed
/// from the first subquery result.
fn extract_primary_entity(&self, results: &[SubqueryResult]) -> String {
results
.first()
.map(|r| {
// Try to extract entity name from content
let words: Vec<&str> = r.content.split_whitespace().collect();
words.first().unwrap_or(&"the subject").to_string()
})
.unwrap_or_else(|| "the subject".to_string())
}
/// Extract entity by index from subquery results
///
/// Retrieves the entity at the specified index position, useful for
/// comparative or relational queries with multiple entities.
fn extract_entity_by_index(&self, results: &[SubqueryResult], index: usize) -> String {
results
.get(index)
.map(|r| {
let words: Vec<&str> = r.content.split_whitespace().collect();
words.first().unwrap_or(&"entity").to_string()
})
.unwrap_or_else(|| format!("entity{}", index + 1))
}
/// Extract relationship information from subquery results
///
/// Searches for and extracts relationship descriptions between entities
/// from the subquery results.
fn extract_relationship(&self, results: &[SubqueryResult]) -> String {
results
.iter()
.find(|r| r.content.contains("related") || r.content.contains("relationship"))
.map(|r| r.content.clone())
.unwrap_or_else(|| "a connection".to_string())
}
/// Generate additional context from secondary subquery results
///
/// Combines content from results beyond the first to provide supporting context.
fn generate_additional_context(&self, results: &[SubqueryResult]) -> String {
if results.len() > 1 {
let additional: Vec<String> =
results.iter().skip(1).map(|r| r.content.clone()).collect();
if !additional.is_empty() {
format!("Additionally, {}", additional.join(". "))
} else {
String::new()
}
} else {
String::new()
}
}
/// Generate details from high-confidence results
///
/// Filters and combines content from subquery results with confidence above 0.6
/// to provide detailed supporting information.
fn generate_details(&self, results: &[SubqueryResult]) -> String {
let details: Vec<String> = results
.iter()
.filter(|r| r.confidence > 0.6)
.map(|r| r.content.clone())
.collect();
if details.len() > 1 {
details.join(". ")
} else {
String::new()
}
}
/// Generate conclusion based on synthesis confidence
///
/// Creates a closing statement that reflects the confidence level
/// of the synthesized information.
fn generate_conclusion(&self, synthesis_result: &SynthesisResult) -> String {
if synthesis_result.confidence > 0.8 {
"This appears to be well-supported by the available information.".to_string()
} else if synthesis_result.confidence > 0.6 {
"This conclusion is supported by the available evidence.".to_string()
} else {
"This is based on limited information.".to_string()
}
}
/// Generate key points summary from subquery results
///
/// Creates a numbered list of the top 3 key points from subquery results.
fn generate_key_points(&self, results: &[SubqueryResult]) -> String {
let points: Vec<String> = results
.iter()
.take(3) // Limit to top 3 points
.enumerate()
.map(|(i, r)| format!("{}. {}", i + 1, r.content))
.collect();
if points.is_empty() {
String::new()
} else {
format!("Key points: {}", points.join("; "))
}
}
/// Clean up content by removing placeholders and normalizing whitespace
///
/// Removes unfilled placeholders, normalizes whitespace, and ensures
/// proper sentence punctuation.
fn clean_content(&self, mut content: String) -> String {
// Remove empty placeholder brackets
content = regex::Regex::new(r"\{\w+\}")
.expect("static regex literal")
.replace_all(&content, "")
.to_string();
// Clean up extra spaces
content = regex::Regex::new(r"\s+")
.expect("static regex literal")
.replace_all(&content, " ")
.to_string();
// Remove trailing punctuation followed by spaces
content = content.trim().to_string();
// Ensure proper sentence ending
if !content.is_empty() && !content.ends_with(['.', '!', '?']) {
content.push('.');
}
content
}
/// Add source attribution to content
///
/// Appends a formatted list of source citations to the response content
/// if citation mode is enabled, limiting to the top 3 sources.
fn add_source_attribution(&self, mut content: String, sources: &[String]) -> String {
if self.config.enable_citations && !sources.is_empty() {
let source_list = sources
.iter()
.take(3) // Limit to 3 sources
.enumerate()
.map(|(i, source)| format!("[{}] {}", i + 1, source))
.join(", ");
content = format!("{content}\n\nSources: {source_list}");
}
content
}
/// Calculate final confidence score
///
/// Combines synthesis confidence with intent classification confidence
/// using weighted averaging when confidence weighting is enabled.
fn calculate_final_confidence(
&self,
synthesis_result: &SynthesisResult,
intent_result: &IntentResult,
) -> f32 {
if self.config.confidence_weighting {
// Weight by both synthesis and intent confidence
(synthesis_result.confidence * 0.7 + intent_result.confidence * 0.3).min(1.0)
} else {
synthesis_result.confidence
}
}
/// Extract all unique sources from subquery results
///
/// Collects and deduplicates all source references from the subquery results.
fn extract_all_sources(&self, results: &[SubqueryResult]) -> Vec<String> {
results
.iter()
.flat_map(|r| r.sources.iter())
.cloned()
.unique()
.collect()
}
/// Generate streaming chunks from a complete response
///
/// Splits a full response into smaller chunks for progressive streaming delivery.
/// Each chunk is marked with its position and whether it's the final chunk.
///
/// # Arguments
///
/// * `response` - The complete response to split into chunks
///
/// # Returns
///
/// A vector of response chunks suitable for streaming
pub async fn generate_streaming_chunks(
&self,
response: &RogragResponse,
) -> Result<Vec<ResponseChunk>> {
let content = &response.content;
let chunk_size = self.config.chunk_size;
let mut chunks = Vec::new();
// Split content into chunks
let words: Vec<&str> = content.split_whitespace().collect();
let total_words = words.len();
for (chunk_idx, chunk_words) in words.chunks(chunk_size).enumerate() {
let chunk_content = chunk_words.join(" ");
let is_final = (chunk_idx + 1) * chunk_size >= total_words;
chunks.push(ResponseChunk {
chunk_id: chunk_idx,
content: chunk_content,
is_final,
confidence: response.confidence,
sources: response.sources.clone(),
metadata: HashMap::new(),
});
}
Ok(chunks)
}
/// Get the current streaming configuration
///
/// Returns a reference to the active configuration settings.
pub fn get_config(&self) -> &StreamingConfig {
&self.config
}
/// Update the streaming configuration
///
/// Replaces the current configuration with new settings.
pub fn update_config(&mut self, config: StreamingConfig) {
self.config = config;
}
}
/// Synthesis engine for combining subquery results
///
/// Provides multiple strategies for merging and synthesizing information
/// from multiple subquery results into coherent responses.
#[cfg(feature = "rograg")]
pub struct SynthesisEngine {
// Configuration could be added here
}
/// Result from synthesis operation
#[cfg(feature = "rograg")]
#[derive(Debug, Clone)]
pub struct SynthesisResult {
/// The synthesized content text
pub content: String,
/// Confidence score of the synthesis (0.0 to 1.0)
pub confidence: f32,
/// Unique sources used in the synthesis
pub sources: Vec<String>,
/// The synthesis strategy that was applied
pub synthesis_method: SynthesisStrategy,
}
#[cfg(feature = "rograg")]
impl Default for SynthesisEngine {
fn default() -> Self {
Self::new()
}
}
impl SynthesisEngine {
/// Create a new synthesis engine
pub fn new() -> Self {
Self {}
}
/// Synthesize multiple subquery results using the specified strategy
///
/// Combines information from multiple subquery results into a single coherent
/// response using one of several synthesis strategies.
///
/// # Arguments
///
/// * `results` - The subquery results to synthesize
/// * `strategy` - The synthesis strategy to apply
///
/// # Returns
///
/// A synthesis result containing the combined content and metadata
///
/// # Errors
///
/// Returns error if results are empty or synthesis fails
pub fn synthesize(
&self,
results: &[SubqueryResult],
strategy: &SynthesisStrategy,
) -> Result<SynthesisResult> {
if results.is_empty() {
return Err(StreamingError::InsufficientResults { got: 0, needed: 1 }.into());
}
match strategy {
SynthesisStrategy::Sequential => self.synthesize_sequential(results),
SynthesisStrategy::Weighted => self.synthesize_weighted(results),
SynthesisStrategy::BestOnly => self.synthesize_best_only(results),
SynthesisStrategy::SmartMerge => self.synthesize_smart_merge(results),
SynthesisStrategy::Hierarchical => self.synthesize_hierarchical(results),
}
}
/// Sequential synthesis strategy
///
/// Concatenates results in order with equal weighting, computing average confidence.
fn synthesize_sequential(&self, results: &[SubqueryResult]) -> Result<SynthesisResult> {
let content = results.iter().map(|r| r.content.clone()).join(". ");
let avg_confidence =
results.iter().map(|r| r.confidence).sum::<f32>() / results.len() as f32;
let sources = results
.iter()
.flat_map(|r| r.sources.iter())
.cloned()
.unique()
.collect();
Ok(SynthesisResult {
content,
confidence: avg_confidence,
sources,
synthesis_method: SynthesisStrategy::Sequential,
})
}
/// Weighted synthesis strategy
///
/// Prioritizes results by confidence score, taking the top 3 highest-confidence
/// results and computing weighted confidence.
fn synthesize_weighted(&self, results: &[SubqueryResult]) -> Result<SynthesisResult> {
let total_weight: f32 = results.iter().map(|r| r.confidence).sum();
if total_weight == 0.0 {
return self.synthesize_sequential(results);
}
// Sort by confidence and combine
let mut sorted_results = results.to_vec();
sorted_results.sort_by(|a, b| {
b.confidence
.partial_cmp(&a.confidence)
.unwrap_or(std::cmp::Ordering::Equal)
});
let content = sorted_results
.iter()
.take(3) // Take top 3 results
.map(|r| r.content.clone())
.join(". ");
let weighted_confidence = sorted_results
.iter()
.map(|r| r.confidence * r.confidence) // Square for weighting
.sum::<f32>()
/ sorted_results.len() as f32;
let sources = results
.iter()
.flat_map(|r| r.sources.iter())
.cloned()
.unique()
.collect();
Ok(SynthesisResult {
content,
confidence: weighted_confidence.sqrt(), // Take square root to normalize
sources,
synthesis_method: SynthesisStrategy::Weighted,
})
}
/// Best only synthesis strategy
///
/// Selects only the single result with the highest confidence score,
/// discarding all other results.
fn synthesize_best_only(&self, results: &[SubqueryResult]) -> Result<SynthesisResult> {
let best_result = results
.iter()
.max_by(|a, b| {
a.confidence
.partial_cmp(&b.confidence)
.unwrap_or(std::cmp::Ordering::Equal)
})
.ok_or_else(|| StreamingError::SynthesisFailed {
reason: "No best result found".to_string(),
})?;
Ok(SynthesisResult {
content: best_result.content.clone(),
confidence: best_result.confidence,
sources: best_result.sources.clone(),
synthesis_method: SynthesisStrategy::BestOnly,
})
}
/// Smart merge synthesis strategy
///
/// Intelligently combines results by grouping by result type (logic form vs fuzzy match)
/// and preferring logic form results when available.
fn synthesize_smart_merge(&self, results: &[SubqueryResult]) -> Result<SynthesisResult> {
// Group by result type and merge intelligently
let mut logic_results = Vec::new();
let mut fuzzy_results = Vec::new();
for result in results {
match result.result_type {
SubqueryResultType::LogicForm => logic_results.push(result),
SubqueryResultType::FuzzyMatch => fuzzy_results.push(result),
SubqueryResultType::Fallback => fuzzy_results.push(result),
}
}
// Prefer logic form results
let primary_results = if !logic_results.is_empty() {
logic_results
} else {
fuzzy_results
};
if primary_results.is_empty() {
return self.synthesize_sequential(results);
}
// Combine the best results
let content = primary_results
.iter()
.take(2) // Take top 2
.map(|r| r.content.clone())
.join(". ");
let confidence = primary_results.iter().map(|r| r.confidence).sum::<f32>()
/ primary_results.len() as f32;
let sources = results
.iter()
.flat_map(|r| r.sources.iter())
.cloned()
.unique()
.collect();
Ok(SynthesisResult {
content,
confidence,
sources,
synthesis_method: SynthesisStrategy::SmartMerge,
})
}
/// Hierarchical synthesis strategy
///
/// Structures results by importance with the highest confidence result as primary
/// and supporting results as secondary context.
fn synthesize_hierarchical(&self, results: &[SubqueryResult]) -> Result<SynthesisResult> {
// Sort by confidence and create hierarchical structure
let mut sorted_results = results.to_vec();
sorted_results.sort_by(|a, b| {
b.confidence
.partial_cmp(&a.confidence)
.unwrap_or(std::cmp::Ordering::Equal)
});
let mut content_parts = Vec::new();
if let Some(primary) = sorted_results.first() {
content_parts.push(primary.content.clone());
}
if sorted_results.len() > 1 {
let supporting: Vec<String> = sorted_results
.iter()
.skip(1)
.take(2)
.map(|r| r.content.clone())
.collect();
if !supporting.is_empty() {
content_parts.push(format!("Additionally: {}", supporting.join("; ")));
}
}
let content = content_parts.join(". ");
let confidence = sorted_results.first().map(|r| r.confidence).unwrap_or(0.0);
let sources = results
.iter()
.flat_map(|r| r.sources.iter())
.cloned()
.unique()
.collect();
Ok(SynthesisResult {
content,
confidence,
sources,
synthesis_method: SynthesisStrategy::Hierarchical,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rograg::{IntentResult, QueryIntent};
/// Create test subquery results for unit testing
#[cfg(feature = "rograg")]
fn create_test_subquery_results() -> Vec<SubqueryResult> {
vec![
SubqueryResult {
subquery: "What is Entity Name?".to_string(),
result_type: SubqueryResultType::LogicForm,
confidence: 0.9,
content: "Entity Name is a young boy character".to_string(),
sources: vec!["source1".to_string()],
},
SubqueryResult {
subquery: "Who is Second Entity?".to_string(),
result_type: SubqueryResultType::FuzzyMatch,
confidence: 0.8,
content: "Second Entity is Tom's friend".to_string(),
sources: vec!["source2".to_string()],
},
]
}
/// Create test intent result for unit testing
#[cfg(feature = "rograg")]
fn create_test_intent_result() -> IntentResult {
IntentResult {
primary_intent: QueryIntent::Factual,
secondary_intents: vec![],
confidence: 0.8,
should_refuse: false,
refusal_reason: None,
suggested_reformulation: None,
complexity_score: 0.3,
}
}
/// Test weighted synthesis strategy
#[cfg(feature = "rograg")]
#[test]
fn test_synthesis_weighted() {
let engine = SynthesisEngine::new();
let results = create_test_subquery_results();
let synthesis = engine
.synthesize(&results, &SynthesisStrategy::Weighted)
.unwrap();
assert!(!synthesis.content.is_empty());
assert!(synthesis.confidence > 0.0);
assert_eq!(synthesis.sources.len(), 2);
}
/// Test best-only synthesis strategy
#[cfg(feature = "rograg")]
#[test]
fn test_synthesis_best_only() {
let engine = SynthesisEngine::new();
let results = create_test_subquery_results();
let synthesis = engine
.synthesize(&results, &SynthesisStrategy::BestOnly)
.unwrap();
assert_eq!(synthesis.content, "Entity Name is a young boy character");
assert_eq!(synthesis.confidence, 0.9);
}
/// Test complete response building pipeline
#[cfg(feature = "rograg")]
#[tokio::test]
async fn test_response_building() {
let builder = StreamingResponseBuilder::new();
let results = create_test_subquery_results();
let intent = create_test_intent_result();
let response = builder
.build_streaming_response("What is Entity Name?".to_string(), results, intent)
.await
.unwrap();
assert!(!response.content.is_empty());
assert!(response.confidence > 0.0);
assert!(!response.sources.is_empty());
assert!(response.is_streaming);
}
/// Test template selection based on type and confidence
/// Test generation of streaming chunks from a complete response
#[cfg(feature = "rograg")]
#[tokio::test]
async fn test_streaming_chunks() {
let builder = StreamingResponseBuilder::new();
let results = create_test_subquery_results();
let intent = create_test_intent_result();
let response = builder
.build_streaming_response("Test query".to_string(), results, intent)
.await
.unwrap();
let chunks = builder.generate_streaming_chunks(&response).await.unwrap();
assert!(!chunks.is_empty());
assert!(chunks.last().unwrap().is_final);
}
}