import { createGetFileOperationUrl, createUploadFileOperationUrl } from "./lib"; import { CloseVectorHNSWNode as HNSWLib } from './hnswlib'; import fs from 'fs'; import tar from 'tar'; import path from 'path'; import axios, { AxiosProgressEvent } from 'axios'; import { CloseVectorEmbeddings } from 'closevector-common'; export async function uploadOrUpdateIndexFile(fileUrl: string, options: { uuid?: string, description: string, accessKey: string, secret: string, uploadUrl: string, onProgress?: (progress: AxiosProgressEvent) => void }) { // checking file existence of fileUrl on local if (!(await fs.promises.access(fileUrl).then(() => true).catch(() => false))) { throw new Error("File not found"); } // creating file readstream let fileStream = fs.createReadStream(fileUrl); let contentLength = (await fs.promises.stat(fileUrl)).size; // uploading using axios const res = await axios.post(options.uploadUrl, fileStream, { headers: { "Content-Type": "application/octet-stream", "Content-Length": contentLength, }, onUploadProgress: (progressEvent) => { if (options.onProgress) { options.onProgress(progressEvent) } } }); if (res.status !== 200) { throw new Error("Upload failed"); } } export const upsertIndex = async (lib: HNSWLib, options: { description: string, public?: boolean, uuid?: string, accessKey: string, secret: string, onProgress?: (progress: AxiosProgressEvent) => void }) => { const { accessKey } = options; try { const uploadUrlResp = await createUploadFileOperationUrl({ ...options, }); const uuidOfNamespace = `${uploadUrlResp.uuid}`; const folderPath = '/tmp/' + uuidOfNamespace + '.hnsw'; const tarPath = '/tmp/' + uuidOfNamespace + '.tar.gz'; console.log("creating index:", uuidOfNamespace, accessKey, folderPath, tarPath); await lib.save(folderPath); // compressing using gzip const tarStream = tar.create({ cwd: path.dirname(folderPath), gzip: true }, [path.basename(folderPath)]); const stream = tarStream .pipe(fs.createWriteStream(tarPath)) let promise = new Promise((resolve, reject) => { stream.on('finish', () => { resolve(uuidOfNamespace); }); stream.on('error', (err: any) => { reject(err); }); stream.on('finish', () => { }) }); await promise; await fs.promises.rm(folderPath, { recursive: true }) await uploadOrUpdateIndexFile(tarPath, { uploadUrl: uploadUrlResp.url, ...options }); await fs.promises.rm(tarPath); return uploadUrlResp.uuid; } catch (e) { throw e; } } export const getIndex = async (options: { uuid: string, accessKey: string, secret: string, embeddings: CloseVectorEmbeddings, onProgress?: (progress: AxiosProgressEvent) => void }) => { try { const uuidOfNamespace = `${options.uuid}`; const folderPath = '/tmp/' + uuidOfNamespace + '.hnsw'; const tarPath = '/tmp/' + uuidOfNamespace + '.tar.gz'; console.log("getting index:", options.uuid); const getUrlResp = await createGetFileOperationUrl({ ...options, }); const url = getUrlResp.url; // download using axios const res = await axios.get(url, { responseType: 'stream', onDownloadProgress: (progressEvent) => { if (options.onProgress) { options.onProgress(progressEvent) } } }); let readSteam = res.data; let newSteam = readSteam.pipe(tar.extract({ cwd: path.dirname(folderPath) })); let promise = new Promise((resolve, reject) => { newSteam.on('finish', () => { resolve(uuidOfNamespace); }); newSteam.on('error', (err: any) => { reject(err); }); newSteam.on('finish', () => { }); }); await promise; const lib = await HNSWLib.load(folderPath, options.embeddings); await fs.promises.rm(folderPath, { recursive: true }); return lib; } catch (e) { throw e; } }