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

Function MicroCompactMessages

agentContext/micro.go:105–155  ·  view source on GitHub ↗
(messages []map[string]any, isCacheHold bool, keepRecent int)

Source from the content-addressed store, hash-verified

103}
104
105func MicroCompactMessages(messages []map[string]any, isCacheHold bool, keepRecent int) ([]map[string]any, []CacheEdit) {
106 result := deepcopy.Copy(messages).([]map[string]any)
107 var edits []CacheEdit
108 keepRecent = MaxInt(keepRecent, 1)
109 toolNameMap := BuildToolNameMap(messages)
110 allBlocks := iterToolResultBlock(result)
111 var compressible []ToolUseResultBlock
112 for _, block := range allBlocks {
113 toolUseID, ok := block.Block["tool_use_id"].(string)
114 if !ok {
115 continue
116 }
117 toolName, ok := toolNameMap[toolUseID]
118 if !ok {
119 continue
120 }
121 if _, ok := compressibleTools[toolName]; ok {
122 compressible = append(compressible, block)
123 }
124 }
125
126 if len(compressible) == 0 {
127 return result, edits
128 }
129
130 stableCount := MaxInt(0, len(compressible)-keepRecent)
131 stableBlocks := compressible[:stableCount]
132 if isCacheHold {
133 for _, block := range stableBlocks {
134 message := result[block.MessageIndex]
135 messageContent, ok := contentBlocks(message["content"])
136 if !ok {
137 continue
138 }
139 if block.BlockIndex < 0 || block.BlockIndex >= len(messageContent) {
140 continue
141 }
142 rawItem := messageContent[block.BlockIndex]
143 rawItem["content"] = cleanPlaceHolder
144 }
145 } else {
146 for _, block := range stableBlocks {
147 toolUseID, ok := block.Block["tool_use_id"].(string)
148 if !ok {
149 continue
150 }
151 edits = append(edits, NewCacheEdit(toolUseID, nil))
152 }
153 }
154 return result, edits
155}
156
157func CountClearedToolResult(messages []map[string]any) int {
158 count := 0

Calls 5

MaxIntFunction · 0.85
BuildToolNameMapFunction · 0.85
iterToolResultBlockFunction · 0.85
NewCacheEditFunction · 0.85
contentBlocksFunction · 0.70