mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 14:33:38 +00:00
* refactor: Move the Subagent Transcript Into @librechat/chat Behind a Subagents Host SubagentActivity, its scroll surface, content, status and guidance history move into @librechat/chat/components, and ScrollToBottom moves into the package with them. A new SubagentsHost carries what still lives in the app: the content-parts and steer renderers, the animated jump-to-latest control, and the read of a shortened guidance receipt, which stays an app query. The prompt preview reuses renderMarkdown with a new codeExecution flag, and the task content scope becomes a plain package context. * refactor: Draw Content Parts Through the Message Parts Host renderContentParts moves from SubagentsHost onto MessagePartsHost as the one content slot, typed as ContentPartsSlotProps: the transcript fields plus the optional row fields message rows pass. The app fills it through a registered renderer, so the host module does not import the content stack, and omitted row fields keep an assistant reply's defaults. * refactor: Let the Content Parts Slot Carry Sparse Content Main message rows keep holes at streaming and parallel indexes, and part indexes depend on those positions, so the slot's content keeps undefined entries as ContentParts already accepts them. * test: Exercise the Real App Renderers in the Host Registration Spec The transcript host test now renders the app's ContentParts and SteerPart under the providers they read, so the registered content renderer and steer slot are proven against the real components; only the receipt request stays mocked.
160 lines
5.9 KiB
TypeScript
160 lines
5.9 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
import { useAtom } from 'jotai';
|
|
import { SubagentActivityScrollSurface } from '@librechat/chat/components';
|
|
import { OGDialog, OGDialogContent, OGDialogHeader, OGDialogTitle } from '@librechat/client';
|
|
import {
|
|
activeSubagentPanel,
|
|
adaptLivePersistedActivity,
|
|
resolveSubagentAgentId,
|
|
isSelfSpawn as isSelfSpawnType,
|
|
messageAuthor,
|
|
findAgentLaneId,
|
|
resolveSubagentAuthor,
|
|
findAgentAuthorMessage,
|
|
} from '@librechat/chat/react';
|
|
import type { TMessage } from 'librechat-data-provider';
|
|
import { MessageSurfaceContext } from '~/components/Chat/Messages/ui/surface';
|
|
import SubagentConversation from './SubagentConversation';
|
|
import { useAgentsMapContext } from '~/Providers';
|
|
import { useLocalize } from '~/hooks';
|
|
|
|
/** Public-share fallback for subagent activity already embedded in the shared message payload. */
|
|
export default function SharedSubagentActivityDialog({
|
|
shareId,
|
|
messages,
|
|
}: {
|
|
shareId?: string;
|
|
/** The shared thread, which names the agent that dispatched the child. */
|
|
messages?: TMessage[];
|
|
}) {
|
|
const localize = useLocalize();
|
|
const agentsMap = useAgentsMapContext();
|
|
const [selected, setSelected] = useAtom(activeSubagentPanel);
|
|
const resetSelection = useCallback(() => setSelected(null), [setSelected]);
|
|
const selection = selected?.host === 'share' && selected.shareId === shareId ? selected : null;
|
|
const restoreSelectionRef = useRef(selection);
|
|
if (selection != null) restoreSelectionRef.current = selection;
|
|
const parentMessageId = selection?.parentMessageId ?? '';
|
|
const parentFallback = localize('com_ui_subagent_parent_agent');
|
|
/** Resolved only while a child is open: the shared thread is never scanned for
|
|
* a dialog nobody is looking at. */
|
|
const parentAuthor = useMemo(() => {
|
|
const message =
|
|
parentMessageId === '' ? undefined : findAgentAuthorMessage(messages, parentMessageId);
|
|
return messageAuthor(
|
|
message,
|
|
agentsMap,
|
|
parentFallback,
|
|
findAgentLaneId(message, selection?.toolCallId, selection?.partIndex),
|
|
);
|
|
}, [
|
|
agentsMap,
|
|
messages,
|
|
parentFallback,
|
|
parentMessageId,
|
|
selection?.toolCallId,
|
|
selection?.partIndex,
|
|
]);
|
|
const childAgentId = resolveSubagentAgentId(null, selection?.subagentIdentity);
|
|
const isSelfSpawn = isSelfSpawnType(
|
|
selection?.subagentType,
|
|
selection?.subagentIdentity?.subagentKind,
|
|
);
|
|
const childAuthor = useMemo(
|
|
() =>
|
|
resolveSubagentAuthor(
|
|
{
|
|
agentId: childAgentId,
|
|
subagentType: selection?.subagentType,
|
|
subagentKind: selection?.subagentIdentity?.subagentKind,
|
|
},
|
|
parentAuthor,
|
|
agentsMap,
|
|
localize('com_ui_subagent_actor'),
|
|
),
|
|
[agentsMap, childAgentId, localize, parentAuthor, selection],
|
|
);
|
|
const title = childAuthor.name;
|
|
const activity = useMemo(
|
|
() =>
|
|
adaptLivePersistedActivity({
|
|
title,
|
|
prompt: selection?.prompt,
|
|
progress: null,
|
|
persistedContent: selection?.persistedContent,
|
|
attachments: selection?.attachments,
|
|
legacyOutput: selection?.legacyOutput,
|
|
initialProgress: selection?.initialProgress ?? 1,
|
|
isSubmitting: false,
|
|
runStepStatus: selection?.runStepStatus,
|
|
approvalVisibility: 'hidden',
|
|
}),
|
|
[selection, title],
|
|
);
|
|
|
|
const restoreTriggerFocus = useCallback((event: Event) => {
|
|
event.preventDefault();
|
|
const selectionToRestore = restoreSelectionRef.current;
|
|
if (selectionToRestore == null) return;
|
|
requestAnimationFrame(() => {
|
|
const trigger = Array.from(
|
|
document.querySelectorAll<HTMLElement>('[data-subagent-tool-call]'),
|
|
).find(
|
|
(element) =>
|
|
element.dataset.subagentToolCall === selectionToRestore.toolCallId &&
|
|
element.dataset.subagentParentMessage === selectionToRestore.parentMessageId &&
|
|
element.dataset.subagentPartIndex === String(selectionToRestore.partIndex),
|
|
);
|
|
trigger?.focus();
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => () => resetSelection(), [resetSelection]);
|
|
useEffect(() => {
|
|
if (selected?.host === 'share' && selected.shareId !== shareId) resetSelection();
|
|
}, [resetSelection, selected, shareId]);
|
|
|
|
return (
|
|
<OGDialog open={selection != null} onOpenChange={(open) => !open && resetSelection()}>
|
|
<OGDialogContent
|
|
className="flex h-[min(90vh,48rem)] w-11/12 max-w-3xl flex-col gap-0 overflow-hidden p-0"
|
|
onCloseAutoFocus={restoreTriggerFocus}
|
|
>
|
|
<OGDialogHeader className="border-border-light shrink-0 border-b px-5 py-4 pe-14">
|
|
<div className="flex min-w-0 items-center gap-2">
|
|
<span
|
|
aria-hidden="true"
|
|
className="flex size-6 shrink-0 items-center justify-center overflow-hidden rounded-full"
|
|
>
|
|
{childAuthor.icon}
|
|
</span>
|
|
<OGDialogTitle className="truncate text-start text-base" title={title}>
|
|
{title}
|
|
</OGDialogTitle>
|
|
</div>
|
|
</OGDialogHeader>
|
|
<MessageSurfaceContext.Provider value="bg-surface-dialog">
|
|
<SubagentActivityScrollSurface padded={false}>
|
|
<SubagentConversation
|
|
author={childAuthor}
|
|
parentAuthor={isSelfSpawn ? childAuthor : parentAuthor}
|
|
turns={[
|
|
{
|
|
taskId:
|
|
selection == null
|
|
? 'shared-subagent'
|
|
: `${selection.parentMessageId}\u0000${selection.toolCallId}\u0000${selection.partIndex}`,
|
|
trigger: {
|
|
kind: 'parent_dispatch',
|
|
summary: selection?.prompt ?? '',
|
|
},
|
|
activity,
|
|
},
|
|
]}
|
|
/>
|
|
</SubagentActivityScrollSurface>
|
|
</MessageSurfaceContext.Provider>
|
|
</OGDialogContent>
|
|
</OGDialog>
|
|
);
|
|
}
|