Add project save/load and improve AI chat formatting
Project persistence: - save_project_transcript command: persists segments, speakers, words to SQLite - load_project_transcript command: loads full transcript with nested words - delete_project command: soft-delete projects - Auto-save after pipeline completes (named from filename) - Project dropdown in header to switch between saved transcripts - Projects load audio, segments, and speakers from database AI chat improvements: - Markdown rendering in assistant messages (headers, lists, bold, italic, code) - Better message spacing and visual distinction (border-left accents) - Styled markdown elements matching dark theme - Improved empty state and quick action button sizing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,72 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::State;
|
||||
|
||||
use crate::db::models::Project;
|
||||
use crate::db::queries;
|
||||
use crate::state::AppState;
|
||||
|
||||
// ── Input types for save_project_transcript ──────────────────────
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct WordInput {
|
||||
pub word: String,
|
||||
pub start_ms: i64,
|
||||
pub end_ms: i64,
|
||||
pub confidence: f64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SegmentInput {
|
||||
pub text: String,
|
||||
pub start_ms: i64,
|
||||
pub end_ms: i64,
|
||||
pub speaker: Option<String>, // speaker label, not id
|
||||
pub words: Vec<WordInput>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SpeakerInput {
|
||||
pub label: String,
|
||||
pub color: String,
|
||||
}
|
||||
|
||||
// ── Output types for load_project_transcript ─────────────────────
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct WordOutput {
|
||||
pub word: String,
|
||||
pub start_ms: i64,
|
||||
pub end_ms: i64,
|
||||
pub confidence: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct SegmentOutput {
|
||||
pub id: String,
|
||||
pub text: String,
|
||||
pub start_ms: i64,
|
||||
pub end_ms: i64,
|
||||
pub speaker: Option<String>, // speaker label
|
||||
pub words: Vec<WordOutput>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct SpeakerOutput {
|
||||
pub id: String,
|
||||
pub label: String,
|
||||
pub display_name: Option<String>,
|
||||
pub color: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ProjectTranscript {
|
||||
pub file_path: String,
|
||||
pub segments: Vec<SegmentOutput>,
|
||||
pub speakers: Vec<SpeakerOutput>,
|
||||
}
|
||||
|
||||
// ── Commands ─────────────────────────────────────────────────────
|
||||
|
||||
#[tauri::command]
|
||||
pub fn create_project(name: String, state: State<AppState>) -> Result<Project, String> {
|
||||
let conn = state.db.lock().map_err(|e| e.to_string())?;
|
||||
@@ -21,3 +84,152 @@ pub fn list_projects(state: State<AppState>) -> Result<Vec<Project>, String> {
|
||||
let conn = state.db.lock().map_err(|e| e.to_string())?;
|
||||
queries::list_projects(&conn).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn delete_project(id: String, state: State<AppState>) -> Result<(), String> {
|
||||
let conn = state.db.lock().map_err(|e| e.to_string())?;
|
||||
queries::delete_project(&conn, &id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_project_transcript(
|
||||
project_id: String,
|
||||
file_path: String,
|
||||
segments: Vec<SegmentInput>,
|
||||
speakers: Vec<SpeakerInput>,
|
||||
state: State<AppState>,
|
||||
) -> Result<Project, String> {
|
||||
let conn = state.db.lock().map_err(|e| e.to_string())?;
|
||||
|
||||
// 1. Create media file entry
|
||||
let media_file =
|
||||
queries::create_media_file(&conn, &project_id, &file_path).map_err(|e| e.to_string())?;
|
||||
|
||||
// 2. Create speaker entries and build label -> id map
|
||||
let mut speaker_map = std::collections::HashMap::new();
|
||||
for speaker_input in &speakers {
|
||||
let speaker = queries::create_speaker(
|
||||
&conn,
|
||||
&project_id,
|
||||
&speaker_input.label,
|
||||
Some(&speaker_input.color),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
speaker_map.insert(speaker_input.label.clone(), speaker.id);
|
||||
}
|
||||
|
||||
// 3. Create segments with words
|
||||
for (index, seg_input) in segments.iter().enumerate() {
|
||||
let speaker_id = seg_input
|
||||
.speaker
|
||||
.as_ref()
|
||||
.and_then(|label| speaker_map.get(label));
|
||||
|
||||
let segment = queries::create_segment(
|
||||
&conn,
|
||||
&project_id,
|
||||
&media_file.id,
|
||||
speaker_id.map(|s| s.as_str()),
|
||||
seg_input.start_ms,
|
||||
seg_input.end_ms,
|
||||
&seg_input.text,
|
||||
index as i32,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// Create words for this segment
|
||||
for (word_index, word_input) in seg_input.words.iter().enumerate() {
|
||||
queries::create_word(
|
||||
&conn,
|
||||
&segment.id,
|
||||
&word_input.word,
|
||||
word_input.start_ms,
|
||||
word_input.end_ms,
|
||||
Some(word_input.confidence),
|
||||
word_index as i32,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Return updated project info
|
||||
queries::get_project(&conn, &project_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| "Project not found".to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn load_project_transcript(
|
||||
project_id: String,
|
||||
state: State<AppState>,
|
||||
) -> Result<Option<ProjectTranscript>, String> {
|
||||
let conn = state.db.lock().map_err(|e| e.to_string())?;
|
||||
|
||||
// 1. Get media files for the project
|
||||
let media_files =
|
||||
queries::get_media_files_for_project(&conn, &project_id).map_err(|e| e.to_string())?;
|
||||
|
||||
let media_file = match media_files.first() {
|
||||
Some(mf) => mf,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
// 2. Get speakers for the project and build id -> label map
|
||||
let speakers =
|
||||
queries::get_speakers_for_project(&conn, &project_id).map_err(|e| e.to_string())?;
|
||||
let speaker_label_map: std::collections::HashMap<String, String> = speakers
|
||||
.iter()
|
||||
.map(|s| (s.id.clone(), s.label.clone()))
|
||||
.collect();
|
||||
|
||||
// 3. Get segments for the media file
|
||||
let db_segments =
|
||||
queries::get_segments_for_media(&conn, &media_file.id).map_err(|e| e.to_string())?;
|
||||
|
||||
// 4. Build output segments with nested words
|
||||
let mut segment_outputs = Vec::with_capacity(db_segments.len());
|
||||
for seg in &db_segments {
|
||||
let words = queries::get_words_for_segment(&conn, &seg.id).map_err(|e| e.to_string())?;
|
||||
let word_outputs: Vec<WordOutput> = words
|
||||
.into_iter()
|
||||
.map(|w| WordOutput {
|
||||
word: w.word,
|
||||
start_ms: w.start_ms,
|
||||
end_ms: w.end_ms,
|
||||
confidence: w.confidence,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let speaker_label = seg
|
||||
.speaker_id
|
||||
.as_ref()
|
||||
.and_then(|sid| speaker_label_map.get(sid))
|
||||
.cloned();
|
||||
|
||||
segment_outputs.push(SegmentOutput {
|
||||
id: seg.id.clone(),
|
||||
text: seg.text.clone(),
|
||||
start_ms: seg.start_ms,
|
||||
end_ms: seg.end_ms,
|
||||
speaker: speaker_label,
|
||||
words: word_outputs,
|
||||
});
|
||||
}
|
||||
|
||||
// 5. Build speaker outputs
|
||||
let speaker_outputs: Vec<SpeakerOutput> = speakers
|
||||
.into_iter()
|
||||
.map(|s| SpeakerOutput {
|
||||
id: s.id,
|
||||
label: s.label,
|
||||
display_name: s.display_name,
|
||||
color: s.color,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Some(ProjectTranscript {
|
||||
file_path: media_file.file_path.clone(),
|
||||
segments: segment_outputs,
|
||||
speakers: speaker_outputs,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -85,6 +85,57 @@ pub fn delete_project(conn: &Connection, id: &str) -> Result<(), DatabaseError>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Media Files ──────────────────────────────────────────────────
|
||||
|
||||
pub fn create_media_file(
|
||||
conn: &Connection,
|
||||
project_id: &str,
|
||||
file_path: &str,
|
||||
) -> Result<MediaFile, DatabaseError> {
|
||||
let id = Uuid::new_v4().to_string();
|
||||
let now = Utc::now().to_rfc3339();
|
||||
conn.execute(
|
||||
"INSERT INTO media_files (id, project_id, file_path, created_at) VALUES (?1, ?2, ?3, ?4)",
|
||||
params![id, project_id, file_path, now],
|
||||
)?;
|
||||
Ok(MediaFile {
|
||||
id,
|
||||
project_id: project_id.to_string(),
|
||||
file_path: file_path.to_string(),
|
||||
file_hash: None,
|
||||
duration_ms: None,
|
||||
sample_rate: None,
|
||||
channels: None,
|
||||
format: None,
|
||||
file_size: None,
|
||||
created_at: now,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_media_files_for_project(
|
||||
conn: &Connection,
|
||||
project_id: &str,
|
||||
) -> Result<Vec<MediaFile>, DatabaseError> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT id, project_id, file_path, file_hash, duration_ms, sample_rate, channels, format, file_size, created_at FROM media_files WHERE project_id = ?1 ORDER BY created_at",
|
||||
)?;
|
||||
let rows = stmt.query_map(params![project_id], |row| {
|
||||
Ok(MediaFile {
|
||||
id: row.get(0)?,
|
||||
project_id: row.get(1)?,
|
||||
file_path: row.get(2)?,
|
||||
file_hash: row.get(3)?,
|
||||
duration_ms: row.get(4)?,
|
||||
sample_rate: row.get(5)?,
|
||||
channels: row.get(6)?,
|
||||
format: row.get(7)?,
|
||||
file_size: row.get(8)?,
|
||||
created_at: row.get(9)?,
|
||||
})
|
||||
})?;
|
||||
Ok(rows.collect::<Result<Vec<_>, _>>()?)
|
||||
}
|
||||
|
||||
// ── Speakers ──────────────────────────────────────────────────────
|
||||
|
||||
pub fn create_speaker(
|
||||
@@ -194,6 +245,39 @@ pub fn reassign_speaker(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Segments (create) ────────────────────────────────────────────
|
||||
|
||||
pub fn create_segment(
|
||||
conn: &Connection,
|
||||
project_id: &str,
|
||||
media_file_id: &str,
|
||||
speaker_id: Option<&str>,
|
||||
start_ms: i64,
|
||||
end_ms: i64,
|
||||
text: &str,
|
||||
segment_index: i32,
|
||||
) -> Result<Segment, DatabaseError> {
|
||||
let id = Uuid::new_v4().to_string();
|
||||
conn.execute(
|
||||
"INSERT INTO segments (id, project_id, media_file_id, speaker_id, start_ms, end_ms, text, is_edited, segment_index) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 0, ?8)",
|
||||
params![id, project_id, media_file_id, speaker_id, start_ms, end_ms, text, segment_index],
|
||||
)?;
|
||||
Ok(Segment {
|
||||
id,
|
||||
project_id: project_id.to_string(),
|
||||
media_file_id: media_file_id.to_string(),
|
||||
speaker_id: speaker_id.map(String::from),
|
||||
start_ms,
|
||||
end_ms,
|
||||
text: text.to_string(),
|
||||
original_text: None,
|
||||
confidence: None,
|
||||
is_edited: false,
|
||||
edited_at: None,
|
||||
segment_index,
|
||||
})
|
||||
}
|
||||
|
||||
// ── Words ─────────────────────────────────────────────────────────
|
||||
|
||||
pub fn get_words_for_segment(
|
||||
@@ -217,6 +301,31 @@ pub fn get_words_for_segment(
|
||||
Ok(rows.collect::<Result<Vec<_>, _>>()?)
|
||||
}
|
||||
|
||||
pub fn create_word(
|
||||
conn: &Connection,
|
||||
segment_id: &str,
|
||||
word: &str,
|
||||
start_ms: i64,
|
||||
end_ms: i64,
|
||||
confidence: Option<f64>,
|
||||
word_index: i32,
|
||||
) -> Result<Word, DatabaseError> {
|
||||
let id = Uuid::new_v4().to_string();
|
||||
conn.execute(
|
||||
"INSERT INTO words (id, segment_id, word, start_ms, end_ms, confidence, word_index) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||
params![id, segment_id, word, start_ms, end_ms, confidence, word_index],
|
||||
)?;
|
||||
Ok(Word {
|
||||
id,
|
||||
segment_id: segment_id.to_string(),
|
||||
word: word.to_string(),
|
||||
start_ms,
|
||||
end_ms,
|
||||
confidence,
|
||||
word_index,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -9,7 +9,10 @@ use tauri::Manager;
|
||||
|
||||
use commands::ai::{ai_chat, ai_configure, ai_list_providers};
|
||||
use commands::export::export_transcript;
|
||||
use commands::project::{create_project, get_project, list_projects};
|
||||
use commands::project::{
|
||||
create_project, delete_project, get_project, list_projects, load_project_transcript,
|
||||
save_project_transcript,
|
||||
};
|
||||
use commands::settings::{load_settings, save_settings};
|
||||
use commands::system::{get_data_dir, llama_list_models, llama_start, llama_status, llama_stop};
|
||||
use commands::transcribe::{download_diarize_model, run_pipeline, transcribe_file};
|
||||
@@ -34,6 +37,9 @@ pub fn run() {
|
||||
create_project,
|
||||
get_project,
|
||||
list_projects,
|
||||
delete_project,
|
||||
save_project_transcript,
|
||||
load_project_transcript,
|
||||
transcribe_file,
|
||||
run_pipeline,
|
||||
download_diarize_model,
|
||||
|
||||
Reference in New Issue
Block a user