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
10 changes: 10 additions & 0 deletions src/Agents/AgentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ public static ChatResponse AsChatResponse(this AgentRunResponse response)

return chatResponse;
}

extension(AIAgent agent)
{
/// <summary>Gets the emoji associated with the agent, if any.</summary>
public string? Emoji => agent is not IHasAdditionalProperties additional
? null
: additional.AdditionalProperties is null
? null
: additional.AdditionalProperties.TryGetValue("Emoji", out var value) ? value as string : null;
}
}
19 changes: 18 additions & 1 deletion src/Agents/ConfigurableAIAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Devlooped.Agents.AI;
/// A configuration-driven <see cref="AIAgent"/> which monitors configuration changes and
/// re-applies them to the inner agent automatically.
/// </summary>
public sealed partial class ConfigurableAIAgent : AIAgent, IDisposable
public sealed partial class ConfigurableAIAgent : AIAgent, IHasAdditionalProperties, IDisposable
{
readonly IServiceProvider services;
readonly IConfiguration configuration;
Expand Down Expand Up @@ -57,6 +57,9 @@ Type t when typeof(AIAgentMetadata).IsAssignableFrom(t) => metadata,
_ => agent.GetService(serviceType, serviceKey)
};

/// <inheritdoc/>
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }

/// <inheritdoc/>
public override string Id => agent.Id;
/// <inheritdoc/>
Expand Down Expand Up @@ -89,6 +92,20 @@ public override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(IEnum
options?.Description = options?.Description?.Dedent();
options?.Instructions = options?.Instructions?.Dedent();

var properties = configSection.Get<AdditionalPropertiesDictionary>();
if (properties is not null)
{
properties?.Remove(nameof(AgentClientOptions.Name));
properties?.Remove(nameof(AgentClientOptions.Description));
properties?.Remove(nameof(AgentClientOptions.Instructions));
properties?.Remove(nameof(AgentClientOptions.Client));
properties?.Remove(nameof(AgentClientOptions.Model));
properties?.Remove(nameof(AgentClientOptions.Use));
properties?.Remove(nameof(AgentClientOptions.Tools));

AdditionalProperties = properties;
}

// If there was a custom id, we must validate it didn't change since that's not supported.
if (configuration[$"{section}:name"] is { } newname && newname != name)
throw new InvalidOperationException($"The name of a configured agent cannot be changed at runtime. Expected '{name}' but was '{newname}'.");
Expand Down
10 changes: 10 additions & 0 deletions src/Agents/IHasAdditionalProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.Extensions.AI;

namespace Devlooped.Agents.AI;

/// <summary>Indicates that the instance can have additional properties associated with it.</summary>
public interface IHasAdditionalProperties
{
/// <summary>Gets or sets any additional properties associated with the instance.</summary>
AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
}
5 changes: 5 additions & 0 deletions src/Tests/ConfigurableAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void CanConfigureAgent()
["ai:agents:bot:description"] = "Helpful chat agent",
["ai:agents:bot:instructions"] = "You are a helpful chat agent.",
["ai:agents:bot:options:temperature"] = "0.5",
["ai:agents:bot:emoji"] = "🤖",
});

builder.AddAIAgents();
Expand All @@ -36,6 +37,10 @@ public void CanConfigureAgent()
Assert.Equal("chat", agent.Name);
Assert.Equal("chat", agent.DisplayName);
Assert.Equal("Helpful chat agent", agent.Description);

var additional = Assert.IsType<IHasAdditionalProperties>(agent, exactMatch: false);
Assert.Equal("🤖", additional.AdditionalProperties?["emoji"]?.ToString());
Assert.Equal("🤖", agent.Emoji);
}

[Fact]
Expand Down
Loading