feat: хуки агента — шаги, инструменты, ответ, остановка

- AgentHooks: onStep, onToolStart, onToolEnd, onAnswer, shouldStop
- Agent принимает hooks (и в fromEnv); исключение в колбэке не ломает ответ
- остановка проверяется между раундами, история остаётся валидной
- пример 14: прогресс в консоли и остановка после первого раунда
- 116 тестов
This commit is contained in:
rsgrinko committed 2026-11-13 00:25:45 +03:00
1 parent ab081a4dcd
commit 4c25111c0e
7 files changed
+332

No files matched your search

+2
View File
@@ -18,9 +18,11 @@
| `11-json.php` | инструменты без модели | `json_query`, `convert_units`, `date_calc`, `uuid`, `hash_text` | — |
| `12-logging.php` | логи запросов | `get_time`, `calculator` | `verbose`, `sink` |
| `13-code-task.php` | агент пишет код и проверяет тестом | `read_file`, `write_file`, `replace_in_file`, `list_dir`, `run_command` | `workdir`, `allow`, `maxRounds` |
| `14-hooks.php` | прогресс и остановка | `get_time`, `calculator` | `hooks`: onStep, onToolStart, onToolEnd, onAnswer, shouldStop |
`10-http.php` и `11-json.php` работают без модели и без сети — их можно запускать
где угодно. Остальные обращаются к модели из `.env`.
+14
View File
@@ -82,6 +82,19 @@ $agent->setSystemPrompt('новый промпт'); // на ходу, истор
$agent->systemPrompt(); // текущий промпт
$agent->reset();
// Хуки хода
$agent = new Agent(
client: $client,
tools: $tools,
hooks: new AgentHooks(
onStep: fn (int $step, int $max) => print("шаг {$step}/{$max}\n"),
onToolStart: fn (string $name, array $args) => print("→ {$name}\n"),
onToolEnd: fn (array $s) => print(' ' . mb_substr($s['output'], 0, 60) . "\n"),
onAnswer: fn (string $text) => print("ответ: {$text}\n"),
shouldStop: fn (): bool => false,
),
);
// Агент целиком из .env
$agent = Agent::fromEnv($tools, 'Отвечай кратко.');
@@ -104,3 +117,4 @@ $tools->add(new FinishTool());