mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Fix potential multiple enumerations (#742)
There are more of these in unit tests and samples, where I think it's not a big deal. The PR fixes code in the SDK (one false positive simply resolved using ICollection) and code that could be copied into other projects. Co-authored-by: Adrian Bonar <[email protected]>
This commit is contained in:
co-authored by
Adrian Bonar
parent
e8dced1f7c
commit
826adf1a95
@@ -147,7 +147,7 @@ public class VolatileMemoryStore : IMemoryStore
|
||||
return AsyncEnumerable.Empty<(MemoryRecord, double)>();
|
||||
}
|
||||
|
||||
IEnumerable<MemoryRecord>? embeddingCollection = null;
|
||||
ICollection<MemoryRecord>? embeddingCollection = null;
|
||||
if (this.TryGetCollection(collectionName, out var collectionDict))
|
||||
{
|
||||
embeddingCollection = collectionDict.Values;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.CommandLine;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -67,14 +68,16 @@ public static class Program
|
||||
.WithRedirectUri(config.RedirectUri)
|
||||
.Build();
|
||||
var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
|
||||
var accounts = await app.GetAccountsAsync();
|
||||
if (!accounts.Any())
|
||||
IEnumerable<IAccount>? accounts = await app.GetAccountsAsync();
|
||||
IAccount? first = accounts.FirstOrDefault();
|
||||
|
||||
if (first is null)
|
||||
{
|
||||
Console.WriteLine("Error: No accounts found");
|
||||
return null;
|
||||
}
|
||||
|
||||
return accounts.First().HomeAccountId.Identifier;
|
||||
return first.HomeAccountId.Identifier;
|
||||
}
|
||||
catch (Exception ex) when (ex is MsalServiceException || ex is MsalClientException)
|
||||
{
|
||||
|
||||
@@ -130,22 +130,16 @@ public class ChatHistoryController : ControllerBase
|
||||
[FromQuery] int startIdx = 0,
|
||||
[FromQuery] int count = -1)
|
||||
{
|
||||
// TODO: the code mixes strings and Guid without being explicit about the serialization format
|
||||
var chatMessages = await this._chatMessageRepository.FindByChatIdAsync(chatId.ToString());
|
||||
if (chatMessages == null)
|
||||
{
|
||||
return this.NotFound($"No messages found for chat of id {chatId}.");
|
||||
return this.NotFound($"No messages found for chat id '{chatId}'.");
|
||||
}
|
||||
|
||||
if (startIdx >= chatMessages.Count())
|
||||
{
|
||||
return this.BadRequest($"Start index {startIdx} is out of range.");
|
||||
}
|
||||
else if (startIdx + count > chatMessages.Count() || count == -1)
|
||||
{
|
||||
count = chatMessages.Count() - startIdx;
|
||||
}
|
||||
chatMessages = chatMessages.OrderByDescending(m => m.Timestamp).Skip(startIdx);
|
||||
if (count >= 0) { chatMessages = chatMessages.Take(count); }
|
||||
|
||||
chatMessages = chatMessages.OrderByDescending(m => m.Timestamp).Skip(startIdx).Take(count);
|
||||
return this.Ok(chatMessages);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,12 +28,13 @@ public class ChatMessageRepository : Repository<ChatMessage>
|
||||
|
||||
public async Task<ChatMessage> FindLastByChatIdAsync(string chatId)
|
||||
{
|
||||
var messages = await this.FindByChatIdAsync(chatId);
|
||||
if (!messages.Any())
|
||||
var chatMessages = await this.FindByChatIdAsync(chatId);
|
||||
var first = chatMessages.MaxBy(e => e.Timestamp);
|
||||
if (first is null)
|
||||
{
|
||||
throw new KeyNotFoundException($"No messages found for chat {chatId}.");
|
||||
throw new KeyNotFoundException($"No messages found for chat '{chatId}'.");
|
||||
}
|
||||
|
||||
return messages.OrderByDescending(e => e.Timestamp).First();
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user