* Update chunks in the library with modified data from chunk visualizer * Handles metadata updates (keywords, weights, links) and text changes (re-vectorization) * * @param {string} collectionId - Collection ID to update * @param {Object} chunks - Modified chunks object { hash: chunkData, ..
(collectionId, chunks)
| 510 | * @returns {Promise<void>} |
| 511 | */ |
| 512 | async function updateChunksInLibrary(collectionId, chunks) { |
| 513 | CarrotDebug.ui('📝 [updateChunksInLibrary] Starting update...', { |
| 514 | collectionId, |
| 515 | chunkCount: Object.keys(chunks).length |
| 516 | }); |
| 517 | |
| 518 | const library = getContextualLibrary(); |
| 519 | |
| 520 | if (!library[collectionId]) { |
| 521 | throw new Error(`Collection ${collectionId} not found in library`); |
| 522 | } |
| 523 | |
| 524 | const chunksToRevectorize = []; |
| 525 | const updatedHashes = []; |
| 526 | |
| 527 | // Determine which chunks were deleted (present in library, absent from the submitted set) |
| 528 | // so they can be removed from the library and purged from the vector DB below. |
| 529 | const removedHashes = Object.keys(library[collectionId]).filter(hash => !(hash in chunks)); |
| 530 | for (const hash of removedHashes) { |
| 531 | delete library[collectionId][hash]; |
| 532 | } |
| 533 | |
| 534 | // Process each modified chunk |
| 535 | for (const [hash, chunkData] of Object.entries(chunks)) { |
| 536 | const existingChunk = library[collectionId][hash]; |
| 537 | |
| 538 | if (!existingChunk) { |
| 539 | CarrotDebug.error(`⚠️ Chunk ${hash} not found in library - skipping`); |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | // Normalize chunk data structure (handle both flat and nested metadata) |
| 544 | const chunkText = chunkData.text; |
| 545 | const metadata = chunkData.metadata || chunkData; |
| 546 | |
| 547 | // Check if text content changed (requires re-vectorization) |
| 548 | const textChanged = existingChunk.text !== chunkText; |
| 549 | |
| 550 | if (textChanged) { |
| 551 | CarrotDebug.ui(`🔄 Text changed for chunk ${hash} - will re-vectorize`); |
| 552 | chunksToRevectorize.push({ |
| 553 | hash: parseInt(hash), |
| 554 | text: chunkText, |
| 555 | index: metadata.index || 0, |
| 556 | metadata: { |
| 557 | ...metadata, |
| 558 | // Ensure text is NOT stored in metadata (it's separate) |
| 559 | text: undefined |
| 560 | } |
| 561 | }); |
| 562 | } |
| 563 | |
| 564 | // Update library with new data (metadata + text) |
| 565 | // Spread metadata first, then override with text to ensure structure |
| 566 | const { text: _, ...metadataOnly } = metadata; |
| 567 | library[collectionId][hash] = { |
| 568 | text: chunkText, |
| 569 | ...metadataOnly |
nothing calls this directly
no test coverage detected