🪪 refactor: Move Subagent Authors Into @librechat/chat Behind a Message Icon Slot (#17635)

* refactor: Move Subagent Authors Into @librechat/chat Behind a Message Icon Slot

* fix: Register the Message Parts Host From the App Entry

* test: Give the Consumer Spec's Messages Host the Icon Slot
This commit is contained in:
Marco Beretta 2026-10-31 22:09:24 +01:00 • committed by GitHub
parent 4a0150e629
commit b4eb59c96c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 149 additions and 55 deletions

View file

@ -17,6 +17,7 @@ import { initializeFontSize } from '~/store/fontSize';
import { reopenTermsOnRefusal } from '~/utils/terms';
import { LiveAnnouncer } from '~/a11y';
import { router } from './routes';
import '~/Providers/MessagePartsHostContext';
import '~/Providers/MessagesHostContext';
const App = () => {

View file

@ -12,6 +12,7 @@ import type {
import type { TMessage } from 'librechat-data-provider';
import useConversationSeen from '~/hooks/Conversations/useConversationSeen';
import useMessageScrolling from '~/hooks/Messages/useMessageScrolling';
import MessageIcon from '~/components/Chat/Messages/MessageIcon';
import Files from '~/components/Chat/Messages/Content/Files';
import { useScreenshot } from '~/hooks/ScreenshotContext';
import { useForkConvoMutation } from '~/data-provider';
@ -78,6 +79,7 @@ export const appMessagesHost: MessagesHost = {
useScreenshotTarget: () => useScreenshot().screenshotTargetRef,
useAutoScroll: () => useAtomValue(autoScrollAtom),
useConversationSeen,
renderMessageIcon: (props) => <MessageIcon {...props} />,
};
setDefaultMessagesHost(appMessagesHost);

View file

@ -0,0 +1,52 @@
import React from 'react';
import { join } from 'path';
import { readFileSync } from 'fs';
import { render, screen } from '@testing-library/react';
import { agentAuthor, useParentAuthor } from '@librechat/chat/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
jest.mock('~/components/Chat/Messages/MessageIcon', () => ({
__esModule: true,
default: ({ iconData }: { iconData?: { modelLabel?: string } }) => (
<span data-testid="app-message-icon">{iconData?.modelLabel}</span>
),
}));
/** The registrations the app entry performs, loaded the way `App.jsx` loads them. Nothing here
* imports a message component, so the hosts cannot arrive as a side effect of one. */
const ENTRY_HOSTS = ['~/Providers/MessagePartsHostContext', '~/Providers/MessagesHostContext'];
ENTRY_HOSTS.forEach((path) => jest.requireActual(path));
function ParentAuthor() {
const author = useParentAuthor('conversation', 'message', 'Parent agent');
return (
<>
<span data-testid="parent-name">{author.name}</span>
{author.icon}
</>
);
}
describe('app host registration', () => {
it('registers both default hosts from the app entry', () => {
const entry = readFileSync(join(__dirname, '..', 'App.jsx'), 'utf8');
for (const path of ENTRY_HOSTS) {
expect(entry).toContain(`import '${path}';`);
}
});
it('renders subagent authors through the app hosts the entry registers', () => {
render(
<QueryClientProvider client={new QueryClient()}>
<ParentAuthor />
{agentAuthor(undefined, 'Child agent').icon}
</QueryClientProvider>,
);
expect(screen.getByTestId('parent-name')).toHaveTextContent('Parent agent');
expect(screen.getAllByTestId('app-message-icon').map((node) => node.textContent)).toEqual([
'Parent agent',
'Child agent',
]);
});
});

View file

@ -8,6 +8,7 @@ import type {
NewConversationParams,
} from '@librechat/chat/react';
import type { UseMutationResult } from '@tanstack/react-query';
import type { MessageIconData } from '@librechat/chat/react';
import type { ExtendedFile } from '@librechat/chat';
import type * as t from 'librechat-data-provider';
import type { LucideIcon } from 'lucide-react';
@ -306,11 +307,7 @@ export type TMessageProps = {
setSiblingIdx?: ((value: number) => void | React.Dispatch<React.SetStateAction<number>>) | null;
};
export type TMessageIcon = { endpoint?: string | null; isCreatedByUser?: boolean } & Pick<
t.TConversation,
'modelLabel'
> &
Pick<t.TMessage, 'model' | 'iconURL'>;
export type TMessageIcon = MessageIconData;
export type TInitialProps = {
text: string;

View file

@ -13,6 +13,7 @@ import {
subagentStatusLabelKey,
getSubagentPrompt,
useParentSubagents,
isSelfSpawn as isSelfSpawnType,
} from '@librechat/chat/react';
import type {
PartMetadata,
@ -22,7 +23,6 @@ import type {
} from 'librechat-data-provider';
import type { SubagentContentPreview } from '@librechat/chat/react';
import type { SubagentTickerLine } from '@librechat/chat';
import { isSelfSpawn as isSelfSpawnType } from '~/components/Chat/Subagents/author';
import { useOpenSubagentPanel } from '~/components/Chat/Subagents/surface';
import { MessageContext } from '~/Providers/MessageContext';
import { useShareContext } from '~/Providers/ShareContext';

View file

@ -1,7 +1,6 @@
import { useMemo, memo } from 'react';
import { getEndpointField } from 'librechat-data-provider';
import type { Assistant, Agent } from 'librechat-data-provider';
import type { TMessageIcon } from '~/common';
import type { MessageIconProps } from '@librechat/chat/react';
import ConvoIconURL from '~/components/Endpoints/ConvoIconURL';
import { useShareContext } from '~/Providers/ShareContext';
import { useGetEndpointsQuery } from '~/data-provider';
@ -9,12 +8,6 @@ import Icon from '~/components/Endpoints/Icon';
import { isImageURL } from '~/utils/icons';
import { getIconEndpoint } from '~/utils';
type MessageIconProps = {
iconData?: TMessageIcon;
assistant?: Assistant;
agent?: Agent;
};
/**
* Compares only the fields MessageIcon actually renders.
* `agent.id` / `assistant.id` are intentionally omitted because

View file

@ -1,15 +1,15 @@
import { useMemo } from 'react';
import { findSubagentDispatch } from '@librechat/chat';
import { MessageRow } from '@librechat/chat/components';
import { useParentSubagents } from '@librechat/chat/react';
import type { WakeupTask } from '@librechat/chat';
import type { ComponentProps } from 'react';
import {
useParentSubagents,
messageAuthor,
resolveSubagentAuthor,
findAgentAuthorMessage,
useParentAuthor,
} from '~/components/Chat/Subagents/author';
} from '@librechat/chat/react';
import type { WakeupTask } from '@librechat/chat';
import type { ComponentProps } from 'react';
import { useOptionalMessagesOperations } from '~/Providers/MessagesViewContext';
import { useShareContext } from '~/Providers/ShareContext';
import { useAgentsMapContext } from '~/Providers';

View file

@ -7,6 +7,8 @@ import type { TMessageChatContext } from '~/common';
import { ShareMessagesProvider } from '~/components/Share/ShareMessagesProvider';
import { ShareContext } from '~/Providers/ShareContext';
import MessageRender from '../MessageRender';
import '~/Providers/MessagePartsHostContext';
import '~/Providers/MessagesHostContext';
const mockAgentsMap = {
agent_reviewer: { id: 'agent_reviewer', name: 'Code Reviewer' },

View file

@ -7,6 +7,8 @@ import SharedSubagentActivityDialog from './SharedSubagentActivityDialog';
import { MessageContext } from '~/Providers/MessageContext';
import { ShareContext } from '~/Providers/ShareContext';
import { ChatSurfaceHarness } from 'test/harness';
import '~/Providers/MessagePartsHostContext';
import '~/Providers/MessagesHostContext';
const mockUseSubagentThreadQuery = jest.fn();

View file

@ -5,15 +5,13 @@ import {
activeSubagentPanel,
adaptLivePersistedActivity,
resolveSubagentAgentId,
} from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import {
isSelfSpawn as isSelfSpawnType,
messageAuthor,
findAgentLaneId,
resolveSubagentAuthor,
findAgentAuthorMessage,
} from './author';
} from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import { MessageSurfaceContext } from '~/components/Chat/Messages/ui/surface';
import { SubagentActivityScrollSurface } from './SubagentActivity';
import SubagentConversation from './SubagentConversation';

View file

@ -8,9 +8,8 @@ import type {
TMessage,
TMessageContentParts,
} from 'librechat-data-provider';
import type { ChildConversationTurn } from '@librechat/chat/react';
import type { ChildConversationTurn, TurnAuthor } from '@librechat/chat/react';
import type { ChatContract } from '~/hooks/Chat/contract';
import type { TurnAuthor } from './author';
import SubagentConversation from './SubagentConversation';
import { ChatContext } from '~/Providers/ChatContext';
import { ChatSurfaceHarness } from 'test/harness';

View file

@ -22,13 +22,12 @@ import {
ElapsedTimer,
MessageRow,
} from '@librechat/chat/components';
import type { ChildConversationTurn, ToolContentQuery } from '@librechat/chat/react';
import type { ChildConversationTurn, ToolContentQuery, TurnAuthor } from '@librechat/chat/react';
import type { TMessage, TMessageContentParts } from 'librechat-data-provider';
import type { ReactNode } from 'react';
import type { TaskContentScope } from '~/data-provider/Subagents/content';
import type { MessagePartsHost } from '~/hooks/Chat/contract';
import type { TranslationKeys } from '~/hooks';
import type { TurnAuthor } from './author';
import {
TaskContentScopeContext,
useSubagentTaskContentQuery,

View file

@ -21,6 +21,8 @@ import { ChatSurfaceHarness, testChatSurface } from 'test/harness';
import SubagentThreadPanel from './SubagentThreadPanel';
import { useOpenSubagentPanel } from './surface';
import { getDraft } from '~/utils';
import '~/Providers/MessagePartsHostContext';
import '~/Providers/MessagesHostContext';
const mockUseSubagentThreadQuery = jest.fn();
const mockUseToolCallPartQuery = jest.fn(

View file

@ -31,6 +31,12 @@ import {
isLiveSubagentStatus,
getSubagentPrompt,
useParentSubagents,
agentAuthor,
resolveSubagentAuthor,
isSelfSpawn as isSelfSpawnType,
resolveChildAgent,
readableSubagentType,
useParentAuthor,
} from '@librechat/chat/react';
import type {
ParentSubagentTaskSummary,
@ -51,14 +57,6 @@ import {
useSubagentControlMutation,
useSubagentThreadQuery,
} from '~/data-provider';
import {
agentAuthor,
resolveSubagentAuthor,
isSelfSpawn as isSelfSpawnType,
resolveChildAgent,
readableSubagentType,
useParentAuthor,
} from './author';
import SubagentActivity, { SubagentActivityScrollSurface } from './SubagentActivity';
import ApprovalProvider from '~/components/Chat/Messages/Content/ApprovalContext';
import { TaskContentScopeContext } from '~/data-provider/Subagents/content';

View file

@ -1,10 +1,11 @@
import { RecoilRoot } from 'recoil';
import { render, screen, waitFor } from '@testing-library/react';
import { agentAuthor, messageAuthor } from '@librechat/chat/react';
import { dataService, EModelEndpoint } from 'librechat-data-provider';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { TMessage } from 'librechat-data-provider';
import { ShareContext } from '~/Providers/ShareContext';
import { agentAuthor, messageAuthor } from './author';
import '~/Providers/MessagesHostContext';
jest.mock('librechat-data-provider', () => {
const actual = jest.requireActual('librechat-data-provider');

View file

@ -1,5 +1,4 @@
import { ContentTypes, EModelEndpoint } from 'librechat-data-provider';
import type { TMessage } from 'librechat-data-provider';
import {
isSelfSpawn,
messageAuthor,
@ -10,7 +9,8 @@ import {
readableSubagentType,
readableSubagentTitle,
findAgentAuthorMessage,
} from './author';
} from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
function message(
messageId: string,

View file

@ -9,6 +9,8 @@ import { ShareMessagesProvider } from './ShareMessagesProvider';
import { AgentsMapContext } from '~/Providers/AgentsMapContext';
import { ShareContext } from '~/Providers/ShareContext';
import MessagesView from './MessagesView';
import '~/Providers/MessagePartsHostContext';
import '~/Providers/MessagesHostContext';
jest.mock('librechat-data-provider', () => {
const actual = jest.requireActual('librechat-data-provider');

View file

@ -26,6 +26,7 @@ export function createMessagesHost(overrides: Partial<MessagesHost> = {}): Messa
useScreenshotTarget: () => undefined,
useAutoScroll: () => false,
useConversationSeen: () => () => undefined,
renderMessageIcon: () => null,
...overrides,
};
}

View file

@ -1,19 +1,13 @@
import { cn, useLocalize } from '@librechat/client';
import type { ReactElement } from 'react';
import { isDocumentId } from '../../core/documents';
type HeaderLabelProps = {
label: string;
hoverLabel?: string | null;
};
/** Agents and assistants are keyed by document id, and a message's `model`
* carries that id rather than a model name, so neither prefix may reach the
* header. */
const DOCUMENT_ID_PREFIXES = ['agent_', 'asst_'];
/** An agent or assistant document id: a storage key, never a name to show. */
export const isDocumentId = (value: string): boolean =>
DOCUMENT_ID_PREFIXES.some((prefix) => value.startsWith(prefix));
export { isDocumentId };
/** Skip document ids so the hover label is a real model name. */
export function getHeaderModelName(

View file

@ -0,0 +1,8 @@
/** Agents and assistants are keyed by document id, and a message's `model`
* carries that id rather than a model name, so neither prefix may reach the
* header. */
const DOCUMENT_ID_PREFIXES = ['agent_', 'asst_'];
/** An agent or assistant document id: a storage key, never a name to show. */
export const isDocumentId = (value: string): boolean =>
DOCUMENT_ID_PREFIXES.some((prefix) => value.startsWith(prefix));

View file

@ -25,3 +25,4 @@ export { strongDirection } from './direction';
export * from './shortcuts';
export { removeCharIfLast } from './textarea';
export * from './pane';
export * from './documents';

View file

@ -127,6 +127,7 @@ export {
useMessagesHost,
MessagesHostProvider,
setDefaultMessagesHost,
MessageIconSlot,
} from './react/messages/host';
export type {
ForkEvents,
@ -135,6 +136,8 @@ export type {
ForkConversation,
MessagesScrolling,
ReportNearBottom,
MessageIconData,
MessageIconProps,
} from './react/messages/host';
export {
SoleToolContext,

View file

@ -1,5 +1,11 @@
import { useRef, createContext, useContext } from 'react';
import type { TMessage, TForkConvoRequest } from 'librechat-data-provider';
import type {
Agent,
TMessage,
Assistant,
TConversation,
TForkConvoRequest,
} from 'librechat-data-provider';
import type { MouseEvent, ReactNode, RefObject } from 'react';
/** The scroll machinery of the message list, as the app's scrolling hook drives it. */
@ -45,6 +51,21 @@ export type ForkEvents = {
/** Starts a fork of a conversation. */
export type ForkConversation = (request: TForkConvoRequest, events: ForkEvents) => void;
/** What a message author's icon is drawn from: the endpoint and model, or the agent or
* assistant that wrote it. */
export type MessageIconData = { endpoint?: string | null; isCreatedByUser?: boolean } & Pick<
TConversation,
'modelLabel'
> &
Pick<TMessage, 'model' | 'iconURL'>;
/** The props the app's message icon renders from. */
export type MessageIconProps = {
iconData?: MessageIconData;
assistant?: Assistant;
agent?: Agent;
};
/**
* Everything the message list reads from the app: viewer preferences, and render slots for the
* pieces of a row that have not moved into this package yet. Members are hooks or slots, called
@ -83,6 +104,9 @@ export interface MessagesHost {
measureNearBottom: () => boolean | null,
isResponseRendered: (messageId: string) => boolean,
) => ReportNearBottom;
/** Renders a message author's icon with the app's endpoint and avatar icons. */
renderMessageIcon: (props: MessageIconProps) => ReactNode;
}
const noop = (): void => undefined;
@ -124,6 +148,7 @@ const fallbackHost: MessagesHost = {
useScreenshotTarget: () => useRef<HTMLDivElement>(null),
useAutoScroll: () => false,
useConversationSeen: () => ignoreNearBottom,
renderMessageIcon: () => null,
};
const MessagesHostContext = createContext<MessagesHost | null>(null);
@ -153,3 +178,8 @@ export function MessagesHostProvider({
export function useMessagesHost(): MessagesHost {
return useContext(MessagesHostContext) ?? defaultHost;
}
/** A message author's icon, drawn by the host's slot. */
export function MessageIconSlot(props: MessageIconProps): ReactNode {
return useMessagesHost().renderMessageIcon(props);
}

View file

@ -1,12 +1,11 @@
import { useMemo, useSyncExternalStore } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { findSubagentDispatch } from '@librechat/chat';
import { isDocumentId } from '@librechat/chat/components';
import { EModelEndpoint, QueryKeys, ContentTypes } from 'librechat-data-provider';
import type { Agent, TMessage } from 'librechat-data-provider';
import type { ReactNode } from 'react';
import MessageIcon from '~/components/Chat/Messages/MessageIcon';
import { useAgentsMapContext } from '~/Providers';
import { findSubagentDispatch, isDocumentId } from '../../core';
import { MessageIconSlot } from '../messages/host';
import { useMessagePartsHost } from '../host';
/** Who wrote a turn, in the form main chat's message header shows an author. */
export type TurnAuthor = { name: string; icon: ReactNode; agent?: Agent; agentId?: string };
@ -136,7 +135,7 @@ export function agentAuthor(agent: Agent | undefined, fallbackName: string): Tur
agentId: agent?.id,
name,
icon: (
<MessageIcon
<MessageIconSlot
iconData={{ endpoint: EModelEndpoint.agents, modelLabel: name, isCreatedByUser: false }}
agent={agent}
/>
@ -191,7 +190,7 @@ export function messageAuthor(
agentId,
name,
icon: (
<MessageIcon
<MessageIconSlot
iconData={{
endpoint: message.endpoint,
model: agentId,
@ -216,7 +215,7 @@ export function useParentAuthor(
threadId?: string,
): TurnAuthor {
const queryClient = useQueryClient();
const agentsMap = useAgentsMapContext();
const host = useMessagePartsHost();
/** A different dispatch must read the latest message snapshot, even when
* both children belong to the same streamed parent turn. */
const source = useMemo(
@ -263,8 +262,16 @@ export function useParentAuthor(
};
}, [queryClient, source]);
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
const agentId = snapshot?.laneId ?? snapshot?.message.model ?? '';
const agent = host.useAgent(agentId);
return useMemo(
() => messageAuthor(snapshot?.message, agentsMap, fallbackName, snapshot?.laneId),
[agentsMap, fallbackName, snapshot],
() =>
messageAuthor(
snapshot?.message,
agent == null ? undefined : { [agentId]: agent },
fallbackName,
snapshot?.laneId,
),
[agent, agentId, fallbackName, snapshot],
);
}

View file

@ -9,3 +9,4 @@ export * from './recovery';
export * from './eventSelection';
export * from './attachments';
export * from './parents';
export * from './author';

View file

@ -204,6 +204,7 @@ const messagesHost: MessagesHost = {
useScreenshotTarget: () => undefined,
useAutoScroll: () => false,
useConversationSeen: () => () => undefined,
renderMessageIcon: () => null,
};
const renderers: PartRenderers = {