File size: 8,400 Bytes
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// llm.js-style wrapper around latest llama.cpp compiled to WASM.
//
// The public API is the one from rahuldshetty/llm.js:
//   new LLM(type, url, init_callback, write_result_callback, on_complete_callback)
//   app.load_worker(); app.run({ prompt, ... });
//
// Additions (all optional):
//   on_error_callback(err), on_progress_callback({ phase, loaded, total, ... }),
//   on_log_callback(line), on_event(message)
//   app.add_url_model({ url, name, expected_sha256 })
//   app.add_file_model({ file, name, keep_copy })
//   app.pause() / app.resume() / app.delete_model(id) / app.list_models()
//
// Deviations from the original: models live in a local library (uuid record per
// model, see storage.js), so `url` became a target object { id, url, name }. A
// plain url string is still accepted and treated as `{ url }`.
//
// The two ingest paths (url, local file) produce the same record and are loaded
// by the same code path; only where the bytes come from differs.

import { action } from "./actions.js";

export const MODELS = {
    GGUF_CPU: "GGUF_CPU",
};

class LLM {
    constructor(
        type,
        target,
        init_callback,
        write_result_callback,
        on_complete_callback,
        on_error_callback,
        on_progress_callback,
        on_log_callback,
    ) {
        this.type = type;
        this.model = typeof target === "string" ? { url: target } : (target || {});
        this.init_callback = init_callback;
        this.write_result_callback = write_result_callback;
        this.on_complete_callback = on_complete_callback;
        this.on_error_callback = on_error_callback;
        this.on_progress_callback = on_progress_callback;
        this.on_log_callback = on_log_callback;
        this.on_event = null; // any message, e.g. PAUSED / DELETED / MODELS

        this.worker = null;
        this.model_loaded = false;
        this.context_size = 2048;
        this._pending = null;
    }

    get url() {
        return this.model.url;
    }

    // autoload: post LOAD for this.model right away (the original behaviour);
    // pass false to only start the worker and drive loads yourself
    load_worker(options = {}) {
        switch (this.type) {
            case MODELS.GGUF_CPU:
                this.worker = new Worker(new URL("./worker.js", import.meta.url), { type: "module" });
                break;
            default:
                throw new Error(`unknown model type: ${this.type}`);
        }

        this.context_size = options.context_size || 2048;

        this.worker.onmessage = (event) => {
            const data = event.data;
            if (this.on_event) this.on_event(data);

            switch (data.event) {
                case action.INITIALIZED:
                    this.model_loaded = true;
                    if (this.init_callback) this.init_callback(data);
                    this._flushPending();
                    break;

                case action.WRITE_RESULT:
                    if (this.write_result_callback) this.write_result_callback(data.line);
                    break;

                case action.RUN_COMPLETED:
                    if (this.on_complete_callback) this.on_complete_callback(data);
                    break;

                case action.PROGRESS:
                    if (this.on_progress_callback) this.on_progress_callback(data);
                    break;

                case action.LOG:
                    if (this.on_log_callback) this.on_log_callback(data.line);
                    break;

                case action.ERROR:
                    if (this.on_error_callback) this.on_error_callback(data.error);
                    else console.error("llm.js:", data.error);
                    break;
            }
        };

        const autoload = options.autoload !== false;
        if (autoload && (this.model.id || this.model.url)) {
            this.worker.postMessage({
                event: action.LOAD,
                id: this.model.id || null,
                url: this.model.url || null,
                context_size: this.context_size,
            });
        }
    }

    post(message) {
        if (this.worker) this.worker.postMessage(message);
    }

    // download a GGUF and add it to the library (then load it)
    add_url_model({ url, name = "", expected_sha256 = "", context_size = 0 } = {}) {
        this.post({
            event: action.ADD_URL,
            url,
            name,
            expectedSha256: expected_sha256,
            context_size: context_size || this.context_size,
        });
    }

    // register a File the user already has; keep_copy decides whether the bytes
    // are duplicated into IndexedDB (default: no, read them in place)
    add_file_model({ file, name = "", keep_copy = false, expected_sha256 = "", context_size = 0 } = {}) {
        this.post({
            event: action.ADD_FILE,
            file,
            name,
            keepCopy: keep_copy,
            expectedSha256: expected_sha256,
            context_size: context_size || this.context_size,
        });
    }

    // load a model from the library by id
    load(id, file = null) {
        this.post({
            event: action.LOAD,
            id,
            file,
            context_size: this.context_size,
        });
    }

    // free the engine and load the same model again
    reload(id) {
        this.post({ event: action.RELOAD_MODEL, id: id || this.model.id, context_size: this.context_size });
    }

    // render a prompt through a chat template without generating; the result
    // arrives as a TEMPLATE_PREVIEW event
    preview_template({ prompt, template_source = "", messages = null,
                       tools = "[]", template_kwargs = "{}" } = {}) {
        this.post({
            event: action.PREVIEW_TEMPLATE,
            prompt,
            template_source,
            messages,
            tools,
            template_kwargs,
        });
    }

    // stop a running generation; the text streamed so far is kept and the run
    // completes with interrupted: true
    stop() {
        this.post({ event: action.STOP });
    }

    // resume an interrupted download / reload the same model
    resume() {
        this.post({ event: action.RESUME });
    }

    pause() {
        this.post({ event: action.PAUSE });
    }

    // drop a model (or the current one) from the library
    delete_model(id) {
        const target = id || this.model.id;
        if (target) this.post({ event: action.DELETE_MODEL, id: target });
    }

    hash_model(id) {
        this.post({ event: action.HASH_MODEL, id: id || this.model.id });
    }

    list_models() {
        this.post({ event: action.LIST_MODELS });
    }

    // mode "chat" runs a full message list through the chat template;
    // mode "completion" sends the prompt text as-is
    run({
        mode = "completion",
        prompt = "",
        messages = null,
        stop = [],
        chat = false,
        seed = -1,
        max_token_len = 64,
        top_k = 40,
        top_p = 0.9,
        temp = 0.7,
        repeat_last_n = 32,
        repeat_penalty = 1.1,
        context_size = 0,
        chat_template = "",
        chat_template_source = "",
        tools = "[]",
        template_kwargs = "{}",
        grammar = "",
        json_schema = "",
        regex = "",
    } = {}) {
        const msg = {
            event: action.RUN_MAIN,
            mode,
            prompt,
            messages,
            stop,
            chat,
            seed,
            max_token_len,
            top_k,
            top_p,
            temp,
            repeat_last_n,
            repeat_penalty,
            context_size,
            chat_template,
            chat_template_source,
            tools,
            template_kwargs,
            grammar,
            json_schema,
            regex,
        };

        // allowed before the model finished loading, like the original docs suggest
        if (!this.model_loaded) {
            this._pending = msg;
            return;
        }
        this.post(msg);
    }

    _flushPending() {
        if (!this._pending) return;
        const msg = this._pending;
        this._pending = null;
        this.post(msg);
    }

    terminate() {
        if (this.worker) this.worker.terminate();
        this.worker = null;
        this.model_loaded = false;
    }
}

export { LLM };