< Summary

Information
Class: Services.AiAssistant.AiThinkingStepsBuilder
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/AiAssistant/AiThinkingStepsBuilder.cs
Line coverage
100%
Covered lines: 127
Uncovered lines: 0
Coverable lines: 127
Total lines: 183
Line coverage: 100%
Branch coverage
100%
Covered branches: 136
Total branches: 136
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildPending(...)100%44100%
BuildCompleted(...)100%2828100%
SummarizeTask(...)100%88100%
GetDbStepLabel(...)100%7272100%
GetAiStepLabel(...)100%2424100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/AiAssistant/AiThinkingStepsBuilder.cs

#LineLine coverage
 1using OzonDomains;
 2using Services.ApiServices.OpenRouterApi.Models;
 3
 4namespace Services.AiAssistant;
 5
 6/// <summary>
 7/// Тексты шагов «Стас думает» в зависимости от распознанной задачи.
 8/// </summary>
 9public static class AiThinkingStepsBuilder
 10{
 11    public static List<AiProcessStepDto> BuildPending(AiQueryIntent? intent, string userMessage)
 12    {
 5413        var tool = intent?.ToolName;
 5414        var taskSummary = SummarizeTask(intent, userMessage);
 15
 5416        if (tool == "orders_filter")
 17        {
 318            return
 319            [
 320                new AiProcessStepDto
 321                {
 322                    Key    = "intent",
 323                    Label  = taskSummary,
 324                    Done   = false,
 325                    Status = "active"
 326                },
 327                new AiProcessStepDto
 328                {
 329                    Key    = "studio",
 330                    Label  = "Студия запроса: склад, клиент, период…",
 331                    Done   = false,
 332                    Status = "pending"
 333                },
 334                new AiProcessStepDto
 335                {
 336                    Key    = "db",
 337                    Label  = GetDbStepLabel(intent),
 338                    Done   = false,
 339                    Status = "pending"
 340                },
 341                new AiProcessStepDto
 342                {
 343                    Key    = "ai",
 344                    Label  = "Формирую ответ…",
 345                    Done   = false,
 346                    Status = "pending"
 347                }
 348            ];
 49        }
 50
 5151        return
 5152        [
 5153            new AiProcessStepDto
 5154            {
 5155                Key    = "intent",
 5156                Label  = taskSummary,
 5157                Done   = false,
 5158                Status = "active"
 5159            },
 5160            new AiProcessStepDto
 5161            {
 5162                Key    = "db",
 5163                Label  = GetDbStepLabel(intent),
 5164                Done   = false,
 5165                Status = "pending"
 5166            },
 5167            new AiProcessStepDto
 5168            {
 5169                Key    = "ai",
 5170                Label  = GetAiStepLabel(intent, userMessage),
 5171                Done   = false,
 5172                Status = "pending"
 5173            }
 5174        ];
 75    }
 76
 77    public static List<AiProcessStepDto> BuildCompleted(
 78        AiQueryIntent? intent,
 79        string userMessage,
 80        int orderCount,
 81        bool dbRan)
 82    {
 2083        var pending = BuildPending(intent, userMessage);
 2084        pending[0].Done = true;
 2085        pending[0].Status = "done";
 2086        pending[0].Label = intent != null
 2087            ? $"Распознано: {AiOrderAnalysisService.GetToolLabel(intent.ToolName)}"
 2088            : "Запрос понят";
 89
 2090        var dbStepIndex = intent?.ToolName == "orders_filter" ? 2 : 1;
 2091        var aiStepIndex = intent?.ToolName == "orders_filter" ? 3 : 2;
 92
 2093        if (intent?.ToolName == "orders_filter" && pending.Count > 1)
 94        {
 195            pending[1].Done = true;
 196            pending[1].Status = "done";
 197            pending[1].Label = "Спецификация фильтра готова";
 98        }
 99
 20100        if (dbRan)
 101        {
 18102            pending[dbStepIndex].Done = true;
 18103            pending[dbStepIndex].Status = "done";
 18104            if (intent?.ToolName == "order_detail" && orderCount > 0)
 1105                pending[dbStepIndex].Label = $"Загружен заказ + товар + остатки 1С + синхронизация + Ozon API ({orderCou
 17106            else if (orderCount > 0)
 2107                pending[dbStepIndex].Label = $"Данные загружены ({orderCount} заказ(ов))";
 108            else
 15109                pending[dbStepIndex].Label = GetDbStepLabel(intent).TrimEnd('…') + " — готово";
 110        }
 111        else
 112        {
 2113            pending[dbStepIndex].Done = true;
 2114            pending[dbStepIndex].Status = "done";
 2115            pending[dbStepIndex].Label = intent != null
 2116                ? GetDbStepLabel(intent).TrimEnd('…') + " — готово"
 2117                : "Запрос к базе не требуется";
 118        }
 119
 20120        pending[aiStepIndex].Done = true;
 20121        pending[aiStepIndex].Status = "pending";
 20122        pending[aiStepIndex].Label = "Формирую ответ…";
 20123        return pending;
 124    }
 125
 126    private static string SummarizeTask(AiQueryIntent? intent, string userMessage)
 127    {
 54128        if (intent == null)
 5129            return "Анализирую вопрос…";
 130
 49131        if (intent.OrderId.HasValue)
 3132            return $"Задача: заказ #{intent.OrderId.Value}";
 133
 46134        if (!string.IsNullOrWhiteSpace(intent.ShipmentNumber))
 1135            return $"Задача: заказ {intent.ShipmentNumber}";
 136
 45137        var trimmed = userMessage.Trim();
 45138        if (trimmed.Length > 72)
 1139            trimmed = trimmed[..72] + "…";
 140
 45141        return $"Задача: {trimmed}";
 142    }
 143
 70144    private static string GetDbStepLabel(AiQueryIntent? intent) => intent?.ToolName switch
 70145    {
 7146        "order_detail"           => "Загружаю заказ, товар, остатки 1С, синхронизацию Ozon и Ozon API…",
 4147        "order_product"          => "Товар, WarehouseStock и движения 1С…",
 4148        "order_history"          => "Загрузка GetOrderHistory…",
 4149        "user_actions_today"     => "Журнал изменений за сегодня…",
 3150        "order_history_recent"   => "Последние записи orders_history…",
 4151        "orders_today"           => "Заказы, принятые сегодня…",
 3152        "overdue_orders"         => "Поиск просроченных заказов…",
 3153        "unprofitable_orders"    => "Поиск невыгодных заказов…",
 3154        "cancelled_orders_today" => "Отменённые заказы за сегодня…",
 3155        "no_supplier_orders"     => "Заказы без поставщика…",
 3156        "orders_filter"          => "Поиск заказов по критериям…",
 4157        "orders_summary"         => "Сводка по статусам…",
 3158        "transaction_types_info" => "Справочник типов транзакций…",
 4159        "bitrix_stock_article"   => "Синхронизация Ozon и 1С…",
 4160        "ozon_live_product"      => "Запрос к Ozon Seller API…",
 14161        _                        => "Запрос к базе данных…"
 70162    };
 163
 164    private static string GetAiStepLabel(AiQueryIntent? intent, string userMessage)
 165    {
 51166        if (intent?.ToolName is "order_detail" or "order_product")
 10167            return "Анализирую заказ, движения 1С, данные Ozon API и формирую вывод…";
 168
 41169        if (intent?.ToolName == "order_history")
 3170            return "Анализирую историю изменений и формирую вывод…";
 171
 38172        if (intent?.ToolName == "bitrix_stock_article")
 3173            return "Анализирую остатки, цены и синхронизации Ozon…";
 174
 35175        if (intent?.ToolName == "ozon_live_product")
 3176            return "Запрашиваю актуальные остатки и цены у Ozon API…";
 177
 32178        if (intent?.ToolName == "user_actions_today")
 3179            return "Сводка по действиям пользователей…";
 180
 29181        return "Формирую ответ на ваш вопрос…";
 182    }
 183}