| | | 1 | | using UIBlazor.Services; |
| | | 2 | | |
| | | 3 | | namespace UIBlazor.Agents; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// То что не должно идти в VS а обрабатывается прямо тут |
| | | 7 | | /// </summary> |
| | 19 | 8 | | public class InternalExecutor(IServiceProvider serviceProvider) : IInternalExecutor |
| | | 9 | | { |
| | | 10 | | public async Task<VsToolResult> ExecuteToolAsync(string name, IReadOnlyDictionary<string, object> args, Cancellation |
| | | 11 | | { |
| | 19 | 12 | | if (name == BasicEnum.SwitchMode) |
| | | 13 | | { |
| | 17 | 14 | | if (args != null && args.TryGetValue("param1", out var modeObj)) |
| | | 15 | | { |
| | 14 | 16 | | if (Enum.TryParse<AppMode>(modeObj.ToString(), true, out var mode)) |
| | | 17 | | { |
| | 13 | 18 | | serviceProvider.GetRequiredService<IChatService>().Session.Mode = mode; |
| | 13 | 19 | | return new VsToolResult |
| | 13 | 20 | | { |
| | 13 | 21 | | Success = true, |
| | 13 | 22 | | Result = $"Switched to {mode} mode successfully. Now you have access to different set of tools." |
| | 13 | 23 | | }; |
| | | 24 | | } |
| | | 25 | | } |
| | 4 | 26 | | return new VsToolResult |
| | 4 | 27 | | { |
| | 4 | 28 | | Success = false, |
| | 4 | 29 | | ErrorMessage = "Not supported mode" |
| | 4 | 30 | | }; |
| | | 31 | | } |
| | | 32 | | |
| | 2 | 33 | | if (name == BasicEnum.AskUser) |
| | | 34 | | { |
| | 0 | 35 | | return ExecuteAskUserAsync(args); |
| | | 36 | | } |
| | | 37 | | |
| | 2 | 38 | | return new VsToolResult |
| | 2 | 39 | | { |
| | 2 | 40 | | Success = false, |
| | 2 | 41 | | ErrorMessage = $"Not supported tool {name}" |
| | 2 | 42 | | }; |
| | 19 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Execute ask_user tool. |
| | | 47 | | /// param1 = question, param2+ = options |
| | | 48 | | /// Returns JSON with question and options for UI to render. |
| | | 49 | | /// </summary> |
| | | 50 | | private static VsToolResult ExecuteAskUserAsync(IReadOnlyDictionary<string, object> args) |
| | | 51 | | { |
| | 0 | 52 | | var question = string.Empty; |
| | 0 | 53 | | var options = new List<string>(); |
| | | 54 | | |
| | 0 | 55 | | if (args != null) |
| | | 56 | | { |
| | | 57 | | // Get param keys |
| | 0 | 58 | | var paramKeys = args.Keys.ToList(); |
| | | 59 | | |
| | | 60 | | // First param is the question |
| | 0 | 61 | | if (paramKeys.Count > 0 && args.TryGetValue(paramKeys[0], out var questionObj)) |
| | | 62 | | { |
| | 0 | 63 | | question = questionObj?.ToString()?.Trim() ?? string.Empty; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // Remaining params are options |
| | 0 | 67 | | for (var i = 1; i < paramKeys.Count; i++) |
| | | 68 | | { |
| | 0 | 69 | | if (args.TryGetValue(paramKeys[i], out var optionObj)) |
| | | 70 | | { |
| | 0 | 71 | | var option = optionObj?.ToString()?.Trim(); |
| | 0 | 72 | | if (!string.IsNullOrEmpty(option)) |
| | | 73 | | { |
| | 0 | 74 | | options.Add(option); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Return result with question and options as JSON |
| | 0 | 81 | | var resultJson = JsonSerializer.Serialize(new { question, options }); |
| | | 82 | | |
| | 0 | 83 | | return new VsToolResult |
| | 0 | 84 | | { |
| | 0 | 85 | | Success = true, |
| | 0 | 86 | | Result = resultJson |
| | 0 | 87 | | }; |
| | | 88 | | } |
| | | 89 | | } |