mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 23:43:35 +00:00
🏷️ fix: Keep a Configured Sender Label From Revealing the Model on Hover (#15489)
A conversation whose sender is a label someone configured shows that label as the header's name. Since #14851 the header's hover crossfade swaps the raw `message.model` back in, and the sr-only "Model:" text announces it regardless of hover. Ask the message, not the settings. `resolveSender` writes an agent's name, then the `getEphemeralSender` chain — a preset's `modelLabel`, a model spec's `label`, an endpoint's `modelDisplayLabel` — and falls back to `getResponseSender` only when none is set, so the persisted `sender` is the one thing that records which it was. The new `isConfiguredSender` compares it against what `getResponseSender` would produce for the same endpoint and model: equal means the header is showing a model-derived name and the hover may reveal the model, different means it is standing in for one. That settles three things a settings lookup cannot. Labels that live in config a caller may not hold — a spec that sets `label` and leaves `preset.modelLabel` unset is the ordinary shape, and the share publisher cannot read the spec list. Labels an endpoint ignores — Anthropic keeps writing `Claude` whatever `chatGptLabel` says, and it matches here without anything enumerating which endpoints honour what. And labels changed since the message was written — the row still shows the sender it was written under, so clearing a label must not start revealing the model behind the rows that carry it. Equality is the test, so the ordinary way to be wrong is a stored name that no longer matches what the current heuristics produce, which withholds a model rather than revealing one. Three cases are decided before any comparison: a user turn is headed by its author and has no model to withhold; agents and assistants are named by whoever authored them, so the header shows that name whether or not the response stored a sender; and an endpoint that cannot be named — an older message saved without one — says nothing either way, where reading silence as a label would withhold the model from every unlabelled row it reached. `getHeaderHoverLabel` takes that answer and produces no hover label when it is true, so the header renders the plain label with no swap and no sr-only model text. Without a configured sender the behaviour is unchanged. - Chat rows: `useMessageActions` and `useMessageHelpers` decide once per row from the message they already hold, so MessageRender, MessageParts and ContentRender consume it like any other derived display state. - Share links: the view has no conversation in scope, so `getSharedMessages` reads the answer off the very messages it is returning, collected in the pass `anonymizeMessages` already makes rather than a second traversal. Only messages the link actually publishes have a say, and nothing about the link is re-derived from the conversation afterwards. `ShareContext` — already the carrier for share-scoped values such as `shareId` — hands it to the header. The flag travels rather than the label text: each surface only needs the decision, and the text is already carried by `message.sender`, which the share content preflight inspects. Search rows are left alone. `GET /api/messages?search` returns each hit's stored sender and endpoint but overwrites `model` with the conversation's current one, so a row cannot be classified from what it carries without changing what that payload means.
This commit is contained in:
parent
9cf33cc8be
commit
d6f8b2e92a
16 changed files with 470 additions and 33 deletions
|
|
@ -1,5 +1,11 @@
|
|||
import { createContext, useContext } from 'react';
|
||||
type TShareContext = { isSharedConvo?: boolean; shareId?: string };
|
||||
type TShareContext = {
|
||||
isSharedConvo?: boolean;
|
||||
shareId?: string;
|
||||
/** Whether the link was published with a configured sender label. No conversation
|
||||
* is in scope under a share link, so the header reads it here. */
|
||||
hasConfiguredSender?: boolean;
|
||||
};
|
||||
|
||||
export const ShareContext = createContext<TShareContext>({} as TShareContext);
|
||||
export const useShareContext = () => useContext(ShareContext);
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ import {
|
|||
areMessageRowPropsEqual,
|
||||
getHeaderPrefixForScreenReader,
|
||||
} from '~/utils';
|
||||
import { useMessageHelpers, useLocalize, useAttachments, useContentMetadata } from '~/hooks';
|
||||
import { useLocalize, useAttachments, useMessageHelpers, useContentMetadata } from '~/hooks';
|
||||
import AuthorHeader from '~/components/Chat/Messages/Content/Parts/AuthorHeader';
|
||||
import { getHeaderModelName } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import { getHeaderHoverLabel } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import { revealOnRowHoverClasses, messageFooterClasses } from './styles';
|
||||
import MessageRow from '~/components/Chat/Messages/ui/MessageRow';
|
||||
import MessageIcon from '~/components/Chat/Messages/MessageIcon';
|
||||
|
|
@ -45,6 +45,7 @@ function MessageParts(props: TMessageProps) {
|
|||
copyToClipboard,
|
||||
getCanCopy,
|
||||
regenerateMessage,
|
||||
hasConfiguredSender,
|
||||
} = useMessageHelpers(props, searchResults);
|
||||
|
||||
const maximizeChatSpace = useRecoilValue(store.maximizeChatSpace);
|
||||
|
|
@ -112,7 +113,8 @@ function MessageParts(props: TMessageProps) {
|
|||
id={messageId ?? ''}
|
||||
icon={<MessageIcon iconData={iconData} assistant={assistant} agent={agent} />}
|
||||
label={name}
|
||||
hoverLabel={getHeaderModelName(
|
||||
hoverLabel={getHeaderHoverLabel(
|
||||
hasConfiguredSender,
|
||||
agent?.model,
|
||||
assistant?.model,
|
||||
message.model,
|
||||
|
|
|
|||
|
|
@ -23,6 +23,21 @@ export function getHeaderModelName(
|
|||
);
|
||||
}
|
||||
|
||||
/** A preset or model spec that configured the sender has chosen what the header
|
||||
* says, and `message.sender` already honours it. Revealing the model on hover
|
||||
* would undo that choice, so no hover label is produced at all; the sr-only
|
||||
* "Model:" text carries the same value and goes with it. Callers pass
|
||||
* `hasConfiguredSender` first, then the candidates `getHeaderModelName` takes. */
|
||||
export function getHeaderHoverLabel(
|
||||
hasConfiguredSender: boolean | undefined,
|
||||
...candidates: Array<string | null | undefined>
|
||||
): string | undefined {
|
||||
if (hasConfiguredSender === true) {
|
||||
return undefined;
|
||||
}
|
||||
return getHeaderModelName(...candidates);
|
||||
}
|
||||
|
||||
/** Both names occupy one grid cell so the slot is sized by the longer of the
|
||||
* two and neither reflows the header as they cross over.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ import {
|
|||
import { revealOnRowHoverClasses, messageFooterClasses } from '~/components/Chat/Messages/styles';
|
||||
import { parseWakeupText } from '~/components/Chat/Messages/Content/Parts/wakeup';
|
||||
import Elapsed, { shouldShowElapsed } from '~/components/Chat/Messages/Elapsed';
|
||||
import { getHeaderHoverLabel } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import MessageContent from '~/components/Chat/Messages/Content/MessageContent';
|
||||
import { getHeaderModelName } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import { useLocalize, useMessageActions, useContentMetadata } from '~/hooks';
|
||||
import SiblingSwitch from '~/components/Chat/Messages/SiblingSwitch';
|
||||
import HoverButtons from '~/components/Chat/Messages/HoverButtons';
|
||||
|
|
@ -96,6 +96,7 @@ const MessageRender = memo(function MessageRender({
|
|||
getCanCopy,
|
||||
regenerateMessage,
|
||||
latestMessageDepth,
|
||||
hasConfiguredSender,
|
||||
} = useMessageActions({
|
||||
message: msg,
|
||||
currentEditId,
|
||||
|
|
@ -157,7 +158,8 @@ const MessageRender = memo(function MessageRender({
|
|||
id={msg.messageId}
|
||||
icon={<MessageIcon iconData={iconData} assistant={assistant} agent={agent} />}
|
||||
label={messageLabel ?? ''}
|
||||
hoverLabel={getHeaderModelName(
|
||||
hoverLabel={getHeaderHoverLabel(
|
||||
hasConfiguredSender,
|
||||
agent?.model,
|
||||
assistant?.model,
|
||||
msg.model,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { render, screen } from '@testing-library/react';
|
||||
import HeaderLabel, { getHeaderModelName } from '../HeaderLabel';
|
||||
import HeaderLabel, { getHeaderHoverLabel, getHeaderModelName } from '../HeaderLabel';
|
||||
|
||||
describe('getHeaderModelName', () => {
|
||||
it('prefers a real model over an agent document id', () => {
|
||||
|
|
@ -21,6 +21,20 @@ describe('getHeaderModelName', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('getHeaderHoverLabel', () => {
|
||||
it('falls through to the model when the sender is not a configured label', () => {
|
||||
expect(getHeaderHoverLabel(false, 'agent_abc', 'gemma4:12b-it-qat')).toBe('gemma4:12b-it-qat');
|
||||
expect(getHeaderHoverLabel(undefined, 'gemma4:12b-it-qat')).toBe('gemma4:12b-it-qat');
|
||||
});
|
||||
|
||||
/* A preset or model spec that configured the sender has chosen what the header
|
||||
says; the hover swap must not reveal the model it stands in for. */
|
||||
it('withholds the model when the sender is a configured label', () => {
|
||||
expect(getHeaderHoverLabel(true, 'z-ai/glm-5.3-flash')).toBeUndefined();
|
||||
expect(getHeaderHoverLabel(true, 'agent_abc', 'z-ai/glm-5.3-flash')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('HeaderLabel', () => {
|
||||
it('renders only the provider when there is no model to show', () => {
|
||||
render(<HeaderLabel label="Ollama" />);
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@ import {
|
|||
getMessageAriaLabel,
|
||||
} from '~/utils';
|
||||
import { revealOnRowHoverClasses, messageFooterClasses } from '~/components/Chat/Messages/styles';
|
||||
import { useAttachments, useLocalize, useMessageActions, useContentMetadata } from '~/hooks';
|
||||
import { useLocalize, useAttachments, useMessageActions, useContentMetadata } from '~/hooks';
|
||||
import ToolCallLimitNotice from '~/components/Chat/Messages/Content/ToolCallLimitNotice';
|
||||
import AuthorHeader from '~/components/Chat/Messages/Content/Parts/AuthorHeader';
|
||||
import Elapsed, { shouldShowElapsed } from '~/components/Chat/Messages/Elapsed';
|
||||
import { getHeaderModelName } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import { getHeaderHoverLabel } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import ContentParts from '~/components/Chat/Messages/Content/ContentParts';
|
||||
import SiblingSwitch from '~/components/Chat/Messages/SiblingSwitch';
|
||||
import HoverButtons from '~/components/Chat/Messages/HoverButtons';
|
||||
|
|
@ -100,6 +100,7 @@ const ContentRender = memo(function ContentRender({
|
|||
getCanCopy,
|
||||
regenerateMessage,
|
||||
latestMessageDepth,
|
||||
hasConfiguredSender,
|
||||
} = useMessageActions({
|
||||
message: msg,
|
||||
searchResults,
|
||||
|
|
@ -158,7 +159,8 @@ const ContentRender = memo(function ContentRender({
|
|||
id={msg.messageId}
|
||||
icon={<MessageIcon iconData={iconData} assistant={assistant} agent={agent} />}
|
||||
label={messageLabel ?? ''}
|
||||
hoverLabel={getHeaderModelName(
|
||||
hoverLabel={getHeaderHoverLabel(
|
||||
hasConfiguredSender,
|
||||
agent?.model,
|
||||
assistant?.model,
|
||||
msg.model,
|
||||
|
|
|
|||
|
|
@ -4,19 +4,20 @@ import SearchContent, {
|
|||
} from '~/components/Chat/Messages/Content/SearchContent';
|
||||
import AuthorHeader from '~/components/Chat/Messages/Content/Parts/AuthorHeader';
|
||||
import MinimalHoverButtons from '~/components/Chat/Messages/MinimalHoverButtons';
|
||||
import { getHeaderHoverLabel } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import MessageContent from '~/components/Chat/Messages/Content/MessageContent';
|
||||
import { getHeaderModelName } from '~/components/Chat/Messages/ui/HeaderLabel';
|
||||
import { getHeaderPrefixForScreenReader, getMessageAriaLabel } from '~/utils';
|
||||
import SiblingSwitch from '~/components/Chat/Messages/SiblingSwitch';
|
||||
import MessageRow from '~/components/Chat/Messages/ui/MessageRow';
|
||||
import { MessageContext, useShareContext } from '~/Providers';
|
||||
import SubRow from '~/components/Chat/Messages/SubRow';
|
||||
import { useAttachments, useLocalize } from '~/hooks';
|
||||
import { MessageContext } from '~/Providers';
|
||||
import MultiMessage from './MultiMessage';
|
||||
import Icon from './MessageIcon';
|
||||
|
||||
export default function Message(props: TMessageProps) {
|
||||
const localize = useLocalize();
|
||||
const { hasConfiguredSender } = useShareContext();
|
||||
const {
|
||||
message,
|
||||
siblingIdx,
|
||||
|
|
@ -59,7 +60,7 @@ export default function Message(props: TMessageProps) {
|
|||
id={messageId}
|
||||
icon={<Icon message={message} conversation={conversation} />}
|
||||
label={messageLabel}
|
||||
hoverLabel={getHeaderModelName(message.model)}
|
||||
hoverLabel={getHeaderHoverLabel(hasConfiguredSender, message.model)}
|
||||
timestamp={message.createdAt ?? message.clientTimestamp}
|
||||
ariaLabel={getMessageAriaLabel(message, localize)}
|
||||
headerPrefix={getHeaderPrefixForScreenReader(message, localize)}
|
||||
|
|
|
|||
|
|
@ -240,7 +240,9 @@ function SharedView() {
|
|||
);
|
||||
|
||||
return (
|
||||
<ShareContext.Provider value={{ isSharedConvo: true, shareId }}>
|
||||
<ShareContext.Provider
|
||||
value={{ isSharedConvo: true, shareId, hasConfiguredSender: data?.hasConfiguredSender }}
|
||||
>
|
||||
<AppChatSurface>
|
||||
<div className="relative flex h-screen w-full overflow-hidden dark:bg-surface-secondary">
|
||||
<main className="relative flex w-full grow overflow-hidden dark:bg-surface-secondary">
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import {
|
|||
getTagByKey,
|
||||
isAgentsEndpoint,
|
||||
SearchResultData,
|
||||
isConfiguredSender,
|
||||
toMinimalFeedback,
|
||||
isAssistantsEndpoint,
|
||||
TUpdateFeedbackRequest,
|
||||
|
|
@ -190,6 +191,15 @@ export default function useMessageActions(props: TMessageActions) {
|
|||
enterEdit,
|
||||
conversation,
|
||||
messageLabel,
|
||||
/** Whether `messageLabel` is a configured sender, so the header can withhold the
|
||||
* model it stands in for. */
|
||||
hasConfiguredSender: isConfiguredSender({
|
||||
sender: message?.sender,
|
||||
endpoint: message?.endpoint ?? conversation?.endpoint,
|
||||
endpointType: conversation?.endpointType,
|
||||
model: message?.model ?? conversation?.model,
|
||||
isCreatedByUser: message?.isCreatedByUser,
|
||||
}),
|
||||
/** Withholding the handler removes the controls: `HoverButtons` renders feedback
|
||||
* only when it has somewhere to send it. */
|
||||
handleFeedback: feedbackEnabled ? handleFeedback : undefined,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
import { useCallback, useMemo } from 'react';
|
||||
import throttle from 'lodash/throttle';
|
||||
import { isAssistantsEndpoint, isAgentsEndpoint } from 'librechat-data-provider';
|
||||
import {
|
||||
isAssistantsEndpoint,
|
||||
isAgentsEndpoint,
|
||||
isConfiguredSender,
|
||||
} from 'librechat-data-provider';
|
||||
import type { SearchResultData } from 'librechat-data-provider';
|
||||
import type { TMessageProps } from '~/common';
|
||||
import { useMessagesViewContext, useAssistantsMapContext, useAgentsMapContext } from '~/Providers';
|
||||
|
|
@ -108,6 +112,15 @@ export default function useMessageHelpers(
|
|||
getCanCopy,
|
||||
enterEdit,
|
||||
conversation,
|
||||
/** Whether the header's label is a configured sender, so it can withhold the model
|
||||
* it stands in for. */
|
||||
hasConfiguredSender: isConfiguredSender({
|
||||
sender: message?.sender,
|
||||
endpoint: message?.endpoint ?? conversation?.endpoint,
|
||||
endpointType: conversation?.endpointType,
|
||||
model: message?.model ?? conversation?.model,
|
||||
isCreatedByUser: message?.isCreatedByUser,
|
||||
}),
|
||||
isSubmitting,
|
||||
handleScroll,
|
||||
handleContinue,
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import {
|
|||
parseCompactConvo,
|
||||
replaceSpecialVars,
|
||||
getEphemeralSender,
|
||||
getResponseSender,
|
||||
isConfiguredSender,
|
||||
encodeEphemeralAgentId,
|
||||
parseEphemeralAgentId,
|
||||
} from '../src/parsers';
|
||||
|
|
@ -819,3 +821,121 @@ describe('getEphemeralSender', () => {
|
|||
expect(getEphemeralSender({ modelLabel: '', specLabel: 'Spec Label' })).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isConfiguredSender', () => {
|
||||
const gptSender = getResponseSender({ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' });
|
||||
|
||||
test('is false without a sender to judge', () => {
|
||||
expect(isConfiguredSender({ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' })).toBe(false);
|
||||
expect(isConfiguredSender({ sender: '', endpoint: EModelEndpoint.openAI })).toBe(false);
|
||||
});
|
||||
|
||||
test('is false for the model-derived name the endpoint produces', () => {
|
||||
expect(
|
||||
isConfiguredSender({ sender: gptSender, endpoint: EModelEndpoint.openAI, model: 'gpt-4o' }),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isConfiguredSender({
|
||||
sender: 'Claude',
|
||||
endpoint: EModelEndpoint.anthropic,
|
||||
model: 'claude-5',
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isConfiguredSender({ sender: 'Gemini', endpoint: EModelEndpoint.google, model: 'gemini-3' }),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test('is true for a label standing in for the model', () => {
|
||||
expect(
|
||||
isConfiguredSender({ sender: 'Acme', endpoint: EModelEndpoint.openAI, model: 'gpt-4o' }),
|
||||
).toBe(true);
|
||||
expect(
|
||||
isConfiguredSender({ sender: 'Acme', endpoint: EModelEndpoint.anthropic, model: 'claude-5' }),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
/* An endpoint that ignores the label writes the model-derived name as the sender, so
|
||||
equality settles the gating without listing which endpoints honour what. */
|
||||
test('follows the sender an endpoint actually wrote', () => {
|
||||
const chatGptLabel = 'Acme';
|
||||
const openAI = { endpoint: EModelEndpoint.openAI, model: 'gpt-4o', chatGptLabel };
|
||||
const anthropic = { endpoint: EModelEndpoint.anthropic, model: 'claude-5', chatGptLabel };
|
||||
|
||||
expect(isConfiguredSender({ ...openAI, sender: getResponseSender(openAI) })).toBe(true);
|
||||
expect(isConfiguredSender({ ...anthropic, sender: getResponseSender(anthropic) })).toBe(false);
|
||||
});
|
||||
|
||||
/* A custom endpoint's `endpoint` is its own configured name, and `getResponseSender`
|
||||
reaches its heuristics only through `endpointType`. */
|
||||
test('reads an unrecognized endpoint as a custom one', () => {
|
||||
expect(
|
||||
isConfiguredSender({ sender: gptSender, endpoint: 'Together AI', model: 'gpt-4o' }),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isConfiguredSender({ sender: 'Together', endpoint: 'Together AI', model: 'gpt-4o' }),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
/* An agent or assistant is named by its author and `getResponseSender` has no branch
|
||||
for it, so the header shows that name whether or not the response stored a sender. */
|
||||
test('is true for an agent or assistant, stored sender or not', () => {
|
||||
expect(isConfiguredSender({ sender: 'My Agent', endpoint: EModelEndpoint.agents })).toBe(true);
|
||||
expect(
|
||||
isConfiguredSender({ sender: 'My Assistant', endpoint: EModelEndpoint.assistants }),
|
||||
).toBe(true);
|
||||
expect(isConfiguredSender({ endpoint: EModelEndpoint.agents, model: 'gpt-4o' })).toBe(true);
|
||||
});
|
||||
|
||||
/* A user turn is headed by the person who wrote it: no model to withhold, and its
|
||||
`User` sender would never match a derived name. */
|
||||
test('is false for a user turn whatever it carries', () => {
|
||||
expect(
|
||||
isConfiguredSender({
|
||||
sender: 'User',
|
||||
endpoint: EModelEndpoint.openAI,
|
||||
model: 'gpt-4o',
|
||||
isCreatedByUser: true,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isConfiguredSender({
|
||||
sender: 'User',
|
||||
endpoint: EModelEndpoint.agents,
|
||||
isCreatedByUser: true,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
/* An endpoint that cannot be named says nothing either way, and reading that silence
|
||||
as "configured" would withhold the model from every unlabelled row it reached — one
|
||||
such row disables the hover for a whole shared transcript. */
|
||||
test('is false when there is no endpoint to derive a name from', () => {
|
||||
expect(isConfiguredSender({ sender: 'GPT-4o', model: 'gpt-4o' })).toBe(false);
|
||||
expect(isConfiguredSender({ sender: 'Acme', model: 'gpt-4o' })).toBe(false);
|
||||
});
|
||||
|
||||
/* The invariant the helper exists to hold: true exactly when the sender the app wrote
|
||||
is one of the configured labels rather than a name derived from the model. */
|
||||
test('agrees with the sender chain on every label source', () => {
|
||||
const cases = [
|
||||
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' },
|
||||
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', modelLabel: 'Acme' },
|
||||
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', specLabel: 'Acme' },
|
||||
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', modelDisplayLabel: 'Acme' },
|
||||
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', chatGptLabel: 'Acme' },
|
||||
{ endpoint: EModelEndpoint.anthropic, model: 'claude-5', chatGptLabel: 'Acme' },
|
||||
{ endpoint: EModelEndpoint.anthropic, model: 'claude-5', modelLabel: 'Acme' },
|
||||
{ endpoint: 'Together AI', endpointType: EModelEndpoint.custom, model: 'qwen' },
|
||||
];
|
||||
for (const endpointOption of cases) {
|
||||
const { modelLabel, specLabel, modelDisplayLabel } = endpointOption as Record<string, string>;
|
||||
/** Mirrors `resolveSender`: the label chain first, `getResponseSender` behind it. */
|
||||
const sender =
|
||||
getEphemeralSender({ modelLabel, specLabel, modelDisplayLabel }) ||
|
||||
getResponseSender(endpointOption as never);
|
||||
|
||||
expect(isConfiguredSender({ ...(endpointOption as never), sender })).toBe(sender === 'Acme');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import {
|
|||
openRouterSchema,
|
||||
googleSchema,
|
||||
EModelEndpoint,
|
||||
isAgentsEndpoint,
|
||||
isAssistantsEndpoint,
|
||||
Providers,
|
||||
anthropicSchema,
|
||||
assistantSchema,
|
||||
|
|
@ -506,6 +508,72 @@ export function getEphemeralSender({
|
|||
return modelLabel ?? specLabel ?? modelDisplayLabel ?? '';
|
||||
}
|
||||
|
||||
/** Built-in endpoints; anything else in `endpoint` is a custom endpoint's own name. */
|
||||
const builtInEndpoints = new Set<string>(Object.values(EModelEndpoint));
|
||||
|
||||
/**
|
||||
* Whether a persisted `sender` is a label someone configured rather than the
|
||||
* model-derived name `getResponseSender` produces.
|
||||
*
|
||||
* The sender is the one thing that records which it was: `resolveSender` writes an
|
||||
* agent's name, then the `getEphemeralSender` chain (`modelLabel` → a model spec's
|
||||
* `label` → an endpoint's `modelDisplayLabel`), and falls back to `getResponseSender`
|
||||
* only when none of those is set. Asking the message rather than the conversation
|
||||
* settles three things a settings lookup cannot: labels that live in config a caller
|
||||
* may not hold, labels an endpoint ignores (Anthropic keeps reading `Claude` whatever
|
||||
* `chatGptLabel` says, and matches here), and labels changed since the message was
|
||||
* written — its header still shows the sender it was written under.
|
||||
*
|
||||
* Equality is the test, so the only way to be wrong is a stored name that no longer
|
||||
* matches what the current heuristics produce, which withholds a model rather than
|
||||
* revealing one.
|
||||
*/
|
||||
export const isConfiguredSender = ({
|
||||
sender,
|
||||
endpoint,
|
||||
endpointType,
|
||||
model,
|
||||
isCreatedByUser,
|
||||
}: {
|
||||
sender?: string | null;
|
||||
endpoint?: EModelEndpoint | string | null;
|
||||
endpointType?: EModelEndpoint | string | null;
|
||||
model?: string | null;
|
||||
isCreatedByUser?: boolean | null;
|
||||
}): boolean => {
|
||||
/** A user turn is headed by the person who wrote it, so it has no model to withhold
|
||||
* and its `User` sender would never match a derived name. */
|
||||
if (isCreatedByUser === true) {
|
||||
return false;
|
||||
}
|
||||
/** Agents and assistants are named by whoever authored them: the header shows that
|
||||
* name whether or not the response stored a sender, and `getResponseSender` has no
|
||||
* branch to derive one. */
|
||||
if (isAgentsEndpoint(endpoint) || isAssistantsEndpoint(endpoint)) {
|
||||
return true;
|
||||
}
|
||||
if (sender == null || sender === '') {
|
||||
return false;
|
||||
}
|
||||
/** A custom endpoint carries its own configured name in `endpoint`, which
|
||||
* `getResponseSender` recognizes only through `endpointType`. */
|
||||
const resolvedType =
|
||||
endpointType ??
|
||||
(endpoint != null && !builtInEndpoints.has(endpoint) ? EModelEndpoint.custom : undefined);
|
||||
const derived = getResponseSender({
|
||||
endpoint: endpoint as EModelEndpoint,
|
||||
endpointType: resolvedType as EModelEndpoint,
|
||||
model,
|
||||
});
|
||||
/** An endpoint this cannot name — an older message stored without one, say — says
|
||||
* nothing either way, and reading that silence as "configured" would withhold the
|
||||
* model from every unlabelled row it reached. */
|
||||
if (derived === '') {
|
||||
return false;
|
||||
}
|
||||
return sender !== derived;
|
||||
};
|
||||
|
||||
/**
|
||||
* Encodes an ephemeral agent ID from endpoint, model, optional sender, and optional index.
|
||||
* Uses __ to replace : (reserved in graph node names) and ___ to separate sender.
|
||||
|
|
|
|||
|
|
@ -475,6 +475,9 @@ export type TPinConversationResponse = TConversation;
|
|||
export type TSharedMessagesResponse = Omit<TSharedLink, 'messages'> & {
|
||||
messages: TMessage[];
|
||||
langfuseSessionUrl?: string;
|
||||
/** Whether the link was published with a configured sender label; withholds the
|
||||
* model on hover. */
|
||||
hasConfiguredSender?: boolean;
|
||||
};
|
||||
|
||||
export type TCreateShareLinkRequest = Pick<TConversation, 'conversationId'>;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
import { nanoid } from 'nanoid';
|
||||
import mongoose from 'mongoose';
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server';
|
||||
import { Constants, ContentTypes, Tools } from 'librechat-data-provider';
|
||||
import {
|
||||
Tools,
|
||||
Constants,
|
||||
ContentTypes,
|
||||
EModelEndpoint,
|
||||
getResponseSender,
|
||||
} from 'librechat-data-provider';
|
||||
import type { SchemaWithMeiliMethods } from '~/models/plugins/mongoMeili';
|
||||
import type * as t from '~/types';
|
||||
import {
|
||||
|
|
@ -81,6 +87,7 @@ describe('Share Methods', () => {
|
|||
default: undefined,
|
||||
},
|
||||
model: String,
|
||||
sender: String,
|
||||
iconURL: String,
|
||||
endpoint: String,
|
||||
conversationSignature: String,
|
||||
|
|
@ -104,6 +111,10 @@ describe('Share Methods', () => {
|
|||
conversationId: { type: String, required: true },
|
||||
title: String,
|
||||
user: String,
|
||||
endpoint: String,
|
||||
endpointType: String,
|
||||
modelLabel: String,
|
||||
chatGptLabel: String,
|
||||
},
|
||||
{ timestamps: true },
|
||||
);
|
||||
|
|
@ -513,6 +524,136 @@ describe('Share Methods', () => {
|
|||
});
|
||||
|
||||
describe('getSharedMessages', () => {
|
||||
/** Publishes through `createSharedLink` with one response whose stored `sender` is
|
||||
* what the header will render, so what a viewer reads back is the published flag. */
|
||||
const publishWithSender = async (sender?: string, endpoint = EModelEndpoint.openAI) => {
|
||||
const userId = new mongoose.Types.ObjectId().toString();
|
||||
const conversationId = `conv_${nanoid()}`;
|
||||
await Conversation.create({ conversationId, user: userId, title: 'Shared', endpoint });
|
||||
await Message.create({
|
||||
messageId: `msg_${nanoid()}`,
|
||||
conversationId,
|
||||
user: userId,
|
||||
text: 'Reply',
|
||||
isCreatedByUser: false,
|
||||
endpoint,
|
||||
model: 'gpt-4o',
|
||||
sender,
|
||||
parentMessageId: Constants.NO_PARENT,
|
||||
});
|
||||
const { shareId } = await shareMethods.createSharedLink(userId, conversationId);
|
||||
return { userId, conversationId, shareId };
|
||||
};
|
||||
|
||||
/* The header renders the stored `sender`, so a link publishing a label must not swap
|
||||
it for the model the label stands in for. */
|
||||
test('flags a link whose messages show a configured sender', async () => {
|
||||
const { shareId } = await publishWithSender('Acme Assistant');
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
expect(result?.hasConfiguredSender).toBe(true);
|
||||
});
|
||||
|
||||
test('leaves a model-derived sender unflagged', async () => {
|
||||
const { shareId } = await publishWithSender(
|
||||
getResponseSender({ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' }),
|
||||
);
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
expect(result).toBeDefined();
|
||||
expect(result).not.toHaveProperty('hasConfiguredSender');
|
||||
});
|
||||
|
||||
/* Anthropic's sender ignores `chatGptLabel` and writes 'Claude', so the model has to
|
||||
stay reachable — the stored sender says so without the settings being consulted. */
|
||||
test('leaves an endpoint that wrote its own name unflagged', async () => {
|
||||
const { shareId } = await publishWithSender('Claude', EModelEndpoint.anthropic);
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
expect(result).not.toHaveProperty('hasConfiguredSender');
|
||||
});
|
||||
|
||||
test('publishes no label text of its own', async () => {
|
||||
const { shareId } = await publishWithSender('Acme Assistant');
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
const senders = result?.messages.map((message) => message.sender);
|
||||
expect(senders).toContain('Acme Assistant');
|
||||
/** Only the messages carry it, where the content preflight inspects it. */
|
||||
expect(result?.hasConfiguredSender).toBe(true);
|
||||
});
|
||||
|
||||
/* The conversation's settings changing cannot start revealing the model on a link
|
||||
that is already public: the messages it published keep the sender they were
|
||||
written under, and nothing about the link is re-derived from the conversation. */
|
||||
test('is unmoved by the conversation changing after the link is published', async () => {
|
||||
const { userId, conversationId, shareId } = await publishWithSender('Acme Assistant');
|
||||
await Conversation.updateOne({ conversationId, user: userId }, { $set: { modelLabel: '' } });
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
expect(result?.hasConfiguredSender).toBe(true);
|
||||
});
|
||||
|
||||
/* One older message stored without an endpoint cannot be named, and reading that
|
||||
silence as a label would disable the hover for the whole transcript. */
|
||||
test('is unmoved by a message stored without an endpoint', async () => {
|
||||
const userId = new mongoose.Types.ObjectId().toString();
|
||||
const conversationId = `conv_${nanoid()}`;
|
||||
await Conversation.create({ conversationId, user: userId, title: 'Legacy' });
|
||||
await Message.create({
|
||||
messageId: `msg_${nanoid()}`,
|
||||
conversationId,
|
||||
user: userId,
|
||||
text: 'Reply',
|
||||
isCreatedByUser: false,
|
||||
model: 'gpt-4o',
|
||||
sender: 'GPT-4o',
|
||||
parentMessageId: Constants.NO_PARENT,
|
||||
});
|
||||
const { shareId } = await shareMethods.createSharedLink(userId, conversationId);
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
expect(result).not.toHaveProperty('hasConfiguredSender');
|
||||
});
|
||||
|
||||
/* Only messages the link actually publishes have a say. */
|
||||
test('ignores a labelled message outside the shared branch', async () => {
|
||||
const userId = new mongoose.Types.ObjectId().toString();
|
||||
const conversationId = `conv_${nanoid()}`;
|
||||
const modelSender = getResponseSender({ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' });
|
||||
await Conversation.create({ conversationId, user: userId, title: 'Branched' });
|
||||
const root = await Message.create({
|
||||
messageId: `msg_${nanoid()}`,
|
||||
conversationId,
|
||||
user: userId,
|
||||
text: 'Shared reply',
|
||||
isCreatedByUser: false,
|
||||
endpoint: EModelEndpoint.openAI,
|
||||
model: 'gpt-4o',
|
||||
sender: modelSender,
|
||||
parentMessageId: Constants.NO_PARENT,
|
||||
});
|
||||
await Message.create({
|
||||
messageId: `msg_${nanoid()}`,
|
||||
conversationId,
|
||||
user: userId,
|
||||
text: 'Later reply',
|
||||
isCreatedByUser: false,
|
||||
endpoint: EModelEndpoint.openAI,
|
||||
model: 'gpt-4o',
|
||||
sender: 'Acme Assistant',
|
||||
parentMessageId: root.messageId,
|
||||
});
|
||||
const { shareId } = await shareMethods.createSharedLink(
|
||||
userId,
|
||||
conversationId,
|
||||
root.messageId,
|
||||
);
|
||||
|
||||
const result = await shareMethods.getSharedMessages(shareId);
|
||||
expect(result).not.toHaveProperty('hasConfiguredSender');
|
||||
});
|
||||
|
||||
test('should retrieve and anonymize shared messages', async () => {
|
||||
const userId = new mongoose.Types.ObjectId().toString();
|
||||
const conversationId = `conv_${nanoid()}`;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
import { nanoid } from 'nanoid';
|
||||
import { Types } from 'mongoose';
|
||||
import { Constants, ContentTypes, FileSources, Tools } from 'librechat-data-provider';
|
||||
import {
|
||||
Tools,
|
||||
Constants,
|
||||
ContentTypes,
|
||||
FileSources,
|
||||
isConfiguredSender,
|
||||
} from 'librechat-data-provider';
|
||||
import type { FilterQuery, Model } from 'mongoose';
|
||||
import type { SchemaWithMeiliMethods } from '~/models/plugins/mongoMeili';
|
||||
import type * as t from '~/types';
|
||||
|
|
@ -340,6 +346,24 @@ function encodeSharedLinksCursor(link: t.ISharedLink, sortBy: string): string {
|
|||
return Buffer.from(JSON.stringify(composite)).toString('base64');
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this message shows a label rather than a model-derived name.
|
||||
*
|
||||
* The public header renders each message's stored `sender`, which froze at whatever the
|
||||
* sender chain produced when it was written, so the messages — not the conversation's
|
||||
* current settings — decide whether a link may reveal a model. A label cleared after the
|
||||
* responses were written, or one that lives in config this package never reads, both
|
||||
* answer correctly here.
|
||||
*/
|
||||
function showsConfiguredSender(message: t.IMessage): boolean {
|
||||
return isConfiguredSender({
|
||||
sender: message.sender,
|
||||
endpoint: message.endpoint,
|
||||
model: message.model,
|
||||
isCreatedByUser: message.isCreatedByUser,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit a lazy snapshot backfill only while the link still has none. An owner can
|
||||
* republish the same shareId while a viewer's first read is in flight, and an
|
||||
|
|
@ -497,13 +521,17 @@ function anonymizeMessages(
|
|||
includeFiles: boolean,
|
||||
anonymizeMessageId: (id: string) => string,
|
||||
anonymizeAssistantId: (id: string) => string,
|
||||
): t.SharedMessage[] {
|
||||
): { messages: t.SharedMessage[]; hasConfiguredSender: boolean } {
|
||||
if (!Array.isArray(messages)) {
|
||||
return [];
|
||||
return { messages: [], hasConfiguredSender: false };
|
||||
}
|
||||
|
||||
/** Collected in this pass rather than a second one over the same array: shared
|
||||
* transcripts can be long and this is their visible loading path. */
|
||||
let hasConfiguredSender = false;
|
||||
const idMap = new Map<string, string>();
|
||||
return messages.map((message) => {
|
||||
const shared = messages.map((message) => {
|
||||
hasConfiguredSender = hasConfiguredSender || showsConfiguredSender(message);
|
||||
const newMessageId = anonymizeMessageId(message.messageId);
|
||||
idMap.set(message.messageId, newMessageId);
|
||||
|
||||
|
|
@ -586,6 +614,8 @@ function anonymizeMessages(
|
|||
...(attachments && { attachments }),
|
||||
};
|
||||
});
|
||||
|
||||
return { messages: shared, hasConfiguredSender };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -874,11 +904,12 @@ export function createShareMethods(mongoose: typeof import('mongoose')): {
|
|||
const perLinkEnabled = share.snapshotFiles !== false;
|
||||
const includeFiles = adminEnabled && perLinkEnabled;
|
||||
let fileSnapshots = share.fileSnapshots;
|
||||
let shouldPersistFileSnapshots = false;
|
||||
if (includeFiles && fileSnapshots === undefined && share._id) {
|
||||
const shouldPersistFileSnapshots =
|
||||
includeFiles && fileSnapshots === undefined && share._id != null;
|
||||
if (shouldPersistFileSnapshots) {
|
||||
fileSnapshots = await buildFileSnapshots(mongoose, messagesToShare, share.user);
|
||||
shouldPersistFileSnapshots = true;
|
||||
}
|
||||
|
||||
const snapshotIds = includeFiles
|
||||
? new Set<string>((fileSnapshots ?? []).map((snapshot) => snapshot.file_id))
|
||||
: new Set<string>();
|
||||
|
|
@ -889,22 +920,26 @@ export function createShareMethods(mongoose: typeof import('mongoose')): {
|
|||
.map((snapshot) => snapshot.file_id),
|
||||
)
|
||||
: new Set<string>();
|
||||
/** The share view has no conversation in scope, so whether this link may reveal a
|
||||
* model travels in the payload — read off the very messages being returned. */
|
||||
const { messages, hasConfiguredSender } = anonymizeMessages(
|
||||
messagesToShare,
|
||||
newConvoId,
|
||||
resolvedShareId,
|
||||
snapshotIds,
|
||||
textSourceIds,
|
||||
includeFiles,
|
||||
anonymizeMessageId,
|
||||
anonymizeAssistantId,
|
||||
);
|
||||
const result: t.SharedMessagesResult = {
|
||||
shareId: resolvedShareId,
|
||||
title: share.title,
|
||||
...(hasConfiguredSender ? { hasConfiguredSender: true } : {}),
|
||||
createdAt: share.createdAt,
|
||||
updatedAt: share.updatedAt,
|
||||
conversationId: newConvoId,
|
||||
messages: anonymizeMessages(
|
||||
messagesToShare,
|
||||
newConvoId,
|
||||
resolvedShareId,
|
||||
snapshotIds,
|
||||
textSourceIds,
|
||||
includeFiles,
|
||||
anonymizeMessageId,
|
||||
anonymizeAssistantId,
|
||||
),
|
||||
messages,
|
||||
};
|
||||
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -114,6 +114,9 @@ export interface SharedMessagesResult {
|
|||
messages: Array<SharedMessage>;
|
||||
shareId: string;
|
||||
title?: string;
|
||||
/** Whether the shared messages show a configured sender label, so the share view can
|
||||
* withhold the model on hover as the chat view does. */
|
||||
hasConfiguredSender?: boolean;
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue