Add a new slash command to review code. (#1071)
* Add a new slash command to review code. * clean code
This commit is contained in:
parent
f9a1a78575
commit
34953757b2
3 changed files with 61 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ import EditSlashCommand from "./edit";
|
|||
import HttpSlashCommand from "./http";
|
||||
import ShareSlashCommand from "./share";
|
||||
import StackOverflowSlashCommand from "./stackOverflow";
|
||||
import ReviewMessageCommand from "./review";
|
||||
|
||||
export default [
|
||||
DraftIssueCommand,
|
||||
|
|
@ -16,4 +17,5 @@ export default [
|
|||
CommentSlashCommand,
|
||||
HttpSlashCommand,
|
||||
CommitMessageCommand,
|
||||
ReviewMessageCommand,
|
||||
];
|
||||
|
|
|
|||
55
core/commands/slash/review.ts
Normal file
55
core/commands/slash/review.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { SlashCommand } from "../..";
|
||||
import { stripImages } from "../../llm/countTokens";
|
||||
import { ChatMessage } from "../..";
|
||||
|
||||
const prompt = `
|
||||
Review the following code, focusing on Readability, Maintainability, Code Smells, Speed, and Memory Performance. Provide feedback with these guidelines:
|
||||
|
||||
Tone: Friendly casual tone of a fellow engineer, ensure the feedback is clear and focused on practical improvements.
|
||||
Orderly Analysis: Address the code sequentially, from top to bottom, to ensure a thorough review without skipping any parts.
|
||||
Descriptive Feedback: Avoid referencing line numbers directly, as they may vary. Instead, describe the code sections or specific constructs that need attention, explaining the reasons clearly.
|
||||
Provide Examples: For each issue identified, offer an example of how the code could be improved or rewritten for better clarity, performance, or maintainability.
|
||||
Your response should be structured to first identify the issue, then explain why it’s a problem, and finally, offer a solution with example code.`;
|
||||
|
||||
|
||||
function getLastUserHistory(history: ChatMessage[]): string {
|
||||
const lastUserHistory = history
|
||||
.reverse()
|
||||
.find((message) => message.role === "user");
|
||||
|
||||
if (!lastUserHistory) return "";
|
||||
|
||||
if (lastUserHistory.content instanceof Array) {
|
||||
return lastUserHistory.content.reduce(
|
||||
(acc: string, current: { type: string; text?: string }) => {
|
||||
return current.type === "text" && current.text
|
||||
? acc + current.text
|
||||
: acc;
|
||||
},
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
return typeof lastUserHistory.content === "string"
|
||||
? lastUserHistory.content
|
||||
: "";
|
||||
}
|
||||
|
||||
const ReviewMessageCommand: SlashCommand = {
|
||||
name: "review",
|
||||
description: "Review code and give feedback",
|
||||
run: async function* ({ llm, history }) {
|
||||
|
||||
let reviewText = getLastUserHistory(history).replace("\\review", "");
|
||||
|
||||
const content = `${prompt} \r\n ${reviewText}`;
|
||||
|
||||
for await (const chunk of llm.streamChat([
|
||||
{ role: "user", content: content },
|
||||
])) {
|
||||
yield stripImages(chunk.content);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default ReviewMessageCommand;
|
||||
|
|
@ -980,7 +980,8 @@
|
|||
"edit",
|
||||
"comment",
|
||||
"http",
|
||||
"commit"
|
||||
"commit",
|
||||
"review"
|
||||
],
|
||||
"type": "string",
|
||||
"markdownEnumDescriptions": [
|
||||
|
|
@ -991,7 +992,8 @@
|
|||
"Edit the highlighted code with given instructions",
|
||||
"Add comments to the highlighted code",
|
||||
"Write a custom slash command at your own HTTP endpoint. Set 'url' in the params object for the endpoint you have setup.",
|
||||
"Generate a commit message for the current changes"
|
||||
"Generate a commit message for the current changes",
|
||||
"Review code and give feedback"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue