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