Skip to content

Commit

Permalink
feat: try to add validator
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 3, 2023
1 parent 05dd33f commit 459b711
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Expand Up @@ -2,6 +2,9 @@ package cc.unitmesh.devti.gui.chat

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.AutoDevIcons
import cc.unitmesh.devti.llms.tokenizer.LLM_MAX_TOKEN
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.Presentation
Expand All @@ -11,6 +14,8 @@ import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComponentValidator
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.openapi.wm.impl.InternalDecorator
import com.intellij.ui.content.ContentManager
Expand All @@ -21,8 +26,10 @@ import com.intellij.util.ui.components.BorderLayoutPanel
import java.awt.Color
import java.awt.Component
import java.awt.Dimension
import java.awt.EventQueue.invokeLater
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.util.function.Supplier
import javax.swing.Box
import javax.swing.JComponent
import kotlin.math.max
Expand All @@ -35,6 +42,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
private val button: ActionButton
private val dispatcher: EventDispatcher<AutoDevInputListener> =
EventDispatcher.create(AutoDevInputListener::class.java)
private var tokenizer: Tokenizer? = null
var text: String
get() {
return input.text
Expand Down Expand Up @@ -66,7 +74,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
input = AutoDevInput(project, listOf(), disposable, this)
documentListener = object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
val i = input.preferredSize.height
val i = input?.preferredSize?.height
if (i != input.height) {
revalidate()
}
Expand All @@ -90,11 +98,21 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
borderLayoutPanel.addToRight(button)
addToBottom(borderLayoutPanel)

ComponentValidator(disposable!!).withValidator(Supplier<ValidationInfo?> {
val validationInfo: ValidationInfo? = this.getInputValidationInfo()
button.setEnabled(validationInfo == null)
return@Supplier validationInfo
}).installOn((this as JComponent)).revalidate()

addListener(object : AutoDevInputListener {
override fun editorAdded(editor: EditorEx) {
this@AutoDevInputSection.initEditor()
}
})

invokeLater {
tokenizer = TokenizerImpl()
}
}

fun initEditor() {
Expand Down Expand Up @@ -143,6 +161,17 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
// input.setSendingMode(sendingMode)
}

private fun getInputValidationInfo(): ValidationInfo? {
val text = input.getDocument().text
val textLength = (this.tokenizer)?.count(text) ?: text.length

val exceed: Int = textLength - LLM_MAX_TOKEN
if (exceed <= 0) return null

val errorMessage = AutoDevBundle.message("chat.too.long.user.message", exceed)
return ValidationInfo(errorMessage, this as JComponent).asWarning()
}

private val maxHeight: Int
get() {
val decorator: InternalDecorator = UIUtil.getParentOfType(
Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/messages/AutoDevBundle.properties
Expand Up @@ -2,21 +2,18 @@ name=AutoDev
notification.group.name=AutoDev
# dont remove following line
intention.category.llm=AutoDev

devti.progress.creatingStory=Processing story detail (1/5)
devti.generatingDtoAndEntity=Create DTO and Entity (2/5)
devti.progress.fetchingSuggestEndpoint=Fetching suggest endpoint (Controller) (3/5)
devti.progress.updatingEndpointMethod=Updating endpoint (Controller) method (4/5)
devti.progress.creatingServiceAndRepository=Create Service and Repository (5/5)
# Config
autodev.flow=AutoDev Flow

devti.chat.send=Send
devti.chat.replaceSelection=Replace Selection
devti.loading=Loading...
chat.label.initial.text='Enter' to start, 'Shift+Enter' for a new line
code.complete.intention.write.action=Generate code

intentions.assistant.name=AutoDev AI AutoAction
intentions.assistant.popup.title=AutoDev AI Assistant
intentions.chat.code.test.step.prepare-context=Prepare context
Expand All @@ -32,10 +29,9 @@ intentions.chat.inlay.complete.family.name=Code complete (Inlay)
intentions.chat.inlay.complete.name=Code complete (Inlay)
intentions.chat.new.family.name=New Chat with code
intentions.chat.new.name=Chat with code

intentions.crud.new.name=AutoCrud with this
intentions.crud.new.family.name=AutoCrud with this
intentions.chat.selected.code.name=Chat with this code
intentions.chat.selected.fragment.name=Chat with {0} fragment
intentions.chat.selected.element.name=Chat with ''{0}'' {1}
tooltip.title.send=Create property
chat.too.long.user.message=Message has is {0} tokens, it's too looooooooooooooooooong

0 comments on commit 459b711

Please sign in to comment.