427 lines
12 KiB
C#
Executable File
427 lines
12 KiB
C#
Executable File
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using MarketAlly.AIPlugin;
|
|
|
|
namespace Test.Context;
|
|
|
|
/// <summary>
|
|
/// Console application demonstrating the Context Management Suite
|
|
/// Shows how to maintain conversation continuity across chat sessions
|
|
/// </summary>
|
|
class Program
|
|
{
|
|
static async Task<int> Main(string[] args)
|
|
{
|
|
var host = CreateHost();
|
|
var service = host.Services.GetRequiredService<ContextExampleService>();
|
|
|
|
try
|
|
{
|
|
ShowWelcome();
|
|
|
|
// If command line arguments provided, execute them first
|
|
if (args.Length > 0)
|
|
{
|
|
await ExecuteCommand(args, service);
|
|
Console.WriteLine();
|
|
}
|
|
|
|
// Start interactive loop
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
Console.Write("Context> ");
|
|
var input = Console.ReadLine();
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
continue;
|
|
|
|
if (input.Trim() == "/exit" || input.Trim() == "exit")
|
|
{
|
|
Console.WriteLine("[INFO] Ending session...");
|
|
await service.EndSessionAsync("Interactive context management session completed");
|
|
Console.WriteLine("[INFO] Goodbye!");
|
|
break;
|
|
}
|
|
|
|
if (input.Trim() == "/help" || input.Trim() == "help")
|
|
{
|
|
ShowHelp();
|
|
continue;
|
|
}
|
|
|
|
var commandArgs = ParseInput(input);
|
|
if (commandArgs.Length == 0)
|
|
continue;
|
|
|
|
await ExecuteCommand(commandArgs, service);
|
|
Console.WriteLine();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[ERROR] Command error: {ex.Message}");
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[ERROR] Application error: {ex.Message}");
|
|
var logger = host.Services.GetRequiredService<ILogger<Program>>();
|
|
logger.LogError(ex, "Application failed");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
private static async Task ExecuteCommand(string[] args, ContextExampleService service)
|
|
{
|
|
if (args.Length == 0)
|
|
{
|
|
ShowHelp();
|
|
return;
|
|
}
|
|
|
|
var command = args[0].ToLower();
|
|
|
|
switch (command)
|
|
{
|
|
case "init":
|
|
case "initialize":
|
|
await HandleInitializeCommand(args, service);
|
|
break;
|
|
|
|
case "store":
|
|
await HandleStoreCommand(args, service);
|
|
break;
|
|
|
|
case "search":
|
|
case "find":
|
|
await HandleSearchCommand(args, service);
|
|
break;
|
|
|
|
case "retrieve":
|
|
case "context":
|
|
await HandleRetrieveCommand(args, service);
|
|
break;
|
|
|
|
case "summary":
|
|
case "summarize":
|
|
await HandleSummaryCommand(args, service);
|
|
break;
|
|
|
|
case "project":
|
|
await HandleProjectContextCommand(args, service);
|
|
break;
|
|
|
|
case "demo":
|
|
await service.RunContextDemoAsync();
|
|
break;
|
|
|
|
case "test":
|
|
await service.TestAllContextPluginsAsync();
|
|
break;
|
|
|
|
case "claude":
|
|
case "chat":
|
|
await HandleClaudeChatCommand(args, service);
|
|
break;
|
|
|
|
case "claude-start":
|
|
await HandleClaudeStartCommand(args, service);
|
|
break;
|
|
|
|
case "claude-interactive":
|
|
await service.StartInteractiveClaudeChatAsync();
|
|
break;
|
|
|
|
case "clear":
|
|
await service.ClearContextAsync();
|
|
break;
|
|
|
|
case "list":
|
|
await service.ListStoredContextAsync();
|
|
break;
|
|
|
|
case "help":
|
|
ShowHelp();
|
|
break;
|
|
|
|
default:
|
|
Console.WriteLine($"[ERROR] Unknown command: {command}");
|
|
ShowHelp();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private static async Task HandleInitializeCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string topic = GetValueFromArgs(args, "--topic", null);
|
|
string projectPath = GetValueFromArgs(args, "--project", null);
|
|
|
|
if (string.IsNullOrEmpty(topic))
|
|
{
|
|
Console.Write("Enter session topic: ");
|
|
topic = Console.ReadLine()?.Trim();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(projectPath))
|
|
{
|
|
Console.Write("Enter project path (or press Enter for current directory): ");
|
|
projectPath = Console.ReadLine()?.Trim();
|
|
if (string.IsNullOrEmpty(projectPath))
|
|
projectPath = Directory.GetCurrentDirectory();
|
|
}
|
|
|
|
await service.InitializeSessionAsync(topic, projectPath);
|
|
}
|
|
|
|
private static async Task HandleStoreCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string type = GetValueFromArgs(args, "--type", "conversation");
|
|
string summary = GetValueFromArgs(args, "--summary", null);
|
|
string content = GetValueFromArgs(args, "--content", null);
|
|
string priority = GetValueFromArgs(args, "--priority", "medium");
|
|
string tags = GetValueFromArgs(args, "--tags", null);
|
|
|
|
if (string.IsNullOrEmpty(summary))
|
|
{
|
|
Console.Write("Enter summary: ");
|
|
summary = Console.ReadLine()?.Trim();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(content))
|
|
{
|
|
Console.Write("Enter content to store: ");
|
|
content = Console.ReadLine()?.Trim();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(summary) || string.IsNullOrEmpty(content))
|
|
{
|
|
Console.WriteLine("[ERROR] Both summary and content are required");
|
|
return;
|
|
}
|
|
|
|
await service.StoreDecisionAsync(type, summary, content, priority, tags);
|
|
}
|
|
|
|
private static async Task HandleSearchCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string query = GetValueFromArgs(args, "--query", null);
|
|
string contextType = GetValueFromArgs(args, "--type", "all");
|
|
int maxResults = int.Parse(GetValueFromArgs(args, "--max", "5"));
|
|
|
|
if (string.IsNullOrEmpty(query))
|
|
{
|
|
Console.Write("Enter search query: ");
|
|
query = Console.ReadLine()?.Trim();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(query))
|
|
{
|
|
Console.WriteLine("[ERROR] Search query is required");
|
|
return;
|
|
}
|
|
|
|
await service.SearchContextAsync(query, contextType, maxResults);
|
|
}
|
|
|
|
private static async Task HandleRetrieveCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string contextType = GetValueFromArgs(args, "--type", "all");
|
|
string projectPath = GetValueFromArgs(args, "--project", null);
|
|
|
|
await service.RetrieveContextAsync(contextType, projectPath);
|
|
}
|
|
|
|
private static async Task HandleSummaryCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string summary = GetValueFromArgs(args, "--summary", null);
|
|
string topic = GetValueFromArgs(args, "--topic", null);
|
|
|
|
if (string.IsNullOrEmpty(summary))
|
|
{
|
|
Console.Write("Enter session summary: ");
|
|
summary = Console.ReadLine()?.Trim();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(summary))
|
|
{
|
|
Console.WriteLine("[ERROR] Session summary is required");
|
|
return;
|
|
}
|
|
|
|
await service.SummarizeSessionAsync(summary, topic);
|
|
}
|
|
|
|
private static async Task HandleProjectContextCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string projectPath = GetValueFromArgs(args, "--project", null);
|
|
|
|
if (string.IsNullOrEmpty(projectPath))
|
|
{
|
|
Console.Write("Enter project path (or press Enter for current directory): ");
|
|
projectPath = Console.ReadLine()?.Trim();
|
|
if (string.IsNullOrEmpty(projectPath))
|
|
projectPath = Directory.GetCurrentDirectory();
|
|
}
|
|
|
|
await service.GetProjectContextAsync(projectPath);
|
|
}
|
|
|
|
private static async Task HandleClaudeChatCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string topic = GetValueFromArgs(args, "--topic", null);
|
|
string projectPath = GetValueFromArgs(args, "--project", null);
|
|
|
|
if (string.IsNullOrEmpty(topic))
|
|
{
|
|
Console.Write("Enter conversation topic: ");
|
|
topic = Console.ReadLine()?.Trim();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(projectPath))
|
|
{
|
|
Console.Write("Enter project path (or press Enter for current directory): ");
|
|
projectPath = Console.ReadLine()?.Trim();
|
|
if (string.IsNullOrEmpty(projectPath))
|
|
projectPath = Directory.GetCurrentDirectory();
|
|
}
|
|
|
|
await service.StartClaudeConversationAsync(topic, projectPath);
|
|
}
|
|
|
|
private static async Task HandleClaudeStartCommand(string[] args, ContextExampleService service)
|
|
{
|
|
string topic = GetValueFromArgs(args, "--topic", "General development discussion");
|
|
string projectPath = GetValueFromArgs(args, "--project", Directory.GetCurrentDirectory());
|
|
|
|
await service.StartClaudeConversationAsync(topic, projectPath);
|
|
}
|
|
|
|
private static string GetValueFromArgs(string[] args, string flag, string defaultValue)
|
|
{
|
|
var index = Array.IndexOf(args, flag);
|
|
return (index >= 0 && index + 1 < args.Length) ? args[index + 1] : defaultValue;
|
|
}
|
|
|
|
private static string[] ParseInput(string input)
|
|
{
|
|
var parts = new List<string>();
|
|
var current = new System.Text.StringBuilder();
|
|
bool inQuotes = false;
|
|
|
|
foreach (char c in input)
|
|
{
|
|
if (c == '"')
|
|
{
|
|
inQuotes = !inQuotes;
|
|
continue;
|
|
}
|
|
|
|
if (char.IsWhiteSpace(c) && !inQuotes)
|
|
{
|
|
if (current.Length > 0)
|
|
{
|
|
parts.Add(current.ToString());
|
|
current.Clear();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
current.Append(c);
|
|
}
|
|
}
|
|
|
|
if (current.Length > 0)
|
|
{
|
|
parts.Add(current.ToString());
|
|
}
|
|
|
|
return parts.ToArray();
|
|
}
|
|
|
|
private static void ShowWelcome()
|
|
{
|
|
Console.WriteLine("MarketAlly AI Plugin - Context Management Suite");
|
|
Console.WriteLine("=" + new string('=', 49));
|
|
Console.WriteLine();
|
|
Console.WriteLine("Maintain conversation continuity across chat sessions");
|
|
Console.WriteLine("Type 'help' for commands or 'exit' to quit");
|
|
Console.WriteLine();
|
|
}
|
|
|
|
private static void ShowHelp()
|
|
{
|
|
Console.WriteLine();
|
|
Console.WriteLine("Context Management Commands:");
|
|
Console.WriteLine(" init [--topic <topic>] [--project <path>] Initialize conversation session");
|
|
Console.WriteLine(" store [--type <type>] [--summary <text>] Store important information");
|
|
Console.WriteLine(" [--content <text>] [--priority <level>]");
|
|
Console.WriteLine(" [--tags <tags>]");
|
|
Console.WriteLine(" search [--query <query>] [--type <type>] Search stored context");
|
|
Console.WriteLine(" [--max <number>]");
|
|
Console.WriteLine(" retrieve [--type <type>] [--project <path>] Retrieve existing context");
|
|
Console.WriteLine(" project [--project <path>] Get comprehensive project context");
|
|
Console.WriteLine(" summary [--summary <text>] [--topic <topic>] End session with summary");
|
|
Console.WriteLine(" demo Run comprehensive demo");
|
|
Console.WriteLine(" test Test all context plugins");
|
|
Console.WriteLine(" claude-interactive Start interactive Claude chat");
|
|
Console.WriteLine(" claude [--topic <topic>] [--project <path>] Start Claude conversation with context");
|
|
Console.WriteLine(" list List all stored context");
|
|
Console.WriteLine(" clear Clear context storage");
|
|
Console.WriteLine(" help Show this help");
|
|
Console.WriteLine(" exit Exit application");
|
|
Console.WriteLine();
|
|
Console.WriteLine("Context Types:");
|
|
Console.WriteLine(" conversation General conversation notes");
|
|
Console.WriteLine(" decision Important decisions made");
|
|
Console.WriteLine(" codechange Code modifications and refactoring");
|
|
Console.WriteLine(" insight Key insights and discoveries");
|
|
Console.WriteLine(" milestone Session markers and summaries");
|
|
Console.WriteLine();
|
|
Console.WriteLine("Priority Levels:");
|
|
Console.WriteLine(" low, medium, high, critical");
|
|
Console.WriteLine();
|
|
Console.WriteLine("Examples:");
|
|
Console.WriteLine(" init --topic \"API refactoring\" --project \".\"");
|
|
Console.WriteLine(" store --type decision --summary \"Use JWT auth\" --content \"Decided to implement JWT...\"");
|
|
Console.WriteLine(" search --query \"authentication security\" --type decision");
|
|
Console.WriteLine(" claude-interactive (Full Claude chat with context)");
|
|
Console.WriteLine(" claude --topic \"Database optimization\" (Start Claude conversation)");
|
|
Console.WriteLine(" retrieve --type all");
|
|
Console.WriteLine(" summary --summary \"Completed API security review\"");
|
|
Console.WriteLine();
|
|
}
|
|
|
|
private static IHost CreateHost()
|
|
{
|
|
var builder = Host.CreateDefaultBuilder()
|
|
.ConfigureAppConfiguration((context, config) =>
|
|
{
|
|
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
|
config.AddUserSecrets<Program>();
|
|
config.AddEnvironmentVariables();
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
services.AddHttpClient();
|
|
services.AddLogging(builder =>
|
|
{
|
|
builder.AddConsole();
|
|
builder.SetMinimumLevel(LogLevel.Information);
|
|
});
|
|
services.AddSingleton<AIPluginRegistry>();
|
|
services.AddTransient<ContextExampleService>();
|
|
services.AddTransient<ContextClaudeService>();
|
|
services.Configure<Claude4Settings>(
|
|
context.Configuration.GetSection("ClaudeSettings"));
|
|
});
|
|
|
|
return builder.Build();
|
|
}
|
|
} |