| | | 1 | | using System.Collections.Generic; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Copilot; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// The structured result of a Copilot web search, including the answer text, |
| | | 7 | | /// source citations, and the search queries that were performed. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// <see cref="ToString"/> returns <see cref="Text"/> so that existing agent |
| | | 12 | | /// tool flows (which stringify tool results) continue to work unchanged. |
| | | 13 | | /// </para> |
| | | 14 | | /// <para> |
| | | 15 | | /// Host code that needs citation or search query metadata can inspect |
| | | 16 | | /// <see cref="Citations"/> and <see cref="SearchQueries"/> directly. |
| | | 17 | | /// </para> |
| | | 18 | | /// </remarks> |
| | | 19 | | /// <example> |
| | | 20 | | /// <code> |
| | | 21 | | /// var tools = CopilotToolSet.Create(opts => opts.EnableWebSearch = true); |
| | | 22 | | /// var webSearch = tools[0]; |
| | | 23 | | /// var result = await webSearch.InvokeAsync(new AIFunctionArguments( |
| | | 24 | | /// new Dictionary<string, object?> { ["query"] = "latest .NET version" })); |
| | | 25 | | /// |
| | | 26 | | /// if (result is WebSearchResult searchResult) |
| | | 27 | | /// { |
| | | 28 | | /// Console.WriteLine(searchResult.Text); |
| | | 29 | | /// foreach (var citation in searchResult.Citations) |
| | | 30 | | /// { |
| | | 31 | | /// Console.WriteLine($" [{citation.Title}]({citation.Url})"); |
| | | 32 | | /// } |
| | | 33 | | /// } |
| | | 34 | | /// </code> |
| | | 35 | | /// </example> |
| | | 36 | | public sealed class WebSearchResult |
| | | 37 | | { |
| | 16 | 38 | | internal WebSearchResult( |
| | 16 | 39 | | string text, |
| | 16 | 40 | | IReadOnlyList<WebSearchCitation> citations, |
| | 16 | 41 | | IReadOnlyList<WebSearchQuery> searchQueries) |
| | | 42 | | { |
| | 16 | 43 | | Text = text; |
| | 16 | 44 | | Citations = citations; |
| | 16 | 45 | | SearchQueries = searchQueries; |
| | 16 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The answer text generated by the web search. May contain |
| | | 50 | | /// <c>【3:N†source】</c> citation markers whose positions are |
| | | 51 | | /// described by <see cref="Citations"/>. |
| | | 52 | | /// </summary> |
| | 24 | 53 | | public string Text { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Source citations extracted from the search result. Each citation maps a |
| | | 57 | | /// character span in <see cref="Text"/> to a source URL. Empty when the |
| | | 58 | | /// API did not return any annotations. |
| | | 59 | | /// </summary> |
| | 24 | 60 | | public IReadOnlyList<WebSearchCitation> Citations { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// The Bing search queries that the Copilot web search tool performed |
| | | 64 | | /// while generating the answer. Empty when no web searches were triggered. |
| | | 65 | | /// </summary> |
| | 13 | 66 | | public IReadOnlyList<WebSearchQuery> SearchQueries { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Returns <see cref="Text"/> so that agent tool flows that stringify |
| | | 70 | | /// results continue to receive the answer content. |
| | | 71 | | /// </summary> |
| | 2 | 72 | | public override string ToString() => Text; |
| | | 73 | | } |