|
| 1 | +import type { AssistantMessage } from "@earendil-works/pi-ai"; |
| 2 | +import { getGraphemeSegmenter } from "@earendil-works/pi-tui"; |
| 3 | + |
| 4 | +type AssistantContentBlock = AssistantMessage["content"][number]; |
| 5 | +export type GraphemeCounter = (index: number, text: string) => number; |
| 6 | +export type GraphemeSlicer = (index: number, text: string, units: number) => string; |
| 7 | + |
| 8 | +function countGraphemesFrom(text: string, start: number): { count: number; tailStart: number } { |
| 9 | + let count = 0; |
| 10 | + let tailStart = start; |
| 11 | + for (const segment of getGraphemeSegmenter().segment(start === 0 ? text : text.slice(start))) { |
| 12 | + count += 1; |
| 13 | + tailStart = start + segment.index; |
| 14 | + } |
| 15 | + return { count, tailStart }; |
| 16 | +} |
| 17 | + |
| 18 | +function segmentFrom(text: string, start: number, clusters: number): { count: number; end: number; lastStart: number } { |
| 19 | + let count = 0; |
| 20 | + let end = start; |
| 21 | + let lastStart = start; |
| 22 | + for (const segment of getGraphemeSegmenter().segment(start === 0 ? text : text.slice(start))) { |
| 23 | + count += 1; |
| 24 | + lastStart = start + segment.index; |
| 25 | + end = lastStart + segment.segment.length; |
| 26 | + if (count >= clusters) break; |
| 27 | + } |
| 28 | + return { count, end, lastStart }; |
| 29 | +} |
| 30 | + |
| 31 | +export class BlockUnitCounter { |
| 32 | + readonly #entries = new Map<number, { text: string; count: number; tailStart: number }>(); |
| 33 | + readonly #sliceEntries = new Map<number, { text: string; units: number; end: number; lastStart: number }>(); |
| 34 | + |
| 35 | + count(index: number, text: string): number { |
| 36 | + const entry = this.#entries.get(index); |
| 37 | + if (entry !== undefined) { |
| 38 | + if (entry.text === text) return entry.count; |
| 39 | + if (entry.count > 0 && text.length > entry.text.length && text.startsWith(entry.text)) { |
| 40 | + const tail = countGraphemesFrom(text, entry.tailStart); |
| 41 | + const next = { text, count: entry.count - 1 + tail.count, tailStart: tail.tailStart }; |
| 42 | + this.#entries.set(index, next); |
| 43 | + return next.count; |
| 44 | + } |
| 45 | + } |
| 46 | + const full = countGraphemesFrom(text, 0); |
| 47 | + this.#entries.set(index, { text, count: full.count, tailStart: full.tailStart }); |
| 48 | + return full.count; |
| 49 | + } |
| 50 | + |
| 51 | + slice(index: number, text: string, units: number): string { |
| 52 | + const wholeUnits = Math.floor(units); |
| 53 | + if (wholeUnits <= 0 || text.length === 0) return ""; |
| 54 | + const entry = this.#sliceEntries.get(index); |
| 55 | + if (entry?.text === text && entry.units === wholeUnits) { |
| 56 | + return entry.end >= text.length ? text : text.slice(0, entry.end); |
| 57 | + } |
| 58 | + if (entry !== undefined && (entry.text === text || text.startsWith(entry.text)) && wholeUnits >= entry.units) { |
| 59 | + const segment = segmentFrom(text, entry.lastStart, wholeUnits - entry.units + 1); |
| 60 | + this.#sliceEntries.set(index, { |
| 61 | + text, |
| 62 | + units: entry.units - 1 + segment.count, |
| 63 | + end: segment.end, |
| 64 | + lastStart: segment.lastStart, |
| 65 | + }); |
| 66 | + return segment.end >= text.length ? text : text.slice(0, segment.end); |
| 67 | + } |
| 68 | + const segment = segmentFrom(text, 0, wholeUnits); |
| 69 | + this.#sliceEntries.set(index, { |
| 70 | + text, |
| 71 | + units: segment.count, |
| 72 | + end: segment.end, |
| 73 | + lastStart: segment.lastStart, |
| 74 | + }); |
| 75 | + return segment.end >= text.length ? text : text.slice(0, segment.end); |
| 76 | + } |
| 77 | + |
| 78 | + reset(): void { |
| 79 | + this.#entries.clear(); |
| 80 | + this.#sliceEntries.clear(); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function countGraphemes(text: string): number { |
| 85 | + return countGraphemesFrom(text, 0).count; |
| 86 | +} |
| 87 | + |
| 88 | +function sliceGraphemes(text: string, units: number): string { |
| 89 | + if (units <= 0 || text.length === 0) return ""; |
| 90 | + const segment = segmentFrom(text, 0, units); |
| 91 | + return segment.end >= text.length ? text : text.slice(0, segment.end); |
| 92 | +} |
| 93 | + |
| 94 | +export function countVisibleUnits(message: AssistantMessage, hideThinking: boolean, countOf: GraphemeCounter): number { |
| 95 | + let total = 0; |
| 96 | + for (let index = 0; index < message.content.length; index++) { |
| 97 | + const block = message.content[index]; |
| 98 | + if (block?.type === "text") { |
| 99 | + total += countOf(index, block.text); |
| 100 | + } else if (block?.type === "thinking" && !hideThinking) { |
| 101 | + total += countOf(index, block.thinking); |
| 102 | + } |
| 103 | + } |
| 104 | + return total; |
| 105 | +} |
| 106 | + |
| 107 | +export function visibleUnits(message: AssistantMessage, hideThinking: boolean): number { |
| 108 | + return countVisibleUnits(message, hideThinking, (_index, text) => countGraphemes(text)); |
| 109 | +} |
| 110 | + |
| 111 | +export function buildDisplayMessage( |
| 112 | + target: AssistantMessage, |
| 113 | + revealed: number, |
| 114 | + hideThinking: boolean, |
| 115 | + countOf: GraphemeCounter = (_index, text) => countGraphemes(text), |
| 116 | + sliceOf: GraphemeSlicer = (_index, text, units) => sliceGraphemes(text, units), |
| 117 | +): AssistantMessage { |
| 118 | + let remaining = Math.max(0, Math.floor(revealed)); |
| 119 | + const content: AssistantContentBlock[] = []; |
| 120 | + for (let index = 0; index < target.content.length; index++) { |
| 121 | + const block = target.content[index]; |
| 122 | + if (!block) continue; |
| 123 | + if (block.type === "text") { |
| 124 | + const units = countOf(index, block.text); |
| 125 | + content.push( |
| 126 | + remaining <= 0 |
| 127 | + ? block.text.length === 0 |
| 128 | + ? block |
| 129 | + : { ...block, text: "" } |
| 130 | + : remaining >= units |
| 131 | + ? block |
| 132 | + : { ...block, text: sliceOf(index, block.text, remaining) }, |
| 133 | + ); |
| 134 | + remaining = Math.max(0, remaining - units); |
| 135 | + } else if (block.type === "thinking" && !hideThinking) { |
| 136 | + const units = countOf(index, block.thinking); |
| 137 | + content.push( |
| 138 | + remaining <= 0 |
| 139 | + ? block.thinking.length === 0 |
| 140 | + ? block |
| 141 | + : { ...block, thinking: "" } |
| 142 | + : remaining >= units |
| 143 | + ? block |
| 144 | + : { ...block, thinking: sliceOf(index, block.thinking, remaining) }, |
| 145 | + ); |
| 146 | + remaining = Math.max(0, remaining - units); |
| 147 | + } else { |
| 148 | + content.push(block); |
| 149 | + } |
| 150 | + } |
| 151 | + return { ...target, content }; |
| 152 | +} |
0 commit comments