|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "metadata": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "source": [ |
| 7 | + "# JetBrains AI Assistant Chat Handler\n", |
| 8 | + "\n", |
| 9 | + "This notebook presents how to extend the [JetBrains AI Assistant](https://plugins.jetbrains.com/plugin/22282-jetbrains-ai-assistant) plugin in the runtime by providing a custom message handler." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "metadata": { |
| 14 | + "ExecuteTime": { |
| 15 | + "end_time": "2025-10-02T04:22:19.265561Z", |
| 16 | + "start_time": "2025-10-02T04:22:17.834847Z" |
| 17 | + }, |
| 18 | + "executionRelatedData": { |
| 19 | + "compiledClasses": [ |
| 20 | + "Line_1_jupyter", |
| 21 | + "Line_2_jupyter" |
| 22 | + ] |
| 23 | + } |
| 24 | + }, |
| 25 | + "cell_type": "code", |
| 26 | + "source": "%use intellij-platform", |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "data": { |
| 30 | + "text/plain": [ |
| 31 | + "IntelliJ Platform integration is loaded" |
| 32 | + ] |
| 33 | + }, |
| 34 | + "metadata": {}, |
| 35 | + "output_type": "display_data", |
| 36 | + "jetTransient": { |
| 37 | + "display_id": null |
| 38 | + } |
| 39 | + } |
| 40 | + ], |
| 41 | + "execution_count": 1 |
| 42 | + }, |
| 43 | + { |
| 44 | + "metadata": {}, |
| 45 | + "cell_type": "markdown", |
| 46 | + "source": "Load the JetBrains AI Assistant bundled plugin using its `com.intellij.ml.llm` ID." |
| 47 | + }, |
| 48 | + { |
| 49 | + "metadata": { |
| 50 | + "ExecuteTime": { |
| 51 | + "end_time": "2025-10-02T04:22:22.365779Z", |
| 52 | + "start_time": "2025-10-02T04:22:21.587057Z" |
| 53 | + }, |
| 54 | + "executionRelatedData": { |
| 55 | + "compiledClasses": [ |
| 56 | + "Line_4_jupyter", |
| 57 | + "Line_5_jupyter", |
| 58 | + "Line_6_jupyter", |
| 59 | + "Line_7_jupyter", |
| 60 | + "Line_8_jupyter" |
| 61 | + ] |
| 62 | + } |
| 63 | + }, |
| 64 | + "cell_type": "code", |
| 65 | + "source": "loadPlugins(\"com.intellij.ml.llm\")", |
| 66 | + "outputs": [], |
| 67 | + "execution_count": 2 |
| 68 | + }, |
| 69 | + { |
| 70 | + "metadata": { |
| 71 | + "ExecuteTime": { |
| 72 | + "end_time": "2025-10-02T03:55:51.653816Z", |
| 73 | + "start_time": "2025-10-02T03:55:51.239719Z" |
| 74 | + } |
| 75 | + }, |
| 76 | + "cell_type": "code", |
| 77 | + "source": [ |
| 78 | + "import com.intellij.ml.llm.agents.ChatAgent\n", |
| 79 | + "import com.intellij.ml.llm.core.chat.messages.ChatMessage\n", |
| 80 | + "import com.intellij.ml.llm.core.chat.messages.UserMessage\n", |
| 81 | + "import com.intellij.ml.llm.core.chat.messages.impl.SimpleCompletableMessage\n", |
| 82 | + "import com.intellij.ml.llm.core.chat.session.ChatKind\n", |
| 83 | + "import com.intellij.ml.llm.core.chat.session.ChatMessageHandler\n", |
| 84 | + "import com.intellij.ml.llm.core.chat.session.ChatSession\n", |
| 85 | + "import com.intellij.ml.llm.smartChat.endpoints.SmartChatEndpoint\n", |
| 86 | + "import com.intellij.openapi.application.ApplicationManager\n", |
| 87 | + "import com.intellij.openapi.extensions.ExtensionPointName\n", |
| 88 | + "import com.intellij.openapi.project.Project\n", |
| 89 | + "\n", |
| 90 | + "class MyChatMessageHandler : ChatMessageHandler {\n", |
| 91 | + " override fun isApplicable(project: Project, kind: ChatKind, userMessage: UserMessage) = true\n", |
| 92 | + "\n", |
| 93 | + " override fun createAnswerMessage(\n", |
| 94 | + " project: Project,\n", |
| 95 | + " chat: ChatSession,\n", |
| 96 | + " userMessage: UserMessage,\n", |
| 97 | + " kind: ChatKind,\n", |
| 98 | + " ) = SimpleCompletableMessage(chat)\n", |
| 99 | + "\n", |
| 100 | + " override suspend fun serveAnswerMessage(\n", |
| 101 | + " project: Project,\n", |
| 102 | + " chat: ChatSession,\n", |
| 103 | + " answerMessage: ChatMessage,\n", |
| 104 | + " smartChatEndpoints: List<SmartChatEndpoint>,\n", |
| 105 | + " ) = ChatAgent.EP_NAME.extensionList.last().serveAnswerMessage(project, chat, answerMessage)\n", |
| 106 | + "}\n", |
| 107 | + "\n", |
| 108 | + "registerExtension(ChatMessageHandler.EP, MyChatMessageHandler())" |
| 109 | + ], |
| 110 | + "outputs": [], |
| 111 | + "execution_count": 6 |
| 112 | + }, |
| 113 | + { |
| 114 | + "metadata": { |
| 115 | + "ExecuteTime": { |
| 116 | + "end_time": "2025-10-02T03:55:51.730786Z", |
| 117 | + "start_time": "2025-10-02T03:55:51.654405Z" |
| 118 | + } |
| 119 | + }, |
| 120 | + "cell_type": "code", |
| 121 | + "source": [ |
| 122 | + "import com.intellij.ml.llm.agents.ChatAgent\n", |
| 123 | + "import com.intellij.ml.llm.core.chat.messages.ChatMessage\n", |
| 124 | + "import com.intellij.ml.llm.core.chat.messages.impl.SimpleCompletableMessage\n", |
| 125 | + "import com.intellij.ml.llm.core.chat.session.ChatSession\n", |
| 126 | + "import com.intellij.ml.llm.privacy.trustedStringBuilders.privacyConst\n", |
| 127 | + "import com.intellij.openapi.application.ApplicationManager\n", |
| 128 | + "import com.intellij.openapi.project.Project\n", |
| 129 | + "\n", |
| 130 | + "class MyChatAgent : ChatAgent {\n", |
| 131 | + "\n", |
| 132 | + " override val id = \"groot\"\n", |
| 133 | + " override val name = \"I am Groot\"\n", |
| 134 | + "\n", |
| 135 | + " override fun createAnswerMessage(\n", |
| 136 | + " project: Project,\n", |
| 137 | + " chat: ChatSession,\n", |
| 138 | + " userMessage: UserMessage,\n", |
| 139 | + " kind: ChatKind\n", |
| 140 | + " ): ChatMessage = SimpleCompletableMessage(chat)\n", |
| 141 | + "\n", |
| 142 | + " override suspend fun serveAnswerMessage(\n", |
| 143 | + " project: Project,\n", |
| 144 | + " chat: ChatSession,\n", |
| 145 | + " answerMessage: ChatMessage,\n", |
| 146 | + " ) {\n", |
| 147 | + " if (answerMessage !is SimpleCompletableMessage) return\n", |
| 148 | + " answerMessage.appendText(\"*I am Groot*\".privacyConst)\n", |
| 149 | + " }\n", |
| 150 | + "}\n", |
| 151 | + "\n", |
| 152 | + "registerExtension(ChatAgent.EP_NAME, MyChatAgent())" |
| 153 | + ], |
| 154 | + "outputs": [ |
| 155 | + { |
| 156 | + "ename": "org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException", |
| 157 | + "evalue": "at Cell In[7], line 9, column 1: Class 'MyChatAgent' is not abstract and does not implement abstract member public abstract val icon: Icon? defined in com.intellij.ml.llm.agents.ChatAgent", |
| 158 | + "output_type": "error", |
| 159 | + "traceback": [ |
| 160 | + "org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException: at Cell In[7], line 9, column 1: Class 'MyChatAgent' is not abstract and does not implement abstract member public abstract val icon: Icon? defined in com.intellij.ml.llm.agents.ChatAgent", |
| 161 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.JupyterCompilerImpl.compileSync(JupyterCompilerImpl.kt:151)", |
| 162 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl.eval(InternalEvaluatorImpl.kt:126)", |
| 163 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute_W38Nk0s$lambda$0$1(CellExecutorImpl.kt:95)", |
| 164 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withHost(ReplForJupyterImpl.kt:730)", |
| 165 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute-W38Nk0s(CellExecutorImpl.kt:93)", |
| 166 | + "\tat org.jetbrains.kotlinx.jupyter.repl.execution.CellExecutor.execute-W38Nk0s$default(CellExecutor.kt:14)", |
| 167 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evaluateUserCode-wNURfNM(ReplForJupyterImpl.kt:591)", |
| 168 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalExImpl(ReplForJupyterImpl.kt:472)", |
| 169 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalEx$lambda$0(ReplForJupyterImpl.kt:466)", |
| 170 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withEvalContext(ReplForJupyterImpl.kt:448)", |
| 171 | + "\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalEx(ReplForJupyterImpl.kt:465)", |
| 172 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.processExecuteRequest$lambda$0$0$0(IdeCompatibleMessageRequestProcessor.kt:161)", |
| 173 | + "\tat org.jetbrains.kotlinx.jupyter.streams.NonBlockingSubstitutionEngine.withDataSubstitution(SubstitutionEngine.kt:124)", |
| 174 | + "\tat org.jetbrains.kotlinx.jupyter.streams.StreamSubstitutionManager.withSubstitutedStreams(StreamSubstitutionManager.kt:118)", |
| 175 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.withForkedIn(IdeCompatibleMessageRequestProcessor.kt:351)", |
| 176 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.evalWithIO$lambda$0$0(IdeCompatibleMessageRequestProcessor.kt:364)", |
| 177 | + "\tat org.jetbrains.kotlinx.jupyter.streams.NonBlockingSubstitutionEngine.withDataSubstitution(SubstitutionEngine.kt:124)", |
| 178 | + "\tat org.jetbrains.kotlinx.jupyter.streams.StreamSubstitutionManager.withSubstitutedStreams(StreamSubstitutionManager.kt:118)", |
| 179 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.withForkedErr(IdeCompatibleMessageRequestProcessor.kt:341)", |
| 180 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.evalWithIO$lambda$0(IdeCompatibleMessageRequestProcessor.kt:363)", |
| 181 | + "\tat org.jetbrains.kotlinx.jupyter.streams.NonBlockingSubstitutionEngine.withDataSubstitution(SubstitutionEngine.kt:124)", |
| 182 | + "\tat org.jetbrains.kotlinx.jupyter.streams.StreamSubstitutionManager.withSubstitutedStreams(StreamSubstitutionManager.kt:118)", |
| 183 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.withForkedOut(IdeCompatibleMessageRequestProcessor.kt:334)", |
| 184 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.evalWithIO(IdeCompatibleMessageRequestProcessor.kt:362)", |
| 185 | + "\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.processExecuteRequest$lambda$0$0(IdeCompatibleMessageRequestProcessor.kt:160)", |
| 186 | + "\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$Task.execute(JupyterExecutorImpl.kt:41)", |
| 187 | + "\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl.executorThread$lambda$0(JupyterExecutorImpl.kt:81)", |
| 188 | + "\tat kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)", |
| 189 | + "" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "execution_count": 7 |
| 194 | + }, |
| 195 | + { |
| 196 | + "metadata": {}, |
| 197 | + "cell_type": "markdown", |
| 198 | + "source": [ |
| 199 | + "To use this newly registered chat agent, click *AI Chat* on the right toolbar to open the AI Assistant chat tool window.\n", |
| 200 | + "\n", |
| 201 | + "In the input field, type your question, for example `Who are you?`. AI Assistant will use the newly registered Chat Agent and provide the reply: `I am Groot`." |
| 202 | + ] |
| 203 | + } |
| 204 | + ], |
| 205 | + "metadata": { |
| 206 | + "kernelspec": { |
| 207 | + "display_name": "Kotlin", |
| 208 | + "language": "kotlin", |
| 209 | + "name": "kotlin" |
| 210 | + }, |
| 211 | + "language_info": { |
| 212 | + "name": "kotlin", |
| 213 | + "version": "1.9.23", |
| 214 | + "mimetype": "text/x-kotlin", |
| 215 | + "file_extension": ".kt", |
| 216 | + "pygments_lexer": "kotlin", |
| 217 | + "codemirror_mode": "text/x-kotlin", |
| 218 | + "nbconvert_exporter": "" |
| 219 | + }, |
| 220 | + "ktnbPluginMetadata": { |
| 221 | + "sessionRunMode": "IDE_PROCESS" |
| 222 | + } |
| 223 | + }, |
| 224 | + "nbformat": 4, |
| 225 | + "nbformat_minor": 0 |
| 226 | +} |
0 commit comments