Skip to content

Commit e65a512

Browse files
authored
Decouple Services and Infrastructure (#125)
* Move *Document types to Infrestructure * Mark DocumentClasses as internal * Change agentContext to agentConfig - remove nameConflict with Core Class * Remove Infrastructure reference from Services - move IRepositories to Domain - move configuration call from Services to Core * Fix invalid message in Agent Config Exception
1 parent a6cc23d commit e65a512

File tree

62 files changed

+771
-806
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+771
-806
lines changed

Frontend/MainFE/Components/Models/AgentContextDto.cs renamed to Frontend/MainFE/Components/Models/AgentConfigDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MaIN.Models.Rag;
44

5-
public class AgentContextDto
5+
public class AgentConfigDto
66
{
77
[JsonPropertyName("instruction")]
88
public string Instruction { get; set; }
@@ -13,4 +13,4 @@ public class AgentContextDto
1313
[JsonPropertyName("relations")]
1414
public List<string>? Relations { get; set; }
1515

16-
}
16+
}

Frontend/MainFE/Components/Models/AgentDto.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class AgentDto
2121
public bool Started { get; set; }
2222
[JsonPropertyName("flow")]
2323
public bool Flow { get; set; }
24-
[JsonPropertyName("context")]
25-
public AgentContextDto Context { get; set; }
24+
[JsonPropertyName("config")]
25+
public AgentConfigDto Config { get; set; }
2626
public AgentProcessingState State { get; set; }
2727
public bool IsProcessing { get; set; }
2828
public List<string>? AgentDependencies { get; set; } = [];
@@ -36,4 +36,4 @@ public string ProgressMessage
3636
set => _progressMessage = value;
3737
}
3838
private string? _progressMessage;
39-
}
39+
}

src/MaIN.Core.UnitTests/AgentContextTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void Constructor_ShouldInitializeNewAgent()
3434
Assert.NotNull(agentId);
3535
Assert.NotEmpty(agentId);
3636
Assert.NotNull(agent);
37-
Assert.NotNull(agent.Context);
37+
Assert.NotNull(agent.Config);
3838
Assert.NotNull(agent.Behaviours);
3939
Assert.Equal("Agent created by MaIN", agent.Description);
4040
}
@@ -88,7 +88,7 @@ public void WithInitialPrompt_ShouldSetInstruction()
8888
var result = _agentContext.WithInitialPrompt(expectedPrompt);
8989

9090
// Assert
91-
Assert.Equal(expectedPrompt, _agentContext.GetAgent().Context.Instruction);
91+
Assert.Equal(expectedPrompt, _agentContext.GetAgent().Config.Instruction);
9292
Assert.Equal(result, _agentContext);
9393
}
9494

@@ -102,7 +102,7 @@ public void WithSteps_ShouldSetAgentSteps()
102102
var result = _agentContext.WithSteps(expectedSteps);
103103

104104
// Assert
105-
Assert.Equal(expectedSteps, _agentContext.GetAgent().Context.Steps);
105+
Assert.Equal(expectedSteps, _agentContext.GetAgent().Config.Steps);
106106
Assert.Equal(result, _agentContext);
107107
}
108108

