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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<BotSharpVersion>5.2.0</BotSharpVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ public TenantConfiguration()
public TenantConfiguration(Guid id, [NotNull] string name)
: this()
{
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Name cannot be null or whitespace.");
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name cannot be null or whitespace.");
}

Id = id;
Name = name;

ConnectionStrings = new ConnectionStrings();
}

public TenantConfiguration(Guid id, [NotNull] string name, [NotNull] string normalizedName)
: this(id, name)
{
if (string.IsNullOrWhiteSpace(normalizedName)) throw new ArgumentException("NormalizedName cannot be null or whitespace.");
if (string.IsNullOrWhiteSpace(normalizedName))
{
throw new ArgumentException("NormalizedName cannot be null or whitespace.");
}

NormalizedName = normalizedName;
}
}
73 changes: 38 additions & 35 deletions src/Infrastructure/BotSharp.Core.A2A/Hooks/A2AAgentHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ public class A2AAgentHook : AgentHookBase
{
public override string SelfId => string.Empty;

private readonly A2ASettings _settings;
private readonly IA2AService _iA2AService;
private readonly A2ASettings _a2aSettings;
private readonly IA2AService _a2aService;

public A2AAgentHook(IServiceProvider services, IA2AService a2AService, A2ASettings settings)
: base(services, new AgentSettings())
public A2AAgentHook(IServiceProvider services, IA2AService a2aService, A2ASettings a2aSettings, AgentSettings agentSettings)
: base(services, agentSettings)
{
_iA2AService = a2AService;
_settings = settings;
_a2aService = a2aService;
_a2aSettings = a2aSettings;
}

public override bool OnAgentLoading(ref string id)
{
var agentId = id;
var remoteConfig = _settings.Agents.FirstOrDefault(x => x.Id == agentId);
var remoteConfig = _a2aSettings.Agents?.FirstOrDefault(x => x.Id == agentId);
if (remoteConfig != null)
{
return true;
Expand All @@ -42,43 +42,46 @@ public override void OnAgentLoaded(Agent agent)
return;
}

var remoteConfig = _settings.Agents.FirstOrDefault(x => x.Id == agent.Id);
var remoteConfig = _a2aSettings.Agents?.FirstOrDefault(x => x.Id == agent.Id);
if (remoteConfig != null)
{
var agentCard = _iA2AService.GetCapabilitiesAsync(remoteConfig.Endpoint).GetAwaiter().GetResult();
agent.Name = agentCard.Name;
agent.Description = agentCard.Description;
agent.Instruction = $"You are a proxy interface for an external intelligent service named '{agentCard.Name}'. " +
$"Your ONLY goal is to forward the user's request verbatim to the external service. " +
$"You must use the function 'delegate_to_a2a' to communicate with it. " +
$"Do not attempt to answer the question yourself.";

var properties = new Dictionary<string, object>
var agentCard = _a2aService.GetCapabilitiesAsync(remoteConfig.Endpoint).GetAwaiter().GetResult();
if (agentCard != null)
{
agent.Name = agentCard.Name;
agent.Description = agentCard.Description;
agent.Instruction = $"You are a proxy interface for an external intelligent service named '{agentCard.Name}'. " +
$"Your ONLY goal is to forward the user's request verbatim to the external service. " +
$"You must use the function 'delegate_to_a2a' to communicate with it. " +
$"Do not attempt to answer the question yourself.";

var properties = new Dictionary<string, object>
{
"user_query",
new
{
type = "string",
description = "The exact user request or task description to be forwarded."
"user_query",
new
{
type = "string",
description = "The exact user request or task description to be forwarded."
}
}
}
};
};

var propertiesJson = JsonSerializer.Serialize(properties);
var propertiesDocument = JsonDocument.Parse(propertiesJson);
var propertiesJson = JsonSerializer.Serialize(properties);
var propertiesDocument = JsonDocument.Parse(propertiesJson);

agent.Functions.Add(new FunctionDef
{
Name = "delegate_to_a2a",
Description = $"Delegates the task to the external {remoteConfig.Name} via A2A protocol.",
Parameters = new FunctionParametersDef()
agent.Functions.Add(new FunctionDef
{
Type = "object",
Properties = propertiesDocument,
Required = new List<string> { "user_query" }
}
});
Name = "delegate_to_a2a",
Description = $"Delegates the task to the external {remoteConfig.Name} via A2A protocol.",
Parameters = new FunctionParametersDef()
{
Type = "object",
Properties = propertiesDocument,
Required = new List<string> { "user_query" }
}
});
}
}
base.OnAgentLoaded(agent);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/BotSharp.Core.A2A/Services/A2AService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class A2AService : IA2AService

