praisonai 0.2.0

Core library for PraisonAI - Agent, Tools, Workflows
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
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
//! Specialized agents for PraisonAI
//!
//! This module provides specialized agent types matching the Python SDK:
//! - `PromptExpanderAgent`: Expands short prompts into detailed, comprehensive prompts
//! - `QueryRewriterAgent`: Transforms queries to improve retrieval quality in RAG
//!
//! # Example
//!
//! ```rust,ignore
//! use praisonai::specialized_agents::{PromptExpanderAgent, ExpandStrategy};
//!
//! let expander = PromptExpanderAgent::new()
//!     .model("gpt-4o-mini")
//!     .build();
//!
//! let result = expander.expand("Write a blog post", ExpandStrategy::Detailed, None).await?;
//! println!("Expanded: {}", result.expanded_prompt);
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// =============================================================================
// PROMPT EXPANDER AGENT
// =============================================================================

/// Expansion strategy for prompts
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExpandStrategy {
    /// Basic expansion with minimal additions
    Basic,
    /// Detailed expansion with comprehensive context
    Detailed,
    /// Structured expansion with clear sections
    Structured,
    /// Creative expansion with imaginative elements
    Creative,
    /// Auto-detect best strategy based on prompt
    #[default]
    Auto,
}

impl std::fmt::Display for ExpandStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExpandStrategy::Basic => write!(f, "basic"),
            ExpandStrategy::Detailed => write!(f, "detailed"),
            ExpandStrategy::Structured => write!(f, "structured"),
            ExpandStrategy::Creative => write!(f, "creative"),
            ExpandStrategy::Auto => write!(f, "auto"),
        }
    }
}

impl ExpandStrategy {
    /// Get all available strategies
    pub fn all() -> Vec<ExpandStrategy> {
        vec![
            ExpandStrategy::Basic,
            ExpandStrategy::Detailed,
            ExpandStrategy::Structured,
            ExpandStrategy::Creative,
            ExpandStrategy::Auto,
        ]
    }

    /// Parse from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "basic" => Some(ExpandStrategy::Basic),
            "detailed" => Some(ExpandStrategy::Detailed),
            "structured" => Some(ExpandStrategy::Structured),
            "creative" => Some(ExpandStrategy::Creative),
            "auto" => Some(ExpandStrategy::Auto),
            _ => None,
        }
    }
}

/// Result of prompt expansion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpandResult {
    /// Original prompt
    pub original_prompt: String,
    /// Expanded prompt
    pub expanded_prompt: String,
    /// Strategy used for expansion
    pub strategy_used: ExpandStrategy,
    /// Additional metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl ExpandResult {
    /// Create a new expand result
    pub fn new(
        original: impl Into<String>,
        expanded: impl Into<String>,
        strategy: ExpandStrategy,
    ) -> Self {
        Self {
            original_prompt: original.into(),
            expanded_prompt: expanded.into(),
            strategy_used: strategy,
            metadata: HashMap::new(),
        }
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Get expansion ratio (expanded length / original length)
    pub fn expansion_ratio(&self) -> f64 {
        if self.original_prompt.is_empty() {
            0.0
        } else {
            self.expanded_prompt.len() as f64 / self.original_prompt.len() as f64
        }
    }
}

/// Prompt expansion prompts for each strategy
pub struct ExpandPrompts;

impl ExpandPrompts {
    /// Basic expansion prompt
    pub const BASIC: &'static str = r#"Expand the following prompt with additional context and clarity while maintaining the original intent. Add relevant details that would help an AI assistant understand and respond better.

Original prompt: {prompt}

Expanded prompt:"#;

    /// Detailed expansion prompt
    pub const DETAILED: &'static str = r#"Transform the following prompt into a comprehensive, detailed request. Include:
- Clear objectives and expected outcomes
- Relevant context and background information
- Specific requirements or constraints
- Quality criteria for the response
- Any relevant examples or references

Original prompt: {prompt}

Detailed expanded prompt:"#;

