* 批量处理文献(分批并发) * @param {Array} references - 文献条目数组(原始文本) * @param {Object} apiConfig - API配置 * @param {string} sourceLang - 源语言 * @param {Function} progressCallback - 进度回调 * @returns {Promise } 处理结果
(references, apiConfig, sourceLang = 'auto', progressCallback = null)
| 293 | * @returns {Promise<Array>} 处理结果 |
| 294 | */ |
| 295 | async function batchProcessReferences(references, apiConfig, sourceLang = 'auto', progressCallback = null) { |
| 296 | if (!references || !Array.isArray(references) || references.length === 0) { |
| 297 | return []; |
| 298 | } |
| 299 | |
| 300 | // 分批 |
| 301 | const batches = []; |
| 302 | for (let i = 0; i < references.length; i += BATCH_SIZE) { |
| 303 | batches.push(references.slice(i, i + BATCH_SIZE)); |
| 304 | } |
| 305 | |
| 306 | console.log(`[ReferenceAIProcessor] Processing ${references.length} references in ${batches.length} batches (${BATCH_SIZE} per batch)`); |
| 307 | |
| 308 | const results = []; |
| 309 | let processedCount = 0; |
| 310 | |
| 311 | // 并发处理所有批次 |
| 312 | const batchPromises = batches.map(async (batch, batchIndex) => { |
| 313 | try { |
| 314 | const batchResults = await callAIExtraction(batch, apiConfig, sourceLang); |
| 315 | |
| 316 | // 更新进度 |
| 317 | processedCount += batch.length; |
| 318 | if (progressCallback) { |
| 319 | progressCallback({ |
| 320 | processed: processedCount, |
| 321 | total: references.length, |
| 322 | batchIndex: batchIndex, |
| 323 | totalBatches: batches.length |
| 324 | }); |
| 325 | } |
| 326 | |
| 327 | return batchResults; |
| 328 | } catch (error) { |
| 329 | console.error(`[ReferenceAIProcessor] Batch ${batchIndex} failed:`, error); |
| 330 | |
| 331 | // 失败时返回原始数据 |
| 332 | return batch.map((ref, idx) => ({ |
| 333 | index: batchIndex * BATCH_SIZE + idx, |
| 334 | rawText: ref, |
| 335 | extractedBy: 'fallback', |
| 336 | error: error.message |
| 337 | })); |
| 338 | } |
| 339 | }); |
| 340 | |
| 341 | // 等待所有批次完成 |
| 342 | const batchResults = await Promise.all(batchPromises); |
| 343 | |
| 344 | // 合并结果 |
| 345 | batchResults.forEach(batchResult => { |
| 346 | results.push(...batchResult); |
| 347 | }); |
| 348 | |
| 349 | return results; |
| 350 | } |
| 351 | |
| 352 | /** |
no test coverage detected