package wire import ( "strings" "unicode" "unicode/utf8" ) // A glance (protocol 23) is the line Base shows the moment an Assistant // person turn starts, until the reply replaces it. A small model writes // it; GlanceText is the guard the runner applies before sending it and // the instance again before publishing it. const ( MaxGlanceWords = 8 MaxGlanceChars = 60 ) // glanceRefused are the words a glance never carries, in any case and // anywhere in a word: the machinery behind Base (the Assistant lane's // leak rules). var glanceRefused = []string{"equipo", "team", "runner", "task", "branch", "merge", "coordinator", "repositor", "deploy", "specialist"} // glanceRefusedWords are refused as whole words only, so "Checking your // calendar" and "Reviewing your draft" stay plain speech. var glanceRefusedWords = map[string]bool{"check": true, "checks": true, "repo": true, "repos": true, "review": true, "reviews": true} // glanceRefusedMarks are a link or a path: never in a glance. var glanceRefusedMarks = []string{"://", "www.", "/", "\\"} // glanceHex says whether a word piece holds an identifier: a run of at // least seven hex characters, digits or not (a short or full SHA). A // rare word like it only means no glance. func glanceHex(piece string) bool { run := 0 for _, r := range piece { if r >= '0' && r <= '9' || r >= 'a' && r <= 'f' { if run++; run >= 7 { return true } continue } run = 0 } return false } // glancePieces are the line's word pieces: split at every character // that is not a letter or digit (hyphens, colons, dots, underscores, // apostrophes), so "pre-checks" holds "checks". func glancePieces(lower string) []string { return strings.FieldsFunc(lower, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) }) } // glanceMarkup is markdown punctuation, dropped from a glance. const glanceMarkup = "*`#>~[]|\"“”" // GlanceText makes raw one plain line: newlines, tabs and control // characters become spaces, markdown and quotes go, as does a link's // target and a leading list mark; then it keeps at most MaxGlanceWords // words and, whole words only, MaxGlanceChars characters. ok is false // when nothing is left or the line names the machinery. func GlanceText(raw string) (string, bool) { var b strings.Builder for i := 0; i < len(raw); { r, n := utf8.DecodeRuneInString(raw[i:]) switch { case r == ']' && strings.HasPrefix(raw[i+n:], "("): // A link: its words stay, its target goes. end := strings.IndexByte(raw[i+n:], ')') if end < 0 { end = len(raw) - i - n - 1 } i += n + end + 1 continue case r == utf8.RuneError && n <= 1, unicode.IsControl(r), unicode.IsSpace(r), r == '_': // An underscore is markdown or joins words: a space either way. b.WriteByte(' ') case strings.ContainsRune(glanceMarkup, r): default: b.WriteRune(r) } i += n } words := strings.Fields(b.String()) if len(words) > 0 && (words[0] == "-" || words[0] == "•" || words[0] == "+") { words = words[1:] } words = words[:min(len(words), MaxGlanceWords)] line := "" for _, w := range words { next := w if line != "" { next = line + " " + w } if utf8.RuneCountInString(next) > MaxGlanceChars { break } line = next } if line == "" { return "", false } lower := strings.ToLower(line) for _, w := range glanceRefused { if strings.Contains(lower, w) { return "", false } } for _, m := range glanceRefusedMarks { if strings.Contains(lower, m) { return "", false } } for _, w := range glancePieces(lower) { if glanceRefusedWords[w] || glanceHex(w) { return "", false } } return line, true }