Make the embedding signature comparable from outside the service

This commit is contained in:
Thorsten Sommer committed 2026-09-18 15:36:58 +02:00
1 parent f512767e8b
commit e355894fba
4 files changed
+260 -4

No files matched your search

@@ -67,7 +67,7 @@ public sealed partial class DataSourceEmbeddingService
private async IAsyncEnumerable<EmbeddingChunk> StreamEmbeddingChunksAsync(string filePath, IDataSource dataSource, EmbeddingProvider embeddingProvider, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken token)
{
var options = this.GetChunkingOptions(dataSource, embeddingProvider);
var options = GetChunkingOptions(dataSource, embeddingProvider);
var strategy = this.GetChunkingStrategy(filePath);
var content = await this.ReadExtractedFileContentAsync(filePath, embeddingProvider, token);
@@ -582,7 +582,18 @@ public sealed partial class DataSourceEmbeddingService
throw new InvalidOperationException(string.Format(TB("The tokens of the text could not be counted for the embedding provider '{0}'. {1}"), embeddingProvider.Name, message));
}
private ChunkingOptions GetChunkingOptions(IDataSource dataSource, EmbeddingProvider embeddingProvider)
/// <summary>
/// Works out how the text of a data source is cut for a given embedding provider.
/// </summary>
/// <remarks>
/// Static, because the answer follows from its two arguments alone. That lets the embedding
/// signature be built for a configuration which is not stored yet, which is what the dialogs ask
/// before they save a change.
/// </remarks>
/// <param name="dataSource">The data source whose own chunk settings apply.</param>
/// <param name="embeddingProvider">The embedding provider whose token limit caps them.</param>
/// <returns>The chunk size and overlap which are actually used.</returns>
internal static ChunkingOptions GetChunkingOptions(IDataSource dataSource, EmbeddingProvider embeddingProvider)
{
var providerMaxChunkTokenLength = Math.Max(1, embeddingProvider.EffectiveTokenLimit);
var dataSourceMaxChunkTokenLength = dataSource is IInternalDataSource { MaxChunkTokenLength: > 0 } internalDataSource
@@ -1026,6 +1037,15 @@ public sealed partial class DataSourceEmbeddingService
chunkingOptions.OverlapTokenLength);
}
/// <summary>
/// Describes how the vectors of a data source were made, working the chunking out along the way.
/// </summary>
/// <param name="dataSource">The data source the vectors belong to.</param>
/// <param name="embeddingProvider">The embedding provider which makes them.</param>
/// <returns>The signature of this pairing.</returns>
internal static string BuildEmbeddingSignature(IDataSource dataSource, EmbeddingProvider embeddingProvider) =>
BuildEmbeddingSignature(dataSource, embeddingProvider, GetChunkingOptions(dataSource, embeddingProvider));
private DataSourceMetadataSnapshot BuildDataSourceMetadataSnapshot(IDataSource dataSource, IReadOnlyList<FileInfo> indexedFiles)
{
var fileHashes = indexedFiles
@@ -209,6 +209,16 @@ public sealed partial class DataSourceEmbeddingService(SettingsManager settingsM
}
var manifest = await indexStore.GetManifestAsync(dataSourceId, token);
return HasStoredIndexState(manifest);
}
/// <summary>
/// Whether the index holds anything at all about a data source.
/// </summary>
/// <param name="manifest">What the index store returned for it.</param>
/// <returns>True when there is stored index state.</returns>
private static bool HasStoredIndexState(DataSourceEmbeddingManifest manifest)
{
return !string.IsNullOrWhiteSpace(manifest.EmbeddingProviderId)
|| !string.IsNullOrWhiteSpace(manifest.EmbeddingSignature)
|| !string.IsNullOrWhiteSpace(manifest.SourceHash)
@@ -220,6 +230,59 @@ public sealed partial class DataSourceEmbeddingService(SettingsManager settingsM
|| manifest.PermanentFailures.Count > 0;
}
/// <summary>
/// Picks the data sources which already hold something in the index.
/// </summary>
/// <remarks>
/// Asked before a setting is saved which would throw those indexes away, so the question can be
/// put to the user with the names in it. Anything unclear counts as holding something — the
/// opposite of IsAwaitingReindexAsync, and for the opposite reason: there, a wrongly greyed-out
/// row would stay wrong for good, while a question asked once too often costs a click, and one
/// skipped costs whatever a cloud provider charges for embedding everything again.
/// </remarks>
/// <param name="dataSources">The data sources to ask about.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>Those of them which have stored index state.</returns>
public async Task<IReadOnlyList<IDataSource>> GetDataSourcesWithStoredIndexAsync(IReadOnlyCollection<IDataSource> dataSources, CancellationToken token = default)
{
//
// Filtering first also keeps the index database from being created while local RAG is off:
// asking for the store runs its migrations on the first call, which must not happen because
// somebody opened a dialog.
//
var candidates = dataSources.Where(this.IsSupportedInternalDataSource).ToList();
if (candidates.Count == 0)
return [];
try
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
timeout.CancelAfter(REINDEX_CHECK_TIMEOUT);
var indexStore = await databaseClientProvider.GetIndexStoreAsync(timeout.Token);
if (!indexStore.IsAvailable)
{
logger.LogWarning("Could not tell which data sources hold a stored index because the local RAG index database '{DatabaseName}' is unavailable. Treating all {DataSourceCount} of them as affected.", indexStore.Name, candidates.Count);
return candidates;
}
var affected = new List<IDataSource>(candidates.Count);
foreach (var dataSource in candidates)
{
var manifest = await indexStore.GetManifestAsync(dataSource.Id, timeout.Token);
if (HasStoredIndexState(manifest))
affected.Add(dataSource);
}
return affected;
}
catch (Exception exception)
{
logger.LogWarning(exception, "Could not tell which of {DataSourceCount} data source(s) hold a stored index. Treating all of them as affected.", candidates.Count);
return candidates;
}
}
/// <summary>
/// Whether a data source cannot answer a search right now because its index has to be built anew.
/// </summary>
@@ -269,7 +332,7 @@ public sealed partial class DataSourceEmbeddingService(SettingsManager settingsM
return false;
var indexState = await indexStore.GetDataSourceStateAsync(dataSource.Id, timeout.Token);
var chunkingOptions = this.GetChunkingOptions(dataSource, embeddingProvider);
var chunkingOptions = GetChunkingOptions(dataSource, embeddingProvider);
var embeddingSignature = BuildEmbeddingSignature(dataSource, embeddingProvider, chunkingOptions);
var runState = this.statuses.TryGetValue(dataSource.Id, out var status) ? status.State : (DataSourceEmbeddingState?)null;
@@ -1444,7 +1507,7 @@ public sealed partial class DataSourceEmbeddingService(SettingsManager settingsM
IndexStoreClient indexStore,
CancellationToken token)
{
var chunkingOptions = this.GetChunkingOptions(dataSource, embeddingProvider);
var chunkingOptions = GetChunkingOptions(dataSource, embeddingProvider);
var embeddingSignature = BuildEmbeddingSignature(dataSource, embeddingProvider, chunkingOptions);
var manifest = await indexStore.GetManifestAsync(dataSource.Id, token);
@@ -0,0 +1,40 @@
using AIStudio.Settings;
namespace AIStudio.Tools.Services;
/// <summary>
/// Answers whether an edit throws the stored index of a data source away.
/// </summary>
/// <remarks>
/// Nothing here knows which settings matter. Both questions are answered by building the embedding
/// signature twice and comparing the two, so the single place which decides stays
/// BuildEmbeddingSignature and this cannot drift away from what an indexing run then does.
/// </remarks>
internal static class EmbeddingChangeImpact
{
/// <summary>
/// Whether an edited embedding provider invalidates what is stored for one of its data sources.
/// </summary>
/// <param name="dataSource">The data source, which the edit leaves alone.</param>
/// <param name="before">The embedding provider as it is stored.</param>
/// <param name="after">The embedding provider as it would be stored.</param>
/// <returns>True when the stored index would be discarded.</returns>
public static bool AffectsStoredIndex(IDataSource dataSource, EmbeddingProvider before, EmbeddingProvider after) =>
!string.Equals(
DataSourceEmbeddingService.BuildEmbeddingSignature(dataSource, before),
DataSourceEmbeddingService.BuildEmbeddingSignature(dataSource, after),
StringComparison.Ordinal);
/// <summary>
/// Whether an edited data source invalidates what is stored for it.
/// </summary>
/// <param name="embeddingProvider">The embedding provider, which the edit leaves alone.</param>
/// <param name="before">The data source as it is stored.</param>
/// <param name="after">The data source as it would be stored.</param>
/// <returns>True when the stored index would be discarded.</returns>
public static bool AffectsStoredIndex(EmbeddingProvider embeddingProvider, IDataSource before, IDataSource after) =>
!string.Equals(
DataSourceEmbeddingService.BuildEmbeddingSignature(before, embeddingProvider),
DataSourceEmbeddingService.BuildEmbeddingSignature(after, embeddingProvider),
StringComparison.Ordinal);
}