| | | 1 | | using Microsoft.Extensions.AI; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Copilot; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Configuration for which Copilot-backed tools to enable. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class CopilotToolSetOptions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Enable the Copilot <c>web_search</c> tool (calls the GitHub MCP server). |
| | | 12 | | /// Defaults to <c>false</c>. |
| | | 13 | | /// </summary> |
| | | 14 | | public bool EnableWebSearch { get; set; } |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Factory for creating Copilot-backed <see cref="AIFunction"/> instances. |
| | | 19 | | /// Use with Needlr's agent framework tool sets or pass directly to |
| | | 20 | | /// <see cref="ChatOptions.Tools"/>. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <example> |
| | | 23 | | /// <code> |
| | | 24 | | /// // Config-driven tool toggle: |
| | | 25 | | /// var tools = CopilotToolSet.Create(new CopilotToolSetOptions { EnableWebSearch = true }); |
| | | 26 | | /// |
| | | 27 | | /// // With callback: |
| | | 28 | | /// var tools = CopilotToolSet.Create(t => t.EnableWebSearch = true); |
| | | 29 | | /// </code> |
| | | 30 | | /// </example> |
| | | 31 | | public static class CopilotToolSet |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates Copilot-backed <see cref="AIFunction"/> instances based on the options. |
| | | 35 | | /// The returned functions call the GitHub Copilot MCP server using the provided |
| | | 36 | | /// OAuth token provider. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="oauthProvider">Provides the GitHub OAuth token for MCP authentication.</param> |
| | | 39 | | /// <param name="toolOptions">Controls which tools are enabled.</param> |
| | | 40 | | /// <param name="clientOptions">Optional Copilot API configuration.</param> |
| | | 41 | | /// <returns>A list of enabled <see cref="AIFunction"/> instances.</returns> |
| | | 42 | | public static IReadOnlyList<AIFunction> Create( |
| | | 43 | | IGitHubOAuthTokenProvider oauthProvider, |
| | | 44 | | CopilotToolSetOptions toolOptions, |
| | | 45 | | CopilotChatClientOptions? clientOptions = null) |
| | | 46 | | { |
| | 2 | 47 | | ArgumentNullException.ThrowIfNull(oauthProvider); |
| | 2 | 48 | | ArgumentNullException.ThrowIfNull(toolOptions); |
| | | 49 | | |
| | 2 | 50 | | var functions = new List<AIFunction>(); |
| | | 51 | | |
| | 2 | 52 | | if (toolOptions.EnableWebSearch) |
| | | 53 | | { |
| | 1 | 54 | | var mcpClient = new CopilotMcpToolClient(oauthProvider, clientOptions); |
| | 1 | 55 | | functions.Add(new CopilotWebSearchFunction(mcpClient)); |
| | | 56 | | } |
| | | 57 | | |
| | 2 | 58 | | return functions; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Creates Copilot-backed tools with automatic OAuth token discovery. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="toolOptions">Controls which tools are enabled.</param> |
| | | 65 | | /// <param name="clientOptions">Optional Copilot API configuration.</param> |
| | | 66 | | /// <returns>A list of enabled <see cref="AIFunction"/> instances.</returns> |
| | | 67 | | public static IReadOnlyList<AIFunction> Create( |
| | | 68 | | CopilotToolSetOptions toolOptions, |
| | | 69 | | CopilotChatClientOptions? clientOptions = null) |
| | | 70 | | { |
| | 2 | 71 | | var options = clientOptions ?? new CopilotChatClientOptions(); |
| | 2 | 72 | | var oauthProvider = new GitHubOAuthTokenProvider(options); |
| | 2 | 73 | | return Create(oauthProvider, toolOptions, options); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates Copilot-backed tools with a configuration callback. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="configure">Callback to configure which tools to enable.</param> |
| | | 80 | | /// <param name="clientOptions">Optional Copilot API configuration.</param> |
| | | 81 | | /// <returns>A list of enabled <see cref="AIFunction"/> instances.</returns> |
| | | 82 | | public static IReadOnlyList<AIFunction> Create( |
| | | 83 | | Action<CopilotToolSetOptions> configure, |
| | | 84 | | CopilotChatClientOptions? clientOptions = null) |
| | | 85 | | { |
| | 1 | 86 | | var toolOptions = new CopilotToolSetOptions(); |
| | 1 | 87 | | configure(toolOptions); |
| | 1 | 88 | | return Create(toolOptions, clientOptions); |
| | | 89 | | } |
| | | 90 | | } |