-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodexServiceCollectionExtensions.cs
More file actions
63 lines (54 loc) · 2.46 KB
/
Copy pathCodexServiceCollectionExtensions.cs
File metadata and controls
63 lines (54 loc) · 2.46 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
56
57
58
59
60
61
62
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Incursa.OpenAI.Codex.Extensions;
// Traceability: REQ-CODEX-SDK-CATALOG-0309.
/// <summary>
/// Provides dependency-injection registration helpers for the Codex SDK.
/// </summary>
public static class CodexServiceCollectionExtensions
{
/// <summary>
/// Registers <see cref="CodexClient"/> with default options.
/// </summary>
/// <param name="services">The service collection to update.</param>
/// <returns>The same service collection for chaining.</returns>
public static IServiceCollection AddCodex(this IServiceCollection services)
=> AddCodex(services, configure: null);
/// <summary>
/// Registers <see cref="CodexClient"/> and applies programmatic option configuration.
/// </summary>
/// <param name="services">The service collection to update.</param>
/// <param name="configure">The callback used to configure <see cref="CodexClientOptions"/>.</param>
/// <returns>The same service collection for chaining.</returns>
public static IServiceCollection AddCodex(
this IServiceCollection services,
Action<CodexClientOptions>? configure)
{
ArgumentNullException.ThrowIfNull(services);
services.AddOptions<CodexClientOptions>();
if (configure is not null)
{
services.Configure(configure);
}
services.TryAddSingleton(sp => new CodexClient(sp.GetRequiredService<IOptions<CodexClientOptions>>().Value));
return services;
}
/// <summary>
/// Registers <see cref="CodexClient"/> and binds options from configuration.
/// </summary>
/// <param name="services">The service collection to update.</param>
/// <param name="configuration">The configuration section to bind to <see cref="CodexClientOptions"/>.</param>
/// <returns>The same service collection for chaining.</returns>
public static IServiceCollection AddCodex(
this IServiceCollection services,
IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
services.AddOptions<CodexClientOptions>().Bind(configuration);
services.TryAddSingleton(sp => new CodexClient(sp.GetRequiredService<IOptions<CodexClientOptions>>().Value));
return services;
}
}