    /// Structured expansion prompt
    pub const STRUCTURED: &'static str = r#"Restructure the following prompt into a well-organized format with clear sections:

1. **Objective**: What needs to be accomplished
2. **Context**: Background information
3. **Requirements**: Specific needs and constraints
4. **Output Format**: Expected format of the response
5. **Success Criteria**: How to evaluate the response

Original prompt: {prompt}

Structured expanded prompt:"#;

    /// Creative expansion prompt
    pub const CREATIVE: &'static str = r#"Expand the following prompt with creative and imaginative elements while maintaining the core intent. Add engaging context, vivid descriptions, and innovative angles that could inspire a more creative response.

Original prompt: {prompt}

Creative expanded prompt:"#;

    /// Auto-detection prompt
    pub const AUTO: &'static str = r#"Analyze the following prompt and expand it using the most appropriate strategy. Consider:
- The type of task (creative, technical, analytical, etc.)
- The level of detail needed
- The expected output format

First, briefly identify the best expansion approach, then provide the expanded prompt.

Original prompt: {prompt}

Analysis and expanded prompt:"#;

    /// Get prompt for strategy
    pub fn get(strategy: ExpandStrategy) -> &'static str {
        match strategy {
            ExpandStrategy::Basic => Self::BASIC,
            ExpandStrategy::Detailed => Self::DETAILED,
            ExpandStrategy::Structured => Self::STRUCTURED,
            ExpandStrategy::Creative => Self::CREATIVE,
            ExpandStrategy::Auto => Self::AUTO,
        }
    }
}

/// Configuration for PromptExpanderAgent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptExpanderConfig {
    /// Agent name
    pub name: String,
    /// LLM model to use
    pub model: String,
    /// Custom instructions
    pub instructions: Option<String>,
    /// Verbose output
    pub verbose: bool,
    /// Temperature for LLM
    pub temperature: f32,
    /// Max tokens for response
    pub max_tokens: usize,
}

impl Default for PromptExpanderConfig {
    fn default() -> Self {
        Self {
            name: "PromptExpanderAgent".to_string(),
            model: "gpt-4o-mini".to_string(),
            instructions: None,
            verbose: false,
            temperature: 0.7,
            max_tokens: 1000,
        }
    }
}

/// Builder for PromptExpanderAgent
#[derive(Debug, Clone, Default)]
pub struct PromptExpanderAgentBuilder {
    config: PromptExpanderConfig,
}

impl PromptExpanderAgentBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set agent name
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.config.name = name.into();
        self
    }

    /// Set LLM model
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.config.model = model.into();
        self
    }

    /// Set custom instructions
    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.config.instructions = Some(instructions.into());
        self
    }

    /// Enable verbose output
    pub fn verbose(mut self) -> Self {
        self.config.verbose = true;
        self
    }

    /// Set temperature
    pub fn temperature(mut self, temp: f32) -> Self {
        self.config.temperature = temp;
        self
    }

    /// Set max tokens
    pub fn max_tokens(mut self, tokens: usize) -> Self {
        self.config.max_tokens = tokens;
        self
    }

    /// Build the agent
    pub fn build(self) -> PromptExpanderAgent {
        PromptExpanderAgent {
            config: self.config,
        }
    }
}

/// Agent for expanding prompts
#[derive(Debug, Clone)]
pub struct PromptExpanderAgent {
    config: PromptExpanderConfig,
}

impl PromptExpanderAgent {
    /// Create a new builder
    pub fn new() -> PromptExpanderAgentBuilder {
        PromptExpanderAgentBuilder::new()
    }

    /// Get agent name
    pub fn name(&self) -> &str {
        &self.config.name
    }

    /// Get model
    pub fn model(&self) -> &str {
        &self.config.model
    }

