forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeterministicConversationMinimum.swift
More file actions
92 lines (82 loc) · 3.05 KB
/
Copy pathDeterministicConversationMinimum.swift
File metadata and controls
92 lines (82 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Foundation
/// Client-side copy of W1 §1.7. Pure: same input, same title; the only clock
/// is `startedAt`. This is the fail-closed result when no local engine can run.
/// Overview stays empty — never a fabricated summary that looks like content.
struct DeterministicConversationMinimum: Sendable, Equatable, Codable {
var title: String
var overview: String
var category: String
var processingState: String
static let emptyOverview = ""
static let requiredCategory = "other"
static let terminalProcessingState = "none"
static let titleCharacterBudget = 60
static func make(from input: DeterministicMinimumInput) -> DeterministicConversationMinimum {
DeterministicConversationMinimum(
title: Self.title(from: input),
overview: emptyOverview,
category: requiredCategory,
processingState: terminalProcessingState
)
}
static func title(from input: DeterministicMinimumInput) -> String {
let sentence = firstNonEmptySentence(input.transcript)
if sentence.isEmpty {
return fallbackTitle(sourceLabel: input.sourceLabel, startedAt: input.startedAt, timeZone: input.timeZone)
}
return truncateToWordBoundary(sentence, maxCharacters: titleCharacterBudget)
}
}
struct DeterministicMinimumInput: Sendable, Equatable {
var transcript: String
var startedAt: Date
var sourceLabel: String
var timeZone: TimeZone
init(
transcript: String,
startedAt: Date,
sourceLabel: String = "Recording",
timeZone: TimeZone = .current
) {
self.transcript = transcript
self.startedAt = startedAt
self.sourceLabel = sourceLabel
self.timeZone = timeZone
}
}
private func firstNonEmptySentence(_ transcript: String) -> String {
let collapsed =
transcript
.replacingOccurrences(of: "\r\n", with: "\n")
.components(separatedBy: .newlines)
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.joined(separator: " ")
guard !collapsed.isEmpty else { return "" }
var sentence = ""
for character in collapsed {
sentence.append(character)
if character == "." || character == "!" || character == "?" {
break
}
}
return sentence.trimmingCharacters(in: .whitespacesAndNewlines)
}
private func truncateToWordBoundary(_ text: String, maxCharacters: Int) -> String {
if text.count <= maxCharacters { return text }
let end = text.index(text.startIndex, offsetBy: maxCharacters)
let prefix = String(text[..<end])
guard let lastSpace = prefix.lastIndex(of: " "), lastSpace > prefix.startIndex else {
return prefix
}
return String(prefix[..<lastSpace]).trimmingCharacters(in: .whitespaces)
}
private func fallbackTitle(sourceLabel: String, startedAt: Date, timeZone: TimeZone) -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = timeZone
formatter.dateFormat = "h:mm a"
let label = sourceLabel.trimmingCharacters(in: .whitespacesAndNewlines)
let source = label.isEmpty ? "Recording" : label
return "\(source) · \(formatter.string(from: startedAt))"
}