Skip to main content

kcl_lib/lsp/copilot/
mod.rs

1//! The copilot lsp server for ghost text.
2#![allow(dead_code)]
3
4pub mod cache;
5pub mod types;
6
7use std::borrow::Cow;
8use std::fmt::Debug;
9use std::sync::Arc;
10use std::sync::RwLock;
11
12use dashmap::DashMap;
13use serde::Deserialize;
14use serde::Serialize;
15use tower_lsp::LanguageServer;
16use tower_lsp::jsonrpc::Error;
17use tower_lsp::jsonrpc::Result;
18use tower_lsp::lsp_types::CreateFilesParams;
19use tower_lsp::lsp_types::DeleteFilesParams;
20use tower_lsp::lsp_types::Diagnostic;
21use tower_lsp::lsp_types::DidChangeConfigurationParams;
22use tower_lsp::lsp_types::DidChangeTextDocumentParams;
23use tower_lsp::lsp_types::DidChangeWatchedFilesParams;
24use tower_lsp::lsp_types::DidChangeWorkspaceFoldersParams;
25use tower_lsp::lsp_types::DidCloseTextDocumentParams;
26use tower_lsp::lsp_types::DidOpenTextDocumentParams;
27use tower_lsp::lsp_types::DidSaveTextDocumentParams;
28use tower_lsp::lsp_types::InitializeParams;
29use tower_lsp::lsp_types::InitializeResult;
30use tower_lsp::lsp_types::InitializedParams;
31use tower_lsp::lsp_types::MessageType;
32use tower_lsp::lsp_types::OneOf;
33use tower_lsp::lsp_types::RenameFilesParams;
34use tower_lsp::lsp_types::ServerCapabilities;
35use tower_lsp::lsp_types::TextDocumentItem;
36use tower_lsp::lsp_types::TextDocumentSyncCapability;
37use tower_lsp::lsp_types::TextDocumentSyncKind;
38use tower_lsp::lsp_types::TextDocumentSyncOptions;
39use tower_lsp::lsp_types::WorkspaceFolder;
40use tower_lsp::lsp_types::WorkspaceFoldersServerCapabilities;
41use tower_lsp::lsp_types::WorkspaceServerCapabilities;
42
43use crate::lsp::backend::Backend as _;
44use crate::lsp::copilot::cache::CopilotCache;
45use crate::lsp::copilot::types::CopilotAcceptCompletionParams;
46use crate::lsp::copilot::types::CopilotCompletionResponse;
47use crate::lsp::copilot::types::CopilotCompletionTelemetry;
48use crate::lsp::copilot::types::CopilotEditorInfo;
49use crate::lsp::copilot::types::CopilotLspCompletionParams;
50use crate::lsp::copilot::types::CopilotRejectCompletionParams;
51use crate::lsp::copilot::types::DocParams;
52
53#[derive(Deserialize, Serialize, Debug)]
54pub struct Success {
55    success: bool,
56}
57impl Success {
58    pub fn new(success: bool) -> Self {
59        Self { success }
60    }
61}
62
63#[derive(Clone)]
64pub struct Backend {
65    /// The client is used to send notifications and requests to the client.
66    pub client: tower_lsp::Client,
67    /// The file system client to use.
68    pub fs: Arc<crate::fs::FileManager>,
69    /// The workspace folders.
70    pub workspace_folders: DashMap<String, WorkspaceFolder>,
71    /// Current code.
72    pub code_map: DashMap<String, Vec<u8>>,
73    /// The Zoo API client.
74    pub zoo_client: kittycad::Client,
75    /// The editor info is used to store information about the editor.
76    pub editor_info: Arc<RwLock<CopilotEditorInfo>>,
77    /// The cache is used to store the results of previous requests.
78    pub cache: Arc<cache::CopilotCache>,
79    /// Storage so we can send telemetry data back out.
80    pub telemetry: DashMap<uuid::Uuid, CopilotCompletionTelemetry>,
81    /// Diagnostics.
82    pub diagnostics_map: DashMap<String, Vec<Diagnostic>>,
83
84    pub is_initialized: Arc<tokio::sync::RwLock<bool>>,
85
86    /// Are we running in dev mode.
87    pub dev_mode: bool,
88}
89
90// Implement the shared backend trait for the language server.
91#[async_trait::async_trait]
92impl crate::lsp::backend::Backend for Backend {
93    fn client(&self) -> &tower_lsp::Client {
94        &self.client
95    }
96
97    fn fs(&self) -> &Arc<crate::fs::FileManager> {
98        &self.fs
99    }
100
101    async fn is_initialized(&self) -> bool {
102        *self.is_initialized.read().await
103    }
104
105    async fn set_is_initialized(&self, is_initialized: bool) {
106        *self.is_initialized.write().await = is_initialized;
107    }
108
109    async fn workspace_folders(&self) -> Vec<WorkspaceFolder> {
110        // TODO: fix clone
111        self.workspace_folders.iter().map(|i| i.clone()).collect()
112    }
113
114    async fn add_workspace_folders(&self, folders: Vec<WorkspaceFolder>) {
115        for folder in folders {
116            self.workspace_folders.insert(folder.name.to_string(), folder);
117        }
118    }
119
120    async fn remove_workspace_folders(&self, folders: Vec<WorkspaceFolder>) {
121        for folder in folders {
122            self.workspace_folders.remove(&folder.name);
123        }
124    }
125
126    fn code_map(&self) -> &DashMap<String, Vec<u8>> {
127        &self.code_map
128    }
129
130    async fn insert_code_map(&self, uri: String, text: Vec<u8>) {
131        self.code_map.insert(uri, text);
132    }
133
134    async fn remove_from_code_map(&self, uri: String) -> Option<Vec<u8>> {
135        self.code_map.remove(&uri).map(|(_, v)| v)
136    }
137
138    async fn clear_code_state(&self) {
139        self.code_map.clear();
140    }
141
142    fn current_diagnostics_map(&self) -> &DashMap<String, Vec<Diagnostic>> {
143        &self.diagnostics_map
144    }
145
146    async fn inner_on_change(&self, _params: TextDocumentItem, _force: bool) {
147        // We don't need to do anything here.
148    }
149}
150
151impl Backend {
152    #[cfg(target_arch = "wasm32")]
153    pub fn new_wasm(
154        client: tower_lsp::Client,
155        fs: crate::fs::wasm::FileSystemManager,
156        zoo_client: kittycad::Client,
157        dev_mode: bool,
158    ) -> Self {
159        Self::new(client, crate::fs::FileManager::new(fs), zoo_client, dev_mode)
160    }
161
162    pub fn new(
163        client: tower_lsp::Client,
164        fs: crate::fs::FileManager,
165        zoo_client: kittycad::Client,
166        dev_mode: bool,
167    ) -> Self {
168        Self {
169            client,
170            fs: Arc::new(fs),
171            workspace_folders: Default::default(),
172            code_map: Default::default(),
173            editor_info: Arc::new(RwLock::new(CopilotEditorInfo::default())),
174            cache: Arc::new(CopilotCache::new()),
175            telemetry: Default::default(),
176            zoo_client,
177
178            is_initialized: Default::default(),
179            diagnostics_map: Default::default(),
180            dev_mode,
181        }
182    }
183
184    /// Get completions from the kittycad api.
185    pub async fn get_completions(&self, language: String, prompt: String, suffix: String) -> Result<Vec<String>> {
186        let body = kittycad::types::KclCodeCompletionRequest {
187            extra: Some(kittycad::types::KclCodeCompletionParams {
188                language: Some(language.to_string()),
189                next_indent: None,
190                trim_by_indentation: true,
191                prompt_tokens: Some(prompt.len() as u32),
192                suffix_tokens: Some(suffix.len() as u32),
193            }),
194            prompt: Some(prompt),
195            suffix: Some(suffix),
196            max_tokens: Some(500),
197            temperature: Some(1.0),
198            top_p: Some(1.0),
199            // We only handle one completion at a time, for now so don't even waste the tokens.
200            n: Some(1),
201            stop: Some(["unset".to_string()].to_vec()),
202            nwo: None,
203            // We haven't implemented streaming yet.
204            stream: false,
205            model_version: None,
206        };
207
208        let resp = self
209            .zoo_client
210            .ml()
211            .create_kcl_code_completions(&body)
212            .await
213            .map_err(|err| Error {
214                code: tower_lsp::jsonrpc::ErrorCode::from(69),
215                data: None,
216                message: Cow::from(format!("Failed to get completions from zoo api: {err}")),
217            })?;
218        Ok(resp.completions)
219    }
220
221    pub async fn set_editor_info(&self, params: CopilotEditorInfo) -> Result<Success> {
222        self.client.log_message(MessageType::INFO, "setEditorInfo").await;
223        let copy = Arc::clone(&self.editor_info);
224        let mut lock = copy.write().map_err(|err| Error {
225            code: tower_lsp::jsonrpc::ErrorCode::from(69),
226            data: None,
227            message: Cow::from(format!("Failed lock: {err}")),
228        })?;
229        *lock = params;
230        Ok(Success::new(true))
231    }
232
233    pub fn get_doc_params(&self, params: &CopilotLspCompletionParams) -> Result<DocParams> {
234        let pos = params.doc.position;
235        let uri = params.doc.uri.to_string();
236        let rope = ropey::Rope::from_str(&params.doc.source);
237        let offset = crate::lsp::util::position_to_offset(pos.into(), &rope).unwrap_or_default();
238
239        Ok(DocParams {
240            uri,
241            pos,
242            language: params.doc.language_id.to_string(),
243            prefix: crate::lsp::util::get_text_before(offset, &rope).unwrap_or_default(),
244            suffix: crate::lsp::util::get_text_after(offset, &rope).unwrap_or_default(),
245            line_before: crate::lsp::util::get_line_before(pos.into(), &rope).unwrap_or_default(),
246            rope,
247        })
248    }
249
250    pub async fn get_completions_cycling(
251        &self,
252        params: CopilotLspCompletionParams,
253    ) -> Result<CopilotCompletionResponse> {
254        let doc_params = self.get_doc_params(&params)?;
255        let cached_result = self.cache.get_cached_result(&doc_params.uri, doc_params.pos.line);
256        if let Some(cached_result) = cached_result {
257            return Ok(cached_result);
258        }
259
260        let doc_params = self.get_doc_params(&params)?;
261        let line_before = doc_params.line_before.to_string();
262
263        // Let's not call it yet since it's not our model.
264        // We will need to wrap in spawn_local like we do in kcl/mod.rs for wasm only.
265        #[cfg(test)]
266        let mut completion_list = self
267            .get_completions(doc_params.language, doc_params.prefix, doc_params.suffix)
268            .await
269            .map_err(|err| Error {
270                code: tower_lsp::jsonrpc::ErrorCode::from(69),
271                data: None,
272                message: Cow::from(format!("Failed to get completions: {err}")),
273            })?;
274        #[cfg(not(test))]
275        let mut completion_list = vec![];
276
277        // if self.dev_mode
278        if false {
279            completion_list.push(
280                r#"fn cube(pos, scale) {
281  sg = startSketchOn(XY)
282    |> startProfile(at = pos)
283    |> line(end = [0, scale])
284    |> line(end = [scale, 0])
285    |> line(end = [0, -scale])
286  return sg
287}
288part001 = cube(pos = [0,0], scale = 20)
289    |> close()
290    |> extrude(length=20)"#
291                    .to_string(),
292            );
293        }
294
295        let response = CopilotCompletionResponse::from_str_vec(completion_list, line_before, doc_params.pos);
296        // Set the telemetry data for each completion.
297        for completion in response.completions.iter() {
298            let telemetry = CopilotCompletionTelemetry {
299                completion: completion.clone(),
300                params: params.clone(),
301            };
302            self.telemetry.insert(completion.uuid, telemetry);
303        }
304        self.cache
305            .set_cached_result(&doc_params.uri, &doc_params.pos.line, &response);
306
307        Ok(response)
308    }
309
310    pub async fn accept_completion(&self, params: CopilotAcceptCompletionParams) {
311        self.client
312            .log_message(MessageType::INFO, format!("Accepted completions: {params:?}"))
313            .await;
314
315        // Get the original telemetry data.
316        let Some(original) = self.telemetry.remove(&params.uuid) else {
317            return;
318        };
319
320        self.client
321            .log_message(MessageType::INFO, format!("Original telemetry: {original:?}"))
322            .await;
323
324        // TODO: Send the telemetry data to the zoo api.
325    }
326
327    pub async fn reject_completions(&self, params: CopilotRejectCompletionParams) {
328        self.client
329            .log_message(MessageType::INFO, format!("Rejected completions: {params:?}"))
330            .await;
331
332        // Get the original telemetry data.
333        let mut originals: Vec<CopilotCompletionTelemetry> = Default::default();
334        for uuid in params.uuids {
335            if let Some(original) = self.telemetry.remove(&uuid).map(|(_, v)| v) {
336                originals.push(original);
337            }
338        }
339
340        self.client
341            .log_message(MessageType::INFO, format!("Original telemetry: {originals:?}"))
342            .await;
343
344        // TODO: Send the telemetry data to the zoo api.
345    }
346}
347
348#[tower_lsp::async_trait]
349impl LanguageServer for Backend {
350    async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
351        Ok(InitializeResult {
352            capabilities: ServerCapabilities {
353                text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
354                    open_close: Some(true),
355                    change: Some(TextDocumentSyncKind::FULL),
356                    ..Default::default()
357                })),
358                workspace: Some(WorkspaceServerCapabilities {
359                    workspace_folders: Some(WorkspaceFoldersServerCapabilities {
360                        supported: Some(true),
361                        change_notifications: Some(OneOf::Left(true)),
362                    }),
363                    file_operations: None,
364                }),
365                ..ServerCapabilities::default()
366            },
367            ..Default::default()
368        })
369    }
370
371    async fn initialized(&self, params: InitializedParams) {
372        self.do_initialized(params).await
373    }
374
375    async fn shutdown(&self) -> tower_lsp::jsonrpc::Result<()> {
376        self.do_shutdown().await
377    }
378
379    async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) {
380        self.do_did_change_workspace_folders(params).await
381    }
382
383    async fn did_change_configuration(&self, params: DidChangeConfigurationParams) {
384        self.do_did_change_configuration(params).await
385    }
386
387    async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
388        self.do_did_change_watched_files(params).await
389    }
390
391    async fn did_create_files(&self, params: CreateFilesParams) {
392        self.do_did_create_files(params).await
393    }
394
395    async fn did_rename_files(&self, params: RenameFilesParams) {
396        self.do_did_rename_files(params).await
397    }
398
399    async fn did_delete_files(&self, params: DeleteFilesParams) {
400        self.do_did_delete_files(params).await
401    }
402
403    async fn did_open(&self, params: DidOpenTextDocumentParams) {
404        self.do_did_open(params).await
405    }
406
407    async fn did_change(&self, params: DidChangeTextDocumentParams) {
408        self.do_did_change(params).await;
409    }
410
411    async fn did_save(&self, params: DidSaveTextDocumentParams) {
412        self.do_did_save(params).await
413    }
414
415    async fn did_close(&self, params: DidCloseTextDocumentParams) {
416        self.do_did_close(params).await
417    }
418}