add putEmbedding API and return getLatestEmbeddings and runEmbeddingsSync

This commit is contained in:
Abhinav 2023-10-18 14:54:29 +05:30
parent 4f5d19de77
commit fbbd4c9c3a
3 changed files with 31 additions and 30 deletions

View file

@ -121,7 +121,7 @@ import { syncEntities } from 'services/entityService';
import { constructUserIDToEmailMap } from 'services/collectionService';
import { getLocalFamilyData } from 'utils/user/family';
import InMemoryStore, { MS_KEYS } from 'services/InMemoryStore';
import { runEmbeddingsSync } from 'services/embeddingService';
import { syncEmbeddings } from 'services/embeddingService';
export const DeadCenter = styled('div')`
flex: 1;
@ -693,7 +693,7 @@ export default function Gallery() {
await syncTrash(collections, setTrashedFiles);
await syncEntities();
await syncMapEnabled();
await runEmbeddingsSync();
await syncEmbeddings();
} catch (e) {
switch (e.message) {
case ServerErrorCodes.SESSION_EXPIRED:

View file

@ -1,4 +1,8 @@
import { Embedding, GetEmbeddingDiffResponse } from 'types/embedding';
import {
Embedding,
GetEmbeddingDiffResponse,
PutEmbeddingRequest,
} from 'types/embedding';
import ComlinkCryptoWorker from 'utils/comlink/ComlinkCryptoWorker';
import { getEndpoint } from 'utils/common/apiUtil';
import { addLogLine } from 'utils/logging';
@ -15,7 +19,7 @@ const DIFF_LIMIT = 500;
const EMBEDDINGS_TABLE = 'embeddings';
const EMBEDDING_SYNC_TIME_TABLE = 'embedding_sync_time';
const getLocalEmbeddings = async () => {
export const getLocalEmbeddings = async () => {
const entities: Array<Embedding> =
(await localForage.getItem<Embedding[]>(EMBEDDINGS_TABLE)) || [];
return entities;
@ -25,31 +29,8 @@ const getEmbeddingSyncTime = async () => {
return (await localForage.getItem<number>(EMBEDDING_SYNC_TIME_TABLE)) ?? 0;
};
export const getLatestEmbeddings = async () => {
export const syncEmbeddings = async () => {
try {
await runEmbeddingsSync();
return await getLocalEmbeddings();
} catch (e) {
logError(e, 'failed to get latest embeddings');
throw e;
}
};
let syncInProgress: Promise<void> = null;
export const runEmbeddingsSync = async () => {
if (syncInProgress) {
return syncInProgress;
}
syncInProgress = syncEmbeddings();
return syncInProgress;
};
const syncEmbeddings = async () => {
try {
if (syncInProgress) {
return syncInProgress;
}
let embeddings = await getLocalEmbeddings();
const localFiles = await getLocalFiles();
const fileIdToKeyMap = new Map<number, string>();
@ -94,8 +75,6 @@ const syncEmbeddings = async () => {
} while (response.diff.length === DIFF_LIMIT);
} catch (e) {
logError(e, 'Sync embeddings failed');
} finally {
syncInProgress = null;
}
};
@ -122,3 +101,18 @@ export const getEmbeddingsDiff = async (
throw e;
}
};
export const putEmbedding = async (putEmbeddingReq: PutEmbeddingRequest) => {
try {
const token = getToken();
if (!token) {
return;
}
await HTTPService.put(`${ENDPOINT}/embeddings`, putEmbeddingReq, null, {
'X-Auth-Token': token,
});
} catch (e) {
logError(e, 'put embedding failed');
throw e;
}
};

View file

@ -21,3 +21,10 @@ export interface Embedding
export interface GetEmbeddingDiffResponse {
diff: EncryptedEmbedding[];
}
export interface PutEmbeddingRequest {
fileID: number;
model: Model;
encryptedEmbedding: string;
decryptionHeader: string;
}