π€― 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
'use client';
import { EditorProvider } from '@lobehub/editor/react';
import { Flexbox } from '@lobehub/ui';
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
import { useParams } from 'react-router';
import AsyncError from '@/components/AsyncError';
import { RouteLoading } from '@/components/Skeleton/RouteSegment';
import { type ComposerTarget, createComposerTarget } from '@/features/Conversation/types';
import { FileDocumentPreview } from '@/features/FileViewer/FileDocumentPreview';
import FloatingChatPanel from '@/features/FloatingChatPanel';
import { useDocumentChatTopic } from '@/features/FloatingChatPanel/useDocumentChatTopic';
import { PageEditor } from '@/features/PageEditor';
import RightPanel from '@/features/RightPanel';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
import { getDocumentRenderMode } from '@/utils/documentRenderMode';
import Header from './Header';
import { buildAgentDocumentsPath } from './navigation';
import { useAgentDocumentItem } from './useAgentDocumentItem';
interface AgentDocumentPageProps {
/** Full `documents` table id, e.g. `docs_MWkYMvbvzssoyWZ9`. */
documentId: string;
}
/**
* Standalone document view at `/agent/:aid/docs/:docId`. Reuses the shared
* `PageEditor` (big title, Ask AI / slash items, width control, autosave) β an
* agent document is a row in the same `documents` table as a page β but swaps in
* an agent breadcrumb header and drops the page copilot panel so the outer
* document layout owns the page-mode right panel.
*/
const AgentDocumentPage = memo<AgentDocumentPageProps>(({ documentId }) => {
const { aid } = useParams<{ aid: string }>();
const agentId = aid ?? '';
const navigate = useWorkspaceAwareNavigate();
const {
error: itemError,
isLoading,
isNotFound,
item,
mutate,
skillBundle,
} = useAgentDocumentItem(agentId, documentId);
// The route owns the agent β `useChatStore.activeAgentId` can be a different
// agent (the user's main chat context). Pulling that one would 404 the
// doc-anchored topic lookup whenever the active agent doesn't own this doc.
const chatAgentId = agentId;
// `item` is resolved out of *this agent's* document list, so its presence is the
// ownership proof `getOrCreateChatTopic` demands. Waiting for it keeps a bad deep
// link from firing a guaranteed-NOT_FOUND lookup before the redirect kicks in.
const ownsDocument = !!item;
const { topicId: docChatTopicId } = useDocumentChatTopic({
agentId: ownsDocument ? chatAgentId : undefined,
documentId: ownsDocument ? documentId : undefined,
});
const askCopilotTarget = useMemo<ComposerTarget>(
() =>
chatAgentId && docChatTopicId
? createComposerTarget(
messageMapKey({
agentId: chatAgentId,
documentId,
scope: 'main',
threadId: null,
topicId: docChatTopicId,
}),
)
: { reason: 'no-composer', writable: false },
[chatAgentId, docChatTopicId, documentId],
);
// The topic panel owns its own visibility instead of the global
// `showRightPanel`: it starts collapsed on every document. The header button
// opens it, and the panel toolbar collapses it, mirroring the task Copilot.
const [topicPanelExpand, setTopicPanelExpand] = useState(false);
const toggleTopicPanel = useCallback(() => setTopicPanelExpand((expand) => !expand), []);
const collapseTopicPanel = useCallback(() => setTopicPanelExpand(false), []);
const hasTopicPanel = !!chatAgentId && !!docChatTopicId;
const backToChat = useCallback(
() => navigate(agentId ? `/agent/${agentId}` : '/agent'),
[agentId, navigate],
);
// Deleting the open document lands on the docs index (empty-state guidance +
// the persistent document tree) rather than the deleted doc's now-404 route.
const backToDocs = useCallback(
() => navigate(agentId ? buildAgentDocumentsPath(agentId) : '/agent'),
[agentId, navigate],
);
// The doc backing this route can vanish while the page is open β most often
// deleted from the working-sidebar tree (which optimistically drops the row
// from the same list this reads). Redirect to the docs index rather than
// stranding the user on a 404 for a doc they just removed. `isNotFound` is
// precise (list resolved, doc genuinely absent β not a load error), so a bad
// deep link also lands on the index instead of a dead end.
useEffect(() => {
if (isNotFound && agentId) navigate(buildAgentDocumentsPath(agentId), { replace: true });
}, [isNotFound, agentId, navigate]);
// A skill index doc is stored as `SKILL.md`; show the skill name (bundle title) instead.
const isSkillIndex = !!skillBundle;
const title = skillBundle
? skillBundle.title || skillBundle.filename || item?.title || item?.filename
: item?.title || item?.filename;
const isFile = !!item && getDocumentRenderMode(item).mode === 'file';
const header = useMemo(
() => (
<Header
agentDocumentId={item?.id}
agentId={agentId}
documentId={documentId}
fileBacked={isFile}
itemError={itemError}
title={title}
topicPanelExpand={hasTopicPanel ? topicPanelExpand : undefined}
updatedAt={item?.updatedAt}
onBack={backToChat}
onDeleted={backToDocs}
onToggleTopicPanel={toggleTopicPanel}
/>
),
[
agentId,
backToChat,
backToDocs,
documentId,
item?.id,
item?.updatedAt,
itemError,
isFile,
title,
hasTopicPanel,
topicPanelExpand,
toggleTopicPanel,
],
);
// Genuinely-absent doc (deleted or bad deep link): render nothing while the
// redirect effect above sends the user to the docs index, instead of flashing
// a 404 for a doc that simply moved to the empty-state landing.
if (isNotFound) return null;
if (isLoading) return <RouteLoading />;
if (itemError && !item)
return <AsyncError error={itemError} variant={'page'} onRetry={() => void mutate()} />;
return (
<Flexbox
horizontal
flex={1}
height={'100%'}
style={{ minHeight: 0, overflow: 'hidden' }}
width={'100%'}
>
<Flexbox flex={1} style={{ minHeight: 0 }} width={'100%'}>
{isFile ? (
<EditorProvider>
{header}
<FileDocumentPreview fileId={item?.fileId} />
</EditorProvider>
) : (
<PageEditor
fullWidthHeader
askCopilotTarget={askCopilotTarget}
header={header}
key={documentId}
// A skill index's visible name is the bundle title; renaming must go
// through the skill APIs, so lock the page title/emoji here. A plain
// title save would overwrite the `SKILL.md` filename and desync the
// bundle (and the bundle rename API rejects managed skill docs anyway).
metaReadOnly={isSkillIndex}
pageId={documentId}
rightPanel={false}
syncPageAgentActiveState={false}
title={title}
// Refresh the list so the breadcrumb and working-sidebar entry pick up
// the new title after the shared page save persists it.
onTitleChange={() => mutate()}
/>
)}
</Flexbox>
{hasTopicPanel && (
<RightPanel
defaultWidth={400}
expand={topicPanelExpand}
maxWidth={720}
minWidth={320}
onExpandChange={setTopicPanelExpand}
>
<Flexbox flex={1} height={'100%'} justify={'flex-end'} style={{ minHeight: 0 }}>
<FloatingChatPanel
agentDocumentId={item?.id}
agentId={chatAgentId}
documentId={documentId}
key={`${chatAgentId}:${docChatTopicId}:${documentId}`}
mode="embedded"
topicId={docChatTopicId}
onCollapse={collapseTopicPanel}
/>
</Flexbox>
</RightPanel>
)}
</Flexbox>
);
});
AgentDocumentPage.displayName = 'AgentDocumentPage';
export default AgentDocumentPage;