Skip to content

Commit 6ced7a0

Browse files
authored
Merge pull request #149 from iceljc/features/update-agent-structure
Features/update agent structure
2 parents 3c0443a + 114bae3 commit 6ced7a0

File tree

17 files changed

+340
-28
lines changed

17 files changed

+340
-28
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ public enum AgentField
66
Name,
77
Description,
88
IsPublic,
9+
Disabled,
10+
AllowRouting,
11+
Profiles,
12+
RoutingRules,
913
Instruction,
1014
Function,
1115
Template,

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Agent
66
{
77
public string Id { get; set; } = string.Empty;
88
public string Name { get; set; } = string.Empty;
9-
public string Description { get; set; }
9+
public string Description { get; set; } = string.Empty;
1010
public DateTime CreatedDateTime { get; set; }
1111
public DateTime UpdatedDateTime { get; set; }
1212

@@ -81,6 +81,10 @@ public static Agent Clone(Agent agent)
8181
Samples = agent.Samples,
8282
Knowledges = agent.Knowledges,
8383
IsPublic = agent.IsPublic,
84+
Disabled = agent.Disabled,
85+
AllowRouting = agent.AllowRouting,
86+
Profiles = agent.Profiles,
87+
RoutingRules = agent.RoutingRules,
8488
CreatedDateTime = agent.CreatedDateTime,
8589
UpdatedDateTime = agent.UpdatedDateTime,
8690
};
@@ -94,7 +98,7 @@ public Agent SetInstruction(string instruction)
9498

9599
public Agent SetTemplates(List<AgentTemplate> templates)
96100
{
97-
Templates = templates;
101+
Templates = templates ?? new List<AgentTemplate>();
98102
return this;
99103
}
100104

@@ -133,4 +137,28 @@ public Agent SetIsPublic(bool isPublic)
133137
IsPublic = isPublic;
134138
return this;
135139
}
140+
141+
public Agent SetDisabled(bool disabled)
142+
{
143+
Disabled = disabled;
144+
return this;
145+
}
146+
147+
public Agent SetAllowRouting(bool allowRouting)
148+
{
149+
AllowRouting = allowRouting;
150+
return this;
151+
}
152+
153+
public Agent SetProfiles(List<string> profiles)
154+
{
155+
Profiles = profiles ?? new List<string>();
156+
return this;
157+
}
158+
159+
public Agent SetRoutingRules(List<RoutingRule> rules)
160+
{
161+
RoutingRules = rules ?? new List<RoutingRule>();
162+
return this;
163+
}
136164
}

src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public override string ToString()
1818
{
1919
return $"{AgentName} {Field}";
2020
}
21+
22+
public RoutingRule()
23+
{
24+
25+
}
2126
}

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ join u in _db.Users on ua.UserId equals u.Id
3535
.SetName(foundAgent.Name)
3636
.SetDescription(foundAgent.Description)
3737
.SetIsPublic(foundAgent.IsPublic)
38+
.SetDisabled(foundAgent.Disabled)
39+
.SetAllowRouting(foundAgent.AllowRouting)
40+
.SetProfiles(foundAgent.Profiles)
41+
.SetRoutingRules(foundAgent.RoutingRules)
3842
.SetInstruction(foundAgent.Instruction)
3943
.SetTemplates(foundAgent.Templates)
4044
.SetFunctions(foundAgent.Functions)

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Repositories;
3+
using BotSharp.Abstraction.Routing.Models;
34
using System.IO;
45

56
namespace BotSharp.Core.Agents.Services;
@@ -15,6 +16,11 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
1516

1617
record.Name = agent.Name ?? string.Empty;
1718
record.Description = agent.Description ?? string.Empty;
19+
record.IsPublic = agent.IsPublic;
20+
record.Disabled = agent.Disabled;
21+
record.AllowRouting = agent.AllowRouting;
22+
record.Profiles = agent.Profiles ?? new List<string>();
23+
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
1824
record.Instruction = agent.Instruction ?? string.Empty;
1925
record.Functions = agent.Functions ?? new List<string>();
2026
record.Templates = agent.Templates ?? new List<AgentTemplate>();
@@ -53,6 +59,10 @@ public async Task UpdateAgentFromFile(string id)
5359
.SetName(foundAgent.Name)
5460
.SetDescription(foundAgent.Description)
5561
.SetIsPublic(foundAgent.IsPublic)
62+
.SetDisabled(foundAgent.Disabled)
63+
.SetAllowRouting(foundAgent.AllowRouting)
64+
.SetProfiles(foundAgent.Profiles)
65+
.SetRoutingRules(foundAgent.RoutingRules)
5666
.SetInstruction(foundAgent.Instruction)
5767
.SetTemplates(foundAgent.Templates)
5868
.SetFunctions(foundAgent.Functions)

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using BotSharp.Abstraction.Users.Models;
55
using BotSharp.Abstraction.Agents.Models;
66
using MongoDB.Driver;
7+
using BotSharp.Abstraction.Routing.Models;
78

