Repository Public
1// ORIX Docs2// File: App/Sources/Intelligence/IntelligenceModel.swift3// Description: State of the intelligence panel: what text an action works on, the running generation, the proposal, and how an4// accepted proposal is applied to the document (always through session.perform, so one Undo restores the text).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import AppKit9import Observation10import DocModel11import DocReferences12import DocAutomation1314enum IntelligenceScope: String, CaseIterable, Identifiable {15 case selection, paragraph, document16 var id: String { rawValue }17 var title: String {18 switch self {19 case .selection: return String(localized: "Selection")20 case .paragraph: return String(localized: "Paragraph")21 case .document: return String(localized: "Document")22 }23 }24}2526/// The text an action was asked to work on, as it was when the request started.27struct IntelligenceSource {28 var scope: IntelligenceScope29 var story: StoryID30 /// Paragraph slices, in document order.31 var slices: [(path: ParaPath, range: Range<Int>)]32 /// The text of each slice, as sent to the model.33 var texts: [String]34 /// The selection holds citations, notes, images or equations: replacing it would delete them.35 var containsObjects: Bool36 var text: String { texts.joined(separator: "\n") }37 var words: Int { DocumentStatistics(text: text).words }38}3940@Observable41final class IntelligenceModel {42 enum Phase: Equatable {43 case idle44 case running(IntelligenceEvent.Stage)45 case proposal46 case failed(String)47 }4849 @ObservationIgnored unowned let session: DocumentSession50 @ObservationIgnored var engine: any TextIntelligence51 private(set) var availability: IntelligenceAvailability52 var scope: IntelligenceScope = .paragraph53 private(set) var phase: Phase = .idle54 private(set) var action: IntelligenceAction?55 private(set) var source: IntelligenceSource?56 /// The proposal so far, while it streams, then the final text.57 private(set) var proposal = ""58 private(set) var outcome: IntelligenceOutcome?59 @ObservationIgnored private var task: Task<Void, Never>?60 @ObservationIgnored private var scopeChosenByUser = false6162 init(session: DocumentSession, engine: any TextIntelligence = FoundationModelsEngine()) {63 self.session = session64 self.engine = engine65 self.availability = engine.availability66 }6768 var isRunning: Bool { if case .running = phase { return true } else { return false } }69 var generationEnabled: Bool { AppSettings.shared.intelligenceEnabled && availability.isAvailable }7071 func refreshAvailability() {72 let now = engine.availability73 if now != availability { availability = now }74 }7576 // MARK: Source text7778 /// Follows the selection until the user picks a scope: a selection when there is one, the paragraph otherwise.79 func selectionChanged() {80 guard phase == .idle else { return }81 if session.selection.isCaret { if scope == .selection { scope = .paragraph; scopeChosenByUser = false } }82 else if !scopeChosenByUser || scope == .paragraph { scope = .selection }83 }8485 func choose(_ scope: IntelligenceScope) { self.scope = scope; scopeChosenByUser = true }8687 func isAvailable(_ scope: IntelligenceScope) -> Bool {88 switch scope {89 case .selection: return !session.selection.isCaret90 case .paragraph: return session.blocks.paragraph(at: session.selection.start.path) != nil91 case .document: return true92 }93 }9495 /// The text of part of a paragraph as a reader sees it: citations and cross-references as displayed, tracked deletions left out.96 static func displayText(of p: Paragraph, range: Range<Int>, session: DocumentSession) -> (text: String, hasObjects: Bool) {97 var out = ""98 var hasObjects = false99 var offset = 0100 let doc = session.doc101 let numbering = session.numbering102 for run in p.runs {103 let runRange = offset..<(offset + run.length)104 offset += run.length105 let lower = max(runRange.lowerBound, range.lowerBound), upper = min(runRange.upperBound, range.upperBound)106 guard lower < upper, !run.isDeleted else { continue }107 if let inline = run.inline {108 hasObjects = true109 switch inline {110 case .citation(let c): out += CitationEngine(document: doc).citationText(c, document: doc, numbering: numbering)111 case .crossRef(let r): out += numbering.crossRefText(r) { _ in nil }112 case .field(let f): out += numbering.staticFieldText(f, document: doc) ?? ""113 case .equation(let e): out += e.latex114 case .image, .shape, .note, .bookmark: break115 }116 } else {117 out += run.text.utf16Substring((lower - runRange.lowerBound)..<(upper - runRange.lowerBound))118 }119 }120 return (out.replacingOccurrences(of: lineSeparatorCharacter, with: " "), hasObjects)121 }122123 func currentSource(for scope: IntelligenceScope) -> IntelligenceSource? {124 var slices: [(path: ParaPath, range: Range<Int>)] = []125 var story = session.selection.story126 switch scope {127 case .selection:128 guard !session.selection.isCaret else { return nil }129 slices = session.editor.selectedParagraphs().filter { !$0.range.isEmpty }130 case .paragraph:131 let path = session.selection.start.path132 guard let p = session.blocks.paragraph(at: path) else { return nil }133 slices = [(path, 0..<p.length)]134 case .document:135 story = .body136 session.doc.forEachBodyParagraph { p, path in if p.length > 0 { slices.append((path, 0..<p.length)) } }137 }138 let blocks = story == .body ? session.doc.body : session.doc.blocks(in: story)139 var texts: [String] = []140 var kept: [(path: ParaPath, range: Range<Int>)] = []141 var objects = false142 for slice in slices {143 guard let p = blocks.paragraph(at: slice.path) else { continue }144 let (text, has) = IntelligenceModel.displayText(of: p, range: slice.range, session: session)145 if text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { continue }146 texts.append(text); kept.append(slice); objects = objects || has147 }148 guard !texts.isEmpty else { return nil }149 return IntelligenceSource(scope: scope, story: story, slices: kept, texts: texts, containsObjects: objects)150 }151152 /// The scope an action runs on: a rewrite never takes the whole document.153 func effectiveScope(for action: IntelligenceAction) -> IntelligenceScope {154 if scope == .document, !action.appliesToWholeDocument { return session.selection.isCaret ? .paragraph : .selection }155 if scope == .selection, session.selection.isCaret { return .paragraph }156 return scope157 }158159 // MARK: Running160161 func run(_ action: IntelligenceAction) {162 stop()163 refreshAvailability()164 self.action = action165 outcome = nil166 proposal = ""167 guard AppSettings.shared.intelligenceEnabled else { phase = .failed(String(localized: "ORIX Intelligence is turned off in Settings.")); return }168 guard availability.isAvailable else { phase = .failed(availability.explanation); return }169 guard let source = currentSource(for: effectiveScope(for: action)) else { self.source = nil; phase = .failed(IntelligenceError.emptyInput.message); return }170 self.source = source171 if let (code, confidence) = TextTools.dominantLanguage(of: String(source.text.prefix(2000))), confidence >= 0.8, source.text.count >= 60, !engine.supportsLanguage(code) {172 phase = .failed(String(localized: "The on-device model does not support \(TextTools.languageName(code)), the language of this text."))173 return174 }175 phase = .running(.preparing)176 let stream = IntelligenceRunner(engine: engine).run(action, on: source.text)177 task = Task { [weak self] in178 do {179 for try await event in stream {180 guard let self, !Task.isCancelled else { return }181 switch event {182 case .stage(let stage): self.phase = .running(stage)183 case .partial(let text): self.proposal = text184 case .finished(let outcome):185 self.outcome = outcome186 self.proposal = outcome.text187 if outcome.text.isEmpty { self.phase = .failed(String(localized: "The model returned no text.")) }188 else if outcome.isUnchanged { self.phase = .failed(String(localized: "The model returned the text unchanged: it proposed no edit. You can try again; the answer may differ.")) }189 else { self.phase = .proposal }190 }191 }192 } catch is CancellationError {193 // Stopped by the user: `stop()` already reset the state.194 } catch let error as IntelligenceError {195 self?.phase = .failed(error.message)196 } catch {197 self?.phase = .failed(IntelligenceError.other(error.localizedDescription).message)198 }199 }200 }201202 func regenerate() { if let action { run(action) } }203204 /// Stops a running generation. Nothing was applied, so there is nothing to undo.205 func stop() {206 task?.cancel()207 task = nil208 if isRunning { phase = .idle; proposal = "" }209 }210211 /// Discards the proposal. The document is untouched.212 func reject() {213 stop()214 phase = .idle; proposal = ""; outcome = nil; source = nil; action = nil215 }216217 // MARK: Applying218219 /// The source paragraphs still read as they did when the proposal was requested.220 var sourceIsUnchanged: Bool {221 _ = session.revision222 guard let source else { return false }223 let blocks = source.story == .body ? session.doc.body : session.doc.blocks(in: source.story)224 for (slice, text) in zip(source.slices, source.texts) {225 guard let p = blocks.paragraph(at: slice.path), slice.range.upperBound <= p.length,226 IntelligenceModel.displayText(of: p, range: slice.range, session: session).text == text else { return false }227 }228 return true229 }230231 /// Why "Accept" cannot replace the source, or nil when it can.232 var replaceBlocker: String? {233 guard let action, let source, outcome != nil else { return String(localized: "There is no proposal.") }234 if !action.replacesSource { return String(localized: "This result does not replace the text.") }235 if !sourceIsUnchanged { return String(localized: "The text changed since this proposal was made. Regenerate it, or copy the result.") }236 if source.containsObjects {237 return String(localized: "The text contains citations, notes, images or equations that a replacement would delete. Insert the proposal below instead.")238 }239 return nil240 }241242 static func paragraphs(of text: String) -> [String] {243 text.components(separatedBy: "\n").map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }244 }245246 /// Replaces the source text by the proposal as one undoable step. Paragraph styles are kept: when the proposal has as many247 /// paragraphs as the source, each one replaces its counterpart in place; otherwise the result takes the style of the first.248 func accept() {249 guard replaceBlocker == nil, let source, let outcome else { return }250 let paragraphs = IntelligenceModel.paragraphs(of: outcome.text)251 guard !paragraphs.isEmpty, let first = source.slices.first, let last = source.slices.last else { return }252 let story = source.story253 session.perform(String(localized: "Apply Proposal")) { e in254 if paragraphs.count == source.slices.count {255 var end = Position(path: last.path, offset: last.range.lowerBound)256 for (i, (slice, text)) in zip(source.slices, paragraphs).enumerated().reversed() {257 e.selection = Selection(story: story, anchor: Position(path: slice.path, offset: slice.range.lowerBound), head: Position(path: slice.path, offset: slice.range.upperBound))258 e.insertText(text)259 if i == source.slices.count - 1 { end = e.selection.head }260 }261 e.selection = Selection(story: story, anchor: Position(path: first.path, offset: first.range.lowerBound), head: end)262 } else {263 e.selection = Selection(story: story, anchor: Position(path: first.path, offset: first.range.lowerBound), head: Position(path: last.path, offset: last.range.upperBound))264 for (i, text) in paragraphs.enumerated() {265 if i > 0 { e.insertParagraphBreak() }266 e.insertText(text)267 }268 }269 e.sanitizeSelection()270 }271 finishApplied(String(localized: "Proposal applied. Undo restores the original text."))272 }273274 /// Turns the lines of a result into paragraphs: "- " lines become a bulleted list, Markdown emphasis marks are dropped.275 private static func blocks(for text: String, in doc: inout DocumentModel, revision: Revision?) -> [Block] {276 var listID: String?277 var out: [Block] = []278 for raw in text.components(separatedBy: "\n") {279 let indent = raw.prefix { $0 == " " }.count280 var line = raw.trimmingCharacters(in: .whitespaces).replacingOccurrences(of: "**", with: "")281 guard !line.isEmpty else { continue }282 var item: ListItem?283 if let marker = ["- ", "* ", "• "].first(where: { line.hasPrefix($0) }) {284 line.removeFirst(marker.count)285 if listID == nil { listID = doc.ensureList(bulleted: true) }286 item = ListItem(listID: listID!, level: min(4, indent / 2))287 } else {288 while line.hasPrefix("#") { line.removeFirst() }289 line = line.trimmingCharacters(in: .whitespaces)290 }291 var p = Paragraph(style: StyleID.normal, runs: [Run(line, revision: revision)], list: item)292 p.markRevision = revision293 out.append(.paragraph(p))294 }295 return out296 }297298 /// Inserts the result as new paragraphs after the source text, as one undoable step. The source is not modified.299 func insertBelow() {300 guard let source, let outcome, let last = source.slices.last else { return }301 let story = source.story302 let text = outcome.text303 session.perform(String(localized: "Insert Proposal")) { e in304 let revision = e.tracking ? Revision(kind: .insertion, author: e.author, date: e.now()) : nil305 if story == .body, last.path.cell == nil, last.path.block < e.doc.body.count {306 let blocks = IntelligenceModel.blocks(for: text, in: &e.doc, revision: revision)307 guard !blocks.isEmpty else { return }308 let at = last.path.block + 1309 e.doc.body.insert(contentsOf: blocks, at: at)310 let end = DocumentEditor.lastPosition(inBlock: at + blocks.count - 1, e.doc.body)311 e.selection = Selection(story: .body, anchor: Position(block: at, offset: 0), head: end)312 } else {313 // In a table cell, a note or a header: typed in after the paragraph.314 let length = e.blocks.paragraph(at: last.path)?.length ?? last.range.upperBound315 e.selection = Selection(story: story, caret: Position(path: last.path, offset: length))316 for line in IntelligenceModel.paragraphs(of: text) {317 e.insertParagraphBreak()318 e.insertText(line.replacingOccurrences(of: "**", with: ""))319 }320 }321 e.sanitizeSelection()322 }323 finishApplied(String(localized: "Result inserted below. Undo removes it."))324 }325326 func copy() {327 guard !proposal.isEmpty else { return }328 NSPasteboard.general.clearContents()329 NSPasteboard.general.setString(proposal, forType: .string)330 session.statusMessage = String(localized: "Result copied.")331 }332333 /// Opens the result as a new untitled document with the styles of this one.334 func openAsNewDocument() {335 guard let outcome, let action else { return }336 var model = DocumentModel(meta: DocumentMetadata(title: action.title, author: session.doc.meta.author, language: session.doc.meta.language),337 styles: session.doc.styles, pageSetup: PageSetup(paper: session.doc.pageSetup.paper), body: [])338 let source = session.doc.meta.title.isEmpty ? (session.document?.displayName ?? "") : session.doc.meta.title339 model.body = [.paragraph(Paragraph(source.isEmpty ? action.title : "\(action.title) — \(source)", style: StyleID.heading(1)))]340 model.body += IntelligenceModel.blocks(for: outcome.text, in: &model, revision: nil)341 ORIXDocument.openUntitled(model, displayName: action.title)342 }343344 private func finishApplied(_ message: String) {345 session.statusMessage = message346 phase = .idle; proposal = ""; outcome = nil; source = nil; action = nil347 session.canvas?.window?.makeFirstResponder(session.canvas)348 }349}350