mirror of
				https://github.com/MindWorkAI/AI-Studio.git
				synced 2025-11-04 07:40:21 +00:00 
			
		
		
		
	
		
			Some checks failed
		
		
	
	Build and Release / Read metadata (push) Has been cancelled
				
			Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Has been cancelled
				
			Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Has been cancelled
				
			Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
				
			Build and Release / Prepare & create release (push) Has been cancelled
				
			Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Has been cancelled
				
			Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Has been cancelled
				
			Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
				
			Build and Release / Publish release (push) Has been cancelled
				
			Co-authored-by: Thorsten Sommer <mail@tsommer.org>
		
			
				
	
	
		
			135 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using AIStudio.Chat;
 | 
						|
using AIStudio.Dialogs.Settings;
 | 
						|
 | 
						|
namespace AIStudio.Assistants.Translation;
 | 
						|
 | 
						|
public partial class AssistantTranslation : AssistantBaseCore<SettingsDialogTranslation>
 | 
						|
{
 | 
						|
    public override Tools.Components Component => Tools.Components.TRANSLATION_ASSISTANT;
 | 
						|
    
 | 
						|
    protected override string Title => T("Translation");
 | 
						|
    
 | 
						|
    protected override string Description => T("Translate text from one language to another.");
 | 
						|
    
 | 
						|
    protected override string SystemPrompt => 
 | 
						|
        """
 | 
						|
        You get text in a source language as input. The user wants to get the text translated into a target language.
 | 
						|
        Provide the translation in the requested language. Do not add any information. Correct any spelling or grammar mistakes.
 | 
						|
        Do not ask for additional information. Do not mirror the user's language. Do not mirror the task. When the target
 | 
						|
        language requires, e.g., shorter sentences, you should split the text into shorter sentences.
 | 
						|
        """;
 | 
						|
    
 | 
						|
    protected override bool AllowProfiles => false;
 | 
						|
    
 | 
						|
    protected override IReadOnlyList<IButtonData> FooterButtons => [];
 | 
						|
    
 | 
						|
    protected override string SubmitText => T("Translate");
 | 
						|
 | 
						|
    protected override Func<Task> SubmitAction => () => this.TranslateText(true);
 | 
						|
 | 
						|
    protected override bool SubmitDisabled => this.isAgentRunning;
 | 
						|
    
 | 
						|
    protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
 | 
						|
    {
 | 
						|
        SystemPrompt = SystemPrompts.DEFAULT,
 | 
						|
    };
 | 
						|
    
 | 
						|
    protected override void ResetForm()
 | 
						|
    {
 | 
						|
        this.inputText = string.Empty;
 | 
						|
        this.inputTextLastTranslation = string.Empty;
 | 
						|
        if (!this.MightPreselectValues())
 | 
						|
        {
 | 
						|
            this.showWebContentReader = false;
 | 
						|
            this.useContentCleanerAgent = false;
 | 
						|
            this.liveTranslation = false;
 | 
						|
            this.selectedTargetLanguage = CommonLanguages.AS_IS;
 | 
						|
            this.customTargetLanguage = string.Empty;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    protected override bool MightPreselectValues()
 | 
						|
    {
 | 
						|
        if (this.SettingsManager.ConfigurationData.Translation.PreselectOptions)
 | 
						|
        {
 | 
						|
            this.showWebContentReader = this.SettingsManager.ConfigurationData.Translation.PreselectWebContentReader;
 | 
						|
            this.useContentCleanerAgent = this.SettingsManager.ConfigurationData.Translation.PreselectContentCleanerAgent;
 | 
						|
            this.liveTranslation = this.SettingsManager.ConfigurationData.Translation.PreselectLiveTranslation;
 | 
						|
            this.selectedTargetLanguage = this.SettingsManager.ConfigurationData.Translation.PreselectedTargetLanguage;
 | 
						|
            this.customTargetLanguage = this.SettingsManager.ConfigurationData.Translation.PreselectOtherLanguage;
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
        
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
    
 | 
						|
    private bool showWebContentReader;
 | 
						|
    private bool useContentCleanerAgent;
 | 
						|
    private bool liveTranslation;
 | 
						|
    private bool isAgentRunning;
 | 
						|
    private string inputText = string.Empty;
 | 
						|
    private string inputTextLastTranslation = string.Empty;
 | 
						|
    private CommonLanguages selectedTargetLanguage;
 | 
						|
    private string customTargetLanguage = string.Empty;
 | 
						|
 | 
						|
    #region Overrides of ComponentBase
 | 
						|
 | 
						|
    protected override async Task OnInitializedAsync()
 | 
						|
    {
 | 
						|
        var deferredContent = MessageBus.INSTANCE.CheckDeferredMessages<string>(Event.SEND_TO_TRANSLATION_ASSISTANT).FirstOrDefault();
 | 
						|
        if (deferredContent is not null)
 | 
						|
            this.inputText = deferredContent;
 | 
						|
        
 | 
						|
        await base.OnInitializedAsync();
 | 
						|
    }
 | 
						|
 | 
						|
    #endregion
 | 
						|
 | 
						|
    private string? ValidatingText(string text)
 | 
						|
    {
 | 
						|
        if(string.IsNullOrWhiteSpace(text))
 | 
						|
            return T("Please provide a text as input. You might copy the desired text from a document or a website.");
 | 
						|
        
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
    
 | 
						|
    private string? ValidatingTargetLanguage(CommonLanguages language)
 | 
						|
    {
 | 
						|
        if(language == CommonLanguages.AS_IS)
 | 
						|
            return T("Please select a target language.");
 | 
						|
        
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
    
 | 
						|
    private string? ValidateCustomLanguage(string language)
 | 
						|
    {
 | 
						|
        if(this.selectedTargetLanguage == CommonLanguages.OTHER && string.IsNullOrWhiteSpace(language))
 | 
						|
            return T("Please provide a custom language.");
 | 
						|
        
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    private async Task TranslateText(bool force)
 | 
						|
    {
 | 
						|
        await this.form!.Validate();
 | 
						|
        if (!this.inputIsValid)
 | 
						|
            return;
 | 
						|
        
 | 
						|
        if(!force && this.inputText == this.inputTextLastTranslation)
 | 
						|
            return;
 | 
						|
        
 | 
						|
        this.inputTextLastTranslation = this.inputText;
 | 
						|
        this.CreateChatThread();
 | 
						|
        var time = this.AddUserRequest(
 | 
						|
            $"""
 | 
						|
                {this.selectedTargetLanguage.PromptTranslation(this.customTargetLanguage)}
 | 
						|
                
 | 
						|
                The given text is:
 | 
						|
                
 | 
						|
                ---
 | 
						|
                {this.inputText}
 | 
						|
             """);
 | 
						|
 | 
						|
        await this.AddAIResponseAsync(time);
 | 
						|
    }
 | 
						|
} |