Skip to content

Commit

Permalink
chore: create upon edit if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 17, 2024
1 parent d707c9f commit 48349ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
20 changes: 11 additions & 9 deletions src/adapters/supabase/helpers/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ export class Embeddings extends Super {
modified_at: new Date().toISOString(),
};

const embeddingData = await this.getEmbedding(sourceId);

if (!embeddingData) {
return await this.createEmbedding(sourceId, type, body, metadata);
}

const { error } = await this.supabase.from("content").update(toStore).eq("source_id", sourceId);

if (error) {
Expand All @@ -136,7 +142,7 @@ export class Embeddings extends Super {

async getEmbedding(sourceId: string): Promise<CommentType> {
const { data, error } = await this.supabase.from("content").select("*").eq("source_id", sourceId).single();
if (error && error.message !== "No records found") {
if (error && error.code !== "PGRST116") {
this.context.logger.error("Error getting comment", { err: error, sourceId });
}
return data;
Expand All @@ -151,7 +157,7 @@ export class Embeddings extends Super {

// Working with embeddings

async findSimilarIssues(markdown: string, threshold: number, currentId: string): Promise<IssueSimilaritySearchResult[] | null> {
async findSimilarIssues(markdown: string, threshold: number, currentId: string): Promise<IssueSimilaritySearchResult[]> {
const embedding = await this._embedWithVoyage(markdown);
const { data, error } = await this.supabase.rpc("find_similar_issues", {
current_id: currentId,
Expand Down Expand Up @@ -214,14 +220,10 @@ export class Embeddings extends Super {
}

private _getBody(payload: Context["payload"]) {
const body = [payload.issue.title];

if (isIssueCommentEvent(payload)) {
body.push(payload.comment.body);
} else if (isIssueEvent(payload) && payload.issue.body) {
body.push(payload.issue.body);
return payload.comment.body;
} else if (isIssueEvent(payload)) {
return payload.issue.body;
}

return body.join("\n\n :: ");
}
}
22 changes: 14 additions & 8 deletions src/handlers/task-deduplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@ export async function taskSimilaritySearch(context: Context<"issues.opened">): P
adapters: { supabase },
octokit,
} = context;
const { payload } = context;
const issue = payload.issue;
const issueContent = issue.title + "\n" + issue.body;
const {
payload: { issue, repository },
} = context;
const similarIssues: IssueSimilaritySearchResult[] = [];

similarIssues.push(...(await supabase.embeddings.findSimilarIssues(issue.title, context.config.warningThreshold, issue.node_id)));
if (issue.body) {
similarIssues.push(...(await supabase.embeddings.findSimilarIssues(issue.body, context.config.warningThreshold, issue.node_id)));
}

logger.info(`Found ${similarIssues.length} similar issues`);

// Fetch all similar issues based on settings.warningThreshold
const similarIssues = await supabase.embeddings.findSimilarIssues(issueContent, context.config.warningThreshold, issue.node_id);
if (similarIssues && similarIssues.length > 0) {
const matchIssues = similarIssues.filter((issue) => issue.similarity >= context.config.matchThreshold);
const matchIssues = similarIssues.filter((issue) => issue?.similarity >= context.config.matchThreshold);

// Handle issues that match the MATCH_THRESHOLD (Very Similar)
if (matchIssues.length > 0) {
logger.info(`Similar issue which matches more than ${context.config.matchThreshold} already exists`);
await octokit.issues.update({
owner: payload.repository.owner.login,
repo: payload.repository.name,
owner: repository.owner.login,
repo: repository.name,
issue_number: issue.number,
state: "closed",
state_reason: "not_planned",
Expand Down
8 changes: 4 additions & 4 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("Plugin tests", () => {
expect(okSpy).toHaveBeenNthCalledWith(1, "Successfully created comment!", {
source_id: "test",
type: "comment",
plaintext: `First Issue :: ${STRINGS.HELLO_WORLD}`,
plaintext: `${STRINGS.HELLO_WORLD}`,
embedding: STRINGS.REMOVED_FOR_BREVITY,
metadata: {
authorAssociation: "OWNER",
Expand All @@ -84,7 +84,7 @@ describe("Plugin tests", () => {
expect(okSpy).toHaveBeenNthCalledWith(1, "Successfully updated comment!", {
source_id: "test",
type: "comment",
plaintext: `First Issue :: ${STRINGS.UPDATED_MESSAGE}`,
plaintext: `${STRINGS.UPDATED_MESSAGE}`,
embedding: STRINGS.REMOVED_FOR_BREVITY,
metadata: {
authorAssociation: "OWNER",
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("Plugin tests", () => {
expect(okSpy).toHaveBeenNthCalledWith(1, "Successfully created issue!", {
source_id: "test_issue1",
type: "task",
plaintext: `First Issue :: ${STRINGS.HELLO_WORLD}`,
plaintext: `${STRINGS.HELLO_WORLD}`,
embedding: STRINGS.REMOVED_FOR_BREVITY,
metadata: {
authorAssociation: "OWNER",
Expand All @@ -145,7 +145,7 @@ describe("Plugin tests", () => {
expect(okSpy).toHaveBeenNthCalledWith(1, "Successfully updated issue!", {
source_id: "test_issue1",
type: "task",
plaintext: `First Issue :: ${STRINGS.UPDATED_MESSAGE}`,
plaintext: `${STRINGS.UPDATED_MESSAGE}`,
embedding: STRINGS.REMOVED_FOR_BREVITY,
metadata: {
authorAssociation: "OWNER",
Expand Down

0 comments on commit 48349ff

Please sign in to comment.