(messages: readonly Message[])
| 224 | * so every interleaved tool_result is included in the rough estimate. |
| 225 | */ |
| 226 | export function tokenCountWithEstimation(messages: readonly Message[]): number { |
| 227 | let i = messages.length - 1 |
| 228 | while (i >= 0) { |
| 229 | const message = messages[i] |
| 230 | const usage = message ? getTokenUsage(message) : undefined |
| 231 | if (message && usage) { |
| 232 | // Walk back past any earlier sibling records split from the same API |
| 233 | // response (same message.id) so interleaved tool_results between them |
| 234 | // are included in the estimation slice. |
| 235 | const responseId = getAssistantMessageId(message) |
| 236 | if (responseId) { |
| 237 | let j = i - 1 |
| 238 | while (j >= 0) { |
| 239 | const prior = messages[j] |
| 240 | const priorId = prior ? getAssistantMessageId(prior) : undefined |
| 241 | if (priorId === responseId) { |
| 242 | // Earlier split of the same API response — anchor here instead. |
| 243 | i = j |
| 244 | } else if (priorId !== undefined) { |
| 245 | // Hit a different API response — stop walking. |
| 246 | break |
| 247 | } |
| 248 | // priorId === undefined: a user/tool_result/attachment message, |
| 249 | // possibly interleaved between splits — keep walking. |
| 250 | j-- |
| 251 | } |
| 252 | } |
| 253 | return ( |
| 254 | getTokenCountFromUsage(usage) + |
| 255 | roughTokenCountEstimationForMessages(messages.slice(i + 1)) |
| 256 | ) |
| 257 | } |
| 258 | i-- |
| 259 | } |
| 260 | return roughTokenCountEstimationForMessages(messages) |
| 261 | } |
| 262 |
no test coverage detected