@@ -132,7 +132,7 @@ public async Task CreateAsync_ShouldCallAgentServiceCreateAgent()
132132
{
133133
Id = Guid.NewGuid().ToString(),
134134
CurrentBehaviour = "Default",
135-
Context = new AgentData()
135+
Config = new AgentConfig()
136136
};
137137
_mockAgentService
138138
.Setup(s => s.CreateAgent(
@@ -224,7 +224,7 @@ public async Task FromExisting_ShouldCreateContextFromExistingAgent()
224224
Id = existingAgentId,
225225
Name = "Existing Agent",
226226
CurrentBehaviour = "Default",
227-
Context = new AgentData()
227+
Config = new AgentConfig()
228228
};
229229

230230
_mockAgentService

src/MaIN.Core.UnitTests/FlowContextTests.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public FlowContextTests()
2525
var testModel = new GenericLocalModel(_testModelId);
2626
ModelRegistry.RegisterOrReplace(testModel);
2727
}
28-
28+
2929
[Fact]
3030
public async Task WithId_ShouldSetFlowId()
3131
{
@@ -34,7 +34,7 @@ public async Task WithId_ShouldSetFlowId()
3434

3535
// Act
3636
var result = _flowContext.WithId(expectedId);
37-
37+
3838
// Setup mock to return flow with the set ID
3939
_mockFlowService
4040
.Setup(s => s.GetFlowById(expectedId))
@@ -55,13 +55,14 @@ public async Task WithName_ShouldSetFlowName()
5555

5656
// Act
5757
var result = _flowContext.WithName(expectedName);
58-
58+
5959
// Setup mock to return flow with the set name
6060
_mockFlowService
6161
.Setup(s => s.GetFlowById(It.IsAny<string>()))
62-
.ReturnsAsync(new AgentFlow {
63-
Id = It.IsAny<string>(),
64-
Name = expectedName
62+
.ReturnsAsync(new AgentFlow
63+
{
64+
Id = It.IsAny<string>(),
65+
Name = expectedName
6566
});
6667

6768
var flow = await _flowContext.GetCurrentFlow();
@@ -92,24 +93,32 @@ public async Task CreateAsync_ShouldCallFlowService()
9293
public async Task ProcessAsync_WithStringMessage_ShouldReturnChatResult()
9394
{
9495
// Arrange
95-
var firstAgent = new Agent { Id = "first-agent", Order = 0, CurrentBehaviour = It.IsAny<string>(), Context = new AgentData()};
96+
var firstAgent = new Agent
97+
{
98+
Id = "first-agent",
99+
Order = 0,
100+
CurrentBehaviour = It.IsAny<string>(),
101+
Config = new AgentConfig()
102+
};
96103
_flowContext.AddAgent(firstAgent);
97104

98105
var message = "Hello, flow!";
99-
var chat = new Chat { Id = firstAgent.Id, Messages = new List<Message>(), ModelId = _testModelId, Name = "test"};
106+
var chat = new Chat { Id = firstAgent.Id, Messages = [], ModelId = _testModelId, Name = "test" };
100107

101108
_mockAgentService
102109
.Setup(s => s.GetChatByAgent(firstAgent.Id))
103110
.ReturnsAsync(chat);
104111

105112
_mockAgentService
106113
.Setup(s => s.Process(It.IsAny<Chat>(), firstAgent.Id, It.IsAny<Knowledge>(), It.IsAny<bool>(), null, null))
107-
.ReturnsAsync(new Chat {
108-
ModelId = _testModelId,
114+
.ReturnsAsync(new Chat
115+
{
116+
ModelId = _testModelId,
109117
Name = "test",
110-
Messages = new List<Message> {
111-
new() { Content = "Response", Role = "Assistant", Type = MessageType.LocalLLM}
112-
}
118+
Messages =
119+
[
120+
new() { Content = "Response", Role = "Assistant", Type = MessageType.LocalLLM}
121+
]
113122
});
114123

115124
// Act
@@ -165,17 +174,17 @@ public async Task FromExisting_ShouldCreateFlowContextFromExistingFlow()
165174
{
166175
// Arrange
167176
var existingFlowId = "existing-flow-id";
168-
var existingFlow = new AgentFlow
169-
{
170-
Id = existingFlowId,
177+
var existingFlow = new AgentFlow
178+
{
179+
Id = existingFlowId,
171180
Name = "Existing Flow",
172181
Agents =
173182
[
174183
new Agent
175184
{
176185
Id = "agent1",
177186
CurrentBehaviour = It.IsAny<string>(),
178-
Context = new AgentData()
187+
Config = new AgentConfig()
179188
}
180189
]
181190
};
@@ -203,4 +212,4 @@ public async Task FromExisting_ShouldThrowArgumentExceptionWhenFlowNotFound()
203212
// Act & Assert
204213
await Assert.ThrowsAsync<FlowNotFoundException>(() => _flowContext.FromExisting(nonExistentFlowId));
205214
}
206-
}
215+
}

src/MaIN.Core/Bootstrapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using MaIN.Core.Interfaces;
33
using MaIN.Core.Services;
44
using MaIN.Domain.Configuration;
5+
using MaIN.Infrastructure;
56
using MaIN.Services;
67
using MaIN.Services.Services;
78
using MaIN.Services.Services.Abstract;
@@ -18,6 +19,7 @@ public static IServiceCollection AddMaIN(
1819
Action<MaINSettings>? configureSettings = null)
1920
{
2021
services.ConfigureMaIN(configuration, configureSettings);
22+
services.ConfigureInfrastructure(configuration);
2123
services.AddAIHub();
2224
return services;
2325
}

src/MaIN.Core/Hub/Contexts/AgentContext.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal AgentContext(IAgentService agentService)
3535
Description = "Agent created by MaIN",
3636
CurrentBehaviour = "Default",
3737
Flow = false,
38-
Context = new AgentData()
38+
Config = new AgentConfig()
3939
{
4040
Instruction = "Hello, I'm your personal assistant. How can I assist you today?",
4141
Relations = [],
@@ -84,7 +84,7 @@ public async Task<IAgentContextExecutor> FromExisting(string agentId)
8484

8585
public IAgentConfigurationBuilder WithInitialPrompt(string prompt)
8686
{
87-
_agent.Context.Instruction = prompt;
87+
_agent.Config.Instruction = prompt;
8888
return this;
8989
}
9090

@@ -114,7 +114,7 @@ public IAgentConfigurationBuilder EnsureModelDownloaded()
114114

115115
public IAgentConfigurationBuilder WithSource(IAgentSource source, AgentSourceType type)
116116
{
117-
_agent.Context.Source = new AgentSource()
117+
_agent.Config.Source = new AgentSource()
118118
{
119119
Details = source,
120120
Type = type
@@ -134,8 +134,7 @@ public IAgentConfigurationBuilder WithMcpConfig(Mcp mcpConfig)
134134
{
135135
mcpConfig.Backend = ModelRegistry.GetById(_agent.Model).Backend;
136136
}
137-
138-
_agent.Context.McpConfig = mcpConfig;
137+
_agent.Config.McpConfig = mcpConfig;
139138
return this;
140139
}
141140

@@ -153,7 +152,7 @@ public IAgentConfigurationBuilder WithMemoryParams(MemoryParams memoryParams)
153152

154153
public IAgentConfigurationBuilder WithSteps(List<string>? steps)
155154
{
156-
_agent.Context.Steps = steps;
155+
_agent.Config.Steps = steps;
157156
return this;
158157
}
159158

src/MaIN.Domain/Entities/Agents/Agent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Agent
1111
public string? Description { get; init; }
1212
public bool Started { get; set; }
1313
public bool Flow { get; set; }
14-
public required AgentData Context { get; init; }
14+
public required AgentConfig Config { get; init; }
1515
public string ChatId { get; set; } = string.Empty;
1616
public int Order { get; set; }
1717
public BackendType? Backend { get; set; }
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
namespace MaIN.Domain.Entities.Agents;
22

3-
public class AgentData
3+
public class AgentConfig
44
{
55
public string? Instruction { get; set; }
66
public AgentSource.AgentSource? Source { get; set; }
77
public Mcp? McpConfig { get; set; }
88
public List<string>? Steps { get; set; }
99
public List<string>? Relations { get; set; }
10-
11-
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Net;
2+
3+
namespace MaIN.Domain.Exceptions.Agents;
4+
5+
public class AgentConfigNotFoundException(string agentId) : MaINCustomException($"Config of the agent with id: '{agentId}' not found.")
6+
{
7+
public override string PublicErrorMessage => "Agent config not found.";
8+
public override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound;
9+
}

src/MaIN.Domain/Exceptions/Agents/AgentContextNotFoundException.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)