Skip to main content

vtcode_core/prompts/
temporal.rs

1use chrono::{DateTime, Local, Utc};
2
3/// Generate a cache-friendly date-only context for the system prompt.
4///
5/// Prompt caching is a prefix match: an in-depth per-second timestamp in the
6/// static prompt breaks the cache on every rebuild. The system prompt carries
7/// only the calendar date (stable all day); precise clock time belongs in a
8/// `<system-reminder>` message appended to the history when a task actually
9/// needs it.
10///
11/// # Examples
12/// ```
13/// use vtcode_core::prompts::temporal::generate_temporal_date_context;
14///
15/// let context = generate_temporal_date_context(false);
16/// assert!(context.contains("Current date:"));
17/// assert!(!context.contains(" at "));
18/// ```
19pub fn generate_temporal_date_context(use_utc: bool) -> String {
20    if use_utc {
21        let now: DateTime<Utc> = Utc::now();
22        format!("\n\nCurrent date (UTC): {}", now.format("%A, %B %d, %Y"))
23    } else {
24        let now: DateTime<Local> = Local::now();
25        format!("\n\nCurrent date: {}", now.format("%A, %B %d, %Y %Z"))
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_temporal_date_context_is_cache_friendly() {
35        let utc = generate_temporal_date_context(true);
36        assert!(utc.contains("Current date (UTC):"), "Should include UTC date label");
37        assert!(!utc.contains(" at "), "Date-only context must not carry clock time");
38        // Only the label colon may remain; HH:MM:SS clock time would add more.
39        assert_eq!(utc.matches(':').count(), 1, "Date-only context must not carry clock time");
40        let local = generate_temporal_date_context(false);
41        assert!(local.contains("Current date:"), "Should include date label");
42        assert!(!local.contains(" at "), "Date-only context must not carry clock time");
43        assert!(!local.contains("Current date and time"), "Must not use the per-second label");
44    }
45}