Skip to content

Commit

Permalink
fix: clean up stuck streaming messages after refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Neet-Nestor committed May 22, 2024
1 parent cfb78d2 commit 33e62aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/client/webllm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ export class WebLLMApi implements LLMApi {
}
}

export const WebLLMContext = createContext<WebLLMApi | null>(null);
export const WebLLMContext = createContext<WebLLMApi | undefined>(undefined);
11 changes: 6 additions & 5 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function _Chat() {
}
};

const doSubmit = (userInput: string) => {
const onSubmit = (userInput: string) => {
if (userInput.trim() === "") return;

const matchCommand = chatCommands.match(userInput);
Expand All @@ -681,6 +681,7 @@ function _Chat() {
}

if (isStreaming) return;

setIsLoading(true);
chatStore
.onUserInput(userInput, webllm, attachImages)
Expand Down Expand Up @@ -740,7 +741,7 @@ function _Chat() {
}
}
});
session.messages.filter((m) => m.content.length > 0);
session.messages = session.messages.filter((m) => m.content.length > 0);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand All @@ -758,7 +759,7 @@ function _Chat() {
return;
}
if (shouldSubmit(e) && promptHints.length === 0) {
doSubmit(userInput);
onSubmit(userInput);
e.preventDefault();
}
};
Expand Down Expand Up @@ -947,7 +948,7 @@ function _Chat() {
useCommand({
fill: setUserInput,
submit: (text) => {
doSubmit(text);
onSubmit(text);
},
});

Expand Down Expand Up @@ -1432,7 +1433,7 @@ function _Chat() {
text={Locale.Chat.Send}
className={styles["chat-input-send"]}
type="primary"
onClick={() => doSubmit(userInput)}
onClick={() => onSubmit(userInput)}
/>
)}
</label>
Expand Down
13 changes: 12 additions & 1 deletion app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useAppConfig } from "../store/config";
import { getClientConfig } from "../config/client";
import { WebLLMApi, WebLLMContext } from "../client/webllm";
import Locale from "../locales";
import { useChatStore } from "../store";

export function Loading(props: { noLogo?: boolean }) {
return (
Expand Down Expand Up @@ -181,7 +182,7 @@ function Screen() {
}

const useWebLLM = () => {
const [webllm, setWebLLM] = useState<WebLLMApi | null>(null);
const [webllm, setWebLLM] = useState<WebLLMApi | undefined>(undefined);
const [isSWAlive, setSWAlive] = useState(true);

useEffect(() => {
Expand Down Expand Up @@ -232,6 +233,15 @@ const useLoadUrlParam = () => {
}, []);
};

const useStopStreamingMessages = () => {
const chatStore = useChatStore();

// Clean up bad chat messages due to refresh during generating
useEffect(() => {
chatStore.stopStreaming();
}, []);
};

export function Home() {
const hasHydrated = useHasHydrated();
const isServiceWorkerReady = useServiceWorkerReady();
Expand All @@ -240,6 +250,7 @@ export function Home() {
useSwitchTheme();
useHtmlLang();
useLoadUrlParam();
useStopStreamingMessages();

if (!hasHydrated || !isServiceWorkerReady) {
return <Loading />;
Expand Down
24 changes: 17 additions & 7 deletions app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,24 @@ export const useChatStore = createPersistStore(
},

stopStreaming() {
get().updateCurrentSession((session) => {
session.messages = session.messages
.map((m: ChatMessage) => ({
...m,
streaming: false,
}))
.filter((m: ChatMessage) => !!m.content);
const sessions = get().sessions;
sessions.forEach((session) => {
if (session.messages.length === 0) {
return;
}
const lastMessage = session.messages[session.messages.length - 1];
if (
lastMessage.role === "assistant" &&
lastMessage.streaming &&
lastMessage.content.length === 0
) {
// This message generation is interrupted by refresh and is stuck
session.messages.splice(session.messages.length - 1, 1);
}
// Reset streaming status for all messages
session.messages.forEach((m) => (m.streaming = false));
});
set(() => ({ sessions }));
},

updateStat(message: ChatMessage) {
Expand Down

0 comments on commit 33e62aa

Please sign in to comment.