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
Next Next commit
feat(knowledge): add token, sentence, recursive, and regex chunkers
  • Loading branch information
waleedlatif1 committed Apr 11, 2026
commit 9f83f8738f302afb3dddc5062cf769dc73f81366
28 changes: 27 additions & 1 deletion apps/sim/app/api/knowledge/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ const CreateKnowledgeBaseSchema = z.object({
minSize: z.number().min(1).max(2000).default(100),
/** Overlap between chunks in tokens (1 token ≈ 4 characters) */
overlap: z.number().min(0).max(500).default(200),
/** Chunking strategy */
strategy: z
.enum(['auto', 'text', 'regex', 'recursive', 'sentence', 'token'])
.default('auto')
.optional(),
/** Strategy-specific options */
strategyOptions: z
.object({
/** Regex pattern for 'regex' strategy */
pattern: z.string().optional(),
/** Custom separator hierarchy for 'recursive' strategy */
separators: z.array(z.string()).optional(),
/** Pre-built separator recipe for 'recursive' strategy */
recipe: z.enum(['plain', 'markdown', 'code']).optional(),
})
.optional(),
})
.default({
maxSize: 1024,
Expand All @@ -45,13 +61,23 @@ const CreateKnowledgeBaseSchema = z.object({
})
.refine(
(data) => {
// Convert maxSize from tokens to characters for comparison (1 token ≈ 4 chars)
const maxSizeInChars = data.maxSize * 4
return data.minSize < maxSizeInChars
},
{
message: 'Min chunk size (characters) must be less than max chunk size (tokens × 4)',
}
)
.refine(
(data) => {
if (data.strategy === 'regex' && !data.strategyOptions?.pattern) {
return false
}
return true
},
{
message: 'Regex pattern is required when using the regex chunking strategy',
}
),
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ import {
ModalHeader,
Textarea,
} from '@/components/emcn'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import type { StrategyOptions } from '@/lib/chunkers/types'
import { cn } from '@/lib/core/utils/cn'
import { formatFileSize, validateKnowledgeBaseFile } from '@/lib/uploads/utils/file-utils'
import { ACCEPT_ATTRIBUTE } from '@/lib/uploads/utils/validation'
Expand All @@ -35,6 +43,15 @@ interface CreateBaseModalProps {
onOpenChange: (open: boolean) => void
}

const STRATEGY_OPTIONS = [
{ value: 'auto', label: 'Auto (detect from content)' },
{ value: 'text', label: 'Text (hierarchical splitting)' },
{ value: 'recursive', label: 'Recursive (configurable separators)' },
{ value: 'sentence', label: 'Sentence' },
{ value: 'token', label: 'Token (fixed-size)' },
{ value: 'regex', label: 'Regex (custom pattern)' },
] as const

const FormSchema = z
.object({
name: z
Expand All @@ -58,10 +75,17 @@ const FormSchema = z
.number()
.min(0, 'Overlap must be non-negative')
.max(500, 'Overlap must be less than 500 tokens'),
/** Chunking strategy */
strategy: z
.enum(['auto', 'text', 'regex', 'recursive', 'sentence', 'token'])
.default('auto'),
/** Regex pattern (required when strategy is 'regex') */
Comment thread
waleedlatif1 marked this conversation as resolved.
Outdated
regexPattern: z.string().optional(),
/** Custom separators for recursive strategy (comma-separated) */
customSeparators: z.string().optional(),
})
.refine(
(data) => {
// Convert maxChunkSize from tokens to characters for comparison (1 token ≈ 4 chars)
const maxChunkSizeInChars = data.maxChunkSize * 4
return data.minChunkSize < maxChunkSizeInChars
},
Expand All @@ -70,6 +94,18 @@ const FormSchema = z
path: ['minChunkSize'],
}
)
.refine(
(data) => {
if (data.strategy === 'regex' && !data.regexPattern?.trim()) {
return false
}
return true
},
{
message: 'Regex pattern is required when using the regex strategy',
path: ['regexPattern'],
}
)

type FormValues = z.infer<typeof FormSchema>

Expand Down Expand Up @@ -124,6 +160,7 @@ export const CreateBaseModal = memo(function CreateBaseModal({
handleSubmit,
reset,
watch,
setValue,
formState: { errors },
} = useForm<FormValues>({
resolver: zodResolver(FormSchema),
Expand All @@ -133,11 +170,15 @@ export const CreateBaseModal = memo(function CreateBaseModal({
minChunkSize: 100,
maxChunkSize: 1024,
overlapSize: 200,
strategy: 'auto',
regexPattern: '',
customSeparators: '',
},
mode: 'onSubmit',
})

const nameValue = watch('name')
const strategyValue = watch('strategy')

useEffect(() => {
if (open) {
Expand All @@ -153,6 +194,9 @@ export const CreateBaseModal = memo(function CreateBaseModal({
minChunkSize: 100,
maxChunkSize: 1024,
overlapSize: 200,
strategy: 'auto',
regexPattern: '',
customSeparators: '',
})
}
}, [open, reset])
Expand Down Expand Up @@ -255,6 +299,17 @@ export const CreateBaseModal = memo(function CreateBaseModal({
setSubmitStatus(null)

try {
const strategyOptions: StrategyOptions | undefined =
data.strategy === 'regex' && data.regexPattern
? { pattern: data.regexPattern }
: data.strategy === 'recursive' && data.customSeparators?.trim()
? {
separators: data.customSeparators
.split(',')
.map((s) => s.trim().replace(/\\n/g, '\n').replace(/\\t/g, '\t')),
}
: undefined

const newKnowledgeBase = await createKnowledgeBaseMutation.mutateAsync({
name: data.name,
description: data.description || undefined,
Expand All @@ -263,6 +318,8 @@ export const CreateBaseModal = memo(function CreateBaseModal({
maxSize: data.maxChunkSize,
minSize: data.minChunkSize,
overlap: data.overlapSize,
...(data.strategy !== 'auto' && { strategy: data.strategy }),
...(strategyOptions && { strategyOptions }),
},
})

Expand Down Expand Up @@ -403,6 +460,69 @@ export const CreateBaseModal = memo(function CreateBaseModal({
</p>
</div>

<div className='flex flex-col gap-2'>
<Label htmlFor='strategy'>Chunking Strategy</Label>
<Select
value={strategyValue}
onValueChange={(value) =>
setValue('strategy', value as FormValues['strategy'])
}
>
<SelectTrigger id='strategy'>
<SelectValue placeholder='Auto (detect from content)' />
</SelectTrigger>
<SelectContent>
{STRATEGY_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<p className='text-[var(--text-muted)] text-xs'>
Auto detects the best strategy based on file content type.
</p>
</div>

{strategyValue === 'regex' && (
<div className='flex flex-col gap-2'>
<Label htmlFor='regexPattern'>Regex Pattern</Label>
<Input
id='regexPattern'
placeholder='e.g. \\n\\n or (?<=\\})\\s*(?=\\{)'
{...register('regexPattern')}
className={cn(errors.regexPattern && 'border-[var(--text-error)]')}
autoComplete='off'
data-form-type='other'
/>
{errors.regexPattern && (
<p className='text-[var(--text-error)] text-xs'>
{errors.regexPattern.message}
</p>
)}
<p className='text-[var(--text-muted)] text-xs'>
Text will be split at each match of this regex pattern.
</p>
</div>
)}

{strategyValue === 'recursive' && (
<div className='flex flex-col gap-2'>
<Label htmlFor='customSeparators'>Custom Separators (optional)</Label>
<Input
id='customSeparators'
placeholder='e.g. \n\n, \n, . , '
{...register('customSeparators')}
autoComplete='off'
data-form-type='other'
/>
<p className='text-[var(--text-muted)] text-xs'>
Comma-separated list of delimiters in priority order. Leave empty for default
separators.
</p>
</div>
)}

<div className='flex flex-col gap-2'>
<Label>Upload Documents</Label>
<Button
Expand Down
3 changes: 3 additions & 0 deletions apps/sim/hooks/queries/kb/knowledge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createLogger } from '@sim/logger'
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { toast } from '@/components/emcn'
import type { ChunkingStrategy, StrategyOptions } from '@/lib/chunkers/types'
import type {
ChunkData,
ChunksPagination,
Expand Down Expand Up @@ -707,6 +708,8 @@ export interface CreateKnowledgeBaseParams {
maxSize: number
minSize: number
overlap: number
strategy?: ChunkingStrategy
strategyOptions?: StrategyOptions
}
}

Expand Down
4 changes: 4 additions & 0 deletions apps/sim/lib/chunkers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export { DocsChunker } from './docs-chunker'
export { JsonYamlChunker } from './json-yaml-chunker'
export { RecursiveChunker } from './recursive-chunker'
export { RegexChunker } from './regex-chunker'
export { SentenceChunker } from './sentence-chunker'
export { StructuredDataChunker } from './structured-data-chunker'
export { TextChunker } from './text-chunker'
export { TokenChunker } from './token-chunker'
export * from './types'
Loading
Loading