Added a component factory and refactored Dropdowns; reformat getters

This commit is contained in:
krut_ni 2025-07-21 16:14:05 +02:00
parent 04c30602e4
commit f5c475f354
8 changed files with 110 additions and 20 deletions

View File

@ -54,15 +54,16 @@ ASSISTANT = {
},
{
Type = "Dropdown",
Props = {
Name = "language",
Label = "Sprache",
Default = "Sprache nicht angeben",
ValueType = "string",
Default = { Value = "", Display = "Sprache nicht angeben."}
Items = {
{ Value = "de-DE", Display = "Deutsch" },
{ Value = "en-UK", Display = "Englisch (UK)" },
{ Value = "en-US", Display = "Englisch (US)" },
}
},
Props = {
Name = "language",
Label = "Sprache",
}
},
{

View File

@ -0,0 +1,31 @@
using AIStudio.Tools.PluginSystem.Assistants.DataModel;
namespace AIStudio.Tools.PluginSystem.Assistants;
public class AssistantComponentFactory
{
private static readonly ILogger<AssistantComponentFactory> LOGGER = Program.LOGGER_FACTORY.CreateLogger<AssistantComponentFactory>();
public static IAssistantComponent CreateComponent(
AssistantUiCompontentType type,
Dictionary<string, object> props,
List<IAssistantComponent> children)
{
switch (type)
{
case AssistantUiCompontentType.FORM:
return new AssistantForm { Props = props, Children = children };
case AssistantUiCompontentType.TEXT_AREA:
return new AssistantTextArea { Props = props, Children = children };
case AssistantUiCompontentType.BUTTON:
return new AssistantButton { Props = props, Children = children};
case AssistantUiCompontentType.DROPDOWN:
return new AssistantDropdown { Props = props, Children = children };
case AssistantUiCompontentType.PROVIDER_SELECTION:
return new AssistantProviderSelection { Props = props, Children = children };
default:
LOGGER.LogError($"Unknown assistant component type!\n{type} is not a supported assistant component type");
throw new Exception($"Unknown assistant component type: {type}");
}
}
}

View File

@ -8,17 +8,23 @@ public class AssistantButton : AssistantComponentBase
public string Name
{
get => this.Props.TryGetValue(nameof(this.Name), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Name), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Name)] = value;
}
public string Text
{
get => this.Props.TryGetValue(nameof(this.Text), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Text), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Text)] = value;
}
public string Action
{
get => this.Props.TryGetValue(nameof(this.Action), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Action), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Action)] = value;
}
}

View File

@ -8,22 +8,65 @@ public class AssistantDropdown : AssistantComponentBase
public string Name
{
get => this.Props.TryGetValue(nameof(this.Name), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Name), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Name)] = value;
}
public string Label
{
get => this.Props.TryGetValue(nameof(this.Label), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Label), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Label)] = value;
}
public string Default
public AssistantDropdownItem Default
{
get => this.Props.TryGetValue(nameof(this.Default), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get
{
if (this.Props.TryGetValue(nameof(this.Default), out var v) && v is AssistantDropdownItem adi)
return adi;
return this.Items.Count > 0 ? this.Items[0] : AssistantDropdownItem.Default();
}
set => this.Props[nameof(this.Default)] = value;
}
public List<AssistantDropdownItem> Items
{
get => this.Props.TryGetValue(nameof(this.Items), out var v) && v is List<AssistantDropdownItem> list ? list : [];
get => this.Props.TryGetValue(nameof(this.Items), out var v) && v is List<AssistantDropdownItem> list
? list
: [];
set => this.Props[nameof(this.Items)] = value;
}
public string ValueType
{
get => this.Props.TryGetValue(nameof(this.ValueType), out var v)
? v.ToString() ?? "string"
: "string";
set => this.Props[nameof(this.ValueType)] = value;
}
public IEnumerable<object> GetParsedDropdownValues()
{
foreach (var item in this.Items)
{
switch (this.ValueType.ToLowerInvariant())
{
case "int":
if (int.TryParse(item.Value, out var i)) yield return i;
break;
case "double":
if (double.TryParse(item.Value, out var d)) yield return d;
break;
case "bool":
if (bool.TryParse(item.Value, out var b)) yield return b;
break;
default:
yield return item.Value;
break;
}
}
}
}

View File

@ -4,4 +4,6 @@ public class AssistantDropdownItem
{
public string Value { get; set; } = string.Empty;
public string Display { get; set; } = string.Empty;
public static AssistantDropdownItem Default() => new() { Value = string.Empty, Display = string.Empty };
}

View File

@ -8,12 +8,16 @@ public class AssistantProviderSelection : AssistantComponentBase
public string Name
{
get => this.Props.TryGetValue(nameof(this.Name), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Name), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Name)] = value;
}
public string Label
{
get => this.Props.TryGetValue(nameof(this.Label), out var v) ? v.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Label), out var v)
? v.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Label)] = value;
}
}

View File

@ -8,13 +8,17 @@ public class AssistantTextArea : AssistantComponentBase
public string Name
{
get => this.Props.TryGetValue(nameof(this.Name), out var val) ? val.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Name), out var val)
? val.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Name)] = value;
}
public string Label
{
get => this.Props.TryGetValue(nameof(this.Label), out var val) ? val.ToString() ?? string.Empty : string.Empty;
get => this.Props.TryGetValue(nameof(this.Label), out var val)
? val.ToString() ?? string.Empty
: string.Empty;
set => this.Props[nameof(this.Label)] = value;
}

View File

@ -5,7 +5,6 @@ public enum AssistantUiCompontentType
FORM,
TEXT_AREA,
BUTTON,
CHECKBOX,
DROPDOWN,
PROVIDER_SELECTION,
}