# AI Integration

Setup and usage of AI-powered features

## Overview

DWN includes optional AI integration for intelligent command execution and semantic web search. These features require API keys from external services.

AI Command Palette
Natural language commands via OpenRouter API. Ask AI to launch apps, answer questions, or perform tasks.
Context Analysis
AI analyzes your current workspace to understand what task you're working on.
Exa Search
Semantic web search that understands meaning, not just keywords. Find relevant docs and tutorials.

## Setup

### OpenRouter API Key

Required for AI Command Palette and Context Analysis.

1. Visit https://openrouter.ai/keys
2. Create an account and generate an API key
3. Set the key via environment variable or config file

    # Option 1: Environment variable (add to ~/.bashrc or ~/.zshrc)
    export OPENROUTER_API_KEY=sk-or-v1-your-key-here

    # Option 2: Config file (~/.config/dwn/config)
    [ai]
    openrouter_api_key = sk-or-v1-your-key-here

### Exa API Key

Required for Exa Semantic Search.

1. Visit https://dashboard.exa.ai/api-keys
2. Create an account and generate an API key
3. Set the key via environment variable or config file

    # Option 1: Environment variable
    export EXA_API_KEY=your-exa-key-here

    # Option 2: Config file (~/.config/dwn/config)
    [ai]
    exa_api_key = your-exa-key-here

Note: AI features are optional. DWN functions fully without them.

## AI Command Palette

Press `Super+Shift+A` to open the AI command palette.

### Usage

1. Press `Super+Shift+A`
2. Type a natural language command
3. Press Enter
4. AI interprets and executes your request

### Example Commands

Command | Result
--- | ---
"open firefox" | Launches Firefox browser
"launch terminal" | Opens configured terminal
"run file manager" | Opens configured file manager
"what time is it" | Shows current time
"how much memory is free" | Shows system memory info

### Supported Actions

- Application launching - "open", "launch", "run", "start"
- System queries - Time, date, system information
- General questions - AI will provide helpful responses

## Context Analysis

Press `Super+A` to see AI analysis of your current workspace.

### What It Shows

- Detected task type (coding, browsing, communication, etc.)
- Currently focused window
- AI-generated suggestions based on context

### Example Output

    Current Task: Software Development

    Focused: Visual Studio Code - project.py
    Windows: VS Code, Terminal, Firefox (Stack Overflow)

    Suggestions:
    - Run tests with Ctrl+Shift+T
    - Toggle terminal with Ctrl+`
    - Search documentation with Super+Shift+E

## Exa Semantic Search

Press `Super+Shift+E` to open semantic web search.

### How It Works

Exa uses neural search to find content based on meaning rather than keyword matching. This produces more relevant results for technical queries.

### Usage

1. Press `Super+Shift+E`
2. Type a natural language query
3. Press Enter
4. Results appear in a selection menu
5. Select a result to open in browser

### Example Queries

- "how to configure nginx reverse proxy"
- "python async await tutorial"
- "X11 window manager development guide"
- "systemd service file examples"

### Best Practices

- Use natural language, not keywords
- Be specific about what you're looking for
- Include context (language, framework, etc.)

## Configuration

AI settings in `~/.config/dwn/config`:

    [ai]
    # AI model for OpenRouter (see https://openrouter.ai/models)
    model = google/gemini-2.0-flash-exp:free

    # API keys (can also use environment variables)
    openrouter_api_key = sk-or-v1-your-key
    exa_api_key = your-exa-key

### Available Models

OpenRouter provides access to many AI models. Some options:

Model | Description
--- | ---
`google/gemini-2.0-flash-exp:free` | Fast, free tier available (default)
`anthropic/claude-3-haiku` | Fast and efficient
`openai/gpt-4o-mini` | Good balance of speed and quality
`x-ai/grok-code-fast-1` | Optimized for code tasks

See OpenRouter Models for the full list.

## Keyboard Shortcuts

Shortcut | Action
--- | ---
`Super+A` | Show AI context analysis
`Super+Shift+A` | Open AI command palette
`Super+Shift+E` | Open Exa semantic search

## Troubleshooting

### AI features not working

1. Verify API key is set correctly
2. Check network connectivity
3. Look for errors in `~/.local/share/dwn/dwn.log`

### Slow responses

- Try a faster model (e.g., `google/gemini-2.0-flash-exp:free`)
- Check network latency to API endpoints

### API rate limits

Free tiers have usage limits. Consider upgrading for heavy use or use a paid model.