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 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