From 847eea8a974c365d532d8b66c290790ef08c1146 Mon Sep 17 00:00:00 2001 From: Alex Komoroske Date: Sun, 12 Nov 2023 10:32:39 -0700 Subject: [PATCH] Wire through last-updated to the similarCards endpoint from client. Part of #670. Part of #646. --- functions/src/types.ts | 4 +++- src/actions/similarity.ts | 28 ++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/functions/src/types.ts b/functions/src/types.ts index d0c46c30..3eb39059 100644 --- a/functions/src/types.ts +++ b/functions/src/types.ts @@ -178,6 +178,8 @@ export type LegalResponseData = { reason: string }; +type MillisecondsSinceEpoch = number; + //Replicated in `src/actions/similarity.ts` export type SimilarCardsRequestData = { card_id: CardID @@ -185,7 +187,7 @@ export type SimilarCardsRequestData = { //timestamp in milliseconds since epoch. If provided, results will only be //provided if the Vector point has a last-updated since then, otherwise //error of `stale`. - last_updated? : number + last_updated? : MillisecondsSinceEpoch //TODO: include a limit diff --git a/src/actions/similarity.ts b/src/actions/similarity.ts index 246d067d..31e49101 100644 --- a/src/actions/similarity.ts +++ b/src/actions/similarity.ts @@ -14,7 +14,7 @@ import { } from '../config.GENERATED.SECRET.js'; import { - selectCardSimilarity + selectCardSimilarity, selectRawCards } from '../selectors.js'; import { @@ -38,6 +38,9 @@ import { //Replicated in src/actions/similarity.ts type EmbeddableCard = Pick; +//Replicated in src/actions/similarity.ts +type MillisecondsSinceEpoch = number; + //Replicated in `src/actions/similarity.ts` type SimilarCardsRequestData = { card_id: CardID @@ -45,7 +48,7 @@ type SimilarCardsRequestData = { //timestamp in milliseconds since epoch. If provided, results will only be //provided if the Vector point has a last-updated since then, otherwise //error of `stale`. - last_updated? : number + last_updated? : MillisecondsSinceEpoch //TODO: include a limit @@ -70,7 +73,7 @@ type SimilarCardsResponseData = { const similarCardsCallable = httpsCallable(functions, 'similarCards'); -const similarCards = async (cardID : CardID) : Promise => { +const similarCards = async (cardID : CardID, lastUpdated : MillisecondsSinceEpoch) : Promise => { if (!QDRANT_ENABLED) { return { success: false, @@ -78,17 +81,22 @@ const similarCards = async (cardID : CardID) : Promise error: 'Qdrant isn\'t enabled' }; } - const request = { - card_id: cardID + + //TODO: there's a massive problem with this flow currently... if a card is updated but + //doesn't have an embedding updated. + + const request : SimilarCardsRequestData = { + card_id: cardID, + last_updated: lastUpdated }; const result = await similarCardsCallable(request); return result.data; }; -const fetchSimilarCards = (cardID : CardID) : ThunkSomeAction => async (dispatch) => { +const fetchSimilarCards = (cardID : CardID, lastUpdated: MillisecondsSinceEpoch) : ThunkSomeAction => async (dispatch) => { if (!cardID) return; - const result = await similarCards(cardID); + const result = await similarCards(cardID, lastUpdated); if (result.success == false) { @@ -124,7 +132,11 @@ export const fetchSimilarCardsIfEnabled = (cardID : CardID) : boolean => { if (similarity[cardID]) { return false; } + + const cards = selectRawCards(state); + const card = cards[cardID]; + if (!card) throw new Error(`Couldn't find card ${cardID}`); //This will return immediately. - store.dispatch(fetchSimilarCards(cardID)); + store.dispatch(fetchSimilarCards(cardID, card.updated.toMillis())); return true; }; \ No newline at end of file