rustyclaw_core/
runtime_ctx.rs1use std::sync::{Arc, Mutex, OnceLock};
7
8#[derive(Debug, Clone, Default)]
10pub struct RuntimeInfo {
11 pub provider: Option<String>,
12 pub model: Option<String>,
13 pub base_url: Option<String>,
14}
15
16pub type SharedRuntimeCtx = Arc<Mutex<RuntimeInfo>>;
18
19static RUNTIME_CTX: OnceLock<SharedRuntimeCtx> = OnceLock::new();
21
22pub fn runtime_ctx() -> &'static SharedRuntimeCtx {
24 RUNTIME_CTX.get_or_init(|| Arc::new(Mutex::new(RuntimeInfo::default())))
25}
26
27pub fn set_model_info(provider: &str, model: &str, base_url: &str) {
29 if let Ok(mut ctx) = runtime_ctx().lock() {
30 ctx.provider = Some(provider.to_string());
31 ctx.model = Some(model.to_string());
32 ctx.base_url = Some(base_url.to_string());
33 }
34}
35
36pub fn get_model_info() -> Option<(String, String, String)> {
38 runtime_ctx()
39 .lock()
40 .ok()
41 .and_then(|ctx| {
42 Some((
43 ctx.provider.clone()?,
44 ctx.model.clone()?,
45 ctx.base_url.clone()?,
46 ))
47 })
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_runtime_ctx() {
56 set_model_info("github-copilot", "claude-sonnet-4", "https://api.githubcopilot.com");
57 let info = get_model_info();
58 assert!(info.is_some());
59 let (provider, model, base_url) = info.unwrap();
60 assert_eq!(provider, "github-copilot");
61 assert_eq!(model, "claude-sonnet-4");
62 assert_eq!(base_url, "https://api.githubcopilot.com");
63 }
64}