Skip to content

Commit 1795ef4

Browse files
committed
feat: Open AI proxy setting (#21).
1 parent 46163f5 commit 1795ef4

File tree

5 files changed

+81
-57
lines changed

5 files changed

+81
-57
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## [Unreleased]
4+
### Added
5+
- Open AI proxy setting.
46

57
## [0.6.2] - 2023-04-11
68

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class OpenAIService {
1919

2020
@OptIn(BetaOpenAI::class)
2121
suspend fun generateCommitMessage(prompt: String, completions: Int): String {
22-
val openAIToken = AppSettings.instance.getOpenAIToken() ?: throw Exception("OpenAI Token is not set.")
23-
val openAI = OpenAI(openAIToken)
22+
val openAI = OpenAI(AppSettings.instance.getOpenAIConfig())
2423

2524
val chatCompletionRequest = ChatCompletionRequest(
2625
ModelId(model),

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.blarc.ai.commits.intellij.plugin.settings
22

3+
import com.aallam.openai.client.OpenAIConfig
4+
import com.aallam.openai.client.ProxyConfig
35
import com.github.blarc.ai.commits.intellij.plugin.notifications.Notification
46
import com.github.blarc.ai.commits.intellij.plugin.notifications.sendNotification
57
import com.github.blarc.ai.commits.intellij.plugin.settings.prompt.Prompt
@@ -28,6 +30,7 @@ class AppSettings : PersistentStateComponent<AppSettings> {
2830
var locale: Locale = Locale.ENGLISH
2931
var requestSupport = true
3032
var lastVersion: String? = null
33+
var proxyUrl: String? = null
3134

3235
var prompts: MutableMap<String, Prompt> = initPrompts()
3336
var currentPrompt: Prompt = prompts["basic"]!!
@@ -57,6 +60,11 @@ class AppSettings : PersistentStateComponent<AppSettings> {
5760
}
5861
}
5962

63+
fun getOpenAIConfig(): OpenAIConfig {
64+
val token = getOpenAIToken() ?: throw Exception("OpenAI Token is not set.")
65+
return OpenAIConfig(token, proxy = proxyUrl?.takeIf { it.isNotBlank() }?.let { ProxyConfig.Http(it) })
66+
}
67+
6068
fun getOpenAIToken(): String? {
6169
val credentialAttributes = getCredentialAttributes(openAITokenTitle)
6270
val credentials: Credentials = PasswordSafe.instance.get(credentialAttributes) ?: return null

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

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import com.intellij.ui.CommonActionsPanel
1616
import com.intellij.ui.ToolbarDecorator
1717
import com.intellij.ui.components.JBLabel
1818
import com.intellij.ui.dsl.builder.*
19+
import com.intellij.ui.util.minimumWidth
20+
import com.intellij.ui.util.preferredWidth
1921
import kotlinx.coroutines.DelicateCoroutinesApi
2022
import kotlinx.coroutines.Dispatchers
2123
import kotlinx.coroutines.GlobalScope
@@ -34,66 +36,78 @@ class AppSettingsConfigurable : BoundConfigurable(message("settings.general.grou
3436
private lateinit var promptComboBox: Cell<ComboBox<Prompt>>
3537
override fun createPanel() = panel {
3638

37-
row {
38-
cell(tokenPasswordField)
39-
.label(message("settings.openAIToken"))
40-
.bindText(
41-
{ AppSettings.instance.getOpenAIToken().orEmpty() },
42-
{ AppSettings.instance.saveOpenAIToken(it) }
43-
)
44-
.align(Align.FILL)
45-
.resizableColumn()
46-
.focused()
47-
button(message("settings.verifyToken")) {
48-
verifyToken()
49-
}.align(AlignX.RIGHT)
50-
}
51-
row {
52-
comment(message("settings.openAITokenComment"))
53-
.align(AlignX.LEFT)
54-
cell(verifyLabel)
55-
.align(AlignX.RIGHT)
56-
}
57-
row {
58-
comboBox(Locale.getAvailableLocales().toList().sortedBy { it.displayName }, AppSettingsListCellRenderer())
59-
.label(message("settings.locale"))
60-
.bindItem(AppSettings.instance::locale.toNullableProperty())
61-
}
62-
row {
63-
promptComboBox = comboBox(AppSettings.instance.prompts.values, AppSettingsListCellRenderer())
64-
.label(message("settings.prompt"))
65-
.bindItem(AppSettings.instance::currentPrompt.toNullableProperty())
39+
group(JBLabel("OpenAI")) {
40+
row {
41+
cell(tokenPasswordField)
42+
.label(message("settings.openAIToken"))
43+
.bindText(
44+
{ AppSettings.instance.getOpenAIToken().orEmpty() },
45+
{ AppSettings.instance.saveOpenAIToken(it) }
46+
)
47+
.align(Align.FILL)
48+
.resizableColumn()
49+
.focused()
50+
button(message("settings.verifyToken")) {
51+
verifyToken()
52+
}.align(AlignX.RIGHT)
53+
}
54+
row {
55+
comment(message("settings.openAITokenComment"))
56+
.align(AlignX.LEFT)
57+
cell(verifyLabel)
58+
.align(AlignX.RIGHT)
59+
}
60+
row {
61+
textField()
62+
.label(message("settings.openAIProxy"))
63+
.bindText(AppSettings.instance::proxyUrl.toNonNullableProperty(""))
64+
.resizableColumn()
65+
.applyToComponent { minimumWidth = 300 }
66+
}
6667
}
67-
row {
68-
toolbarDecorator = ToolbarDecorator.createDecorator(promptTable.table)
69-
.setAddAction {
70-
promptTable.addPrompt().let {
71-
promptComboBox.component.addItem(it)
68+
69+
group(JBLabel("Prompt")) {
70+
row {
71+
comboBox(Locale.getAvailableLocales().toList().sortedBy { it.displayName }, AppSettingsListCellRenderer())
72+
.label(message("settings.locale"))
73+
.bindItem(AppSettings.instance::locale.toNullableProperty())
74+
}
75+
row {
76+
promptComboBox = comboBox(AppSettings.instance.prompts.values, AppSettingsListCellRenderer())
77+
.label(message("settings.prompt"))
78+
.bindItem(AppSettings.instance::currentPrompt.toNullableProperty())
79+
}
80+
row {
81+
toolbarDecorator = ToolbarDecorator.createDecorator(promptTable.table)
82+
.setAddAction {
83+
promptTable.addPrompt().let {
84+
promptComboBox.component.addItem(it)
85+
}
7286
}
73-
}
74-
.setEditAction {
75-
promptTable.editPrompt()?.let {
76-
promptComboBox.component.removeItem(it.first)
77-
promptComboBox.component.addItem(it.second)
87+
.setEditAction {
88+
promptTable.editPrompt()?.let {
89+
promptComboBox.component.removeItem(it.first)
90+
promptComboBox.component.addItem(it.second)
91+
}
7892
}
79-
}
80-
.setEditActionUpdater {
81-
updateActionAvailability(CommonActionsPanel.Buttons.EDIT)
82-
true
83-
}
84-
.setRemoveAction {
85-
promptTable.removePrompt()?.let {
86-
promptComboBox.component.removeItem(it)
93+
.setEditActionUpdater {
94+
updateActionAvailability(CommonActionsPanel.Buttons.EDIT)
95+
true
8796
}
88-
}
89-
.setRemoveActionUpdater {
90-
updateActionAvailability(CommonActionsPanel.Buttons.REMOVE)
91-
true
92-
}
93-
.disableUpDownActions()
97+
.setRemoveAction {
98+
promptTable.removePrompt()?.let {
99+
promptComboBox.component.removeItem(it)
100+
}
101+
}
102+
.setRemoveActionUpdater {
103+
updateActionAvailability(CommonActionsPanel.Buttons.REMOVE)
104+
true
105+
}
106+
.disableUpDownActions()
94107

95-
cell(toolbarDecorator.createPanel())
96-
.align(Align.FILL)
108+
cell(toolbarDecorator.createPanel())
109+
.align(Align.FILL)
110+
}.resizableRow()
97111
}.resizableRow()
98112

99113
row {

src/main/resources/messages/MyBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ settings.prompt.edit.title=Edit Prompt
4545
settings.prompt.add.title=Add Prompt
4646
settings.prompt.description=Description
4747
validation.unique=Value already exists.
48+
settings.openAIProxy=OpenAI proxy url
4849

0 commit comments

Comments
 (0)