hermes-agent-features/ui-tui/src/domain/viewport.ts
Brooklyn Nicholson 9bf6e1cd6e refactor(ui-tui): clean touched resize and sticky prompt paths
Trim comment noise, remove redundant typing, normalize sticky prompt viewport args to top→bottom order, and reuse one sticky viewport helper instead of duplicating the math.
2026-04-23 14:37:00 -05:00

48 lines
1.0 KiB
TypeScript

import type { Msg } from '../types.js'
import { userDisplay } from './messages.js'
const upperBound = (offsets: ArrayLike<number>, target: number) => {
let lo = 0
let hi = offsets.length
while (lo < hi) {
const mid = (lo + hi) >> 1
offsets[mid]! <= target ? (lo = mid + 1) : (hi = mid)
}
return lo
}
export const stickyPromptFromViewport = (
messages: readonly Msg[],
offsets: ArrayLike<number>,
top: number,
bottom: number,
sticky: boolean
) => {
if (sticky || !messages.length) {
return ''
}
const first = Math.max(0, Math.min(messages.length - 1, upperBound(offsets, top) - 1))
const last = Math.max(first, Math.min(messages.length - 1, upperBound(offsets, bottom) - 1))
for (let i = first; i <= last; i++) {
if (messages[i]?.role === 'user') {
return ''
}
}
for (let i = first - 1; i >= 0; i--) {
if (messages[i]?.role !== 'user') {
continue
}
return (offsets[i] ?? 0) + 1 < top ? userDisplay(messages[i]!.text.trim()).replace(/\s+/g, ' ').trim() : ''
}
return ''
}