-
Notifications
You must be signed in to change notification settings - Fork 329
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
[wip] fix: better chatbot input & bubble #3404
Open
Light2Dark
wants to merge
6
commits into
marimo-team:main
Choose a base branch
from
Light2Dark:better-chat-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−33
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b01da38
render chat bubble w/o breaking formatting
Light2Dark 0e6ff83
use codemirror as input
Light2Dark 4f72fdd
small refactor
Light2Dark d68f247
remove unecc extension
Light2Dark 4b38701
modify comment
Light2Dark dc0803d
small right margin
Light2Dark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,14 @@ import { renderHTML } from "@/plugins/core/RenderHTML"; | |
import { Input } from "@/components/ui/input"; | ||
import { PopoverAnchor } from "@radix-ui/react-popover"; | ||
import { copyToClipboard } from "@/utils/copy"; | ||
import { | ||
type AdditionalCompletions, | ||
PromptInput, | ||
} from "@/components/editor/ai/add-cell-with-ai"; | ||
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror"; | ||
import { useTheme } from "@/theme/useTheme"; | ||
import { moveToEndOfEditor } from "@/core/codemirror/utils"; | ||
import type { Completion } from "@codemirror/autocomplete"; | ||
|
||
interface Props { | ||
prompts: string[]; | ||
|
@@ -60,18 +68,19 @@ interface Props { | |
} | ||
|
||
export const Chatbot: React.FC<Props> = (props) => { | ||
const inputRef = React.useRef<HTMLInputElement>(null); | ||
const [config, setConfig] = useState<ChatConfig>(props.config); | ||
const [files, setFiles] = useState<FileList | undefined>(undefined); | ||
const fileInputRef = useRef<HTMLInputElement>(null); | ||
const formRef = useRef<HTMLFormElement>(null); | ||
const codeMirrorInputRef = useRef<ReactCodeMirrorRef>(null); | ||
const scrollContainerRef = useRef<HTMLDivElement>(null); | ||
const { theme } = useTheme(); | ||
|
||
const { | ||
messages, | ||
setMessages, | ||
input, | ||
setInput, | ||
handleInputChange, | ||
handleSubmit, | ||
isLoading, | ||
stop, | ||
|
@@ -196,6 +205,17 @@ export const Chatbot: React.FC<Props> = (props) => { | |
props.allowAttachments.length > 0) || | ||
props.allowAttachments === true; | ||
|
||
const promptCompletions: AdditionalCompletions = { | ||
triggerSymbol: "/", | ||
completions: props.prompts.map( | ||
(prompt): Completion => ({ | ||
label: `/${prompt}`, | ||
displayLabel: prompt, | ||
apply: prompt, | ||
}), | ||
), | ||
}; | ||
|
||
useEffect(() => { | ||
// When the message length changes, scroll to the bottom | ||
scrollContainerRef.current?.scrollTo({ | ||
|
@@ -242,7 +262,11 @@ export const Chatbot: React.FC<Props> = (props) => { | |
: "bg-[var(--slate-4)] text-[var(--slate-12)]" | ||
}`} | ||
> | ||
<p>{renderMessage(message)}</p> | ||
<p | ||
className={cn(message.role === "user" && "whitespace-pre-wrap")} | ||
> | ||
{renderMessage(message)} | ||
</p> | ||
</div> | ||
<div className="flex justify-end text-xs gap-2 invisible group-hover:visible"> | ||
<button | ||
|
@@ -298,6 +322,7 @@ export const Chatbot: React.FC<Props> = (props) => { | |
experimental_attachments: files, | ||
}); | ||
}} | ||
ref={formRef} | ||
className="flex w-full border-t border-[var(--slate-6)] px-2 py-1 items-center" | ||
> | ||
{props.showConfigurationControls && ( | ||
|
@@ -309,22 +334,29 @@ export const Chatbot: React.FC<Props> = (props) => { | |
onSelect={(prompt) => { | ||
setInput(prompt); | ||
requestAnimationFrame(() => { | ||
inputRef.current?.focus(); | ||
inputRef.current?.setSelectionRange( | ||
prompt.length, | ||
prompt.length, | ||
); | ||
codeMirrorInputRef.current?.view?.focus(); | ||
moveToEndOfEditor(codeMirrorInputRef.current?.view); | ||
}); | ||
}} | ||
/> | ||
)} | ||
<input | ||
name="prompt" | ||
ref={inputRef} | ||
<PromptInput | ||
className="rounded-sm mr-2" | ||
placeholder="Type your message here, / for prompts" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we change the placeholder of prompts exist or not |
||
value={input} | ||
onChange={handleInputChange} | ||
className="flex w-full outline-none bg-transparent ml-2 text-[var(--slate-12)] mr-2" | ||
placeholder="Type your message..." | ||
inputRef={codeMirrorInputRef} | ||
theme={theme} | ||
onChange={setInput} | ||
onSubmit={(_evt, newValue) => { | ||
if (!newValue.trim()) { | ||
return; | ||
} | ||
formRef.current?.requestSubmit(); | ||
}} | ||
onClose={() => { | ||
// no-op | ||
}} | ||
additionalCompletions={promptCompletions} | ||
/> | ||
{files && files.length === 1 && ( | ||
<span | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do 2 separate mentions plugins? One for "@" data and one for "/" prompts; instead of appending the trigger, replace it.