AI (developers)
Module packages/core/src/Modules/Ai. Every model call goes through Ai\Contracts\Assistant (bound to Assistant\GatedAssistant).
-
ask(AiFeature, AiRequest, ?userId): AiAnswerchecks, 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 (fromai_calls.cost) is under its budgets. Otherwise it logs arefusedcall and throwsData\AiUnavailablewith the reason (off,no_provider,budget_day,budget_month,no_price,busy); a provider error is loggedfailedand thrown asfailed. 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 underCache::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>) fromSettings\AiSettings(keysencrypted;urls;features;pricesin bani per million tokens;style). The feature's provider and its fallback are passed toprompt()as a failover list. -
GatedAssistant::RULESopen every system prompt; the feature's instructions and the shop's tone follow. Stored text goes inAiRequest::$data, each piece inside<data name="…">with any<data/</datainside it neutralised, so it cannot close its block. -
A schema (
AiRequest::$schema) usesAssistant\StructuredShopAgent; the answer'sstructuredarray is validated by the SDK against it. -
ai_callsholds 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 withShopAgent::assertPrompted(fn (AgentPrompt $p) => ...). -
Settings → AI (
admin.settings.ai.*, permissionai.manage, owner and administrators only): keys (write-only, kept out of flashed input bydontFlash('keys')), features, prices, usage, test bench. -
Settings shapes on
AiSettingsuse@phpstan-varonly: 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 throughCatalog\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, themediumconversion attached withImage::fromStorage()). Each field becomes anai_draftsrow (currentsnapshot,proposed), replacing an older pending draft for the same field; withfill_emptyon and the field empty, it is written at once (filled). -
Actions\DecideDraftaccepts (optionally edited, under a row lock, once) throughProductTexts::write()/writeAlt(), which save the model so the page is reindexed and search engines told; or rejects. -
Jobs\DraftProductsdrafts in bulk forProductTexts::missing()and stops on any refusal except a provider failure. -
Drafts\WordDiffis 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 sentencecontent.ai_processors;packages/core/resources/legal/ro/privacy.mdcarries it.
Tests: tests/Feature/Ai/AssistantTest.php, AiSettingsTest.php, DraftsTest.php, DisclosuresTest.php.