Skip to content

Commit

Permalink
cleanup, error handling, xlang sample publishes messages that can be …
Browse files Browse the repository at this point in the history
…heard by .NET and vice versa
  • Loading branch information
rysweet committed Nov 17, 2024
1 parent ec923b4 commit 9ace81b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
18 changes: 11 additions & 7 deletions dotnet/samples/Hello/HelloAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// step 4: send a message to the agent

// step 5: wait for the agent runtime to shutdown
// var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
// {
// Message = "World"
// }, local: true);
var app = await AgentsApp.StartAsync();
var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
{
Message = "World"
}, local: false);
//var app = await AgentsApp.StartAsync();
await app.WaitForShutdownAsync();

namespace Hello
Expand All @@ -33,7 +33,8 @@ public class HelloAgent(
ISayHello,
IHandleConsole,
IHandle<NewMessageReceived>,
IHandle<ConversationClosed>
IHandle<ConversationClosed>,
IHandle<Shutdown>
{
public async Task Handle(NewMessageReceived item)
{
Expand All @@ -55,8 +56,11 @@ public async Task Handle(ConversationClosed item)
Message = goodbye
};
await PublishMessageAsync(evt).ConfigureAwait(false);
}

// Signal shutdown.
public async Task Handle(Shutdown item)
{
Console.WriteLine("Shutting down...");
hostApplicationLifetime.StopApplication();
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/Hello/HelloAgentState/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
{
Message = "World"
}, local: true);
}, local: false);

await app.WaitForShutdownAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ private async Task RunWritePump()
item.WriteCompletionSource?.TrySetCanceled();
break;
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Unavailable)
{
// we could not connect to the endpoint - most likely we have the wrong port or failed ssl
// we need to let the user know what port we tried to connect to and then do backoff and retry
_logger.LogError(ex, "Error connecting to GRPC endpoint {Endpoint}.", channel.ToString());
break;
}
catch (Exception ex) when (!_shutdownCts.IsCancellationRequested)
{
item.WriteCompletionSource?.TrySetException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.AutoGen.Agents;

public static class GrpcAgentWorkerHostBuilderExtensions
{
private const string _defaultAgentServiceAddress = "https://localhost:5001";
private const string _defaultAgentServiceAddress = "https://localhost:53071";
public static IHostApplicationBuilder AddGrpcAgentWorker(this IHostApplicationBuilder builder, string? agentServiceAddress = null)
{
builder.Services.AddGrpcClient<AgentRpc.AgentRpcClient>(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.AutoGen.Agents;

public static class HostBuilderExtensions
{
private const string _defaultAgentServiceAddress = "https://localhost:5001";
private const string _defaultAgentServiceAddress = "https://localhost:53071";
public static AgentApplicationBuilder AddAgentWorker(this IHostApplicationBuilder builder, string? agentServiceAddress = null, bool local = false)
{
agentServiceAddress ??= builder.Configuration["AGENT_HOST"] ?? _defaultAgentServiceAddress;
Expand Down
12 changes: 8 additions & 4 deletions python/packages/autogen-core/samples/xlang/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from autogen_core.application import SingleThreadedAgentRuntime, WorkerAgentRuntime
from autogen_core.application.protos.agent_events_pb2 import NewMessageReceived
from autogen_core.base import MessageContext, try_get_known_serializers_for_type
from autogen_core.base import AgentId, MessageContext, try_get_known_serializers_for_type
from autogen_core.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handler

# Add the local package directory to sys.path
Expand All @@ -29,13 +29,17 @@ async def main() -> None:

agnext_logger.info("2")

await UserProxy.register(runtime, "proxy", lambda: UserProxy())
await runtime.add_subscription(DefaultSubscription(agent_type="proxy"))
await UserProxy.register(runtime, "HelloAgents", lambda: UserProxy())
await runtime.add_subscription(DefaultSubscription(agent_type="HelloAgents"))
agnext_logger.info("3")

message = NewMessageReceived(message="Hello from Python!")

await runtime.publish_message(message=message, topic_id=DefaultTopicId("HelloAgents"))
await runtime.publish_message(
message=message,
topic_id=DefaultTopicId("agents.NewMessageReceived"),
sender=AgentId("HelloAgents", "python"),
)
await runtime.stop_when_signal()
# await runtime.stop_when_idle()

Expand Down

0 comments on commit 9ace81b

Please sign in to comment.