vtcode-core 0.185.1

Core library for VT Code - a Rust-based terminal coding agent
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
//! Shared prompt resource discovery for system prompt layers and prompt templates.

use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use std::sync::{OnceLock, RwLock};
use std::time::{Duration, SystemTime};

use serde::Deserialize;
use tokio::fs;
use tracing::warn;
use vtcode_commons::VtCodePaths;

const PROMPTS_DIR: &str = ".vtcode/prompts";
const TEMPLATES_DIR: &str = "templates";
const SYSTEM_PROMPT_FILENAME: &str = "system.md";
const APPEND_SYSTEM_PROMPT_FILENAME: &str = "append-system.md";
const PROMPT_RESOURCE_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
const PROMPT_RESOURCE_CACHE_MAX_ENTRIES: usize = 32;

static SYSTEM_PROMPT_LAYERS_CACHE: OnceLock<RwLock<HashMap<PromptResourceCacheKey, CachedSystemPromptLayers>>> =
    OnceLock::new();
static PROMPT_TEMPLATES_CACHE: OnceLock<RwLock<HashMap<PromptResourceCacheKey, CachedPromptTemplates>>> =
    OnceLock::new();

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PromptTemplate {
    pub name: String,
    pub description: String,
    pub body: String,
    pub path: PathBuf,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SystemPromptLayers {
    pub override_body: Option<String>,
    pub append_bodies: Vec<String>,
}

#[derive(Debug, Clone, Copy)]
enum PromptResourceScope {
    User,
    Workspace,
}

#[derive(Debug, Clone)]
struct PromptResourceOptions<'a> {
    workspace_root: &'a Path,
    home_dir: Option<PathBuf>,
}

#[derive(Debug, Clone, Default, Deserialize)]
struct PromptTemplateFrontmatter {
    description: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct PromptResourceCacheKey {
    workspace_root: PathBuf,
    home_dir: Option<PathBuf>,
}

impl PromptResourceCacheKey {
    fn new(options: &PromptResourceOptions<'_>) -> Self {
        Self {
            workspace_root: normalize_cache_path(options.workspace_root),
            home_dir: options.home_dir.as_deref().map(normalize_cache_path),
        }
    }
}

#[derive(Clone)]
struct CachedPromptTemplates {
    templates: Vec<PromptTemplate>,
    timestamp: SystemTime,
    source_mtime: SystemTime,
    source_paths: (PathBuf, Option<PathBuf>),
}

impl CachedPromptTemplates {
    fn is_expired(&self) -> bool {
        self.timestamp.elapsed().unwrap_or(PROMPT_RESOURCE_CACHE_TTL) > PROMPT_RESOURCE_CACHE_TTL
    }

    fn is_source_stale(&self) -> bool {
        let mut check: Vec<&Path> = vec![self.source_paths.0.as_path()];
        if let Some(ref h) = self.source_paths.1 {
            check.push(h.as_path());
        }
        newest_mtime(check) > self.source_mtime
    }
}

#[derive(Clone)]
struct CachedSystemPromptLayers {
    layers: SystemPromptLayers,
    timestamp: SystemTime,
    source_mtime: SystemTime,
}

impl CachedSystemPromptLayers {
    fn is_expired(&self) -> bool {
        self.timestamp.elapsed().unwrap_or(PROMPT_RESOURCE_CACHE_TTL) > PROMPT_RESOURCE_CACHE_TTL
    }

    fn is_source_stale(&self, check_paths: &[&Path]) -> bool {
        newest_mtime(check_paths.iter().copied()) > self.source_mtime
    }
}

fn normalize_cache_path(path: &Path) -> PathBuf {
    dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
}

pub(crate) fn user_prompts_dir(home: &Path) -> PathBuf {
    if dirs::home_dir().as_deref() == Some(home)
        && let Ok(paths) = VtCodePaths::resolve()
        && let Ok(prompt_dir) = paths.config_path("prompts")
    {
        return prompt_dir;
    }
    home.join(PROMPTS_DIR)
}

/// Newest mtime across `paths`; missing or unreadable paths are ignored
/// (they contribute `UNIX_EPOCH`, never the maximum).
fn newest_mtime<'a>(paths: impl IntoIterator<Item = &'a Path>) -> SystemTime {
    let mut newest = SystemTime::UNIX_EPOCH;
    for path in paths {
        if let Ok(metadata) = std::fs::metadata(path) {
            if let Ok(mtime) = metadata.modified() {
                newest = newest.max(mtime);
            }
        }
    }
    newest
}

