From 1214b4ddc385efebbb74172d5fdc26a75477e430 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 9 Apr 2024 21:12:00 +0530 Subject: [PATCH] Web enum cleanup --- web/apps/photos/src/services/clip-service.ts | 5 ++--- .../photos/src/services/embeddingService.ts | 18 +++++++++++------- web/apps/photos/src/types/embedding.tsx | 16 +++++++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/web/apps/photos/src/services/clip-service.ts b/web/apps/photos/src/services/clip-service.ts index 4df9e5728..c6a94213f 100644 --- a/web/apps/photos/src/services/clip-service.ts +++ b/web/apps/photos/src/services/clip-service.ts @@ -8,7 +8,7 @@ import { LS_KEYS, getData } from "@ente/shared/storage/localStorage"; import { FILE_TYPE } from "constants/file"; import isElectron from "is-electron"; import PQueue from "p-queue"; -import { Embedding, Model } from "types/embedding"; +import { Embedding } from "types/embedding"; import { EnteFile } from "types/file"; import { getPersonalFiles } from "utils/file"; import downloadManager from "./download"; @@ -339,12 +339,11 @@ class CLIPService { log.info( `putting clip embedding to server for file: ${file.metadata.title} fileID: ${file.id}`, ); - const model = Model.ONNX_CLIP; await putEmbedding({ fileID: file.id, encryptedEmbedding: encryptedEmbeddingData.encryptedData, decryptionHeader: encryptedEmbeddingData.decryptionHeader, - model, + model: "onnx-clip", }); }; diff --git a/web/apps/photos/src/services/embeddingService.ts b/web/apps/photos/src/services/embeddingService.ts index 79fa5ef7e..c4c0075c6 100644 --- a/web/apps/photos/src/services/embeddingService.ts +++ b/web/apps/photos/src/services/embeddingService.ts @@ -5,11 +5,11 @@ import HTTPService from "@ente/shared/network/HTTPService"; import { getEndpoint } from "@ente/shared/network/api"; import localForage from "@ente/shared/storage/localForage"; import { getToken } from "@ente/shared/storage/localStorage/helpers"; -import { +import type { Embedding, + EmbeddingModel, EncryptedEmbedding, GetEmbeddingDiffResponse, - Model, PutEmbeddingRequest, } from "types/embedding"; import { EnteFile } from "types/file"; @@ -40,10 +40,10 @@ export const getAllLocalEmbeddings = async () => { export const getLocalEmbeddings = async () => { const embeddings = await getAllLocalEmbeddings(); - return embeddings.filter((embedding) => embedding.model === Model.ONNX_CLIP); + return embeddings.filter((embedding) => embedding.model === "onnx-clip"); }; -const getModelEmbeddingSyncTime = async (model: Model) => { +const getModelEmbeddingSyncTime = async (model: EmbeddingModel) => { return ( (await localForage.getItem( `${model}-${EMBEDDING_SYNC_TIME_TABLE}`, @@ -51,11 +51,15 @@ const getModelEmbeddingSyncTime = async (model: Model) => { ); }; -const setModelEmbeddingSyncTime = async (model: Model, time: number) => { +const setModelEmbeddingSyncTime = async ( + model: EmbeddingModel, + time: number, +) => { await localForage.setItem(`${model}-${EMBEDDING_SYNC_TIME_TABLE}`, time); }; -export const syncEmbeddings = async (models: Model[] = [Model.ONNX_CLIP]) => { +export const syncEmbeddings = async () => { + const models: EmbeddingModel[] = ["onnx-clip"]; try { let allEmbeddings = await getAllLocalEmbeddings(); const localFiles = await getAllLocalFiles(); @@ -138,7 +142,7 @@ export const syncEmbeddings = async (models: Model[] = [Model.ONNX_CLIP]) => { export const getEmbeddingsDiff = async ( sinceTime: number, - model: Model, + model: EmbeddingModel, ): Promise => { try { const token = getToken(); diff --git a/web/apps/photos/src/types/embedding.tsx b/web/apps/photos/src/types/embedding.tsx index b618ed315..c0014d01e 100644 --- a/web/apps/photos/src/types/embedding.tsx +++ b/web/apps/photos/src/types/embedding.tsx @@ -1,10 +1,16 @@ -export enum Model { - ONNX_CLIP = "onnx-clip", -} +/** + * The embeddings models that we support. + * + * This is an exhaustive set of values we pass when PUT-ting encrypted + * embeddings on the server. However, we should be prepared to receive an + * {@link EncryptedEmbedding} with a model value distinct from one of these. + */ +export type EmbeddingModel = "onnx-clip"; export interface EncryptedEmbedding { fileID: number; - model: Model; + /** @see {@link EmbeddingModel} */ + model: string; encryptedEmbedding: string; decryptionHeader: string; updatedAt: number; @@ -24,7 +30,7 @@ export interface GetEmbeddingDiffResponse { export interface PutEmbeddingRequest { fileID: number; - model: Model; + model: EmbeddingModel; encryptedEmbedding: string; decryptionHeader: string; }