mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-12 11:01:38 +00:00
Added a component factory and refactored Dropdowns; reformat getters
This commit is contained in:
parent
04c30602e4
commit
f5c475f354
@ -54,15 +54,16 @@ ASSISTANT = {
|
||||
},
|
||||
{
|
||||
Type = "Dropdown",
|
||||
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",
|
||||
Default = "Sprache nicht angeben",
|
||||
Items = {
|
||||
{ Value = "de-DE", Display = "Deutsch" },
|
||||
{ Value = "en-UK", Display = "Englisch (UK)" },
|
||||
{ Value = "en-US", Display = "Englisch (US)" },
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -5,25 +5,68 @@ public class AssistantDropdown : AssistantComponentBase
|
||||
public override AssistantUiCompontentType Type => AssistantUiCompontentType.DROPDOWN;
|
||||
public Dictionary<string, object> Props { get; set; } = new();
|
||||
public List<IAssistantComponent> Children { get; set; } = new();
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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 };
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@ public enum AssistantUiCompontentType
|
||||
FORM,
|
||||
TEXT_AREA,
|
||||
BUTTON,
|
||||
CHECKBOX,
|
||||
DROPDOWN,
|
||||
PROVIDER_SELECTION,
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user