feat: 将插件 stdin 协议定为 v1,并按声明注入凭据
进程外插件改为只读环境变量和自身配置,避免读宿主 appsettings;宿主按 plugin.json 声明注入 LLM / OpenWeather 等凭据。
This commit is contained in:
@@ -16,14 +16,16 @@ public sealed class PluginProcessRunner(PluginOptions options, CredentialStore c
|
||||
LoadedPlugin plugin,
|
||||
Dictionary<string, object?> inputs,
|
||||
string? credentialId,
|
||||
IReadOnlyDictionary<string, string>? credentialBindings,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
PluginManifest manifest = plugin.Manifest;
|
||||
ValidateDeclaredInputs(manifest, inputs);
|
||||
|
||||
Dictionary<string, PluginCredentialPayload> payload = ResolveCredentials(manifest, credentialId);
|
||||
Dictionary<string, PluginCredentialPayload> payload = ResolveCredentials(manifest, credentialId, credentialBindings);
|
||||
PluginRequest request = new()
|
||||
{
|
||||
ProtocolVersion = PluginProtocol.Normalize(manifest.ProtocolVersion),
|
||||
Inputs = inputs,
|
||||
Credentials = payload,
|
||||
};
|
||||
@@ -75,33 +77,58 @@ public sealed class PluginProcessRunner(PluginOptions options, CredentialStore c
|
||||
return outputs;
|
||||
}
|
||||
|
||||
private Dictionary<string, PluginCredentialPayload> ResolveCredentials(PluginManifest manifest, string? credentialId)
|
||||
private Dictionary<string, PluginCredentialPayload> ResolveCredentials(
|
||||
PluginManifest manifest,
|
||||
string? credentialId,
|
||||
IReadOnlyDictionary<string, string>? credentialBindings)
|
||||
{
|
||||
Dictionary<string, PluginCredentialPayload> map = new(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (PluginCredentialNeed need in manifest.Credentials)
|
||||
{
|
||||
if (need.Type is "openai-compatible" or "llm" || need.Name.Equals("llm", StringComparison.OrdinalIgnoreCase))
|
||||
if (string.IsNullOrWhiteSpace(need.Type) && string.IsNullOrWhiteSpace(need.Name))
|
||||
{
|
||||
CredentialRecord record = credentials.Resolve(credentialId);
|
||||
if (need.Required && string.IsNullOrWhiteSpace(record.ApiKey))
|
||||
continue;
|
||||
}
|
||||
|
||||
string declaredType = string.IsNullOrWhiteSpace(need.Type) ? need.Name : need.Type;
|
||||
string? preferredId = null;
|
||||
if (credentialBindings is not null
|
||||
&& !string.IsNullOrWhiteSpace(need.Name)
|
||||
&& credentialBindings.TryGetValue(need.Name, out string? bound)
|
||||
&& !string.IsNullOrWhiteSpace(bound))
|
||||
{
|
||||
preferredId = bound;
|
||||
}
|
||||
else if (PluginCredentialTypes.IsLlm(declaredType) || need.Name.Equals("llm", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
preferredId = credentialId;
|
||||
}
|
||||
|
||||
CredentialRecord? record = credentials.TryResolveForType(declaredType, preferredId);
|
||||
if (record is null)
|
||||
{
|
||||
if (need.Required)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"插件 {manifest.Id} 需要 LLM 凭据,但 `{record.Id}` 没有 API Key。请在环境变量 OPENAI_API_KEY 或 appsettings.json 中配置。");
|
||||
$"插件 {manifest.Id} 需要类型 `{declaredType}` 的凭据 `{need.Name}`,但宿主 Credentials 里没有匹配项。");
|
||||
}
|
||||
|
||||
map[need.Name] = credentials.ToPayload(record);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (manifest.Credentials.Count == 0)
|
||||
{
|
||||
map["llm"] = credentials.ToPayload(credentials.Resolve(credentialId));
|
||||
if (need.Required && PluginCredentialTypes.IsLlm(declaredType) && string.IsNullOrWhiteSpace(record.ApiKey))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"插件 {manifest.Id} 需要 LLM 凭据,但 `{record.Id}` 没有 API Key。请在环境变量 OPENAI_API_KEY 或 appsettings.json 中配置。");
|
||||
}
|
||||
|
||||
map[string.IsNullOrWhiteSpace(need.Name) ? declaredType : need.Name] = credentials.ToPayload(record);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// <summary>工作目录=插件文件夹;环境变量带上 OPENAI_* 和 MAF1_CONTENT_ROOT。</summary>
|
||||
/// <summary>工作目录=插件文件夹。凭据写入子进程环境变量,不读宿主 appsettings 路径。</summary>
|
||||
private ProcessStartInfo CreateStartInfo(LoadedPlugin plugin, Dictionary<string, PluginCredentialPayload> payload)
|
||||
{
|
||||
string command = plugin.Manifest.Launch.Command;
|
||||
@@ -132,32 +159,13 @@ public sealed class PluginProcessRunner(PluginOptions options, CredentialStore c
|
||||
start.ArgumentList.Add(arg);
|
||||
}
|
||||
|
||||
start.Environment["MAF1_CONTENT_ROOT"] = AppContext.BaseDirectory;
|
||||
ApplyDotEnv(plugin.FolderPath, start);
|
||||
foreach (KeyValuePair<string, string> pair in plugin.Manifest.Env)
|
||||
{
|
||||
start.Environment[pair.Key] = pair.Value;
|
||||
}
|
||||
|
||||
if (payload.TryGetValue("llm", out PluginCredentialPayload? llm) || payload.Count > 0)
|
||||
{
|
||||
llm ??= payload.Values.First();
|
||||
if (!string.IsNullOrWhiteSpace(llm.ApiKey))
|
||||
{
|
||||
start.Environment["OPENAI_API_KEY"] = llm.ApiKey;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(llm.Endpoint))
|
||||
{
|
||||
start.Environment["OPENAI_ENDPOINT"] = llm.Endpoint;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(llm.Model))
|
||||
{
|
||||
start.Environment["OPENAI_CHAT_MODEL"] = llm.Model;
|
||||
}
|
||||
}
|
||||
|
||||
PluginStdio.ApplyCredentialsToEnvironment(payload, start.Environment);
|
||||
return start;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user