| 233 | } |
| 234 | |
| 235 | func ProjectCollapsedView(messages []map[string]any, regions []CollapsedRegion) []map[string]any { |
| 236 | if regions == nil || len(regions) == 0 { |
| 237 | return deepcopy.Copy(messages).([]map[string]any) |
| 238 | } |
| 239 | sortedRegions := append([]CollapsedRegion(nil), regions...) |
| 240 | sort.Slice(sortedRegions, func(i, j int) bool { |
| 241 | return sortedRegions[i].StartIdx < sortedRegions[j].StartIdx |
| 242 | }) |
| 243 | insertMap := map[int]string{} |
| 244 | skipIndices := mapset.NewSet[int]() |
| 245 | for _, region := range sortedRegions { |
| 246 | insertMap[region.StartIdx] = region.Summary |
| 247 | for i := region.StartIdx; i < region.EndIdx; i++ { |
| 248 | skipIndices.Add(i) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | var result []map[string]any |
| 253 | for i, msg := range messages { |
| 254 | if skipIndices.Contains(i) { |
| 255 | if _, ok := insertMap[i]; ok { |
| 256 | result = append(result, map[string]any{ |
| 257 | "role": "user", |
| 258 | "content": []map[string]any{ |
| 259 | map[string]any{ |
| 260 | "type": "text", |
| 261 | "text": insertMap[i], |
| 262 | }, |
| 263 | }, |
| 264 | }) |
| 265 | } |
| 266 | continue |
| 267 | } |
| 268 | result = append(result, deepcopy.Copy(msg).(map[string]any)) |
| 269 | } |
| 270 | return result |
| 271 | } |
| 272 | |
| 273 | func ApplyCollapseIfNeed(messages []map[string]any, currentTokens, collapseThresholdTokens int, existingRegions []CollapsedRegion) (bool, []CollapsedRegion) { |
| 274 | var regions []CollapsedRegion |