Spaces:
Running
Running
File size: 928 Bytes
1a6d0d2 | 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 | /// <reference lib="webworker" />
import { allocatePageExcerpts } from "./pageExcerptAllocation";
import type {
WorkerRequest,
WorkerResponse,
} from "./pageExcerptWorkerProtocol";
/**
* Answers one allocation request with the worker's response message.
*
* Exported as a plain function so the handler can be tested directly under
* jsdom - no bundler, no live worker - and so the worker's answer is
* provably the same computation the synchronous fallback runs.
*/
export function handleExcerptRequest(data: WorkerRequest): WorkerResponse {
try {
return {
type: "excerpts",
excerpts: allocatePageExcerpts(data.contents, data.tokenBudget),
};
} catch (error) {
return {
type: "error",
message: error instanceof Error ? error.message : String(error),
};
}
}
self.onmessage = ({ data }: MessageEvent<WorkerRequest>) => {
self.postMessage(handleExcerptRequest(data));
};
|