-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathAgentService.UpdateAgent.cs
More file actions
110 lines (93 loc) · 3.63 KB
/
AgentService.UpdateAgent.cs
File metadata and controls
110 lines (93 loc) · 3.63 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
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task UpdateAgent(Agent agent, AgentField updateField)
{
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
var userService = _services.GetRequiredService<IUserService>();
var auth = await userService.GetUserAuthorizations([agent.Id]);
var allowEdit = auth.IsAgentActionAllowed(agent.Id, UserAction.Edit);
if (!allowEdit)
{
return;
}
var record = await _db.GetAgent(agent.Id);
if (record == null) return;
record.Name = agent.Name ?? string.Empty;
record.Description = agent.Description ?? string.Empty;
record.IsPublic = agent.IsPublic;
record.Disabled = agent.Disabled;
record.MergeUtility = agent.MergeUtility;
record.MaxMessageCount = agent.MaxMessageCount;
record.Type = agent.Type;
record.Mode = agent.Mode;
record.FuncVisMode = agent.FuncVisMode;
record.Profiles = agent.Profiles ?? [];
record.Labels = agent.Labels ?? [];
record.RoutingRules = agent.RoutingRules ?? [];
record.Instruction = agent.Instruction ?? string.Empty;
record.ChannelInstructions = agent.ChannelInstructions ?? [];
record.Functions = agent.Functions ?? [];
record.Templates = agent.Templates ?? [];
record.Responses = agent.Responses ?? [];
record.Samples = agent.Samples ?? [];
record.Utilities = agent.Utilities ?? [];
record.McpTools = agent.McpTools ?? [];
record.Skills = agent.Skills ?? [];
record.KnowledgeBases = agent.KnowledgeBases ?? [];
record.Rules = agent.Rules ?? [];
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
{
record.LlmConfig = agent.LlmConfig;
}
await _db.UpdateAgent(record, updateField);
Utilities.ClearCache();
await Task.CompletedTask;
}
public async Task<string> PatchAgentTemplate(Agent agent)
{
var patchResult = string.Empty;
if (agent == null || agent.Templates.IsNullOrEmpty())
{
patchResult = $"Null agent instance or empty input templates";
_logger.LogWarning(patchResult);
return patchResult;
}
var record = await _db.GetAgent(agent.Id);
if (record == null)
{
patchResult = $"Cannot find agent {agent.Id}";
_logger.LogWarning(patchResult);
return patchResult;
}
var successTemplates = new List<string>();
var failTemplates = new List<string>();
foreach (var template in agent.Templates)
{
if (template == null) continue;
var result = await _db.PatchAgentTemplate(agent.Id, template);
if (result)
{
successTemplates.Add(template.Name);
_logger.LogInformation($"Template {template.Name} is updated successfully!");
}
else
{
failTemplates.Add(template.Name);
_logger.LogWarning($"Template {template.Name} is failed to be updated!");
}
}
Utilities.ClearCache();
if (!successTemplates.IsNullOrEmpty())
{
patchResult += $"Success templates:\n{string.Join('\n', successTemplates)}\n\n";
}
if (!failTemplates.IsNullOrEmpty())
{
patchResult += $"Failed templates:\n{string.Join('\n', failTemplates)}";
}
return patchResult;
}
}