|
|
@ -5,24 +5,29 @@ using Microsoft.Extensions.Localization; |
|
|
using System.Collections.Concurrent; |
|
|
using System.Collections.Concurrent; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using Volo.Abp.AI; |
|
|
using Volo.Abp.AI; |
|
|
|
|
|
using Volo.Abp.Authorization; |
|
|
using Volo.Abp.DependencyInjection; |
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
using Volo.Abp.SimpleStateChecking; |
|
|
|
|
|
|
|
|
namespace LINGYUN.Abp.AI.Agent; |
|
|
namespace LINGYUN.Abp.AI.Agent; |
|
|
public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDependency |
|
|
public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDependency |
|
|
{ |
|
|
{ |
|
|
private readonly static ConcurrentDictionary<string, ChatClientAgent> _chatClientAgentCache = new(); |
|
|
private readonly static ConcurrentDictionary<string, ChatClientAgent> _chatClientAgentCache = new(); |
|
|
|
|
|
|
|
|
private readonly IChatClientFactory _chatClientFactory; |
|
|
protected IChatClientFactory ChatClientFactory { get; } |
|
|
private readonly IStringLocalizerFactory _stringLocalizerFactory; |
|
|
protected IStringLocalizerFactory StringLocalizerFactory { get; } |
|
|
private readonly IWorkspaceDefinitionManager _workspaceDefinitionManager; |
|
|
protected IWorkspaceDefinitionManager WorkspaceDefinitionManager { get; } |
|
|
|
|
|
protected ISimpleStateCheckerManager<WorkspaceDefinition> StateCheckerManager { get; } |
|
|
public ChatClientAgentFactory( |
|
|
public ChatClientAgentFactory( |
|
|
IChatClientFactory chatClientFactory, |
|
|
IChatClientFactory chatClientFactory, |
|
|
IStringLocalizerFactory stringLocalizerFactory, |
|
|
IStringLocalizerFactory stringLocalizerFactory, |
|
|
IWorkspaceDefinitionManager workspaceDefinitionManager) |
|
|
IWorkspaceDefinitionManager workspaceDefinitionManager, |
|
|
|
|
|
ISimpleStateCheckerManager<WorkspaceDefinition> stateCheckerManager) |
|
|
{ |
|
|
{ |
|
|
_chatClientFactory = chatClientFactory; |
|
|
ChatClientFactory = chatClientFactory; |
|
|
_stringLocalizerFactory = stringLocalizerFactory; |
|
|
StringLocalizerFactory = stringLocalizerFactory; |
|
|
_workspaceDefinitionManager = workspaceDefinitionManager; |
|
|
WorkspaceDefinitionManager = workspaceDefinitionManager; |
|
|
|
|
|
StateCheckerManager = stateCheckerManager; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async virtual Task<ChatClientAgent> CreateAsync<TWorkspace>() |
|
|
public async virtual Task<ChatClientAgent> CreateAsync<TWorkspace>() |
|
|
@ -33,14 +38,19 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende |
|
|
return chatClientAgent; |
|
|
return chatClientAgent; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var chatClient = await _chatClientFactory.CreateAsync<TWorkspace>(); |
|
|
var chatClient = await ChatClientFactory.CreateAsync<TWorkspace>(); |
|
|
|
|
|
|
|
|
var workspaceDefine = await _workspaceDefinitionManager.GetOrNullAsync(workspace); |
|
|
var workspaceDefine = await WorkspaceDefinitionManager.GetOrNullAsync(workspace); |
|
|
|
|
|
|
|
|
|
|
|
if (workspaceDefine != null) |
|
|
|
|
|
{ |
|
|
|
|
|
await CheckWorkspaceStateAsync(workspaceDefine); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
string? description = null; |
|
|
string? description = null; |
|
|
if (workspaceDefine?.Description != null) |
|
|
if (workspaceDefine?.Description != null) |
|
|
{ |
|
|
{ |
|
|
description = workspaceDefine.Description.Localize(_stringLocalizerFactory); |
|
|
description = workspaceDefine.Description.Localize(StringLocalizerFactory); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
chatClientAgent = chatClient.CreateAIAgent( |
|
|
chatClientAgent = chatClient.CreateAIAgent( |
|
|
@ -59,13 +69,16 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende |
|
|
{ |
|
|
{ |
|
|
return chatClientAgent; |
|
|
return chatClientAgent; |
|
|
} |
|
|
} |
|
|
var workspaceDefine = await _workspaceDefinitionManager.GetAsync(workspace); |
|
|
var workspaceDefine = await WorkspaceDefinitionManager.GetAsync(workspace); |
|
|
var chatClient = await _chatClientFactory.CreateAsync(workspace); |
|
|
|
|
|
|
|
|
await CheckWorkspaceStateAsync(workspaceDefine); |
|
|
|
|
|
|
|
|
|
|
|
var chatClient = await ChatClientFactory.CreateAsync(workspace); |
|
|
|
|
|
|
|
|
string? description = null; |
|
|
string? description = null; |
|
|
if (workspaceDefine.Description != null) |
|
|
if (workspaceDefine.Description != null) |
|
|
{ |
|
|
{ |
|
|
description = workspaceDefine.Description.Localize(_stringLocalizerFactory); |
|
|
description = workspaceDefine.Description.Localize(StringLocalizerFactory); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
chatClientAgent = chatClient.CreateAIAgent( |
|
|
chatClientAgent = chatClient.CreateAIAgent( |
|
|
@ -77,4 +90,15 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende |
|
|
|
|
|
|
|
|
return chatClientAgent; |
|
|
return chatClientAgent; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected async virtual Task CheckWorkspaceStateAsync(WorkspaceDefinition workspace) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!await StateCheckerManager.IsEnabledAsync(workspace)) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new AbpAuthorizationException( |
|
|
|
|
|
$"Workspace is not enabled: {workspace.Name}!", |
|
|
|
|
|
AbpAIErrorCodes.WorkspaceIsNotEnabled) |
|
|
|
|
|
.WithData("Workspace", workspace.Name); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|