chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Aspire.AppHost.Sdk" Version="13.0.0" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>2d10c3b5-399d-40cc-bbf3-143be681db63</UserSecretsId>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ChatWithAgent.ApiService\ChatWithAgent.ApiService.csproj" />
<ProjectReference Include="..\ChatWithAgent.Configuration\ChatWithAgent.Configuration.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\ChatWithAgent.Web\ChatWithAgent.Web.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
<PackageReference Include="Aspire.Hosting.Azure.CognitiveServices" />
<PackageReference Include="Aspire.Hosting.Azure.Search" />
<PackageReference Include="MessagePack" /> <!-- Override vulnerable transitive dependency (2.5.192) from Aspire packages -->
</ItemGroup>
</Project>
@@ -0,0 +1,92 @@
// Copyright (c) Microsoft. All rights reserved.
using ChatWithAgent.Configuration;
namespace ChatWithAgent.AppHost.Extensions;
/// <summary>
/// Resource builder extensions.
/// </summary>
public static class ResourceBuilderExtensions
{
/// <summary>
/// Adds host configuration as environment variables to the resource.
/// </summary>
/// <typeparam name="T">The resource type.</typeparam>
/// <param name="builder">The resource builder.</param>
/// <param name="config">The host configuration.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> builder, HostConfig config) where T : IResourceWithEnvironment
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(config);
// Add AI chat service configuration to the environment variables so that Api Service can access it.
builder.WithEnvironment(nameof(config.AIChatService), config.AIChatService);
switch (config.AIChatService)
{
case AzureOpenAIChatConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIChat)}__{nameof(config.AzureOpenAIChat.DeploymentName)}", config.AzureOpenAIChat.DeploymentName);
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIChat)}__{nameof(config.AzureOpenAIChat.ModelName)}", config.AzureOpenAIChat.ModelName);
break;
}
case OpenAIChatConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.OpenAIChat)}__{nameof(config.OpenAIChat.ModelName)}", config.OpenAIChat.ModelName);
break;
}
default:
throw new NotSupportedException($"AI service '{config.AIChatService}' is not supported.");
}
// Add RAG configuration to the environment variables so that Api Service can access it.
builder.WithEnvironment($"{nameof(config.Rag)}__{nameof(config.Rag.AIEmbeddingService)}", config.Rag.AIEmbeddingService);
builder.WithEnvironment($"{nameof(config.Rag)}__{nameof(config.Rag.VectorStoreType)}", config.Rag.VectorStoreType);
builder.WithEnvironment($"{nameof(config.Rag)}__{nameof(config.Rag.CollectionName)}", config.Rag.CollectionName);
switch (config.Rag.AIEmbeddingService)
{
case AzureOpenAIEmbeddingsConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIEmbeddings)}__{nameof(config.AzureOpenAIEmbeddings.DeploymentName)}", config.AzureOpenAIEmbeddings.DeploymentName);
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIEmbeddings)}__{nameof(config.AzureOpenAIEmbeddings.ModelName)}", config.AzureOpenAIEmbeddings.ModelName);
break;
}
case OpenAIEmbeddingsConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.OpenAIEmbeddings)}__{nameof(config.OpenAIEmbeddings.ModelName)}", config.OpenAIEmbeddings.ModelName);
break;
}
default:
throw new NotSupportedException($"AI service '{config.Rag.AIEmbeddingService}' is not supported.");
}
return builder;
}
/// <summary>
/// Adds connection strings of source resources to a destination resource.
/// </summary>
/// <typeparam name="T">The type of the destination resource.</typeparam>
/// <param name="builder">The destination resource.</param>
/// <param name="resources">The source resource with the connection string.</param>
/// <returns>The updated resource builder.</returns>
public static IResourceBuilder<T> WithReferences<T>(this IResourceBuilder<T> builder, IList<IResourceBuilder<IResourceWithConnectionString>> resources) where T : IResourceWithEnvironment
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(resources);
foreach (var resource in resources)
{
builder.WithReference(resource);
}
return builder;
}
}
@@ -0,0 +1,128 @@
// Copyright (c) Microsoft. All rights reserved.
using ChatWithAgent.AppHost.Extensions;
using ChatWithAgent.Configuration;
var builder = DistributedApplication.CreateBuilder(args);
// Load host configuration.
var hostConfig = new HostConfig(builder.Configuration);
// Add Api Service AI upstream dependencies
var aiServices = AddAIServices(builder, hostConfig);
// Add Vector Store
var vectorStore = AddVectorStore(builder, hostConfig);
// Add Api Service
var apiService = builder.AddProject<Projects.ChatWithAgent_ApiService>("apiservice")
.WithEnvironment(hostConfig) // Add some host configuration as environment variables so that the Api Service can access them
.WithReferences(aiServices)
.WithReference(vectorStore);
// Add Web Frontend
builder.AddProject<Projects.ChatWithAgent_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
static List<IResourceBuilder<IResourceWithConnectionString>> AddAIServices(IDistributedApplicationBuilder builder, HostConfig config)
{
IResourceBuilder<IResourceWithConnectionString>? chatResource = null;
IResourceBuilder<IResourceWithConnectionString>? embeddingsResource = null;
// Add Azure OpenAI service and configured AI models
if (config.AIChatService == AzureOpenAIChatConfig.ConfigSectionName || config.Rag.AIEmbeddingService == AzureOpenAIEmbeddingsConfig.ConfigSectionName)
{
if (builder.ExecutionContext.IsPublishMode)
{
// Add Azure OpenAI service
var azureOpenAI = builder.AddAzureOpenAI(HostConfig.AzureOpenAIConnectionStringName);
// Add chat deployment
if (config.AIChatService == AzureOpenAIChatConfig.ConfigSectionName)
{
chatResource = azureOpenAI
.AddDeployment(
name: config.AzureOpenAIChat.DeploymentName,
modelName: config.AzureOpenAIChat.ModelName,
modelVersion: config.AzureOpenAIChat.ModelVersion)
.WithProperties((resource) =>
{
if (config.AzureOpenAIChat.SkuName is { } skuName)
{
resource.SkuName = skuName;
}
if (config.AzureOpenAIChat.SkuCapacity is { } skuCapacity)
{
resource.SkuCapacity = skuCapacity;
}
});
}
// Add deployment
if (config.Rag.AIEmbeddingService == AzureOpenAIEmbeddingsConfig.ConfigSectionName)
{
embeddingsResource = azureOpenAI
.AddDeployment(
name: config.AzureOpenAIEmbeddings.DeploymentName,
modelName: config.AzureOpenAIEmbeddings.ModelName,
modelVersion: config.AzureOpenAIEmbeddings.ModelVersion)
.WithProperties((resource) =>
{
if (config.AzureOpenAIEmbeddings.SkuName is { } skuName)
{
resource.SkuName = skuName;
}
if (config.AzureOpenAIEmbeddings.SkuCapacity is { } skuCapacity)
{
resource.SkuCapacity = skuCapacity;
}
});
}
}
else
{
// Use an existing Azure OpenAI service via connection string
chatResource = embeddingsResource = builder.AddConnectionString(HostConfig.AzureOpenAIConnectionStringName);
}
}
// Add OpenAI service via connection string
if (config.AIChatService == OpenAIChatConfig.ConfigSectionName || config.Rag.AIEmbeddingService == OpenAIEmbeddingsConfig.ConfigSectionName)
{
chatResource = embeddingsResource = builder.AddConnectionString(HostConfig.OpenAIConnectionStringName);
}
if (chatResource is null)
{
throw new NotSupportedException($"AI Chat service '{config.AIChatService}' is not supported.");
}
if (embeddingsResource is null)
{
throw new NotSupportedException($"AI Embedding service '{config.Rag.AIEmbeddingService}' is not supported.");
}
return [chatResource, embeddingsResource];
}
static IResourceBuilder<IResourceWithConnectionString> AddVectorStore(IDistributedApplicationBuilder builder, HostConfig config)
{
switch (config.Rag.VectorStoreType)
{
case AzureAISearchConfig.ConfigSectionName:
{
return builder.ExecutionContext.IsPublishMode ?
builder.AddAzureSearch(AzureAISearchConfig.ConnectionStringName) :
builder.AddConnectionString(AzureAISearchConfig.ConnectionStringName);
}
default:
{
throw new NotSupportedException($"Vector Store type '{config.Rag.VectorStoreType}' is not supported.");
}
}
}
@@ -0,0 +1,39 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
},
"AIServices": {
"AzureOpenAIChat": {
"DeploymentName": "gpt-4o-mini",
"ModelName": "gpt-4o-mini",
"ModelVersion": "2024-07-18",
"SkuCapacity": 20
},
"AzureOpenAIEmbeddings": {
"DeploymentName": "text-embedding-3-small",
"ModelName": "text-embedding-3-small",
"ModelVersion": "1",
"SkuCapacity": 20
},
"OpenAIChat": {
"ModelName": "gpt-4o-mini"
},
"OpenAIEmbeddings": {
"ModelName": "text-embedding-3-small"
}
},
"VectorStores": {
"AzureAISearch": {
}
},
"AIChatService": "AzureOpenAIChat",
"Rag": {
"AIEmbeddingService": "AzureOpenAIEmbeddings",
"CollectionName": "",
"VectorStoreType": "AzureAISearch"
}
}