From 4bce42c2437bf0da9796827744e7d3a7e6fcdf54 Mon Sep 17 00:00:00 2001 From: Shivaditya Shivganesh Date: Thu, 10 Oct 2024 16:28:52 -0400 Subject: [PATCH] fix: changed the order of the similarity search result --- src/adapters/supabase/helpers/comment.ts | 27 ++++++++++++++++++------ src/adapters/supabase/helpers/issues.ts | 25 ++++++++++++---------- src/handlers/issue-deduplication.ts | 3 +++ src/handlers/update-comments.ts | 7 ++++-- src/handlers/update-issue.ts | 3 ++- 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/adapters/supabase/helpers/comment.ts b/src/adapters/supabase/helpers/comment.ts index a0fa89e..295f8ae 100644 --- a/src/adapters/supabase/helpers/comment.ts +++ b/src/adapters/supabase/helpers/comment.ts @@ -54,7 +54,14 @@ export class Comment extends SuperSupabase { this.context.logger.info("Comment created successfully"); } - async updateComment(markdown: string | null, commentNodeId: string, payload: Record | null, isPrivate: boolean) { + async updateComment( + markdown: string | null, + commentNodeId: string, + authorId: number, + payload: Record | null, + isPrivate: boolean, + issueId: string + ) { //Create the embedding for this comment const embedding = Array.from(await this.context.adapters.voyage.embedding.createEmbedding(markdown)); let plaintext: string | null = markdownToPlainText(markdown); @@ -63,12 +70,18 @@ export class Comment extends SuperSupabase { payload = null as Record | null; plaintext = null as string | null; } - const { error } = await this.supabase - .from("issue_comments") - .update({ markdown, plaintext, embedding: embedding, payload, modified_at: new Date() }) - .eq("id", commentNodeId); - if (error) { - this.context.logger.error("Error updating comment", error); + const comments = await this.getComment(commentNodeId); + if (comments && comments.length == 0) { + this.context.logger.info("Comment does not exist, creating a new one"); + await this.createComment(markdown, commentNodeId, authorId, payload, isPrivate, issueId); + } else { + const { error } = await this.supabase + .from("issue_comments") + .update({ markdown, plaintext, embedding: embedding, payload, modified_at: new Date() }) + .eq("id", commentNodeId); + if (error) { + this.context.logger.error("Error updating comment", error); + } } } diff --git a/src/adapters/supabase/helpers/issues.ts b/src/adapters/supabase/helpers/issues.ts index b55e901..059ee75 100644 --- a/src/adapters/supabase/helpers/issues.ts +++ b/src/adapters/supabase/helpers/issues.ts @@ -52,21 +52,24 @@ export class Issues extends SuperSupabase { this.context.logger.info("Issue created successfully"); } - async updateIssue(markdown: string | null, issueNodeId: string, payload: Record | null, isPrivate: boolean) { - //Create the embedding for this comment + async updateIssue(markdown: string | null, issueNodeId: string, payload: Record | null, isPrivate: boolean, authorId: number) { const embedding = Array.from(await this.context.adapters.voyage.embedding.createEmbedding(markdown)); let plaintext: string | null = markdownToPlainText(markdown); if (isPrivate) { - markdown = null as string | null; - payload = null as Record | null; - plaintext = null as string | null; + markdown = null; + payload = null; + plaintext = null; } - const { error } = await this.supabase - .from("issues") - .update({ markdown, plaintext, embedding: embedding, payload, modified_at: new Date() }) - .eq("id", issueNodeId); - if (error) { - this.context.logger.error("Error updating comment", error); + const issues = await this.getIssue(issueNodeId); + if (issues && issues.length == 0) { + this.context.logger.info("Issue does not exist, creating a new one"); + await this.createIssue(issueNodeId, payload, isPrivate, markdown, authorId); + } else { + const { error } = await this.supabase.from("issues").update({ markdown, plaintext, embedding, payload, modified_at: new Date() }).eq("id", issueNodeId); + + if (error) { + this.context.logger.error("Error updating comment", error); + } } } diff --git a/src/handlers/issue-deduplication.ts b/src/handlers/issue-deduplication.ts index 3273a09..0cca940 100644 --- a/src/handlers/issue-deduplication.ts +++ b/src/handlers/issue-deduplication.ts @@ -176,6 +176,9 @@ async function handleSimilarIssuesComment( const highestFootnoteIndex = existingFootnotes.length > 0 ? Math.max(...existingFootnotes.map((fn) => parseInt(fn.match(/\d+/)?.[0] ?? "0"))) : 0; let updatedBody = issueBody; let footnotes: string[] | undefined; + // Sort relevant issues by similarity in ascending order + relevantIssues.sort((a, b) => parseFloat(a.similarity) - parseFloat(b.similarity)); + relevantIssues.forEach((issue, index) => { const footnoteIndex = highestFootnoteIndex + index + 1; // Continue numbering from the highest existing footnote number const footnoteRef = `[^0${footnoteIndex}^]`; diff --git a/src/handlers/update-comments.ts b/src/handlers/update-comments.ts index b1b9d18..6cc9545 100644 --- a/src/handlers/update-comments.ts +++ b/src/handlers/update-comments.ts @@ -7,15 +7,18 @@ export async function updateComment(context: Context) { adapters: { supabase }, } = context; const { payload } = context as { payload: CommentPayload }; + const markdown = payload.comment.body; + const authorId = payload.comment.user?.id || -1; const nodeId = payload.comment.node_id; const isPrivate = payload.repository.private; - const markdown = payload.comment.body || null; + const issueId = payload.issue.node_id; + // Fetch the previous comment and update it in the db try { if (!markdown) { throw new Error("Comment body is empty"); } - await supabase.comment.updateComment(markdown, nodeId, payload, isPrivate); + await supabase.comment.updateComment(markdown, nodeId, authorId, payload, isPrivate, issueId); } catch (error) { if (error instanceof Error) { logger.error(`Error updating comment:`, { error: error, stack: error.stack }); diff --git a/src/handlers/update-issue.ts b/src/handlers/update-issue.ts index 4ec5f34..cec5de2 100644 --- a/src/handlers/update-issue.ts +++ b/src/handlers/update-issue.ts @@ -12,6 +12,7 @@ export async function updateIssue(context: Context) { const nodeId = payload.issue.node_id; const isPrivate = payload.repository.private; const markdown = payload.issue.body + " " + payload.issue.title || null; + const authorId = payload.issue.user?.id || -1; // Fetch the previous issue and update it in the db try { if (!markdown) { @@ -19,7 +20,7 @@ export async function updateIssue(context: Context) { } //clean issue by removing footnotes const cleanedIssue = removeFootnotes(markdown); - await supabase.issue.updateIssue(cleanedIssue, nodeId, payloadObject, isPrivate); + await supabase.issue.updateIssue(cleanedIssue, nodeId, payloadObject, isPrivate, authorId); } catch (error) { if (error instanceof Error) { logger.error(`Error updating issue:`, { error: error, stack: error.stack });