    /// Detect the best expansion strategy for a prompt
    pub fn detect_strategy(&self, prompt: &str) -> ExpandStrategy {
        let prompt_lower = prompt.to_lowercase();
        let word_count = prompt.split_whitespace().count();

        // Creative indicators
        if prompt_lower.contains("creative")
            || prompt_lower.contains("story")
            || prompt_lower.contains("imagine")
            || prompt_lower.contains("write a poem")
            || prompt_lower.contains("fiction")
        {
            return ExpandStrategy::Creative;
        }

        // Structured indicators
        if prompt_lower.contains("analyze")
            || prompt_lower.contains("compare")
            || prompt_lower.contains("evaluate")
            || prompt_lower.contains("report")
            || prompt_lower.contains("document")
        {
            return ExpandStrategy::Structured;
        }

        // Detailed indicators
        if prompt_lower.contains("explain")
            || prompt_lower.contains("describe in detail")
            || prompt_lower.contains("comprehensive")
            || prompt_lower.contains("thorough")
        {
            return ExpandStrategy::Detailed;
        }

        // Short prompts benefit from detailed expansion
        if word_count < 10 {
            return ExpandStrategy::Detailed;
        }

        // Default to basic for longer, clear prompts
        ExpandStrategy::Basic
    }

    /// Expand a prompt (sync placeholder - actual implementation would use LLM)
    pub fn expand_sync(
        &self,
        prompt: &str,
        strategy: ExpandStrategy,
        context: Option<&str>,
    ) -> ExpandResult {
        let actual_strategy = if strategy == ExpandStrategy::Auto {
            self.detect_strategy(prompt)
        } else {
            strategy
        };

        // Build the expansion prompt
        let expansion_prompt = ExpandPrompts::get(actual_strategy).replace("{prompt}", prompt);

        // Add context if provided
        let full_prompt = if let Some(ctx) = context {
            format!("{}\n\nAdditional context: {}", expansion_prompt, ctx)
        } else {
            expansion_prompt
        };

        // Placeholder: In real implementation, this would call the LLM
        // For now, return a simple expansion
        let expanded = format!(
            "## Expanded Prompt\n\n{}\n\n### Original Intent\n{}\n\n### Additional Context\nThis prompt has been expanded using the {} strategy.",
            prompt,
            prompt,
            actual_strategy
        );

        ExpandResult::new(prompt, expanded, actual_strategy)
            .with_metadata("expansion_prompt", full_prompt)
            .with_metadata("model", self.config.model.clone())
    }
}

impl Default for PromptExpanderAgent {
    fn default() -> Self {
        Self::new().build()
    }
}

// =============================================================================
// QUERY REWRITER AGENT
// =============================================================================

/// Rewriting strategy for queries
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RewriteStrategy {
    /// Basic query rewriting
    Basic,
    /// HyDE (Hypothetical Document Embeddings)
    Hyde,
    /// Step-back prompting
    StepBack,
    /// Break into sub-queries
    SubQueries,
    /// Generate multiple query variations
    MultiQuery,
    /// Context-aware rewriting
    Contextual,
    /// Auto-detect best strategy
    #[default]
    Auto,
}

impl std::fmt::Display for RewriteStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RewriteStrategy::Basic => write!(f, "basic"),
            RewriteStrategy::Hyde => write!(f, "hyde"),
            RewriteStrategy::StepBack => write!(f, "step_back"),
            RewriteStrategy::SubQueries => write!(f, "sub_queries"),
            RewriteStrategy::MultiQuery => write!(f, "multi_query"),
            RewriteStrategy::Contextual => write!(f, "contextual"),
            RewriteStrategy::Auto => write!(f, "auto"),
        }
    }
}

impl RewriteStrategy {
    /// Get all available strategies
    pub fn all() -> Vec<RewriteStrategy> {
        vec![
            RewriteStrategy::Basic,
            RewriteStrategy::Hyde,
            RewriteStrategy::StepBack,
            RewriteStrategy::SubQueries,
            RewriteStrategy::MultiQuery,
            RewriteStrategy::Contextual,
            RewriteStrategy::Auto,
        ]
    }

