🤯 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 CompressionGroupMetadata,
type ConversationContext,
type UIChatMessage,
} from '@lobechat/types';
import { type Operation } from '../slices/operation/types';
export const isCompressionOperationType = (type?: string) =>
type === 'contextCompression' || type === 'generateSummary';
/**
* @param options.beforeMessageId Keep this message and everything after it out of the
* summary, e.g. the user turn a recovery card regenerates after compacting. Compressing it
* would hide it from the conversation, leaving nothing to regenerate.
*/
export const getCompressionCandidateMessageIds = (
messages: UIChatMessage[],
options?: { beforeMessageId?: string },
) => {
const keepFrom = options?.beforeMessageId
? messages.findIndex((message) => message.id === options.beforeMessageId)
: -1;
return (keepFrom === -1 ? messages : messages.slice(0, keepFrom))
.filter((message) => message.role !== 'compressedGroup')
.map((message) => message.id)
.filter(Boolean);
};
export const createPendingCompressedGroup = ({
agentId,
content = '...',
groupId,
id,
threadId,
topicId,
}: {
agentId: string;
content?: string;
groupId?: string | null;
id: string;
threadId?: string | null;
topicId?: string | null;
}): UIChatMessage => {
const now = Date.now();
const metadata: CompressionGroupMetadata = { expanded: true };
return {
agentId,
compressedMessages: [],
content,
createdAt: now,
groupId: groupId ?? undefined,
id,
metadata: metadata as UIChatMessage['metadata'],
role: 'compressedGroup',
threadId: threadId ?? undefined,
topicId: topicId ?? undefined,
updatedAt: now,
};
};
export const hasRunningCompressionOperation = (
operations: Operation[],
context: Pick<ConversationContext, 'agentId' | 'groupId' | 'threadId' | 'topicId'>,
) =>
operations.some((operation) => {
if (operation.status !== 'running' || !isCompressionOperationType(operation.type)) return false;
return (
operation.context.agentId === context.agentId &&
(operation.context.groupId ?? null) === (context.groupId ?? null) &&
(operation.context.threadId ?? null) === (context.threadId ?? null) &&
(operation.context.topicId ?? null) === (context.topicId ?? null)
);
});