Skip to content

Commit 0e2cd27

Browse files
committed
Move *Document types to Infrestructure
1 parent d43f4d2 commit 0e2cd27

42 files changed

Lines changed: 671 additions & 699 deletions

Some content is hidden

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

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/Hub/Contexts/AgentContext.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using MaIN.Domain.Entities.Tools;
88
using MaIN.Domain.Exceptions.Agents;
99
using MaIN.Domain.Models;
10-
using MaIN.Domain.Models.Abstract;
1110
using MaIN.Services.Constants;
1211
using MaIN.Services.Services.Abstract;
1312
using MaIN.Services.Services.Models;
@@ -35,7 +34,7 @@ internal AgentContext(IAgentService agentService)
3534
Description = "Agent created by MaIN",
3635
CurrentBehaviour = "Default",
3736
Flow = false,
38-
Context = new AgentData()
37+
Config = new AgentConfig()
3938
{
4039
Instruction = "Hello, I'm your personal assistant. How can I assist you today?",
4140
Relations = [],
@@ -84,7 +83,7 @@ public async Task<IAgentContextExecutor> FromExisting(string agentId)
8483

8584
public IAgentConfigurationBuilder WithInitialPrompt(string prompt)
8685
{
87-
_agent.Context.Instruction = prompt;
86+
_agent.Config.Instruction = prompt;
8887
return this;
8988
}
9089

@@ -114,7 +113,7 @@ public IAgentConfigurationBuilder EnsureModelDownloaded()
114113

115114
public IAgentConfigurationBuilder WithSource(IAgentSource source, AgentSourceType type)
116115
{
117-
_agent.Context.Source = new AgentSource()
116+
_agent.Config.Source = new AgentSource()
118117
{
119118
Details = source,
120119
Type = type
@@ -134,8 +133,7 @@ public IAgentConfigurationBuilder WithMcpConfig(Mcp mcpConfig)
134133
{
135134
mcpConfig.Backend = ModelRegistry.GetById(_agent.Model).Backend;
136135
}
137-
138-
_agent.Context.McpConfig = mcpConfig;
136+
_agent.Config.McpConfig = mcpConfig;
139137
return this;
140138
}
141139

@@ -153,7 +151,7 @@ public IAgentConfigurationBuilder WithMemoryParams(MemoryParams memoryParams)
153151

154152
public IAgentConfigurationBuilder WithSteps(List<string>? steps)
155153
{
156-
_agent.Context.Steps = steps;
154+
_agent.Config.Steps = steps;
157155
return this;
158156
}
159157

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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using MaIN.Domain.Entities.Agents;
2+
using MaIN.Domain.Entities.Agents.AgentSource;
3+
using MaIN.Domain.Exceptions.Agents;
4+
using MaIN.Infrastructure.Models;
5+
using System.Text.Json;
6+
7+
namespace MaIN.Infrastructure.Mappers;
8+
9+
internal static class AgentDocumentMapper
10+
{
11+
internal static AgentDocument ToDocument(this Agent agent) => new()
12+
{
13+
Id = agent.Id,
14+
Name = agent.Name,
15+
Model = agent.Model,
16+
Order = agent.Order,
17+
Started = agent.Started,
18+
Flow = agent.Flow,
19+
ToolsConfiguration = agent.ToolsConfiguration,
20+
Backend = agent.Backend,
21+
ChatId = agent.ChatId,
22+
Description = agent.Description,
23+
Behaviours = agent.Behaviours,
24+
CurrentBehaviour = agent.CurrentBehaviour,
25+
Config = agent.Config.ToDocument()
26+
};
27+
28+
internal static Agent ToDomain(this AgentDocument agent) => new()
29+
{
30+
Id = agent.Id,
31+
Name = agent.Name,
32+
Model = agent.Model,
33+
Started = agent.Started,
34+
Order = agent.Order,
35+
Flow = agent.Flow,
36+
ToolsConfiguration = agent.ToolsConfiguration,
37+
Backend = agent.Backend,
38+
Description = agent.Description,
39+
Behaviours = agent.Behaviours,
40+
CurrentBehaviour = agent.CurrentBehaviour,
41+
Config = agent.Config?.ToDomain() ?? throw new AgentContextNotFoundException(agent.Id)
42+
};
43+
44+
internal static AgentConfigDocument ToDocument(this AgentConfig context) => new()
45+
{
46+
Instruction = context.Instruction,
47+
Relations = context.Relations?.ToList(),
48+
Steps = context.Steps?.ToList(),
49+
McpConfig = context.McpConfig,
50+
Source = context.Source is not null
51+
? new AgentSourceDocument
52+
{
53+
DetailsSerialized = JsonSerializer.Serialize(context.Source.Details),
54+
AdditionalMessage = context.Source.AdditionalMessage,
55+
Type = Enum.Parse<AgentSourceTypeDocument>(context.Source.Type.ToString())
56+
}
57+
: null
58+
};
59+
60+
internal static AgentConfig ToDomain(this AgentConfigDocument agentContextDocument) => new()
61+
{
62+
Instruction = agentContextDocument.Instruction,
63+
Relations = agentContextDocument.Relations,
64+
McpConfig = agentContextDocument.McpConfig,
65+
Source = agentContextDocument.Source is not null
66+
? new AgentSource
67+
{
68+
AdditionalMessage = agentContextDocument.Source.AdditionalMessage,
69+
Details = agentContextDocument.Source.DetailsSerialized,
70+
Type = Enum.Parse<AgentSourceType>(agentContextDocument.Source.Type.ToString())
71+
}
72+
: null,
73+
Steps = agentContextDocument.Steps
74+
};
75+
76+
internal static AgentFlowDocument ToDocument(this AgentFlow agentFlow) => new()
77+
{
78+
Id = agentFlow.Id!,
79+
Name = agentFlow.Name,
80+
Description = agentFlow.Description!,
81+
Agents = [.. agentFlow.Agents.Select(x => x.ToDocument())]
82+
};
83+
84+
internal static AgentFlow ToDomain(this AgentFlowDocument agentFlow) => new()
85+
{
86+
Id = agentFlow.Id,
87+
Name = agentFlow.Name,
88+
Description = agentFlow.Description,
89+
Agents = [.. agentFlow.Agents.Select(x => x.ToDomain())]
90+
};
91+
}

0 commit comments

Comments
 (0)