(message: TokenEstimatableMessage)
| 55 | } |
| 56 | |
| 57 | export function estimateTokensForMessage(message: TokenEstimatableMessage): number { |
| 58 | const cached = messageTokenEstimateCache.get(message); |
| 59 | if (cached !== undefined) { |
| 60 | return cached; |
| 61 | } |
| 62 | |
| 63 | let total = estimateTokens(message.role); |
| 64 | total += estimateTokensForContentParts(message.content); |
| 65 | if (message.toolCalls !== undefined) { |
| 66 | for (const call of message.toolCalls) { |
| 67 | total += estimateTokens(call.name); |
| 68 | total += estimateTokens(JSON.stringify(call.arguments)); |
| 69 | } |
| 70 | } |
| 71 | messageTokenEstimateCache.set(message, total); |
| 72 | return total; |
| 73 | } |
| 74 | |
| 75 | export function estimateTokensForContentParts(parts: readonly ContentPart[]): number { |
| 76 | let total = 0; |
no test coverage detected