@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
35 lines • 1.16 kB
JavaScript
const DEFAULT_CAPACITY = 256 * 1024;
export class TextTap {
#capacity;
#text = "";
/** Offset of `#text[0]`. */
#start = 0;
#open = true;
constructor(id, label, capacity = DEFAULT_CAPACITY) {
this.id = id;
this.label = label;
this.#capacity = Math.max(1, capacity);
}
append(text) {
if (!this.#open || text.length === 0)
return;
this.#text += text;
// Trimmed in batches so a stream of small appends does not copy the retained text every chunk.
const excess = this.#text.length - this.#capacity;
if (excess > this.#capacity / 4) {
this.#text = this.#text.slice(excess);
this.#start += excess;
}
}
/** No more text will arrive. */
close() {
this.#open = false;
}
read(from) {
const end = this.#start + this.#text.length;
const kept = Math.max(this.#start, end - this.#capacity);
const start = Math.min(Math.max(from, kept), end);
return { start, end, text: this.#text.slice(start - this.#start), open: this.#open };
}
}
//# sourceMappingURL=text-tap.js.map