Skip to content

Commit ac72106

Browse files
committed
clean code
1 parent 85ebbff commit ac72106

File tree

9 files changed

+27
-18
lines changed

9 files changed

+27
-18
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ public string RenderedInstruction(Agent agent)
1212
var conv = _services.GetRequiredService<IConversationService>();
1313

1414
// merge instructions
15-
var texts = new List<string> { agent.Instruction };
16-
texts.AddRange(agent.SecondaryInstructions ?? []);
15+
var instructions = new List<string> { agent.Instruction };
16+
var secondaryInstructions = agent.SecondaryInstructions?.Where(x => !string.IsNullOrWhiteSpace(x)).ToList() ?? [];
17+
instructions.AddRange(secondaryInstructions);
1718

1819
// update states
1920
foreach (var t in conv.States.GetStates())
2021
{
2122
agent.TemplateDict[t.Key] = t.Value;
2223
}
2324

24-
var res = render.Render(string.Join("\r\n", texts), agent.TemplateDict);
25+
var res = render.Render(string.Join("\r\n", instructions), agent.TemplateDict);
2526
return res;
2627
}
2728

src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogM
101101

102102
var agentService = _services.GetRequiredService<IAgentService>();
103103

104-
if (!string.IsNullOrEmpty(agent.Instruction))
104+
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
105105
{
106106
instruction += agentService.RenderedInstruction(agent);
107107
}
@@ -197,7 +197,8 @@ public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogM
197197
ReferenceHandler = ReferenceHandler.IgnoreCycles,
198198
};
199199

200-
foreach (var fn in agent.Functions)
200+
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
201+
foreach (var fn in functions)
201202
{
202203
/*var inputschema = new InputSchema()
203204
{

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
230230
MaxOutputTokenCount = maxTokens
231231
};
232232

233-
foreach (var function in agent.Functions)
233+
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
234+
foreach (var function in functions)
234235
{
235236
if (!agentService.RenderFunction(agent, function)) continue;
236237

@@ -242,7 +243,7 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
242243
functionParameters: BinaryData.FromObjectAsJson(property)));
243244
}
244245

245-
if (!string.IsNullOrEmpty(agent.Instruction))
246+
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
246247
{
247248
var instruction = agentService.RenderedInstruction(agent);
248249
messages.Add(new SystemChatMessage(instruction));

src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void SetModelName(string model)
107107
var funcDeclarations = new List<FunctionDeclaration>();
108108

109109
var systemPrompts = new List<string>();
110-
if (!string.IsNullOrEmpty(agent.Instruction))
110+
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
111111
{
112112
var instruction = agentService.RenderedInstruction(agent);
113113
contents.Add(new Content(instruction)
@@ -119,7 +119,8 @@ public void SetModelName(string model)
119119
}
120120

121121
var funcPrompts = new List<string>();
122-
foreach (var function in agent.Functions)
122+
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
123+
foreach (var function in functions)
123124
{
124125
if (!agentService.RenderFunction(agent, function)) continue;
125126

src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
9999

100100
var agentService = _services.GetRequiredService<IAgentService>();
101101

102-
if (!string.IsNullOrEmpty(agent.Instruction))
102+
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
103103
{
104104
prompt += agentService.RenderedInstruction(agent);
105105
}
@@ -110,10 +110,11 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
110110
var messages = conversations.Select(c => new PalmChatMessage(c.Content, c.Role == AgentRole.User ? "user" : "AI"))
111111
.ToList();
112112

113-
if (agent.Functions != null && agent.Functions.Count > 0)
113+
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
114+
if (!functions.IsNullOrEmpty())
114115
{
115116
prompt += "\r\n\r\n[Functions] defined in JSON Schema:\r\n";
116-
prompt += JsonSerializer.Serialize(agent.Functions, new JsonSerializerOptions
117+
prompt += JsonSerializer.Serialize(functions, new JsonSerializerOptions
117118
{
118119
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
119120
WriteIndented = true

src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Utilities;
2+
13
namespace BotSharp.Plugin.MetaGLM.Providers;
24

35
public class ChatCompletionProvider : IChatCompletion
@@ -86,7 +88,7 @@ private string PrepareOptions(Agent agent, List<RoleDialogModel> conversations,
8688
List<MessageItem> messages = new List<MessageItem>();
8789
List<FunctionTool> toolcalls = new List<FunctionTool>();
8890

89-
if (!string.IsNullOrEmpty(agent.Instruction))
91+
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
9092
{
9193
var instruction = agentService.RenderedInstruction(agent);
9294
messages.Add(new MessageItem("system", instruction));
@@ -105,7 +107,8 @@ private string PrepareOptions(Agent agent, List<RoleDialogModel> conversations,
105107
new MessageItem("assistant", message.Content));
106108
}
107109

108-
foreach (var function in agent.Functions)
110+
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
111+
foreach (var function in functions)
109112
{
110113
var functionTool = ConvertToFunctionTool(function);
111114
toolcalls.Add(functionTool);

src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
6666

6767
if (_services.GetService<IAgentService>() is { } agentService)
6868
{
69-
foreach (var function in agent.Functions)
69+
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
70+
foreach (var function in functions)
7071
{
7172
if (agentService.RenderFunction(agent, function))
7273
{

src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
210210
MaxOutputTokenCount = maxTokens
211211
};
212212

213-
214213
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
215214
foreach (var function in functions)
216215
{

src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void SetModelName(string model)
176176
var agentService = _services.GetRequiredService<IAgentService>();
177177
var messages = new List<ChatMessage>();
178178

179-
if (!string.IsNullOrEmpty(agent.Instruction))
179+
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
180180
{
181181
var instruction = agentService.RenderedInstruction(agent);
182182
messages.Add(ChatMessage.FromSystem(instruction));
@@ -193,7 +193,8 @@ public void SetModelName(string model)
193193
ChatMessage.FromAssistant(message.Content));
194194
}
195195

196-
foreach (var function in agent.Functions)
196+
var agentFuncs = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
197+
foreach (var function in agentFuncs)
197198
{
198199
functions.Add(ConvertToFunctionDef(function));
199200
}

0 commit comments

Comments
 (0)