| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Net.Http.Headers; |
| | | 4 | | using System.Net.Http.Json; |
| | | 5 | | using System.Net.Mime; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using UIBlazor.Services.Models; |
| | | 8 | | using UIBlazor.Services.Settings; |
| | | 9 | | |
| | | 10 | | namespace UIBlazor.Services; |
| | | 11 | | |
| | 9 | 12 | | public class ChatService( |
| | 9 | 13 | | HttpClient httpClient, |
| | 9 | 14 | | IProfileManager profileManager, |
| | 9 | 15 | | ISystemPromptBuilder systemPromptBuilder, |
| | 9 | 16 | | ILocalStorageService localStorage, |
| | 9 | 17 | | ILogger<IChatService> logger |
| | 9 | 18 | | ) : IChatService |
| | | 19 | | { |
| | | 20 | | private const string _thinkStart = "<think>"; |
| | | 21 | | private const string _thinkEnd = "</think>"; |
| | | 22 | | private const string _complitions = "/v1/chat/completions"; |
| | | 23 | | private const string _models = "/v1/models"; |
| | 9 | 24 | | private readonly JsonSerializerOptions _jsonSerializerOptions = new() |
| | 9 | 25 | | { |
| | 9 | 26 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| | 9 | 27 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| | 9 | 28 | | }; |
| | | 29 | | |
| | 69 | 30 | | public ConnectionProfile Options => profileManager.ActiveProfile; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Подписка на события после создания экземпляра |
| | | 34 | | /// </summary> |
| | | 35 | | public void Initialize() |
| | | 36 | | { |
| | 0 | 37 | | Session.PropertyChanged -= SessionPropertyChanged; |
| | 0 | 38 | | Session.PropertyChanged += SessionPropertyChanged; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public ConversationSession Session |
| | | 42 | | { |
| | 158 | 43 | | get; |
| | | 44 | | private set |
| | | 45 | | { |
| | 2 | 46 | | if (field == value) |
| | 0 | 47 | | return; |
| | 2 | 48 | | field?.PropertyChanged -= SessionPropertyChanged; |
| | 2 | 49 | | field = value; |
| | 2 | 50 | | field?.PropertyChanged += SessionPropertyChanged; |
| | 2 | 51 | | SessionChanged?.Invoke(field, new PropertyChangedEventArgs(nameof(ConversationSession))); |
| | 0 | 52 | | } |
| | 9 | 53 | | } = CreateNewSession(); |
| | | 54 | | |
| | | 55 | | public event PropertyChangedEventHandler? SessionChanged; |
| | | 56 | | |
| | | 57 | | private void SessionPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | 0 | 58 | | => SessionChanged?.Invoke(sender, e); |
| | | 59 | | |
| | | 60 | | public async Task<AiModelList> GetModelsAsync(CancellationToken cancellationToken) |
| | | 61 | | { |
| | 1 | 62 | | using var request = new HttpRequestMessage(HttpMethod.Get, $"{Options.Endpoint}{_models}"); |
| | | 63 | | |
| | 1 | 64 | | if (!string.IsNullOrEmpty(Options.ApiKey)) |
| | | 65 | | { |
| | 0 | 66 | | if (string.IsNullOrWhiteSpace(Options.ApiKeyHeader)) |
| | | 67 | | { |
| | 0 | 68 | | throw new InvalidOperationException("API key header must be specified when an API key is provided."); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | if (string.Equals(Options.ApiKeyHeader, "Authorization", StringComparison.OrdinalIgnoreCase)) |
| | | 72 | | { |
| | 0 | 73 | | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Options.ApiKey); |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | 0 | 77 | | request.Headers.Add(Options.ApiKeyHeader, Options.ApiKey); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | 1 | 81 | | if (string.IsNullOrEmpty(Options.Endpoint)) |
| | | 82 | | { |
| | 1 | 83 | | throw new InvalidOperationException("Endpoint must be specified."); |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken). |
| | | 87 | | |
| | 0 | 88 | | if (!response.IsSuccessStatusCode) |
| | | 89 | | { |
| | 0 | 90 | | throw new HttpRequestException($"Getting models failed: {await response.Content.ReadAsStringAsync(cancellati |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | return await response.Content.ReadFromJsonAsync<AiModelList>(cancellationToken) |
| | 0 | 94 | | ?? throw new JsonException("Models deserialization exception"); |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | public bool NeedCompression => Options.TokensToCompress > 0 && Session.Messages.Count > 5 && Session.TotalTokens > O |
| | | 98 | | |
| | | 99 | | public async IAsyncEnumerable<ChatDelta> CompressSessionAsync([EnumeratorCancellation] CancellationToken cancellatio |
| | | 100 | | { |
| | 0 | 101 | | var (Messages, LastUserMessage) = Session.GetFormattedMessagesForCompress(await systemPromptBuilder.PrepareSyste |
| | | 102 | | |
| | | 103 | | // Получаем сжатый текст от LLM |
| | 0 | 104 | | var contentSb = new StringBuilder(); |
| | 0 | 105 | | await foreach (var chatDelta in GetCompletionsAsync(Messages, cancellationToken)) |
| | | 106 | | { |
| | 0 | 107 | | if (chatDelta.Content is not null) |
| | | 108 | | { |
| | 0 | 109 | | contentSb.Append(chatDelta.Content); |
| | | 110 | | } |
| | 0 | 111 | | yield return chatDelta; |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | if (!cancellationToken.IsCancellationRequested) |
| | | 115 | | { |
| | | 116 | | // Создаем новый объект сообщения со сжатым контекстом |
| | 0 | 117 | | var compressedMessage = new VisualChatMessage() |
| | 0 | 118 | | { |
| | 0 | 119 | | Content = contentSb.ToString(), |
| | 0 | 120 | | Role = ChatMessageRole.Assistant, |
| | 0 | 121 | | IsExpanded = true, |
| | 0 | 122 | | }; |
| | | 123 | | |
| | 0 | 124 | | int totalCount = Session.Messages.Count; |
| | 0 | 125 | | int windowSize = totalCount < 6 ? 2 : 3; |
| | | 126 | | |
| | 0 | 127 | | var topMessages = new List<VisualChatMessage>(); |
| | 0 | 128 | | var bottomMessages = new List<VisualChatMessage>(); |
| | | 129 | | |
| | 0 | 130 | | for (int i = 0; i < totalCount - 1; i++) |
| | | 131 | | { |
| | 0 | 132 | | var msg = Session.Messages[i]; |
| | | 133 | | |
| | 0 | 134 | | if (msg.Id == LastUserMessage?.Id) |
| | | 135 | | continue; |
| | | 136 | | |
| | | 137 | | // Первые сообщения |
| | 0 | 138 | | if (i < windowSize) |
| | | 139 | | { |
| | 0 | 140 | | topMessages.Add(msg); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | // Оставшиеся сообщения |
| | 0 | 144 | | else if (i >= totalCount - 1 - windowSize) |
| | | 145 | | { |
| | 0 | 146 | | bottomMessages.Add(msg); |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | var keptMessages = new List<VisualChatMessage>(topMessages.Count + bottomMessages.Count + 2); |
| | 0 | 151 | | keptMessages.AddRange(topMessages); |
| | 0 | 152 | | keptMessages.AddRange(bottomMessages); |
| | 0 | 153 | | keptMessages.Add(compressedMessage); |
| | | 154 | | |
| | | 155 | | // Восстанавливаем сообщение пользователя после компрессии |
| | 0 | 156 | | if (LastUserMessage is not null) |
| | | 157 | | { |
| | 0 | 158 | | keptMessages.Add(LastUserMessage); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // Перезаписываем историю |
| | 0 | 162 | | Session.Messages = keptMessages; |
| | | 163 | | } |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Asynchronously saves the current session data to local storage using the session ID as the key. |
| | | 168 | | /// </summary> |
| | | 169 | | /// <returns></returns> |
| | | 170 | | public async Task SaveSessionAsync() |
| | | 171 | | { |
| | 0 | 172 | | await localStorage.SetItemAsync(Session.Id, Session); |
| | 0 | 173 | | UpdateSessionCache(Session); |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | private void UpdateSessionCache(ConversationSession session) |
| | | 177 | | { |
| | 0 | 178 | | if (_recentSessionsCache == null) return; |
| | | 179 | | |
| | 0 | 180 | | var existing = _recentSessionsCache.FirstOrDefault(s => s.Id == session.Id); |
| | 0 | 181 | | var firstMessage = session.Messages.FirstOrDefault(m => m.Role == ChatMessageRole.User)?.Content ?? string.Empty |
| | 0 | 182 | | var preview = firstMessage is { Length: > 40 } ? firstMessage[..40] + "..." : firstMessage; |
| | | 183 | | |
| | 0 | 184 | | if (existing != null) |
| | | 185 | | { |
| | 0 | 186 | | existing.FirstUserMessage = preview; |
| | | 187 | | } |
| | | 188 | | else |
| | | 189 | | { |
| | 0 | 190 | | _recentSessionsCache.Add(new SessionSummary |
| | 0 | 191 | | { |
| | 0 | 192 | | Id = session.Id, |
| | 0 | 193 | | CreatedAt = session.CreatedAt, |
| | 0 | 194 | | FirstUserMessage = preview |
| | 0 | 195 | | }); |
| | 0 | 196 | | _recentSessionsCache = [.. _recentSessionsCache.OrderByDescending(s => s.CreatedAt)]; |
| | | 197 | | } |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Модель, которая последняя отвечала |
| | | 202 | | /// </summary> |
| | 151 | 203 | | public string? LastCompletionsModel { get; private set; } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Текст ошибки |
| | | 207 | | /// </summary> |
| | 7 | 208 | | public string? LastError { get; private set; } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Последнее использование токенов |
| | | 212 | | /// </summary> |
| | 6 | 213 | | public UsageInfo? LastUsage { get; private set; } |
| | | 214 | | |
| | 9 | 215 | | public string? FinishReason { get; private set; } |
| | | 216 | | |
| | | 217 | | private async IAsyncEnumerable<ChatDelta> GetCompletionsAsync(IEnumerable<object> messages, [EnumeratorCancellation] |
| | | 218 | | { |
| | 5 | 219 | | LastCompletionsModel = null; |
| | 5 | 220 | | LastUsage = null; |
| | 5 | 221 | | LastError = null; |
| | 5 | 222 | | FinishReason = null; |
| | | 223 | | |
| | | 224 | | // Use runtime parameters or fall back to configured options |
| | 5 | 225 | | var url = $"{Options.Endpoint}{_complitions}"; |
| | 5 | 226 | | var effectiveApiKeyHeader = Options.ApiKeyHeader; |
| | | 227 | | |
| | 5 | 228 | | var payload = new |
| | 5 | 229 | | { |
| | 5 | 230 | | model = Options.Model, |
| | 5 | 231 | | messages = messages, |
| | 5 | 232 | | temperature = Options.Temperature, |
| | 5 | 233 | | max_tokens = Options.MaxTokens >= 1000 ? Options.MaxTokens : -1, |
| | 5 | 234 | | stream = Options.Stream, |
| | 5 | 235 | | stream_options = Options.Stream ? new { include_usage = true } : null |
| | 5 | 236 | | }; |
| | | 237 | | |
| | 5 | 238 | | var request = new HttpRequestMessage(HttpMethod.Post, url) |
| | 5 | 239 | | { |
| | 5 | 240 | | Content = new StringContent( |
| | 5 | 241 | | JsonSerializer.Serialize(payload, _jsonSerializerOptions), |
| | 5 | 242 | | Encoding.UTF8, |
| | 5 | 243 | | MediaTypeNames.Application.Json) |
| | 5 | 244 | | }; |
| | | 245 | | |
| | 5 | 246 | | if (!string.IsNullOrEmpty(Options.ApiKey)) |
| | | 247 | | { |
| | 5 | 248 | | if (string.Equals(effectiveApiKeyHeader, "Authorization", StringComparison.OrdinalIgnoreCase)) |
| | | 249 | | { |
| | 5 | 250 | | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Options.ApiKey); |
| | | 251 | | } |
| | | 252 | | else |
| | | 253 | | { |
| | 0 | 254 | | request.Headers.Add(effectiveApiKeyHeader, Options.ApiKey); |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | 10 | 258 | | foreach (var header in Options.ExtraHeaders.Where(h => !string.IsNullOrEmpty(h.Name))) |
| | | 259 | | { |
| | 0 | 260 | | request.Headers.TryAddWithoutValidation(header.Name, header.Value); |
| | | 261 | | } |
| | | 262 | | |
| | 5 | 263 | | var response = await httpClient.SendAsync(request, Options.Stream ? HttpCompletionOption.ResponseHeadersRead : H |
| | | 264 | | |
| | 5 | 265 | | if (!response.IsSuccessStatusCode) |
| | | 266 | | { |
| | 0 | 267 | | var result = $"HttpCode: {response.StatusCode} | server failed: {await response.Content.ReadAsStringAsync(ca |
| | 0 | 268 | | throw new Exception(result); |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | // если не стрим, то возвращаем как один чанк |
| | 5 | 272 | | if (!Options.Stream) |
| | | 273 | | { |
| | 0 | 274 | | var chunk = await response.Content.ReadFromJsonAsync<StreamChunk>(cancellationToken); |
| | 0 | 275 | | var message = chunk?.Choice?.Message; |
| | 0 | 276 | | if (message?.Content != null) |
| | | 277 | | { |
| | | 278 | | // Удаление <think> блока из контента и перенос его в ReasoningContent если его там нет. |
| | 0 | 279 | | var regex = Regex.Match(message.Content, $"^{_thinkStart}(?<reason>.*){_thinkEnd}", RegexOptions.Singlel |
| | 0 | 280 | | if (regex.Success) |
| | | 281 | | { |
| | 0 | 282 | | message.ReasoningContent ??= regex.Groups["reason"].Value; |
| | 0 | 283 | | message.Content = message.Content[regex.Length..]; |
| | | 284 | | } |
| | 0 | 285 | | LastCompletionsModel ??= chunk?.Model; |
| | 0 | 286 | | if (chunk?.Usage != null) |
| | | 287 | | { |
| | 0 | 288 | | LastUsage = chunk.Usage; |
| | 0 | 289 | | Session.TotalTokens = chunk.Usage.TotalTokens; |
| | | 290 | | } |
| | 0 | 291 | | yield return message; |
| | | 292 | | } |
| | 0 | 293 | | yield break; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | // стрим |
| | 5 | 297 | | await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); |
| | 5 | 298 | | using var reader = new StreamReader(stream); |
| | | 299 | | |
| | | 300 | | string? line; |
| | 5 | 301 | | var isReasoningContent = false; |
| | 5 | 302 | | var isStart = true; |
| | 5 | 303 | | string? role = null; |
| | | 304 | | |
| | | 305 | | // чтобы html-теги <function> склеивать в один чанк |
| | 5 | 306 | | var _pendingText = string.Empty; |
| | 5 | 307 | | ChatChoice lastChoise = null!; |
| | 148 | 308 | | while ((line = await reader.ReadLineAsync(cancellationToken)) is not null && !cancellationToken.IsCancellationRe |
| | | 309 | | { |
| | 147 | 310 | | if (string.IsNullOrWhiteSpace(line) || !line.StartsWith("data:")) |
| | | 311 | | { |
| | | 312 | | continue; |
| | | 313 | | } |
| | | 314 | | |
| | 147 | 315 | | var json = line[6..]; |
| | | 316 | | |
| | 147 | 317 | | if (json == "[DONE]") |
| | | 318 | | { |
| | 4 | 319 | | FinishReason = lastChoise?.FinishReason; |
| | 4 | 320 | | break; |
| | | 321 | | } |
| | | 322 | | |
| | 143 | 323 | | if (json.StartsWith("{\"error\"")) |
| | | 324 | | { |
| | 1 | 325 | | LastError = json; |
| | 1 | 326 | | continue; |
| | | 327 | | } |
| | | 328 | | |
| | 142 | 329 | | var chunk = JsonUtils.Deserialize<StreamChunk>(json); |
| | 142 | 330 | | if (chunk == null) |
| | | 331 | | { |
| | | 332 | | continue; |
| | | 333 | | } |
| | | 334 | | |
| | 142 | 335 | | if (chunk.Usage != null) |
| | | 336 | | { |
| | 1 | 337 | | LastUsage = chunk.Usage; |
| | 1 | 338 | | Session.TotalTokens = chunk.Usage.TotalTokens; |
| | | 339 | | } |
| | | 340 | | |
| | 142 | 341 | | if (chunk.Choices.Count != 1 || chunk.Choices[0].Delta == null) |
| | | 342 | | { |
| | | 343 | | continue; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | // Динамический подсчёт токенов во время стрима (приблизительный) |
| | 142 | 347 | | Session.TotalTokens++; |
| | | 348 | | |
| | 142 | 349 | | LastCompletionsModel ??= chunk.Model; |
| | 142 | 350 | | lastChoise = chunk.Choices[0]; |
| | 142 | 351 | | var delta = lastChoise.Delta; |
| | 142 | 352 | | var content = delta!.Content; |
| | 142 | 353 | | role ??= delta?.Role; |
| | | 354 | | |
| | | 355 | | // Размышляющие модели по разному отдают размышления |
| | | 356 | | // |
| | | 357 | | // ReasoningContent | Content |
| | | 358 | | // GLM 4.7 +++ | --- |
| | | 359 | | // Kimi 2 +++ | <think> |
| | | 360 | | // Deepseek R1 --- | <think> |
| | | 361 | | // |
| | | 362 | | // обрабатываем размышления как Z.ai GLM. |
| | | 363 | | // Все размышления идут в ReasoningContent с пустым Content |
| | | 364 | | |
| | | 365 | | // Преобразрвания нужны если есть контент с блоком <think> |
| | 142 | 366 | | if (!string.IsNullOrEmpty(content)) |
| | | 367 | | { |
| | 140 | 368 | | if (!isReasoningContent) // не думаем |
| | | 369 | | { |
| | 140 | 370 | | if (isStart && content.StartsWith(_thinkStart)) |
| | | 371 | | { |
| | | 372 | | // начать думать можно только в первом чанке |
| | 0 | 373 | | isReasoningContent = true; |
| | 0 | 374 | | delta.ReasoningContent = content.Replace(_thinkStart, string.Empty); |
| | 0 | 375 | | delta.Content = null; |
| | | 376 | | } |
| | | 377 | | } |
| | | 378 | | else // внутри <think> блока |
| | | 379 | | { |
| | 0 | 380 | | if (content.Contains(_thinkEnd)) |
| | | 381 | | { |
| | | 382 | | // если закончил думать, то можно в контент добавить часть чанка (актуально для Kimi2) |
| | 0 | 383 | | isReasoningContent = false; |
| | 0 | 384 | | delta.Content = content.Replace(_thinkEnd, string.Empty); |
| | 0 | 385 | | delta.ReasoningContent = null; |
| | | 386 | | } |
| | | 387 | | else |
| | | 388 | | { |
| | | 389 | | // если не конец - то все пихаем в ReasoningContent и очищаем Content |
| | 0 | 390 | | delta.Content = null; |
| | 0 | 391 | | delta.ReasoningContent = content; |
| | | 392 | | } |
| | | 393 | | } |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | // Если есть контент, то проверяем на разрезанные теги и склеиваем их |
| | 142 | 397 | | if (delta.Content != null) |
| | | 398 | | { |
| | 142 | 399 | | var incomingText = _pendingText + delta.Content; |
| | 142 | 400 | | _pendingText = string.Empty; |
| | | 401 | | |
| | 142 | 402 | | var lastOpenIndex = incomingText.LastIndexOf('<'); |
| | | 403 | | |
| | | 404 | | // Проверяем, есть ли незакрытый тег в конце строки |
| | 142 | 405 | | if (lastOpenIndex >= 0) |
| | | 406 | | { |
| | 83 | 407 | | var potentialTag = incomingText[lastOpenIndex..]; |
| | | 408 | | |
| | | 409 | | // Если в "потенциальном теге" нет символов закрытия |
| | 83 | 410 | | if (potentialTag.IndexOfAny(['>', '\n']) == -1) |
| | | 411 | | { |
| | | 412 | | // Сохраняем в буфер ТОЛЬКО незакрытую часть |
| | 71 | 413 | | _pendingText = potentialTag; |
| | | 414 | | // А из текущей дельты вырезаем этот кусок |
| | 71 | 415 | | incomingText = incomingText[..lastOpenIndex]; |
| | | 416 | | } |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | // Если после обрезки буфера текста не осталось — идем за следующей дельтой |
| | 142 | 420 | | if (string.IsNullOrEmpty(incomingText) && !string.IsNullOrEmpty(_pendingText)) |
| | | 421 | | continue; |
| | | 422 | | |
| | 74 | 423 | | delta.Role ??= role; |
| | 74 | 424 | | delta.Content = incomingText; |
| | | 425 | | } |
| | | 426 | | |
| | 74 | 427 | | yield return delta; |
| | | 428 | | |
| | 74 | 429 | | isStart = false; |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | // если после окончания стрима остался неотправленный текст, отправляем его |
| | 5 | 433 | | if (!string.IsNullOrEmpty(_pendingText)) |
| | | 434 | | { |
| | 0 | 435 | | yield return new ChatDelta() { Content = _pendingText }; |
| | | 436 | | } |
| | 5 | 437 | | } |
| | | 438 | | |
| | | 439 | | /// <summary> |
| | | 440 | | /// Asynchronously generates a sequence of chat completion deltas for the current conversation session. |
| | | 441 | | /// </summary> |
| | | 442 | | /// <remarks> |
| | | 443 | | /// This method streams chat completion results as they become available, allowing for real-time |
| | | 444 | | /// processing of partial responses. The returned sequence may include reasoning content or message content |
| | | 445 | | /// depending on the model and response format. If streaming is not enabled, the method yields a single completion |
| | | 446 | | /// result. |
| | | 447 | | /// </remarks> |
| | | 448 | | /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</par |
| | | 449 | | /// <returns> |
| | | 450 | | /// An asynchronous stream of <see cref="ChatDelta"/> objects representing incremental updates to the chat |
| | | 451 | | /// completion. The stream completes when the response is fully received. |
| | | 452 | | /// </returns> |
| | | 453 | | /// <exception cref="Exception">Thrown if the chat completion request fails or the server returns an unsuccessful re |
| | | 454 | | public async IAsyncEnumerable<ChatDelta> GetCompletionsAsync([EnumeratorCancellation] CancellationToken cancellation |
| | | 455 | | { |
| | | 456 | | // Get formatted messages including conversation history |
| | 5 | 457 | | var messages = Session.GetFormattedMessages(await systemPromptBuilder.PrepareSystemPromptAsync(Session.Mode, can |
| | | 458 | | |
| | 158 | 459 | | await foreach (var chatDelta in GetCompletionsAsync(messages, cancellationToken)) |
| | | 460 | | { |
| | 74 | 461 | | yield return chatDelta; |
| | | 462 | | } |
| | 5 | 463 | | } |
| | | 464 | | |
| | | 465 | | private const int _maxSessions = 5; |
| | | 466 | | private List<SessionSummary>? _recentSessionsCache; |
| | | 467 | | |
| | | 468 | | public async Task<List<SessionSummary>> GetRecentSessionsAsync(int count) |
| | | 469 | | { |
| | 0 | 470 | | if (_recentSessionsCache != null) |
| | 0 | 471 | | return [.. _recentSessionsCache.Take(count)]; |
| | | 472 | | |
| | 0 | 473 | | var sessionIds = await GetAllSessionIdsAsync(); |
| | 0 | 474 | | var summaries = new List<SessionSummary>(); |
| | | 475 | | |
| | 0 | 476 | | foreach (var id in sessionIds) |
| | | 477 | | { |
| | 0 | 478 | | var session = await localStorage.TryGetItemAsync<ConversationSession>(id); |
| | 0 | 479 | | var firstMessage = session?.Messages.FirstOrDefault(m => m.Role == ChatMessageRole.User)?.Content; |
| | 0 | 480 | | if (session != null && firstMessage != null) |
| | | 481 | | { |
| | 0 | 482 | | var preview = firstMessage.Length > 40 ? firstMessage[..40] + "..." : firstMessage; |
| | | 483 | | |
| | 0 | 484 | | summaries.Add(new SessionSummary |
| | 0 | 485 | | { |
| | 0 | 486 | | Id = id, |
| | 0 | 487 | | CreatedAt = session.CreatedAt, |
| | 0 | 488 | | FirstUserMessage = preview |
| | 0 | 489 | | }); |
| | | 490 | | } |
| | | 491 | | else |
| | | 492 | | { |
| | 0 | 493 | | await localStorage.RemoveItemAsync(id); |
| | 0 | 494 | | logger.LogError("Invalid session {id} is removed", id); |
| | | 495 | | } |
| | 0 | 496 | | } |
| | | 497 | | |
| | 0 | 498 | | _recentSessionsCache = [.. summaries.OrderByDescending(s => s.CreatedAt)]; |
| | | 499 | | |
| | 0 | 500 | | return [.. _recentSessionsCache.Take(count)]; |
| | 0 | 501 | | } |
| | | 502 | | |
| | | 503 | | public async Task NewSessionAsync() |
| | | 504 | | { |
| | | 505 | | // Save current session if it has messages |
| | 0 | 506 | | if (Session?.Messages.Count > 0) |
| | | 507 | | { |
| | 0 | 508 | | await SaveSessionAsync(); |
| | | 509 | | } |
| | | 510 | | |
| | 0 | 511 | | Session = CreateNewSession(); |
| | | 512 | | |
| | 0 | 513 | | await CleanupOldSessionsAsync(); |
| | 0 | 514 | | } |
| | | 515 | | |
| | | 516 | | private async Task CleanupOldSessionsAsync() |
| | | 517 | | { |
| | 0 | 518 | | var recent = await GetRecentSessionsAsync(int.MaxValue); |
| | 0 | 519 | | if (recent.Count > _maxSessions) |
| | | 520 | | { |
| | 0 | 521 | | var sessionsToDelete = recent.Skip(_maxSessions).ToList(); |
| | 0 | 522 | | foreach (var sessionToDelete in sessionsToDelete) |
| | | 523 | | { |
| | 0 | 524 | | await DeleteSessionAsync(sessionToDelete.Id); |
| | | 525 | | } |
| | | 526 | | } |
| | 0 | 527 | | } |
| | | 528 | | |
| | | 529 | | public async Task LoadSessionAsync(string id) |
| | | 530 | | { |
| | 0 | 531 | | var session = await localStorage.TryGetItemAsync<ConversationSession>(id); |
| | 0 | 532 | | if (session != null) |
| | | 533 | | { |
| | 0 | 534 | | session.Id = id; |
| | 0 | 535 | | Session = session; |
| | | 536 | | } |
| | 0 | 537 | | } |
| | | 538 | | |
| | | 539 | | public async Task DeleteSessionAsync(string id) |
| | | 540 | | { |
| | 0 | 541 | | if (Session?.Id == id) |
| | | 542 | | { |
| | 0 | 543 | | Session = CreateNewSession(); |
| | | 544 | | } |
| | 0 | 545 | | await localStorage.RemoveItemAsync(id); |
| | | 546 | | |
| | 0 | 547 | | _recentSessionsCache?.RemoveAll(s => s.Id == id); |
| | 0 | 548 | | } |
| | | 549 | | |
| | | 550 | | private async Task<List<string>> GetAllSessionIdsAsync() |
| | | 551 | | { |
| | 3 | 552 | | return [.. (await localStorage.GetAllKeysAsync()).Where(k => k.StartsWith("session_"))]; |
| | 2 | 553 | | } |
| | | 554 | | |
| | 10 | 555 | | private static string GenerateSessionId() => $"session_{DateTime.Now:s}"; |
| | | 556 | | |
| | 10 | 557 | | private static ConversationSession CreateNewSession() => new() { Id = GenerateSessionId() }; |
| | | 558 | | |
| | | 559 | | public async Task LoadLastSessionOrGenerateNewAsync() |
| | | 560 | | { |
| | 2 | 561 | | var sessionList = await GetAllSessionIdsAsync(); |
| | | 562 | | // сортируем сессии по времени создания и берем самую свежую |
| | 2 | 563 | | var lastSessionId = sessionList.OrderByDescending(id => |
| | 2 | 564 | | { |
| | 1 | 565 | | if (DateTime.TryParseExact(id.Substring(8), "s", CultureInfo.InvariantCulture, DateTimeStyles.None, out var |
| | 2 | 566 | | { |
| | 1 | 567 | | return result; |
| | 2 | 568 | | } |
| | 0 | 569 | | return DateTime.MinValue; |
| | 2 | 570 | | }).FirstOrDefault(); |
| | 2 | 571 | | if (lastSessionId != default) |
| | | 572 | | { |
| | 1 | 573 | | var fromStorage = await localStorage.TryGetItemAsync<ConversationSession>(lastSessionId); |
| | 1 | 574 | | fromStorage?.Id = lastSessionId; |
| | 1 | 575 | | Session = fromStorage ?? CreateNewSession(); |
| | | 576 | | } |
| | | 577 | | else |
| | | 578 | | { |
| | 1 | 579 | | Session = CreateNewSession(); |
| | | 580 | | } |
| | 2 | 581 | | } |
| | | 582 | | |
| | | 583 | | public void Dispose() |
| | | 584 | | { |
| | 0 | 585 | | Session.PropertyChanged -= SessionPropertyChanged; |
| | 0 | 586 | | } |
| | | 587 | | } |