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

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

21 lines
529 B
TypeScript

import workerpool from "workerpool"
import { Anthropic } from "@anthropic-ai/sdk"
import { tiktoken } from "../utils/tiktoken"
import { type CountTokensResult } from "./types"
async function countTokens(content: Anthropic.Messages.ContentBlockParam[]): Promise<CountTokensResult> {
try {
const count = await tiktoken(content)
return { success: true, count }
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : "Unknown error",
}
}
}
workerpool.worker({ countTokens })