build method
Build the final system prompt.
Implementation
String build({int? maxTokens}) {
final sorted = List.of(_sections)
..sort((a, b) => a.priority.compareTo(b.priority));
final buf = StringBuffer();
for (final section in sorted) {
if (buf.isNotEmpty) buf.write('\n\n');
buf.write(section.content);
}
var result = buf.toString();
// Truncate if needed (rough token estimate: 1 token ≈ 4 chars)
if (maxTokens != null) {
final maxChars = maxTokens * 4;
if (result.length > maxChars) {
result = result.substring(0, maxChars);
result += '\n\n[System prompt truncated due to length]';
}
}
return result;
}