Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tk/issue14 #41

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions src/SessionMessages/SessionMessage/MessageResponseRecommended.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { ChatContext } from '@/ChatContext';
import { Slot } from '@radix-ui/react-slot';
import { motion } from 'framer-motion';
import { cn } from 'reablocks';
import { FC, PropsWithChildren, useContext } from 'react';
import { Markdown } from '@/Markdown';
import { PluggableList } from 'react-markdown/lib';

export interface MessageResponseRecommendedProps extends PropsWithChildren {
/**
* Follow-up response to render (array of values).
*/
followUpResponse: string[];

/**
* Whether the response is loading.
*/
isLoading?: boolean;

/**
* Function to handle clicks on the follow-up response.
*/
onClickFollowUpResponse?: (followUpResponse: string) => void;
}

// Helper function to check if the URL is a valid image URL
const isImageUrl = (url: string) => {
return /\.(jpeg|jpg|gif|png|svg|webp)$/i.test(url);
};

export const MessageResponseRecommended: FC<
MessageResponseRecommendedProps
> = ({ followUpResponse, isLoading, children, onClickFollowUpResponse }) => {
const { theme, isCompact, remarkPlugins } = useContext(ChatContext);
const Comp = children ? Slot : 'div';

return (
<Comp
data-compact={isCompact}
className={cn(theme.messages.message.recommended)}
>
{children || (
<>
{followUpResponse.map((responseItem, index) => (
<div
key={index}
onClick={() => onClickFollowUpResponse?.(responseItem)}
>
{isImageUrl(responseItem) ? (
<figure>
<img
src={responseItem}
alt={responseItem}
className={cn(theme.messages.message.rimage)}
/>
</figure>
) : (
<div className={cn(theme.messages.message.markdownBorder)}>
<Markdown remarkPlugins={remarkPlugins as PluggableList[]}>
{responseItem}
</Markdown>
</div>
)}
</div>
))}

{isLoading && (
<motion.div
className={cn(theme.messages.message.cursor)}
animate={{ opacity: [1, 0] }}
transition={{
duration: 0.7,
repeat: Infinity,
repeatType: 'reverse'
}}
/>
)}
</>
)}
</Comp>
);
};
22 changes: 18 additions & 4 deletions src/SessionMessages/SessionMessage/SessionMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MessageQuestion } from './MessageQuestion';
import { MessageResponse } from './MessageResponse';
import { MessageSources } from './MessageSources';
import { MessageActions } from './MessageActions';
import { MessageResponseRecommended } from './MessageResponseRecommended';