fn system_prompt_layers_cache() -> &'static RwLock<HashMap<PromptResourceCacheKey, CachedSystemPromptLayers>> {
    SYSTEM_PROMPT_LAYERS_CACHE.get_or_init(|| RwLock::new(HashMap::new()))
}

fn prompt_templates_cache() -> &'static RwLock<HashMap<PromptResourceCacheKey, CachedPromptTemplates>> {
    PROMPT_TEMPLATES_CACHE.get_or_init(|| RwLock::new(HashMap::new()))
}

/// Entry contract for the bounded prompt-resource caches: expiry drives
/// reads, and expiry plus insertion age drive eviction.
trait ExpirableCacheEntry {
    fn is_expired(&self) -> bool;
    fn timestamp(&self) -> SystemTime;
}

impl ExpirableCacheEntry for CachedSystemPromptLayers {
    fn is_expired(&self) -> bool {
        self.is_expired()
    }

    fn timestamp(&self) -> SystemTime {
        self.timestamp
    }
}

impl ExpirableCacheEntry for CachedPromptTemplates {
    fn is_expired(&self) -> bool {
        self.is_expired()
    }

    fn timestamp(&self) -> SystemTime {
        self.timestamp
    }
}

/// Read-through guard shared by both caches: expired entries miss.
fn get_cached<K, V>(cache: &RwLock<HashMap<K, V>>, key: &K, what: &str) -> Option<V>
where
    K: Eq + std::hash::Hash,
    V: ExpirableCacheEntry + Clone,
{
    match cache.read() {
        Ok(cache) => cache.get(key).filter(|cached| !cached.is_expired()).cloned(),
        Err(_) => {
            warn!("{what} cache lock poisoned while reading cache");
            None
        }
    }
}

/// Make room for `key` when the cache is full: purge expired entries first,
/// then evict the oldest. No-op when under budget or when `key` is present.
fn evict_if_full<K, V>(cache: &mut HashMap<K, V>, key: &K)
where
    K: Eq + std::hash::Hash + Clone,
    V: ExpirableCacheEntry,
{
    if cache.len() < PROMPT_RESOURCE_CACHE_MAX_ENTRIES || cache.contains_key(key) {
        return;
    }
    let expired: Vec<K> = cache
        .iter()
        .filter(|(_, value)| value.is_expired())
        .map(|(cache_key, _)| cache_key.clone())
        .collect();
    for cache_key in expired {
        cache.remove(&cache_key);
    }

    if cache.len() >= PROMPT_RESOURCE_CACHE_MAX_ENTRIES {
        let oldest_key = cache
            .iter()
            .min_by_key(|(_, value)| value.timestamp())
            .map(|(cache_key, _)| cache_key.clone());
        if let Some(oldest_key) = oldest_key {
            cache.remove(&oldest_key);
        }
    }
}

fn get_cached_system_prompt_layers(key: &PromptResourceCacheKey) -> Option<CachedSystemPromptLayers> {
    get_cached(system_prompt_layers_cache(), key, "system prompt layers")
}

fn cache_system_prompt_layers(key: PromptResourceCacheKey, layers: &SystemPromptLayers) {
    match system_prompt_layers_cache().write() {
        Ok(mut cache) => {
            evict_if_full(&mut cache, &key);

            let mut source_paths = vec![
                key.workspace_root.join(PROMPTS_DIR).join(SYSTEM_PROMPT_FILENAME),
                key.workspace_root.join(PROMPTS_DIR).join(APPEND_SYSTEM_PROMPT_FILENAME),
            ];
            if let Some(ref home) = key.home_dir {
                let prompt_dir = user_prompts_dir(home);
                source_paths.push(prompt_dir.join(SYSTEM_PROMPT_FILENAME));
                source_paths.push(prompt_dir.join(APPEND_SYSTEM_PROMPT_FILENAME));
            }
            let source_mtime = newest_mtime(source_paths.iter().map(PathBuf::as_path));

            cache.insert(
                key,
                CachedSystemPromptLayers {
                    layers: layers.clone(),
                    timestamp: SystemTime::now(),
                    source_mtime,
                },
            );
        }
        Err(_) => warn!("system prompt layers cache lock poisoned while writing cache"),
    }
}

fn get_cached_prompt_templates(key: &PromptResourceCacheKey) -> Option<CachedPromptTemplates> {
    get_cached(prompt_templates_cache(), key, "prompt templates")
}

