Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

7.7 KB
Raw Blame
import { useChatStore } from '@/store/chat';
import { threadSelectors } from '@/store/chat/selectors';
import { getForwardableMessages } from '@/store/chat/slices/forward/helpers';

import { type State } from '../../initialState';
import { indexDisplayMessages } from '../data/messageIndex';
import { dataSelectors } from '../data/selectors';

/**
 * Check if a message is currently collapsed (from message metadata)
 */
const isMessageCollapsed = (id: string) => (s: State) => {
  const message = s.dbMessages.find((m) => m.id === id);
  return message?.metadata?.collapsed ?? false;
};

/**
 * Check if a message is currently being edited
 */
const isMessageEditing = (id: string) => (s: State) => s.messageEditingIds.includes(id);

/**
 * Check if a message is currently loading
 */
const isMessageLoading = (id: string) => (s: State) => s.messageLoadingIds.includes(id);

/**
 * Get all message IDs currently being edited
 */
const messageEditingIds = (s: State) => s.messageEditingIds;

/**
 * Get all message IDs currently loading
 */
const messageLoadingIds = (s: State) => s.messageLoadingIds;

// ===== Multi-select selectors =====

/**
 * Whether the conversation is in multi-select mode
 */
const isSelectionMode = (s: State) => s.selectionMode;

/**
 * Whether a given message is checked in multi-select mode
 */
const isMessageSelected = (id: string) => (s: State) => s.selectedMessageIds.includes(id);

/**
 * Number of checked messages
 */
const selectedMessageCount = (s: State) => s.selectedMessageIds.length;

// ===== Operation-based selectors (read from external operationState) =====
// Note: These selectors read from operationState which is passed externally from ChatStore.
// This ensures proper React reactivity while keeping operations global.

/**
 * Check if AI is generating in the current conversation context
 */
const isAIGenerating = (s: State) => s.operationState.isAIGenerating;

/**
 * Check if input actions should stay blocked until operation bookkeeping ends.
 */
const isInputLoading = (s: State) => s.operationState.isInputLoading;

/**
 * Check if input should show visible loading controls.
 */
const isInputVisiblyLoading = (s: State) => s.operationState.isInputVisiblyLoading;

/**
 * Get send message error for this context (if any)
 */
const sendMessageError = (s: State) => s.operationState.sendMessageError;

/**
 * Check if a message is being created (sendMessage or createAssistantMessage)
 */
const isMessageCreating = (id: string) => (s: State) =>
  s.operationState.getMessageOperationState(id).isCreating;

/**
 * Check if a message is being generated (streaming response)
 */
const isMessageGenerating = (id: string) => (s: State) =>
  s.operationState.getMessageOperationState(id).isGenerating;

/**
 * Check if an AssistantGroup root or child block is generating.
 * A group is generating when itself or any child block has a running generation operation.
 * A child block is generating when itself or its parent group has a running generation operation.
 */
const isAssistantGroupItemGenerating = (id: string) => (s: State) => {
  if (isMessageGenerating(id)(s)) return true;

  const { byId, groupOfBlock } = indexDisplayMessages(s.displayMessages);
  const message = byId.get(id);
  if (message?.role === 'assistantGroup') {
    return message.children?.some((block) => isMessageGenerating(block.id)(s)) ?? false;
  }

  const parentMessage = groupOfBlock.get(id);
  return parentMessage ? isMessageGenerating(parentMessage.id)(s) : false;
};

const isRowGenerating = (id: string) => (s: State) =>
  dataSelectors
    .rowMemberIds(id)(s)
    .some((memberId) => isAssistantGroupItemGenerating(memberId)(s));

const selectedMemberMessageIds = (s: State) =>
  s.selectedMessageIds.flatMap((id) => dataSelectors.rowMemberIds(id)(s));

const selectedDeletableMessageIds = (s: State) => [
  ...new Set(s.selectedMessageIds.flatMap((id) => dataSelectors.deletableRowMessageIds(id)(s))),
];

