private void checkLastResponseAndEditHistory() {
    getCurrentResponse()
        .ifPresent(
            currentResponse -> {
              setCurrentResponse(Optional.empty());
              checkFinishReasonAndEditHistory(currentResponse);
              history.add(getContent(currentResponse));
              // If `checkFinishReasonAndEditHistory` passes, we add 2 contents(user's message and
              // model's response) to the history and then one round of conversation is finished. So
              // we add 2 to the previousHistorySize.
              setPreviousHistorySize(getPreviousHistorySize() + 2);
            });
    getCurrentResponseStream()
        .ifPresent(
            responseStream -> {
              if (!responseStream.isConsumed()) {
                throw new IllegalStateException("Response stream is not consumed");
              } else {
                setCurrentResponseStream(Optional.empty());
                GenerateContentResponse response = aggregateStreamIntoResponse(responseStream);
                checkFinishReasonAndEditHistory(response);
                history.add(getContent(response));
                // If `checkFinishReasonAndEditHistory` passes, we add 2 contents(user's message and
                // model's response) to the history and then one round of conversation is finished.
                // So we add 2 to the previousHistorySize.
                setPreviousHistorySize(getPreviousHistorySize() + 2);
              }
            });
  }
Checks whether the last response is available and edit the history if necessary.

@throws IllegalStateException if the response stream is not finished.
java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java