1use anyhow::Result;
 2use gpui::SharedString;
 3use handlebars::Handlebars;
 4use rust_embed::RustEmbed;
 5use serde::Serialize;
 6use std::sync::Arc;
 7
 8#[derive(RustEmbed)]
 9#[folder = "src/templates"]
10#[include = "*.hbs"]
11struct Assets;
12
13pub struct Templates(Handlebars<'static>);
14
15impl Templates {
16    pub fn new() -> Arc<Self> {
17        let mut handlebars = Handlebars::new();
18        handlebars.set_strict_mode(true);
19        handlebars.register_helper("contains", Box::new(contains));
20        handlebars.register_embed_templates::<Assets>().unwrap();
21        Arc::new(Self(handlebars))
22    }
23}
24
25pub trait Template: Sized {
26    const TEMPLATE_NAME: &'static str;
27
28    fn render(&self, templates: &Templates) -> Result<String>
29    where
30        Self: Serialize + Sized,
31    {
32        Ok(templates.0.render(Self::TEMPLATE_NAME, self)?)
33    }
34}
35
36#[derive(Serialize)]
37pub struct SystemPromptTemplate<'a> {
38    #[serde(flatten)]
39    pub project: &'a prompt_store::ProjectContext,
40    pub available_tools: Vec<SharedString>,
41}
42
43impl Template for SystemPromptTemplate<'_> {
44    const TEMPLATE_NAME: &'static str = "system_prompt.hbs";
45}
46
47/// Handlebars helper for checking if an item is in a list
48fn contains(
49    h: &handlebars::Helper,
50    _: &handlebars::Handlebars,
51    _: &handlebars::Context,
52    _: &mut handlebars::RenderContext,
53    out: &mut dyn handlebars::Output,
54) -> handlebars::HelperResult {
55    let list = h
56        .param(0)
57        .and_then(|v| v.value().as_array())
58        .ok_or_else(|| {
59            handlebars::RenderError::new("contains: missing or invalid list parameter")
60        })?;
61    let query = h.param(1).map(|v| v.value()).ok_or_else(|| {
62        handlebars::RenderError::new("contains: missing or invalid query parameter")
63    })?;
64
65    if list.contains(query) {
66        out.write("true")?;
67    }
68
69    Ok(())
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_system_prompt_template() {
78        let project = prompt_store::ProjectContext::default();
79        let template = SystemPromptTemplate {
80            project: &project,
81            available_tools: vec!["echo".into()],
82        };
83        let templates = Templates::new();
84        let rendered = template.render(&templates).unwrap();
85        assert!(rendered.contains("## Fixing Diagnostics"));
86    }
87}