()
| 96 | }) |
| 97 | |
| 98 | const processBuffer = async () => { |
| 99 | if (lineBuffer.length === 0) return |
| 100 | |
| 101 | const bufferText = lineBuffer.join("\n") |
| 102 | lineBuffer = [] // Clear buffer before processing |
| 103 | |
| 104 | try { |
| 105 | const contentBlocks: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: bufferText }] |
| 106 | const chunkTokens = await countTokens(contentBlocks) |
| 107 | tokenEstimate += chunkTokens |
| 108 | } catch (error) { |
| 109 | // On tokenizer error, use conservative estimate: 2 char ≈ 1 token |
| 110 | tokenEstimate += Math.ceil(bufferText.length / 2) |
| 111 | } |
| 112 | |
| 113 | // Check if we've exceeded budget |
| 114 | if (budgetTokens !== undefined && tokenEstimate > budgetTokens) { |
| 115 | complete = false |
| 116 | shouldClose = true |
| 117 | rl.close() |
| 118 | readStream.destroy() |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | rl.on("line", (line) => { |
| 123 | lineCount++ |
no test coverage detected