2026-11-21 19:47:10 +01:00
using AIStudio.Chat ;
using AIStudio.Provider ;
using AIStudio.Settings ;
namespace AIStudio.AIOperations ;
/// <summary>
/// The base of the AI operations, with what each of them needs to ask a model.
/// </summary>
/// <remarks>
/// It keeps nothing of a call: every request gets a chat thread of its own, and the provider comes with the
/// context. Derived operations keep it that way, see IAIOperation; AIOperationsAreStatelessTests checks it.
/// </remarks>
/// <typeparam name="TRequest">What the operation works on.</typeparam>
/// <typeparam name="TResult">What the operation returns.</typeparam>
public abstract class AIOperationBase < TRequest , TResult >( ILogger logger , SettingsManager settingsManager ) : IAIOperation < TRequest , TResult >
{
protected ILogger Logger { get ; } = logger ;
protected SettingsManager SettingsManager { get ; } = settingsManager ;
#region Implementation of IAIOperation
/// <inheritdoc />
public abstract string Id { get ; }
/// <inheritdoc />
public abstract Tools . Components Component { get ; }
/// <inheritdoc />
public abstract ConfidenceLevel GetRequiredConfidence ( TRequest request );
/// <inheritdoc />
public abstract Task < TResult > RunAsync ( TRequest request , AIOperationContext context , CancellationToken token = default );
#endregion
/// <summary>
/// Sends one request to the provider of the context and waits for the whole answer.
/// </summary>
/// <remarks>
/// The provider is checked once more right before: the organization must not have prohibited it, and it
/// has to reach the global minimum and the confidence the content of the request requires. The minimum
2026-11-27 16:35:51 +01:00
/// of the operation itself is not checked here, because a caller may exempt a fallback from it.<br/><br/>
///
/// Content from a chat brings the restrictions of that chat along, such as a data source whose data
/// only self-hosted models may see. The thread of the request then demands them of the provider, as the
/// chat itself would, see ChatThread.RequireRestrictions.
2026-11-21 19:47:10 +01:00
/// </remarks>
/// <param name="request">The request, for the confidence its content requires.</param>
/// <param name="context">What the call runs with, the provider above all.</param>
/// <param name="systemPrompt">The system prompt.</param>
/// <param name="userPrompt">The user prompt.</param>
/// <param name="token">The cancellation token.</param>
2026-11-27 16:35:51 +01:00
/// <param name="restrictions">The restrictions of the chat the content of the request comes from, or null when it comes from none.</param>
2026-11-21 19:47:10 +01:00
/// <returns>The text of the answer, or null when the provider must not see the request or did not answer.</returns>
2026-11-27 16:35:51 +01:00
protected async Task < string? > AskAsync ( TRequest request , AIOperationContext context , string systemPrompt , string userPrompt , CancellationToken token , ChatRestrictions ? restrictions = null )
2026-11-21 19:47:10 +01:00
{
var provider = context . Provider ;
if (! this . MayAsk ( provider , request ))
return null ;
var thread = new ChatThread
{
WorkspaceId = Guid . Empty ,
ChatId = Guid . NewGuid (),
Name = string . Empty ,
SystemPrompt = systemPrompt ,
Blocks = [],
RuntimeComponent = this . Component ,
};
2026-11-27 16:35:51 +01:00
if ( restrictions is not null )
thread . RequireRestrictions ( restrictions );
2026-11-21 19:47:10 +01:00
var time = DateTimeOffset . Now ;
var prompt = new ContentText
{
Text = userPrompt ,
};
thread . Blocks . Add ( new ContentBlock
{
Time = time ,
ContentType = ContentType . TEXT ,
Role = ChatRole . USER ,
Content = prompt ,
});
var answer = new ContentText
{
// We have to wait for the remote for the content stream:
InitialRemoteWait = true ,
};
thread . Blocks . Add ( new ContentBlock
{
Time = time ,
ContentType = ContentType . TEXT ,
Role = ChatRole . AI ,
Content = answer ,
});
// Awaiting this line waits for the entire content to be streamed:
await answer . CreateFromProviderAsync ( provider . CreateProvider (), provider . Model , prompt , thread , token );
if ( string . IsNullOrWhiteSpace ( answer . Text ))
{
this . Logger . LogWarning ( $"The AI operation '{this.Id}' got no answer from the provider '{provider.InstanceName}'." );
return null ;
}
return answer . Text ;
}
private bool MayAsk ( Settings . Provider provider , TRequest request )
{
if ( provider == Settings . Provider . NONE || provider . UsedLLMProvider is LLMProviders . NONE )
{
this . Logger . LogWarning ( $"The AI operation '{this.Id}' has no provider." );
return false ;
}
var minimumLevel = this . SettingsManager . GlobalMinimumConfidenceLevel ;
var requiredLevel = this . GetRequiredConfidence ( request );
if ( requiredLevel > minimumLevel )
minimumLevel = requiredLevel ;
if (! this . SettingsManager . IsProviderUsable ( provider . UsedLLMProvider , minimumLevel ))
{
this . Logger . LogWarning ( $"The AI operation '{this.Id}' must not use the provider '{provider.InstanceName}': the organization prohibited it, or its confidence is below '{minimumLevel.Name}'." );
return false ;
}
return true ;
}
}