|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text; |
| 3 | +using DotNetConfig; |
| 4 | +using Microsoft.Extensions.AI; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Microsoft.Extensions.Hosting; |
| 7 | +using Spectre.Console; |
| 8 | +using xAI; |
| 9 | +using xAI.Protocol; |
| 10 | + |
| 11 | +namespace Grok; |
| 12 | + |
| 13 | +partial class Interactive(IConfiguration configuration) : IHostedService |
| 14 | +{ |
| 15 | + readonly CancellationTokenSource cts = new(); |
| 16 | + string? apiKey = configuration["grok:apikey"]; |
| 17 | + GrokClient? client; |
| 18 | + |
| 19 | + public Task StartAsync(CancellationToken cancellationToken) |
| 20 | + { |
| 21 | + if (string.IsNullOrEmpty(apiKey)) |
| 22 | + { |
| 23 | + apiKey = AnsiConsole.Ask<string>("Enter Grok API key:"); |
| 24 | + Config.Build(ConfigLevel.Global).SetString("grok", "apikey", apiKey); |
| 25 | + } |
| 26 | + |
| 27 | + client = new GrokClient(apiKey); |
| 28 | + |
| 29 | + _ = Task.Run(InputListener, cancellationToken); |
| 30 | + |
| 31 | + return Task.CompletedTask; |
| 32 | + } |
| 33 | + |
| 34 | + public Task StopAsync(CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + cts.Cancel(); |
| 37 | + AnsiConsole.MarkupLine($":robot: Stopping"); |
| 38 | + return Task.CompletedTask; |
| 39 | + } |
| 40 | + |
| 41 | + async Task InputListener() |
| 42 | + { |
| 43 | + Debug.Assert(client != null); |
| 44 | + |
| 45 | + var models = await client.GetModelsClient().ListLanguageModelsAsync(); |
| 46 | + var modelId = configuration["grok:modelid"]; |
| 47 | + var choices = models.Select(x => x.Aliases.OrderBy(a => a.Length).FirstOrDefault() ?? x.Name).ToList(); |
| 48 | + if (modelId != null && choices.IndexOf(modelId) is var index && index > 0) |
| 49 | + { |
| 50 | + choices.RemoveAt(index); |
| 51 | + choices.Insert(0, modelId); |
| 52 | + } |
| 53 | + |
| 54 | + modelId = AnsiConsole.Prompt(new SelectionPrompt<string>().Title("Select model").AddChoices(choices)); |
| 55 | + Config.Build(ConfigLevel.Global).SetString("grok", "modelid", modelId); |
| 56 | + |
| 57 | + var chat = client!.GetChatClient().AsIChatClient(modelId); |
| 58 | + var options = new ChatOptions |
| 59 | + { |
| 60 | + Tools = [new GrokXSearchTool(), new HostedWebSearchTool(), new HostedCodeInterpreterTool()] |
| 61 | + }; |
| 62 | + var conversation = new List<ChatMessage>(); |
| 63 | + |
| 64 | + AnsiConsole.MarkupLine($":robot: Ready"); |
| 65 | + AnsiConsole.Markup($":person_beard: "); |
| 66 | + |
| 67 | + while (!cts.IsCancellationRequested) |
| 68 | + { |
| 69 | + var input = Console.ReadLine()?.Trim(); |
| 70 | + if (!string.IsNullOrWhiteSpace(input)) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + if (input is "cls" or "clear") |
| 75 | + { |
| 76 | + Console.Clear(); |
| 77 | + conversation.Clear(); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + conversation.Add(new ChatMessage(ChatRole.User, input)); |
| 82 | + var contents = await AnsiConsole.Status().StartAsync("Sending...", async ctx => |
| 83 | + { |
| 84 | + var contents = new List<TextContent>(); |
| 85 | + await foreach (var update in chat.GetStreamingResponseAsync(conversation, options, cts.Token)) |
| 86 | + { |
| 87 | + foreach (var tool in update.Contents.Select(x => x.RawRepresentation as ToolCall).Where(x => x != null)) |
| 88 | + ctx.Status($"Calling: {tool!.Function.Name.EscapeMarkup()}"); |
| 89 | + foreach (var thinking in update.Contents.OfType<TextReasoningContent>()) |
| 90 | + ctx.Status($"Thinking: {thinking.Text.EscapeMarkup()}"); |
| 91 | + |
| 92 | + contents.AddRange(update.Contents.OfType<TextContent>()); |
| 93 | + } |
| 94 | + return contents; |
| 95 | + }); |
| 96 | + |
| 97 | + foreach (var content in contents) |
| 98 | + Console.Write(content); |
| 99 | + |
| 100 | + Console.WriteLine(); |
| 101 | + } |
| 102 | + } |
| 103 | + catch (Exception e) |
| 104 | + { |
| 105 | + AnsiConsole.WriteException(e); |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + AnsiConsole.Markup($":person_beard: "); |
| 110 | + } |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + AnsiConsole.Markup($":person_beard: "); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments