Skip to content

Commit 6051700

Browse files
author
Piotr Stachaczynski
committed
feat: big biggy cleanup
1 parent 48adff7 commit 6051700

90 files changed

Lines changed: 347 additions & 474 deletions

File tree

Some content is hidden

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

Examples/Examples/Examples.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,4 @@
3838
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3939
</None>
4040
</ItemGroup>
41-
42-
<ItemGroup>
43-
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
44-
</ItemGroup>
45-
4641
</Project>

Examples/Examples/Utils/ConsoleRenderer.cs

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

Examples/Examples/Utils/ImagePreviewer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void ShowImage(byte[]? imageData, string extension = "png")
2121
$"{Guid.NewGuid()}.{extension}"
2222
);
2323

24-
File.WriteAllBytes(tempFile, imageData);
24+
File.WriteAllBytes(tempFile, imageData!);
2525

2626
try
2727
{

MaIN.Server/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
return Results.Ok(models);
4747
});
4848

49-
app.MapDelete("/api/llm/session/{chatId}", async ([FromServices] ILLMService llmService, string chatId) =>
49+
app.MapDelete("/api/llm/session/{chatId}", async ([FromServices] ILLMService llmService, string? chatId) =>
5050
{
5151
await llmService.CleanSessionCache(chatId);
5252
return Results.Ok($"Session chat {chatId} has been cleared.");
@@ -67,6 +67,6 @@
6767
app.Run();
6868

6969
// Request DTOs
70-
record ChatRequest(Chat? Chat, bool InteractiveUpdates = false, bool NewSession = false);
70+
record ChatRequest(Chat Chat, bool InteractiveUpdates = false, bool NewSession = false);
7171

72-
record AskMemoryRequest(Chat? Chat, Dictionary<string,string>? TextData = null, Dictionary<string,string>? FileData = null, List<string>? Memory = null);
72+
record AskMemoryRequest(Chat Chat, Dictionary<string,string>? TextData = null, Dictionary<string,string>? FileData = null, List<string>? Memory = null);

src/MaIN.Core.UnitTests/AgentContextTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void WithBehaviour_ShouldAddBehaviourAndSetCurrent()
115115

116116
// Assert
117117
var agent = _agentContext.GetAgent();
118-
Assert.Contains(behaviourName, agent.Behaviours.Keys);
118+
Assert.Contains(behaviourName, agent.Behaviours!.Keys);
119119
Assert.Equal(behaviourInstruction, agent.Behaviours[behaviourName]);
120120
Assert.Equal(behaviourName, agent.CurrentBehaviour);
121121
Assert.Equal(result, _agentContext);
@@ -125,7 +125,7 @@ public void WithBehaviour_ShouldAddBehaviourAndSetCurrent()
125125
public async Task CreateAsync_ShouldCallAgentServiceCreateAgent()
126126
{
127127
// Arrange
128-
var agent = new Agent() {Id = Guid.NewGuid().ToString()};
128+
var agent = new Agent() {Id = Guid.NewGuid().ToString(), CurrentBehaviour = "Default", Context = new AgentData()};
129129
_mockAgentService
130130
.Setup(s => s.CreateAgent(
131131
It.IsAny<Agent>(),
@@ -154,7 +154,7 @@ public async Task ProcessAsync_WithStringMessage_ShouldReturnChatResult()
154154
// Arrange
155155
var message = "Hello, agent!";
156156
var chat = new Chat { Id = _agentContext.GetAgentId(), Messages = new List<Message>() };
157-
var chatResult = new ChatResult { Done = true, Model = "test-model" };
157+
var chatResult = new ChatResult { Done = true, Model = "test-model", Message = new MessageDto()};
158158

159159
_mockAgentService
160160
.Setup(s => s.GetChatByAgent(_agentContext.GetAgentId()))
@@ -183,7 +183,7 @@ public async Task FromExisting_ShouldCreateContextFromExistingAgent()
183183
{
184184
// Arrange
185185
var existingAgentId = "existing-agent-id";
186-
var existingAgent = new Agent { Id = existingAgentId, Name = "Existing Agent" };
186+
var existingAgent = new Agent { Id = existingAgentId, Name = "Existing Agent", CurrentBehaviour = "Default", Context = new AgentData() };
187187

188188
_mockAgentService
189189
.Setup(s => s.GetAgentById(existingAgentId))
@@ -205,7 +205,7 @@ public async Task FromExisting_ShouldThrowArgumentExceptionWhenAgentNotFound()
205205

206206
_mockAgentService
207207
.Setup(s => s.GetAgentById(nonExistentAgentId))
208-
.ReturnsAsync((Agent)null);
208+
.ReturnsAsync((Agent)null!);
209209

210210
// Act & Assert
211211
await Assert.ThrowsAsync<ArgumentException>(() =>

src/MaIN.Core.UnitTests/ChatContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void WithFiles_ShouldAttachFilesToLastMessage()
7575
public async Task CompleteAsync_ShouldCallChatService()
7676
{
7777
// Arrange
78-
var chatResult = new ChatResult();
78+
var chatResult = new ChatResult(){ Model = "test-model", Message = new MessageDto() };
7979
_mockChatService.Setup(s => s.Completions(It.IsAny<Chat>(), It.IsAny<bool>(), It.IsAny<bool>(), null))
8080
.ReturnsAsync(chatResult);
8181

src/MaIN.Core.UnitTests/FlowContextTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task WithId_ShouldSetFlowId()
3232
// Setup mock to return flow with the set ID
3333
_mockFlowService
3434
.Setup(s => s.GetFlowById(expectedId))
35-
.ReturnsAsync(new AgentFlow { Id = expectedId });
35+
.ReturnsAsync(new AgentFlow { Id = expectedId, Name = It.IsAny<string>() });
3636

3737
var flow = await _flowContext.GetCurrentFlow();
3838

@@ -69,7 +69,7 @@ public async Task WithName_ShouldSetFlowName()
6969
public async Task CreateAsync_ShouldCallFlowService()
7070
{
7171
// Arrange
72-
var expectedFlow = new AgentFlow { Id = "new-flow-id" };
72+
var expectedFlow = new AgentFlow { Id = "new-flow-id", Name = It.IsAny<string>() };
7373
_mockFlowService
7474
.Setup(s => s.CreateFlow(It.IsAny<AgentFlow>()))
7575
.ReturnsAsync(expectedFlow);
@@ -86,7 +86,7 @@ public async Task CreateAsync_ShouldCallFlowService()
8686
public async Task ProcessAsync_WithStringMessage_ShouldReturnChatResult()
8787
{
8888
// Arrange
89-
var firstAgent = new Agent { Id = "first-agent", Order = 0 };
89+
var firstAgent = new Agent { Id = "first-agent", Order = 0, CurrentBehaviour = It.IsAny<string>(), Context = new AgentData()};
9090
_flowContext.AddAgent(firstAgent);
9191

9292
var message = "Hello, flow!";
@@ -138,8 +138,8 @@ public async Task GetAllFlows_ShouldReturnListOfFlows()
138138
// Arrange
139139
var expectedFlows = new List<AgentFlow>
140140
{
141-
new() { Id = "flow1" },
142-
new() { Id = "flow2" }
141+
new() { Id = "flow1", Name = "Flow 1" },
142+
new() { Id = "flow2", Name = "Flow 2" }
143143
};
144144

145145
_mockFlowService
@@ -162,7 +162,15 @@ public async Task FromExisting_ShouldCreateFlowContextFromExistingFlow()
162162
{
163163
Id = existingFlowId,
164164
Name = "Existing Flow",
165-
Agents = new List<Agent> { new Agent { Id = "agent1" } }
165+
Agents =
166+
[
167+
new Agent
168+
{
169+
Id = "agent1",
170+
CurrentBehaviour = It.IsAny<string>(),
171+
Context = new AgentData()
172+
}
173+
]
166174
};
167175

168176
_mockFlowService

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal AgentContext(IAgentService agentService)
2323
Behaviours = new Dictionary<string, string>(),
2424
Name = $"Agent-{Guid.NewGuid()}",
2525
Description = "Agent created by MaIN",
26+
CurrentBehaviour = "Default",
2627
Flow = false,
2728
Context = new AgentData()
2829
{
@@ -40,7 +41,7 @@ internal AgentContext(IAgentService agentService, Agent existingAgent)
4041
_agent = existingAgent;
4142
}
4243

43-
public AgentContext WithId(string? id)
44+
public AgentContext WithId(string id)
4445
{
4546
_agent.Id = id;
4647
return this;
@@ -71,7 +72,7 @@ public AgentContext WithName(string name)
7172
return this;
7273
}
7374

74-
public AgentContext WithModel(string model)
75+
public AgentContext WithModel(string? model)
7576
{
7677
_agent.Model = model;
7778
return this;
@@ -83,7 +84,7 @@ public AgentContext WithInferenceParams(InferenceParams inferenceParams)
8384
return this;
8485
}
8586

86-
public AgentContext WithCustomModel(string model, string path)
87+
public AgentContext WithCustomModel(string? model, string path)
8788
{
8889
KnownModels.AddModel(model, path);
8990
_agent.Model = model;
@@ -103,7 +104,7 @@ public AgentContext WithSteps(List<string>? steps)
103104
return this;
104105
}
105106

106-
public AgentContext WithBehaviour(string? name, string instruction)
107+
public AgentContext WithBehaviour(string name, string instruction)
107108
{
108109
_agent.Behaviours ??= new Dictionary<string, string>();
109110
_agent.Behaviours[name] = instruction;
@@ -123,7 +124,7 @@ public AgentContext Create(bool flow = false, bool interactiveResponse = false)
123124
return this;
124125
}
125126

126-
public async Task<ChatResult> ProcessAsync(Chat? chat, bool translate = false)
127+
public async Task<ChatResult> ProcessAsync(Chat chat, bool translate = false)
127128
{
128129
var result = await _agentService.Process(chat, _agent.Id, translate);
129130
var message = result!.Messages!.LastOrDefault()!.ToDto();
@@ -139,7 +140,7 @@ public async Task<ChatResult> ProcessAsync(Chat? chat, bool translate = false)
139140
public async Task<ChatResult> ProcessAsync(string message, bool translate = false)
140141
{
141142
var chat = await _agentService.GetChatByAgent(_agent.Id);
142-
chat.Messages?.Add(new Message()
143+
chat?.Messages.Add(new Message()
143144
{
144145
Content = message,
145146
Role = "User",
@@ -159,9 +160,9 @@ public async Task<ChatResult> ProcessAsync(string message, bool translate = fals
159160
public async Task<ChatResult> ProcessAsync(Message message, bool translate = false)
160161
{
161162
var chat = await _agentService.GetChatByAgent(_agent.Id);
162-
chat.Messages?.Add(message);
163+
chat?.Messages.Add(message);
163164
var result = await _agentService.Process(chat, _agent.Id, translate);
164-
var messageResult = result!.Messages!.LastOrDefault()!.ToDto();
165+
var messageResult = result!.Messages.LastOrDefault()!.ToDto();
165166
return new ChatResult()
166167
{
167168
Done = true,
@@ -171,12 +172,12 @@ public async Task<ChatResult> ProcessAsync(Message message, bool translate = fal
171172
};
172173
}
173174

174-
public async Task<Chat?> GetChat()
175+
public async Task<Chat> GetChat()
175176
{
176177
return await _agentService.GetChatByAgent(_agent.Id);
177178
}
178179

179-
public async Task<Chat?> RestartChat()
180+
public async Task<Chat> RestartChat()
180181
{
181182
return await _agentService.Restart(_agent.Id);
182183
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal ChatContext(IChatService chatService, Chat existingChat)
2828
_chat = existingChat;
2929
}
3030

31-
public ChatContext WithModel(string model)
31+
public ChatContext WithModel(string? model)
3232
{
3333
_chat.Model = model;
3434
return this;
@@ -40,7 +40,7 @@ public ChatContext WithInferenceParams(InferenceParams inferenceParams)
4040
return this;
4141
}
4242

43-
public ChatContext WithCustomModel(string model, string path)
43+
public ChatContext WithCustomModel(string? model, string path)
4444
{
4545
KnownModels.AddModel(model, path);
4646
_chat.Model = model;
@@ -107,7 +107,7 @@ public ChatContext EnableVisual()
107107
return this;
108108
}
109109

110-
public string GetChatId() => _chat.Id;
110+
public string? GetChatId() => _chat.Id;
111111

112112
public async Task<ChatResult> CompleteAsync(
113113
bool translate = false,
@@ -122,7 +122,7 @@ public async Task<ChatResult> CompleteAsync(
122122
}
123123

124124

125-
public async Task<Chat?> GetCurrentChat()
125+
public async Task<Chat> GetCurrentChat()
126126
{
127127
if (_chat.Id == null)
128128
throw new InvalidOperationException("Chat has not been created yet. Call CompleteAsync first.");
@@ -143,7 +143,7 @@ public async Task DeleteChat()
143143
await _chatService.Delete(_chat.Id);
144144
}
145145

146-
private async Task<bool> ChatExists(string id)
146+
private async Task<bool> ChatExists(string? id)
147147
{
148148
try
149149
{
@@ -157,7 +157,7 @@ private async Task<bool> ChatExists(string id)
157157
}
158158

159159
// Static methods to create builder from existing chat
160-
public async Task<ChatContext> FromExisting(string chatId)
160+
public async Task<ChatContext> FromExisting(string? chatId)
161161
{
162162
var existingChat = await _chatService.GetById(chatId);
163163
if (existingChat == null)

0 commit comments

Comments
 (0)