| 1 | import request from "request"; |
| 2 | import { Chunk } from "../.."; |
| 3 | |
| 4 | export interface SiteIndexingResults { |
| 5 | chunks: (Chunk & { embedding: number[] })[]; |
| 6 | url: string; |
| 7 | title: string; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Strip off the trailing forward slash from the given URL |
| 12 | * @param url The URL of the document |
| 13 | * @returns A sanitized URL without trailing forward slash |
| 14 | */ |
| 15 | const stripTrailingSlash = (url: string): string => { |
| 16 | return url.replace(/\/+$/, ""); |
| 17 | }; |
| 18 | |
| 19 | export class DocsCache { |
| 20 | static readonly AWS_REGION: string = "us-west-1"; |
| 21 | static readonly BUCKET_NAME: string = "continue-preindexed-docs"; |
| 22 | |
| 23 | /** |
| 24 | * Normalizes an embedding ID by stripping the constructor name part. |
| 25 | * This is done because we don't care about the provider, just the |
| 26 | * model and the max embedding chunk size. |
| 27 | */ |
| 28 | static normalizeEmbeddingId(embeddingId: string): string { |
| 29 | // Split by "::" and remove the first part (constructor name) |
| 30 | const parts = embeddingId.split("::"); |
| 31 | if (parts.length <= 1) return embeddingId; // Return original if no "::" found |
| 32 | |
| 33 | // Return everything except the first part, joining with "::" |
| 34 | return parts.slice(1).join("::"); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Gets the filepath for a given embedding ID and URL |
| 39 | */ |
| 40 | static getFilepathForEmbeddingIdAndUrl( |
| 41 | embeddingId: string, |
| 42 | url: string, |
| 43 | ): string { |
| 44 | const normalizedEmbeddingId = DocsCache.normalizeEmbeddingId(embeddingId); |
| 45 | const sanitizedUrl = stripTrailingSlash(url); |
| 46 | const normalizedUrl = encodeURIComponent(sanitizedUrl.replace(/\//g, "_")); |
| 47 | return normalizedEmbeddingId + "/" + normalizedUrl; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Gets the fully qualified S3 URL for a given filepath |
| 52 | */ |
| 53 | private static getS3Url(filepath: string): string { |
| 54 | const pathname = filepath.split("/").map(encodeURIComponent).join("/"); |
| 55 | return `https://${this.BUCKET_NAME}.s3.${this.AWS_REGION}.amazonaws.com/${pathname}`; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Downloads cached site indexing results from S3 for a given embedding ID and URL |
| 60 | * @param embeddingId The embedding ID |
| 61 | * @param url The URL of the document |
| 62 | * @returns The downloaded data as a string |
| 63 | */ |
| 64 | static async getDocsCacheForUrl( |
| 65 | embeddingId: string, |
| 66 | url: string, |
| 67 | ): Promise<string> { |
| 68 | const filepath = DocsCache.getFilepathForEmbeddingIdAndUrl( |
| 69 | embeddingId, |
| 70 | url, |
| 71 | ); |
| 72 | |
| 73 | return new Promise<string>((resolve, reject) => { |
| 74 | let data = ""; |
| 75 | const url = this.getS3Url(filepath); |
| 76 | const download = request({ |
| 77 | url, |
| 78 | }); |
| 79 | |
| 80 | download.on("response", (response: any) => { |
| 81 | if (response.statusCode !== 200) { |
| 82 | reject( |
| 83 | new Error("There was an error retrieving the pre-indexed doc"), |
| 84 | ); |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | download.on("error", (err: any) => reject(err)); |
| 89 | download.on("data", (chunk: any) => (data += chunk)); |
| 90 | download.on("end", () => resolve(data)); |
| 91 | }); |
| 92 | } |
| 93 | } |