11package com.github.blarc.ai.commits.intellij.plugin.settings
22
3+ import com.aallam.openai.api.exception.OpenAIAPIException
34import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
5+ import com.github.blarc.ai.commits.intellij.plugin.OpenAIService
6+ import com.intellij.icons.AllIcons
47import com.intellij.openapi.options.BoundConfigurable
8+ import com.intellij.openapi.progress.runBackgroundableTask
9+ import com.intellij.ui.components.JBLabel
510import com.intellij.ui.dsl.builder.*
6- import java.awt.Component
11+ import kotlinx.coroutines.DelicateCoroutinesApi
12+ import kotlinx.coroutines.Dispatchers
13+ import kotlinx.coroutines.GlobalScope
14+ import kotlinx.coroutines.launch
715import java.util.*
8- import javax.swing.DefaultListCellRenderer
9- import javax.swing.JList
16+ import javax.swing.JPasswordField
1017
1118class AppSettingsConfigurable : BoundConfigurable (message("settings.general.group.title")) {
19+
20+ private val tokenPasswordField = JPasswordField ()
21+ private val verifyLabel = JBLabel ()
1222 override fun createPanel () = panel {
1323
1424 row {
15- passwordField( )
25+ cell(tokenPasswordField )
1626 .label(message(" settings.openAIToken" ))
1727 .bindText(
1828 { AppSettings .instance.getOpenAIToken().orEmpty() },
1929 { AppSettings .instance.saveOpenAIToken(it)}
2030 )
2131 .align(Align .FILL )
32+ .resizableColumn()
33+ .focused()
34+ button(message(" settings.verifyToken" )) {
35+ verifyToken()
36+ }.align(AlignX .RIGHT )
2237 }
2338 row {
2439 comment(message(" settings.openAITokenComment" ))
40+ .align(AlignX .LEFT )
41+ cell(verifyLabel)
42+ .align(AlignX .RIGHT )
2543 }
2644 row {
2745 comboBox(Locale .getAvailableLocales().toList(), AppSettingsListCellRenderer ())
2846 .label(message(" settings.locale" ))
2947 .bindItem(AppSettings .instance::locale.toNullableProperty())
3048 }
3149 }
50+
51+ @OptIn(DelicateCoroutinesApi ::class )
52+ private fun verifyToken () {
53+ runBackgroundableTask(message(" settings.verify.running" )) {
54+ if (tokenPasswordField.password.isEmpty()) {
55+ verifyLabel.icon = AllIcons .General .InspectionsError
56+ verifyLabel.text = message(" settings.verify.token-is-empty" )
57+ } else {
58+ verifyLabel.icon = AllIcons .General .InlineRefreshHover
59+ verifyLabel.text = message(" settings.verify.running" )
60+
61+ GlobalScope .launch(Dispatchers .IO ) {
62+ try {
63+ println (String (tokenPasswordField.password))
64+ OpenAIService .instance.verifyToken(String (tokenPasswordField.password))
65+ verifyLabel.text = message(" settings.verify.valid" )
66+ verifyLabel.icon = AllIcons .General .InspectionsOK
67+ }
68+ catch (e: OpenAIAPIException ) {
69+ verifyLabel.text = message(" settings.verify.invalid" , e.statusCode)
70+ verifyLabel.icon = AllIcons .General .InspectionsError
71+ }
72+ catch (e: Exception ) {
73+ verifyLabel.text = message(" settings.verify.invalid" , " Unknown" )
74+ verifyLabel.icon = AllIcons .General .InspectionsError
75+ }
76+ }
77+ }
78+ }
79+
80+ }
3281}
0 commit comments