hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
hop / Sources / SmartRank.swift
| 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..<n).map { Row(kind: v[1 + 4 * $0], index: v[2 + 4 * $0], score: v[3 + 4 * $0], promoted: v[4 + 4 * $0] == 1) } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// What the one box offers before anything is typed, and how much habit lifts an app. The rules are Omega code |
| 51 | /// (`Omega/hopcore.om`, fn swsuggest and fn swlifts); this packs numbers, asks the core and reads the answer back. |
| 52 | enum SmartSuggest { |
| 53 | struct App { var liftFrom = 0, liftHour = 0, used = 0; var running = false, previous = false; var lately = 0 } |
| 54 | struct Input { |
| 55 | var hasLatest = false // something was copied |
| 56 | var age = 0 // whole seconds since then |
| 57 | var latestIsAddress = false // it is a web address written with http |
| 58 | var frontIsBrowser = false |
| 59 | var rest = 0 // other copied things that follow (at most 20) |
| 60 | var paused = false // the clipboard history is paused |
| 61 | var apps: [App] = [] // in the order of their ids |
| 62 | } |
| 63 | struct Row: Equatable { var kind = 0, index = 0, score = 0, why = 0 } |
| 64 | static let app = 1, address = 2, screen = 3, clip = 6 |
| 65 | |
| 66 | /// Nothing when the core cannot be reached: the box then offers nothing rather than guessing. |
| 67 | static func rows(_ i: Input) -> [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..<n).map { Row(kind: v[1 + 4 * $0], index: v[2 + 4 * $0], score: v[3 + 4 * $0], why: v[4 + 4 * $0]) } |
| 73 | } |
| 74 | |
| 75 | /// The lift of each of `apps` apps: from the app in front, and from the hour. `launches`: made from the app now |
| 76 | /// in front, its hour, and which app it opened (its place among the apps, or -1). No lift when the core cannot |
| 77 | /// be reached, or for apps past the first 2048 and launches past the newest 4096. |
| 78 | static func lifts(hour: Int, hasFront: Bool, apps: Int, launches: [(here: Bool, hour: Int, app: Int)]) -> [(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..<n { out[k] = (v[1 + 2 * k], v[2 + 2 * k]) } |
| 86 | return out |
| 87 | } |
| 88 | } |