    /// Parse from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "basic" => Some(RewriteStrategy::Basic),
            "hyde" => Some(RewriteStrategy::Hyde),
            "step_back" | "stepback" => Some(RewriteStrategy::StepBack),
            "sub_queries" | "subqueries" => Some(RewriteStrategy::SubQueries),
            "multi_query" | "multiquery" => Some(RewriteStrategy::MultiQuery),
            "contextual" => Some(RewriteStrategy::Contextual),
            "auto" => Some(RewriteStrategy::Auto),
            _ => None,
        }
    }
}

/// Result of query rewriting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RewriteResult {
    /// Original query
    pub original_query: String,
    /// Rewritten queries
    pub rewritten_queries: Vec<String>,
    /// Strategy used
    pub strategy_used: RewriteStrategy,
    /// Hypothetical document (for HyDE strategy)
    pub hypothetical_document: Option<String>,
    /// Step-back question (for step-back strategy)
    pub step_back_question: Option<String>,
    /// Sub-queries (for sub-queries strategy)
    pub sub_queries: Option<Vec<String>>,
    /// Additional metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl RewriteResult {
    /// Create a new rewrite result
    pub fn new(
        original: impl Into<String>,
        rewritten: Vec<String>,
        strategy: RewriteStrategy,
    ) -> Self {
        Self {
            original_query: original.into(),
            rewritten_queries: rewritten,
            strategy_used: strategy,
            hypothetical_document: None,
            step_back_question: None,
            sub_queries: None,
            metadata: HashMap::new(),
        }
    }

    /// Set hypothetical document
    pub fn with_hypothetical_document(mut self, doc: impl Into<String>) -> Self {
        self.hypothetical_document = Some(doc.into());
        self
    }

    /// Set step-back question
    pub fn with_step_back_question(mut self, question: impl Into<String>) -> Self {
        self.step_back_question = Some(question.into());
        self
    }

    /// Set sub-queries
    pub fn with_sub_queries(mut self, queries: Vec<String>) -> Self {
        self.sub_queries = Some(queries);
        self
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Get the primary rewritten query
    pub fn primary_query(&self) -> Option<&str> {
        self.rewritten_queries.first().map(|s| s.as_str())
    }

    /// Get all queries for retrieval
    pub fn all_queries(&self) -> Vec<&str> {
        let mut queries: Vec<&str> = self.rewritten_queries.iter().map(|s| s.as_str()).collect();
        if let Some(sub) = &self.sub_queries {
            queries.extend(sub.iter().map(|s| s.as_str()));
        }
        queries
    }
}

/// Query rewriting prompts for each strategy
pub struct RewritePrompts;

impl RewritePrompts {
    /// Basic rewriting prompt
    pub const BASIC: &'static str = r#"Rewrite the following query to be clearer and more specific for information retrieval. Maintain the original intent while improving clarity.

Original query: {query}

Rewritten query:"#;

    /// HyDE prompt
    pub const HYDE: &'static str = r#"Given the following query, write a hypothetical document that would perfectly answer this query. This document will be used to find similar real documents.

Query: {query}

Hypothetical document that answers this query:"#;

    /// Step-back prompt
    pub const STEP_BACK: &'static str = r#"Given the following specific query, generate a more general "step-back" question that would provide broader context helpful for answering the original query.

Original query: {query}

Step-back question (more general):"#;

    /// Sub-queries prompt
    pub const SUB_QUERIES: &'static str = r#"Break down the following complex query into simpler sub-queries that together would help answer the original question. Generate 2-4 sub-queries.

Original query: {query}

Sub-queries (one per line):
1."#;

    /// Multi-query prompt
    pub const MULTI_QUERY: &'static str = r#"Generate {num_queries} different variations of the following query. Each variation should approach the question from a different angle while maintaining the same intent.

Original query: {query}

Query variations:
1."#;

    /// Contextual prompt
    pub const CONTEXTUAL: &'static str = r#"Given the chat history and the current query, rewrite the query to be self-contained and clear without requiring the chat history for context.

Chat history:
{chat_history}

Current query: {query}

Self-contained rewritten query:"#;

