add conditional sync logic

This commit is contained in:
Abhinav 2024-01-10 10:39:01 +05:30
parent f38b5b0a53
commit ce28f030fe

View file

@ -26,7 +26,7 @@ const EMBEDDINGS_TABLE_V1 = 'embeddings';
const EMBEDDINGS_TABLE = 'embeddings_v2'; const EMBEDDINGS_TABLE = 'embeddings_v2';
const EMBEDDING_SYNC_TIME_TABLE = 'embedding_sync_time'; const EMBEDDING_SYNC_TIME_TABLE = 'embedding_sync_time';
export const getLocalEmbeddings = async () => { export const getLocalEmbeddings = async (model?: Model) => {
const embeddings: Array<Embedding> = await localForage.getItem<Embedding[]>( const embeddings: Array<Embedding> = await localForage.getItem<Embedding[]>(
EMBEDDINGS_TABLE EMBEDDINGS_TABLE
); );
@ -36,14 +36,18 @@ export const getLocalEmbeddings = async () => {
await localForage.setItem(EMBEDDING_SYNC_TIME_TABLE, 0); await localForage.setItem(EMBEDDING_SYNC_TIME_TABLE, 0);
return []; return [];
} }
if (model) {
return embeddings.filter((embedding) => embedding.model === model);
} else {
return embeddings; return embeddings;
}
}; };
const getEmbeddingSyncTime = async () => { const getEmbeddingSyncTime = async () => {
return (await localForage.getItem<number>(EMBEDDING_SYNC_TIME_TABLE)) ?? 0; return (await localForage.getItem<number>(EMBEDDING_SYNC_TIME_TABLE)) ?? 0;
}; };
export const syncEmbeddings = async () => { export const syncEmbeddings = async (model: Model = Model.ONNX_CLIP) => {
try { try {
let embeddings = await getLocalEmbeddings(); let embeddings = await getLocalEmbeddings();
const localFiles = await getAllLocalFiles(); const localFiles = await getAllLocalFiles();
@ -58,7 +62,7 @@ export const syncEmbeddings = async () => {
addLogLine(`Syncing embeddings sinceTime: ${sinceTime}`); addLogLine(`Syncing embeddings sinceTime: ${sinceTime}`);
let response: GetEmbeddingDiffResponse; let response: GetEmbeddingDiffResponse;
do { do {
response = await getEmbeddingsDiff(sinceTime); response = await getEmbeddingsDiff(sinceTime, model);
if (!response.diff?.length) { if (!response.diff?.length) {
return; return;
} }
@ -117,7 +121,8 @@ export const syncEmbeddings = async () => {
}; };
export const getEmbeddingsDiff = async ( export const getEmbeddingsDiff = async (
sinceTime: number sinceTime: number,
model: Model
): Promise<GetEmbeddingDiffResponse> => { ): Promise<GetEmbeddingDiffResponse> => {
try { try {
const token = getToken(); const token = getToken();
@ -129,7 +134,7 @@ export const getEmbeddingsDiff = async (
{ {
sinceTime, sinceTime,
limit: DIFF_LIMIT, limit: DIFF_LIMIT,
model: Model.ONNX_CLIP, model,
}, },
{ {
'X-Auth-Token': token, 'X-Auth-Token': token,