Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

2.6 KB
Raw Blame
/**
 * Client-minted ids for optimistic (not-yet-persisted) chat rows.
 *
 * Optimistic rows carry a NEGATIVE id until the turn's ``done`` event reports
 * the persisted ones; ``lib/message-branches`` relies on that sign (a more
 * negative id ranks as the newer sibling) and ``lib/turn-reconcile`` keys its
 * optimistic→persisted remap by it.
 *
 * Keying by id is why plain ``-Date.now()`` was not enough: the user row and
 * the assistant placeholder are dispatched back-to-back (``ADD_USER_MSG``
 * then ``STREAM_START``), so they routinely landed in the same millisecond
 * and shared one id. The remap then collapsed both rows onto a single
 * persisted id, which pinned the next turn's ``parent_message_id`` to the
 * previous USER message and dropped the assistant reply out of the visible
 * path until a session reload (issue #698).
 *
 * So: keep the timestamp shape, but never return the same value twice —
 * every call is strictly smaller than the last.
 */

let lastOptimisticId = 0;

export function nextOptimisticId(): number {
  const stamp = -Date.now();
  lastOptimisticId = stamp < lastOptimisticId ? stamp : lastOptimisticId - 1;
  return lastOptimisticId;
}

interface IdentifiedMessage {
  id?: number;
  role: string;
  failedSubmissionId?: string;
  metadata?: Record<string, unknown>;
}

/**
 * Resolve a message to its persisted row before a server-side mutation.
 * The refresh result is used directly because React state refs are only
 * updated after the reducer commit and can still contain the optimistic id.
 */
export async function resolvePersistedMessage<T extends IdentifiedMessage>(
  messages: readonly T[],
  messageId: number,
  expectedRole: string,
  refreshMessages: () => Promise<readonly T[] | undefined>,
): Promise<T | undefined> {
  const index = messages.findIndex(
    (message) => message.id === messageId && message.role === expectedRole,
  );
  if (index === -1) return undefined;

  const original = messages[index];
  if (typeof original.id === "number" && original.id >= 0) return original;

  const submissionId = original.failedSubmissionId ?? original.metadata?.client_submission_id;
  if (typeof submissionId !== "string" || !submissionId) return undefined;
  const refreshed = await refreshMessages();
  const candidates = refreshed?.filter((message) => message.role === expectedRole &&
    (message.metadata?.client_submission_id ?? message.failedSubmissionId) === submissionId);
  const candidate = candidates?.length === 1 ? candidates[0] : undefined;
  if (
    !candidate ||
    candidate.role !== expectedRole ||
    typeof candidate.id !== "number" ||
    candidate.id < 0
  ) {
    return undefined;
  }
  return candidate;
}