(messages: readonly Message[])
| 249 | * so every interleaved tool_result is included in the rough estimate. |
| 250 | */ |
| 251 | export function tokenCountWithEstimation(messages: readonly Message[]): number { |
| 252 | let i = messages.length - 1 |
| 253 | while (i >= 0) { |
| 254 | const message = messages[i] |
| 255 | const usage = message ? getTokenUsage(message) : undefined |
| 256 | if (message && usage) { |
| 257 | // Walk back past any earlier sibling records split from the same API |
| 258 | // response (same message.id) so interleaved tool_results between them |
| 259 | // are included in the estimation slice. |
| 260 | const responseId = getAssistantMessageId(message) |
| 261 | if (responseId) { |
| 262 | let j = i - 1 |
| 263 | while (j >= 0) { |
| 264 | const prior = messages[j] |
| 265 | const priorId = prior ? getAssistantMessageId(prior) : undefined |
| 266 | if (priorId === responseId) { |
| 267 | // Earlier split of the same API response — anchor here instead. |
| 268 | i = j |
| 269 | } else if (priorId !== undefined) { |
| 270 | // Hit a different API response — stop walking. |
| 271 | break |
| 272 | } |
| 273 | // priorId === undefined: a user/tool_result/attachment message, |
| 274 | // possibly interleaved between splits — keep walking. |
| 275 | j-- |
| 276 | } |
| 277 | } |
| 278 | return ( |
| 279 | getTokenCountFromUsage(usage) + |
| 280 | roughTokenCountEstimationForMessages( |
| 281 | messages.slice(i + 1) as Parameters< |
| 282 | typeof roughTokenCountEstimationForMessages |
| 283 | >[0], |
| 284 | ) |
| 285 | ) |
| 286 | } |
| 287 | i-- |
| 288 | } |
| 289 | return roughTokenCountEstimationForMessages( |
| 290 | messages as Parameters<typeof roughTokenCountEstimationForMessages>[0], |
| 291 | ) |
| 292 | } |
no test coverage detected