Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions examples/ai_assistant/chat_handler.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": [
"# JetBrains AI Assistant Chat Handler\n",
"\n",
"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."
]
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-10-02T04:22:19.265561Z",
"start_time": "2025-10-02T04:22:17.834847Z"
},
"executionRelatedData": {
"compiledClasses": [
"Line_1_jupyter",
"Line_2_jupyter"
]
}
},
"cell_type": "code",
"source": "%use intellij-platform",
"outputs": [
{
"data": {
"text/plain": [
"IntelliJ Platform integration is loaded"
]
},
"metadata": {},
"output_type": "display_data",
"jetTransient": {
"display_id": null
}
}
],
"execution_count": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Load the JetBrains AI Assistant bundled plugin using its `com.intellij.ml.llm` ID."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-10-02T04:22:22.365779Z",
"start_time": "2025-10-02T04:22:21.587057Z"
},
"executionRelatedData": {
"compiledClasses": [
"Line_4_jupyter",
"Line_5_jupyter",
"Line_6_jupyter",
"Line_7_jupyter",
"Line_8_jupyter"
]
}
},
"cell_type": "code",
"source": "loadPlugins(\"com.intellij.ml.llm\")",
"outputs": [],
"execution_count": 2
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-10-02T03:55:51.653816Z",
"start_time": "2025-10-02T03:55:51.239719Z"
}
},
"cell_type": "code",
"source": [
"import com.intellij.ml.llm.agents.ChatAgent\n",
"import com.intellij.ml.llm.core.chat.messages.ChatMessage\n",
"import com.intellij.ml.llm.core.chat.messages.UserMessage\n",
"import com.intellij.ml.llm.core.chat.messages.impl.SimpleCompletableMessage\n",
"import com.intellij.ml.llm.core.chat.session.ChatKind\n",
"import com.intellij.ml.llm.core.chat.session.ChatMessageHandler\n",
"import com.intellij.ml.llm.core.chat.session.ChatSession\n",
"import com.intellij.ml.llm.smartChat.endpoints.SmartChatEndpoint\n",
"import com.intellij.openapi.application.ApplicationManager\n",
"import com.intellij.openapi.extensions.ExtensionPointName\n",
"import com.intellij.openapi.project.Project\n",
"\n",
"class MyChatMessageHandler : ChatMessageHandler {\n",
" override fun isApplicable(project: Project, kind: ChatKind, userMessage: UserMessage) = true\n",
"\n",
" override fun createAnswerMessage(\n",
" project: Project,\n",
" chat: ChatSession,\n",
" userMessage: UserMessage,\n",
" kind: ChatKind,\n",
" ) = SimpleCompletableMessage(chat)\n",
"\n",
" override suspend fun serveAnswerMessage(\n",
" project: Project,\n",
" chat: ChatSession,\n",
" answerMessage: ChatMessage,\n",
" smartChatEndpoints: List<SmartChatEndpoint>,\n",
" ) = ChatAgent.EP_NAME.extensionList.last().serveAnswerMessage(project, chat, answerMessage)\n",
"}\n",
"\n",
"registerExtension(ChatMessageHandler.EP, MyChatMessageHandler())"
],
"outputs": [],
"execution_count": 6
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-10-02T03:55:51.730786Z",
"start_time": "2025-10-02T03:55:51.654405Z"
}
},
"cell_type": "code",
"source": [
"import com.intellij.ml.llm.agents.ChatAgent\n",
"import com.intellij.ml.llm.core.chat.messages.ChatMessage\n",
"import com.intellij.ml.llm.core.chat.messages.impl.SimpleCompletableMessage\n",
"import com.intellij.ml.llm.core.chat.session.ChatSession\n",
"import com.intellij.ml.llm.privacy.trustedStringBuilders.privacyConst\n",
"import com.intellij.openapi.application.ApplicationManager\n",
"import com.intellij.openapi.project.Project\n",
"\n",
"class MyChatAgent : ChatAgent {\n",
"\n",
" override val id = \"groot\"\n",
" override val name = \"I am Groot\"\n",
"\n",
" override fun createAnswerMessage(\n",
" project: Project,\n",
" chat: ChatSession,\n",
" userMessage: UserMessage,\n",
" kind: ChatKind\n",
" ): ChatMessage = SimpleCompletableMessage(chat)\n",
"\n",
" override suspend fun serveAnswerMessage(\n",
" project: Project,\n",
" chat: ChatSession,\n",
" answerMessage: ChatMessage,\n",
" ) {\n",
" if (answerMessage !is SimpleCompletableMessage) return\n",
" answerMessage.appendText(\"*I am Groot*\".privacyConst)\n",
" }\n",
"}\n",
"\n",
"registerExtension(ChatAgent.EP_NAME, MyChatAgent())"
],
"outputs": [
{
"ename": "org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException",
"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",
"output_type": "error",
"traceback": [
"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",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.JupyterCompilerImpl.compileSync(JupyterCompilerImpl.kt:151)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl.eval(InternalEvaluatorImpl.kt:126)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute_W38Nk0s$lambda$0$1(CellExecutorImpl.kt:95)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withHost(ReplForJupyterImpl.kt:730)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute-W38Nk0s(CellExecutorImpl.kt:93)",
"\tat org.jetbrains.kotlinx.jupyter.repl.execution.CellExecutor.execute-W38Nk0s$default(CellExecutor.kt:14)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evaluateUserCode-wNURfNM(ReplForJupyterImpl.kt:591)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalExImpl(ReplForJupyterImpl.kt:472)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalEx$lambda$0(ReplForJupyterImpl.kt:466)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withEvalContext(ReplForJupyterImpl.kt:448)",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalEx(ReplForJupyterImpl.kt:465)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.processExecuteRequest$lambda$0$0$0(IdeCompatibleMessageRequestProcessor.kt:161)",
"\tat org.jetbrains.kotlinx.jupyter.streams.NonBlockingSubstitutionEngine.withDataSubstitution(SubstitutionEngine.kt:124)",
"\tat org.jetbrains.kotlinx.jupyter.streams.StreamSubstitutionManager.withSubstitutedStreams(StreamSubstitutionManager.kt:118)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.withForkedIn(IdeCompatibleMessageRequestProcessor.kt:351)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.evalWithIO$lambda$0$0(IdeCompatibleMessageRequestProcessor.kt:364)",
"\tat org.jetbrains.kotlinx.jupyter.streams.NonBlockingSubstitutionEngine.withDataSubstitution(SubstitutionEngine.kt:124)",
"\tat org.jetbrains.kotlinx.jupyter.streams.StreamSubstitutionManager.withSubstitutedStreams(StreamSubstitutionManager.kt:118)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.withForkedErr(IdeCompatibleMessageRequestProcessor.kt:341)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.evalWithIO$lambda$0(IdeCompatibleMessageRequestProcessor.kt:363)",
"\tat org.jetbrains.kotlinx.jupyter.streams.NonBlockingSubstitutionEngine.withDataSubstitution(SubstitutionEngine.kt:124)",
"\tat org.jetbrains.kotlinx.jupyter.streams.StreamSubstitutionManager.withSubstitutedStreams(StreamSubstitutionManager.kt:118)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.withForkedOut(IdeCompatibleMessageRequestProcessor.kt:334)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.evalWithIO(IdeCompatibleMessageRequestProcessor.kt:362)",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor.processExecuteRequest$lambda$0$0(IdeCompatibleMessageRequestProcessor.kt:160)",
"\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$Task.execute(JupyterExecutorImpl.kt:41)",
"\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl.executorThread$lambda$0(JupyterExecutorImpl.kt:81)",
"\tat kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)",
""
]
}
],
"execution_count": 7
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"To use this newly registered chat agent, click *AI Chat* on the right toolbar to open the AI Assistant chat tool window.\n",
"\n",
"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`."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"name": "kotlin",
"version": "1.9.23",
"mimetype": "text/x-kotlin",
"file_extension": ".kt",
"pygments_lexer": "kotlin",
"codemirror_mode": "text/x-kotlin",
"nbconvert_exporter": ""
},
"ktnbPluginMetadata": {
"sessionRunMode": "IDE_PROCESS"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading