Files
pipelines/internal/protocol/result.go
T
alan bfef465977 Fix build: define protocol.NormalizeText used by the summariser
internal/summarise called protocol.NormalizeText, which was never defined, so the tree did not compile. Add the shared normalisation helper: drop zero-width formatting runes, turn control characters into spaces and collapse whitespace runs (including the non-breaking space variants) into single spaces. Reuse it in OutputPreview instead of duplicating the mapping logic.

Cover the awkward inputs (newlines, tabs, non-breaking spaces, zero-width runes, surrounding quotes, trailing punctuation) in the protocol and summarise tests. Unrelated working-tree changes are left uncommitted.
2026-10-16 12:28:27 +01:00

86 lines
2.4 KiB
Go

package protocol
import (
"encoding/json"
"sort"
"strings"
"unicode"
)
// NormalizeText reduces text to a single clean line. Zero-width formatting
// runes (the zero-width space, the joiners, the word joiner, the byte-order
// mark, the Mongolian vowel separator and the soft hyphen) are dropped, control
// characters become spaces, and every run of whitespace - including the
// non-breaking space variants - is collapsed, so the result has no leading,
// trailing or repeated spaces. It is the shared primitive behind OutputPreview
// and search snippets, so a retained preview and a snippet stay single-line and
// safe to render in a terminal.
func NormalizeText(text string) string {
text = strings.Map(func(r rune) rune {
if isZeroWidth(r) {
return -1
}
if unicode.IsControl(r) {
return ' '
}
return r
}, text)
// strings.Fields splits on unicode.IsSpace, which covers the non-breaking
// space variants, and single spaces join the surviving fields.
return strings.Join(strings.Fields(text), " ")
}
// isZeroWidth reports whether r is an invisible zero-width rune that must not
// survive into a one-line preview.
func isZeroWidth(r rune) bool {
switch r {
case '\u180e', '\u00ad', '\u200b', '\u200c', '\u200d', '\u2060', '\ufeff':
return true
}
return false
}
// OutputPreview quotes retained output, never infers accomplishments from a
// successful process or invents a schema for technology-neutral workflows.
func OutputPreview(output map[string]any) string {
keys := []string{"summary", "result", "report", "message", "accomplished", "artifacts", "evidence", "reason", "error"}
seen := map[string]bool{}
remaining := make([]string, 0, len(output))
for key := range output {
remaining = append(remaining, key)
}
sort.Strings(remaining)
keys = append(keys, remaining...)
for _, key := range keys {
if seen[key] || key == "input" {
continue
}
seen[key] = true
value, exists := output[key]
if !exists || value == nil {
continue
}
text, ok := value.(string)
if !ok {
encoded, err := json.Marshal(value)
if err != nil {
continue
}
text = string(encoded)
}
text = NormalizeText(text)
if text == "" || text == "[]" || text == "{}" {
continue
}
if key != "summary" && key != "result" && key != "report" && key != "message" {
text = key + ": " + text
}
runes := []rune(text)
if len(runes) > 280 {
text = string(runes[:279]) + "…"
}
return text
}
return ""
}