2025-12-23 17:08:42 +08:00
|
|
|
|
using System.ClientModel;
|
|
|
|
|
|
using System.Reflection;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using System.Text;
|
2025-12-23 17:29:07 +08:00
|
|
|
|
using System.Text.Json;
|
2025-12-23 17:08:42 +08:00
|
|
|
|
using Dm.util;
|
|
|
|
|
|
using Microsoft.Agents.AI;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-12-23 17:08:42 +08:00
|
|
|
|
using Microsoft.Extensions.AI;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-12-23 17:08:42 +08:00
|
|
|
|
using ModelContextProtocol.Server;
|
|
|
|
|
|
using OpenAI;
|
2025-12-23 17:40:00 +08:00
|
|
|
|
using OpenAI.Chat;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
using Volo.Abp.Domain.Services;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.Chat;
|
|
|
|
|
|
using Yi.Framework.AiHub.Domain.AiGateWay;
|
|
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.Model;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
2025-12-24 22:51:18 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Attributes;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
|
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.AiHub.Domain.Managers;
|
|
|
|
|
|
|
|
|
|
|
|
public class ChatManager : DomainService
|
|
|
|
|
|
{
|
2025-12-23 17:08:42 +08:00
|
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
private readonly ISqlSugarRepository<MessageAggregateRoot> _messageRepository;
|
|
|
|
|
|
private readonly ISqlSugarRepository<AgentStoreAggregateRoot> _agentStoreRepository;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
private readonly AiMessageManager _aiMessageManager;
|
|
|
|
|
|
private readonly UsageStatisticsManager _usageStatisticsManager;
|
|
|
|
|
|
private readonly PremiumPackageManager _premiumPackageManager;
|
|
|
|
|
|
private readonly AiGateWayManager _aiGateWayManager;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
private readonly ISqlSugarRepository<AiModelEntity, Guid> _aiModelRepository;
|
2025-12-24 12:20:09 +08:00
|
|
|
|
|
2025-12-24 12:18:33 +08:00
|
|
|
|
public ChatManager(ILoggerFactory loggerFactory,
|
2025-12-24 00:22:46 +08:00
|
|
|
|
ISqlSugarRepository<MessageAggregateRoot> messageRepository,
|
2025-12-24 12:20:09 +08:00
|
|
|
|
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository, AiMessageManager aiMessageManager,
|
|
|
|
|
|
UsageStatisticsManager usageStatisticsManager, PremiumPackageManager premiumPackageManager,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
AiGateWayManager aiGateWayManager, ISqlSugarRepository<AiModelEntity, Guid> aiModelRepository)
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-23 17:08:42 +08:00
|
|
|
|
_loggerFactory = loggerFactory;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
_messageRepository = messageRepository;
|
|
|
|
|
|
_agentStoreRepository = agentStoreRepository;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
_aiMessageManager = aiMessageManager;
|
|
|
|
|
|
_usageStatisticsManager = usageStatisticsManager;
|
|
|
|
|
|
_premiumPackageManager = premiumPackageManager;
|
|
|
|
|
|
_aiGateWayManager = aiGateWayManager;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
_aiModelRepository = aiModelRepository;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 22:51:18 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// agent流式对话
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <param name="sessionId"></param>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
|
/// <param name="tokenId"></param>
|
|
|
|
|
|
/// <param name="modelId"></param>
|
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
|
/// <param name="tools"></param>
|
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
2025-12-24 00:22:46 +08:00
|
|
|
|
public async Task AgentCompleteChatStreamAsync(HttpContext httpContext,
|
|
|
|
|
|
Guid sessionId,
|
|
|
|
|
|
string content,
|
2025-12-24 12:18:33 +08:00
|
|
|
|
string token,
|
2025-12-24 00:22:46 +08:00
|
|
|
|
Guid tokenId,
|
|
|
|
|
|
string modelId,
|
|
|
|
|
|
Guid userId,
|
|
|
|
|
|
List<string> tools
|
|
|
|
|
|
, CancellationToken cancellationToken)
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-24 00:22:46 +08:00
|
|
|
|
// HttpClient.DefaultProxy = new WebProxy("127.0.0.1:8888");
|
|
|
|
|
|
var response = httpContext.Response;
|
|
|
|
|
|
// 设置响应头,声明是 SSE 流
|
|
|
|
|
|
response.ContentType = "text/event-stream;charset=utf-8;";
|
|
|
|
|
|
response.Headers.TryAdd("Cache-Control", "no-cache");
|
|
|
|
|
|
response.Headers.TryAdd("Connection", "keep-alive");
|
|
|
|
|
|
|
2025-12-24 12:20:09 +08:00
|
|
|
|
var modelDescribe = await _aiGateWayManager.GetModelAsync(ModelApiTypeEnum.OpenAi, modelId);
|
|
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//token状态检查,在应用层统一处理
|
2025-12-24 12:18:33 +08:00
|
|
|
|
var client = new OpenAIClient(new ApiKeyCredential(token),
|
2025-12-23 17:29:07 +08:00
|
|
|
|
new OpenAIClientOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
Endpoint = new Uri("https://yxai.chat/v1"),
|
|
|
|
|
|
});
|
2025-12-24 00:22:46 +08:00
|
|
|
|
|
2025-12-24 12:18:33 +08:00
|
|
|
|
#pragma warning disable OPENAI001
|
2025-12-23 17:08:42 +08:00
|
|
|
|
var agent = client.GetChatClient(modelId)
|
2025-12-24 12:18:33 +08:00
|
|
|
|
#pragma warning restore OPENAI001
|
|
|
|
|
|
.CreateAIAgent(new ChatClientAgentOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
ChatOptions = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Instructions = """
|
|
|
|
|
|
你是一个专业的智能助手,是 “意心Ai” 平台自研的 “橙子小弟” 智能体
|
|
|
|
|
|
擅长于精准解决用户提出的各类问题
|
|
|
|
|
|
但性格暴躁,脏话连篇,喜欢说幽默讽刺的话,攻击性极强
|
|
|
|
|
|
"""
|
|
|
|
|
|
},
|
|
|
|
|
|
Name = "橙子小弟",
|
|
|
|
|
|
ChatMessageStoreFactory = ctx => new InMemoryChatMessageStore(
|
|
|
|
|
|
#pragma warning disable MEAI001
|
|
|
|
|
|
new MessageCountingChatReducer(10), // 保留最近10条非系统消息
|
|
|
|
|
|
#pragma warning restore MEAI001
|
|
|
|
|
|
ctx.SerializedState,
|
|
|
|
|
|
ctx.JsonSerializerOptions
|
|
|
|
|
|
)
|
|
|
|
|
|
});
|
2025-12-24 00:22:46 +08:00
|
|
|
|
|
|
|
|
|
|
//线程根据sessionId数据库中获取
|
|
|
|
|
|
var agentStore =
|
|
|
|
|
|
await _agentStoreRepository.GetFirstAsync(x => x.SessionId == sessionId);
|
|
|
|
|
|
if (agentStore is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
agentStore = new AgentStoreAggregateRoot(sessionId);
|
|
|
|
|
|
}
|
2025-12-23 17:29:07 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//获取当前线程
|
|
|
|
|
|
AgentThread currentThread;
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(agentStore.Store))
|
|
|
|
|
|
{
|
|
|
|
|
|
//获取当前存储
|
|
|
|
|
|
JsonElement reloaded = JsonSerializer.Deserialize<JsonElement>(agentStore.Store, JsonSerializerOptions.Web);
|
|
|
|
|
|
currentThread = agent.DeserializeThread(reloaded, JsonSerializerOptions.Web);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
currentThread = agent.GetNewThread();
|
|
|
|
|
|
}
|
2025-12-23 00:49:17 +08:00
|
|
|
|
|
2025-12-24 14:17:32 +08:00
|
|
|
|
//给agent塞入工具
|
2025-12-24 00:22:46 +08:00
|
|
|
|
var toolContents = GetTools();
|
2025-12-23 17:08:42 +08:00
|
|
|
|
var chatOptions = new ChatOptions()
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-24 22:51:18 +08:00
|
|
|
|
Tools = toolContents
|
|
|
|
|
|
.Where(x => tools.Contains(x.Code))
|
|
|
|
|
|
.Select(x => (AITool)x.Tool).ToList(),
|
2025-12-23 17:08:42 +08:00
|
|
|
|
ToolMode = ChatToolMode.Auto
|
|
|
|
|
|
};
|
2025-12-23 17:29:07 +08:00
|
|
|
|
|
2025-12-24 12:18:33 +08:00
|
|
|
|
await foreach (var update in agent.RunStreamingAsync(content, currentThread,
|
|
|
|
|
|
new ChatClientAgentRunOptions(chatOptions), cancellationToken))
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-23 17:08:42 +08:00
|
|
|
|
// 检查每个更新中的内容
|
2025-12-24 00:22:46 +08:00
|
|
|
|
foreach (var updateContent in update.Contents)
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-24 00:22:46 +08:00
|
|
|
|
switch (updateContent)
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//工具调用中
|
2025-12-23 17:08:42 +08:00
|
|
|
|
case FunctionCallContent functionCall:
|
2025-12-24 00:22:46 +08:00
|
|
|
|
await SendHttpStreamMessageAsync(httpContext,
|
|
|
|
|
|
new AgentResultOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
TypeEnum = AgentResultTypeEnum.ToolCalling,
|
|
|
|
|
|
Content = functionCall.Name
|
|
|
|
|
|
},
|
|
|
|
|
|
isDone: false, cancellationToken);
|
2025-12-23 17:08:42 +08:00
|
|
|
|
break;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//工具调用完成
|
2025-12-23 17:08:42 +08:00
|
|
|
|
case FunctionResultContent functionResult:
|
2025-12-24 00:22:46 +08:00
|
|
|
|
await SendHttpStreamMessageAsync(httpContext,
|
|
|
|
|
|
new AgentResultOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
TypeEnum = AgentResultTypeEnum.ToolCalled,
|
|
|
|
|
|
Content = functionResult.Result
|
|
|
|
|
|
},
|
|
|
|
|
|
isDone: false, cancellationToken);
|
2025-12-23 17:08:42 +08:00
|
|
|
|
break;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//内容输出
|
2025-12-23 17:08:42 +08:00
|
|
|
|
case TextContent textContent:
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//发送消息给前端
|
|
|
|
|
|
await SendHttpStreamMessageAsync(httpContext,
|
|
|
|
|
|
new AgentResultOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
TypeEnum = AgentResultTypeEnum.Text,
|
|
|
|
|
|
Content = textContent.Text
|
|
|
|
|
|
},
|
|
|
|
|
|
isDone: false, cancellationToken);
|
2025-12-23 17:08:42 +08:00
|
|
|
|
break;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//用量统计
|
2025-12-23 17:08:42 +08:00
|
|
|
|
case UsageContent usageContent:
|
2025-12-24 12:18:33 +08:00
|
|
|
|
var usage = new ThorUsageResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
InputTokens = Convert.ToInt32(usageContent.Details.InputTokenCount ?? 0),
|
|
|
|
|
|
OutputTokens = Convert.ToInt32(usageContent.Details.OutputTokenCount ?? 0),
|
|
|
|
|
|
TotalTokens = usageContent.Details.TotalTokenCount ?? 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
//设置倍率
|
|
|
|
|
|
usage.SetSupplementalMultiplier(modelDescribe.Multiplier);
|
2025-12-24 12:20:09 +08:00
|
|
|
|
|
2025-12-24 12:18:33 +08:00
|
|
|
|
//创建系统回答,用于计费统计
|
|
|
|
|
|
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId, new MessageInputDto
|
|
|
|
|
|
{
|
|
|
|
|
|
Content = "不与存储",
|
|
|
|
|
|
ModelId = modelId,
|
|
|
|
|
|
TokenUsage = usage
|
|
|
|
|
|
}, tokenId);
|
2025-12-24 12:20:09 +08:00
|
|
|
|
|
2025-12-24 12:18:33 +08:00
|
|
|
|
//创建用量统计,用于统计分析
|
|
|
|
|
|
await _usageStatisticsManager.SetUsageAsync(userId, modelId, usage, tokenId);
|
|
|
|
|
|
|
|
|
|
|
|
//扣减尊享token包用量
|
2026-01-01 00:44:02 +08:00
|
|
|
|
var isPremium = await _aiModelRepository._DbQueryable
|
|
|
|
|
|
.Where(x => x.ModelId == modelId)
|
|
|
|
|
|
.Select(x => x.IsPremium)
|
|
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (isPremium)
|
2025-12-24 12:18:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
var totalTokens = usage?.TotalTokens ?? 0;
|
|
|
|
|
|
if (totalTokens > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _premiumPackageManager.TryConsumeTokensAsync(userId, totalTokens);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-24 12:20:09 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
await SendHttpStreamMessageAsync(httpContext,
|
|
|
|
|
|
new AgentResultOutput
|
|
|
|
|
|
{
|
2025-12-24 12:18:33 +08:00
|
|
|
|
TypeEnum = update.RawRepresentation is ChatResponseUpdate raw
|
|
|
|
|
|
? raw.FinishReason?.Value == "tool_calls"
|
|
|
|
|
|
? AgentResultTypeEnum.ToolCallUsage
|
|
|
|
|
|
: AgentResultTypeEnum.Usage
|
|
|
|
|
|
: AgentResultTypeEnum.Usage,
|
|
|
|
|
|
Content = usage!
|
2025-12-24 00:22:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
isDone: false, cancellationToken);
|
2025-12-23 17:08:42 +08:00
|
|
|
|
break;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|
2025-12-23 17:08:42 +08:00
|
|
|
|
}
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|
2025-12-23 17:29:07 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//断开连接
|
|
|
|
|
|
await SendHttpStreamMessageAsync(httpContext, null, isDone: true, cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
//将线程持久化到数据库
|
|
|
|
|
|
string serializedJson = currentThread.Serialize(JsonSerializerOptions.Web).GetRawText();
|
|
|
|
|
|
agentStore.Store = serializedJson;
|
2025-12-23 17:29:07 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//插入或者更新
|
|
|
|
|
|
await _agentStoreRepository.InsertOrUpdateAsync(agentStore);
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-23 17:08:42 +08:00
|
|
|
|
|
2025-12-24 14:17:32 +08:00
|
|
|
|
public List<(string Code, string Name, AIFunction Tool)> GetTools()
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-23 17:08:42 +08:00
|
|
|
|
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
|
2025-12-24 22:51:18 +08:00
|
|
|
|
.Where(x => x.GetCustomAttribute<YiAgentToolAttribute>() is not null)
|
2025-12-23 17:08:42 +08:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
2025-12-24 14:17:32 +08:00
|
|
|
|
List<(string Code, string Name, AIFunction Tool)> mcpTools = new();
|
2025-12-23 17:08:42 +08:00
|
|
|
|
foreach (var toolClass in toolClasses)
|
2025-12-23 00:49:17 +08:00
|
|
|
|
{
|
2025-12-23 17:08:42 +08:00
|
|
|
|
var instance = LazyServiceProvider.GetRequiredService(toolClass);
|
|
|
|
|
|
var toolMethods = toolClass.GetMethods()
|
2025-12-24 22:51:18 +08:00
|
|
|
|
.Where(y => y.GetCustomAttribute<YiAgentToolAttribute>() is not null).ToList();
|
2025-12-23 17:08:42 +08:00
|
|
|
|
foreach (var toolMethod in toolMethods)
|
|
|
|
|
|
{
|
2025-12-24 22:51:18 +08:00
|
|
|
|
var display = toolMethod.GetCustomAttribute<YiAgentToolAttribute>()?.Name;
|
2025-12-24 14:17:32 +08:00
|
|
|
|
var tool = AIFunctionFactory.Create(toolMethod, instance);
|
|
|
|
|
|
mcpTools.add((tool.Name, display, tool));
|
2025-12-23 17:08:42 +08:00
|
|
|
|
}
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|
2025-12-24 22:51:18 +08:00
|
|
|
|
|
2025-12-23 17:08:42 +08:00
|
|
|
|
return mcpTools;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|
2025-12-24 00:22:46 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <param name="isDone"></param>
|
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private async Task SendHttpStreamMessageAsync(HttpContext httpContext,
|
|
|
|
|
|
AgentResultOutput? content,
|
|
|
|
|
|
bool isDone = false,
|
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = httpContext.Response;
|
|
|
|
|
|
string output;
|
|
|
|
|
|
if (isDone)
|
|
|
|
|
|
{
|
|
|
|
|
|
output = "[DONE]";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-12-24 12:18:33 +08:00
|
|
|
|
output = JsonSerializer.Serialize(content, ThorJsonSerializer.DefaultOptions);
|
2025-12-24 00:22:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await response.WriteAsync($"data: {output}\n\n", Encoding.UTF8, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
await response.Body.FlushAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
}
|
2025-12-23 00:49:17 +08:00
|
|
|
|
}
|