forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalProjectionStore.swift
More file actions
69 lines (60 loc) · 2.16 KB
/
Copy pathLocalProjectionStore.swift
File metadata and controls
69 lines (60 loc) · 2.16 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
import Foundation
@preconcurrency import GRDB
/// Exact stored `client_processing` bytes. A retry must send `json` unchanged.
struct StoredClientProjection: Sendable, Equatable {
var transcriptSha256: String
var json: Data
}
protocol LocalProjectionStoring: Sendable {
func load(sessionId: Int64) async throws -> StoredClientProjection?
func save(sessionId: Int64, projection: StoredClientProjection) async throws
}
/// In-process store for hermetic summarizer tests. Not the production path.
actor MemoryLocalProjectionStore: LocalProjectionStoring {
private var rows: [Int64: StoredClientProjection] = [:]
func load(sessionId: Int64) async throws -> StoredClientProjection? {
rows[sessionId]
}
func save(sessionId: Int64, projection: StoredClientProjection) async throws {
rows[sessionId] = projection
}
}
/// GRDB-backed store against `transcription_sessions.clientProcessingJson`.
///
/// The queue is injected so tests can use an in-memory database that has run
/// only `addClientProcessingProjection`. Production passes the Rewind pool.
actor GRDBLocalProjectionStore: LocalProjectionStoring {
private let queue: any DatabaseWriter
init(queue: any DatabaseWriter) {
self.queue = queue
}
func load(sessionId: Int64) async throws -> StoredClientProjection? {
try await queue.read { db in
guard
let json = try String.fetchOne(
db,
sql: "SELECT clientProcessingJson FROM transcription_sessions WHERE id = ?",
arguments: [sessionId]
), !json.isEmpty
else {
return nil
}
let data = Data(json.utf8)
let payload = try ClientProcessingContract.decode(data)
return StoredClientProjection(transcriptSha256: payload.transcriptSha256, json: data)
}
}
func save(sessionId: Int64, projection: StoredClientProjection) async throws {
let json = String(decoding: projection.json, as: UTF8.self)
try await queue.write { db in
try db.execute(
sql: """
UPDATE transcription_sessions
SET clientProcessingJson = ?, updatedAt = ?
WHERE id = ?
""",
arguments: [json, Date(), sessionId]
)
}
}
}