feat #14740: merge speakers and name calls

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-06 11:17:40 +01:00
co-authored by Claude Opus 5.5
parent 5dfe1d9357
commit 345dd01147
12 changed files with 1213 additions and 29 deletions
+40
View File
@@ -146,6 +146,46 @@ pub fn rename_speaker(
Ok(())
}
/// Merges remote speaker `from_key` into `into_key` (undone by
/// [`unmerge_speaker`]).
#[tauri::command]
pub fn merge_speakers(
app: AppHandle,
state: State<'_, AppState>,
id: CallId,
from_key: String,
into_key: String,
) -> CmdResult<()> {
state.merge_speakers(id, &from_key, &into_key)?;
processing::emit_updated(&app, id);
Ok(())
}
#[tauri::command]
pub fn unmerge_speaker(
app: AppHandle,
state: State<'_, AppState>,
id: CallId,
from_key: String,
) -> CmdResult<()> {
state.unmerge_speaker(id, &from_key)?;
processing::emit_updated(&app, id);
Ok(())
}
/// Names a call (an empty or missing title restores the default heading).
#[tauri::command]
pub fn rename_call(
app: AppHandle,
state: State<'_, AppState>,
id: CallId,
title: Option<String>,
) -> CmdResult<()> {
state.rename_call(id, title.as_deref())?;
processing::emit_updated(&app, id);
Ok(())
}
#[tauri::command]
pub fn list_accounts() -> Vec<Account> {
accounts::list()
+3
View File
@@ -115,6 +115,9 @@ pub fn run() {
commands::list_calls,
commands::get_call,
commands::rename_speaker,
commands::merge_speakers,
commands::unmerge_speaker,
commands::rename_call,
commands::list_accounts,
commands::set_account,
])
+142 -2
View File
@@ -441,12 +441,14 @@ impl AppState {
Ok(calls.iter().map(CallListItem::of).collect())
}
/// The selected call with its transcript and speaker names.
/// The selected call with its transcript (speaker merges applied) and
/// speaker names.
pub fn call_detail(&self, id: CallId) -> Result<CallDetail, String> {
let call = self.find_call(id)?;
let segments = self.store.segments(id).map_err(|e| e.to_string())?;
let names = self.store.speaker_names(id).map_err(|e| e.to_string())?;
Ok(CallDetail::of(&call, &segments, names))
let merges = self.store.speaker_merges(id).map_err(|e| e.to_string())?;
Ok(CallDetail::of(&call, &segments, names, &merges))
}
/// Renames a speaker of call `id`; a blank `name` restores the default
@@ -465,6 +467,35 @@ impl AppState {
result.map_err(|e| format!("Name konnte nicht gespeichert werden: {e}"))
}
/// Merges remote speaker `from_key` of call `id` into `into_key`; both
/// must speak in the call's transcript.
pub fn merge_speakers(&self, id: CallId, from_key: &str, into_key: &str) -> Result<(), String> {
self.find_call(id)?;
let segments = self.store.segments(id).map_err(|e| e.to_string())?;
for key in [from_key, into_key] {
if !segments.iter().any(|s| s.speaker.key() == key) {
return Err(format!("Unbekannter Sprecher: {key}"));
}
}
self.store
.set_speaker_merge(id, from_key, into_key)
.map_err(|e| e.to_string())
}
/// Undoes the merge of speaker `from_key` of call `id`.
pub fn unmerge_speaker(&self, id: CallId, from_key: &str) -> Result<(), String> {
self.find_call(id)?;
self.store
.remove_speaker_merge(id, from_key)
.map_err(|e| format!("Zusammenführung konnte nicht aufgehoben werden: {e}"))
}
/// Names call `id`; an empty or missing title clears the name.
pub fn rename_call(&self, id: CallId, title: Option<&str>) -> Result<(), String> {
self.find_call(id)?;
self.store.set_title(id, title).map_err(|e| e.to_string())
}
/// Assigns call `id` to one of the [`accounts`], or to none.
pub fn set_account(&self, id: CallId, account_id: Option<&str>) -> Result<(), String> {
if account_id.is_some_and(|account| !accounts::exists(account)) {
@@ -1072,6 +1103,115 @@ mod tests {
assert!(f.state.store.speaker_names(id).unwrap().is_empty());
}
/// A finished call whose transcript has "Ich" and remote speakers 1 and 2.
fn transcribed_call(f: &Fixture) -> CallId {
let (id, _, _) = finished_call(f, "a");
let line = |speaker, start: f64, text: &str| Segment {
speaker,
start,
end: start + 1.0,
text: text.into(),
};
f.state
.store
.save_segments(
id,
&[
line(Speaker::Remote(1), 0.0, "Guten Tag,"),
line(Speaker::Remote(2), 1.5, "hier Weingut Beispiel."),
line(Speaker::Me, 3.0, "Hallo."),
],
)
.unwrap();
id
}
fn transcript(f: &Fixture, id: CallId) -> Vec<(String, String)> {
f.state
.call_detail(id)
.unwrap()
.segments
.into_iter()
.map(|s| (s.label, s.text))
.collect()
}
#[test]
fn merging_speakers_joins_their_lines_until_undone() {
let f = fixture();
let id = transcribed_call(&f);
let before = transcript(&f, id);
f.state.merge_speakers(id, "remote:2", "remote:1").unwrap();
assert_eq!(
transcript(&f, id),
[
(
"Sprecher 1".into(),
"Guten Tag, hier Weingut Beispiel.".into()
),
("Ich".into(), "Hallo.".into()),
]
);
assert_eq!(f.state.call_detail(id).unwrap().merges.len(), 1);
f.state.unmerge_speaker(id, "remote:2").unwrap();
assert_eq!(transcript(&f, id), before);
assert!(f.state.call_detail(id).unwrap().merges.is_empty());
}
#[test]
fn merging_needs_a_known_call_and_speakers_of_the_call() {
let f = fixture();
let id = transcribed_call(&f);
let merge = |from, into| f.state.merge_speakers(id, from, into);
assert_eq!(
f.state.merge_speakers(id + 1, "remote:2", "remote:1"),
Err("Gespräch nicht gefunden".to_string())
);
assert_eq!(
merge("remote:x", "remote:1"),
Err("Unbekannter Sprecher: remote:x".to_string())
);
assert_eq!(
merge("remote:2", "remote:3"),
Err("Unbekannter Sprecher: remote:3".to_string())
);
assert!(merge("me", "remote:1").is_err());
merge("remote:2", "remote:1").unwrap();
assert_eq!(
merge("remote:1", "remote:2"),
Err("Diese Zuordnung würde eine Schleife ergeben".to_string())
);
assert!(f.state.unmerge_speaker(id + 1, "remote:2").is_err());
assert_eq!(f.state.store.speaker_merges(id).unwrap().len(), 1);
}
#[test]
fn a_call_is_named_and_the_name_cleared() {
let f = fixture();
let (id, _, _) = finished_call(&f, "a");
let title = |f: &Fixture| f.state.call_list().unwrap()[0].title.clone();
f.state.rename_call(id, Some(" Angebot Rioja ")).unwrap();
assert_eq!(title(&f).as_deref(), Some("Angebot Rioja"));
assert_eq!(
f.state.call_detail(id).unwrap().call.item.title.as_deref(),
Some("Angebot Rioja")
);
assert_eq!(
f.state.rename_call(id, Some(&"x".repeat(121))),
Err("Titel zu lang (max. 120 Zeichen)".to_string())
);
f.state.rename_call(id, Some("")).unwrap();
assert_eq!(title(&f), None);
assert_eq!(
f.state.rename_call(id + 1, Some("Test")),
Err("Gespräch nicht gefunden".to_string())
);
}
#[test]
fn the_account_is_set_and_cleared() {
let f = fixture();