applyCacheBreakpoints function
List<Map<String, dynamic> >
applyCacheBreakpoints(
- List<
Map< messages,String, dynamic> > - CacheConfig config
Apply cache breakpoints to a list of message maps destined for the API.
Inserts cache_control: {"type": "ephemeral"} on the last content block
of each message at the specified breakpoint indices.
Implementation
List<Map<String, dynamic>> applyCacheBreakpoints(
List<Map<String, dynamic>> messages,
CacheConfig config,
) {
if (!config.enabled || config.breakpoints.isEmpty) return messages;
// Deep copy to avoid mutating the originals.
final result = messages.map((m) => Map<String, dynamic>.from(m)).toList();
for (final idx in config.breakpoints) {
if (idx < 0 || idx >= result.length) continue;
final message = result[idx];
final content = message['content'];
if (content is List && content.isNotEmpty) {
final contentList = List<Map<String, dynamic>>.from(
content.map((c) => Map<String, dynamic>.from(c as Map)),
);
// Mark the last content block.
contentList.last['cache_control'] = {'type': 'ephemeral'};
message['content'] = contentList;
} else if (content is String) {
// If content is a plain string, wrap it in a block with cache_control.
message['content'] = [
{
'type': 'text',
'text': content,
'cache_control': {'type': 'ephemeral'},
},
];
}
}
return result;
}