    /// Auto-detection prompt
    pub const AUTO: &'static str = r#"Analyze the following query and determine the best rewriting strategy, then apply it.

Query: {query}

First identify if this query would benefit from:
- Basic rewriting (simple clarification)
- HyDE (generating a hypothetical answer document)
- Step-back (asking a broader question first)
- Sub-queries (breaking into smaller questions)
- Multi-query (generating variations)

Then provide the rewritten query/queries."#;

    /// Get prompt for strategy
    pub fn get(strategy: RewriteStrategy) -> &'static str {
        match strategy {
            RewriteStrategy::Basic => Self::BASIC,
            RewriteStrategy::Hyde => Self::HYDE,
            RewriteStrategy::StepBack => Self::STEP_BACK,
            RewriteStrategy::SubQueries => Self::SUB_QUERIES,
            RewriteStrategy::MultiQuery => Self::MULTI_QUERY,
            RewriteStrategy::Contextual => Self::CONTEXTUAL,
            RewriteStrategy::Auto => Self::AUTO,
        }
    }
}

/// Configuration for QueryRewriterAgent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryRewriterConfig {
    /// Agent name
    pub name: String,
    /// LLM model to use
    pub model: String,
    /// Custom instructions
    pub instructions: Option<String>,
    /// Verbose output
    pub verbose: bool,
    /// Max queries to generate
    pub max_queries: usize,
    /// Abbreviations to expand
    pub abbreviations: HashMap<String, String>,
    /// Temperature for LLM
    pub temperature: f32,
    /// Max tokens for response
    pub max_tokens: usize,
}

impl Default for QueryRewriterConfig {
    fn default() -> Self {
        Self {
            name: "QueryRewriterAgent".to_string(),
            model: "gpt-4o-mini".to_string(),
            instructions: None,
            verbose: false,
            max_queries: 5,
            abbreviations: HashMap::new(),
            temperature: 0.3,
            max_tokens: 500,
        }
    }
}

/// Builder for QueryRewriterAgent
#[derive(Debug, Clone, Default)]
pub struct QueryRewriterAgentBuilder {
    config: QueryRewriterConfig,
}

impl QueryRewriterAgentBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set agent name
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.config.name = name.into();
        self
    }

    /// Set LLM model
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.config.model = model.into();
        self
    }

    /// Set custom instructions
    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.config.instructions = Some(instructions.into());
        self
    }

    /// Enable verbose output
    pub fn verbose(mut self) -> Self {
        self.config.verbose = true;
        self
    }

    /// Set max queries
    pub fn max_queries(mut self, max: usize) -> Self {
        self.config.max_queries = max;
        self
    }

    /// Add abbreviation expansion
    pub fn abbreviation(mut self, abbrev: impl Into<String>, expansion: impl Into<String>) -> Self {
        self.config.abbreviations.insert(abbrev.into(), expansion.into());
        self
    }

    /// Set abbreviations map
    pub fn abbreviations(mut self, abbrevs: HashMap<String, String>) -> Self {
        self.config.abbreviations = abbrevs;
        self
    }

    /// Set temperature
    pub fn temperature(mut self, temp: f32) -> Self {
        self.config.temperature = temp;
        self
    }

    /// Set max tokens
    pub fn max_tokens(mut self, tokens: usize) -> Self {
        self.config.max_tokens = tokens;
        self
    }

    /// Build the agent
    pub fn build(self) -> QueryRewriterAgent {
        QueryRewriterAgent {
            config: self.config,
        }
    }
}

/// Agent for rewriting queries
#[derive(Debug, Clone)]
pub struct QueryRewriterAgent {
    config: QueryRewriterConfig,
}

impl QueryRewriterAgent {
    /// Create a new builder
    pub fn new() -> QueryRewriterAgentBuilder {
        QueryRewriterAgentBuilder::new()
    }

    /// Get agent name
    pub fn name(&self) -> &str {
        &self.config.name
    }

    /// Get model
    pub fn model(&self) -> &str {
        &self.config.model
    }

    /// Expand abbreviations in query
    pub fn expand_abbreviations(&self, query: &str) -> String {
        let mut result = query.to_string();
        for (abbrev, expansion) in &self.config.abbreviations {
            result = result.replace(abbrev, expansion);
        }
        result
    }

