forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationFinalizationService.swift
More file actions
583 lines (541 loc) · 21.8 KB
/
Copy pathConversationFinalizationService.swift
File metadata and controls
583 lines (541 loc) · 21.8 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import Foundation
actor ConversationFinalizationService {
static let shared = ConversationFinalizationService()
private let maxRetries = 5
private let maxLocalFallbackRetries = 3
private var apiClient = APIClient.shared
private init() {}
func setAPIClientForTesting(_ client: APIClient?) {
apiClient = client ?? APIClient.shared
}
func finalizeSession(
id sessionId: Int64,
reason: TranscriptionFinalizationReason,
allowCloudForceProcess: Bool = false
) async {
do {
guard let session = try await TranscriptionStorage.shared.getSession(id: sessionId) else {
return
}
await finalizeSession(session, reason: reason, allowCloudForceProcess: allowCloudForceProcess)
} catch {
logError("ConversationFinalization: Failed to load session \(sessionId)", error: error)
}
}
func recoverPendingFinalizations() async {
do {
let sessions = try await TranscriptionStorage.shared.getSessionsNeedingFinalization(maxRetries: maxRetries)
let exhaustedLocalFallbackSessions = try await TranscriptionStorage.shared
.getExhaustedCloudSessionsWithLocalSegments(
maxRetries: maxRetries,
maxLocalFallbackRetries: maxLocalFallbackRetries
)
let sessionsById = Dictionary(
grouping: sessions + exhaustedLocalFallbackSessions,
by: { $0.id ?? -1 }
).compactMap { $0.value.first }
if !sessionsById.isEmpty {
log(
"ConversationFinalization: Recovering \(sessionsById.count) pending sessions (\(exhaustedLocalFallbackSessions.count) exhausted cloud sessions have local fallback data)"
)
}
let exhaustedLocalFallbackIds = Set(exhaustedLocalFallbackSessions.compactMap(\.id))
for session in sessionsById
where session.isReadyForRetry() || session.status != .failed || session.retryCount >= maxRetries {
if let sessionId = session.id, exhaustedLocalFallbackIds.contains(sessionId) {
await finalizeExhaustedCloudSessionFromLocalSegments(session)
continue
}
await finalizeSession(
session,
reason: .retry,
allowCloudForceProcess: session.backendId?.isEmpty == false
)
}
} catch {
logError("ConversationFinalization: Recovery failed", error: error)
}
}
private func finalizeExhaustedCloudSessionFromLocalSegments(_ session: TranscriptionSessionRecord) async {
guard let sessionId = session.id else { return }
guard session.status != .completed && !session.backendSynced else { return }
log("ConversationFinalization: Retrying exhausted cloud session \(sessionId) from saved local segments")
do {
guard try await TranscriptionStorage.shared.markSessionUploading(id: sessionId) else {
return
}
guard let latestSession = try await TranscriptionStorage.shared.getSession(id: sessionId) else {
throw TranscriptionStorageError.sessionNotFound
}
guard try await resolveExhaustedCloudReconciliation(session: latestSession, sessionId: sessionId) else {
throw TranscriptionStorageError.invalidState("Exhausted cloud session has no local fallback")
}
} catch {
await markRetryableFailure(sessionId: sessionId, error: error)
}
}
private func finalizeSession(
_ session: TranscriptionSessionRecord,
reason: TranscriptionFinalizationReason,
allowCloudForceProcess: Bool
) async {
guard let sessionId = session.id else { return }
guard session.status != .completed && !session.backendSynced else { return }
let strategy = session.finalizationStrategy ?? defaultStrategy(for: session)
log(
"ConversationFinalization: Finalizing session \(sessionId) strategy=\(strategy.rawValue) reason=\(reason.rawValue)"
)
do {
guard try await TranscriptionStorage.shared.markSessionUploading(id: sessionId) else {
return
}
switch strategy {
case .localSegments:
try await uploadLocalSegments(sessionId: sessionId)
case .cloudReconcile:
guard let latestSession = try await TranscriptionStorage.shared.getSession(id: sessionId) else {
throw TranscriptionStorageError.sessionNotFound
}
try await finalizeCloudSession(session: latestSession, allowForceProcess: allowCloudForceProcess)
}
} catch {
await markRetryableFailure(sessionId: sessionId, error: error)
}
}
private func defaultStrategy(for session: TranscriptionSessionRecord) -> TranscriptionFinalizationStrategy {
if session.backendId?.isEmpty == false {
return .cloudReconcile
}
return session.source == ConversationSource.desktop.rawValue ? .localSegments : .cloudReconcile
}
private func uploadLocalSegments(sessionId: Int64, allowBackendIdOverride: Bool = false) async throws {
guard let bundle = try await TranscriptionStorage.shared.getSessionWithSegments(id: sessionId) else {
throw TranscriptionStorageError.sessionNotFound
}
guard !bundle.segments.isEmpty else {
log("ConversationFinalization: Deleting empty local session \(sessionId)")
try await TranscriptionStorage.shared.deleteSession(id: sessionId)
return
}
var merged: [APIClient.UploadSegment] = []
for seg in bundle.segments {
let upload = APIClient.UploadSegment(
text: seg.text,
speaker: seg.speakerLabel ?? String(format: "SPEAKER_%02d", seg.speaker),
speaker_id: seg.speaker,
is_user: seg.isUser,
person_id: seg.personId,
start: seg.startTime,
end: seg.endTime
)
if let last = merged.last,
last.speaker_id == upload.speaker_id,
last.speaker == upload.speaker,
last.is_user == upload.is_user,
last.person_id == upload.person_id
{
merged[merged.count - 1] = APIClient.UploadSegment(
text: last.text + " " + upload.text,
speaker: last.speaker,
speaker_id: last.speaker_id,
is_user: last.is_user,
person_id: last.person_id,
start: last.start,
end: upload.end
)
} else {
merged.append(upload)
}
}
let uploadSegments = Self.compactSegmentsForBackendLimit(merged)
if uploadSegments.count != merged.count {
log(
"ConversationFinalization: Compacted local session \(sessionId) from \(merged.count) to \(uploadSegments.count) segments for backend upload"
)
}
let iso = ISO8601DateFormatter()
let request = APIClient.CreateConversationFromSegmentsRequest(
transcript_segments: uploadSegments,
source: bundle.session.source,
started_at: iso.string(from: bundle.session.startedAt),
finished_at: bundle.session.finishedAt.map { iso.string(from: $0) },
language: bundle.session.language,
client_conversation_id: Self.localClientConversationId(session: bundle.session, sessionId: sessionId)
)
let response = try await apiClient.createConversationFromSegments(request)
let status = LocalConversationStatus(rawValue: response.status) ?? .processing
let completed = try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: response.id,
conversationStatus: status,
allowBackendIdOverride: allowBackendIdOverride
)
guard completed else {
if let latest = try await TranscriptionStorage.shared.getSession(id: sessionId),
latest.status == .completed,
latest.backendSynced
{
return
}
throw TranscriptionStorageError.invalidState(
"from-segments returned \(response.id) but local completion was rejected"
)
}
await hydrateUploadedLocalConversation(id: response.id)
log("ConversationFinalization: Uploaded local session \(sessionId) -> backend conversation \(response.id)")
}
private func hydrateUploadedLocalConversation(id conversationId: String) async {
do {
let conversation = try await apiClient.getConversation(id: conversationId)
_ = try await TranscriptionStorage.shared.syncServerConversation(conversation)
log("ConversationFinalization: Hydrated uploaded local conversation \(conversationId)")
} catch {
logError(
"ConversationFinalization: Failed to hydrate uploaded local conversation \(conversationId)",
error: error
)
}
}
static func compactSegmentsForBackendLimit(
_ segments: [APIClient.UploadSegment],
maxSegments: Int = 500
) -> [APIClient.UploadSegment] {
guard maxSegments > 0, segments.count > maxSegments else { return segments }
var compacted: [APIClient.UploadSegment] = []
compacted.reserveCapacity(maxSegments)
for index in 0..<maxSegments {
let startIndex = index * segments.count / maxSegments
let endIndex = (index + 1) * segments.count / maxSegments
let group = Array(segments[startIndex..<endIndex])
guard let first = group.first, let last = group.last else { continue }
let sameSpeaker = group.allSatisfy { segment in
segment.speaker == first.speaker
&& segment.speaker_id == first.speaker_id
&& segment.is_user == first.is_user
&& segment.person_id == first.person_id
}
compacted.append(
APIClient.UploadSegment(
text: group.map(\.text).joined(separator: " "),
speaker: sameSpeaker ? first.speaker : "MIXED",
speaker_id: sameSpeaker ? first.speaker_id : nil,
is_user: sameSpeaker ? first.is_user : false,
person_id: sameSpeaker ? first.person_id : nil,
start: first.start,
end: last.end
)
)
}
return compacted
}
private func finalizeCloudSession(
session: TranscriptionSessionRecord,
allowForceProcess: Bool
) async throws {
guard let sessionId = session.id else { return }
if let backendId = session.backendId, !backendId.isEmpty {
if let clientConversationId = session.clientConversationId,
!clientConversationId.isEmpty,
backendId != clientConversationId
{
log(
"ConversationFinalization: Rejecting mismatched backend binding for session \(sessionId); resolving exact client recording id instead"
)
if try await completeCloudConversation(
id: clientConversationId,
sessionId: sessionId,
allowForceProcess: allowForceProcess,
allowBackendIdOverride: true
) {
return
}
throw TranscriptionStorageError.invalidState(
"Bound backend conversation conflicts with client recording identity")
}
let conversation: ServerConversation
if allowForceProcess {
conversation = try await apiClient.finalizeConversation(id: backendId)
} else {
conversation = try await apiClient.getConversation(id: backendId)
}
if DesktopConversationMatchPolicy.canCompleteBoundBackendConversation(
id: conversation.id,
boundBackendId: backendId,
status: conversation.status,
source: conversation.source
) {
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status
)
log("ConversationFinalization: Finalized cloud session \(sessionId) by backend id \(conversation.id)")
return
}
throw TranscriptionStorageError.invalidState("Bound backend conversation is not completed")
}
if let clientConversationId = session.clientConversationId, !clientConversationId.isEmpty {
if try await completeCloudConversation(
id: clientConversationId,
sessionId: sessionId,
allowForceProcess: true
) {
return
}
}
if allowForceProcess, let conversation = try await apiClient.forceProcessConversation() {
if DesktopConversationMatchPolicy.matchesDesktopConversation(
startedAt: conversation.startedAt,
source: conversation.source,
sessionStartedAt: session.startedAt
) {
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status
)
log("ConversationFinalization: Force-processed unbound cloud session \(sessionId) -> \(conversation.id)")
return
}
}
let finishedAt = session.finishedAt ?? session.startedAt.addingTimeInterval(1)
let existing = try await apiClient.getConversations(
limit: 5,
statuses: DesktopConversationMatchPolicy.cloudReconciliationStatuses,
includeDiscarded: true,
startDate: session.startedAt.addingTimeInterval(-5),
endDate: finishedAt.addingTimeInterval(5)
)
let timestampMatches = existing.filter { conv in
DesktopConversationMatchPolicy.matchesDesktopConversation(
startedAt: conv.startedAt,
source: conv.source,
sessionStartedAt: session.startedAt
)
}
for match in timestampMatches {
if try await completeTimestampMatchedConversation(match, sessionId: sessionId) {
return
}
}
if session.retryCount >= maxRetries - 1 {
if let clientConversationId = session.clientConversationId, !clientConversationId.isEmpty {
if try await completeCloudConversation(
id: clientConversationId,
sessionId: sessionId,
allowForceProcess: true
) {
return
}
}
if try await resolveExhaustedCloudReconciliation(session: session, sessionId: sessionId) {
return
}
}
throw TranscriptionStorageError.invalidState("No matching backend conversation found")
}
private func completeTimestampMatchedConversation(
_ match: ServerConversation,
sessionId: Int64
) async throws -> Bool {
let conversation: ServerConversation
if DesktopConversationMatchPolicy.shouldFinalizeTimestampMatchedConversation(status: match.status) {
conversation = try await apiClient.finalizeConversation(id: match.id)
} else {
conversation = match
}
guard
DesktopConversationMatchPolicy.canCompleteTimestampMatchedConversation(
status: conversation.status,
source: conversation.source
), conversation.id == match.id
else {
return false
}
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status
)
log("ConversationFinalization: Reconciled cloud session \(sessionId) by timestamp \(conversation.id)")
return true
}
@discardableResult
func resolveExhaustedCloudReconciliation(
session: TranscriptionSessionRecord,
sessionId: Int64
) async throws -> Bool {
let segmentCount = try await TranscriptionStorage.shared.getSegmentCount(sessionId: sessionId)
switch Self.cloudReconciliationExhaustionAction(session: session, segmentCount: segmentCount) {
case .keepRetrying:
return false
case .uploadLocalSegments:
log(
"ConversationFinalization: Cloud reconciliation exhausted for session \(sessionId); uploading \(segmentCount) saved local segments"
)
try await uploadLocalSegments(
sessionId: sessionId,
allowBackendIdOverride: session.backendId?.isEmpty == false
)
return true
case .discardEmptyDesktopSession:
log("ConversationFinalization: Deleting empty unreconciled desktop session \(sessionId)")
try await TranscriptionStorage.shared.deleteSession(id: sessionId)
return true
case .reportFailure:
return false
}
}
enum CloudReconciliationExhaustionAction: Equatable {
case keepRetrying
case uploadLocalSegments
case discardEmptyDesktopSession
case reportFailure
}
static func cloudReconciliationExhaustionAction(
session: TranscriptionSessionRecord,
segmentCount: Int,
maxRetries: Int = 5
) -> CloudReconciliationExhaustionAction {
guard session.retryCount >= maxRetries - 1 else {
return .keepRetrying
}
guard segmentCount == 0 else {
return .uploadLocalSegments
}
guard session.source == ConversationSource.desktop.rawValue else {
return .reportFailure
}
return .discardEmptyDesktopSession
}
private func completeCloudConversation(
id conversationId: String,
sessionId: Int64,
allowForceProcess: Bool,
allowBackendIdOverride: Bool = false
) async throws -> Bool {
let conversation: ServerConversation
do {
if allowForceProcess {
conversation = try await apiClient.finalizeConversation(id: conversationId)
} else {
conversation = try await apiClient.getConversation(id: conversationId)
}
} catch APIError.httpError(let statusCode, _) where statusCode == 404 {
return false
}
guard
DesktopConversationMatchPolicy.canCompleteBoundBackendConversation(
id: conversation.id,
boundBackendId: conversationId,
status: conversation.status,
source: conversation.source
)
else {
return false
}
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status,
allowBackendIdOverride: allowBackendIdOverride
)
log("ConversationFinalization: Reconciled cloud session \(sessionId) by conversation id \(conversation.id)")
return true
}
private func markRetryableFailure(sessionId: Int64, error: Error) async {
let message = error.localizedDescription
do {
let session = try await TranscriptionStorage.shared.getSession(id: sessionId)
let retryCount = (session?.retryCount ?? 0) + 1
if retryCount >= maxRetries {
// Retries are exhausted. The in-line reconciliation fallback (resolveExhaustedCloudReconciliation)
// only runs when the final attempt returns cleanly with no match; when it fails by *throwing*
// (backend/network error), we land here instead and would abandon the session, dropping any
// recorded audio/transcript we still hold locally (#9083). Try to finalize from saved local
// segments first so the recording is not lost.
if let session,
let recovered = try? await resolveExhaustedCloudReconciliation(session: session, sessionId: sessionId),
recovered
{
log("ConversationFinalization: Recovered exhausted session \(sessionId) from local data after finalize error")
return
}
let segmentCount = try? await TranscriptionStorage.shared.getSegmentCount(sessionId: sessionId)
let diagnostics = ReconciliationFailureDiagnostics(
session: session,
segmentCount: segmentCount,
retryCount: retryCount,
maxRetries: maxRetries,
maxLocalFallbackRetries: maxLocalFallbackRetries
)
await AnalyticsManager.shared.conversationReconciliationFailed(
error: "session_reconciliation_failed",
reason: "cloud_reconcile_exhausted",
source: session?.source,
stage: session?.finalizationStrategy?.rawValue,
retryCount: retryCount,
hasBackendId: session?.backendId?.isEmpty == false,
hasClientConversationId: session?.clientConversationId?.isEmpty == false,
segmentCount: segmentCount,
diagnostics: diagnostics
)
}
try await TranscriptionStorage.shared.incrementRetryCount(id: sessionId)
try await TranscriptionStorage.shared.markSessionFailed(id: sessionId, error: message)
} catch {
logError("ConversationFinalization: Failed to record finalization failure for session \(sessionId)", error: error)
}
}
static func localClientConversationId(session: TranscriptionSessionRecord, sessionId: Int64) -> String {
let startedAtMs = Int64((session.startedAt.timeIntervalSince1970 * 1000).rounded())
return session.clientConversationId ?? "macos-local-\(sessionId)-\(startedAtMs)"
}
}
struct ReconciliationFailureDiagnostics {
let sessionStatus: String?
let conversationStatus: String?
let finalizationReason: String?
let hasFinishedAt: Bool
let hasFinalizationStartedAt: Bool
let hasFinalizationCompletedAt: Bool
let hasInputDeviceName: Bool
let hasLocalSegments: Bool?
let sessionAgeSeconds: Int?
let sessionDurationSeconds: Int?
let localFallbackAvailable: Bool
let localFallbackRetriesRemaining: Int
init(
session: TranscriptionSessionRecord?,
segmentCount: Int?,
retryCount: Int,
maxRetries: Int,
maxLocalFallbackRetries: Int
) {
let now = Date()
sessionStatus = session?.status.rawValue
conversationStatus = session?.conversationStatus.rawValue
finalizationReason = session?.finalizationReason?.rawValue
hasFinishedAt = session?.finishedAt != nil
hasFinalizationStartedAt = session?.finalizationStartedAt != nil
hasFinalizationCompletedAt = session?.finalizationCompletedAt != nil
hasInputDeviceName = session?.inputDeviceName?.isEmpty == false
hasLocalSegments = segmentCount.map { $0 > 0 }
sessionAgeSeconds = session.map { max(0, Int(now.timeIntervalSince($0.createdAt).rounded())) }
if let startedAt = session?.startedAt {
let finishedAt = session?.finishedAt ?? now
sessionDurationSeconds = max(0, Int(finishedAt.timeIntervalSince(startedAt).rounded()))
} else {
sessionDurationSeconds = nil
}
localFallbackAvailable =
session?.finalizationStrategy == .cloudReconcile
&& (segmentCount ?? 0) > 0
&& retryCount >= maxRetries
localFallbackRetriesRemaining = max(0, maxRetries + maxLocalFallbackRetries - retryCount)
}
}