import type { ChatContextTokenUsage, TokenUsageType } from '.'; import type { ModelContextWindow } from '..'; import type { Message, PromptString } from '..'; /** * A class to manage the token allocation during prompt building. */ export declare class TokenCounter { /** * The maximum number of tokens that can be used by Chat Messages. */ readonly maxChatTokens: number; /** * The maximum number of tokens that can be used by each context type: * - User-Context: tokens reserved for User-added context, like @-mentions. * - Enhanced-Context: % (defined by ENHANCED_CONTEXT_ALLOCATION) of the latest Chat budget. */ readonly maxContextTokens: ChatContextTokenUsage; /** * The number of tokens used by chat and context respectively. */ private usedTokens; /** * Indicates whether the Chat and User-Context share the same token budget. * - If true, all types of messages share the same token budget with Chat. * - If false (Feature Flag required), the User-Context will has a separated budget. * NOTE: Used in remainingTokens for calculating the remaining token budget for each budget type. */ private shareChatAndUserBudget; constructor(contextWindow: ModelContextWindow); /** * Updates the token usage for the messages of a specified token usage type. * * @param type - The type of token usage to update. * @param messages - The messages to calculate the token count for. * @returns `true` if the token usage can be allocated, `false` otherwise. */ updateUsage(type: TokenUsageType, messages: Message[]): boolean; /** * NOTE: Should only be used by @canAllocateTokens to determine if the token usage can be allocated in correct order. * * Calculates the remaining token budget for each token usage type. * * @returns The remaining token budget for chat, User-Context, and Enhanced-Context (if applicable). */ private get remainingTokens(); /** * Checks if the specified token usage type has enough remaining tokens to allocate the given count. * * @param type - The type of token usage to check. * @param count - The number of tokens to allocate. * @returns `true` if the tokens can be allocated, `false` otherwise. */ private canAllocateTokens; /** * The default tokenizer is cl100k_base. */ private static tokenizer; /** * Encode the given text using the tokenizer. * The text is first normalized to NFKC to handle different character representations consistently. * All special tokens are included in the token count. */ static encode(text: string): number[]; static decode(encoded: number[]): string; /** * Counts the number of tokens in the given text using the tokenizer. * * @param text - The input text to count tokens for. * @returns The number of tokens in the input text. */ static countTokens(text: string): number; static countPromptString(text: PromptString): number; /** * Counts the number of tokens in the given message using the tokenizer. * * @param message - The message to count tokens for. * @returns The number of tokens in the message. */ private static getTokenCountForMessage; /** * Calculates the total number of tokens across the given array of messages. * * @param messages - An array of messages to count the tokens for. * @returns The total number of tokens in the provided messages. */ static getMessagesTokenCount(messages: Message[]): number; } //# sourceMappingURL=counter.d.ts.map