Skip to main content

lean_ctx/core/providers/
provider_trait.rs

1use super::{ProviderItem, ProviderResult};
2
3/// Plugin-ready trait for external context providers.
4///
5/// A ContextProvider connects LeanCTX to external data sources (issue trackers,
6/// CI systems, documentation, etc.) and returns structured context that flows
7/// through the standard compression and IR pipeline.
8///
9/// The existing GitLab provider implements this interface pattern.
10/// Future plugins will register implementations dynamically via the provider
11/// framework contract.
12pub trait ContextProvider: Send + Sync {
13    /// Unique identifier for this provider (e.g. "gitlab", "github", "jira").
14    fn id(&self) -> &'static str;
15
16    /// Human-readable display name.
17    fn display_name(&self) -> &'static str;
18
19    /// Returns the actions this provider supports (e.g. "issues", "mrs", "pipelines").
20    fn supported_actions(&self) -> &[&str];
21
22    /// Execute a provider action and return structured results.
23    fn execute(&self, action: &str, params: &ProviderParams) -> Result<ProviderResult, String>;
24
25    /// Stable, credential-bound identity of a read-only resource. A name alone
26    /// does not authorize reuse; providers without this contract remain unbound.
27    fn reuse_binding(&self, _action: &str, _params: &ProviderParams) -> Option<String> {
28        None
29    }
30
31    /// Fetch current evidence for one previously observed item. Never implement
32    /// this with a cached result or a project-only permission probe.
33    fn reacquire_item(
34        &self,
35        _action: &str,
36        _params: &ProviderParams,
37        _item_id: &str,
38    ) -> Result<ProviderResult, String> {
39        Err("provider does not support current item authorization".into())
40    }
41
42    /// TTL for caching results from this provider (in seconds).
43    fn cache_ttl_secs(&self) -> u64 {
44        120
45    }
46
47    /// Whether this provider requires authentication.
48    fn requires_auth(&self) -> bool {
49        true
50    }
51
52    /// Check if the provider is configured and ready to serve requests.
53    fn is_available(&self) -> bool;
54}
55
56/// Parameters passed to a provider action.
57#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
58pub struct ProviderParams {
59    pub project: Option<String>,
60    pub state: Option<String>,
61    pub limit: Option<usize>,
62    pub query: Option<String>,
63    pub id: Option<String>,
64}
65
66/// A context packet produced by a provider, ready for the IR pipeline.
67#[derive(Debug, Clone)]
68pub struct ContextPacket {
69    pub provider_id: String,
70    pub action: String,
71    pub items: Vec<ProviderItem>,
72    pub token_count_raw: usize,
73    pub token_count_compressed: usize,
74    pub cache_hit: bool,
75}