Added a way to read what the AI received from your sources (#1100)

Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
Paul KoudelkaandThorsten Sommer authored and GitHub committed 2026-10-26 10:00:01 +01:00
1 parent e0d8e18f34
commit cbc4ce5413
229 files changed
+12739 -1269

No files matched your search

@@ -2,6 +2,8 @@ using AIStudio.Tools.Databases.IndexStore;
using AIStudio.Tools.Services.Indexing;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
namespace AIStudio.Tests.Tools.Databases;
@@ -16,17 +18,45 @@ internal sealed class TemporaryIndexStore(string directory, string databasePath)
public SqliteIndexStoreClientImplementation Client { get; } = new("SQLite", databasePath, directory, string.Empty);
public static async Task<TemporaryIndexStore> CreateAsync()
{
var store = Create();
await store.MigrateAsync();
return store;
}
/// <summary>
/// Creates a store migrated only up to the given migration, the way an older version of the app left it.
/// </summary>
/// <remarks>
/// The client maps the current schema, so it may fail on such a store. Write the rows of the
/// older schema with raw SQL through CreateContext, then call MigrateAsync.
/// </remarks>
/// <param name="targetMigration">The id of the last migration to apply, e.g., "20261018000000_MediaTranscriptKey".</param>
public static async Task<TemporaryIndexStore> CreateAsync(string targetMigration)
{
var store = Create();
await using var context = store.CreateContext();
await context.GetService<IMigrator>().MigrateAsync(targetMigration);
return store;
}
/// <summary>
/// Applies every migration which is still missing, the way the app does when it starts.
/// </summary>
public async Task MigrateAsync()
{
await using var context = this.CreateContext();
await IndexStoreSchemaMigrator.MigrateAsync(context, CancellationToken.None);
}
private static TemporaryIndexStore Create()
{
SQLitePCL.Batteries_V2.Init();
var directory = Path.Combine(Path.GetTempPath(), $"ai-studio-index-store-{Guid.NewGuid():N}");
Directory.CreateDirectory(directory);
var databasePath = Path.Combine(directory, "rag-index.sqlite3");
await using (var context = new IndexStoreDbContext(IndexStoreDbContext.CreateOptions(databasePath)))
await IndexStoreSchemaMigrator.MigrateAsync(context, CancellationToken.None);
return new TemporaryIndexStore(directory, databasePath);
return new TemporaryIndexStore(directory, Path.Combine(directory, "rag-index.sqlite3"));
}
/// <summary>
@@ -50,7 +80,7 @@ internal sealed class TemporaryIndexStore(string directory, string databasePath)
var now = DateTimeOffset.UtcNow;
await this.Client.UpsertFileAsync(dataSourceId, new EmbeddingStateFile(mailId, key, subject, key, "mail", name, 2048, now, now, now, 1), CancellationToken.None);
await this.Client.UpsertChunksAsync(dataSourceId, [new EmbeddingStateChunk(IndexedDocumentIds.CreateChunkId(dataSourceId, name, 0), mailId, null, 0, text, now)], CancellationToken.None);
await this.Client.UpsertChunksAsync(dataSourceId, [new EmbeddingStateChunk(IndexedDocumentIds.CreateChunkId(dataSourceId, name, 0), mailId, null, null, 0, text, now)], CancellationToken.None);
await this.Client.UpsertMailAsync(dataSourceId, mail with { MailId = mailId }, CancellationToken.None);
return mailId;
}