PHP code example of configured / race

1. Go to this page and download the library: Download configured/race 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/ */

    

configured / race example snippets


return [
    'poll_interval_ms' => 10,
    'execution_buffer_seconds' => 5,
];

$result = race(
    times: 3,
    timeout: 20,
    callback: fn () => Ai::agent()->prompt($prompt, timeout: 15),
    valid: fn ($result) => filled((string) $result),
    map: fn ($result) => (string) $result,
);

use Configured\Race\Exceptions\RaceFailedException;

$result = Race::times(3)
    ->timeout(20)
    ->valid(fn ($result) => filled($result))
    ->map(fn ($result) => (string) $result)
    ->failed(function (RaceFailedException $failure): string {
        logger()->warning('All race attempts failed.', [
            'attempts' => $failure->attempts(),
            'timed_out' => $failure->timedOut(),
            'failures' => $failure->failures(),
            'invalid_results' => $failure->invalidResults(),
        ]);

        return 'fallback';
    })
    ->run(fn () => Ai::agent()->prompt($prompt, timeout: 15));

$failure->attempts();       // Total attempts considered by the race.
$failure->timedOut();       // True when child processes were still running at timeout.
$failure->failures();       // Thrown attempt errors: attempt, class, message.
$failure->invalidResults(); // Results that completed but failed your valid callback.
bash
php artisan vendor:publish --tag=race-config