matrixcode-core 0.4.26

MatrixCode Agent Core - Pure logic, no UI
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
//! Prompt Orchestrator
//!
//! Core prompt assembly system that:
//! - Composes sections in order
//! - Inserts cache boundary markers
//! - Manages caching for static sections
//! - Injects runtime context

use std::sync::Arc;
use crate::prompt::{PromptSection, SectionCache, CacheKey};
use crate::prompt::{ContextInjector, UserContext, SystemContext};

/// Cache boundary marker for API caching
/// 
/// This marker indicates where cached content ends and dynamic content begins.
/// APIs like Claude can use this for prompt prefix caching.
pub const CACHE_BOUNDARY: &str = "\n<!-- CACHE_BOUNDARY -->\n";

/// Prompt profile type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PromptProfile {
    /// Default profile (full capabilities)
    Default,
    /// Safe profile (restricted operations)
    Safe,
    /// Fast profile (minimal prompt)
    Fast,
    /// Review profile (code review focus)
    Review,
}

impl PromptProfile {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Safe => "safe",
            Self::Fast => "fast",
            Self::Review => "review",
        }
    }

    pub fn from_str(s: &str) -> Self {
        match s {
            "safe" => Self::Safe,
            "fast" => Self::Fast,
            "review" => Self::Review,
            _ => Self::Default,
        }
    }
}

impl std::fmt::Display for PromptProfile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

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

/// Prompt orchestrator - manages prompt assembly
pub struct PromptOrchestrator {
    /// Cache for static sections
    cache: Arc<SectionCache>,
    /// Context injector
    context_injector: ContextInjector,
    /// Current profile
    profile: PromptProfile,
    /// Sections to include
    sections: Vec<PromptSection>,
    /// Whether to include cache boundary
    include_boundary: bool,
    /// Whether to inject context
    inject_context: bool,
}

impl PromptOrchestrator {
    /// Create a new orchestrator
    pub fn new<P: Into<std::path::PathBuf>>(working_dir: P) -> Self {
        Self {
            cache: Arc::new(SectionCache::new()),
            context_injector: ContextInjector::new(working_dir.into()),
            profile: PromptProfile::Default,
            sections: Vec::new(),
            include_boundary: true,
            inject_context: true,
        }
    }

    /// Create with shared cache
    pub fn with_cache(mut self, cache: Arc<SectionCache>) -> Self {
        self.cache = cache;
        self
    }

    /// Set profile
    pub fn with_profile(mut self, profile: PromptProfile) -> Self {
        self.profile = profile;
        self
    }

    /// Set whether to include cache boundary
    pub fn with_boundary(mut self, include: bool) -> Self {
        self.include_boundary = include;
        self
    }

    /// Set whether to inject context
    pub fn with_context_injection(mut self, inject: bool) -> Self {
        self.inject_context = inject;
        self
    }

    /// Add a section
    pub fn add_section(&mut self, section: PromptSection) -> &mut Self {
        self.sections.push(section);
        self
    }

    /// Add multiple sections
    pub fn add_sections(&mut self, sections: Vec<PromptSection>) -> &mut Self {
        self.sections.extend(sections);
        self
    }

    /// Clear all sections
    pub fn clear_sections(&mut self) -> &mut Self {
        self.sections.clear();
        self
    }

    /// Invalidate cache (e.g., when context changes)
    pub fn invalidate_cache(&mut self) {
        self.cache.clear();
        self.context_injector.invalidate();
    }

    /// Render a section with caching
    fn render_section(&self, section: &PromptSection) -> String {
        if section.cacheable {
            let key = CacheKey::new(&section.name, self.profile.as_str());
            self.cache.get_or_compute(&key, || section.compute_content())
        } else {
            section.compute_content()
        }
    }

