use std::collections::HashMap;
use langfuse_core::error::LangfuseError;
#[derive(Debug, Clone)]
pub struct TextPromptClient {
pub name: String,
pub version: i32,
pub template: String,
pub config: serde_json::Value,
pub labels: Vec<String>,
pub tags: Vec<String>,
pub is_fallback: bool,
}
impl TextPromptClient {
pub fn compile(&self, variables: &HashMap<String, String>) -> Result<String, LangfuseError> {
compile_template(&self.template, variables)
}
}
pub(crate) fn compile_template(
template: &str,
variables: &HashMap<String, String>,
) -> Result<String, LangfuseError> {
let mut result = String::with_capacity(template.len());
let mut rest = template;
while let Some(start) = rest.find("{{") {
result.push_str(&rest[..start]);
let after_open = &rest[start + 2..];
let end = after_open
.find("}}")
.ok_or_else(|| LangfuseError::PromptCompilation {
variable: "unclosed {{".into(),
})?;
let var_name = after_open[..end].trim();
let value = variables
.get(var_name)
.ok_or_else(|| LangfuseError::PromptCompilation {
variable: var_name.to_owned(),
})?;
result.push_str(value);
rest = &after_open[end + 2..];
}
result.push_str(rest);
Ok(result)
}