| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode" |
| 6 | "unicode/utf8" |
| 7 | ) |
| 8 | |
| 9 | // A glance (protocol 23) is the line Base shows the moment an Assistant |
| 10 | // person turn starts, until the reply replaces it. A small model writes |
| 11 | // it; GlanceText is the guard the runner applies before sending it and |
| 12 | // the instance again before publishing it. |
| 13 | const ( |
| 14 | MaxGlanceWords = 8 |
| 15 | MaxGlanceChars = 60 |
| 16 | ) |
| 17 | |
| 18 | // glanceRefused are the words a glance never carries, in any case and |
| 19 | // anywhere in a word: the machinery behind Base (the Assistant lane's |
| 20 | // leak rules). |
| 21 | var glanceRefused = []string{"equipo", "team", "runner", "task", "branch", "merge", "coordinator", "repositor", "deploy", "specialist"} |
| 22 | |
| 23 | // glanceRefusedWords are refused as whole words only, so "Checking your |
| 24 | // calendar" and "Reviewing your draft" stay plain speech. |
| 25 | var glanceRefusedWords = map[string]bool{"check": true, "checks": true, "repo": true, "repos": true, "review": true, "reviews": true} |
| 26 | |
| 27 | // glanceRefusedMarks are a link or a path: never in a glance. |
| 28 | var glanceRefusedMarks = []string{"://", "www.", "/", "\\"} |
| 29 | |
| 30 | // glanceHex says whether a word piece holds an identifier: a run of at |
| 31 | // least seven hex characters, digits or not (a short or full SHA). A |
| 32 | // rare word like it only means no glance. |
| 33 | func glanceHex(piece string) bool { |
| 34 | run := 0 |
| 35 | for _, r := range piece { |
| 36 | if r >= '0' && r <= '9' || r >= 'a' && r <= 'f' { |
| 37 | if run++; run >= 7 { |
| 38 | return true |
| 39 | } |
| 40 | continue |
| 41 | } |
| 42 | run = 0 |
| 43 | } |
| 44 | return false |
| 45 | } |
| 46 | |
| 47 | // glancePieces are the line's word pieces: split at every character |
| 48 | // that is not a letter or digit (hyphens, colons, dots, underscores, |
| 49 | // apostrophes), so "pre-checks" holds "checks". |
| 50 | func glancePieces(lower string) []string { |
| 51 | return strings.FieldsFunc(lower, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) }) |
| 52 | } |
| 53 | |
| 54 | // glanceMarkup is markdown punctuation, dropped from a glance. |
| 55 | const glanceMarkup = "*`#>~[]|\"“”" |
| 56 | |
| 57 | // GlanceText makes raw one plain line: newlines, tabs and control |
| 58 | // characters become spaces, markdown and quotes go, as does a link's |
| 59 | // target and a leading list mark; then it keeps at most MaxGlanceWords |
| 60 | // words and, whole words only, MaxGlanceChars characters. ok is false |
| 61 | // when nothing is left or the line names the machinery. |
| 62 | func GlanceText(raw string) (string, bool) { |
| 63 | var b strings.Builder |
| 64 | for i := 0; i < len(raw); { |
| 65 | r, n := utf8.DecodeRuneInString(raw[i:]) |
| 66 | switch { |
| 67 | case r == ']' && strings.HasPrefix(raw[i+n:], "("): |
| 68 | // A link: its words stay, its target goes. |
| 69 | end := strings.IndexByte(raw[i+n:], ')') |
| 70 | if end < 0 { |
| 71 | end = len(raw) - i - n - 1 |
| 72 | } |
| 73 | i += n + end + 1 |
| 74 | continue |
| 75 | case r == utf8.RuneError && n <= 1, unicode.IsControl(r), unicode.IsSpace(r), r == '_': |
| 76 | // An underscore is markdown or joins words: a space either way. |
| 77 | b.WriteByte(' ') |
| 78 | case strings.ContainsRune(glanceMarkup, r): |
| 79 | default: |
| 80 | b.WriteRune(r) |
| 81 | } |
| 82 | i += n |
| 83 | } |
| 84 | words := strings.Fields(b.String()) |
| 85 | if len(words) > 0 && (words[0] == "-" || words[0] == "•" || words[0] == "+") { |
| 86 | words = words[1:] |
| 87 | } |
| 88 | words = words[:min(len(words), MaxGlanceWords)] |
| 89 | line := "" |
| 90 | for _, w := range words { |
| 91 | next := w |
| 92 | if line != "" { |
| 93 | next = line + " " + w |
| 94 | } |
| 95 | if utf8.RuneCountInString(next) > MaxGlanceChars { |
| 96 | break |
| 97 | } |
| 98 | line = next |
| 99 | } |
| 100 | if line == "" { |
| 101 | return "", false |
| 102 | } |
| 103 | lower := strings.ToLower(line) |
| 104 | for _, w := range glanceRefused { |
| 105 | if strings.Contains(lower, w) { |
| 106 | return "", false |
| 107 | } |
| 108 | } |
| 109 | for _, m := range glanceRefusedMarks { |
| 110 | if strings.Contains(lower, m) { |
| 111 | return "", false |
| 112 | } |
| 113 | } |
| 114 | for _, w := range glancePieces(lower) { |
| 115 | if glanceRefusedWords[w] || glanceHex(w) { |
| 116 | return "", false |
| 117 | } |
| 118 | } |
| 119 | return line, true |
| 120 | } |
| 121 | |