# MxChat Assistant API

A powerful WordPress plugin that integrates OpenAI Assistants API with MxChat, allowing you to bypass the default knowledge base and use your custom OpenAI Assistant for chat interactions.

## Description

MxChat Assistant API is an addon for the MxChat plugin that provides seamless integration with OpenAI's Assistants API. When enabled, it completely overrides MxChat's default functionality and routes all chat interactions through your custom OpenAI Assistant, providing more sophisticated AI responses tailored to your specific needs.

## Features

- **Complete Override Mode**: Bypasses MxChat's knowledge base and uses your OpenAI Assistant exclusively
- **Thread Management**: Maintains conversation context through OpenAI's thread system
- **Session Persistence**: Preserves chat sessions for up to 24 hours
- **Real-time Processing**: Handles assistant responses with proper polling and status checking
- **Admin Integration**: User-friendly admin interface for configuration and testing
- **Rate Limiting**: Respects MxChat's built-in rate limiting system
- **Transcript Logging**: Maintains chat transcripts compatible with MxChat's system
- **Email Notifications**: Sends notifications for new chat sessions
- **Connection Testing**: Built-in tools to verify your assistant configuration

## Requirements

- WordPress 5.0 or higher
- PHP 7.4 or higher
- **MxChat Basic** plugin (installed and active)
- **MxChat Pro** license (active)
- OpenAI API key
- OpenAI Assistant (created in your OpenAI dashboard)

## Installation

1. Download the plugin files
2. Upload to `/wp-content/plugins/mxchat-assistant-api/`
3. Activate the plugin through the 'Plugins' menu in WordPress
4. Ensure MxChat Basic is installed and MxChat Pro license is active
5. Configure your OpenAI API key in MxChat settings
6. Navigate to **MxChat > Assistant API** to configure the addon

## Configuration

### Step 1: OpenAI Setup
1. Create an Assistant in your [OpenAI Dashboard](https://platform.openai.com/assistants)
2. Note your Assistant ID (starts with `asst_`)
3. Ensure your OpenAI API key is configured in MxChat settings

### Step 2: Plugin Configuration
1. Go to **MxChat > Assistant API** in your WordPress admin
2. Enter your Assistant ID
3. Enable the "Complete override" option
4. Save settings
5. Use the test button to verify connection

### Step 3: Testing
- Use the built-in connection test to verify your configuration
- Test chat functionality on your website
- Monitor chat transcripts in **MxChat > Transcripts**

## How It Works

1. **Message Interception**: The plugin intercepts chat messages before MxChat processes them
2. **Thread Management**: Creates or retrieves OpenAI threads for each chat session
3. **Assistant Processing**: Sends messages to your OpenAI Assistant and waits for responses
4. **Response Handling**: Returns assistant responses to the chat interface
5. **Logging**: Saves all interactions to MxChat's transcript system

## API Integration Details

- **OpenAI API Version**: Assistants v2
- **Thread Persistence**: 2 hours per session
- **Polling Timeout**: 60 seconds maximum
- **HTTP Timeout**: 30 seconds for API calls
- **Rate Limiting**: Inherits from MxChat core plugin

## Admin Features

- **Real-time Validation**: Assistant ID format checking
- **Connection Testing**: Verify assistant accessibility
- **Session Management**: Clear assistant sessions for testing
- **Status Indicators**: Visual feedback for configuration state
- **Error Handling**: Comprehensive error reporting and logging

## Multisite Support

The plugin supports WordPress multisite installations:
- Network-level configuration available
- Site-specific assistant configurations
- Inherits network-wide MxChat Pro license status

## Troubleshooting

### Common Issues

**"MxChat Assistant API requires the MxChat plugin to be installed and active"**
- Ensure MxChat Basic plugin is installed and activated

**"MxChat Assistant API requires MxChat Pro to be activated"**
- Activate your MxChat Pro license in MxChat settings

**"OpenAI Assistant is enabled but not properly configured"**
- Verify your OpenAI API key is set in MxChat settings
- Ensure your Assistant ID is correct and starts with `asst_`

**"Assistant not found"**
- Double-check your Assistant ID in the OpenAI dashboard
- Verify the assistant hasn't been deleted

### Debug Information

Enable WordPress debugging to see detailed error logs:
```php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
```

Check `/wp-content/debug.log` for MxChat Assistant API errors.

## Limitations

- Function calling is not currently supported
- Image responses from assistants show placeholder text
- Maximum 60-second response timeout
- Requires active internet connection for all chat interactions

## Support

For support and questions:
- Visit [MxChat.ai](https://mxchat.ai/)
- Check MxChat documentation
- Review OpenAI Assistants API documentation

## Changelog

### 1.0.0
- Initial release
- OpenAI Assistants API integration
- Complete override functionality
- Admin interface and testing tools
- Thread management and session persistence

## License

This plugin is proprietary software. Usage requires a valid MxChat Pro license.

---

**Note**: This plugin completely overrides MxChat's default functionality when enabled. Make sure to test thoroughly before deploying to production.