use std::collections::HashMap;
use crate::baml_client::baml_source_map::get_baml_files;
use std::sync::OnceLock;
#[derive(Debug, Clone)]
pub struct FunctionOptions {
env: Option<HashMap<String, String>>,
tags: Option<HashMap<String, String>>,
type_builder: Option<super::type_builder::TypeBuilder>,
collectors: Option<Vec<baml::Collector>>,
client: Option<String>,
client_registry: Option<baml::ClientRegistry>,
cancellation_token: Option<baml::CancellationToken>,
}
impl Default for FunctionOptions {
fn default() -> Self {
Self::new()
}
}
impl FunctionOptions {
pub const fn new() -> Self {
Self {
env: None,
tags: None,
type_builder: None,
collectors: None,
client: None,
client_registry: None,
cancellation_token: None,
}
}
pub fn reset(self) -> Self {
Self::default()
}
pub fn with_env(mut self, env: HashMap<String, String>) -> Self {
self.env = Some(env);
self
}
pub fn with_env_var(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.env
.get_or_insert_with(HashMap::new)
.insert(key.into(), value.into());
self
}
pub fn with_tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.tags
.get_or_insert_with(HashMap::new)
.insert(key.into(), value.into());
self
}
pub fn with_type_builder(mut self, tb: &super::type_builder::TypeBuilder) -> Self {
self.type_builder = Some(tb.clone());
self
}
pub fn with_collector(mut self, collector: &baml::Collector) -> Self {
self.collectors
.get_or_insert_with(Vec::new)
.push(collector.clone());
self
}
pub fn with_collectors(mut self, collectors: &[baml::Collector]) -> Self {
self.collectors
.get_or_insert_with(Vec::new)
.extend(collectors.iter().map(|c| c.clone()));
self
}
pub fn with_cancellation_token(mut self, token: Option<baml::CancellationToken>) -> Self {
self.cancellation_token = token;
self
}
pub fn with_client(mut self, client_name: impl Into<String>) -> Self {
self.client = Some(client_name.into());
self
}
pub fn with_client_registry(mut self, registry: &baml::ClientRegistry) -> Self {
self.client_registry = Some(registry.clone());
self
}
pub(super) fn to_baml_args(&self) -> baml::FunctionArgs {
let mut args = baml::FunctionArgs::new();
for (env, values) in std::env::vars() {
args = args.with_env(env.as_str(), values.as_str());
}
if let Some(env) = &self.env {
for (key, value) in env {
args = args.with_env(key, value);
}
}
if let Some(tags) = &self.tags {
for (key, value) in tags {
args = args.with_tag(key, value);
}
}
if let Some(type_builder) = &self.type_builder {
args = args.with_type_builder(type_builder.inner());
}
if let Some(collectors) = &self.collectors {
for collector in collectors {
args = args.with_collector(collector);
}
}
let effective_registry = if let Some(client_name) = &self.client {
let mut registry = self
.client_registry
.clone()
.unwrap_or_else(baml::ClientRegistry::new);
registry.set_primary_client(client_name);
Some(registry)
} else {
self.client_registry.clone()
};
if let Some(registry) = &effective_registry {
args = args.with_client_registry(registry);
}
if let Some(cancellation_token) = &self.cancellation_token {
args = args.with_cancellation_token(Some(cancellation_token.clone()));
}
args
}
}
static RUNTIME: OnceLock<baml::BamlRuntime> = OnceLock::new();
pub(super) fn get_runtime() -> &'static baml::BamlRuntime {
RUNTIME.get_or_init(|| {
baml::BamlRuntime::new(
std::env::current_dir().unwrap().to_str().unwrap(),
get_baml_files(),
&std::env::vars().collect(),
)
.unwrap()
})
}
pub fn new_image_from_url(url: &str, mime_type: Option<&str>) -> baml::Image {
get_runtime().new_image_from_url(url, mime_type)
}
pub fn new_image_from_base64(base64: &str, mime_type: Option<&str>) -> baml::Image {
get_runtime().new_image_from_base64(base64, mime_type)
}
pub fn new_audio_from_url(url: &str, mime_type: Option<&str>) -> baml::Audio {
get_runtime().new_audio_from_url(url, mime_type)
}
pub fn new_audio_from_base64(base64: &str, mime_type: Option<&str>) -> baml::Audio {
get_runtime().new_audio_from_base64(base64, mime_type)
}
pub fn new_pdf_from_url(url: &str, mime_type: Option<&str>) -> baml::Pdf {
get_runtime().new_pdf_from_url(url, mime_type)
}
pub fn new_pdf_from_base64(base64: &str, mime_type: Option<&str>) -> baml::Pdf {
get_runtime().new_pdf_from_base64(base64, mime_type)
}
pub fn new_video_from_url(url: &str, mime_type: Option<&str>) -> baml::Video {
get_runtime().new_video_from_url(url, mime_type)
}
pub fn new_video_from_base64(base64: &str, mime_type: Option<&str>) -> baml::Video {
get_runtime().new_video_from_base64(base64, mime_type)
}
pub fn new_collector(name: &str) -> baml::Collector {
get_runtime().new_collector(name)
}