Build an assistant tool
On this page

Build an assistant tool

Let admins look up store data or confirm a change.

Add a read tool

Use an enabled Magento module. The example uses the namespace Vendor\StoreGuide and the existing Use AI Assistant permission. Include Arlanix_Ai in your module's etc/module.xml sequence.

  1. Create etc/arlanix_ai.xml in your module. The declaration names the tool and specifies which keys the provider may see. Each tool needs a unique name.

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:module:Arlanix_Ai:etc/arlanix_ai.xsd">
        <tool name="vendor_display_currency" access="read" resource="Arlanix_Ai::use"
              title="Default display currency" service="Vendor\StoreGuide\Model\DisplayCurrency" method="read">
            <description>Get the default display currency for the current store view.</description>
            <field name="currency" privacy="public"/>
        </tool>
    </config>
    
  2. Create Model/DisplayCurrency.php. The assistant supplies the current store view as $storeId.

    <?php
    declare(strict_types=1);
    
    namespace Vendor\StoreGuide\Model;
    
    use Magento\Framework\App\Config\ScopeConfigInterface;
    use Magento\Store\Model\ScopeInterface;
    
    class DisplayCurrency
    {
        public function __construct(private readonly ScopeConfigInterface $scopeConfig)
        {
        }
    
        public function read(int $storeId): array
        {
            $currency = $this->scopeConfig->getValue('currency/options/default', ScopeInterface::SCOPE_STORE, $storeId);
    
            return ['currency' => (string)$currency];
        }
    }
    
  3. Deploy the module.

  4. Clean the configuration cache with bin/magento cache:clean config.

  5. Ask What is the default display currency for this store view?

Add a write tool

Use your module's existing records grid, edit route, repository and permission to create records.

  1. Declare the action under config in etc/arlanix_ai.xml.

    <tool name="vendor_create_record" access="write" scope="page" resource="Vendor_Module::record"
          title="Create record" service="Vendor\Module\Model\CreateRecord" method="execute" confirm-label="Create record">
        <description>Create one record with the title the admin requests.</description>
        <param name="title" type="string" required="true" editable="true" edit-label="Title"
               min-length="1" max-length="80"/>
        <open-route route="vendor/record/edit" param="id" resource="Vendor_Module::record"/>
        <field name="open" privacy="public"/>
        <field name="label" privacy="public"/>
        <field name="target" privacy="public"/>
    </tool>
    
  2. Create Model/CreateRecord.php with your record factory and repository.

    <?php
    declare(strict_types=1);
    
    namespace Vendor\Module\Model;
    
    use Vendor\Module\Api\Data\RecordInterfaceFactory;
    use Vendor\Module\Api\RecordRepositoryInterface;
    
    class CreateRecord
    {
        public function __construct(
            private readonly RecordInterfaceFactory $recordFactory,
            private readonly RecordRepositoryInterface $recordRepository
        ) {
        }
    
        public function preview(string $title): array
        {
            return ['title' => 'Create record', 'lines' => ['Title: ' . $title]];
        }
    
        public function execute(string $title): array
        {
            $record = $this->recordFactory->create();
            $record->setTitle($title);
            $saved = $this->recordRepository->save($record);
    
            return ['open' => ['label' => 'Open record', 'target' => 'edit:vendor/record/edit:' . $saved->getId()]];
        }
    }
    

The card shows an editable title. Create record saves it. On the grid, Open record links to the new record.

Offer the tool on a page

Declare the grid handle and skill in the same file. On the grid, choose Create a record. Enter the title.

<page code="vendor_records" resource="Vendor_Module::record" title="Records">
    <handle name="vendor_record_index"/>
    <tool name="vendor_create_record"/>
    <skill label="Create a record" resource="Vendor_Module::record">Create a record with the title I supply.</skill>
</page>

Tool declaration

Element or attribute Meaning
tool Declare under config. Use a unique lowercase name, an admin-facing title and an existing ACL resource.
access Use read for lookups or write for actions that require confirmation and Arlanix_Ai::act.
scope Defaults to global. Choose page and reference the tool on each permitted page or form to restrict where it is offered.
service, method PHP class and public, non-static method. Return an array of plain data. Enforce record and scope access in the service.
description, instructions Describe when to call the tool and optionally how to interpret a read result.
field Declare each allowed result key by name, including nested keys. Use privacy="public" for shareable values or personal for values the provider sees as placeholders.
max-chars For read tools only. Maximum result size in characters, including the wrapper (1 to 16,000).
requires-module Offer the tool only when the named Magento module is enabled.
preview-method For writes, defaults to preview. Return a card title and a list of lines before making any change.
confirm-label, irreversible Name the confirmation button and require the extra acknowledgment for an irreversible write.
uses-images Mark an image tool. The image settings and limits determine when the assistant can offer it.
open-route On a write tool, allow a record destination by its three-part admin route, ID param and ACL resource.

For a write, keep the preview free of side effects. Include the affected records and any consequences. Recheck access and record state when the admin confirms. Return only safe text in errors.

Inside a customer lookup tool, mark the email as personal so the provider receives a placeholder.

<field name="customer_id" privacy="public"/>
<field name="email" privacy="personal"/>

Inputs

Element or attribute Meaning
param Declare a named input. Match a snake_case XML name to a camelCase PHP argument.
type Choose string, integer, number, boolean or array (a list of strings).
required, description Require the argument or describe its purpose. Give optional PHP arguments defaults.
min-length, max-length Bound the length of a string.
option Add one child per accepted string, with a value attribute.
min, max Set inclusive numeric bounds or bound the number of array items.
item-max-length, unique Limit individual string items or require distinct items in an array.
editable, edit-label Let the admin edit a write tool's string input on the card and give it a label.

Reserve storeId, adminUserId, chatContext and previewData for assistant-supplied arguments. CLI calls have no admin user.

Pages and skills

Element or attribute Meaning
page For a new admin page, declare a unique code, its title, ACL resource and layout handle by name.
tool under a page or form Reference a declared tool by name. To contribute to an existing page or form, reuse its code and add only tool references or skills.
data Map a request param to a named page value with type integer or string.
context-service, context-method Supply additional page context from a PHP service that returns text.
feature, alternate-resource, requires-module Set the usage label, allow an alternative page permission or require an enabled module.
link Identify the page's destination as menu:Vendor_Module::item or config:section_id.
skill Supply prompt text and a label, with optional description and ACL resource. Declare it globally or inside a page or form.

To make form fields editable, see Build an assistant form.

Check your declaration

  1. Run bin/magento arlanix:ai:tool:verify.
  2. Check for [ok] tool vendor_display_currency.
  3. For the write example, also check for [ok] tool vendor_create_record (write) and [ok] page vendor_records.
  4. Run bin/magento arlanix:ai:tool:verify vendor_display_currency to inspect its definition.
  5. Run bin/magento arlanix:ai:tool:verify vendor_display_currency '{}' to read the default store view's currency.

To run the command for an existing store view with ID 2, add --store 2.