=== AIPCF - AI Provider for Cloudflare ===
Contributors: deshabhishek007, fitehal
Tags: ai, cloudflare, workers-ai, llama, mistral
Requires at least: 7.0
Tested up to: 7.0
Requires PHP: 7.4
Stable tag: 1.0.3
License: GPL-2.0-or-later
License URI: https://spdx.org/licenses/GPL-2.0-or-later.html

Run Cloudflare Workers AI models from your WordPress site — edge inference, a generous free tier, and your data stays on Cloudflare's network.

== Description ==

This plugin connects WordPress to [Cloudflare Workers AI](https://developers.cloudflare.com/workers-ai/), making its hosted large language models available to any WordPress feature or plugin that uses the WordPress AI Client (introduced in WordPress 7.0).

= Supported models =

The plugin fetches the full list of available text generation models directly from your Cloudflare account and shows them in the settings dropdown, so you always see what is actually available to you. The list is cached for 12 hours and refreshed whenever you save your credentials.

A built-in fallback list is used when your credentials are not yet configured or the API is unreachable. See [Cloudflare's model catalogue](https://developers.cloudflare.com/workers-ai/models/) for the current full list.

= Why Cloudflare Workers AI? =

Unlike hosted AI services that charge per seat or per API call at a premium rate, Cloudflare Workers AI runs inference at the edge on a usage-based model. The free tier covers 10,000 neurons per day (Cloudflare's billing unit — roughly 200–500 average generation requests) — enough for a content-heavy site to generate post summaries, meta descriptions, and moderation checks without any cost. Paid usage is billed per neuron, making it significantly cheaper at scale compared to per-token pricing from closed providers.

Your prompts and completions also never leave Cloudflare's network to reach a third-party AI company's servers, which matters for sites with privacy or data-residency requirements.

= What you need =

1. A [Cloudflare account](https://dash.cloudflare.com/sign-up) with Workers AI enabled.
2. Your **Account ID** — visible on the Workers AI dashboard sidebar.
3. A **Cloudflare API token** with the "Workers AI - Edit" permission.

== Installation ==

1. Upload the plugin folder to `/wp-content/plugins/`.
2. Activate the plugin through the **Plugins** menu in WordPress.
3. Go to **Settings → Cloudflare Workers AI**.
4. Enter your Cloudflare **Account ID** and **API token**.
5. Click **Save Changes**, then **Test connection** to verify.
6. Use the **Text generation model** dropdown to choose your preferred model. The full list is fetched live from your Cloudflare account and cached for 12 hours.

= wp-config.php constants (optional) =

You can define credentials in `wp-config.php` instead of the settings page:

    define( 'CLOUDFLARE_WORKERS_AI_ACCOUNT_ID', 'your-account-id' );
    define( 'CLOUDFLARE_WORKERS_AI_API_TOKEN',  'your-api-token' );

You can also hard-code the default model (the settings page dropdown takes precedence over this constant):

    define( 'CLOUDFLARE_WORKERS_AI_DEFAULT_MODEL', '@cf/meta/llama-3.3-70b-instruct-fp8-fast' );

Environment variables with the same names are also supported.

== Frequently Asked Questions ==

= Where do I find my Account ID? =

Go to [dash.cloudflare.com](https://dash.cloudflare.com/?to=/:account/home) and select your account. The Account ID appears in the right-hand sidebar of the account home page.

= How do I create an API token? =

Visit [dash.cloudflare.com/profile/api-tokens](https://dash.cloudflare.com/profile/api-tokens), click **Create Token**, and select the **Workers AI** template (or manually add "Workers AI - Edit" permission).

= Can I choose which model WordPress AI features use? =

Yes. Go to **Settings → Cloudflare Workers AI** and use the **Text generation model** dropdown. The list is fetched live from your Cloudflare account (all text generation models available to you are shown) and cached for 12 hours.

The default when nothing is selected is Meta Llama 4 Scout 17B — a good all-round starting point. You can also override the model in `wp-config.php` — the settings page takes precedence over the constant.

= Results look off — what should I try? =

Different models have different strengths, response styles, and context lengths. If the default model isn't producing the results you want:

* Try **Meta Llama 3.3 70B** for richer, more detailed output.
* Try **Meta Llama 3.2 3B** or **Meta Llama 3.1 8B** for faster, lighter responses.
* Try **Qwen QwQ 32B** or **DeepSeek R1** for reasoning-heavy tasks.

Swap models from the **Text generation model** dropdown in Settings — no code changes needed.

= Does this plugin support image generation? =

Not in version 1.0. Text generation is supported in this release. Image generation support via Cloudflare Workers AI is planned for a future version.

= I get "connector has not been approved" — what does that mean? =

WordPress requires site administrators to explicitly allow each AI connector before any feature can use it. Go to **Settings → Connectors**, find Cloudflare Workers AI, and click **Allow**. You only need to do this once.

= Does this work without WordPress 7.0? =

No. The WordPress AI Client API is introduced in WordPress 7.0. The plugin will not register itself if the AI Client is not available.

== Usage ==

Once the plugin is configured, any WordPress AI Client-compatible feature or plugin will automatically use Cloudflare Workers AI as its provider.

Developers can also call it directly:

    use WordPress\AiClient\AiClient;

    $result = AiClient::prompt( 'Summarise this post in two sentences.' )
        ->usingProvider( 'cloudflare-workers-ai' )
        ->usingModel( '@cf/meta/llama-4-scout-17b-16e-instruct' )
        ->generateTextResult();

    echo $result->toText();

Switch models without changing any other code — swap `llama-4-scout-17b-16e-instruct` for `qwen2.5-coder-32b-instruct` and you are running a coding assistant instead.

This plugin does not add a chatbot or front-end widget by itself. It registers the provider so that other AI Client-powered features, plugins, and themes can use it.

== External Services ==

This plugin connects to the Cloudflare Workers AI REST API to run AI inference.

The service is used to:

- Run text generation requests against the model you select.
- Verify your Account ID and API token when you click "Test connection".

= What data is sent and when =

When you click "Test connection" in the plugin settings, your Account ID and API token are sent to `https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/models/search` to verify the credentials. No prompt data is sent during this check.

When a compatible AI Client feature generates text, the plugin sends your API token, the selected model ID, and the prompt messages to `https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model_id}`. Requests are only made when AI generation is explicitly triggered — nothing is sent automatically in the background.

= Service provider =

Cloudflare Workers AI is provided by Cloudflare, Inc.

* Cloudflare Workers AI documentation: https://developers.cloudflare.com/workers-ai/
* Cloudflare Terms of Service: https://www.cloudflare.com/terms/
* Cloudflare Privacy Policy: https://www.cloudflare.com/privacypolicy/

== Screenshots ==

1. Settings page before entering credentials — enter your Account ID here.
2. Adding your API key via Settings → Connectors.
3. Cloudflare Workers AI shows as Connected on the Connectors screen.
4. Connector Approvals — allow AIPCF to use the Cloudflare connector.
5. Settings page after a successful connection test.
6. AI-powered content suggestions working in the WordPress block editor.
7. AI-generated meta description for a page.
8. AI Request Logs showing Cloudflare Workers AI handling requests.

== Roadmap ==

Current version supports Workers AI edge inference. AI Gateway support for caching, rate-limiting, and analytics coming soon.

== Changelog ==

= 1.0.3 =
* Fixed "API token is not set" error on the Test Connection button when the API key is managed via Settings → Connectors. The plugin now reads the key from the AI Client registry as a final fallback so all credential sources are covered.
* Updated model selector to note that different models produce different results, with the default recommended for most use cases.
* Added screenshots and model troubleshooting guidance.

= 1.0.2 =
* Fixed "It was not possible to connect to the provider using this key" error on the Connectors screen. The plugin now reads the API key from the AI Client registry so keys set via Settings → Connectors are correctly recognised. Token validation no longer requires the Account ID to be saved first.

= 1.0.1 =
* Fixed broken Cloudflare dashboard link — updated to the Workers AI API quick-start page.

= 1.0.0 =
* Initial release.
* Text generation support — live model list fetched from your Cloudflare account, with a built-in fallback.
* Settings page with Account ID and API token fields.
* Connection test button.
* Support for wp-config.php constants and environment variables.
