-
Notifications
You must be signed in to change notification settings - Fork 45
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
Clear regular prompt message if using Re-Ask Option #616
base: main
Are you sure you want to change the base?
Conversation
…ompt definition, updated onPrompt call in onResubmitClick
Moving to draft because I haven't yet checked how re-asking with an Image has been affected through 7cc1e2f and/or my PR (if at all). |
seems to work well! thanks for jumping on this |
I've added my Observations for Re-asking with Image as Input 1 before and after the message duplication bug.
Future ConsiderationsAs of e4967a9, we currently don't pass any imageUrls (from Image as Input) in the onPrompt() call in onResubmitClick() during the Re-Ask Option. However, if this changes in the future, we might see this bug again, in the form of Input Image(s) duplication. // If retrying, empty the prompt and imageUrls to avoid duplicate message
if (retry) {
prompt = "";
imageUrls = [];
} |
Deploying chatcraft-org with Cloudflare Pages
|
@humphd this looks good to me, can you take a look? |
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.
I think this is fine, but I'd change the onPrompt
signature.
src/components/MessagesView.tsx
Outdated
@@ -99,7 +99,7 @@ function MessagesView({ | |||
isLoading={isLoading} | |||
onResubmitClick={async (promptText?: string) => { | |||
await deleteMessages(message.id, "after"); | |||
onPrompt(promptText); | |||
onPrompt(promptText, undefined, true); // pass prompt text and true for retry, don't include any imageURLs |
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.
A better pattern to use for this is to convert the args for onPrompt
to be an Object
:
async ({ prompt, imageUrls, retry = false }: { prompt?: string; imageUrls?; string[], retry?; boolean } = {}) => {
Now you can call it like this:
onPrompt({ prompt: promptText, retry: true });
This deals with undefined/optional args much better, and makes it self-documenting.
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.
@humphd
I updated the onPrompt signature in ChatBase and MessagesView, but to get this working I also had to make the same updates to the onPrompt and onSendClick (and any calls to these) signatures in various other components due to errors like the ones below:
These changes still fix the duplication issues observed and preserve the other behaviours I've tested.
The onPrompt/onSendClick signature updates don't seem like they'll break anything (as long as any associated calls have also been updated accordingly), but I can't confirm anything at this time.
Should we put my recent commits (everything from bbdc1ab onward) on the backburner for a follow-up PR?
…g function results back to LLM
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.
You could also define a reusable type for these options, since you have to use them in so many places.
src/Chat/ChatBase.tsx
Outdated
prompt?: string; | ||
imageUrls?: string[]; | ||
retry?: boolean; | ||
}) => { |
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.
A trick you can use when all properties on the object are optional is to set the options object to have a default value:
async ({
prompt,
imageUrls,
retry = false,
}: {
prompt?: string;
imageUrls?: string[];
retry?: boolean;
} = {}) => {
The value of this is that it lets callers do onPrompt()
and it will work.
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.
Something like this?
async ({
prompt = "",
imageUrls = [],
retry = false,
//...
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.
No, see my code above. I'm setting the value to {}
by default.
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.
Oh! I see it now. My bad
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.
I followed your advice and added a reusable type called OnPromptFunction.
I tried the original trick you suggested but got this error when I applied it to the signature on other files on some files:
So I ended up using a different syntax suggested by ChatCraft:
export type OnPromptFunction = (options?: {
prompt?: string;
imageUrls?: string[];
retry?: boolean;
}) => void;
I wasn't sure where to place the reusable type, so I placed it in a new file, src/lib/OnPromptFunction.ts
.
const onPrompt: OnPromptFunction = useCallback( | ||
async (options?: { prompt?: string; imageUrls?: string[]; retry?: boolean }) => { | ||
let prompt = options?.prompt; | ||
const { imageUrls, retry } = options || {}; |
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.
I'm still confused why you don't use the pattern I mentioned and assign = {}
as the default instead of doing this.
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.
The pattern was fine for the onPrompt
implementation in ChatBase
, but when I tried using the same pattern throughout the onPrompt/onSendClick
props TypeScript reports this error (props/type can't have default values):
To create a common type for both the onPrompt implementation and the onPrompt component props I had to use an options
parameter and make that optional.
Although, I think if we apply the OnPromptFunction
to the component props only, we can still use the original pattern in the onPrompt implementation, we just won't be able to turn that pattern into a reusable type because it uses a parameter initializer/default value(s)
@@ -5,11 +5,12 @@ import DesktopPromptForm from "./DesktopPromptForm"; | |||
import useMobileBreakpoint from "../../hooks/use-mobile-breakpoint"; | |||
import { useSettings } from "../../hooks/use-settings"; | |||
import { ChatCraftChat } from "../../lib/ChatCraftChat"; | |||
import { OnPromptFunction } from "../../lib/OnPromptFunction"; |
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.
Move your exported types into this file.
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.
I hope I did this correctly.
I put PromptFunctionOptions
inside OnPromptFunction.ts
with OnPromptFunction
.
This fixes #587
Background
7cc1e2f allowed web handlers and slash commands to be executed with the Re-ask option (issue described in #541) by passing the prompt text to the onPrompt call in MessagesView::onResubmitClick().
Before this change, retrying with a regular prompt message would trigger the regular re-try routine (since the prompt text wasn't passed to the onPrompt call in MessagesView::onResubmitClick():
chatcraft.org/src/Chat/ChatBase.tsx
Lines 283 to 297 in 9f73dea
After this change however, if re-asking with a regular prompt message, the updated message will be passed to onPrompt (as prompt) and add a duplicate message to the chat (line 278):
chatcraft.org/src/Chat/ChatBase.tsx
Lines 275 to 278 in 9f73dea
Changes
I added a boolean parameter to ChatBase::onPrompt called
retry
(false by default):If
retry
is True, the prompt will be cleared to prevent message duplication.retry
is False by default because I only want to explicitly set it to True when calling onPrompt() from onResubmitClick():I had to update the onPrompt signature in MessagesViewProps for syntax reasons. If there's a better way to implement this I'm open to suggestions.
Comparison to Production (updated with Image as Input observations)
Below are the observations I've made on the Re-Ask options before and after 7cc1e2f, as well as for this Pull Request:
My PR preserves the changes from 7cc1e2f but prevent the message duplication when re-trying with a regular prompt message.