improved provider export to handle formatting of comments

This commit is contained in:
PaulKoudelka 2026-04-24 17:45:58 +02:00
parent e3cb7e9734
commit 027b80eac7
3 changed files with 76 additions and 72 deletions

View File

@ -176,30 +176,36 @@ public sealed record EmbeddingProvider(
/// <returns>A Lua configuration section string.</returns> /// <returns>A Lua configuration section string.</returns>
public string ExportAsConfigurationSection(string? encryptedApiKey = null) public string ExportAsConfigurationSection(string? encryptedApiKey = null)
{ {
var apiKeyLine = string.Empty; var lines = new List<string>
if (!string.IsNullOrWhiteSpace(encryptedApiKey))
{ {
apiKeyLine = $""" "CONFIG[\"EMBEDDING_PROVIDERS\"][#CONFIG[\"EMBEDDING_PROVIDERS\"]+1] = {",
["APIKey"] = "{LuaTools.EscapeLuaString(encryptedApiKey)}", $" [\"Id\"] = \"{Guid.NewGuid()}\",",
"""; $" [\"Name\"] = \"{LuaTools.EscapeLuaString(this.Name)}\",",
$" [\"UsedLLMProvider\"] = \"{this.UsedLLMProvider}\",",
};
if (!string.IsNullOrWhiteSpace(this.TokenizerPath))
{
lines.Add(string.Empty);
lines.Add(" -- The tokenizer path shown is local to this model. To use it with the plugin:");
lines.Add(" -- 1. Copy the tokenizer into the zip.");
lines.Add(" -- 2. Update the path in the plugin file to be relative to the zip's root.");
lines.Add($" [\"TokenizerPath\"] = \"{LuaTools.EscapeLuaString(this.TokenizerPath)}\",");
} }
return $$""" lines.Add(string.Empty);
CONFIG["EMBEDDING_PROVIDERS"][#CONFIG["EMBEDDING_PROVIDERS"]+1] = { lines.Add($" [\"Host\"] = \"{this.Host}\",");
["Id"] = "{{Guid.NewGuid().ToString()}}", lines.Add($" [\"Hostname\"] = \"{LuaTools.EscapeLuaString(this.Hostname)}\",");
["Name"] = "{{LuaTools.EscapeLuaString(this.Name)}}",
["UsedLLMProvider"] = "{{this.UsedLLMProvider}}", if (!string.IsNullOrWhiteSpace(encryptedApiKey))
lines.Add($" [\"APIKey\"] = \"{LuaTools.EscapeLuaString(encryptedApiKey)}\",");
["TokenizerPath"] = "{{this.TokenizerPath}}",
lines.Add(" [\"Model\"] = {");
["Host"] = "{{this.Host}}", lines.Add($" [\"Id\"] = \"{LuaTools.EscapeLuaString(this.Model.Id)}\",");
["Hostname"] = "{{LuaTools.EscapeLuaString(this.Hostname)}}", lines.Add($" [\"DisplayName\"] = \"{LuaTools.EscapeLuaString(this.Model.DisplayName ?? string.Empty)}\",");
{{apiKeyLine}} lines.Add(" },");
["Model"] = { lines.Add("}");
["Id"] = "{{LuaTools.EscapeLuaString(this.Model.Id)}}",
["DisplayName"] = "{{LuaTools.EscapeLuaString(this.Model.DisplayName ?? string.Empty)}}", return string.Join(Environment.NewLine, lines);
},
}
""";
} }
} }

View File

