Skip to content

feat: Add opt-in AI Chat Assistant dock panel (OpenAI-compatible API)#580

Open
Vickyrrrrrr wants to merge 4 commits into
CadQuery:masterfrom
Vickyrrrrrr:master
Open

feat: Add opt-in AI Chat Assistant dock panel (OpenAI-compatible API)#580
Vickyrrrrrr wants to merge 4 commits into
CadQuery:masterfrom
Vickyrrrrrr:master

Conversation

@Vickyrrrrrr
Copy link
Copy Markdown

What this PR does

Adds an AI Chat Assistant as an optional, dockable panel inside CQ-editor.
Users can describe what they want to model in plain English, receive valid
CadQuery code from any OpenAI-compatible LLM, and insert it directly into
the editor — all without leaving the app.

Motivation

CadQuery is powerful but has a steep learning curve for new users. An
in-editor AI assistant lowers that barrier significantly: instead of
looking up API docs, users can describe intent ("make a box with filleted
edges") and get runnable code instantly. Power users benefit too — they
can ask the AI to iterate on an existing model using the current script
as context.

Changes

cq_editor/widgets/ai_chat.py (new file)

  • AIChatWidget — dockable QWidget, integrates like any existing panel
  • Chat history with colour-coded roles (You / AI / Error)
  • Context injection: current editor script is prepended to every prompt
    so the LLM edits the existing model rather than starting from scratch
  • Async LLM calls via QThread — UI never freezes
  • Insert & Run button: extracts code from reply, pushes to editor,
    optionally triggers an immediate re-render
  • preferences Parameter tree: Enabled, Provider URL, Model, API Key,
    Auto-run — appears under Edit → Preferences → AI Assistant

cq_editor/main_window.py (minimal, additive changes only)

  • Register AIChatWidget as a dock panel (right side, default)
  • Wire insert_code signal → editor.set_text
  • Add AI Assistant toggle under Tools menu

How to use

  1. pip install openai
  2. Edit → Preferences → AI Assistant → enter API Key + Model
  3. Tools → AI Assistant
  4. Type a prompt, click Insert & Run

Works with OpenAI, Anthropic (via OpenRouter), local Ollama, or any
OpenAI-compatible endpoint via the Provider URL field.

Compatibility

  • openai is optional — guarded by try/except ImportError; CQ-editor
    starts normally without it
  • Zero changes to existing behaviour or existing widgets
  • All new logic is self-contained in ai_chat.py

Testing

  • CQ-editor launches normally with and without openai installed
  • Panel docks, undocks, and toggles via View menu
  • Preferences save and restore across sessions
  • Context injection sends current script to LLM
  • Insert & Run loads code into editor and triggers re-render
  • Tested with OpenAI (gpt-4o, o3) and OpenRouter

Future work (follow-up PRs)

  • Streaming token-by-token response display
  • Diff-based code patching instead of full replace
  • Auto-fix: feed traceback back to LLM on render error

…port

- Add cq_editor/widgets/ai_chat.py: new QDockWidget with chat UI,
  async LLM calls via QThread, context injection (sends current editor
  code to LLM), and one-click code insertion + auto-run
- Edit cq_editor/main_window.py: register AIChatWidget as a dockable
  panel, wire insert_code signal to editor, add View menu toggle
- Edit cq_editor/preferences.py: add AI Settings preference group
  (provider, model, API key, base URL, enabled toggle)

All AI features are optional and guarded; existing behavior unchanged.
feat: Add AI Chat Assistant dock panel (OpenAI-compatible, opt-in)
docs: update README with AI Chat Assistant feature and usage guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant