Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/sim/lib/chunkers/token-chunker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,18 @@ describe('TokenChunker', () => {
expect(combined).toContain(word)
}
})

it.concurrent('keeps all text when a chunk ends at a space before the overlap', async () => {
const chunker = new TokenChunker({
chunkSize: 20,
chunkOverlap: 5,
minCharactersPerChunk: 1,
})
const unbroken = Array.from({ length: 200 }, (_, i) => String.fromCodePoint(0x4e00 + i))
const chunks = await chunker.chunk(`API ${unbroken.join('')}`)

const combined = chunks.map((c) => c.text).join('')
expect(unbroken.filter((character) => !combined.includes(character))).toEqual([])
})
})
})
4 changes: 3 additions & 1 deletion apps/sim/lib/chunkers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ export function* iterateWordBoundaryChunkSpans(
}

if (stepChars !== undefined) {
const nextPos = pos + Math.max(1, stepChars)
// A window that snapped back to a space ends before `pos + stepChars`; stepping
// past its end would skip the text between them, so never start beyond it.
const nextPos = Math.min(pos + Math.max(1, stepChars), end)
if (nextPos >= text.length) break
pos = nextPos
} else {
Expand Down