Skip to content

Commit b1d29ad

Browse files
westey-mCopilot
andcommitted
Replace inline string literals with constants in ChatHistoryMemoryProvider
Extract 11 private const string fields for vector store property names (Key, Role, MessageId, AuthorName, ApplicationId, AgentId, UserId, SessionId, Content, CreatedAt, ContentEmbedding) and replace all inline usages across the collection definition, store dictionary, search result access, and filter expressions. Fixes #3801 Co-authored-by: Copilot <[email protected]>
1 parent b0fd494 commit b1d29ad

1 file changed

Lines changed: 39 additions & 27 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProvider.cs‎

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public sealed class ChatHistoryMemoryProvider : AIContextProvider, IDisposable
4141
private const string DefaultFunctionToolName = "Search";
4242
private const string DefaultFunctionToolDescription = "Allows searching for related previous chat history to help answer the user question.";
4343

44+
private const string KeyField = "Key";
45+
private const string RoleField = "Role";
46+
private const string MessageIdField = "MessageId";
47+
private const string AuthorNameField = "AuthorName";
48+
private const string ApplicationIdField = "ApplicationId";
49+
private const string AgentIdField = "AgentId";
50+
private const string UserIdField = "UserId";
51+
private const string SessionIdField = "SessionId";
52+
private const string ContentField = "Content";
53+
private const string CreatedAtField = "CreatedAt";
54+
private const string ContentEmbeddingField = "ContentEmbedding";
55+
4456
private readonly ProviderSessionState<State> _sessionState;
4557

