Roo-Code/src/utils/countTokens.ts
Chris Estreich 65cb9244ee
Count tokens worker (#3037)
* Count tokens worker

* Appease knip
2025-04-29 12:29:25 -07:00

45 lines
1.1 KiB
TypeScript

import { Anthropic } from "@anthropic-ai/sdk"
import workerpool from "workerpool"
import { countTokensResultSchema } from "../workers/types"
import { tiktoken } from "./tiktoken"
let pool: workerpool.Pool | null | undefined = undefined
export type CountTokensOptions = {
useWorker?: boolean
}
export async function countTokens(
content: Anthropic.Messages.ContentBlockParam[],
{ useWorker = true }: CountTokensOptions = {},
): Promise<number> {
// Lazily create the worker pool if it doesn't exist.
if (useWorker && typeof pool === "undefined") {
pool = workerpool.pool(__dirname + "/workers/countTokens.js", {
maxWorkers: 1,
maxQueueSize: 10,
})
}
// If the worker pool doesn't exist or the caller doesn't want to use it
// then, use the non-worker implementation.
if (!useWorker || !pool) {
return tiktoken(content)
}
try {
const data = await pool.exec("countTokens", [content])
const result = countTokensResultSchema.parse(data)
if (!result.success) {
throw new Error(result.error)
}
return result.count
} catch (error) {
pool = null
console.error(error)
return tiktoken(content)
}
}