|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +using Microsoft.Extensions.AI; |
| 6 | + |
| 7 | +namespace GitHub.Copilot.SDK; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Provides helpers for defining Copilot tools. |
| 11 | +/// </summary> |
| 12 | +public static class CopilotTool |
| 13 | +{ |
| 14 | + /// <summary>The key used in <see cref="AITool.AdditionalProperties"/> to indicate that a tool intentionally overrides a built-in Copilot tool with the same name.</summary> |
| 15 | + internal const string OverridesBuiltInToolKey = "is_override"; |
| 16 | + |
| 17 | + /// <summary>The key used in <see cref="AITool.AdditionalProperties"/> to indicate that a tool can execute without a permission prompt.</summary> |
| 18 | + internal const string SkipPermissionKey = "skip_permission"; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Defines a tool for use in a <see cref="CopilotSession"/>. |
| 22 | + /// </summary> |
| 23 | + /// <param name="method">The delegate to invoke when the tool is called.</param> |
| 24 | + /// <param name="factoryOptions">The Microsoft.Extensions.AI options used to create the function.</param> |
| 25 | + /// <param name="toolOptions">Copilot-specific tool options.</param> |
| 26 | + /// <returns>An <see cref="AIFunction"/> that can be added to <see cref="SessionConfig.Tools"/> or <see cref="ResumeSessionConfig.Tools"/>.</returns> |
| 27 | + /// <remarks> |
| 28 | + /// This is a helper on top of <see cref="AIFunctionFactory.Create(Delegate, AIFunctionFactoryOptions)"/> that applies additional configuration to support |
| 29 | + /// Copilot tools, such as binding a <see cref="ToolInvocation"/> parameter and adding Copilot-specific metadata properties based on the provided |
| 30 | + /// <see cref="CopilotToolOptions"/>. Any <see cref="AIFunction"/> may be used as a Copilot tool; this helper simply provides additional conveniences |
| 31 | + /// for tools that opt in to advanced features. |
| 32 | + /// </remarks> |
| 33 | + public static AIFunction DefineTool( |
| 34 | + Delegate method, |
| 35 | + CopilotToolOptions? toolOptions = null, |
| 36 | + AIFunctionFactoryOptions? factoryOptions = null) |
| 37 | + { |
| 38 | + ArgumentNullException.ThrowIfNull(method); |
| 39 | + |
| 40 | + factoryOptions ??= new(); |
| 41 | + |
| 42 | + ApplyToolOptions(factoryOptions, toolOptions); |
| 43 | + ApplyToolInvocationBinding(factoryOptions); |
| 44 | + |
| 45 | + return AIFunctionFactory.Create(method, factoryOptions); |
| 46 | + |
| 47 | + static void ApplyToolInvocationBinding(AIFunctionFactoryOptions factoryOptions) |
| 48 | + { |
| 49 | + var configureParameterBinding = factoryOptions.ConfigureParameterBinding; |
| 50 | + factoryOptions.ConfigureParameterBinding = pi => |
| 51 | + { |
| 52 | + var bindingOptions = configureParameterBinding?.Invoke(pi) ?? default; |
| 53 | + |
| 54 | + if (bindingOptions.BindParameter is null && |
| 55 | + !bindingOptions.ExcludeFromSchema && |
| 56 | + pi.ParameterType == typeof(ToolInvocation)) |
| 57 | + { |
| 58 | + return new AIFunctionFactoryOptions.ParameterBindingOptions |
| 59 | + { |
| 60 | + ExcludeFromSchema = true, |
| 61 | + BindParameter = static (pi, arguments) => |
| 62 | + { |
| 63 | + // CopilotClient/CopilotSession attach this context object before invoking the AIFunction. |
| 64 | + if (arguments.Context is not null && |
| 65 | + arguments.Context.TryGetValue(typeof(ToolInvocation), out var invocation) && |
| 66 | + invocation is ToolInvocation toolInvocation) |
| 67 | + { |
| 68 | + return toolInvocation; |
| 69 | + } |
| 70 | + |
| 71 | + if (pi.HasDefaultValue) |
| 72 | + { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + throw new InvalidOperationException($"No {nameof(ToolInvocation)} was provided for the tool call."); |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + return bindingOptions; |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + static void ApplyToolOptions(AIFunctionFactoryOptions factoryOptions, CopilotToolOptions? toolOptions) |
| 86 | + { |
| 87 | + if (toolOptions is not null && (toolOptions.OverridesBuiltInTool || toolOptions.SkipPermission)) |
| 88 | + { |
| 89 | + Dictionary<string, object?> additionalProperties = new(StringComparer.Ordinal); |
| 90 | + if (factoryOptions.AdditionalProperties is not null) |
| 91 | + { |
| 92 | + foreach (var (key, value) in factoryOptions.AdditionalProperties) |
| 93 | + { |
| 94 | + additionalProperties[key] = value; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (toolOptions.OverridesBuiltInTool) |
| 99 | + { |
| 100 | + additionalProperties[OverridesBuiltInToolKey] = true; |
| 101 | + } |
| 102 | + |
| 103 | + if (toolOptions.SkipPermission) |
| 104 | + { |
| 105 | + additionalProperties[SkipPermissionKey] = true; |
| 106 | + } |
| 107 | + |
| 108 | + factoryOptions.AdditionalProperties = additionalProperties; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +/// <summary> |
| 116 | +/// Copilot-specific options for tools defined with <see cref="CopilotTool"/>. |
| 117 | +/// </summary> |
| 118 | +public sealed class CopilotToolOptions |
| 119 | +{ |
| 120 | + /// <summary> |
| 121 | + /// Gets or sets a value indicating whether this tool intentionally overrides a built-in Copilot tool with the same name. |
| 122 | + /// </summary> |
| 123 | + /// <remarks> |
| 124 | + /// When a <see cref="CopilotToolOptions"/> with <see cref="OverridesBuiltInTool"/> set to true is used to define a tool, |
| 125 | + /// the resulting <see cref="AIFunction"/> will include "is_override": true in its <see cref="AITool.AdditionalProperties"/>. |
| 126 | + /// </remarks> |
| 127 | + public bool OverridesBuiltInTool { get; set; } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Gets or sets a value indicating whether this tool can execute without a permission prompt. |
| 131 | + /// </summary> |
| 132 | + /// <remarks> |
| 133 | + /// When a <see cref="CopilotToolOptions"/> with <see cref="SkipPermission"/> set to true is used to define a tool, |
| 134 | + /// the resulting <see cref="AIFunction"/> will include "skip_permission": true in its <see cref="AITool.AdditionalProperties"/>. |
| 135 | + /// </remarks> |
| 136 | + public bool SkipPermission { get; set; } |
| 137 | +} |
0 commit comments