2025-03-12 11:31:01 +01:00
using AIStudio.Dialogs.Settings ;
2026-07-03 15:02:20 +02:00
using AIStudio.Tools.AssistantSessions ;
2025-03-12 11:31:01 +01:00
2024-08-21 08:30:01 +02:00
namespace AIStudio.Assistants.IconFinder ;
2024-07-14 21:46:17 +02:00
2025-03-12 11:31:01 +01:00
public partial class AssistantIconFinder : AssistantBaseCore < SettingsDialogIconFinder >
2024-07-14 21:46:17 +02:00
{
2026-02-01 14:50:19 +01:00
protected override Tools . Components Component => Tools . Components . ICON_FINDER_ASSISTANT ;
2024-07-14 21:46:17 +02:00
2025-04-27 09:06:05 +02:00
protected override string Title => T ( "Icon Finder" );
2024-07-14 21:46:17 +02:00
2025-04-27 09:06:05 +02:00
protected override string Description => T ( """Finding the right icon for a context, such as for a piece of text, is not easy. The first challenge: You need to extract a concept from your context, such as from a text. Let's take an example where your text contains statements about multiple departments. The sought-after concept could be " departments . " The next challenge is that we need to anticipate the bias of the icon designers: under the search term " departments , " there may be no relevant icons or only unsuitable ones. Depending on the icon source, it might be more effective to search for " buildings , " for instance. LLMs assist you with both steps.""" );
2024-07-14 21:46:17 +02:00
protected override string SystemPrompt =>
"""
I can search for icons using US English keywords . Please help me come up with the right search queries .
I don ' t want you to translate my requests word - for - word into US English . Instead , you should provide keywords
that are likely to yield suitable icons . For example , I might ask for an icon about departments , but icons
related to the keyword "buildings" might be the best match . Provide your keywords in a Markdown list without
quotation marks .
""";
2024-09-08 21:01:51 +02:00
protected override bool AllowProfiles => false ;
2024-08-18 12:32:18 +02:00
2024-09-04 15:44:23 +02:00
protected override IReadOnlyList < IButtonData > FooterButtons => [];
2024-07-14 21:46:17 +02:00
2025-04-27 09:06:05 +02:00
protected override string SubmitText => T ( "Find Icon" );
2024-09-11 23:08:02 +02:00
protected override Func < Task > SubmitAction => this . FindIcon ;
2025-03-12 11:31:01 +01:00
2026-04-16 16:12:45 +02:00
protected override string SendToChatVisibleUserPromptText =>
$"""
{string.Format(T(" Find icon suggestions on { 0 } for the following context : "), this.selectedIconSource.Name())}
{ this . inputContext }
""";
2025-01-01 15:49:27 +01:00
protected override void ResetForm ()
2024-08-18 21:48:35 +02:00
{
this . inputContext = string . Empty ;
if (! this . MightPreselectValues ())
{
this . selectedIconSource = IconSources . GENERIC ;
}
}
2024-08-18 12:32:18 +02:00
2024-08-18 21:48:35 +02:00
protected override bool MightPreselectValues ()
2024-08-18 12:32:18 +02:00
{
if ( this . SettingsManager . ConfigurationData . IconFinder . PreselectOptions )
{
this . selectedIconSource = this . SettingsManager . ConfigurationData . IconFinder . PreselectedSource ;
2024-08-18 21:48:35 +02:00
return true ;
2024-08-18 12:32:18 +02:00
}
2024-08-18 21:48:35 +02:00
return false ;
}
2024-09-04 15:44:23 +02:00
private string inputContext = string . Empty ;
private IconSources selectedIconSource ;
2026-07-03 15:02:20 +02:00
private static readonly AssistantSessionStateKey < string > INPUT_CONTEXT_STATE_KEY = new ( nameof ( inputContext ));
private static readonly AssistantSessionStateKey < IconSources > SELECTED_ICON_SOURCE_STATE_KEY = new ( nameof ( selectedIconSource ));
/// <inheritdoc />
protected override void CaptureCustomAssistantSessionState ( AssistantSessionStateWriter state )
{
state . Set ( INPUT_CONTEXT_STATE_KEY , this . inputContext );
state . Set ( SELECTED_ICON_SOURCE_STATE_KEY , this . selectedIconSource );
}
/// <inheritdoc />
protected override void RestoreCustomAssistantSessionState ( AssistantSessionStateReader state )
{
state . Restore ( INPUT_CONTEXT_STATE_KEY , value => this . inputContext = value );
state . Restore ( SELECTED_ICON_SOURCE_STATE_KEY , value => this . selectedIconSource = value );
}
2024-09-04 15:44:23 +02:00
2024-08-18 21:48:35 +02:00
#region Overrides of ComponentBase
2024-08-18 12:32:18 +02:00
2024-08-18 21:48:35 +02:00
protected override async Task OnInitializedAsync ()
{
2026-08-23 21:15:37 +02:00
var deferredContent = MessageBus . INSTANCE . TakeDeferredMessages < string >( Event . SEND_TO_ICON_FINDER_ASSISTANT ). LastOrDefault ();
2024-08-18 12:32:18 +02:00
if ( deferredContent is not null )
this . inputContext = deferredContent ;
await base . OnInitializedAsync ();
}
#endregion
2024-07-14 21:46:17 +02:00
private string? ValidatingContext ( string context )
{
if ( string . IsNullOrWhiteSpace ( context ))
2025-04-27 09:06:05 +02:00
return T ( "Please provide a context. This will help the AI to find the right icon. You might type just a keyword or copy a sentence from your text, e.g., from a slide where you want to use the icon." );
2024-07-14 21:46:17 +02:00
return null ;
}
private async Task FindIcon ()
{
2026-04-16 16:48:47 +02:00
await this . Form !. Validate ();
if (! this . InputIsValid )
2024-07-14 21:46:17 +02:00
return ;
this . CreateChatThread ();
var time = this . AddUserRequest (
$"""
{this.selectedIconSource.Prompt()} I search for an icon for the following context:
```
{this.inputContext}
```
""" );
await this . AddAIResponseAsync ( time );
}
}