Skip to content

Commit

Permalink
Add actual llm dropdown for inline edits
Browse files Browse the repository at this point in the history
  • Loading branch information
mkondratek committed Mar 28, 2024
1 parent 5ddec4e commit 7516a74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
42 changes: 17 additions & 25 deletions src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.wm.WindowManager
import com.intellij.ui.JBColor
import com.intellij.ui.components.fields.ExpandableTextField
import com.intellij.util.concurrency.annotations.RequiresEdt
import java.awt.*
import com.sourcegraph.cody.agent.protocol.ModelUsage
import com.sourcegraph.cody.chat.ui.LlmDropdown
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import java.awt.event.KeyAdapter
Expand Down Expand Up @@ -39,7 +44,12 @@ class EditCommandPrompt(val controller: FixupService, val editor: Editor, val di
minimumSize = Dimension(preferredWidth, minimumSize.height)
}

lateinit var modelComboBox: ComboBox<String>
val llmDropdown =
LlmDropdown(
modelUsage = ModelUsage.EDIT,
project = controller.project,
onSetSelectedItem = controller::setCurrentModel,
chatModelProviderFromState = null)

// History navigation helper
private val historyCursor = HistoryCursor()
Expand Down Expand Up @@ -171,9 +181,9 @@ class EditCommandPrompt(val controller: FixupService, val editor: Editor, val di
@RequiresEdt
override fun doOKAction() {
val text = instructionsField.text
val model = modelComboBox.item
val chatModelProvider = llmDropdown.item
super.doOKAction()
controller.setCurrentModel(model)
controller.setCurrentModel(chatModelProvider)
if (text.isNotBlank()) {
addToHistory(text)
val project = editor.project
Expand All @@ -183,7 +193,7 @@ class EditCommandPrompt(val controller: FixupService, val editor: Editor, val di
return
}
controller.addSession(
EditSession(controller, editor, project, editor.document, text, model))
EditSession(controller, editor, project, editor.document, text, chatModelProvider))
}
}

Expand Down Expand Up @@ -218,25 +228,7 @@ class EditCommandPrompt(val controller: FixupService, val editor: Editor, val di
}
southRow.add(historyLabel, BorderLayout.CENTER)

modelComboBox =
ComboBox(controller.getModels().toTypedArray()).apply {
selectedItem = controller.getCurrentModel()
addKeyListener(
object : KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (e.isActionKey() ||
e.keyCode == KeyEvent.VK_TAB ||
e.isControlDown ||
e.isMetaDown) {
return
}
if (!instructionsField.hasFocus()) {
instructionsField.requestFocusInWindow()
}
}
})
}
southRow.add(modelComboBox, BorderLayout.EAST)
southRow.add(llmDropdown, BorderLayout.EAST)

root.add(topRow, BorderLayout.NORTH)
root.add(southRow, BorderLayout.SOUTH)
Expand Down
9 changes: 4 additions & 5 deletions src/main/kotlin/com/sourcegraph/cody/edit/EditSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.sourcegraph.cody.agent.CodyAgent
import com.sourcegraph.cody.agent.protocol.ChatModelsResponse
import com.sourcegraph.cody.agent.protocol.EditTask
import com.sourcegraph.cody.agent.protocol.InlineEditParams
import java.util.concurrent.CompletableFuture
Expand All @@ -20,20 +21,18 @@ class EditSession(
project: Project,
document: Document,
val instructions: String,
val model: String,
private val chatModelProvider: ChatModelsResponse.ChatModelProvider,
) : FixupSession(controller, editor, project, document) {
private val logger = Logger.getInstance(EditSession::class.java)

override fun makeEditingRequest(agent: CodyAgent): CompletableFuture<EditTask> {
val params = InlineEditParams(instructions, model)
val params = InlineEditParams(instructions, chatModelProvider.model)
return agent.server.commandsEdit(params)
}

override fun dispose() {}

override fun diff() {
logger.warn("Diff: Not yet implemented")
}
override fun diff() {}

override fun retry() {
// TODO: The actual prompt is displayed as ghost text in the text input field.
Expand Down
10 changes: 3 additions & 7 deletions src/main/kotlin/com/sourcegraph/cody/edit/FixupService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.sourcegraph.cody.agent.protocol.ChatModelsResponse
import com.sourcegraph.cody.agent.protocol.EditTask
import com.sourcegraph.config.ConfigUtil.isCodyEnabled
import com.sourcegraph.utils.CodyEditorUtil
Expand All @@ -19,7 +20,7 @@ class FixupService(val project: Project) : Disposable {
// TODO: Consider doing the multiplexing in CodyAgentClient instead.
private var activeSessions: MutableMap<String, FixupSession> = mutableMapOf()

private var lastSelectedModel = "GPT-3.5"
private var lastSelectedModel: ChatModelsResponse.ChatModelProvider? = null

// Sessions for which we have not yet received a task ID, but may receive an edit anyway.
private var pendingSessions: MutableSet<FixupSession> = mutableSetOf()
Expand Down Expand Up @@ -52,12 +53,7 @@ class FixupService(val project: Project) : Disposable {
return true
}

// TODO: get model list from protocol
fun getModels(): List<String> = listOf("GPT-4", "GPT-3.5")

fun getCurrentModel(): String = lastSelectedModel

fun setCurrentModel(model: String) {
fun setCurrentModel(model: ChatModelsResponse.ChatModelProvider) {
lastSelectedModel = model
}

Expand Down

0 comments on commit 7516a74

Please sign in to comment.