Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/hop/blob/main/Sources/SmartRank.swift # hop Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key. Brought by drakestapleton Sign in to star 1 stars 1 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `hop / Sources / SmartRank.swift` 88 lines ยท 5844 bytes Raw Blame History 1 import Foundation 2 3 /// The rows of the one box for a typed text, and their order. The rules and the scores are Omega code 4 /// (`Omega/hopcore.om`, fn swrank); this file packs what Hop knows about each candidate into numbers, asks the 5 /// core, and reads the rows back. Omega/tests/check-core.sh checks this very file against the Swift rules it replaced. 6 enum SmartRank { 7 struct App { var score = 0, boost = 0, lift = 0; var previous = false } 8 struct File { var score = 0, boost = 0; var isApp = false } 9 struct Account { var contains = false, prefix = false } 10 struct Input { 11 var low: [UInt8] = [] // the typed text, trimmed and in lower case 12 var lowCount = 0 // its length in characters 13 var apps: [App] = [] 14 var address = 0 // 0 not a web address, 1 typed with its scheme, 2 a bare address 15 var searched = true // this Mac has finished searching its files for the text 16 var exactFile = false // a file or folder has exactly this name 17 var files: [File] = [] 18 var screens: [[[UInt8]]] = [] // the words of each screen that is switched on 19 var commands: [[[UInt8]]] = [] 20 var accounts: [Account] = [] 21 var clips: [Bool] = [] // pinned or not, newest first 22 var question = false 23 var agentWanted = false // a sentence may go to the agent 24 } 25 struct Row: Equatable { var kind = 0, index = 0, score = 0; var promoted = false } 26 static let app = 1, address = 2, screen = 3, command = 4, account = 5, clip = 6, file = 7, ask = 8, agent = 9 27 28 static func rank(_ i: Input) -> [Row] { 29 // Limits: the first 2048 apps that matched, 4000 files, 800 accounts (a Mac has a few hundred apps). 30 let fallback = [Row(kind: ask, index: 0, score: 2), Row(kind: agent, index: 0, score: 1)] // the core cannot be reached: only Ask and the agent 31 let apps = i.apps.prefix(2048), files = i.files.prefix(4000), accounts = i.accounts.prefix(800), clips = i.clips.prefix(8) 32 var numbers = [i.lowCount, i.low.count, i.address, i.searched ? 1 : 0, i.exactFile ? 1 : 0, i.question ? 1 : 0, i.agentWanted ? 1 : 0, 33 apps.count, files.count, i.screens.count, i.commands.count, accounts.count, clips.count] 34 for a in apps { numbers += [a.score, a.boost, a.lift, a.previous ? 1 : 0] } 35 for f in files { numbers += [f.score, f.boost, f.isApp ? 1 : 0] } 36 for a in accounts { numbers += [a.contains ? 1 : 0, a.prefix ? 1 : 0] } 37 for c in clips { numbers.append(c ? 1 : 0) } 38 var bytes = i.low 39 for list in [i.screens, i.commands] { 40 for words in list { 41 numbers.append(words.count) 42 for w in words { numbers.append(w.count); bytes += w } 43 } 44 } 45 guard let v = SwarmCore.statusNumbers(17, numbers, bytes: bytes), let n = v.first, n >= 0, v.count == 1 + 4 * n else { return fallback } 46 return (0.. [Row] { 68 let apps = i.apps.prefix(2048) 69 var numbers = [i.hasLatest ? 1 : 0, i.age, i.latestIsAddress ? 1 : 0, i.frontIsBrowser ? 1 : 0, min(max(i.rest, 0), 20), i.paused ? 1 : 0, apps.count] 70 for a in apps { numbers += [a.liftFrom, a.liftHour, a.used, a.running ? 1 : 0, a.previous ? 1 : 0, a.lately] } 71 guard let v = SwarmCore.statusNumbers(18, numbers), let n = v.first, n >= 0, v.count == 1 + 4 * n else { return [] } 72 return (0.. [(from: Int, hour: Int)] { 79 let none = [(from: Int, hour: Int)](repeating: (0, 0), count: apps) 80 let n = min(apps, 2048), ls = launches.suffix(4096) 81 var numbers = [hour, hasFront ? 1 : 0, n, ls.count] 82 for l in ls { numbers += [l.here ? 1 : 0, l.hour, l.app >= 0 && l.app < n ? l.app : -1] } 83 guard let v = SwarmCore.statusNumbers(19, numbers), v.first == n, v.count == 1 + 2 * n else { return none } 84 var out = none 85 for k in 0..