(filePath: string)
| 229 | * is ~2M tokens, which is well under 100 MB of JSONL. |
| 230 | */ |
| 231 | export async function readJSONLFile<T>(filePath: string): Promise<T[]> { |
| 232 | const { size } = await stat(filePath) |
| 233 | if (size <= MAX_JSONL_READ_BYTES) { |
| 234 | return parseJSONL<T>(await readFile(filePath)) |
| 235 | } |
| 236 | await using fd = await open(filePath, 'r') |
| 237 | const buf = Buffer.allocUnsafe(MAX_JSONL_READ_BYTES) |
| 238 | let totalRead = 0 |
| 239 | const fileOffset = size - MAX_JSONL_READ_BYTES |
| 240 | while (totalRead < MAX_JSONL_READ_BYTES) { |
| 241 | const { bytesRead } = await fd.read( |
| 242 | buf, |
| 243 | totalRead, |
| 244 | MAX_JSONL_READ_BYTES - totalRead, |
| 245 | fileOffset + totalRead, |
| 246 | ) |
| 247 | if (bytesRead === 0) break |
| 248 | totalRead += bytesRead |
| 249 | } |
| 250 | // Skip the first partial line |
| 251 | const newlineIndex = buf.indexOf(0x0a) |
| 252 | if (newlineIndex !== -1 && newlineIndex < totalRead - 1) { |
| 253 | return parseJSONL<T>(buf.subarray(newlineIndex + 1, totalRead)) |
| 254 | } |
| 255 | return parseJSONL<T>(buf.subarray(0, totalRead)) |
| 256 | } |
| 257 | |
| 258 | export function addItemToJSONCArray(content: string, newItem: unknown): string { |
| 259 | try { |
nothing calls this directly
no test coverage detected