Files
Lumotia/crates/llm/src/lib.rs
Jake 34fce3cf9e
Some checks failed
check / cargo check (macos-latest) (push) Has been cancelled
check / cargo check (ubuntu-22.04) (push) Has been cancelled
check / cargo check (windows-latest) (push) Has been cancelled
check / svelte build + lint (push) Has been cancelled
feat: OpenWhispr-inspired transcription polish pass
Major quality pass on top of Phase 2. Five substantive changes plus
cross-cutting touches across audio, hotkey, transcription, and Tauri
command layers.

  Transcription quality

  - Long-audio chunking in commands/transcription.rs: Parakeet and large
    file transcription now chunk-and-recompose with overlap trimming, so
    the live-path chunking advantage extends to file-based workflows.
  - Stateful live speech gate in commands/live.rs on top of the earlier
    duplicate-boundary filtering — distinguishes start-of-speech from
    mid-speech and holds state across chunks.

  Auto-learning corrections

  - New crates/ai-formatting/src/correction_learning.rs: extracts user
    text corrections from viewer edits and proposes additions to the
    active profile's vocabulary.
  - src-tauri/src/commands/profiles.rs bridge for frontend-driven
    confirmation of learned terms.
  - src/routes/viewer/+page.svelte hooks the learning path into the
    segment-edit flow so corrections feed profile_terms without a
    separate 'train this profile' UX.

  Transcript profile provenance

  - Migration v8 (crates/storage/src/migrations.rs) adds profile_id to
    transcripts, defaulting to DEFAULT_PROFILE_ID so existing rows stay
    valid.
  - crates/storage/src/database.rs: TranscriptRow + CRUD carry profile_id.
  - src-tauri/src/commands/transcripts.rs: add_transcript accepts and
    persists profile_id.
  - DictationPage.svelte + FilesPage.svelte send activeProfileId on
    capture so learned corrections are attributed to the right profile.

  Cleanup prompt contract

  - crates/ai-formatting/src/llm_client.rs hardened: the CLEANUP_PROMPT
    now specifies concrete do/do-not rules, ready for a real model-backed
    cleanup pass. The llm_client is still a stub — kon-llm remains unwired
    — but the prompt shape is final.

  Cross-cutting polish

  - Minor touches in audio (capture/decode/resample), hotkey (lib/linux/stub),
    core, transcription (concurrency/model_manager/local_engine/whisper_rs),
    and the rest of src-tauri/src/commands/*: error-path tightening, log
    clarity, TS-migration follow-ups (@ts-nocheck additions for incremental
    typing).

Verified locally: npm run check, cargo test -p kon-ai-formatting,
cargo test -p kon-storage, cargo test -p kon --lib commands::live::tests,
cargo check — all green.

Scope boundary: kon-llm crate is still a stub; task extraction remains
rule-based. Bundled local-LLM runtime is the next clean step and is not
in this commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 22:39:08 +01:00

72 lines
1.9 KiB
Rust

use std::sync::{Arc, Mutex};
struct LlmState {
loaded: bool,
}
/// Shared handle to the LLM engine. Cheap to clone (Arc).
/// Phase 3 will replace the stub body with a real llama-cpp-2 model.
#[derive(Clone)]
pub struct LlmEngine {
state: Arc<Mutex<LlmState>>,
}
impl LlmEngine {
pub fn new() -> Self {
Self {
state: Arc::new(Mutex::new(LlmState { loaded: false })),
}
}
pub fn is_loaded(&self) -> bool {
self.state.lock().unwrap().loaded
}
/// Break a task description into 3-7 physical micro-steps.
/// Returns Err if no model is loaded — the caller surfaces this to the UI.
pub fn decompose_task(&self, _task_text: &str) -> Result<Vec<String>, String> {
if !self.is_loaded() {
return Err("Download an AI model in Settings to break down tasks.".to_string());
}
// Phase 3: call llama-cpp-2 with GBNF-constrained prompt here.
Err("LLM not yet wired.".to_string())
}
}
impl Default for LlmEngine {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decompose_returns_error_when_not_loaded() {
let engine = LlmEngine::new();
assert!(!engine.is_loaded());
let result = engine.decompose_task("Write a blog post");
assert!(result.is_err());
assert!(
result.unwrap_err().contains("Download an AI model"),
"error message should tell user to download a model"
);
}
#[test]
fn default_creates_unloaded_engine() {
let engine = LlmEngine::default();
assert!(!engine.is_loaded());
}
#[test]
fn engine_is_clone_and_shares_state() {
let engine = LlmEngine::new();
let clone = engine.clone();
// Both point to the same Arc — neither is loaded
assert!(!clone.is_loaded());
}
}