Skip to main content

lash_protocol_rlm/plugin/
factory.rs

1use std::sync::{Arc, RwLock};
2
3use lash_core::plugin::{
4    PluginError, PluginFactory, PluginRegistrar, PluginSessionContext, SessionPlugin,
5};
6
7use super::registration::register_rlm_protocol_plugin;
8use super::{RLM_PROTOCOL_PLUGIN_ID, RlmProtocolPluginConfig};
9use crate::driver::SharedPromptUsage;
10use crate::projection::{ProjectionRegistry, ProjectionResolver};
11
12pub struct RlmProtocolPluginFactory {
13    config: RlmProtocolPluginConfig,
14    projection_resolver: Arc<dyn ProjectionResolver>,
15}
16
17impl RlmProtocolPluginFactory {
18    pub fn new(config: RlmProtocolPluginConfig) -> Self {
19        Self {
20            config,
21            projection_resolver: Arc::new(ProjectionRegistry::default()),
22        }
23    }
24
25    pub fn with_projection_resolver(
26        mut self,
27        projection_resolver: Arc<dyn ProjectionResolver>,
28    ) -> Self {
29        self.projection_resolver = projection_resolver;
30        self
31    }
32}
33
34impl Default for RlmProtocolPluginFactory {
35    fn default() -> Self {
36        Self::new(RlmProtocolPluginConfig::default())
37    }
38}
39
40impl PluginFactory for RlmProtocolPluginFactory {
41    fn id(&self) -> &'static str {
42        RLM_PROTOCOL_PLUGIN_ID
43    }
44
45    fn lashlang_abilities(&self) -> lashlang::LashlangAbilities {
46        self.config.lashlang_abilities
47    }
48
49    fn lashlang_language_features(&self) -> lashlang::LashlangLanguageFeatures {
50        self.config.lashlang_language_features
51    }
52
53    fn build(&self, _ctx: &PluginSessionContext) -> Result<Arc<dyn SessionPlugin>, PluginError> {
54        Ok(Arc::new(RlmProtocolPlugin {
55            config: self.config.clone(),
56            projection_resolver: Arc::clone(&self.projection_resolver),
57            last_prompt_usage: Arc::new(RwLock::new(None)),
58        }))
59    }
60}
61
62struct RlmProtocolPlugin {
63    config: RlmProtocolPluginConfig,
64    projection_resolver: Arc<dyn ProjectionResolver>,
65    last_prompt_usage: SharedPromptUsage,
66}
67
68impl SessionPlugin for RlmProtocolPlugin {
69    fn id(&self) -> &'static str {
70        RLM_PROTOCOL_PLUGIN_ID
71    }
72
73    fn register(&self, reg: &mut PluginRegistrar) -> Result<(), PluginError> {
74        register_rlm_protocol_plugin(
75            reg,
76            self.config.clone(),
77            Arc::clone(&self.projection_resolver),
78            Arc::clone(&self.last_prompt_usage),
79        )
80    }
81}