| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.AI.Abstractions.Contracts; |
| | | 4 | | using Elsa.AI.Abstractions.Models; |
| | | 5 | | using GitHub.Copilot; |
| | | 6 | | using Microsoft.Extensions.AI; |
| | | 7 | | |
| | | 8 | | namespace Elsa.AI.Copilot.Adapters; |
| | | 9 | | |
| | 2 | 10 | | public class ElsaCopilotToolFunction(AIToolDefinition definition, IAIProviderToolInvoker toolInvoker) : AIFunction |
| | | 11 | | { |
| | 1 | 12 | | private static readonly MethodInfo InvokeMethod = typeof(ElsaCopilotToolFunction).GetMethod(nameof(InvokeToolAsync), |
| | 1 | 13 | | private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web); |
| | 2 | 14 | | private readonly JsonElement _jsonSchema = JsonSerializer.SerializeToElement(definition.Schema, SerializerOptions); |
| | 2 | 15 | | private readonly IReadOnlyDictionary<string, object?> _additionalProperties = CreateAdditionalProperties(definition) |
| | | 16 | | |
| | 1 | 17 | | public override string Name => definition.Name; |
| | 1 | 18 | | public override string Description => definition.Description; |
| | 1 | 19 | | public override JsonElement JsonSchema => _jsonSchema; |
| | 0 | 20 | | public override JsonSerializerOptions JsonSerializerOptions => SerializerOptions; |
| | 0 | 21 | | public override MethodInfo UnderlyingMethod => InvokeMethod; |
| | 1 | 22 | | public override IReadOnlyDictionary<string, object?> AdditionalProperties => _additionalProperties; |
| | | 23 | | |
| | | 24 | | protected override async ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancell |
| | | 25 | | { |
| | 1 | 26 | | var result = await InvokeToolAsync(arguments, cancellationToken); |
| | 1 | 27 | | return new ToolResultAIContent(new ToolResultObject |
| | 1 | 28 | | { |
| | 1 | 29 | | TextResultForLlm = result.Summary, |
| | 1 | 30 | | ResultType = result.Status == AIToolInvocationStatus.Failed ? "error" : "text", |
| | 1 | 31 | | Error = result.Error, |
| | 1 | 32 | | ToolTelemetry = new Dictionary<string, object> |
| | 1 | 33 | | { |
| | 1 | 34 | | ["status"] = result.Status.ToString(), |
| | 1 | 35 | | ["toolName"] = definition.Name |
| | 1 | 36 | | } |
| | 1 | 37 | | }); |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | private async ValueTask<AIToolResult> InvokeToolAsync(AIFunctionArguments arguments, CancellationToken cancellationT |
| | | 41 | | { |
| | 1 | 42 | | var invocation = new AIProviderToolInvocation |
| | 1 | 43 | | { |
| | 1 | 44 | | Id = ReadToolCallId(arguments) ?? Guid.NewGuid().ToString("N"), |
| | 1 | 45 | | ToolName = definition.Name, |
| | 1 | 46 | | Arguments = ToJsonObject(arguments) |
| | 1 | 47 | | }; |
| | | 48 | | |
| | 1 | 49 | | return await toolInvoker.InvokeAsync(invocation, cancellationToken); |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static string? ReadToolCallId(AIFunctionArguments arguments) |
| | | 53 | | { |
| | 1 | 54 | | if (arguments.Context == null) |
| | 0 | 55 | | return null; |
| | | 56 | | |
| | 3 | 57 | | foreach (var value in arguments.Context.Values) |
| | | 58 | | { |
| | 1 | 59 | | if (value is ToolInvocation invocation && !string.IsNullOrWhiteSpace(invocation.ToolCallId)) |
| | 1 | 60 | | return invocation.ToolCallId; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return null; |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | private static JsonObject ToJsonObject(AIFunctionArguments arguments) |
| | | 67 | | { |
| | 1 | 68 | | var json = new JsonObject(); |
| | 4 | 69 | | foreach (var (key, value) in arguments) |
| | 1 | 70 | | json[key] = value == null ? null : JsonSerializer.SerializeToNode(value, SerializerOptions); |
| | | 71 | | |
| | 1 | 72 | | return json; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private static IReadOnlyDictionary<string, object?> CreateAdditionalProperties(AIToolDefinition definition) |
| | | 76 | | { |
| | 2 | 77 | | var properties = new Dictionary<string, object?> |
| | 2 | 78 | | { |
| | 2 | 79 | | ["elsa_mutability"] = definition.Mutability.ToString(), |
| | 2 | 80 | | ["elsa_danger_level"] = definition.DangerLevel.ToString(), |
| | 2 | 81 | | ["skip_permission"] = definition.Mutability == AIToolMutability.ReadOnly && definition.DangerLevel == AITool |
| | 2 | 82 | | }; |
| | | 83 | | |
| | 2 | 84 | | if (!string.IsNullOrWhiteSpace(definition.Provider)) |
| | 0 | 85 | | properties["elsa_provider"] = definition.Provider; |
| | | 86 | | |
| | 2 | 87 | | return properties; |
| | | 88 | | } |
| | | 89 | | } |