Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9f83f87
feat(knowledge): add token, sentence, recursive, and regex chunkers
waleedlatif1 Apr 11, 2026
59f86e9
fix(chunkers): standardize token estimation and use emcn dropdown
waleedlatif1 Apr 11, 2026
25abb8a
fix(chunkers): address research audit findings
waleedlatif1 Apr 11, 2026
211fe90
fix(chunkers): fix remaining audit issues across all chunkers
waleedlatif1 Apr 11, 2026
4872e75
chore(chunkers): lint formatting
waleedlatif1 Apr 11, 2026
fc006ee
updated styling
waleedlatif1 Apr 11, 2026
c5b9b2f
fix(chunkers): audit fixes and comprehensive tests
waleedlatif1 Apr 11, 2026
cb814ff
chore(chunkers): remove unnecessary comments and dead code
waleedlatif1 Apr 11, 2026
899fc68
fix(chunkers): address PR review comments
waleedlatif1 Apr 11, 2026
4c3508b
fix(chunkers): use consistent overlap pattern in regex fallback
waleedlatif1 Apr 11, 2026
3a26dad
fix(chunkers): prevent content loss in word boundary splitting
waleedlatif1 Apr 11, 2026
5e8b051
fix(chunkers): restore structured data token ratio and overlap joiner
waleedlatif1 Apr 11, 2026
a53f760
lint
waleedlatif1 Apr 11, 2026
ec6fa58
fix(chunkers): fall back to character-level overlap in sentence chunker
waleedlatif1 Apr 11, 2026
e391efa
fix(chunkers): fix log message and add missing month abbreviations
waleedlatif1 Apr 11, 2026
f7fe06a
lint
waleedlatif1 Apr 11, 2026
9c624db
fix(chunkers): restore structured data detection threshold to > 2
waleedlatif1 Apr 11, 2026
4fd7685
fix(chunkers): pass chunkOverlap to buildChunks in TokenChunker
waleedlatif1 Apr 11, 2026
97a0bd4
fix(chunkers): restore separator-as-joiner pattern in splitRecursively
waleedlatif1 Apr 11, 2026
2c5a852
feat(knowledge): add JSONL file support for knowledge base uploads
waleedlatif1 Apr 11, 2026
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
Prev Previous commit
Next Next commit
fix(chunkers): restore structured data token ratio and overlap joiner
- Restore /3 token estimation for StructuredDataChunker (structured data
  is denser than prose, ~3 chars/token vs ~4)
- Change addOverlap joiner from \n to space to match original TextChunker
  behavior
  • Loading branch information
waleedlatif1 committed Apr 11, 2026
commit 5e8b0515c3f6fc228356dad4480dce9db71a9214
18 changes: 11 additions & 7 deletions apps/sim/lib/chunkers/structured-data-chunker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createLogger } from '@sim/logger'
import type { Chunk, StructuredDataOptions } from '@/lib/chunkers/types'
import { estimateTokens } from '@/lib/chunkers/utils'
/** Structured data is denser in tokens (~3 chars/token vs ~4 for prose) */
function estimateStructuredTokens(text: string): number {
if (!text?.trim()) return 0
return Math.ceil(text.length / 3)
}

const logger = createLogger('StructuredDataChunker')

Expand Down Expand Up @@ -28,7 +32,7 @@ export class StructuredDataChunker {
const headerLine = options.headers?.join('\t') || lines[0]
const dataStartIndex = options.headers ? 0 : 1

const estimatedTokensPerRow = StructuredDataChunker.estimateTokensPerRow(
const estimatedTokensPerRow = StructuredDataChunker.estimateStructuredTokensPerRow(
lines.slice(dataStartIndex, Math.min(10, lines.length))
)
const optimalRowsPerChunk = StructuredDataChunker.calculateOptimalRowsPerChunk(
Expand All @@ -42,12 +46,12 @@ export class StructuredDataChunker {

let currentChunkRows: string[] = []
let currentTokenEstimate = 0
const headerTokens = estimateTokens(headerLine)
const headerTokens = estimateStructuredTokens(headerLine)
let chunkStartRow = dataStartIndex

for (let i = dataStartIndex; i < lines.length; i++) {
const row = lines[i]
const rowTokens = estimateTokens(row)
const rowTokens = estimateStructuredTokens(row)

const projectedTokens =
currentTokenEstimate +
Expand Down Expand Up @@ -111,18 +115,18 @@ export class StructuredDataChunker {
private static createChunk(content: string, startRow: number, endRow: number): Chunk {
return {
text: content,
tokenCount: estimateTokens(content),
tokenCount: estimateStructuredTokens(content),
metadata: {
startIndex: startRow,
endIndex: endRow,
},
}
}

private static estimateTokensPerRow(sampleRows: string[]): number {
private static estimateStructuredTokensPerRow(sampleRows: string[]): number {
if (sampleRows.length === 0) return 50

const totalTokens = sampleRows.reduce((sum, row) => sum + estimateTokens(row), 0)
const totalTokens = sampleRows.reduce((sum, row) => sum + estimateStructuredTokens(row), 0)
return Math.ceil(totalTokens / sampleRows.length)
}

Expand Down
8 changes: 3 additions & 5 deletions apps/sim/lib/chunkers/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ describe('addOverlap', () => {
expect(result[1].length).toBeGreaterThan('second chunk here'.length)
})

it('joins overlap text with \\n', () => {
it('joins overlap text with space', () => {
const chunks = ['first chunk here', 'second chunk here']
const result = addOverlap(chunks, 10)
expect(result[1]).toContain('\n')
expect(result[1]).toContain('here second')
})

it('snaps overlap to word boundary', () => {
const chunks = ['hello beautiful world', 'next chunk']
const result = addOverlap(chunks, 15)
const overlapPart = result[1].split('\n')[0]
expect(overlapPart).toBe('beautiful world')
expect(result[1]).toBe('beautiful world\nnext chunk')
expect(result[1]).toBe('beautiful world next chunk')
})
})

Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/chunkers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function addOverlap(chunks: string[], overlapChars: number): string[] {
: overlapText

if (cleanOverlap.trim()) {
chunk = `${cleanOverlap.trim()}\n${chunk}`
chunk = `${cleanOverlap.trim()} ${chunk}`
}
}

Expand Down
Loading