Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Terminal/PaletteLogic.swift3// Description: Pure logic of the command palette: how a local command matches what was typed, the list of recent searches, and where the server's navigation targets (`nav:<place>`) lead in this app.4import Foundation56public enum PaletteMatch {7 static func fold(_ s: String) -> String { s.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: Locale(identifier: "en_US_POSIX")).lowercased() }89 /// 0 = no match, 1 = the title itself. Title prefix > word prefix > keyword prefix > substring > letters in order.10 public static func score(query: String, title: String, keywords: [String] = []) -> Double {11 let q = fold(query.trimmingCharacters(in: .whitespaces)), t = fold(title)12 guard !q.isEmpty else { return 0 }13 if t == q { return 1 }14 if t.hasPrefix(q) { return 0.9 }15 let words = t.split(whereSeparator: { !$0.isLetter && !$0.isNumber }).map(String.init)16 if words.contains(where: { $0.hasPrefix(q) }) { return 0.8 }17 let keys = keywords.map(fold)18 if keys.contains(q) { return 0.75 }19 if keys.contains(where: { $0.hasPrefix(q) }) { return 0.65 }20 if t.contains(q) { return 0.55 }21 if keys.contains(where: { $0.contains(q) }) { return 0.45 }22 // every letter of the query in order ("nwc" → "New Chart Window"): at least three letters, or noise matches everything23 guard q.count >= 3 else { return 0 }24 var it = t.makeIterator()25 for ch in q { var found = false; while let c = it.next() { if c == ch { found = true; break } }; if !found { return 0 } }26 return 0.327 }28}2930public enum RecentSearches {31 public static let limit = 1232 /// Most recent first, without duplicates (case and accents ignored), at most `limit` entries of at most 80 characters.33 public static func adding(_ query: String, to list: [String], limit: Int = RecentSearches.limit) -> [String] {34 let q = String(query.trimmingCharacters(in: .whitespacesAndNewlines).prefix(80))35 guard q.count >= 2 else { return list }36 let key = PaletteMatch.fold(q)37 return Array(([q] + list.filter { PaletteMatch.fold($0) != key }).prefix(limit))38 }39}4041/// Where a navigation target of the universal search (`nav:<place>`, CONTRACT §3) leads in the Mac app.42public enum PaletteDestination: Sendable, Equatable {43 /// An `AppSection` raw value.44 case section(String)45 case newConversation46 case labTool(String)47 /// A pane of the Settings window (nil = the window as it was).48 case settings(String?)4950 public static func forPlace(_ place: String) -> PaletteDestination? {51 switch place {52 case "home", "history": .section("research")53 case "chat": .newConversation54 case "terminal", "macro": .section("overview")55 case "markets": .section("markets")56 case "charts": .section("charting")57 case "quant": .section("quant")58 case "portfolio": .section("portfolio")59 case "watchlists": .section("watchlists")60 case "research", "notes": .section("studio")61 case "statements": .section("statements")62 case "options": .labTool("options_lab")63 case "agent": .section("agent")64 case "usage": .settings("usage")65 case "security": .settings("security")66 case "settings": .settings(nil)67 default: nil68 }69 }70}71