🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { HelperMaps, Message, MessageGroupMetadata, ThreadScope } from './types';
interface CompressedDisplayNode {
children?: CompressedDisplayNode[];
compressedMessages?: CompressedDisplayNode[];
id?: string;
lastMessageId?: string;
tools?: Array<{ result_msg_id?: string }>;
}
const getLastMessageIdFromCompressedDisplay = (
message: CompressedDisplayNode | undefined,
): string | undefined => {
if (!message) return;
if (message.lastMessageId) return message.lastMessageId;
const compressedMessages = message.compressedMessages;
if (compressedMessages && compressedMessages.length > 0) {
return getLastMessageIdFromCompressedDisplay(compressedMessages.at(-1));
}
if (message.children && message.children.length > 0) {
return getLastMessageIdFromCompressedDisplay(message.children.at(-1));
}
if (message.tools && message.tools.length > 0) {
return message.tools.at(-1)?.result_msg_id ?? message.id;
}
return message.id;
};
/**
* Resolve the flat-list thread scope. An explicit `threadId` (including `null`) wins; without
* one, mixed input is scoped to the main flow and thread-only input keeps every message.
*/
export const resolveThreadScope = (messages: Message[], threadId?: string | null): ThreadScope => {
if (threadId !== undefined) return threadId || null;
return messages.some((message) => !message.threadId) ? null : undefined;
};
/** Whether `message` belongs to the flat list under `scope`. */
export const isInThreadScope = (message: Message | undefined, scope: ThreadScope): boolean => {
if (!message) return false;
if (scope === undefined || !message.threadId) return true;
return message.threadId === scope;
};
/**
* Phase 1: Indexing
* Builds helper maps for efficient querying during parsing
*
* @param messages - Flat array of messages from backend
* @param messageGroups - Optional array of message group metadata
* @returns Helper maps for efficient access
*/
export function buildHelperMaps(
messages: Message[],
messageGroups?: MessageGroupMetadata[],
threadId?: string | null,
): HelperMaps {
const messageMap = new Map<string, Message>();
const childrenMap = new Map<string | null, string[]>();
const threadMap = new Map<string, Message[]>();
const messageGroupMap = new Map<string, MessageGroupMetadata>();
const messageIds = new Set(messages.map((message) => message.id));
// Build a map of lastMessageId -> compressedGroup.id for parent redirection
// This handles the case where messages are created after compression:
// - The new message's parentId points to the last compressed message (lastMessageId)
// - But the lastMessageId is hidden inside the compressedGroup
// - We need to redirect children of lastMessageId to be children of compressedGroup
const lastMessageIdToGroupId = new Map<string, string>();
for (const message of messages) {
if (message.role === 'compressedGroup') {
const lastMessageId = getLastMessageIdFromCompressedDisplay(message);
if (lastMessageId) {
lastMessageIdToGroupId.set(lastMessageId, message.id);
}
}
}
// Single pass through messages to build all maps
for (const message of messages) {
// 1. Build messageMap for O(1) access
messageMap.set(message.id, message);
// 2. Build childrenMap for parent -> children lookup
let parentId = message.parentId ?? null;
// Redirect parentId if it points to a compressed message's lastMessageId
// This ensures messages created after compression become children of the compressedGroup
if (parentId && lastMessageIdToGroupId.has(parentId)) {
parentId = lastMessageIdToGroupId.get(parentId)!;
} else if (parentId && !messageIds.has(parentId)) {
// Orphan fallback:
// If parent is not present in the current queried message set,
// treat this message as a root so post-compression follow-up chains
// remain visible instead of being dropped entirely.
parentId = null;
}
const siblings = childrenMap.get(parentId);
if (siblings) {
siblings.push(message.id);
} else {
childrenMap.set(parentId, [message.id]);
}
// 3. Build threadMap for thread aggregation
if (message.threadId) {
const threadMessages = threadMap.get(message.threadId);
if (threadMessages) {
threadMessages.push(message);
} else {
threadMap.set(message.threadId, [message]);
}
}
}
// 4. Build messageGroupMap from provided metadata
if (messageGroups) {
for (const group of messageGroups) {
messageGroupMap.set(group.id, group);
}
}
return {
childrenMap,
messageGroupMap,
messageMap,
threadMap,
threadScope: resolveThreadScope(messages, threadId),
};
}