// 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 <tool>", "open <tool>" and "go to <tool>" 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 <record>" (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.importCottonKitimportFoundation@MainActorenumSpokenHome {/// What a spoken line does on Home.enumGoes: Equatable {/// The fan turns to the tile with this key.case turn(String)/// The page opens; `name` is what the toast says it opened.caseopen(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".staticfunc backs(_ words: String) -> Bool {let filler: Set<String> = ["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.staticlet 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".staticlet filler: Set<String> = ["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.staticfunc 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 {guardlet 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 }returnnil }/// 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.staticfunc tile(_ target: String, on fan: [(key: String, name: String)], definitions: TableDefinitions) -> String? {iflet named = fan.first(where: { $0.name.lowercased() == target }) { return named.key }iflet 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<String> = []var nearest = Int.maxfor 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) }elseif distance == nearest { best.insert(tile.key) } } }return best.count == 1 ? best.first : nil }/// The tool `target` calls by its name or plural.staticfunc 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.staticfunc database(_ target: String, in databases: [(id: String, name: String)]) -> String? {let target = target.trimmingCharacters(in: .whitespaces)guard !target.isEmpty else { returnnil }iflet 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 <target>": the hit titled/// `target`, else the ONE hit whose title begins with it as a word.staticfunc record(_ target: String, in hits: [FindHit]) -> Goes? {let opening = hits.filter { $0.opens != nil }let begun = opening.filter { $0.title.lowercased().hasPrefix(target + " ") }guardlet hit = opening.first(where: { $0.title.lowercased() == target }) ?? (begun.count == 1 ? begun[0] : nil),let route = hit.openselse { returnnil }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.staticfunc goes( _ words: String, definitions: TableDefinitions, fan: [(key: String, name: String)], databases: [(id: String, name: String)] = [], find: (String) async -> [FindHit], command: (String) async -> Goes? = { _ innil } ) async -> Goes? {if backs(words) { return .back }iflet ran = await command(words) { return ran }guardlet target = navigated(words) else { returnnil }iflet key = tile(target, on: fan, definitions: definitions) { return .turn(key) }iflet 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 "] {guardlet 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[..<split.lowerBound]), on: fan, definitions: definitions)) }iflet tool = tool(target, in: definitions) {return .open(BoardLayout.listRoute(for: tool), name: tool.plural) }return record(target, in: await find(target)) }/// The line run as a shipped command's spoken words, in the core; nil when/// no command hears it. What it made opens.staticfunc command(_ words: String, core: (any CottonCalling)?) async -> Goes? {guardlet core, let ran = try? await core.callJSON("records.spoken", ["words": words]), ran["command"]?.string != nilelse { returnnil }let made = ran["answer"]?["record"]guardlet 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.staticfunc find(_ query: String, core: (any CottonCalling)?) async -> [FindHit] {guardlet core, let read = try? await core.callJSON(CottonMethod.readFind, SearchRequest(query: query)) else {return [] }returnFindSection.all(read).flatMap(\.hits) }}