2025-03-12 11:31:01 +01:00
using AIStudio.Dialogs.Settings ;
2026-07-03 15:02:20 +02:00
using AIStudio.Tools.AssistantSessions ;
2024-09-15 22:06:22 +02:00
2024-08-23 21:47:07 +02:00
namespace AIStudio.Assistants.LegalCheck ;
2025-03-12 11:31:01 +01:00
public partial class AssistantLegalCheck : AssistantBaseCore < SettingsDialogLegalCheck >
2024-08-23 21:47:07 +02:00
{
2026-02-01 14:50:19 +01:00
protected override Tools . Components Component => Tools . Components . LEGAL_CHECK_ASSISTANT ;
2024-09-04 15:44:23 +02:00
2025-04-27 09:06:05 +02:00
protected override string Title => T ( "Legal Check" );
2024-08-23 21:47:07 +02:00
2025-04-27 09:06:05 +02:00
protected override string Description => T ( "Provide a legal document and ask a question about it. This assistant does not replace legal advice. Consult a lawyer to get professional advice. Remember that LLMs can invent answers and facts. Please do not rely on this answers." );
2024-08-23 21:47:07 +02:00
protected override string SystemPrompt =>
"""
You are an expert in legal matters . You have studied international law and are familiar with the law in
various countries . You receive a legal document and answer the user ' s questions . You are a friendly and
professional assistant . You respond to the user in the language in which the questions were asked . If
you are unsure , unfamiliar with the legal area , or do not know the answers , you inform the user .
Never invent facts !
""";
2024-09-04 15:44:23 +02:00
protected override IReadOnlyList < IButtonData > FooterButtons => [];
2024-08-23 21:47:07 +02:00
2025-04-27 09:06:05 +02:00
protected override string SubmitText => T ( "Ask your questions" );
2024-09-11 23:08:02 +02:00
2026-07-05 17:48:00 +02:00
protected override Func < Task > SubmitAction => this . AskQuestions ;
2025-03-12 11:31:01 +01:00
2024-09-11 23:08:02 +02:00
protected override bool SubmitDisabled => this . isAgentRunning ;
2026-04-16 16:12:45 +02:00
protected override string SendToChatVisibleUserPromptPrefix => T ( "Answer the following questions about a legal document:" );
protected override string SendToChatVisibleUserPromptContent => this . inputQuestions ;
2024-09-15 22:06:22 +02:00
2025-01-01 15:49:27 +01:00
protected override void ResetForm ()
2024-08-23 21:47:07 +02:00
{
this . inputLegalDocument = string . Empty ;
this . inputQuestions = string . Empty ;
2026-09-13 16:24:36 +02:00
this . webContentURL = string . Empty ;
2025-09-01 19:13:02 +02:00
if (! this . MightPreselectValues ())
{
this . showWebContentReader = false ;
this . useContentCleanerAgent = false ;
}
}
protected override bool MightPreselectValues ()
{
if ( this . SettingsManager . ConfigurationData . LegalCheck . PreselectOptions )
{
this . showWebContentReader = this . SettingsManager . ConfigurationData . LegalCheck . PreselectWebContentReader ;
this . useContentCleanerAgent = this . SettingsManager . ConfigurationData . LegalCheck . PreselectContentCleanerAgent ;
return true ;
}
return false ;
2024-08-23 21:47:07 +02:00
}
2024-09-04 15:44:23 +02:00
2025-09-01 19:13:02 +02:00
private bool showWebContentReader ;
private bool useContentCleanerAgent ;
2024-08-23 21:47:07 +02:00
private bool isAgentRunning ;
2026-09-13 16:24:36 +02:00
private string webContentURL = string . Empty ;
2024-08-23 21:47:07 +02:00
private string inputLegalDocument = string . Empty ;
private string inputQuestions = string . Empty ;
2026-07-03 15:02:20 +02:00
private static readonly AssistantSessionStateKey < bool > SHOW_WEB_CONTENT_READER_STATE_KEY = new ( nameof ( showWebContentReader ));
private static readonly AssistantSessionStateKey < bool > USE_CONTENT_CLEANER_AGENT_STATE_KEY = new ( nameof ( useContentCleanerAgent ));
private static readonly AssistantSessionStateKey < bool > IS_AGENT_RUNNING_STATE_KEY = new ( nameof ( isAgentRunning ));
2026-09-13 16:24:36 +02:00
private static readonly AssistantSessionStateKey < string > WEB_CONTENT_URL_STATE_KEY = new ( nameof ( webContentURL ));
2026-07-03 15:02:20 +02:00
private static readonly AssistantSessionStateKey < string > INPUT_LEGAL_DOCUMENT_STATE_KEY = new ( nameof ( inputLegalDocument ));
private static readonly AssistantSessionStateKey < string > INPUT_QUESTIONS_STATE_KEY = new ( nameof ( inputQuestions ));
/// <inheritdoc />
protected override void CaptureCustomAssistantSessionState ( AssistantSessionStateWriter state )
{
state . Set ( SHOW_WEB_CONTENT_READER_STATE_KEY , this . showWebContentReader );
state . Set ( USE_CONTENT_CLEANER_AGENT_STATE_KEY , this . useContentCleanerAgent );
state . Set ( IS_AGENT_RUNNING_STATE_KEY , this . isAgentRunning );
2026-09-13 16:24:36 +02:00
state . Set ( WEB_CONTENT_URL_STATE_KEY , this . webContentURL );
2026-07-03 15:02:20 +02:00
state . Set ( INPUT_LEGAL_DOCUMENT_STATE_KEY , this . inputLegalDocument );
state . Set ( INPUT_QUESTIONS_STATE_KEY , this . inputQuestions );
}
/// <inheritdoc />
protected override void RestoreCustomAssistantSessionState ( AssistantSessionStateReader state )
{
state . Restore ( SHOW_WEB_CONTENT_READER_STATE_KEY , value => this . showWebContentReader = value );
state . Restore ( USE_CONTENT_CLEANER_AGENT_STATE_KEY , value => this . useContentCleanerAgent = value );
state . Restore ( IS_AGENT_RUNNING_STATE_KEY , value => this . isAgentRunning = value );
2026-09-13 16:24:36 +02:00
state . Restore ( WEB_CONTENT_URL_STATE_KEY , value => this . webContentURL = value );
2026-07-03 15:02:20 +02:00
state . Restore ( INPUT_LEGAL_DOCUMENT_STATE_KEY , value => this . inputLegalDocument = value );
state . Restore ( INPUT_QUESTIONS_STATE_KEY , value => this . inputQuestions = value );
}
2024-08-23 21:47:07 +02:00
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync ()
{
2026-08-23 21:15:37 +02:00
var deferredContent = MessageBus . INSTANCE . TakeDeferredMessages < string >( Event . SEND_TO_LEGAL_CHECK_ASSISTANT ). LastOrDefault ();
2024-08-23 21:47:07 +02:00
if ( deferredContent is not null )
this . inputQuestions = deferredContent ;
await base . OnInitializedAsync ();
}
#endregion
private string? ValidatingLegalDocument ( string text )
{
if ( string . IsNullOrWhiteSpace ( text ))
2025-04-27 09:06:05 +02:00
return T ( "Please provide a legal document as input. You might copy the desired text from a document or a website." );
2024-08-23 21:47:07 +02:00
return null ;
}
private string? ValidatingQuestions ( string text )
{
if ( string . IsNullOrWhiteSpace ( text ))
2025-04-27 09:06:05 +02:00
return T ( "Please provide your questions as input." );
2024-08-23 21:47:07 +02:00
return null ;
}
2026-07-05 17:48:00 +02:00
private async Task AskQuestions ()
2024-08-23 21:47:07 +02:00
{
2026-04-16 16:48:47 +02:00
await this . Form !. Validate ();
if (! this . InputIsValid )
2024-08-23 21:47:07 +02:00
return ;
this . CreateChatThread ();
var time = this . AddUserRequest (
$"""
# The legal document
{this.inputLegalDocument}
# The questions
{this.inputQuestions}
""" );
await this . AddAIResponseAsync ( time );
}
}