Skip to content

Commit

Permalink
feat: add new code review mode (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellebore authored Aug 2, 2023
1 parent 90f6003 commit 062bf1d
Show file tree
Hide file tree
Showing 6 changed files with 683 additions and 28 deletions.
21 changes: 21 additions & 0 deletions media/chat.css
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,24 @@ button.sidebar__chat-assistant--chat-bubble-text--code-action-button:hover {
.sidebar__textarea-send-button--disabled svg {
opacity: 0.2;
}

.sidebar__branch-form {
display: grid;
grid-template-columns: min-content auto;
align-items: center;
}

.sidebar__branch-form label {
margin-right: 10px;
font-weight: bold;
}

.columnOne {
grid-column: 1/2;
grid-row: auto;
}

.columnTwo {
grid-column: 2/2;
grid-row: auto;
}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@
"contextualTitle": "Recipes",
"when": "sourcery.features.coding_assistant"
},
{
"id": "sourcery.code_review",
"name": "Code Review",
"type": "webview",
"icon": "sourcery-icon.png",
"contextualTitle": "Code Review",
"when": "sourcery.features.coding_assistant && sourcery.features.code_review"
},
{
"id": "sourcery.rules",
"name": "Rules",
Expand Down Expand Up @@ -229,6 +237,11 @@
"title": "Clear",
"category": "Sourcery"
},
{
"command": "sourcery.chat.clearCodeReview",
"title": "Clear",
"category": "Sourcery"
},
{
"command": "sourcery.chat.ask",
"title": "Ask Sourcery",
Expand Down Expand Up @@ -286,6 +299,11 @@
"command": "sourcery.chat.clearChat",
"when": "view == sourcery.chat",
"group": "navigation"
},
{
"command": "sourcery.chat.clearCodeReview",
"when": "view == sourcery.code_review",
"group": "navigation"
}
],
"view/item/context": [
Expand Down Expand Up @@ -360,6 +378,10 @@
"command": "sourcery.chat.clearChat",
"when": "false"
},
{
"command": "sourcery.chat.clearCodeReview",
"when": "false"
},
{
"command": "sourcery.chat.ask",
"when": "sourcery.features.coding_assistant"
Expand Down
59 changes: 34 additions & 25 deletions src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ marked.use(
})
);

enum ChatResultOutcome {
export enum ChatResultOutcome {
Success = "success",
Error = "error",
Finished = "finished",
}

enum ChatResultRole {
export enum ChatResultRole {
Assistant = "assistant",
User = "user",
}

type ChatResult = {
export type ChatResult = {
outcome: ChatResultOutcome;
textContent: string;
role: ChatResultRole;
Expand All @@ -38,15 +38,21 @@ export type ChatRequestData = {
message: string;
};

export type CodeReviewRequestData = {
kind: "review_request";
main: string;
current: string;
};

export type RecipeRequestData = {
kind: "recipe_request";
name: string;
id: string;
};

export type ChatRequest = {
type: "recipe_request" | "chat_request";
data: ChatRequestData | RecipeRequestData;
type: "recipe_request" | "chat_request" | "review_request";
data: ChatRequestData | RecipeRequestData | CodeReviewRequestData;
context_range?: any;
};

Expand All @@ -60,7 +66,7 @@ export type OpenLinkRequest = {
link: string;
};

type InsertAtCursorInstruction = {
export type InsertAtCursorInstruction = {
type: "insert_at_cursor";
content: string;
};
Expand Down Expand Up @@ -214,7 +220,7 @@ export class ChatProvider implements vscode.WebviewViewProvider {
this._currentAssistantMessage = result.textContent;
}

let sanitized = this.renderAssistantMessage(this._currentAssistantMessage);
let sanitized = renderAssistantMessage(this._currentAssistantMessage);

this._view.webview.postMessage({
command: "add_result",
Expand All @@ -226,23 +232,6 @@ export class ChatProvider implements vscode.WebviewViewProvider {
});
}

private renderAssistantMessage(message: string) {
// Send the whole message we've been streamed so far to the webview,
// after converting from markdown to html

const rendered = marked(message, {
gfm: true,
breaks: true,
mangle: false,
headerIds: false,
});

// Allow any classes on span and code blocks or highlightjs classes get removed
return sanitizeHtml(rendered, {
allowedClasses: { span: false, code: false },
});
}

public clearChat() {
this._view.webview.postMessage({ command: "clear_chat" });
this._currentAssistantMessage = "";
Expand Down Expand Up @@ -316,7 +305,6 @@ export class ChatProvider implements vscode.WebviewViewProvider {
<section id="message-container" class="sidebar__section-container active" data-section="chat-assistant">
<ul class="sidebar__chat-assistant--dialogue-container">
<li id="anchor"></li>
</ul>
</section>
<footer class="sidebar__chat-assistant--footer">
Expand Down Expand Up @@ -344,3 +332,24 @@ export class ChatProvider implements vscode.WebviewViewProvider {
</html>`;
}
}

export function renderAssistantMessage(message: string) {
// Send the whole message we've been streamed so far to the webview,
// after converting from markdown to html

const rendered = marked(message, {
gfm: true,
breaks: true,
mangle: false,
headerIds: false,
});

// Allow any classes on span and code blocks or highlightjs classes get removed
return sanitizeHtml(rendered, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([
"details",
"summary",
]),
allowedClasses: { span: false, code: false },
});
}
Loading

0 comments on commit 062bf1d

Please sign in to comment.