=== SOZO AI Chatbot ===
Contributors: sozodesign
Tags: chatbot, ai, openai, rag, lead-generation
Requires at least: 6.0
Tested up to: 6.7
Requires PHP: 8.2
Stable tag: 1.3.0
License: GPL-2.0-or-later
License URI: http://www.gnu.org/licenses/gpl-2.0.txt

AI-powered chatbot with retrieval-augmented generation, lead capture, and idle-conversation Slack alerts.

== Description ==

SOZO AI Chatbot is a WordPress plugin that embeds a retrieval-augmented chat experience on your site. User questions are vectorised via OpenAI embeddings, matched against your content in Supabase pgvector (hybrid keyword + cosine search), then answered by GPT-4o with the retrieved context injected.

= Features =

* **Two chat UIs** — a drop-in vanilla-JS widget (via `[sozo_chatbot]` shortcode) and a Sage/Acorn-compatible Alpine.js block for Blade themes. Both talk to the same REST and AJAX backend.
* **Streaming responses** — Server-Sent Events stream tokens directly from OpenAI for instant feedback, with a non-streaming fallback for hardened hosts.
* **Retrieval-augmented generation** — Supabase `hybrid_search()` blends cosine similarity with PostgreSQL full-text search for precise, diverse retrieval.
* **Content sync pipeline** — Posts and pages are rendered to markdown and uploaded to Google Drive; n8n watches the folder, chunks, embeds, and writes to Supabase.
* **Lead capture** — In-chat forms detect intent and route qualified leads to email and Slack, with detected topics and the full conversation transcript attached.
* **Idle conversation alerts** — A 5-minute cron detects conversations that go quiet mid-chat and posts the full transcript to Slack so the team can follow up without opening the admin.
* **Admin tooling** — Batch sync with progress, failure retries, schema health checks, sync-coverage audits, and per-source resync.

= External services =

This plugin integrates with OpenAI (embeddings + chat completions), Supabase (pgvector storage and RPC search), Google Drive (markdown source of truth), and optionally Slack (webhook notifications) and n8n (automation). API keys and service-account credentials are stored encrypted using WordPress salts.

== Installation ==

1. Upload the plugin to `/wp-content/plugins/sozo-ai-chatbot/` or install via the Plugins screen.
2. Activate the plugin.
3. Go to **SOZO Chatbot → Settings** and add your OpenAI, Supabase, and Google Drive credentials.
4. Run the Supabase migrations in `supabase/migrations/` against your project in numerical order.
5. Optionally configure a Slack webhook and enable idle-conversation notifications.
6. Trigger an initial content sync from **SOZO Chatbot → Sync**.

== Frequently Asked Questions ==

= Does the plugin write directly to the `documents` table? =

No. WordPress writes markdown files to Google Drive and writes to the `sync_sources` table. The `documents` table (chunks, embeddings) is owned by the n8n pipeline.

= Where are API keys stored? =

Keys are encrypted at rest using OpenSSL with a key derived from WordPress salts (`AUTH_KEY`, `SECURE_AUTH_KEY`). They are never written to the database in plain text and never logged.

= Can I use this without Slack? =

Yes. Slack is optional — the plugin falls back to email notifications for leads. Idle-conversation alerts simply don't fire if no webhook is configured.

== Changelog ==

= 1.3.0 =
* Added: Idle-conversation Slack notifications with a 5-minute cron detection loop and admin configuration.
* Added: Full conversation transcripts inside Slack Block Kit payloads for both idle and lead notifications — admins no longer need to open the WordPress admin to read the exchange.
* Added: Slack deep-link auto-opens the conversation modal on admin page load.
* Changed: RAG similarity threshold lowered to 0.5 and match count raised to 8 for better retrieval diversity.
* Changed: Google Drive uploads no longer include YAML frontmatter (previously polluted ~13.6% of embedding vectors).
* Changed: Content chunker now strips legacy frontmatter, decodes HTML entities, and normalises non-breaking spaces.
* Fixed: `/chat/` page excluded from RAG source results.
* Fixed: `idle_minutes` zero-as-empty edge case.
* Fixed: Version drift across plugin header, version constant, and `composer.json` (now all 1.3.0).

= 1.2.0 =
* Changed: Admin JS/CSS cache-busting version bump.
* Changed: Service-page RAG boost rewritten with a topic-to-URL map; source threshold raised.
* Changed: WooCommerce and Magento topic boosts redirected to the Shopify services page.
* Fixed: Sources not being shown on the frontend in certain RAG response shapes.

= 1.1.0 =
* Widget + custom block chat UIs sharing the same PHP backend (REST + AJAX).
* SSE streaming chat completions with non-streaming fallback.
* Supabase hybrid search (pgvector + full-text) for RAG retrieval.
* Google Drive content sync pipeline with n8n handling embeddings.
* Lead-capture forms with Slack and email notifications.
* Admin analytics, sync coverage audit, and schema health checks.

= 1.0.0 =
* Initial development: plugin scaffold, encryption layer, WordPress CPTs, and service classes.

== Upgrade Notice ==

= 1.3.0 =
First tagged release. Brings idle-conversation Slack alerts with full transcripts, substantial RAG retrieval improvements, and normalises prior version drift. Safe to upgrade; no database migrations required beyond those already applied in `supabase/migrations/`.
