From 485f19572b346a26812a80ac1de93f180ac07c47 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Sun, 25 Jan 2026 14:09:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=88=E5=B9=B6=E7=9F=A5=E8=AF=86?= =?UTF-8?q?=E5=BA=93=E7=9B=AE=E5=BD=95=E4=B8=8E=E5=86=85=E5=AE=B9=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原有“目录查询”和“按目录获取内容”两个工具合并为单一接口,一次性返回所有目录及对应内容,简化调用方式;新增统一的知识库项模型,并补充异常与失败场景的日志与兜底处理。 --- .../Mcp/YxaiKnowledgeTool.cs | 126 +++++++++++------- 1 file changed, 79 insertions(+), 47 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs index cbaf42cf..42f6b198 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs @@ -26,64 +26,80 @@ public class YxaiKnowledgeTool : ISingletonDependency _logger = logger; } - [YiAgentTool("意心Ai平台知识库目录"), DisplayName("YxaiKnowledgeDirectory"), - Description("获取意心AI相关内容的知识库目录列表")] - public async Task> YxaiKnowledgeDirectory() + [YiAgentTool("意心Ai平台知识库"), DisplayName("YxaiKnowledge"), + Description("获取意心AI相关内容的知识库目录及内容列表")] + public async Task> YxaiKnowledge() { try { var client = _httpClientFactory.CreateClient(); - var response = await client.GetAsync(DirectoryUrl); - if (!response.IsSuccessStatusCode) + // 1. 先获取目录列表 + var directoryResponse = await client.GetAsync(DirectoryUrl); + if (!directoryResponse.IsSuccessStatusCode) { - _logger.LogError("意心知识库目录接口调用失败: {StatusCode}", response.StatusCode); - return new List(); + _logger.LogError("意心知识库目录接口调用失败: {StatusCode}", directoryResponse.StatusCode); + return new List(); } - var json = await response.Content.ReadAsStringAsync(); - var result = JsonSerializer.Deserialize(json, YxaiKnowledgeJsonContext.Default.ListYxaiKnowledgeDirectoryItem); + var directoryJson = await directoryResponse.Content.ReadAsStringAsync(); + var directories = JsonSerializer.Deserialize(directoryJson, + YxaiKnowledgeJsonContext.Default.ListYxaiKnowledgeDirectoryItem); - return result ?? new List(); + if (directories == null || directories.Count == 0) + { + return new List(); + } + + // 2. 循环调用内容接口获取每个目录的内容 + var result = new List(); + foreach (var directory in directories) + { + try + { + var contentUrl = string.Format(ContentUrlTemplate, directory.Id); + var contentResponse = await client.GetAsync(contentUrl); + + if (contentResponse.IsSuccessStatusCode) + { + var contentJson = await contentResponse.Content.ReadAsStringAsync(); + var contentResult = JsonSerializer.Deserialize(contentJson, + YxaiKnowledgeJsonContext.Default.YxaiKnowledgeContentResponse); + + result.Add(new YxaiKnowledgeItem + { + Name = directory.Name, + Content = contentResult?.Content ?? "" + }); + } + else + { + _logger.LogWarning("获取知识库内容失败: {StatusCode}, DirectoryId: {DirectoryId}", + contentResponse.StatusCode, directory.Id); + result.Add(new YxaiKnowledgeItem + { + Name = directory.Name, + Content = $"获取内容失败: {contentResponse.StatusCode}" + }); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "获取知识库内容发生异常, DirectoryId: {DirectoryId}", directory.Id); + result.Add(new YxaiKnowledgeItem + { + Name = directory.Name, + Content = "获取内容发生异常" + }); + } + } + + return result; } catch (Exception ex) { - _logger.LogError(ex, "获取意心知识库目录发生异常"); - return new List(); - } - } - - [YiAgentTool("意心Ai平台知识库内容"), DisplayName("YxaiKnowledge"), - Description("根据目录ID获取意心AI知识库的具体内容")] - public async Task YxaiKnowledge([Description("知识库目录ID")] string directoryId) - { - if (string.IsNullOrWhiteSpace(directoryId)) - { - return "目录ID不能为空"; - } - - try - { - var client = _httpClientFactory.CreateClient(); - var url = string.Format(ContentUrlTemplate, directoryId); - var response = await client.GetAsync(url); - - if (!response.IsSuccessStatusCode) - { - _logger.LogError("意心知识库内容接口调用失败: {StatusCode}, DirectoryId: {DirectoryId}", - response.StatusCode, directoryId); - return "获取知识库内容失败"; - } - - var json = await response.Content.ReadAsStringAsync(); - var result = JsonSerializer.Deserialize(json, YxaiKnowledgeJsonContext.Default.YxaiKnowledgeContentResponse); - - return result?.Content ?? "未找到相关内容"; - } - catch (Exception ex) - { - _logger.LogError(ex, "获取意心知识库内容发生异常, DirectoryId: {DirectoryId}", directoryId); - return "获取知识库内容发生异常"; + _logger.LogError(ex, "获取意心知识库发生异常"); + return new List(); } } } @@ -102,6 +118,22 @@ public class YxaiKnowledgeContentResponse [JsonPropertyName("content")] public string? Content { get; set; } } +/// +/// 合并后的知识库项,包含目录和内容 +/// +public class YxaiKnowledgeItem +{ + /// + /// 目录名称 + /// + public string Name { get; set; } = ""; + + /// + /// 知识库内容 + /// + public string Content { get; set; } = ""; +} + #endregion #region JSON 序列化上下文 @@ -112,4 +144,4 @@ internal partial class YxaiKnowledgeJsonContext : JsonSerializerContext { } -#endregion +#endregion \ No newline at end of file