in projects/deliberation_at_scale/packages/edge-functions/supabase/functions/transcribe/index.ts [127:240]
async function upsertMesagesForTranscript(context: MessagesContext) {
const { text: rawText, roomId, participantId } = context;
const text = rawText?.trim() ?? '';
const existingMessages = await getMessagesByContext(context);
const existingMessageAmount = existingMessages.length;
const sentences = getSentencesWithPunctuation(text);
const targetMessageAmount = Math.ceil(calculateAmountOfSentences(text) / SENTENCES_PER_MESSAGE);
const upsertPromises = [];
const handledExistingMessageIds = [];
// guard: check if the text is long enough to be split into messages
if (text.length <= MIN_TEXT_LENGTH_BEFORE_UPSERT) {
return;
}
// loop all the messages
for (let messageIndex = 0; messageIndex < targetMessageAmount; messageIndex++) {
// get the content for the message
// NOTE: multiply index by 2 because we need to get the punctuation as well
let messageContent = '';
let sentenceAmount = 0;
let targetSentenceAmount = SENTENCES_PER_MESSAGE;
let targetCharacterAmount = MIN_TEXT_LENGTH_SENTENCE * SENTENCES_PER_MESSAGE;
const copiedSentences = [...sentences];
// loop all the sentences
for (let sentenceIndex = 0; sentenceIndex < copiedSentences.length; sentenceIndex += 2) {
const punctuationIndex = sentenceIndex + 1;
const sentence = copiedSentences?.[sentenceIndex] ?? '';
const punctuation = copiedSentences?.[punctuationIndex] ?? '';
const sentenceLength = sentence.length;
// guard: check if the message is long enough
if (messageContent.length >= targetCharacterAmount) {
break;
}
// guard: check if the message has enough sentences
if (sentenceAmount >= targetSentenceAmount) {
break;
}
// add the sentence and punctuation to the message
messageContent += sentence + punctuation;
// update the sentence amount
sentenceAmount += 1;
// remove the sentence and punctuation so it won't be used again
// NOTE: always remove the first one, because splice is cutting the array
sentences.splice(0, 2);
}
// guard: make sure the message is valid
if (!messageContent) {
break;
}
// check whether this should be existing message or a new one
if (messageIndex < existingMessageAmount) {
const existingMessage = existingMessages[messageIndex];
const { id: existingMessageId, content: existingMessageContent } = existingMessage;
// guard: skip when content is identical
if (existingMessageContent === messageContent) {
continue;
}
const updatePromise = supabaseAdminClient
.from('messages')
.update({
active: true,
content: messageContent,
})
.eq('id', existingMessageId);
// console.log('updatePromise', existingMessageId, messageContent);
upsertPromises.push(updatePromise);
handledExistingMessageIds.push(existingMessageId);
} else {
const insertPromise = supabaseAdminClient
.from('messages')
.insert({
active: true,
content: messageContent,
room_id: roomId,
participant_id: participantId,
type: 'voice',
});
// console.log('insertPromise', messageContent);
upsertPromises.push(insertPromise);
}
}
// deactivate the unhandled existing messages where no content was left for anymore
// const unhandledExistingMessages = existingMessages.filter((message) => {
// const { id: existingMessageId } = message;
// return !handledExistingMessageIds.includes(existingMessageId);
// });
// unhandledExistingMessages.map((message) => {
// const { id: existingMessageId } = message;
// const updatePromise = supabaseAdminClient
// .from('messages')
// .update({
// active: false,
// })
// .eq('id', existingMessageId);
// upsertPromises.push(updatePromise);
// });
const results = await Promise.allSettled(upsertPromises);
}