cognis 0.2.0

LLM application framework built on cognis-core
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
//! Prompt management — versioning, storage, composition, few-shot selection,
//! and simple optimisation utilities.

use std::collections::HashMap;

use serde_json::Value;

use cognis_core::error::{CognisError, Result};

// ---------------------------------------------------------------------------
// PromptVersion
// ---------------------------------------------------------------------------

/// A single immutable snapshot of a prompt at a particular version.
#[derive(Debug, Clone)]
pub struct PromptVersion {
    /// Semantic or sequential version string (e.g. "1", "1.0.0").
    pub version: String,
    /// The full prompt content for this version.
    pub content: String,
    /// ISO-8601 timestamp of when this version was created.
    pub created_at: String,
    /// Optional author who created this version.
    pub author: Option<String>,
    /// Optional changelog entry describing what changed.
    pub changelog: Option<String>,
}

impl PromptVersion {
    /// Serialise this version to a JSON [`Value`].
    pub fn to_json(&self) -> Value {
        let mut map = serde_json::Map::new();
        map.insert("version".into(), Value::String(self.version.clone()));
        map.insert("content".into(), Value::String(self.content.clone()));
        map.insert("created_at".into(), Value::String(self.created_at.clone()));
        map.insert(
            "author".into(),
            match &self.author {
                Some(a) => Value::String(a.clone()),
                None => Value::Null,
            },
        );
        map.insert(
            "changelog".into(),
            match &self.changelog {
                Some(c) => Value::String(c.clone()),
                None => Value::Null,
            },
        );
        Value::Object(map)
    }
}

// ---------------------------------------------------------------------------
// VersionedPrompt
// ---------------------------------------------------------------------------

/// A named prompt that tracks multiple versions with rollback support.
#[derive(Debug, Clone)]
pub struct VersionedPrompt {
    /// Human-readable name of this prompt.
    pub name: String,
    /// Ordered list of versions (oldest first).
    versions: Vec<PromptVersion>,
    /// Index into `versions` that is considered "current".
    current_idx: usize,
}

impl VersionedPrompt {
    /// Create a new versioned prompt with initial content (version "1").
    pub fn new(name: String, initial_content: String) -> Self {
        let version = PromptVersion {
            version: "1".to_string(),
            content: initial_content,
            created_at: "2026-03-08T00:00:00Z".to_string(),
            author: None,
            changelog: Some("Initial version".to_string()),
        };
        Self {
            name,
            versions: vec![version],
            current_idx: 0,
        }
    }

    /// Add a new version with the given content, author, and changelog.
    pub fn add_version(
        &mut self,
        content: String,
        author: Option<String>,
        changelog: Option<String>,
    ) {
        let next_num = self.versions.len() + 1;
        let version = PromptVersion {
            version: next_num.to_string(),
            content,
            created_at: "2026-03-08T00:00:00Z".to_string(),
            author,
            changelog,
        };
        self.versions.push(version);
        self.current_idx = self.versions.len() - 1;
    }

    /// Return a reference to the current version.
    pub fn current(&self) -> &PromptVersion {
        &self.versions[self.current_idx]
    }

    /// Look up a version by its version string.
    pub fn get_version(&self, version: &str) -> Option<&PromptVersion> {
        self.versions.iter().find(|v| v.version == version)
    }

    /// Roll back the current pointer to the specified version string.
    pub fn rollback(&mut self, version: &str) -> Result<()> {
        let idx = self
            .versions
            .iter()
            .position(|v| v.version == version)
            .ok_or_else(|| CognisError::Other(format!("Version '{}' not found", version)))?;
        self.current_idx = idx;
        Ok(())
    }

    /// Return the full ordered history of versions.
    pub fn history(&self) -> &[PromptVersion] {
        &self.versions
    }

    /// Return the number of stored versions.
    pub fn version_count(&self) -> usize {
        self.versions.len()
    }
}

// ---------------------------------------------------------------------------
// PromptLibrary
// ---------------------------------------------------------------------------

/// An in-memory library of named [`VersionedPrompt`]s with search support.
#[derive(Debug)]
pub struct PromptLibrary {
    prompts: HashMap<String, VersionedPrompt>,
}

impl PromptLibrary {
    /// Create an empty library.
    pub fn new() -> Self {
        Self {
            prompts: HashMap::new(),
        }
    }

    /// Add a new prompt (creates version 1).
    pub fn add(&mut self, name: &str, content: &str) {
        let vp = VersionedPrompt::new(name.to_string(), content.to_string());
        self.prompts.insert(name.to_string(), vp);
    }

    /// Get an immutable reference to a prompt by name.
    pub fn get(&self, name: &str) -> Option<&VersionedPrompt> {
        self.prompts.get(name)
    }

    /// Get a mutable reference to a prompt by name.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut VersionedPrompt> {
        self.prompts.get_mut(name)
    }

    /// Remove a prompt by name, returning `true` if it existed.
    pub fn remove(&mut self, name: &str) -> bool {
        self.prompts.remove(name).is_some()
    }

    /// Search prompts whose name or current content contains `query`
    /// (case-insensitive).
    pub fn search(&self, query: &str) -> Vec<&VersionedPrompt> {
        let q = query.to_lowercase();
        self.prompts
            .values()
            .filter(|vp| {
                vp.name.to_lowercase().contains(&q)
                    || vp.current().content.to_lowercase().contains(&q)
            })
            .collect()
    }

    /// List all prompt names in the library.
    pub fn list(&self) -> Vec<&str> {
        self.prompts.keys().map(|s| s.as_str()).collect()
    }

    /// Return the number of prompts.
    pub fn len(&self) -> usize {
        self.prompts.len()
    }

    /// Return whether the library is empty.
    pub fn is_empty(&self) -> bool {
        self.prompts.is_empty()
    }

    /// Serialise the library to a JSON [`Value`].
    pub fn to_json(&self) -> Value {
        let mut map = serde_json::Map::new();
        for (name, vp) in &self.prompts {
            let versions: Vec<Value> = vp.versions.iter().map(|v| v.to_json()).collect();
            map.insert(name.clone(), Value::Array(versions));
        }
        Value::Object(map)
    }
}

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

// ---------------------------------------------------------------------------
// PromptSectionComposer
// ---------------------------------------------------------------------------

/// Builds a single prompt string from multiple structured sections (system
/// message, context, examples, instructions).
#[derive(Debug, Clone)]
pub struct PromptSectionComposer {
    system: Option<String>,
    context: Vec<(String, String)>,
    examples: Vec<(String, String)>,
    instructions: Option<String>,
}

impl PromptSectionComposer {
    /// Create an empty composer.
    pub fn new() -> Self {
        Self {
            system: None,
            context: Vec::new(),
            examples: Vec::new(),
            instructions: None,
        }
    }

    /// Set the system message section.
    pub fn add_system(&mut self, content: &str) {
        self.system = Some(content.to_string());
    }

    /// Append a key-value context entry.
    pub fn add_context(&mut self, key: &str, value: &str) {
        self.context.push((key.to_string(), value.to_string()));
    }

    /// Append input/output example pairs.
    pub fn add_examples(&mut self, examples: Vec<(String, String)>) {
        self.examples.extend(examples);
    }

    /// Set the instructions section.
    pub fn add_instructions(&mut self, content: &str) {
        self.instructions = Some(content.to_string());
    }

    /// Compose all sections into a single prompt string.
    pub fn compose(&self) -> String {
        let mut parts: Vec<String> = Vec::new();

        if let Some(sys) = &self.system {
            parts.push(format!("[System]\n{}", sys));
        }

        if !self.context.is_empty() {
            let ctx_lines: Vec<String> = self
                .context
                .iter()
                .map(|(k, v)| format!("- {}: {}", k, v))
                .collect();
            parts.push(format!("[Context]\n{}", ctx_lines.join("\n")));
        }

        if !self.examples.is_empty() {
            let ex_lines: Vec<String> = self
                .examples
                .iter()
                .enumerate()
                .map(|(i, (input, output))| {
                    format!(
                        "Example {}:\n  Input: {}\n  Output: {}",
                        i + 1,
                        input,
                        output
                    )
                })
                .collect();
            parts.push(format!("[Examples]\n{}", ex_lines.join("\n")));
        }

        if let Some(instr) = &self.instructions {
            parts.push(format!("[Instructions]\n{}", instr));
        }

        parts.join("\n\n")
    }
}

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

