AI-Studio/app/MindWork AI Studio/Models/ModelFamilyBuilder.cs
Thorsten Sommer d85b4e71b6
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
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, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Rebuilt how AI Studio knows what a model can do (#960)
2026-09-13 14:17:25 +02:00

73 lines
3.0 KiB
C#

using AIStudio.Models.Matching;
namespace AIStudio.Models;
/// <summary>
/// Collects the rules of one family as they are stated.
/// </summary>
/// <remarks>
/// The order rules are stated in changes nothing about which one wins -- that is what the computed
/// specificity is for. It matters in one place only: a variant which inherits takes what the rule
/// before it stated, so that a family can say what its models have in common once and then say
/// only what makes each variant different.
/// </remarks>
/// <param name="origin">What the rules name as their origin, which is the family's name.</param>
public sealed class ModelFamilyBuilder(string origin)
{
private readonly List<ModelRuleBuilder> stated = [];
/// <summary>
/// States a rule which chooses the model.
/// </summary>
/// <param name="text">The name, or the part of it, this rule answers for. In normalized form.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Rule(string text) => this.Add(text, ModelRuleKind.SELECTOR);
/// <summary>
/// States a rule which adjusts whatever chose the model.
/// </summary>
/// <param name="text">The name, or the part of it, this rule answers for. In normalized form.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Modifier(string text) => this.Add(text, ModelRuleKind.MODIFIER);
/// <summary>
/// Turns everything stated into rules.
/// </summary>
/// <returns>The rules, in the order they were stated.</returns>
internal IReadOnlyList<ModelRule> Build()
{
//
// The same text may well be stated twice, with different conditions on top -- that is how
// a variant of a generation is written. What cannot be done is naming that text to inherit
// from, because it names two rules and taking either of them would be a coin toss. Found
// before anything is built, so that where the two stand in the file makes no difference.
//
var statedMoreThanOnce = this.stated
.GroupBy(statement => statement.PatternText, StringComparer.Ordinal)
.Where(group => group.Count() > 1)
.Select(group => group.Key)
.ToHashSet(StringComparer.Ordinal);
var built = new List<ModelRule>(this.stated.Count);
var byPatternText = new Dictionary<string, ModelProfileChange>(StringComparer.Ordinal);
ModelProfileChange? previous = null;
foreach (var statement in this.stated)
{
var rule = statement.Build(statement.InheritanceBasis(byPatternText, statedMoreThanOnce, previous));
built.Add(rule);
byPatternText[rule.Pattern.Text] = rule.Change;
previous = rule.Change;
}
return built;
}
private ModelRuleBuilder Add(string text, ModelRuleKind kind)
{
var statement = new ModelRuleBuilder(text, kind, origin);
this.stated.Add(statement);
return statement;
}
}