    /// Detect the best rewriting strategy for a query
    pub fn detect_strategy(&self, query: &str, has_chat_history: bool) -> RewriteStrategy {
        let query_lower = query.to_lowercase();
        let word_count = query.split_whitespace().count();

        // If we have chat history, use contextual
        if has_chat_history {
            return RewriteStrategy::Contextual;
        }

        // Complex queries benefit from sub-queries
        if query_lower.contains(" and ")
            || query_lower.contains(" or ")
            || word_count > 20
            || query.contains("?") && query.matches("?").count() > 1
        {
            return RewriteStrategy::SubQueries;
        }

        // Questions about concepts benefit from step-back
        if query_lower.starts_with("what is")
            || query_lower.starts_with("how does")
            || query_lower.starts_with("why")
            || query_lower.contains("explain")
        {
            return RewriteStrategy::StepBack;
        }

        // Specific factual queries benefit from HyDE
        if query_lower.starts_with("who")
            || query_lower.starts_with("when")
            || query_lower.starts_with("where")
            || query_lower.contains("specific")
        {
            return RewriteStrategy::Hyde;
        }

        // Short ambiguous queries benefit from multi-query
        if word_count < 5 {
            return RewriteStrategy::MultiQuery;
        }

        // Default to basic
        RewriteStrategy::Basic
    }

    /// Rewrite a query (sync placeholder - actual implementation would use LLM)
    pub fn rewrite_sync(
        &self,
        query: &str,
        strategy: RewriteStrategy,
        chat_history: Option<&[HashMap<String, String>]>,
        context: Option<&str>,
        num_queries: Option<usize>,
    ) -> RewriteResult {
        // Expand abbreviations first
        let expanded_query = self.expand_abbreviations(query);

        let has_history = chat_history.map(|h| !h.is_empty()).unwrap_or(false);
        let actual_strategy = if strategy == RewriteStrategy::Auto {
            self.detect_strategy(&expanded_query, has_history)
        } else {
            strategy
        };

        let num = num_queries.unwrap_or(self.config.max_queries);

        // Build result based on strategy
        let mut result = match actual_strategy {
            RewriteStrategy::Basic => {
                let rewritten = format!("{} (clarified and optimized for retrieval)", expanded_query);
                RewriteResult::new(query, vec![rewritten], actual_strategy)
            }
            RewriteStrategy::Hyde => {
                let hypothetical = format!(
                    "This document discusses {}. It provides detailed information about the topic, \
                    including key concepts, examples, and practical applications.",
                    expanded_query
                );
                RewriteResult::new(query, vec![expanded_query.clone()], actual_strategy)
                    .with_hypothetical_document(hypothetical)
            }
            RewriteStrategy::StepBack => {
                let step_back = format!("What are the fundamental concepts related to: {}", expanded_query);
                RewriteResult::new(query, vec![expanded_query.clone(), step_back.clone()], actual_strategy)
                    .with_step_back_question(step_back)
            }
            RewriteStrategy::SubQueries => {
                let sub = vec![
                    format!("What is {}?", expanded_query.split_whitespace().take(3).collect::<Vec<_>>().join(" ")),
                    format!("How does {} work?", expanded_query.split_whitespace().take(3).collect::<Vec<_>>().join(" ")),
                    format!("Examples of {}", expanded_query.split_whitespace().take(3).collect::<Vec<_>>().join(" ")),
                ];
                RewriteResult::new(query, vec![expanded_query.clone()], actual_strategy)
                    .with_sub_queries(sub)
            }
            RewriteStrategy::MultiQuery => {
                let variations: Vec<String> = (0..num.min(5))
                    .map(|i| format!("{} (variation {})", expanded_query, i + 1))
                    .collect();
                RewriteResult::new(query, variations, actual_strategy)
            }
            RewriteStrategy::Contextual => {
                let rewritten = format!("{} (contextualized from chat history)", expanded_query);
                RewriteResult::new(query, vec![rewritten], actual_strategy)
            }
            RewriteStrategy::Auto => {
                // Should not reach here as we detect above
                RewriteResult::new(query, vec![expanded_query], actual_strategy)
            }
        };

        // Add context to metadata if provided
        if let Some(ctx) = context {
            result = result.with_metadata("context", ctx.to_string());
        }

        result.with_metadata("model", self.config.model.clone())
    }
}