const messageVariants = {
hidden: {
Expand Down Expand Up @@ -41,17 +42,32 @@ export const SessionMessage: FC<SessionMessageProps> = ({
children
}) => {
const { theme, isLoading } = useContext(ChatContext);
const hasFollowUpResponse =
Array.isArray(conversation.followUpResponse) &&
conversation.followUpResponse.length > 0;

return (
<motion.div key={conversation.id} variants={messageVariants}>
<Card className={cn(theme.messages.message.base)}>
{children || (
<>
<MessageQuestion question={conversation.question} files={conversation.files} />
<MessageQuestion
question={conversation.question}
files={conversation.files}
/>

<MessageResponse
response={conversation.response}
isLoading={isLast && isLoading}
/>

{hasFollowUpResponse && (
<MessageResponseRecommended
followUpResponse={conversation.followUpResponse}
isLoading={isLast && isLoading}
/>
)}
Copy link
Contributor

@steppy452 steppy452 Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here instead of an either or here, we would always show the <MessageResponse /> and optionally show <FollowUpResponse /> if some exist

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steppy452 Thank you for clarifying the requirements. Please review the changes I have made. Also, regarding the image, what are we expecting the image payload to be? Currently, I have it as a URL.


<MessageSources sources={conversation.sources} />
<MessageActions
question={conversation.question}
Expand All @@ -60,9 +76,7 @@ export const SessionMessage: FC<SessionMessageProps> = ({
</>
)}
</Card>
{!isLast && (
<Divider />
)}
{!isLast && <Divider />}
</motion.div>
);
};
1 change: 1 addition & 0 deletions src/SessionMessages/SessionMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './MessageFiles';
export * from './MessageQuestion';
export * from './MessageResponse';
export * from './MessageSources';
export * from './MessageResponseRecommended';
20 changes: 17 additions & 3 deletions src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export interface ChatTheme {
cursor: string;
overlay: string;
expand: string;
recommended: string;
rimage: string;
markdownBorder: string;
files: {
base: string;
file: {
Expand Down Expand Up @@ -132,10 +135,20 @@ export const chatTheme: ChatTheme = {
'relative font-semibold mb-4 px-4 py-4 pb-2 rounded-3xl rounded-br-none text-typography border bg-gray-200 border-gray-300 text-gray-900',
'dark:bg-gray-900/60 dark:border-gray-700/50 dark:text-gray-100'
].join(' '),
response: ['relative data-[compact=false]:px-4 text-gray-900', 'dark:text-gray-100'].join(' '),
overlay: `overflow-y-hidden max-h-[350px] after:content-[''] after:absolute after:inset-x-0 after:bottom-0 after:h-16 after:bg-gradient-to-b after:from-transparent dark:after:to-gray-900 after:to-gray-200`,
response: [
'relative data-[compact=false]:px-4 text-gray-900',
'dark:text-gray-100'
].join(' '),
overlay:
"overflow-y-hidden max-h-[350px] after:content-[''] after:absolute after:inset-x-0 after:bottom-0 after:h-16 after:bg-gradient-to-b after:from-transparent dark:after:to-gray-900 after:to-gray-200",
cursor: 'inline-block w-1 h-4 bg-current',
expand: 'absolute bottom-1 right-1 z-10',
recommended: 'flex gap-4 justify-around mt-8',
rimage: ' max-w-[250px] max-h-[250px] object-cover cursor-pointer',
markdownBorder: [
'relative font-semibold mb-4 px-4 py-4 pb-2 rounded-4xl rounded-[10px] cursor-pointer text-typography border bg-gray-200 border-gray-300 text-gray-900',
'dark:bg-gray-900/60 dark:border-gray-700/50 dark:text-gray-100 '
].join(' '),
files: {
base: 'mb-2 flex flex-wrap gap-3 ',
file: {
Expand Down Expand Up @@ -167,7 +180,8 @@ export const chatTheme: ChatTheme = {
th: 'px-4 py-2 text-left font-bold border-b border-gray-500',
td: 'px-4 py-2',
code: 'm-2 rounded-b relative',
toolbar: 'text-xs dark:bg-gray-700/50 flex items-center justify-between px-2 py-1 rounded-t sticky top-0 backdrop-blur-md bg-gray-200 ',
toolbar:
'text-xs dark:bg-gray-700/50 flex items-center justify-between px-2 py-1 rounded-t sticky top-0 backdrop-blur-md bg-gray-200 ',
li: 'mb-2 ml-6',
ul: 'mb-4 list-disc',
ol: 'mb-4 list-decimal'
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export interface Conversation {
* The AI's response to the user's question
*/
response?: string;
/**
* The AI's follow-upresponse to the user's question
*/

followUpResponse?: string[];

/**
* Array of sources referenced in the conversation
Expand Down
38 changes: 37 additions & 1 deletion stories/Console.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import {
fakeSessionsWithEmbeds,
sessionWithSources,
sessionsWithFiles,
sessionsWithPartialConversation
sessionsWithPartialConversation,
sessionWithMessageResponseRecommended
} from './examples';

export default {
Expand Down Expand Up @@ -1093,3 +1094,38 @@ export const ImageFiles = () => {
</div>
);
};

export const FollowUpResponses = () => {
return (
<div
className="dark:bg-gray-950 bg-white"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
padding: 20,
margin: 20,
borderRadius: 5
}}
>
<Chat
viewType="console"
sessions={sessionWithMessageResponseRecommended}
activeSessionId="session-sources"
>
<SessionsList>
<NewSessionButton />
<SessionGroups />
</SessionsList>

<SessionMessagePanel>
<SessionMessagesHeader />
<SessionMessages />
<ChatInput />
</SessionMessagePanel>
</Chat>
</div>
);
};
43 changes: 43 additions & 0 deletions stories/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,46 @@ export const sessionsWithPartialConversation: Session[] = [
]
}
];

export const sessionWithMessageResponseRecommended: Session[] = [
{
id: 'session-1',
title: 'Session with Image',
createdAt: subHours(new Date(), 1),
updatedAt: new Date(),
conversations: [
{
id: 'conversation-1',
question: 'What are the benefits of using React?',
createdAt: new Date(),
response:
'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1024px-React-icon.svg.png',
followUpResponse: [
'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1024px-React-icon.svg.png',
'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1024px-React-icon.svg.png'
],
updatedAt: new Date()
}
]
},
{
id: 'session-2',
title: 'Session with Text',
createdAt: subHours(new Date(), 1),
updatedAt: new Date(),
conversations: [
{
id: 'conversation-2',
question: 'What are the benefits of using React?',
createdAt: new Date(),
response:
'React benefits include a declarative coding style, component-based architecture, virtual DOM, and a large community and ecosystem.',
followUpResponse: [
'What are some downsides of React?',
'What are alternative options to React?'
],
updatedAt: new Date()
}
]
}
];
Loading