From f7dae92b2df9c311fc003ac96c11f1195178aba5 Mon Sep 17 00:00:00 2001 From: Devis Lucato Date: Tue, 10 Sep 2024 15:45:47 -0700 Subject: [PATCH] Examples bug fixes (#38) * Don't share config object across agent instances of the same type * Graceful failure in case of empty config files --- .../WorkbenchConnector/Storage/AgentServiceStorage.cs | 2 ++ examples/dotnet-01-echo-bot/MyAgent.cs | 4 +++- examples/dotnet-02-message-types-demo/MyAgent.cs | 6 ++++-- examples/dotnet-03-simple-chatbot/MyAgent.cs | 9 ++++++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/assistant-connector/dotnet/WorkbenchConnector/Storage/AgentServiceStorage.cs b/assistant-connector/dotnet/WorkbenchConnector/Storage/AgentServiceStorage.cs index 2e4c0c0a..ba996e18 100644 --- a/assistant-connector/dotnet/WorkbenchConnector/Storage/AgentServiceStorage.cs +++ b/assistant-connector/dotnet/WorkbenchConnector/Storage/AgentServiceStorage.cs @@ -136,6 +136,8 @@ private async Task> GetAllAsync(string prefix, string suffix, Cancell if (!filename.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)) { continue; } var content = await File.ReadAllTextAsync(filePath, cancellationToken).ConfigureAwait(false); + if (string.IsNullOrEmpty(content)) { continue; } + result.Add(JsonSerializer.Deserialize(content)!); } diff --git a/examples/dotnet-01-echo-bot/MyAgent.cs b/examples/dotnet-01-echo-bot/MyAgent.cs index 21196564..cf61f294 100644 --- a/examples/dotnet-01-echo-bot/MyAgent.cs +++ b/examples/dotnet-01-echo-bot/MyAgent.cs @@ -38,7 +38,9 @@ public MyAgent( { this.Id = agentId; this.Name = agentName; - this.Config = agentConfig ?? new MyAgentConfig(); + + // Clone object to avoid config object being shared + this.Config = JsonSerializer.Deserialize(JsonSerializer.Serialize(agentConfig)) ?? new MyAgentConfig(); } /// diff --git a/examples/dotnet-02-message-types-demo/MyAgent.cs b/examples/dotnet-02-message-types-demo/MyAgent.cs index 2d1e8000..7cb243c7 100644 --- a/examples/dotnet-02-message-types-demo/MyAgent.cs +++ b/examples/dotnet-02-message-types-demo/MyAgent.cs @@ -44,10 +44,12 @@ public MyAgent( storage, loggerFactory?.CreateLogger() ?? new NullLogger()) { + this._contentSafety = contentSafety; this.Id = agentId; this.Name = agentName; - this.Config = agentConfig ?? new MyAgentConfig(); - this._contentSafety = contentSafety; + + // Clone object to avoid config object being shared + this.Config = JsonSerializer.Deserialize(JsonSerializer.Serialize(agentConfig)) ?? new MyAgentConfig(); } /// diff --git a/examples/dotnet-03-simple-chatbot/MyAgent.cs b/examples/dotnet-03-simple-chatbot/MyAgent.cs index f7f8be20..642240d6 100644 --- a/examples/dotnet-03-simple-chatbot/MyAgent.cs +++ b/examples/dotnet-03-simple-chatbot/MyAgent.cs @@ -52,11 +52,14 @@ public MyAgent( storage, loggerFactory?.CreateLogger() ?? new NullLogger()) { - this.Id = agentId; - this.Name = agentName; - this.Config = agentConfig ?? new MyAgentConfig(); this._appConfig = appConfig; this._contentSafety = contentSafety; + + this.Id = agentId; + this.Name = agentName; + + // Clone object to avoid config object being shared + this.Config = JsonSerializer.Deserialize(JsonSerializer.Serialize(agentConfig)) ?? new MyAgentConfig(); } ///