fn cache_prompt_templates(key: PromptResourceCacheKey, templates: &[PromptTemplate]) {
    match prompt_templates_cache().write() {
        Ok(mut cache) => {
            evict_if_full(&mut cache, &key);

            let source_paths = (
                key.workspace_root.join(PROMPTS_DIR).join(TEMPLATES_DIR),
                key.home_dir.as_ref().map(|h| user_prompts_dir(h).join(TEMPLATES_DIR)),
            );
            let mut mtime_paths = vec![source_paths.0.clone()];
            if let Some(ref p) = source_paths.1 {
                mtime_paths.push(p.clone());
            }
            let source_mtime = newest_mtime(mtime_paths.iter().map(PathBuf::as_path));

            cache.insert(
                key,
                CachedPromptTemplates {
                    templates: templates.to_vec(),
                    timestamp: SystemTime::now(),
                    source_mtime,
                    source_paths,
                },
            );
        }
        Err(_) => warn!("prompt templates cache lock poisoned while writing cache"),
    }
}

#[cfg(test)]
fn clear_prompt_resource_caches() {
    if let Ok(mut cache) = system_prompt_layers_cache().write() {
        cache.clear();
    }
    if let Ok(mut cache) = prompt_templates_cache().write() {
        cache.clear();
    }
}

pub async fn resolve_system_prompt_layers(workspace_root: &Path) -> SystemPromptLayers {
    resolve_system_prompt_layers_with_options(PromptResourceOptions::new(workspace_root)).await
}

pub async fn discover_prompt_templates(workspace_root: &Path) -> Vec<PromptTemplate> {
    discover_prompt_templates_with_options(PromptResourceOptions::new(workspace_root)).await
}

pub async fn find_prompt_template(workspace_root: &Path, name: &str) -> Option<PromptTemplate> {
    let normalized = name.trim();
    if normalized.is_empty() {
        return None;
    }

    find_prompt_template_with_options(PromptResourceOptions::new(workspace_root), normalized).await
}

impl SystemPromptLayers {
    /// Override body that reaches the final prompt: trimmed and non-empty.
    fn effective_override_body(&self) -> Option<&str> {
        self.override_body.as_deref().map(str::trim).filter(|body| !body.is_empty())
    }

    /// Append bodies that reach the final prompt: trimmed, non-empty, in order.
    fn effective_append_bodies(&self) -> impl Iterator<Item = &str> {
        self.append_bodies
            .iter()
            .map(|body| body.trim())
            .filter(|body| !body.is_empty())
    }

    /// Exact layer text spliced into the composed prompt: the effective
    /// override (if any) plus every effective append body, joined the way
    /// [`apply_system_prompt_layers`] splices them. Empty when no layer file
    /// contributes. Token accounting measures this string so the figure tracks
    /// what the model sees, modulo separators and later identity substitutions.
    pub fn applied_text(&self) -> String {
        let mut text = String::new();
        let bodies = self.effective_override_body().into_iter().chain(self.effective_append_bodies());
        for body in bodies {
            if !text.is_empty() {
                text.push_str("\n\n");
            }
            text.push_str(body);
        }
        text
    }
}

pub fn apply_system_prompt_layers(base_prompt: &str, layers: &SystemPromptLayers) -> String {
    let mut prompt = String::new();

    if let Some(override_body) = layers.effective_override_body() {
        prompt.push_str(override_body);
    } else {
        prompt.push_str(base_prompt);
    }

    for append_body in layers.effective_append_bodies() {
        if !prompt.is_empty() {
            prompt.push_str("\n\n");
        }
        prompt.push_str(append_body);
    }

    prompt
}

