Created
June 11, 2025 05:08
-
-
Save ileasile/78ec987724afd47ff804650bfa720d99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells" : [ { | |
"metadata" : { | |
"collapsed" : true, | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:29.932236Z", | |
"start_time" : "2025-06-11T05:07:29.913714Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "// %use @/Users/hsz/Projects/JetBrains/kotlin-jupyter-intellij-sdk/IntelliJ Platform.json\n", "\n", "// No worries, soon it'll be just `%use intellij-platform`" ], | |
"id" : "95808d414bd84074", | |
"outputs" : [ ], | |
"execution_count" : 1 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:35.826022Z", | |
"start_time" : "2025-06-11T05:07:29.935769Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "USE {\n", " // repositories(\"https://packages.jetbrains.team/maven/p/kds/kotlin-ds-maven\")\n", " dependencies(\"org.jetbrains.kotlinx:kotlin-jupyter-intellij-platform:0.0.2-5-SNAPSHOT\")\n", "}" ], | |
"id" : "c3eefe9a4c176e21", | |
"outputs" : [ ], | |
"execution_count" : 2 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:36.863896Z", | |
"start_time" : "2025-06-11T05:07:35.829058Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "val plugin = loadPlugin(\n", " \"com.intellij.ml.llm\",\n", " loadClasses = true,\n", " loadClassLoader = false,\n", ")" ], | |
"id" : "491a971a78dc8b45", | |
"outputs" : [ ], | |
"execution_count" : 3 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:36.979490Z", | |
"start_time" : "2025-06-11T05:07:36.866082Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "fun <T> topologicalMergeSortedAndNew(\n", " sorted: List<T>,\n", " new: List<T>,\n", " getParents: (T) -> List<T>\n", "): List<T> {\n", " val allElements = (sorted + new).distinct()\n", " val dependencyGraph = mutableMapOf<T, MutableList<T>>() // children -> parent\n", " val inDegree = mutableMapOf<T, Int>().withDefault { 0 }\n", " // Initialize inDegree and graph\n", " for (element in allElements) {\n", " val parents = getParents(element)\n", " for (parent in parents) {\n", " if (parent !in allElements) continue // Skip missing parents\n", " dependencyGraph.getOrPut(element) { mutableListOf() }.add(parent)\n", " inDegree.compute(parent) { _, v -> (v ?: 0) + 1 }\n", " }\n", " inDegree.putIfAbsent(element, 0)\n", " }\n", " // Kahn's algorithm\n", " val queue = ArrayDeque(allElements.filter { inDegree.getValue(it) == 0 })\n", " val result = mutableListOf<T>()\n", " while (queue.isNotEmpty()) {\n", " val node = queue.removeFirst()\n", " result.add(node)\n", " for (parent in dependencyGraph[node].orEmpty()) {\n", " val deg = inDegree.getValue(parent) - 1\n", " inDegree[parent] = deg\n", " if (deg == 0) queue.add(parent)\n", " }\n", " }\n", " // Optional: Detect cycles\n", " if (result.size < allElements.size) {\n", " error(\"Cycle detected or unresolved dependencies\")\n", " }\n", " return result\n", "}" ], | |
"id" : "c558c529bd2e9bb8", | |
"outputs" : [ ], | |
"execution_count" : 4 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.029063Z", | |
"start_time" : "2025-06-11T05:07:36.981033Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import com.intellij.ide.plugins.cl.PluginClassLoader\n", "import com.intellij.ide.plugins.cl.ResolveScopeManager\n", "\n", "val resolveScopeField = PluginClassLoader::class.java.getDeclaredField(\"_resolveScopeManager\").apply {\n", " isAccessible = true\n", "}\n", "\n", "fun PluginClassLoader.calculateConsistency(name: String, force: Boolean = false): String? {\n", " return this.packagePrefix?.let {\n", " (resolveScopeField.get(this) as ResolveScopeManager)\n", " .isDefinitelyAlienClass(name = name, packagePrefix = it, force = force)\n", " }\n", "}" ], | |
"id" : "b0a7b43146f407d8", | |
"outputs" : [ ], | |
"execution_count" : 5 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.142053Z", | |
"start_time" : "2025-06-11T05:07:37.030998Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import com.intellij.ide.plugins.cl.PluginClassLoader\n", "import org.jetbrains.kotlinx.jupyter.util.ModifiableParentsClassLoader\n", "import java.io.IOException\n", "import java.net.URL\n", "import java.util.Collections\n", "import java.util.Enumeration\n", "\n", "class IntelliJPlatformClassloader : ModifiableParentsClassLoader() {\n", "\n", " val allParents = mutableListOf<ClassLoader>()\n", "\n", " override fun addParent(parent: ClassLoader) {\n", " addParents(listOf(parent))\n", " }\n", "\n", " private fun getParents(classLoader: ClassLoader): List<ClassLoader> {\n", " return when {\n", " classLoader is PluginClassLoader -> classLoader.getAllParentsClassLoaders().toList()\n", " else -> generateSequence(classLoader.parent) { it.parent }.toList()\n", " }\n", " }\n", "\n", "\n", " fun addParents(parents: List<ClassLoader>) {\n", " val newParents = parents // collectWithParents(parents, ::getParents)\n", " val sorted = topologicalMergeSortedAndNew(allParents, newParents, ::getParents)\n", "\n", " allParents.clear()\n", " allParents.addAll(sorted)\n", " }\n", "\n", " override fun loadClass(name: String, resolve: Boolean): Class<*> {\n", " // First, check if a class is already loaded\n", " val loaded = findLoadedClass(name)\n", " if (loaded != null) {\n", " if (resolve) resolveClass(loaded)\n", " return loaded\n", " }\n", "\n", " // Attempt to load a class from each parent in order\n", " for (parent in allParents) {\n", " try {\n", " val clazz = if (parent is PluginClassLoader) {\n", " if (parent.calculateConsistency(name) != null) {\n", " continue\n", " }\n", " parent.loadClassInsideSelf(name)\n", " } else {\n", " parent.loadClass(name)\n", " }\n", " if (clazz == null) continue\n", " if (resolve) resolveClass(clazz)\n", " return clazz\n", " } catch (_: ClassNotFoundException) {\n", " // Try next\n", " }\n", " }\n", "\n", " // Class isn't found in any parent\n", " throw ClassNotFoundException(\"Class $name not found in any delegate classloader\")\n", " }\n", "\n", " // Optional: restrict resources the same way\n", " override fun getResource(name: String): URL? {\n", " for (parent in allParents) {\n", " val resource = parent.getResource(name)\n", " if (resource != null) return resource\n", " }\n", " return null\n", " }\n", "\n", " override fun getResources(name: String): Enumeration<URL?>? {\n", " val resources = allParents.flatMap {\n", " try {\n", " it.getResources(name).toList()\n", " } catch (_: IOException) {\n", " emptyList()\n", " }\n", " }\n", " return Collections.enumeration(resources)\n", " }\n", "}" ], | |
"id" : "814fef9a50d81c6", | |
"outputs" : [ ], | |
"execution_count" : 6 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.159419Z", | |
"start_time" : "2025-06-11T05:07:37.144632Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : "val intelliJPlatformClassLoader = IntelliJPlatformClassloader()", | |
"id" : "35815d124d6efd43", | |
"outputs" : [ ], | |
"execution_count" : 7 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.177663Z", | |
"start_time" : "2025-06-11T05:07:37.160843Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import org.jetbrains.kotlinx.jupyter.util.ModifiableParentsClassLoader\n", "\n", "val base = notebook.intermediateClassLoader as ModifiableParentsClassLoader\n", "base.addParent(intelliJPlatformClassLoader)" ], | |
"id" : "134b4da3b47717f8", | |
"outputs" : [ ], | |
"execution_count" : 8 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.232048Z", | |
"start_time" : "2025-06-11T05:07:37.183998Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "val pluginClassloaders = plugin.content.modules\n", " .map { it.descriptor.classLoader } + listOf(plugin.classLoader)" ], | |
"id" : "c0bbdf4edd6f242f", | |
"outputs" : [ ], | |
"execution_count" : 9 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.290129Z", | |
"start_time" : "2025-06-11T05:07:37.259478Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "\n", "//classloaders.forEach { intelliJPlatformClassLoader.addParent(it) }\n", "intelliJPlatformClassLoader.addParents(pluginClassloaders)\n", "\n", "// classloaders.forEach { base.addParent(it) }" ], | |
"id" : "6aec7107e41b4f7f", | |
"outputs" : [ ], | |
"execution_count" : 10 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.324531Z", | |
"start_time" : "2025-06-11T05:07:37.297145Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import com.intellij.openapi.application.ApplicationManager\n", "import com.intellij.ml.llm.agents.ChatAgent\n", "\n", "ApplicationManager.getApplication()\n", " .extensionArea\n", " .getExtensionPoint<ChatAgent>(\"com.intellij.ml.llm.agents.chatAgent\")\n", " .extensions" ], | |
"id" : "cb4ca0db15349642", | |
"outputs" : [ { | |
"data" : { | |
"text/plain" : [ "[com.intellij.ml.llm.agents.wrench.WrenchChatAgent@5541a112]" ] | |
}, | |
"execution_count" : 11, | |
"metadata" : { }, | |
"output_type" : "execute_result" | |
} ], | |
"execution_count" : 11 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-06-11T05:07:37.352268Z", | |
"start_time" : "2025-06-11T05:07:37.330482Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : "ChatAgent::class.java.classLoader", | |
"id" : "563003f7164eefdd", | |
"outputs" : [ { | |
"data" : { | |
"text/plain" : [ "PluginClassLoader(plugin=ContentModuleDescriptor(moduleName=intellij.ml.llm.agents, package=com.intellij.ml.llm.agents) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), packagePrefix=com.intellij.ml.llm.agents., state=active, parents=ContentModuleDescriptor(moduleName=intellij.ml.llm.tokenizer, package=com.intellij.ml.llm.tokenizer) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), ContentModuleDescriptor(moduleName=intellij.ml.llm.tasks, package=com.intellij.ml.llm.tasks) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), ContentModuleDescriptor(moduleName=intellij.ml.llm.libraries.grazie, package=com.intellij.ml.llm.libraries.grazie) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), ContentModuleDescriptor(moduleName=intellij.ml.llm.context, package=com.intellij.ml.llm.context) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), ContentModuleDescriptor(moduleName=intellij.ml.llm.core, package=com.intellij.ml.llm) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), ContentModuleDescriptor(moduleName=intellij.ml.llm.privacy.shared) <- PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), PluginMainDescriptor(name=JetBrains AI Assistant, id=com.intellij.ml.llm, version=252.21982.0, isBundled=false, path=~/Library/Application Support/JetBrains/IntelliJIdea2025.2/plugins/ml-llm), )" ] | |
}, | |
"execution_count" : 12, | |
"metadata" : { }, | |
"output_type" : "execute_result" | |
} ], | |
"execution_count" : 12 | |
} ], | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment