From 28c4c110f7b272f789f6bc2d336b10ec58433171 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 24 Mar 2025 10:55:17 -0700 Subject: [PATCH] remove CDN cache --- core/indexing/docs/DocsCache.ts | 54 +++++++++++++++++++++++++-------- core/util/CDNClient.ts | 40 ------------------------ 2 files changed, 42 insertions(+), 52 deletions(-) delete mode 100644 core/util/CDNClient.ts diff --git a/core/indexing/docs/DocsCache.ts b/core/indexing/docs/DocsCache.ts index 7f7737db0..5fba54afd 100644 --- a/core/indexing/docs/DocsCache.ts +++ b/core/indexing/docs/DocsCache.ts @@ -1,5 +1,5 @@ +import request from "request"; import { Chunk } from "../.."; -import { CdnClient } from "../../util/CDNClient"; export interface SiteIndexingResults { chunks: (Chunk & { embedding: number[] })[]; @@ -8,7 +8,8 @@ export interface SiteIndexingResults { } export class DocsCache { - static readonly DIR_PREFIX: string = "docs-cache"; + static readonly AWS_REGION: string = "us-west-1"; + static readonly BUCKET_NAME: string = "continue-indexed-docs"; /** * Normalizes an embedding ID by stripping the constructor name part. @@ -24,19 +25,27 @@ export class DocsCache { return parts.slice(1).join("::"); } - static getFilepathForEmbeddingId(embeddingId: string, url: string): string { - // Use normalized embedding ID for the cache key + /** + * Gets the filepath for a given embedding ID and URL + */ + static getFilepathForEmbeddingIdAndUrl( + embeddingId: string, + url: string, + ): string { const normalizedEmbeddingId = DocsCache.normalizeEmbeddingId(embeddingId); const normalizedUrl = encodeURIComponent(url.replace(/\//g, "_")); - - // Organize by URL -> embedding ID for easier bulk deletion of a site - const filepath = `${DocsCache.DIR_PREFIX}/${normalizedUrl}/${normalizedEmbeddingId}`; - - return filepath; + return normalizedEmbeddingId + "/" + normalizedUrl; } /** - * Downloads cached site indexing results for a given embedding ID and URL + * Gets the fully qualified S3 URL for a given filepath + */ + private static getS3Url(filepath: string): string { + return `https://${this.BUCKET_NAME}.s3.${this.AWS_REGION}.amazonaws.com/${filepath}`; + } + + /** + * Downloads cached site indexing results from S3 for a given embedding ID and URL * @param embeddingId The embedding ID * @param url The URL of the document * @returns The downloaded data as a string @@ -45,7 +54,28 @@ export class DocsCache { embeddingId: string, url: string, ): Promise { - const filepath = this.getFilepathForEmbeddingId(embeddingId, url); - return await CdnClient.downloadFromCdn(filepath); + const filepath = DocsCache.getFilepathForEmbeddingIdAndUrl( + embeddingId, + url, + ); + + return new Promise((resolve, reject) => { + let data = ""; + const download = request({ + url: this.getS3Url(filepath), + }); + + download.on("response", (response: any) => { + if (response.statusCode !== 200) { + reject( + new Error("There was an error retrieving the pre-indexed doc"), + ); + } + }); + + download.on("error", (err: any) => reject(err)); + download.on("data", (chunk: any) => (data += chunk)); + download.on("end", () => resolve(data)); + }); } } diff --git a/core/util/CDNClient.ts b/core/util/CDNClient.ts deleted file mode 100644 index 58d9e0fcc..000000000 --- a/core/util/CDNClient.ts +++ /dev/null @@ -1,40 +0,0 @@ -import request from "request"; - -/** - * Generic CDN client for handling downloads from content delivery networks - */ -export class CdnClient { - // Define the CDN base URL as a static readonly property - private static readonly CDN_BASE_URL: string = "cdn.continue.dev"; - - /** - * Gets the fully qualified CDN URL for a given filepath - */ - static getCdnUrlForFilename(filepath: string): string { - return `https://${CdnClient.CDN_BASE_URL}/${filepath}`; - } - - /** - * Downloads a file from the CDN - * @param filepath The path to the file on the CDN - * @returns The downloaded data as a string - */ - static async downloadFromCdn(filepath: string): Promise { - return new Promise((resolve, reject) => { - let data = ""; - const download = request({ - url: CdnClient.getCdnUrlForFilename(filepath), - }); - - download.on("response", (response: any) => { - if (response.statusCode !== 200) { - reject(new Error("There was an error retrieving the file from CDN")); - } - }); - - download.on("error", (err: any) => reject(err)); - download.on("data", (chunk: any) => (data += chunk)); - download.on("end", () => resolve(data)); - }); - } -}