Skip to content

feat: add support for generating commit message #5828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions core/commands/slash/commit.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
import { SlashCommand } from "../../index.js";
import { IDE, ILLM, SlashCommand } from "../../index.js";
import { renderChatMessage } from "../../util/messageContent.js";

interface GenerateCommitMessageParams {
ide: IDE;
llm: ILLM;
includeUnstaged?: boolean;
diff?: string[];
signal?: AbortSignal;
}

export async function* generateCommitMessage({
ide,
llm,
includeUnstaged,
diff,
signal,
}: GenerateCommitMessageParams): AsyncGenerator<string> {
if (!diff) {
diff = await ide.getDiff(includeUnstaged ?? false);
}

if (diff.length === 0) {
yield "No changes detected. Make sure you are in a git repository with current changes.";
return;
}

const prompt = `${diff.join("\n")}\n\nGenerate a commit message for the above set of changes. First, give a single sentence, no more than 80 characters. Then, after 2 line breaks, give a list of no more than 5 short bullet points, each no more than 40 characters. Output nothing except for the commit message, and don't surround it in quotes.`;
for await (const chunk of llm.streamChat(
[{ role: "user", content: prompt }],
signal ?? new AbortController().signal,
)) {
yield renderChatMessage(chunk);
}
}

const CommitMessageCommand: SlashCommand = {
name: "commit",
description: "Generate a commit message for current changes",
run: async function* ({ ide, llm, params }) {
const includeUnstaged = params?.includeUnstaged ?? false;
const diff = await ide.getDiff(includeUnstaged);

if (diff.length === 0) {
yield "No changes detected. Make sure you are in a git repository with current changes.";
return;
}

const prompt = `${diff.join("\n")}\n\nGenerate a commit message for the above set of changes. First, give a single sentence, no more than 80 characters. Then, after 2 line breaks, give a list of no more than 5 short bullet points, each no more than 40 characters. Output nothing except for the commit message, and don't surround it in quotes.`;
for await (const chunk of llm.streamChat(
[{ role: "user", content: prompt }],
new AbortController().signal,
)) {
yield renderChatMessage(chunk);
}
},
run: ({ ide, llm, params }) =>
generateCommitMessage({
ide,
llm,
includeUnstaged:
typeof params?.includeUnstaged === "boolean"
? params.includeUnstaged
: undefined,
}),
};

export default CommitMessageCommand;
46 changes: 46 additions & 0 deletions core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
} from ".";

import { ConfigYaml } from "@continuedev/config-yaml";
import { generateCommitMessage } from "./commands/slash/commit";
import { isLocalAssistantFile } from "./config/loadLocalAssistants";
import {
setupBestConfig,
Expand Down Expand Up @@ -750,6 +751,51 @@ export class Core {
return isBackgrounded; // Return true to indicate the message was handled successfully
},
);

on("generateCommitMessage", (msg) => {
return this.generateCommitMessageStream(msg);
});
}

private async *generateCommitMessageStream(
msg: Message<ToCoreProtocol["generateCommitMessage"][0]>,
): AsyncGenerator<string> {
const { config } = await this.configHandler.loadConfig();
if (!config) {
throw new Error("Failed to load config");
}

const { modelTitle, diff } = msg.data;

const llm =
config.modelsByRole.chat.find((m) => m.title === modelTitle) ??
config.selectedModelByRole.chat;

if (!llm) {
throw new Error("No model selected");
}

const abortController = this.addMessageAbortController(msg.messageId);

try {
for await (const content of generateCommitMessage({
ide: this.ide,
llm,
diff,
signal: abortController.signal,
})) {
yield content;
}
} catch (e) {
// Ignore this error as it may occur during manual reruns
if (e instanceof Error && !e.message.includes("operation was aborted")) {
console.log("Failed to generate commit message", e);
throw e;
}
} finally {
// Always clean up the abortController on success/failure.
this.abortById(msg.messageId);
}
}

private async isItemTooBig(item: ContextItemWithId) {
Expand Down
8 changes: 8 additions & 0 deletions core/protocol/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,12 @@ export type ToCoreFromIdeOrWebviewProtocol = {
];
"process/markAsBackgrounded": [{ toolCallId: string }, void];
"process/isBackgrounded": [{ toolCallId: string }, boolean];

"generateCommitMessage": [
{
modelTitle?: string;
diff?: string[];
},
AsyncGenerator<string>,
];
};
2 changes: 1 addition & 1 deletion extensions/intellij/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ platformVersion=2022.3.3
#platformVersion = LATEST-EAP-SNAPSHOT
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
platformPlugins=org.jetbrains.plugins.terminal
platformPlugins=org.jetbrains.plugins.terminal,Git4Idea
# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion=8.3
# Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.continuedev.continueintellijextension

import com.intellij.openapi.util.IconLoader
import com.intellij.ui.AnimatedIcon

/**
* @author lk
*/
object ContinueIcons {

val CONTINUE = icon("/icons/continue.svg")

val CLOSE = icon("/icons/close.svg")

val SPINNING = AnimatedIcon.Default()

private fun icon(path: String) = IconLoader.getIcon(path, ContinueIcons::class.java)
}
Loading
Loading