Skip to content

Commit

Permalink
feat(ui): extract for new ui
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 30, 2023
1 parent 0af1217 commit 86a5c72
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 33 deletions.
Expand Up @@ -68,7 +68,8 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB
}

fun add(message: String, isMe: Boolean = false) {
val messageView = MessageView(message, ChatRole.User)
val role = if (isMe) ChatRole.User else ChatRole.Assistant
val messageView = MessageView(message, role)

myList.add(messageView)
updateLayout()
Expand Down Expand Up @@ -124,7 +125,7 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB
}

private suspend fun updateMessageInUi(content: Flow<String>): String {
val messageView = MessageView("...", ChatRole.Assistant)
val messageView = MessageView("", ChatRole.Assistant)
myList.add(messageView)

var text = ""
Expand Down
Expand Up @@ -4,6 +4,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.llms.ConnectorFactory
import cc.unitmesh.devti.editor.LLMCoroutineScopeService
import cc.unitmesh.devti.gui.chat.block.SimpleMessage
import cc.unitmesh.devti.parser.PostCodeProcessor
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
Expand All @@ -25,7 +26,7 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
prompt: ContextPrompter,
context: ChatContext? = null
) {
ui.add(prompt.displayPrompt(), true)
ui.add(prompt.requestPrompt(), true)
ui.add(AutoDevBundle.message("devti.loading"))

ApplicationManager.getApplication().executeOnPooledThread {
Expand Down
89 changes: 61 additions & 28 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/MessageView.kt
Expand Up @@ -40,47 +40,53 @@ class MessageView(private val message: String, role: ChatRole) : JBPanel<Message

add(centerPanel, BorderLayout.CENTER)

// layoutAll(message)
val parts = layoutAll(message, SimpleMessage(message, message, role))
println(parts)
}

fun layoutAll(messageText: String): List<MessageBlock> {
var currentContextType = MessageBlockType.PlainText
fun layoutAll(messageText: String, message: CompletableMessage): List<MessageBlock> {
val currentContextTypeRef = Ref.ObjectRef<MessageBlockType>()
currentContextTypeRef.element = MessageBlockType.PlainText

val blockStart: Ref.IntRef = Ref.IntRef()

val parts = ArrayList<MessageBlock>()
val parts = mutableListOf<MessageBlock>()

for ((index, item) in messageText.withIndex()) {
val param = Parameters(item, index, messageText)
val suggestTypeChange =
MessageCodeBlockCharProcessor().suggestTypeChange(param, currentContextType, blockStart.element)

if (suggestTypeChange != null) {
when {
suggestTypeChange.contextType == currentContextType -> {
if (suggestTypeChange.borderType == BorderType.START) {
logger.error("suggestTypeChange return $currentContextType START while there is already $currentContextType opened")
} else {
pushPart(blockStart, messageText, currentContextType, message, parts, index)
}
MessageCodeBlockCharProcessor().suggestTypeChange(
param,
currentContextTypeRef.element,
blockStart.element
)
?: continue

when {
suggestTypeChange.contextType == currentContextTypeRef.element -> {
if (suggestTypeChange.borderType == BorderType.START) {
logger.error("suggestTypeChange return ${currentContextTypeRef.element} START while there is already ${currentContextTypeRef.element} opened")
} else {
pushPart(blockStart, messageText, currentContextTypeRef, message, parts, index)
}
}

suggestTypeChange.borderType == BorderType.START -> {
if (index > blockStart.element) {
pushPart(blockStart, messageText, currentContextType, message, parts, index - 1)
}
blockStart.element = index
currentContextType = suggestTypeChange.contextType
suggestTypeChange.borderType == BorderType.START -> {
if (index > blockStart.element) {
pushPart(blockStart, messageText, currentContextTypeRef, message, parts, index - 1)
}
blockStart.element = index
currentContextTypeRef.element = suggestTypeChange.contextType
}

else -> {
logger.error("suggestTypeChange return $currentContextType END when there wasn't open tag")
}
else -> {
logger.error("suggestTypeChange return ${currentContextTypeRef.element} END when there wasn't open tag")
}
}
}

if (blockStart.element < messageText.length) {
pushPart(blockStart, messageText, currentContextType, message, parts, messageText.length - 1)
pushPart(blockStart, messageText, currentContextTypeRef, message, parts, messageText.length - 1)
}

return parts
Expand All @@ -89,12 +95,39 @@ class MessageView(private val message: String, role: ChatRole) : JBPanel<Message
private fun pushPart(
blockStart: Ref.IntRef,
messageText: String,
currentContextType: MessageBlockType,
message: String,
list: List<MessageBlock>,
currentContextType: Ref.ObjectRef<MessageBlockType>,
message: CompletableMessage,
list: MutableList<MessageBlock>,
partUpperOffset: Int
) {
val newPart = createPart(blockStart.element, partUpperOffset, messageText, currentContextType, message)
list.add(newPart)

blockStart.element = partUpperOffset + 1
currentContextType.element = MessageBlockType.PlainText
}

private fun createPart(
blockStart: Int,
partUpperOffset: Int,
messageText: String,
currentContextType: Ref.ObjectRef<MessageBlockType>,
message: CompletableMessage
): MessageBlock {
check(blockStart < messageText.length)
check(partUpperOffset < messageText.length)

val blockText = messageText.substring(blockStart, partUpperOffset + 1)
val part: MessageBlock = when (currentContextType.element!!) {
MessageBlockType.CodeEditor -> TextBlock(message)
MessageBlockType.PlainText -> CodeBlock(message)
}

if (blockText.isNotEmpty()) {
part.addContent(blockText)
}

return part
}

fun updateContent(content: String) {
Expand Down Expand Up @@ -135,6 +168,6 @@ class MessageView(private val message: String, role: ChatRole) : JBPanel<Message
}

override fun getData(dataId: String): Any? {
TODO("Not yet implemented")
return message
}
}
Expand Up @@ -7,8 +7,6 @@ interface CompletableMessage {
val displayText: String
val role: ChatRole

fun addContent(addedContent: String)
fun replaceContent(content: String)
fun addTextListener(textListener: MessageBlockTextListener)
fun removeTextListener(textListener: MessageBlockTextListener)
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/block/MessageBlock.kt
Expand Up @@ -4,16 +4,26 @@ interface MessageBlock {
val type: MessageBlockType
val message: CompletableMessage
val textContent: String

fun addContent(blockText: String)
}

class TextBlock(override val message: CompletableMessage) : MessageBlock {
override val type: MessageBlockType = MessageBlockType.PlainText
override val textContent: String
get() = message.text

override fun addContent(blockText: String) {

}
}

class CodeBlock(override val message: CompletableMessage) : MessageBlock {
override val type: MessageBlockType = MessageBlockType.CodeEditor
override val textContent: String
get() = message.text

override fun addContent(blockText: String) {

}
}
19 changes: 19 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/block/SimpleMessage.kt
@@ -0,0 +1,19 @@
package cc.unitmesh.devti.gui.chat.block

import cc.unitmesh.devti.gui.chat.ChatRole

class SimpleMessage(
override val displayText: String,
override val text: String,
override val role: ChatRole
) : CompletableMessage {
private val textListeners: MutableList<MessageBlockTextListener> = mutableListOf()

override fun addTextListener(textListener: MessageBlockTextListener) {
textListeners += textListener
}

override fun removeTextListener(textListener: MessageBlockTextListener) {
textListeners -= textListener
}
}

0 comments on commit 86a5c72

Please sign in to comment.