generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cea5919
commit 10dd236
Showing
10 changed files
with
150 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html --> | ||
<idea-plugin> | ||
<id>com.github.vadymignatiev.octopusrider</id> | ||
<name>OctopusRider Template</name> | ||
<vendor>vadymignatiev</vendor> | ||
|
||
<depends>com.intellij.modules.platform</depends> | ||
|
||
<resource-bundle>messages.MyBundle</resource-bundle> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<toolWindow factoryClass="com.github.vadymignatiev.octopusrider.toolWindow.MyToolWindowFactory" id="MyToolWindow"/> | ||
</extensions> | ||
|
||
<actions> | ||
|
||
<action id="OrderFood" class="com.github.vadymignatiev.octopusrider.OrderFoodAction" | ||
text="Order Food!"> | ||
|
||
<add-to-group group-id="ToolbarRunGroup" anchor="last"/> | ||
</action> | ||
|
||
<action id="ChatAction" class="com.github.vadymignatiev.octopusrider.ChatAction" text="Open Chat Window" | ||
description="Opens the chat window"> | ||
<!-- <add-to-group group-id="ToolsMenu" anchor="last"/>--> | ||
</action> | ||
|
||
</actions> | ||
|
||
<applicationListeners> | ||
<listener class="com.github.vadymignatiev.octopusrider.listeners.MyApplicationActivationListener" topic="com.intellij.openapi.application.ApplicationActivationListener"/> | ||
</applicationListeners> | ||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
projectService=Project service: {0} | ||
randomLabel=The random number is: {0} | ||
shuffle=Shuffle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.vadymignatiev.octopusrider | ||
|
||
import com.aallam.openai.api.chat.* | ||
import com.aallam.openai.api.model.ModelId | ||
import com.aallam.openai.client.OpenAI | ||
import kotlinx.serialization.json.* | ||
import java.awt.Color | ||
import javax.swing.SwingWorker | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
class ChatPresenter { | ||
private val token = "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" | ||
private val openAI = OpenAI(token) | ||
private val modelId = ModelId("gpt-3.5-turbo-1106") | ||
private val chatMessages = mutableListOf<ChatMessage>() | ||
|
||
val coroutineScope = CoroutineScope(Dispatchers.Main) | ||
|
||
val window = ChatWindow(this) | ||
|
||
init { window.isVisible = true } | ||
|
||
fun sendMessage(userMessage: String) { | ||
val userChatMessage = ChatMessage(ChatRole.User, userMessage) | ||
chatMessages.add(userChatMessage) | ||
|
||
coroutineScope.launch { | ||
val serviceResponse = sendToService(userMessage) | ||
window.appendMessage(serviceResponse, Color.BLUE) | ||
val serviceChatMessage = ChatMessage(ChatRole.System, serviceResponse) | ||
chatMessages.add(serviceChatMessage) | ||
} | ||
} | ||
|
||
private suspend fun sendToService(message: String): String { | ||
val request = chatCompletionRequest { | ||
model = modelId | ||
messages = chatMessages | ||
} | ||
val response = openAI.chatCompletion(request) | ||
return response.choices.first().message.content.orEmpty() | ||
} | ||
|
||
} |
41 changes: 36 additions & 5 deletions
41
src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,55 @@ | ||
package com.github.vadymignatiev.octopusrider | ||
|
||
import java.awt.* | ||
import javax.swing.* | ||
import javax.swing.text.SimpleAttributeSet | ||
import javax.swing.text.StyleConstants | ||
import java.awt.BorderLayout | ||
import java.awt.Color | ||
import java.awt.event.WindowAdapter | ||
import java.awt.event.WindowEvent | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.swing.Swing | ||
import com.aallam.openai.api.chat.* | ||
import com.aallam.openai.api.model.ModelId | ||
import com.aallam.openai.client.OpenAI | ||
import kotlinx.serialization.json.* | ||
|
||
class ChatWindow(val presenter: ChatPresenter) : JFrame() { | ||
|
||
private val messageArea = JTextPane() | ||
|
||
class ChatWindow : JFrame() { | ||
init { | ||
title = "Chat Window" | ||
setSize(400, 300) | ||
setSize(800, 600) | ||
defaultCloseOperation = JFrame.DISPOSE_ON_CLOSE | ||
contentPane.layout = BorderLayout() | ||
val messageArea = JTextArea() | ||
messageArea.isEditable = false | ||
contentPane.add(JScrollPane(messageArea), BorderLayout.CENTER) | ||
|
||
val textField = JTextField() | ||
textField.addActionListener { | ||
val text = textField.text | ||
messageArea.append(text + "\n") | ||
appendMessage(text, Color.BLACK) | ||
presenter.sendMessage(text) | ||
textField.text = "" | ||
} | ||
|
||
contentPane.add(textField, BorderLayout.SOUTH) | ||
|
||
addWindowListener(object : WindowAdapter() { | ||
override fun windowClosing(e: WindowEvent?) { | ||
presenter.coroutineScope.cancel() | ||
super.windowClosing(e) | ||
} | ||
}) | ||
|
||
pack() | ||
} | ||
|
||
fun appendMessage(message: String, color: Color) { | ||
val doc = messageArea.styledDocument | ||
val style = SimpleAttributeSet() | ||
StyleConstants.setForeground(style, color) | ||
doc.insertString(doc.length, "$message\n", style) | ||
} | ||
} |