# AI (T10: A1 and A2)

AI on WPFactory AI credits through the bundled library `wpfactory/wpfactory-ai` (root `composer.json`, loaded from
`vendor/autoload.php` for free and Pro). Product id `free-shipping-bar` ("Free Shipping Bar"), registered on `init`.
Nothing is sent until an administrator connects the site on WPFactory > AI Credits, and nothing is sent until someone
clicks an AI button.

## Files

- `class-alg-wc-alfs-ai.php`: the module (returns its instance; `get_module( 'ai' )`). Library wrapper (`complete()`,
  `refund()`, `reject()`), REST routes, the Status card, the Amounts and Messages buttons, the header credits pill.
- `class-alg-wc-alfs-ai-stats.php`: local order numbers per zone, free and without AI, cached 12 hours.
- `class-alg-wc-alfs-ai-threshold.php`: A1 "Suggest an amount with AI" (`claude-sonnet-5-5`, 900 tokens, task
  `threshold-advisor`), "Use this amount" and Undo (30 days).
- `class-alg-wc-alfs-ai-messages.php`: A2 "Write with AI" (`claude-haiku-5-5`, 700 tokens per language, task
  `message-writer`), placeholder check.
- `assets/js/alfs-ai.js`, `assets/css/alfs-ai.css` (scoped `.alfs-app`), loaded on `admin.php?page=alfs`.

## REST (`alfs/v1`, `manage_woocommerce` plus the `wp_rest` nonce)

- `GET ai/stats` (`fresh=1` counts again): numbers and the card body HTML.
- `POST ai/threshold`: one AI call for all zones with 30 or more orders; zones with fewer get "too little data"
  without AI. A bad answer is refunded.
- `POST ai/threshold/apply` `{ zone_id, amount }`: writes the zone's Free shipping method `min_amount`, logs the old
  value (option `alg_wc_left_to_free_shipping_ai_threshold_log`, not autoloaded).
- `POST ai/threshold/undo` `{ id }`: puts the old value back, unless the amount was changed again since.
- `POST ai/messages` `{ tone, languages[] }`: one call per language; an answer that lost or invented a `%placeholder%`
  is rejected and refunded.

## Hooks

- Filter `alfs_ai_complete` ( `$result, $system, $prompt, $task` ): return a string (the answer) or a `WP_Error` to
  skip the WPFactory AI call (tests, or another AI service). While something is hooked here the AI buttons are active.
- Filter `alfs_ai_request_body` ( `$body, $model` ): the Messages API body sent to WPFactory AI.
- Action `alfs_ai_rejected` ( `$why, $refunded, $result` ): an AI answer was not used.

## Readme: External services (paste into `== External services ==`, word for word)

This plugin can use WPFactory AI (https://ai.wpfactory.com), a service by WPFactory, for its AI features, paid with WPFactory credits. Nothing is sent until a site administrator connects the site on the AI Credits screen with an email address and a 6-digit code sent to it. When connecting, the site address and that email address are sent. After that, the text of each AI request the plugin makes is sent when a user starts it; WPFactory AI passes it to Anthropic, its AI provider, to produce the answer. WPFactory AI keeps only numbers about each request (tokens, credits, time), never its content. Terms: https://wpfactory.com/terms-conditions/#wpfactory-ai Privacy: https://wpfactory.com/privacy-policy/#wpfactory-ai

What this plugin sends with a request: order amounts per shipping zone (counts, typical values, shares; no names,
emails, addresses or products) for a suggested amount; the store name, store country, currency and the current bar
texts for message drafts.

## Uninstall

Never delete the option `wpfactory_ai_connection` (shared by every WPFactory AI plugin on the site).
