= {}): Messa
setRememberOption: () => undefined,
}),
useForkConversation: () => null,
+ useMessageCopy: () => ({ copyToClipboard: () => undefined, canCopy: false }),
useScrolling: () => ({
contentRef: { current: null },
scrollableRef: { current: null },
diff --git a/packages/chat/src/components.ts b/packages/chat/src/components.ts
index 29682f7493..b09a5f4e0d 100644
--- a/packages/chat/src/components.ts
+++ b/packages/chat/src/components.ts
@@ -82,6 +82,7 @@ export { isValidTimestamp, getMessageTimestamp } from './components/messages/tim
export type { MessageTimestamp as MessageTimestampParts } from './components/messages/time';
export { default as Fork } from './components/messages/Fork';
export { default as Feedback } from './components/messages/Feedback';
+export { default as MinimalHoverButtons } from './components/messages/MinimalHoverButtons';
/** Parts: the leaf components and row classes tool cards share. */
export { default as Stdout } from './components/parts/Stdout';
diff --git a/client/src/components/Chat/Messages/MinimalHoverButtons.tsx b/packages/chat/src/components/messages/MinimalHoverButtons.tsx
similarity index 59%
rename from client/src/components/Chat/Messages/MinimalHoverButtons.tsx
rename to packages/chat/src/components/messages/MinimalHoverButtons.tsx
index c99eb3f1c0..a752946f94 100644
--- a/client/src/components/Chat/Messages/MinimalHoverButtons.tsx
+++ b/packages/chat/src/components/messages/MinimalHoverButtons.tsx
@@ -1,16 +1,11 @@
-import { useState, useMemo } from 'react';
+import { useState } from 'react';
import { Copy, Check } from 'lucide';
-import { revealOnRowHoverClasses } from '@librechat/chat/components';
-import { Button, MorphIcon, TooltipAnchor } from '@librechat/client';
+import { cn, Button, MorphIcon, useLocalize, TooltipAnchor } from '@librechat/client';
import type { TMessage, SearchResultData } from 'librechat-data-provider';
-import type { MarkdownVariant } from '~/utils/richtext';
-import {
- useLocalize,
- useCopyMessageToClipboard,
- getMessageClipboardSource,
- hasCopyableText,
-} from '~/hooks';
-import { cn } from '~/utils';
+import type { ReactNode } from 'react';
+import type { MarkdownVariant } from '../../react/messages/host';
+import { useMessagesHost } from '../../react/messages/host';
+import { revealOnRowHoverClasses } from './styles';
type THoverButtons = {
message: TMessage;
@@ -19,19 +14,15 @@ type THoverButtons = {
variant?: MarkdownVariant;
};
-export default function MinimalHoverButtons({ message, searchResults, variant }: THoverButtons) {
+export default function MinimalHoverButtons({
+ message,
+ searchResults,
+ variant,
+}: THoverButtons): ReactNode {
const localize = useLocalize();
+ const { useMessageCopy } = useMessagesHost();
const [isCopied, setIsCopied] = useState(false);
- const clipboardSource = useMemo(() => getMessageClipboardSource(message), [message]);
- const copyToClipboard = useCopyMessageToClipboard({
- ...clipboardSource,
- searchResults,
- variant: clipboardSource.variant ?? variant,
- });
- const canCopy = useMemo(
- () => hasCopyableText({ ...clipboardSource, searchResults }),
- [clipboardSource, searchResults],
- );
+ const { copyToClipboard, canCopy } = useMessageCopy({ message, searchResults, variant });
return (
diff --git a/packages/chat/src/components/messages/__tests__/MinimalHoverButtons.spec.tsx b/packages/chat/src/components/messages/__tests__/MinimalHoverButtons.spec.tsx
new file mode 100644
index 0000000000..6a385d2a2d
--- /dev/null
+++ b/packages/chat/src/components/messages/__tests__/MinimalHoverButtons.spec.tsx
@@ -0,0 +1,49 @@
+/**
+ * @jest-environment jsdom
+ */
+import { fireEvent, render, screen } from '@testing-library/react';
+import type { TMessage } from 'librechat-data-provider';
+import type { MessageCopy, MessageCopyInput } from '../../../react/messages/host';
+import { createMessagesHost } from '../../../__fixtures__/messagesHost';
+import { MessagesHostProvider } from '../../../react/messages/host';
+import MinimalHoverButtons from '../MinimalHoverButtons';
+
+jest.mock('@librechat/client', () => ({
+ ...jest.requireActual('@librechat/client'),
+ useLocalize: () => (key: string) => key,
+}));
+
+const message = { messageId: 'm1', text: 'Shared result.', isCreatedByUser: false } as TMessage;
+
+function renderWithCopy(useMessageCopy: (input: MessageCopyInput) => MessageCopy) {
+ const host = createMessagesHost({ useMessageCopy });
+ return render(
+
+
+ ,
+ );
+}
+
+describe('MinimalHoverButtons', () => {
+ it('asks the host to copy the row and shows the copied state it reports', () => {
+ const useMessageCopy = jest.fn(() => ({
+ copyToClipboard: (setIsCopied) => setIsCopied(true),
+ canCopy: true,
+ }));
+ renderWithCopy(useMessageCopy);
+
+ expect(useMessageCopy).toHaveBeenCalledWith({
+ message,
+ searchResults: undefined,
+ variant: 'lite',
+ });
+ fireEvent.click(screen.getByRole('button', { name: 'com_ui_copy_to_clipboard' }));
+ expect(screen.getByRole('button', { name: 'com_ui_copied_to_clipboard' })).toBeInTheDocument();
+ });
+
+ it('disables the copy control when the host cannot copy', () => {
+ renderWithCopy(() => ({ copyToClipboard: jest.fn(), canCopy: false }));
+
+ expect(screen.getByRole('button', { name: 'com_ui_copy_to_clipboard' })).toBeDisabled();
+ });
+});
diff --git a/packages/chat/src/react.ts b/packages/chat/src/react.ts
index 7bdc9c52ad..46305e91cd 100644
--- a/packages/chat/src/react.ts
+++ b/packages/chat/src/react.ts
@@ -125,6 +125,8 @@ export { composerLiftFamily } from './react/lift';
export { showSkillsPopoverFamily } from './react/skills';
export { useComposerOverlay, composerOverlayCountFamily } from './react/overlay';
export { default as useTimeTick } from './react/tick';
+export { default as useGenerationsByLatest } from './react/messages/generations';
+export type { Generations, TUseGenerations } from './react/messages/generations';
export {
useMessagesHost,
MessagesHostProvider,
@@ -133,7 +135,10 @@ export {
} from './react/messages/host';
export type {
ForkEvents,
+ MessageCopy,
MessagesHost,
+ MarkdownVariant,
+ MessageCopyInput,
ForkPreferences,
ForkConversation,
MessagesScrolling,
diff --git a/client/src/hooks/__tests__/useGenerationsByLatest.spec.ts b/packages/chat/src/react/messages/__tests__/generations.spec.ts
similarity index 92%
rename from client/src/hooks/__tests__/useGenerationsByLatest.spec.ts
rename to packages/chat/src/react/messages/__tests__/generations.spec.ts
index f281edf8b4..c40d3ddaa9 100644
--- a/client/src/hooks/__tests__/useGenerationsByLatest.spec.ts
+++ b/packages/chat/src/react/messages/__tests__/generations.spec.ts
@@ -1,5 +1,5 @@
import { Constants, EModelEndpoint } from 'librechat-data-provider';
-import useGenerationsByLatest from '../useGenerationsByLatest';
+import useGenerationsByLatest from '../generations';
const base = {
endpoint: EModelEndpoint.agents,
diff --git a/client/src/hooks/useGenerationsByLatest.ts b/packages/chat/src/react/messages/generations.ts
similarity index 92%
rename from client/src/hooks/useGenerationsByLatest.ts
rename to packages/chat/src/react/messages/generations.ts
index 422cfc7f49..061c7413a8 100644
--- a/client/src/hooks/useGenerationsByLatest.ts
+++ b/packages/chat/src/react/messages/generations.ts
@@ -1,6 +1,6 @@
import { Constants, EModelEndpoint, isAssistantsEndpoint } from 'librechat-data-provider';
-type TUseGenerations = {
+export type TUseGenerations = {
error?: boolean;
endpoint?: string;
messageId?: string;
@@ -25,6 +25,16 @@ type TUseGenerations = {
isUserInitiatedCompaction?: boolean;
};
+/** Which rerun, edit, continue and fork controls a message row offers. */
+export type Generations = {
+ forkingSupported: boolean;
+ continueSupported: boolean;
+ regenerateEnabled: boolean;
+ isActiveStreamingMessage: boolean;
+ isEditableEndpoint: boolean;
+ hideEditButton: boolean;
+};
+
export default function useGenerationsByLatest({
error = false,
endpoint,
@@ -38,7 +48,7 @@ export default function useGenerationsByLatest({
getHasEditablePart,
parentIsUserMessage,
isUserInitiatedCompaction = false,
-}: TUseGenerations) {
+}: TUseGenerations): Generations {
const isEditableEndpoint = Boolean(
[
EModelEndpoint.openAI,
diff --git a/packages/chat/src/react/messages/host.tsx b/packages/chat/src/react/messages/host.tsx
index 147a5a45b6..d0683bb144 100644
--- a/packages/chat/src/react/messages/host.tsx
+++ b/packages/chat/src/react/messages/host.tsx
@@ -4,9 +4,10 @@ import type {
TMessage,
Assistant,
TConversation,
+ SearchResultData,
TForkConvoRequest,
} from 'librechat-data-provider';
-import type { MouseEvent, ReactNode, RefObject } from 'react';
+import type { Dispatch, ReactNode, MouseEvent, RefObject, SetStateAction } from 'react';
/** The scroll machinery of the message list, as the app's scrolling hook drives it. */
export type MessagesScrolling = {
@@ -51,6 +52,24 @@ export type ForkEvents = {
/** Starts a fork of a conversation. */
export type ForkConversation = (request: TForkConvoRequest, events: ForkEvents) => void;
+/** The renderer a message is displayed with: `full` for model text, `lite` for user markdown. */
+export type MarkdownVariant = 'full' | 'lite';
+
+/** What a row asks the host to copy. */
+export type MessageCopyInput = {
+ message: TMessage;
+ /** Search results the message cites, resolved into the copied text. */
+ searchResults?: { [key: string]: SearchResultData };
+ /** The renderer the row displayed the message with, when it is not the authorship default. */
+ variant?: MarkdownVariant;
+};
+
+/** A row's copy action: `copyToClipboard` flips `setIsCopied` on while the copied state shows. */
+export type MessageCopy = {
+ copyToClipboard: (setIsCopied: Dispatch>) => void;
+ canCopy: boolean;
+};
+
/** 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<
@@ -83,7 +102,8 @@ export interface MessagesHost {
useForkPreferences: () => ForkPreferences;
/** Forks a conversation and opens the copy; `null` where forking is unavailable. */
useForkConversation: () => ForkConversation | null;
-
+ /** Copies a message the way the app renders it; `canCopy` is `false` where copying is unavailable. */
+ useMessageCopy: (input: MessageCopyInput) => MessageCopy;
/**
* Drives the list's scrolling: follows the stream, lands on a conversation's end and keeps
* the reader's position.
@@ -111,6 +131,8 @@ export interface MessagesHost {
const noop = (): void => undefined;
+const fallbackMessageCopy: MessageCopy = { copyToClipboard: noop, canCopy: false };
+
const fallbackForkPreferences: ForkPreferences = {
option: '',
setOption: noop,
@@ -137,13 +159,14 @@ function useInertScrolling(): MessagesScrolling {
const ignoreNearBottom: ReportNearBottom = () => undefined;
-/** The host rows read outside any provider before an app registers one: locale clock, no slots, no forking. */
+/** The host rows read outside any provider before an app registers one: locale clock, no slots,
+ * no forking, no copying, inert scrolling. */
const fallbackHost: MessagesHost = {
useHour12: () => undefined,
renderFiles: () => null,
useForkPreferences: () => fallbackForkPreferences,
useForkConversation: () => null,
-
+ useMessageCopy: () => fallbackMessageCopy,
useScrolling: useInertScrolling,
useScreenshotTarget: () => useRef(null),
useAutoScroll: () => false,
diff --git a/packages/chat/test/consumer.spec.tsx b/packages/chat/test/consumer.spec.tsx
index a5f6174375..90985aba83 100644
--- a/packages/chat/test/consumer.spec.tsx
+++ b/packages/chat/test/consumer.spec.tsx
@@ -214,6 +214,7 @@ const messagesHost: MessagesHost = {
setRememberOption: () => undefined,
}),
useForkConversation: () => null,
+ useMessageCopy: () => ({ copyToClipboard: () => undefined, canCopy: false }),
useScrolling: () => ({
contentRef: { current: null },
scrollableRef: { current: null },