Files
eleith a8c5dc5475 feat(ai): Implement multi-call AI grouping and summarization (#56)
This commit enhances the AI view to be more reliable and performant.

The AI digest generation now makes multiple calls to the LLM to create its own primary and sub-groups, replacing the previous single-call, category-based approach. The overview summary has been removed in favor of per-group summaries to manage context length. To improve reliability, the LLM calls for summarization and sub-grouping have been separated, and the timeout handling has been refactored to use a per-try timeout, making it more resilient to transient server errors.

The email template has also been updated to include the primary group summaries, aligning its purpose with the overview HTML page.

Reviewed-on: #56
2025-09-05 19:11:22 +00:00

104 lines
2.8 KiB
Go

package view
import (
"miniflux-digest/internal/models"
"sort"
"time"
"miniflux-digest/internal/utils"
)
func GroupByDate(entries []*models.Entry) []*models.PrimaryGroup {
groups := make(map[string]*models.PrimaryGroup)
for _, entry := range entries {
dateKey := entry.Date.Format("2006-01-02")
if _, ok := groups[dateKey]; !ok {
groups[dateKey] = &models.PrimaryGroup{
Title: entry.Date.Format("Jan 2, 2006"),
}
}
groups[dateKey].Entries = append(groups[dateKey].Entries, entry)
}
var result []*models.PrimaryGroup
for _, pg := range groups {
result = append(result, pg)
}
sort.Slice(result, func(i, j int) bool {
iDate, _ := time.Parse("Jan 2, 2006", result[i].Title)
jDate, _ := time.Parse("Jan 2, 2006", result[j].Title)
return iDate.Before(jDate)
})
return result
}
const (
DayGroupLayout = "2006-01-02"
DayGroupTitleLayout = "Jan 2, 2006"
)
func SubGroupByDay(pgs []*models.PrimaryGroup) []*models.PrimaryGroupDigestData {
var allPrimaryGroups []*models.PrimaryGroupDigestData
for _, pg := range pgs {
entryGroupsMap := make(map[string]*models.EntryGroup)
for _, entry := range pg.Entries {
dateKey := entry.Date.Format(DayGroupLayout)
if _, ok := entryGroupsMap[dateKey]; !ok {
entryGroupsMap[dateKey] = &models.EntryGroup{
Title: entry.Date.Format(DayGroupTitleLayout),
Entries: []*models.Entry{},
Slug: utils.Slugify(entry.Date.Format(DayGroupTitleLayout)),
}
}
entryGroupsMap[dateKey].Entries = append(entryGroupsMap[dateKey].Entries, entry)
}
sortedEntryGroups := make([]*models.EntryGroup, 0, len(entryGroupsMap))
for _, group := range entryGroupsMap {
uniqueFeedIDs := make(map[int64]bool)
for _, entry := range group.Entries {
uniqueFeedIDs[entry.FeedID] = true
}
group.TotalEntries = len(group.Entries)
group.TotalFeeds = len(uniqueFeedIDs)
sortedEntryGroups = append(sortedEntryGroups, group)
}
sort.Slice(sortedEntryGroups, func(i, j int) bool {
iDate, _ := time.Parse("Jan 2, 2006", sortedEntryGroups[i].Title)
jDate, _ := time.Parse("Jan 2, 2006", sortedEntryGroups[j].Title)
return iDate.Before(jDate)
})
for _, group := range sortedEntryGroups {
sort.Slice(group.Entries, func(i, j int) bool {
return group.Entries[i].Date.Before(group.Entries[j].Date)
})
}
allPrimaryGroups = append(allPrimaryGroups, &models.PrimaryGroupDigestData{
ID: pg.ID,
Title: pg.Title,
Slug: utils.Slugify(pg.Title),
SubGroups: sortedEntryGroups,
})
}
return allPrimaryGroups
}
func BuildDigestDataByDate(entries []*models.Entry) []*models.PrimaryGroupDigestData {
primaryGroups := GroupByDate(entries)
allPrimaryGroups := SubGroupByDay(primaryGroups)
sort.Slice(allPrimaryGroups, func(i, j int) bool {
return allPrimaryGroups[i].Title < allPrimaryGroups[j].Title
})
return allPrimaryGroups
}