-
-
Notifications
You must be signed in to change notification settings - Fork 85
Open
Description
Is there a way to disable the "Submit" button if user enters only empty spaces?
There is disableSubmitButton option under ComposerOptions but we need latest prompt text typed in the prompt box.
I have verified that useAsStreamAdapter and useAiChatApi doesn't return prompt text. We can use disableSubmitButton only if current prompt text is available.
So, is there a way to get current prompt text or disable "submit" button if user enters only empty spaces?
const sendMessage = async (prompt, observer, extras) => {
const userPrompt = prompt?.trim();
try {
const response = await fetch('api/chat',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: userPrompt,
}),
},
);
if (response.status !== 200) {
observer.error(new Error('Unable to connect to the API.'));
return;
}
if (!response.body) {
observer.error(new Error('Empty response body.'));
return;
}
// Read a stream of server-sent events and feed them to the observer as they are being generated
const reader = response.body.getReader();
const textDecoder = new TextDecoder();
/* eslint-disable no-await-in-loop */
while (true) {
const { value, done } = await reader.read();
if (done) {
observer.complete();
break;
}
const content = textDecoder.decode(value, { stream: true });
if (content?.trim()) {
observer.next(content);
}
}
} catch (error) {
observer.error(error);
}
};
const adapter = useAsStreamAdapter(sendMessage, []);
const chatApi = useAiChatApi();
return (
<AiChat
api={chatApi}
adapter={adapter}
displayOptions={{ colorScheme: 'light', themeId: 'nova' }}
conversationOptions={{ layout: 'bubbles' }}
messageOptions={{
waitTimeBeforeStreamCompletion: 3000,
}}
composerOptions={{
autoFocus: true,
}}
events={{
error: error => {
// eslint-disable-next-line no-console
console.log('Error Occurred!', error);
},
}}
/>
);
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels