Skip to content

Commit

Permalink
fix: changed the order of the similarity search result
Browse files Browse the repository at this point in the history
  • Loading branch information
sshivaditya committed Oct 10, 2024
1 parent 03f0110 commit 4bce42c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
27 changes: 20 additions & 7 deletions src/adapters/supabase/helpers/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | null, isPrivate: boolean) {
async updateComment(
markdown: string | null,
commentNodeId: string,
authorId: number,
payload: Record<string, unknown> | 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);
Expand All @@ -63,12 +70,18 @@ export class Comment extends SuperSupabase {
payload = null as Record<string, unknown> | 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);
}
}
}

Expand Down
25 changes: 14 additions & 11 deletions src/adapters/supabase/helpers/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | null, isPrivate: boolean) {
//Create the embedding for this comment
async updateIssue(markdown: string | null, issueNodeId: string, payload: Record<string, unknown> | 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<string, unknown> | 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);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/handlers/issue-deduplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}^]`;
Expand Down
7 changes: 5 additions & 2 deletions src/handlers/update-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/update-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ 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) {
throw new Error("Issue body is empty");
}
//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 });
Expand Down

0 comments on commit 4bce42c

Please sign in to comment.