├── config.example.json ├── OpenRecall.Library ├── Models │ ├── ActivitySnapshot.cs │ └── Activity.cs ├── Repositories │ └── IActivityRepository.cs ├── Utilities │ ├── ActiveWindowUtility.cs │ ├── AiUtility.cs │ └── ScreenshotUtility.cs ├── OpenRecall.Library.csproj ├── Configuration.cs ├── Collections │ ├── LICENSE.txt │ ├── TopNCollection.cs │ ├── ScoredValue.cs │ └── MinHeap.cs ├── Ai │ ├── AiChatBot.cs │ └── ActivityPlugin.cs └── ActivityManager.cs ├── OpenRecall.Cli ├── Migrations │ ├── 20240615205828_ActivityDescriptionVector.cs │ ├── 20240614174116_InitialMigration.cs │ ├── 20240614174116_InitialMigration.Designer.cs │ ├── DatabaseContextModelSnapshot.cs │ └── 20240615205828_ActivityDescriptionVector.Designer.cs ├── OpenRecall.Cli.csproj ├── DatabaseContext.cs ├── Repositories │ └── ActivityRepository.cs └── Program.cs ├── LICENSE.txt ├── OpenRecall.sln ├── .gitattributes ├── README.md └── .gitignore /config.example.json: -------------------------------------------------------------------------------- 1 | { 2 | "OpenAiApiKey": "sk-***********************************", 3 | "SnapshotInterval": 20000, 4 | "ActivitySnapshotThreashold": 3 5 | } 6 | -------------------------------------------------------------------------------- /OpenRecall.Library/Models/ActivitySnapshot.cs: -------------------------------------------------------------------------------- 1 | using System.Drawing; 2 | 3 | namespace OpenRecall.Library.Models 4 | { 5 | public class ActivitySnapshot 6 | { 7 | public string ActiveWindowTitle { get; set; } = string.Empty; 8 | public Bitmap? Screenshot { get; set; } 9 | public DateTime Timestamp { get; set; } = DateTime.Now; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /OpenRecall.Library/Repositories/IActivityRepository.cs: -------------------------------------------------------------------------------- 1 | using OpenRecall.Library.Models; 2 | 3 | namespace OpenRecall.Library.Repositories 4 | { 5 | public interface IActivityRepository 6 | { 7 | Task> GetActivities(); 8 | Task> GetActivitiesBetweenDates(DateTime startDate, DateTime endDate); 9 | Task> GetActivitiesBeforeDate(DateTime date); 10 | Task> GetActivitiesAfterDate(DateTime date); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /OpenRecall.Library/Utilities/ActiveWindowUtility.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | using System.Text; 3 | 4 | namespace OpenRecall.Library.Utilities 5 | { 6 | public class ActiveWindowUtility 7 | { 8 | [DllImport("user32.dll")] 9 | static extern IntPtr GetForegroundWindow(); 10 | 11 | [DllImport("user32.dll")] 12 | static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 13 | 14 | public string GetActiveWindowTitle() 15 | { 16 | IntPtr handle = GetForegroundWindow(); 17 | StringBuilder buffer = new StringBuilder(256); 18 | if (GetWindowText(handle, buffer, buffer.Capacity) > 0) 19 | { 20 | return buffer.ToString(); 21 | } 22 | 23 | return string.Empty; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /OpenRecall.Library/OpenRecall.Library.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /OpenRecall.Library/Models/Activity.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using System.ComponentModel.DataAnnotations.Schema; 3 | 4 | namespace OpenRecall.Library.Models 5 | { 6 | public class Activity 7 | { 8 | [Key] 9 | public int Id { get; set; } 10 | 11 | [NotMapped] 12 | public IList Snapshots { get; set; } = new List(); 13 | public string Description { get; set; } = string.Empty; 14 | public ReadOnlyMemory DescriptionVector { get; set; } 15 | public DateTime StartTime { get; set; } 16 | public DateTime EndTime { get; set; } 17 | 18 | public override string ToString() 19 | { 20 | return $"{StartTime.ToString("dddd, dd MMMM yyyy HH:mm:ss")} - {EndTime.ToString("dddd, dd MMMM yyyy HH:mm:ss")}: {Description}"; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Migrations/20240615205828_ActivityDescriptionVector.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | #nullable disable 4 | 5 | namespace OpenRecall.Cli.Migrations 6 | { 7 | /// 8 | public partial class ActivityDescriptionVector : Migration 9 | { 10 | /// 11 | protected override void Up(MigrationBuilder migrationBuilder) 12 | { 13 | migrationBuilder.AddColumn( 14 | name: "DescriptionVector", 15 | table: "Activities", 16 | type: "TEXT", 17 | nullable: false, 18 | defaultValue: ""); 19 | } 20 | 21 | /// 22 | protected override void Down(MigrationBuilder migrationBuilder) 23 | { 24 | migrationBuilder.DropColumn( 25 | name: "DescriptionVector", 26 | table: "Activities"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /OpenRecall.Cli/OpenRecall.Cli.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | runtime; build; native; contentfiles; analyzers; buildtransitive 18 | all 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /OpenRecall.Library/Configuration.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json; 2 | 3 | namespace OpenRecall.Library 4 | { 5 | public class Configuration 6 | { 7 | private const string ConfigurationFileName = "config.json"; 8 | 9 | public string OpenAiApiKey { get; set; } = string.Empty; 10 | public int SnapshotInterval { get; set; } 11 | public int ActivitySnapshotThreashold { get; set; } 12 | 13 | public static Configuration Load() 14 | { 15 | if (!File.Exists(ConfigurationFileName)) 16 | { 17 | throw new FileNotFoundException("Configuration file not found."); 18 | } 19 | 20 | var json = File.ReadAllText(ConfigurationFileName); 21 | var config = JsonSerializer.Deserialize(json) ?? throw new Exception("Failed to deserialize configuration file."); 22 | 23 | if (string.IsNullOrWhiteSpace(config.OpenAiApiKey)) 24 | { 25 | throw new Exception("OpenAI API key is missing."); 26 | } 27 | 28 | return config; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2024] [Amir Halloul] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OpenRecall.Library/Collections/LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE -------------------------------------------------------------------------------- /OpenRecall.Cli/DatabaseContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using OpenRecall.Library.Models; 3 | using System.Text.Json; 4 | 5 | namespace OpenRecall.Cli 6 | { 7 | internal class DatabaseContext: DbContext 8 | { 9 | public DbSet Activities { get; set; } 10 | 11 | public string DbPath { get; } 12 | 13 | public DatabaseContext() 14 | { 15 | var folder = Environment.SpecialFolder.LocalApplicationData; 16 | var path = Environment.GetFolderPath(folder); 17 | DbPath = Path.Join(path, "openrecall.db"); 18 | } 19 | 20 | protected override void OnConfiguring(DbContextOptionsBuilder options) 21 | => options.UseSqlite($"Data Source={DbPath}"); 22 | 23 | protected override void OnModelCreating(ModelBuilder modelBuilder) 24 | { 25 | var jsonSerliazerOptions = new JsonSerializerOptions 26 | { 27 | WriteIndented = false, 28 | }; 29 | 30 | modelBuilder.Entity() 31 | .Property(a => a.DescriptionVector) 32 | .HasConversion( 33 | v => JsonSerializer.Serialize(v, jsonSerliazerOptions), 34 | v => JsonSerializer.Deserialize>(v, jsonSerliazerOptions) 35 | ); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Migrations/20240614174116_InitialMigration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | #nullable disable 5 | 6 | namespace OpenRecall.Cli.Migrations 7 | { 8 | /// 9 | public partial class InitialMigration : Migration 10 | { 11 | /// 12 | protected override void Up(MigrationBuilder migrationBuilder) 13 | { 14 | migrationBuilder.CreateTable( 15 | name: "Activities", 16 | columns: table => new 17 | { 18 | Id = table.Column(type: "INTEGER", nullable: false) 19 | .Annotation("Sqlite:Autoincrement", true), 20 | Description = table.Column(type: "TEXT", nullable: false), 21 | StartTime = table.Column(type: "TEXT", nullable: false), 22 | EndTime = table.Column(type: "TEXT", nullable: false) 23 | }, 24 | constraints: table => 25 | { 26 | table.PrimaryKey("PK_Activities", x => x.Id); 27 | }); 28 | } 29 | 30 | /// 31 | protected override void Down(MigrationBuilder migrationBuilder) 32 | { 33 | migrationBuilder.DropTable( 34 | name: "Activities"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Repositories/ActivityRepository.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using OpenRecall.Library.Models; 3 | using OpenRecall.Library.Repositories; 4 | 5 | namespace OpenRecall.Cli.Repositories 6 | { 7 | internal class ActivityRepository : IActivityRepository 8 | { 9 | public async Task> GetActivities() 10 | { 11 | using var dbContext = new DatabaseContext(); 12 | return await dbContext.Activities.ToListAsync(); 13 | } 14 | 15 | public async Task> GetActivitiesAfterDate(DateTime date) 16 | { 17 | using var dbContext = new DatabaseContext(); 18 | return await dbContext.Activities.Where(a => a.StartTime > date).ToListAsync(); 19 | } 20 | 21 | public async Task> GetActivitiesBeforeDate(DateTime date) 22 | { 23 | using var dbContext = new DatabaseContext(); 24 | return await dbContext.Activities.Where(a => a.StartTime < date).ToListAsync(); 25 | } 26 | 27 | public async Task> GetActivitiesBetweenDates(DateTime startDate, DateTime endDate) 28 | { 29 | using var dbContext = new DatabaseContext(); 30 | return await dbContext.Activities.Where(a => a.StartTime > startDate && a.StartTime < endDate).ToListAsync(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Migrations/20240614174116_InitialMigration.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Migrations; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using OpenRecall.Cli; 8 | 9 | #nullable disable 10 | 11 | namespace OpenRecall.Cli.Migrations 12 | { 13 | [DbContext(typeof(DatabaseContext))] 14 | [Migration("20240614174116_InitialMigration")] 15 | partial class InitialMigration 16 | { 17 | /// 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder.HasAnnotation("ProductVersion", "8.0.6"); 22 | 23 | modelBuilder.Entity("OpenRecall.Library.Models.Activity", b => 24 | { 25 | b.Property("Id") 26 | .ValueGeneratedOnAdd() 27 | .HasColumnType("INTEGER"); 28 | 29 | b.Property("Description") 30 | .IsRequired() 31 | .HasColumnType("TEXT"); 32 | 33 | b.Property("EndTime") 34 | .HasColumnType("TEXT"); 35 | 36 | b.Property("StartTime") 37 | .HasColumnType("TEXT"); 38 | 39 | b.HasKey("Id"); 40 | 41 | b.ToTable("Activities"); 42 | }); 43 | #pragma warning restore 612, 618 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Migrations/DatabaseContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 6 | using OpenRecall.Cli; 7 | 8 | #nullable disable 9 | 10 | namespace OpenRecall.Cli.Migrations 11 | { 12 | [DbContext(typeof(DatabaseContext))] 13 | partial class DatabaseContextModelSnapshot : ModelSnapshot 14 | { 15 | protected override void BuildModel(ModelBuilder modelBuilder) 16 | { 17 | #pragma warning disable 612, 618 18 | modelBuilder.HasAnnotation("ProductVersion", "8.0.6"); 19 | 20 | modelBuilder.Entity("OpenRecall.Library.Models.Activity", b => 21 | { 22 | b.Property("Id") 23 | .ValueGeneratedOnAdd() 24 | .HasColumnType("INTEGER"); 25 | 26 | b.Property("Description") 27 | .IsRequired() 28 | .HasColumnType("TEXT"); 29 | 30 | b.Property("DescriptionVector") 31 | .IsRequired() 32 | .HasColumnType("TEXT"); 33 | 34 | b.Property("EndTime") 35 | .HasColumnType("TEXT"); 36 | 37 | b.Property("StartTime") 38 | .HasColumnType("TEXT"); 39 | 40 | b.HasKey("Id"); 41 | 42 | b.ToTable("Activities"); 43 | }); 44 | #pragma warning restore 612, 618 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /OpenRecall.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.9.34622.214 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRecall.Cli", "OpenRecall.Cli\OpenRecall.Cli.csproj", "{72BB0270-A1B7-46A6-B3E8-6FDED91DC8BD}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRecall.Library", "OpenRecall.Library\OpenRecall.Library.csproj", "{EF8E9271-39F2-4AD7-A23B-233E2171804F}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {72BB0270-A1B7-46A6-B3E8-6FDED91DC8BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {72BB0270-A1B7-46A6-B3E8-6FDED91DC8BD}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {72BB0270-A1B7-46A6-B3E8-6FDED91DC8BD}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {72BB0270-A1B7-46A6-B3E8-6FDED91DC8BD}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {EF8E9271-39F2-4AD7-A23B-233E2171804F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {EF8E9271-39F2-4AD7-A23B-233E2171804F}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {EF8E9271-39F2-4AD7-A23B-233E2171804F}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {EF8E9271-39F2-4AD7-A23B-233E2171804F}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {27B57607-4D00-42F5-A27F-774F0C5DCD8D} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Migrations/20240615205828_ActivityDescriptionVector.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Migrations; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using OpenRecall.Cli; 8 | 9 | #nullable disable 10 | 11 | namespace OpenRecall.Cli.Migrations 12 | { 13 | [DbContext(typeof(DatabaseContext))] 14 | [Migration("20240615205828_ActivityDescriptionVector")] 15 | partial class ActivityDescriptionVector 16 | { 17 | /// 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder.HasAnnotation("ProductVersion", "8.0.6"); 22 | 23 | modelBuilder.Entity("OpenRecall.Library.Models.Activity", b => 24 | { 25 | b.Property("Id") 26 | .ValueGeneratedOnAdd() 27 | .HasColumnType("INTEGER"); 28 | 29 | b.Property("Description") 30 | .IsRequired() 31 | .HasColumnType("TEXT"); 32 | 33 | b.Property("DescriptionVector") 34 | .IsRequired() 35 | .HasColumnType("TEXT"); 36 | 37 | b.Property("EndTime") 38 | .HasColumnType("TEXT"); 39 | 40 | b.Property("StartTime") 41 | .HasColumnType("TEXT"); 42 | 43 | b.HasKey("Id"); 44 | 45 | b.ToTable("Activities"); 46 | }); 47 | #pragma warning restore 612, 618 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /OpenRecall.Cli/Program.cs: -------------------------------------------------------------------------------- 1 | using OpenRecall.Cli.Repositories; 2 | using OpenRecall.Library; 3 | using OpenRecall.Library.Ai; 4 | using OpenRecall.Library.Utilities; 5 | 6 | namespace OpenRecall.Cli 7 | { 8 | internal class Program 9 | { 10 | 11 | static void Main(string[] args) 12 | { 13 | var configuration = Configuration.Load(); 14 | var aiUtility = new AiUtility(configuration.OpenAiApiKey); 15 | var activityManager = new ActivityManager(aiUtility, configuration.SnapshotInterval, configuration.ActivitySnapshotThreashold); 16 | var chatBot = new AiChatBot(new ActivityRepository(), configuration.OpenAiApiKey); 17 | 18 | activityManager.ActivityCreated += ActivityManager_ActivityCreated; 19 | 20 | activityManager.Start(); 21 | 22 | while (true) 23 | { 24 | Console.Write("You: "); 25 | var input = Console.ReadLine(); 26 | 27 | if (input == null) 28 | { 29 | continue; 30 | } 31 | 32 | if (input.Trim().ToLower() == "quit") 33 | { 34 | activityManager.Stop(); 35 | break; 36 | } else 37 | { 38 | // Get response from AI asynchronusly on a separate thread 39 | var response = chatBot.GetResponse(input).Result; 40 | Console.WriteLine($"OpenRecall AI: {response}"); 41 | } 42 | } 43 | 44 | } 45 | 46 | private static async void ActivityManager_ActivityCreated(object? sender, ActivityEventArgs e) 47 | { 48 | using var dbContext = new DatabaseContext(); 49 | using var transaction = await dbContext.Database.BeginTransactionAsync(); 50 | try 51 | { 52 | dbContext.Activities.Add(e.Activity); 53 | await dbContext.SaveChangesAsync(); 54 | await transaction.CommitAsync(); 55 | } 56 | catch (Exception ex) 57 | { 58 | Console.WriteLine(ex.ToString()); 59 | await transaction.RollbackAsync(); 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /OpenRecall.Library/Ai/AiChatBot.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using Microsoft.Extensions.Logging; 3 | using Microsoft.SemanticKernel; 4 | using Microsoft.SemanticKernel.ChatCompletion; 5 | using Microsoft.SemanticKernel.Connectors.OpenAI; 6 | using OpenRecall.Library.Repositories; 7 | 8 | #pragma warning disable SKEXP0010 9 | 10 | namespace OpenRecall.Library.Ai 11 | { 12 | public class AiChatBot 13 | { 14 | private readonly IActivityRepository _activityRepository; 15 | private readonly Kernel _kernel; 16 | private readonly ChatHistory _chatMessages = new ChatHistory(); 17 | 18 | 19 | public AiChatBot(IActivityRepository activityRepository, string openAiApiKey) 20 | { 21 | _activityRepository = activityRepository; 22 | var builder = Kernel.CreateBuilder(); 23 | builder.Services.AddOpenAIChatCompletion("gpt-4o", openAiApiKey) 24 | .AddOpenAITextEmbeddingGeneration("text-embedding-3-large", openAiApiKey) 25 | // .AddLogging(c => c.SetMinimumLevel(LogLevel.Trace).AddConsole()) 26 | .AddSingleton(activityRepository); 27 | builder.Plugins.AddFromType(); 28 | 29 | _kernel = builder.Build(); 30 | _chatMessages.AddSystemMessage($"You are OpenRecall AI, an assistant embedded in OpenRecall which is a desktop activity monitoring tool. You have access to the user's activity history and you help the user recall his old activities. Current time is {DateTime.Now}"); 31 | } 32 | 33 | public async Task GetResponse(string input) 34 | { 35 | _chatMessages.AddUserMessage(input); 36 | IChatCompletionService chatCompletionService = _kernel.GetRequiredService(); 37 | OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() 38 | { 39 | ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions, 40 | }; 41 | var result = chatCompletionService.GetStreamingChatMessageContentsAsync(_chatMessages, executionSettings: openAIPromptExecutionSettings, kernel: _kernel); 42 | 43 | string fullMessage = ""; 44 | await foreach (var content in result) 45 | { 46 | fullMessage += content.Content; 47 | } 48 | 49 | _chatMessages.AddAssistantMessage(fullMessage); 50 | return fullMessage; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /OpenRecall.Library/Utilities/AiUtility.cs: -------------------------------------------------------------------------------- 1 | using OpenAI.Chat; 2 | using OpenAI.Embeddings; 3 | using OpenRecall.Library.Models; 4 | 5 | namespace OpenRecall.Library.Utilities 6 | { 7 | public class AiUtility 8 | { 9 | private readonly ChatClient _chatClient; 10 | private readonly EmbeddingClient _embeddingClient; 11 | private readonly ScreenshotUtility _screenshotUtility = new(); 12 | 13 | public AiUtility(string apiKey) 14 | { 15 | _chatClient = new("gpt-4o", apiKey); 16 | _embeddingClient = new("text-embedding-3-large", apiKey); 17 | } 18 | 19 | public async Task SummarizeActivityAsync(Activity activity) 20 | { 21 | var activeTabNames = activity.Snapshots.Select(snapshot => snapshot.ActiveWindowTitle).Where(s => !string.IsNullOrEmpty(s)).ToList(); 22 | var imageBytes = activity.Snapshots.Where(snapshot => snapshot.Screenshot is not null).Select(snapshot => BinaryData.FromStream(_screenshotUtility.ImageToStream(_screenshotUtility.Resize(snapshot.Screenshot!, 960, 540)))).Where(data => data is not null).ToList(); 23 | 24 | List messages = 25 | [ 26 | ChatMessage.CreateSystemMessage("You are a desktop activity monitoring tool. Given a list of active windows the user had open and screenshots of them, you provide a very brief and accurate description of the user's activity."), 27 | ]; 28 | 29 | var userMessage = new UserChatMessage(ChatMessageContentPart.CreateTextMessageContentPart("Windows I had open:")); 30 | 31 | foreach (var tabName in activeTabNames) 32 | { 33 | userMessage.Content.Add(ChatMessageContentPart.CreateTextMessageContentPart($"- {tabName}")); 34 | } 35 | 36 | foreach (var image in imageBytes) 37 | { 38 | userMessage.Content.Add(ChatMessageContentPart.CreateImageMessageContentPart(image, "image/jpeg")); 39 | } 40 | 41 | messages.Add(userMessage); 42 | 43 | 44 | ChatCompletion chatCompletion = await _chatClient.CompleteChatAsync(messages, new ChatCompletionOptions 45 | { 46 | MaxTokens = 300, 47 | }); 48 | 49 | return chatCompletion.Content[0].Text; 50 | } 51 | 52 | public async Task> VectorizeStringAsync(string input) 53 | { 54 | var result = await _embeddingClient.GenerateEmbeddingAsync(input); 55 | return result.Value.Vector; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenRecall 2 | 3 | **OpenRecall** Is my attempt to recreate a similar tool to Microsoft's Recall. It uses AI to monitor and log your activity. 4 | Why would anyone use this? I don't know but please don't be evil and use it to monitor your employees or some other dystopian purpose. 5 | 6 | ## Table of Contents 7 | 8 | - [Features](#features) 9 | - [Installation](#installation) 10 | - [Usage](#usage) 11 | - [Configuration](#configuration) 12 | - [Contributing](#contributing) 13 | - [License](#license) 14 | - [Contact](#contact) 15 | 16 | ## Features 17 | 18 | - **Desktop Screenshots**: Periodically captures screenshots of your desktop. 19 | - **Active Window Monitoring**: Keeps track of your active windows. 20 | - **AI Description Generation**: Generates and stores detailed descriptions of your activities using AI. 21 | 22 | ## Installation 23 | 24 | ### Prerequisites 25 | 26 | - .NET 8.0 SDK or later 27 | - Supported OS (Currently only Windows) 28 | 29 | ### Steps 30 | 31 | 1. **Clone the Repository** 32 | ```bash 33 | git clone https://github.com/amir-halloul/OpenRecall.git 34 | ``` 35 | 2. **Navigate to the Project Directory** 36 | ```bash 37 | cd OpenRecall 38 | ``` 39 | 3. **Restore Dependencies** 40 | ```bash 41 | dotnet restore 42 | ``` 43 | 4. **Build the Project** 44 | ```bash 45 | dotnet build 46 | ``` 47 | 5. **Run the Application** 48 | ```bash 49 | dotnet run 50 | ``` 51 | 52 | ## Usage 53 | 54 | 1. **Launch OpenRecall**: Open the application by running it from the command line or your preferred development environment. 55 | 2. **Leave CLI Open**: Leave the CLI open in the background while you work. 56 | 3. **View Logs**: View your logs in the SQLite DB in %localappdata%\openrecall.db 57 | 58 | ## Configuration 59 | 60 | OpenRecall provides several configuration options to tailor its behavior to your needs: 61 | 62 | - **OpenAI API Key**: The tool uses gpt-4o so you'll need an API key to use it. You can get one [here](https://openai.com/). 63 | - **Snapshot Interval**: Set how frequently activity snapshots are taken. 64 | - **Activity Snapshot Threshold**: How many snapshots are required to trigger an activity description. 65 | 66 | Create a `config.json` file and place it at the same directory as the executable. An example configuration file is provided at `config.example.json`. 67 | 68 | ## Contributing 69 | 70 | We welcome contributions to enhance OpenRecall! To contribute, follow these steps: 71 | 72 | 1. **Fork the Repository** 73 | 2. **Create a Feature Branch** 74 | ```bash 75 | git checkout -b feature/YourFeatureName 76 | ``` 77 | 3. **Commit Your Changes** 78 | ```bash 79 | git commit -m 'Add some feature' 80 | ``` 81 | 4. **Push to the Branch** 82 | ```bash 83 | git push origin feature/YourFeatureName 84 | ``` 85 | 5. **Create a Pull Request** 86 | 87 | ## License 88 | 89 | OpenRecall is licensed under the MIT License. You can view the full license [here](LICENSE). 90 | 91 | ## Contact 92 | 93 | For questions, issues, or feature requests, please reach out to me at: 94 | 95 | - **Email**: amirhalloul@gmail.com 96 | - **GitHub Issues**: [https://github.com/amir-halloul/OpenRecall/issues](https://github.com/amir-halloul/OpenRecall/issues) 97 | 98 | Thank you for your interest in OpenRecall! 99 | -------------------------------------------------------------------------------- /OpenRecall.Library/Utilities/ScreenshotUtility.cs: -------------------------------------------------------------------------------- 1 | using System.Drawing; 2 | using System.Drawing.Drawing2D; 3 | using System.Drawing.Imaging; 4 | using System.Management; 5 | using System.Runtime.InteropServices; 6 | 7 | namespace OpenRecall.Library.Utilities 8 | { 9 | public class ScreenshotUtility 10 | { 11 | [DllImport("user32.dll")] 12 | static extern IntPtr GetForegroundWindow(); 13 | 14 | private Size GetScreenSize() 15 | { 16 | ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2"); 17 | ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_VideoController " + "Where DeviceID=\"VideoController1\""); 18 | 19 | ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 20 | 21 | ManagementObjectCollection queryCollection = searcher.Get(); 22 | 23 | foreach (ManagementObject m in queryCollection) 24 | { 25 | return new Size(int.Parse(m["CurrentHorizontalResolution"].ToString() ?? "0"), int.Parse(m["CurrentVerticalResolution"].ToString() ?? "0")); 26 | } 27 | return new Size(0, 0); 28 | } 29 | 30 | public Bitmap? TakeScreenshot() 31 | { 32 | Size screenSize = GetScreenSize(); 33 | Bitmap bmp = new Bitmap(screenSize.Width, screenSize.Height, PixelFormat.Format32bppArgb); 34 | using (Graphics g = Graphics.FromImage(bmp)) 35 | { 36 | g.CopyFromScreen(0, 0, 0, 0, screenSize); 37 | return bmp; 38 | } 39 | } 40 | 41 | public MemoryStream ImageToStream(Image image) 42 | { 43 | var stream = new MemoryStream(); 44 | ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); 45 | EncoderParameters encoderParameters = new EncoderParameters(1); 46 | EncoderParameter encoderQualityParameter = new EncoderParameter(Encoder.Quality, 75L); 47 | 48 | encoderParameters.Param[0] = encoderQualityParameter; 49 | image.Save(stream, jpgEncoder, encoderParameters); 50 | stream.Position = 0; 51 | return stream; 52 | } 53 | 54 | private ImageCodecInfo GetEncoder(ImageFormat format) 55 | { 56 | ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 57 | foreach (ImageCodecInfo codec in codecs) 58 | { 59 | if (codec.FormatID == format.Guid) 60 | { 61 | return codec; 62 | } 63 | } 64 | return null; 65 | } 66 | 67 | public Image Resize(Image originalImage, int w, int h) 68 | { 69 | //Original Image attributes 70 | int originalWidth = originalImage.Width; 71 | int originalHeight = originalImage.Height; 72 | 73 | // Figure out the ratio 74 | double ratioX = (double)w / (double)originalWidth; 75 | double ratioY = (double)h / (double)originalHeight; 76 | // use whichever multiplier is smaller 77 | double ratio = ratioX < ratioY ? ratioX : ratioY; 78 | 79 | // now we can get the new height and width 80 | int newHeight = Convert.ToInt32(originalHeight * ratio); 81 | int newWidth = Convert.ToInt32(originalWidth * ratio); 82 | 83 | Image thumbnail = new Bitmap(newWidth, newHeight); 84 | Graphics graphic = Graphics.FromImage(thumbnail); 85 | 86 | graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 87 | graphic.SmoothingMode = SmoothingMode.HighQuality; 88 | graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; 89 | graphic.CompositingQuality = CompositingQuality.HighQuality; 90 | 91 | graphic.Clear(Color.Transparent); 92 | graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight); 93 | 94 | return thumbnail; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /OpenRecall.Library/Ai/ActivityPlugin.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.SemanticKernel; 2 | using Microsoft.SemanticKernel.Embeddings; 3 | using OpenRecall.Library.Collections; 4 | using OpenRecall.Library.Models; 5 | using OpenRecall.Library.Repositories; 6 | using System.ComponentModel; 7 | using System.Numerics.Tensors; 8 | 9 | // Embedding functionality is experimental 10 | #pragma warning disable SKEXP0001 11 | 12 | namespace OpenRecall.Library.Ai 13 | { 14 | internal class ActivityPlugin 15 | { 16 | [KernelFunction] 17 | [Description("Gets a list of all activities")] 18 | public async Task> GetAllActivities(Kernel kernel) 19 | { 20 | var activityRepository = kernel.GetRequiredService(); 21 | var activities = await activityRepository.GetActivities(); 22 | return activities.Select(a => a.ToString()); 23 | } 24 | 25 | [KernelFunction] 26 | [Description("Gets a list of all activities the user performed between two datetimes")] 27 | public async Task> GetAllActivitiesBetweenDates(Kernel kernel, 28 | [Description("Start datetime")] DateTime startDateTime, 29 | [Description("End datetime")] DateTime endDateTime) 30 | { 31 | var activityRepository = kernel.GetRequiredService(); 32 | var activities = await activityRepository.GetActivitiesBetweenDates(startDateTime, endDateTime); 33 | return activities.Select(a => a.ToString()); 34 | } 35 | 36 | [KernelFunction] 37 | [Description("Gets a list of all activities the user performed before a given datetime")] 38 | public async Task> GetAllActivitiesBefore(Kernel kernel, DateTime dateTime) 39 | { 40 | var activityRepository = kernel.GetRequiredService(); 41 | var activities = await activityRepository.GetActivitiesBeforeDate(dateTime); 42 | return activities.Select(a => a.ToString()); 43 | } 44 | 45 | [KernelFunction] 46 | [Description("Gets a list of all activities the user performed after a given datetime until now")] 47 | public async Task> GetAllActivitiesAfter(Kernel kernel, DateTime dateTime) 48 | { 49 | var activityRepository = kernel.GetRequiredService(); 50 | var activities = await activityRepository.GetActivitiesAfterDate(dateTime); 51 | return activities.Select(a => a.ToString()); 52 | } 53 | 54 | [KernelFunction] 55 | [Description("Gets a list of all activities the user performed that match a given description")] 56 | public async Task> GetActivitiesByDescription(Kernel kernel, 57 | [Description("A brief description of the task to search for using semantic search")]string description, 58 | [Description("The maximum number of activities to return.")] int limit) 59 | { 60 | var activityRepository = kernel.GetRequiredService(); 61 | var activities = await activityRepository.GetActivities(); 62 | 63 | var textEmbeddingService = kernel.GetRequiredService(); 64 | 65 | var descriptionVector = await textEmbeddingService.GenerateEmbeddingAsync(description); 66 | 67 | TopNCollection topActivities = new(limit); 68 | 69 | foreach (var activity in activities) 70 | { 71 | double similarity = TensorPrimitives.CosineSimilarity(descriptionVector.Span, activity.DescriptionVector.Span); 72 | 73 | if (similarity >= 0.1) 74 | { 75 | topActivities.Add(new(activity, similarity)); 76 | } 77 | } 78 | 79 | topActivities.SortByScore(); 80 | return topActivities.Select(x => x.Value.ToString()); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /OpenRecall.Library/ActivityManager.cs: -------------------------------------------------------------------------------- 1 | using OpenRecall.Library.Models; 2 | using OpenRecall.Library.Utilities; 3 | 4 | public class ActivitySnapshotEventArgs : EventArgs 5 | { 6 | public ActivitySnapshot Snapshot { get; set; } 7 | public int SnapshotIndex { get; set; } 8 | public int SnapshotThreshold { get; set; } 9 | } 10 | 11 | public class ActivityEventArgs : EventArgs 12 | { 13 | public Activity Activity { get; set; } 14 | } 15 | 16 | public class ActivityManager 17 | { 18 | public event EventHandler ActivitySnapshotTaken; 19 | public event EventHandler ActivityCreated; 20 | 21 | private readonly ScreenshotUtility _screenshotUtility = new ScreenshotUtility(); 22 | private readonly ActiveWindowUtility _activeWindowUtility = new ActiveWindowUtility(); 23 | private readonly AiUtility _aiUtility; 24 | private readonly int _snapshotInterval; 25 | private readonly int _snapshotThreshold; 26 | 27 | private CancellationTokenSource _cancellationTokenSource; 28 | 29 | public ActivityManager(AiUtility aiUtility, int snapshotInterval, int snapshotThreshold) 30 | { 31 | _aiUtility = aiUtility; 32 | _snapshotInterval = snapshotInterval; 33 | _snapshotThreshold = snapshotThreshold; 34 | } 35 | 36 | public void Start() 37 | { 38 | if (_cancellationTokenSource != null) 39 | { 40 | throw new InvalidOperationException("ActivityManager is already running."); 41 | } 42 | 43 | _cancellationTokenSource = new CancellationTokenSource(); 44 | var cancellationToken = _cancellationTokenSource.Token; 45 | 46 | Task.Run(() => CaptureActivityLoopAsync(cancellationToken), cancellationToken); 47 | } 48 | 49 | public void Stop() 50 | { 51 | _cancellationTokenSource?.Cancel(); 52 | _cancellationTokenSource = null; 53 | } 54 | 55 | private async Task CaptureActivityLoopAsync(CancellationToken cancellationToken) 56 | { 57 | while (!cancellationToken.IsCancellationRequested) 58 | { 59 | var activity = new Activity 60 | { 61 | Snapshots = new List(), 62 | StartTime = DateTime.Now, 63 | EndTime = DateTime.Now, 64 | }; 65 | 66 | int snapshotIndex = 0; 67 | 68 | while (snapshotIndex < _snapshotThreshold && !cancellationToken.IsCancellationRequested) 69 | { 70 | var screenshot = _screenshotUtility.TakeScreenshot(); 71 | var activeWindow = _activeWindowUtility.GetActiveWindowTitle(); 72 | var activitySnapshot = new ActivitySnapshot 73 | { 74 | ActiveWindowTitle = activeWindow, 75 | Screenshot = screenshot, 76 | Timestamp = DateTime.Now 77 | }; 78 | 79 | activity.Snapshots.Add(activitySnapshot); 80 | activity.EndTime = DateTime.Now; 81 | snapshotIndex++; 82 | 83 | // Raising the ActivitySnapshotTaken event 84 | ActivitySnapshotTaken?.Invoke(this, new ActivitySnapshotEventArgs 85 | { 86 | Snapshot = activitySnapshot, 87 | SnapshotIndex = snapshotIndex, 88 | SnapshotThreshold = _snapshotThreshold 89 | }); 90 | 91 | 92 | await Task.Delay(_snapshotInterval, cancellationToken); 93 | } 94 | 95 | if (!cancellationToken.IsCancellationRequested) 96 | { 97 | activity.Description = await _aiUtility.SummarizeActivityAsync(activity); 98 | activity.DescriptionVector = await _aiUtility.VectorizeStringAsync(activity.Description); 99 | 100 | // Raising the ActivityCreated event 101 | ActivityCreated?.Invoke(this, new ActivityEventArgs { Activity = activity }); 102 | } 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /OpenRecall.Library/Collections/TopNCollection.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace OpenRecall.Library.Collections 4 | { 5 | /// 6 | /// A collector for Top N matches. Keeps only the best N matches by Score. 7 | /// Automatically flushes out any not in the top N. 8 | /// By default, items are not sorted by score until you call . 9 | /// 10 | internal sealed class TopNCollection(int maxItems) : IEnumerable> 11 | { 12 | private readonly MinHeap> _heap = new(ScoredValue.Min(), maxItems); 13 | private bool _sorted = false; 14 | 15 | /// 16 | /// Gets the maximum number of items allowed in the collection. 17 | /// 18 | public int MaxItems { get; } = maxItems; 19 | 20 | /// 21 | /// Gets the current number of items in the collection. 22 | /// 23 | public int Count => this._heap.Count; 24 | 25 | internal ScoredValue this[int i] => this._heap[i]; 26 | internal ScoredValue Top => this._heap.Top; 27 | 28 | /// 29 | /// Resets the collection, allowing it to be reused. 30 | /// 31 | public void Reset() 32 | { 33 | this._heap.Clear(); 34 | } 35 | 36 | /// 37 | /// Adds a single scored value to the collection. 38 | /// 39 | /// The scored value to add. 40 | public void Add(ScoredValue value) 41 | { 42 | if (this._sorted) 43 | { 44 | this._heap.Restore(); 45 | this._sorted = false; 46 | } 47 | 48 | if (this._heap.Count == this.MaxItems) 49 | { 50 | // Queue is full. We will need to dequeue the item with lowest weight 51 | if (value.Score <= this.Top.Score) 52 | { 53 | // This score is lower than the lowest score on the queue right now. Ignore it 54 | return; 55 | } 56 | 57 | this._heap.RemoveTop(); 58 | } 59 | 60 | this._heap.Add(value); 61 | } 62 | 63 | /// 64 | /// Adds a value with a specified score to the collection. 65 | /// 66 | /// The value to add. 67 | /// The score associated with the value. 68 | public void Add(T value, double score) 69 | { 70 | this.Add(new ScoredValue(value, score)); 71 | } 72 | 73 | /// 74 | /// Sorts the collection in descending order by score. 75 | /// 76 | public void SortByScore() 77 | { 78 | if (!this._sorted && this._heap.Count > 0) 79 | { 80 | this._heap.SortDescending(); 81 | this._sorted = true; 82 | } 83 | } 84 | 85 | /// 86 | /// Returns a list containing the scored values in the collection. 87 | /// 88 | /// A list of scored values. 89 | public IList> ToList() 90 | { 91 | var list = new List>(this.Count); 92 | for (int i = 0, count = this.Count; i < count; ++i) 93 | { 94 | list.Add(this[i]); 95 | } 96 | 97 | return list; 98 | } 99 | 100 | /// 101 | /// Returns an enumerator that iterates through the collection. 102 | /// 103 | /// An enumerator for the collection. 104 | public IEnumerator> GetEnumerator() 105 | { 106 | return this._heap.GetEnumerator(); 107 | } 108 | 109 | IEnumerator IEnumerable.GetEnumerator() 110 | { 111 | return this._heap.GetEnumerator(); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /OpenRecall.Library/Collections/ScoredValue.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | 3 | 4 | namespace OpenRecall.Library.Collections 5 | { 6 | /// 7 | /// Structure for storing data which can be scored. 8 | /// 9 | /// Data type. 10 | internal readonly struct ScoredValue(T item, double score) : IComparable>, IEquatable> 11 | { 12 | /// 13 | /// Gets the value of the scored item. 14 | /// 15 | public T Value { get; } = item; 16 | /// 17 | /// Gets the score of the item. 18 | /// 19 | public double Score { get; } = score; 20 | 21 | /// 22 | /// Compares the current instance with another instance of . 23 | /// 24 | /// The other instance of to compare with. 25 | /// A value indicating the relative order of the instances. 26 | public int CompareTo(ScoredValue other) 27 | { 28 | return this.Score.CompareTo(other.Score); 29 | } 30 | 31 | /// 32 | /// Returns a string representation of the current instance. 33 | /// 34 | /// A string representation of the current instance. 35 | public override string ToString() 36 | { 37 | return $"{this.Score}, {this.Value}"; 38 | } 39 | 40 | /// 41 | /// Converts the score of the current instance to a double. 42 | /// 43 | /// The current instance of . 44 | public static explicit operator double(ScoredValue src) 45 | { 46 | return src.Score; 47 | } 48 | 49 | /// 50 | /// Converts the value of the current instance to the specified type. 51 | /// 52 | /// The current instance of . 53 | public static explicit operator T(ScoredValue src) 54 | { 55 | return src.Value; 56 | } 57 | 58 | /// 59 | /// Converts a to a . 60 | /// 61 | /// The to convert. 62 | public static implicit operator ScoredValue(KeyValuePair src) 63 | { 64 | return new ScoredValue(src.Key, src.Value); 65 | } 66 | 67 | /// 68 | public override bool Equals([NotNullWhen(true)] object? obj) 69 | { 70 | return (obj is ScoredValue other) && this.Equals(other); 71 | } 72 | 73 | /// 74 | /// Determines whether the current instance is equal to another instance of . 75 | /// 76 | /// The other instance of to compare with. 77 | /// True if the instances are equal, false otherwise. 78 | public bool Equals(ScoredValue other) 79 | { 80 | return EqualityComparer.Default.Equals(this.Value, other.Value) && 81 | this.Score.Equals(other.Score); 82 | } 83 | 84 | /// 85 | public override int GetHashCode() 86 | { 87 | return HashCode.Combine(this.Value, this.Score); 88 | } 89 | 90 | /// 91 | /// Determines whether two instances of are equal. 92 | /// 93 | public static bool operator ==(ScoredValue left, ScoredValue right) 94 | { 95 | return left.Equals(right); 96 | } 97 | 98 | /// 99 | /// Determines whether two instances of are not equal. 100 | /// 101 | public static bool operator !=(ScoredValue left, ScoredValue right) 102 | { 103 | return !(left == right); 104 | } 105 | 106 | /// 107 | /// Determines whether the left instance of is less than the right instance. 108 | /// 109 | public static bool operator <(ScoredValue left, ScoredValue right) 110 | { 111 | return left.CompareTo(right) < 0; 112 | } 113 | 114 | /// 115 | /// Determines whether the left instance of is less than or equal to the right instance. 116 | /// 117 | public static bool operator <=(ScoredValue left, ScoredValue right) 118 | { 119 | return left.CompareTo(right) <= 0; 120 | } 121 | 122 | /// 123 | /// Determines whether the left instance of is greater than the right instance. 124 | /// 125 | public static bool operator >(ScoredValue left, ScoredValue right) 126 | { 127 | return left.CompareTo(right) > 0; 128 | } 129 | 130 | /// 131 | /// Determines whether the left instance of is greater than or equal to the right instance. 132 | /// 133 | public static bool operator >=(ScoredValue left, ScoredValue right) 134 | { 135 | return left.CompareTo(right) >= 0; 136 | } 137 | 138 | /// 139 | /// Returns the minimum possible value of a . 140 | /// 141 | internal static ScoredValue Min() 142 | { 143 | return new ScoredValue(default!, double.MinValue); 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /OpenRecall.Library/Collections/MinHeap.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace OpenRecall.Library.Collections 4 | { 5 | 6 | /// 7 | /// Implements the classic 'heap' data structure. By default, the item with the lowest value is at the top of the heap. 8 | /// 9 | /// Data type. 10 | internal sealed class MinHeap : IEnumerable where T : IComparable 11 | { 12 | private const int DefaultCapacity = 7; 13 | private const int MinCapacity = 0; 14 | 15 | private static readonly T[] s_emptyBuffer = []; 16 | 17 | private T[] _items; 18 | private int _count; 19 | 20 | /// 21 | /// Initializes a new instance of the class. 22 | /// 23 | /// Heap minimum value, which will be used as first item in collection. 24 | /// Number of elements that collection can hold. 25 | public MinHeap(T minValue, int capacity = DefaultCapacity) 26 | { 27 | if (capacity < MinCapacity) 28 | { 29 | throw new ArgumentOutOfRangeException(nameof(capacity), capacity, $"MinHeap capacity must be greater than {MinCapacity}."); 30 | } 31 | 32 | this._items = new T[capacity + 1]; 33 | // 34 | // The 0'th item is a sentinel entry that simplifies the code 35 | // 36 | this._items[0] = minValue; 37 | } 38 | 39 | /// 40 | /// Initializes a new instance of the class. 41 | /// 42 | /// Heap minimum value, which will be used as first item in collection. 43 | /// List of items to add. 44 | public MinHeap(T minValue, IList items) 45 | : this(minValue, items.Count) 46 | { 47 | this.Add(items); 48 | } 49 | 50 | /// 51 | /// Gets the current number of items in the collection. 52 | /// 53 | public int Count 54 | { 55 | get => this._count; 56 | internal set 57 | { 58 | Debug.Assert(value <= this.Capacity); 59 | this._count = value; 60 | } 61 | } 62 | 63 | /// 64 | /// Gets the number of elements that collection can hold. 65 | /// 66 | public int Capacity => this._items.Length - 1; // 0'th item is always a sentinel to simplify code 67 | 68 | /// 69 | /// Gets the element at the specified index. 70 | /// 71 | public T this[int index] 72 | { 73 | get => this._items[index + 1]; 74 | internal set { this._items[index + 1] = value; } 75 | } 76 | 77 | /// 78 | /// Gets first item in collection. 79 | /// 80 | public T Top => this._items[1]; 81 | 82 | /// 83 | /// Gets the boolean flag which indicates if collection is empty. 84 | /// 85 | public bool IsEmpty => (this._count == 0); 86 | 87 | /// 88 | /// Sets collection item count to zero. 89 | /// 90 | public void Clear() 91 | { 92 | this._count = 0; 93 | } 94 | 95 | /// 96 | /// Sets collection item count to zero and removes all items in collection. 97 | /// 98 | public void Erase() 99 | { 100 | Array.Clear(this._items, 1, this._count); 101 | this._count = 0; 102 | } 103 | 104 | /// 105 | /// Removes all items in collection and returns them. 106 | /// 107 | public T[] DetachBuffer() 108 | { 109 | T[] buf = this._items; 110 | this._items = s_emptyBuffer; 111 | this._count = 0; 112 | return buf; 113 | } 114 | 115 | /// 116 | /// Adds new item to collection. 117 | /// 118 | /// Item to add. 119 | public void Add(T item) 120 | { 121 | // 122 | // the 0'th item is always a sentinel and not included in this._count. 123 | // The length of the buffer is always this._count + 1 124 | // 125 | this._count++; 126 | this.EnsureCapacity(); 127 | this._items[this._count] = item; 128 | this.UpHeap(this._count); 129 | } 130 | 131 | /// 132 | /// Adds new items to collection. 133 | /// 134 | /// Items to add. 135 | public void Add(IEnumerable items) 136 | { 137 | foreach (T item in items) 138 | { 139 | this.Add(item); 140 | } 141 | } 142 | 143 | /// 144 | /// Adds new items starting from specified index. 145 | /// 146 | /// Items to add. 147 | /// Starting point of items to add. 148 | public void Add(IList items, int startAt = 0) 149 | { 150 | if (items is null) 151 | { 152 | throw new ArgumentNullException(nameof(items)); 153 | } 154 | 155 | int newItemCount = items.Count; 156 | if (startAt >= newItemCount) 157 | { 158 | throw new ArgumentOutOfRangeException(nameof(startAt), startAt, $"{nameof(startAt)} value must be less than {nameof(items)}.{nameof(items.Count)}."); 159 | } 160 | 161 | this.EnsureCapacity(this._count + (newItemCount - startAt)); 162 | for (int i = startAt; i < newItemCount; ++i) 163 | { 164 | // 165 | // the 0'th item is always a sentinel and not included in this._count. 166 | // The length of the buffer is always this._count + 1 167 | // 168 | this._count++; 169 | this._items[this._count] = items[i]; 170 | this.UpHeap(this._count); 171 | } 172 | } 173 | 174 | /// 175 | /// Removes first item in collection and returns it. 176 | /// 177 | public T RemoveTop() 178 | { 179 | if (this._count == 0) 180 | { 181 | throw new InvalidOperationException("MinHeap is empty."); 182 | } 183 | 184 | T item = this._items[1]; 185 | this._items[1] = this._items[this._count--]; 186 | this.DownHeap(1); 187 | return item; 188 | } 189 | 190 | /// 191 | /// Removes all items in collection and returns them. 192 | /// 193 | public IEnumerable RemoveAll() 194 | { 195 | while (this._count > 0) 196 | { 197 | yield return this.RemoveTop(); 198 | } 199 | } 200 | 201 | /// 202 | /// Resizes collection to specified capacity. 203 | /// 204 | /// Number of elements that collection can hold. 205 | public void EnsureCapacity(int capacity) 206 | { 207 | if (capacity < MinCapacity) 208 | { 209 | throw new ArgumentOutOfRangeException(nameof(capacity), capacity, $"MinHeap capacity must be greater than {MinCapacity}."); 210 | } 211 | 212 | // 0th item is always a sentinel 213 | capacity++; 214 | if (capacity > this._items.Length) 215 | { 216 | Array.Resize(ref this._items, capacity); 217 | } 218 | } 219 | 220 | /// 221 | /// Doubles collection capacity. 222 | /// 223 | public void EnsureCapacity() 224 | { 225 | if (this._count == this._items.Length) 226 | { 227 | Array.Resize(ref this._items, (this._count * 2) + 1); 228 | } 229 | } 230 | 231 | private void UpHeap(int startAt) 232 | { 233 | int i = startAt; 234 | T[] items = this._items; 235 | T item = items[i]; 236 | int parent = i >> 1; //i / 2; 237 | 238 | while (parent > 0 && items[parent].CompareTo(item) > 0) 239 | { 240 | // Child > parent. Exchange with parent, thus moving the child up the queue 241 | items[i] = items[parent]; 242 | i = parent; 243 | parent = i >> 1; //i / 2; 244 | } 245 | 246 | items[i] = item; 247 | } 248 | 249 | private void DownHeap(int startAt) 250 | { 251 | int i = startAt; 252 | int count = this._count; 253 | int maxParent = count >> 1; 254 | T[] items = this._items; 255 | T item = items[i]; 256 | 257 | while (i <= maxParent) 258 | { 259 | int child = i + i; 260 | // 261 | // Exchange the item with the smaller of its two children - if one is smaller, i.e. 262 | // 263 | // First, find the smaller child 264 | // 265 | if (child < count && items[child].CompareTo(items[child + 1]) > 0) 266 | { 267 | child++; 268 | } 269 | 270 | if (item.CompareTo(items[child]) <= 0) 271 | { 272 | // Heap condition is satisfied. Parent <= both its children 273 | break; 274 | } 275 | 276 | // Else, swap parent with the smallest child 277 | items[i] = items[child]; 278 | i = child; 279 | } 280 | 281 | items[i] = item; 282 | } 283 | 284 | /// 285 | /// Returns an enumerator that iterates through the collection. 286 | /// 287 | public IEnumerator GetEnumerator() 288 | { 289 | // The 0'th item in the queue is a sentinel. i is 1 based. 290 | for (int i = 1; i <= this._count; ++i) 291 | { 292 | yield return this._items[i]; 293 | } 294 | } 295 | 296 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 297 | { 298 | return this.GetEnumerator(); 299 | } 300 | 301 | /// 302 | /// Heap Sort in-place. 303 | /// This is destructive. Once you do this, the heap order is lost. 304 | /// The advantage on in-place is that we don't need to do another allocation 305 | /// 306 | public void SortDescending() 307 | { 308 | int count = this._count; 309 | int i = count; // remember that the 0'th item in the queue is always a sentinel. So i is 1 based 310 | 311 | while (this._count > 0) 312 | { 313 | // 314 | // this dequeues the item with the current LOWEST relevancy 315 | // We take that and place it at the 'back' of the array - thus inverting it 316 | // 317 | T item = this.RemoveTop(); 318 | this._items[i--] = item; 319 | } 320 | 321 | this._count = count; 322 | } 323 | 324 | /// 325 | /// Restores heap order 326 | /// 327 | internal void Restore() 328 | { 329 | this.Clear(); 330 | this.Add(this._items, 1); 331 | } 332 | 333 | internal void Sort(IComparer comparer) 334 | { 335 | Array.Sort(this._items, 1, this._count, comparer); 336 | } 337 | } 338 | } 339 | --------------------------------------------------------------------------------