SPB Git

Repository Public

InboxRepository.swift 3.8 KB 68 lines swift

1// SP Notes Core2// File: Sources/SPNotesSync/Repositories/InboxRepository.swift3// Description: Inbox for the apps: capture, the list of captures, triage actions with undo, and the AI suggestions (never applied without the user's choice). Online; the pages themselves follow through the sync engine.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import SPNotesAPI9import SPNotesStore1011public struct InboxRepository: Sendable {12    let context: RepositoryContext13    public init(context: RepositoryContext) { self.context = context }1415    /// The captures waiting for triage, from the server; offline, the last list fetched (with its date). The inbox16    /// pages themselves are also in the local store (`SmartList.inbox`).17    public func items(policy: CachePolicy = .networkFirst) async throws -> Cached<[InboxItem]> {18        try await context.read("inbox.items", policy: policy) { try await context.api.inbox() }19    }2021    public func count() async throws -> Int { try await context.online { try await context.api.inboxCount() } }2223    /// Captures something into the inbox (`CaptureKindName`). The key makes a retry safe; the new page arrives with the24    /// next cycle, requested here. For a capture that must work offline, create the page locally with `inboxAt` set25    /// (the 1.0 path) instead.26    @discardableResult27    public func capture(_ draft: CaptureDraft, idempotencyKey: String = NotesAPI.newIdempotencyKey()) async throws -> CaptureResult {28        let result = try await context.online { try await context.api.capture(draft, idempotencyKey: idempotencyKey) }29        await context.scheduleSync()30        return result31    }3233    /// Triages one capture now (`keep | task | database | reference | archive | trash`). Returns the journal action id34    /// for `undo`. Offline → `.offline`; the 1.0 queued triage (`OpKind.triage`) remains available for keep / archive / trash.35    @discardableResult36    public func triage(id: String, _ action: InboxAction, idempotencyKey: String = NotesAPI.newIdempotencyKey()) async throws -> TriageResult {37        let result = try await context.online { try await context.api.triageInboxItem(id: id, action, idempotencyKey: idempotencyKey) }38        _ = try? await context.store.removeCached(key: "inbox.items")39        await context.scheduleSync()40        return result41    }4243    /// Reverses a triage or an applied suggestion (`already == true` when it was already undone).44    @discardableResult45    public func undo(actionId: String) async throws -> UndoResult {46        let result = try await context.online { try await context.api.undoInboxAction(actionID: actionId) }47        _ = try? await context.store.removeCached(key: "inbox.items")48        await context.scheduleSync()49        return result50    }5152    /// AI suggestions for a capture (title, tags, destination, relations, summary). **Suggestions only.**53    /// `cachedOnly: true` (default) never spends; pass false on an explicit user action (one cheap model call).54    /// A server without the route → `.unsupported`.55    public func suggestions(id: String, cachedOnly: Bool = true, refresh: Bool = false, model: String? = nil) async throws -> InboxSuggestions {56        try await context.online { try await context.api.inboxSuggestions(id: id, refresh: refresh, cachedOnly: cachedOnly, model: model) }57    }5859    /// Applies the subset the user chose (possibly edited). Journaled: `undo(actionId:)` reverses it.60    @discardableResult61    public func apply(id: String, _ selection: InboxSuggestionSelection) async throws -> InboxApplyResult {62        let result = try await context.online { try await context.api.applyInboxSuggestions(id: id, selection) }63        _ = try? await context.store.removeCached(key: "inbox.items")64        await context.scheduleSync()65        return result66    }67}68
Files main
Menu

Theme