10 Commits

Author SHA1 Message Date
a0ed0eaaf0 Fixed bug in cost display and handling 2026-01-26 11:39:42 +01:00
9a76ce2c1f Fixed bug in cost display and handling 2026-01-26 11:16:39 +01:00
106ef676e2 Updated to RC1 2026-01-15 10:13:49 +01:00
8c49339452 Minor bug fixes 2026-01-08 15:00:59 +01:00
8a113a9bbe Fixed some regex for files 2026-01-08 13:12:17 +01:00
c305d5cf49 More bugfixes 2026-01-07 11:09:13 +01:00
2c9f33868e More bugfixes 2026-01-07 09:58:58 +01:00
d4f1a1c6a4 Fixed bug in ctrl+c handling during response streaming 2026-01-07 08:01:33 +01:00
2e7c49bf68 Major update 2026-01-06 13:52:44 +01:00
1ef7918291 1.9.6 (#1)
New functionality and bugfixes.

Reviewed-on: #1
Co-authored-by: Rune Olsen <rune@rune.pm>
Co-committed-by: Rune Olsen <rune@rune.pm>
2025-12-30 15:46:40 +01:00
4 changed files with 4914 additions and 435 deletions

9
.gitignore vendored
View File

@@ -22,8 +22,9 @@ Pipfile.lock # Consider if you want to include or exclude
._* ._*
*~.nib *~.nib
*~.xib *~.xib
README.md.old
oai.zip # Added by author
*.zip
.note .note
diagnose.py diagnose.py
*.log *.log
@@ -34,3 +35,7 @@ compiled/
images/oai-iOS-Default-1024x1024@1x.png images/oai-iOS-Default-1024x1024@1x.png
images/oai.icon/ images/oai.icon/
b0.sh b0.sh
*.bak
*.old
*.sh
*.back

578
README.md
View File

@@ -1,53 +1,87 @@
# oAI - OpenRouter AI Chat # oAI - OpenRouter AI Chat
A terminal-based chat interface for OpenRouter API with conversation management, cost tracking, and rich formatting. A powerful terminal-based chat interface for OpenRouter API with **MCP (Model Context Protocol)** support, enabling AI agents to access local files and query SQLite databases directly.
## Description ## Description
oAI is a command-line chat application that provides an interactive interface to OpenRouter's AI models. It features conversation persistence, file attachments, export capabilities, and detailed session metrics. oAI is a feature-rich command-line chat application that provides an interactive interface to OpenRouter's AI models. It now includes **MCP integration** for local file system access and read-only database querying, allowing AI to help with code analysis, data exploration, and more.
## Features ## Features
- Interactive chat with multiple AI models via OpenRouter ### Core Features
- Model selection with search functionality - 🤖 Interactive chat with 300+ AI models via OpenRouter
- Conversation save/load/export (Markdown, JSON, HTML) - 🔍 Model selection with search and capability filtering
- File attachment support (code files and images) - 💾 Conversation save/load/export (Markdown, JSON, HTML)
- Session cost tracking and credit monitoring - 📎 File attachment support (images, PDFs, code files)
- Rich terminal formatting with syntax highlighting - 💰 Session cost tracking and credit monitoring
- Persistent command history - 🎨 Rich terminal formatting with syntax highlighting
- Configurable system prompts and token limits - 📝 Persistent command history with search (Ctrl+R)
- SQLite-based configuration and conversation storage - ⚙️ Configurable system prompts and token limits
- 🗄️ SQLite-based configuration and conversation storage
- 🌐 Online mode (web search capabilities)
- 🧠 Conversation memory toggle (save costs with stateless mode)
### NEW: MCP (Model Context Protocol) v2.1.0-beta
- 🔧 **File Mode**: AI can read, search, and list your local files
- Automatic .gitignore filtering
- Virtual environment exclusion (venv, node_modules, etc.)
- Supports code files, text, JSON, YAML, and more
- Large file handling (auto-truncates >50KB)
- ✍️ **Write Mode** (NEW!): AI can modify files with your permission
- Create and edit files within allowed folders
- Delete files (always requires confirmation)
- Move, copy, and organize files
- Create directories
- Ignores .gitignore for write operations
- OFF by default - explicit opt-in required
- 🗄️ **Database Mode**: AI can query your SQLite databases
- Read-only access (no data modification possible)
- Schema inspection (tables, columns, indexes)
- Full-text search across all tables
- SQL query execution (SELECT, JOINs, CTEs, subqueries)
- Query validation and timeout protection
- Result limiting (max 1000 rows)
- 🔒 **Security Features**:
- Explicit folder/database approval required
- System directory blocking
- Write mode OFF by default (non-persistent)
- Delete operations always require user confirmation
- Read-only database access
- SQL injection protection
- Query timeout (5 seconds)
## Requirements ## Requirements
- Python 3.7 or higher - Python 3.10-3.13 (3.14 not supported yet)
- OpenRouter API key (get one at https://openrouter.ai) - OpenRouter API key (get one at https://openrouter.ai)
- Function-calling model required for MCP features (GPT-4, Claude, etc.)
## Screenshot ## Screenshot
[<img src="https://gitlab.pm/rune/oai/raw/branch/main/images/screenshot_01.png">](https://gitlab.pm/rune/oai/src/branch/main/README.md) [<img src="https://gitlab.pm/rune/oai/raw/branch/main/images/screenshot_01.png">](https://gitlab.pm/rune/oai/src/branch/main/README.md)
Screenshot of `/help` screen. *Screenshot from version 1.0 - MCP interface shows mode indicators like `[🔧 MCP: Files]` or `[🗄️ MCP: DB #1]`*
## Installation ## Installation
### 1. Install Dependencies ### Option 1: From Source (Recommended for Development)
Use the included `requirements.txt` file to install the dependencies: #### 1. Install Dependencies
```bash ```bash
pip install -r requirements.txt pip install -r requirements.txt
``` ```
### 2. Make the Script Executable #### 2. Make Executable
```bash ```bash
chmod +x oai.py chmod +x oai.py
``` ```
### 3. Copy to PATH #### 3. Copy to PATH
Copy the script to a directory in your `$PATH` environment variable. Common locations include:
```bash ```bash
# Option 1: System-wide (requires sudo) # Option 1: System-wide (requires sudo)
@@ -57,145 +91,448 @@ sudo cp oai.py /usr/local/bin/oai
mkdir -p ~/.local/bin mkdir -p ~/.local/bin
cp oai.py ~/.local/bin/oai cp oai.py ~/.local/bin/oai
# Add to PATH if not already (add to ~/.bashrc or ~/.zshrc) # Add to PATH if needed (add to ~/.bashrc or ~/.zshrc)
export PATH="$HOME/.local/bin:$PATH" export PATH="$HOME/.local/bin:$PATH"
``` ```
### 4. Verify Installation #### 4. Verify Installation
```bash
oai --version
```
### Option 2: Pre-built Binaries
Download platform-specific binaries:
- **macOS (Apple Silicon)**: `oai_vx.x.x_mac_arm64.zip`
- **Linux (x86_64)**: `oai_vx.x.x-linux-x86_64.zip`
```bash
# Extract and install
unzip oai_vx.x.x_mac_arm64.zip # or `oai_vx.x.x-linux-x86_64.zip`
chmod +x oai
mkdir -p ~/.local/bin # Remember to add this to your path. Or just move to folder already in your $PATH
mv oai ~/.local/bin/
```
### Alternative: Shell Alias
```bash
# Add to ~/.bashrc or ~/.zshrc
alias oai='python3 /path/to/oai.py'
```
## Quick Start
### First Run Setup
```bash ```bash
oai oai
``` ```
### 5. Alternative Installation (for *nix systems) On first run, you'll be prompted to enter your OpenRouter API key.
If you have issues with the above method you can add an alias in your `.bashrc`, `.zshrc` etc. ### Basic Usage
```bash
alias oai='python3 <path to your file>'
```
On first run, you will be prompted to enter your OpenRouter API key.
### 6. Use Binaries
You can also just download the supplied binary for either Mac wit Mx (M1, M2 etc) `oai_mac_arm64.zip` and follow [#3](https://gitlab.pm/rune/oai#3-copy-to-path). Or download for Linux (64bit) `oai_linux_x86_64.zip` and also follow [#3](https://gitlab.pm/rune/oai#3-copy-to-path).
## Usage
### Starting the Application
```bash ```bash
# Start chatting
oai oai
# Select a model
You> /model
# Enable MCP for file access
You> /mcp on
You> /mcp add ~/Documents
# Ask AI to help with files (read-only)
[🔧 MCP: Files] You> List all Python files in Documents
[🔧 MCP: Files] You> Read and explain main.py
# Enable write mode to let AI modify files
You> /mcp write on
[🔧✍️ MCP: Files+Write] You> Create a new Python file with helper functions
[🔧✍️ MCP: Files+Write] You> Refactor main.py to use async/await
# Switch to database mode
You> /mcp add db ~/myapp/data.db
You> /mcp db 1
[🗄️ MCP: DB #1] You> Show me all tables
[🗄️ MCP: DB #1] You> Find all users created this month
``` ```
### Basic Commands ## MCP Guide
``` ### File Mode (Default)
/help Show all available commands
/model Select an AI model **Setup:**
/config api Set OpenRouter API key ```bash
exit Quit the application /mcp on # Start MCP server
/mcp add ~/Projects # Grant access to folder
/mcp add ~/Documents # Add another folder
/mcp list # View all allowed folders
``` ```
### Configuration **Natural Language Usage:**
All configuration is stored in `~/.config/oai/`:
- `oai_config.db` - SQLite database for settings and conversations
- `oai.log` - Application log file
- `history.txt` - Command history
### Common Workflows
**Select a Model:**
``` ```
/model "List all Python files in Projects"
"Read and explain config.yaml"
"Search for files containing 'TODO'"
"What's in my Documents folder?"
``` ```
**Paste from clipboard:** **Available Tools (Read-Only):**
Paste and send content to model - `read_file` - Read complete file contents
``` - `list_directory` - List files/folders (recursive optional)
/paste - `search_files` - Search by name or content
**Available Tools (Write Mode - requires `/mcp write on`):**
- `write_file` - Create new files or overwrite existing ones
- `edit_file` - Find and replace text in existing files
- `delete_file` - Delete files (always requires confirmation)
- `create_directory` - Create directories
- `move_file` - Move or rename files
- `copy_file` - Copy files to new locations
**Features:**
- ✅ Automatic .gitignore filtering (read operations only)
- ✅ Skips virtual environments (venv, node_modules)
- ✅ Handles large files (auto-truncates >50KB)
- ✅ Cross-platform (macOS, Linux, Windows via WSL)
- ✅ Write mode OFF by default for safety
- ✅ Delete operations require user confirmation with LLM's reason
### Database Mode
**Setup:**
```bash
/mcp add db ~/app/database.db # Add SQLite database
/mcp db list # View all databases
/mcp db 1 # Switch to database #1
``` ```
Paste with prompt and send content to model **Natural Language Usage:**
``` ```
/paste Analyze this text "Show me all tables in this database"
"Find records mentioning 'error'"
"How many users registered last week?"
"Get the schema for the orders table"
"Show me the 10 most recent transactions"
``` ```
**Start Chatting:** **Available Tools:**
``` - `inspect_database` - View schema, tables, columns, indexes
You> Hello, how are you? - `search_database` - Full-text search across tables
- `query_database` - Execute read-only SQL queries
**Supported Queries:**
- ✅ SELECT statements
- ✅ JOINs (INNER, LEFT, RIGHT, FULL)
- ✅ Subqueries
- ✅ CTEs (Common Table Expressions)
- ✅ Aggregations (COUNT, SUM, AVG, etc.)
- ✅ WHERE, GROUP BY, HAVING, ORDER BY, LIMIT
- ❌ INSERT/UPDATE/DELETE (blocked for safety)
### Write Mode
**Enable Write Mode:**
```bash
/mcp write on # Enable write mode (shows warning, requires confirmation)
``` ```
**Attach Files:** **Natural Language Usage:**
``` ```
You> Debug this code @/path/to/script.py "Create a new Python file called utils.py with helper functions"
You> Analyze this image @/path/to/image.png "Edit main.py and replace the old API endpoint with the new one"
"Delete the backup.old file" (will prompt for confirmation)
"Create a directory called tests"
"Move config.json to the config folder"
``` ```
**Save Conversation:** **Important:**
``` - ⚠️ Write mode is **OFF by default** and resets each session
/save my_conversation - ⚠️ Delete operations **always** require user confirmation
- ⚠️ All operations are limited to allowed MCP folders
- ✅ Write operations ignore .gitignore (can write to any file in allowed folders)
**Disable Write Mode:**
```bash
/mcp write off # Disable write mode (back to read-only)
``` ```
**Export to File:** ### Mode Management
```
/export md notes.md
/export json backup.json
/export html report.html
```
**View Session Stats:**
```bash
/mcp status # Show current mode, write mode, stats, folders/databases
/mcp files # Switch to file mode
/mcp db <number> # Switch to database mode
/mcp gitignore on # Enable .gitignore filtering (default)
/mcp write on|off # Enable/disable write mode
/mcp remove 2 # Remove folder/database by number
``` ```
/stats
/credits
```
**Prevous commands input:**
Use the up/down arrows to see earlier `/`commands and earlier input to model and `<enter>` to execute the same command or resend the same input.
## Command Reference ## Command Reference
Use `/help` within the application for a complete command reference organized by category: ### Session Commands
- Session Commands ```
- Model Commands /help [command] Show help menu or detailed command help
- Configuration /help mcp Comprehensive MCP guide
- Token & System /clear or /cl Clear terminal screen (or Ctrl+L)
- Conversation Management /memory on|off Toggle conversation memory (save costs)
- Monitoring & Stats /online on|off Enable/disable web search
- File Attachments /paste [prompt] Paste clipboard content
/retry Resend last prompt
/reset Clear history and system prompt
/prev View previous response
/next View next response
```
### MCP Commands
```
/mcp on Start MCP server
/mcp off Stop MCP server
/mcp status Show comprehensive status (includes write mode)
/mcp add <folder> Add folder for file access
/mcp add db <path> Add SQLite database
/mcp list List all folders
/mcp db list List all databases
/mcp db <number> Switch to database mode
/mcp files Switch to file mode
/mcp remove <num> Remove folder/database
/mcp gitignore on Enable .gitignore filtering
/mcp write on Enable write mode (create/edit/delete files)
/mcp write off Disable write mode (read-only)
```
### Model Commands
```
/model [search] Select/change AI model
/info [model_id] Show model details (pricing, capabilities)
```
### Configuration
```
/config View all settings
/config api Set API key
/config model Set default model
/config online Set default online mode (on|off)
/config stream Enable/disable streaming (on|off)
/config maxtoken Set max token limit
/config costwarning Set cost warning threshold ($)
/config loglevel Set log level (debug/info/warning/error)
/config log Set log file size (MB)
```
### Conversation Management
```
/save <name> Save conversation
/load <name|num> Load saved conversation
/delete <name|num> Delete conversation
/list List saved conversations
/export md|json|html <file> Export conversation
```
### Token & System
```
/maxtoken [value] Set session token limit
/system [prompt] Set system prompt (use 'clear' to reset)
/middleout on|off Enable prompt compression
```
### Monitoring
```
/stats View session statistics
/credits Check OpenRouter credits
```
### File Attachments
```
@/path/to/file Attach file (images, PDFs, code)
Examples:
Debug @script.py
Analyze @data.json
Review @screenshot.png
```
## Configuration Options ## Configuration Options
- API Key: `/config api` All configuration stored in `~/.config/oai/`:
- Base URL: `/config url`
- Streaming: `/config stream on|off`
- Default Model: `/config model`
- Cost Warning: `/config costwarning <amount>`
- Max Token Limit: `/config maxtoken <value>`
## File Support ### Files
- `oai_config.db` - SQLite database (settings, conversations, MCP config)
- `oai.log` - Application logs (rotating, configurable size)
- `history.txt` - Command history (searchable with Ctrl+R)
**Supported Code Extensions:** ### Key Settings
.py, .js, .ts, .cs, .java, .c, .cpp, .h, .hpp, .rb, .ruby, .php, .swift, .kt, .kts, .go, .sh, .bat, .ps1, .R, .scala, .pl, .lua, .dart, .elm, .xml, .json, .yaml, .yml, .md, .txt - **API Key**: OpenRouter authentication
- **Default Model**: Auto-select on startup
- **Streaming**: Real-time response display
- **Max Tokens**: Global and session limits
- **Cost Warning**: Alert threshold for expensive requests
- **Online Mode**: Default web search setting
- **Log Level**: debug/info/warning/error/critical
- **Log Size**: Rotating file size in MB
**Image Support:** ## Supported File Types
Any image format with proper MIME type (PNG, JPEG, GIF, etc.)
## Data Storage ### Code Files
`.py, .js, .ts, .cs, .java, .c, .cpp, .h, .hpp, .rb, .ruby, .php, .swift, .kt, .kts, .go, .sh, .bat, .ps1, .R, .scala, .pl, .lua, .dart, .elm`
- Configuration: `~/.config/oai/oai_config.db` ### Data Files
- Logs: `~/.config/oai/oai.log` `.json, .yaml, .yml, .xml, .csv, .txt, .md`
- History: `~/.config/oai/history.txt`
### Images
All standard formats: PNG, JPEG, JPG, GIF, WEBP, BMP
### Documents
PDF (models with document support)
### Size Limits
- Images: 10 MB max
- Code/Text: Auto-truncates files >50KB
- Binary data: Displayed as `<binary: X bytes>`
## MCP Security
### Access Control
- ✅ Explicit folder/database approval required
- ✅ System directories blocked automatically
- ✅ User confirmation for each addition
- ✅ .gitignore patterns respected (file mode)
### Database Safety
- ✅ Read-only mode (cannot modify data)
- ✅ SQL query validation (blocks INSERT/UPDATE/DELETE)
- ✅ Query timeout (5 seconds max)
- ✅ Result limits (1000 rows max)
- ✅ Database opened in `mode=ro`
### File System Safety
- ✅ Read-only by default (write mode requires explicit opt-in)
- ✅ Write mode OFF by default each session (non-persistent)
- ✅ Delete operations always require user confirmation
- ✅ Write operations limited to allowed folders only
- ✅ System directories blocked
- ✅ Virtual environment exclusion
- ✅ Build artifact filtering
- ✅ Maximum file size (10 MB)
## Tips & Tricks
### Command History
- **↑/↓ arrows**: Navigate previous commands
- **Ctrl+R**: Search command history
- **Auto-complete**: Start typing `/` for command suggestions
### Cost Optimization
```bash
/memory off # Disable context (stateless mode)
/maxtoken 1000 # Limit response length
/config costwarning 0.01 # Set alert threshold
```
### MCP Best Practices
```bash
# Check status frequently
/mcp status
# Use specific paths to reduce search time
"List Python files in Projects/app/" # Better than
"List all Python files" # Slower
# Database queries - be specific
"SELECT * FROM users LIMIT 10" # Good
"SELECT * FROM users" # May hit row limit
```
### Debugging
```bash
# Enable debug logging
/config loglevel debug
# Check log file
tail -f ~/.config/oai/oai.log
# View MCP statistics
/mcp status # Shows tool call counts
```
## Troubleshooting
### MCP Not Working
```bash
# 1. Check if MCP is installed
python3 -c "import mcp; print('MCP OK')"
# 2. Verify model supports function calling
/info # Look for "tools" in supported parameters
# 3. Check MCP status
/mcp status
# 4. Review logs
tail ~/.config/oai/oai.log
```
### Import Errors
```bash
# Reinstall dependencies
pip install --force-reinstall -r requirements.txt
```
### Binary Issues (macOS)
```bash
# Remove quarantine
xattr -cr ~/.local/bin/oai
# Check security settings
# System Settings > Privacy & Security > "Allow Anyway"
```
### Database Errors
```bash
# Verify it's a valid SQLite database
sqlite3 database.db ".tables"
# Check file permissions
ls -la database.db
```
## Version History
### v2.1.0-RC1 (Current)
-**NEW**: MCP (Model Context Protocol) integration
-**NEW**: File system access (read, search, list)
-**NEW**: Write mode - AI can create, edit, and delete files
- 6 write tools: write_file, edit_file, delete_file, create_directory, move_file, copy_file
- OFF by default - requires explicit `/mcp write on` activation
- Delete operations always require user confirmation
- Non-persistent setting (resets each session)
-**NEW**: SQLite database querying (read-only)
-**NEW**: Dual mode support (Files & Database)
-**NEW**: .gitignore filtering
-**NEW**: Binary data handling in databases
-**NEW**: Mode indicators in prompt (shows ✍️ when write mode active)
-**NEW**: Comprehensive `/help mcp` guide
- 🔧 Improved error handling for tool calls
- 🔧 Enhanced logging for MCP operations
- 🔧 Statistics tracking for tool usage
### v1.9.6
- Base version with core chat functionality
- Conversation management
- File attachments
- Cost tracking
- Export capabilities
## License ## License
MIT License MIT License
Copyright (c) 2024 Rune Olsen Copyright (c) 2024-2025 Rune Olsen
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -221,12 +558,27 @@ Full license: https://opensource.org/licenses/MIT
**Rune Olsen** **Rune Olsen**
Blog: https://blog.rune.pm - Homepage: https://ai.fubar.pm/
- Blog: https://blog.rune.pm
- Project: https://iurl.no/oai
## Version ## Contributing
1.0 Contributions welcome! Please:
1. Fork the repository
2. Create a feature branch
3. Submit a pull request with detailed description
## Support ## Acknowledgments
For issues, questions, or contributions, visit https://iurl.no/oai and create an issue. - OpenRouter team for the unified AI API
- Rich library for beautiful terminal output
- MCP community for the protocol specification
---
**Star ⭐ this project if you find it useful!**
---
Did you really read all the way down here? WOW! You deserve a 🍾 🥂!

4648
oai.py

File diff suppressed because it is too large Load Diff

View File

@@ -1,38 +1,26 @@
anyio==4.11.0 # oai.py v2.1.0-beta - Core Dependencies
beautifulsoup4==4.14.2 anyio>=4.11.0
charset-normalizer==3.4.4 charset-normalizer>=3.4.4
click==8.3.1 click>=8.3.1
docopt==0.6.2 h11>=0.16.0
h11==0.16.0 httpcore>=1.0.9
httpcore==1.0.9 httpx>=0.28.1
httpx==0.28.1 idna>=3.11
idna==3.11 markdown-it-py>=4.0.0
latex2mathml==3.78.1 mdurl>=0.1.2
loguru==0.7.3 openrouter>=0.0.19
markdown-it-py==4.0.0 packaging>=25.0
markdown2==2.5.4 prompt-toolkit>=3.0.52
mdurl==0.1.2 Pygments>=2.19.2
natsort==8.4.0 pyperclip>=1.11.0
openrouter==0.0.19 requests>=2.32.5
packaging==25.0 rich>=14.2.0
pipreqs==0.4.13 shellingham>=1.5.4
prompt-toolkit==3.0.52 sniffio>=1.3.1
Pygments==2.19.2 typer>=0.20.0
pyperclip==1.11.0 typing-extensions>=4.15.0
python-dateutil==2.9.0.post0 urllib3>=2.5.0
python-magic==0.4.27 wcwidth>=0.2.14
PyYAML==6.0.3
requests==2.32.5 # MCP (Model Context Protocol)
rich==14.2.0 mcp>=1.25.0
shellingham==1.5.4
six==1.17.0
sniffio==1.3.1
soupsieve==2.8
svgwrite==1.4.3
tqdm==4.67.1
typer==0.20.0
typing-extensions==4.15.0
urllib3==2.5.0
wavedrom==2.0.3.post3
wcwidth==0.2.14
yarg==0.1.10