From dc3dd5f77925fdcc2e55e33a714ac503b48f1947 Mon Sep 17 00:00:00 2001 From: southclown Date: Fri, 7 Feb 2025 17:12:49 +0000 Subject: [PATCH 1/2] Main In AgentState.cs: Added Metadata dictionary to store flexible key-value pairs for additional agent information Added LastUpdated timestamp to track state changes Added CurrentStatus property to monitor agent status Implemented UpdateState() method for clean state updates Implemented AddMetadata() method for metadata management Added property initializers for clean instantiation In IAgent.cs: Enhanced interface with core agent operations: GetStateAsync(): Retrieves current agent state UpdateStateAsync(): Updates agent state ProcessMessageAsync(): Handles message processing ResetStateAsync(): Resets agent to initial state --- src/Oagents.Core/Abstractions/AgentState.cs | 18 ++++++++++++++++-- src/Oagents.Core/Abstractions/IAgent.cs | 6 +++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Oagents.Core/Abstractions/AgentState.cs b/src/Oagents.Core/Abstractions/AgentState.cs index 10bc212f..640a1e0f 100644 --- a/src/Oagents.Core/Abstractions/AgentState.cs +++ b/src/Oagents.Core/Abstractions/AgentState.cs @@ -2,6 +2,20 @@ namespace Microsoft.AI.Agents.Abstractions; public class AgentState where T: class, new() { - public List History { get; set; } - public T Data { get; set; } + public List History { get; set; } = new(); + public T Data { get; set; } = new(); + public Dictionary Metadata { get; set; } = new(); + public DateTime LastUpdated { get; set; } = DateTime.UtcNow; + public string CurrentStatus { get; set; } = "Ready"; + + public void UpdateState(T newData) + { + Data = newData; + LastUpdated = DateTime.UtcNow; + } + + public void AddMetadata(string key, object value) + { + Metadata[key] = value; + } } diff --git a/src/Oagents.Core/Abstractions/IAgent.cs b/src/Oagents.Core/Abstractions/IAgent.cs index d05c4c37..a24b5d45 100644 --- a/src/Oagents.Core/Abstractions/IAgent.cs +++ b/src/Oagents.Core/Abstractions/IAgent.cs @@ -1,7 +1,11 @@ namespace Microsoft.AI.Agents.Abstractions; -public interface IAgent +public interface IAgent where T : class, new() { Task HandleEvent(Event item); Task PublishEvent(string ns, string id, Event item); + Task> GetStateAsync(); + Task UpdateStateAsync(AgentState state); + Task ProcessMessageAsync(string message); + Task ResetStateAsync(); } \ No newline at end of file From a1ba42d5f0814a71149532bdaebb69073fc785e7 Mon Sep 17 00:00:00 2001 From: southclown Date: Sun, 9 Feb 2025 14:51:12 +0000 Subject: [PATCH 2/2] Enhanced memory management and performance monitoring --- .../Memory/EnhancedMemoryManager.cs | 72 +++++++++++++++++++ .../Monitoring/PerformanceMonitor.cs | 49 +++++++++++++ src/Oagents.Core/Oagents.Core.csproj | 4 +- 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/Oagents.Core/Memory/EnhancedMemoryManager.cs create mode 100644 src/Oagents.Core/Monitoring/PerformanceMonitor.cs diff --git a/src/Oagents.Core/Memory/EnhancedMemoryManager.cs b/src/Oagents.Core/Memory/EnhancedMemoryManager.cs new file mode 100644 index 00000000..461048c7 --- /dev/null +++ b/src/Oagents.Core/Memory/EnhancedMemoryManager.cs @@ -0,0 +1,72 @@ +using Microsoft.SemanticKernel.Memory; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AI.Agents.Core.Memory +{ + public class EnhancedMemoryManager + { + private readonly ISemanticTextMemory _memory; + private readonly ILogger _logger; + private readonly int _maxRetries = 3; + + public EnhancedMemoryManager(ISemanticTextMemory memory, ILogger logger) + { + _memory = memory; + _logger = logger; + } + + public async Task StoreWithRetryAsync(string collection, string text, string id, string description = "", int attempt = 0) + { + try + { + await _memory.SaveInformationAsync(collection, text, id, description); + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, $"Error storing memory on attempt {attempt + 1}"); + + if (attempt < _maxRetries) + { + await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, attempt))); + return await StoreWithRetryAsync(collection, text, id, description, attempt + 1); + } + + return false; + } + } + + public async Task SearchMemoryAsync(string collection, string query, double minRelevance = 0.7, int limit = 5) + { + try + { + var results = _memory.SearchAsync(collection, query, limit: limit, minRelevanceScore: minRelevance); + + var items = new List(); + await foreach (var item in results) + { + items.Add(item.Metadata); + } + + return new MemoryQueryResult + { + Query = query, + Results = items, + Timestamp = DateTime.UtcNow + }; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error searching memory"); + return null; + } + } + } + + public class MemoryQueryResult + { + public string Query { get; set; } + public List Results { get; set; } + public DateTime Timestamp { get; set; } + } +} \ No newline at end of file diff --git a/src/Oagents.Core/Monitoring/PerformanceMonitor.cs b/src/Oagents.Core/Monitoring/PerformanceMonitor.cs new file mode 100644 index 00000000..a9bbe630 --- /dev/null +++ b/src/Oagents.Core/Monitoring/PerformanceMonitor.cs @@ -0,0 +1,49 @@ +using System.Diagnostics; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AI.Agents.Core.Monitoring +{ + public class PerformanceMonitor + { + private readonly ILogger _logger; + private readonly Dictionary _operations; + + public PerformanceMonitor(ILogger logger) + { + _logger = logger; + _operations = new Dictionary(); + } + + public void StartOperation(string operationName) + { + var watch = new Stopwatch(); + watch.Start(); + _operations[operationName] = watch; + } + + public TimeSpan EndOperation(string operationName) + { + if (_operations.TryGetValue(operationName, out var watch)) + { + watch.Stop(); + _operations.Remove(operationName); + + var duration = watch.Elapsed; + _logger.LogInformation($"Operation {operationName} completed in {duration.TotalMilliseconds}ms"); + + return duration; + } + + _logger.LogWarning($"No operation found with name: {operationName}"); + return TimeSpan.Zero; + } + + public Dictionary GetActiveOperations() + { + return _operations.ToDictionary( + kvp => kvp.Key, + kvp => kvp.Value.Elapsed + ); + } + } +} \ No newline at end of file diff --git a/src/Oagents.Core/Oagents.Core.csproj b/src/Oagents.Core/Oagents.Core.csproj index 1ce891cf..1abcb97a 100644 --- a/src/Oagents.Core/Oagents.Core.csproj +++ b/src/Oagents.Core/Oagents.Core.csproj @@ -16,7 +16,9 @@ - + + + \ No newline at end of file