134 lines
4.3 KiB
C#
134 lines
4.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using WebAccess.MCP.WebCache;
|
|
|
|
namespace WebAccess.MCP.Browser;
|
|
|
|
internal sealed class BrowserPageTextSearchCoordinator(
|
|
IBrowserPageTextReader pageTextReader,
|
|
IWebCacheService webCacheService,
|
|
ILogger<BrowserPageTextSearchCoordinator> logger)
|
|
{
|
|
public async Task<string> FindPageTextAsync(
|
|
string url,
|
|
string query,
|
|
IReadOnlyList<string>? relatedTerms,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var input = ValidateInputs(url, query, relatedTerms);
|
|
var searchResult = await SearchAsync(
|
|
url,
|
|
input.Query,
|
|
input.RelatedTerms,
|
|
cancellationToken);
|
|
|
|
if (searchResult.Status is WebCacheSearchStatus.Hit
|
|
or WebCacheSearchStatus.NoMatches)
|
|
{
|
|
return FormatResult(searchResult);
|
|
}
|
|
|
|
var uri = WebCacheService.CreateCacheIdentity(url).NormalizedUrl;
|
|
var snapshot = await pageTextReader.NavigateAndReadAsync(
|
|
new Uri(uri),
|
|
cancellationToken);
|
|
var request = new WebCachePageWriteRequest(
|
|
url,
|
|
snapshot.FinalUrl,
|
|
snapshot.HttpStatusCode,
|
|
snapshot.Title,
|
|
snapshot.ContentType,
|
|
null,
|
|
snapshot.NormalizedText,
|
|
snapshot.IsComplete);
|
|
|
|
try
|
|
{
|
|
await webCacheService.StoreAsync(request, cancellationToken);
|
|
}
|
|
catch (WebCacheUnavailableException exception)
|
|
{
|
|
logger.LogWarning(
|
|
"WebCache store failed during relevance lookup ({FailureType}).",
|
|
exception.GetType().Name);
|
|
throw new PageTextSearchUnavailableException(
|
|
"The page was fetched, but its searchable snapshot could not be stored.");
|
|
}
|
|
|
|
searchResult = await SearchAsync(
|
|
url,
|
|
input.Query,
|
|
input.RelatedTerms,
|
|
cancellationToken);
|
|
if (searchResult.Status is WebCacheSearchStatus.PageNotFound
|
|
or WebCacheSearchStatus.Expired)
|
|
{
|
|
throw new PageTextSearchUnavailableException(
|
|
"The searchable page snapshot was unavailable after refresh.");
|
|
}
|
|
|
|
return FormatResult(searchResult);
|
|
}
|
|
|
|
internal static WebCacheSearchInput ValidateInputs(
|
|
string url,
|
|
string query,
|
|
IReadOnlyList<string>? relatedTerms)
|
|
{
|
|
_ = WebCacheService.CreateCacheIdentity(url);
|
|
return new WebCacheSearchInput(
|
|
WebCacheService.ValidateSearchQuery(query),
|
|
WebCacheService.NormalizeRelatedTerms(relatedTerms));
|
|
}
|
|
|
|
internal static string FormatResult(WebCacheSearchResult result)
|
|
{
|
|
if (result.Status == WebCacheSearchStatus.NoMatches || result.Hits.Count == 0)
|
|
{
|
|
return "Targeted cached search did not identify a confident location. "
|
|
+ "Use get_page_text sequentially if broader reading is needed.";
|
|
}
|
|
|
|
var hit = result.Hits[0];
|
|
var pageLocation = hit.StartPage == hit.EndPage
|
|
? $"Page {hit.StartPage}"
|
|
: $"Pages {hit.StartPage}-{hit.EndPage}";
|
|
|
|
return $"Found targeted context at {pageLocation} of {result.ChunkCount}."
|
|
+ Environment.NewLine
|
|
+ Environment.NewLine
|
|
+ hit.Context
|
|
+ Environment.NewLine
|
|
+ Environment.NewLine
|
|
+ "Use get_page_text for the identified page or page range if more context is needed.";
|
|
}
|
|
|
|
private async Task<WebCacheSearchResult> SearchAsync(
|
|
string url,
|
|
string query,
|
|
IReadOnlyList<string> relatedTerms,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
return await webCacheService.SearchFreshPageAsync(
|
|
url,
|
|
query,
|
|
relatedTerms,
|
|
cancellationToken);
|
|
}
|
|
catch (WebCacheUnavailableException exception)
|
|
{
|
|
logger.LogWarning(
|
|
"WebCache relevance lookup failed ({FailureType}).",
|
|
exception.GetType().Name);
|
|
throw new PageTextSearchUnavailableException(
|
|
"Cached page relevance search is unavailable.");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed record WebCacheSearchInput(
|
|
string Query,
|
|
IReadOnlyList<string> RelatedTerms);
|