using MAF1.Agents; namespace MAF1.Route; /// 控制台里用户原话。路径还是描述,交给专家自己判断,不在 C# 里写死。 public sealed class RouteUserInput { public string Raw { get; init; } = ""; public string? ResolvedFilePath { get; init; } public bool HasRaw => !string.IsNullOrWhiteSpace(Raw); public string Describe() { if (!string.IsNullOrWhiteSpace(ResolvedFilePath)) { return $"已解析到文件 {ResolvedFilePath}"; } string preview = Raw.Replace("\r\n", " ").Replace('\n', ' ').Trim(); if (preview.Length > 80) { preview = preview[..80] + "…"; } return $"用户原话「{preview}」"; } public string ToTask() => $""" 请根据用户下面这句话完成任务:提取有效城市,并在图允许时查询天气。 若这句话是本地文件路径,就读文件;若是描述、问句或城市名单,就直接从这句话提取,不要读文件。 用户输入: {Raw} """; public static RouteUserInput ReadFromConsole(RouteGraph graph) { string? fallback = graph.Nodes .Select(n => n.Type == SystemNodeTypes.FileCity && n.Config.TryGetValue("filePath", out string? path) ? path : null) .FirstOrDefault(p => !string.IsNullOrWhiteSpace(p)); Console.WriteLine("请输入:本地文件路径,或一段描述/城市名单(例如:帮我查成都和杭州的天气)。"); if (!string.IsNullOrWhiteSpace(fallback)) { Console.WriteLine($"直接回车则使用图里的默认文件:{fallback}"); } Console.Write("> "); string? line = Console.ReadLine(); string trimmed = line?.Trim() ?? ""; if (trimmed.Length == 0) { trimmed = fallback ?? ""; } return new RouteUserInput { Raw = trimmed, ResolvedFilePath = ResolveExisting(trimmed), }; } public static string? ResolveExisting(string path) { if (string.IsNullOrWhiteSpace(path)) { return null; } try { if (path.IndexOfAny(Path.GetInvalidPathChars()) >= 0) { return null; } } catch (ArgumentException) { return null; } if (File.Exists(path)) { return Path.GetFullPath(path); } string fromBase = Path.Combine(AppContext.BaseDirectory, path); if (File.Exists(fromBase)) { return Path.GetFullPath(fromBase); } return null; } }