Skip to content

Commit

Permalink
Fix typo: "/" -> "\" (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
exigow authored and Sa1to committed Feb 7, 2024
1 parent 1204f1c commit 3960751
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
6 changes: 3 additions & 3 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Prerequisite: You have to be **signed in**. This is important because we expect
System.out.
```
2. Place a cursor at the end of the `System.out.` line.
3. Trigger autocompletion with <kbd>Alt</kbd> + <kbd>/</kbd>.
3. Trigger autocompletion with <kbd>Alt</kbd> + <kbd>\</kbd> (or <kbd>option</kbd> + <kbd>\</kbd> on Mac).

#### Expected behaviour

Expand All @@ -85,7 +85,7 @@ Prerequisite: You have to be **signed in**. This is important because we expect
public void bubbleSort(int[] array) {
```
2. Place the cursor at the end of the line.
3. Trigger autocompletion with <kbd>Alt</kbd> + <kbd>/</kbd>.
3. Trigger autocompletion with <kbd>Alt</kbd> + <kbd>\</kbd> (or <kbd>option</kbd> + <kbd>\</kbd> on Mac).

#### Expected behaviour

Expand All @@ -99,7 +99,7 @@ Prerequisite: You have to be **signed in**. This is important because we expect
System.out.println("Hello World!");
```
2. Place cursor at the end of the `// print ` line.
3. Trigger autocompletion with <kbd>Alt</kbd> + <kbd>/</kbd>.
3. Trigger autocompletion with <kbd>Alt</kbd> + <kbd>\</kbd> (or <kbd>option</kbd> + <kbd>\</kbd> on Mac).

