PHP code example of evolvex / laravel-agent-fabric

1. Go to this page and download the library: Download evolvex/laravel-agent-fabric library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

evolvex / laravel-agent-fabric example snippets


'routing' => [
    'models' => [
        'private-reasoning' => [
            'provider' => env('AI_PRIMARY_PROVIDER'),
            'model' => env('AI_PRIMARY_MODEL'),
            'capabilities' => ['text', 'reasoning', 'tools', 'structured_output'],
            'quality' => .92,
            'tool_accuracy' => .95,
            'reliability' => .95,
            'latency' => .70,
            'cost' => .55,
            'historical_eval' => .90,
            'regions' => ['uae'],
            'allowed_classifications' => ['public', 'internal', 'confidential', 'sensitive'],
            'privacy_tier' => 'private',
            'zero_retention' => true,
            'data_residency' => 'uae',
        ],
    ],
],

use Evolvex\AgentFabric\Agents\AgentBlueprint;

final class SupportAgent extends AgentBlueprint
{
    public function name(): string { return 'support'; }
    public function version(): string { return '2.0.0'; }
    public function goal(): string { return 'Resolve customer support cases end-to-end.'; }

    public function instructions(): string
    {
        return <<<'PROMPT'
Never claim an action succeeded without evidence.
Ask for clarification when tured_output'];
    }
}

'agents' => [
    'support' => App\Ai\Agents\SupportAgent::class,
],

use Evolvex\AgentFabric\Facades\AgentFabric;

$result = AgentFabric::agent('support')
    ->tenant($tenant->id)
    ->actor($customer->id, $customer::class)
    ->dataClassification('sensitive')
    ->region('uae')
    ->

$result = AgentFabric::agent('support')
    ->tenant($tenant->id)
    ->actor($customer->id, $customer::class)
    ->simulate()
    ->ask('Refund order 551');

use Evolvex\AgentFabric\Tools\QueryTool;
use Evolvex\AgentFabric\Tools\CommandTool;
use Evolvex\AgentFabric\Tools\RemoteOperationTool;

final class FindOrder extends QueryTool
{
    public function name(): string { return 'find_order'; }
    public function description(): string { return 'Read one accessible order.'; }
    public function risk(): ToolRisk { return ToolRisk::Read; }

    public function inputSchema(): array
    {
        return [
            'type' => 'object',
            'properties' => ['order_id' => ['type' => 'string']],
            'ToolResult::failure('Order not found.');
    }
}

if ($result->status === RunStatus::WaitingForApproval) {
    $approvalId = $result->metadata['approval_id'];

    AgentFabric::approve($approvalId, auth()->id());

    $result = AgentFabric::agent('support')
        ->tenant($tenant->id)
        ->actor($customer->id, $customer::class)
        ->resume($result->runId, $approvalId);
}

final class RefundWorkflow implements Workflow
{
    public function name(): string { return 'refund-order'; }
    public function version(): string { return '1.0.0'; }

    public function definition(): WorkflowDefinition
    {
        return (new WorkflowDefinition($this->name(), $this->version()))
            ->step('validate', ValidateRefund::class)
            ->approval('finance-approval', ['validate'])
            ->step('provider-refund', ExecuteRefund::class, ['finance-approval'], compensation: ReconcileRefund::class)
            ->step('update-order', MarkOrderRefunded::class, ['provider-refund'])
            ->verify('verify-state', VerifyRefundState::class, ['update-order']);
    }
}

$result = AgentFabric::workflow(
    'refund-order',
    new AgentContext($tenant->id, $customer->id, $customer::class),
    ['order_id' => '551']
);

interface Connector
{
    public function name(): string;
    public function schema(): ConnectorSchema;
    public function query(AgentContext $context, ConnectorQuery $query): ConnectorResult;
    public function execute(AgentContext $context, ConnectorAction $action): ConnectorResult;
}

$connector = new EloquentConnector('commerce', [
    'orders' => [
        'model' => Order::class,
        'tenant_column' => 'tenant_id',
        'fields' => ['id','customer_id','status','total','created_at'],
    ],
]);

final class EcommercePack extends AbstractDomainPack
{
    public function name(): string { return 'ecommerce'; }
    public function domain(): string { return 'ecommerce'; }

    public function register(PluginContext $context): void
    {
        $context->workflows->register(RefundWorkflow::class);
        $context->agents->register('support', SupportAgent::class);
    }
}

$envelope = new ConversationEnvelope(
    channel: ChannelType::WhatsApp,
    tenantId: $tenant->id,
    parts: [
        InputPart::text('Inspect this damage'),
        InputPart::image($imageUrl, 'image/jpeg'),
    ],
    actorId: $customer->id,
    actorType: $customer::class,
);

$result = AgentFabric::fromEnvelope('claims', $envelope);

new KnowledgeDocument(
    sourceType: 'policy',
    sourceKey: 'refund-v4',
    title: 'Refund Policy',
    content: $text,
    tenantId: $tenant->id,
    provenance: new KnowledgeProvenance(
        source: 'official-policy',
        sourceId: 'v4',
        authority: 1.0,
        observedAt: new DateTimeImmutable(),
    ),
    entities: [new EntityReference('policy', 'refund', $tenant->id)],
);

$gate = new ReleaseGate(
    minimums: ['task_success' => .97, 'citation_accuracy' => .98],
    maximums: ['unsafe_action_rate' => 0.0, 'p95_latency_ms' => 5000],
);

$gateResult = $gate->evaluate($metrics);

AgentFabric::deploy('support', '2.1.0', DeploymentMode::Canary, 5, $gateResult);

AgentFabric::agent('support')
    ->tenant($tenant->id)
    ->dataClassification('sensitive')
    ->region('uae')
    ->
bash
php artisan agent-fabric:replay <run-id>
bash
php artisan agent-fabric:doctor
php artisan agent-fabric:capabilities
php artisan agent-fabric:knowledge:sync 'App\Ai\Knowledge\ProductKnowledgeSource'
php artisan agent-fabric:run support "Where is order 551?" --tenant=tenant-1
php artisan agent-fabric:workflow:run refund-order --tenant=tenant-1 --input='{"order_id":"551"}'
php artisan agent-fabric:replay <run-id>
php artisan make:agent-fabric Support
php artisan make:agent-tool FindOrder
php artisan make:agent-workflow RefundOrder
bash
composer test
composer lint
composer analyse
php scripts/smoke.php