feat: Add opt-in AI Chat Assistant dock panel (OpenAI-compatible API)#580
feat: Add opt-in AI Chat Assistant dock panel (OpenAI-compatible API)#580Vickyrrrrrr wants to merge 9 commits into
Conversation
…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
|
Claude Code Review Correctness / Bugs
Safety / Privacy
Resource / Thread Safety
Architecture
Missing
|
|
Thanks , agreed on the main points. I’ll fix the enabled toggle, remove the unsafe raw-reply fallback, move the regex import, make QAction creation explicit, and clean up dock visibility handling. |
Correctness / Bugs: - Wire Enabled preference to actually show/hide the dock panel via preferences.sigTreeStateChanged; panel starts hidden when Enabled=False - Fix _toggle_ai_panel: remove raise_() from the hide branch (it re-showed the widget immediately after hiding it) - Remove dangerous bare-code fallback in _on_response: if no fenced code block is found, show an informational message and disable Insert button instead of pasting raw LLM text into the editor - Move `import re` to module level (was inside _extract_code staticmethod) - Fix QAction construction: set toolTip via explicit .setToolTip() call after construction for full PyQt5 compatibility Safety / Privacy: - Replace plaintext API key in preferences with system keyring storage (keyring package, falls back to plaintext with a warning if unavailable) - Add first-use privacy consent dialog: warns user that their script is sent to the configured API endpoint; stores consent in preferences so dialog only shows once - Update API Key tooltip to accurately state storage mechanism Resource / Thread Safety: - Add closeEvent to AIChatWidget that calls _thread.quit()/_thread.wait() so in-flight requests are cleanly stopped when the app closes - Bound conversation history: keep at most MAX_HISTORY_TURNS (10) turns; older messages are pruned (system prompt always retained) to prevent unbounded token growth Architecture: - Pass editor and debugger properly via set_dependencies() method instead of post-construction attribute assignment; main_window.py updated to call set_dependencies() in prepare_actions() - Move preferences from class-level to instance-level Parameter to avoid shared-state bugs if multiple instances are ever created - Make AIChatWidget import conditional in main_window.py with try/except so the claim of optional dependency is accurate end-to-end
Feature/ai chat assistant
…, and add model-specific configuration support.


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— dockableQWidget, integrates like any existing panelso the LLM edits the existing model rather than starting from scratch
QThread— UI never freezesoptionally triggers an immediate re-render
preferencesParameter tree: Enabled, Provider URL, Model, API Key,Auto-run — appears under Edit → Preferences → AI Assistant
cq_editor/main_window.py(minimal, additive changes only)AIChatWidgetas a dock panel (right side, default)insert_codesignal →editor.set_textHow to use
pip install openaiWorks with OpenAI, Anthropic (via OpenRouter), local Ollama, or any
OpenAI-compatible endpoint via the Provider URL field.
Compatibility
openaiis optional — guarded bytry/except ImportError; CQ-editorstarts normally without it
ai_chat.pyTesting
openaiinstalledgpt-4o,o3) and OpenRouterFuture work (follow-up PRs)