// ---------------------------------------------------------------------------
// FewShotExample
// ---------------------------------------------------------------------------

/// A single few-shot example with an input, expected output, and optional
/// explanation.
#[derive(Debug, Clone)]
pub struct FewShotExample {
    /// The example input / question.
    pub input: String,
    /// The expected output / answer.
    pub output: String,
    /// Optional explanation of the reasoning.
    pub explanation: Option<String>,
}

impl FewShotExample {
    /// Serialise this example to a JSON [`Value`].
    pub fn to_json(&self) -> Value {
        let mut map = serde_json::Map::new();
        map.insert("input".into(), Value::String(self.input.clone()));
        map.insert("output".into(), Value::String(self.output.clone()));
        map.insert(
            "explanation".into(),
            match &self.explanation {
                Some(e) => Value::String(e.clone()),
                None => Value::Null,
            },
        );
        Value::Object(map)
    }

    /// Format the example as a human-readable string.
    pub fn format(&self) -> String {
        let mut s = format!("Input: {}\nOutput: {}", self.input, self.output);
        if let Some(explanation) = &self.explanation {
            s.push_str(&format!("\nExplanation: {}", explanation));
        }
        s
    }
}

// ---------------------------------------------------------------------------
// FewShotSelector
// ---------------------------------------------------------------------------

/// Selects relevant [`FewShotExample`]s using simple keyword similarity or
/// random sampling.
#[derive(Debug, Clone)]
pub struct FewShotSelector {
    examples: Vec<FewShotExample>,
}

impl FewShotSelector {
    /// Create a selector with the given pool of examples.
    pub fn new(examples: Vec<FewShotExample>) -> Self {
        Self { examples }
    }

    /// Select up to `k` examples whose input shares the most words with
    /// `query` (simple keyword overlap scoring).
    pub fn select_by_similarity<'a>(&'a self, query: &str, k: usize) -> Vec<&'a FewShotExample> {
        let query_words: Vec<String> = query
            .to_lowercase()
            .split_whitespace()
            .map(|w| w.to_string())
            .collect();

        let mut scored: Vec<(usize, &FewShotExample)> = self
            .examples
            .iter()
            .map(|ex| {
                let input_lower = ex.input.to_lowercase();
                let score = query_words
                    .iter()
                    .filter(|w| input_lower.contains(w.as_str()))
                    .count();
                (score, ex)
            })
            .collect();

        // Sort descending by score, then take top-k.
        scored.sort_by_key(|b| std::cmp::Reverse(b.0));
        scored.into_iter().take(k).map(|(_, ex)| ex).collect()
    }

    /// Select up to `k` examples in a deterministic round-robin fashion
    /// (evenly spaced indices). This avoids pulling in a random number
    /// generator dependency while still providing variety.
    pub fn select_random(&self, k: usize) -> Vec<&FewShotExample> {
        if self.examples.is_empty() || k == 0 {
            return Vec::new();
        }
        let n = self.examples.len();
        let take = k.min(n);
        let step = n as f64 / take as f64;
        (0..take)
            .map(|i| {
                let idx = (i as f64 * step) as usize;
                &self.examples[idx.min(n - 1)]
            })
            .collect()
    }

    /// Return the total number of examples in the pool.
    pub fn len(&self) -> usize {
        self.examples.len()
    }

    /// Return whether the pool is empty.
    pub fn is_empty(&self) -> bool {
        self.examples.is_empty()
    }
}

// ---------------------------------------------------------------------------
// PromptOptimizer
// ---------------------------------------------------------------------------

