Files
ten31-transcripts/Ten31Transcripts/Session/TranscriptAssembler.swift
T
Grant Gilliam 4c086251d9 Speaker corrections: rename / merge / reassign + voice learning
Native editor to fix speaker-ID errors after transcription (modeled on recap-relay's
correction UX): rename a speaker in the legend, merge two speakers, or reassign an
individual transcript line. Saving rewrites speakers.json, re-renders transcript.md +
recap.html, and updates the voiceprint memory — so a correction compounds: naming an
"Unknown" speaker teaches that voice for future calls.

- SpeakerEditing (pure, tested): replaceSpeaker (rename = merge-onto-existing),
  reassign, netNameMap (compose ops), and remap (apply a name map to a recap's
  structured fields + whole-word free text, so summaries/extras update without re-LLM).
- RecapEditModel (@MainActor): loads speakers.json (+ optional recap.json +
  cluster_fingerprints.json); on save writes the resolved speakers.json, re-renders,
  and reconciles voiceprints — merge keeps the survivor's print; rename/name-an-Unknown
  enrolls the cluster's fingerprint under the new name.
- TranscriptEditorView (SwiftUI) + EditorWindow (AppKit window for the LSUIElement app);
  menu gains "Edit speakers".
- Pipeline now persists cluster_fingerprints.json (every cluster incl. Unknown) and
  recap.json (RecapFile) so the editor can learn voices + re-render offline.
- RecapModels made Codable; TranscriptAssembler exposes allFingerprints;
  VoiceprintStore gains enroll() + merge().

52/52 XCTest (6 new, incl. a full rename→artifacts→voiceprint round-trip on disk).
2026-06-06 15:12:23 -05:00

84 lines
3.6 KiB
Swift

import Foundation
/// Concatenates per-chunk `label-merge` results into one global `speakers.json`:
/// segment times offset back to global seconds, speakers unified across chunks by
/// name, and fingerprints collected for the voiceprint store.
enum TranscriptAssembler {
struct ChunkResult {
let chunkStart: Double // global seconds
let response: LabelMergeResponse
}
struct Assembled {
let speakersFile: SpeakersFile
let fingerprints: [String: [Float]] // confidently-named only, for VoiceprintStore
let allFingerprints: [String: [Float]] // EVERY cluster incl. Unknown for editor voice-learning
}
/// Source ranking when the same name appears across chunks with different sources.
/// `mic_channel` (the local user's own microphone) is the most authoritative.
private static func rank(_ source: String) -> Int {
switch source {
case "mic_channel": return 4
case "visual": return 3
case "voiceprint": return 2
default: return 1 // unmatched
}
}
private static func isUnknown(_ name: String) -> Bool {
LabelMergeResponse.isUnknownName(name)
}
static func assemble(sessionId: String, app: String, chunks: [ChunkResult]) -> Assembled {
var segments: [SpeakersFile.Segment] = []
var bestSpeaker: [String: SpeakersFile.Speaker] = [:]
var fingerprints: [String: [Float]] = [:]
var allFingerprints: [String: [Float]] = [:]
var models: [String: String] = [:]
var duration = 0.0
for chunk in chunks {
let offset = chunk.chunkStart
// Audio length from the chunk window, so silent/all-unknown calls still
// report a real duration (not just the last segment's end).
duration = max(duration, offset + chunk.response.duration)
for seg in chunk.response.segments {
let start = seg.startSeconds + offset
let end = seg.endSeconds + offset
segments.append(.init(start: start, end: end, speaker: seg.speaker, text: seg.text))
duration = max(duration, end)
}
for sp in chunk.response.speakers {
let candidate = SpeakersFile.Speaker(
name: sp.name, source: sp.source,
overlapConfidence: sp.overlapConfidence, matchSimilarity: sp.matchSimilarity)
if let existing = bestSpeaker[sp.name] {
if rank(sp.source) > rank(existing.source) { bestSpeaker[sp.name] = candidate }
} else {
bestSpeaker[sp.name] = candidate
}
if let fp = sp.fingerprint, fp.count > 0 {
allFingerprints[sp.name] = fp // every cluster, for the editor
if !isUnknown(sp.name) { fingerprints[sp.name] = fp } // named only, for the store
}
}
for (name, fp) in chunk.response.fingerprints where fp.count > 0 {
allFingerprints[name] = fp
if !isUnknown(name) { fingerprints[name] = fp }
}
}
segments.sort { $0.start < $1.start }
let speakers = bestSpeaker.values.sorted { $0.name < $1.name }
models = chunks.last?.response.models ?? [:]
let file = SpeakersFile(
sessionId: sessionId, app: app, durationSec: duration,
speakers: speakers, segments: segments, models: models)
return Assembled(speakersFile: file, fingerprints: fingerprints, allFingerprints: allFingerprints)
}
}