2026-09-18 14:48:02 +02:00
using AIStudio.Dialogs ;
using AIStudio.Tools.PluginSystem ;
using AIStudio.Tools.Services ;
namespace AIStudio.Tools ;
/// <summary>
/// Asks whether a data source should be indexed anew, and starts the rebuild when the user agrees.
/// </summary>
/// <remarks>
/// Kept here rather than in the two places which offer the repair -- the background embeddings page
/// and the data source table -- so the sentence naming what a rebuild costs cannot drift apart
/// between them. Naming both costs is the whole reason for asking at all.
/// </remarks>
public static class DataSourceRepair
{
private static string TB ( string fallbackEN ) => I18N . I . T ( fallbackEN , typeof ( DataSourceRepair ). Namespace , nameof ( DataSourceRepair ));
2026-11-13 10:16:56 +01:00
private static string TA ( string fallbackEN ) => I18N . I . TA ( fallbackEN , typeof ( DataSourceRepair ). Namespace , nameof ( DataSourceRepair ));
2026-09-18 14:48:02 +02:00
/// <summary>
/// Asks the user, and rebuilds the index of the data source when they agree.
/// </summary>
/// <param name="dialogService">The dialog service to ask with.</param>
/// <param name="embeddingService">The service which does the rebuild.</param>
/// <param name="dataSourceId">The data source to repair.</param>
/// <param name="dataSourceName">The name of that data source, as the question names it.</param>
/// <returns>True when the rebuild was started.</returns>
public static async Task < bool > ConfirmAndRepairAsync ( IDialogService dialogService , DataSourceEmbeddingService embeddingService , string dataSourceId , string dataSourceName )
{
2026-10-15 16:42:02 +02:00
// A repair discards the index, which waits for the end of a pause:
if ( embeddingService . IsIndexingPaused )
{
2026-11-13 10:16:56 +01:00
await dialogService . ShowMessageBoxAsync ( TB ( "Repair Data Source" ), TA ( "AI Studio is backing up or restoring the indexes of your data sources right now. Please try again once that is done." ), TB ( "Close" ));
2026-10-15 16:42:02 +02:00
return false ;
}
2026-09-18 14:48:02 +02:00
var dialogParameters = new DialogParameters < ConfirmDialog >
{
2026-11-13 10:16:56 +01:00
{ x => x . Message , string . Format ( TA ( "The index of the data source '{0}' cannot be read anymore. Repairing it means building the index from scratch: everything indexed so far is thrown away, and every document of this data source is sent to your embedding provider once more. With a cloud provider, this costs money, and with a large data source it takes a while. Do you want to repair this data source now?" ), dataSourceName ) },
2026-09-18 14:48:02 +02:00
};
var dialogReference = await dialogService . ShowAsync < ConfirmDialog >( TB ( "Repair Data Source" ), dialogParameters , Dialogs . DialogOptions . FULLSCREEN );
var dialogResult = await dialogReference . Result ;
if ( dialogResult is null || dialogResult . Canceled )
return false ;
await embeddingService . RepairDataSourceAsync ( dataSourceId );
return true ;
}
}