-
Notifications
You must be signed in to change notification settings - Fork 117
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
Refactor InputBar with Tip Tap Library #2992
Merged
Merged
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f73e9d7
tmp
flvndvd c7dc00c
Merge branch 'main' into flav/tip-tap
flvndvd 8389b61
Tmp
flvndvd 318739c
Tmp
flvndvd 1ca9047
Tmp
flvndvd 549265f
Fix each child should have a key warning
flvndvd 6b1fe1d
:scissors:
flvndvd 9591d07
:sparkles:
flvndvd 829062a
Clean up step 1
flvndvd 68e09a2
Final clean up
flvndvd 2bf290d
:scissors:
flvndvd c0e0692
:art:
flvndvd be3534b
Rename
flvndvd b09bef4
:sparkles:
flvndvd d121f2d
Move files around
flvndvd aa5eccf
:scissors:
flvndvd 4e6f6f0
Merge branch 'main' into flav/tip-tap
flvndvd 34e44ff
Move files to input_bar folder
flvndvd 88acac5
Fix cursor on placeholder
flvndvd 3508337
:books:
flvndvd 8038b9c
Only accept supported file extensions
flvndvd f52269b
Support tab to complete with selected assistant
flvndvd c61fe56
Select item on exact query match, prevent space default
flvndvd f6f1349
Auto focus the editor
flvndvd 2482350
Hack to patch up the attachment issue
flvndvd 5e1f3bd
Clean timeout on useEffect
flvndvd 4c7ac30
:sparkles:
flvndvd 0ed8056
Focus editor after input file loaded
flvndvd 11622b1
Revert hack
flvndvd 47aba84
Prevent default on enter.
flvndvd b2f597c
Handle Shift + Enter // Fix autofocus on end
flvndvd 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 was deleted.
Oops, something went wrong.
292 changes: 292 additions & 0 deletions
292
front/components/assistant/conversation/input_bar/InputBar.tsx
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 |
---|---|---|
@@ -0,0 +1,292 @@ | ||
import { Button, Citation, StopIcon } from "@dust-tt/sparkle"; | ||
import { WorkspaceType } from "@dust-tt/types"; | ||
import { AgentConfigurationType } from "@dust-tt/types"; | ||
import { AgentMention, MentionType } from "@dust-tt/types"; | ||
import { | ||
createContext, | ||
Fragment, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import { mutate } from "swr"; | ||
|
||
import { GenerationContext } from "@app/components/assistant/conversation/GenerationContextProvider"; | ||
import InputBarContainer, { | ||
InputBarContainerProps, | ||
} from "@app/components/assistant/conversation/input_bar/InputBarContainer"; | ||
import { SendNotificationsContext } from "@app/components/sparkle/Notification"; | ||
import { compareAgentsForSort } from "@app/lib/assistant"; | ||
import { handleFileUploadToText } from "@app/lib/client/handle_file_upload"; | ||
import { useAgentConfigurations } from "@app/lib/swr"; | ||
import { classNames } from "@app/lib/utils"; | ||
|
||
// AGENT MENTION | ||
|
||
function AgentMention({ | ||
agentConfiguration, | ||
}: { | ||
agentConfiguration: AgentConfigurationType; | ||
}) { | ||
return ( | ||
<div | ||
className={classNames("inline-block font-medium text-brand")} | ||
contentEditable={false} | ||
data-agent-configuration-id={agentConfiguration?.sId} | ||
data-agent-name={agentConfiguration?.name} | ||
> | ||
@{agentConfiguration.name} | ||
</div> | ||
); | ||
} | ||
|
||
// AGENT LIST | ||
|
||
export function AssistantInputBar({ | ||
owner, | ||
onSubmit, | ||
conversationId, | ||
stickyMentions, | ||
}: { | ||
owner: WorkspaceType; | ||
onSubmit: ( | ||
input: string, | ||
mentions: MentionType[], | ||
contentFragment?: { title: string; content: string } | ||
) => void; | ||
conversationId: string | null; | ||
stickyMentions?: AgentMention[]; | ||
}) { | ||
const [contentFragmentBody, setContentFragmentBody] = useState< | ||
string | undefined | ||
>(undefined); | ||
const [contentFragmentFilename, setContentFragmentFilename] = useState< | ||
string | undefined | ||
>(undefined); | ||
const { agentConfigurations } = useAgentConfigurations({ | ||
workspaceId: owner.sId, | ||
agentsGetView: conversationId ? { conversationId } : "list", | ||
}); | ||
const sendNotification = useContext(SendNotificationsContext); | ||
|
||
const [isAnimating, setIsAnimating] = useState<boolean>(false); | ||
const { animate, selectedAssistant } = useContext(InputBarContext); | ||
useEffect(() => { | ||
if (animate && !isAnimating) { | ||
setIsAnimating(true); | ||
setTimeout(() => setIsAnimating(false), 1500); | ||
} | ||
}, [animate, isAnimating]); | ||
|
||
const activeAgents = agentConfigurations.filter((a) => a.status === "active"); | ||
activeAgents.sort(compareAgentsForSort); | ||
|
||
const handleSubmit: InputBarContainerProps["onEnterKeyDown"] = ( | ||
isEmpty, | ||
textAndMentions, | ||
resetEditorText | ||
) => { | ||
if (isEmpty) { | ||
return; | ||
} | ||
|
||
const { mentions: rawMentions, text } = textAndMentions; | ||
const mentions: MentionType[] = rawMentions.map((m) => ({ | ||
configurationId: m.id, | ||
})); | ||
|
||
let contentFragment: | ||
| { | ||
title: string; | ||
content: string; | ||
url: string | null; | ||
contentType: string; | ||
} | ||
| undefined = undefined; | ||
if (contentFragmentBody && contentFragmentFilename) { | ||
contentFragment = { | ||
title: contentFragmentFilename, | ||
content: contentFragmentBody, | ||
url: null, | ||
contentType: "file_attachment", | ||
}; | ||
} | ||
onSubmit(text, mentions, contentFragment); | ||
resetEditorText(); | ||
setContentFragmentFilename(undefined); | ||
setContentFragmentBody(undefined); | ||
}; | ||
|
||
const onInputFileChange: InputBarContainerProps["onInputFileChange"] = async ( | ||
event | ||
) => { | ||
const file = (event?.target as HTMLInputElement)?.files?.[0]; | ||
if (!file) return; | ||
if (file.size > 10_000_000) { | ||
sendNotification({ | ||
type: "error", | ||
title: "File too large.", | ||
description: | ||
"PDF uploads are limited to 10Mb per file. Please consider uploading a smaller file.", | ||
}); | ||
return; | ||
} | ||
const res = await handleFileUploadToText(file); | ||
|
||
if (res.isErr()) { | ||
sendNotification({ | ||
type: "error", | ||
title: "Error uploading file.", | ||
description: res.error.message, | ||
}); | ||
return; | ||
} | ||
if (res.value.content.length > 1_000_000) { | ||
// This error should pretty much never be triggered but it is a possible case, so here it is. | ||
sendNotification({ | ||
type: "error", | ||
title: "File too large.", | ||
description: | ||
"The extracted text from your PDF has more than 1 million characters. This will overflow the assistant context. Please consider uploading a smaller file.", | ||
}); | ||
return; | ||
} | ||
|
||
setContentFragmentFilename(res.value.title); | ||
setContentFragmentBody(res.value.content); | ||
}; | ||
|
||
const [isProcessing, setIsProcessing] = useState<boolean>(false); | ||
|
||
// GenerationContext: to know if we are generating or not | ||
const generationContext = useContext(GenerationContext); | ||
if (!generationContext) { | ||
throw new Error( | ||
"FixedAssistantInputBar must be used within a GenerationContextProvider" | ||
); | ||
} | ||
|
||
const handleStopGeneration = async () => { | ||
if (!conversationId) { | ||
return; | ||
} | ||
setIsProcessing(true); // we don't set it back to false immediately cause it takes a bit of time to cancel | ||
await fetch( | ||
`/api/w/${owner.sId}/assistant/conversations/${conversationId}/cancel`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
action: "cancel", | ||
messageIds: generationContext.generatingMessageIds, | ||
}), | ||
} | ||
); | ||
await mutate( | ||
`/api/w/${owner.sId}/assistant/conversations/${conversationId}` | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isProcessing && generationContext.generatingMessageIds.length === 0) { | ||
setIsProcessing(false); | ||
} | ||
}, [isProcessing, generationContext.generatingMessageIds.length]); | ||
|
||
return ( | ||
<> | ||
{generationContext.generatingMessageIds.length > 0 && ( | ||
<div className="flex justify-center px-4 pb-4"> | ||
<Button | ||
className="mt-4" | ||
variant="tertiary" | ||
label={isProcessing ? "Stopping generation..." : "Stop generation"} | ||
icon={StopIcon} | ||
onClick={handleStopGeneration} | ||
disabled={isProcessing} | ||
/> | ||
</div> | ||
)} | ||
|
||
<div className="flex flex-1 px-0 sm:px-4"> | ||
<div className="flex w-full flex-1 flex-col items-end self-stretch sm:flex-row"> | ||
<div | ||
className={classNames( | ||
"relative flex w-full flex-1 flex-col items-stretch gap-0 self-stretch pl-4 sm:flex-row", | ||
"border-struture-200 border-t bg-white/80 shadow-[0_0_36px_-15px_rgba(0,0,0,0.3)] backdrop-blur focus-within:border-structure-300 sm:rounded-3xl sm:border-2 sm:border-element-500 sm:shadow-[0_12px_36px_-15px_rgba(0,0,0,0.3)] sm:focus-within:border-element-600", | ||
"transition-all duration-300", | ||
isAnimating | ||
? "animate-shake border-action-500 focus-within:border-action-800" | ||
: "" | ||
)} | ||
> | ||
<div className="relative flex w-full flex-1 flex-col"> | ||
{contentFragmentFilename && contentFragmentBody && ( | ||
<div className="border-b border-structure-300/50 pb-3 pt-5"> | ||
<Citation | ||
title={contentFragmentFilename} | ||
description={contentFragmentBody?.substring(0, 100)} | ||
onClose={() => { | ||
setContentFragmentBody(undefined); | ||
setContentFragmentFilename(undefined); | ||
}} | ||
/> | ||
</div> | ||
)} | ||
|
||
<InputBarContainer | ||
allAssistants={activeAgents} | ||
agentConfigurations={agentConfigurations} | ||
owner={owner} | ||
selectedAssistant={selectedAssistant} | ||
onEnterKeyDown={handleSubmit} | ||
stickyMentions={stickyMentions} | ||
onInputFileChange={onInputFileChange} | ||
disableAttachment={!!contentFragmentFilename} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export function FixedAssistantInputBar({ | ||
owner, | ||
onSubmit, | ||
stickyMentions, | ||
conversationId, | ||
}: { | ||
owner: WorkspaceType; | ||
onSubmit: ( | ||
input: string, | ||
mentions: MentionType[], | ||
contentFragment?: { title: string; content: string } | ||
) => void; | ||
stickyMentions?: AgentMention[]; | ||
conversationId: string | null; | ||
}) { | ||
return ( | ||
<div className="4xl:px-0 fixed bottom-0 left-0 right-0 z-20 flex-initial lg:left-80"> | ||
<div className="mx-auto max-h-screen max-w-4xl pb-0 sm:pb-8"> | ||
<AssistantInputBar | ||
owner={owner} | ||
onSubmit={onSubmit} | ||
conversationId={conversationId} | ||
stickyMentions={stickyMentions} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const InputBarContext = createContext<{ | ||
animate: boolean; | ||
selectedAssistant: AgentMention | null; | ||
}>({ | ||
animate: false, | ||
selectedAssistant: null, | ||
}); |
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.
are we not missing a timeout clean up here in the return of useEffect()?