Repository Public
1// SP Notes Core2// File: Sources/SPNotesSync/Repositories/AudioRepository.swift3// Description: Voice notes for the apps: upload the recording as an attachment, ask for its transcription (a paid job on the server), poll until it is done, and hand back what the editor writes into the audio node. Online only.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import SPNotesAPI9import SPNotesDocument10import SPNotesStore1112public struct AudioRepository: Sendable {13 let context: RepositoryContext14 public init(context: RepositoryContext) { self.context = context }1516 /// Uploads a recording under a client-chosen id (a retry returns the stored attachment). Returns the attachment id17 /// to put in the `audio` node. For an offline-first recording, register the file with the store instead18 /// (`addLocalAttachment`): the engine uploads it; then call `transcribe` once it is uploaded.19 public func upload(fileURL: URL, pageId: String?, id: String = NotesAPI.newID(), filename: String? = nil, mimeType: String? = nil) async throws -> String {20 try await context.online { try await context.api.uploadAttachment(fileURL: fileURL, id: id, pageID: pageId, filename: filename, mimeType: mimeType).value.id }21 }2223 /// Asks for the transcription (**a paid call on the server, only on an explicit user action**; a second request24 /// while one is pending or done does not charge again unless `force`). A server without the voice routes →25 /// `.unsupported`.26 public func transcribe(attachmentId: String, summarize: Bool = false, force: Bool = false) async throws -> AudioTranscription {27 try await voice { try await context.api.transcribeAudio(attachmentID: attachmentId, summarize: summarize ? true : nil, force: force ? true : nil) }28 }2930 public func status(attachmentId: String) async throws -> AudioTranscription {31 try await voice { try await context.api.audioTranscription(attachmentID: attachmentId) }32 }3334 /// Polls until the transcription leaves `pending` (every `interval`, at most `timeout`); returns the last state35 /// (`done`, `failed`, or still `pending` at the timeout). Cancelling the task stops the polling.36 public func waitForTranscription(attachmentId: String, interval: Duration = .seconds(2.5), timeout: Duration = .seconds(600)) async throws -> AudioTranscription {37 let clock = ContinuousClock()38 let deadline = clock.now + timeout39 while true {40 let state = try await status(attachmentId: attachmentId)41 if state.status != TranscriptionStatus.pending || clock.now >= deadline { return state }42 try await Task.sleep(for: interval)43 }44 }4546 /// Upload → request → poll, in one call.47 public func transcribeRecording(fileURL: URL, pageId: String?, summarize: Bool = false, interval: Duration = .seconds(2.5), timeout: Duration = .seconds(600)) async throws -> (attachmentId: String, transcription: AudioTranscription) {48 let id = try await upload(fileURL: fileURL, pageId: pageId)49 _ = try await transcribe(attachmentId: id, summarize: summarize)50 return (id, try await waitForTranscription(attachmentId: id, interval: interval, timeout: timeout))51 }5253 /// What the editor copies into the `audio` node when the transcription is done (the server never edits the page):54 /// `transcript`, `transcriptStatus`, `transcriptEngine` (`<engine>:<model>`).55 public func nodeAttributes(from transcription: AudioTranscription) -> (transcript: [TranscriptSegment], status: String, engine: String?) {56 let segments = (transcription.transcript ?? []).map { TranscriptSegment(t: $0.t, text: $0.text) }57 let engine = transcription.engine.map { $0 + (transcription.model.map { ":" + $0 } ?? "") }58 return (segments, transcription.status, engine)59 }6061 private func voice<T: Sendable>(_ work: @Sendable () async throws -> T) async throws -> T {62 do { return try await work() } catch let error as APIError where error.isRouteMissing {63 throw RepositoryError.unsupported(feature: "voice")64 } catch { throw RepositoryError.from(error) }65 }66}67