refactor: убрать дубли, развести слои, разбить тесты

- Support/FileWalker: один обход каталога вместо двух копий
- Support/Bytes: форматирование размера вместо трёх копий
- Support/UrlGuard: SSRF-проверки вынесены из HttpRequestTool
- Support/AtomicWriter: запись вынесена из FileSandbox
- HttpRequest: normalizeUrl/resolveRedirect — одна реализация URL-логики
- AiClient поверх HttpTransport::request: один транспорт, одна TLS-логика
  (было две копии cURL-настроек), подсказка про AI_CAINFO переехала в HttpTransport
- Http переименован в HttpTransport: транспорт/значение/инструмент больше
  не называются одинаково
- Agent: удалён мёртвый withDefaults — ядро больше не зависит от tools
- tests: раннер + support + cases по темам (agent, files, http, shell,
  logging, tools, architecture)
- архитектурные тесты: src не импортирует Tools, tools не создают AiClient
  и не вызывают cURL напрямую
- 100 тестов
This commit is contained in:
rsgrinko committed 2026-11-12 23:13:45 +03:00
1 parent 74bb23c159
commit 76b3be7115
28 files changed
+2022 -1749

No files matched your search

+4 -41
View File
@@ -5,13 +5,14 @@ declare(strict_types=1);
namespace AiSdk\Tools;
use AiSdk\Tool;
use AiSdk\Tools\Support\Bytes;
use AiSdk\Tools\Support\FileWalker;
/**
* Поиск файлов по маске имени внутри песочницы.
*/
final class FindFilesTool implements Tool
{
private const IGNORED = ['.git', '.idea', '.vscode', 'vendor', 'node_modules', 'var'];
public function __construct(
private readonly FileSandbox $sandbox,
@@ -78,7 +79,7 @@ final class FindFilesTool implements Tool
$truncated = false;
try {
$this->eachEntry($start, function (string $path) use (&$found, $pattern, $limit): void {
FileWalker::walk($start, function (string $path) use (&$found, $pattern, $limit): void {
$relative = $this->sandbox->relative($path);
if (!fnmatch($pattern, $relative) && !fnmatch($pattern, basename($path))) {
@@ -86,7 +87,7 @@ final class FindFilesTool implements Tool
}
$size = @filesize($path);
$found[] = $relative . ' (' . $this->human($size === false ? 0 : $size) . ')';
$found[] = $relative . ' (' . Bytes::human($size === false ? 0 : $size) . ')';
if (count($found) >= $limit) {
throw new \RuntimeException('__limit__');
@@ -111,43 +112,5 @@ final class FindFilesTool implements Tool
/**
* @param callable(string): void $onFile
*/
private function eachEntry(string $directory, callable $onFile): void
{
$entries = @scandir($directory);
if ($entries === false) {
return;
}
foreach ($entries as $name) {
if ($name === '.' || $name === '..' || in_array($name, self::IGNORED, true)) {
continue;
}
$path = $directory . DIRECTORY_SEPARATOR . $name;
if (is_dir($path)) {
$this->eachEntry($path, $onFile);
continue;
}
if (is_file($path)) {
$onFile($path);
}
}
}
private function human(int $bytes): string
{
if ($bytes >= 1048576) {
return round($bytes / 1048576, 1) . ' МБ';
}
if ($bytes >= 1024) {
return round($bytes / 1024, 1) . ' КБ';
}
return $bytes . ' Б';
}
}