@ -233,40 +233,40 @@ public sealed record Provider(
/// <returns>A Lua configuration section string.</returns> /// <returns>A Lua configuration section string.</returns>
public string ExportAsConfigurationSection(string? encryptedApiKey = null) public string ExportAsConfigurationSection(string? encryptedApiKey = null)
{ {
var hfInferenceProviderLine = string.Empty; var lines = new List<string>
{
"CONFIG[\"LLM_PROVIDERS\"][#CONFIG[\"LLM_PROVIDERS\"]+1] = {",
$" [\"Id\"] = \"{Guid.NewGuid()}\",",
$" [\"InstanceName\"] = \"{LuaTools.EscapeLuaString(this.InstanceName)}\",",
$" [\"UsedLLMProvider\"] = \"{this.UsedLLMProvider}\",",
};
if (!string.IsNullOrWhiteSpace(this.TokenizerPath))
{
lines.Add(string.Empty);
lines.Add(" -- The tokenizer path shown is local to this model. To use it with the plugin:");
lines.Add(" -- 1. Copy the tokenizer into the zip.");
lines.Add(" -- 2. Update the path in the plugin file to be relative to the zip's root.");
lines.Add($" [\"TokenizerPath\"] = \"{LuaTools.EscapeLuaString(this.TokenizerPath)}\",");
}
lines.Add(string.Empty);
lines.Add($" [\"Host\"] = \"{this.Host}\",");
lines.Add($" [\"Hostname\"] = \"{LuaTools.EscapeLuaString(this.Hostname)}\",");
if (this.HFInferenceProvider is not HFInferenceProvider.NONE) if (this.HFInferenceProvider is not HFInferenceProvider.NONE)
{ lines.Add($" [\"HFInferenceProvider\"] = \"{this.HFInferenceProvider}\",");
hfInferenceProviderLine = $"""
["HFInferenceProvider"] = "{this.HFInferenceProvider}",
""";
}
var apiKeyLine = string.Empty;
if (!string.IsNullOrWhiteSpace(encryptedApiKey)) if (!string.IsNullOrWhiteSpace(encryptedApiKey))
{ lines.Add($" [\"APIKey\"] = \"{LuaTools.EscapeLuaString(encryptedApiKey)}\",");
apiKeyLine = $"""
["APIKey"] = "{LuaTools.EscapeLuaString(encryptedApiKey)}",
""";
}
return $$""" lines.Add($" [\"AdditionalJsonApiParameters\"] = \"{LuaTools.EscapeLuaString(this.AdditionalJsonApiParameters)}\",");
CONFIG["LLM_PROVIDERS"][#CONFIG["LLM_PROVIDERS"]+1] = { lines.Add(" [\"Model\"] = {");
["Id"] = "{{Guid.NewGuid().ToString()}}", lines.Add($" [\"Id\"] = \"{LuaTools.EscapeLuaString(this.Model.Id)}\",");
["InstanceName"] = "{{LuaTools.EscapeLuaString(this.InstanceName)}}", lines.Add($" [\"DisplayName\"] = \"{LuaTools.EscapeLuaString(this.Model.DisplayName ?? this.Model.Id)}\",");
["UsedLLMProvider"] = "{{this.UsedLLMProvider}}", lines.Add(" },");
lines.Add("}");
["TokenizerPath"] = "{{this.TokenizerPath}}",
["Host"] = "{{this.Host}}", return string.Join(Environment.NewLine, lines);
["Hostname"] = "{{LuaTools.EscapeLuaString(this.Hostname)}}",
{{hfInferenceProviderLine}}
{{apiKeyLine}}
["AdditionalJsonApiParameters"] = "{{LuaTools.EscapeLuaString(this.AdditionalJsonApiParameters)}}",
["Model"] = {
["Id"] = "{{LuaTools.EscapeLuaString(this.Model.Id)}}",
["DisplayName"] = "{{LuaTools.EscapeLuaString(this.Model.DisplayName ?? this.Model.Id)}}",
},
}
""";
} }
} }

View File

@ -167,28 +167,26 @@ public sealed record TranscriptionProvider(
/// <returns>A Lua configuration section string.</returns> /// <returns>A Lua configuration section string.</returns>
public string ExportAsConfigurationSection(string? encryptedApiKey = null) public string ExportAsConfigurationSection(string? encryptedApiKey = null)
{ {
var apiKeyLine = string.Empty; var lines = new List<string>
if (!string.IsNullOrWhiteSpace(encryptedApiKey))
{ {
apiKeyLine = $""" "CONFIG[\"TRANSCRIPTION_PROVIDERS\"][#CONFIG[\"TRANSCRIPTION_PROVIDERS\"]+1] = {",
["APIKey"] = "{LuaTools.EscapeLuaString(encryptedApiKey)}", $" [\"Id\"] = \"{Guid.NewGuid()}\",",
"""; $" [\"Name\"] = \"{LuaTools.EscapeLuaString(this.Name)}\",",
} $" [\"UsedLLMProvider\"] = \"{this.UsedLLMProvider}\",",
string.Empty,
$" [\"Host\"] = \"{this.Host}\",",
$" [\"Hostname\"] = \"{LuaTools.EscapeLuaString(this.Hostname)}\",",
};
return $$""" if (!string.IsNullOrWhiteSpace(encryptedApiKey))
CONFIG["TRANSCRIPTION_PROVIDERS"][#CONFIG["TRANSCRIPTION_PROVIDERS"]+1] = { lines.Add($" [\"APIKey\"] = \"{LuaTools.EscapeLuaString(encryptedApiKey)}\",");
["Id"] = "{{Guid.NewGuid().ToString()}}",
["Name"] = "{{LuaTools.EscapeLuaString(this.Name)}}",
["UsedLLMProvider"] = "{{this.UsedLLMProvider}}",
["Host"] = "{{this.Host}}", lines.Add(" [\"Model\"] = {");
["Hostname"] = "{{LuaTools.EscapeLuaString(this.Hostname)}}", lines.Add($" [\"Id\"] = \"{LuaTools.EscapeLuaString(this.Model.Id)}\",");
{{apiKeyLine}} lines.Add($" [\"DisplayName\"] = \"{LuaTools.EscapeLuaString(this.Model.DisplayName ?? string.Empty)}\",");
["Model"] = { lines.Add(" },");
["Id"] = "{{LuaTools.EscapeLuaString(this.Model.Id)}}", lines.Add("}");
["DisplayName"] = "{{LuaTools.EscapeLuaString(this.Model.DisplayName ?? string.Empty)}}",
}, return string.Join(Environment.NewLine, lines);
}
""";
} }
} }