Skip to main content

glass_dev/
fim.rs

1//! Fill-in-the-middle ghosts served by a resident Pi AgentSession.
2//!
3//! Local identifier/LSP/`TODO` fallbacks stay available. A `stub://` endpoint
4//! is for tests and never starts Pi. Otherwise Glass queues `complete` on a
5//! resident Pi session so ghosts use the same SDK, auth, and model as the
6//! factory — not a second HTTP client.
7
8use crate::customization::EditorConfig;
9use serde_json::Value;
10use std::env;
11
12const MAX_PREFIX_BYTES: usize = 6 * 1024;
13const MAX_SUFFIX_BYTES: usize = 2 * 1024;
14const MAX_GHOST_BYTES: usize = 256;
15
16/// Who supplies the ghost fill.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum FimBackend {
19    Stub,
20    Pi,
21}
22
23/// Resolved FIM provider.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct FimProvider {
26    pub backend: FimBackend,
27    pub model: Option<String>,
28}
29
30impl FimProvider {
31    pub fn from_editor(editor: &EditorConfig) -> Option<Self> {
32        let endpoint = editor
33            .fim
34            .endpoint
35            .as_deref()
36            .map(str::trim)
37            .filter(|value| !value.is_empty())
38            .map(str::to_string)
39            .or_else(|| env_nonempty("GLASS_FIM_ENDPOINT"));
40        if endpoint
41            .as_deref()
42            .is_some_and(|value| matches!(value, "off" | "none" | "disable"))
43        {
44            return None;
45        }
46        if endpoint
47            .as_deref()
48            .is_some_and(|value| value.starts_with("stub:"))
49        {
50            return Some(Self {
51                backend: FimBackend::Stub,
52                model: None,
53            });
54        }
55        Some(Self {
56            backend: FimBackend::Pi,
57            model: editor
58                .fim
59                .model
60                .as_deref()
61                .map(str::trim)
62                .filter(|value| !value.is_empty())
63                .map(str::to_string)
64                .or_else(|| env_nonempty("GLASS_FIM_MODEL")),
65        })
66    }
67
68    pub fn complete(&self, prefix: &str, suffix: &str) -> Result<String, String> {
69        let prefix = trim_prefix(prefix);
70        let suffix = trim_suffix(suffix);
71        match self.backend {
72            FimBackend::Stub => Ok(bound_ghost(&stub_complete(prefix, suffix))),
73            FimBackend::Pi => {
74                Err("Pi FIM is served by a resident AgentSession complete operation".into())
75            }
76        }
77    }
78}
79
80pub fn parse_fim_text(value: &Value) -> Option<String> {
81    let text = value
82        .pointer("/result/text")
83        .and_then(Value::as_str)
84        .or_else(|| value.get("text").and_then(Value::as_str))
85        .or_else(|| value.pointer("/choices/0/text").and_then(Value::as_str))?;
86    let text = text.trim_end_matches('\0').to_string();
87    (!text.trim().is_empty()).then_some(text)
88}
89
90fn stub_complete(prefix: &str, suffix: &str) -> String {
91    let _ = suffix;
92    let line = prefix.rsplit('\n').next().unwrap_or(prefix);
93    let trimmed = line.trim_end();
94    if trimmed.ends_with('(') {
95        return "ctx)".into();
96    }
97    if prefix.ends_with("{\n")
98        || prefix.ends_with("{\n    ")
99        || trimmed.ends_with('{')
100        || line.chars().all(|character| character.is_whitespace())
101    {
102        return "todo!()".into();
103    }
104    "todo!()".into()
105}
106
107fn trim_prefix(prefix: &str) -> &str {
108    if prefix.len() <= MAX_PREFIX_BYTES {
109        return prefix;
110    }
111    let start = prefix.len() - MAX_PREFIX_BYTES;
112    let start = prefix
113        .char_indices()
114        .find(|(index, _)| *index >= start)
115        .map(|(index, _)| index)
116        .unwrap_or(start);
117    &prefix[start..]
118}
119
120fn trim_suffix(suffix: &str) -> &str {
121    if suffix.len() <= MAX_SUFFIX_BYTES {
122        return suffix;
123    }
124    let mut end = MAX_SUFFIX_BYTES;
125    while end > 0 && !suffix.is_char_boundary(end) {
126        end -= 1;
127    }
128    &suffix[..end]
129}
130
131fn bound_ghost(text: &str) -> String {
132    let mut end = text.len().min(MAX_GHOST_BYTES);
133    while end > 0 && !text.is_char_boundary(end) {
134        end -= 1;
135    }
136    text[..end].to_string()
137}
138
139fn env_nonempty(name: &str) -> Option<String> {
140    env::var(name)
141        .ok()
142        .map(|value| value.trim().to_string())
143        .filter(|value| !value.is_empty())
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149    use crate::customization::{EditorConfig, FimConfig};
150    use serde_json::json;
151
152    #[test]
153    fn stub_provider_fills_an_empty_block() {
154        let provider = FimProvider {
155            backend: FimBackend::Stub,
156            model: None,
157        };
158        let text = provider
159            .complete("fn main() {\n    ", "\n}\n")
160            .expect("stub");
161        assert_eq!(text, "todo!()");
162        assert_eq!(
163            provider.complete("fn greet(", ") {}\n").expect("call"),
164            "ctx)"
165        );
166    }
167
168    #[test]
169    fn parse_fim_text_reads_pi_complete_and_legacy_shapes() {
170        assert_eq!(
171            parse_fim_text(&json!({"result":{"text":" hello"}})).as_deref(),
172            Some(" hello")
173        );
174        assert_eq!(
175            parse_fim_text(&json!({"choices":[{"text":"world"}]})).as_deref(),
176            Some("world")
177        );
178        assert!(parse_fim_text(&json!({"result":{"text":"   "}})).is_none());
179    }
180
181    #[test]
182    fn from_editor_uses_stub_or_resident_pi() {
183        let stub = EditorConfig {
184            fim: FimConfig {
185                endpoint: Some("stub://unit".into()),
186                model: Some("demo".into()),
187                ..FimConfig::default()
188            },
189            ..EditorConfig::default()
190        };
191        assert_eq!(
192            FimProvider::from_editor(&stub).map(|provider| provider.backend),
193            Some(FimBackend::Stub)
194        );
195        let pi = EditorConfig::default();
196        assert_eq!(
197            FimProvider::from_editor(&pi).map(|provider| provider.backend),
198            Some(FimBackend::Pi)
199        );
200        let off = EditorConfig {
201            fim: FimConfig {
202                endpoint: Some("off".into()),
203                ..FimConfig::default()
204            },
205            ..EditorConfig::default()
206        };
207        assert!(FimProvider::from_editor(&off).is_none());
208    }
209}