Skip to content

Commit

Permalink
Hook up a document handler for cards when they change.
Browse files Browse the repository at this point in the history
This is the first time exercising the current pipeline, and revels a few problems, like "invalid-card-id" is somehow in the document?

Part of #646.
  • Loading branch information
jkomoros committed Oct 29, 2023
1 parent 1ec5189 commit 695779b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
27 changes: 25 additions & 2 deletions functions/src/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import {

import {
FieldValue,
Timestamp
Timestamp,
} from 'firebase-admin/firestore';

import {
Change,
firestore
} from 'firebase-functions'

const DOM = new JSDOM();

const EMBEDDINGS_COLLECTION = 'embeddings';
Expand Down Expand Up @@ -79,6 +84,7 @@ const innerTextForHTML = (html : string) : string => {

const textContentForEmbeddingForCard = (card : Card) : string => {
//TODO: ideally this would literally be the cardPlainContent implementation from src/util.ts
//TODO: this shouldn't use the title for working-notes cards, since it's computed.
if (card.card_type != CARD_TYPE_CONTENT && card.card_type != CARD_TYPE_WORKING_NOTES) return '';
const body = innerTextForHTML(card[TEXT_FIELD_BODY]);
const title = card[TEXT_FIELD_TITLE] || '';
Expand Down Expand Up @@ -116,7 +122,7 @@ const embeddingInfoIDForCard = (card : Card, embeddingType : EmbeddingType = DEF
return card.id + '+' + embeddingType + '+' + version;
};

export const processCard = async (card : Card) : Promise<void> => {
const processCard = async (card : Card) : Promise<void> => {
const id = embeddingInfoIDForCard(card);
const record = await db.collection(EMBEDDINGS_COLLECTION).doc(id).get();
const text = textContentForEmbeddingForCard(card);
Expand All @@ -141,4 +147,21 @@ export const processCard = async (card : Card) : Promise<void> => {

await db.collection(EMBEDDINGS_COLLECTION).doc(id).update(info);

};

const deleteCard = async (card : Card) : Promise<void> => {
const id = embeddingInfoIDForCard(card);
await db.collection(EMBEDDINGS_COLLECTION).doc(id).delete();
//TODO: also delete item from hnsw index.
};

export const processCardEmbedding = async (change : Change<firestore.DocumentSnapshot>) : Promise<void> => {
//TODO: if openai key not set, then silently exit.
if (!change.after.exists) {
const card = {id : change.before.id, ...change.before.data()} as Card;
await deleteCard(card);
return;
}
const card = {id : change.after.id, ...change.after.data()} as Card;
await processCard(card);
};
9 changes: 9 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
slug
} from "./legal.js";

import {
processCardEmbedding
} from "./embeddings.js";

import * as openaiimpl from "./openai.js";

//Runs every three hours
Expand All @@ -41,6 +45,11 @@ export const emailAdminOnMessage = functions.firestore.
document('messages/{messageId}').
onCreate(email.onMessage);

//TODO: only allow a single instance
export const updateCardEmbedding = functions.firestore.
document('cards/{cardID}').
onWrite(processCardEmbedding);

const screenshotApp = express();
screenshotApp.get('/:id', async (req, res) => {
const png = await fetchScreenshotByIDOrSlug(req.params.id);
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ gulp.task(BUILD_TASK, makeExecutor('npm run build'));

gulp.task(GENERATE_SEO_PAGES, makeExecutor('npm run generate:seo:pages'));

gulp.task(FIREBASE_DEPLOY_TASK, makeExecutor(ENABLE_TWITTER ? 'firebase deploy' : 'firebase deploy --only hosting,storage,firestore,functions:emailAdminOnMessage,functions:emailAdminOnStar,functions:legal' + (OPENAI_ENABLED ? ',functions:openai' : '')));
gulp.task(FIREBASE_DEPLOY_TASK, makeExecutor(ENABLE_TWITTER ? 'firebase deploy' : 'firebase deploy --only hosting,storage,firestore,functions:emailAdminOnMessage,functions:emailAdminOnStar,functions:legal' + (OPENAI_ENABLED ? ',functions:openai,functions:updateCardEmbedding' : '')));

gulp.task(FIREBASE_SET_CONFIG_LAST_DEPLOY_AFFECTING_RENDERING, makeExecutor('firebase functions:config:set site.last_deploy_affecting_rendering=' + RELEASE_TAG));

Expand Down

0 comments on commit 695779b

Please sign in to comment.