CompactionInput returns the chat messages that the compactor should summarize together with their origin indices in s.Messages. The returned messages are independent copies safe for the caller to mutate (cloned via snapshotItems); the parallel sessIndices slice maps each entry back to its source ite
()
| 1193 | // method is safe to call concurrently with AddMessage / ApplyCompaction |
| 1194 | // on the same session. |
| 1195 | func (s *Session) CompactionInput() ([]chat.Message, []int) { |
| 1196 | items := s.snapshotItems() |
| 1197 | |
| 1198 | lastSummaryIndex := -1 |
| 1199 | for i := range slices.Backward(items) { |
| 1200 | if items[i].Summary != "" { |
| 1201 | lastSummaryIndex = i |
| 1202 | break |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | var ( |
| 1207 | messages []chat.Message |
| 1208 | sessIndices []int |
| 1209 | ) |
| 1210 | |
| 1211 | if lastSummaryIndex >= 0 { |
| 1212 | messages = append(messages, chat.Message{ |
| 1213 | Role: chat.MessageRoleUser, |
| 1214 | Content: "Session Summary: " + items[lastSummaryIndex].Summary, |
| 1215 | CreatedAt: s.now().Format(time.RFC3339), |
| 1216 | }) |
| 1217 | // The synthetic message stands in for the prior summary item; |
| 1218 | // when this index lands inside the kept tail we want the |
| 1219 | // summary item itself preserved so the next compaction round |
| 1220 | // still sees it via buildSessionSummaryMessages. |
| 1221 | sessIndices = append(sessIndices, lastSummaryIndex) |
| 1222 | } |
| 1223 | |
| 1224 | startIndex := lastSummaryIndex + 1 |
| 1225 | if lastSummaryIndex >= 0 { |
| 1226 | kept := items[lastSummaryIndex].FirstKeptEntry |
| 1227 | if kept > 0 && kept < lastSummaryIndex { |
| 1228 | startIndex = kept |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | for i := startIndex; i < len(items); i++ { |
| 1233 | if !items[i].IsMessage() { |
| 1234 | continue |
| 1235 | } |
| 1236 | msg := items[i].Message.Message |
| 1237 | if msg.Role == chat.MessageRoleSystem { |
| 1238 | continue |
| 1239 | } |
| 1240 | messages = append(messages, msg) |
| 1241 | sessIndices = append(sessIndices, i) |
| 1242 | } |
| 1243 | return messages, sessIndices |
| 1244 | } |
| 1245 | |
| 1246 | func (s *Session) GetMessages(a *agent.Agent, extraSystemMessages ...chat.Message) []chat.Message { |
| 1247 | slog.Debug("Getting messages for agent", "agent", a.Name(), "session_id", s.ID) |