( strategy: ChunkingStrategy, content: string, chunkSize: number, chunkOverlap: number, minCharactersPerChunk: number, strategyOptions?: StrategyOptions )
| 119 | } |
| 120 | |
| 121 | async function applyStrategy( |
| 122 | strategy: ChunkingStrategy, |
| 123 | content: string, |
| 124 | chunkSize: number, |
| 125 | chunkOverlap: number, |
| 126 | minCharactersPerChunk: number, |
| 127 | strategyOptions?: StrategyOptions |
| 128 | ): Promise<Chunk[]> { |
| 129 | const baseOptions = { chunkSize, chunkOverlap, minCharactersPerChunk } |
| 130 | |
| 131 | switch (strategy) { |
| 132 | case 'token': { |
| 133 | const chunker = new TokenChunker(baseOptions) |
| 134 | return chunker.chunk(content) |
| 135 | } |
| 136 | case 'sentence': { |
| 137 | const chunker = new SentenceChunker(baseOptions) |
| 138 | return chunker.chunk(content) |
| 139 | } |
| 140 | case 'recursive': { |
| 141 | const chunker = new RecursiveChunker({ |
| 142 | ...baseOptions, |
| 143 | separators: strategyOptions?.separators, |
| 144 | recipe: strategyOptions?.recipe, |
| 145 | }) |
| 146 | return chunker.chunk(content) |
| 147 | } |
| 148 | case 'regex': { |
| 149 | if (!strategyOptions?.pattern) { |
| 150 | logger.warn( |
| 151 | 'Regex strategy requested but no pattern provided, falling back to text chunker' |
| 152 | ) |
| 153 | const chunker = new TextChunker(baseOptions) |
| 154 | return chunker.chunk(content) |
| 155 | } |
| 156 | const chunker = new RegexChunker({ |
| 157 | ...baseOptions, |
| 158 | pattern: strategyOptions.pattern, |
| 159 | strictBoundaries: strategyOptions.strictBoundaries, |
| 160 | }) |
| 161 | return chunker.chunk(content) |
| 162 | } |
| 163 | default: { |
| 164 | const chunker = new TextChunker(baseOptions) |
| 165 | return chunker.chunk(content) |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | export async function processDocument( |
| 171 | fileUrl: string, |
no test coverage detected