feat: 拆分 CLI 与网页宿主,并在无有效城市时暂停等人确认

将原单体 MAF1 拆成 MAF1(控制台工作流)和 MAF1.Web(可视化编排)。
抽城市失败时暂停:网页弹确认、CLI 控制台询问,超时采用默认结束查询;
也可改填城市后继续。共享决策模型放在 MAF1.Core。
This commit is contained in:
2026-08-28 10:43:57 +08:00
parent 5544788917
commit a3cca78592
42 changed files with 1427 additions and 457 deletions
+115
View File
@@ -0,0 +1,115 @@
using MAF1.PluginContract;
using MAF1.Utils;
using Microsoft.Extensions.Configuration;
namespace MAF1.Plugins;
public sealed class CredentialRecord
{
public string Id { get; init; } = "";
public string Name { get; init; } = "";
public string Type { get; init; } = "";
public string Endpoint { get; init; } = "";
public string ApiKey { get; init; } = "";
public string Model { get; init; } = "";
public Dictionary<string, string> Extra { get; init; } = new(StringComparer.OrdinalIgnoreCase);
}
public sealed class CredentialPublicView
{
public string Id { get; init; } = "";
public string Name { get; init; } = "";
public string Type { get; init; } = "";
public string Endpoint { get; init; } = "";
public string Model { get; init; } = "";
public bool HasApiKey { get; init; }
}
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",
Endpoint = llm.Endpoint,
ApiKey = llm.ApiKey,
Model = llm.Model,
};
IConfigurationSection section = config.GetSection("Credentials");
foreach (IConfigurationSection child in section.GetChildren())
{
string id = child.Key;
string type = child["type"] ?? "openai-compatible";
bool useLlm = string.Equals(child["source"], "llm-section", StringComparison.OrdinalIgnoreCase)
|| child.GetValue("useLlmSection", false);
CredentialRecord fallback = _named.GetValueOrDefault("llm-default")!;
_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),
Model = First(child["model"], useLlm ? fallback.Model : null),
};
}
}
public IReadOnlyList<CredentialPublicView> ListPublic()
=> _named.Values
.OrderBy(item => item.Id, StringComparer.OrdinalIgnoreCase)
.Select(ToPublic)
.ToList();
public CredentialRecord Resolve(string? id)
{
if (string.IsNullOrWhiteSpace(id))
{
return _named["llm-default"];
}
if (_named.TryGetValue(id, out CredentialRecord? record))
{
return record;
}
throw new InvalidOperationException($"找不到凭据 `{id}`。请在 appsettings.json 的 Credentials 中配置,或改用 llm-default。");
}
public PluginCredentialPayload ToPayload(CredentialRecord record)
=> new()
{
Id = record.Id,
Type = record.Type,
Endpoint = record.Endpoint,
ApiKey = record.ApiKey,
Model = record.Model,
Extra = record.Extra,
};
public static CredentialPublicView ToPublic(CredentialRecord record)
=> new()
{
Id = record.Id,
Name = record.Name,
Type = record.Type,
Endpoint = MaskEndpoint(record.Endpoint),
Model = record.Model,
HasApiKey = !string.IsNullOrWhiteSpace(record.ApiKey),
};
private static string MaskEndpoint(string endpoint)
=> endpoint;
private static string First(params string?[] values)
=> values.FirstOrDefault(v => !string.IsNullOrWhiteSpace(v)) ?? "";
}
+111
View File
@@ -0,0 +1,111 @@
using MAF1.PluginContract;
using MAF1.Web;
namespace MAF1.Plugins;
public sealed class NodeCatalogSnapshot
{
public string PluginsRoot { get; init; } = "";
public List<AgentTypeInfo> System { get; init; } = [];
public List<AgentTypeInfo> Plugins { get; init; } = [];
public List<AgentTypeInfo> Nodes { get; init; } = [];
public List<CatalogIssue> Issues { get; init; } = [];
public IReadOnlyList<LoadedPlugin> LoadedPlugins { get; init; } = [];
}
public sealed class NodeCatalogService(PluginScanner scanner)
{
public NodeCatalogSnapshot Load()
{
PluginScanResult scan = scanner.Scan();
List<CatalogIssue> issues = [.. scan.Issues];
List<AgentTypeInfo> system = AgentCatalog.SystemNodes
.Select(Clone)
.ToList();
foreach (AgentTypeInfo item in system)
{
item.Origin = "system";
}
Dictionary<string, AgentTypeInfo> systemByType = system.ToDictionary(n => n.Type, StringComparer.OrdinalIgnoreCase);
List<AgentTypeInfo> plugins = [];
foreach (LoadedPlugin plugin in scan.Plugins)
{
AgentTypeInfo info = ToTypeInfo(plugin);
if (systemByType.ContainsKey(info.Type))
{
issues.Add(new CatalogIssue
{
Level = "info",
Source = info.Type,
Message = $"插件 `{info.Type}` 覆盖了同名系统节点,画布运行将走插件进程。",
});
systemByType[info.Type].Overridden = true;
}
plugins.Add(info);
}
Dictionary<string, AgentTypeInfo> merged = systemByType
.ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase);
foreach (AgentTypeInfo plugin in plugins)
{
merged[plugin.Type] = plugin;
}
return new NodeCatalogSnapshot
{
PluginsRoot = scan.Root,
System = system,
Plugins = plugins,
Nodes = merged.Values.OrderBy(n => n.Origin).ThenBy(n => n.Name).ToList(),
Issues = issues,
LoadedPlugins = scan.Plugins,
};
}
public LoadedPlugin? FindPlugin(string type)
=> Load().LoadedPlugins.FirstOrDefault(p => p.Manifest.Id.Equals(type, StringComparison.OrdinalIgnoreCase));
private static AgentTypeInfo ToTypeInfo(LoadedPlugin plugin)
{
PluginManifest manifest = plugin.Manifest;
return new AgentTypeInfo
{
Type = manifest.Id,
Name = string.IsNullOrWhiteSpace(manifest.Name) ? manifest.Id : manifest.Name,
Description = manifest.Description,
Origin = "plugin",
Version = manifest.Version,
Folder = plugin.FolderName,
Inputs = manifest.Inputs.Select(ToPort).ToList(),
Outputs = manifest.Outputs.Select(ToPort).ToList(),
Credentials = manifest.Credentials,
};
}
private static PortInfo ToPort(PluginPort port)
=> new()
{
Name = port.Name,
Type = port.Type,
Description = port.Description,
Required = port.Required,
};
private static AgentTypeInfo Clone(AgentTypeInfo source)
=> new()
{
Type = source.Type,
Name = source.Name,
Description = source.Description,
Origin = source.Origin,
Version = source.Version,
Folder = source.Folder,
Overridden = source.Overridden,
Handler = source.Handler,
Inputs = source.Inputs,
Outputs = source.Outputs,
Credentials = source.Credentials,
};
}
+36
View File
@@ -0,0 +1,36 @@
using Microsoft.Extensions.Configuration;
namespace MAF1.Plugins;
public sealed class PluginOptions
{
public string Directory { get; set; } = "plugins";
public int DefaultTimeoutSeconds { get; set; } = 180;
public string ResolveRoot()
{
if (Path.IsPathRooted(Directory))
{
return Directory;
}
return Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, Directory));
}
public static PluginOptions Load(IConfiguration config)
=> config.GetSection("Plugins").Get<PluginOptions>() ?? new PluginOptions();
}
public sealed class LoadedPlugin
{
public required string FolderName { get; init; }
public required string FolderPath { get; init; }
public required PluginContract.PluginManifest Manifest { get; init; }
}
public sealed class CatalogIssue
{
public string Level { get; init; } = "error";
public string Source { get; init; } = "";
public string Message { get; init; } = "";
}
+249
View File
@@ -0,0 +1,249 @@
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using MAF1.PluginContract;
using MAF1.Utils;
namespace MAF1.Plugins;
public sealed class PluginProcessRunner(PluginOptions options, CredentialStore credentials)
{
public async Task<Dictionary<string, object?>> RunAsync(
LoadedPlugin plugin,
Dictionary<string, object?> inputs,
string? credentialId,
CancellationToken cancellationToken)
{
PluginManifest manifest = plugin.Manifest;
ValidateDeclaredInputs(manifest, inputs);
Dictionary<string, PluginCredentialPayload> payload = ResolveCredentials(manifest, credentialId);
PluginRequest request = new()
{
Inputs = inputs,
Credentials = payload,
};
int timeoutSeconds = manifest.TimeoutSeconds > 0 ? manifest.TimeoutSeconds : options.DefaultTimeoutSeconds;
using CancellationTokenSource timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(TimeSpan.FromSeconds(timeoutSeconds));
ProcessStartInfo start = CreateStartInfo(plugin, payload);
using Process process = new() { StartInfo = start, EnableRaisingEvents = true };
if (!process.Start())
{
throw new InvalidOperationException($"无法启动插件 {manifest.Id}{start.FileName}");
}
string json = JsonSerializer.Serialize(request, PluginJson.Options);
await process.StandardInput.WriteAsync(json.AsMemory(), timeout.Token);
await process.StandardInput.FlushAsync(timeout.Token);
process.StandardInput.Close();
Task<string> stdoutTask = process.StandardOutput.ReadToEndAsync(timeout.Token);
Task<string> stderrTask = process.StandardError.ReadToEndAsync(timeout.Token);
try
{
await process.WaitForExitAsync(timeout.Token);
}
catch (OperationCanceledException)
{
TryKill(process);
throw new TimeoutException($"插件 {manifest.Id} 超过 {timeoutSeconds} 秒未结束,已终止。");
}
string stdout = await stdoutTask;
string stderr = await stderrTask;
if (process.ExitCode != 0)
{
throw new InvalidOperationException(
$"插件 {manifest.Id} 退出码 {process.ExitCode}。{FormatStd(stderr, stdout)}");
}
Dictionary<string, object?> outputs = ParseOutputs(stdout);
ValidateDeclaredOutputs(manifest, outputs);
if (!string.IsNullOrWhiteSpace(stderr))
{
outputs["_stderr"] = stderr.Trim();
}
return outputs;
}
private Dictionary<string, PluginCredentialPayload> ResolveCredentials(PluginManifest manifest, string? credentialId)
{
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))
{
CredentialRecord record = credentials.Resolve(credentialId);
if (need.Required && string.IsNullOrWhiteSpace(record.ApiKey))
{
throw new InvalidOperationException(
$"插件 {manifest.Id} 需要 LLM 凭据,但 `{record.Id}` 没有 API Key。请在环境变量 OPENAI_API_KEY 或 appsettings.json 中配置。");
}
map[need.Name] = credentials.ToPayload(record);
}
}
if (manifest.Credentials.Count == 0)
{
map["llm"] = credentials.ToPayload(credentials.Resolve(credentialId));
}
return map;
}
private ProcessStartInfo CreateStartInfo(LoadedPlugin plugin, Dictionary<string, PluginCredentialPayload> payload)
{
string command = plugin.Manifest.Launch.Command;
if (!Path.IsPathRooted(command))
{
string local = Path.Combine(plugin.FolderPath, command);
if (File.Exists(local))
{
command = local;
}
}
ProcessStartInfo start = new()
{
FileName = command,
WorkingDirectory = plugin.FolderPath,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardInputEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
};
foreach (string arg in plugin.Manifest.Launch.Args)
{
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;
}
}
return start;
}
private static void ApplyDotEnv(string pluginDir, ProcessStartInfo start)
{
string path = Path.Combine(pluginDir, ".env");
if (!File.Exists(path))
{
return;
}
foreach (string raw in File.ReadAllLines(path))
{
string line = raw.Trim();
if (line.Length == 0 || line.StartsWith('#') || !line.Contains('='))
{
continue;
}
int split = line.IndexOf('=');
string key = line[..split].Trim();
string value = line[(split + 1)..].Trim().Trim('"');
if (!string.IsNullOrWhiteSpace(key))
{
start.Environment[key] = value;
}
}
}
private static void ValidateDeclaredInputs(PluginManifest manifest, Dictionary<string, object?> inputs)
{
foreach (PluginPort port in manifest.Inputs.Where(p => p.Required))
{
if (!inputs.TryGetValue(port.Name, out object? value) || value is null || value.ToString() == "")
{
throw new InvalidOperationException(
$"插件 {manifest.Id} 的必填输入 `{port.Name}` 为空。请在节点配置里填写,或从上一节点连到这个端口。字段名必须和 plugin.json / 插件代码一致。");
}
}
}
private static void ValidateDeclaredOutputs(PluginManifest manifest, Dictionary<string, object?> outputs)
{
List<string> missing = manifest.Outputs
.Select(p => p.Name)
.Where(name => !outputs.Keys.Any(k => k.Equals(name, StringComparison.OrdinalIgnoreCase)))
.ToList();
if (missing.Count > 0)
{
throw new InvalidOperationException(
$"插件 {manifest.Id} 的输出字段与 plugin.json 不一致,缺少:{string.Join(", ", missing)}。请让插件 stdout 的 JSON 字段和清单 outputs 完全对应。");
}
}
private static Dictionary<string, object?> ParseOutputs(string stdout)
{
string json = JsonText.UnwrapObject(stdout);
using JsonDocument doc = JsonDocument.Parse(json);
if (doc.RootElement.ValueKind != JsonValueKind.Object)
{
throw new InvalidOperationException("插件 stdout 必须是一个 JSON 对象。");
}
Dictionary<string, object?> map = new(StringComparer.OrdinalIgnoreCase);
foreach (JsonProperty property in doc.RootElement.EnumerateObject())
{
map[property.Name] = property.Value.Clone();
}
return map;
}
private static string FormatStd(string stderr, string stdout)
{
string err = string.IsNullOrWhiteSpace(stderr) ? "" : $"stderr: {stderr.Trim()}";
string outText = string.IsNullOrWhiteSpace(stdout) ? "" : $"stdout: {stdout.Trim()}";
return string.Join(" ", new[] { err, outText }.Where(s => s.Length > 0));
}
private static void TryKill(Process process)
{
try
{
if (!process.HasExited)
{
process.Kill(entireProcessTree: true);
}
}
catch
{
// 进程可能已经退出。
}
}
}
+117
View File
@@ -0,0 +1,117 @@
using System.Text.Json;
using MAF1.PluginContract;
namespace MAF1.Plugins;
public sealed class PluginScanner(PluginOptions options)
{
public PluginScanResult Scan()
{
string root = options.ResolveRoot();
Directory.CreateDirectory(root);
List<LoadedPlugin> plugins = [];
List<CatalogIssue> issues = [];
HashSet<string> ids = new(StringComparer.OrdinalIgnoreCase);
foreach (string folder in Directory.GetDirectories(root))
{
string folderName = Path.GetFileName(folder);
if (folderName.StartsWith('_') || folderName.StartsWith('.'))
{
continue;
}
string manifestPath = Path.Combine(folder, "plugin.json");
if (!File.Exists(manifestPath))
{
issues.Add(new CatalogIssue
{
Level = "warning",
Source = folderName,
Message = "目录里没有 plugin.json,已跳过。",
});
continue;
}
PluginManifest? manifest;
try
{
string json = File.ReadAllText(manifestPath);
manifest = JsonSerializer.Deserialize<PluginManifest>(json, PluginJson.Options);
}
catch (Exception ex)
{
issues.Add(new CatalogIssue
{
Level = "error",
Source = folderName,
Message = $"plugin.json 无法解析:{ex.Message}",
});
continue;
}
if (manifest is null || string.IsNullOrWhiteSpace(manifest.Id))
{
issues.Add(new CatalogIssue
{
Level = "error",
Source = folderName,
Message = "plugin.json 缺少 id。",
});
continue;
}
if (string.IsNullOrWhiteSpace(manifest.Launch.Command))
{
issues.Add(new CatalogIssue
{
Level = "error",
Source = manifest.Id,
Message = "plugin.json 缺少 launch.command,宿主不会替插件拼命令。",
});
continue;
}
if (!ids.Add(manifest.Id))
{
issues.Add(new CatalogIssue
{
Level = "error",
Source = manifest.Id,
Message = $"插件 id `{manifest.Id}` 重复,已忽略目录 {folderName}。",
});
continue;
}
if (DuplicatePortNames(manifest.Inputs))
{
issues.Add(new CatalogIssue { Level = "error", Source = manifest.Id, Message = "inputs 字段名重复。" });
continue;
}
if (DuplicatePortNames(manifest.Outputs))
{
issues.Add(new CatalogIssue { Level = "error", Source = manifest.Id, Message = "outputs 字段名重复。" });
continue;
}
plugins.Add(new LoadedPlugin
{
FolderName = folderName,
FolderPath = folder,
Manifest = manifest,
});
}
return new PluginScanResult(root, plugins, issues);
}
private static bool DuplicatePortNames(List<PluginPort> ports)
=> ports.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase).Any(g => g.Count() > 1);
}
public sealed record PluginScanResult(
string Root,
IReadOnlyList<LoadedPlugin> Plugins,
IReadOnlyList<CatalogIssue> Issues);