Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AWS.AgentCore.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<Project Path="sampleapps/ChatBotUI/ChatBotUI.csproj" />
<Project Path="sampleapps/StreamingAgent/StreamingAgent.csproj" />
<Project Path="sampleapps/AnnotationsStreamingAgent/AnnotationsStreamingAgent.csproj" />
<Project Path="sampleapps/NativeAotExtensions/NativeAotExtensions.csproj" />
<Project Path="sampleapps/NativeAotAnnotations/NativeAotAnnotations.csproj" />
</Folder>
<Folder Name="/src/">
<Project Path="src/AWS.AgentCore.SourceGenerator/AWS.AgentCore.SourceGenerator.csproj" />
Expand Down
31 changes: 31 additions & 0 deletions sampleapps/NativeAotAnnotations/Agent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using AWS.AgentCore;
using NativeAotAnnotations.Models;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging;

namespace NativeAotAnnotations;

public class Agent(IChatClient chatClient, ILogger<Agent> logger)
{
[AgentCoreHandler(JsonContext = typeof(AppJsonContext))]
public async Task<string> HandleInvocation(
PromptRequest request,
AgentCoreRuntimeContext context,
CancellationToken cancellationToken)
{
logger.LogInformation("Invocation — SessionId={SessionId}, RequestId={RequestId}",
context.SessionId, context.RequestId);

var agent = chatClient.AsAIAgent();
var session = await agent.CreateSessionAsync(cancellationToken: cancellationToken);
var response = await agent.RunAsync(request.Prompt ?? "Hello!", session, cancellationToken: cancellationToken);

return response.ToString();
}

[AgentCorePing]
public object Ping() => new { status = "Healthy", time_of_last_update = DateTimeOffset.UtcNow.ToUnixTimeSeconds() };
}
10 changes: 10 additions & 0 deletions sampleapps/NativeAotAnnotations/AppJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json.Serialization;
using NativeAotAnnotations.Models;

namespace NativeAotAnnotations;

[JsonSerializable(typeof(PromptRequest))]
internal partial class AppJsonContext : JsonSerializerContext;
18 changes: 18 additions & 0 deletions sampleapps/NativeAotAnnotations/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
RUN apt-get update && apt-get install -y clang zlib1g-dev && rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY ["sampleapps/NativeAotAnnotations/NativeAotAnnotations.csproj", "sampleapps/NativeAotAnnotations/"]
COPY ["src/AWS.AgentCore/AWS.AgentCore.csproj", "src/AWS.AgentCore/"]
COPY ["src/AWS.AgentCore.SourceGenerator/AWS.AgentCore.SourceGenerator.csproj", "src/AWS.AgentCore.SourceGenerator/"]
RUN dotnet restore "sampleapps/NativeAotAnnotations/NativeAotAnnotations.csproj"
COPY . .
WORKDIR "/src/sampleapps/NativeAotAnnotations"
RUN dotnet publish "./NativeAotAnnotations.csproj" -c $BUILD_CONFIGURATION -o /app/publish

FROM mcr.microsoft.com/dotnet/runtime-deps:10.0 AS final
USER $APP_UID
WORKDIR /app
EXPOSE 8080
COPY --from=build /app/publish .
ENTRYPOINT ["./NativeAotAnnotations"]
6 changes: 6 additions & 0 deletions sampleapps/NativeAotAnnotations/Models/PromptRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace NativeAotAnnotations.Models;

public record PromptRequest(string? Prompt);
18 changes: 18 additions & 0 deletions sampleapps/NativeAotAnnotations/NativeAotAnnotations.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AWS.AgentCore\AWS.AgentCore.csproj" />
<ProjectReference Include="..\..\src\AWS.AgentCore.SourceGenerator\AWS.AgentCore.SourceGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions sampleapps/NativeAotAnnotations/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using AWS.AgentCore;
using AWS.AgentCore.Extensions;
using Microsoft.AspNetCore.Builder;

namespace NativeAotAnnotations;

[AgentCoreStartup]
public class Startup
{
public void ConfigureServices(WebApplicationBuilder builder)
{
builder.AddAgentCore(options =>
{
options.ModelId = "global.anthropic.claude-sonnet-4-20250514-v1:0";
});
}
}
18 changes: 18 additions & 0 deletions sampleapps/NativeAotExtensions/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
RUN apt-get update && apt-get install -y clang zlib1g-dev && rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY ["sampleapps/NativeAotExtensions/NativeAotExtensions.csproj", "sampleapps/NativeAotExtensions/"]
COPY ["src/AWS.AgentCore/AWS.AgentCore.csproj", "src/AWS.AgentCore/"]
COPY ["src/AWS.AgentCore.SourceGenerator/AWS.AgentCore.SourceGenerator.csproj", "src/AWS.AgentCore.SourceGenerator/"]
RUN dotnet restore "sampleapps/NativeAotExtensions/NativeAotExtensions.csproj"
COPY . .
WORKDIR "/src/sampleapps/NativeAotExtensions"
RUN dotnet publish "./NativeAotExtensions.csproj" -c $BUILD_CONFIGURATION -o /app/publish

FROM mcr.microsoft.com/dotnet/runtime-deps:10.0 AS final
USER $APP_UID
WORKDIR /app
EXPOSE 8080
COPY --from=build /app/publish .
ENTRYPOINT ["./NativeAotExtensions"]
6 changes: 6 additions & 0 deletions sampleapps/NativeAotExtensions/Models/PromptRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace NativeAotExtensions.Models;

public record PromptRequest(string? Prompt);
14 changes: 14 additions & 0 deletions sampleapps/NativeAotExtensions/NativeAotExtensions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AWS.AgentCore\AWS.AgentCore.csproj" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions sampleapps/NativeAotExtensions/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json.Serialization;
using AWS.AgentCore;
using AWS.AgentCore.Extensions;
using Microsoft.Extensions.AI;
using NativeAotExtensions.Models;

var builder = WebApplication.CreateBuilder(args);

builder.AddAgentCore(options =>
{
options.ModelId = "global.anthropic.claude-sonnet-4-20250514-v1:0";
});

var app = builder.Build();

// NativeAOT-safe: strongly-typed handler with IServiceProvider for DI access
app.MapAgentCore<PromptRequest>(
async (request, context, services, ct) =>
{
var chatClient = services.GetRequiredService<IChatClient>();
var logger = services.GetRequiredService<ILogger<Program>>();

logger.LogInformation("Invocation — SessionId={SessionId}, RequestId={RequestId}",
context.SessionId, context.RequestId);

var agent = chatClient.AsAIAgent();
var session = await agent.CreateSessionAsync(cancellationToken: ct);
var response = await agent.RunAsync(request.Prompt ?? "Hello!", session, cancellationToken: ct);

return response.ToString();
},
AppJsonContext.Default.PromptRequest);

app.Run();

[JsonSerializable(typeof(PromptRequest))]
internal partial class AppJsonContext : JsonSerializerContext;
Loading
Loading