mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 06:51:59 +00:00
🪪 fix: Draw a Subagent's Brief as the Dispatching Agent's Turn (#18278)
* 🪪 fix: Draw a Subagent's Brief as the Dispatching Agent's Turn * 🪪 fix: Name a Self-Spawn's Brief for Its Own Agent and Drop the Child's Model From the Brief's Heading
This commit is contained in:
parent
7d1f618b24
commit
2a63b30948
7 changed files with 105 additions and 17 deletions
|
|
@ -8,6 +8,7 @@ import { areMessageFieldsEqual, isSameTailRelation } from '../../react/messages/
|
|||
import { getHeaderPrefixForScreenReader, getMessageAriaLabel } from './labels';
|
||||
import { MessageIconSlot, useMessagesHost } from '../../react/messages/host';
|
||||
import useContentMetadata from '../../react/messages/useContentMetadata';
|
||||
import { useUserTurnAuthor } from '../../react/messages/author';
|
||||
import { parseWakeupMessage } from '../../core/parts/wakeup';
|
||||
import { MessageContext } from '../../react/message';
|
||||
import { getHeaderHoverLabel } from './HeaderLabel';
|
||||
|
|
@ -103,6 +104,7 @@ const MessageRender: NamedExoticComponent<MessageRenderProps> = memo(function Me
|
|||
chatContext,
|
||||
});
|
||||
const maximizeChatSpace = host.useMaximizeChatSpace();
|
||||
const userAuthor = useUserTurnAuthor();
|
||||
|
||||
const handleRegenerateMessage = useCallback(() => regenerateMessage(), [regenerateMessage]);
|
||||
const getLatestMessageId = useCallback(() => chatContext.latestMessageId, [chatContext]);
|
||||
|
|
@ -156,6 +158,8 @@ const MessageRender: NamedExoticComponent<MessageRenderProps> = memo(function Me
|
|||
const Row = subagentWakeup == null ? MessageRow : WakeupRow;
|
||||
const isUserSide =
|
||||
msg.isCreatedByUser === true && (edit || wakeupDisplay == null || subagentWakeup != null);
|
||||
/** A user-side turn someone other than the reader wrote is drawn as that author's. */
|
||||
const authored = msg.isCreatedByUser === true && wakeupDisplay == null ? userAuthor : undefined;
|
||||
|
||||
return (
|
||||
<Row
|
||||
|
|
@ -164,15 +168,27 @@ const MessageRender: NamedExoticComponent<MessageRenderProps> = memo(function Me
|
|||
conversationId={msg.conversationId ?? conversation?.conversationId ?? ''}
|
||||
message={wakeupDisplay != null ? undefined : msg}
|
||||
id={msg.messageId}
|
||||
icon={<MessageIconSlot iconData={iconData} assistant={assistant} agent={agent} />}
|
||||
label={messageLabel ?? ''}
|
||||
hoverLabel={getHeaderHoverLabel(
|
||||
hasConfiguredSender,
|
||||
agent?.model,
|
||||
assistant?.model,
|
||||
msg.model,
|
||||
conversation?.model,
|
||||
)}
|
||||
icon={
|
||||
authored != null ? (
|
||||
authored.icon
|
||||
) : (
|
||||
<MessageIconSlot iconData={iconData} assistant={assistant} agent={agent} />
|
||||
)
|
||||
}
|
||||
label={authored != null ? authored.label : (messageLabel ?? '')}
|
||||
showAuthor={authored != null}
|
||||
hoverLabel={
|
||||
/** The model is the conversation's agent's, not this author's, so it is not shown. */
|
||||
authored != null
|
||||
? undefined
|
||||
: getHeaderHoverLabel(
|
||||
hasConfiguredSender,
|
||||
agent?.model,
|
||||
assistant?.model,
|
||||
msg.model,
|
||||
conversation?.model,
|
||||
)
|
||||
}
|
||||
timestamp={msg.createdAt ?? msg.clientTimestamp}
|
||||
ariaLabel={getMessageAriaLabel(msg, localize)}
|
||||
headerPrefix={getHeaderPrefixForScreenReader(msg, localize)}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ import { useMemo } from 'react';
|
|||
import { buildTree } from 'librechat-data-provider';
|
||||
import type { TConversation } from 'librechat-data-provider';
|
||||
import type { ReactElement } from 'react';
|
||||
import type { TMessageAuthor } from '../../react/messages/author';
|
||||
import type { MessagesHost } from '../../react/messages/host';
|
||||
import type { ChatScreenMessagesSlots } from './messages';
|
||||
import type { FollowedChat } from '../../react/follow';
|
||||
import { useMessagesHost, inertSeenSource, MessagesHostProvider } from '../../react/messages/host';
|
||||
import { ChatContext, useChatContext } from '../../react/chat/context';
|
||||
import { UserTurnAuthorContext } from '../../react/messages/author';
|
||||
import { useFollowedContract } from '../../react/chat/followed';
|
||||
import ScreenMessages from './messages';
|
||||
|
||||
|
|
@ -19,6 +21,8 @@ export type FollowedThreadProps = {
|
|||
conversation: TConversation | null;
|
||||
/** Replacements for the list's slots, over the ones a followed conversation leaves empty. */
|
||||
slots?: ChatScreenMessagesSlots;
|
||||
/** Who wrote the conversation's user-side turns, when the reader did not. */
|
||||
userAuthor?: TMessageAuthor;
|
||||
};
|
||||
|
||||
const none = (): null => null;
|
||||
|
|
@ -69,6 +73,7 @@ export default function FollowedThread({
|
|||
index,
|
||||
conversation,
|
||||
slots,
|
||||
userAuthor,
|
||||
}: FollowedThreadProps): ReactElement {
|
||||
const contract = useFollowedContract({ id, followed, index, conversation });
|
||||
const { messages } = followed;
|
||||
|
|
@ -78,12 +83,14 @@ export default function FollowedThread({
|
|||
return (
|
||||
<ChatContext.Provider value={contract}>
|
||||
<MessagesHostProvider host={messagesHost}>
|
||||
<ScreenMessages
|
||||
messagesTree={messagesTree}
|
||||
messages={messages}
|
||||
slots={listSlots}
|
||||
primary={false}
|
||||
/>
|
||||
<UserTurnAuthorContext.Provider value={userAuthor}>
|
||||
<ScreenMessages
|
||||
messagesTree={messagesTree}
|
||||
messages={messages}
|
||||
slots={listSlots}
|
||||
primary={false}
|
||||
/>
|
||||
</UserTurnAuthorContext.Provider>
|
||||
</MessagesHostProvider>
|
||||
</ChatContext.Provider>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { Provider, createStore } from 'jotai';
|
|||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { Constants, ContentTypes, QueryKeys } from 'librechat-data-provider';
|
||||
import type { TMessage, SubagentControlReceipt } from 'librechat-data-provider';
|
||||
import type { Agent, TMessage, SubagentControlReceipt } from 'librechat-data-provider';
|
||||
import type { ReactElement } from 'react';
|
||||
import type { ConversationSeenSource } from '../../../react/messages/host';
|
||||
import type { ToolContentQuery } from '../../../react/parts/disclosure';
|
||||
|
|
@ -95,6 +95,8 @@ const messagesHost = createMessagesHost({
|
|||
});
|
||||
|
||||
const partsHost = createMessagePartsHost({
|
||||
/** The child's agent has a model, which a reply's header may show; a brief's must not. */
|
||||
useAgent: () => ({ id: 'agent-1', name: 'Child agent', model: 'child-model' }) as Agent,
|
||||
renderContentParts: ({ messageId, content }) => (
|
||||
<RowProbe
|
||||
messageId={messageId}
|
||||
|
|
@ -201,6 +203,24 @@ describe('FollowedChildThread', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("draws the child's brief as the turn of the agent that sent it, not the reader's", () => {
|
||||
mount(followedChat(), {}, { briefAuthor: { label: 'Lead agent', icon: <span>LA</span> } });
|
||||
|
||||
const brief = screen.getByRole('heading', { name: /Lead agent/ });
|
||||
expect(brief).toBeVisible();
|
||||
expect(brief).not.toHaveClass('sr-only');
|
||||
expect(brief).toHaveTextContent('LA');
|
||||
/** Named for its author alone: the model on offer is the child's, not the sender's. */
|
||||
expect(brief).not.toHaveTextContent('child-model');
|
||||
expect(rows()[0]).toBe('task:user=Investigate the release.');
|
||||
});
|
||||
|
||||
it('leaves a brief with no known sender as a plain user-side turn', () => {
|
||||
mount(followedChat());
|
||||
|
||||
expect(screen.queryByText('LA')).toBeNull();
|
||||
});
|
||||
|
||||
it("reads a tool call's full output through the host's parent-scoped query", () => {
|
||||
const useThreadToolContent = jest.fn() as unknown as ToolContentQuery;
|
||||
mount(followedChat(), { useThreadToolContent });
|
||||
|
|
|
|||
|
|
@ -319,11 +319,13 @@ jest.mock('../followed', () => ({
|
|||
threadId,
|
||||
agentId,
|
||||
followed,
|
||||
briefAuthor,
|
||||
controls,
|
||||
onCancelControl,
|
||||
}: {
|
||||
threadId: string;
|
||||
agentId?: string;
|
||||
briefAuthor?: { label: string };
|
||||
followed: { isLoading: boolean };
|
||||
controls?: Array<{ controlId?: string; status: string }>;
|
||||
onCancelControl?: (controlId: string) => void;
|
||||
|
|
@ -338,6 +340,7 @@ jest.mock('../followed', () => ({
|
|||
data-testid="followed-thread"
|
||||
data-thread={threadId}
|
||||
data-agent={agentId}
|
||||
data-brief-author={briefAuthor?.label}
|
||||
data-loading={String(followed.isLoading)}
|
||||
data-receipt-scope={JSON.stringify(scope)}
|
||||
data-controls={(controls ?? []).map((control) => control.status).join(',')}
|
||||
|
|
@ -4751,6 +4754,8 @@ describe('SubagentThreadPanel', () => {
|
|||
const thread = screen.getByTestId('followed-thread');
|
||||
expect(thread).toHaveAttribute('data-thread', 'child-thread');
|
||||
expect(thread).toHaveAttribute('data-agent', 'agent-1');
|
||||
/** The brief is the dispatching agent's turn; unresolved here, so its fallback name. */
|
||||
expect(thread).toHaveAttribute('data-brief-author', 'com_ui_subagent_parent_agent');
|
||||
expect(thread).toHaveAttribute(
|
||||
'data-receipt-scope',
|
||||
JSON.stringify({
|
||||
|
|
@ -4766,6 +4771,18 @@ describe('SubagentThreadPanel', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("names a self-spawn's brief for the agent it runs as, as the durable view does", () => {
|
||||
view('completed');
|
||||
mockUseThreadChat.mockReturnValue(followedChat());
|
||||
|
||||
mount({ ...selection, subagentType: 'self' });
|
||||
|
||||
expect(screen.getByTestId('followed-thread')).toHaveAttribute(
|
||||
'data-brief-author',
|
||||
'Analyst One',
|
||||
);
|
||||
});
|
||||
|
||||
it('shows the thread while its messages load', () => {
|
||||
view('completed');
|
||||
mockUseThreadChat.mockReturnValue(followedChat({ isLoading: true }));
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { EModelEndpoint } from 'librechat-data-provider';
|
|||
import type { TConversation, TMessageContentParts } from 'librechat-data-provider';
|
||||
import type { ReactElement } from 'react';
|
||||
import type { ChildActivity } from '../../react/subagents/adapters';
|
||||
import type { TMessageAuthor } from '../../react/messages/author';
|
||||
import type { ChatScreenMessagesSlots } from '../pane/messages';
|
||||
import type { FollowedChat } from '../../react/follow';
|
||||
import { MessagePartsHostProvider, useMessagePartsHost } from '../../react/host';
|
||||
|
|
@ -24,6 +25,8 @@ export type FollowedChildThreadProps = {
|
|||
followed: FollowedChat;
|
||||
/** The agent the child runs as, which its replies are drawn for. */
|
||||
agentId?: string;
|
||||
/** Who briefed the child: the agent that dispatched it, which its brief is drawn for. */
|
||||
briefAuthor?: TMessageAuthor;
|
||||
/** The guidance sent to the task, as the task's durable view reports it. */
|
||||
controls?: ChildActivity['controls'];
|
||||
/** Earlier guidance than `controls` holds exists. */
|
||||
|
|
@ -43,6 +46,7 @@ export default function FollowedChildThread({
|
|||
threadId,
|
||||
followed,
|
||||
agentId,
|
||||
briefAuthor,
|
||||
controls = noControls,
|
||||
controlsTruncated = false,
|
||||
onCancelControl,
|
||||
|
|
@ -127,6 +131,7 @@ export default function FollowedChildThread({
|
|||
followed={followed}
|
||||
conversation={conversation}
|
||||
slots={slots}
|
||||
userAuthor={briefAuthor}
|
||||
/>
|
||||
</div>
|
||||
</ToolContentQueryContext.Provider>
|
||||
|
|
|
|||
|
|
@ -1036,6 +1036,16 @@ export default function SubagentThreadPanel({
|
|||
threadView?.title,
|
||||
],
|
||||
);
|
||||
/**
|
||||
* Who a child's brief came from. A self-spawn is briefed by the agent it runs as, and its
|
||||
* enclosing message can belong to another agent on a parallel lane, so the child's author is
|
||||
* the one to show; any other child is briefed by the agent that dispatched it.
|
||||
*/
|
||||
const dispatcher = isSelfSpawn && selection.event == null ? childAuthor : parentAuthor;
|
||||
const briefAuthor = useMemo(
|
||||
() => ({ icon: dispatcher.icon, label: dispatcher.name }),
|
||||
[dispatcher.icon, dispatcher.name],
|
||||
);
|
||||
const panelTitle = childAuthor.name;
|
||||
const actorOptions = useMemo<OptionWithIcon[]>(() => {
|
||||
if (selection.event == null) return [];
|
||||
|
|
@ -1597,7 +1607,7 @@ export default function SubagentThreadPanel({
|
|||
<SubagentConversation
|
||||
turns={conversationTurns}
|
||||
author={childAuthor}
|
||||
parentAuthor={isSelfSpawn && selection.event == null ? childAuthor : parentAuthor}
|
||||
parentAuthor={dispatcher}
|
||||
conversationId={threadId || selection.parentConversationId}
|
||||
interactive={selection.host === 'conversation' && selection.durable == null}
|
||||
recovery={
|
||||
|
|
@ -1752,6 +1762,7 @@ export default function SubagentThreadPanel({
|
|||
threadId={threadId}
|
||||
followed={followed}
|
||||
agentId={taskView?.agentId ?? foregroundAgentId}
|
||||
briefAuthor={briefAuthor}
|
||||
controls={activity.controls}
|
||||
controlsTruncated={activity.controlsTruncated}
|
||||
onCancelControl={
|
||||
|
|
|
|||
|
|
@ -17,3 +17,15 @@ export const AuthorContext: Context<TMessageAuthor | null> = createContext<TMess
|
|||
null,
|
||||
);
|
||||
export const useAuthorContext = (): TMessageAuthor | null => useContext(AuthorContext);
|
||||
|
||||
/**
|
||||
* Who wrote a list's user-side turns, when that is not the reader. A conversation the reader
|
||||
* follows can hold such turns: a subagent's brief comes from the agent that dispatched it. A
|
||||
* list under this context draws them with this author's name and face, shown, instead of the
|
||||
* reader's.
|
||||
*/
|
||||
export const UserTurnAuthorContext: Context<TMessageAuthor | undefined> = createContext<
|
||||
TMessageAuthor | undefined
|
||||
>(undefined);
|
||||
export const useUserTurnAuthor = (): TMessageAuthor | undefined =>
|
||||
useContext(UserTurnAuthorContext);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue