( items: T[], concurrency: number, processor: (item: T, index: number) => Promise<R> )
| 320 | } |
| 321 | |
| 322 | async function processWithConcurrency<T, R>( |
| 323 | items: T[], |
| 324 | concurrency: number, |
| 325 | processor: (item: T, index: number) => Promise<R> |
| 326 | ): Promise<R[]> { |
| 327 | const results: R[] = new Array(items.length) |
| 328 | let currentIndex = 0 |
| 329 | |
| 330 | const workers = Array.from({ length: Math.min(concurrency, items.length) }, async () => { |
| 331 | while (currentIndex < items.length) { |
| 332 | const index = currentIndex++ |
| 333 | results[index] = await processor(items[index], index) |
| 334 | } |
| 335 | }) |
| 336 | |
| 337 | await Promise.all(workers) |
| 338 | return results |
| 339 | } |
| 340 | |
| 341 | export interface GenerateEmbeddingsResult { |
| 342 | embeddings: number[][] |
no outgoing calls
no test coverage detected