Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions AISmart.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.GAgent.Config", "sr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.GAgent.Core", "src\AISmart.GAgent.Core\AISmart.GAgent.Core.csproj", "{CFFF8DF8-25AC-48E7-9799-BBFC5C0EA594}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.CQRS", "src\AISmart.CQRS\AISmart.CQRS.csproj", "{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.Cqrs.Tests", "test\AISmart.Cqrs.Tests\AISmart.Cqrs.Tests.csproj", "{2605D902-3B04-4386-9DE7-99FCBC5A4C8A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.AuthServer", "src\AISmart.AuthServer\AISmart.AuthServer.csproj", "{1C434C04-119E-4885-A7EA-1E7C03972805}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.Rag.Contracts", "src\AISmart.Rag.Contracts\AISmart.Rag.Contracts.csproj", "{ADFE5268-C662-450B-A727-A6CC974FEE15}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.GAgent.MicroAI", "src\AISmart.GAgent.MicroAI\AISmart.GAgent.MicroAI.csproj", "{F8C887E6-77D0-47C2-8B6B-FFB51E094396}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AiSmart.GAgent.TestAgent", "src\AiSmart.GAgent.TestAgent\AiSmart.GAgent.TestAgent.csproj", "{4EBBE2C2-7C37-4D3F-9F38-916A2758BDD1}"
Expand All @@ -95,6 +91,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.Core", "src\AISmart
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AiSmart.GAgent.SocialAgent", "src\AiSmart.GAgent.SocialGAgent\AiSmart.GAgent.SocialAgent.csproj", "{CC99AB12-4463-4386-BF17-CE0E26797E23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.CQRS", "src\AISmart.CQRS\AISmart.CQRS.csproj", "{23A8822F-83B5-4ABA-B05B-BAE9670ACD68}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AISmart.Rag.Contracts", "src\AISmart.Rag.Contracts\AISmart.Rag.Contracts.csproj", "{3A58DC0A-BA95-48B9-9468-F7892A71F4FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -221,10 +221,6 @@ Global
{BC0E7140-4A81-46BF-9A35-250996F3DC53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC0E7140-4A81-46BF-9A35-250996F3DC53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC0E7140-4A81-46BF-9A35-250996F3DC53}.Release|Any CPU.Build.0 = Release|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Release|Any CPU.Build.0 = Release|Any CPU
{1C434C04-119E-4885-A7EA-1E7C03972805}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C434C04-119E-4885-A7EA-1E7C03972805}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C434C04-119E-4885-A7EA-1E7C03972805}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -233,10 +229,6 @@ Global
{CFFF8DF8-25AC-48E7-9799-BBFC5C0EA594}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFFF8DF8-25AC-48E7-9799-BBFC5C0EA594}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFFF8DF8-25AC-48E7-9799-BBFC5C0EA594}.Release|Any CPU.Build.0 = Release|Any CPU
{ADFE5268-C662-450B-A727-A6CC974FEE15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADFE5268-C662-450B-A727-A6CC974FEE15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADFE5268-C662-450B-A727-A6CC974FEE15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADFE5268-C662-450B-A727-A6CC974FEE15}.Release|Any CPU.Build.0 = Release|Any CPU
{89A0820D-CBB0-4061-A939-F30EF486BA93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89A0820D-CBB0-4061-A939-F30EF486BA93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89A0820D-CBB0-4061-A939-F30EF486BA93}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -249,10 +241,6 @@ Global
{4EBBE2C2-7C37-4D3F-9F38-916A2758BDD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EBBE2C2-7C37-4D3F-9F38-916A2758BDD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EBBE2C2-7C37-4D3F-9F38-916A2758BDD1}.Release|Any CPU.Build.0 = Release|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673}.Release|Any CPU.Build.0 = Release|Any CPU
{2605D902-3B04-4386-9DE7-99FCBC5A4C8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2605D902-3B04-4386-9DE7-99FCBC5A4C8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2605D902-3B04-4386-9DE7-99FCBC5A4C8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -273,6 +261,14 @@ Global
{CC99AB12-4463-4386-BF17-CE0E26797E23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC99AB12-4463-4386-BF17-CE0E26797E23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC99AB12-4463-4386-BF17-CE0E26797E23}.Release|Any CPU.Build.0 = Release|Any CPU
{23A8822F-83B5-4ABA-B05B-BAE9670ACD68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23A8822F-83B5-4ABA-B05B-BAE9670ACD68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23A8822F-83B5-4ABA-B05B-BAE9670ACD68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23A8822F-83B5-4ABA-B05B-BAE9670ACD68}.Release|Any CPU.Build.0 = Release|Any CPU
{3A58DC0A-BA95-48B9-9468-F7892A71F4FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A58DC0A-BA95-48B9-9468-F7892A71F4FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A58DC0A-BA95-48B9-9468-F7892A71F4FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A58DC0A-BA95-48B9-9468-F7892A71F4FA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -308,20 +304,18 @@ Global
{C09D0934-CD67-47A0-9179-806C05ABFB26} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{BC0E7140-4A81-46BF-9A35-250996F3DC53} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{CFFF8DF8-25AC-48E7-9799-BBFC5C0EA594} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{ADFE5268-C662-450B-A727-A6CC974FEE15} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{F8C887E6-77D0-47C2-8B6B-FFB51E094396} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{4EBBE2C2-7C37-4D3F-9F38-916A2758BDD1} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{1C434C04-119E-4885-A7EA-1E7C03972805} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{2605D902-3B04-4386-9DE7-99FCBC5A4C8A} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{4808BEAC-3B68-45DB-AED3-FA6D5BB94673} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{ADFE5268-C662-450B-A727-A6CC974FEE15} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{29300C8F-53C6-4A40-86C4-D321679D5A7C} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{609DECCF-39E8-4182-98CF-51533A9B6156} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{49868FC0-2F38-4EF4-9719-6578CDF9C453} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{CC99AB12-4463-4386-BF17-CE0E26797E23} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{482AC386-F3A8-4791-A708-414E52A8177A} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{89A0820D-CBB0-4061-A939-F30EF486BA93} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{23A8822F-83B5-4ABA-B05B-BAE9670ACD68} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{3A58DC0A-BA95-48B9-9468-F7892A71F4FA} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {28315BFD-90E7-4E14-A2EA-F3D23AF4126F}
Expand Down
7 changes: 4 additions & 3 deletions src/AISmart.Application.Contracts/Agents/EventWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ public class EventWrapper<T> : EventWrapperBase
[Id(0)] public T Event { get; private set; }
[Id(1)] public Guid EventId { get; private set; }
[Id(2)] public GrainId GrainId { get; private set; }
[Id(3)] public GrainId? ContextGrainId { get; set; }
[Id(3)] public GrainId? ContextStorageGrainId { get; set; }

// Constructor
public EventWrapper(T @event, Guid eventId, GrainId grainId)
public EventWrapper(T @event, Guid eventId, GrainId grainId, GrainId? contextStorageGrainId)
{
Event = @event;
EventId = eventId;
GrainId = grainId;
ContextGrainId = null;
ContextStorageGrainId = contextStorageGrainId;

}

// Optionally, you can add methods or other functionality as needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override Task<string> GetDescriptionAsync()

public async Task<ImplementationEvent> HandleEventAsync(SocialEvent eventData)
{
Logger.LogInformation($"{this.GetType().ToString()} ExecuteAsync: Market Leader analyses content:{eventData.Content}");
Logger.LogInformation($"{GetType()} ExecuteAsync: Market Leader analyses content:{eventData.Content}");
await PublishAsync(new SendMessageEvent
{
Message = "MarketLeaderGAgent Completed."
Expand Down
2 changes: 1 addition & 1 deletion src/AISmart.Application.Grains/Agents/X/XGAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task HandleEventAsync(XThreadCreatedEvent eventData)
var publishEvent = new SocialEvent
{
Content = $"X Thread {eventData.Content} has been published."
};
}.WithContext("ThreadId", eventData.Id);
await PublishAsync(publishEvent);
await PublishAsync(new RequestAllSubscriptionsEvent());
}
Expand Down
2 changes: 2 additions & 0 deletions src/AISmart.GAgent.Core/AISmart.GAgent.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" />
<PackageReference Include="Microsoft.Orleans.EventSourcing" />
<PackageReference Include="Microsoft.Orleans.Reminders" />
<PackageReference Include="Microsoft.Orleans.Streaming" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/AISmart.GAgent.Core/AISmartGAgentConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public static class AISmartGAgentConstants
public const string SubscribersStateName = "Subscribers";
public const string SubscriptionsStateName = "Subscriptions";
public const string PublishersStateName = "Publishers";
public const string ContextStorageGrainSelfTerminateReminderName = "DeleteSelfReminder";
public static TimeSpan DefaultContextStorageGrainSelfDeleteTime = TimeSpan.FromMinutes(10);
}
51 changes: 45 additions & 6 deletions src/AISmart.GAgent.Core/ContextStorageGrain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Orleans.Storage;
using Microsoft.Extensions.Logging;

namespace AISmart.GAgent.Core;

Expand All @@ -13,32 +12,42 @@ public interface IContextStorageGrain : IGrainWithGuidKey
Task AddContext(string key, object? value);
Task AddContext(Dictionary<string, object?> context);
Task<Dictionary<string, object?>> GetContext();
Task ResetSelfTerminateTime(TimeSpan timeSpan);
}

public class ContextStorageGrain : Grain<ContextStorageState>, IContextStorageGrain
public class ContextStorageGrain : Grain<ContextStorageState>, IContextStorageGrain, IRemindable
{
private readonly IPersistentState<ContextStorageState> _context;
private readonly ILogger<ContextStorageGrain> _logger;
private IGrainReminder _reminder;

private readonly IGrainStorage _grainStorage;

public ContextStorageGrain([PersistentState("State")] IPersistentState<ContextStorageState> context)
public ContextStorageGrain([PersistentState("ContextStorage")] IPersistentState<ContextStorageState> context,
ILogger<ContextStorageGrain> logger)
{
_context = context;
_logger = logger;
}

public Task AddContext(string key, object? value)
{
_logger.LogInformation($"Adding context {key}: {value}, ContextStorageGrain's GrainId: {this.GetGrainId()}");

_context.ReadStateAsync();

if (_context.State.Context.IsNullOrEmpty())
{
_context.State.Context = [];
}

_context.State.Context[key] = value;
_context.WriteStateAsync();
return Task.CompletedTask;
}

public Task AddContext(Dictionary<string, object?> context)
{
_context.ReadStateAsync();

if (_context.State.Context.IsNullOrEmpty())
{
_context.State.Context = context;
Expand All @@ -47,15 +56,45 @@ public Task AddContext(Dictionary<string, object?> context)
{
foreach (var keyPair in context)
{
_logger.LogInformation(
$"Adding context {keyPair.Key}: {keyPair.Value}, ContextStorageGrain's GrainId: {this.GetGrainId()}");
_context.State.Context[keyPair.Key] = keyPair.Value;
}
}

_context.WriteStateAsync();
return Task.CompletedTask;
}

public Task<Dictionary<string, object?>> GetContext()
{
_context.ReadStateAsync();
return Task.FromResult(_context.State.Context);
}

public async Task ReceiveReminder(string reminderName, TickStatus status)
{
if (reminderName == "DeleteSelfReminder")
{
_logger.LogInformation($"Reminder triggered for grain {this.GetGrainId()}, deleting grain.");
await ClearStateAsync();
DeactivateOnIdle();
}
}

public async Task ResetSelfTerminateTime(TimeSpan timeSpan)
{
_reminder = await this.RegisterOrUpdateReminder(
AISmartGAgentConstants.ContextStorageGrainSelfTerminateReminderName,
timeSpan, timeSpan);
}

public override async Task OnActivateAsync(CancellationToken cancellationToken)
{
_reminder = await this.RegisterOrUpdateReminder(
AISmartGAgentConstants.ContextStorageGrainSelfTerminateReminderName,
AISmartGAgentConstants.DefaultContextStorageGrainSelfDeleteTime,
AISmartGAgentConstants.DefaultContextStorageGrainSelfDeleteTime);
await base.OnActivateAsync(cancellationToken);
}
}
60 changes: 60 additions & 0 deletions src/AISmart.GAgent.Core/GAgentBase.Context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace AISmart.GAgent.Core;

public abstract partial class GAgentBase<TState, TEvent>
{
private GrainId? _contextStorageGrainId;

protected async Task SetContextAsync(string key, object? value)
{
if (_contextStorageGrainId != null)
{
var contextStorageGrain = GrainFactory.GetGrain<IContextStorageGrain>(_contextStorageGrainId.Value.GetGuidKey());
await contextStorageGrain.AddContext(key, value);
}
}

protected async Task SetContextAsync(Dictionary<string, object?> context)
{
if (_contextStorageGrainId != null)
{
var contextStorageGrain = GrainFactory.GetGrain<IContextStorageGrain>(_contextStorageGrainId.Value.GetGuidKey());
await contextStorageGrain.AddContext(context);
}
}

protected async Task ResetContextStorageGrainTerminateTimeAsync(TimeSpan timeSpan)
{
if (_contextStorageGrainId != null)
{
var contextStorageGrain = GrainFactory.GetGrain<IContextStorageGrain>(_contextStorageGrainId.Value.GetGuidKey());
await contextStorageGrain.ResetSelfTerminateTime(timeSpan);
}
}

protected async Task<Dictionary<string, object?>> GetContextAsync()
{
if (_contextStorageGrainId != null)
{
var contextStorageGrain =
GrainFactory.GetGrain<IContextStorageGrain>(_contextStorageGrainId.Value.GetGuidKey());
return await contextStorageGrain.GetContext();
}

return new Dictionary<string, object?>();
}

private void SetContextStorageGrainId(GrainId? grainId)
{
_contextStorageGrainId = grainId;
}

private GrainId? GetContextStorageGrainId()
{
return _contextStorageGrainId;
}

private void ClearContext()
{
_contextStorageGrainId = null;
}
}
Loading