-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAgentService.cs
More file actions
170 lines (144 loc) · 5.81 KB
/
AgentService.cs
File metadata and controls
170 lines (144 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using MaIN.Domain.Configuration;
using MaIN.Domain.Entities;
using MaIN.Domain.Entities.Agents;
using MaIN.Domain.Exceptions;
using MaIN.Infrastructure.Repositories.Abstract;
using MaIN.Services.Constants;
using MaIN.Services.Mappers;
using MaIN.Services.Services.Abstract;
using MaIN.Services.Services.ImageGenServices;
using MaIN.Services.Services.LLMService.Factory;
using MaIN.Services.Services.Models.Commands;
using MaIN.Services.Services.Steps.Commands;
using MaIN.Services.Utils;
using Microsoft.Extensions.Logging;
namespace MaIN.Services.Services;
public class AgentService(
IAgentRepository agentRepository,
IChatRepository chatRepository,
ILogger<AgentService> logger,
INotificationService notificationService,
IStepProcessor stepProcessor,
ICommandDispatcher commandDispatcher,
ILLMServiceFactory llmServiceFactory,
MaINSettings maInSettings)
: IAgentService
{
public async Task<Chat> Process(Chat chat, string agentId, bool translatePrompt = false)
{
var agent = await agentRepository.GetAgentById(agentId);
if (agent == null)
{
throw new AgentNotFoundException(agentId);
}
if (agent.Context == null)
{
throw new AgentContextNotFoundException(agentId);
}
await notificationService.DispatchNotification(
NotificationMessageBuilder.ProcessingStarted(agentId, agent.CurrentBehaviour), "ReceiveAgentUpdate");
try
{
chat = await stepProcessor.ProcessSteps(
agent.Context,
agent,
chat,
async (status, id, progress, behaviour) =>
{
await notificationService.DispatchNotification(
NotificationMessageBuilder.CreateActorProgress(id, status, progress, behaviour), "ReceiveAgentUpdate"); //TODO prepare static lookup for magic string :)
},
async c => await chatRepository.UpdateChat(c.Id, c.ToDocument()),
logger
);
await agentRepository.UpdateAgent(agent.Id, agent);
await notificationService.DispatchNotification(
NotificationMessageBuilder.ProcessingComplete(agentId, agent.CurrentBehaviour), "ReceiveAgentUpdate");
return chat;
}
catch (Exception)
{
await notificationService.DispatchNotification(
NotificationMessageBuilder.ProcessingFailed(agentId, agent.CurrentBehaviour), "ReceiveAgentUpdate");
throw;
}
}
public async Task<Agent> CreateAgent(Agent agent, bool flow = false, bool interactiveResponse = false,
InferenceParams? inferenceParams = null, MemoryParams? memoryParams = null, bool disableCache = false)
{
var chat = new Chat
{
Id = Guid.NewGuid().ToString(),
Model = agent.Model,
Name = agent.Name,
Visual = agent.Model == ImageGenService.LocalImageModels.FLUX,
InterferenceParams = inferenceParams ?? new InferenceParams(),
MemoryParams = memoryParams ?? new MemoryParams(),
Messages = new List<Message>(),
Interactive = interactiveResponse,
Backend = agent.Backend,
Type = flow ? ChatType.Flow : ChatType.Rag,
};
if (disableCache)
{
chat.Properties.AddProperty(ServiceConstants.Properties.DisableCacheProperty);
}
var startCommand = new StartCommand
{
Chat = chat,
InitialPrompt = agent.Context.Instruction
};
await commandDispatcher.DispatchAsync(startCommand);
agent.Started = true;
agent.Flow = flow;
agent.Behaviours ??= new Dictionary<string, string>();
agent.Behaviours.Add("Default", agent.Context.Instruction!);
agent.CurrentBehaviour = "Default";
var agentDocument = agent.ToDocument();
agentDocument.ChatId = chat.Id;
await chatRepository.AddChat(chat.ToDocument());
await agentRepository.AddAgent(agentDocument);
return agent;
}
public async Task<Chat> GetChatByAgent(string agentId)
{
var agent = await agentRepository.GetAgentById(agentId);
if (agent == null)
{
throw new AgentNotFoundException(agentId);
}
var chat = await chatRepository.GetChatById(agent.ChatId);
return chat!.ToDomain();
}
public async Task<Chat> Restart(string agentId)
{
var agent = await agentRepository.GetAgentById(agentId);
if (agent == null)
{
throw new AgentNotFoundException(agentId);
}
var chat = (await chatRepository.GetChatById(agent.ChatId))!.ToDomain();
var llmService = llmServiceFactory.CreateService(agent.Backend ?? maInSettings.BackendType);
await llmService.CleanSessionCache(chat.Id!);
AgentStateManager.ClearState(agent, chat);
await chatRepository.UpdateChat(chat.Id!, chat.ToDocument());
await agentRepository.UpdateAgent(agent.Id, agent);
return chat;
}
public async Task<List<Agent>> GetAgents() =>
(await agentRepository.GetAllAgents())
.Select(x => x.ToDomain())
.ToList();
public async Task<Agent?> GetAgentById(string id) =>
(await agentRepository.GetAgentById(id))?.ToDomain();
public async Task DeleteAgent(string id)
{
var chat = await GetChatByAgent(id);
var llmService = llmServiceFactory.CreateService(chat.Backend ?? maInSettings.BackendType);
await llmService.CleanSessionCache(chat.Id);
await chatRepository.DeleteChat(chat.Id);
await agentRepository.DeleteAgent(id);
}
public Task<bool> AgentExists(string id) =>
agentRepository.Exists(id);
}