feat: AI-assisted compilation and code generation

- New src/ai_assist.nim module with DeepSeek/OpenAI/MiMo API support
- AI explains compiler errors automatically when compilation fails
- New CLI command: cljnim ai '<description>' for code generation
- REPL :ai command for interactive AI assistance
- API keys read from environment vars (DEEPSEEK_API_KEY, OPENAI_API_KEY, MIMO_API_KEY)
- Tests for prompt building and response formatting
- Updated README with AI integration docs
This commit is contained in:
2026-05-08 22:21:27 +03:00
parent 6bed638300
commit 6249d8a251
7 changed files with 382 additions and 32 deletions
+72 -10
View File
@@ -103,6 +103,58 @@ $ ./cljnim repl --json
See [`docs/AI_FIRST.md`](docs/AI_FIRST.md) for the full AI integration design.
## AI-Powered Assistance
Clojure/Nim can use paid AI APIs (DeepSeek, OpenAI, Xiaomi MiMo) to help you write and debug code.
### Setup
Set one of these environment variables:
```bash
# DeepSeek (recommended, fast & cheap)
export DEEPSEEK_API_KEY="sk-..."
# OpenAI / OpenRouter
export OPENAI_API_KEY="sk-..."
# Xiaomi MiMo
export MIMO_API_KEY="..."
```
### Features
**Auto-error explanation** — When compilation fails, the compiler automatically asks AI for a fix:
```bash
$ ./cljnim run broken.clj
Compilation failed
Error: type mismatch: got <int> but expected <string>
💡 AI Suggestion:
The function `greet` expects a string but you passed an integer.
Fix: (defn greet [name] (str "Hello " name))
```
**REPL AI command** — Generate code interactively:
```bash
$ ./cljnim repl
user> :ai function that calculates fibonacci using loop/recur
🤖 Thinking...
💡 AI Suggestion:
(defn fib [n]
(loop [a 0 b 1 i n]
(if (= i 0)
a
(recur b (+ a b) (dec i)))))
```
**CLI code generation** — Generate code from terminal:
```bash
$ ./cljnim ai "function that reads a file line by line"
(defn read-lines [filename]
(let [content (slurp filename)]
(clojure.string/split-lines content)))
```
## Architecture
```
@@ -131,14 +183,25 @@ See [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) for details.
│ ├── reader.nim # Clojure reader / EDN parser
│ ├── emitter.nim # Nim code generator
│ ├── repl.nim # Human & AI REPL
│ ├── eval.nim # Tree-walking interpreter
│ ├── deps.nim # Dependency resolution
│ ├── core.nim # Core runtime functions
│ ├── types.nim # Clojure value types
│ ├── macros.nim # Macro expansion engine
│ └── runtime.nim # Runtime implementation
├── lib/
── cljnim_runtime.nim # Clojure runtime in Nim
── cljnim_runtime.nim # Clojure runtime in Nim
│ ├── cljnim_pvec.nim # Persistent Vector (HAMT)
│ ├── cljnim_pmap.nim # Persistent Map (HAMT)
│ └── cljnim_async.nim # core.async channels
├── examples/
│ ├── hello.clj
── math.clj
── math.clj
│ ├── core.clj
│ ├── macros.clj
│ ├── ffi.clj
│ ├── interop.clj
│ └── ai_tools.clj
├── docs/ # Documentation (EN + BG)
├── tests/
├── Makefile
@@ -148,19 +211,18 @@ See [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) for details.
## Supported Features
### Working Now ✅
- Reader: numbers, strings, symbols, keywords, lists `()`, vectors `[]`
- Reader: numbers, strings, symbols, keywords, lists `()`, vectors `[]`, maps `{}`
- Special forms: `def`, `defn`, `fn`, `let`, `if`, `do`, `quote`
- Arithmetic: `+`, `-`, `*`, `/`, `=`, `<`, `>`
- Functions: `println`
- Functions: `println`, `map`, `filter`, `reduce`
- Human REPL and JSON REPL
- AOT compilation to native binaries
### In Progress 🔄
- Persistent data structures (Vector, Map, Set)
- `defmacro` system
- Persistent data structures (Vector, Map, Set) via HAMT
- Macro system (`defmacro`, `->`, `->>`, `and`, `or`, `when`, `cond`)
- Nim/C interop
- File operations from REPL
- Git operations from REPL
- File and Git operations from REPL
- Tree-walking interpreter for fast REPL eval
- Atoms, Agents, and core.async channels
See [`docs/ROADMAP.md`](docs/ROADMAP.md) for the full roadmap.