mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 15:33:45 +00:00
⏰ refactor: Move the Wake-Up Row Into @librechat/chat (#17780)
* refactor: Move the Wake-Up Row Into @librechat/chat * test: Drive the Wake-Up Row Spec Through the Real Subagent Index
This commit is contained in:
parent
83f7c6f820
commit
299d1fa6a3
6 changed files with 241 additions and 35 deletions
|
|
@ -6,6 +6,7 @@ import {
|
|||
MessageFooter,
|
||||
getHeaderHoverLabel,
|
||||
MessageRow,
|
||||
WakeupRow,
|
||||
} from '@librechat/chat/components';
|
||||
import type { TMessage } from 'librechat-data-provider';
|
||||
import type { TMessageProps, TMessageIcon, TMessageChatContext } from '~/common';
|
||||
|
|
@ -18,10 +19,9 @@ import {
|
|||
import MessageContent from '~/components/Chat/Messages/Content/MessageContent';
|
||||
import { useLocalize, useMessageActions, useContentMetadata } from '~/hooks';
|
||||
import { PrivateText } from '~/components/Chat/Messages/PrivateText';
|
||||
import { MessageContext, useAgentsMapContext } from '~/Providers';
|
||||
import MessageIcon from '~/components/Chat/Messages/MessageIcon';
|
||||
import Wakeup from '~/components/Chat/Messages/Content/Wakeup';
|
||||
import { MessageContext } from '~/Providers';
|
||||
import WakeupRow from './WakeupRow';
|
||||
import store from '~/store';
|
||||
|
||||
type MessageRenderProps = {
|
||||
|
|
@ -86,6 +86,7 @@ const MessageRender = memo(function MessageRender({
|
|||
latestMessageDepth,
|
||||
}: MessageRenderProps) {
|
||||
const localize = useLocalize();
|
||||
const agentsMap = useAgentsMapContext();
|
||||
const {
|
||||
ask,
|
||||
edit,
|
||||
|
|
@ -165,6 +166,7 @@ const MessageRender = memo(function MessageRender({
|
|||
return (
|
||||
<Row
|
||||
task={subagentWakeup}
|
||||
agentsMap={agentsMap}
|
||||
conversationId={msg.conversationId ?? conversation?.conversationId ?? ''}
|
||||
message={wakeupDisplay != null ? undefined : msg}
|
||||
id={msg.messageId}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { RecoilRoot } from 'recoil';
|
|||
import { render, screen } from '@testing-library/react';
|
||||
import { ContentTypes, QueryKeys } from 'librechat-data-provider';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { ParentSubagentsProvider, StreamHostProvider } from '@librechat/chat/react';
|
||||
import type { ParentSubagentSummary, TMessage } from 'librechat-data-provider';
|
||||
import type { TMessageChatContext } from '~/common';
|
||||
import { ShareMessagesProvider } from '~/components/Share/ShareMessagesProvider';
|
||||
|
|
@ -9,6 +10,7 @@ import { ShareContext } from '~/Providers/ShareContext';
|
|||
import MessageRender from '../MessageRender';
|
||||
import '~/Providers/MessagePartsHostContext';
|
||||
import '~/Providers/MessagesHostContext';
|
||||
import { appStreamHost } from '~/Providers/StreamHostContext';
|
||||
|
||||
const mockAgentsMap = {
|
||||
agent_reviewer: { id: 'agent_reviewer', name: 'Code Reviewer' },
|
||||
|
|
@ -16,13 +18,9 @@ const mockAgentsMap = {
|
|||
};
|
||||
|
||||
let mockChildren = new Map<string, ParentSubagentSummary>();
|
||||
jest.mock('@librechat/chat/react', () => ({
|
||||
...jest.requireActual('@librechat/chat/react'),
|
||||
useParentSubagents: () => ({ byThreadId: mockChildren }),
|
||||
}));
|
||||
|
||||
jest.mock('~/Providers', () => ({
|
||||
...jest.requireActual('~/Providers/MessageContext'),
|
||||
...jest.requireActual('~/Providers'),
|
||||
useAgentsMapContext: () => mockAgentsMap,
|
||||
}));
|
||||
|
||||
|
|
@ -116,6 +114,11 @@ function renderMessage(
|
|||
},
|
||||
],
|
||||
);
|
||||
queryClient.setQueryData([QueryKeys.parentSubagents, 'conversation-1'], {
|
||||
parentConversationId: 'conversation-1',
|
||||
children: [...mockChildren.values()],
|
||||
childrenTruncated: false,
|
||||
});
|
||||
const message = {
|
||||
messageId: 'wake',
|
||||
parentMessageId: 'parent',
|
||||
|
|
@ -125,13 +128,17 @@ function renderMessage(
|
|||
text,
|
||||
} as unknown as TMessage;
|
||||
const row = (
|
||||
<MessageRender
|
||||
message={message}
|
||||
chatContext={chatContext}
|
||||
currentEditId={null}
|
||||
setSiblingIdx={() => undefined}
|
||||
{...siblings}
|
||||
/>
|
||||
<StreamHostProvider host={appStreamHost}>
|
||||
<ParentSubagentsProvider conversationId="conversation-1" enabled>
|
||||
<MessageRender
|
||||
message={message}
|
||||
chatContext={chatContext}
|
||||
currentEditId={null}
|
||||
setSiblingIdx={() => undefined}
|
||||
{...siblings}
|
||||
/>
|
||||
</ParentSubagentsProvider>
|
||||
</StreamHostProvider>
|
||||
);
|
||||
const sharedReply: TMessage = {
|
||||
messageId: 'shared-reply',
|
||||
|
|
@ -272,7 +279,7 @@ describe('MessageRender wake-up rows', () => {
|
|||
});
|
||||
renderMessage(subagentWakeup('agent_unknown'));
|
||||
|
||||
expect(screen.getByRole('heading', { name: /com_ui_subagent_actor$/ })).toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { name: 'Agent' })).toBeInTheDocument();
|
||||
expect(screen.queryByText(/agent_unknown/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { parseWakeupMessage } from '@librechat/chat';
|
|||
import {
|
||||
SubRow,
|
||||
MessageRow,
|
||||
WakeupRow,
|
||||
AuthorHeader,
|
||||
SiblingSwitch,
|
||||
getHeaderHoverLabel,
|
||||
|
|
@ -12,17 +13,17 @@ import type { TMessageProps } from '~/common';
|
|||
import SearchContent, {
|
||||
rendersMarkdownLite,
|
||||
} from '~/components/Chat/Messages/Content/SearchContent';
|
||||
import { MessageContext, useShareContext, useAgentsMapContext } from '~/Providers';
|
||||
import MessageContent from '~/components/Chat/Messages/Content/MessageContent';
|
||||
import { getHeaderPrefixForScreenReader, getMessageAriaLabel } from '~/utils';
|
||||
import WakeupRow from '~/components/Chat/Messages/ui/WakeupRow';
|
||||
import Wakeup from '~/components/Chat/Messages/Content/Wakeup';
|
||||
import { MessageContext, useShareContext } from '~/Providers';
|
||||
import { useAttachments, useLocalize } from '~/hooks';
|
||||
import MultiMessage from './MultiMessage';
|
||||
import Icon from './MessageIcon';
|
||||
|
||||
export default function Message(props: TMessageProps) {
|
||||
const localize = useLocalize();
|
||||
const agentsMap = useAgentsMapContext();
|
||||
const { hasConfiguredSender } = useShareContext();
|
||||
const {
|
||||
message,
|
||||
|
|
@ -68,6 +69,7 @@ export default function Message(props: TMessageProps) {
|
|||
<div className="m-auto justify-center px-4 py-3 sm:px-0">
|
||||
<Row
|
||||
task={subagentWakeup}
|
||||
agentsMap={agentsMap}
|
||||
conversationId={message.conversationId ?? conversation?.conversationId ?? ''}
|
||||
message={wakeupDisplay == null ? message : undefined}
|
||||
id={messageId}
|
||||
|
|
|
|||
|
|
@ -88,6 +88,7 @@ export { messageFooterClasses, revealOnRowHoverClasses } from './components/mess
|
|||
export { getRunStepDurationLabels, getElapsedDurationLabels } from './components/messages/duration';
|
||||
export type { RunStepDurationLabels } from './components/messages/duration';
|
||||
export { default as MessageRow } from './components/messages/MessageRow';
|
||||
export { default as WakeupRow } from './components/messages/WakeupRow';
|
||||
export { default as MessageTimestamp } from './components/messages/MessageTimestamp';
|
||||
export { getMessageRowWidthClass } from './components/messages/width';
|
||||
export { isValidTimestamp, getMessageTimestamp } from './components/messages/time';
|
||||
|
|
|
|||
|
|
@ -1,36 +1,40 @@
|
|||
import { useMemo } from 'react';
|
||||
import { findSubagentDispatch } from '@librechat/chat';
|
||||
import { MessageRow } from '@librechat/chat/components';
|
||||
import { useLocalize } from '@librechat/client';
|
||||
import type { ComponentProps, ReactElement } from 'react';
|
||||
import type { Agent } from 'librechat-data-provider';
|
||||
import type { WakeupTask } from '../../core/parts/wakeup';
|
||||
import {
|
||||
useParentSubagents,
|
||||
messageAuthor,
|
||||
useParentAuthor,
|
||||
resolveSubagentAuthor,
|
||||
findAgentAuthorMessage,
|
||||
useParentAuthor,
|
||||
} 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';
|
||||
import { useLocalize } from '~/hooks';
|
||||
} from '../../react/subagents/author';
|
||||
import { findSubagentDispatch } from '../../core/subagents/dispatch';
|
||||
import { useParentSubagents } from '../../react/subagents/parents';
|
||||
import { useMessagesHost } from '../../react/messages/host';
|
||||
import { useMessagePartsHost } from '../../react/host';
|
||||
import MessageRow from './MessageRow';
|
||||
|
||||
/** Only wake-up rows observe the child index; ordinary rows keep their subscriptions. */
|
||||
export default function WakeupRow({
|
||||
task,
|
||||
conversationId,
|
||||
agentsMap,
|
||||
...props
|
||||
}: ComponentProps<typeof MessageRow> & { task?: WakeupTask; conversationId: string }) {
|
||||
}: ComponentProps<typeof MessageRow> & {
|
||||
task?: WakeupTask;
|
||||
conversationId: string;
|
||||
/** The viewer's agents by id, for naming and drawing a turn's author; undefined until loaded. */
|
||||
agentsMap: Record<string, Agent | undefined> | undefined;
|
||||
}): ReactElement {
|
||||
const localize = useLocalize();
|
||||
const agentsMap = useAgentsMapContext();
|
||||
const { isSharedConvo } = useShareContext();
|
||||
const { getMessages } = useOptionalMessagesOperations();
|
||||
const { isSharedConvo } = useMessagePartsHost().useShare();
|
||||
const getMessages = useMessagesHost().useGetMessages();
|
||||
const { byThreadId } = useParentSubagents();
|
||||
const child = task?.threadId == null ? undefined : byThreadId.get(task.threadId);
|
||||
const dispatch = useMemo(
|
||||
() =>
|
||||
child == null ? findSubagentDispatch(getMessages(conversationId), task?.threadId) : undefined,
|
||||
[child, conversationId, getMessages, task?.threadId],
|
||||
() => (child == null ? findSubagentDispatch(getMessages(), task?.threadId) : undefined),
|
||||
[child, getMessages, task?.threadId],
|
||||
);
|
||||
const parentMessageId = child?.parentMessageId ?? dispatch?.message.messageId ?? props.id ?? '';
|
||||
const fallbackName = localize('com_ui_subagent_actor');
|
||||
|
|
@ -0,0 +1,190 @@
|
|||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { ContentTypes, EModelEndpoint } from 'librechat-data-provider';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import type { Agent, ParentSubagentSummary, TMessage } from 'librechat-data-provider';
|
||||
import type { MessagePartsHost, MessagePartsShare } from '../../../react/host';
|
||||
import { createMessagesHost } from '../../../__fixtures__/messagesHost';
|
||||
import { MessagesHostProvider } from '../../../react/messages/host';
|
||||
import { MessagePartsHostProvider } from '../../../react/host';
|
||||
import { initEnglish } from '../../../__fixtures__/i18n';
|
||||
import WakeupRow from '../WakeupRow';
|
||||
|
||||
let mockChildren = new Map<string, ParentSubagentSummary>();
|
||||
jest.mock('../../../react/subagents/parents', () => ({
|
||||
...jest.requireActual('../../../react/subagents/parents'),
|
||||
useParentSubagents: () => ({ byThreadId: mockChildren }),
|
||||
}));
|
||||
|
||||
beforeAll(initEnglish);
|
||||
beforeEach(() => {
|
||||
mockChildren = new Map();
|
||||
});
|
||||
|
||||
const BODY = 'One finding.';
|
||||
|
||||
const agent = (id: string, name: string): Agent => ({
|
||||
id,
|
||||
name,
|
||||
description: null,
|
||||
created_at: 0,
|
||||
avatar: null,
|
||||
provider: EModelEndpoint.openAI,
|
||||
model: 'test',
|
||||
model_parameters: {
|
||||
temperature: 1,
|
||||
maxContextTokens: 4096,
|
||||
max_context_tokens: 4096,
|
||||
max_output_tokens: 1024,
|
||||
top_p: 1,
|
||||
frequency_penalty: 0,
|
||||
presence_penalty: 0,
|
||||
},
|
||||
});
|
||||
|
||||
const child = (overrides: Partial<ParentSubagentSummary> = {}): ParentSubagentSummary => ({
|
||||
threadId: 'thread-1',
|
||||
parentMessageId: 'parent-1',
|
||||
subagentType: 'agent_reviewer',
|
||||
subagentKind: 'agent',
|
||||
agentId: 'agent_reviewer',
|
||||
title: 'Reviewer',
|
||||
origin: 'event',
|
||||
status: 'completed',
|
||||
tasks: [],
|
||||
tasksTruncated: false,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const dispatchMessage: TMessage = {
|
||||
messageId: 'dispatch',
|
||||
parentMessageId: null,
|
||||
conversationId: 'convo-1',
|
||||
isCreatedByUser: false,
|
||||
text: '',
|
||||
sender: 'Outer Parent',
|
||||
model: 'agent_outer',
|
||||
endpoint: EModelEndpoint.agents,
|
||||
content: [
|
||||
{
|
||||
type: ContentTypes.TOOL_CALL,
|
||||
agentId: 'agent_lane',
|
||||
tool_call: {
|
||||
id: 'call',
|
||||
name: 'subagent',
|
||||
args: { run_in_background: true },
|
||||
output: JSON.stringify({
|
||||
background_task_id: 'task-1',
|
||||
subagent_thread_id: 'thread-1',
|
||||
tool: 'subagent',
|
||||
subagent_type: 'self',
|
||||
status: 'running',
|
||||
message: 'Poll with background_task_id task-1.',
|
||||
}),
|
||||
subagentIdentity: { subagentKind: 'agent', subagentAgentId: 'agent_lane' },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const partsHost: MessagePartsHost = {
|
||||
useMessage: () => ({ messageId: 'wake', isSubmitting: false, isLatestMessage: true }),
|
||||
renderMarkdown: ({ content }) => <span>{content}</span>,
|
||||
renderImage: () => null,
|
||||
renderFilePreview: () => null,
|
||||
useSmoothStreaming: () => false,
|
||||
useAgent: () => undefined,
|
||||
renderAgentIcon: () => null,
|
||||
renderAgentAvatar: () => null,
|
||||
renderContentParts: () => null,
|
||||
useFontSize: () => 'text-base',
|
||||
useShowThinking: () => false,
|
||||
useAutoExpandTools: () => false,
|
||||
useLaTeXParsing: () => false,
|
||||
useChatDirection: () => 'ltr',
|
||||
useUserTextPreferences: () => ({
|
||||
usernameDisplay: true,
|
||||
enableUserMsgMarkdown: true,
|
||||
collapseLongUserMessages: false,
|
||||
}),
|
||||
useUser: () => undefined,
|
||||
useFileMap: () => undefined,
|
||||
useToast: () => () => undefined,
|
||||
useSandboxStarting: () => false,
|
||||
useToolArtifactClaim: () => [null, () => undefined],
|
||||
useSteerEscalating: () => false,
|
||||
usePaneConversationId: () => null,
|
||||
useLiveAppliedSteer: () => [false, () => undefined],
|
||||
useShare: () => ({}),
|
||||
};
|
||||
|
||||
function renderRow({
|
||||
agentsMap,
|
||||
share = {},
|
||||
messages,
|
||||
subagentType = 'agent_reviewer',
|
||||
}: {
|
||||
agentsMap?: Record<string, Agent | undefined>;
|
||||
share?: MessagePartsShare;
|
||||
messages?: TMessage[];
|
||||
subagentType?: string;
|
||||
}): void {
|
||||
const queryClient = new QueryClient();
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MessagePartsHostProvider host={{ ...partsHost, useShare: () => share }}>
|
||||
<MessagesHostProvider host={createMessagesHost({ useGetMessages: () => () => messages })}>
|
||||
<WakeupRow
|
||||
id="wake"
|
||||
task={{
|
||||
taskId: 'task-1',
|
||||
status: 'completed',
|
||||
result: BODY,
|
||||
threadId: 'thread-1',
|
||||
subagentType,
|
||||
}}
|
||||
conversationId="convo-1"
|
||||
agentsMap={agentsMap}
|
||||
icon={null}
|
||||
label="ignored"
|
||||
footer={null}
|
||||
isCreatedByUser
|
||||
>
|
||||
<p>{BODY}</p>
|
||||
</WakeupRow>
|
||||
</MessagesHostProvider>
|
||||
</MessagePartsHostProvider>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe('WakeupRow', () => {
|
||||
it('labels the row with the known child author and outlines it', () => {
|
||||
mockChildren = new Map([['thread-1', child()]]);
|
||||
renderRow({ agentsMap: { agent_reviewer: agent('agent_reviewer', 'Reviewer') } });
|
||||
|
||||
expect(screen.getByRole('heading', { name: 'Reviewer' })).toBeVisible();
|
||||
expect(screen.getByText(BODY).parentElement).toHaveClass('border', 'border-border-medium');
|
||||
expect(screen.getByText(BODY).parentElement).not.toHaveClass('bg-surface-user-message');
|
||||
});
|
||||
|
||||
it('takes the author from the dispatch message and the agents map in a shared conversation', () => {
|
||||
renderRow({
|
||||
agentsMap: { agent_lane: agent('agent_lane', 'Lane Parent') },
|
||||
share: { isSharedConvo: true },
|
||||
messages: [dispatchMessage],
|
||||
subagentType: 'self',
|
||||
});
|
||||
|
||||
expect(screen.getByRole('heading', { name: 'Lane Parent' })).toBeVisible();
|
||||
expect(screen.queryByRole('heading', { name: 'Outer Parent' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('falls back to the localized subagent label when no agent is found', () => {
|
||||
renderRow({ agentsMap: undefined, subagentType: 'self' });
|
||||
|
||||
expect(screen.getByRole('heading', { name: 'Agent' })).toBeVisible();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue