lean_ctx/core/providers/
provider_trait.rs1use super::{ProviderItem, ProviderResult};
2
3pub trait ContextProvider: Send + Sync {
13 fn id(&self) -> &'static str;
15
16 fn display_name(&self) -> &'static str;
18
19 fn supported_actions(&self) -> &[&str];
21
22 fn execute(&self, action: &str, params: &ProviderParams) -> Result<ProviderResult, String>;
24
25 fn reuse_binding(&self, _action: &str, _params: &ProviderParams) -> Option<String> {
28 None
29 }
30
31 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 fn cache_ttl_secs(&self) -> u64 {
44 120
45 }
46
47 fn descriptor(&self) -> ConnectorDescriptor {
52 ConnectorDescriptor::list_only("token or none")
53 }
54
55 fn requires_auth(&self) -> bool {
57 true
58 }
59
60 fn is_available(&self) -> bool;
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
67#[serde(rename_all = "snake_case")]
68pub enum CapabilityLevel {
69 Supported,
70 Partial,
71 Unsupported,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
78pub struct ConnectorDescriptor {
79 pub principal_binding: &'static str,
81 pub search: CapabilityLevel,
82 pub list: CapabilityLevel,
83 pub get: CapabilityLevel,
84 pub delta: CapabilityLevel,
85 pub revalidate_acl: CapabilityLevel,
88 pub revalidate_revision: CapabilityLevel,
90 pub delete_event: CapabilityLevel,
91 pub content_trust: &'static str,
93 pub write_surface: &'static str,
95 pub failure_policy: &'static str,
97 pub notes: &'static str,
98}
99
100impl ConnectorDescriptor {
101 pub(crate) const UNTRUSTED: &'static str = "untrusted data";
102 pub(crate) const NO_WRITES: &'static str = "none";
103 pub(crate) const FAIL_CLOSED: &'static str = "deny; no stale or cached answer";
104
105 pub(crate) fn list_only(principal_binding: &'static str) -> Self {
106 Self {
107 principal_binding,
108 search: CapabilityLevel::Unsupported,
109 list: CapabilityLevel::Supported,
110 get: CapabilityLevel::Unsupported,
111 delta: CapabilityLevel::Unsupported,
112 revalidate_acl: CapabilityLevel::Unsupported,
113 revalidate_revision: CapabilityLevel::Unsupported,
114 delete_event: CapabilityLevel::Unsupported,
115 content_trust: Self::UNTRUSTED,
116 write_surface: Self::NO_WRITES,
117 failure_policy: Self::FAIL_CLOSED,
118 notes: "no item re-check: personal-local use only, withheld from governed reuse",
119 }
120 }
121
122 pub fn allows_shared_reuse(&self) -> bool {
124 self.revalidate_acl == CapabilityLevel::Supported
125 }
126}
127
128#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
130pub struct ProviderParams {
131 pub project: Option<String>,
132 pub state: Option<String>,
133 pub limit: Option<usize>,
134 pub query: Option<String>,
135 pub id: Option<String>,
136}
137
138#[derive(Debug, Clone)]
140pub struct ContextPacket {
141 pub provider_id: String,
142 pub action: String,
143 pub items: Vec<ProviderItem>,
144 pub token_count_raw: usize,
145 pub token_count_compressed: usize,
146 pub cache_hit: bool,
147}