feat(link-summ): add SummarizeLink API and server integration; youtube oEmbed returns HTML only to avoid duplicate thumbs

This commit is contained in:
Thomas Cravey 2025-08-17 18:52:39 -05:00
parent 29d94c13d5
commit 575622b45c
3 changed files with 116 additions and 17 deletions

View file

@ -257,7 +257,9 @@ func (s *Server) handleLinkCard(w http.ResponseWriter, r *http.Request) {
respTw, errTw := client.Do(reqTw)
if errTw == nil && respTw.StatusCode >= 200 && respTw.StatusCode < 300 {
defer respTw.Body.Close()
var o struct{ HTML string `json:"html"` }
var o struct {
HTML string `json:"html"`
}
if err := json.NewDecoder(respTw.Body).Decode(&o); err == nil && o.HTML != "" {
card := linkCard{URL: raw, HTML: o.HTML}
s.cardCache[raw] = card
@ -291,7 +293,8 @@ func (s *Server) handleLinkCard(w http.ResponseWriter, r *http.Request) {
HTML string `json:"html"`
}
if err := json.NewDecoder(respY.Body).Decode(&o); err == nil {
card := linkCard{URL: raw, Title: o.Title, Image: o.Thumb, HTML: o.HTML}
card := linkCard{URL: raw, Title: o.Title, HTML: o.HTML}
// Note: do not set Image when HTML is provided to avoid duplicate thumbnails (embed already includes preview)
s.cardCache[raw] = card
s.cardCacheExp[raw] = time.Now().Add(24 * time.Hour)
w.Header().Set("Content-Type", "application/json")
@ -422,25 +425,38 @@ func (s *Server) handleLinkSummary(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("summarizer not configured"))
return
}
if s.summaryCache == nil {
s.summaryCache = make(map[string]string)
}
if s.summaryCacheExp == nil {
s.summaryCacheExp = make(map[string]time.Time)
}
if s.summaryCache == nil { s.summaryCache = make(map[string]string) }
if s.summaryCacheExp == nil { s.summaryCacheExp = make(map[string]time.Time) }
if exp, ok := s.summaryCacheExp[raw]; ok && time.Now().Before(exp) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"summary": s.summaryCache[raw]})
return
}
// Prefer link-specific summarization when available
if sl, ok := s.Summarizer.(interface{ SummarizeLink(context.Context, string) (string, error) }); ok {
tout := s.SummarizerTimeout
if tout <= 0 { tout = 5 * time.Minute }
if tout > 2*time.Minute { tout = 2 * time.Minute }
ctx, cancel := context.WithTimeout(r.Context(), tout)
defer cancel()
sum, err := sl.SummarizeLink(ctx, raw)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte("summarizer error"))
return
}
if sum == "" { sum = "(no summary)" }
s.summaryCache[raw] = sum
s.summaryCacheExp[raw] = time.Now().Add(24 * time.Hour)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"summary": sum})
return
}
// Fallback to generic path
msgs := []store.Message{{Channel: "#links", Author: "link", Body: raw, Time: time.Now().UTC()}}
tout := s.SummarizerTimeout
if tout <= 0 {
tout = 5 * time.Minute
}
if tout > 2*time.Minute {
tout = 2 * time.Minute
}
if tout <= 0 { tout = 5 * time.Minute }
if tout > 2*time.Minute { tout = 2 * time.Minute }
ctx, cancel := context.WithTimeout(r.Context(), tout)
defer cancel()
sum, err := s.Summarizer.Summarize(ctx, "#links", msgs, 0)
@ -449,9 +465,7 @@ func (s *Server) handleLinkSummary(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("summarizer error"))
return
}
if sum == "" {
sum = "(no summary)"
}
if sum == "" { sum = "(no summary)" }
s.summaryCache[raw] = sum
s.summaryCacheExp[raw] = time.Now().Add(24 * time.Hour)
w.Header().Set("Content-Type", "application/json")