| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.AI.Abstractions.Contracts; |
| | | 4 | | using Elsa.AI.Abstractions.Models; |
| | | 5 | | using Elsa.AI.Host.Endpoints.AI; |
| | | 6 | | using Elsa.AI.Host.Options; |
| | | 7 | | using Elsa.AI.Host.Permissions; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.AI.Host.Endpoints.AI.Tools; |
| | | 12 | | |
| | | 13 | | [PublicAPI] |
| | 3 | 14 | | public class Endpoint(IAIToolRegistry toolRegistry, IOptions<AIHostOptions> options) : ElsaEndpoint<Request, IReadOnlyCo |
| | | 15 | | { |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 0 | 18 | | Get("/ai/tools"); |
| | 0 | 19 | | RequirePermission(Elsa.AI.Host.Permissions.AIResourcePermissions.Tools, CoreVerbs.View); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public override async Task<IReadOnlyCollection<AIToolDefinition>> ExecuteAsync(Request request, CancellationToken ca |
| | | 23 | | { |
| | 3 | 24 | | var userPermissions = AIHttpContextIdentity.GetPermissions(HttpContext); |
| | 3 | 25 | | return await toolRegistry.ListAsync(new AIToolQuery |
| | 3 | 26 | | { |
| | 3 | 27 | | // HttpContext?.User, not HttpContext.User: unlike the chat endpoint, this method never dereferences |
| | 3 | 28 | | // HttpContext unconditionally, and the sibling calls below all accept a null context — so it really |
| | 3 | 29 | | // can be null here. GetAuthorizedAgent treats a null principal as holding nothing, while an agent |
| | 3 | 30 | | // that declares no required permissions stays authorized either way. |
| | 3 | 31 | | Agent = AIHttpContextIdentity.GetAuthorizedAgent(request.Agent, options.Value, HttpContext?.User), |
| | 3 | 32 | | ActorId = AIHttpContextIdentity.GetActorId(HttpContext), |
| | 3 | 33 | | TenantId = AIHttpContextIdentity.GetTenantId(HttpContext), |
| | 3 | 34 | | UserPermissions = userPermissions |
| | 3 | 35 | | }, cancellationToken); |
| | 3 | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public class Request |
| | | 40 | | { |
| | | 41 | | public string? Agent { get; set; } |
| | | 42 | | } |