2024-07-12 15:25:41 +00:00
|
|
|
import type { Message } from 'ai';
|
2024-07-10 16:44:39 +00:00
|
|
|
import type { LegacyRef } from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
import { ClientOnly } from 'remix-utils/client-only';
|
2024-07-17 18:54:46 +00:00
|
|
|
import { classNames } from '../../utils/classNames';
|
|
|
|
import { IconButton } from '../ui/IconButton';
|
|
|
|
import { Workbench } from '../workbench/Workbench.client';
|
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 {
|
|
|
|
textareaRef?: LegacyRef<HTMLTextAreaElement> | undefined;
|
|
|
|
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-07-10 16:44:39 +00:00
|
|
|
sendMessage?: () => void;
|
|
|
|
handleInputChange?: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
|
|
|
enhancePrompt?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EXAMPLES = [{ text: 'Example' }, { text: 'Example' }, { text: 'Example' }, { text: 'Example' }];
|
|
|
|
|
|
|
|
const TEXTAREA_MIN_HEIGHT = 72;
|
|
|
|
|
|
|
|
export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
textareaRef,
|
|
|
|
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-07-12 15:25:41 +00:00
|
|
|
<div ref={ref} className="relative flex h-full w-full overflow-hidden ">
|
|
|
|
<div className="flex overflow-scroll w-full h-full">
|
|
|
|
<div id="chat" className="flex flex-col w-full h-full px-6">
|
2024-07-10 16:44:39 +00:00
|
|
|
{!chatStarted && (
|
2024-07-12 15:25:41 +00:00
|
|
|
<div id="intro" className="mt-[20vh] mb-14 max-w-3xl mx-auto">
|
2024-07-10 16:44:39 +00:00
|
|
|
<h2 className="text-4xl text-center font-bold text-slate-800 mb-2">Where ideas begin.</h2>
|
|
|
|
<p className="mb-14 text-center">Bring ideas to life in seconds or get help on existing projects.</p>
|
2024-07-12 15:25:41 +00:00
|
|
|
<div className="grid max-md:grid-cols-[repeat(1,1fr)] md:grid-cols-[repeat(2,minmax(300px,1fr))] gap-4">
|
2024-07-10 16:44:39 +00:00
|
|
|
{EXAMPLES.map((suggestion, index) => (
|
|
|
|
<button key={index} className="p-4 rounded-lg shadow-xs bg-white border border-gray-200 text-left">
|
|
|
|
{suggestion.text}
|
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div
|
2024-07-12 15:25:41 +00:00
|
|
|
className={classNames('pt-10', {
|
|
|
|
'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
|
|
|
|
className="flex flex-col w-full flex-1 max-w-3xl px-4 pb-10 mx-auto z-1"
|
|
|
|
messages={messages}
|
|
|
|
isStreaming={isStreaming}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
}}
|
|
|
|
</ClientOnly>
|
|
|
|
<div
|
|
|
|
className={classNames('relative w-full max-w-3xl 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(
|
|
|
|
'shadow-sm border border-gray-200 bg-white/85 backdrop-filter backdrop-blur-[8px] rounded-lg overflow-hidden',
|
|
|
|
)}
|
2024-07-12 15:25:41 +00:00
|
|
|
>
|
|
|
|
<textarea
|
|
|
|
ref={textareaRef}
|
2024-07-17 18:54:46 +00:00
|
|
|
className={`w-full pl-4 pt-4 pr-16 focus:outline-none resize-none 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-12 15:25:41 +00:00
|
|
|
sendMessage?.();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
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}
|
|
|
|
onClick={() => {
|
|
|
|
if (isStreaming) {
|
|
|
|
handleStop?.();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage?.();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</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 icon="i-ph:microphone-duotone" className="-ml-1" />
|
|
|
|
<IconButton icon="i-ph:plus-circle-duotone" />
|
|
|
|
<IconButton icon="i-ph:pencil-simple-duotone" />
|
|
|
|
<IconButton
|
|
|
|
disabled={input.length === 0 || enhancingPrompt}
|
|
|
|
className={classNames({
|
|
|
|
'opacity-100!': enhancingPrompt,
|
|
|
|
'text-accent! pr-1.5 enabled:hover:bg-accent/12!': promptEnhanced,
|
|
|
|
})}
|
|
|
|
onClick={() => enhancePrompt?.()}
|
|
|
|
>
|
|
|
|
{enhancingPrompt ? (
|
|
|
|
<>
|
|
|
|
<div className="i-svg-spinners:90-ring-with-bg text-black text-xl"></div>
|
|
|
|
<div className="ml-1.5">Enhancing prompt...</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="i-blitz:stars text-xl"></div>
|
|
|
|
{promptEnhanced && <div className="ml-1.5">Prompt enhanced</div>}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
{input.length > 3 ? (
|
|
|
|
<div className="text-xs">
|
|
|
|
Use <kbd className="bg-gray-100 p-1 rounded-md">Shift</kbd> +{' '}
|
|
|
|
<kbd className="bg-gray-100 p-1 rounded-md">Return</kbd> for a new line
|
|
|
|
</div>
|
|
|
|
) : null}
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
2024-07-12 15:25:41 +00:00
|
|
|
</div>
|
2024-07-17 18:54:46 +00:00
|
|
|
<div className="bg-white pb-6">{/* Ghost Element */}</div>
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-07-17 18:54:46 +00:00
|
|
|
<ClientOnly>{() => <Workbench chatStarted={chatStarted} />}</ClientOnly>
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|