pub fn expand_prompt_template(body: &str, args: &[String]) -> String {
    let joined_args = args.join(" ");
    let mut expanded = String::with_capacity(body.len() + joined_args.len());
    let chars: Vec<char> = body.chars().collect();
    let mut index = 0;

    while index < chars.len() {
        if chars[index] != '$' {
            expanded.push(chars[index]);
            index += 1;
            continue;
        }

        if index + 1 >= chars.len() {
            expanded.push('$');
            index += 1;
            continue;
        }

        match chars[index + 1] {
            '@' => {
                expanded.push_str(&joined_args);
                index += 2;
            }
            'A' => {
                const ARGUMENTS_TOKEN: &str = "ARGUMENTS";
                let remaining: String = chars[index + 1..].iter().collect();
                if remaining.starts_with(ARGUMENTS_TOKEN) {
                    expanded.push_str(&joined_args);
                    index += ARGUMENTS_TOKEN.chars().count() + 1;
                } else {
                    expanded.push('$');
                    index += 1;
                }
            }
            digit if digit.is_ascii_digit() => {
                let mut cursor = index + 1;
                while cursor < chars.len() && chars[cursor].is_ascii_digit() {
                    cursor += 1;
                }
                let ordinal: String = chars[index + 1..cursor].iter().collect();
                let replacement = ordinal
                    .parse::<usize>()
                    .ok()
                    .and_then(|value| value.checked_sub(1))
                    .and_then(|position| args.get(position))
                    .map(String::as_str)
                    .unwrap_or("");
                expanded.push_str(replacement);
                index = cursor;
            }
            _ => {
                expanded.push('$');
                index += 1;
            }
        }
    }

    expanded
}

impl<'a> PromptResourceOptions<'a> {
    fn new(workspace_root: &'a Path) -> Self {
        #[cfg(test)]
        let home_dir = None;

        #[cfg(not(test))]
        let home_dir = dirs::home_dir();

        Self { workspace_root, home_dir }
    }
}

async fn resolve_system_prompt_layers_with_options(options: PromptResourceOptions<'_>) -> SystemPromptLayers {
    let cache_key = PromptResourceCacheKey::new(&options);
    if let Some(cached) = get_cached_system_prompt_layers(&cache_key) {
        let check_paths = [
            options.workspace_root.join(PROMPTS_DIR).join(SYSTEM_PROMPT_FILENAME),
            options
                .home_dir
                .as_ref()
                .map(|h| user_prompts_dir(h).join(SYSTEM_PROMPT_FILENAME))
                .unwrap_or_default(),
            options.workspace_root.join(PROMPTS_DIR).join(APPEND_SYSTEM_PROMPT_FILENAME),
            options
                .home_dir
                .as_ref()
                .map(|h| user_prompts_dir(h).join(APPEND_SYSTEM_PROMPT_FILENAME))
                .unwrap_or_default(),
        ];
        let check_refs: Vec<_> = check_paths.iter().map(|p| p.as_path()).collect();
        if !cached.is_source_stale(&check_refs) {
            return cached.layers;
        }
    }

    let layers = resolve_system_prompt_layers_uncached(&options).await;
    cache_system_prompt_layers(cache_key, &layers);
    layers
}

async fn resolve_system_prompt_layers_uncached(options: &PromptResourceOptions<'_>) -> SystemPromptLayers {
    let mut layers = SystemPromptLayers::default();

    let user_system_path = options
        .home_dir
        .as_ref()
        .map(|home| user_prompts_dir(home).join(SYSTEM_PROMPT_FILENAME));
    let workspace_system_path = options.workspace_root.join(PROMPTS_DIR).join(SYSTEM_PROMPT_FILENAME);

    if let Some(path) = user_system_path.as_ref() {
        layers.override_body = read_optional_markdown(path).await;
    }

    if let Some(workspace_override) = read_optional_markdown(&workspace_system_path).await {
        layers.override_body = Some(workspace_override);
    }

    if let Some(path) = options
        .home_dir
        .as_ref()
        .map(|home| user_prompts_dir(home).join(APPEND_SYSTEM_PROMPT_FILENAME))
        && let Some(contents) = read_optional_markdown(&path).await
    {
        layers.append_bodies.push(contents);
    }

    let workspace_append = options.workspace_root.join(PROMPTS_DIR).join(APPEND_SYSTEM_PROMPT_FILENAME);
    if let Some(contents) = read_optional_markdown(&workspace_append).await {
        layers.append_bodies.push(contents);
    }

    layers
}

async fn discover_prompt_templates_with_options(options: PromptResourceOptions<'_>) -> Vec<PromptTemplate> {
    let cache_key = PromptResourceCacheKey::new(&options);
    if let Some(cached) = get_cached_prompt_templates(&cache_key) {
        if !cached.is_source_stale() {
            return cached.templates;
        }
    }

    let templates = discover_prompt_templates_uncached(&options).await;
    cache_prompt_templates(cache_key, &templates);
    templates
}

