🤯 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 { GROUP_CHAT_TOPIC_URL, GROUP_CHAT_URL } from '@lobechat/const';
import { type NewChatGroup } from '@lobechat/types';
import { chatGroupService } from '@/services/chatGroup';
import { useChatStore } from '@/store/chat';
import { getHomeStoreState } from '@/store/home';
import { type ChatGroupStore } from '../store';
type ChatGroupStoreWithSwitchTopic = ChatGroupStore & {
switchTopic: (topicId?: string | null) => void;
};
export class ChatGroupLifecycleAction {
readonly #get: () => ChatGroupStoreWithSwitchTopic;
constructor(_set: unknown, get: () => ChatGroupStoreWithSwitchTopic, _api?: unknown) {
// keep signature aligned with StateCreator params: (set, get, api)
void _set;
void _api;
this.#get = get;
}
createGroup = async (
newGroup: Omit<NewChatGroup, 'userId'>,
agentIds?: string[],
silent: any = false,
) => {
const { switchToGroup, refreshAgentList } = getHomeStoreState();
const { group } = await chatGroupService.createGroup(newGroup);
if (agentIds && agentIds.length > 0) {
await chatGroupService.addAgentsToGroup(group.id, agentIds);
// Wait a brief moment to ensure database transactions are committed
// This prevents race condition where loadGroups() executes before member addition is fully persisted
await new Promise<void>((resolve) => {
setTimeout(resolve, 100);
});
}
this.#get().internal_addGroup(group);
// Fetch full group detail to get supervisorAgentId and agents for tools injection
await this.#get().internal_fetchGroupDetail(group.id);
refreshAgentList();
if (!silent) {
switchToGroup(group.id);
}
return group.id;
};
/**
* Switch to a new topic in the group
* Clears activeTopicId and navigates to group root
*/
switchToNewTopic = () => {
this.#get().switchTopic(undefined);
};
/**
* Switch to a topic in the group with proper route handling
* @param topicId - Topic ID to switch to, or undefined/null for new topic
*/
switchTopic = (topicId?: string | null) => {
const { activeGroupId, router } = this.#get();
if (!activeGroupId || !router) return;
// Update chat store's activeTopicId
useChatStore.getState().switchTopic(topicId ?? undefined);
// Navigate to the topic path (or group root for a new topic), aligning with
// the agent route's `/agent/:aid/:topicId` scheme. Replace to avoid stacking
// history entries on rapid topic switches.
const target = topicId
? GROUP_CHAT_TOPIC_URL(activeGroupId, topicId)
: GROUP_CHAT_URL(activeGroupId);
router.push(target, { replace: true });
};
}