Skip to content

Commit

Permalink
fix: add factory fn to determine whether a msg is ai generated
Browse files Browse the repository at this point in the history
  • Loading branch information
isekovanic committed Dec 5, 2024
1 parent b52c1df commit 5220352
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 37 deletions.
2 changes: 2 additions & 0 deletions examples/SampleApp/src/screens/ChannelScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useTheme,
useTypingString,
AITypingIndicatorView,
MessageType,
} from 'stream-chat-react-native';
import { Platform, StyleSheet, View } from 'react-native';
import type { StackNavigationProp } from '@react-navigation/stack';
Expand Down Expand Up @@ -158,6 +159,7 @@ export const ChannelScreen: React.FC<ChannelScreenProps> = ({
messageId={messageId}
NetworkDownIndicator={() => null}
thread={selectedThread}
isMessageAIGenerated={(message: MessageType) => message.ai_generated}
>
<ChannelHeader channel={channel} />
<MessageList<StreamChatGenerics>
Expand Down
25 changes: 5 additions & 20 deletions examples/SampleApp/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6837,10 +6837,10 @@ statuses@~1.5.0:
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==

stream-chat-react-native-core@5.43.2:
version "5.43.2"
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.43.2.tgz#b16add60d231509f864d7301ae281c4b4681ff7a"
integrity sha512-pdSaqw1aPHtxH0md7nnw/TLWPIqsb5TKFzGoWYd/cKRAw4pVDvxolSlha8vYSCwz7MmlarfsJPB0TlS9WcGsDQ==
stream-chat-react-native-core@5.44.0:
version "5.44.0"
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.44.0.tgz#2d3f8b8ca8a15b6f15fa03bea099e706e4c88e79"
integrity sha512-gN7/aaDvESPc2s7d6xyCTu4vpiok1HCwcNiHxvNC2NlvL0O5xd/7240L+eoSPhECXTPZvCgVJVEra9CWMM4pXw==
dependencies:
"@gorhom/bottom-sheet" "^4.6.4"
dayjs "1.10.5"
Expand All @@ -6853,7 +6853,7 @@ [email protected]:
path "0.12.7"
react-native-markdown-package "1.8.2"
react-native-url-polyfill "^1.3.0"
stream-chat "8.45.1"
stream-chat "8.46.0"

"stream-chat-react-native-core@link:../../package":
version "0.0.0"
Expand All @@ -6863,21 +6863,6 @@ [email protected]:
version "0.0.0"
uid ""

[email protected]:
version "8.45.1"
resolved "https://registry.yarnpkg.com/stream-chat/-/stream-chat-8.45.1.tgz#001f452520602ebffe45f1634b5f5dd1ff036ec5"
integrity sha512-7OMpL2RHUd+PXSWzhTUAIjjXlI9Oqc4HhUBRfc5i6dK+Ug9S5ertb7RvyzGL5N4ITpq/6ZUAwXtTqRaN9+UUkw==
dependencies:
"@babel/runtime" "^7.16.3"
"@types/jsonwebtoken" "~9.0.0"
"@types/ws" "^7.4.0"
axios "^1.6.0"
base64-js "^1.5.1"
form-data "^4.0.0"
isomorphic-ws "^4.0.1"
jsonwebtoken "~9.0.0"
ws "^7.5.10"

[email protected]:
version "8.46.0"
resolved "https://registry.yarnpkg.com/stream-chat/-/stream-chat-8.46.0.tgz#416b325e05b144d0937a3527d1e622463113d605"
Expand Down
4 changes: 4 additions & 0 deletions package/src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { useCreateTypingContext } from './hooks/useCreateTypingContext';

import { useTargetedMessage } from './hooks/useTargetedMessage';

import { MessageContextValue } from '../../contexts';
import { ChannelContextValue, ChannelProvider } from '../../contexts/channelContext/ChannelContext';
import type { UseChannelStateValue } from '../../contexts/channelsStateContext/useChannelState';
import { useChannelState } from '../../contexts/channelsStateContext/useChannelState';
Expand Down Expand Up @@ -338,6 +339,7 @@ export type ChannelPropsWithContext<
| 'StreamingMessageView'
>
> &
Partial<Pick<MessageContextValue<StreamChatGenerics>, 'isMessageAIGenerated'>> &
Partial<Pick<ThreadContextValue<StreamChatGenerics>, 'allowThreadMessagesInChannel'>> & {
shouldSyncChannel: boolean;
thread: ThreadType<StreamChatGenerics>;
Expand Down Expand Up @@ -532,6 +534,7 @@ const ChannelWithContext = <
InputGiphySearch = InputGiphyCommandInputDefault,
InputReplyStateHeader = InputReplyStateHeaderDefault,
isAttachmentEqual,
isMessageAIGenerated = () => false,
keyboardBehavior,
KeyboardCompatibleView = KeyboardCompatibleViewDefault,
keyboardVerticalOffset,
Expand Down Expand Up @@ -2424,6 +2427,7 @@ const ChannelWithContext = <
InlineDateSeparator,
InlineUnreadIndicator,
isAttachmentEqual,
isMessageAIGenerated,
legacyImageViewerSwipeBehaviour,
markdownRules,
Message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const useCreateMessagesContext = <
InlineDateSeparator,
InlineUnreadIndicator,
isAttachmentEqual,
isMessageAIGenerated,
legacyImageViewerSwipeBehaviour,
markdownRules,
Message,
Expand Down Expand Up @@ -152,6 +153,7 @@ export const useCreateMessagesContext = <
InlineDateSeparator,
InlineUnreadIndicator,
isAttachmentEqual,
isMessageAIGenerated,
legacyImageViewerSwipeBehaviour,
markdownRules,
Message,
Expand Down
19 changes: 14 additions & 5 deletions package/src/components/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,16 @@ export type MessagePropsWithContext<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = Pick<ChannelContextValue<StreamChatGenerics>, 'channel' | 'enforceUniqueReaction' | 'members'> &
Pick<KeyboardContextValue, 'dismissKeyboard'> &
Partial<Omit<MessageContextValue<StreamChatGenerics>, 'groupStyles' | 'message'>> &
Pick<MessageContextValue<StreamChatGenerics>, 'groupStyles' | 'message'> &
Partial<
Omit<
MessageContextValue<StreamChatGenerics>,
'groupStyles' | 'message' | 'isMessageAIGenerated'
>
> &
Pick<
MessageContextValue<StreamChatGenerics>,
'groupStyles' | 'message' | 'isMessageAIGenerated'
> &
Pick<
MessagesContextValue<StreamChatGenerics>,
| 'sendReaction'
Expand Down Expand Up @@ -305,6 +313,7 @@ const MessageWithContext = <
threadList = false,
updateMessage,
} = props;
const isMessageAIGenerated = messagesContext.isMessageAIGenerated;
const { client } = chatContext;
const {
theme: {
Expand Down Expand Up @@ -457,7 +466,7 @@ const MessageWithContext = <
case 'poll':
return !!message.poll_id;
case 'ai_text':
return !!message.ai_generated;
return isMessageAIGenerated(message);
case 'text':
default:
return !!message.text;
Expand Down Expand Up @@ -704,6 +713,7 @@ const MessageWithContext = <
hasReactions,
images: attachments.images,
isEditedMessageOpen,
isMessageAIGenerated,
isMyMessage,
lastGroupMessage: groupStyles?.[0] === 'single' || groupStyles?.[0] === 'bottom',
lastReceivedId,
Expand Down Expand Up @@ -865,8 +875,7 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
prevMessage.text === nextMessage.text &&
prevMessage.pinned === nextMessage.pinned &&
`${prevMessage?.updated_at}` === `${nextMessage?.updated_at}` &&
prevMessage.i18n === nextMessage.i18n &&
prevMessage.ai_generated === nextMessage.ai_generated;
prevMessage.i18n === nextMessage.i18n;

if (!messageEqual) return false;

Expand Down
11 changes: 7 additions & 4 deletions package/src/components/Message/MessageSimple/MessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type MessageContentPropsWithContext<
| 'preventPress'
| 'showMessageStatus'
| 'threadList'
| 'isMessageAIGenerated'
> &
Pick<
MessagesContextValue<StreamChatGenerics>,
Expand Down Expand Up @@ -122,6 +123,7 @@ const MessageContentWithContext = <
Gallery,
groupStyles,
hasReactions,
isMessageAIGenerated,
isMyMessage,
lastGroupMessage,
members,
Expand Down Expand Up @@ -396,15 +398,15 @@ const MessageContentWithContext = <
) : null;
}
case 'ai_text':
return message.ai_generated ? (
return isMessageAIGenerated(message) ? (
<StreamingMessageView
key={`ai_message_text_container_${messageContentOrderIndex}`}
/>
) : null;
case 'text':
default:
return (otherAttachments.length && otherAttachments[0].actions) ||
message.ai_generated ? null : (
isMessageAIGenerated(message) ? null : (
<MessageTextContainer<StreamChatGenerics>
key={`message_text_container_${messageContentOrderIndex}`}
/>
Expand Down Expand Up @@ -493,8 +495,7 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
prevMessage.type === nextMessage.type &&
prevMessage.text === nextMessage.text &&
prevMessage.pinned === nextMessage.pinned &&
prevMessage.i18n === nextMessage.i18n &&
prevMessage.ai_generated === nextMessage.ai_generated;
prevMessage.i18n === nextMessage.i18n;
if (!messageEqual) return false;

const isPrevQuotedMessageTypeDeleted = prevMessage.quoted_message?.type === 'deleted';
Expand Down Expand Up @@ -577,6 +578,7 @@ export const MessageContent = <
groupStyles,
hasReactions,
isEditedMessageOpen,
isMessageAIGenerated,
isMyMessage,
lastGroupMessage,
lastReceivedId,
Expand Down Expand Up @@ -624,6 +626,7 @@ export const MessageContent = <
hasReactions,
isAttachmentEqual,
isEditedMessageOpen,
isMessageAIGenerated,
isMyMessage,
lastGroupMessage,
lastReceivedId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type MessageFooterPropsWithContext<
| 'otherAttachments'
| 'showMessageStatus'
| 'lastGroupMessage'
| 'isMessageAIGenerated'
> &
Pick<
MessagesContextValue<StreamChatGenerics>,
Expand Down Expand Up @@ -94,6 +95,7 @@ const MessageFooterWithContext = <
formattedDate,
isDeleted,
isEditedMessageOpen,
isMessageAIGenerated,
lastGroupMessage,
members,
message,
Expand Down Expand Up @@ -129,7 +131,7 @@ const MessageFooterWithContext = <
return null;
}

const isEdited = isEditedMessage(message);
const isEdited = isEditedMessage(message) && !isMessageAIGenerated(message);

return (
<>
Expand Down Expand Up @@ -267,6 +269,7 @@ export const MessageFooter = <
const {
alignment,
isEditedMessageOpen,
isMessageAIGenerated,
lastGroupMessage,
members,
message,
Expand All @@ -283,6 +286,7 @@ export const MessageFooter = <
alignment,
deletedMessagesVisibilityType,
isEditedMessageOpen,
isMessageAIGenerated,
lastGroupMessage,
members,
message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
prevMessage.type === nextMessage.type &&
prevMessage.text === nextMessage.text &&
prevMessage.i18n === nextMessage.i18n &&
prevMessage.pinned === nextMessage.pinned &&
prevMessage.ai_generated === nextMessage.ai_generated;
prevMessage.pinned === nextMessage.pinned;
if (!messageEqual) return false;

const isPrevQuotedMessageTypeDeleted = prevMessage.quoted_message?.type === 'deleted';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const useCreateMessageContext = <
hasReactions,
images,
isEditedMessageOpen,
isMessageAIGenerated,
isMyMessage,
lastGroupMessage,
lastReceivedId,
Expand Down Expand Up @@ -78,6 +79,7 @@ export const useCreateMessageContext = <
hasReactions,
images,
isEditedMessageOpen,
isMessageAIGenerated,
isMyMessage,
lastGroupMessage,
lastReceivedId,
Expand Down
3 changes: 2 additions & 1 deletion package/src/components/MessageOverlay/MessageOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ const MessageOverlayWithContext = <
const halfScreenHeight = vh(50);

const myMessageTheme = messagesContext?.myMessageTheme;
const isMessageAIGenerated = messagesContext?.isMessageAIGenerated;
const wrapMessageInTheme = clientId === message?.user?.id && !!myMessageTheme;

const [reactionListHeight, setReactionListHeight] = useState(0);
Expand Down Expand Up @@ -462,7 +463,7 @@ const MessageOverlayWithContext = <
case 'text':
default:
return (otherAttachments?.length && otherAttachments[0].actions) ||
message.ai_generated ? null : (
isMessageAIGenerated?.(message) ? null : (
<MessageTextContainer<StreamChatGenerics>
key={`message_text_container_${messageContentOrderIndex}`}
message={message}
Expand Down
4 changes: 4 additions & 0 deletions package/src/contexts/messageContext/MessageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export type MessageContextValue<
images: Attachment<StreamChatGenerics>[];
/** Boolean that determines if the edited message is pressed. */
isEditedMessageOpen: boolean;
/**
* A factory function that determines whether a message is AI generated or not.
*/
isMessageAIGenerated: (message: MessageType<StreamChatGenerics>) => boolean;
/** Whether or not this is the active user's message */
isMyMessage: boolean;
/** Whether or not this is the last message in a group of messages */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export type InputMessageInputContextValue<
* Defaults to and accepts same props as: [AudioRecordingButton](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/MessageInput/components/AudioRecorder/AudioRecordingButton.tsx)
*/
StartAudioRecordingButton: React.ComponentType<AudioRecordingButtonProps<StreamChatGenerics>>;
StopMessageStreamingButton: React.ComponentType<StopMessageStreamingButtonProps>;
StopMessageStreamingButton: React.ComponentType<StopMessageStreamingButtonProps> | null;
/**
* Custom UI component to render upload progress indicator on attachment preview.
*
Expand Down
4 changes: 2 additions & 2 deletions package/src/contexts/messagesContext/MessagesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import type { ReplyProps } from '../../components/Reply/Reply';
import type { FlatList } from '../../native';
import type { DefaultStreamChatGenerics, UnknownType } from '../../types/types';
import type { ReactionData } from '../../utils/utils';
import type { Alignment } from '../messageContext/MessageContext';
import type { Alignment, MessageContextValue } from '../messageContext/MessageContext';
import type { SuggestionCommand } from '../suggestionsContext/SuggestionsContext';
import type { DeepPartial } from '../themeContext/ThemeContext';
import type { Theme } from '../themeContext/utils/theme';
Expand All @@ -73,7 +73,7 @@ export type DeletedMessagesVisibilityType = 'always' | 'never' | 'receiver' | 's

export type MessagesContextValue<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = {
> = Pick<MessageContextValue<StreamChatGenerics>, 'isMessageAIGenerated'> & {
/**
* UI component for Attachment.
* Defaults to: [Attachment](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/Attachment/Attachment.tsx)
Expand Down
2 changes: 1 addition & 1 deletion package/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const isEditedMessage = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
message: MessageType<StreamChatGenerics>,
) => !!message.message_text_updated_at && !message.ai_generated;
) => !!message.message_text_updated_at;

/**
* Default emoji search index for auto complete text input
Expand Down

0 comments on commit 5220352

Please sign in to comment.