parseInlineThinkingMessageId static method

({int blockIndex, String parentLlmMessageId})? parseInlineThinkingMessageId(
  1. String thinkingMessageId
)

Parsed parent LLM id and block index from a thinking sibling id.

Implementation

static ({String parentLlmMessageId, int blockIndex})?
parseInlineThinkingMessageId(String thinkingMessageId) {
  final String trimmedId = thinkingMessageId.trim();
  final RegExpMatch? indexedMatch = RegExp(
    r'^(.+)_thinking_(\d+)$',
  ).firstMatch(trimmedId);
  if (indexedMatch != null) {
    final String? indexText = indexedMatch.group(2);
    final int? blockIndex = indexText == null
        ? null
        : int.tryParse(indexText);
    if (blockIndex != null && blockIndex > 0) {
      return (
        parentLlmMessageId: indexedMatch.group(1) ?? '',
        blockIndex: blockIndex,
      );
    }
  }
  if (trimmedId.endsWith(idSuffix) &&
      !_indexedIdSuffixRegex.hasMatch(trimmedId)) {
    return (
      parentLlmMessageId: trimmedId.substring(
        0,
        trimmedId.length - idSuffix.length,
      ),
      blockIndex: 1,
    );
  }
  return null;
}