mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Initial draft of actor runtime abstractions (#197)
* Initial draft of actor runtime abstractions
This commit is contained in:
committed by
GitHub
Unverified
parent
8f2d3da80d
commit
41d441420e
+32
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Extensions.AI.Agents.Runtime;
|
||||
|
||||
namespace HelloHttpApi.ApiService;
|
||||
|
||||
public static class HostApplicationBuilderAgentExtensions
|
||||
{
|
||||
public static IHostApplicationBuilder AddChatClientAgent(this IHostApplicationBuilder builder, string name, string instructions, string? chatClientKey = null)
|
||||
{
|
||||
var agentKey = $"agent:{name}";
|
||||
builder.Services.AddKeyedSingleton(agentKey, (sp, key) =>
|
||||
{
|
||||
var chatClient = chatClientKey is null ? sp.GetRequiredService<IChatClient>() : sp.GetRequiredKeyedService<IChatClient>(chatClientKey);
|
||||
return new ChatClientAgent(chatClient, instructions, name);
|
||||
});
|
||||
var actorBuilder = builder.AddActorRuntime();
|
||||
|
||||
actorBuilder.AddActorType(
|
||||
new ActorType(agentKey),
|
||||
(sp, ctx) => new ChatClientAgentActor(
|
||||
sp.GetRequiredKeyedService<ChatClientAgent>(agentKey),
|
||||
sp.GetService<JsonSerializerOptions>() ?? JsonSerializerOptions.Web,
|
||||
ctx,
|
||||
sp.GetRequiredService<ILogger<ChatClientAgentActor>>()));
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user