package websource import ( "bytes" "encoding/json" "encoding/xml" "errors" "io" "strings" ) // MaxReadable bounds a page's readable text (spec 2026-11-26-assistant- // abilities ยง4.3: each page at most 16 KiB before the answer step's // budget cuts it further). const MaxReadable = 16 << 10 // Readable is a fetched body's title and readable text, by its sniffed // type (guard.Response.Type): HTML through ExtractHTML (scripts and // styles dropped), a feed's channel title and each item's title and // summary (their markup reduced to text), JSON pretty-printed, XML's // text, plain text as it is. Anything else gives nothing. The text // is at most MaxReadable bytes, cut on a character boundary. func Readable(body []byte, typ string, base URL) (title, text string) { switch typ { case "html": p := ExtractHTML(body, base, MaxReadable, 0) return p.Title, p.Text case "feed": return readFeed(body, base) case "json": var v any if json.Unmarshal(body, &v) != nil { return "", "" } out, err := json.MarshalIndent(v, "", " ") if err != nil { return "", "" } return "", clip(string(out), MaxReadable) case "xml": return "", clip(strings.Join(xmlText(body), "\n"), MaxReadable) case "text": return "", clip(strings.ToValidUTF8(string(body), ""), MaxReadable) } return "", "" } // readFeed reads an RSS or Atom feed: its title, then each item's title // and summary (RSS description, Atom summary or content), a blank line // between items. func readFeed(body []byte, base URL) (string, string) { d := xml.NewDecoder(bytes.NewReader(body)) d.Strict = false d.CharsetReader = func(_ string, r io.Reader) (io.Reader, error) { return r, nil } var title string var items []string var cur []string var buf strings.Builder inItem := false for { tok, err := d.Token() if err != nil { if !errors.Is(err, io.EOF) && len(items) == 0 && title == "" { return "", "" } break } switch t := tok.(type) { case xml.StartElement: name := strings.ToLower(t.Name.Local) if name == "item" || name == "entry" { inItem, cur = true, nil } buf.Reset() case xml.CharData: buf.Write(t) case xml.EndElement: name := strings.ToLower(t.Name.Local) value := strings.TrimSpace(buf.String()) buf.Reset() switch { case name == "item" || name == "entry": if len(cur) > 0 { items = append(items, strings.Join(cur, "\n")) } inItem = false case inItem && (name == "title" || name == "description" || name == "summary" || name == "content"): // A summary may carry escaped markup: its text only. if v := ExtractHTML([]byte(value), base, MaxReadable, 0).Text; v != "" { cur = append(cur, oneLine(v)) } case !inItem && name == "title" && title == "": title = clip(oneLine(value), maxMetaValue) } } } return title, clip(strings.Join(items, "\n\n"), MaxReadable) } // xmlText is a document's text runs, each trimmed, in order. func xmlText(body []byte) []string { d := xml.NewDecoder(bytes.NewReader(body)) d.Strict = false d.CharsetReader = func(_ string, r io.Reader) (io.Reader, error) { return r, nil } var out []string size := 0 for size < MaxReadable { tok, err := d.Token() if err != nil { break } if c, ok := tok.(xml.CharData); ok { if s := oneLine(string(c)); s != "" { out = append(out, s) size += len(s) + 1 } } } return out }