-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClaudeServiceCollectionExtensions.cs
More file actions
55 lines (46 loc) · 2.15 KB
/
ClaudeServiceCollectionExtensions.cs
File metadata and controls
55 lines (46 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using ManagedCode.ClaudeCodeSharpSDK.Extensions.AI;
using ManagedCode.ClaudeCodeSharpSDK.Extensions.AI.Extensions;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace ManagedCode.ClaudeCodeSharpSDK.Extensions.AgentFramework.Extensions;
public static class ClaudeServiceCollectionExtensions
{
public static IServiceCollection AddClaudeCodeAgent(
this IServiceCollection services,
Action<ClaudeChatClientOptions>? configureChatClient = null,
Action<ChatClientAgentOptions>? configureAgent = null)
{
ArgumentNullException.ThrowIfNull(services);
services.AddClaudeChatClient(configureChatClient);
services.AddSingleton<AIAgent>(serviceProvider => CreateAgent(serviceProvider, serviceKey: null, configureAgent));
return services;
}
public static IServiceCollection AddKeyedClaudeCodeAgent(
this IServiceCollection services,
object serviceKey,
Action<ClaudeChatClientOptions>? configureChatClient = null,
Action<ChatClientAgentOptions>? configureAgent = null)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(serviceKey);
services.AddKeyedClaudeChatClient(serviceKey, configureChatClient);
services.AddKeyedSingleton<AIAgent>(serviceKey, (serviceProvider, key) => CreateAgent(serviceProvider, key, configureAgent));
return services;
}
private static ChatClientAgent CreateAgent(
IServiceProvider serviceProvider,
object? serviceKey,
Action<ChatClientAgentOptions>? configureAgent)
{
ArgumentNullException.ThrowIfNull(serviceProvider);
var options = new ChatClientAgentOptions();
configureAgent?.Invoke(options);
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
var chatClient = serviceKey is null
? serviceProvider.GetRequiredService<IChatClient>()
: serviceProvider.GetRequiredKeyedService<IChatClient>(serviceKey);
return chatClient.AsAIAgent(options, loggerFactory, serviceProvider);
}
}