This repository has been archived on 2026-10-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-voice-ios/Sources/LiveKit/TranscriptHistory.swift
T

44 lines
1.3 KiB
Swift

import Foundation
struct HistoryEntry: Codable, Equatable {
let role: String
let text: String
let timestamp: Date
}
/// Last 50 final transcript entries persisted as JSON in UserDefaults.
@MainActor
final class TranscriptHistory: ObservableObject {
@Published private(set) var entries: [HistoryEntry] = []
private static let key = "voice_history"
private static let maxEntries = 50
init() {
if let raw = UserDefaults.standard.string(forKey: Self.key),
let data = raw.data(using: .utf8),
let decoded = try? JSONDecoder().decode([HistoryEntry].self, from: data) {
entries = Array(decoded.suffix(Self.maxEntries))
}
}
func add(role: String, text: String) {
guard !text.isEmpty else { return }
entries.append(HistoryEntry(role: role, text: text, timestamp: Date()))
if entries.count > Self.maxEntries {
entries.removeFirst(entries.count - Self.maxEntries)
}
persist()
}
func clear() {
entries.removeAll()
persist()
}
private func persist() {
guard let data = try? JSONEncoder().encode(entries) else { return }
UserDefaults.standard.set(String(decoding: data, as: UTF8.self), forKey: Self.key)
}
}