SPB Git

Repository Public License

Intents.swift 6.4 KB 130 lines swift

1// ORIX Scan2// File: App/Sources/Platform/Intents.swift3// Description: App Intents for Shortcuts and Siri: scan, extract text from a file, make a searchable PDF, search the library. All on device.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AppIntents8import Foundation9import ORIXKit10import SwiftData11import UniformTypeIdentifiers1213struct ScanDocumentIntent: AppIntent {14    static let title: LocalizedStringResource = "Scan Document"15    static let description = IntentDescription("Opens ORIX Scan on the document scanner.")16    static let openAppWhenRun = true1718    @MainActor19    func perform() async throws -> some IntentResult {20        // Give the interface a moment to be on screen, then ask it to present the scanner.21        try? await Task.sleep(for: .milliseconds(400))22        NotificationCenter.default.post(name: AppRequests.scan, object: nil)23        return .result()24    }25}2627enum IntentFailure: Error, CustomLocalizedStringResourceConvertible {28    case unsupportedFile, noText, failed2930    var localizedStringResource: LocalizedStringResource {31        switch self {32        case .unsupportedFile: "ORIX Scan works with images and PDF files."33        case .noText: "No text was found in this file."34        case .failed: "The file could not be processed."35        }36    }37}3839/// Runs a file through the engines in a throwaway store, so Shortcuts actions never add anything to the library.40nonisolated enum IntentPipeline {41    static func withDocument<T: Sendable>(from file: IntentFile, _ body: @Sendable (DocumentProcessor, UUID) async throws -> T) async throws -> T {42        let fm = FileManager.default43        let root = fm.temporaryDirectory.appendingPathComponent("orix-intent-\(UUID().uuidString)", isDirectory: true)44        defer { try? fm.removeItem(at: root) }45        let processor = DocumentProcessor(store: try DocumentStore(root: root))46        let type = file.type ?? UTType(filenameExtension: (file.filename as NSString).pathExtension) ?? .data47        var draft = try await processor.store.beginDraft(title: (file.filename as NSString).deletingPathExtension)48        if type.conforms(to: .pdf) {49            let url = root.appendingPathComponent("input.pdf")50            try file.data.write(to: url)51            do { try await processor.store.addPDF(at: url, to: &draft) } catch { throw IntentFailure.failed }52        } else if type.conforms(to: .image) {53            do {54                try await processor.store.addImage(file.data, fileExtension: type.preferredFilenameExtension ?? "jpg", edit: PageEdit(), to: &draft)55            } catch { throw IntentFailure.failed }56        } else {57            throw IntentFailure.unsupportedFile58        }59        let manifest = try await processor.store.commit(draft)60        return try await body(processor, manifest.id)61    }62}6364struct ExtractTextIntent: AppIntent {65    static let title: LocalizedStringResource = "Extract Text from File"66    static let description = IntentDescription("Recognizes the text of an image or PDF on this device and returns it. Nothing is uploaded or saved.")6768    @Parameter(title: "File", supportedContentTypes: [.image, .pdf]) var file: IntentFile6970    static var parameterSummary: some ParameterSummary { Summary("Extract text from \(\.$file)") }7172    func perform() async throws -> some IntentResult & ReturnsValue<String> {73        let text = try await IntentPipeline.withDocument(from: file) { processor, id in74            _ = try await processor.recognize(id, options: OCROptions())75            return try await processor.fullText(of: id)76        }77        guard !text.isEmpty else { throw IntentFailure.noText }78        return .result(value: text)79    }80}8182struct MakeSearchablePDFIntent: AppIntent {83    static let title: LocalizedStringResource = "Make Searchable PDF"84    static let description = IntentDescription("Turns an image or a scanned PDF into a PDF with selectable, searchable text. Runs entirely on this device.")8586    @Parameter(title: "File", supportedContentTypes: [.image, .pdf]) var file: IntentFile8788    static var parameterSummary: some ParameterSummary { Summary("Make a searchable PDF from \(\.$file)") }8990    func perform() async throws -> some IntentResult & ReturnsValue<IntentFile> {91        let name = (file.filename as NSString).deletingPathExtension92        let data = try await IntentPipeline.withDocument(from: file) { processor, id in93            _ = try await processor.recognize(id, options: OCROptions())94            let export = try await processor.exportPDF(id, options: ExportOptions(preset: .balanced, searchable: true))95            return try Data(contentsOf: export.url)96        }97        return .result(value: IntentFile(data: data, filename: "\(name).pdf", type: .pdf))98    }99}100101struct SearchDocumentsIntent: AppIntent {102    static let title: LocalizedStringResource = "Search Documents"103    static let description = IntentDescription("Finds documents in your ORIX Scan library by title or recognized text and returns their names.")104105    @Parameter(title: "Search") var query: String106107    static var parameterSummary: some ParameterSummary { Summary("Search ORIX Scan for \(\.$query)") }108109    @MainActor110    func perform() async throws -> some IntentResult & ReturnsValue<[String]> {111        let root = try DocumentStore.defaultRoot()112        let container = try Library.makeContainer(url: root.appendingPathComponent("Library.store"))113        let records = try container.mainContext.fetch(FetchDescriptor<DocumentRecord>())114        return .result(value: Library.query(records, search: query).map(\.title))115    }116}117118struct ORIXShortcuts: AppShortcutsProvider {119    static var appShortcuts: [AppShortcut] {120        AppShortcut(intent: ScanDocumentIntent(), phrases: ["Scan a document with \(.applicationName)", "Scan with \(.applicationName)"],121                    shortTitle: "Scan Document", systemImageName: "doc.viewfinder")122        AppShortcut(intent: ExtractTextIntent(), phrases: ["Extract text with \(.applicationName)"],123                    shortTitle: "Extract Text", systemImageName: "text.viewfinder")124        AppShortcut(intent: MakeSearchablePDFIntent(), phrases: ["Make a searchable PDF with \(.applicationName)"],125                    shortTitle: "Searchable PDF", systemImageName: "doc.text.magnifyingglass")126        AppShortcut(intent: SearchDocumentsIntent(), phrases: ["Search \(.applicationName)"],127                    shortTitle: "Search Documents", systemImageName: "magnifyingglass")128    }129}130
Files main
Menu

Theme