Computing & Chips

JetBrains Open-Sources KotlinLLM for Runtime Code Generation

By Interestana AI EditorialAI summary of reporting by MarkTechPostHow we report

MarkTechPost3 min read

JetBrains Research has open-sourced KotlinLLM, an IntelliJ IDEA plugin designed for Kotlin/JVM projects that introduces a new language feature termed "Smart macros."

These Smart macros are essentially standard Kotlin function calls whose implementation is dynamically generated Kotlin code. The public API for KotlinLLM is intentionally kept minimal, featuring two primary functions: `asLlm<F, T>(from, hint)` and `mockLlm<T>()`.

The `asLlm` function facilitates the conversion of an input value of type `F` into a typed value of type `T`, supporting various Kotlin data structures such as data classes, enums, lists, and primitive types.

The `mockLlm` function, on the other hand, generates a stateful implementation for an interface `T`, where its behavior is contingent on the methods invoked on it. An illustrative example provided shows how `asLlm` can be used to fetch a GitHub API URL based on repository input and a hint, and subsequently to parse an API response into a list of `Issue` objects, specifically filtering for beginner-friendly issues.

The core functionality of KotlinLLM revolves around a runtime loop initiated when a project is launched using the KotlinLLM run configuration. During this process, the plugin identifies `asLlm` and `mockLlm` calls within the project. It then proceeds to update generated bootstrap, provider, parser, and mock files.

The plugin launches the run configuration under the Java Debug Interface (JDI) and strategically places breakpoints on generated "regenerate hooks." If the generated code logic fails to align with a runtime scenario, execution halts at one of these hooks. At this point, the plugin captures runtime values and type information from the suspended execution frame.

This captured data is then sent to an LLM agent, which generates an updated code snippet. The plugin compiles this new code and redefines the loaded class in memory before retrying the original call. KotlinLLM's specific targeting of Kotlin/JVM is attributed to its reliance on JVM class redefinition via JDI for its runtime evolution loop.

The plugin's development process includes an explainer that details the Smart macro API, animates the nine-step runtime loop, and models how specific scenarios can reduce inference calls. Initial reported results on an adapted Spring Petclinic Kotlin project, which contained 18 `asLlm` call sites, demonstrated that all 24 application scenarios completed successfully after the Smart macro evolution process, indicating the effectiveness of the dynamic code generation and hot-reloading capabilities.

Sources and references

Original report

JetBrains Open-Sources KotlinLLM: Smart Macros That Generate Kotlin Source Code at Runtime and Hot-Reload It Through JDI

MarkTechPost

Read on MarkTechPost (opens in a new tab)

Ref 00UDTMV2

Share this story

Follow this story

Get Computing & Chips in your inbox — new stories in 40 words or fewer, once a week.

Subscribe

Read next