// Where a line spoken to Home's record button goes (lane fan, stage 5; the // owner's go at 19:05 IST, 14 Oct 2026: "voice navigation"). // // - "show ", "open " and "go to " all turn the fan to that // tool's tile, whose page shows in place (fan2 part 5: a tool is never // pushed over a fan Home). A tool with no tile there (or a grid Home) // opens its list. The ask is read anywhere in the line, its filler dropped // ("Can you go to the machine screen?"; fan2 part 7, `navigated`). // - "open " (any ask to go somewhere that names no tile, database // or tool) opens the record whose title is the words — exactly, or the one // record whose title begins with them ("open Rhyn" for Rhyn Kestrel). A // misheard name matches nothing and opens nothing. // - "go back" / "back" / "close" lowers the record risen over the fan, or // leaves the screen on top (lane fannav, 15 Oct 2026). // - A line a shipped command's spoken words read whole runs that command in // the core (`records.spoken`) and opens what it made. // - Everything else is a thought: the dock keeps it. // // Only a dictated line is asked (`DockModel.send(runsSpokenCommand:)`, `HomeVoice`); a typed one // is always a thought. import CottonKit import Foundation @MainActor enum SpokenHome { /// What a spoken line does on Home. enum Goes: Equatable { /// The fan turns to the tile with this key. case turn(String) /// The page opens; `name` is what the toast says it opened. case open(Route, name: String) /// Another database is chosen, as its cap is (fan2 part 6) — and, /// when the line named one of its tools, its fan turns to that tile. case database(String, tile: String?) /// The record on top comes down. case back /// A spoken command ran; its record opens when it made one. case ran(Route?) } /// The words that mean Back, said alone (with "please" or "this" around /// them at most): "go back", "back", "close", "close this". static func backs(_ words: String) -> Bool { let filler: Set = ["please", "this", "it", "can", "you", "now"] let said = words.lowercased() .components(separatedBy: CharacterSet.letters.inverted) .filter { !$0.isEmpty && !filler.contains($0) } return said == ["go", "back"] || said == ["back"] || said == ["close"] } /// The words that ask to go somewhere, longest first so "take me to" /// is read before "to" could be. static let navigations = ["take me to", "switch to", "jump to", "go to", "bring up", "show me", "open", "show"] /// Words around a tool's name that name nothing themselves: "can you go /// to THE machine SCREEN please". static let filler: Set = [ "the", "my", "a", "an", "me", "to", "screen", "page", "tab", "tool", "list", "view", "section", "app", "please", "now", "up", "over", "there", "again", ] /// What a line asks to go to, wherever in the line it asks (fan2 part 7, /// architect: "Can you go to the machine screen?" turns the fan to /// Machines): the clause after the first navigation word — up to a full /// stop, a comma, "and" or "then" — without its filler, lowercased. Nil /// when the line asks to go nowhere. static func navigated(_ words: String) -> String? { var clauses: [[String]] = [[]] var word = "" func close() { if !word.isEmpty { clauses[clauses.count - 1].append(word) } word = "" } for character in words.lowercased() { if character.isLetter || character.isNumber || character == "'" || character == "’" { word.append(character) } else { close() if ".,;:!?".contains(character) { clauses.append([]) } } } close() let tokens = clauses.flatMap { $0 + ["."] } let asks = navigations.map { $0.split(separator: " ").map(String.init) } for start in tokens.indices { guard let ask = asks.first(where: { Array(tokens[start...].prefix($0.count)) == $0 }) else { continue } let clause = tokens[(start + ask.count)...].prefix { !([".", "and", "then"].contains($0)) } let target = clause.filter { !filler.contains($0) }.joined(separator: " ") return target.isEmpty ? nil : target } return nil } /// The tile on the fan that `target` names: by its name, or by the tool /// whose name or plural it is — else the ONE tile whose name is a /// near-miss of it, as the dictation's own snap allows (`DictationSnap`: /// "machine" for Machines, "peeple" for People); two equally near turn /// nothing. static func tile(_ target: String, on fan: [(key: String, name: String)], definitions: TableDefinitions) -> String? { if let named = fan.first(where: { $0.name.lowercased() == target }) { return named.key } if let tool = tool(target, in: definitions), let key = fan.first(where: { $0.key == tool.id || $0.name.lowercased() == tool.plural.lowercased() })?.key { return key } var best: Set = [] var nearest = Int.max for tile in fan { let tool = definitions.all.first { $0.id == tile.key } for name in [tile.name, tool?.name, tool?.plural].compactMap({ $0?.lowercased() }) where name.count > 3 { let distance = DictationSnap.editDistance(target, name) guard distance <= DictationSnap.allowance(name.count) else { continue } if distance < nearest { (best, nearest) = ([tile.key], distance) } else if distance == nearest { best.insert(tile.key) } } } return best.count == 1 ? best.first : nil } /// The tool `target` calls by its name or plural. static func tool(_ target: String, in definitions: TableDefinitions) -> TableDefinition? { definitions.all.first { $0.name.lowercased() == target || $0.plural.lowercased() == target } } /// The held database `target` names: by its name, else the ONE database /// whose name is a near-miss of it, as a tile's is. static func database(_ target: String, in databases: [(id: String, name: String)]) -> String? { let target = target.trimmingCharacters(in: .whitespaces) guard !target.isEmpty else { return nil } if let named = databases.first(where: { $0.name.lowercased() == target }) { return named.id } let near = databases.filter { let name = $0.name.lowercased() return name.count > 3 && DictationSnap.editDistance(target, name) <= DictationSnap.allowance(name.count) } return near.count == 1 ? near[0].id : nil } /// The record a search answered for "open ": the hit titled /// `target`, else the ONE hit whose title begins with it as a word. static func record(_ target: String, in hits: [FindHit]) -> Goes? { let opening = hits.filter { $0.opens != nil } let begun = opening.filter { $0.title.lowercased().hasPrefix(target + " ") } guard let hit = opening.first(where: { $0.title.lowercased() == target }) ?? (begun.count == 1 ? begun[0] : nil), let route = hit.opens else { return nil } return .open(route, name: hit.title) } /// Where `words` go; nil for a thought. `fan` is Home's fan, empty when /// Home is a grid; `find` searches the database for records. static func goes( _ words: String, definitions: TableDefinitions, fan: [(key: String, name: String)], databases: [(id: String, name: String)] = [], find: (String) async -> [FindHit], command: (String) async -> Goes? = { _ in nil } ) async -> Goes? { if backs(words) { return .back } if let ran = await command(words) { return ran } guard let target = navigated(words) else { return nil } if let key = tile(target, on: fan, definitions: definitions) { return .turn(key) } if let id = database(target, in: databases) { return .database(id, tile: nil) } // "notes on Tin Factory": the database, then its tool. for joint in [" on ", " in ", " at ", " from "] { guard let split = target.range(of: joint, options: .backwards), let id = database(String(target[split.upperBound...]), in: databases) else { continue } return .database(id, tile: tile(String(target[.. Goes? { guard let core, let ran = try? await core.callJSON("records.spoken", ["words": words]), ran["command"]?.string != nil else { return nil } let made = ran["answer"]?["record"] guard let table = made?["table"]?.string, let id = made?["id"]?.string else { return .ran(nil) } return .ran(.tableRecord(table: table, id: id)) } /// The database's records that `query` finds, as Home's search reads them. static func find(_ query: String, core: (any CottonCalling)?) async -> [FindHit] { guard let core, let read = try? await core.callJSON(CottonMethod.readFind, SearchRequest(query: query)) else { return [] } return FindSection.all(read).flatMap(\.hits) } }