Skip to content

Commit

Permalink
Rough in local similar card fetching machinery.
Browse files Browse the repository at this point in the history
Part of #646.
  • Loading branch information
jkomoros committed Nov 16, 2023
1 parent d176b65 commit 3b1357b
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/actions/similarity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'firebase/functions';

import {
EDITING_UPDATE_SIMILAR_CARDS,
UPDATE_CARD_SIMILARITY
} from '../actions.js';

Expand Down Expand Up @@ -73,7 +74,7 @@ type SimilarCardsResponseData = {

//Extracts only the properties necessary for EmbeddableCard, which for example
//is useful when transmitting to similarCards endpoint.
export const pickEmbeddableCard = (card : Card) : EmbeddableCard => {
const pickEmbeddableCard = (card : Card) : EmbeddableCard => {
return {
id: card.id,
body: card.body,
Expand Down Expand Up @@ -102,6 +103,23 @@ const similarCards = async (cardID : CardID, lastUpdated? : MillisecondsSinceEpo
return result.data;
};

const similarCardsForRawCard = async (card : EmbeddableCard) : Promise<SimilarCardsResponseData> => {
if (!QDRANT_ENABLED) {
return {
success: false,
code: 'qdrant-disabled',
error: 'Qdrant isn\'t enabled'
};
}

const request : SimilarCardsRequestData = {
card_id: card.id,
card: card
};
const result = await similarCardsCallable(request);
return result.data;
};

const TIME_TO_WAIT_FOR_STALE : MillisecondsSinceEpoch = 10 * 60 * 1000;
const DELAY_FOR_STALE : MillisecondsSinceEpoch = 2.5 * 1000;

Expand Down Expand Up @@ -159,4 +177,35 @@ export const fetchSimilarCardsIfEnabled = (cardID : CardID) : boolean => {
//This will return immediately.
store.dispatch(fetchSimilarCards(cardID, card?.updated?.toMillis()));
return true;
};

const fetchSimilarCardsToCardContent = (card : Card) : ThunkSomeAction => async (dispatch) => {
const embeddableCard = pickEmbeddableCard(card);

const result = await similarCardsForRawCard(embeddableCard);

if (result.success == false) {
console.warn(`similarCards failed: ${result.code}: ${result.error}`);
dispatch({
type: EDITING_UPDATE_SIMILAR_CARDS,
//Signal that it failed but still did get a response, so the results are now final.
similarity: {}
});
return;
}

dispatch({
type: EDITING_UPDATE_SIMILAR_CARDS,
similarity: Object.fromEntries(result.cards)
});

};

//Returns true if you should expect an UPDATE_CARD_SIMLIARITY for that cardID in the future, and false if not.
export const fetchSimilarCardsForCardIfEnabled = (card : Card) : boolean => {
if (!QDRANT_ENABLED) return false;

//This will return immediately.
store.dispatch(fetchSimilarCardsToCardContent(card));
return true;
};

0 comments on commit 3b1357b

Please sign in to comment.