    /// Assemble the full prompt
    pub fn assemble(&mut self) -> AssembledPrompt {
        let mut cached_parts = Vec::new();
        let mut dynamic_parts = Vec::new();
        let mut cached_tokens = 0;
        let mut dynamic_tokens = 0;

        // Sort sections by order
        let mut sections = self.sections.clone();
        sections.sort_by_key(|s| s.order);

        // Process each section
        for section in &sections {
            let content = self.render_section(section);
            
            if section.cacheable {
                cached_parts.push((section.name.clone(), content.clone()));
                cached_tokens += self.estimate_tokens(&content);
            } else {
                dynamic_parts.push((section.name.clone(), content.clone()));
                dynamic_tokens += self.estimate_tokens(&content);
            }
        }

        // Inject runtime context if enabled
        let context_section = if self.inject_context {
            let ctx = self.context_injector.render_full_context();
            dynamic_tokens += self.estimate_tokens(&ctx);
            Some(ctx)
        } else {
            None
        };

        // Assemble final prompt
        let mut final_parts = Vec::new();

        // Add cached parts first
        for (name, content) in &cached_parts {
            if !content.is_empty() {
                final_parts.push(format!("[{}]\n{}", name, content));
            }
        }

        // Add cache boundary if there are both cached and dynamic parts
        if self.include_boundary && !cached_parts.is_empty() && (!dynamic_parts.is_empty() || context_section.is_some()) {
            final_parts.push(CACHE_BOUNDARY.to_string());
        }

        // Add dynamic parts
        for (name, content) in &dynamic_parts {
            if !content.is_empty() {
                final_parts.push(format!("[{}]\n{}", name, content));
            }
        }

        // Add context section
        if let Some(ctx) = context_section {
            final_parts.push(ctx);
        }

        let full_prompt = final_parts.join("\n\n");

        // Get cache stats
        let stats = self.cache.stats();

        AssembledPrompt {
            prompt: full_prompt,
            cached_sections: cached_parts.len(),
            dynamic_sections: dynamic_parts.len(),
            cached_tokens,
            dynamic_tokens,
            total_tokens: cached_tokens + dynamic_tokens,
            cache_hit_rate: stats.hit_rate(),
            profile: self.profile,
        }
    }

    /// Assemble prompt for specific profile
    pub fn assemble_for_profile(&mut self, profile: PromptProfile) -> AssembledPrompt {
        self.profile = profile;
        self.assemble()
    }

    /// Estimate token count
    fn estimate_tokens(&self, content: &str) -> usize {
        crate::prompt::cache::estimate_tokens(content)
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> crate::prompt::cache::CacheStats {
        self.cache.stats()
    }

    /// Get user context
    pub fn get_user_context(&mut self) -> &UserContext {
        self.context_injector.get_user_context()
    }

    /// Get system context
    pub fn get_system_context(&mut self) -> &SystemContext {
        self.context_injector.get_system_context()
    }
}

/// Assembled prompt with metadata
#[derive(Debug, Clone)]
pub struct AssembledPrompt {
    /// The full prompt text
    pub prompt: String,
    /// Number of cached sections
    pub cached_sections: usize,
    /// Number of dynamic sections
    pub dynamic_sections: usize,
    /// Estimated cached tokens
    pub cached_tokens: usize,
    /// Estimated dynamic tokens
    pub dynamic_tokens: usize,
    /// Total estimated tokens
    pub total_tokens: usize,
    /// Cache hit rate
    pub cache_hit_rate: f64,
    /// Profile used
    pub profile: PromptProfile,
}

impl AssembledPrompt {
    /// Check if prompt is empty
    pub fn is_empty(&self) -> bool {
        self.prompt.is_empty()
    }

    /// Get prompt length in characters
    pub fn len(&self) -> usize {
        self.prompt.len()
    }

    /// Get cache efficiency percentage
    pub fn cache_efficiency(&self) -> f64 {
        if self.total_tokens == 0 {
            0.0
        } else {
            (self.cached_tokens as f64 / self.total_tokens as f64) * 100.0
        }
    }

    /// Split prompt at cache boundary
    pub fn split_at_boundary(&self) -> (Option<&str>, Option<&str>) {
        if let Some(idx) = self.prompt.find(CACHE_BOUNDARY) {
            let cached = &self.prompt[..idx];
            let dynamic = &self.prompt[idx + CACHE_BOUNDARY.len()..];
            (
                if cached.is_empty() { None } else { Some(cached) },
                if dynamic.is_empty() { None } else { Some(dynamic) },
            )
        } else {
            if self.prompt.is_empty() {
                (None, None)
            } else {
                (Some(&self.prompt), None)
            }
        }
    }
}

/// Builder for creating prompt orchestrators
pub struct PromptBuilder {
    working_dir: std::path::PathBuf,
    profile: PromptProfile,
    sections: Vec<PromptSection>,
    include_boundary: bool,
    inject_context: bool,
}

impl PromptBuilder {
    pub fn new<P: Into<std::path::PathBuf>>(working_dir: P) -> Self {
        Self {
            working_dir: working_dir.into(),
            profile: PromptProfile::Default,
            sections: Vec::new(),
            include_boundary: true,
            inject_context: true,
        }
    }

    pub fn profile(mut self, profile: PromptProfile) -> Self {
        self.profile = profile;
        self
    }

