viewengine.console/Program.cs

227 lines
9.3 KiB
C#
Executable File

using ViewEngine.Client;
using ViewEngine.Client.Models;
namespace ViewEngine.Console;
/// <summary>
/// Demo console application showing how to use the ViewEngine.Client library
/// </summary>
class Program
{
static async Task Main(string[] args)
{
System.Console.WriteLine("╔══════════════════════════════════════════════════════════╗");
System.Console.WriteLine("║ ViewEngine Client Library Demo ║");
System.Console.WriteLine("║ Demonstrates using ViewEngine.Client NuGet package ║");
System.Console.WriteLine("╚══════════════════════════════════════════════════════════╝");
System.Console.WriteLine();
// Get API key from command line or prompt
string? apiKey;
if (args.Length > 0)
{
apiKey = args[0];
}
else
{
System.Console.Write("Enter your API key: ");
apiKey = System.Console.ReadLine();
}
if (string.IsNullOrWhiteSpace(apiKey))
{
System.Console.WriteLine("❌ Error: API key is required");
System.Console.WriteLine();
System.Console.WriteLine("Usage:");
System.Console.WriteLine(" dotnet run <api-key>");
System.Console.WriteLine(" OR");
System.Console.WriteLine(" dotnet run (you'll be prompted for the API key)");
return;
}
try
{
await RunDemoAsync(apiKey);
}
catch (Exception ex)
{
System.Console.WriteLine($"❌ Error: {ex.Message}");
System.Console.WriteLine();
System.Console.WriteLine("Stack trace:");
System.Console.WriteLine(ex.StackTrace);
}
System.Console.WriteLine();
System.Console.WriteLine("Press any key to exit...");
System.Console.ReadKey();
}
static async Task RunDemoAsync(string apiKey)
{
// Create the ViewEngine client
using var client = new ViewEngineClient(apiKey);
System.Console.WriteLine("🔍 Step 1: Discovering available MCP tools...");
System.Console.WriteLine();
try
{
var tools = await client.ListToolsAsync();
System.Console.WriteLine($"✅ Tools response received");
System.Console.WriteLine($" {tools}");
}
catch (Exception ex)
{
System.Console.WriteLine($"⚠️ Could not list tools: {ex.Message}");
}
System.Console.WriteLine();
System.Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.Console.WriteLine();
// Get URL to retrieve
System.Console.Write("Enter a URL to retrieve (or press Enter for example.com): ");
var url = System.Console.ReadLine();
if (string.IsNullOrWhiteSpace(url))
{
url = "https://example.com";
}
System.Console.WriteLine();
System.Console.Write("Force fresh retrieval? (y/n, default: n - use cache if available): ");
var forceRefreshInput = System.Console.ReadLine()?.ToLower();
var forceRefresh = forceRefreshInput == "y";
System.Console.WriteLine();
System.Console.Write("Generate AI summary? (y/n, default: n): ");
var summaryInput = System.Console.ReadLine()?.ToLower();
var generateSummary = summaryInput == "y";
System.Console.WriteLine();
System.Console.WriteLine($"🌐 Step 2: Submitting retrieval request for {url}...");
if (forceRefresh)
{
System.Console.WriteLine(" (Forcing fresh retrieval, bypassing cache)");
}
if (generateSummary)
{
System.Console.WriteLine(" (AI summary will be generated)");
}
System.Console.WriteLine();
// Submit the retrieval request
var request = new SubmitRetrievalRequest
{
Url = url,
ForceRefresh = forceRefresh,
GenerateSummary = generateSummary,
TimeoutSeconds = 60,
Priority = 5
};
var submitResponse = await client.SubmitRetrievalAsync(request);
System.Console.WriteLine($"✅ Request submitted successfully!");
System.Console.WriteLine($" Request ID: {submitResponse.RequestId}");
System.Console.WriteLine($" Status: {submitResponse.Status}");
System.Console.WriteLine($" Estimated wait: {submitResponse.EstimatedWaitTimeSeconds}s");
System.Console.WriteLine();
System.Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.Console.WriteLine();
System.Console.WriteLine("⏳ Step 3: Polling for results...");
System.Console.WriteLine();
// Poll for completion
RetrievalStatusResponse status;
int attempt = 0;
const int maxAttempts = 60;
do
{
attempt++;
await Task.Delay(2000);
status = await client.GetRetrievalStatusAsync(submitResponse.RequestId);
System.Console.WriteLine($" [{attempt}/{maxAttempts}] Status: {status.Status} - {status.Message}");
if (status.Status == "failed" || status.Status == "canceled")
{
System.Console.WriteLine($" Error: {status.Error}");
return;
}
} while (status.Status != "complete" && attempt < maxAttempts);
if (status.Status != "complete")
{
System.Console.WriteLine("⚠️ Timeout: Maximum polling attempts reached");
return;
}
System.Console.WriteLine();
System.Console.WriteLine($"✅ Retrieval completed!");
System.Console.WriteLine($" URL: {status.Url}");
System.Console.WriteLine($" Completed at: {status.CompletedAt}");
// Download the page content
System.Console.WriteLine();
System.Console.WriteLine("⬇️ Step 4: Downloading page content...");
var pageData = await client.GetPageContentAsync(submitResponse.RequestId);
System.Console.WriteLine();
System.Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.Console.WriteLine("📄 Page Content:");
System.Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.Console.WriteLine($" Title: {pageData.Title}");
System.Console.WriteLine($" URL: {pageData.Url}");
System.Console.WriteLine($" Meta Description: {pageData.MetaDescription ?? "(none)"}");
System.Console.WriteLine($" Favicon: {pageData.FaviconUrl ?? "(none)"}");
System.Console.WriteLine($" Routes: {pageData.Routes?.Count ?? 0} links");
System.Console.WriteLine($" Body Routes: {pageData.BodyRoutes?.Count ?? 0} links");
// Display HTTP status code (new feature!)
if (pageData.HttpStatusCode.HasValue)
{
System.Console.WriteLine($" HTTP Status: {pageData.HttpStatusCode}");
if (!pageData.IsSuccess)
{
System.Console.WriteLine($" ⚠️ Warning: Page returned error status");
System.Console.WriteLine($" IsClientError (4xx): {pageData.IsClientError}");
System.Console.WriteLine($" IsServerError (5xx): {pageData.IsServerError}");
}
}
if (!string.IsNullOrEmpty(pageData.Summary))
{
System.Console.WriteLine();
System.Console.WriteLine("🤖 AI Summary:");
System.Console.WriteLine($" {pageData.Summary}");
}
System.Console.WriteLine();
System.Console.WriteLine("📝 Body preview (first 300 chars):");
var bodyPreview = pageData.Body?.Length > 300
? pageData.Body.Substring(0, 300) + "..."
: pageData.Body ?? "(empty)";
System.Console.WriteLine($" {bodyPreview}");
if (pageData.Routes?.Count > 0)
{
System.Console.WriteLine();
System.Console.WriteLine("🔗 Top 5 Routes:");
foreach (var route in pageData.Routes.Take(5))
{
var adFlag = route.IsPotentialAd ? " [AD]" : "";
System.Console.WriteLine($" • {route.Text}: {route.Url}{adFlag}");
}
}
System.Console.WriteLine();
System.Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.Console.WriteLine();
System.Console.WriteLine("✅ Demo completed successfully!");
}
}