Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 9e36309

Browse files
committed
feat: Add support for hints in commit message generation
This commit adds support for hints in the commit message generation process. If the current commit message starts with a colon, it is treated as a hint and passed to the OpenAI API to generate a more accurate commit message. The hint is displayed in the notification to the user. Changes Made: - Added a new parameter to the `generateCommitMessage` function in `OpenAIService.kt` to accept a hint - Updated the `AICommitAction.kt` file to extract the hint from the commit message field and pass it to the `generateCommitMessage` function - Updated the `AppSettings.kt` file to update the default prompt message to include a placeholder for hints - Added a new notification message to display the prompt message with the hint if provided
1 parent 7d8b5ec commit 9e36309

5 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/AICommitAction.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.intellij.openapi.project.DumbAware
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.vcs.VcsDataKeys
1414
import com.intellij.openapi.vcs.changes.Change
15+
import com.intellij.openapi.vcs.ui.CommitMessage
1516
import com.intellij.vcs.commit.AbstractCommitWorkflowHandler
1617
import git4idea.repo.GitRepositoryManager
1718
import kotlinx.coroutines.DelicateCoroutinesApi
@@ -33,6 +34,16 @@ class AICommitAction : AnAction(), DumbAware {
3334
runBackgroundableTask(message("action.background"), project) {
3435
val diff = computeDiff(includedChanges, project)
3536

37+
val hintMessageField = VcsDataKeys.COMMIT_MESSAGE_CONTROL.getData(e.dataContext) as CommitMessage
38+
val currentCommitMessage = hintMessageField.text
39+
40+
// if currentCommitMessage starts with : then it is a hint
41+
val hint = if (currentCommitMessage.startsWith("!")) {
42+
currentCommitMessage.substring(1)
43+
} else {
44+
null
45+
}
46+
3647
if (diff.isBlank()) {
3748
sendNotification(Notification.emptyDiff())
3849
return@runBackgroundableTask
@@ -47,7 +58,7 @@ class AICommitAction : AnAction(), DumbAware {
4758
GlobalScope.launch(Dispatchers.Main) {
4859
commitMessageField.setCommitMessage("Generating commit message...")
4960
try {
50-
val generatedCommitMessage = openAIService.generateCommitMessage(diff, 1)
61+
val generatedCommitMessage = openAIService.generateCommitMessage(diff, hint, 1)
5162
commitMessageField.setCommitMessage(generatedCommitMessage)
5263
} catch (e: Exception) {
5364
commitMessageField.setCommitMessage("Error generating commit message")

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/OpenAIService.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package com.github.blarc.ai.commits.intellij.plugin
22

33
import com.aallam.openai.api.BetaOpenAI
44
import com.aallam.openai.api.chat.*
5-
import com.aallam.openai.api.completion.CompletionRequest
65
import com.aallam.openai.api.model.ModelId
76
import com.aallam.openai.client.OpenAI
7+
import com.github.blarc.ai.commits.intellij.plugin.notifications.Notification
8+
import com.github.blarc.ai.commits.intellij.plugin.notifications.sendNotification
89
import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings
910
import com.intellij.openapi.application.ApplicationManager
1011
import com.intellij.openapi.components.Service
@@ -18,12 +19,15 @@ class OpenAIService {
1819
}
1920

2021
@OptIn(BetaOpenAI::class)
21-
suspend fun generateCommitMessage(diff: String, completions: Int): String {
22+
suspend fun generateCommitMessage(diff: String, hint: String?, completions: Int): String {
2223
val openAiToken = AppSettings.instance.getOpenAIToken() ?: throw Exception("OpenAI Token is not set")
2324

2425
val openAI = OpenAI(openAiToken)
2526

2627
val prompt = AppSettings.instance.getPrompt().replace("{diffs}", diff)
28+
.replace("{hint}", hint ?: "No hint provided")
29+
30+
sendNotification(Notification.usedPrompt(prompt))
2731

2832
val chatCompletionRequest = ChatCompletionRequest(
2933
ModelId(model),

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/notifications/Notification.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ data class Notification(
4646
fun unsuccessfulRequest(message: String) = Notification(message = message("notifications.unsuccessful-request", message))
4747

4848
fun unableToSaveToken() = Notification(message = message("notifications.unable-to-save-token"))
49+
50+
fun usedPrompt(diff: String) = Notification (
51+
message = message("notifications.uses-prompt", diff)
52+
)
4953
}
5054

5155
fun isTransient() = type == Type.TRANSIENT

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/AppSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AppSettings : PersistentStateComponent<AppSettings> {
2222
private val openAITokenTitle = "OpenAIToken"
2323
private val openAIPromptTitle = "OpenAIPrompt"
2424

25-
private var defaultPrompt = "As a professional developer working with a Git repository, it's important to maintain clear and concise commit messages that follow the standard format. Please create a commit message for the following changes: {diffs}. Remember to label everything logically and keep it short. Please use the following format <type>(<scope>): <short> \n\n [...]'. Thank you very much!" // And here's a helpful hint to remember: {hint}.
25+
private var defaultPrompt = "As a professional developer working with a Git repository, it's important to maintain clear and concise commit messages that follow the standard format. Please create a commit message for the following changes: {diffs}. Remember to label everything logically and keep it short. Please use the following format <type>(<scope>): <short> \n\n [...]'. And here's a helpful hint from another developer: {hint}. Maybe you can use it to help you write your commit message."
2626

2727
var requestSupport = true
2828

src/main/resources/messages/MyBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ actions.do-not-ask-again = Do not ask me again.
3030
actions.take-me-there = Take me there.
3131
actions.sure-take-me-there=Sure, take me there.
3232
settings.promptValidation=Need to include: {diffs}
33+
notifications.uses-prompt=Prompt:\n{0}
3334

0 commit comments

Comments
 (0)