Galosys.Foundation.Agents.AI.Hosting 26.8.17.1

dotnet add package Galosys.Foundation.Agents.AI.Hosting --version 26.8.17.1
                    
NuGet\Install-Package Galosys.Foundation.Agents.AI.Hosting -Version 26.8.17.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting" Version="26.8.17.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.Agents.AI.Hosting" Version="26.8.17.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Galosys.Foundation.Agents.AI.Hosting --version 26.8.17.1
                    
#r "nuget: Galosys.Foundation.Agents.AI.Hosting, 26.8.17.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Galosys.Foundation.Agents.AI.Hosting@26.8.17.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Galosys.Foundation.Agents.AI.Hosting&version=26.8.17.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.Agents.AI.Hosting&version=26.8.17.1
                    
Install as a Cake Tool

Galosys.Foundation.Agents.AI.Hosting

Agent Hosting 基础托管模块(worker/控制台)。为 Galosys.Foundation.Agents.AI 创建的智能体提供通用主机(Generic Host)托管基础:内存 Agent 会话存储,不依赖 ASP.NET Core,适用于 worker/控制台应用。

需要 ASP.NET Core 托管(OpenAI Responses/Conversations、DevUI、A2A、AGUI)请引用 Galosys.Foundation.Agents.AI.Hosting.AspNetCore

安装

<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting" />

注册

worker/控制台应用可直接调用 AddAgentsHostingCore(),或依赖模块自动发现(程序集被引用时 AgentsAIHostingModule 自动注册):

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddAgentsHostingCore(); // 注册内存 Agent 会话存储

AddAgentsHostingCore() 内部使用 TryAdd 幂等注册:

  • InMemoryAgentSessionStore
  • AgentSessionStore(解析为内存实现)

注册并运行 Agent(worker 控制台)

using Microsoft.Agents.AI.Hosting;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

// 注册托管 Agent(MAF AddAIAgent)
builder.Services.AddAgentsHostingCore();
builder.Services.AddAIAgent("assistant", "You are a helpful assistant.");

var host = builder.Build();

// 通过 AgentSessionStore 管理会话
using var scope = host.Services.CreateScope();
var store = scope.ServiceProvider.GetRequiredService<AgentSessionStore>();
var agent = scope.ServiceProvider.GetRequiredService<Microsoft.Agents.AI.AIAgent>();
var session = await agent.CreateSessionAsync();
await store.SaveSessionAsync(agent, "conversation-1", session);

await host.RunAsync();

注册并接线 Agent(官方 AddAIAgent factory 方式)

官方 AddAIAgentfactory 重载 AddAIAgent(name, (sp, key) => ...) 提供完整的 agent 构造控制:可指定 keyed IChatClient、自由设置 ChatOptions、手动挂载 ChatHistoryProvider / AIContextProvider,并链式 WithAITools(官方接线工具)与 WithEfCoreSessionStore(EFCore 模块注册会话存储)。

using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

// 输出护栏评估器与工具级指标由 Agents.AI 模块自动注册(TryAdd 幂等);
// 如需自定义评估器/metrics,可先 RemoveAll 后自注册覆盖。

// 1. 注册各 Provider(各消息级模块,非 Hosting 依赖)
services.AddAgentRedisMemoryProvider(options => options.Namespace = "user");
services.AddEfCoreChatHistoryProvider<ApplicationDbContext>(o => o.SigningKeyProvider = ...);
services.AddAgentMilvusRagProvider(...);
services.AddAgentCompaction(strategy);

