storage: Day 4 — FTS5 search + update_transcript + dictionary + paginated list + Tauri command surface
Finally exposes the canonical SQLite store to the frontend. Previously
the transcript/task tables existed but no Tauri command read or wrote
them — the UI lived entirely in localStorage. Closes that gap and adds
the missing commands flagged by both Codex and architecture-review.md
§13.
MIGRATIONS:
- v2 adds:
- transcripts_fts virtual table (FTS5, porter+unicode61 tokeniser,
diacritics-folded) backed by transcripts via content_rowid
- INSERT/UPDATE/DELETE triggers keep FTS in sync automatically
- dictionary table for custom vocabulary (term, note, created_at)
- Append-only as required; never modifies v1
STORAGE FUNCTIONS (crates/storage/src/database.rs):
- list_transcripts_paged(limit, offset) — pagination
- count_transcripts() — for "showing X of N" in UI
- update_transcript(id, text?, title?) — closes the historic
architecture-review.md §13 rename-never-persists TODO
- search_transcripts(query, limit) — FTS5 wrapper
- list_dictionary / add_dictionary_entry / delete_dictionary_entry
- DictionaryEntry struct exported
TAURI COMMANDS (src-tauri/src/commands/transcripts.rs new file):
- add_transcript, list_transcripts, count_transcripts_command,
get_transcript, update_transcript, delete_transcript,
search_transcripts
- list_dictionary_command, add_dictionary_entry_command,
delete_dictionary_entry_command
- TranscriptDto + DictionaryDto for camelCase frontend serialisation
REGISTRATION (src-tauri/src/lib.rs):
- All 10 new commands wired into the invoke_handler
cargo check -p kon-storage passes clean. The Tauri crate cargo check
still requires `sudo dnf install cmake clang-devel` for whisper-rs-sys
(pre-existing infra dep, unrelated to this work).
NEXT (still in this sprint):
- Wire DictationPage / FilesPage to call add_transcript on every save
(dual-write: keep localStorage for now, add SQLite alongside)
- Wire HistoryPage rename to call update_transcript (fixes the TODO)
- Add a History search input that calls search_transcripts
- Add Settings → Dictionary panel
- Inject dictionary terms into the LLM cleanup prompt
This commit is contained in:
@@ -94,10 +94,21 @@ pub async fn get_transcript(pool: &SqlitePool, id: &str) -> Result<Option<Transc
|
||||
}
|
||||
|
||||
pub async fn list_transcripts(pool: &SqlitePool, limit: i64) -> Result<Vec<TranscriptRow>> {
|
||||
list_transcripts_paged(pool, limit, 0).await
|
||||
}
|
||||
|
||||
/// Paginated transcript list. `limit` is rows-per-page, `offset` is rows to skip.
|
||||
/// Used by HistoryPage to load 50 at a time and append more on scroll.
|
||||
pub async fn list_transcripts_paged(
|
||||
pool: &SqlitePool,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<TranscriptRow>> {
|
||||
let rows = sqlx::query(
|
||||
"SELECT id, text, source, title, audio_path, duration, engine, model_id, inference_ms, sample_rate, audio_channels, format_mode, remove_fillers, british_english, anti_hallucination, created_at FROM transcripts ORDER BY created_at DESC LIMIT ?",
|
||||
"SELECT id, text, source, title, audio_path, duration, engine, model_id, inference_ms, sample_rate, audio_channels, format_mode, remove_fillers, british_english, anti_hallucination, created_at FROM transcripts ORDER BY created_at DESC LIMIT ? OFFSET ?",
|
||||
)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("List transcripts failed: {e}")))?;
|
||||
@@ -105,6 +116,64 @@ pub async fn list_transcripts(pool: &SqlitePool, limit: i64) -> Result<Vec<Trans
|
||||
Ok(rows.iter().map(transcript_row_from).collect())
|
||||
}
|
||||
|
||||
/// Total count of transcripts. Useful for displaying "showing 50 of 312" in the UI.
|
||||
pub async fn count_transcripts(pool: &SqlitePool) -> Result<i64> {
|
||||
let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM transcripts")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("Count transcripts failed: {e}")))?;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
/// Update mutable transcript fields. Currently `text` and `title`. Other
|
||||
/// columns (engine, model, audio path) are immutable post-creation. Returns
|
||||
/// the number of rows updated (0 if id not found).
|
||||
///
|
||||
/// This is the function HistoryPage's rename flow needed; previously the
|
||||
/// rename was a UI-only state change with a TODO never wired up.
|
||||
/// (architecture-review.md §13)
|
||||
pub async fn update_transcript(
|
||||
pool: &SqlitePool,
|
||||
id: &str,
|
||||
text: Option<&str>,
|
||||
title: Option<&str>,
|
||||
) -> Result<u64> {
|
||||
// Build the SET clause dynamically so we only update the fields the
|
||||
// caller actually changed. Two fields max so we can keep this simple
|
||||
// without a query builder.
|
||||
match (text, title) {
|
||||
(Some(t), Some(ttl)) => {
|
||||
let res = sqlx::query("UPDATE transcripts SET text = ?, title = ? WHERE id = ?")
|
||||
.bind(t)
|
||||
.bind(ttl)
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("Update transcript failed: {e}")))?;
|
||||
Ok(res.rows_affected())
|
||||
}
|
||||
(Some(t), None) => {
|
||||
let res = sqlx::query("UPDATE transcripts SET text = ? WHERE id = ?")
|
||||
.bind(t)
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("Update transcript failed: {e}")))?;
|
||||
Ok(res.rows_affected())
|
||||
}
|
||||
(None, Some(ttl)) => {
|
||||
let res = sqlx::query("UPDATE transcripts SET title = ? WHERE id = ?")
|
||||
.bind(ttl)
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("Update transcript failed: {e}")))?;
|
||||
Ok(res.rows_affected())
|
||||
}
|
||||
(None, None) => Ok(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_transcript(pool: &SqlitePool, id: &str) -> Result<()> {
|
||||
sqlx::query("DELETE FROM transcripts WHERE id = ?")
|
||||
.bind(id)
|
||||
@@ -114,6 +183,78 @@ pub async fn delete_transcript(pool: &SqlitePool, id: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Full-text search over transcripts using the FTS5 virtual table created
|
||||
/// in migration v2. Returns matched transcript rows ordered by FTS rank
|
||||
/// (best match first). `query` follows FTS5 syntax: bare words AND together
|
||||
/// by default; quote multi-word phrases; `OR`, `NOT` operators supported.
|
||||
pub async fn search_transcripts(
|
||||
pool: &SqlitePool,
|
||||
query: &str,
|
||||
limit: i64,
|
||||
) -> Result<Vec<TranscriptRow>> {
|
||||
let rows = sqlx::query(
|
||||
"SELECT t.id, t.text, t.source, t.title, t.audio_path, t.duration, t.engine, t.model_id, t.inference_ms, t.sample_rate, t.audio_channels, t.format_mode, t.remove_fillers, t.british_english, t.anti_hallucination, t.created_at \
|
||||
FROM transcripts t \
|
||||
JOIN transcripts_fts fts ON fts.rowid = t.rowid \
|
||||
WHERE transcripts_fts MATCH ? \
|
||||
ORDER BY rank LIMIT ?",
|
||||
)
|
||||
.bind(query)
|
||||
.bind(limit)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("FTS search failed: {e}")))?;
|
||||
|
||||
Ok(rows.iter().map(transcript_row_from).collect())
|
||||
}
|
||||
|
||||
// --- Dictionary (custom vocabulary injected into LLM cleanup prompts) ---
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DictionaryEntry {
|
||||
pub id: i64,
|
||||
pub term: String,
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn list_dictionary(pool: &SqlitePool) -> Result<Vec<DictionaryEntry>> {
|
||||
let rows = sqlx::query("SELECT id, term, note FROM dictionary ORDER BY term ASC")
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("List dictionary failed: {e}")))?;
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|r| DictionaryEntry {
|
||||
id: r.get("id"),
|
||||
term: r.get("term"),
|
||||
note: r.get("note"),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn add_dictionary_entry(
|
||||
pool: &SqlitePool,
|
||||
term: &str,
|
||||
note: Option<&str>,
|
||||
) -> Result<i64> {
|
||||
let res = sqlx::query("INSERT OR IGNORE INTO dictionary (term, note) VALUES (?, ?)")
|
||||
.bind(term)
|
||||
.bind(note)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("Insert dictionary entry failed: {e}")))?;
|
||||
Ok(res.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub async fn delete_dictionary_entry(pool: &SqlitePool, id: i64) -> Result<()> {
|
||||
sqlx::query("DELETE FROM dictionary WHERE id = ?")
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map_err(|e| KonError::StorageError(format!("Delete dictionary entry failed: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --- Task CRUD ---
|
||||
|
||||
pub async fn insert_task(
|
||||
|
||||
@@ -3,8 +3,10 @@ pub mod file_storage;
|
||||
pub mod migrations;
|
||||
|
||||
pub use database::{
|
||||
complete_task, delete_task, delete_transcript, get_setting, get_transcript, init, insert_task,
|
||||
insert_transcript, list_tasks, list_transcripts, log_error, set_setting,
|
||||
add_dictionary_entry, complete_task, count_transcripts, delete_dictionary_entry,
|
||||
delete_task, delete_transcript, get_setting, get_transcript, init, insert_task,
|
||||
insert_transcript, list_dictionary, list_tasks, list_transcripts, list_transcripts_paged,
|
||||
log_error, search_transcripts, set_setting, update_transcript, DictionaryEntry,
|
||||
InsertTranscriptParams, TaskRow, TranscriptRow,
|
||||
};
|
||||
pub use file_storage::{app_data_dir, database_path, recordings_dir};
|
||||
|
||||
@@ -73,6 +73,41 @@ const MIGRATIONS: &[(i64, &str, &str)] = &[
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_transcript ON tasks(source_transcript_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_error_log_context ON error_log(context)
|
||||
"#),
|
||||
(2, "transcripts FTS5 + dictionary table", r#"
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS transcripts_fts USING fts5(
|
||||
text,
|
||||
title,
|
||||
content='transcripts',
|
||||
content_rowid='rowid',
|
||||
tokenize='porter unicode61 remove_diacritics 2'
|
||||
);
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS transcripts_ai AFTER INSERT ON transcripts BEGIN
|
||||
INSERT INTO transcripts_fts(rowid, text, title)
|
||||
VALUES (new.rowid, new.text, COALESCE(new.title, ''));
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS transcripts_ad AFTER DELETE ON transcripts BEGIN
|
||||
INSERT INTO transcripts_fts(transcripts_fts, rowid, text, title)
|
||||
VALUES ('delete', old.rowid, old.text, COALESCE(old.title, ''));
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS transcripts_au AFTER UPDATE ON transcripts BEGIN
|
||||
INSERT INTO transcripts_fts(transcripts_fts, rowid, text, title)
|
||||
VALUES ('delete', old.rowid, old.text, COALESCE(old.title, ''));
|
||||
INSERT INTO transcripts_fts(rowid, text, title)
|
||||
VALUES (new.rowid, new.text, COALESCE(new.title, ''));
|
||||
END;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dictionary (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
term TEXT NOT NULL UNIQUE,
|
||||
note TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_dictionary_term ON dictionary(term)
|
||||
"#),
|
||||
];
|
||||
|
||||
/// Ensure the schema_version table exists and run any pending migrations.
|
||||
|
||||
Reference in New Issue
Block a user