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, ]; } }