Skip to main content

rustyclaw_core/
runtime_ctx.rs

1//! Global runtime context for tool access.
2//!
3//! This module provides a global store for runtime information that tools
4//! need to access, such as the current model context.
5
6use std::sync::{Arc, Mutex, OnceLock};
7
8/// Model information available to tools.
9#[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
16/// Shared runtime context.
17pub type SharedRuntimeCtx = Arc<Mutex<RuntimeInfo>>;
18
19/// Global runtime context instance.
20static RUNTIME_CTX: OnceLock<SharedRuntimeCtx> = OnceLock::new();
21
22/// Get the global runtime context.
23pub fn runtime_ctx() -> &'static SharedRuntimeCtx {
24    RUNTIME_CTX.get_or_init(|| Arc::new(Mutex::new(RuntimeInfo::default())))
25}
26
27/// Update the runtime context with model information.
28pub 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
36/// Get current model information.
37pub 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}