#### Expected behaviour

Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ interface CodyAgentServer {
@JsonRequest("chat/models")
fun chatModels(params: ChatModelsParams): CompletableFuture<ChatModelsResponse>

@JsonRequest("chat/setModel")
fun chatSetModel(params: ChatSetModelParams): CompletableFuture<Boolean>

@JsonRequest("chat/restore") fun chatRestore(params: ChatRestoreParams): CompletableFuture<String>

@JsonRequest("attribution/search")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.sourcegraph.cody.agent.protocol

data class ChatSetModelParams(val id: String, val model: String)
30 changes: 24 additions & 6 deletions src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private constructor(
private val project: Project,
newSessionId: CompletableFuture<SessionId>,
private val internalId: String = UUID.randomUUID().toString(),
private var selectedModel: String = ""
) : ChatSession {

/**
Expand All @@ -43,19 +44,22 @@ private constructor(

init {
cancellationToken.get().dispose()
sessionId.get().thenAccept { sessionId ->
chatPanel.addModelDropdown(project, sessionId, selectedModel)
}
}

fun restoreAgentSession(agent: CodyAgent) {
// todo serialize model
val model = "anthropic/claude-2.0"
val messagesToReload =
messages
.toList()
.dropWhile { it.speaker == Speaker.ASSISTANT }
.fold(emptyList<ChatMessage>()) { acc, msg ->
if (acc.lastOrNull()?.speaker == msg.speaker) acc else acc.plus(msg)
}
val restoreParams = ChatRestoreParams(model, messagesToReload, UUID.randomUUID().toString())
val restoreParams =
ChatRestoreParams(selectedModel, messagesToReload, UUID.randomUUID().toString())
val newSessionId = agent.server.chatRestore(restoreParams)
sessionId.getAndSet(newSessionId)
}
Expand Down Expand Up @@ -177,9 +181,21 @@ private constructor(
if (messages.lastOrNull()?.id == message.id) {
messages.removeLast()
}
messages.add(message)
chatPanel.addOrUpdateMessage(message)
HistoryService.getInstance().update(project, internalId, messages)
if (messages.size == 0) {
selectedModel = chatPanel.modelDropdown.selectedItem?.toString() ?: ""
sessionId.get().thenAccept { sessionId ->
CodyAgentService.applyAgentOnBackgroundThread(project) { agent ->
agent.server.chatSetModel(ChatSetModelParams(sessionId, selectedModel)).get()
messages.add(message)
chatPanel.addOrUpdateMessage(message)
HistoryService.getInstance().update(project, internalId, messages, selectedModel)
}
}
} else {
messages.add(message)
chatPanel.addOrUpdateMessage(message)
HistoryService.getInstance().update(project, internalId, messages)
}
}

@RequiresEdt
Expand All @@ -193,6 +209,7 @@ private constructor(

companion object {
private val logger = LoggerFactory.getLogger(AgentChatSession::class.java)
private const val defaultModel = "anthropic/claude-2.0"

@RequiresEdt
fun createNew(project: Project): AgentChatSession {
Expand Down Expand Up @@ -238,7 +255,8 @@ private constructor(
@RequiresEdt
fun createFromState(project: Project, state: ChatState): AgentChatSession {
val sessionId = createNewPanel(project) { it.server.chatNew() }
val chatSession = AgentChatSession(project, sessionId, state.internalId!!)
val chatSession =
AgentChatSession(project, sessionId, state.internalId!!, state.model ?: defaultModel)
for (message in state.messages) {
val parsed =
when (val speaker = message.speaker) {
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/sourcegraph/cody/chat/ui/ChatPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package com.sourcegraph.cody.chat.ui

import com.intellij.icons.AllIcons
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.VerticalFlowLayout
import com.intellij.util.IconUtil
import com.intellij.util.concurrency.annotations.RequiresEdt
import com.sourcegraph.cody.PromptPanel
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.protocol.ChatMessage
import com.sourcegraph.cody.agent.protocol.ChatModelsParams
import com.sourcegraph.cody.chat.ChatSession
import com.sourcegraph.cody.context.ui.EnhancedContextPanel
import com.sourcegraph.cody.ui.ChatScrollPane
Expand All @@ -24,6 +27,7 @@ class ChatPanel(project: Project, chatSession: ChatSession) :
val promptPanel: PromptPanel = PromptPanel(chatSession)
private val messagesPanel = MessagesPanel(project, chatSession)
private val chatPanel = ChatScrollPane(messagesPanel)
val modelDropdown = ComboBox<String>()
private val contextView: EnhancedContextPanel = EnhancedContextPanel(project)

private val stopGeneratingButton =
Expand All @@ -48,10 +52,31 @@ class ChatPanel(project: Project, chatSession: ChatSession) :
add(lowerPanel, BorderLayout.SOUTH)
}

fun addModelDropdown(project: Project, sessionId: String, selectedModel: String) {
CodyAgentService.applyAgentOnBackgroundThread(project) { agent ->
agent.server.isCurrentUserPro().thenApply { isUserPro ->
if (isUserPro == true) {
add(modelDropdown, BorderLayout.NORTH)
if (selectedModel == "") {
val chatModels = agent.server.chatModels(ChatModelsParams(sessionId))
chatModels.thenApply { response ->
response.models.forEach { model -> modelDropdown.addItem(model.model) }
}
} else {
modelDropdown.addItem(selectedModel)
}
}
}
}
}

fun isEnhancedContextEnabled(): Boolean = contextView.isEnhancedContextEnabled.get()

@RequiresEdt
fun addOrUpdateMessage(message: ChatMessage, shouldAddBlinkingCursor: Boolean = true) {
if (messagesPanel.componentCount == 1) {
modelDropdown.isEnabled = false
}
messagesPanel.addOrUpdateMessage(message, shouldAddBlinkingCursor)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class HistoryService : SimplePersistentStateComponent<HistoryState>(HistoryState
synchronized(listeners) { listeners += listener }
}

fun update(project: Project, internalId: String, chatMessages: List<ChatMessage>, selectedModel: String) {
val found = getChatOrCreate(project, internalId)
found.model = selectedModel
update(project, internalId, chatMessages)
}

fun update(project: Project, internalId: String, chatMessages: List<ChatMessage>) {
val found = getChatOrCreate(project, internalId)
found.messages = chatMessages.map(::convertToMessageState).toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ChatState : BaseState() {

@get:OptionTag(tag = "updatedAt", nameAttribute = "") var updatedAt: String? by string()

@get:OptionTag(tag = "model", nameAttribute = "") var model: String? by string()

@get:OptionTag(tag = "accountId", nameAttribute = "") var accountId: String? by string()

fun title(): String? = messages.firstOrNull()?.text
Expand Down

0 comments on commit 3960751

Please sign in to comment.