apply method

bool apply(
  1. AgentMessage message, {
  2. String? path,
})

Implementation

bool apply(AgentMessage message, {String? path}) {
  if (message is! AgentThreadMessage || message.threadId.trim().isEmpty) {
    return false;
  }
  final normalizedThreadPath = message.threadId.trim();

  switch (message.type) {
    case agentThreadStatusType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is AgentThreadStatus && _applyStatus(normalizedThreadPath, message);
    case agentTurnStartType:
    case agentTurnSteerType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return _applyTurnInput(normalizedThreadPath, message);
    case agentTurnStartAcceptedType:
    case agentTurnSteerAcceptedType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is TurnStartAccepted && _applyAccepted(normalizedThreadPath, message);
    case agentTurnStartedType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is TurnStarted && _markPendingApplied(normalizedThreadPath, message.sourceMessageId, turnId: message.turnId);
    case agentTurnSteeredType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is TurnSteered && _markPendingApplied(normalizedThreadPath, message.sourceMessageId);
    case agentToolCallArgumentsDeltaType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is AgentToolCallArgumentsDelta && _applyToolCallArgumentsDelta(normalizedThreadPath, message);
    case agentToolCallPendingType:
    case agentToolCallInProgressType:
    case agentToolCallStartedType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is AgentToolCallPending && _applyToolCallLifecycle(normalizedThreadPath, message);
    case agentToolCallEndedType:
      _touchedThreadPaths.add(normalizedThreadPath);
      return message is AgentToolCallEnded && _clearToolCallBytes(normalizedThreadPath, message);
    case agentTurnStartRejectedType:
    case agentTurnSteerRejectedType:
      _touchedThreadPaths.add(normalizedThreadPath);
      final sourceMessageId = switch (message) {
        TurnStartRejected() => message.sourceMessageId,
        TurnSteerRejected() => message.sourceMessageId,
        _ => null,
      };
      return sourceMessageId != null && _removePending(normalizedThreadPath, sourceMessageId);
    case agentTurnEndedType:
    case agentThreadClearedType:
      _touchedThreadPaths.add(normalizedThreadPath);
      final hadPending = _pendingMessagesByThreadPath.remove(normalizedThreadPath)?.isNotEmpty == true;
      final hadStatus = _statusByThreadPath.remove(normalizedThreadPath) != null;
      final hadTools = _toolCallAccumulatorsByThreadPath.remove(normalizedThreadPath)?.isEmpty == false;
      return hadPending || hadStatus || hadTools;
  }

  return false;
}