Skip to content

Commit a08010a

Browse files
authored
Merge pull request #517 from SciSharp/aspire
Aspire support
2 parents 7d31df3 + 164f69b commit a08010a

File tree

14 files changed

+288
-17
lines changed

14 files changed

+288
-17
lines changed

BotSharp.sln

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Dashboard",
8585
EndProject
8686
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.SparkDesk", "src\Plugins\BotSharp.Plugin.SparkDesk\BotSharp.Plugin.SparkDesk.csproj", "{289E25C8-63F1-4D52-9909-207724DB40CB}"
8787
EndProject
88-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.MetaGLM", "src\Plugins\BotSharp.Plugin.MetaGLM\BotSharp.Plugin.MetaGLM.csproj", "{CCF745F2-0C95-4ED0-983B-507C528B39EA}"
88+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MetaGLM", "src\Plugins\BotSharp.Plugin.MetaGLM\BotSharp.Plugin.MetaGLM.csproj", "{CCF745F2-0C95-4ED0-983B-507C528B39EA}"
8989
EndProject
9090
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AnthropicAI", "src\Plugins\BotSharp.Plugin.AnthropicAI\BotSharp.Plugin.AnthropicAI.csproj", "{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}"
9191
EndProject
92+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.ServiceDefaults", "src\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj", "{6406DC61-0F30-42E8-A1DB-B38CDF454273}"
93+
EndProject
94+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.AppHost", "src\BotSharp.AppHost\BotSharp.AppHost.csproj", "{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}"
95+
EndProject
9296
Global
9397
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9498
Debug|Any CPU = Debug|Any CPU
@@ -353,6 +357,22 @@ Global
353357
{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}.Release|Any CPU.Build.0 = Release|Any CPU
354358
{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}.Release|x64.ActiveCfg = Release|Any CPU
355359
{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}.Release|x64.Build.0 = Release|Any CPU
360+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
361+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|Any CPU.Build.0 = Debug|Any CPU
362+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|x64.ActiveCfg = Debug|Any CPU
363+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|x64.Build.0 = Debug|Any CPU
364+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|Any CPU.ActiveCfg = Release|Any CPU
365+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|Any CPU.Build.0 = Release|Any CPU
366+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|x64.ActiveCfg = Release|Any CPU
367+
{6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|x64.Build.0 = Release|Any CPU
368+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
369+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
370+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|x64.ActiveCfg = Debug|Any CPU
371+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|x64.Build.0 = Debug|Any CPU
372+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
373+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|Any CPU.Build.0 = Release|Any CPU
374+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|x64.ActiveCfg = Release|Any CPU
375+
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|x64.Build.0 = Release|Any CPU
356376
EndGlobalSection
357377
GlobalSection(SolutionProperties) = preSolution
358378
HideSolutionNode = FALSE
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsAspireHost>true</IsAspireHost>
9+
<UserSecretsId>e4bd9862-7edd-45f0-9a89-b8e3b3339c71</UserSecretsId>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.0.0" />
14+
<PackageReference Include="Aspire.Hosting.NodeJs" Version="8.0.1" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj" />
19+
<ProjectReference Include="..\WebStarter\WebStarter.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

src/BotSharp.AppHost/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var builder = DistributedApplication.CreateBuilder(args);
2+
3+
var apiService = builder.AddProject<Projects.WebStarter>("apiservice")
4+
.WithExternalHttpEndpoints();
5+
6+
builder.AddNpmApp("BotSharpUI", "../../../BotSharp-UI")
7+
.WithReference(apiService)
8+
.WithHttpEndpoint(env: "PORT")
9+
.WithExternalHttpEndpoints()
10+
.PublishAsDockerFile();
11+
12+
builder.Build().Run();
13+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:17248;http://localhost:15140",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"DOTNET_ENVIRONMENT": "Development",
12+
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21247",
13+
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22140"
14+
}
15+
},
16+
"http": {
17+
"commandName": "Project",
18+
"dotnetRunMessages": true,
19+
"launchBrowser": true,
20+
"applicationUrl": "http://localhost:15140",
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development",
23+
"DOTNET_ENVIRONMENT": "Development",
24+
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19185",
25+
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20069"
26+
}
27+
}
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsAspireSharedProject>true</IsAspireSharedProject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
13+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
14+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
15+
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="3.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.3.0" />
17+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0" />
18+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
19+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
20+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
21+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
22+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.ServiceDiscovery;
7+
using OpenTelemetry;
8+
using OpenTelemetry.Logs;
9+
using OpenTelemetry.Metrics;
10+
using OpenTelemetry.Trace;
11+
using Serilog;
12+
13+
namespace Microsoft.Extensions.Hosting
14+
{
15+
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
16+
// This project should be referenced by each service project in your solution.
17+
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
18+
public static class Extensions
19+
{
20+
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
21+
{
22+
builder.ConfigureOpenTelemetry();
23+
24+
builder.AddDefaultHealthChecks();
25+
26+
builder.Services.AddServiceDiscovery();
27+
28+
builder.Services.ConfigureHttpClientDefaults(http =>
29+
{
30+
// Turn on resilience by default
31+
http.AddStandardResilienceHandler();
32+
33+
// Turn on service discovery by default
34+
http.AddServiceDiscovery();
35+
});
36+
37+
// Uncomment the following to restrict the allowed schemes for service discovery.
38+
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
39+
// {
40+
// options.AllowedSchemes = ["https"];
41+
// });
42+
43+
return builder;
44+
}
45+
46+
public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
47+
{
48+
builder.Logging.AddOpenTelemetry(logging =>
49+
{ // Use Serilog
50+
Log.Logger = new LoggerConfiguration()
51+
// 对请求路径为 / heathz和 / metrics不进行日志记录
52+
.Filter.ByExcluding(
53+
e => e.Properties.TryGetValue("RequestPath", out var value) && (value.ToString().StartsWith("\"/metrics\"") || value.ToString().StartsWith("\"/healthz\""))
54+
)
55+
#if DEBUG
56+
.MinimumLevel.Information()
57+
#else
58+
.MinimumLevel.Warning()
59+
#endif
60+
.WriteTo.Console()
61+
.WriteTo.File("logs/log-.txt",
62+
shared: true,
63+
rollingInterval: RollingInterval.Day)
64+
//.WriteTo.OpenTelemetry(options =>
65+
//{
66+
// options.Endpoint = builder.Configuration["OpenTelemetry:Endpoint"];
67+
// options.ResourceAttributes = new Dictionary<string, object>
68+
// {
69+
// ["service.name"] = builder.Configuration["OpenTelemetry:ServiceName"],
70+
// ["index"] = 10,
71+
// ["flag"] = true,
72+
// ["value"] = 3.14
73+
// };
74+
//})
75+
.CreateLogger();
76+
77+
logging.IncludeFormattedMessage = true;
78+
logging.IncludeScopes = true;
79+
});
80+
81+
builder.Services.AddOpenTelemetry()
82+
.WithMetrics(metrics =>
83+
{
84+
metrics.AddAspNetCoreInstrumentation()
85+
.AddHttpClientInstrumentation()
86+
.AddRuntimeInstrumentation();
87+
})
88+
.WithTracing(tracing =>
89+
{
90+
tracing.AddAspNetCoreInstrumentation()
91+
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
92+
//.AddGrpcClientInstrumentation()
93+
.AddHttpClientInstrumentation();
94+
});
95+
96+
builder.AddOpenTelemetryExporters();
97+
98+
return builder;
99+
}
100+
101+
private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
102+
{
103+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
104+
105+
if (useOtlpExporter)
106+
{
107+
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
108+
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
109+
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
110+
111+
}
112+
113+
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
114+
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
115+
//{
116+
// builder.Services.AddOpenTelemetry()
117+
// .UseAzureMonitor();
118+
//}
119+
120+
return builder;
121+
}
122+
123+
public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
124+
{
125+
builder.Services.AddHealthChecks()
126+
// Add a default liveness check to ensure app is responsive
127+
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
128+
129+
return builder;
130+
}
131+
132+
public static WebApplication MapDefaultEndpoints(this WebApplication app)
133+
{
134+
// Adding health checks endpoints to applications in non-development environments has security implications.
135+
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
136+
if (app.Environment.IsDevelopment())
137+
{
138+
// All health checks must pass for app to be considered ready to accept traffic after starting
139+
app.MapHealthChecks("/health");
140+
141+
// Only health checks tagged with the "live" tag must pass for app to be considered alive
142+
app.MapHealthChecks("/alive", new HealthCheckOptions
143+
{
144+
Predicate = r => r.Tags.Contains("live")
145+
});
146+
}
147+
148+
return app;
149+
}
150+
}
151+
}

src/Infrastructure/BotSharp.Core/Routing/Planning/NaivePlanner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ private void FixMalformedResponse(FunctionCallFromLlm args)
169169
if (args.Function == "route_to_agent")
170170
{
171171
// Action agent name
172-
if (!agents.Any(x => x.Name == args.AgentName))
172+
if (!agents.Any(x => x.Name == args.AgentName) && !string.IsNullOrEmpty(args.AgentName))
173173
{
174174
args.AgentName = agents.FirstOrDefault(x => args.AgentName.Contains(x.Name))?.Name ?? args.AgentName;
175175
}
176176

177177
// Goal agent name
178-
if (!agents.Any(x => x.Name == args.OriginalAgent))
178+
if (!agents.Any(x => x.Name == args.OriginalAgent) && !string.IsNullOrEmpty(args.OriginalAgent))
179179
{
180180
args.OriginalAgent = agents.FirstOrDefault(x => args.OriginalAgent.Contains(x.Name))?.Name ?? args.OriginalAgent;
181181
}

src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Aspire.MongoDB.Driver" Version="8.0.1" />
1415
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
1516
</ItemGroup>
1617

0 commit comments

Comments
 (0)