fix(ui): exclude tool metadata from token estimate

This commit is contained in:
Shantur Rathore
2026-03-03 13:32:48 +00:00
parent cd6266757d
commit c766b5ab62

View File

@@ -7,8 +7,10 @@ import type { ClientPart } from "../types/message"
* bulk-delete toolbar token pills (message-section) so both surfaces
* derive token estimates from the same logic.
*
* Skips `filediff` metadata — it contains full before/after file content
* and would inflate the character count by 10-100x for large files.
* Note: For tool parts we intentionally only count `state.input` and
* `state.output`. We exclude `state.metadata` from token estimation since
* metadata can contain large or verbose diagnostic payloads that are not
* representative of model context.
*/
export function getPartCharCount(part: ClientPart): number {
if (!part) return 0
@@ -35,18 +37,6 @@ export function getPartCharCount(part: ClientPart): number {
} catch {}
}
}
if (state.metadata) {
for (const [key, val] of Object.entries(state.metadata)) {
if (key === "filediff") continue
if (typeof val === "string") {
count += val.length
} else if (val && typeof val === "object") {
try {
count += JSON.stringify(val).length
} catch {}
}
}
}
}
}