| 33 | |
| 34 | // this class does not yet do anything asynchronous |
| 35 | export class GPTAsyncEncoder implements AsyncEncoder { |
| 36 | private workerPool: workerpool.Pool; |
| 37 | |
| 38 | constructor() { |
| 39 | this.workerPool = workerpool.pool( |
| 40 | workerCodeFilePath("tiktokenWorkerPool.mjs"), |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | async encode(text: string): Promise<number[]> { |
| 45 | return this.workerPool.exec("encode", [text]); |
| 46 | } |
| 47 | |
| 48 | async decode(tokens: number[]): Promise<string> { |
| 49 | return this.workerPool.exec("decode", [tokens]); |
| 50 | } |
| 51 | |
| 52 | // TODO: this should be called somewhere before exit or potentially with a shutdown hook |
| 53 | public async close(): Promise<void> { |
| 54 | await this.workerPool.terminate(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | function workerCodeFilePath(workerFileName: string): string { |
| 59 | if (process.env.NODE_ENV === "test") { |
nothing calls this directly
no outgoing calls
no test coverage detected