File size: 2,274 Bytes
7b0f1ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Read {text,context?,threshold?} objects as JSON lines. Assets and inference stay on this host.
import {readFile} from "node:fs/promises";
import {resolve,relative,sep} from "node:path";
import {pathToFileURL,fileURLToPath} from "node:url";
import {createInterface} from "node:readline";
const site = resolve(process.argv[2] ?? fileURLToPath(new URL("../browser/site",import.meta.url)));
const assetUrl = name => pathToFileURL(resolve(site,name)).href;
globalThis.fetch = async url => {
  const target=new URL(url);
  if(target.protocol!=="file:") throw new Error("Only bundled assets are supported");
  const path=fileURLToPath(target);
  const within=relative(site,path);
  if(within===".." || within.startsWith(".."+sep)) throw new Error("Asset outside model directory");
  return new Response(await readFile(path));
};
let output;
globalThis.self={postMessage:value=>{output=value;}};
await import(assetUrl("worker.mjs"));
const {filtered}=await import(assetUrl("core.mjs"));
const reader=createInterface({input:process.stdin,crlfDelay:Infinity,terminal:false});
let sequence=0;
for await(const line of reader){
  if(!line.trim()) continue;
  try{
    const request=JSON.parse(line);
    if(!request || typeof request.text!=="string" || request.text.length>12000 ||
       (request.context!==undefined && (typeof request.context!=="string" || request.context.length>512)))
      throw new Error("Expected a reply of at most 12000 characters and an optional question of at most 512");
    const threshold=request.threshold??0;
    if(!Number.isFinite(threshold)||threshold<0||threshold>1) throw new Error("Invalid threshold");
    output=undefined;
    const start=performance.now();
    await self.onmessage({data:{id:++sequence,text:request.text,context:request.context}});
    if(!output || output.error) throw new Error(output?.error ?? "Worker did not produce output");
    const result=filtered(request.text,output.groups,threshold,output.model);
    console.log(JSON.stringify({...result,elapsed_ms:Math.round((performance.now()-start)*100)/100}));
  }catch(error){
    // Do not repeat input or parser details on invalid requests.
    console.log(JSON.stringify({error:error instanceof SyntaxError?"Invalid JSON":error.message}));
    process.exitCode=1;
  }
}