|
| 1 | +interface OutputBuilderOptions { |
| 2 | + maxSize?: number // Max size of the buffer (in bytes). |
| 3 | + preserveStartPercent?: number // % of `maxSize` to preserve at start. |
| 4 | + preserveEndPercent?: number // % of `maxSize` to preserve at end |
| 5 | + truncationMessage?: string |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * OutputBuilder manages terminal output with intelligent middle truncation. |
| 10 | + * |
| 11 | + * When output exceeds a specified size limit, this class truncates content |
| 12 | + * primarily from the middle, preserving both the beginning (command context) |
| 13 | + * and the end (recent output) of the buffer for better diagnostic context. |
| 14 | + */ |
| 15 | +export class OutputBuilder { |
| 16 | + public readonly preserveStartSize: number |
| 17 | + public readonly preserveEndSize: number |
| 18 | + public readonly truncationMessage: string |
| 19 | + |
| 20 | + private startBuffer = "" |
| 21 | + private endBuffer = "" |
| 22 | + private _bytesProcessed = 0 |
| 23 | + private _bytesRemoved = 0 |
| 24 | + private _cursor = 0 |
| 25 | + |
| 26 | + constructor({ |
| 27 | + maxSize = 100 * 1024, // 100KB |
| 28 | + preserveStartPercent = 50, // 50% of `maxSize` |
| 29 | + preserveEndPercent = 50, // 50% of `maxSize` |
| 30 | + truncationMessage = "\n[... OUTPUT TRUNCATED ...]\n", |
| 31 | + }: OutputBuilderOptions = {}) { |
| 32 | + this.preserveStartSize = Math.floor((preserveStartPercent / 100) * maxSize) |
| 33 | + this.preserveEndSize = Math.floor((preserveEndPercent / 100) * maxSize) |
| 34 | + |
| 35 | + if (this.preserveStartSize + this.preserveEndSize > maxSize) { |
| 36 | + throw new Error("Invalid configuration: preserve sizes exceed maxSize") |
| 37 | + } |
| 38 | + |
| 39 | + this.truncationMessage = truncationMessage |
| 40 | + } |
| 41 | + |
| 42 | + append(content: string): this { |
| 43 | + if (content.length === 0) { |
| 44 | + return this |
| 45 | + } |
| 46 | + |
| 47 | + console.log(`[OutputBuilder#append] ${content.length} bytes`) |
| 48 | + |
| 49 | + this._bytesProcessed += content.length |
| 50 | + |
| 51 | + if (!this.isTruncated) { |
| 52 | + this.startBuffer += content |
| 53 | + |
| 54 | + const excessBytes = this.startBuffer.length - (this.preserveStartSize + this.preserveEndSize) |
| 55 | + |
| 56 | + if (excessBytes <= 0) { |
| 57 | + return this |
| 58 | + } |
| 59 | + |
| 60 | + this.endBuffer = this.startBuffer.slice(-this.preserveEndSize) |
| 61 | + this.startBuffer = this.startBuffer.slice(0, this.preserveStartSize) |
| 62 | + this._bytesRemoved += excessBytes |
| 63 | + } else { |
| 64 | + // Already in truncation mode; append to `endBuffer`. |
| 65 | + this.endBuffer += content |
| 66 | + |
| 67 | + // If `endBuffer` gets too large, trim it. |
| 68 | + if (this.endBuffer.length > this.preserveEndSize) { |
| 69 | + const excessBytes = this.endBuffer.length - this.preserveEndSize |
| 70 | + this.endBuffer = this.endBuffer.slice(excessBytes) |
| 71 | + this._bytesRemoved += excessBytes |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return this |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Reads unprocessed content from the current cursor position, handling both |
| 80 | + * truncated and non-truncated states. |
| 81 | + * |
| 82 | + * The algorithm handles three cases: |
| 83 | + * 1. Non-truncated buffer: |
| 84 | + * - Simply returns remaining content from cursor position. |
| 85 | + * |
| 86 | + * 2. Truncated buffer, cursor in start portion: |
| 87 | + * - Returns remaining start content plus all end content. |
| 88 | + * - This ensures we don't miss the transition between buffers. |
| 89 | + * |
| 90 | + * 3. Truncated buffer, cursor in end portion: |
| 91 | + * - Adjusts cursor position by subtracting removed bytes and start buffer length. |
| 92 | + * - Uses Math.max to prevent negative indices if cursor adjustment overshoots. |
| 93 | + * - Returns remaining content from adjusted position in end buffer. |
| 94 | + * |
| 95 | + * This approach ensures continuous reading even across truncation |
| 96 | + * boundaries, while properly tracking position in both start and end |
| 97 | + * portions of truncated content. |
| 98 | + */ |
| 99 | + read() { |
| 100 | + let output |
| 101 | + |
| 102 | + if (!this.isTruncated) { |
| 103 | + output = this.startBuffer.slice(this.cursor) |
| 104 | + } else if (this.cursor < this.startBuffer.length) { |
| 105 | + output = this.startBuffer.slice(this.cursor) + this.endBuffer |
| 106 | + } else { |
| 107 | + output = this.endBuffer.slice(Math.max(this.cursor - this.bytesRemoved - this.startBuffer.length, 0)) |
| 108 | + } |
| 109 | + |
| 110 | + this._cursor = this.bytesProcessed |
| 111 | + |
| 112 | + return output |
| 113 | + } |
| 114 | + |
| 115 | + public reset(content?: string) { |
| 116 | + console.log(`[OutputBuilder#reset] ${this.bytesProcessed} bytes processed, ${this.bytesRemoved} bytes removed`) |
| 117 | + |
| 118 | + this.startBuffer = "" |
| 119 | + this.endBuffer = "" |
| 120 | + this._bytesProcessed = 0 |
| 121 | + this._bytesRemoved = 0 |
| 122 | + this._cursor = 0 |
| 123 | + |
| 124 | + if (content) { |
| 125 | + this.append(content) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public get content() { |
| 130 | + return this.isTruncated ? this.startBuffer + this.truncationMessage + this.endBuffer : this.startBuffer |
| 131 | + } |
| 132 | + |
| 133 | + public get size() { |
| 134 | + return this.isTruncated |
| 135 | + ? this.startBuffer.length + this.truncationMessage.length + this.endBuffer.length |
| 136 | + : this.startBuffer.length |
| 137 | + } |
| 138 | + |
| 139 | + public get isTruncated() { |
| 140 | + return this._bytesRemoved > 0 |
| 141 | + } |
| 142 | + |
| 143 | + public get bytesProcessed() { |
| 144 | + return this._bytesProcessed |
| 145 | + } |
| 146 | + |
| 147 | + public get bytesRemoved() { |
| 148 | + return this._bytesRemoved |
| 149 | + } |
| 150 | + |
| 151 | + public get cursor() { |
| 152 | + return this._cursor |
| 153 | + } |
| 154 | +} |
0 commit comments