Use comlink without vite-plugin-comlink

For some reason the vite-plugin-comlink doesn't seem to work in this
constellation.
I tried to reproduce in a new vite project and there it did work.
But also, the plugin does some horrible magic by replacing a
non-existant constructor `ComLinkWorker()` with the `wrap()` method
and then the only real benefit is, that we wouldn't need to write the
`expose()` method in the worker. So for that reason I'm removing the
plugin and using the comlink package directly.
This commit is contained in:
ZauberNerd
2023-07-08 15:25:25 +02:00
parent ff9f3517ec
commit 0701303563
4 changed files with 8 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
import { expose } from "comlink";
import * as methods from ".";
import { OpenAIMessage } from "../chat/types";
import { ChatHistoryTrimmer, ChatHistoryTrimmerOptions } from "./chat-history-trimmer";
@@ -13,4 +14,6 @@ export function countTokensForText(text: string) {
export function countTokensForMessages(messages: OpenAIMessage[]) {
return methods.countTokensForMessages(messages);
}
}
expose({ runChatTrimmer, countTokensForText, countTokensForMessages });

View File

@@ -1,10 +1,11 @@
import { wrap } from "comlink";
import { OpenAIMessage } from "../chat/types";
import type { ChatHistoryTrimmerOptions } from "./chat-history-trimmer";
// @ts-ignore
import tokenizer from "./worker?worker&url";
const worker = new ComlinkWorker<typeof import("./worker")>(
new URL(tokenizer, import.meta.url)
const worker = wrap<typeof import("./worker")>(
new Worker(new URL(tokenizer, import.meta.url), { type: "module" })
);
export async function runChatTrimmer(