mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-18 10:39:07 +00:00
Keep one unreadable page from failing the whole web search
This commit is contained in:
2 files changed
+35
-2
No files matched your search
+10
-1
@@ -4,6 +4,8 @@ namespace AIStudio.Tools.ToolCallingSystem.ToolCallingImplementations.WebSearch;
|
|||||||
|
|
||||||
internal sealed class WebSearchResultRetrievalService(WebPageRetrievalService webPageRetrievalService)
|
internal sealed class WebSearchResultRetrievalService(WebPageRetrievalService webPageRetrievalService)
|
||||||
{
|
{
|
||||||
|
private static readonly ILogger<WebSearchResultRetrievalService> LOGGER = Program.LOGGER_FACTORY.CreateLogger<WebSearchResultRetrievalService>();
|
||||||
|
|
||||||
private const int MAX_PARALLEL_RETRIEVALS = 4;
|
private const int MAX_PARALLEL_RETRIEVALS = 4;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -110,9 +112,16 @@ internal sealed class WebSearchResultRetrievalService(WebPageRetrievalService we
|
|||||||
Interlocked.Increment(ref counters.PageTimedOut);
|
Interlocked.Increment(ref counters.PageTimedOut);
|
||||||
return new(candidate, null, WebSearchPageRetrievalOutcome.PAGE_TIMED_OUT);
|
return new(candidate, null, WebSearchPageRetrievalOutcome.PAGE_TIMED_OUT);
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException)
|
catch (InvalidOperationException exception)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
// The only outcome here which is not an expected one: a page was blocked on purpose,
|
||||||
|
// and a timeout is a limit the user set, but this is something going wrong. It is
|
||||||
|
// logged rather than only counted, because a search which quietly returns one result
|
||||||
|
// fewer is a search nobody can tell was incomplete.
|
||||||
|
//
|
||||||
Interlocked.Increment(ref counters.Failed);
|
Interlocked.Increment(ref counters.Failed);
|
||||||
|
LOGGER.LogError(exception, "Reading a search result page failed. Url={Url}", candidate.RetrievalUrl);
|
||||||
return new(candidate, null, WebSearchPageRetrievalOutcome.FAILED);
|
return new(candidate, null, WebSearchPageRetrievalOutcome.FAILED);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ internal static class WebPageContentExtractor
|
|||||||
.Select(x => LimitLength(x, MAX_OUTLINE_ITEM_CHARACTERS))
|
.Select(x => LimitLength(x, MAX_OUTLINE_ITEM_CHARACTERS))
|
||||||
.Distinct(StringComparer.Ordinal)
|
.Distinct(StringComparer.Ordinal)
|
||||||
.ToList();
|
.ToList();
|
||||||
var markdown = HTMLParser.ParseToMarkdown(contentRoot.InnerHtml)
|
var markdown = ConvertToMarkdown(contentRoot.InnerHtml, finalUrl)
|
||||||
.Replace("\r\n", "\n", StringComparison.Ordinal)
|
.Replace("\r\n", "\n", StringComparison.Ordinal)
|
||||||
.Replace('\r', '\n')
|
.Replace('\r', '\n')
|
||||||
.Trim();
|
.Trim();
|
||||||
@@ -141,6 +141,30 @@ internal static class WebPageContentExtractor
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the readable part of the page to Markdown.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Only the call into the Markdown library is wrapped, not the extraction around it: a fault of
|
||||||
|
/// our own has to keep surfacing as what it is, instead of being filed away as an unreadable
|
||||||
|
/// page.<br/><br/>
|
||||||
|
/// What the library throws depends on the HTML it was handed, and it says nothing beyond "this
|
||||||
|
/// page could not be converted". Reported as an InvalidOperationException, the retrieval treats
|
||||||
|
/// it like any other page it could not read, which costs this one page rather than the whole
|
||||||
|
/// search it belongs to.
|
||||||
|
/// </remarks>
|
||||||
|
private static string ConvertToMarkdown(string html, Uri finalUrl)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return HTMLParser.ParseToMarkdown(html);
|
||||||
|
}
|
||||||
|
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Converting the HTML of '{finalUrl}' to Markdown failed: {exception.Message}", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static JsonLdMetadata ExtractJsonLdMetadata(HtmlDocument document, Uri finalUrl)
|
private static JsonLdMetadata ExtractJsonLdMetadata(HtmlDocument document, Uri finalUrl)
|
||||||
{
|
{
|
||||||
JsonLdCandidate? bestCandidate = null;
|
JsonLdCandidate? bestCandidate = null;
|
||||||
|
|||||||
Reference in new issue
Block a user