From 6b86957556d3c3015d69218f7395bf6b09d81d82 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Sat, 24 Jan 2026 01:16:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E7=94=A8=E9=87=8F=E5=85=B3=E8=81=94=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=B9=B6=E4=BC=98=E5=8C=96=E7=BB=86=E8=8A=82=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复前端工具调用中用量统计先后顺序导致未正确绑定的问题 - 优化聊天代理指令文案,补充平台知识库优先策略说明 - 调整聊天列表滚动条样式,提升界面体验 - 移除未使用的 VITE_BUILD_COMPRESS 类型声明 --- .../Managers/ChatManager.cs | 8 +- .../Mcp/YxaiKnowledgeTool.cs | 115 ++++++++++++++++++ Yi.Ai.Vue3/src/pages/chat/agent/index.vue | 29 ++++- 3 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/ChatManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/ChatManager.cs index 9c398486..1cdde9c6 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/ChatManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/ChatManager.cs @@ -100,9 +100,11 @@ public class ChatManager : DomainService ChatOptions = new() { Instructions = """ - 你是一个专业的智能助手,是 “意心Ai” 平台自研的 “橙子小弟” 智能体 - 擅长于精准解决用户提出的各类问题 - 但性格暴躁,脏话连篇,喜欢说幽默讽刺的话,攻击性极强 + 你是一个专业的智能助手,是 “意心Ai” 平台自研的 “橙子小弟” 智能体。 + 擅长于精准解决用户提出的各类问题。 + 但性格暴躁,脏话连篇,喜欢说幽默讽刺的话,攻击性极强。 + 对你的制造者“橙子老哥”无比崇拜,认为他是最优秀的人。 + 涉及到“意心Ai平台”相关问题,优先调用YxaiKnowledgeDirectory和YxaiKnowledge查找意心Ai知识库内容 """ }, Name = "橙子小弟", 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 new file mode 100644 index 00000000..cbaf42cf --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Mcp/YxaiKnowledgeTool.cs @@ -0,0 +1,115 @@ +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 _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 logger) + { + _httpClientFactory = httpClientFactory; + _logger = logger; + } + + [YiAgentTool("意心Ai平台知识库目录"), DisplayName("YxaiKnowledgeDirectory"), + Description("获取意心AI相关内容的知识库目录列表")] + public async Task> YxaiKnowledgeDirectory() + { + try + { + var client = _httpClientFactory.CreateClient(); + var response = await client.GetAsync(DirectoryUrl); + + if (!response.IsSuccessStatusCode) + { + _logger.LogError("意心知识库目录接口调用失败: {StatusCode}", response.StatusCode); + return new List(); + } + + var json = await response.Content.ReadAsStringAsync(); + var result = JsonSerializer.Deserialize(json, YxaiKnowledgeJsonContext.Default.ListYxaiKnowledgeDirectoryItem); + + return result ?? new List(); + } + 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 "获取知识库内容发生异常"; + } + } +} + +#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))] +[JsonSerializable(typeof(YxaiKnowledgeContentResponse))] +internal partial class YxaiKnowledgeJsonContext : JsonSerializerContext +{ +} + +#endregion diff --git a/Yi.Ai.Vue3/src/pages/chat/agent/index.vue b/Yi.Ai.Vue3/src/pages/chat/agent/index.vue index b4d5f2bd..21f8fc8d 100644 --- a/Yi.Ai.Vue3/src/pages/chat/agent/index.vue +++ b/Yi.Ai.Vue3/src/pages/chat/agent/index.vue @@ -235,12 +235,16 @@ function handleAgentChunk(data: AgentResultOutput) { case 'toolCalling': // 工具调用中 if (!latest.toolCalls) latest.toolCalls = []; - latest.toolCalls.push({ + const newToolCall: { name: string; status: 'calling' | 'called'; result?: any; usage?: { prompt: number; completion: number; total: number } } = { name: data.content as string, status: 'calling', - }); - // 清空待处理的用量 - pendingToolUsage = null; + }; + // 如果有待处理的用量(toolCallUsage 先于 toolCalling 到达),设置到这个工具调用 + if (pendingToolUsage) { + newToolCall.usage = pendingToolUsage; + pendingToolUsage = null; + } + latest.toolCalls.push(newToolCall); break; case 'toolCallUsage': // 工具调用用量统计 - 先保存,等 toolCalled 时再设置 @@ -626,6 +630,23 @@ function cancelSSE() { flex: 1; overflow-y: auto; padding: 8px; + + &::-webkit-scrollbar { + width: 4px; + } + + &::-webkit-scrollbar-track { + background: transparent; + } + + &::-webkit-scrollbar-thumb { + background: var(--el-border-color-lighter); + border-radius: 2px; + + &:hover { + background: var(--el-border-color); + } + } } .session-item {