Add proper delay for assistant switching (#2070)

* add proper delay for assistant switching

* persist input if possible
This commit is contained in:
pablodanswer
2024-08-07 14:46:15 -07:00
committed by GitHub
parent 53d976234a
commit eab82782ca

View File

@@ -412,6 +412,8 @@ export function ChatPage({
completeMessageDetail.messageMap completeMessageDetail.messageMap
); );
const [isStreaming, setIsStreaming] = useState(false); const [isStreaming, setIsStreaming] = useState(false);
const [abortController, setAbortController] =
useState<AbortController | null>(null);
// uploaded files // uploaded files
const [currentMessageFiles, setCurrentMessageFiles] = useState< const [currentMessageFiles, setCurrentMessageFiles] = useState<
@@ -614,18 +616,30 @@ export function ChatPage({
return this.stack.length === 0; return this.stack.length === 0;
} }
} }
async function updateCurrentMessageFIFO( async function updateCurrentMessageFIFO(
stack: CurrentMessageFIFO, stack: CurrentMessageFIFO,
params: any params: any
) { ) {
try { try {
for await (const packetBunch of sendMessage(params)) { for await (const packetBunch of sendMessage(params)) {
if (params.signal?.aborted) {
throw new Error("AbortError");
}
for (const packet of packetBunch) { for (const packet of packetBunch) {
stack.push(packet); stack.push(packet);
} }
} }
} catch (error) { } catch (error: unknown) {
stack.error = String(error); if (error instanceof Error) {
if (error.name === "AbortError") {
console.debug("Stream aborted");
} else {
stack.error = error.message;
}
} else {
stack.error = String(error);
}
} finally { } finally {
stack.isComplete = true; stack.isComplete = true;
} }
@@ -663,6 +677,9 @@ export function ChatPage({
return; return;
} }
const controller = new AbortController();
setAbortController(controller);
setAlternativeGeneratingAssistant(alternativeAssistantOverride); setAlternativeGeneratingAssistant(alternativeAssistantOverride);
clientScrollToBottom(); clientScrollToBottom();
let currChatSessionId: number; let currChatSessionId: number;
@@ -775,6 +792,8 @@ export function ChatPage({
const stack = new CurrentMessageFIFO(); const stack = new CurrentMessageFIFO();
updateCurrentMessageFIFO(stack, { updateCurrentMessageFIFO(stack, {
signal: controller.signal, // Add this line
message: currMessage, message: currMessage,
alternateAssistantId: currentAssistantId, alternateAssistantId: currentAssistantId,
fileDescriptors: currentMessageFiles, fileDescriptors: currentMessageFiles,
@@ -987,9 +1006,12 @@ export function ChatPage({
const onAssistantChange = (assistant: Persona | null) => { const onAssistantChange = (assistant: Persona | null) => {
if (assistant && assistant.id !== liveAssistant.id) { if (assistant && assistant.id !== liveAssistant.id) {
// remove uploaded files // Abort the ongoing stream if it exists
setCurrentMessageFiles([]); if (abortController && isStreaming) {
setSelectedAssistant(assistant); abortController.abort();
resetInputBar();
}
textAreaRef.current?.focus(); textAreaRef.current?.focus();
router.push(buildChatUrl(searchParams, null, assistant.id)); router.push(buildChatUrl(searchParams, null, assistant.id));
} }