-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
79 lines (63 loc) · 2.68 KB
/
Program.cs
File metadata and controls
79 lines (63 loc) · 2.68 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Runtime.InteropServices;
using System.Text;
using Devlooped.Extensions.AI;
using Microsoft.Agents.AI.Hosting;
using Microsoft.Agents.AI.Hosting.OpenAI;
using Microsoft.Extensions.AI;
using Spectre.Console;
var builder = WebApplication.CreateBuilder(args);
#if DEBUG
builder.Environment.EnvironmentName = Environments.Development;
// Fixes console rendering when running from Visual Studio
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8;
#endif
builder.AddServiceDefaults();
builder.ConfigureReload();
// 👇 showcases using dynamic AI context from configuration
builder.Services.AddKeyedSingleton("get_date", AIFunctionFactory.Create(() => DateTimeOffset.UtcNow, "get_date"));
// dummy ones for illustration
builder.Services.AddKeyedSingleton("create_order", AIFunctionFactory.Create(() => "OK", "create_order"));
builder.Services.AddKeyedSingleton("cancel_order", AIFunctionFactory.Create(() => "OK", "cancel_order"));
builder.Services.AddKeyedSingleton("save_notes", AIFunctionFactory.Create((string notes) => true, "save_notes"));
// 👇 implicitly calls AddChatClients
builder.AddAIAgents();
var app = builder.Build();
// From ServiceDefaults.cs
app.MapDefaultEndpoints();
#if DEBUG
// 👇 render all configured agents
await app.Services.RenderAgentsAsync(builder.Services);
#endif
// Map each agent's endpoints via response API
var catalog = app.Services.GetRequiredService<AgentCatalog>();
// List configured agents
await foreach (var agent in catalog.GetAgentsAsync())
{
if (agent.Name != null)
app.MapOpenAIResponses(agent.Name);
}
// Map the agents HTTP endpoints
app.MapAgentDiscovery("/agents");
if (!app.Environment.IsProduction())
{
app.Lifetime.ApplicationStarted.Register(() =>
{
var baseUrl = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
AnsiConsole.MarkupLine("[orange1]Registered Routes:[/]");
var endpoints = ((IEndpointRouteBuilder)app).DataSources
.SelectMany(es => es.Endpoints)
.OfType<RouteEndpoint>()
.Where(e => e.RoutePattern.RawText != null)
.OrderBy(e => e.RoutePattern.RawText);
foreach (var endpoint in endpoints)
{
var httpMethods = endpoint.Metadata
.OfType<HttpMethodMetadata>()
.SelectMany(m => m.HttpMethods) ?? [];
var methods = httpMethods.Any() ? $"{string.Join(", ", httpMethods)}" : "ANY";
AnsiConsole.MarkupLineInterpolated($"[blue][[{methods}]][/] [lime][link={baseUrl}{endpoint.RoutePattern.RawText}]{endpoint.RoutePattern.RawText}[/][/]");
}
});
}
app.Run();