buildSessionSummaryMessages builds system messages containing the session summary if one exists. Session summaries are context-specific per session and thus should not have a checkpoint (they will be cached alongside the first user message anyway) startIndex is the index in items from which convers
(items []Item)
| 1132 | // Otherwise it is lastSummaryIndex+1 (i.e. right after the summary item), or |
| 1133 | // 0 when there is no summary. |
| 1134 | func (s *Session) buildSessionSummaryMessages(items []Item) ([]chat.Message, int) { |
| 1135 | var messages []chat.Message |
| 1136 | // Find the last summary index to determine where conversation messages start |
| 1137 | // and to include the summary in session summary messages |
| 1138 | lastSummaryIndex := -1 |
| 1139 | for i := range slices.Backward(items) { |
| 1140 | if items[i].Summary != "" { |
| 1141 | lastSummaryIndex = i |
| 1142 | break |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | if lastSummaryIndex >= 0 && lastSummaryIndex < len(items) { |
| 1147 | messages = append(messages, chat.Message{ |
| 1148 | Role: chat.MessageRoleUser, |
| 1149 | Content: "Session Summary: " + items[lastSummaryIndex].Summary, |
| 1150 | CreatedAt: s.now().Format(time.RFC3339), |
| 1151 | }) |
| 1152 | } |
| 1153 | |
| 1154 | // Determine where conversation messages should start. |
| 1155 | // If the summary has a FirstKeptEntry, we start from there so that |
| 1156 | // messages kept during compaction are included after the summary. |
| 1157 | startIndex := lastSummaryIndex + 1 |
| 1158 | if lastSummaryIndex >= 0 { |
| 1159 | kept := items[lastSummaryIndex].FirstKeptEntry |
| 1160 | if kept > 0 && kept < lastSummaryIndex { |
| 1161 | startIndex = kept |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | return messages, startIndex |
| 1166 | } |
| 1167 | |
| 1168 | // CompactionInput returns the chat messages that the compactor should |
| 1169 | // summarize together with their origin indices in s.Messages. The |
no test coverage detected