/// Simple, dependency-free prompt optimisation utilities.
pub struct PromptOptimizer;

impl PromptOptimizer {
    /// Collapse runs of whitespace into single spaces and trim.
    pub fn trim_whitespace(prompt: &str) -> String {
        prompt.split_whitespace().collect::<Vec<_>>().join(" ")
    }

    /// Rough token count estimation (~4 characters per token, which is the
    /// commonly cited average for English text with GPT-style tokenisers).
    pub fn estimate_tokens(prompt: &str) -> usize {
        let chars = prompt.len();
        chars.div_ceil(4)
    }

    /// Truncate a prompt so that its estimated token count does not exceed
    /// `max_tokens`. Truncation happens at a word boundary when possible.
    pub fn truncate_to_tokens(prompt: &str, max_tokens: usize) -> String {
        let max_chars = max_tokens * 4;
        if prompt.len() <= max_chars {
            return prompt.to_string();
        }
        // Try to break at a word boundary.
        let truncated = &prompt[..max_chars];
        if let Some(last_space) = truncated.rfind(' ') {
            format!("{}...", &prompt[..last_space])
        } else {
            format!("{}...", truncated)
        }
    }

    /// Return a summary of the prompt by taking the first `max_chars`
    /// characters, breaking at a word boundary, and appending an ellipsis.
    pub fn summarize_prompt(prompt: &str, max_chars: usize) -> String {
        if prompt.len() <= max_chars {
            return prompt.to_string();
        }
        let slice = &prompt[..max_chars];
        if let Some(last_space) = slice.rfind(' ') {
            format!("{}...", &prompt[..last_space])
        } else {
            format!("{}...", slice)
        }
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    // -- PromptVersion -------------------------------------------------------

    #[test]
    fn test_prompt_version_to_json() {
        let v = PromptVersion {
            version: "1".into(),
            content: "Hello".into(),
            created_at: "2026-01-01".into(),
            author: Some("alice".into()),
            changelog: Some("init".into()),
        };
        let j = v.to_json();
        assert_eq!(j["version"], "1");
        assert_eq!(j["content"], "Hello");
        assert_eq!(j["author"], "alice");
        assert_eq!(j["changelog"], "init");
    }

    #[test]
    fn test_prompt_version_to_json_nulls() {
        let v = PromptVersion {
            version: "2".into(),
            content: "Hi".into(),
            created_at: "2026-01-02".into(),
            author: None,
            changelog: None,
        };
        let j = v.to_json();
        assert!(j["author"].is_null());
        assert!(j["changelog"].is_null());
    }

    // -- VersionedPrompt -----------------------------------------------------

    #[test]
    fn test_versioned_prompt_new() {
        let vp = VersionedPrompt::new("greet".into(), "Hello {name}".into());
        assert_eq!(vp.name, "greet");
        assert_eq!(vp.version_count(), 1);
        assert_eq!(vp.current().version, "1");
        assert_eq!(vp.current().content, "Hello {name}");
    }

    #[test]
    fn test_versioned_prompt_add_version() {
        let mut vp = VersionedPrompt::new("greet".into(), "v1".into());
        vp.add_version("v2".into(), Some("bob".into()), Some("updated".into()));
        assert_eq!(vp.version_count(), 2);
        assert_eq!(vp.current().version, "2");
        assert_eq!(vp.current().content, "v2");
        assert_eq!(vp.current().author.as_deref(), Some("bob"));
    }

    #[test]
    fn test_versioned_prompt_get_version() {
        let mut vp = VersionedPrompt::new("x".into(), "a".into());
        vp.add_version("b".into(), None, None);
        assert!(vp.get_version("1").is_some());
        assert!(vp.get_version("2").is_some());
        assert!(vp.get_version("3").is_none());
    }

    #[test]
    fn test_versioned_prompt_rollback_ok() {
        let mut vp = VersionedPrompt::new("x".into(), "a".into());
        vp.add_version("b".into(), None, None);
        vp.add_version("c".into(), None, None);
        assert_eq!(vp.current().version, "3");
        vp.rollback("1").unwrap();
        assert_eq!(vp.current().version, "1");
        assert_eq!(vp.current().content, "a");
    }

    #[test]
    fn test_versioned_prompt_rollback_err() {
        let mut vp = VersionedPrompt::new("x".into(), "a".into());
        let res = vp.rollback("99");
        assert!(res.is_err());
    }

    #[test]
    fn test_versioned_prompt_history() {
        let mut vp = VersionedPrompt::new("x".into(), "a".into());
        vp.add_version("b".into(), None, None);
        let h = vp.history();
        assert_eq!(h.len(), 2);
        assert_eq!(h[0].content, "a");
        assert_eq!(h[1].content, "b");
    }

    #[test]
    fn test_versioned_prompt_rollback_then_add() {
        let mut vp = VersionedPrompt::new("x".into(), "a".into());
        vp.add_version("b".into(), None, None);
        vp.rollback("1").unwrap();
        assert_eq!(vp.current().content, "a");
        vp.add_version("c".into(), None, None);
        assert_eq!(vp.current().content, "c");
        assert_eq!(vp.version_count(), 3);
    }

    // -- PromptLibrary -------------------------------------------------------

    #[test]
    fn test_library_new_empty() {
        let lib = PromptLibrary::new();
        assert_eq!(lib.len(), 0);
        assert!(lib.is_empty());
    }

    #[test]
    fn test_library_add_and_get() {
        let mut lib = PromptLibrary::new();
        lib.add("greet", "Hello {name}");
        assert_eq!(lib.len(), 1);
        let vp = lib.get("greet").unwrap();
        assert_eq!(vp.current().content, "Hello {name}");
    }

    #[test]
    fn test_library_get_missing() {
        let lib = PromptLibrary::new();
        assert!(lib.get("nope").is_none());
    }

    #[test]
    fn test_library_get_mut() {
        let mut lib = PromptLibrary::new();
        lib.add("greet", "v1");
        {
            let vp = lib.get_mut("greet").unwrap();
            vp.add_version("v2".into(), None, None);
        }
        assert_eq!(lib.get("greet").unwrap().current().content, "v2");
    }

    #[test]
    fn test_library_remove() {
        let mut lib = PromptLibrary::new();
        lib.add("greet", "Hello");
        assert!(lib.remove("greet"));
        assert!(!lib.remove("greet"));
        assert_eq!(lib.len(), 0);
    }

    #[test]
    fn test_library_search_by_name() {
        let mut lib = PromptLibrary::new();
        lib.add("greeting", "Hello");
        lib.add("farewell", "Goodbye");
        let results = lib.search("greet");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "greeting");
    }

