MCPcopy Create free account
hub / github.com/GongShichen/LuminaCode / CollapseMessages

Function CollapseMessages

agentContext/collapse.go:151–196  ·  view source on GitHub ↗
(messages []map[string]any, keepRecent int)

Source from the content-addressed store, hash-verified

149}
150
151func CollapseMessages(messages []map[string]any, keepRecent int) []map[string]any {
152 if len(messages) == 0 {
153 return messages
154 }
155 exchanges := partitionExchanges(messages)
156 if len(exchanges) <= keepRecent {
157 return messages
158 }
159 var older [][]map[string]any
160 var recent [][]map[string]any
161 if keepRecent == 0 {
162 older = [][]map[string]any{}
163 recent = exchanges
164 } else {
165 older = exchanges[:len(exchanges)-keepRecent]
166 recent = exchanges[len(exchanges)-keepRecent:]
167 }
168 var projections []ProjectedExchange
169 for _, exchange := range older {
170 proj := projectExchange(exchange)
171 if proj != nil {
172 projections = append(projections, *proj)
173 }
174 }
175
176 if len(projections) == 0 {
177 return messages
178 }
179 collapsedText := buildProjectionText(projections)
180 result := []map[string]any{
181 map[string]any{
182 "role": "user",
183 "content": []map[string]any{
184 map[string]any{
185 "type": "text",
186 "text": collapsedText,
187 },
188 },
189 },
190 }
191
192 for _, exchange := range recent {
193 result = append(result, exchange...)
194 }
195 return result
196}
197
198func CollapseMessagesDefault(messages []map[string]any) []map[string]any {
199 return CollapseMessages(messages, defaultKeepRecent)

Calls 3

partitionExchangesFunction · 0.85
projectExchangeFunction · 0.85
buildProjectionTextFunction · 0.85