4658
#pragma warning disable CA2213 // VectorStore is not owned by this class - caller is responsible for disposal
@@ -98,17 +110,17 @@ public ChatHistoryMemoryProvider(
98110
{
99111
Properties =
100112
[
101-
new VectorStoreKeyProperty("Key", typeof(Guid)),
102-
new VectorStoreDataProperty("Role", typeof(string)) { IsIndexed = true },
103-
new VectorStoreDataProperty("MessageId", typeof(string)) { IsIndexed = true },
104-
new VectorStoreDataProperty("AuthorName", typeof(string)),
105-
new VectorStoreDataProperty("ApplicationId", typeof(string)) { IsIndexed = true },
106-
new VectorStoreDataProperty("AgentId", typeof(string)) { IsIndexed = true },
107-
new VectorStoreDataProperty("UserId", typeof(string)) { IsIndexed = true },
108-
new VectorStoreDataProperty("SessionId", typeof(string)) { IsIndexed = true },
109-
new VectorStoreDataProperty("Content", typeof(string)) { IsFullTextIndexed = true },
110-
new VectorStoreDataProperty("CreatedAt", typeof(string)) { IsIndexed = true },
111-
new VectorStoreVectorProperty("ContentEmbedding", typeof(string), Throw.IfLessThan(vectorDimensions, 1))
113+
new VectorStoreKeyProperty(KeyField, typeof(Guid)),
114+
new VectorStoreDataProperty(RoleField, typeof(string)) { IsIndexed = true },
115+
new VectorStoreDataProperty(MessageIdField, typeof(string)) { IsIndexed = true },
116+
new VectorStoreDataProperty(AuthorNameField, typeof(string)),
117+
new VectorStoreDataProperty(ApplicationIdField, typeof(string)) { IsIndexed = true },
118+
new VectorStoreDataProperty(AgentIdField, typeof(string)) { IsIndexed = true },
119+
new VectorStoreDataProperty(UserIdField, typeof(string)) { IsIndexed = true },
120+
new VectorStoreDataProperty(SessionIdField, typeof(string)) { IsIndexed = true },
121+
new VectorStoreDataProperty(ContentField, typeof(string)) { IsFullTextIndexed = true },
122+
new VectorStoreDataProperty(CreatedAtField, typeof(string)) { IsIndexed = true },
123+
new VectorStoreVectorProperty(ContentEmbeddingField, typeof(string), Throw.IfLessThan(vectorDimensions, 1))
112124
]
113125
};
114126

@@ -207,17 +219,17 @@ protected override async ValueTask StoreAIContextAsync(InvokedContext context, C
207219
.Concat(context.ResponseMessages ?? [])
208220
.Select(message => new Dictionary<string, object?>
209221
{
210-
["Key"] = Guid.NewGuid(),
211-
["Role"] = message.Role.ToString(),
212-
["MessageId"] = message.MessageId,
213-
["AuthorName"] = message.AuthorName,
214-
["ApplicationId"] = storageScope.ApplicationId,
215-
["AgentId"] = storageScope.AgentId,
216-
["UserId"] = storageScope.UserId,
217-
["SessionId"] = storageScope.SessionId,
218-
["Content"] = message.Text,
219-
["CreatedAt"] = message.CreatedAt?.ToString("O") ?? DateTimeOffset.UtcNow.ToString("O"),
220-
["ContentEmbedding"] = message.Text,
222+
[KeyField] = Guid.NewGuid(),
223+
[RoleField] = message.Role.ToString(),
224+
[MessageIdField] = message.MessageId,
225+
[AuthorNameField] = message.AuthorName,
226+
[ApplicationIdField] = storageScope.ApplicationId,
227+
[AgentIdField] = storageScope.AgentId,
228+
[UserIdField] = storageScope.UserId,
229+
[SessionIdField] = storageScope.SessionId,
230+
[ContentField] = message.Text,
231+
[CreatedAtField] = message.CreatedAt?.ToString("O") ?? DateTimeOffset.UtcNow.ToString("O"),
232+
[ContentEmbeddingField] = message.Text,
221233
})
222234
.ToList();
223235

@@ -262,7 +274,7 @@ private async Task<string> SearchTextAsync(string userQuestion, ChatHistoryMemor
262274
}
263275

264276
// Format the results as a single context message
265-
var outputResultsText = string.Join("\n", results.Select(x => (string?)x["Content"]).Where(c => !string.IsNullOrWhiteSpace(c)));
277+
var outputResultsText = string.Join("\n", results.Select(x => (string?)x[ContentField]).Where(c => !string.IsNullOrWhiteSpace(c)));
266278
if (string.IsNullOrWhiteSpace(outputResultsText))
267279
{
268280
return string.Empty;
@@ -314,28 +326,28 @@ private async Task<string> SearchTextAsync(string userQuestion, ChatHistoryMemor
314326
Expression<Func<Dictionary<string, object?>, bool>>? filter = null;
315327
if (applicationId != null)
316328
{
317-
filter = x => (string?)x["ApplicationId"] == applicationId;
329+
filter = x => (string?)x[ApplicationIdField] == applicationId;
318330
}
319331

320332
if (agentId != null)
321333
{
322-
Expression<Func<Dictionary<string, object?>, bool>> agentIdFilter = x => (string?)x["AgentId"] == agentId;
334+
Expression<Func<Dictionary<string, object?>, bool>> agentIdFilter = x => (string?)x[AgentIdField] == agentId;
323335
filter = filter == null ? agentIdFilter : Expression.Lambda<Func<Dictionary<string, object?>, bool>>(
324336
Expression.AndAlso(filter.Body, agentIdFilter.Body),
325337
filter.Parameters);
326338
}
327339

328340
if (userId != null)
329341
{
330-
Expression<Func<Dictionary<string, object?>, bool>> userIdFilter = x => (string?)x["UserId"] == userId;
342+
Expression<Func<Dictionary<string, object?>, bool>> userIdFilter = x => (string?)x[UserIdField] == userId;
331343
filter = filter == null ? userIdFilter : Expression.Lambda<Func<Dictionary<string, object?>, bool>>(
332344
Expression.AndAlso(filter.Body, userIdFilter.Body),
333345
filter.Parameters);
334346
}
335347

336348
if (sessionId != null)
337349
{
338-
Expression<Func<Dictionary<string, object?>, bool>> sessionIdFilter = x => (string?)x["SessionId"] == sessionId;
350+
Expression<Func<Dictionary<string, object?>, bool>> sessionIdFilter = x => (string?)x[SessionIdField] == sessionId;
339351
filter = filter == null ? sessionIdFilter : Expression.Lambda<Func<Dictionary<string, object?>, bool>>(
340352
Expression.AndAlso(filter.Body, sessionIdFilter.Body),
341353
filter.Parameters);

0 commit comments

Comments
 (0)