2024-07-12 15:25:41 +00:00
|
|
|
import type { Message } from 'ai';
|
2024-08-01 14:54:59 +00:00
|
|
|
import React, { type RefCallback } from 'react';
|
2024-07-10 16:44:39 +00:00
|
|
|
import { ClientOnly } from 'remix-utils/client-only';
|
2024-08-01 14:54:59 +00:00
|
|
|
import { Menu } from '~/components/sidebar/Menu.client';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { IconButton } from '~/components/ui/IconButton';
|
|
|
|
import { Workbench } from '~/components/workbench/Workbench.client';
|
|
|
|
import { classNames } from '~/utils/classNames';
|
2024-07-12 15:25:41 +00:00
|
|
|
import { Messages } from './Messages.client';
|
2024-07-10 16:44:39 +00:00
|
|
|
import { SendButton } from './SendButton.client';
|
|
|
|
|
|
|
|
interface BaseChatProps {
|
2024-08-01 14:54:59 +00:00
|
|
|
textareaRef?: React.RefObject<HTMLTextAreaElement> | undefined;
|
2024-07-24 13:47:48 +00:00
|
|
|
messageRef?: RefCallback<HTMLDivElement> | undefined;
|
|
|
|
scrollRef?: RefCallback<HTMLDivElement> | undefined;
|
2024-07-10 16:44:39 +00:00
|
|
|
chatStarted?: boolean;
|
2024-07-12 15:25:41 +00:00
|
|
|
isStreaming?: boolean;
|
|
|
|
messages?: Message[];
|
2024-07-10 16:44:39 +00:00
|
|
|
enhancingPrompt?: boolean;
|
|
|
|
promptEnhanced?: boolean;
|
|
|
|
input?: string;
|
2024-07-18 09:10:12 +00:00
|
|
|
handleStop?: () => void;
|
2024-08-01 14:54:59 +00:00
|
|
|
sendMessage?: (event: React.UIEvent, messageInput?: string) => void;
|
2024-07-10 16:44:39 +00:00
|
|
|
handleInputChange?: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
|
|
|
enhancePrompt?: () => void;
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:54:59 +00:00
|
|
|
const EXAMPLE_PROMPTS = [
|
|
|
|
{ text: 'Build a todo app in React using Tailwind' },
|
|
|
|
{ text: 'Build a simple blog using Astro' },
|
|
|
|
{ text: 'Create a cookie consent form using Material UI' },
|
|
|
|
{ text: 'Make a space invaders game' },
|
2024-08-01 15:31:18 +00:00
|
|
|
{ text: 'How do I center a div?' },
|
2024-08-01 14:54:59 +00:00
|
|
|
];
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-08-08 13:56:36 +00:00
|
|
|
const TEXTAREA_MIN_HEIGHT = 76;
|
2024-07-10 16:44:39 +00:00
|
|
|
|
|
|
|
export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
textareaRef,
|
2024-07-24 13:47:48 +00:00
|
|
|
messageRef,
|
|
|
|
scrollRef,
|
2024-07-10 16:44:39 +00:00
|
|
|
chatStarted = false,
|
2024-07-12 15:25:41 +00:00
|
|
|
isStreaming = false,
|
2024-07-10 16:44:39 +00:00
|
|
|
enhancingPrompt = false,
|
|
|
|
promptEnhanced = false,
|
2024-07-12 15:25:41 +00:00
|
|
|
messages,
|
2024-07-10 16:44:39 +00:00
|
|
|
input = '',
|
|
|
|
sendMessage,
|
|
|
|
handleInputChange,
|
|
|
|
enhancePrompt,
|
2024-07-18 09:10:12 +00:00
|
|
|
handleStop,
|
2024-07-10 16:44:39 +00:00
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const TEXTAREA_MAX_HEIGHT = chatStarted ? 400 : 200;
|
|
|
|
|
|
|
|
return (
|
2024-08-08 13:56:36 +00:00
|
|
|
<div ref={ref} className="relative flex h-full w-full overflow-hidden bg-bolt-elements-background-depth-1">
|
2024-07-31 21:21:40 +00:00
|
|
|
<ClientOnly>{() => <Menu />}</ClientOnly>
|
2024-07-24 13:47:48 +00:00
|
|
|
<div ref={scrollRef} className="flex overflow-scroll w-full h-full">
|
2024-08-01 14:54:59 +00:00
|
|
|
<div className="flex flex-col w-full h-full px-6">
|
2024-07-10 16:44:39 +00:00
|
|
|
{!chatStarted && (
|
2024-08-01 14:54:59 +00:00
|
|
|
<div id="intro" className="mt-[26vh] max-w-2xl mx-auto">
|
2024-08-08 13:56:36 +00:00
|
|
|
<h1 className="text-5xl text-center font-bold text-bolt-elements-textPrimary mb-2">
|
|
|
|
Where ideas begin
|
|
|
|
</h1>
|
|
|
|
<p className="mb-4 text-center text-bolt-elements-textSecondary">
|
|
|
|
Bring ideas to life in seconds or get help on existing projects.
|
|
|
|
</p>
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div
|
2024-08-01 14:54:59 +00:00
|
|
|
className={classNames('pt-6', {
|
2024-07-12 15:25:41 +00:00
|
|
|
'h-full flex flex-col': chatStarted,
|
|
|
|
})}
|
2024-07-10 16:44:39 +00:00
|
|
|
>
|
2024-07-12 15:25:41 +00:00
|
|
|
<ClientOnly>
|
|
|
|
{() => {
|
|
|
|
return chatStarted ? (
|
|
|
|
<Messages
|
2024-07-24 13:47:48 +00:00
|
|
|
ref={messageRef}
|
2024-08-08 13:56:36 +00:00
|
|
|
className="flex flex-col w-full flex-1 max-w-2xl px-4 pb-6 mx-auto z-1"
|
2024-07-12 15:25:41 +00:00
|
|
|
messages={messages}
|
|
|
|
isStreaming={isStreaming}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
}}
|
|
|
|
</ClientOnly>
|
|
|
|
<div
|
2024-08-01 14:54:59 +00:00
|
|
|
className={classNames('relative w-full max-w-2xl md:mx-auto z-2', {
|
2024-07-17 18:54:46 +00:00
|
|
|
'sticky bottom-0': chatStarted,
|
2024-07-12 15:25:41 +00:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div
|
2024-07-17 18:54:46 +00:00
|
|
|
className={classNames(
|
2024-08-08 13:56:36 +00:00
|
|
|
'shadow-sm border border-bolt-elements-borderColor bg-bolt-elements-prompt-background backdrop-filter backdrop-blur-[8px] rounded-lg overflow-hidden',
|
2024-07-17 18:54:46 +00:00
|
|
|
)}
|
2024-07-12 15:25:41 +00:00
|
|
|
>
|
|
|
|
<textarea
|
|
|
|
ref={textareaRef}
|
2024-08-08 13:56:36 +00:00
|
|
|
className={`w-full pl-4 pt-4 pr-16 focus:outline-none resize-none text-md text-bolt-elements-textPrimary placeholder-bolt-elements-textTertiary bg-transparent`}
|
2024-07-12 15:25:41 +00:00
|
|
|
onKeyDown={(event) => {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
if (event.shiftKey) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-07-12 15:25:41 +00:00
|
|
|
event.preventDefault();
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-07-25 15:28:23 +00:00
|
|
|
sendMessage?.(event);
|
2024-07-12 15:25:41 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
value={input}
|
|
|
|
onChange={(event) => {
|
|
|
|
handleInputChange?.(event);
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
minHeight: TEXTAREA_MIN_HEIGHT,
|
|
|
|
maxHeight: TEXTAREA_MAX_HEIGHT,
|
|
|
|
}}
|
|
|
|
placeholder="How can Bolt help you today?"
|
|
|
|
translate="no"
|
|
|
|
/>
|
2024-07-18 09:10:12 +00:00
|
|
|
<ClientOnly>
|
|
|
|
{() => (
|
|
|
|
<SendButton
|
|
|
|
show={input.length > 0 || isStreaming}
|
|
|
|
isStreaming={isStreaming}
|
2024-07-25 15:28:23 +00:00
|
|
|
onClick={(event) => {
|
2024-07-18 09:10:12 +00:00
|
|
|
if (isStreaming) {
|
|
|
|
handleStop?.();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-25 15:28:23 +00:00
|
|
|
sendMessage?.(event);
|
2024-07-18 09:10:12 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ClientOnly>
|
2024-07-12 15:25:41 +00:00
|
|
|
<div className="flex justify-between text-sm p-4 pt-2">
|
|
|
|
<div className="flex gap-1 items-center">
|
|
|
|
<IconButton
|
2024-08-08 13:56:36 +00:00
|
|
|
title="Enhance prompt"
|
2024-07-12 15:25:41 +00:00
|
|
|
disabled={input.length === 0 || enhancingPrompt}
|
|
|
|
className={classNames({
|
|
|
|
'opacity-100!': enhancingPrompt,
|
2024-08-08 13:56:36 +00:00
|
|
|
'text-bolt-elements-item-contentAccent! pr-1.5 enabled:hover:bg-bolt-elements-item-backgroundAccent!':
|
|
|
|
promptEnhanced,
|
2024-07-12 15:25:41 +00:00
|
|
|
})}
|
|
|
|
onClick={() => enhancePrompt?.()}
|
|
|
|
>
|
|
|
|
{enhancingPrompt ? (
|
|
|
|
<>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="i-svg-spinners:90-ring-with-bg text-bolt-elements-loader-progress text-xl"></div>
|
2024-07-12 15:25:41 +00:00
|
|
|
<div className="ml-1.5">Enhancing prompt...</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="i-bolt:stars text-xl"></div>
|
2024-07-12 15:25:41 +00:00
|
|
|
{promptEnhanced && <div className="ml-1.5">Prompt enhanced</div>}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
{input.length > 3 ? (
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="text-xs text-bolt-elements-textTertiary">
|
|
|
|
Use <kbd className="kdb">Shift</kbd> + <kbd className="kdb">Return</kbd> for a new line
|
2024-07-12 15:25:41 +00:00
|
|
|
</div>
|
|
|
|
) : null}
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
2024-07-12 15:25:41 +00:00
|
|
|
</div>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="bg-bolt-elements-background-depth-1 pb-6">{/* Ghost Element */}</div>
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-01 14:54:59 +00:00
|
|
|
{!chatStarted && (
|
2024-08-08 13:56:36 +00:00
|
|
|
<div id="examples" className="relative w-full max-w-2xl mx-auto mt-8 flex justify-center">
|
|
|
|
<div className="flex flex-col space-y-2 [mask-image:linear-gradient(to_bottom,black_0%,transparent_180%)] hover:[mask-image:none]">
|
2024-08-01 14:54:59 +00:00
|
|
|
{EXAMPLE_PROMPTS.map((examplePrompt, index) => {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
key={index}
|
|
|
|
onClick={(event) => {
|
|
|
|
sendMessage?.(event, examplePrompt.text);
|
|
|
|
}}
|
2024-08-08 13:56:36 +00:00
|
|
|
className="group flex items-center w-full gap-2 justify-center bg-transparent text-bolt-elements-textTertiary hover:text-bolt-elements-textPrimary transition-theme"
|
2024-08-01 14:54:59 +00:00
|
|
|
>
|
|
|
|
{examplePrompt.text}
|
|
|
|
<div className="i-ph:arrow-bend-down-left" />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
2024-07-18 21:07:04 +00:00
|
|
|
<ClientOnly>{() => <Workbench chatStarted={chatStarted} isStreaming={isStreaming} />}</ClientOnly>
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|