Skip to content

Commit

Permalink
Use comlink without vite-plugin-comlink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ZauberNerd committed Jul 8, 2023
1 parent ff9f351 commit 0701303
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
3 changes: 1 addition & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"@vitejs/plugin-react": "^4.0.2",
"babel-plugin-formatjs": "^10.5.3",
"typescript": "^4.9.5",
"vite": "^4.4.1",
"vite-plugin-comlink": "^3.0.5"
"vite": "^4.4.1"
}
}
5 changes: 4 additions & 1 deletion app/src/core/tokenizer/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expose } from "comlink";
import * as methods from ".";
import { OpenAIMessage } from "../chat/types";
import { ChatHistoryTrimmer, ChatHistoryTrimmerOptions } from "./chat-history-trimmer";
Expand All @@ -13,4 +14,6 @@ export function countTokensForText(text: string) {

export function countTokensForMessages(messages: OpenAIMessage[]) {
return methods.countTokensForMessages(messages);
}
}

expose({ runChatTrimmer, countTokensForText, countTokensForMessages });
5 changes: 3 additions & 2 deletions app/src/core/tokenizer/wrapper.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
5 changes: 0 additions & 5 deletions app/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import { comlink } from "vite-plugin-comlink";

export default defineConfig(() => {
return {
Expand Down Expand Up @@ -35,10 +34,6 @@ export default defineConfig(() => {
],
},
}),
comlink(),
],
worker: {
plugins: [comlink()],
},
};
});

0 comments on commit 0701303

Please sign in to comment.