Skip to content

Commit 8fe8139

Browse files
committed
feat: Implement Duck Brain service with WebLLM integration and File System Access API
- Added DuckBrainService for managing WebLLM engine with WebGPU support. - Implemented Web Worker for off-main-thread processing of WebLLM. - Created File System Access API service for persistent folder access and file management. - Developed Brain page UI for selecting AI providers, managing API keys, and loading models. - Integrated cache management for IndexedDB and Cache API. - Added type declarations for File System Access API.
1 parent 6359ae1 commit 8fe8139

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+6505
-163
lines changed

bun.lock

Lines changed: 296 additions & 81 deletions
Large diffs are not rendered by default.

docs/.vitepress/config.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export default defineConfig({
3838
text: 'Getting Started',
3939
items: [
4040
{ text: 'Quick Start', link: '/getting-started' },
41+
{ text: 'Duck Brain AI', link: '/duck-brain' },
42+
{ text: 'Folder Access', link: '/folder-access' },
4143
{ text: 'Charts', link: '/charts' },
4244
{ text: 'Environment Variables', link: '/environment-variables' },
4345
{ text: 'Troubleshooting', link: '/troubleshooting' }

docs/bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"lockfileVersion": 1,
3+
"configVersion": 0,
34
"workspaces": {
45
"": {
56
"name": "ch-ui-docs",

docs/duck-brain.md

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
# Duck Brain AI Assistant
2+
3+
Duck Brain is an AI-powered assistant that helps you write SQL queries using natural language. It understands your database schema and generates accurate SQL tailored to your data.
4+
5+
## Overview
6+
7+
Duck Brain offers two ways to use AI:
8+
9+
1. **Local AI (WebLLM)** - Runs entirely in your browser using WebGPU. Your data never leaves your machine.
10+
2. **Cloud AI** - Connect to OpenAI (GPT-4), Anthropic (Claude), or Google (Gemini) for more powerful models.
11+
12+
::: tip Privacy First
13+
With local AI, all processing happens in your browser. Your queries, data, and schema never leave your machine.
14+
:::
15+
16+
## Browser Requirements
17+
18+
### For Local AI (WebLLM)
19+
20+
Local AI requires **WebGPU** support:
21+
22+
| Browser | Version | Support |
23+
|---------|---------|---------|
24+
| Chrome | 113+ | ✅ Full support |
25+
| Edge | 113+ | ✅ Full support |
26+
| Firefox | - | ❌ Not supported |
27+
| Safari | - | ❌ Not supported |
28+
29+
::: warning WebGPU Required
30+
WebGPU is different from WebGL. Chrome/Edge 113+ have WebGPU enabled by default. Check `chrome://gpu` for WebGPU status.
31+
:::
32+
33+
### For Cloud AI
34+
35+
Cloud AI works in any modern browser - no special requirements.
36+
37+
## Getting Started
38+
39+
### Opening Duck Brain
40+
41+
1. Click the **Brain icon** in the bottom-right corner of the SQL editor
42+
2. The Duck Brain panel opens on the right side
43+
44+
### Using Local AI
45+
46+
1. On first use, click **Load AI Model**
47+
2. Wait for the model to download (~2GB for Phi-3.5 Mini)
48+
3. Once loaded, the model is cached for future sessions
49+
4. Type your question and press Enter
50+
51+
### Using Cloud AI
52+
53+
1. Go to **Settings** (gear icon in header)
54+
2. Navigate to the **Duck Brain** section
55+
3. Enter your API key for OpenAI, Anthropic, or Gemini
56+
4. Select your preferred model
57+
5. Return to Duck Brain and start chatting
58+
59+
## Available Models
60+
61+
### Local Models (WebLLM)
62+
63+
| Model | Size | Best For |
64+
|-------|------|----------|
65+
| **Phi-3.5 Mini** | ~2.3GB | Best quality, recommended for most users |
66+
| Llama 3.2 1B | ~1.1GB | Fastest, good for quick queries |
67+
| Qwen 2.5 1.5B | ~1GB | Good balance of size and capability |
68+
69+
### Cloud Models
70+
71+
**OpenAI:**
72+
- GPT-4o - Most capable
73+
- GPT-4o Mini - Fast and affordable
74+
- GPT-4 Turbo - GPT-4 with vision
75+
- GPT-3.5 Turbo - Fast, simple tasks
76+
77+
**Anthropic:**
78+
- Claude Sonnet 4 - Best balance
79+
- Claude 3.5 Sonnet - Fast and capable
80+
- Claude 3.5 Haiku - Fastest, affordable
81+
82+
**Google:**
83+
- Gemini 1.5 Pro - Most capable
84+
- Gemini 1.5 Flash - Fast and efficient
85+
86+
## Using Duck Brain
87+
88+
### Asking Questions
89+
90+
Duck Brain understands natural language. Just describe what you want:
91+
92+
```
93+
Show me the top 10 customers by total sales
94+
```
95+
96+
```
97+
Find all orders from last month where the amount is over $1000
98+
```
99+
100+
```
101+
What's the average order value by product category?
102+
```
103+
104+
### Referencing Tables with @
105+
106+
Use `@` to reference specific tables or columns:
107+
108+
```
109+
Show me all rows from @customers where status is active
110+
```
111+
112+
```
113+
Join @orders with @products and show the top sellers
114+
```
115+
116+
When you type `@`, an autocomplete menu shows available tables and columns.
117+
118+
### Schema Awareness
119+
120+
Duck Brain automatically knows your database schema:
121+
- Table names and their columns
122+
- Column types (VARCHAR, INTEGER, etc.)
123+
- Row counts for each table
124+
125+
This context helps generate accurate SQL for your specific data.
126+
127+
### Running Generated SQL
128+
129+
1. Duck Brain generates SQL and shows it in a code block
130+
2. Click **Execute** to run the query immediately
131+
3. Click **Insert** to add the SQL to your editor
132+
4. Results appear inline in the chat
133+
134+
### Switching Providers
135+
136+
If you have multiple AI providers configured:
137+
138+
1. Look for the provider selector in the Duck Brain header
139+
2. Click to switch between local and cloud models
140+
3. Your choice is remembered for the session
141+
142+
## How It Works
143+
144+
### Local AI Architecture
145+
146+
```
147+
User Query → Duck Brain → WebLLM Engine → WebGPU → GPU
148+
149+
Schema Context
150+
151+
SQL Generation
152+
153+
Response
154+
```
155+
156+
1. Your question is combined with database schema context
157+
2. The local LLM (running via WebLLM) generates SQL
158+
3. All processing happens on your GPU via WebGPU
159+
4. No data ever leaves your browser
160+
161+
### Cloud AI Flow
162+
163+
```
164+
User Query + Schema → API Request → Cloud Provider → Response
165+
```
166+
167+
1. Your question and schema summary are sent to the cloud API
168+
2. The cloud model generates SQL
169+
3. Response is streamed back to your browser
170+
171+
::: warning Cloud Privacy
172+
When using cloud AI, your query and a summary of your schema are sent to the provider. Actual data values are never sent.
173+
:::
174+
175+
## Best Practices
176+
177+
### Writing Good Prompts
178+
179+
1. **Be specific**: "Show sales by month for 2024" is better than "show me sales"
180+
2. **Reference tables**: Use `@table_name` to be explicit
181+
3. **Describe the output**: "as a percentage" or "ordered by date descending"
182+
183+
### For Complex Queries
184+
185+
1. Start simple, then refine
186+
2. Ask for explanations: "Explain this query"
187+
3. Request modifications: "Now add a filter for status = 'active'"
188+
189+
### Performance Tips
190+
191+
1. **Local AI**:
192+
- First load downloads ~2GB model
193+
- Subsequent loads use cached model
194+
- GPU memory affects performance
195+
196+
2. **Cloud AI**:
197+
- Faster initial response
198+
- No local GPU required
199+
- Requires internet connection
200+
201+
## Troubleshooting
202+
203+
### "WebGPU Not Supported"
204+
205+
**Problem**: Can't use local AI
206+
207+
**Solutions**:
208+
1. Update to Chrome/Edge 113+
209+
2. Check `chrome://gpu` for WebGPU status
210+
3. Try enabling `#enable-unsafe-webgpu` flag
211+
4. Use cloud AI as alternative
212+
213+
### "Model download failed"
214+
215+
**Problem**: Can't download the AI model
216+
217+
**Solutions**:
218+
1. Check internet connection
219+
2. Clear browser cache and retry
220+
3. Try a smaller model (Llama 3.2 1B)
221+
4. Check available disk space
222+
223+
### "Generation is slow"
224+
225+
**Problem**: AI responses take too long
226+
227+
**Solutions**:
228+
1. Try a smaller model
229+
2. Close other GPU-intensive applications
230+
3. Use cloud AI for faster responses
231+
4. Reduce query complexity
232+
233+
### "API key invalid"
234+
235+
**Problem**: Cloud AI not working
236+
237+
**Solutions**:
238+
1. Verify API key is correct
239+
2. Check API key permissions
240+
3. Ensure you have API credits
241+
4. Try generating a new key
242+
243+
## Technical Details
244+
245+
### WebLLM Integration
246+
247+
Duck Brain uses [WebLLM](https://webllm.mlc.ai/) for local inference:
248+
249+
- Runs optimized LLMs in browser via WebGPU
250+
- Models are quantized (4-bit) for efficiency
251+
- Cached in browser's Cache Storage
252+
- No backend server required
253+
254+
### Provider Abstraction
255+
256+
All AI providers implement a common interface:
257+
258+
```typescript
259+
interface AIProvider {
260+
generateStreaming(messages, callbacks): Promise<void>;
261+
generateText(messages): Promise<string>;
262+
abort(): void;
263+
}
264+
```
265+
266+
This allows seamless switching between local and cloud AI.
267+
268+
### Schema Context
269+
270+
Before each generation, Duck Brain builds a schema context:
271+
272+
```sql
273+
-- Table: customers (1,234 rows)
274+
-- Columns: id (INTEGER), name (VARCHAR), email (VARCHAR), ...
275+
276+
-- Table: orders (5,678 rows)
277+
-- Columns: id (INTEGER), customer_id (INTEGER), amount (DECIMAL), ...
278+
```
279+
280+
This context is prepended to your query to help the AI understand your data structure.

0 commit comments

Comments
 (0)