const forwardableSelectedMessages = (s: State) => {
  const selected = new Set(selectedMemberMessageIds(s));
  return getForwardableMessages(s.displayMessages.filter((m) => selected.has(m.id)));
};

/**
 * Check if a message is being regenerated
 */
const isMessageRegenerating = (id: string) => (s: State) =>
  s.operationState.getMessageOperationState(id).isRegenerating;

/**
 * Check if a message is continuing generation
 */
const isMessageContinuing = (id: string) => (s: State) =>
  s.operationState.getMessageOperationState(id).isContinuing;

/**
 * Check if a message generation was interrupted by user: its operation was
 * cancelled in this session, or the server persisted the stopped marker (which
 * is what survives a refresh, on partial output and on a bare placeholder).
 */
const isMessageInterrupted = (id: string) => (s: State) => {
  const { isGenerating, isInterrupted } = s.operationState.getMessageOperationState(id);
  if (isInterrupted || isGenerating) return isInterrupted;

  const message = dataSelectors.getDbMessageById(id)(s);
  return !!message?.metadata?.interruptedMidStream;
};

/**
 * Check if a message is in reasoning state
 */
const isMessageInReasoning = (id: string) => (s: State) =>
  s.operationState.getMessageOperationState(id).isInReasoning;

/**
 * Check if a message is processing (any operation)
 */
const isMessageProcessing = (id: string) => (s: State) =>
  s.operationState.getMessageOperationState(id).isProcessing;

// ===== Tool-related selectors (read from external operationState) =====

/**
 * Check if plugin API is currently invoking
 */
const isPluginApiInvoking = (id: string) => (s: State) =>
  s.operationState.getToolOperationState(id, 0).isInvoking;

/**
 * Check if a tool call is currently streaming
 */
const isToolCallStreaming = (id: string, index: number) => (s: State) =>
  s.operationState.getToolOperationState(id, index).isStreaming;

/**
 * Check if tool API name should be shining (streaming or invoking)
 */
const isToolApiNameShining =
  (messageId: string, index: number, toolCallId: string) => (s: State) => {
    const toolMessage = dataSelectors.getDbMessageByToolCallId(toolCallId)(s);
    const toolMessageId = toolMessage?.id;
    const toolState = s.operationState.getToolOperationState(messageId, index, toolCallId);
    const isStreaming = toolState.isStreaming;
    const isInvoking = !toolMessageId
      ? true
      : s.operationState.getToolOperationState(toolMessageId, 0).isInvoking;

    return isStreaming || isInvoking;
  };

// ===== Thread-related selectors (still bridge from ChatStore) =====
// Note: Thread state is global and not part of operation state.

/**
 * Check if a message has a thread by source message ID
 * This is a bridge selector that reads from global ChatStore
 */

const hasThreadBySourceMsgId = (id: string) => (_s: State) => {
  const chatState = useChatStore.getState();
  return threadSelectors.hasThreadBySourceMsgId(id)(chatState);
};

/**
 * Check if we are in thread mode (has active thread ID)
 * This is a bridge selector that reads from global ChatStore
 */

const isThreadMode = (_s: State) => {
  const chatState = useChatStore.getState();
  return !!chatState.activeThreadId;
};

export const messageStateSelectors = {
  forwardableSelectedMessages,
  hasThreadBySourceMsgId,
  isAIGenerating,
  isAssistantGroupItemGenerating,
  isInputLoading,
  isInputVisiblyLoading,
  isMessageCollapsed,
  isMessageContinuing,
  isMessageCreating,
  isMessageEditing,
  isMessageGenerating,
  isMessageInReasoning,
  isMessageInterrupted,
  isMessageLoading,
  isMessageProcessing,
  isMessageRegenerating,
  isMessageSelected,
  isPluginApiInvoking,
  isRowGenerating,
  isSelectionMode,
  isThreadMode,
  isToolApiNameShining,
  isToolCallStreaming,
  messageEditingIds,
  messageLoadingIds,
  selectedDeletableMessageIds,
  selectedMessageCount,
  sendMessageError,
};