( owner: string, repo: string, content: string, fileName: string, vectorize?: Vectorize, )
| 661 | * @returns Number of vectors stored |
| 662 | */ |
| 663 | export async function storeDocumentationVectors( |
| 664 | owner: string, |
| 665 | repo: string, |
| 666 | content: string, |
| 667 | fileName: string, |
| 668 | vectorize?: Vectorize, |
| 669 | ): Promise<number> { |
| 670 | try { |
| 671 | console.log(`Storing vectors for ${owner}/${repo}`); |
| 672 | |
| 673 | // Check if Vectorize is available |
| 674 | if (!vectorize) { |
| 675 | console.warn("Vectorize binding not available. Skipping vector storage."); |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | // Generate namespace for this repository |
| 680 | const namespace = getRepoNamespace(owner, repo); |
| 681 | console.log(`Using namespace: ${namespace}`); |
| 682 | |
| 683 | // First delete any existing vectors for this repo's namespace |
| 684 | try { |
| 685 | // Query existing vectors in this namespace |
| 686 | const existingVectors = await vectorize.query( |
| 687 | await getEmbeddings(""), // Empty query will match based on namespace |
| 688 | { |
| 689 | namespace: namespace, |
| 690 | returnValues: false, |
| 691 | topK: 100, // Respecting Vectorize's limit of 100 max results |
| 692 | }, |
| 693 | ); |
| 694 | |
| 695 | if (existingVectors?.matches?.length > 0) { |
| 696 | // Extract IDs of vectors to delete |
| 697 | const idsToDelete = existingVectors.matches.map((match) => match.id); |
| 698 | |
| 699 | // Delete the vectors by IDs |
| 700 | await vectorize.deleteByIds(idsToDelete); |
| 701 | console.log( |
| 702 | `Deleted ${idsToDelete.length} existing vectors for ${namespace}`, |
| 703 | ); |
| 704 | } else { |
| 705 | console.log(`No existing vectors found for ${namespace}`); |
| 706 | } |
| 707 | } catch (error) { |
| 708 | console.log(`Error managing existing vectors: ${error}`); |
| 709 | } |
| 710 | |
| 711 | // Use specialized documentation chunking for better results |
| 712 | const chunks = chunkDocumentation(content, fileName); |
| 713 | console.log(`Created ${chunks.length} chunks for ${owner}/${repo}`); |
| 714 | |
| 715 | // Generate embeddings and upsert vectors |
| 716 | const vectors = []; |
| 717 | |
| 718 | for (let i = 0; i < chunks.length; i++) { |
| 719 | const chunk = chunks[i]; |
| 720 | const embedding = await getEmbeddings(chunk); |
nothing calls this directly
no test coverage detected