AI (developers)

Module packages/core/src/Modules/Ai. Every model call goes through Ai\Contracts\Assistant (bound to Assistant\GatedAssistant).

  • ask(AiFeature, AiRequest, ?userId): AiAnswer checks, in order: the feature is switched on, it has a route (a provider with a key, a URL where needed, and a model), and the day's and month's spend (from ai_calls.cost) is under its budgets. Otherwise it logs a refused call and throws Data\AiUnavailable with the reason (off, no_provider, budget_day, budget_month, no_price, busy); a provider error is logged failed and thrown as failed. Callers catch it and carry on without AI.

  • A budgeted feature needs a price for every model on its routes (else no_price), and its calls run one at a time under Cache::lock('ai-budget:<feature>') from the budget check to the log, so parallel calls cannot overspend together (at most one call over). Cost and the logged model are those configured for the route that answered, never the name the provider reports back (it may carry a date suffix the price table does not know).

  • Providers are built on demand with Laravel\Ai\Ai::build() (syncshop_<driver>) from Settings\AiSettings (keys encrypted; urls; features; prices in bani per million tokens; style). The feature's provider and its fallback are passed to prompt() as a failover list.

  • GatedAssistant::RULES open every system prompt; the feature's instructions and the shop's tone follow. Stored text goes in AiRequest::$data, each piece inside <data name="…"> with any <data/</data inside it neutralised, so it cannot close its block.

  • A schema (AiRequest::$schema) uses Assistant\StructuredShopAgent; the answer's structured array is validated by the SDK against it.

  • ai_calls holds metadata only: feature, provider, model, tokens, cost (null without a price), milliseconds, outcome, reason, user. Never the prompt or the answer.

  • Tests fake the model with ShopAgent::fake([...]) / StructuredShopAgent::fake(...) and assert with ShopAgent::assertPrompted(fn (AgentPrompt $p) => ...).

  • Settings → AI (admin.settings.ai.*, permission ai.manage, owner and administrators only): keys (write-only, kept out of flashed input by dontFlash('keys')), features, prices, usage, test bench.

  • Settings shapes on AiSettings use @phpstan-var only: spatie/laravel-settings reads a plain var tag and cannot type a mixed map.

  • Catalog features: Actions\DraftProductTexts::handle(feature, productId, locale, userId) reads the product through Catalog\Contracts\ProductTexts::source() (name, brand, category, specifications, fields, images) and asks for a structured answer (product copy: short_description + description; SEO: seo_title + seo_description; translation: every field the Romanian source has; alt text: one call per image without alt, the medium conversion attached with Image::fromStorage()). Each field becomes an ai_drafts row (current snapshot, proposed), replacing an older pending draft for the same field; with fill_empty on and the field empty, it is written at once (filled).

  • Actions\DecideDraft accepts (optionally edited, under a row lock, once) through ProductTexts::write() / writeAlt(), which save the model so the page is reindexed and search engines told; or rejects.

  • Jobs\DraftProducts drafts in bulk for ProductTexts::missing() and stops on any refusal except a provider failure.

  • Drafts\WordDiff is a word-level LCS diff (removals before additions; very long texts fall back to all-out/all-in).

  • Admin: admin.ai.drafts.* (Catalog → Propuneri AI, catalog.manage); the product edit page shows the AI card when a feature is available.

  • Disclosure: Assistant::providersInUse() (providers on the routes of available features; an OpenAI-compatible endpoint by its host) fills the {ai} placeholder of the legal pages (Storefront\Http\Controllers\PageController), with the sentence content.ai_processors; packages/core/resources/legal/ro/privacy.md carries it.

Tests: tests/Feature/Ai/AssistantTest.php, AiSettingsTest.php, DraftsTest.php, DisclosuresTest.php.