Files
Lumotia/src-tauri/src/commands/profiles.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

186 lines
5.3 KiB
Rust

// Tauri commands wrapping kon_storage profile + profile_term CRUD.
// Pattern mirrors tasks.rs — flat imports from `kon_storage` with a `db_`
// alias prefix to avoid name collisions with the command functions, plain
// snake_case parameters (Tauri 2.x auto-converts camelCase JS keys),
// `.map_err(|e| e.to_string())` for error conversion, and camelCase DTOs
// for the frontend-facing shape (the storage row types don't derive
// Serialize — same reason TaskRow → TaskDto exists).
//
// The Default profile is guarded at the storage layer (SQLite triggers +
// Rust pre-checks), so no extra guarding is needed here.
use serde::Serialize;
use kon_ai_formatting::extract_corrections;
use kon_storage::{
add_profile_term as db_add_profile_term, create_profile as db_create_profile,
delete_profile as db_delete_profile, delete_profile_term as db_delete_profile_term,
get_profile as db_get_profile, list_profile_terms as db_list_profile_terms,
list_profiles as db_list_profiles, update_profile as db_update_profile, ProfileRow,
ProfileTermRow,
};
use crate::AppState;
const AUTO_LEARNED_NOTE: &str = "Auto-learned from transcript edit";
/// Frontend-facing profile shape. Matches the object the Svelte profile
/// picker + editor will consume.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProfileDto {
pub id: String,
pub name: String,
pub initial_prompt: String,
pub created_at: String,
}
impl From<ProfileRow> for ProfileDto {
fn from(r: ProfileRow) -> Self {
Self {
id: r.id,
name: r.name,
initial_prompt: r.initial_prompt,
created_at: r.created_at,
}
}
}
/// Frontend-facing profile term (dictionary-style vocabulary hint).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProfileTermDto {
pub id: String,
pub profile_id: String,
pub term: String,
pub note: String,
pub created_at: String,
}
impl From<ProfileTermRow> for ProfileTermDto {
fn from(r: ProfileTermRow) -> Self {
Self {
id: r.id,
profile_id: r.profile_id,
term: r.term,
note: r.note,
created_at: r.created_at,
}
}
}
#[tauri::command]
pub async fn list_profiles_cmd(
state: tauri::State<'_, AppState>,
) -> Result<Vec<ProfileDto>, String> {
db_list_profiles(&state.db)
.await
.map(|rows| rows.into_iter().map(ProfileDto::from).collect())
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn get_profile_cmd(
state: tauri::State<'_, AppState>,
id: String,
) -> Result<Option<ProfileDto>, String> {
db_get_profile(&state.db, &id)
.await
.map(|opt| opt.map(ProfileDto::from))
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn create_profile_cmd(
state: tauri::State<'_, AppState>,
name: String,
initial_prompt: String,
) -> Result<ProfileDto, String> {
db_create_profile(&state.db, &name, &initial_prompt)
.await
.map(ProfileDto::from)
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn update_profile_cmd(
state: tauri::State<'_, AppState>,
id: String,
name: String,
initial_prompt: String,
) -> Result<(), String> {
db_update_profile(&state.db, &id, &name, &initial_prompt)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn delete_profile_cmd(
state: tauri::State<'_, AppState>,
id: String,
) -> Result<(), String> {
db_delete_profile(&state.db, &id)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn list_profile_terms_cmd(
state: tauri::State<'_, AppState>,
profile_id: String,
) -> Result<Vec<ProfileTermDto>, String> {
db_list_profile_terms(&state.db, &profile_id)
.await
.map(|rows| rows.into_iter().map(ProfileTermDto::from).collect())
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn add_profile_term_cmd(
state: tauri::State<'_, AppState>,
profile_id: String,
term: String,
note: String,
) -> Result<ProfileTermDto, String> {
db_add_profile_term(&state.db, &profile_id, &term, &note)
.await
.map(ProfileTermDto::from)
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn learn_profile_terms_from_edit_cmd(
state: tauri::State<'_, AppState>,
profile_id: String,
original_text: String,
edited_text: String,
) -> Result<Vec<ProfileTermDto>, String> {
let existing_terms: Vec<String> = db_list_profile_terms(&state.db, &profile_id)
.await
.map_err(|e| e.to_string())?
.into_iter()
.map(|row| row.term)
.collect();
let corrections = extract_corrections(&original_text, &edited_text, &existing_terms);
let mut learned = Vec::new();
for term in corrections {
let row = db_add_profile_term(&state.db, &profile_id, &term, AUTO_LEARNED_NOTE)
.await
.map_err(|e| e.to_string())?;
learned.push(ProfileTermDto::from(row));
}
Ok(learned)
}
#[tauri::command]
pub async fn delete_profile_term_cmd(
state: tauri::State<'_, AppState>,
id: String,
) -> Result<(), String> {
db_delete_profile_term(&state.db, &id)
.await
.map_err(|e| e.to_string())
}