Skip to content

Commit

Permalink
Examples bug fixes (#38)
Browse files Browse the repository at this point in the history
* Don't share config object across agent instances of the same type
* Graceful failure in case of empty config files
  • Loading branch information
dluc authored Sep 10, 2024
1 parent 4fce6e8 commit f7dae92
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private async Task<List<T>> GetAllAsync<T>(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<T>(content)!);
}

Expand Down
4 changes: 3 additions & 1 deletion examples/dotnet-01-echo-bot/MyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyAgentConfig>(JsonSerializer.Serialize(agentConfig)) ?? new MyAgentConfig();
}

/// <inheritdoc />
Expand Down
6 changes: 4 additions & 2 deletions examples/dotnet-02-message-types-demo/MyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public MyAgent(
storage,
loggerFactory?.CreateLogger<MyAgent>() ?? new NullLogger<MyAgent>())
{
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<MyAgentConfig>(JsonSerializer.Serialize(agentConfig)) ?? new MyAgentConfig();
}

/// <inheritdoc />
Expand Down
9 changes: 6 additions & 3 deletions examples/dotnet-03-simple-chatbot/MyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ public MyAgent(
storage,
loggerFactory?.CreateLogger<MyAgent>() ?? new NullLogger<MyAgent>())
{
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<MyAgentConfig>(JsonSerializer.Serialize(agentConfig)) ?? new MyAgentConfig();
}

/// <inheritdoc />
Expand Down

0 comments on commit f7dae92

Please sign in to comment.