impl Default for QueryRewriterAgent {
    fn default() -> Self {
        Self::new().build()
    }
}

// =============================================================================
// TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // PromptExpanderAgent tests
    #[test]
    fn test_expand_strategy_display() {
        assert_eq!(ExpandStrategy::Basic.to_string(), "basic");
        assert_eq!(ExpandStrategy::Detailed.to_string(), "detailed");
        assert_eq!(ExpandStrategy::Auto.to_string(), "auto");
    }

    #[test]
    fn test_expand_strategy_from_str() {
        assert_eq!(ExpandStrategy::from_str("basic"), Some(ExpandStrategy::Basic));
        assert_eq!(ExpandStrategy::from_str("DETAILED"), Some(ExpandStrategy::Detailed));
        assert_eq!(ExpandStrategy::from_str("invalid"), None);
    }

    #[test]
    fn test_expand_result() {
        let result = ExpandResult::new("test", "expanded test", ExpandStrategy::Basic)
            .with_metadata("key", "value");

        assert_eq!(result.original_prompt, "test");
        assert_eq!(result.expanded_prompt, "expanded test");
        assert_eq!(result.strategy_used, ExpandStrategy::Basic);
        assert!(result.metadata.contains_key("key"));
    }

    #[test]
    fn test_expand_result_ratio() {
        let result = ExpandResult::new("test", "expanded test content", ExpandStrategy::Basic);
        assert!(result.expansion_ratio() > 1.0);

        let empty = ExpandResult::new("", "expanded", ExpandStrategy::Basic);
        assert_eq!(empty.expansion_ratio(), 0.0);
    }

    #[test]
    fn test_prompt_expander_builder() {
        let agent = PromptExpanderAgent::new()
            .name("TestExpander")
            .model("gpt-4")
            .temperature(0.5)
            .verbose()
            .build();

        assert_eq!(agent.name(), "TestExpander");
        assert_eq!(agent.model(), "gpt-4");
    }

    #[test]
    fn test_prompt_expander_detect_strategy() {
        let agent = PromptExpanderAgent::default();

        // Creative detection
        assert_eq!(
            agent.detect_strategy("Write a creative story about dragons"),
            ExpandStrategy::Creative
        );

        // Structured detection
        assert_eq!(
            agent.detect_strategy("Analyze the market trends"),
            ExpandStrategy::Structured
        );

        // Short prompts get detailed
        assert_eq!(
            agent.detect_strategy("Hello"),
            ExpandStrategy::Detailed
        );
    }

    #[test]
    fn test_prompt_expander_expand_sync() {
        let agent = PromptExpanderAgent::default();
        let result = agent.expand_sync("Write a blog post", ExpandStrategy::Basic, None);

        assert_eq!(result.original_prompt, "Write a blog post");
        assert!(!result.expanded_prompt.is_empty());
        assert_eq!(result.strategy_used, ExpandStrategy::Basic);
    }

    // QueryRewriterAgent tests
    #[test]
    fn test_rewrite_strategy_display() {
        assert_eq!(RewriteStrategy::Basic.to_string(), "basic");
        assert_eq!(RewriteStrategy::Hyde.to_string(), "hyde");
        assert_eq!(RewriteStrategy::StepBack.to_string(), "step_back");
    }

    #[test]
    fn test_rewrite_strategy_from_str() {
        assert_eq!(RewriteStrategy::from_str("basic"), Some(RewriteStrategy::Basic));
        assert_eq!(RewriteStrategy::from_str("hyde"), Some(RewriteStrategy::Hyde));
        assert_eq!(RewriteStrategy::from_str("step_back"), Some(RewriteStrategy::StepBack));
        assert_eq!(RewriteStrategy::from_str("stepback"), Some(RewriteStrategy::StepBack));
        assert_eq!(RewriteStrategy::from_str("invalid"), None);
    }

    #[test]
    fn test_rewrite_result() {
        let result = RewriteResult::new("test query", vec!["rewritten".to_string()], RewriteStrategy::Basic)
            .with_hypothetical_document("hypothetical doc")
            .with_step_back_question("broader question")
            .with_sub_queries(vec!["sub1".to_string(), "sub2".to_string()])
            .with_metadata("key", "value");

        assert_eq!(result.original_query, "test query");
        assert_eq!(result.primary_query(), Some("rewritten"));
        assert!(result.hypothetical_document.is_some());
        assert!(result.step_back_question.is_some());
        assert!(result.sub_queries.is_some());
    }

    #[test]
    fn test_rewrite_result_all_queries() {
        let result = RewriteResult::new("test", vec!["q1".to_string(), "q2".to_string()], RewriteStrategy::Basic)
            .with_sub_queries(vec!["sub1".to_string(), "sub2".to_string()]);

        let all = result.all_queries();
        assert_eq!(all.len(), 4);
    }

    #[test]
    fn test_query_rewriter_builder() {
        let agent = QueryRewriterAgent::new()
            .name("TestRewriter")
            .model("gpt-4")
            .max_queries(3)
            .abbreviation("ML", "Machine Learning")
            .temperature(0.2)
            .verbose()
            .build();

        assert_eq!(agent.name(), "TestRewriter");
        assert_eq!(agent.model(), "gpt-4");
    }

    #[test]
    fn test_query_rewriter_expand_abbreviations() {
        let agent = QueryRewriterAgent::new()
            .abbreviation("ML", "Machine Learning")
            .abbreviation("AI", "Artificial Intelligence")
            .build();

        let expanded = agent.expand_abbreviations("What is ML and AI?");
        assert!(expanded.contains("Machine Learning"));
        assert!(expanded.contains("Artificial Intelligence"));
    }

    #[test]
    fn test_query_rewriter_detect_strategy() {
        let agent = QueryRewriterAgent::default();

        // Complex query -> SubQueries
        assert_eq!(
            agent.detect_strategy("What is X and how does Y relate to Z?", false),
            RewriteStrategy::SubQueries
        );

        // Concept question -> StepBack
        assert_eq!(
            agent.detect_strategy("What is machine learning?", false),
            RewriteStrategy::StepBack
        );

        // Factual query -> Hyde
        assert_eq!(
            agent.detect_strategy("Who invented the telephone?", false),
            RewriteStrategy::Hyde
        );

        // With chat history -> Contextual
        assert_eq!(
            agent.detect_strategy("Tell me more", true),
            RewriteStrategy::Contextual
        );

        // Short query -> MultiQuery
        assert_eq!(
            agent.detect_strategy("Python", false),
            RewriteStrategy::MultiQuery
        );
    }

    #[test]
    fn test_query_rewriter_rewrite_sync() {
        let agent = QueryRewriterAgent::default();
        let result = agent.rewrite_sync("What is Rust?", RewriteStrategy::Basic, None, None, None);

        assert_eq!(result.original_query, "What is Rust?");
        assert!(!result.rewritten_queries.is_empty());
        assert_eq!(result.strategy_used, RewriteStrategy::Basic);
    }

    #[test]
    fn test_query_rewriter_hyde_strategy() {
        let agent = QueryRewriterAgent::default();
        let result = agent.rewrite_sync("What is Rust?", RewriteStrategy::Hyde, None, None, None);

        assert!(result.hypothetical_document.is_some());
    }

    #[test]
    fn test_query_rewriter_sub_queries_strategy() {
        let agent = QueryRewriterAgent::default();
        let result = agent.rewrite_sync("Complex query", RewriteStrategy::SubQueries, None, None, None);

        assert!(result.sub_queries.is_some());
        assert!(!result.sub_queries.as_ref().unwrap().is_empty());
    }
}