async fn discover_prompt_templates_uncached(options: &PromptResourceOptions<'_>) -> Vec<PromptTemplate> {
    let mut discovered = BTreeMap::new();

    if let Some(home) = options.home_dir.as_deref() {
        let user_templates = user_prompts_dir(home).join(TEMPLATES_DIR);
        merge_prompt_templates(&mut discovered, &user_templates, PromptResourceScope::User).await;
    }

    let workspace_templates = options.workspace_root.join(PROMPTS_DIR).join(TEMPLATES_DIR);
    merge_prompt_templates(&mut discovered, &workspace_templates, PromptResourceScope::Workspace).await;

    discovered.into_values().collect()
}

async fn find_prompt_template_with_options(options: PromptResourceOptions<'_>, name: &str) -> Option<PromptTemplate> {
    if !is_safe_template_name(name) {
        return None;
    }

    discover_prompt_templates_with_options(options)
        .await
        .into_iter()
        .find(|template| template.name == name)
}

async fn merge_prompt_templates(
    discovered: &mut BTreeMap<String, PromptTemplate>,
    directory: &Path,
    scope: PromptResourceScope,
) {
    let Ok(mut entries) = fs::read_dir(directory).await else {
        return;
    };

    let mut markdown_files = Vec::new();
    loop {
        match entries.next_entry().await {
            Ok(Some(entry)) => {
                let path = entry.path();
                if path.extension().and_then(|ext| ext.to_str()) == Some("md") {
                    markdown_files.push(path);
                }
            }
            Ok(None) => break,
            Err(err) => {
                warn!("failed to read prompt templates directory {}: {}", directory.display(), err);
                break;
            }
        }
    }

    markdown_files.sort();

    for path in markdown_files {
        let Some(name) = path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .map(str::trim)
            .filter(|stem| !stem.is_empty())
            .map(str::to_string)
        else {
            continue;
        };

        match load_prompt_template(&path, name.clone()).await {
            Some(template) => {
                if matches!(scope, PromptResourceScope::Workspace) {
                    discovered.insert(name, template);
                } else {
                    discovered.entry(name).or_insert(template);
                }
            }
            None => continue,
        }
    }
}

async fn load_prompt_template(path: &Path, name: String) -> Option<PromptTemplate> {
    let raw = read_optional_markdown(path).await?;
    let normalized = normalize_newlines(&raw);
    let (frontmatter, body) = parse_frontmatter(&normalized);
    let description = frontmatter
        .description
        .filter(|value| !value.trim().is_empty())
        .unwrap_or_else(|| derive_template_description(&body, &name));

    Some(PromptTemplate {
        name,
        description,
        body: body.trim().to_string(),
        path: path.to_path_buf(),
    })
}

async fn read_optional_markdown(path: &Path) -> Option<String> {
    match fs::read_to_string(path).await {
        Ok(contents) => Some(contents),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
        Err(err) => {
            warn!("failed to read prompt resource {}: {}", path.display(), err);
            None
        }
    }
}

fn parse_frontmatter(content: &str) -> (PromptTemplateFrontmatter, String) {
    if !content.starts_with("---\n") {
        return (PromptTemplateFrontmatter::default(), content.to_string());
    }

    let Some(frontmatter_end) = content[4..].find("\n---\n").map(|idx| idx + 4) else {
        return (PromptTemplateFrontmatter::default(), content.to_string());
    };

    let yaml = &content[4..frontmatter_end];
    let body_start = frontmatter_end + 5;
    let body = if body_start < content.len() {
        content[body_start..].to_string()
    } else {
        String::new()
    };

    let metadata = match serde_saphyr::from_str::<PromptTemplateFrontmatter>(yaml.trim()) {
        Ok(value) => value,
        Err(err) => {
            warn!("failed to parse prompt template frontmatter: {}", err);
            PromptTemplateFrontmatter::default()
        }
    };

    (metadata, body)
}

fn derive_template_description(body: &str, name: &str) -> String {
    for line in body.lines().map(str::trim) {
        if line.is_empty() {
            continue;
        }
        if let Some(heading) = line.strip_prefix('#') {
            let trimmed = heading.trim_start_matches('#').trim();
            if !trimmed.is_empty() {
                return trimmed.to_string();
            }
        }
        return line.to_string();
    }

    format!("Prompt template `{name}`")
}

fn normalize_newlines(content: &str) -> String {
    content.replace("\r\n", "\n")
}

fn is_safe_template_name(name: &str) -> bool {
    !name.is_empty() && !name.contains('/') && !name.contains('\\') && !name.contains("..")
}

#[cfg(test)]
mod tests;