| | | 1 | | @using System.ComponentModel |
| | | 2 | | @using UIBlazor.Models |
| | | 3 | | @inherits RadzenComponent |
| | | 4 | | |
| | | 5 | | <div class="chat-input-wrapper"> |
| | | 6 | | @* Контейнер для чипов (над полем ввода) *@ |
| | 0 | 7 | | @if (tokens.OfType<FileToken>().Any()) |
| | | 8 | | { |
| | | 9 | | <div class="chips-list"> |
| | 0 | 10 | | @foreach (var token in tokens.OfType<FileToken>()) |
| | | 11 | | { |
| | | 12 | | <FileChip Token="token" |
| | | 13 | | Icon="@GetFileIcon(token.FileName)" |
| | | 14 | | OnRemoveClick="RemoveToken" /> |
| | | 15 | | } |
| | | 16 | | </div> |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | <div class="input-container"> |
| | | 20 | | @* Поле ввода *@ |
| | 0 | 21 | | <textarea @ref="textareaRef" |
| | 0 | 22 | | rows="1" |
| | 0 | 23 | | class="text-input" |
| | 0 | 24 | | placeholder="@ModePlaceholder" |
| | 0 | 25 | | @bind="currentInput" |
| | 0 | 26 | | @oninput="HandleInput" |
| | | 27 | | @onkeydown="OnKeyDown" |
| | | 28 | | @onkeydown:preventDefault="shouldPreventDefault" |
| | | 29 | | disabled="@IsLoading"></textarea> |
| | | 30 | | |
| | | 31 | | @* Нижняя панель с кнопками *@ |
| | | 32 | | <div class="input-footer"> |
| | | 33 | | <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.SpaceBetween" AlignItems="A |
| | | 34 | | <RadzenStack Orientation="Orientation.Horizontal" Gap="8px" AlignItems="AlignItems.Center"> |
| | | 35 | | <RadzenDropDown TValue="AppMode" class="mode-dropdown" Data="@AppModeValues" @bind-Value="@CurrentMo |
| | | 36 | | <ModelSelector class="model-dropdown" /> |
| | | 37 | | </RadzenStack> |
| | | 38 | | |
| | | 39 | | <RadzenStack Orientation="Orientation.Horizontal" Gap="4px" JustifyContent="JustifyContent.End" AlignIte |
| | | 40 | | <RadzenButton Icon="gavel" ButtonStyle="ButtonStyle.Base" Variant="Variant.Text" Size="ButtonSize.Sm |
| | | 41 | | <RadzenButton Icon="extension" ButtonStyle="ButtonStyle.Base" Variant="Variant.Text" Size="ButtonSiz |
| | | 42 | | |
| | | 43 | | <RadzenStack Orientation="Orientation.Horizontal" Gap="8px" JustifyContent="JustifyContent.End" Alig |
| | | 44 | | <RadzenButton Icon="send" |
| | | 45 | | ButtonStyle="ButtonStyle.Primary" |
| | | 46 | | Variant="Variant.Text" |
| | | 47 | | Visible="@(!IsLoading)" |
| | | 48 | | Disabled="@(!CanSend())" |
| | | 49 | | Click="@OnSendClick" |
| | | 50 | | title="@SharedResource.SendMessage" |
| | | 51 | | class="rz-chat-send-btn" /> |
| | | 52 | | <RadzenButton Icon="dangerous" |
| | | 53 | | ButtonStyle="ButtonStyle.Danger" |
| | | 54 | | Variant="Variant.Text" |
| | | 55 | | Visible="@IsLoading" |
| | | 56 | | Click="@Cancel.InvokeAsync" |
| | | 57 | | title="@SharedResource.Cancel" |
| | | 58 | | class="rz-chat-cancel-btn" /> |
| | | 59 | | </RadzenStack> |
| | | 60 | | </RadzenStack> |
| | | 61 | | </RadzenStack> |
| | | 62 | | </div> |
| | | 63 | | </div> |
| | | 64 | | |
| | | 65 | | @* Всплывающее меню с файлами *@ |
| | 0 | 66 | | @if (filteredFiles.Any()) |
| | | 67 | | { |
| | | 68 | | <div class="hints-menu"> |
| | | 69 | | <ul class="hints-list"> |
| | 0 | 70 | | @for (int i = 0; i < filteredFiles.Count; i++) |
| | | 71 | | { |
| | 0 | 72 | | var index = i; |
| | 0 | 73 | | var file = filteredFiles[i]; |
| | 0 | 74 | | var isSelected = index == selectedIndex; |
| | 0 | 75 | | var fileName = Path.GetFileName(file.Replace('\\', '/')); |
| | 0 | 76 | | var fileDir = Path.GetDirectoryName(file.Replace('\\', '/')) ?? ""; |
| | 0 | 77 | | var icon = GetFileIcon(fileName); |
| | | 78 | | <li class="hint-item @(isSelected ? "selected" : "")" |
| | 0 | 79 | | @onclick="() => AddFileToken(file)"> |
| | 0 | 80 | | <span class="file-icon">@icon</span> |
| | 0 | 81 | | <span class="file-name">@fileName</span> |
| | 0 | 82 | | <span class="file-path">@fileDir</span> |
| | | 83 | | </li> |
| | | 84 | | } |
| | | 85 | | </ul> |
| | | 86 | | </div> |
| | | 87 | | } |
| | | 88 | | </div> |
| | | 89 | | |
| | | 90 | | @code { |
| | 0 | 91 | | [Parameter] public EventCallback<string> SendMessage { get; set; } |
| | 0 | 92 | | [Parameter] public EventCallback<string> Cancel { get; set; } |
| | 0 | 93 | | [Parameter] public bool IsLoading { get; set; } |
| | | 94 | | |
| | 0 | 95 | | [Inject] private ChatService ChatService { get; set; } = null!; |
| | 0 | 96 | | [Inject] private IVsCodeContextService VsCodeContextService { get; set; } = null!; |
| | 0 | 97 | | [Inject] private IJSRuntime JS { get; set; } = null!; |
| | 0 | 98 | | [Inject] private IVsBridge VsBridge { get; set; } = null!; |
| | | 99 | | |
| | | 100 | | private ElementReference textareaRef; |
| | 0 | 101 | | private List<InputToken> tokens = new(); |
| | 0 | 102 | | private string currentInput = string.Empty; |
| | 0 | 103 | | private List<string> filteredFiles = new(); |
| | | 104 | | private int selectedIndex = 0; |
| | | 105 | | private bool shouldPreventDefault; |
| | | 106 | | |
| | 0 | 107 | | private List<AppMode> AppModeValues { get; } = [.. Enum.GetValues<AppMode>()]; |
| | | 108 | | |
| | 0 | 109 | | private string ModePlaceholder => CurrentMode switch |
| | 0 | 110 | | { |
| | 0 | 111 | | AppMode.Plan => SharedResource.InputPlaceholderPlan, |
| | 0 | 112 | | AppMode.Agent => SharedResource.InputPlaceholderAgent, |
| | 0 | 113 | | _ => SharedResource.InputPlaceholder |
| | 0 | 114 | | }; |
| | | 115 | | |
| | | 116 | | private AppMode CurrentMode |
| | | 117 | | { |
| | 0 | 118 | | get => ChatService.Session?.Mode ?? AppMode.Chat; |
| | 0 | 119 | | set => ChatService.Session?.Mode = value; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private bool CanSend() |
| | | 123 | | { |
| | 0 | 124 | | return !IsLoading && (!string.IsNullOrWhiteSpace(currentInput) || tokens.Any()); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private async Task HandleInput(ChangeEventArgs e) |
| | | 128 | | { |
| | 0 | 129 | | currentInput = e.Value?.ToString() ?? string.Empty; |
| | | 130 | | |
| | | 131 | | // Автоматически изменяем высоту textarea |
| | 0 | 132 | | await JS.InvokeVoidAsync("autoResizeTextarea", textareaRef); |
| | | 133 | | |
| | | 134 | | // Проверяем, есть ли @ в конце |
| | 0 | 135 | | if (currentInput.EndsWith(" @")) |
| | | 136 | | { |
| | | 137 | | // Показываем все файлы |
| | 0 | 138 | | var source = VsCodeContextService.CurrentContext?.SolutionFiles ?? |
| | 0 | 139 | | ["test1.cs", "C:\\asdtes\\t2aksjhdgj\\ahsghd hjagshjdg\\ajhsgdahjkg.cs"]; |
| | 0 | 140 | | filteredFiles = source.Take(10).ToList(); |
| | 0 | 141 | | selectedIndex = 0; |
| | | 142 | | } |
| | 0 | 143 | | else if (currentInput.Contains('@')) |
| | | 144 | | { |
| | | 145 | | // Ищем текст после последнего @ |
| | 0 | 146 | | var lastAtIndex = currentInput.LastIndexOf('@'); |
| | 0 | 147 | | var query = currentInput.Substring(lastAtIndex + 1); |
| | | 148 | | |
| | 0 | 149 | | var source = VsCodeContextService.CurrentContext?.SolutionFiles ?? |
| | 0 | 150 | | ["test1.cs", "C:\\asdtes\\t2aksjhdgj\\ahsghd hjagshjdg\\ajhsgdahjkg.cs"]; |
| | | 151 | | |
| | 0 | 152 | | filteredFiles = source |
| | 0 | 153 | | .Where(f => f.Contains(query, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 154 | | .Take(10) |
| | 0 | 155 | | .ToList(); |
| | 0 | 156 | | selectedIndex = 0; |
| | | 157 | | } |
| | | 158 | | else |
| | | 159 | | { |
| | 0 | 160 | | filteredFiles.Clear(); |
| | | 161 | | } |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | private async Task AddFileToken(string filePath) |
| | | 165 | | { |
| | 0 | 166 | | var fileName = Path.GetFileName(filePath.Replace('\\', '/')); |
| | | 167 | | |
| | | 168 | | // Удаляем @ и текст после него из currentInput |
| | 0 | 169 | | var lastAtIndex = currentInput.LastIndexOf('@'); |
| | 0 | 170 | | if (lastAtIndex >= 0) |
| | | 171 | | { |
| | 0 | 172 | | currentInput = currentInput.Substring(0, lastAtIndex); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | // Добавляем токен файла |
| | 0 | 176 | | tokens.Add(new FileToken |
| | 0 | 177 | | { |
| | 0 | 178 | | FilePath = filePath, |
| | 0 | 179 | | FileName = fileName |
| | 0 | 180 | | }); |
| | | 181 | | |
| | 0 | 182 | | filteredFiles.Clear(); |
| | 0 | 183 | | selectedIndex = 0; |
| | | 184 | | |
| | | 185 | | // Фокус обратно на input |
| | 0 | 186 | | await FocusInput(); |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | private async Task RemoveToken(FileToken token) |
| | | 190 | | { |
| | 0 | 191 | | tokens.Remove(token); |
| | 0 | 192 | | await FocusInput(); |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | private async Task FocusInput() |
| | | 196 | | { |
| | | 197 | | try |
| | | 198 | | { |
| | 0 | 199 | | await textareaRef.FocusAsync(); |
| | 0 | 200 | | } |
| | 0 | 201 | | catch { } |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | private async Task OnKeyDown(KeyboardEventArgs e) |
| | | 205 | | { |
| | 0 | 206 | | if (filteredFiles.Any()) |
| | | 207 | | { |
| | 0 | 208 | | if (e.Key == "ArrowDown") |
| | | 209 | | { |
| | 0 | 210 | | selectedIndex = (selectedIndex + 1) % filteredFiles.Count; |
| | 0 | 211 | | shouldPreventDefault = true; |
| | 0 | 212 | | return; |
| | | 213 | | } |
| | 0 | 214 | | if (e.Key == "ArrowUp") |
| | | 215 | | { |
| | 0 | 216 | | selectedIndex = (selectedIndex - 1 + filteredFiles.Count) % filteredFiles.Count; |
| | 0 | 217 | | shouldPreventDefault = true; |
| | 0 | 218 | | return; |
| | | 219 | | } |
| | 0 | 220 | | if (e.Key == "Tab" || e.Key == "Enter") |
| | | 221 | | { |
| | 0 | 222 | | shouldPreventDefault = true; |
| | 0 | 223 | | if (selectedIndex >= 0 && selectedIndex < filteredFiles.Count) |
| | | 224 | | { |
| | 0 | 225 | | await AddFileToken(filteredFiles[selectedIndex]); |
| | | 226 | | } |
| | 0 | 227 | | return; |
| | | 228 | | } |
| | 0 | 229 | | if (e.Key == "Escape") |
| | | 230 | | { |
| | 0 | 231 | | shouldPreventDefault = true; |
| | 0 | 232 | | filteredFiles.Clear(); |
| | 0 | 233 | | return; |
| | | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | // Enter без Shift/Ctrl — отправка |
| | 0 | 238 | | if (e is { Key: "Enter", CtrlKey: false, ShiftKey: false } && !filteredFiles.Any()) |
| | | 239 | | { |
| | 0 | 240 | | shouldPreventDefault = true; |
| | 0 | 241 | | await OnSendClick(); |
| | | 242 | | } |
| | | 243 | | else |
| | | 244 | | { |
| | 0 | 245 | | shouldPreventDefault = false; |
| | | 246 | | } |
| | 0 | 247 | | } |
| | | 248 | | |
| | | 249 | | private async Task OnSendClick() |
| | | 250 | | { |
| | 0 | 251 | | if (!CanSend()) return; |
| | | 252 | | |
| | | 253 | | // Загружаем содержимое файлов перед отправкой |
| | 0 | 254 | | foreach (var fileToken in tokens.OfType<FileToken>()) |
| | | 255 | | { |
| | 0 | 256 | | if (string.IsNullOrEmpty(fileToken.FileContent)) |
| | | 257 | | { |
| | 0 | 258 | | var readFileTool = await VsBridge.ExecuteToolAsync( |
| | 0 | 259 | | BuiltInToolEnum.ReadFiles, |
| | 0 | 260 | | new Dictionary<string, object> { { "param1", fileToken.FilePath } }); |
| | | 261 | | |
| | 0 | 262 | | fileToken.FileContent = readFileTool.Result; |
| | | 263 | | } |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | // Формируем сообщение для отправки |
| | 0 | 267 | | var messageBuilder = new System.Text.StringBuilder(); |
| | | 268 | | |
| | | 269 | | // Добавляем текущий ввод |
| | 0 | 270 | | if (!string.IsNullOrWhiteSpace(currentInput)) |
| | | 271 | | { |
| | 0 | 272 | | messageBuilder.Append(currentInput.Trim()); |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | // Добавляем файлы с их содержимым |
| | 0 | 276 | | foreach (var fileToken in tokens.OfType<FileToken>()) |
| | | 277 | | { |
| | 0 | 278 | | if (messageBuilder.Length > 0) |
| | 0 | 279 | | messageBuilder.AppendLine(); |
| | | 280 | | |
| | 0 | 281 | | messageBuilder.Append(fileToken.GetLlmText()); |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | var message = messageBuilder.ToString(); |
| | | 285 | | |
| | | 286 | | // Очищаем состояние |
| | 0 | 287 | | currentInput = string.Empty; |
| | 0 | 288 | | tokens.Clear(); |
| | 0 | 289 | | filteredFiles.Clear(); |
| | | 290 | | |
| | | 291 | | // Сбрасываем высоту textarea |
| | 0 | 292 | | await JS.InvokeVoidAsync("autoResizeTextarea", textareaRef, true); |
| | | 293 | | |
| | 0 | 294 | | await SendMessage.InvokeAsync(message); |
| | 0 | 295 | | } |
| | | 296 | | |
| | | 297 | | private string GetFileIcon(string fileName) |
| | | 298 | | { |
| | 0 | 299 | | var extension = Path.GetExtension(fileName.Replace('\\', '/')).ToLowerInvariant(); |
| | 0 | 300 | | return extension switch |
| | 0 | 301 | | { |
| | 0 | 302 | | ".cs" => "📄", |
| | 0 | 303 | | ".razor" => "⚡", |
| | 0 | 304 | | ".html" => "🌐", |
| | 0 | 305 | | ".css" => "🎨", |
| | 0 | 306 | | ".js" => "📜", |
| | 0 | 307 | | ".json" => "📋", |
| | 0 | 308 | | ".xml" => "📋", |
| | 0 | 309 | | ".txt" => "📝", |
| | 0 | 310 | | ".md" => "📖", |
| | 0 | 311 | | ".png" or ".jpg" or ".jpeg" or ".gif" or ".svg" => "🖼️", |
| | 0 | 312 | | ".dll" or ".exe" => "⚙️", |
| | 0 | 313 | | ".config" => "🔧", |
| | 0 | 314 | | ".csproj" or ".sln" => "📦", |
| | 0 | 315 | | _ => "📄" |
| | 0 | 316 | | }; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | public async Task Clear() |
| | | 320 | | { |
| | 0 | 321 | | currentInput = string.Empty; |
| | 0 | 322 | | tokens.Clear(); |
| | 0 | 323 | | filteredFiles.Clear(); |
| | | 324 | | |
| | | 325 | | // Сбрасываем высоту textarea |
| | 0 | 326 | | await JS.InvokeVoidAsync("eval", "arguments[0].style.height = 'auto'", textareaRef); |
| | | 327 | | |
| | 0 | 328 | | StateHasChanged(); |
| | 0 | 329 | | } |
| | | 330 | | |
| | | 331 | | protected override void OnInitialized() |
| | | 332 | | { |
| | 0 | 333 | | base.OnInitialized(); |
| | 0 | 334 | | ChatService.OnSessionChanged += HandleSessionChanged; |
| | 0 | 335 | | VsBridge.OnModeSwitched += HandleModeSwitched; |
| | 0 | 336 | | } |
| | | 337 | | |
| | | 338 | | private void HandleModeSwitched(AppMode mode) |
| | | 339 | | { |
| | 0 | 340 | | CurrentMode = mode; |
| | 0 | 341 | | InvokeAsync(StateHasChanged); |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | private void HandleSessionChanged() |
| | | 345 | | { |
| | 0 | 346 | | InvokeAsync(StateHasChanged); |
| | 0 | 347 | | } |
| | | 348 | | |
| | | 349 | | private void HandleProfileChanged() |
| | | 350 | | { |
| | 0 | 351 | | InvokeAsync(StateHasChanged); |
| | 0 | 352 | | } |
| | | 353 | | |
| | | 354 | | private async Task OpenRules() |
| | | 355 | | { |
| | 0 | 356 | | await VsBridge.ExecuteToolAsync(BasicEnum.OpenFile, new Dictionary<string, object> { { "param1", ".agents/rules. |
| | 0 | 357 | | } |
| | | 358 | | |
| | | 359 | | private async Task OpenSkills() |
| | | 360 | | { |
| | 0 | 361 | | await VsBridge.ExecuteToolAsync(BasicEnum.OpenFolder, new Dictionary<string, object> { { "param1", ".agents/skil |
| | 0 | 362 | | } |
| | | 363 | | |
| | | 364 | | public override void Dispose() |
| | | 365 | | { |
| | 0 | 366 | | base.Dispose(); |
| | 0 | 367 | | ChatService.OnSessionChanged -= HandleSessionChanged; |
| | 0 | 368 | | VsBridge.OnModeSwitched -= HandleModeSwitched; |
| | 0 | 369 | | } |
| | | 370 | | } |