Skip to main content
Glama
MIT License
27,120
19,780
  • Linux
  • Apple
tokenCounterFactory.ts•1.1 kB
import type { TiktokenEncoding } from 'tiktoken'; import { logger } from '../../shared/logger.js'; import { TokenCounter } from './TokenCounter.js'; // Worker-level cache for TokenCounter instances by encoding const tokenCounters = new Map<TiktokenEncoding, TokenCounter>(); /** * Get or create a TokenCounter instance for the given encoding. * This ensures only one TokenCounter exists per encoding per worker thread to optimize memory usage. */ export const getTokenCounter = (encoding: TiktokenEncoding): TokenCounter => { let tokenCounter = tokenCounters.get(encoding); if (!tokenCounter) { tokenCounter = new TokenCounter(encoding); tokenCounters.set(encoding, tokenCounter); } return tokenCounter; }; /** * Free all TokenCounter resources and clear the cache. * This should be called when the worker is terminating. */ export const freeTokenCounters = (): void => { for (const [encoding, tokenCounter] of tokenCounters.entries()) { tokenCounter.free(); logger.debug(`Freed TokenCounter resources for encoding: ${encoding}`); } tokenCounters.clear(); };

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yamadashy/repomix'

If you have feedback or need assistance with the MCP directory API, please join our Discord server