|
1 | | -import { chunk } from './chunk' |
| 1 | +import { chunk as chunkFn, chunkStream as streamFn } from './chunk' |
| 2 | +import { DEFAULT_CHUNK_OPTIONS } from './chunking' |
2 | 3 | import type { Chunk, Chunker, ChunkOptions } from './types' |
3 | 4 |
|
4 | | -/** |
5 | | - * Default options for the chunker |
6 | | - */ |
7 | | -const DEFAULT_OPTIONS: ChunkOptions = { |
8 | | - maxChunkSize: 4096, |
9 | | - contextMode: 'full', |
10 | | - siblingDetail: 'signatures', |
11 | | - filterImports: false, |
12 | | -} |
13 | | - |
14 | 5 | /** |
15 | 6 | * Implementation of the Chunker interface |
16 | 7 | * |
17 | | - * Provides a stateful wrapper around the chunk function that: |
18 | | - * - Stores default options |
19 | | - * - Tracks the filepath for language detection |
| 8 | + * Provides a stateful wrapper around the chunk and stream functions that |
| 9 | + * stores default options and allows per-call overrides. |
20 | 10 | */ |
21 | 11 | class ChunkerImpl implements Chunker { |
22 | | - private readonly filepath: string |
23 | 12 | private readonly defaultOptions: ChunkOptions |
24 | 13 |
|
25 | | - constructor(filepath: string, options: ChunkOptions = {}) { |
26 | | - this.filepath = filepath |
27 | | - this.defaultOptions = { ...DEFAULT_OPTIONS, ...options } |
| 14 | + constructor(options: ChunkOptions = {}) { |
| 15 | + this.defaultOptions = { ...DEFAULT_CHUNK_OPTIONS, ...options } |
28 | 16 | } |
29 | 17 |
|
30 | 18 | /** |
31 | 19 | * Chunk source code into pieces with context |
32 | 20 | * |
33 | | - * @param source - The source code to chunk |
| 21 | + * @param filepath - The file path (used for language detection) |
| 22 | + * @param code - The source code to chunk |
34 | 23 | * @param options - Optional overrides for chunking options |
35 | 24 | * @returns Promise resolving to array of chunks |
36 | 25 | */ |
37 | | - async chunk(source: string, options?: ChunkOptions): Promise<Chunk[]> { |
| 26 | + async chunk( |
| 27 | + filepath: string, |
| 28 | + code: string, |
| 29 | + options?: ChunkOptions, |
| 30 | + ): Promise<Chunk[]> { |
38 | 31 | const mergedOptions = { ...this.defaultOptions, ...options } |
39 | | - return chunk(this.filepath, source, mergedOptions) |
| 32 | + return chunkFn(filepath, code, mergedOptions) |
40 | 33 | } |
41 | 34 |
|
42 | 35 | /** |
43 | 36 | * Stream chunks as they are generated |
44 | 37 | * |
45 | | - * @param source - The source code to chunk |
| 38 | + * @param filepath - The file path (used for language detection) |
| 39 | + * @param code - The source code to chunk |
46 | 40 | * @param options - Optional overrides for chunking options |
47 | 41 | * @returns Async iterable of chunks |
48 | | - * |
49 | | - * TODO: Implement true streaming - for now, this just iterates the array |
50 | 42 | */ |
51 | | - async *stream(source: string, options?: ChunkOptions): AsyncIterable<Chunk> { |
| 43 | + async *stream( |
| 44 | + filepath: string, |
| 45 | + code: string, |
| 46 | + options?: ChunkOptions, |
| 47 | + ): AsyncIterable<Chunk> { |
52 | 48 | const mergedOptions = { ...this.defaultOptions, ...options } |
53 | | - const chunks = await chunk(this.filepath, source, mergedOptions) |
54 | | - |
55 | | - for (const c of chunks) { |
56 | | - yield c |
57 | | - } |
| 49 | + yield* streamFn(filepath, code, mergedOptions) |
58 | 50 | } |
59 | 51 | } |
60 | 52 |
|
61 | 53 | /** |
62 | | - * Create a new Chunker instance for a specific file |
| 54 | + * Create a new Chunker instance with default options |
63 | 55 | * |
64 | 56 | * The Chunker provides a convenient interface for chunking source code |
65 | 57 | * with pre-configured options. It's particularly useful when you need to |
66 | | - * chunk multiple versions of the same file or want to stream chunks. |
| 58 | + * chunk multiple files with the same configuration. |
67 | 59 | * |
68 | | - * @param filepath - The file path (used for language detection) |
69 | 60 | * @param options - Default options for all chunking operations |
70 | 61 | * @returns A Chunker instance |
71 | 62 | * |
72 | 63 | * @example |
73 | 64 | * ```ts |
74 | 65 | * import { createChunker } from 'astchunk' |
75 | 66 | * |
76 | | - * const chunker = createChunker('src/utils.ts', { maxChunkSize: 2048 }) |
| 67 | + * const chunker = createChunker({ maxChunkSize: 2048 }) |
77 | 68 | * |
78 | 69 | * // Chunk synchronously |
79 | | - * const chunks = await chunker.chunk(sourceCode) |
| 70 | + * const chunks = await chunker.chunk('src/utils.ts', sourceCode) |
80 | 71 | * |
81 | 72 | * // Or stream chunks |
82 | | - * for await (const chunk of chunker.stream(sourceCode)) { |
| 73 | + * for await (const chunk of chunker.stream('src/utils.ts', sourceCode)) { |
83 | 74 | * process.stdout.write(chunk.text) |
84 | 75 | * } |
85 | 76 | * ``` |
86 | 77 | */ |
87 | | -export function createChunker( |
88 | | - filepath: string, |
89 | | - options?: ChunkOptions, |
90 | | -): Chunker { |
91 | | - return new ChunkerImpl(filepath, options) |
| 78 | +export function createChunker(options?: ChunkOptions): Chunker { |
| 79 | + return new ChunkerImpl(options) |
92 | 80 | } |
93 | | - |
94 | | -/** |
95 | | - * Re-export the Chunker type for convenience |
96 | | - */ |
97 | | -export type { Chunker } from './types' |
0 commit comments