refactor: убрать onAnswer из хуков агента

- onAnswer дублировал возврат Agent::ask() — убран
- остались onStep, onToolStart, onToolEnd, shouldStop
- пример 14 печатает ответ из возврата ask()
- 116 тестов
This commit is contained in:
rsgrinko committed 2026-11-13 00:38:11 +03:00
1 parent 4c25111c0e
commit 204d449519
7 files changed
+15 -27

No files matched your search

+1 -3
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace AiSdk;
/**
* Диалог с моделью и цикл вызова инструментов.
*
@@ -173,7 +172,6 @@ final class Agent
$text = $result->text !== '' ? $result->text : '(модель вернула пустой ответ)';
$this->messages[] = ['role' => 'assistant', 'content' => $text];
$this->hooks?->answer($text);
return $text;
}
@@ -341,7 +339,6 @@ final class Agent
{
$text = $result !== '' ? $result : '(модель завершила работу без результата)';
$this->messages[] = ['role' => 'assistant', 'content' => $text];
$this->hooks?->answer($text);
return $text;
}
@@ -468,3 +465,4 @@ final class Agent
*/
}
+2 -9
View File
@@ -8,8 +8,8 @@ namespace AiSdk;
* Колбэки хода работы агента.
*
* Нужны, чтобы интерфейс (CLI, веб, лог) видел, что происходит, в реальном
* времени: номер шага, начало и конец инструмента, финальный ответ и запрос
* на остановку. Любой колбэк можно не задавать.
* времени: номер шага, начало и конец инструмента и запрос на остановку.
* Любой колбэк можно не задавать.
*
* $agent = new Agent(
* client: $client,
@@ -29,14 +29,12 @@ final class AgentHooks
* @param (\Closure(string, array<string, mixed>): void)|null $onToolStart имя инструмента и аргументы
* @param (\Closure(array{name: string, arguments: array<string, mixed>, output: string, ms: int, blocked: bool}): void)|null $onToolEnd
* запись трассировки после выполнения инструмента
* @param (\Closure(string): void)|null $onAnswer финальный текст ответа
* @param (\Closure(): bool)|null $shouldStop возвращает true — остановиться между раундами
*/
public function __construct(
public readonly ?\Closure $onStep = null,
public readonly ?\Closure $onToolStart = null,
public readonly ?\Closure $onToolEnd = null,
public readonly ?\Closure $onAnswer = null,
public readonly ?\Closure $shouldStop = null,
) {
}
@@ -62,11 +60,6 @@ final class AgentHooks
$this->fire($this->onToolEnd, $step);
}
public function answer(string $text): void
{
$this->fire($this->onAnswer, $text);
}
/**
* Пользователь попросил остановиться. Проверяется между раундами,
* чтобы история осталась согласованной.