checkReasoningStatus static method

ReasoningDetectionResult checkReasoningStatus({
  1. required Map<String, dynamic>? delta,
  2. required bool hasReasoningContent,
  3. required String lastChunk,
})

Check if reasoning just finished based on delta content This matches the TypeScript isReasoningJustDone function

Implementation

static ReasoningDetectionResult checkReasoningStatus({
  required Map<String, dynamic>? delta,
  required bool hasReasoningContent,
  required String lastChunk,
}) {
  // 如果有reasoning_content或reasoning或thinking,说明是在思考中
  bool updatedHasReasoningContent = hasReasoningContent;
  if (delta != null &&
      (delta['reasoning_content'] != null ||
          delta['reasoning'] != null ||
          delta['thinking'] != null)) {
    updatedHasReasoningContent = true;
  }

  if (delta == null || delta['content'] == null) {
    return ReasoningDetectionResult(
      isReasoningJustDone: false,
      hasReasoningContent: updatedHasReasoningContent,
      updatedLastChunk: lastChunk,
    );
  }

  final deltaContent = delta['content'] as String;

  // 检查当前chunk和上一个chunk的组合是否形成###Response标记
  final combinedChunks = lastChunk + deltaContent;
  final updatedLastChunk = deltaContent;

  // 检测思考结束
  if (combinedChunks.contains('###Response') || deltaContent == '</think>') {
    return ReasoningDetectionResult(
      isReasoningJustDone: true,
      hasReasoningContent: hasReasoningContent,
      updatedLastChunk: updatedLastChunk,
    );
  }

  // 如果之前有reasoning_content或reasoning,现在有普通content,说明思考结束
  if (hasReasoningContent && deltaContent.isNotEmpty) {
    return ReasoningDetectionResult(
      isReasoningJustDone: true,
      hasReasoningContent: updatedHasReasoningContent,
      updatedLastChunk: updatedLastChunk,
    );
  }

  return ReasoningDetectionResult(
    isReasoningJustDone: false,
    hasReasoningContent: updatedHasReasoningContent,
    updatedLastChunk: updatedLastChunk,
  );
}