Files
php-ai-sdk/examples/14-hooks.php
T
rsgrinko 204d449519 refactor: убрать onAnswer из хуков агента
- onAnswer дублировал возврат Agent::ask() — убран
- остались onStep, onToolStart, onToolEnd, shouldStop
- пример 14 печатает ответ из возврата ask()
- 116 тестов
2026-11-13 00:38:11 +03:00

70 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/**
* Пример 14. Хуки агента: прогресс, инструменты, остановка.
*
* Инструменты: get_time, calculator.
* Флаги: hooks (AgentHooks с onStep, onToolStart, onToolEnd, shouldStop).
* shouldStop проверяется между раундами.
*
* Хуки — это колбэки без логики: агент вызывает их в ходе работы, а вы решаете,
* что делать — печатать прогресс, писать лог, показать кнопку «стоп».
*
* Запуск:
* php examples/14-hooks.php
*/
require dirname(__DIR__) . '/bootstrap.php';
use AiSdk\Agent;
use AiSdk\AgentHooks;
use AiSdk\AiClient;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\TimeTool;
$tools = (new ToolRegistry())->add(new TimeTool(), new CalculatorTool());
// 1. Полный набор хуков: как это выглядит в консоли.
$hooks = new AgentHooks(
onStep: static fn (int $step, int $max) => print("шаг {$step}/{$max}" . PHP_EOL),
onToolStart: static fn (string $name, array $args) => print(
' → ' . $name . ' ' . json_encode($args, JSON_UNESCAPED_UNICODE) . PHP_EOL
),
onToolEnd: static fn (array $step) => print(
' ← ' . mb_substr(str_replace("\n", ' ', $step['output']), 0, 70) . PHP_EOL
),
);
$agent = new Agent(
client: AiClient::fromEnv(timeout: 120.0),
tools: $tools,
systemPrompt: 'Отвечай кратко, одной строкой.',
maxRounds: 4,
hooks: $hooks,
);
echo '1) обычный ход' . PHP_EOL;
echo 'ответ: ' . $agent->ask('Который час в Москве и сколько будет 2+2?') . PHP_EOL;
// 2. Остановка: shouldStop проверяется между раундами, поэтому история
// остаётся целой, а модель больше не вызывается.
echo PHP_EOL . '2) shouldStop: остановка после первого раунда' . PHP_EOL;
$stopped = new Agent(
client: AiClient::fromEnv(timeout: 120.0),
tools: $tools,
systemPrompt: 'Отвечай кратко.',
maxRounds: 4,
hooks: new AgentHooks(
onStep: static fn (int $step, int $max) => print("шаг {$step}/{$max}" . PHP_EOL),
shouldStop: static fn (): bool => true,
),
);
echo 'ответ: ' . $stopped->ask('Который час в Москве?') . PHP_EOL;