isResultSuccessful function

bool isResultSuccessful(
  1. Map<String, dynamic>? message, {
  2. String? stopReason,
})

Checks if the result should be considered successful based on the last message.

Implementation

bool isResultSuccessful(Map<String, dynamic>? message, {String? stopReason}) {
  if (message == null) return false;

  final type = message['type'] as String?;

  if (type == 'assistant') {
    final content = message['message']?['content'];
    if (content is List && content.isNotEmpty) {
      final lastContent = content.last;
      if (lastContent is Map<String, dynamic>) {
        final blockType = lastContent['type'] as String?;
        return blockType == 'text' ||
            blockType == 'thinking' ||
            blockType == 'redacted_thinking';
      }
    }
  }

  if (type == 'user') {
    final content = message['message']?['content'];
    if (content is List &&
        content.isNotEmpty &&
        content.every(
          (block) =>
              block is Map<String, dynamic> && block['type'] == 'tool_result',
        )) {
      return true;
    }
  }

  // API completed but yielded no assistant content.
  return stopReason == 'end_turn';
}