Gtstudio_AiAgents

March 24, 2026 · View on GitHub

Agent and tool registry for Magento 2. Provides a full admin UI to define, store, and execute AI agents backed by Gtstudio_AiConnector.

Preview

AiAgents — creating an agent, attaching tools, and running it on-demand

AI Studio Ecosystem

Part of the AI Studio suite for Magento 2. See all modules:

ModuleRepositoryDescription
Gtstudio_AiConnectormodule-aiconnectorCore AI provider abstraction
Gtstudio_AiAgents(this module)Agent & tool orchestration, cron scheduling, execution log
Gtstudio_AiWidgetsmodule-ai-widgetsFloating admin chat widget + PageBuilder AI generator
Gtstudio_AiDataQuerymodule-ai-data-queryNatural-language store analytics (privacy-first)
Gtstudio_AiKnowledgeBasemodule-ai-knowledge-baseDocument upload & RAG retrieval for agents
Gtstudio_AiDashboardmodule-ai-dashboardAI-powered KPI dashboard with ML insights

What It Does

  • Admin CRUD for Agents — each agent has a system prompt (background, steps, output instructions), a list of tools it can call, and optional cron scheduling
  • Admin CRUD for Tools — reusable tool definitions that the NeuronAI framework exposes to agents
  • Run Immediately — execute any agent from its edit page with a custom input and see the result in a modal
  • Cron Scheduling — enable a cron expression per agent so it runs automatically on a schedule
  • Execution Log — every run is recorded with status, input, output, token counts, and trigger source
  • Auto-pruning — execution log entries are deleted after a configurable number of days

Requirements

  • Magento 2.4.4+
  • PHP 8.1+
  • Gtstudio_AiConnector enabled and configured

Installation

composer require gtstudio/module-ai-agents
php bin/magento module:enable Gtstudio_AiAgents
php bin/magento setup:upgrade

Admin UI

AI Studio → Agents & Tools

Agents

FieldDescription
CodeUnique machine-readable identifier, e.g. customer_support
DescriptionInternal label — not sent to the LLM
BackgroundAgent identity (maps to NeuronAI SystemPrompt::$background)
StepsInternal reasoning steps (SystemPrompt::$steps)
Output InstructionsResponse format rules (SystemPrompt::$output)
ToolsMulti-select of registered tools
Additional ConfigsOptional JSON for advanced NeuronAI overrides

Scheduling

FieldDescription
Enable CronToggle to activate scheduled execution
Cron ExpressionStandard 5-field expression, e.g. 0 * * * *
Cron Input TemplateInput sent to the agent on each scheduled run. Supports {{date}}, {{datetime}}, {{timestamp}}, {{store_name}}, {{store_url}}

Execution Log

AI Studio → Agents & Tools → Execution Log

Lists every agent run with status, token counts, trigger source (manual / cron), and error messages.

Log retention is controlled by: Stores → Configuration → aiagents → Execution → Log Retention Days (default: 30)

Running an Agent Programmatically

use Gtstudio\AiAgents\Api\AgentRunInterface;

class MyService
{
    public function __construct(private readonly AgentRunInterface $agentRunner) {}

    public function run(): void
    {
        $result = $this->agentRunner->run('customer_support', 'How do I reset my password?');

        echo $result['content'];        // agent reply
        echo $result['tokens'];         // total tokens used
        echo $result['input_tokens'];   // input tokens
        echo $result['output_tokens'];  // output tokens
        echo $result['model'];          // model used
        echo $result['provider'];       // provider key
    }
}

Extensibility

Registering a Tool Executor

Tools define what the agent can call; executors contain the actual PHP logic that runs when an agent chooses a tool.

  1. Create the tool record in AI Studio → Agents & Tools → Tools with a unique code (e.g. get_order_status).
  2. Implement Gtstudio\AiAgents\Api\ToolExecutorInterface:
namespace Vendor\Module\Model\Tool;

use Gtstudio\AiAgents\Api\ToolExecutorInterface;

class GetOrderStatusExecutor implements ToolExecutorInterface
{
    /**
     * @param array $parameters  Parameters chosen by the LLM from the tool's property definitions.
     * @return string|array      Result shown to the LLM (string) or returned as data (array).
     */
    public function execute(array $parameters): string|array
    {
        $orderId = $parameters['order_id'] ?? null;
        // ... fetch order, return status string
        return "Order #{$orderId} is currently: Shipped.";
    }
}
  1. Register it in etc/di.xml:
<type name="Gtstudio\AiAgents\Model\Tool\ToolExecutorPool">
    <arguments>
        <argument name="executors" xsi:type="array">
            <item name="get_order_status" xsi:type="object">
                Vendor\Module\Model\Tool\GetOrderStatusExecutor
            </item>
        </argument>
    </arguments>
</type>

The key (get_order_status) must match the code field stored in the gtstudio_ai_tools table.

Using AgentExecutionService Directly

For advanced use-cases (custom trigger sources, post-processing), inject AgentExecutionService:

use Gtstudio\AiAgents\Model\AgentExecutionLogModel;
use Gtstudio\AiAgents\Model\Service\AgentExecutionService;

$log = $this->executionService->execute(
    'my_agent_code',
    'Input text here',
    AgentExecutionLogModel::TRIGGERED_MANUAL  // or TRIGGERED_CRON
);

if ($log->getData('status') === AgentExecutionLogModel::STATUS_SUCCESS) {
    $output = $log->getData('output');
}

Cron Expression Syntax

The built-in matcher supports the five standard fields (minute, hour, day, month, weekday):

PatternMeaning
* * * * *Every minute
0 * * * *Every hour
0 0 * * *Daily at midnight
*/15 * * * *Every 15 minutes
0 9 * * 1-5Weekdays at 09:00
0 6,18 * * *06:00 and 18:00 daily

Template Variables in Cron Input

TokenReplaced With
{{date}}2026-03-08
{{datetime}}2026-03-08 14:00:00
{{timestamp}}Unix timestamp
{{store_name}}Default store view name
{{store_url}}Default store base URL

Example: Generate a daily sales summary for {{date}} on {{store_name}}.

Database Tables

TablePurpose
gtstudio_ai_agentAgent definitions
gtstudio_ai_toolsTool definitions
gtstudio_ai_agent_execution_logExecution history