    pub fn add_section(mut self, section: PromptSection) -> Self {
        self.sections.push(section);
        self
    }

    pub fn add_static(self, name: impl Into<String>, content: &'static str) -> Self {
        self.add_section(PromptSection::static_section(name, content))
    }

    pub fn add_dynamic<F>(self, name: impl Into<String>, compute: F) -> Self
    where
        F: Fn() -> String + Send + Sync + 'static,
    {
        self.add_section(PromptSection::dynamic_section(name, compute))
    }

    pub fn no_boundary(mut self) -> Self {
        self.include_boundary = false;
        self
    }

    pub fn no_context(mut self) -> Self {
        self.inject_context = false;
        self
    }

    pub fn build(self) -> PromptOrchestrator {
        let mut orchestrator = PromptOrchestrator::new(self.working_dir)
            .with_profile(self.profile)
            .with_boundary(self.include_boundary)
            .with_context_injection(self.inject_context);
        orchestrator.add_sections(self.sections);
        orchestrator
    }
}

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

    #[test]
    fn test_assemble_simple() {
        let mut orchestrator = PromptOrchestrator::new(std::env::current_dir().unwrap());
        orchestrator.add_section(PromptSection::static_section("identity", "You are an AI assistant."));
        
        let assembled = orchestrator.assemble();
        assert!(!assembled.prompt.is_empty());
        assert!(assembled.prompt.contains("identity"));
        assert!(assembled.cached_sections >= 1);
    }

    #[test]
    fn test_assemble_with_dynamic() {
        let mut orchestrator = PromptOrchestrator::new(std::env::current_dir().unwrap());
        orchestrator.add_section(PromptSection::static_section("identity", "You are an AI."));
        orchestrator.add_section(PromptSection::dynamic_section("date", || {
            format!("Current date: {}", chrono::Local::now().format("%Y-%m-%d"))
        }));
        
        let assembled = orchestrator.assemble();
        assert!(assembled.dynamic_sections >= 1);
        assert!(assembled.cached_sections >= 1);
    }

    #[test]
    fn test_cache_boundary() {
        let mut orchestrator = PromptOrchestrator::new(std::env::current_dir().unwrap())
            .with_boundary(true)
            .with_context_injection(false);
        
        orchestrator.add_section(PromptSection::static_section("cached", "cached content"));
        orchestrator.add_section(PromptSection::dynamic_section("dynamic", || "dynamic content".to_string()));
        
        let assembled = orchestrator.assemble();
        assert!(assembled.prompt.contains(CACHE_BOUNDARY));
        
        let (cached, dynamic) = assembled.split_at_boundary();
        assert!(cached.is_some());
        assert!(dynamic.is_some());
    }

    #[test]
    fn test_profile() {
        let orchestrator = PromptOrchestrator::new(std::env::current_dir().unwrap())
            .with_profile(PromptProfile::Fast);
        
        assert_eq!(orchestrator.profile, PromptProfile::Fast);
    }

    #[test]
    fn test_builder() {
        let mut orchestrator = PromptBuilder::new(std::env::current_dir().unwrap())
            .profile(PromptProfile::Review)
            .add_static("identity", "You are a code reviewer.")
            .add_dynamic("date", || "Today".to_string())
            .build();
        
        let assembled = orchestrator.assemble();
        assert!(assembled.prompt.contains("identity"));
    }

    #[test]
    fn test_cache_efficiency() {
        let mut orchestrator = PromptOrchestrator::new(std::env::current_dir().unwrap())
            .with_context_injection(false);
        
        // Add static content with words (to have proper token estimate)
        let static_content = "static content that should be cached properly test test test";
        orchestrator.add_section(PromptSection::static_section("big", static_content));
        orchestrator.add_section(PromptSection::dynamic_section("small", || "dynamic".to_string()));
        
        let assembled = orchestrator.assemble();
        let efficiency = assembled.cache_efficiency();
        
        // Static content should be cached
        assert!(efficiency >= 50.0, "Cache efficiency: {}", efficiency);
    }

    #[test]
    fn test_invalidate_cache() {
        let mut orchestrator = PromptOrchestrator::new(std::env::current_dir().unwrap());
        orchestrator.add_section(PromptSection::static_section("test", "test content"));
        
        // First assembly
        let _ = orchestrator.assemble();
        
        // Invalidate
        orchestrator.invalidate_cache();
        
        // Should recalculate
        let assembled = orchestrator.assemble();
        assert!(assembled.prompt.contains("test"));
    }
}