Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring back grpc service #5377

Merged
merged 62 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
16b20f2
feat: Set up Scaffolding for Core.Grpc
jackgerrits Jan 28, 2025
e5da437
refactor: Extract Message channel logic to MessageRouter
lokitoth Jan 30, 2025
df3ca72
refactor: Move GrpcMessageRouter to own file
lokitoth Jan 30, 2025
9d981dc
refactor: Add default IAgentRuntime.GetAgent implementations
lokitoth Jan 30, 2025
edf64ba
feat: Implement remaining methods in GrpcAgentRuntime
lokitoth Jan 30, 2025
1369a5d
fix: Get Core.Grpc test project building
lokitoth Jan 30, 2025
414c407
fixup: Remove commented out Payload serialization code
lokitoth Jan 30, 2025
7c3c934
fixup: Merge Builder extension method classes
lokitoth Jan 30, 2025
72168b5
Cleanup constants, put back rpc based impl
jackgerrits Feb 3, 2025
f87497b
Finish implementation including initial manual testing
jackgerrits Feb 3, 2025
2d5fb6d
Merge branch 'main' into grpc_dotnet_worker
jackgerrits Feb 3, 2025
f8f7093
remove unrelated files
jackgerrits Feb 3, 2025
7fb61ca
fix build
jackgerrits Feb 3, 2025
734230b
interim - adding tests for GrpcAgentRuntime
rysweet Feb 4, 2025
ef9230e
moving all the tests to the right runtime
rysweet Feb 4, 2025
05a077b
cleanup each test
rysweet Feb 4, 2025
7432909
Simplify test server side
rysweet Feb 4, 2025
0dc34f7
interim commit so I can rebase
rysweet Feb 5, 2025
1167274
rebase
rysweet Feb 5, 2025
b140a85
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 5, 2025
4aa987e
re-add project
rysweet Feb 5, 2025
73ece88
fixing up the rename
rysweet Feb 5, 2025
2d31835
interim
rysweet Feb 5, 2025
f1aa1fa
resolve build breaks and add doc comments
rysweet Feb 5, 2025
388f021
fix path
rysweet Feb 5, 2025
ee7032b
more fixup
rysweet Feb 5, 2025
611b859
more cleanup
rysweet Feb 6, 2025
d3d9128
add topicPrefix
rysweet Feb 7, 2025
d16a978
remove unused
rysweet Feb 7, 2025
759a4da
update test helpers
rysweet Feb 7, 2025
50007b2
cleaning up tests
rysweet Feb 7, 2025
c8d5d6b
tests working
rysweet Feb 7, 2025
3b51495
add back the test projects
rysweet Feb 7, 2025
df4a3b8
adding back Agents folder
rysweet Feb 7, 2025
e825443
adding back agents package and agent_events.proto used by xlang integ…
rysweet Feb 7, 2025
fbbcfdc
trying to avoid messy rebase
rysweet Feb 7, 2025
9b47878
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 7, 2025
7312e67
removing this again
rysweet Feb 7, 2025
92dc5a7
format
rysweet Feb 7, 2025
c97339e
updating to deal with the proto changes
rysweet Feb 7, 2025
c8ebf4e
format
rysweet Feb 7, 2025
7fe1b48
move to tests
rysweet Feb 7, 2025
a42ab96
remove agentsmetadata
rysweet Feb 7, 2025
273df31
remove unused.
rysweet Feb 7, 2025
46f6b81
try output logging
rysweet Feb 7, 2025
2c00f2e
remove unused
rysweet Feb 7, 2025
08ed259
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 7, 2025
349577a
add python and uv setup to dotnet build
rysweet Feb 7, 2025
7a02b5c
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 7, 2025
97bf2c5
remove uv output redir
rysweet Feb 7, 2025
65ea8a6
one more move
rysweet Feb 7, 2025
8d5152a
cleaning up unused
rysweet Feb 7, 2025
207794f
rename variable for clarity
rysweet Feb 7, 2025
dbe744c
remove unused
rysweet Feb 7, 2025
f97d8c3
remove unused
rysweet Feb 7, 2025
ded46df
remove unused
rysweet Feb 7, 2025
57b36a6
remove unused
rysweet Feb 7, 2025
fcceecb
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 7, 2025
9d018e5
add test app host back into sln
rysweet Feb 7, 2025
a7a9f58
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 7, 2025
ed2df8c
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 7, 2025
ecb15ae
Merge branch 'main' into rysweet-grpc-service
rysweet Feb 8, 2025
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
12 changes: 12 additions & 0 deletions .github/workflows/dotnet-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ jobs:
- uses: actions/checkout@v4
with:
lfs: true
- uses: astral-sh/setup-uv@v5
with:
enable-cache: true
version: "0.5.18"
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: uv sync --locked --all-extras
working-directory: ./python
- name: Prepare python venv
run: |
source ${{ github.workspace }}/python/.venv/bin/activate
- name: Setup .NET 8.0
uses: actions/setup-dotnet@v4
with:
Expand Down
57 changes: 50 additions & 7 deletions dotnet/AutoGen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStartedGrpc", "sampl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Core.Grpc.Tests", "test\Microsoft.AutoGen.Core.Grpc.Tests\Microsoft.AutoGen.Core.Grpc.Tests.csproj", "{23A028D3-5EB1-4FA0-9CD1-A1340B830579}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.RuntimeGateway.Grpc", "src\Microsoft.AutoGen\RuntimeGateway.Grpc\Microsoft.AutoGen.RuntimeGateway.Grpc.csproj", "{BE420A71-7615-4DFD-BE94-9409397949F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.RuntimeGateway.Grpc.Tests", "test\Microsoft.AutoGen.RuntimeGateway.Grpc.Tests\Microsoft.AutoGen.RuntimeGateway.Grpc.Tests.csproj", "{CDD859F3-1B60-4ECE-8472-54DF8EFCA682}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Integration.Tests", "test\Microsoft.AutoGen.Integration.Tests\Microsoft.AutoGen.Integration.Tests.csproj", "{7A11022E-4E5D-4A4A-AADF-E715C2ECF800}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.AgentHost", "src\Microsoft.AutoGen\AgentHost\Microsoft.AutoGen.AgentHost.csproj", "{50C2E8D5-68AB-45A3-B96F-355E1F8AC039}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello.AppHost", "samples\Hello\Hello.AppHost\Hello.AppHost.csproj", "{B8E77E57-C983-4EEA-9589-906271486D80}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.AutoGen", "Microsoft.AutoGen", "{81BA12F2-2D2F-42C1-AF83-FBDAA1A78A45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Agents", "src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj", "{EF954ED3-87D5-40F1-8557-E7179F43EA0E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -300,16 +314,14 @@ Global
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Release|Any CPU.Build.0 = Release|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.CoreOnly|Any CPU.ActiveCfg = Debug|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.CoreOnly|Any CPU.Build.0 = Debug|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Release|Any CPU.Build.0 = Release|Any CPU
{AAD593FE-A49B-425E-A9FE-A0022CD25E3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAD593FE-A49B-425E-A9FE-A0022CD25E3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAD593FE-A49B-425E-A9FE-A0022CD25E3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAD593FE-A49B-425E-A9FE-A0022CD25E3D}.Release|Any CPU.Build.0 = Release|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135}.Release|Any CPU.Build.0 = Release|Any CPU
{C3740DF1-18B1-4607-81E4-302F0308C848}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3740DF1-18B1-4607-81E4-302F0308C848}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3740DF1-18B1-4607-81E4-302F0308C848}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -318,6 +330,30 @@ Global
{23A028D3-5EB1-4FA0-9CD1-A1340B830579}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23A028D3-5EB1-4FA0-9CD1-A1340B830579}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23A028D3-5EB1-4FA0-9CD1-A1340B830579}.Release|Any CPU.Build.0 = Release|Any CPU
{BE420A71-7615-4DFD-BE94-9409397949F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE420A71-7615-4DFD-BE94-9409397949F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE420A71-7615-4DFD-BE94-9409397949F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE420A71-7615-4DFD-BE94-9409397949F1}.Release|Any CPU.Build.0 = Release|Any CPU
{CDD859F3-1B60-4ECE-8472-54DF8EFCA682}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDD859F3-1B60-4ECE-8472-54DF8EFCA682}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDD859F3-1B60-4ECE-8472-54DF8EFCA682}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDD859F3-1B60-4ECE-8472-54DF8EFCA682}.Release|Any CPU.Build.0 = Release|Any CPU
{7A11022E-4E5D-4A4A-AADF-E715C2ECF800}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A11022E-4E5D-4A4A-AADF-E715C2ECF800}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A11022E-4E5D-4A4A-AADF-E715C2ECF800}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A11022E-4E5D-4A4A-AADF-E715C2ECF800}.Release|Any CPU.Build.0 = Release|Any CPU
{50C2E8D5-68AB-45A3-B96F-355E1F8AC039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50C2E8D5-68AB-45A3-B96F-355E1F8AC039}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50C2E8D5-68AB-45A3-B96F-355E1F8AC039}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50C2E8D5-68AB-45A3-B96F-355E1F8AC039}.Release|Any CPU.Build.0 = Release|Any CPU
{B8E77E57-C983-4EEA-9589-906271486D80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8E77E57-C983-4EEA-9589-906271486D80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8E77E57-C983-4EEA-9589-906271486D80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8E77E57-C983-4EEA-9589-906271486D80}.Release|Any CPU.Build.0 = Release|Any CPU
{EF954ED3-87D5-40F1-8557-E7179F43EA0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF954ED3-87D5-40F1-8557-E7179F43EA0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF954ED3-87D5-40F1-8557-E7179F43EA0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF954ED3-87D5-40F1-8557-E7179F43EA0E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -368,11 +404,18 @@ Global
{EAFFE339-26CB-4019-991D-BCCE8E7D33A1} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{58AD8E1D-83BD-4950-A324-1A20677D78D9} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D} = {CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{AAD593FE-A49B-425E-A9FE-A0022CD25E3D} = {F42F9C8E-7BD9-4687-9B63-AFFA461AF5C1}
{F42F9C8E-7BD9-4687-9B63-AFFA461AF5C1} = {CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}
{3D83C6DB-ACEA-48F3-959F-145CCD2EE135} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{C3740DF1-18B1-4607-81E4-302F0308C848} = {CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}
{23A028D3-5EB1-4FA0-9CD1-A1340B830579} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{BE420A71-7615-4DFD-BE94-9409397949F1} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{CDD859F3-1B60-4ECE-8472-54DF8EFCA682} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{7A11022E-4E5D-4A4A-AADF-E715C2ECF800} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{50C2E8D5-68AB-45A3-B96F-355E1F8AC039} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{B8E77E57-C983-4EEA-9589-906271486D80} = {F42F9C8E-7BD9-4687-9B63-AFFA461AF5C1}
{81BA12F2-2D2F-42C1-AF83-FBDAA1A78A45} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{EF954ED3-87D5-40F1-8557-E7179F43EA0E} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Microsoft.AutoGen/AgentHost/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AutoGen.Runtime.Grpc;
namespace Microsoft.AutoGen.RuntimeGateway.Grpc;

public static class Host
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Runtime.Grpc\Microsoft.AutoGen.Runtime.Grpc.csproj" />
<ProjectReference Include="..\RuntimeGateway.Grpc\Microsoft.AutoGen.RuntimeGateway.Grpc.csproj" />
<ProjectReference Include="..\Extensions\Aspire\Microsoft.AutoGen.Extensions.Aspire.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion dotnet/src/Microsoft.AutoGen/AgentHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// Program.cs
using Microsoft.Extensions.Hosting;

var app = await Microsoft.AutoGen.Runtime.Grpc.Host.StartAsync(local: false, useGrpc: true).ConfigureAwait(false);
var app = await Microsoft.AutoGen.RuntimeGateway.Grpc.Host.StartAsync(local: false, useGrpc: true).ConfigureAwait(false);
await app.WaitForShutdownAsync();
44 changes: 44 additions & 0 deletions dotnet/src/Microsoft.AutoGen/Agents/AIAgent/InferenceAgent.cs
rysweet marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// InferenceAgent.cs
using Google.Protobuf;
using Microsoft.AutoGen.Contracts;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging;
namespace Microsoft.AutoGen.Agents;
/// <summary>
/// Base class for inference agents using the Microsoft.Extensions.AI library.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="runtime"></param>
/// <param name="name"></param>
/// <param name="logger"></param>
/// <param name="client"></param>
public abstract class InferenceAgent<T>(
AgentId id,
IAgentRuntime runtime,
string name,
ILogger<InferenceAgent<T>>? logger,
IChatClient client)
: BaseAgent(id, runtime, name, logger)
where T : IMessage, new()
{
protected IChatClient ChatClient { get; } = client;
private ILogger<InferenceAgent<T>>? Logger => _logger as ILogger<InferenceAgent<T>>;
private Task<ChatCompletion> CompleteAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
{
return ChatClient.CompleteAsync(chatMessages, options, cancellationToken);
}
private IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
{
return ChatClient.CompleteStreamingAsync(chatMessages, options, cancellationToken);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IHandleConsole.cs
using Google.Protobuf;
using Microsoft.AutoGen.Contracts;

namespace Microsoft.AutoGen.Agents;
/// <summary>
/// Default interface methods for an event handler for Input and Output that writes or reads from the console
/// Can be used inside your agents by inheriting from this interface
/// public class MyAgent : BaseAgent, IHandleConsole
/// </summary>
public interface IHandleConsole : IHandle<Output>, IHandle<Input>, IProcessIO
{
/// <summary>
/// Prototype for Publish Message Async method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="topic"></param>
/// <param name="messageId"></param>
/// <param name="token"></param>
/// <returns>ValueTask</returns>
ValueTask PublishMessageAsync<T>(T message, TopicId topic, string? messageId, CancellationToken token = default) where T : IMessage;

/// <summary>
/// Receives events of type Output and writes them to the console
/// then runs the ProcessOutputAsync method which you should implement in your agent
/// </summary>
/// <param name="item"></param>
/// <param name="messageContext"></param>
/// <returns>ValueTask</returns>
async ValueTask IHandle<Output>.HandleAsync(Output item, MessageContext messageContext)
{
// Assuming item has a property `Message` that we want to write to the console
Console.WriteLine(item.Message);
await ProcessOutputAsync(item.Message);

var evt = new OutputWritten
{
Route = "console"
};
await PublishMessageAsync(evt, new TopicId("OutputWritten"), null, token: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Receives events of type Input and reads from the console, then runs the ProcessInputAsync method
/// which you should implement in your agent
/// </summary>
/// <param name="item"></param>
/// <param name="messageContext"></param>
/// <returns></returns>
async ValueTask IHandle<Input>.HandleAsync(Input item, MessageContext messageContext)
{
Console.WriteLine("Please enter input:");
string content = Console.ReadLine() ?? string.Empty;

await ProcessInputAsync(content);

var evt = new InputProcessed
{
Route = "console"
};
await PublishMessageAsync(evt, new TopicId("InputProcessed"), null, token: CancellationToken.None).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IHandleFileIO.cs

using Google.Protobuf;
using Microsoft.AutoGen.Contracts;
using Microsoft.Extensions.Logging;

namespace Microsoft.AutoGen.Agents;
/// <summary>
/// Default interface methods for an event handler for Input and Output that writes or reads from a file
/// Can be used inside your agents by inheriting from this interface
/// public class MyAgent : BaseAgent, IHandleFileIO
/// </summary>
public interface IHandleFileIO : IHandle<Input>, IHandle<Output>, IProcessIO
{
// A Logger instance to log messages
ILogger LogTarget { get; }
// The path to the input file
string InputPath { get; }
// The path to the output file
string OutputPath { get; }
// The route of the agent (used in the post-process events)
const string Route = "Microsoft.AutoGen.Agents.IHandleFileIO";

/// <summary>
/// Prototype for Publish Message Async method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="topic"></param>
/// <param name="messageId"></param>
/// <param name="token"></param>
/// <returns>ValueTask</returns>
ValueTask PublishMessageAsync<T>(T message, TopicId topic, string? messageId, CancellationToken token = default) where T : IMessage;
async ValueTask IHandle<Input>.HandleAsync(Input item, MessageContext messageContext)
{

// validate that the file exists
if (!File.Exists(InputPath))
{
var errorMessage = $"File not found: {InputPath}";
LogTarget.LogError(errorMessage);
//publish IOError event
var err = new IOError
{
Message = errorMessage
};
await PublishMessageAsync(err, new TopicId("IOError"), null, token: CancellationToken.None).ConfigureAwait(false);
return;
}
string content;
using (var reader = new StreamReader(item.Message))
{
content = await reader.ReadToEndAsync(CancellationToken.None);
}
await ProcessInputAsync(content);
var evt = new InputProcessed
{
Route = Route
};
await PublishMessageAsync(evt, new TopicId("InputProcessed"), null, token: CancellationToken.None).ConfigureAwait(false);
}
async ValueTask IHandle<Output>.HandleAsync(Output item, MessageContext messageContext)
{
using (var writer = new StreamWriter(OutputPath, append: true))
{
await writer.WriteLineAsync(item.Message);
}
var evt = new OutputWritten
{
Route = Route
};
await PublishMessageAsync(evt, new TopicId("OutputWritten"), null, token: CancellationToken.None).ConfigureAwait(false);
}
}
23 changes: 23 additions & 0 deletions dotnet/src/Microsoft.AutoGen/Agents/IOAgent/IProcessIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IProcessIO.cs

namespace Microsoft.AutoGen.Agents;

/// <summary>
/// Default Interface methods for processing input and output shared by IOAgents that should be implemented in your agent
/// </summary>
public interface IProcessIO
{
/// <summary>
/// Implement this method in your agent to process the input
/// </summary>
/// <param name="message"></param>
/// <returns>Task</returns>
static Task ProcessOutputAsync(string message) { return Task.CompletedTask; }
/// <summary>
/// Implement this method in your agent to process the output
/// </summary>
/// <param name="message"></param>
/// <returns>Task</returns>
static Task<string> ProcessInputAsync(string message) { return Task.FromResult(message); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<Import Project="$(RepoRoot)/nuget/nuget-package.props" />

<ItemGroup>
<ProjectReference Include="..\Contracts\Microsoft.AutoGen.Contracts.csproj" />
<ProjectReference Include="..\Core\Microsoft.AutoGen.Core.csproj" />
<ProjectReference Include="..\Extensions\Aspire\Microsoft.AutoGen.Extensions.Aspire.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
<PackageReference Include="Grpc.Tools" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="protos\agent_events.proto" GrpcServices="Client;Server" Link="Protos\agent_events.proto" />
</ItemGroup>
</Project>
Loading
Loading