Skip to content

Commit 5bf0037

Browse files
committed
fix(review): review refactoring
1 parent 3dcd73c commit 5bf0037

4 files changed

Lines changed: 82 additions & 47 deletions

File tree

.github/actions/javascript/proposalPoliceComment/index.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11543,11 +11543,35 @@ function wrappy (fn, cb) {
1154311543

1154411544
"use strict";
1154511545

11546+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
11547+
if (k2 === undefined) k2 = k;
11548+
var desc = Object.getOwnPropertyDescriptor(m, k);
11549+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11550+
desc = { enumerable: true, get: function() { return m[k]; } };
11551+
}
11552+
Object.defineProperty(o, k2, desc);
11553+
}) : (function(o, m, k, k2) {
11554+
if (k2 === undefined) k2 = k;
11555+
o[k2] = m[k];
11556+
}));
11557+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
11558+
Object.defineProperty(o, "default", { enumerable: true, value: v });
11559+
}) : function(o, v) {
11560+
o["default"] = v;
11561+
});
11562+
var __importStar = (this && this.__importStar) || function (mod) {
11563+
if (mod && mod.__esModule) return mod;
11564+
var result = {};
11565+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
11566+
__setModuleDefault(result, mod);
11567+
return result;
11568+
};
1154611569
var __importDefault = (this && this.__importDefault) || function (mod) {
1154711570
return (mod && mod.__esModule) ? mod : { "default": mod };
1154811571
};
1154911572
Object.defineProperty(exports, "__esModule", ({ value: true }));
1155011573
const core_1 = __nccwpck_require__(42186);
11574+
const core = __importStar(__nccwpck_require__(42186));
1155111575
const github_1 = __nccwpck_require__(95438);
1155211576
const date_fns_1 = __nccwpck_require__(25468);
1155311577
const date_fns_tz_1 = __nccwpck_require__(99297);
@@ -11611,7 +11635,9 @@ async function run() {
1161111635
// Fetch all comments in the issue
1161211636
console.log('Get comments for issue #', issueNumber);
1161311637
const commentsResponse = await GithubUtils_1.default.getAllCommentDetails(issueNumber);
11638+
core.startGroup('Comments Response');
1161411639
console.log('commentsResponse', commentsResponse);
11640+
core.endGroup();
1161511641
let didFindDuplicate = false;
1161611642
for (const previousProposal of commentsResponse) {
1161711643
const isProposal = !!previousProposal.body?.includes(CONST_1.default.PROPOSAL_KEYWORD);
@@ -11629,7 +11655,9 @@ async function run() {
1162911655
const duplicateCheckResponse = await openAI.promptAssistant(assistantID, duplicateCheckPrompt);
1163011656
let similarityPercentage = 0;
1163111657
const parsedDuplicateCheckResponse = openAI.parseAssistantResponse(duplicateCheckResponse);
11658+
core.startGroup('Parsed Duplicate Check Response');
1163211659
console.log('parsedDuplicateCheckResponse: ', parsedDuplicateCheckResponse);
11660+
core.endGroup();
1163311661
if (parsedDuplicateCheckResponse) {
1163411662
const { similarity = 0 } = parsedDuplicateCheckResponse ?? {};
1163511663
similarityPercentage = (0, ActionUtils_1.convertToNumber)(similarity);
@@ -11663,7 +11691,9 @@ async function run() {
1166311691
: proposalPolice_1.default.getPromptForEditedProposal(payload.changes.body?.from, payload.comment?.body);
1166411692
const assistantResponse = await openAI.promptAssistant(assistantID, prompt);
1166511693
const parsedAssistantResponse = openAI.parseAssistantResponse(assistantResponse);
11694+
core.startGroup('Parsed Assistant Response');
1166611695
console.log('parsedAssistantResponse: ', parsedAssistantResponse);
11696+
core.endGroup();
1166711697
// fallback to empty strings to avoid crashing in case parsing fails
1166811698
const { action = '', message = '' } = parsedAssistantResponse ?? {};
1166911699
const isNoAction = action.trim() === CONST_1.default.NO_ACTION;
@@ -12485,24 +12515,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1248512515
};
1248612516
Object.defineProperty(exports, "__esModule", ({ value: true }));
1248712517
const CONST_1 = __importDefault(__nccwpck_require__(29873));
12488-
class ProposalPoliceTemplates {
12489-
static getPromptForNewProposalTemplateCheck(commentBody) {
12518+
const PROPOSAL_POLICE_TEMPLATES = {
12519+
getPromptForNewProposalTemplateCheck: (commentBody) => {
1249012520
return `I NEED HELP WITH CASE (1.), CHECK IF COMMENT IS PROPOSAL AND IF TEMPLATE IS FOLLOWED AS PER INSTRUCTIONS. IT IS MANDATORY THAT YOU RESPOND ONLY WITH "${CONST_1.default.NO_ACTION}" IN CASE THE COMMENT IS NOT A PROPOSAL. Comment content: ${commentBody}`;
12491-
}
12492-
static getPromptForNewProposalDuplicateCheck(existingProposal, newProposalBody) {
12521+
},
12522+
getPromptForNewProposalDuplicateCheck: (newProposalBody, existingProposal) => {
1249312523
return `I NEED HELP WITH CASE (3.) [INSTRUCTIONS SECTION: IX. DUPLICATE PROPOSAL DETECTION], COMPARE THE FOLLOWING TWO PROPOSALS AND RETURN A SIMILARITY PERCENTAGE (0-100) REPRESENTING HOW SIMILAR THESE TWO PROPOSALS ARE IN THOSE SECTIONS AS PER THE INSTRUCTIONS. \n\nProposal 1:\n${existingProposal}\n\nProposal 2:\n${newProposalBody}`;
12494-
}
12495-
static getPromptForEditedProposal(previousBody, editedBody) {
12524+
},
12525+
getPromptForEditedProposal: (previousBody, editedBody) => {
1249612526
return `I NEED HELP WITH CASE (2.) WHEN A USER THAT POSTED AN INITIAL PROPOSAL OR COMMENT (UNEDITED) THEN EDITS THE COMMENT - WE NEED TO CLASSIFY THE COMMENT BASED IN THE GIVEN INSTRUCTIONS AND IF TEMPLATE IS FOLLOWED AS PER INSTRUCTIONS. IT IS MANDATORY THAT YOU RESPOND ONLY WITH "${CONST_1.default.NO_ACTION}" IN CASE THE COMMENT IS NOT A PROPOSAL. \n\nPrevious comment content: ${previousBody}.\n\nEdited comment content: ${editedBody}`;
12497-
}
12498-
static getDuplicateCheckWithdrawMessage() {
12527+
},
12528+
getDuplicateCheckWithdrawMessage: () => {
1249912529
return '#### 🚫 Duplicated proposal withdrawn by 🤖 ProposalPolice.';
12500-
}
12501-
static getDuplicateCheckNoticeMessage(proposalAuthor) {
12530+
},
12531+
getDuplicateCheckNoticeMessage: (proposalAuthor) => {
1250212532
return `⚠️ @${proposalAuthor} Your proposal is a duplicate of an already existing proposal and has been automatically withdrawn to prevent spam. Please review the existing proposals before submitting a new one.`;
12503-
}
12504-
}
12505-
exports["default"] = ProposalPoliceTemplates;
12533+
},
12534+
};
12535+
exports["default"] = PROPOSAL_POLICE_TEMPLATES;
1250612536

1250712537

1250812538
/***/ }),

.github/actions/javascript/proposalPoliceComment/proposalPoliceComment.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import {getInput, setFailed} from '@actions/core';
2+
import * as core from '@actions/core';
23
import {context} from '@actions/github';
34
import type {IssueCommentCreatedEvent, IssueCommentEditedEvent, IssueCommentEvent} from '@octokit/webhooks-types';
45
import {format} from 'date-fns';
56
import {toZonedTime} from 'date-fns-tz';
67
import {convertToNumber} from '@github/libs/ActionUtils';
78
import CONST from '@github/libs/CONST';
89
import GithubUtils from '@github/libs/GithubUtils';
9-
import ProposalPoliceTemplates from '@prompts/proposalPolice';
10+
import PROPOSAL_POLICE_TEMPLATES from '@prompts/proposalPolice';
1011
import OpenAIUtils from '@scripts/utils/OpenAIUtils';
11-
import type {AssistantResponse, DuplicateProposalResponse} from '@scripts/utils/OpenAIUtils';
12+
13+
type AssistantResponse = {
14+
action: typeof CONST.NO_ACTION | typeof CONST.ACTION_REQUIRED | typeof CONST.ACTION_EDIT;
15+
message: string;
16+
};
17+
18+
type DuplicateProposalResponse = AssistantResponse & {
19+
similarity?: number;
20+
};
1221

1322
function isCommentCreatedEvent(payload: IssueCommentEvent): payload is IssueCommentCreatedEvent {
1423
return payload.action === CONST.ACTIONS.CREATED;
@@ -77,7 +86,9 @@ async function run() {
7786
// Fetch all comments in the issue
7887
console.log('Get comments for issue #', issueNumber);
7988
const commentsResponse = await GithubUtils.getAllCommentDetails(issueNumber);
89+
core.startGroup('Comments Response');
8090
console.log('commentsResponse', commentsResponse);
91+
core.endGroup();
8192

8293
let didFindDuplicate = false;
8394
for (const previousProposal of commentsResponse) {
@@ -93,11 +104,13 @@ async function run() {
93104
continue;
94105
}
95106

96-
const duplicateCheckPrompt = ProposalPoliceTemplates.getPromptForNewProposalDuplicateCheck(previousProposal.body, newProposalBody);
107+
const duplicateCheckPrompt = PROPOSAL_POLICE_TEMPLATES.getPromptForNewProposalDuplicateCheck(previousProposal.body, newProposalBody);
97108
const duplicateCheckResponse = await openAI.promptAssistant(assistantID, duplicateCheckPrompt);
98109
let similarityPercentage = 0;
99110
const parsedDuplicateCheckResponse = openAI.parseAssistantResponse<DuplicateProposalResponse>(duplicateCheckResponse);
111+
core.startGroup('Parsed Duplicate Check Response');
100112
console.log('parsedDuplicateCheckResponse: ', parsedDuplicateCheckResponse);
113+
core.endGroup();
101114
if (parsedDuplicateCheckResponse) {
102115
const {similarity = 0} = parsedDuplicateCheckResponse ?? {};
103116
similarityPercentage = convertToNumber(similarity);
@@ -110,8 +123,8 @@ async function run() {
110123
}
111124

112125
if (didFindDuplicate) {
113-
const duplicateCheckWithdrawMessage = ProposalPoliceTemplates.getDuplicateCheckWithdrawMessage();
114-
const duplicateCheckNoticeMessage = ProposalPoliceTemplates.getDuplicateCheckNoticeMessage(newProposalAuthor);
126+
const duplicateCheckWithdrawMessage = PROPOSAL_POLICE_TEMPLATES.getDuplicateCheckWithdrawMessage();
127+
const duplicateCheckNoticeMessage = PROPOSAL_POLICE_TEMPLATES.getDuplicateCheckNoticeMessage(newProposalAuthor);
115128
// If a duplicate proposal is detected, update the comment to withdraw it
116129
console.log('ProposalPolice™ withdrawing duplicated proposal...');
117130
await GithubUtils.octokit.issues.updateComment({
@@ -129,12 +142,14 @@ async function run() {
129142
}
130143

131144
const prompt = isCommentCreatedEvent(payload)
132-
? ProposalPoliceTemplates.getPromptForNewProposalTemplateCheck(payload.comment?.body)
133-
: ProposalPoliceTemplates.getPromptForEditedProposal(payload.changes.body?.from, payload.comment?.body);
145+
? PROPOSAL_POLICE_TEMPLATES.getPromptForNewProposalTemplateCheck(payload.comment?.body)
146+
: PROPOSAL_POLICE_TEMPLATES.getPromptForEditedProposal(payload.changes.body?.from, payload.comment?.body);
134147

135148
const assistantResponse = await openAI.promptAssistant(assistantID, prompt);
136149
const parsedAssistantResponse = openAI.parseAssistantResponse<AssistantResponse>(assistantResponse);
150+
core.startGroup('Parsed Assistant Response');
137151
console.log('parsedAssistantResponse: ', parsedAssistantResponse);
152+
core.endGroup();
138153

139154
// fallback to empty strings to avoid crashing in case parsing fails
140155
const {action = '', message = ''} = parsedAssistantResponse ?? {};
@@ -175,3 +190,5 @@ run().catch((error) => {
175190
// which means that no failure notification is sent to issue's subscribers
176191
process.exit(0);
177192
});
193+
194+
export type {AssistantResponse, DuplicateProposalResponse};

prompts/proposalPolice.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import CONST from '@github/libs/CONST';
22

3-
export default class ProposalPoliceTemplates {
4-
static getPromptForNewProposalTemplateCheck(commentBody?: string): string {
3+
const PROPOSAL_POLICE_TEMPLATES = {
4+
getPromptForNewProposalTemplateCheck: (commentBody: string): string => {
55
return `I NEED HELP WITH CASE (1.), CHECK IF COMMENT IS PROPOSAL AND IF TEMPLATE IS FOLLOWED AS PER INSTRUCTIONS. IT IS MANDATORY THAT YOU RESPOND ONLY WITH "${CONST.NO_ACTION}" IN CASE THE COMMENT IS NOT A PROPOSAL. Comment content: ${commentBody}`;
6-
}
7-
8-
static getPromptForNewProposalDuplicateCheck(existingProposal?: string, newProposalBody?: string): string {
6+
},
7+
getPromptForNewProposalDuplicateCheck: (newProposalBody: string | undefined, existingProposal: string): string => {
98
return `I NEED HELP WITH CASE (3.) [INSTRUCTIONS SECTION: IX. DUPLICATE PROPOSAL DETECTION], COMPARE THE FOLLOWING TWO PROPOSALS AND RETURN A SIMILARITY PERCENTAGE (0-100) REPRESENTING HOW SIMILAR THESE TWO PROPOSALS ARE IN THOSE SECTIONS AS PER THE INSTRUCTIONS. \n\nProposal 1:\n${existingProposal}\n\nProposal 2:\n${newProposalBody}`;
10-
}
11-
12-
static getPromptForEditedProposal(previousBody?: string, editedBody?: string): string {
9+
},
10+
getPromptForEditedProposal: (previousBody: string | undefined, editedBody: string): string => {
1311
return `I NEED HELP WITH CASE (2.) WHEN A USER THAT POSTED AN INITIAL PROPOSAL OR COMMENT (UNEDITED) THEN EDITS THE COMMENT - WE NEED TO CLASSIFY THE COMMENT BASED IN THE GIVEN INSTRUCTIONS AND IF TEMPLATE IS FOLLOWED AS PER INSTRUCTIONS. IT IS MANDATORY THAT YOU RESPOND ONLY WITH "${CONST.NO_ACTION}" IN CASE THE COMMENT IS NOT A PROPOSAL. \n\nPrevious comment content: ${previousBody}.\n\nEdited comment content: ${editedBody}`;
14-
}
15-
16-
static getDuplicateCheckWithdrawMessage(): string {
12+
},
13+
getDuplicateCheckWithdrawMessage: (): string => {
1714
return '#### 🚫 Duplicated proposal withdrawn by 🤖 ProposalPolice.';
18-
}
19-
20-
static getDuplicateCheckNoticeMessage(proposalAuthor: string): string {
15+
},
16+
getDuplicateCheckNoticeMessage: (proposalAuthor: string | undefined): string => {
2117
return `⚠️ @${proposalAuthor} Your proposal is a duplicate of an already existing proposal and has been automatically withdrawn to prevent spam. Please review the existing proposals before submitting a new one.`;
22-
}
23-
}
18+
},
19+
};
20+
21+
export default PROPOSAL_POLICE_TEMPLATES;

scripts/utils/OpenAIUtils.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import OpenAI from 'openai';
22
import type {ChatCompletionMessageParam, ChatModel} from 'openai/resources';
33
import type {MessageContent, TextContentBlock} from 'openai/resources/beta/threads';
4-
import type CONST from '@github/libs/CONST';
4+
import type {AssistantResponse} from '@github/actions/javascript/proposalPoliceComment/proposalPoliceComment';
55
import sanitizeJSONStringValues from '@github/libs/sanitizeJSONStringValues';
66
import retryWithBackoff from '@scripts/utils/retryWithBackoff';
77

8-
type AssistantResponse = {
9-
action: typeof CONST.NO_ACTION | typeof CONST.ACTION_REQUIRED | typeof CONST.ACTION_EDIT;
10-
message: string;
11-
};
12-
13-
type DuplicateProposalResponse = AssistantResponse & {
14-
similarity?: number;
15-
};
16-
178
class OpenAIUtils {
189
/**
1910
* How frequently to poll a thread to wait for it to be done.
@@ -190,4 +181,3 @@ class OpenAIUtils {
190181
}
191182

192183
export default OpenAIUtils;
193-
export type {AssistantResponse, DuplicateProposalResponse};

0 commit comments

Comments
 (0)