Files
Yi.Framework/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs
ccnetcore 6b86957556 fix: 修复工具调用用量关联错误并优化细节配置
- 修复前端工具调用中用量统计先后顺序导致未正确绑定的问题
- 优化聊天代理指令文案,补充平台知识库优先策略说明
- 调整聊天列表滚动条样式,提升界面体验
- 移除未使用的 VITE_BUILD_COMPRESS 类型声明
2026-01-24 01:16:38 +08:00

116 lines
3.9 KiB
C#

using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection;
using Yi.Framework.AiHub.Domain.Shared.Attributes;
namespace Yi.Framework.AiHub.Domain.Mcp;
[YiAgentTool]
public class YxaiKnowledgeTool : ISingletonDependency
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<YxaiKnowledgeTool> _logger;
private const string DirectoryUrl =
"https://ccnetcore.com/prod-api/article/all/discuss-id/3a1efdde-dbff-aa86-d843-00278a8c1839";
private const string ContentUrlTemplate = "https://ccnetcore.com/prod-api/article/{0}";
public YxaiKnowledgeTool(
IHttpClientFactory httpClientFactory,
ILogger<YxaiKnowledgeTool> logger)
{
_httpClientFactory = httpClientFactory;
_logger = logger;
}
[YiAgentTool("意心Ai平台知识库目录"), DisplayName("YxaiKnowledgeDirectory"),
Description("获取意心AI相关内容的知识库目录列表")]
public async Task<List<YxaiKnowledgeDirectoryItem>> YxaiKnowledgeDirectory()
{
try
{
var client = _httpClientFactory.CreateClient();
var response = await client.GetAsync(DirectoryUrl);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("意心知识库目录接口调用失败: {StatusCode}", response.StatusCode);
return new List<YxaiKnowledgeDirectoryItem>();
}
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize(json, YxaiKnowledgeJsonContext.Default.ListYxaiKnowledgeDirectoryItem);
return result ?? new List<YxaiKnowledgeDirectoryItem>();
}
catch (Exception ex)
{
_logger.LogError(ex, "获取意心知识库目录发生异常");
return new List<YxaiKnowledgeDirectoryItem>();
}
}
[YiAgentTool("意心Ai平台知识库内容"), DisplayName("YxaiKnowledge"),
Description("根据目录ID获取意心AI知识库的具体内容")]
public async Task<string> 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 "获取知识库内容发生异常";
}
}
}
#region DTO
public class YxaiKnowledgeDirectoryItem
{
[JsonPropertyName("id")] public string Id { get; set; } = "";
[JsonPropertyName("name")] public string Name { get; set; } = "";
}
public class YxaiKnowledgeContentResponse
{
[JsonPropertyName("content")] public string? Content { get; set; }
}
#endregion
#region JSON
[JsonSerializable(typeof(List<YxaiKnowledgeDirectoryItem>))]
[JsonSerializable(typeof(YxaiKnowledgeContentResponse))]
internal partial class YxaiKnowledgeJsonContext : JsonSerializerContext
{
}
#endregion