using System.Text.Json;
namespace MAF1.Agents;
///
/// 一次 Agent 执行的统一结果。网页编排器和进程外插件都用这套字段,方便画布把输出接到下一节点。
/// Message 是给日志看的原文;Outputs 才是下游节点真正读取的端口数据。
///
public sealed class AgentStepResult
{
public string Message { get; init; } = "";
public Dictionary Inputs { get; init; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary Outputs { get; init; } = new(StringComparer.OrdinalIgnoreCase);
}
///
/// 从图 JSON / 插件 stdin 读入参。值可能是 string、数组,也可能是 System.Text.Json 反序列化后的 JsonElement。
///
public static class AgentInputs
{
/// 按端口名读一个字符串;没有该键则返回 null。
public static string? ReadString(IReadOnlyDictionary inputs, string key)
{
if (!inputs.TryGetValue(key, out object? value) || value is null)
{
return null;
}
if (value is JsonElement el)
{
return el.ValueKind == JsonValueKind.String ? el.GetString() : el.ToString();
}
return value.ToString();
}
/// 读城市列表:支持 JSON 数组、["a","b"] 风格,或用顿号/逗号分隔的一串文字。
public static List ReadStringList(IReadOnlyDictionary inputs, string key)
{
if (!inputs.TryGetValue(key, out object? value) || value is null)
{
return [];
}
if (value is JsonElement el)
{
if (el.ValueKind == JsonValueKind.Array)
{
return el.EnumerateArray()
.Select(item => item.ValueKind == JsonValueKind.String ? item.GetString() : item.ToString())
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s => s!.Trim())
.ToList();
}
if (el.ValueKind == JsonValueKind.String)
{
return Split(el.GetString());
}
}
if (value is IEnumerable typed)
{
return typed.Where(v => !string.IsNullOrWhiteSpace(v)).Select(v => v.Trim()).ToList();
}
if (value is IEnumerable