refactor: remove redundant ToolCallViewAdapter

This commit is contained in:
Carl-Robert Linnupuu 2026-01-10 17:17:08 +00:00
parent a311d90ca8
commit dbb6158162
7 changed files with 68 additions and 163 deletions

View file

@ -624,7 +624,7 @@ class AgentEventHandler(
if (existing != null) return existing
val vm = AgentRunViewModel()
val view = AgentRunDslPanel(vm)
val view = AgentRunDslPanel(project, vm)
currentResponseBody?.addToolStatusPanel(view.component)
val viewHolder = RunViewHolder(vm, view)
subagentViewHolders[taskId] = viewHolder

View file

@ -1,17 +1,21 @@
package ee.carlrobert.codegpt.toolwindow.agent.ui
import com.intellij.openapi.project.Project
import com.intellij.ui.components.ActionLink
import com.intellij.ui.components.JBScrollPane
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor.ToolCallDescriptorFactory
import ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor.ToolCallView
import ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor.ToolCallViewAdapter
import java.awt.BorderLayout
import javax.swing.BoxLayout
import javax.swing.JComponent
import javax.swing.JPanel
class AgentRunDslPanel(private val vm: AgentRunViewModel) {
class AgentRunDslPanel(
private val project: Project,
private val vm: AgentRunViewModel
) {
private val rowsPanel = JPanel().apply {
isOpaque = false
@ -89,7 +93,14 @@ class AgentRunDslPanel(private val vm: AgentRunViewModel) {
rowsPanel.removeAll()
finalList.forEach { value ->
val view = ToolCallViewAdapter.build(value)
val descriptor = ToolCallDescriptorFactory.create(
project = project,
toolName = value.toolName,
args = value.args ?: "",
result = value.result,
overrideKind = value.kind
)
val view = ToolCallView(descriptor)
viewByEntryId[value.id] = view
val leftIndent = if (value.parentId != null) 12 else 0
rowsPanel.add(BorderLayoutPanel().apply {

View file

@ -7,15 +7,19 @@ sealed class RunEntry {
abstract val id: String
abstract val parentId: String?
abstract val kind: ToolKind
abstract val toolName: String
abstract val args: Any?
abstract val result: Any?
abstract fun withAnyResult(result: Any?): RunEntry
data class ReadEntry(
override val id: String,
override val parentId: String? = null,
val args: ReadTool.Args? = null,
val result: ReadTool.Result? = null,
override val args: ReadTool.Args? = null,
override val result: ReadTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.READ
override val toolName: String = "Read"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? ReadTool.Result)
}
@ -23,10 +27,11 @@ sealed class RunEntry {
data class IntelliJSearchEntry(
override val id: String,
override val parentId: String? = null,
val args: IntelliJSearchTool.Args? = null,
val result: IntelliJSearchTool.Result? = null,
override val args: IntelliJSearchTool.Args? = null,
override val result: IntelliJSearchTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.SEARCH
override val toolName: String = "IntelliJSearch"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? IntelliJSearchTool.Result)
}
@ -34,10 +39,11 @@ sealed class RunEntry {
data class BashEntry(
override val id: String,
override val parentId: String? = null,
val args: BashTool.Args? = null,
val result: BashTool.Result? = null,
override val args: BashTool.Args? = null,
override val result: BashTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.BASH
override val toolName: String = "Bash"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? BashTool.Result)
}
@ -45,10 +51,11 @@ sealed class RunEntry {
data class WebEntry(
override val id: String,
override val parentId: String? = null,
val args: WebSearchTool.Args? = null,
val result: WebSearchTool.Result? = null,
override val args: WebSearchTool.Args? = null,
override val result: WebSearchTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.WEB
override val toolName: String = "WebSearch"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? WebSearchTool.Result)
}
@ -56,10 +63,11 @@ sealed class RunEntry {
data class WriteEntry(
override val id: String,
override val parentId: String? = null,
val args: WriteTool.Args? = null,
val result: WriteTool.Result? = null,
override val args: WriteTool.Args? = null,
override val result: WriteTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.WRITE
override val toolName: String = "Write"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? WriteTool.Result)
}
@ -67,10 +75,11 @@ sealed class RunEntry {
data class EditEntry(
override val id: String,
override val parentId: String? = null,
val args: EditTool.Args? = null,
val result: EditTool.Result? = null,
override val args: EditTool.Args? = null,
override val result: EditTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.EDIT
override val toolName: String = "Edit"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? EditTool.Result)
}
@ -78,11 +87,12 @@ sealed class RunEntry {
data class TaskEntry(
override val id: String,
override val parentId: String? = null,
val args: TaskTool.Args? = null,
val result: TaskTool.Result? = null,
override val args: TaskTool.Args? = null,
override val result: TaskTool.Result? = null,
val summary: TaskSummary? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.TASK
override val toolName: String = "Task"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? TaskTool.Result)
}
@ -90,10 +100,11 @@ sealed class RunEntry {
data class LibraryResolveEntry(
override val id: String,
override val parentId: String? = null,
val args: ResolveLibraryIdTool.Args? = null,
val result: ResolveLibraryIdTool.Result? = null,
override val args: ResolveLibraryIdTool.Args? = null,
override val result: ResolveLibraryIdTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.LIBRARY_RESOLVE
override val toolName: String = "ResolveLibraryId"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? ResolveLibraryIdTool.Result)
}
@ -101,10 +112,11 @@ sealed class RunEntry {
data class LibraryDocsEntry(
override val id: String,
override val parentId: String? = null,
val args: GetLibraryDocsTool.Args? = null,
val result: GetLibraryDocsTool.Result? = null,
override val args: GetLibraryDocsTool.Args? = null,
override val result: GetLibraryDocsTool.Result? = null,
) : RunEntry() {
override val kind: ToolKind = ToolKind.LIBRARY_DOCS
override val toolName: String = "GetLibraryDocs"
override fun withAnyResult(result: Any?): RunEntry =
copy(result = result as? GetLibraryDocsTool.Result)
}
@ -115,6 +127,9 @@ sealed class RunEntry {
val name: String,
) : RunEntry() {
override val kind: ToolKind = ToolKind.OTHER
override val toolName: String get() = name
override val args: Any? = null
override val result: Any? = null
override fun withAnyResult(result: Any?): RunEntry = this
}
}

View file

@ -6,39 +6,39 @@ import com.intellij.util.ui.JBUI
import ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor.ToolCallDescriptorFactory
import ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor.ToolCallView
import ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor.ToolKind
import java.awt.BorderLayout
class ToolCallCard(
project: Project,
private val project: Project,
private val toolName: String,
private val args: Any?,
private val overrideKind: ToolKind? = null
) : JBPanel<ToolCallCard>() {
private val view: ToolCallView
private val projectId: String = project.locationHash
init {
layout = java.awt.BorderLayout()
layout = BorderLayout()
isOpaque = false
border = JBUI.Borders.empty()
val descriptor = ToolCallDescriptorFactory.create(
project = project,
toolName = toolName,
args = args ?: Unit,
result = null,
projectId = projectId,
overrideKind = overrideKind
)
view = ToolCallView(descriptor)
add(view, java.awt.BorderLayout.CENTER)
add(view, BorderLayout.CENTER)
}
fun complete(success: Boolean, result: Any?) {
val updated = ToolCallDescriptorFactory.create(
project = project,
toolName = toolName,
args = args ?: Unit,
result = result,
projectId = projectId,
overrideKind = overrideKind
)
view.refreshDescriptor(updated)

View file

@ -5,18 +5,13 @@ import com.intellij.diff.DiffManager
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.ProjectLocator
import com.intellij.openapi.vfs.VirtualFileManager
import ee.carlrobert.codegpt.util.ApplicationUtil
import java.awt.Component
object DiffViewAction {
fun showDiff(filePath: String, component: Component) {
val project = ProjectManager.getInstance().openProjects.firstOrNull()
if (project != null) {
showDiff(filePath, project)
}
}
fun showDiff(filePath: String, project: Project) {
val virtualFile = VirtualFileManager.getInstance().findFileByUrl("file://$filePath")
if (virtualFile != null) {

View file

@ -1,6 +1,7 @@
package ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor
import com.intellij.icons.AllIcons
import com.intellij.openapi.project.Project
import com.intellij.ui.JBColor
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
@ -18,29 +19,26 @@ import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.nio.file.Files
import java.nio.file.Path
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTextArea
import javax.swing.*
import kotlin.math.absoluteValue
object ToolCallDescriptorFactory {
fun create(
project: Project,
toolName: String,
args: Any,
result: Any? = null,
projectId: String? = null,
overrideKind: ToolKind? = null
): ToolCallDescriptor {
val kind = overrideKind ?: detectToolKind(toolName, args)
val projectId = project.locationHash
return when (kind) {
ToolKind.SEARCH -> createSearchDescriptor(args, result, projectId)
ToolKind.READ -> createReadDescriptor(args, result, projectId)
ToolKind.WRITE -> createWriteDescriptor(args, result, projectId)
ToolKind.EDIT -> createEditDescriptor(args, result, projectId)
ToolKind.WRITE -> createWriteDescriptor(project, args, result, projectId)
ToolKind.EDIT -> createEditDescriptor(project, args, result, projectId)
ToolKind.BASH -> createBashDescriptor(args, result, projectId)
ToolKind.WEB -> createWebDescriptor(args, result, projectId)
ToolKind.TASK -> createTaskDescriptor(args, result, projectId)
@ -182,6 +180,7 @@ object ToolCallDescriptorFactory {
}
private fun createWriteDescriptor(
project: Project,
args: Any,
result: Any?,
projectId: String?
@ -197,8 +196,8 @@ object ToolCallDescriptorFactory {
is WriteTool.Result.Success -> {
badges.add(Badge("${result.bytesWritten} bytes", JBColor.GREEN))
actions.add(
ToolAction("View Changes", AllIcons.Actions.Diff) { component ->
DiffViewAction.showDiff(writeArgs.filePath, component)
ToolAction("View Changes", AllIcons.Actions.Diff) {
DiffViewAction.showDiff(writeArgs.filePath, project)
}
)
}
@ -231,6 +230,7 @@ object ToolCallDescriptorFactory {
}
private fun createEditDescriptor(
project: Project,
args: Any,
result: Any?,
projectId: String?
@ -282,7 +282,7 @@ object ToolCallDescriptorFactory {
null
)
} catch (_: Exception) {
DiffViewAction.showDiff(editArgs.filePath, component)
DiffViewAction.showDiff(editArgs.filePath, project)
}
}
)

View file

@ -1,116 +0,0 @@
package ee.carlrobert.codegpt.toolwindow.agent.ui.descriptor
import ee.carlrobert.codegpt.toolwindow.agent.ui.RunEntry
object ToolCallViewAdapter {
fun build(entry: RunEntry): ToolCallView {
val descriptor = createDescriptor(entry)
return ToolCallView(descriptor)
}
private fun createDescriptor(entry: RunEntry): ToolCallDescriptor {
return when (entry) {
is RunEntry.TaskEntry -> createTaskDescriptor(entry)
is RunEntry.ReadEntry -> createReadDescriptor(entry)
is RunEntry.BashEntry -> createBashDescriptor(entry)
is RunEntry.IntelliJSearchEntry -> createIntelliJSearchDescriptor(entry)
is RunEntry.WebEntry -> createWebDescriptor(entry)
is RunEntry.WriteEntry -> createWriteDescriptor(entry)
is RunEntry.EditEntry -> createEditDescriptor(entry)
is RunEntry.LibraryResolveEntry -> createLibraryResolveDescriptor(entry)
is RunEntry.LibraryDocsEntry -> createLibraryDocsDescriptor(entry)
else -> createOtherDescriptor(entry)
}
}
private fun createTaskDescriptor(entry: RunEntry.TaskEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "Task",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.TASK
)
}
private fun createReadDescriptor(entry: RunEntry.ReadEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "Read",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.READ
)
}
private fun createBashDescriptor(entry: RunEntry.BashEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "Bash",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.BASH
)
}
private fun createIntelliJSearchDescriptor(entry: RunEntry.IntelliJSearchEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "IntelliJSearch",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.SEARCH
)
}
private fun createWebDescriptor(entry: RunEntry.WebEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "WebSearch",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.WEB
)
}
private fun createWriteDescriptor(entry: RunEntry.WriteEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "Write",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.WRITE
)
}
private fun createEditDescriptor(entry: RunEntry.EditEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "Edit",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.EDIT
)
}
private fun createLibraryResolveDescriptor(entry: RunEntry.LibraryResolveEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "ResolveLibraryId",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.LIBRARY_RESOLVE
)
}
private fun createLibraryDocsDescriptor(entry: RunEntry.LibraryDocsEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = "GetLibraryDocs",
args = entry.args ?: "",
result = entry.result,
overrideKind = ToolKind.LIBRARY_DOCS
)
}
private fun createOtherDescriptor(entry: RunEntry): ToolCallDescriptor {
return ToolCallDescriptorFactory.create(
toolName = entry.kind.name,
args = "",
result = null,
overrideKind = ToolKind.OTHER
)
}
}