89
namespace BotSharp.Core.Repository;
910

@@ -240,6 +241,18 @@ public void UpdateAgent(Agent agent, AgentField field)
240241
case AgentField.IsPublic:
241242
UpdateAgentIsPublic(agent.Id, agent.IsPublic);
242243
break;
244+
case AgentField.Disabled:
245+
UpdateAgentDisabled(agent.Id, agent.Disabled);
246+
break;
247+
case AgentField.AllowRouting:
248+
UpdateAgentAllowRouting(agent.Id, agent.AllowRouting);
249+
break;
250+
case AgentField.Profiles:
251+
UpdateAgentProfiles(agent.Id, agent.Profiles);
252+
break;
253+
case AgentField.RoutingRules:
254+
UpdateAgentRoutingRules(agent.Id, agent.RoutingRules);
255+
break;
243256
case AgentField.Instruction:
244257
UpdateAgentInstruction(agent.Id, agent.Instruction);
245258
break;
@@ -298,6 +311,54 @@ private void UpdateAgentIsPublic(string agentId, bool isPublic)
298311
File.WriteAllText(agentFile, json);
299312
}
300313

314+
private void UpdateAgentDisabled(string agentId, bool disabled)
315+
{
316+
var (agent, agentFile) = GetAgentFromFile(agentId);
317+
if (agent == null) return;
318+
319+
agent.Disabled = disabled;
320+
agent.UpdatedDateTime = DateTime.UtcNow;
321+
var json = JsonSerializer.Serialize(agent, _options);
322+
File.WriteAllText(agentFile, json);
323+
}
324+
325+
private void UpdateAgentAllowRouting(string agentId, bool allowRouting)
326+
{
327+
var (agent, agentFile) = GetAgentFromFile(agentId);
328+
if (agent == null) return;
329+
330+
agent.AllowRouting = allowRouting;
331+
agent.UpdatedDateTime = DateTime.UtcNow;
332+
var json = JsonSerializer.Serialize(agent, _options);
333+
File.WriteAllText(agentFile, json);
334+
}
335+
336+
private void UpdateAgentProfiles(string agentId, List<string> profiles)
337+
{
338+
if (profiles.IsNullOrEmpty()) return;
339+
340+
var (agent, agentFile) = GetAgentFromFile(agentId);
341+
if (agent == null) return;
342+
343+
agent.Profiles = profiles;
344+
agent.UpdatedDateTime = DateTime.UtcNow;
345+
var json = JsonSerializer.Serialize(agent, _options);
346+
File.WriteAllText(agentFile, json);
347+
}
348+
349+
private void UpdateAgentRoutingRules(string agentId, List<RoutingRule> rules)
350+
{
351+
if (rules.IsNullOrEmpty()) return;
352+
353+
var (agent, agentFile) = GetAgentFromFile(agentId);
354+
if (agent == null) return;
355+
356+
agent.RoutingRules = rules;
357+
agent.UpdatedDateTime = DateTime.UtcNow;
358+
var json = JsonSerializer.Serialize(agent, _options);
359+
File.WriteAllText(agentFile, json);
360+
}
361+
301362
private void UpdateAgentInstruction(string agentId, string instruction)
302363
{
303364
if (string.IsNullOrEmpty(instruction)) return;
@@ -396,6 +457,10 @@ private void UpdateAgentAllFields(Agent inputAgent)
396457
agent.Name = inputAgent.Name;
397458
agent.Description = inputAgent.Description;
398459
agent.IsPublic = inputAgent.IsPublic;
460+
agent.Disabled = inputAgent.Disabled;
461+
agent.AllowRouting = inputAgent.AllowRouting;
462+
agent.Profiles = inputAgent.Profiles;
463+
agent.RoutingRules = inputAgent.RoutingRules;
399464
agent.UpdatedDateTime = DateTime.UtcNow;
400465
var json = JsonSerializer.Serialize(agent, _options);
401466
File.WriteAllText(agentFile, json);

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ public async Task UpdateAgentIsPublic([FromRoute] string agentId, [FromBody] Age
6666
await _agentService.UpdateAgent(model, AgentField.IsPublic);
6767
}
6868

69+
[HttpPut("/agent/{agentId}/disabled")]
70+
public async Task UpdateAgentDisabled([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
71+
{
72+
var model = agent.ToAgent();
73+
model.Id = agentId;
74+
await _agentService.UpdateAgent(model, AgentField.Disabled);
75+
}
76+
77+
[HttpPut("/agent/{agentId}/allow-routing")]
78+
public async Task UpdateAgentAllowRouting([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
79+
{
80+
var model = agent.ToAgent();
81+
model.Id = agentId;
82+
await _agentService.UpdateAgent(model, AgentField.AllowRouting);
83+
}
84+
85+
[HttpPut("/agent/{agentId}/profiles")]
86+
public async Task UpdateAgentProfiles([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
87+
{
88+
var model = agent.ToAgent();
89+
model.Id = agentId;
90+
await _agentService.UpdateAgent(model, AgentField.Profiles);
91+
}
92+
93+
[HttpPut("/agent/{agentId}/routing-rules")]
94+
public async Task UpdateAgentRoutingRules([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
95+
{
96+
var model = agent.ToAgent();
97+
model.Id = agentId;
98+
await _agentService.UpdateAgent(model, AgentField.RoutingRules);
99+
}
100+
69101
[HttpPut("/agent/{agentId}/instruction")]
70102
public async Task UpdateAgentInstruction([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
71103
{
@@ -83,7 +115,7 @@ public async Task UpdateAgentFunctions([FromRoute] string agentId, [FromBody] Ag
83115
}
84116

85117
[HttpPut("/agent/{agentId}/templates")]
86-
public async Task UpdateAgenttemplates([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
118+
public async Task UpdateAgentTemplates([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
87119
{
88120
var model = agent.ToAgent();
89121
model.Id = agentId;

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Abstraction.Routing.Models;
23

34
namespace BotSharp.OpenAPI.ViewModels.Agents;
45

@@ -11,6 +12,10 @@ public class AgentCreationModel
1112
public List<string> Functions { get; set; }
1213
public List<AgentResponse> Responses { get; set; }
1314
public bool IsPublic { get; set; }
15+
public bool AllowRouting { get; set; }
16+
public bool Disabled { get; set; }
17+
public List<string> Profiles { get; set; }
18+
public List<RoutingRuleUpdateModel> RoutingRules { get; set; }
1419

1520
public Agent ToAgent()
1621
{
@@ -22,7 +27,13 @@ public Agent ToAgent()
2227
Templates = Templates,
2328
Functions = Functions,
2429
Responses = Responses,
25-
IsPublic = IsPublic
30+
IsPublic = IsPublic,
31+
AllowRouting = AllowRouting,
32+
Disabled = Disabled,
33+
Profiles = Profiles,
34+
RoutingRules = RoutingRules?
35+
.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?
36+
.ToList() ?? new List<RoutingRule>()
2637
};
2738
}
2839
}

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Abstraction.Routing.Models;
23

34
namespace BotSharp.OpenAPI.ViewModels.Agents;
45

@@ -32,12 +33,32 @@ public class AgentUpdateModel
3233
/// </summary>
3334
public List<AgentResponse>? Responses { get; set; }
3435

36+
public bool IsPublic { get; set; }
37+
38+
public bool AllowRouting { get; set; }
39+
40+
public bool Disabled { get; set; }
41+
42+
/// <summary>
43+
/// Profile by channel
44+
/// </summary>
45+
public List<string>? Profiles { get; set; }
46+
47+
public List<RoutingRuleUpdateModel>? RoutingRules { get; set; }
48+
3549
public Agent ToAgent()
3650
{
3751
var agent = new Agent()
3852
{
3953
Name = Name ?? string.Empty,
4054
Description = Description ?? string.Empty,
55+
IsPublic = IsPublic,
56+
Disabled = Disabled,
57+
AllowRouting = AllowRouting,
58+
Profiles = Profiles ?? new List<string>(),
59+
RoutingRules = RoutingRules?
60+
.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?
61+
.ToList() ?? new List<RoutingRule>(),
4162
Instruction = Instruction ?? string.Empty,
4263
Templates = Templates ?? new List<AgentTemplate>(),
4364
Functions = Functions ?? new List<string>(),

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Abstraction.Routing.Models;
23

34
namespace BotSharp.OpenAPI.ViewModels.Agents;
45

@@ -12,6 +13,11 @@ public class AgentViewModel
1213
public List<string> Functions { get; set; }
1314
public List<AgentResponse> Responses { get; set; }
1415
public bool IsPublic { get; set; }
16+
public bool AllowRouting { get; set; }
17+
public bool Disabled { get; set; }
18+
public List<string> Profiles { get; set; }
19+
public List<RoutingRule> RoutingRules { get; set; }
20+
1521
public DateTime CreatedDateTime { get; set; }
1622
public DateTime UpdatedDateTime { get; set; }
1723

@@ -27,6 +33,10 @@ public static AgentViewModel FromAgent(Agent agent)
2733
Functions = agent.Functions,
2834
Responses = agent.Responses,
2935
IsPublic= agent.IsPublic,
36+
Disabled = agent.Disabled,
37+
AllowRouting = agent.AllowRouting,
38+
Profiles = agent.Profiles,
39+
RoutingRules = agent.RoutingRules,
3040
CreatedDateTime = agent.CreatedDateTime,
3141
UpdatedDateTime = agent.UpdatedDateTime
3242
};

0 commit comments

Comments
 (0)