package domain import ( "strings" "git.cin.su/m/Assistant/contracts/interaction" ) // StripAssistantVocative removes a leading assistant name used as an address. // The original utterance remains the historical source; this value is only for // interpretation, cognitive prompts and external queries. func StripAssistantVocative(message string) string { trimmed := strings.TrimSpace(message) lower := strings.ToLower(trimmed) for _, name := range []string{"дед", "домовой"} { if !strings.HasPrefix(lower, name) { continue } rest := trimmed[len(name):] if rest == "" || !strings.ContainsRune(" \t,;:!?—-", []rune(rest)[0]) { continue } return strings.TrimSpace(strings.TrimLeft(rest, " \t,;:!?—-")) } return trimmed } // IndependentRequestParts keeps separate user actions visible to the // orchestrator. It is intentionally limited to the ordinary coordinating // conjunction; the cognitive route remains responsible for ambiguous prose. func IndependentRequestParts(message string) []string { cleaned := StripAssistantVocative(message) parts := strings.Split(cleaned, " и ") if len(parts) < 2 { return []string{cleaned} } result := make([]string, 0, len(parts)) for _, part := range parts { part = strings.TrimSpace(part) if part != "" { result = append(result, part) } } if len(result) < 2 { return []string{cleaned} } return result } // LocalSearchClarification prevents a proximity search from silently using a // default city when the user did not provide a place. func LocalSearchClarification(message string) string { tokens := semanticTokens(StripAssistantVocative(message)) if !containsStemInTokens(tokens, "ресторан", "кафе") || !containsStemInTokens(tokens, "ближ", "рядом") { return "" } for index, token := range tokens { if token != "в" && token != "на" && token != "около" && !containsStem(token, "район", "город") { continue } if index+1 < len(tokens) || containsStem(token, "район", "город") { return "" } } return "В каком городе или районе искать ресторан?" } // ContinueLocalSearch turns a location-only follow-up into the previous local // search request, but only when that request is present in this conversation. func ContinueLocalSearch(message string, history []interaction.HumanUtterance) string { current := strings.TrimSpace(StripAssistantVocative(message)) tokens := semanticTokens(current) if len(tokens) == 0 || len(tokens) > 3 || (tokens[0] != "в" && tokens[0] != "на" && tokens[0] != "около") { return "" } var latest *interaction.HumanUtterance for index := range history { candidate := history[index] candidateText := strings.TrimSpace(StripAssistantVocative(candidate.Content)) candidateTokens := semanticTokens(candidateText) if !containsStemInTokens(candidateTokens, "ресторан", "кафе") || !containsStemInTokens(candidateTokens, "ближ", "рядом") { continue } if latest == nil || candidate.ReceivedAtUnixMillis > latest.ReceivedAtUnixMillis { latest = &candidate } } if latest != nil { previous := strings.TrimSpace(StripAssistantVocative(latest.Content)) return strings.TrimSpace(previous + " " + current) } return "" } // TravelSearchClarification keeps an incomplete travel request in dialogue // instead of reducing it to an unrelated generic web search. func TravelSearchClarification(message string, history []interaction.HumanUtterance) string { current := semanticTokens(StripAssistantVocative(message)) travel := travelCue(current) if !travel && travelFollowUpCue(current) { for index := len(history) - 1; index >= 0; index-- { if travelCue(semanticTokens(history[index].Content)) { travel = true break } } } if !travel { return "" } origin := containsStemInTokens(current, "из", "откуда", "от") destination := containsStemInTokens(current, "в", "куда", "до") date := containsStemInTokens(current, "сегодня", "завтра", "послезавтр", "январ", "феврал", "март", "апрел", "мая", "июн", "июл", "август", "сентябр", "октябр", "ноябр", "декабр") if !date { for _, token := range current { if hasDigit(token) { date = true break } } } if origin && destination && date { return "" } if !destination { if contextDestination := travelContextDestination(history); contextDestination != "" { destination = true if !origin && !date { return "Из какого города вылетать в " + contextDestination + " и на какую дату?" } if !origin { return "Из какого города вылетать в " + contextDestination + "?" } } } if destination && !origin && !date { return "Из какого города вылетать и на какую дату?" } if destination && !origin { return "Из какого города вылетать?" } if destination && !date { return "На какую дату нужны билеты?" } return "Уточните маршрут и дату: откуда, куда и на какой день нужны билеты?" } func travelContextDestination(history []interaction.HumanUtterance) string { for index := len(history) - 1; index >= 0; index-- { tokens := semanticTokens(history[index].Content) for tokenIndex, token := range tokens { if token != "в" && token != "до" { continue } if tokenIndex+1 < len(tokens) && len([]rune(tokens[tokenIndex+1])) >= 3 { return capitalizeWord(tokens[tokenIndex+1]) } } } return "" } func capitalizeWord(value string) string { runes := []rune(value) if len(runes) == 0 { return value } return strings.ToUpper(string(runes[0])) + string(runes[1:]) } func travelCue(tokens []string) bool { return containsStemInTokens(tokens, "билет", "рейс", "поезд", "автобус", "перелет", "перелёт") } func travelFollowUpCue(tokens []string) bool { return containsStemInTokens(tokens, "куда", "откуда", "маршрут", "летим", "дата", "число") } func hasDigit(value string) bool { for _, runeValue := range value { if runeValue >= '0' && runeValue <= '9' { return true } } return false }