getContents function

List<Content> getContents({
  1. required String? currentBranch,
  2. required List<Event> events,
  3. String agentName = '',
  4. bool preserveFunctionCallIds = false,
  5. String? isolationScope,
  6. bool isSingleTurn = false,
  7. Content? userContent,
  8. bool includeThoughtsFromOtherAgents = false,
  9. String? nodePath,
})

Builds context contents from events with compaction and branch handling.

Implementation

List<Content> getContents({
  required String? currentBranch,
  required List<Event> events,
  String agentName = '',
  bool preserveFunctionCallIds = false,
  String? isolationScope,
  bool isSingleTurn = false,
  Content? userContent,
  bool includeThoughtsFromOtherAgents = false,
  String? nodePath,
}) {
  final List<Event> rewindFiltered = _filterSingleTurnNodeEvents(
    currentBranch,
    _filterRewoundEvents(events),
    isSingleTurn: isSingleTurn,
    nodePath: nodePath,
    allowAncestorNodePaths: true,
  );
  final Set<String> toolCallIds = _collectFunctionCallIds(rewindFiltered);
  final List<Event> rawFiltered = rewindFiltered
      .where(
        (Event event) =>
            shouldIncludeEventInContext(
              currentBranch,
              event,
              isolationScope: isolationScope,
              includeThoughts:
                  includeThoughtsFromOtherAgents &&
                  _isOtherAgentReply(agentName, event),
            ) &&
            !isToolSubBranchEvent(
              currentBranch,
              event,
              agentName,
              toolCallIds,
            ),
      )
      .toList(growable: false);

  List<Event> eventsToProcess;
  if (_hasCompactionEvents(rawFiltered)) {
    eventsToProcess = _processCompactionEvents(
      rawFiltered,
      agentName: agentName,
    );
    // Compaction may have removed a function_call whose response survives
    // (e.g. a long-running call resumed after it was compacted); restore it so
    // the call/response pairing is intact.
    eventsToProcess = _recoverCompactedFunctionCalls(
      eventsToProcess,
      rawFiltered,
    );
  } else {
    eventsToProcess = rawFiltered;
  }

  // Build mapping of function call IDs to their authors.
  final Map<String, String> fcAuthorById = <String, String>{};
  for (final Event e in eventsToProcess) {
    final Content? content = e.content;
    if (content == null) {
      continue;
    }
    for (final Part part in content.parts) {
      final String? functionCallId = part.functionCall?.id;
      if (functionCallId != null && functionCallId.isNotEmpty) {
        fcAuthorById[functionCallId] = e.author;
      }
    }
  }

  eventsToProcess = _mergeTranscriptions(eventsToProcess);

  final List<Event> filtered = <Event>[];
  for (final Event event in eventsToProcess) {
    bool isOtherReply = _isOtherAgentReply(agentName, event);

    // Check if it's a FunctionResponse for another agent's call.
    if (!isOtherReply && event.content != null) {
      for (final Part part in event.content!.parts) {
        final FunctionResponse? fr = part.functionResponse;
        if (fr != null) {
          final String? respId = fr.id;
          final String? callAuthor = (respId != null && respId.isNotEmpty)
              ? fcAuthorById[respId]
              : null;
          if (callAuthor != null &&
              callAuthor != agentName &&
              callAuthor != 'user') {
            isOtherReply = true;
            break;
          }
        }
      }
    }

    if (isOtherReply) {
      final Event? converted = _presentOtherAgentMessage(
        event,
        includeThoughts: includeThoughtsFromOtherAgents,
      );
      if (converted != null) {
        filtered.add(converted);
      }
      continue;
    }
    filtered.add(event);
  }

  // Rearrange events for proper function call/response pairing.
  List<Event> resultEvents = _dropOrphanedFunctionResponses(filtered);
  resultEvents = _rearrangeEventsForLatestFunctionResponse(resultEvents);
  resultEvents = _rearrangeEventsForAsyncFunctionResponsesInHistory(
    resultEvents,
  );
  resultEvents = _dropOrphanedFunctionCalls(resultEvents);

  final List<Content> contents = <Content>[];
  for (final Event event in resultEvents) {
    final Content? content = event.content?.copyWith();
    if (content == null) {
      continue;
    }
    content.parts.removeWhere(isAdkLiveArtifactPart);
    if (content.parts.isEmpty) {
      continue;
    }
    if (!preserveFunctionCallIds) {
      removeClientFunctionCallId(content);
    }
    contents.add(content);
  }

  final Content? leading = buildTaskInputUserContent(
    events: events,
    isolationScope: isolationScope,
    isSingleTurn: isSingleTurn,
    userContent: userContent,
  );
  if (leading != null) {
    contents.insert(0, leading);
  }
  return contents;
}