feat: LoggingClient — декоратор с логом запросов и ответов
- пишет строку на каждый запрос и ответ: сообщения, инструменты, tool_calls, токены, время - свой приёмник через sink (по умолчанию error_log) - verbose печатает содержимое сообщений и ответа - ошибки логируются и пробрасываются, декоратор ничего не глотает - пример 12-logging.php, 97 тестов
This commit is contained in:
1 parent
9f02a0c00b
commit
74bb23c159
4 files changed
+286
-2
No files matched your search
@@ -19,6 +19,7 @@ use AiSdk\ChatClient;
|
||||
use AiSdk\ChatResult;
|
||||
use AiSdk\Http;
|
||||
use AiSdk\HttpRequest;
|
||||
use AiSdk\LoggingClient;
|
||||
use AiSdk\Tool;
|
||||
use AiSdk\ToolRegistry;
|
||||
use AiSdk\Tools\CalculatorTool;
|
||||
@@ -1283,6 +1284,91 @@ test('sandbox: ограниченный режим по-прежнему бло
|
||||
assertContains('не должен содержать', $result);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Логирование
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test('logging: прокидывает ответ и пишет строки', function (): void {
|
||||
$lines = [];
|
||||
$client = new LoggingClient(
|
||||
new FakeClient([textResult('привет')]),
|
||||
sink: static function (string $line) use (&$lines): void {
|
||||
$lines[] = $line;
|
||||
},
|
||||
);
|
||||
|
||||
$result = $client->chat([['role' => 'user', 'content' => 'вопрос']]);
|
||||
|
||||
assertSame('привет', $result->text);
|
||||
assertSame(2, count($lines), 'ожидались две строки: запрос и ответ');
|
||||
assertContains('LLM →', $lines[0]);
|
||||
assertContains('LLM ←', $lines[1]);
|
||||
assertContains('текст 6 симв.', $lines[1]);
|
||||
});
|
||||
|
||||
test('logging: ошибка пишется и пробрасывается', function (): void {
|
||||
$broken = new class implements ChatClient {
|
||||
public function chat(array $messages, array $tools = [], array $options = []): ChatResult
|
||||
{
|
||||
throw new RuntimeException('сеть упала');
|
||||
}
|
||||
};
|
||||
|
||||
$lines = [];
|
||||
$client = new LoggingClient($broken, sink: static function (string $line) use (&$lines): void {
|
||||
$lines[] = $line;
|
||||
});
|
||||
|
||||
assertThrows(static fn () => $client->chat([['role' => 'user', 'content' => 'x']]));
|
||||
|
||||
$log = implode("\n", $lines);
|
||||
|
||||
assertContains('LLM ✗', $log);
|
||||
assertContains('сеть упала', $log);
|
||||
});
|
||||
|
||||
test('logging: verbose печатает сообщения и инструменты', function (): void {
|
||||
$lines = [];
|
||||
$client = new LoggingClient(
|
||||
new FakeClient([textResult('ок')]),
|
||||
sink: static function (string $line) use (&$lines): void {
|
||||
$lines[] = $line;
|
||||
},
|
||||
verbose: true,
|
||||
);
|
||||
|
||||
$tools = (new ToolRegistry())->add(new TimeTool())->definitions();
|
||||
$client->chat([['role' => 'user', 'content' => 'который час']], $tools);
|
||||
|
||||
$log = implode("\n", $lines);
|
||||
|
||||
assertContains('[0] user: который час', $log);
|
||||
assertContains('инструменты: get_time', $log);
|
||||
assertContains('ответ: ок', $log);
|
||||
});
|
||||
|
||||
test('logging: в агенте логирует каждый раунд', function (): void {
|
||||
$lines = [];
|
||||
$client = new LoggingClient(
|
||||
new FakeClient([
|
||||
toolCall('calculator', ['expression' => '2+2'], 'c1'),
|
||||
textResult('4'),
|
||||
]),
|
||||
sink: static function (string $line) use (&$lines): void {
|
||||
$lines[] = $line;
|
||||
},
|
||||
);
|
||||
|
||||
$agent = new Agent($client, (new ToolRegistry())->add(new CalculatorTool()), 'тест', 3);
|
||||
|
||||
assertSame('4', $agent->ask('посчитай'));
|
||||
|
||||
$log = implode("\n", $lines);
|
||||
|
||||
assertContains('tool_calls: calculator', $log);
|
||||
assertSame(4, count($lines), 'два раунда — по две строки');
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// run_command: оболочка
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1341,3 +1427,4 @@ exit($failed === 0 ? 0 : 1);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user