chidori_prompt_format/
lib.rs1pub mod templating;
2mod utils;
3use handlebars::Template;
4use lazy_static::lazy_static;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::collections::HashMap;
8use std::sync::Mutex;
9use wasm_bindgen::prelude::*;
10pub use serde_json;
11
12use crate::templating::templates::{ChatModelRoles, PromptLibraryRecord, TemplateWithSource};
13use wasm_bindgen::prelude::*;
14
15#[wasm_bindgen]
16extern "C" {
17 }
20
21#[wasm_bindgen]
22pub fn render_template_prompt(
23 template_str: &str,
24 json_value: JsValue,
25 ) -> Result<JsValue, JsValue> {
27 let json_value: Value = serde_wasm_bindgen::from_value(json_value)
28 .map_err(|e| JsValue::from_str(&e.to_string()))?;
29
30 let partials: HashMap<String, PromptLibraryRecord> = HashMap::new();
34
35 let result =
36 crate::templating::templates::render_template_prompt(template_str, &json_value, &partials)
37 .map_err(|e| JsValue::from_str(&e.to_string()))?;
38
39 serde_wasm_bindgen::to_value(&result).map_err(|e| JsValue::from_str(&e.to_string()))
40}
41
42#[derive(Serialize, Deserialize, Debug)]
43struct TemplateWithRole {
44 role: ChatModelRoles,
45 source: String,
46}
47
48#[wasm_bindgen]
49pub fn extract_roles_from_template(template: &str) -> JsValue {
50 let mut role_blocks = crate::templating::templates::extract_roles_from_template(&template);
51 let templates_with_roles: Vec<TemplateWithRole> = role_blocks
52 .into_iter()
53 .map(|(a, b)| TemplateWithRole {
54 role: a,
55 source: b.unwrap().source.to_string(),
56 })
57 .collect();
58 serde_wasm_bindgen::to_value(&templates_with_roles)
59 .map_err(|e| JsValue::from_str(&e.to_string()))
60 .unwrap()
61}
62
63pub fn extract_yaml_frontmatter_string(template: &str) -> (HashMap<String, String>, String) {
64 let result = crate::templating::templates::split_frontmatter(&template).unwrap();
65 if result.0.is_empty() {
66 return (HashMap::new(), result.1);
67 }
68 let deserialized_data: HashMap<String, String> = serde_yaml::from_str(&result.0).unwrap();
69 (deserialized_data, result.1)
70}
71
72#[wasm_bindgen]
73pub fn extract_yaml_frontmatter(template: &str) -> JsValue {
74 let x = extract_yaml_frontmatter_string(&template);
75 serde_wasm_bindgen::to_value(&x)
76 .map_err(|e| JsValue::from_str(&e.to_string()))
77 .unwrap()
78}
79
80#[wasm_bindgen]
81pub fn analyze_referenced_partials(template: &str) -> JsValue {
82 let schema = crate::templating::templates::analyze_referenced_partials(&template).unwrap();
83 serde_wasm_bindgen::to_value(&schema)
84 .map_err(|e| JsValue::from_str(&e.to_string()))
85 .unwrap()
86}