# OpenRouter provider (WoowBot Free)

One API key, many models. Works like the OpenAI and Gemini providers and plugs into the
provider router, so only one AI provider answers chat at a time.

## Files

| File | Purpose |
| --- | --- |
| `qcld-bot-openrouter.php` | AJAX handlers: save settings (with connection test), model list, visitor chat, streaming, product details. |
| `class-qcld-openrouter-client.php` | All HTTP to openrouter.ai: chat, stream (cURL), models, embeddings, error wording, masking helper. |
| `admin/settings.php` | The panel on **AI Settings** (Settings, RAG, Help tabs). |
| `assets/js/qcld-openrouter-admin.js` | Save and "Fetch Models" buttons. Strings come from `qcld_openrouter_admin_data.i18n`. |
| `../class-qcld-ai-provider-router.php` | Shared: decides which single provider answers (outside this folder). |

Outside this folder the feature touches: the `require` lines in `qcld-woowbot.php`, the card and panel in
`ai-admin.php`, provider-neutral routing in `qcld-woo-chatbot-frontend.js`, `class-qcld-bot-rag.php`
(embedding provider and size check), the setup wizard (`wizard-popup.php`, wizard handlers in
`openai/qcld-bot-openai.php`) and the `External services` section of `readme.txt`.

## How routing works

`Qcld_AI_Provider_Router::active()` returns the first provider that is switched on **and** has a key,
in this order: Gemini, OpenAI, OpenRouter. Chat, streaming, product details, RAG embeddings and the
admin screen all ask it. Saving a provider turns the others off. For OpenRouter the key and every
setting are always saved first, and OpenRouter is switched on (the others off) whatever the connection
test says, like the OpenAI and Gemini panels; the test result is only reported. The one exception is
"enabled but no key saved", which leaves the other providers alone.

## Options (all `qcld_openrouter_*` unless noted)

`enabled`, `api_key` (autoload off), `model`, `embedding_model`, `system_content`, `prepend_content`,
`append_content`, `context_awareness_enabled`, `page_suggestion_enabled`, `stream_enabled`,
`redact_pii`, `max_tokens`, `temperature`, plus `last_error` (transient, one day).
Shared with the other providers: `is_page_rag_enabled`, `qcldai_product_card_enable`,
`qcld_openai_relevant_post`. `qcld_openrouter_enabled`, `_api_key` and `_rag_enabled` already existed.

## AJAX actions

`qcld_openrouter_settings_option`, `qcld_openrouter_get_model_list` (admin: nonce `wp_chatbot` + `manage_options`),
`qcld_openrouter_response`, `qcld_stream_openrouter`, and `qcld_woo_get_product_details` (only registered when
OpenRouter is the active provider and the setting is on).

## Hooks

* `qcld_ai_providers` (filter): the provider registry; add a provider at the end for the lowest precedence.
* `qcld_ai_active_provider` (filter): override which provider answers; return `''` to switch AI answers off.
* `qcld_openrouter_request_args` (filter): change the arguments of the chat request (timeout, headers, body).
* `qcld_openrouter_api_base` (filter): change the API base URL (for example a proxy). It receives requests that
  carry the API key, so only trusted code should use it.

## Behaviour worth knowing

* Visitors only ever see a generic "could not get an answer" message. Logged-in administrators also see the reason.
* The connection test counts any valid reply as working, even one with no text (reasoning models can spend a
  small token limit on hidden thinking).
* The saved key is shown in the settings field as dots plus its last four characters; the full key never
  reaches the page. Posting the dots back (an untouched field) keeps the saved key.
* The setup wizard accepts a key when OpenRouter answers 402, 403 or 429 (the key is recognised but cannot
  answer right now), and only stops on a refused key or no connection.
* Streaming needs PHP cURL. The chat script cannot build product cards from a stream, so the model is not asked
  for them while streaming.
* If vectors in the knowledge base have a different size than the current embedding model makes, those
  documents are skipped and a notice on AI Settings asks for a re-embed.
* "Mask email addresses and long numbers" is best effort: it does not catch names or street addresses.

## Tests

Not part of the plugin package. During development: mocked HTTP for settings, chat, embeddings, wizard and
the size check; a local mock OpenRouter (comment keep-alives, split lines, mid-stream errors, HTTP 401/429)
for streaming; Node tests for the chat script's markdown renderer.
