import Foundation /// Decides, with plain rules and no model, whether a message can be answered by the small local /// chat model or needs the cloud one. Because nothing here runs a model, the local model is only /// ever touched when a query is actually routed to it. enum QueryRouter { enum Route: Equatable { case local case cloud(reason: String) } static let maxLocalLength = 160 static let maxLocalWords = 28 /// - Parameters: /// - turnCount: messages already in the conversation before this one. static func route(text: String, hasAttachments: Bool, turnCount: Int) -> Route { if hasAttachments { return .cloud(reason: "attachment") } // Follow-ups lean on earlier context, which a small model tracks poorly. if turnCount > 4 { return .cloud(reason: "long conversation") } let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines) let lower = trimmed.lowercased() guard !lower.isEmpty else { return .cloud(reason: "empty") } if trimmed.count > maxLocalLength { return .cloud(reason: "long query") } if trimmed.split(whereSeparator: \.isWhitespace).count > maxLocalWords { return .cloud(reason: "long query") } if trimmed.contains("```") || trimmed.contains("\n") { return .cloud(reason: "multi-line or code") } // The system prompt already tells the model the current date and time, so those aren't "fresh" lookups. var checked = lower for phrase in ["right now", "current time", "current date", "time now", "date now", "now?"] { checked = checked.replacingOccurrences(of: phrase, with: " ") } let words = Set(checked.split(whereSeparator: { !$0.isLetter && $0 != "'" }).map(String.init)) if !words.isDisjoint(with: actionWords) { return .cloud(reason: "possible computer action") } if !words.isDisjoint(with: screenWords) { return .cloud(reason: "refers to the screen") } if !words.isDisjoint(with: freshWords) { return .cloud(reason: "needs current information") } if !words.isDisjoint(with: heavyWords) { return .cloud(reason: "needs reasoning or long output") } for phrase in phrases where lower.contains(phrase) { return .cloud(reason: "matched \"\(phrase)\"") } if lower.hasPrefix("teach seer") { return .cloud(reason: "teach") } return .local } /// Verbs that ask Seer to do something on the Mac. Chat decides whether to call `use_computer` /// only on the cloud model, so anything that might be an action goes there. private static let actionWords: Set = [ "open", "launch", "start", "quit", "close", "click", "press", "type", "tap", "scroll", "send", "play", "pause", "mute", "unmute", "turn", "enable", "disable", "toggle", "switch", "set", "change", "go", "navigate", "visit", "search", "find", "create", "make", "delete", "remove", "save", "download", "install", "move", "drag", "copy", "paste", "run", "show", "take", "screenshot", "remind", "schedule", "book", "call", "text", "email", "message", "post", ] private static let screenWords: Set = [ "screen", "window", "tab", "this", "here", "desktop", "cursor", "attached", "attachment", "image", "picture", "photo", ] private static let freshWords: Set = [ "weather", "forecast", "news", "latest", "current", "currently", "today's", "tonight", "tomorrow", "yesterday", "price", "stock", "stocks", "score", "scores", "trending", "recent", "recently", "live", "release", "released", ] private static let heavyWords: Set = [ "essay", "story", "poem", "code", "function", "script", "program", "debug", "bug", "regex", "sql", "algorithm", "prove", "proof", "derive", "analyze", "analyse", "compare", "explain", "translate", "summarize", "summarise", "rewrite", "draft", "plan", "step-by-step", "detailed", "why", "how", ] private static let phrases = ["step by step", "in depth", "in detail", "write me", "write a", "what do you think"] }