| 300 | } |
| 301 | |
| 302 | private mergeTableChunks( |
| 303 | chunks: string[], |
| 304 | tableBoundaries: { start: number; end: number }[], |
| 305 | originalContent: string |
| 306 | ): string[] { |
| 307 | if (tableBoundaries.length === 0) { |
| 308 | return chunks |
| 309 | } |
| 310 | |
| 311 | const mergedChunks: string[] = [] |
| 312 | let currentPosition = 0 |
| 313 | |
| 314 | for (const chunk of chunks) { |
| 315 | const chunkStart = originalContent.indexOf(chunk, currentPosition) |
| 316 | if (chunkStart === -1) { |
| 317 | mergedChunks.push(chunk) |
| 318 | continue |
| 319 | } |
| 320 | const chunkEnd = chunkStart + chunk.length |
| 321 | |
| 322 | const intersectsTable = tableBoundaries.some( |
| 323 | (table) => |
| 324 | (chunkStart >= table.start && chunkStart <= table.end) || |
| 325 | (chunkEnd >= table.start && chunkEnd <= table.end) || |
| 326 | (chunkStart <= table.start && chunkEnd >= table.end) |
| 327 | ) |
| 328 | |
| 329 | if (intersectsTable) { |
| 330 | const affectedTables = tableBoundaries.filter( |
| 331 | (table) => |
| 332 | (chunkStart >= table.start && chunkStart <= table.end) || |
| 333 | (chunkEnd >= table.start && chunkEnd <= table.end) || |
| 334 | (chunkStart <= table.start && chunkEnd >= table.end) |
| 335 | ) |
| 336 | |
| 337 | const minStart = Math.min(chunkStart, ...affectedTables.map((t) => t.start)) |
| 338 | const maxEnd = Math.max(chunkEnd, ...affectedTables.map((t) => t.end)) |
| 339 | const completeChunk = originalContent.slice(minStart, maxEnd).trim() |
| 340 | |
| 341 | if (completeChunk && !mergedChunks.some((existing) => existing === completeChunk)) { |
| 342 | mergedChunks.push(completeChunk) |
| 343 | } |
| 344 | } else { |
| 345 | mergedChunks.push(chunk) |
| 346 | } |
| 347 | |
| 348 | currentPosition = chunkEnd |
| 349 | } |
| 350 | |
| 351 | return mergedChunks.filter((chunk) => chunk.length > 50) |
| 352 | } |
| 353 | |
| 354 | private enforceSizeLimit(chunks: string[]): string[] { |
| 355 | const finalChunks: string[] = [] |