feat: 将插件 stdin 协议定为 v1,并按声明注入凭据
进程外插件改为只读环境变量和自身配置,避免读宿主 appsettings;宿主按 plugin.json 声明注入 LLM / OpenWeather 等凭据。
This commit is contained in:
@@ -32,18 +32,16 @@ public sealed class CredentialPublicView
|
||||
/// </summary>
|
||||
public sealed class CredentialStore
|
||||
{
|
||||
private readonly IConfiguration _config;
|
||||
private readonly Dictionary<string, CredentialRecord> _named = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public CredentialStore(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
LlmOptions llm = AgentFactory.Load(config);
|
||||
_named["llm-default"] = new CredentialRecord
|
||||
{
|
||||
Id = "llm-default",
|
||||
Name = "系统默认模型",
|
||||
Type = "openai-compatible",
|
||||
Type = PluginCredentialTypes.OpenAiCompatible,
|
||||
Endpoint = llm.Endpoint,
|
||||
ApiKey = llm.ApiKey,
|
||||
Model = llm.Model,
|
||||
@@ -53,23 +51,38 @@ public sealed class CredentialStore
|
||||
foreach (IConfigurationSection child in section.GetChildren())
|
||||
{
|
||||
string id = child.Key;
|
||||
string type = child["type"] ?? "openai-compatible";
|
||||
string type = child["type"] ?? PluginCredentialTypes.OpenAiCompatible;
|
||||
bool useLlm = string.Equals(child["source"], "llm-section", StringComparison.OrdinalIgnoreCase)
|
||||
|| child.GetValue("useLlmSection", false);
|
||||
CredentialRecord fallback = _named.GetValueOrDefault("llm-default")!;
|
||||
CredentialRecord fallback = _named["llm-default"];
|
||||
Dictionary<string, string> extra = new(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (IConfigurationSection extraChild in child.GetSection("extra").GetChildren())
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(extraChild.Value))
|
||||
{
|
||||
extra[extraChild.Key] = extraChild.Value;
|
||||
}
|
||||
}
|
||||
|
||||
string apiKey = First(
|
||||
child["apiKey"],
|
||||
useLlm ? fallback.ApiKey : null,
|
||||
PluginCredentialTypes.IsOpenWeather(type) ? Environment.GetEnvironmentVariable("OPENWEATHER_API_KEY") : null);
|
||||
|
||||
_named[id] = new CredentialRecord
|
||||
{
|
||||
Id = id,
|
||||
Name = child["name"] ?? id,
|
||||
Type = type,
|
||||
Endpoint = First(child["endpoint"], useLlm ? fallback.Endpoint : null),
|
||||
ApiKey = First(child["apiKey"], useLlm ? fallback.ApiKey : null),
|
||||
ApiKey = apiKey,
|
||||
Model = First(child["model"], useLlm ? fallback.Model : null),
|
||||
Extra = extra,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>给前端的列表。Endpoint 目前未打码(学习项目);ApiKey 绝不会出现在这里。</summary>
|
||||
/// <summary>给前端的列表。ApiKey 绝不会出现在这里。</summary>
|
||||
public IReadOnlyList<CredentialPublicView> ListPublic()
|
||||
=> _named.Values
|
||||
.OrderBy(item => item.Id, StringComparer.OrdinalIgnoreCase)
|
||||
@@ -91,6 +104,23 @@ public sealed class CredentialStore
|
||||
throw new InvalidOperationException($"找不到凭据 `{id}`。请在 appsettings.json 的 Credentials 中配置,或改用 llm-default。");
|
||||
}
|
||||
|
||||
/// <summary>按插件声明的 type 取凭据。preferredId 类型不匹配时不会凑合用。</summary>
|
||||
public CredentialRecord? TryResolveForType(string declaredType, string? preferredId)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(preferredId) && _named.TryGetValue(preferredId, out CredentialRecord? named))
|
||||
{
|
||||
if (!PluginCredentialTypes.Matches(declaredType, named.Type))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"凭据 `{preferredId}` 的类型是 `{named.Type}`,插件声明需要 `{declaredType}`。");
|
||||
}
|
||||
|
||||
return named;
|
||||
}
|
||||
|
||||
return _named.Values.FirstOrDefault(item => PluginCredentialTypes.Matches(declaredType, item.Type));
|
||||
}
|
||||
|
||||
public PluginCredentialPayload ToPayload(CredentialRecord record)
|
||||
=> new()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user