microcompact method
Lightweight pre-API clearing of old tool results.
Keeps the last keepRecent tool-result-bearing messages intact and
replaces older compactable tool results with clearedMessage.
This is a cheap way to reclaim tokens without an API round-trip.
Implementation
List<Message> microcompact(List<Message> messages, {int keepRecent = 5}) {
final result = List<Message>.from(messages);
final toolResultIndices = <int>[];
for (int i = 0; i < result.length; i++) {
final msg = result[i];
if (msg.role != MessageRole.user) continue;
for (final block in msg.content) {
if (block is ToolResultBlock) {
toolResultIndices.add(i);
break;
}
}
}
if (toolResultIndices.length <= keepRecent) return result;
final toClear = toolResultIndices.sublist(
0,
toolResultIndices.length - keepRecent,
);
for (final idx in toClear) {
final msg = result[idx];
final clearedContent =
msg.content.map((block) {
if (block is ToolResultBlock && _isCompactable(block)) {
return ToolResultBlock(
toolUseId: block.toolUseId,
content: clearedMessage,
isError: block.isError,
);
}
return block;
}).toList();
result[idx] = Message(
id: msg.id,
role: msg.role,
content: clearedContent,
stopReason: msg.stopReason,
usage: msg.usage,
);
}
return result;
}