// 2. 官方 factory 重载:完整控制 agent 构造(keyed chat client、ChatOptions、Provider/Store 手动接线)
services.AddAIAgent("default", (sp, key) =>
{
    var chatClient = sp.GetRequiredKeyedService<IChatClient>("Deepseek");   // keyed chat client
    return chatClient.AsAIAgent(
                    new ChatClientAgentOptions
                    {
                        Name = "default",
                        ChatOptions = new ChatOptions
                        {
                            Instructions = "你一个相当于c#语言创作者安德斯·海尔斯伯格那样的技术专家。",
                            // 可自由配置 Temperature / MaxOutputTokens / ResponseFormat 等
                        },
                        ChatHistoryProvider = sp.GetRequiredService<RedisChatHistoryProvider>(), // 消息级历史
                        AIContextProviders = [sp.GetRequiredService<AgentRedisMemoryProvider>()] // 长记忆/RAG/压缩
                    }).AsBuilder()
                    .Build();
})
 .WithEfCoreSessionStore<ApplicationDbContext>()   // 会话级存储(注册 AgentSessionStore)
 .WithAITools(AIFunctionFactory.Create(Tools.GetWeather)); // 官方接线工具

要点:

  • keyed chat clientAddAIAgent(name, (sp, key) => ...) 内用 sp.GetRequiredKeyedService<IChatClient>("Deepseek") 按 key 解析,支持多模型/多租户 agent 并存。
  • 自由 ChatOptions:直接在 ChatClientAgentOptions.ChatOptions 配置 Instructions / Temperature / MaxOutputTokens 等。
  • Provider/Store 手动接线:官方 builder 只接线 IChatClientAITool,不接线 keyed AIContextProvider / ChatHistoryProvider / AgentSessionStore,故须在 factory 内手动挂载:
    • ChatHistoryProvider → 消息级聊天历史持久化。
    • AIContextProviders → 长记忆 / RAG / 压缩等上下文注入。
    • WithEfCoreSessionStore → 会话级存储(EFCore 模块,注册 AgentSessionStore;会话持久化须结合 AIHostAgent 包装,见下)。
  • 会话持久化(AIHostAgent):官方 builder 不从 keyed store 自动 new AIHostAgent。需要会话落库时,在 factory 内手动包装:
    var store = sp.GetRequiredService<AgentSessionStore>();
    var inner = chatClient.AsAIAgent(new ChatClientAgentOptions { ... });
    return new AIHostAgent(inner, store);
    
  • 对齐官方.WithAITools(...) 等官方 IHostedAgentBuilder 扩展完全继承。

迁移说明(原 aspnetcore 用户)

Galosys.Foundation.Agents.AI.Hosting 中的 ASP.NET Core 托管能力(AddAgentsHosting() / UseAgentsHosting(),含 OpenAI Responses/Conversations、DevUI、A2A、AGUI)已迁移至独立模块 Galosys.Foundation.Agents.AI.Hosting.AspNetCore。ASP.NET Core 应用请改引用新模块:

builder.Services.AddAgentsHosting();   // 来自 AspNetCore 模块
app.UseAgentsHosting();

依赖

  • Galosys.Foundation.Core — 基础设施
  • Galosys.Foundation.Agents.AI — Agent 核心模块
  • Microsoft.Agents.AI.Hosting — MAF Hosting 基础(无 ASP.NET Core 依赖)
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Galosys.Foundation.Agents.AI.Hosting:

Package Downloads
Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore

Galosys.Foundation快速开发库

Galosys.Foundation.Agents.AI.Hosting.AspNetCore

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.8.17.1 19 8/17/2026
26.8.13.2 52 8/13/2026
26.8.13.1 58 8/13/2026
26.8.12.2 58 8/12/2026
26.8.12.1 58 8/12/2026
26.8.10.1 71 8/10/2026
26.8.5.1 78 8/5/2026
26.8.4.1 71 8/4/2026
26.8.3.1 56 8/3/2026
26.7.31.1 66 7/31/2026
26.7.30.1 59 7/30/2026
26.7.29.1 62 7/29/2026
26.7.28.1 60 7/28/2026
26.7.26.2 57 7/26/2026
26.7.26.1 60 7/25/2026
26.7.25.1 53 7/25/2026
26.7.24.2 58 7/24/2026
26.7.24.1 60 7/24/2026
26.7.22.1 61 7/23/2026