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.

4.2 KB
Raw Blame
import { Message } from 'chat';

import type { DeferredBotMessage } from './deferredMessages';

/**
 * Field stamped on a merged message so downstream consumers (notably
 * `AgentBridgeService.resolveFiles`) can reach every original message's
 * `raw`, not just the last one's. Per-platform `extractFiles` re-downloads
 * media from `raw`, so without this an image that arrived one message before
 * the text is lost when the two are merged into a single turn.
 *
 * The field is in-process only: the Chat SDK's `Message.toJSON` drops it.
 * Replay must enqueue original messages separately and merge only after the
 * SDK has restored them from its queue.
 */
export const SOURCE_MESSAGES_FIELD = 'sourceMessages';

type MessageWithSources = Message & { [SOURCE_MESSAGES_FIELD]?: Message[] };

/** Earlier messages may contribute content and triggers only for the same sender. */
export function getSameSenderMessages(message: Message, earlier?: Message[]): Message[] {
  const senderId = message.author?.userId;
  if (!senderId) return [];

  return (earlier ?? []).filter(
    (source) =>
      source.author?.userId === senderId &&
      source.author?.isBot === message.author.isBot &&
      source.author?.isMe === message.author.isMe,
  );
}

/**
 * Merge earlier messages (chronological) into `message`, producing a single
 * message with combined text + attachments and the full source list. Returns
 * `message` untouched when there is nothing to merge.
 */
export function mergeBotMessages(message: Message, earlier: Message[] | undefined): Message {
  const sameSender = getSameSenderMessages(message, earlier);
  if (sameSender.length === 0) return message;

  const allMessages = [...sameSender, message].flatMap(getSourceMessages);
  const mergedText = allMessages
    .map((m) => m.text)
    .filter(Boolean)
    .join('\n');
  const mergedAttachments = allMessages.flatMap((m) => (m as any).attachments || []);

  return Object.assign(Object.create(Object.getPrototypeOf(message)), message, {
    attachments: mergedAttachments,
    [SOURCE_MESSAGES_FIELD]: allMessages,
    text: mergedText,
  });
}

/**
 * Every original message behind `message`: the merged sources when it was
 * produced by {@link mergeBotMessages}, otherwise the message itself.
 */
export function getSourceMessages(message: Message): Message[] {
  const sources = (message as MessageWithSources)[SOURCE_MESSAGES_FIELD];
  return sources?.length ? sources : [message];
}

/** What {@link buildReplayMessages} appends to a replay's id. */
const REPLAY_ID_SUFFIX = /:replay:\d+:\d+$/;

/**
 * Every replay suffix on an id. A replay that is parked again used to keep its replay id, so the
 * next replay stacked another suffix on it (`<id>:replay:1:0:replay:2:1`); those are still in the
 * deferred-message store until they expire.
 */
const REPLAY_ID_SUFFIXES = /(?::replay:\d+:\d+)+$/;

/**
 * The id a message carried before {@link buildReplayMessages} rewrote it.
 *
 * A replay gets a fresh id so the Chat SDK does not drop it as a duplicate, but the rest of the
 * pipeline keys on the platform id — the wake scheduler releases a batch by the ids it parked, so
 * it has to match the original id rather than the replay one.
 */
export const getOriginMessageId = (message: Pick<Message, 'id'>): string =>
  message.id.replace(REPLAY_ID_SUFFIXES, '');

/** Whether {@link buildReplayMessages} produced this message from a parked one. */
export const isReplayMessage = (message: Pick<Message, 'id'>): boolean =>
  REPLAY_ID_SUFFIX.test(message.id);

/**
 * Rebuild original messages from deferred payloads (oldest first).
 *
 * The replayed message gets a fresh id: the Chat SDK dedupes inbound messages
 * by `<adapter>:<id>` for 10 minutes, and the originals already went through
 * `processMessage` once when they were deferred. A message parked again by its own replay keeps
 * its replay id, so the fresh id is built from the origin id: suffixes never stack, and the id
 * still matches the one the wake scheduler released.
 */
export function buildReplayMessages(entries: DeferredBotMessage[]): Message[] {
  const now = Date.now();
  return entries.map((entry, index) => {
    return Message.fromJSON({
      ...entry,
      id: `${getOriginMessageId(entry)}:replay:${now}:${index}`,
    });
  });
}