Files
onboard/app/Services/Ai/AiDispatcherService.php
brusnitsyn a9e032f658 Добавлен сервис AI
Добавлен ai агент для формирования и проверки статистики
Добавлен ai агент для поддержки
2026-07-20 16:58:21 +09:00

59 lines
1.9 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
namespace App\Services\Ai;
use App\Ai\Agents\RouterAgent;
use App\Ai\Agents\StatisticsAnalyst;
use App\Ai\Agents\SupportAgent;
use App\Services\DutyReportService;
class AiDispatcherService
{
public function __construct(
private readonly DutyReportService $reportService
) {}
public function dispatch(string $question, string $role = 'guest'): array
{
// 1. Маршрутизация
$router = new RouterAgent();
$routerResponse = $router->prompt($question)->text; // получаем текстовый ответ
// Парсим JSON из ответа роутера
$routeData = json_decode($routerResponse, true);
$route = $routeData['route'] ?? 'other';
// 2. Выбор агента
$agent = match ($route) {
'stats' => new StatisticsAnalyst($this->reportService, $role),
'support' => new SupportAgent(),
default => null,
};
if (!$agent) {
return [
'answer' => 'Извините, я не могу ответить на этот вопрос. Пожалуйста, уточните, что вы имеете в виду.',
'structured' => null,
];
}
// 3. Запрос к специалисту
$responseText = $agent->prompt($question)->text;
// Пытаемся распарсить JSON (если агент вернул структурированный ответ)
$parsed = json_decode($responseText, true);
if (json_last_error() === JSON_ERROR_NONE && isset($parsed['text'])) {
return [
'answer' => $parsed['text'],
'structured' => $parsed['data'] ?? null,
];
}
// Если не JSON возвращаем как есть
return [
'answer' => $responseText,
'structured' => null,
];
}
}