# Exemples

Les exemples exécutables sont dans `examples/` et chargent `vendor/autoload.php`.

## Client partagé

```php
$token = getenv('RELAI_APP_TOKEN');
if ($token === false || $token === '') {
    throw new RuntimeException('RELAI_APP_TOKEN is required.');
}

$relai = new RelaiClient(
    baseUrl: getenv('RELAI_BASE_URL') ?: 'http://localhost:3000',
    token: $token,
    appSlug: getenv('RELAI_APP_SLUG') ?: 'demo',
);
```

## Chat et reprise

```php
$first = $relai->chat()->send(['message' => 'Summarize the current project notes.']);
$next = $relai->chat()->send([
    'conversationId' => $first['conversationId'],
    'message' => 'List three next actions.',
]);
```

## Chat avec tools

```php
$response = $relai->chat()->send([
    'message' => 'Summarize the selected data.',
    'tools' => ['demo.period_summary'],
    'metadata' => [
        'periodStart' => '2026-07-01',
        'periodEnd' => '2026-07-31',
    ],
]);

echo $response['assistantMessage'] . PHP_EOL;
print_r($response['toolCalls'] ?? []);
```

## Structured

```php
$result = $relai->structured()->run([
    'messages' => [['role' => 'user', 'content' => 'Retourne un score.']],
    'schema' => [
        'type' => 'object',
        'properties' => ['score' => ['type' => 'integer']],
        'required' => ['score'],
    ],
]);
```

## Tool, conversation et run

```php
$tool = $relai->tools()->execute('myapp.get_document', ['documentId' => 'doc_123']);
$conversation = $relai->conversations()->get($first['conversationId']);
$run = $relai->runs()->get($first['runId']);
```

## Laravel

```php
final class MonthlyReportService
{
    public function __construct(private RelaiClient $relai) {}

    public function generate(): array
    {
        return $this->relai->chat()->send([
            'message' => 'Summarize open tasks.',
            'tools' => ['myapp.task_summary'],
        ]);
    }
}
```

## Erreur API

```php
try {
    $relai->runs()->list();
} catch (RelaiApiException $e) {
    logger()->warning('RelAI rejected the request', [
        'status' => $e->getStatusCode(),
        'body' => $e->getResponseBody(),
    ]);
}
```
