Skip to content

Commit

Permalink
feat: MessageRuntime.unstable_getCopyText API (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 14, 2024
1 parent 0c8277e commit cf6861c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-mayflies-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

refactor!: simplify SpeechSynthesisAdapter to accept a text string
6 changes: 6 additions & 0 deletions packages/react/src/api/MessageRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ContentPartStatus,
ToolCallContentPartStatus,
} from "../types/AssistantTypes";
import { getThreadMessageText } from "../utils/getThreadMessageText";
import {
AttachmentRuntime,
AttachmentState,
Expand Down Expand Up @@ -128,6 +129,7 @@ export type MessageRuntime = {
position?: "previous" | "next" | undefined;
branchId?: string | undefined;
}): void;
unstable_getCopyText(): string;

subscribe(callback: () => void): Unsubscribe;

Expand Down Expand Up @@ -223,6 +225,10 @@ export class MessageRuntimeImpl implements MessageRuntime {
this._threadBinding.getState().switchToBranch(targetBranch);
}

public unstable_getCopyText() {
return getThreadMessageText(this.getState());
}

public subscribe(callback: () => void) {
return this._core.subscribe(callback);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/primitive-hooks/actionBar/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export {
/**
* @deprecated Use `useMessageRuntime().copy()` instead. This will be removed in 0.6.
* @deprecated Use `useMessageRuntime().unstable_getCopyText()` instead. This will be removed in 0.6.
*/
useActionBarCopy,
/**
* @deprecated Use `useMessageRuntime().copy()` instead. This will be removed in 0.6.
* @deprecated Use `useMessageRuntime().unstable_getCopyText()` instead. This will be removed in 0.6.
*/
type UseActionBarCopyProps,
} from "./useActionBarCopy";
Expand Down
29 changes: 11 additions & 18 deletions packages/react/src/primitive-hooks/actionBar/useActionBarCopy.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useCallback } from "react";
import {
useMessage,
useMessageRuntime,
useMessageUtilsStore,
useMessageUtils,
} from "../../context/react/MessageContext";
import { useCombinedStore } from "../../utils/combined/useCombinedStore";
import { getThreadMessageText } from "../../utils/getThreadMessageText";
import { useComposerRuntime } from "../../context";

export type UseActionBarCopyProps = {
Expand All @@ -16,32 +15,26 @@ export const useActionBarCopy = ({
}: UseActionBarCopyProps = {}) => {
const messageRuntime = useMessageRuntime();
const composerRuntime = useComposerRuntime();
const messageUtilsStore = useMessageUtilsStore();
const hasCopyableContent = useCombinedStore(
[messageRuntime, composerRuntime],
(message, c) => {
return (
!c.isEditing &&
(message.role !== "assistant" || message.status.type !== "running") &&
message.content.some((c) => c.type === "text" && c.text.length > 0)
);
},
);
const setIsCopied = useMessageUtils((s) => s.setIsCopied);
const hasCopyableContent = useMessage((message) => {
return (
(message.role !== "assistant" || message.status.type !== "running") &&
message.content.some((c) => c.type === "text" && c.text.length > 0)
);
});

const callback = useCallback(() => {
const message = messageRuntime.getState();
const { setIsCopied } = messageUtilsStore.getState();
const { isEditing, text: composerValue } = composerRuntime.getState();

const valueToCopy = isEditing
? composerValue
: getThreadMessageText(message);
: messageRuntime.unstable_getCopyText();

navigator.clipboard.writeText(valueToCopy).then(() => {
setIsCopied(true);
setTimeout(() => setIsCopied(false), copiedDuration);
});
}, [messageRuntime, messageUtilsStore, composerRuntime, copiedDuration]);
}, [messageRuntime, setIsCopied, composerRuntime, copiedDuration]);

if (!hasCopyableContent) return null;
return callback;
Expand Down

0 comments on commit cf6861c

Please sign in to comment.