splitThinking function
Splits the <think>...</think> part that models such as DeepSeek R1
and Qwen put at the start of a reply off the answer.
Implementation
ThinkingSplit splitThinking(String text, {required bool complete}) {
final start = text.length - text.trimLeft().length;
final rest = text.substring(start);
for (final (open, close) in _tags) {
if (rest.startsWith(open)) {
final body = rest.substring(open.length);
final end = body.indexOf(close);
if (end == -1) {
if (complete) return ThinkingSplit(_orNull(body), '', done: true);
return ThinkingSplit(_withoutPartial(body, close), '', done: false);
}
return ThinkingSplit(
_orNull(body.substring(0, end)),
body.substring(end + close.length).replaceFirst(_leadingBlankLines, ''),
done: true,
);
}
}
// "<thi" might still become "<think>"
if (!complete &&
rest.isNotEmpty &&
_tags.any((tag) => tag.$1.startsWith(rest))) {
return const ThinkingSplit('', '', done: false);
}
return ThinkingSplit(null, text, done: true);
}