feat(ai): add cloud providers and local model selection

Add OpenAI-compatible, Anthropic, and custom endpoint support while
keeping the local model path intact. The UI now lets users choose the
provider and local model, and staged diffs are prepared more carefully
so generated commit messages stay focused and usable.

- src-tauri/crates/commit_ai/*
  - Add HTTP-based generators for OpenAI, Anthropic, and custom APIs.
  - Introduce shared request/response handling and message sanitizing.
  - Expand prompt building to require a body and trim long diffs safely.
  - Expose selectable local model metadata and loading by model ID.
- src-tauri/src/git.rs
  - Add commands for listing local models and loading them in background.
  - Route generation by provider and include staged file lists in prompts.
  - Exclude noisy lockfiles from detailed staged diffs.
- src-tauri/src/main.rs
  - Wire the new AI commands into the Tauri app setup.
- src/lib/components/*
  - Add an AI settings dialog and update the commit panel for provider
    and model selection.
- src/lib/git.ts, src/lib/types.ts, src/App.svelte, src/app.css
  - Extend frontend state, types, and styling for AI provider settings.
- src-tauri/Cargo.lock, src-tauri/crates/commit_ai/Cargo.toml
  - Add reqwest and serde_json for cloud API requests.
This commit is contained in:
Christoph Brandau
2026-07-02 21:19:16 +02:00
parent d415cbd3a1
commit 6b7186d040
13 changed files with 924 additions and 80 deletions
+2
View File
@@ -8,3 +8,5 @@ edition = "2024"
mistralrs = "0.8"
tokio = { version = "1", features = ["sync"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
+203
View File
@@ -0,0 +1,203 @@
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::{build_messages, sanitize_message};
// Großzügig bemessen, damit ein ausführlicher Body mit Stichpunkten nicht abgeschnitten wird.
const DEFAULT_MAX_TOKENS: u32 = 1500;
// Ohne Timeout würde ein hängender Endpoint den "AI"-Button dauerhaft blockieren.
const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
fn http_client() -> Result<reqwest::Client, String> {
reqwest::Client::builder()
.timeout(REQUEST_TIMEOUT)
.build()
.map_err(|err| format!("HTTP-Client konnte nicht erstellt werden: {err}"))
}
#[derive(Serialize)]
struct OpenAiMessage {
role: &'static str,
content: String,
}
#[derive(Serialize)]
struct OpenAiRequest {
model: String,
messages: Vec<OpenAiMessage>,
temperature: f32,
}
#[derive(Deserialize)]
struct OpenAiResponseMessage {
content: Option<String>,
}
#[derive(Deserialize)]
struct OpenAiChoice {
message: OpenAiResponseMessage,
}
#[derive(Deserialize)]
struct OpenAiResponse {
#[serde(default)]
choices: Vec<OpenAiChoice>,
}
async fn openai_compatible_request(
url: String,
bearer: Option<&str>,
model: &str,
diff: &str,
notes: Option<&str>,
) -> Result<String, String> {
let (system, user) = build_messages(diff, notes)?;
let body = OpenAiRequest {
model: model.to_string(),
messages: vec![
OpenAiMessage { role: "system", content: system },
OpenAiMessage { role: "user", content: user },
],
temperature: 0.3,
};
let client = http_client()?;
let mut request = client.post(url).json(&body);
if let Some(key) = bearer.filter(|k| !k.trim().is_empty()) {
request = request.bearer_auth(key);
}
let response = request
.send()
.await
.map_err(|err| format!("Anfrage an das KI-Modell fehlgeschlagen: {err}"))?;
let status = response.status();
let text = response
.text()
.await
.map_err(|err| format!("Antwort konnte nicht gelesen werden: {err}"))?;
if !status.is_success() {
return Err(format!("API-Fehler ({status}): {text}"));
}
let parsed: OpenAiResponse = serde_json::from_str(&text)
.map_err(|err| format!("Antwort konnte nicht verarbeitet werden: {err}"))?;
parsed
.choices
.into_iter()
.next()
.and_then(|choice| choice.message.content)
.map(|content| sanitize_message(&content))
.filter(|content| !content.is_empty())
.ok_or_else(|| "Das Modell hat keine Antwort geliefert.".to_string())
}
pub async fn generate_openai(
api_key: &str,
model: &str,
diff: &str,
notes: Option<&str>,
) -> Result<String, String> {
if api_key.trim().is_empty() {
return Err("OpenAI-API-Key fehlt.".to_string());
}
openai_compatible_request(
"https://api.openai.com/v1/chat/completions".to_string(),
Some(api_key),
model,
diff,
notes,
)
.await
}
pub async fn generate_custom(
base_url: &str,
api_key: Option<&str>,
model: &str,
diff: &str,
notes: Option<&str>,
) -> Result<String, String> {
if base_url.trim().is_empty() {
return Err("Endpoint-URL fehlt.".to_string());
}
let url = format!("{}/chat/completions", base_url.trim_end_matches('/'));
openai_compatible_request(url, api_key, model, diff, notes).await
}
#[derive(Serialize)]
struct AnthropicMessage {
role: &'static str,
content: String,
}
#[derive(Serialize)]
struct AnthropicRequest {
model: String,
max_tokens: u32,
system: String,
messages: Vec<AnthropicMessage>,
}
#[derive(Deserialize)]
struct AnthropicContentBlock {
#[serde(default)]
text: Option<String>,
}
#[derive(Deserialize)]
struct AnthropicResponse {
#[serde(default)]
content: Vec<AnthropicContentBlock>,
}
pub async fn generate_anthropic(
api_key: &str,
model: &str,
diff: &str,
notes: Option<&str>,
) -> Result<String, String> {
if api_key.trim().is_empty() {
return Err("Anthropic-API-Key fehlt.".to_string());
}
let (system, user) = build_messages(diff, notes)?;
let body = AnthropicRequest {
model: model.to_string(),
max_tokens: DEFAULT_MAX_TOKENS,
system,
messages: vec![AnthropicMessage { role: "user", content: user }],
};
let client = http_client()?;
let response = client
.post("https://api.anthropic.com/v1/messages")
.header("x-api-key", api_key)
.header("anthropic-version", "2023-06-01")
.json(&body)
.send()
.await
.map_err(|err| format!("Anfrage an Anthropic fehlgeschlagen: {err}"))?;
let status = response.status();
let text = response
.text()
.await
.map_err(|err| format!("Antwort konnte nicht gelesen werden: {err}"))?;
if !status.is_success() {
return Err(format!("API-Fehler ({status}): {text}"));
}
let parsed: AnthropicResponse = serde_json::from_str(&text)
.map_err(|err| format!("Antwort konnte nicht verarbeitet werden: {err}"))?;
parsed
.content
.into_iter()
.find_map(|block| block.text)
.map(|text| sanitize_message(&text))
.filter(|text| !text.is_empty())
.ok_or_else(|| "Das Modell hat keine Antwort geliefert.".to_string())
}
+130 -30
View File
@@ -1,13 +1,56 @@
mod cloud;
pub use cloud::{generate_anthropic, generate_custom, generate_openai};
use std::sync::Arc;
use mistralrs::{GgufModelBuilder, Model, TextMessageRole, TextMessages};
use tokio::sync::RwLock;
// Small instruct model, quantized to keep the one-time download reasonable (~1 GB) while
// still being fast enough for short, structured generations like a commit message on CPU.
const HF_REPO: &str = "Qwen/Qwen2.5-1.5B-Instruct-GGUF";
const GGUF_FILE: &str = "qwen2.5-1.5b-instruct-q4_k_m.gguf";
const TOKENIZER_MODEL_ID: &str = "Qwen/Qwen2.5-1.5B-Instruct";
/// One selectable local (on-device) model. Larger models produce better commit messages
/// but take longer to download (first run only, then cached) and run slower on CPU.
#[derive(Debug, Clone, serde::Serialize)]
pub struct LocalModelOption {
pub id: &'static str,
pub label: &'static str,
pub approx_size_mb: u32,
repo: &'static str,
file: &'static str,
tokenizer_repo: &'static str,
}
pub const DEFAULT_LOCAL_MODEL_ID: &str = "qwen2.5-1.5b";
pub const LOCAL_MODELS: &[LocalModelOption] = &[
LocalModelOption {
id: "qwen2.5-0.5b",
label: "Qwen2.5 0.5B Instruct — schnell, geringere Qualität",
approx_size_mb: 490,
repo: "Qwen/Qwen2.5-0.5B-Instruct-GGUF",
file: "qwen2.5-0.5b-instruct-q4_k_m.gguf",
tokenizer_repo: "Qwen/Qwen2.5-0.5B-Instruct",
},
LocalModelOption {
id: "qwen2.5-1.5b",
label: "Qwen2.5 1.5B Instruct — empfohlen",
approx_size_mb: 1050,
repo: "Qwen/Qwen2.5-1.5B-Instruct-GGUF",
file: "qwen2.5-1.5b-instruct-q4_k_m.gguf",
tokenizer_repo: "Qwen/Qwen2.5-1.5B-Instruct",
},
LocalModelOption {
id: "qwen2.5-3b",
label: "Qwen2.5 3B Instruct — beste Qualität, langsamer",
approx_size_mb: 2100,
repo: "Qwen/Qwen2.5-3B-Instruct-GGUF",
file: "qwen2.5-3b-instruct-q4_k_m.gguf",
tokenizer_repo: "Qwen/Qwen2.5-3B-Instruct",
},
];
fn find_local_model(model_id: &str) -> Option<&'static LocalModelOption> {
LOCAL_MODELS.iter().find(|option| option.id == model_id)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "lowercase")]
@@ -23,16 +66,19 @@ pub enum CommitAiPhase {
#[derive(Debug, Clone, serde::Serialize)]
pub struct CommitAiStatus {
pub phase: CommitAiPhase,
pub model_id: Option<String>,
pub error: Option<String>,
}
struct Inner {
phase: CommitAiPhase,
model_id: Option<String>,
error: Option<String>,
model: Option<Arc<Model>>,
}
/// Cheap to clone: shares one model instance across the app via an inner `Arc`.
/// Manages the local (on-device) model only. Cloud providers are stateless HTTP calls
/// (see [`cloud`]) and don't need this — there's nothing to download or keep loaded.
#[derive(Clone)]
pub struct CommitAiEngine {
inner: Arc<RwLock<Inner>>,
@@ -43,6 +89,7 @@ impl Default for CommitAiEngine {
Self {
inner: Arc::new(RwLock::new(Inner {
phase: CommitAiPhase::Idle,
model_id: None,
error: None,
model: None,
})),
@@ -59,30 +106,52 @@ impl CommitAiEngine {
let guard = self.inner.read().await;
CommitAiStatus {
phase: guard.phase,
model_id: guard.model_id.clone(),
error: guard.error.clone(),
}
}
/// Downloads (first run only; hf-hub caches the files afterwards) and loads the model.
/// Safe to call multiple times — only the first caller actually triggers a load, later
/// callers just return once the in-flight or previous attempt is done.
pub async fn ensure_loaded(&self) {
/// Downloads (first run only; hf-hub caches the files afterwards) and loads the given
/// local model. Safe to call repeatedly — a call for the model that's already
/// ready/loading is a no-op; a call for a *different* model switches to it (the
/// previous one is dropped once no generation is still using it).
pub async fn ensure_loaded(&self, model_id: &str) {
{
let mut guard = self.inner.write().await;
if guard.phase != CommitAiPhase::Idle {
let guard = self.inner.read().await;
let same_model = guard.model_id.as_deref() == Some(model_id);
if same_model && matches!(guard.phase, CommitAiPhase::Ready | CommitAiPhase::Loading) {
return;
}
guard.phase = CommitAiPhase::Loading;
guard.error = None;
}
let result = GgufModelBuilder::new(HF_REPO, vec![GGUF_FILE])
.with_tok_model_id(TOKENIZER_MODEL_ID)
let Some(option) = find_local_model(model_id) else {
let mut guard = self.inner.write().await;
guard.phase = CommitAiPhase::Error;
guard.model_id = Some(model_id.to_string());
guard.error = Some(format!("Unbekanntes lokales Modell: {model_id}"));
return;
};
{
let mut guard = self.inner.write().await;
guard.phase = CommitAiPhase::Loading;
guard.model_id = Some(model_id.to_string());
guard.error = None;
guard.model = None;
}
let result = GgufModelBuilder::new(option.repo, vec![option.file])
.with_tok_model_id(option.tokenizer_repo)
.with_logging()
.build()
.await;
let mut guard = self.inner.write().await;
// If the user switched to yet another model while this one was loading, drop this
// (now stale) result instead of overwriting the newer request's state.
if guard.model_id.as_deref() != Some(model_id) {
return;
}
match result {
Ok(model) => {
guard.model = Some(Arc::new(model));
@@ -105,17 +174,11 @@ impl CommitAiEngine {
let guard = self.inner.read().await;
match (guard.phase, &guard.model) {
(CommitAiPhase::Ready, Some(model)) => model.clone(),
_ => return Err("Das KI-Modell ist noch nicht bereit.".to_string()),
_ => return Err("Das lokale KI-Modell ist noch nicht bereit.".to_string()),
}
};
if diff.trim().is_empty() {
return Err(
"Keine gestagten Änderungen für eine Commit-Message vorhanden.".to_string(),
);
}
let (system, user) = build_messages(diff, notes);
let (system, user) = build_messages(diff, notes)?;
let messages = TextMessages::new()
.add_message(TextMessageRole::System, system)
.add_message(TextMessageRole::User, user);
@@ -131,23 +194,60 @@ impl CommitAiEngine {
.and_then(|choice| choice.message.content.clone())
.ok_or_else(|| "Das Modell hat keine Antwort geliefert.".to_string())?;
Ok(content.trim().to_string())
let message = sanitize_message(&content);
if message.is_empty() {
return Err("Das Modell hat keine Antwort geliefert.".to_string());
}
Ok(message)
}
}
fn build_messages(diff: &str, notes: Option<&str>) -> (String, String) {
/// Models occasionally ignore the "no code fences" instruction (small local models
/// especially) — strip a wrapping ``` fence and wrapping quotes so the result can go
/// straight into the commit-message box.
pub(crate) fn sanitize_message(raw: &str) -> String {
let mut text = raw.trim().to_string();
if text.starts_with("```") {
text = match text.split_once('\n') {
// Drop the opening fence line (which may carry a language tag) and the closing fence.
Some((_fence, rest)) => rest.trim_end().trim_end_matches("```").trim().to_string(),
None => text.trim_matches('`').trim().to_string(),
};
}
let trimmed = text.trim();
if trimmed.len() >= 2 && trimmed.starts_with('"') && trimmed.ends_with('"') {
return trimmed[1..trimmed.len() - 1].trim().to_string();
}
trimmed.to_string()
}
pub(crate) fn build_messages(diff: &str, notes: Option<&str>) -> Result<(String, String), String> {
if diff.trim().is_empty() {
return Err("Keine gestagten Änderungen für eine Commit-Message vorhanden.".to_string());
}
// grobe Token-Schätzung, kleine Modelle haben oft 832k Kontext
const MAX_CHARS: usize = 24_000;
let diff = if diff.len() > MAX_CHARS {
format!("{}\n\n[... Diff gekürzt ...]", &diff[..MAX_CHARS])
// Byte-Index auf eine gültige UTF-8-Zeichengrenze zurückziehen, sonst
// paniken Slices mitten in einem Umlaut o. Ä.
let mut cut = MAX_CHARS;
while !diff.is_char_boundary(cut) {
cut -= 1;
}
format!("{}\n\n[... Diff gekürzt ...]", &diff[..cut])
} else {
diff.to_string()
};
let system = "Du bist ein Werkzeug, das Git-Commit-Messages erzeugt. \
Antworte ausschließlich mit der Commit-Message im Conventional-Commits-Format \
(<type>(<scope>): <subject>), optional gefolgt von einem Body nach einer Leerzeile. \
Subject imperativ, max. 72 Zeichen. Kein Vorspann, keine Erklärung, keine Code-Fences, in Englisch antworten"
(<type>(<scope>): <subject>), gefolgt von einem Body nach einer Leerzeile. \
Subject imperativ, max. 72 Zeichen. \
Der Body ist Pflicht: Fasse in einem kurzen Absatz zusammen, was und warum geändert wurde, \
und liste danach die wesentlichen Änderungen als Stichpunkte (- ...) auf, \
gruppiert nach betroffenem Bereich/Datei. Zeilen im Body max. 72 Zeichen. \
Kein Vorspann, keine Erklärung, keine Code-Fences, in Englisch antworten"
.to_string();
let mut user = String::new();
@@ -155,5 +255,5 @@ Subject imperativ, max. 72 Zeichen. Kein Vorspann, keine Erklärung, keine Code-
user.push_str(&format!("Anmerkungen des Entwicklers:\n{n}\n\n"));
}
user.push_str(&format!("Staged diff:\n{diff}"));
(system, user)
Ok((system, user))
}