|
1 | | -import { Effect } from 'effect' |
2 | | -import { chunk as chunkInternal } from './chunking' |
| 1 | +import { Effect, Stream } from 'effect' |
| 2 | +import { |
| 3 | + chunk as chunkInternal, |
| 4 | + streamChunks as streamChunksInternal, |
| 5 | +} from './chunking' |
3 | 6 | import { extractEntities } from './extract' |
4 | 7 | import { parseCode } from './parser' |
5 | 8 | import { detectLanguage } from './parser/languages' |
6 | 9 | import { buildScopeTree } from './scope' |
7 | | -import type { Chunk, ChunkOptions, Language } from './types' |
| 10 | +import type { |
| 11 | + Chunk, |
| 12 | + ChunkOptions, |
| 13 | + Language, |
| 14 | + ParseResult, |
| 15 | + ScopeTree, |
| 16 | +} from './types' |
8 | 17 |
|
9 | 18 | /** |
10 | 19 | * Error thrown when chunking fails |
@@ -74,14 +83,15 @@ const chunkEffect = ( |
74 | 83 | new ChunkingError('Failed to build scope tree', error), |
75 | 84 | ) |
76 | 85 |
|
77 | | - // Step 5: Chunk the code |
| 86 | + // Step 5: Chunk the code (passing filepath for context) |
78 | 87 | const chunks = yield* Effect.mapError( |
79 | 88 | chunkInternal( |
80 | 89 | parseResult.tree.rootNode, |
81 | 90 | code, |
82 | 91 | scopeTree, |
83 | 92 | language, |
84 | 93 | options, |
| 94 | + filepath, |
85 | 95 | ), |
86 | 96 | (error: unknown) => new ChunkingError('Failed to chunk code', error), |
87 | 97 | ) |
@@ -133,3 +143,172 @@ export async function chunk( |
133 | 143 | ): Promise<Chunk[]> { |
134 | 144 | return Effect.runPromise(chunkEffect(filepath, code, options)) |
135 | 145 | } |
| 146 | + |
| 147 | +/** |
| 148 | + * Prepare the chunking pipeline (parse, extract, build scope tree) |
| 149 | + * Returns the parsed result and scope tree needed for chunking |
| 150 | + */ |
| 151 | +const prepareChunking = ( |
| 152 | + filepath: string, |
| 153 | + code: string, |
| 154 | + options?: ChunkOptions, |
| 155 | +): Effect.Effect< |
| 156 | + { parseResult: ParseResult; scopeTree: ScopeTree; language: Language }, |
| 157 | + ChunkingError | UnsupportedLanguageError |
| 158 | +> => { |
| 159 | + return Effect.gen(function* () { |
| 160 | + // Step 1: Detect language (or use override) |
| 161 | + const language: Language | null = |
| 162 | + options?.language ?? detectLanguage(filepath) |
| 163 | + |
| 164 | + if (!language) { |
| 165 | + return yield* Effect.fail(new UnsupportedLanguageError(filepath)) |
| 166 | + } |
| 167 | + |
| 168 | + // Step 2: Parse the code |
| 169 | + const parseResult = yield* Effect.tryPromise({ |
| 170 | + try: () => parseCode(code, language), |
| 171 | + catch: (error: unknown) => |
| 172 | + new ChunkingError('Failed to parse code', error), |
| 173 | + }) |
| 174 | + |
| 175 | + // Step 3: Extract entities from AST |
| 176 | + const entities = yield* Effect.mapError( |
| 177 | + extractEntities(parseResult.tree.rootNode, language, code), |
| 178 | + (error: unknown) => |
| 179 | + new ChunkingError('Failed to extract entities', error), |
| 180 | + ) |
| 181 | + |
| 182 | + // Step 4: Build scope tree |
| 183 | + const scopeTree = yield* Effect.mapError( |
| 184 | + buildScopeTree(entities), |
| 185 | + (error: unknown) => |
| 186 | + new ChunkingError('Failed to build scope tree', error), |
| 187 | + ) |
| 188 | + |
| 189 | + return { parseResult, scopeTree, language } |
| 190 | + }) |
| 191 | +} |
| 192 | + |
| 193 | +/** |
| 194 | + * Create an Effect Stream that yields chunks |
| 195 | + * |
| 196 | + * This is the Effect-native streaming API. Use this if you're working |
| 197 | + * within the Effect ecosystem and want full composability. |
| 198 | + * |
| 199 | + * @param filepath - The file path (used for language detection) |
| 200 | + * @param code - The source code to chunk |
| 201 | + * @param options - Optional chunking configuration |
| 202 | + * @returns Effect Stream of chunks with context |
| 203 | + * |
| 204 | + * @example |
| 205 | + * ```ts |
| 206 | + * import { chunkStreamEffect } from 'astchunk' |
| 207 | + * import { Effect, Stream } from 'effect' |
| 208 | + * |
| 209 | + * const program = Stream.runForEach( |
| 210 | + * chunkStreamEffect('src/utils.ts', sourceCode), |
| 211 | + * (chunk) => Effect.log(chunk.text) |
| 212 | + * ) |
| 213 | + * |
| 214 | + * Effect.runPromise(program) |
| 215 | + * ``` |
| 216 | + */ |
| 217 | +export const chunkStreamEffect = ( |
| 218 | + filepath: string, |
| 219 | + code: string, |
| 220 | + options?: ChunkOptions, |
| 221 | +): Stream.Stream<Chunk, ChunkingError | UnsupportedLanguageError> => { |
| 222 | + return Stream.unwrap( |
| 223 | + Effect.map(prepareChunking(filepath, code, options), (prepared) => { |
| 224 | + const { parseResult, scopeTree, language } = prepared |
| 225 | + |
| 226 | + // Create stream from the internal generator |
| 227 | + return Stream.fromAsyncIterable( |
| 228 | + streamChunksInternal( |
| 229 | + parseResult.tree.rootNode, |
| 230 | + code, |
| 231 | + scopeTree, |
| 232 | + language, |
| 233 | + options, |
| 234 | + filepath, |
| 235 | + ), |
| 236 | + (error) => new ChunkingError('Stream iteration failed', error), |
| 237 | + ).pipe( |
| 238 | + // Attach parse error to chunks if present |
| 239 | + Stream.map((chunk) => |
| 240 | + parseResult.error |
| 241 | + ? { |
| 242 | + ...chunk, |
| 243 | + context: { |
| 244 | + ...chunk.context, |
| 245 | + parseError: parseResult.error, |
| 246 | + }, |
| 247 | + } |
| 248 | + : chunk, |
| 249 | + ), |
| 250 | + ) |
| 251 | + }), |
| 252 | + ) |
| 253 | +} |
| 254 | + |
| 255 | +/** |
| 256 | + * Stream source code chunks as they are generated |
| 257 | + * |
| 258 | + * This function returns an async generator that yields chunks one at a time, |
| 259 | + * which is useful for processing large files without waiting for all chunks |
| 260 | + * to be generated. |
| 261 | + * |
| 262 | + * @param filepath - The file path (used for language detection) |
| 263 | + * @param code - The source code to chunk |
| 264 | + * @param options - Optional chunking configuration |
| 265 | + * @returns Async generator of chunks with context |
| 266 | + * @throws ChunkingError if chunking fails |
| 267 | + * @throws UnsupportedLanguageError if the file type is not supported |
| 268 | + * |
| 269 | + * @example |
| 270 | + * ```ts |
| 271 | + * import { chunkStream } from 'astchunk' |
| 272 | + * |
| 273 | + * for await (const chunk of chunkStream('src/utils.ts', sourceCode)) { |
| 274 | + * console.log(chunk.text, chunk.context) |
| 275 | + * } |
| 276 | + * ``` |
| 277 | + */ |
| 278 | +export async function* chunkStream( |
| 279 | + filepath: string, |
| 280 | + code: string, |
| 281 | + options?: ChunkOptions, |
| 282 | +): AsyncGenerator<Chunk> { |
| 283 | + // Prepare the chunking pipeline |
| 284 | + const prepared = await Effect.runPromise( |
| 285 | + prepareChunking(filepath, code, options), |
| 286 | + ) |
| 287 | + |
| 288 | + const { parseResult, scopeTree, language } = prepared |
| 289 | + |
| 290 | + // Stream chunks from the internal generator |
| 291 | + const chunkGenerator = streamChunksInternal( |
| 292 | + parseResult.tree.rootNode, |
| 293 | + code, |
| 294 | + scopeTree, |
| 295 | + language, |
| 296 | + options, |
| 297 | + filepath, |
| 298 | + ) |
| 299 | + |
| 300 | + // Yield chunks, optionally attaching parse error if present |
| 301 | + for await (const chunk of chunkGenerator) { |
| 302 | + if (parseResult.error) { |
| 303 | + yield { |
| 304 | + ...chunk, |
| 305 | + context: { |
| 306 | + ...chunk.context, |
| 307 | + parseError: parseResult.error, |
| 308 | + }, |
| 309 | + } |
| 310 | + } else { |
| 311 | + yield chunk |
| 312 | + } |
| 313 | + } |
| 314 | +} |
0 commit comments