mirror of
https://github.com/DustinBrett/daedalOS.git
synced 2025-12-06 00:20:05 +01:00
Re-use WebLLM worker
This commit is contained in:
parent
6ead64d8a0
commit
b80dfc54a0
|
|
@ -390,18 +390,16 @@ const Chat: FC<ComponentProcessProps> = ({ id }) => {
|
|||
}, [messages]);
|
||||
|
||||
useEffect(() => {
|
||||
let cleanUp: (() => void) | undefined;
|
||||
|
||||
if (initRef.current) {
|
||||
cleanUp = () => AI?.destroy?.();
|
||||
} else if (AI) {
|
||||
if (AI && !initRef.current) {
|
||||
initRef.current = true;
|
||||
|
||||
AI.init().then(() => addMessage(AI.greeting));
|
||||
inputRef.current?.focus(PREVENT_SCROLL);
|
||||
}
|
||||
|
||||
return cleanUp;
|
||||
return () => {
|
||||
if (initRef.current) AI?.destroy?.();
|
||||
};
|
||||
}, [AI, addMessage, apiKey]);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,12 @@ import type { Engine } from "hooks/useInference/useInference";
|
|||
type Log = { message: string; type: string };
|
||||
type WorkerMessage = { data: Log | string };
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
webLLM?: Worker;
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_GREETING = {
|
||||
text: "Hello, I am an AI assistant. How can I help you today?",
|
||||
type: "assistant",
|
||||
|
|
@ -18,14 +24,16 @@ export class WebLLM implements Engine {
|
|||
|
||||
public destroy(): void {
|
||||
this.reset();
|
||||
setTimeout(() => this.worker?.terminate(), 500);
|
||||
}
|
||||
|
||||
public async init(): Promise<void> {
|
||||
this.worker = new Worker(
|
||||
new URL("hooks/useInference/WebLLM.worker.ts", import.meta.url),
|
||||
{ name: "WebLLM" }
|
||||
);
|
||||
window.webLLM =
|
||||
window.webLLM ||
|
||||
new Worker(
|
||||
new URL("hooks/useInference/WebLLM.worker.ts", import.meta.url),
|
||||
{ name: "WebLLM" }
|
||||
);
|
||||
this.worker = window.webLLM;
|
||||
this.worker.postMessage({ type: "init" });
|
||||
|
||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user