findMatchingFunctionCall method

Event? findMatchingFunctionCall(
  1. Event functionResponseEvent
)

Implementation

Event? findMatchingFunctionCall(Event functionResponseEvent) {
  final List<FunctionResponse> responses = functionResponseEvent
      .getFunctionResponses();
  if (responses.isEmpty) {
    return null;
  }

  final String? functionCallId = responses.first.id;
  if (functionCallId == null) {
    return null;
  }

  final List<Event> invocationEvents = getEvents(currentInvocation: true);
  final int responseIndex = invocationEvents.lastIndexWhere(
    (Event event) => event.id == functionResponseEvent.id,
  );
  final int startIndex = responseIndex >= 0
      ? responseIndex - 1
      : invocationEvents.length - 1;

  for (int index = startIndex; index >= 0; index -= 1) {
    final Event event = invocationEvents[index];
    for (final call in event.getFunctionCalls()) {
      if (call.id == functionCallId) {
        return event;
      }
    }
  }

  return null;
}