π€― 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 { ChannelListItem, GroupedTopic, ShareVisitorConversation } from '@lobechat/types';
import {
getTopicWorkingDirectoryEffectivePath,
getTopicWorkingDirectorySourcePath,
} from '@lobechat/utils/client/topic';
import dayjs from 'dayjs';
import { TopicTrigger } from '@/const/topic';
import type { ChatTopic } from '@/types/topic';
import type {
BotChannelFilter,
BotChannelOption,
SortBy,
StatusFilter,
TimeRangeFilter,
TriggerFilter,
} from './types';
const DAY_MS = 24 * 60 * 60 * 1000;
export const matchesStatus = (topic: ChatTopic, status: StatusFilter): boolean => {
switch (status) {
case 'all': {
return true;
}
case 'archived': {
return topic.status === 'archived';
}
case 'completed': {
return topic.status === 'completed';
}
case 'running': {
return topic.status === 'running';
}
case 'active': {
return !topic.status || topic.status === 'active';
}
default: {
return true;
}
}
};
/**
* True when every selected topic is already archived (`completed` in the UI),
* so the bulk bar should offer "Unarchive" instead of "Archive".
*/
export const areAllTopicsArchived = (topics: ChatTopic[], ids: string[]): boolean => {
if (ids.length === 0) return false;
const statusById = new Map(topics.map((t) => [t.id, t.status]));
return ids.every((id) => {
const status = statusById.get(id);
return status === 'completed' || status === 'archived';
});
};
export const matchesGroup = (topic: ChatTopic, groupIds: string[]): boolean => {
if (groupIds.length === 0) return true;
// ChatTopic doesn't surface groupId on the client type; fall back to the
// working-directory source path as the project bucket key (matches sidebar
// ByProjectMode). A selected git worktree still belongs to its source repo.
const project = getTopicWorkingDirectorySourcePath(topic) ?? '';
return groupIds.includes(project);
};
export const matchesTrigger = (topic: ChatTopic, triggers: TriggerFilter[]): boolean => {
// Search can hydrate older cached results before the request revalidates.
if (topic.trigger === TopicTrigger.Channel) return false;
if (triggers.length === 0) return true;
const effective: TriggerFilter = (topic.trigger as TriggerFilter | null | undefined) ?? 'chat';
return triggers.includes(effective);
};
export const matchesBotChannel = (topic: ChatTopic, botChannels: BotChannelFilter[]): boolean => {
if (botChannels.length === 0) return true;
const platform = topic.metadata?.bot?.platform;
return !!platform && botChannels.includes(platform);
};
export const matchesTimeRange = (
item: { updatedAt?: Date | number | string | null },
range: TimeRangeFilter,
): boolean => {
if (range === 'all') return true;
const updated = item.updatedAt ? new Date(item.updatedAt).getTime() : 0;
if (!updated) return false;
const now = Date.now();
const diff = now - updated;
switch (range) {
case 'today': {
return diff < DAY_MS;
}
case 'week': {
return diff < 7 * DAY_MS;
}
case 'month': {
return diff < 30 * DAY_MS;
}
default: {
return true;
}
}
};
/**
* A Channel's status as the topic status tabs read it: archived Channels sit under the
* Archived (`completed`) tab, and one with a reply under way also counts as running.
*/
export const matchesChannelStatus = (channel: ChannelListItem, status: StatusFilter): boolean => {
switch (status) {
case 'all': {
return true;
}
case 'archived':
case 'completed': {
return channel.status === 'archived';
}
case 'running': {
return (
channel.status !== 'archived' &&
!!channel.replyJobs?.some((job) => job.status === 'running')
);
}
default: {
return channel.status !== 'archived';
}
}
};
/**
* Group Channels the Agent is an active member of. Its run topics there stay hidden from
* topic lists (they carry other members' messages), so the page lists the Channels instead.
* Direct conversations open from the Agent itself and are left out.
*/
export const agentGroupChannels = (
channels: ChannelListItem[] | undefined,
agentId: string,
): ChannelListItem[] =>
channels?.filter((channel) => !channel.agentId && !!channel.agentIds?.includes(agentId)) ?? [];
export const matchesChannelSearch = (channel: ChannelListItem, search: string): boolean => {
const keyword = search.trim().toLowerCase();
return !keyword || channel.title.toLowerCase().includes(keyword);
};
export const sortChannelsBy = (channels: ChannelListItem[], sortBy: SortBy): ChannelListItem[] => {
const time = (value: Date | string | undefined) => (value ? new Date(value).getTime() : 0);
return [...channels].sort((a, b) => {
if (sortBy === 'title') return a.title.localeCompare(b.title);
if (sortBy === 'createdAt') return time(b.createdAt) - time(a.createdAt);
return time(b.updatedAt ?? b.createdAt) - time(a.updatedAt ?? a.createdAt);
});
};
/**
* A visitor's conversation under the topic status tabs: it is never archived or completed and
* reports no run, so it counts as active.
*/
export const matchesShareVisitorStatus = (status: StatusFilter): boolean =>
status === 'all' || status === 'active';
/** Search matches the visitor's name or the conversation's latest message. */
export const matchesShareVisitorSearch = (
conversation: ShareVisitorConversation,
search: string,
): boolean => {
const keyword = search.trim().toLowerCase();
if (!keyword) return true;
return [conversation.visitor.name, conversation.lastMessage?.content].some((text) =>
text?.toLowerCase().includes(keyword),
);
};
export const sortShareVisitorsBy = (
conversations: ShareVisitorConversation[],
sortBy: SortBy,
): ShareVisitorConversation[] => {
const time = (value: Date | string) => new Date(value).getTime();
return [...conversations].sort((a, b) => {
if (sortBy === 'title') return (a.visitor.name ?? '').localeCompare(b.visitor.name ?? '');
if (sortBy === 'createdAt') return time(b.createdAt) - time(a.createdAt);
return time(b.updatedAt) - time(a.updatedAt);
});
};
export const sortTopics = (topics: ChatTopic[], sortBy: SortBy): ChatTopic[] => {
const sorted = [...topics];
switch (sortBy) {
case 'updatedAt': {
sorted.sort(
(a, b) => new Date(b.updatedAt ?? 0).getTime() - new Date(a.updatedAt ?? 0).getTime(),
);
break;
}
case 'createdAt': {
sorted.sort(
(a, b) => new Date(b.createdAt ?? 0).getTime() - new Date(a.createdAt ?? 0).getTime(),
);
break;
}
case 'title': {
sorted.sort((a, b) => (a.title ?? '').localeCompare(b.title ?? ''));
break;
}
}
return sorted;
};
const getPathName = (path: string): string => path.split(/[/\\]+/).findLast(Boolean) ?? path;
export const getProjectFilterLabel = (topic: ChatTopic): string | undefined => {
const source = getTopicWorkingDirectorySourcePath(topic);
return source ? getPathName(source) : undefined;
};
export interface ProjectInfo {
branch?: string;
path: string;
}
export const getProjectInfo = (topic: ChatTopic): ProjectInfo | undefined => {
const source = getTopicWorkingDirectorySourcePath(topic);
const effective = getTopicWorkingDirectoryEffectivePath(topic);
const labelPath = effective ?? source;
if (!labelPath) return undefined;
const label = getPathName(labelPath);
const sourceName = source ? getPathName(source) : undefined;
const path = sourceName && sourceName !== label ? `${sourceName}/${label}` : label;
const branch = topic.metadata?.workingDirectoryConfig?.git?.branch || undefined;
return { branch, path };
};
export const getProjectLabel = (topic: ChatTopic): string | undefined => {
const info = getProjectInfo(topic);
if (!info) return undefined;
return info.branch ? `${info.path} Β· ${info.branch}` : info.path;
};
/** Client-side platform display names (server `PlatformDefinition.name`). */
const PLATFORM_DISPLAY_NAMES: Record<string, string> = {
dingtalk: 'DingTalk',
discord: 'Discord',
feishu: 'Feishu',
googlechat: 'Google Chat',
imessage: 'iMessage',
lark: 'Lark',
line: 'Line',
msteams: 'Microsoft Teams',
qq: 'QQ',
slack: 'Slack',
telegram: 'Telegram',
wechat: 'WeChat',
whatsapp: 'WhatsApp',
};
/** Human-readable bot (platform) name, e.g. `discord` β `Discord`. */
export const getBotPlatformName = (platform: string): string =>
PLATFORM_DISPLAY_NAMES[platform.toLowerCase()] ?? platform;
/**
* Derive the flattened bot-source option list from a topic set β one entry
* per platform that actually owns topics (per review: only distinguish
* tg/discord level, no per-channel nesting).
*/
export const buildBotChannelOptions = (topics: ChatTopic[]): BotChannelOption[] => {
const seen = new Set<string>();
for (const topic of topics) {
const platform = topic.metadata?.bot?.platform;
if (platform) seen.add(platform);
}
return Array.from(seen).map((platform) => ({
key: platform,
label: getBotPlatformName(platform),
}));
};
/**
* Resolve the human-readable title for a time-bucket group ID produced by
* `groupTopicsByUpdatedTime` (today / yesterday / week / month) or a dynamic
* year-month / year segment (e.g. `2025-04`, `2024`).
*/
// Accept any react-i18next TFunction shape β overloads make the strict type
// finicky and we only need string β string here.
type LooseT = (key: any) => any;
export const getTimeGroupTitle = (id: string, t: LooseT): string => {
// Year-month like "2025-04" β localized month name; year-only like "2025" β as-is.
if (/^\d{4}/.test(id)) {
return id.includes('-') ? dayjs(id).format('MMMM') : id;
}
return t(`groupTitle.byTime.${id}`);
};
/**
* Resolve the human-readable title for a project-bucket group produced by
* `groupTopicsByProject` (`project-id:{projectId}`, `project-directory:{id}`,
* `project:{device}:{path}` or `no-project`).
*
* Merged `project-id:` groups follow the most recently active directory, so
* the util already carries a path-derived title; when it doesn't (project
* topics without directory metadata), derive the label from the first child's
* source path β the same basename the project filter chips use.
*/
export const getProjectGroupTitle = (group: GroupedTopic, t: LooseT): string => {
if (group.id === 'no-project') return t('management.group.noProject');
if (group.title) return group.title;
const withPath = group.children.find((topic) => getTopicWorkingDirectorySourcePath(topic));
const label = withPath ? getProjectFilterLabel(withPath) : undefined;
if (label) return label;
return group.id.replace(/^(project-id|project-directory|project):/, '');
};
/**
* Whether the Channel list is still loading or failed. A failed first fetch leaves no list
* behind, so it must surface as an error instead of loading forever; a failed refresh keeps
* showing the list already loaded.
*/
export const channelListState = (
enabled: boolean,
channels: readonly unknown[] | undefined,
error: unknown,
): { error: unknown; loading: boolean } => {
if (!enabled || channels) return { error: undefined, loading: false };
return error ? { error, loading: false } : { error: undefined, loading: true };
};