import { addDocs } from "./db.js";
const request = require("request");
export async function downloadFromS3(bucket, fileName, region) {
    return new Promise((resolve, reject) => {
        let data = "";
        const download = request({
            url: `https://${bucket}.s3.${region}.amazonaws.com/${fileName}`,
        });
        download.on("response", (response) => {
            if (response.statusCode !== 200) {
                reject(new Error("No body returned when downloading from S3 bucket"));
            }
        });
        download.on("error", (err) => {
            reject(err);
        });
        download.on("data", (chunk) => {
            data += chunk;
        });
        download.on("end", () => {
            resolve(data);
        });
    });
}
export async function downloadPreIndexedDocs(embeddingsProviderId, title) {
    const data = await downloadFromS3("continue-indexed-docs", `${embeddingsProviderId}/${title}`, "us-west-1");
    const results = JSON.parse(data);
    await addDocs(results.title, new URL(results.url), results.chunks, results.chunks.map((c) => c.embedding));
}
