Files
BeWoPlaner/Server/Components/AICore/Facade/AiPromptsApiClient.cs

100 lines
2.5 KiB
C#

using BeWo.ServerUtils.ApiFacade;
using BS.Shared.DataContracts.Feature.AI;
using BS.Shared.DataContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BS.Shared;
using BS.Shared.Interface;
using System.ComponentModel.Design;
using System.Runtime.Remoting.Messaging;
using log4net;
using BS.Shared.Core;
using BeWo.ServerUtils.Core;
namespace AICore.Facade
{
public class AiPromptsApiClient
{
private protected readonly HttpClientFacade _GetClientFacade;
private protected readonly IApiErrorExtractor _ErrorExtractor;
private ILog _logger;
public AiPromptsApiClient(string url)
{
var base_url = url;
_logger = LoggerUtils.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
_GetClientFacade = new HttpClientFacade(base_url, null, false);
_ErrorExtractor = new DefaultErrorExtractor();
}
public IEnumerable<AiPromptbausteinPromptDC> GetAiPrompts(AiActionType aiActionType)
{
var prompts = new List<AiPromptbausteinPromptDC>();
var response_format = new[]
{
new
{
promptid = "",
title = "",
prompt = "",
description = "",
helpurl = "",
aiactiontype = "",
samples_count = 0,
show_prompt_text = true
}
};
var method = "api/prompts/4368658435";
_logger.Info($"GetAiPrompts: Hole aktuelle ownSoft Prompts von '{_GetClientFacade.Base_Url}/{method}'");
var response = _GetClientFacade.GetAnonymousTypeAsync(method, response_format, _ErrorExtractor).GetAwaiter().GetResult();
if (!response.Success)
{
_logger.Error($"GetAiPrompts: " + response.ToErrorString());
return null;
}
var data = response.Data.Select(x => new AiPromptbausteinPromptDC()
{
PromptId = x.promptid,
Title = x.title,
Description = x.description,
Prompt = x.prompt,
OwnsoftRefLink = x.helpurl,
IsOwnsoftPrompt = true,
ActionType = GetAiActionType(x.aiactiontype, aiActionType),
SamplesCount = x.samples_count,
ShowPromptByTenant = true,
ShowPrompt = x.show_prompt_text
});
var log_str = string.Join("", data.Select(x => $"\r\n\t\t\t\t- ('{x.PromptId}','{x.Title}','{x.ActionType}')"));
_logger.Info($"GetAiPrompts: ownSoft Prompts: {log_str}");
return data.Where(x => x.ActionType == aiActionType); ;
}
private AiActionType GetAiActionType(string aiactiontype, AiActionType aiActionType)
{
if(Enum.TryParse<AiActionType>(aiactiontype, out var result))
{
return result;
}
return aiActionType;
}
}
}