private readonly Dictionary<string, A2AClient> _clientCache = new Dictionary<string, A2AClient>();

public A2AService(IHttpClientFactory httpClientFactory, IServiceProvider serviceProvider, ILogger<A2AService> logger)
public A2AService(IHttpClientFactory httpClientFactory, IServiceProvider services, ILogger<A2AService> logger)
{
_httpClientFactory = httpClientFactory;
_services = serviceProvider;
_services = services;
_logger = logger;
}

Expand Down
6 changes: 0 additions & 6 deletions src/Infrastructure/BotSharp.Core.A2A/Settings/A2ASettings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BotSharp.Core.A2A.Settings;

public class A2ASettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
if (provider != null)
{
var cs = provider.GetConnectionString("BotSharpMongoDb");
if (!string.IsNullOrWhiteSpace(cs)) dbSettings.BotSharpMongoDb = cs;
if (!string.IsNullOrWhiteSpace(cs))
{
dbSettings.BotSharpMongoDb = cs;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using BotSharp.Abstraction.MultiTenancy;
using BotSharp.Abstraction.MultiTenancy.Options;
using BotSharp.Plugin.MultiTenancy.Interfaces;
using BotSharp.Plugin.MultiTenancy.Models;
using BotSharp.Plugin.MultiTenancy.MultiTenancy;
using BotSharp.Plugin.MultiTenancy.MultiTenancy.Providers;
using BotSharp.Plugin.MultiTenancy.MultiTenancy.Resolvers;
using BotSharp.Plugin.MultiTenancy.MultiTenancy.Tenant;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BotSharp.Plugin.MultiTenancy.Models;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.Interfaces;

public interface ICurrentTenantAccessor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Providers;

public class ConfigTenantOptionProvider : ITenantOptionProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Generic;
using System.Linq;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Providers
{
public class DefaultConnectionStringResolver : IConnectionStringResolver
{
Expand All @@ -19,7 +19,11 @@ public DefaultConnectionStringResolver(IOptionsMonitor<TenantStoreOptions> tenan

public string? GetConnectionString(string connectionStringName)
{
if (!_tenantStoreOptions.Enabled || !_tenantStoreOptions.Tenants.Any()) return null;
if (!_tenantStoreOptions.Enabled || !_tenantStoreOptions.Tenants.Any())
{
return null;
}

if (_currentTenant.Id.HasValue)
{
var tenant = _tenantStoreOptions.Tenants.FirstOrDefault(t => t.Id == _currentTenant.Id.Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BotSharp.Abstraction.MultiTenancy;
using Microsoft.Extensions.Configuration;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Providers;

public class TenantConnectionProvider : ITenantConnectionProvider
{
Expand All @@ -17,7 +17,10 @@ public TenantConnectionProvider(IConnectionStringResolver resolver, IConfigurati
public string GetConnectionString(string name)
{
var cs = _resolver.GetConnectionString(name);
if (!string.IsNullOrWhiteSpace(cs)) return cs;
if (!string.IsNullOrWhiteSpace(cs))
{
return cs;
}
return _configuration.GetConnectionString(name) ?? string.Empty;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using BotSharp.Plugin.MultiTenancy.Interfaces;
using BotSharp.Plugin.MultiTenancy.Models;
using System.Threading;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Tenant;

public class AsyncLocalCurrentTenantAccessor : ICurrentTenantAccessor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using BotSharp.Abstraction.MultiTenancy;
using BotSharp.Plugin.MultiTenancy.Interfaces;
using BotSharp.Plugin.MultiTenancy.Models;
using System;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Tenant;

public class CurrentTenant : ICurrentTenant
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using BotSharp.Abstraction.MultiTenancy.Options;
using Microsoft.Extensions.Options;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Tenant;

public class TenantFeature : ITenantFeature
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BotSharp.Plugin.MultiTenancy.MultiTenancy;
namespace BotSharp.Plugin.MultiTenancy.MultiTenancy.Tenant;

public class TenantResolver : ITenantResolver
{
Expand All @@ -19,7 +19,10 @@ public async Task<TenantResolveResult> ResolveAsync(TenantResolveContext context
foreach (var c in _contributors)
{
var result = await c.ResolveAsync(context);
if (result.Succeeded) return result;
if (result.Succeeded)
{
return result;
}
}

return new TenantResolveResult();
Expand Down
Loading