Skip to content

Commit

Permalink
achieved mvp of the settings pannel
Browse files Browse the repository at this point in the history
  • Loading branch information
itishermann committed Jun 22, 2024
1 parent 8c40151 commit 8a3a919
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ class OllamaSettingsComponent {
updateModelNames()
}
modelNameComboBox.isVisible = false
modelNameComboBox.addItemListener { event ->
if (event.stateChange == java.awt.event.ItemEvent.SELECTED) {
val selectedItem = event.item.toString()
println("Selected item: $selectedItem")
modelName = selectedItem
}
}
refreshModelsButton.isEnabled = false
promptText.margin = JBUI.insets(5)
val promptPanel: JPanel = UI.PanelFactory.panel(promptText).withComment("<p>Plugin is based on Ollama API. Please download" +
" <a href=\"https://ollama.ai/\">Ollama</a> firstly.</p>" +
"<br/>" +
"<p> Parameters can be used in prompt: </p> " +
"<p> {{fileChangeCount}}: number of changed files</p>" +
"<p> {{gitDiff}}: changed code by git unified view</p>")
val promptPanel: JPanel = UI.PanelFactory.panel(promptText).withComment("<p>Plugin is based on <a href=\"https://ollama.ai/\">Ollama</a>")
.createPanel()
panel = FormBuilder.createFormBuilder()
.addLabeledComponent(JBLabel("Server URL:"), serverUrlText, 1, false)
Expand All @@ -58,31 +60,36 @@ class OllamaSettingsComponent {
.panel
}

fun checkApiReachability() {
private fun checkApiReachability() {
loadingLabel.text = "Checking API..."
if(serverUrl.isNullOrEmpty()) {
reachabilityStatusLabel.text = "API is not reachable, server URL is empty"
loadingLabel.text = ""
return
}
SwingUtilities.invokeLater {
val ollamaAPI = OllamaAPI(serverUrl)
if(!userName.isNullOrEmpty() && !password.isNullOrEmpty()) {
ollamaAPI.setBasicAuth(userName!!, password!!)
}
// Comment before deploying
ollamaAPI.setVerbose(true)
OllamaClientManager.setOllamaClient(ollamaAPI)
val isOllamaServerReachable = ollamaAPI.ping()
if (isOllamaServerReachable) {
reachabilityStatusLabel.text = "API is reachable"
refreshModelsButton.isEnabled = true
updateModelNames()
} else {
try {
val ollamaAPI = OllamaAPI(serverUrl)
if(!userName.isNullOrEmpty() && !password.isNullOrEmpty()) {
ollamaAPI.setBasicAuth(userName!!, password!!)
}
// Comment before deploying
ollamaAPI.setVerbose(true)
OllamaClientManager.setOllamaClient(ollamaAPI)
val isOllamaServerReachable = ollamaAPI.ping()
if (isOllamaServerReachable) {
reachabilityStatusLabel.text = "API is reachable"
refreshModelsButton.isEnabled = true
updateModelNames()
} else {
reachabilityStatusLabel.text = "API is not reachable"
refreshModelsButton.isEnabled = false
}
loadingLabel.text = ""
} catch (e: Exception){
loadingLabel.text = "An error occurred: ${e.message}"
reachabilityStatusLabel.text = "API is not reachable"
refreshModelsButton.isEnabled = false
}
loadingLabel.text = ""
}
}

Expand All @@ -109,12 +116,14 @@ class OllamaSettingsComponent {
// Set the first model as selected
if(modelNames.isNotEmpty()) {
modelNameComboBox.isVisible = true
modelName = modelNames[0]
if(modelName.isNullOrEmpty()){
modelName = modelNames[0]
}
} else {
modelNameComboBox.isVisible = false
loadingModelsLabel.text = "No models found"
}
loadingModelsLabel.text = "Models list successfully"
loadingModelsLabel.text = "Models fetched successfully"
}

val preferredFocusedComponent: JComponent
Expand Down Expand Up @@ -145,7 +154,7 @@ class OllamaSettingsComponent {
var modelName: String?
get() = modelNameComboBox.item as String? ?: ""

Check warning on line 155 in src/main/kotlin/me/itishermann/ollamacommitsummarizer/settings/OllamaSettingsComponent.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

No cast needed
set(newText) {
modelNameComboBox.item = newText
modelNameComboBox.selectedItem = newText
}

@get:NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ internal class OllamaSettingsConfigurable: Configurable {
}

override fun isModified(): Boolean {
val state: OllamaSettingsState.State =
Objects.requireNonNull(OllamaSettingsState.instance.state)
return !ollamaSettingsComponent.serverUrl.equals(state.serverUrl) ||
!ollamaSettingsComponent.userName.equals(state.userName) ||
!ollamaSettingsComponent.password.equals(state.password) ||
!ollamaSettingsComponent.modelName.equals(state.modelName) ||
!ollamaSettingsComponent.prompt.equals(state.prompt)
}
val state: OllamaSettingsState.State =
Objects.requireNonNull(OllamaSettingsState.instance.state)
return !ollamaSettingsComponent.serverUrl.equals(state.serverUrl) ||
!ollamaSettingsComponent.userName.equals(state.userName) ||
!ollamaSettingsComponent.password.equals(state.password) ||
!ollamaSettingsComponent.modelName.equals(state.modelName) ||
!ollamaSettingsComponent.prompt.equals(state.prompt)
}

override fun apply() {
val state: OllamaSettingsState.State =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class OllamaSettingsState
var serverUrl: @NotNull @NonNls String? = "http://localhost:11434"
var userName: @Nullable @NonNls String? = null
var password: @Nullable @NonNls String? = null
var modelName: @NotNull @NonNls String? = "codegemma:2b"
var modelName: @NotNull @NonNls String? = null
var prompt: @NotNull @NonNls String? = """
It is the code changes gives by unified view, changed file number is {{fileChangeCount}}:
{{gitDiff}}
Expand Down

0 comments on commit 8a3a919

Please sign in to comment.