isPausedInvocation function

bool isPausedInvocation(
  1. InvocationContext invocationContext
)

Returns whether invocationContext ended in a paused state awaiting long-running tool results, tool confirmation, or authentication.

Implementation

bool isPausedInvocation(InvocationContext invocationContext) {
  if (invocationContext.isAborted) {
    return false;
  }
  final Set<String> openInterrupts = <String>{};
  final Map<String, String> childToOriginalId = <String, String>{};
  final Map<String, Set<String>> originalToChildIds = <String, Set<String>>{};

  for (final Event event in invocationContext.session.events) {
    if (event.invocationId != invocationContext.invocationId) {
      continue;
    }

    if (event.longRunningToolIds != null) {
      openInterrupts.addAll(event.longRunningToolIds!);
    }
    openInterrupts.addAll(event.actions.requestedToolConfirmations.keys);
    openInterrupts.addAll(event.actions.requestedAuthConfigs.keys);

    for (final FunctionCall call in event.getFunctionCalls()) {
      final String? callId = call.id;
      if (callId == null || callId.isEmpty) {
        continue;
      }
      if (event.longRunningToolIds?.contains(callId) == true ||
          event.actions.requestedToolConfirmations.containsKey(callId) ||
          event.actions.requestedAuthConfigs.containsKey(callId)) {
        openInterrupts.add(callId);
      }
      final Object? originalCall = call.args['originalFunctionCall'];
      if (originalCall is Map && originalCall['id'] is String) {
        final String origId = originalCall['id'] as String;
        childToOriginalId[callId] = origId;
        originalToChildIds.putIfAbsent(origId, () => <String>{}).add(callId);
      }
      final Object? authCallId =
          call.args['function_call_id'] ?? call.args['functionCallId'];
      if (authCallId is String && authCallId.isNotEmpty) {
        childToOriginalId[callId] = authCallId;
        originalToChildIds
            .putIfAbsent(authCallId, () => <String>{})
            .add(callId);
      }
    }

    for (final FunctionResponse response in event.getFunctionResponses()) {
      final String? id = response.id;
      if (id == null || id.isEmpty) {
        continue;
      }
      final bool isReflectAndRetryFailure =
          response.response['response_type'] == reflectAndRetryResponseType;
      if (!isReflectAndRetryFailure &&
          (event.actions.requestedToolConfirmations.containsKey(id) ||
              event.actions.requestedAuthConfigs.containsKey(id))) {
        continue;
      }
      openInterrupts.remove(id);
      final String? origId = childToOriginalId[id];
      if (origId != null) {
        openInterrupts.remove(origId);
      }
      final Set<String>? children = originalToChildIds[id];
      if (children != null) {
        openInterrupts.removeAll(children);
      }
    }
  }

  return openInterrupts.isNotEmpty;
}