From 34953757b263eddebae6bbb75ec5f4f231aca287 Mon Sep 17 00:00:00 2001 From: Jason Jacobs Date: Thu, 4 Apr 2024 13:04:07 -0700 Subject: [PATCH] Add a new slash command to review code. (#1071) * Add a new slash command to review code. * clean code --- core/commands/slash/index.ts | 2 + core/commands/slash/review.ts | 55 ++++++++++++++++++++++++++++ extensions/vscode/config_schema.json | 6 ++- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 core/commands/slash/review.ts diff --git a/core/commands/slash/index.ts b/core/commands/slash/index.ts index 0c95060f5..fe51fac2e 100644 --- a/core/commands/slash/index.ts +++ b/core/commands/slash/index.ts @@ -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, ]; diff --git a/core/commands/slash/review.ts b/core/commands/slash/review.ts new file mode 100644 index 000000000..87ff75764 --- /dev/null +++ b/core/commands/slash/review.ts @@ -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; diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 2454df1f9..e3edf8e51 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -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" ] }, {