-
Notifications
You must be signed in to change notification settings - Fork 877
/
service_worker.ts
253 lines (230 loc) · 7.83 KB
/
service_worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import * as tvmjs from "@mlc-ai/web-runtime";
import log from "loglevel";
import { ChatOptions, MLCEngineConfig } from "./config";
import { ReloadParams, WorkerRequest, WorkerResponse } from "./message";
import { InitProgressReport } from "./types";
import {
WebWorkerMLCEngineHandler,
WebWorkerMLCEngine,
ChatWorker,
} from "./web_worker";
import { areArraysEqual, areChatOptionsListEqual } from "./utils";
import {
NoServiceWorkerAPIError,
NonWorkerEnvironmentError,
ServiceWorkerInitializationError,
} from "./error";
/* Service Worker Script */
type IServiceWorker = globalThis.ServiceWorker;
/**
* Worker handler that can be used in a ServiceWorker.
*
* @example
*
* const engine = new MLCEngine();
* let handler;
* chrome.runtime.onConnect.addListener(function (port) {
* if (handler === undefined) {
* handler = new ServiceWorkerMLCEngineHandler(engine, port);
* } else {
* handler.setPort(port);
* }
* port.onMessage.addListener(handler.onmessage.bind(handler));
* });
*/
export class ServiceWorkerMLCEngineHandler extends WebWorkerMLCEngineHandler {
private clientRegistry = new Map<
string,
IServiceWorker | Client | MessagePort
>();
private initRequestUuid?: string;
constructor() {
if (!self || !("addEventListener" in self)) {
throw new NonWorkerEnvironmentError("ServiceWorkerMLCEngineHandler");
}
super();
const onmessage = this.onmessage.bind(this);
this.engine.setInitProgressCallback((report: InitProgressReport) => {
const msg: WorkerResponse = {
kind: "initProgressCallback",
uuid: this.initRequestUuid || "",
content: report,
};
this.postMessage(msg);
});
self.addEventListener("message", (event) => {
const message = event as unknown as ExtendableMessageEvent;
if (message.source) {
this.clientRegistry.set(message.data.uuid, message.source);
}
message.waitUntil(
new Promise((resolve, reject) => {
onmessage(message, resolve, reject);
}),
);
});
}
postMessage(message: WorkerResponse) {
if (this.clientRegistry.has(message.uuid)) {
const client = this.clientRegistry.get(message.uuid);
client?.postMessage(message);
if (message.kind === "return" || message.kind === "throw") {
this.clientRegistry.delete(message.uuid);
} else {
// TODO(nestor): Delete clientRegistry after complete to avoid memory leak?
}
}
}
onmessage(
event: ExtendableMessageEvent,
onComplete?: (value: any) => void,
onError?: () => void,
): void {
const msg = event.data as WorkerRequest;
log.trace(
`ServiceWorker message: [${msg.kind}] ${JSON.stringify(msg.content)}`,
);
// Special case message handling different from WebWorkerMLCEngineHandler
if (msg.kind === "keepAlive") {
const reply: WorkerResponse = {
kind: "heartbeat",
uuid: msg.uuid,
};
this.postMessage(reply);
onComplete?.(reply);
return;
}
if (msg.kind === "reload") {
this.handleTask(msg.uuid, async () => {
const params = msg.content as ReloadParams;
// If the modelId, chatOpts, and appConfig are the same, immediately return
if (
areArraysEqual(this.modelId, params.modelId) &&
areChatOptionsListEqual(this.chatOpts, params.chatOpts)
) {
log.info("Already loaded the model. Skip loading");
const gpuDetectOutput = await tvmjs.detectGPUDevice();
if (gpuDetectOutput == undefined) {
throw Error("Cannot find WebGPU in the environment");
}
let gpuLabel = "WebGPU";
if (gpuDetectOutput.adapterInfo.description.length != 0) {
gpuLabel += " - " + gpuDetectOutput.adapterInfo.description;
} else {
gpuLabel += " - " + gpuDetectOutput.adapterInfo.vendor;
}
this.engine.getInitProgressCallback()?.({
progress: 1,
timeElapsed: 0,
text: "Finish loading on " + gpuLabel,
});
onComplete?.(null);
return null;
}
this.initRequestUuid = msg.uuid;
await this.engine.reload(params.modelId, params.chatOpts);
this.modelId = params.modelId;
this.chatOpts = params.chatOpts;
onComplete?.(null);
return null;
});
return;
}
// All rest of message handling are the same as WebWorkerMLCEngineHandler
super.onmessage(msg, onComplete, onError);
}
}
/* Webapp Client */
export class ServiceWorker implements ChatWorker {
_onmessage: (event: MessageEvent) => void = () => {};
get onmessage() {
return this._onmessage;
}
set onmessage(handler: (event: any) => void) {
this._onmessage = handler;
if (!("serviceWorker" in navigator)) {
throw new NoServiceWorkerAPIError();
}
(navigator.serviceWorker as ServiceWorkerContainer).onmessage = handler;
}
postMessage(message: WorkerRequest) {
if (!("serviceWorker" in navigator)) {
throw new NoServiceWorkerAPIError();
}
const serviceWorker = (navigator.serviceWorker as ServiceWorkerContainer)
.controller;
if (!serviceWorker) {
throw new Error("There is no active service worker");
}
serviceWorker.postMessage(message);
}
}
/**
* Create a ServiceWorkerMLCEngine.
*
* @param modelId model_id of the model to load, either string or string[]. When multiple models
* are provided, we load all models sequentially. Each modelId needs to either be in
* `webllm.prebuiltAppConfig`, or in `engineCOnfig.appConfig`.
* @param engineConfig Optionally configures the engine, see `webllm.MLCEngineConfig` for more.
* @param chatOpts Extra options to optionally override the `mlc-chat-config.json` of `modelId`.
* The size of which needs to match that of `modelId`; chatOpts[i] will be used for modelId[i].
* @returns An initialized `WebLLM.ServiceWorkerMLCEngine` with `modelId` loaded.
*/
export async function CreateServiceWorkerMLCEngine(
modelId: string | string[],
engineConfig?: MLCEngineConfig,
chatOpts?: ChatOptions | ChatOptions[],
keepAliveMs = 10000,
): Promise<ServiceWorkerMLCEngine> {
if (!("serviceWorker" in navigator)) {
throw new NoServiceWorkerAPIError();
}
const serviceWorkerAPI = navigator.serviceWorker as ServiceWorkerContainer;
const registration = await serviceWorkerAPI.ready;
const serviceWorker = registration.active || serviceWorkerAPI.controller;
if (!serviceWorker) {
throw new ServiceWorkerInitializationError();
}
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(
engineConfig,
keepAliveMs,
);
await serviceWorkerMLCEngine.reload(modelId, chatOpts);
return serviceWorkerMLCEngine;
}
/**
* A client of MLCEngine that exposes the same interface
*/
export class ServiceWorkerMLCEngine extends WebWorkerMLCEngine {
missedHeatbeat = 0;
constructor(engineConfig?: MLCEngineConfig, keepAliveMs = 10000) {
if (!("serviceWorker" in navigator)) {
throw new NoServiceWorkerAPIError();
}
super(new ServiceWorker(), engineConfig);
// Keep alive through periodical heartbeat signals
setInterval(() => {
this.worker.postMessage({ kind: "keepAlive", uuid: crypto.randomUUID() });
this.missedHeatbeat += 1;
log.trace("missedHeatbeat", this.missedHeatbeat);
}, keepAliveMs);
}
onmessage(event: any): void {
const msg = event.data;
log.trace(
`MLC client message: [${msg.kind}] ${JSON.stringify(msg.content)}`,
);
try {
if (msg.kind === "heartbeat") {
this.missedHeatbeat = 0;
return;
}
super.onmessage(msg);
} catch (err: any) {
// This is expected to throw if user has multiple windows open
if (!err.message.startsWith("return from a unknown uuid")) {
log.error("CreateWebServiceWorkerMLCEngine.onmessage", err);
}
}
}
}