mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-26 06:01:49 +00:00
feat: fork ink and make it work nicely
This commit is contained in:
parent
cb79018977
commit
8760faf991
139 changed files with 24952 additions and 140 deletions
28
ui-tui/packages/hermes-ink/src/ink/line-width-cache.ts
Normal file
28
ui-tui/packages/hermes-ink/src/ink/line-width-cache.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { stringWidth } from './stringWidth.js'
|
||||
|
||||
// During streaming, text grows but completed lines are immutable.
|
||||
// Caching stringWidth per-line avoids re-measuring hundreds of
|
||||
// unchanged lines on every token (~50x reduction in stringWidth calls).
|
||||
const cache = new Map<string, number>()
|
||||
|
||||
const MAX_CACHE_SIZE = 4096
|
||||
|
||||
export function lineWidth(line: string): number {
|
||||
const cached = cache.get(line)
|
||||
|
||||
if (cached !== undefined) {
|
||||
return cached
|
||||
}
|
||||
|
||||
const width = stringWidth(line)
|
||||
|
||||
// Evict when cache grows too large (e.g. after many different responses).
|
||||
// Simple full-clear is fine — the cache repopulates in one frame.
|
||||
if (cache.size >= MAX_CACHE_SIZE) {
|
||||
cache.clear()
|
||||
}
|
||||
|
||||
cache.set(line, width)
|
||||
|
||||
return width
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue