Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: helix editor build crash #73

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions crates/custom-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "custom-types"
version = "0.1.0"
edition.workspace = true
license.workspace = true
authors.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lsp-types = "0.94"
serde = "1"
serde_json = "1"
uuid = "1"
2 changes: 2 additions & 0 deletions crates/custom-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod llm_ls;
pub mod request;
118 changes: 118 additions & 0 deletions crates/custom-types/src/llm_ls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use std::{fmt::Display, path::PathBuf};

use lsp_types::TextDocumentPositionParams;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};
use uuid::Uuid;

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AcceptCompletionParams {
pub request_id: Uuid,
pub accepted_completion: u32,
pub shown_completions: Vec<u32>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RejectCompletionParams {
pub request_id: Uuid,
pub shown_completions: Vec<u32>,
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Ide {
Neovim,
VSCode,
JetBrains,
Emacs,
Jupyter,
Sublime,
VisualStudio,
#[default]
Unknown,
}

impl Display for Ide {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.serialize(f)
}
}

fn parse_ide<'de, D>(d: D) -> std::result::Result<Ide, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(d).map(|b: Option<_>| b.unwrap_or(Ide::Unknown))
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Backend {
#[default]
HuggingFace,
Ollama,
OpenAi,
Tgi,
}

impl Display for Backend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.serialize(f)
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FimParams {
pub enabled: bool,
pub prefix: String,
pub middle: String,
pub suffix: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum TokenizerConfig {
Local {
path: PathBuf,
},
HuggingFace {
repository: String,
api_token: Option<String>,
},
Download {
url: String,
to: PathBuf,
},
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetCompletionsParams {
#[serde(flatten)]
pub text_document_position: TextDocumentPositionParams,
#[serde(default)]
#[serde(deserialize_with = "parse_ide")]
pub ide: Ide,
pub fim: FimParams,
pub api_token: Option<String>,
pub model: String,
pub backend: Backend,
pub tokens_to_clear: Vec<String>,
pub tokenizer_config: Option<TokenizerConfig>,
pub context_window: usize,
pub tls_skip_verify_insecure: bool,
pub request_body: Map<String, Value>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Completion {
pub generated_text: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GetCompletionsResult {
pub request_id: Uuid,
pub completions: Vec<Completion>,
}
32 changes: 32 additions & 0 deletions crates/custom-types/src/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use lsp_types::request::Request;

use crate::llm_ls::{
AcceptCompletionParams, GetCompletionsParams, GetCompletionsResult, RejectCompletionParams,
};

#[derive(Debug)]
pub enum GetCompletions {}

impl Request for GetCompletions {
type Params = GetCompletionsParams;
type Result = GetCompletionsResult;
const METHOD: &'static str = "llm-ls/getCompletions";
}

#[derive(Debug)]
pub enum AcceptCompletion {}

impl Request for AcceptCompletion {
type Params = AcceptCompletionParams;
type Result = ();
const METHOD: &'static str = "llm-ls/acceptCompletion";
}

#[derive(Debug)]
pub enum RejectCompletion {}

impl Request for RejectCompletion {
type Params = RejectCompletionParams;
type Result = ();
const METHOD: &'static str = "llm-ls/rejectCompletion";
}
1 change: 1 addition & 0 deletions crates/llm-ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ name = "llm-ls"

[dependencies]
clap = { version = "4", features = ["derive"] }
custom-types = { path = "../custom-types" }
home = "0.5"
ropey = { version = "1.6", default-features = false, features = [
"simd",
Expand Down
15 changes: 3 additions & 12 deletions crates/llm-ls/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{APIError, APIResponse, CompletionParams, Generation, Ide, NAME, VERSION};
use super::{APIError, APIResponse, Generation, NAME, VERSION};
use custom_types::llm_ls::{Backend, GetCompletionsParams, Ide};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, USER_AGENT};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -150,17 +151,7 @@ fn parse_openai_text(text: &str) -> Result<Vec<Generation>> {
}
}

#[derive(Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Backend {
#[default]
HuggingFace,
Ollama,
OpenAi,
Tgi,
}

pub fn build_body(prompt: String, params: &CompletionParams) -> Map<String, Value> {
pub fn build_body(prompt: String, params: &GetCompletionsParams) -> Map<String, Value> {
let mut body = params.request_body.clone();
match params.backend {
Backend::HuggingFace | Backend::Tgi => {
Expand Down
Loading
Loading