    #[test]
    fn test_library_search_by_content() {
        let mut lib = PromptLibrary::new();
        lib.add("p1", "summarize the document");
        lib.add("p2", "translate text");
        let results = lib.search("summarize");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_library_search_case_insensitive() {
        let mut lib = PromptLibrary::new();
        lib.add("MyPrompt", "Content here");
        let results = lib.search("myprompt");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_library_search_no_match() {
        let mut lib = PromptLibrary::new();
        lib.add("p1", "hello");
        let results = lib.search("xyz");
        assert!(results.is_empty());
    }

    #[test]
    fn test_library_list() {
        let mut lib = PromptLibrary::new();
        lib.add("alpha", "a");
        lib.add("beta", "b");
        let mut names = lib.list();
        names.sort();
        assert_eq!(names, vec!["alpha", "beta"]);
    }

    #[test]
    fn test_library_to_json() {
        let mut lib = PromptLibrary::new();
        lib.add("p1", "content");
        let j = lib.to_json();
        assert!(j.is_object());
        assert!(j["p1"].is_array());
    }

    #[test]
    fn test_library_default() {
        let lib = PromptLibrary::default();
        assert!(lib.is_empty());
    }

    // -- PromptComposer ------------------------------------------------------

    #[test]
    fn test_composer_empty() {
        let c = PromptSectionComposer::new();
        assert_eq!(c.compose(), "");
    }

    #[test]
    fn test_composer_system_only() {
        let mut c = PromptSectionComposer::new();
        c.add_system("You are helpful.");
        assert!(c.compose().contains("[System]"));
        assert!(c.compose().contains("You are helpful."));
    }

    #[test]
    fn test_composer_context() {
        let mut c = PromptSectionComposer::new();
        c.add_context("language", "Rust");
        let out = c.compose();
        assert!(out.contains("[Context]"));
        assert!(out.contains("- language: Rust"));
    }

    #[test]
    fn test_composer_examples() {
        let mut c = PromptSectionComposer::new();
        c.add_examples(vec![("hi".into(), "hello".into())]);
        let out = c.compose();
        assert!(out.contains("[Examples]"));
        assert!(out.contains("Input: hi"));
        assert!(out.contains("Output: hello"));
    }

    #[test]
    fn test_composer_instructions() {
        let mut c = PromptSectionComposer::new();
        c.add_instructions("Be concise.");
        let out = c.compose();
        assert!(out.contains("[Instructions]"));
        assert!(out.contains("Be concise."));
    }

    #[test]
    fn test_composer_full() {
        let mut c = PromptSectionComposer::new();
        c.add_system("System msg");
        c.add_context("k", "v");
        c.add_examples(vec![("a".into(), "b".into())]);
        c.add_instructions("Do stuff");
        let out = c.compose();
        assert!(out.contains("[System]"));
        assert!(out.contains("[Context]"));
        assert!(out.contains("[Examples]"));
        assert!(out.contains("[Instructions]"));
    }

    #[test]
    fn test_composer_default() {
        let c = PromptSectionComposer::default();
        assert_eq!(c.compose(), "");
    }

    #[test]
    fn test_composer_multiple_contexts() {
        let mut c = PromptSectionComposer::new();
        c.add_context("a", "1");
        c.add_context("b", "2");
        let out = c.compose();
        assert!(out.contains("- a: 1"));
        assert!(out.contains("- b: 2"));
    }

    // -- FewShotExample ------------------------------------------------------

    #[test]
    fn test_few_shot_example_format() {
        let ex = FewShotExample {
            input: "2+2".into(),
            output: "4".into(),
            explanation: None,
        };
        let f = ex.format();
        assert!(f.contains("Input: 2+2"));
        assert!(f.contains("Output: 4"));
        assert!(!f.contains("Explanation"));
    }

    #[test]
    fn test_few_shot_example_format_with_explanation() {
        let ex = FewShotExample {
            input: "2+2".into(),
            output: "4".into(),
            explanation: Some("addition".into()),
        };
        let f = ex.format();
        assert!(f.contains("Explanation: addition"));
    }

    #[test]
    fn test_few_shot_example_to_json() {
        let ex = FewShotExample {
            input: "q".into(),
            output: "a".into(),
            explanation: Some("reason".into()),
        };
        let j = ex.to_json();
        assert_eq!(j["input"], "q");
        assert_eq!(j["output"], "a");
        assert_eq!(j["explanation"], "reason");
    }

    #[test]
    fn test_few_shot_example_to_json_null_explanation() {
        let ex = FewShotExample {
            input: "q".into(),
            output: "a".into(),
            explanation: None,
        };
        let j = ex.to_json();
        assert!(j["explanation"].is_null());
    }

    // -- FewShotSelector -----------------------------------------------------

    fn sample_examples() -> Vec<FewShotExample> {
        vec![
            FewShotExample {
                input: "translate hello to french".into(),
                output: "bonjour".into(),
                explanation: None,
            },
            FewShotExample {
                input: "translate goodbye to french".into(),
                output: "au revoir".into(),
                explanation: None,
            },
            FewShotExample {
                input: "summarize the article about rust".into(),
                output: "Rust is a systems language.".into(),
                explanation: None,
            },
            FewShotExample {
                input: "write a poem about nature".into(),
                output: "Trees sway gently...".into(),
                explanation: None,
            },
        ]
    }

    #[test]
    fn test_selector_len() {
        let sel = FewShotSelector::new(sample_examples());
        assert_eq!(sel.len(), 4);
        assert!(!sel.is_empty());
    }

    #[test]
    fn test_selector_empty() {
        let sel = FewShotSelector::new(vec![]);
        assert_eq!(sel.len(), 0);
        assert!(sel.is_empty());
    }

    #[test]
    fn test_selector_similarity_translate() {
        let sel = FewShotSelector::new(sample_examples());
        let results = sel.select_by_similarity("translate this to french", 2);
        assert_eq!(results.len(), 2);
        // Both translate examples should be top-scored.
        assert!(results[0].input.contains("translate"));
        assert!(results[1].input.contains("translate"));
    }

    #[test]
    fn test_selector_similarity_k_larger_than_pool() {
        let sel = FewShotSelector::new(sample_examples());
        let results = sel.select_by_similarity("translate", 100);
        assert_eq!(results.len(), 4);
    }

    #[test]
    fn test_selector_similarity_no_overlap() {
        let sel = FewShotSelector::new(sample_examples());
        let results = sel.select_by_similarity("xyzzy", 2);
        // Still returns 2 examples (with score 0), just no particular ordering.
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_selector_random() {
        let sel = FewShotSelector::new(sample_examples());
        let results = sel.select_random(2);
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_selector_random_k_zero() {
        let sel = FewShotSelector::new(sample_examples());
        let results = sel.select_random(0);
        assert!(results.is_empty());
    }

    #[test]
    fn test_selector_random_empty_pool() {
        let sel = FewShotSelector::new(vec![]);
        let results = sel.select_random(5);
        assert!(results.is_empty());
    }

    #[test]
    fn test_selector_random_k_exceeds_pool() {
        let sel = FewShotSelector::new(sample_examples());
        let results = sel.select_random(100);
        assert_eq!(results.len(), 4);
    }

    // -- PromptOptimizer -----------------------------------------------------

    #[test]
    fn test_optimizer_trim_whitespace() {
        let result = PromptOptimizer::trim_whitespace("  hello   world  ");
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_optimizer_trim_whitespace_newlines() {
        let result = PromptOptimizer::trim_whitespace("hello\n\n  world\t!");
        assert_eq!(result, "hello world !");
    }

    #[test]
    fn test_optimizer_trim_whitespace_empty() {
        assert_eq!(PromptOptimizer::trim_whitespace(""), "");
    }

    #[test]
    fn test_optimizer_estimate_tokens() {
        // 12 chars -> ceil(12/4) = 3
        assert_eq!(PromptOptimizer::estimate_tokens("hello world!"), 3);
    }

    #[test]
    fn test_optimizer_estimate_tokens_empty() {
        assert_eq!(PromptOptimizer::estimate_tokens(""), 0);
    }

    #[test]
    fn test_optimizer_truncate_no_op() {
        let prompt = "short";
        let result = PromptOptimizer::truncate_to_tokens(prompt, 100);
        assert_eq!(result, "short");
    }

    #[test]
    fn test_optimizer_truncate_cuts() {
        let prompt = "this is a longer prompt that should be truncated";
        let result = PromptOptimizer::truncate_to_tokens(prompt, 3); // ~12 chars
        assert!(result.len() < prompt.len());
        assert!(result.ends_with("..."));
    }

    #[test]
    fn test_optimizer_summarize_short() {
        let result = PromptOptimizer::summarize_prompt("hi", 100);
        assert_eq!(result, "hi");
    }

    #[test]
    fn test_optimizer_summarize_long() {
        let prompt = "This is a long prompt that needs to be summarized down";
        let result = PromptOptimizer::summarize_prompt(prompt, 20);
        assert!(result.ends_with("..."));
        assert!(result.len() <= 24); // 20 + "..."
    }

    #[test]
    fn test_optimizer_estimate_tokens_one_char() {
        assert_eq!(PromptOptimizer::estimate_tokens("a"), 1);
    }
}