From aaf1bd572abcf813d111dab93ba391c83cf5cf22 Mon Sep 17 00:00:00 2001 From: Rene Evertz Date: Wed, 27 May 2026 15:34:53 +0200 Subject: [PATCH] Routine bliblablubb --- .../GeneratedAiEnhancedService.cs | 161 +++++++++++++++--- BeWo/ViewModel/AiPromptbausteinPromptVM.cs | 6 +- .../Controls/AI/AiChatButtonViewModel.cs | 14 +- BeWo/ViewModel/VMFactory.cs | 6 +- .../View/AI/AiConversationChatViewModel.cs | 108 +++++++++--- Server/Components/AICore/AICore.csproj | 2 +- .../Context/Core/AiContextSummaryFactory.cs | 24 +-- .../AICore/Prompt/AiSystemPromptFactory.cs | 37 ++++ .../AiConversationSystemPromptBuilder.cs | 2 +- .../AiFunctionSystemPromptBuilder.cs | 2 +- .../SystemPrompts/BaseSystemPromptBuilder.cs | 2 +- Service/Plugins/AiFunctionService.cs | 152 ++++++++--------- Service/Plugins/AiService2.cs | 94 +++++++--- .../Enhanced/IAiEnhancedService.cs | 17 +- .../Enhanced/AiEnhancedServiceImp.cs | 27 +-- Service/ServiceUtils/AiContextLoader.cs | 19 ++- .../Feature/AI/AiViewContextDC.cs | 2 +- 17 files changed, 470 insertions(+), 205 deletions(-) create mode 100644 Server/Components/AICore/Prompt/AiSystemPromptFactory.cs diff --git a/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs b/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs index c1d8f9169..c3589da7e 100644 --- a/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs +++ b/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs @@ -35,12 +35,108 @@ namespace BeWo.ServiceProxy [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/CreateAiConversation", ReplyAction="http://tempuri.org/IAiEnhancedService/CreateAiConversationResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/CreateAiConversationBeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] - BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversation(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, long modell_oid); + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.BeWoDataContract))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConfigDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiModelDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationMessageDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinFolderDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinPromptDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineStepDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiUserSettingDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Compact.CompactEmployeeDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary>))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFault))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFaultType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiDataContextType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiModelSource))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiActionType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.TableID))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiConversationMessageRole))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ActivationTypeId))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ValueListEntryType))] + BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversation(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context); [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/CreateAiConversationWithMessage", ReplyAction="http://tempuri.org/IAiEnhancedService/CreateAiConversationWithMessageResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/CreateAiConversationWithMessageBeWoFaultFau" + "lt", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] - BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversationWithMessage(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, long modell_oid, string message); + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.BeWoDataContract))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConfigDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiModelDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationMessageDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinFolderDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinPromptDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineStepDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiUserSettingDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Compact.CompactEmployeeDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary>))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFault))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFaultType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiDataContextType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiModelSource))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiActionType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.TableID))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiConversationMessageRole))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ActivationTypeId))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ValueListEntryType))] + BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversationWithMessage(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, string message); + + [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/CreateAiConversationWithPromptbaustein", ReplyAction="http://tempuri.org/IAiEnhancedService/CreateAiConversationWithPromptbausteinRespo" + + "nse")] + [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/CreateAiConversationWithPromptbausteinBeWoF" + + "aultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.BeWoDataContract))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConfigDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiModelDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationMessageDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinFolderDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinPromptDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineStepDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiUserSettingDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Compact.CompactEmployeeDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary>))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFault))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFaultType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiDataContextType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiModelSource))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiActionType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.TableID))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiConversationMessageRole))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ActivationTypeId))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ValueListEntryType))] + BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversationWithPromptbaustein(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, long prompt_oid); [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/CloneAiConversation", ReplyAction="http://tempuri.org/IAiEnhancedService/CloneAiConversationResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/CloneAiConversationBeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] @@ -61,7 +157,37 @@ namespace BeWo.ServiceProxy [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/CheckContextSize", ReplyAction="http://tempuri.org/IAiEnhancedService/CheckContextSizeResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/CheckContextSizeBeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] - bool CheckContextSize(BS.Shared.AiActionType actionType, System.Collections.Generic.Dictionary> context, System.Nullable modell_oid); + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.BeWoDataContract))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConfigDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiModelDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiConversationMessageDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinFolderDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinPromptDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineStepDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Feature.AI.AiUserSettingDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.Compact.CompactEmployeeDC))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary>))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.List))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(System.Collections.Generic.Dictionary))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFault))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.Core.BeWoFaultType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiDataContextType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiModelSource))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiActionType))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.TableID))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiConversationMessageRole))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ActivationTypeId))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ValueListEntryType))] + bool CheckContextSize(BS.Shared.AiActionType actionType, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context); [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/GetAiPromptbausteinFolder", ReplyAction="http://tempuri.org/IAiEnhancedService/GetAiPromptbausteinFolderResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/GetAiPromptbausteinFolderBeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] @@ -106,13 +232,6 @@ namespace BeWo.ServiceProxy "", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] void DeleteAiPromptbausteinRoutine(BS.Shared.DataContracts.Feature.AI.AiPromptbausteinRoutineDC routine); - [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/ExecuteAiFunctionServiceRecordDocumentation" + - "", ReplyAction="http://tempuri.org/IAiEnhancedService/ExecuteAiFunctionServiceRecordDocumentation" + - "Response")] - [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/ExecuteAiFunctionServiceRecordDocumentation" + - "BeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] - BS.Shared.DataContracts.Feature.AI.Functions.ServiceRecords.AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(BS.Shared.DataContracts.Feature.AI.Functions.ServiceRecords.AiFunctionDocRequest request); - [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/GetUserSetting", ReplyAction="http://tempuri.org/IAiEnhancedService/GetUserSettingResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/GetUserSettingBeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] BS.Shared.DataContracts.Feature.AI.AiUserSettingDC GetUserSetting(); @@ -176,14 +295,19 @@ namespace BeWo.ServiceProxy return base.Channel.GetAiConversations(actionType, oid); } - public BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversation(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, long modell_oid) + public BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversation(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context) { - return base.Channel.CreateAiConversation(conversation, modell_oid); + return base.Channel.CreateAiConversation(conversation, view_context); } - public BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversationWithMessage(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, long modell_oid, string message) + public BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversationWithMessage(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, string message) { - return base.Channel.CreateAiConversationWithMessage(conversation, modell_oid, message); + return base.Channel.CreateAiConversationWithMessage(conversation, view_context, message); + } + + public BS.Shared.DataContracts.Feature.AI.AiConversationDC CreateAiConversationWithPromptbaustein(BS.Shared.DataContracts.Feature.AI.AiConversationDC conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, long prompt_oid) + { + return base.Channel.CreateAiConversationWithPromptbaustein(conversation, view_context, prompt_oid); } public BS.Shared.DataContracts.Feature.AI.AiConversationDC CloneAiConversation(long conversation_oid, System.Nullable message_oid) @@ -206,9 +330,9 @@ namespace BeWo.ServiceProxy return base.Channel.SendNewMessage(conversation, message); } - public bool CheckContextSize(BS.Shared.AiActionType actionType, System.Collections.Generic.Dictionary> context, System.Nullable modell_oid) + public bool CheckContextSize(BS.Shared.AiActionType actionType, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context) { - return base.Channel.CheckContextSize(actionType, context, modell_oid); + return base.Channel.CheckContextSize(actionType, view_context); } public System.Collections.Generic.List GetAiPromptbausteinFolder(BS.Shared.AiActionType aiActionType) @@ -261,11 +385,6 @@ namespace BeWo.ServiceProxy base.Channel.DeleteAiPromptbausteinRoutine(routine); } - public BS.Shared.DataContracts.Feature.AI.Functions.ServiceRecords.AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(BS.Shared.DataContracts.Feature.AI.Functions.ServiceRecords.AiFunctionDocRequest request) - { - return base.Channel.ExecuteAiFunctionServiceRecordDocumentation(request); - } - public BS.Shared.DataContracts.Feature.AI.AiUserSettingDC GetUserSetting() { return base.Channel.GetUserSetting(); diff --git a/BeWo/ViewModel/AiPromptbausteinPromptVM.cs b/BeWo/ViewModel/AiPromptbausteinPromptVM.cs index bd4ac188b..58fa8e50c 100644 --- a/BeWo/ViewModel/AiPromptbausteinPromptVM.cs +++ b/BeWo/ViewModel/AiPromptbausteinPromptVM.cs @@ -166,8 +166,6 @@ namespace BeWo.ViewModel public override void UpdateByDataContract(AiPromptbausteinPromptDC pDataContract) { - DataContract = pDataContract; - Title = pDataContract.Title; Description = pDataContract.Description; ParentFolderOid = pDataContract.ParentFolderOid; @@ -181,6 +179,10 @@ namespace BeWo.ViewModel OwnsoftRefLink = pDataContract.OwnsoftRefLink; IsFavorite = pDataContract.IsFavorite; ActionType = pDataContract.ActionType; + + DataContract = pDataContract; + + SetDirty(false); } public override bool IsDirty diff --git a/BeWo/ViewModel/Controls/AI/AiChatButtonViewModel.cs b/BeWo/ViewModel/Controls/AI/AiChatButtonViewModel.cs index 03979ba74..49cf02378 100644 --- a/BeWo/ViewModel/Controls/AI/AiChatButtonViewModel.cs +++ b/BeWo/ViewModel/Controls/AI/AiChatButtonViewModel.cs @@ -109,9 +109,7 @@ namespace BeWo.ViewModel.Controls.AI IsVisible = true; - var context2 = context.References.ToDictionary(kv => kv.Key, kv => kv.Value.ToList()); - - ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.CheckContextSize(ActionType, context2, null), + ServiceFacade.DoAiEnhancedServiceAsnyc(x => x.CheckContextSize(ActionType, context), isValid => { if (isValid) @@ -127,7 +125,7 @@ namespace BeWo.ViewModel.Controls.AI { var context = GetAiVisualContext(); - var chat_viewmodel = VMFactory.CreateAiConversationChatViewModel(ActionType, context.References, (long)context.Primary, CanSendNewMessage); + var chat_viewmodel = VMFactory.CreateAiConversationChatViewModel(ActionType, context, CanSendNewMessage); return chat_viewmodel; } @@ -171,13 +169,19 @@ namespace BeWo.ViewModel.Controls.AI { var vm = GetConversationViewModel(); vm.SendAfterLoaded = true; + vm.PromptInput = prompt; vm.MessageInput = prompt.Prompt; BeWoApp.MainControl.WindowService.Show(vm); return; } else if(e is AiPromptbausteinRoutineVM routine) { - + var vm = GetConversationViewModel(); + vm.SendAfterLoaded = true; + vm.RoutineInput = routine; + vm.MessageInput = routine.Steps.VMList[0].PromptReference.Prompt; + BeWoApp.MainControl.WindowService.Show(vm); + return; } throw new NotImplementedException(); diff --git a/BeWo/ViewModel/VMFactory.cs b/BeWo/ViewModel/VMFactory.cs index 670dccbd2..181c2f851 100644 --- a/BeWo/ViewModel/VMFactory.cs +++ b/BeWo/ViewModel/VMFactory.cs @@ -912,14 +912,12 @@ namespace BeWo.ViewModel einheit_vm_list.VMList.Sort(WohneinheitVM.Compare); } - public static AiConversationChatViewModel CreateAiConversationChatViewModel(AiActionType actionType, Dictionary bewoObjects, - long? reference_oid = null, bool canSendNewMessage = true) + public static AiConversationChatViewModel CreateAiConversationChatViewModel(AiActionType actionType, AiViewContextDC view_context, bool canSendNewMessage = true) { var vm = new AiConversationChatViewModel(); vm.ActionType = actionType; - vm.ContextBeWoObjects = bewoObjects; - vm.ReferenceObjectOid = reference_oid; + vm.ViewContext = view_context; vm.CanSendNewMessage = canSendNewMessage; return vm; diff --git a/BeWo/ViewModel/View/AI/AiConversationChatViewModel.cs b/BeWo/ViewModel/View/AI/AiConversationChatViewModel.cs index 747735fc8..1903e6688 100644 --- a/BeWo/ViewModel/View/AI/AiConversationChatViewModel.cs +++ b/BeWo/ViewModel/View/AI/AiConversationChatViewModel.cs @@ -14,6 +14,9 @@ using DevExpress.Mvvm; using BeWo.ServiceProxy; using BS.Shared.Extensions; using System.Diagnostics; +using DevExpress.XtraCharts.Native; +using static DevExpress.Utils.Drawing.Helpers.NativeMethods; +using System.Security.Cryptography; namespace BeWo.ViewModel.View.AI { @@ -24,7 +27,6 @@ namespace BeWo.ViewModel.View.AI private bool _IsSending; private string _MessageInput; - private string _ContextInput; private AiModelDC _SelectedModel; private AiConfigVM _Config; @@ -121,18 +123,8 @@ namespace BeWo.ViewModel.View.AI FirePropertyChanged(nameof(MessageInput)); } } - public string ContextInput - { - get { return _ContextInput; } - set - { - if (ContextInput == value) - return; - - _ContextInput = value; - FirePropertyChanged(nameof(ContextInput)); - } - } + public AiPromptbausteinPromptVM PromptInput { get; set; } + public AiPromptbausteinRoutineVM RoutineInput { get; set; } public bool IsSettingsOpen { @@ -164,8 +156,7 @@ namespace BeWo.ViewModel.View.AI public bool IsNewConversationVisible => ListVM.SelectedVM is null; public AiActionType ActionType { get; set; } - public long? ReferenceObjectOid { get; set; } - public Dictionary ContextBeWoObjects { get; set; } + public AiViewContextDC ViewContext { get; set; } public AiPromptbausteinPromptVM SavePromptVM { @@ -201,7 +192,7 @@ namespace BeWo.ViewModel.View.AI conv.Created = DateTime.Now; conv.Updated = DateTime.Now; conv.ActionType = ActionType; - conv.ContextBeWoObjects = ContextBeWoObjects; + conv.ContextBeWoObjects = ViewContext.References; conv.Messages = new List() { new AiConversationMessageDC() @@ -213,8 +204,36 @@ namespace BeWo.ViewModel.View.AI var vm = InsertConverstion(conv); - ServiceFacade.DoAiEnhancedServiceAsnyc( - x => x.CreateAiConversationWithMessage(conv, Config.SelectedModel.Oid.Value, message), + Action error = (e) => + { + EndSending(); + ListVM.VMList.Remove(vm); + }; + + if (RoutineInput != null) + { + ServiceFacade.DoAiEnhancedServiceAsnyc( + x => x.CreateAiConversationWithPromptbaustein(conv, ViewContext, RoutineInput.Steps.VMList[0].PromptReference.Oid.Value), + dc => + { + vm.UpdateByDataContract(dc); + ExecuteChain(dc.Oid.Value, 1, error); + }, + error + ); + } + else if (PromptInput != null) + { + ServiceFacade.DoAiEnhancedServiceAsnyc( + x => x.CreateAiConversationWithPromptbaustein(conv, ViewContext, PromptInput.DataContract.Oid.Value), + dc => UpdateConverstion(vm, dc), + error + ); + } + else + { + ServiceFacade.DoAiEnhancedServiceAsnyc( + x => x.CreateAiConversationWithMessage(conv, ViewContext, message), dc => UpdateConverstion(vm, dc), e => { @@ -222,12 +241,51 @@ namespace BeWo.ViewModel.View.AI ListVM.VMList.Remove(vm); } ); + } } catch (Exception ex) { EndSending(); } } + private void ExecuteChain(long oid, int c, Action error) + { + if (c >= RoutineInput.Steps.Count) + { + FinishSending(); + + return; + } + + MessageInput = RoutineInput.Steps.VMList[c].PromptReference.Prompt; + + var conv = ListVM.SelectedVM; + + var msg_dc = new AiConversationMessageDC(); + + msg_dc.Message = MessageInput; + msg_dc.Role = BS.Shared.AiConversationMessageRole.User; + msg_dc.Created = DateTime.Now; + + var msg_vm = new AiConversationMessageVM(msg_dc); + + conv.Messages.VMList.Add(msg_vm); + + ServiceFacade.DoAiEnhancedServiceAsnyc( + x => x.SendNewMessage(conv.DataContract.Oid.Value, MessageInput), + (messages) => + { + if (messages.Count != 2) + throw new InvalidOperationException("message count: " + messages.Count); + + msg_vm.DataContract = messages[0]; + + conv.Messages.VMList.Add(new AiConversationMessageVM(messages[1])); + + ExecuteChain(oid, c + 1, error); + }, + error); + } private void OpenNewConversation() { ListVM.SelectedVM = null; @@ -261,11 +319,7 @@ namespace BeWo.ViewModel.View.AI conv.Messages.VMList.Add(new AiConversationMessageVM(messages[1])); - MessageInput = string.Empty; - - EndSending(); - - SendNewMessageSuccess?.Invoke(this, EventArgs.Empty); + FinishSending(); }, (exception) => { @@ -286,6 +340,7 @@ namespace BeWo.ViewModel.View.AI throw new InvalidOperationException("Sendet bereits"); IsSending = true; + IsWaiting = true; } private void EndSending() { @@ -293,6 +348,7 @@ namespace BeWo.ViewModel.View.AI throw new InvalidOperationException("Sendet nicht"); IsSending = false; + IsWaiting = false; } private AiConversationVM InsertConverstion(AiConversationDC dc) @@ -308,6 +364,10 @@ namespace BeWo.ViewModel.View.AI { vm.UpdateByDataContract(dc); + FinishSending(); + } + private void FinishSending() + { MessageInput = string.Empty; EndSending(); @@ -356,7 +416,7 @@ namespace BeWo.ViewModel.View.AI private void ReloadConversations() { var ui_context = ActionType; - var reference = ReferenceObjectOid; + var reference = ViewContext.Primary; var list = VMFactory.CreateAiConversationListVM(ui_context, reference); diff --git a/Server/Components/AICore/AICore.csproj b/Server/Components/AICore/AICore.csproj index 79e85626b..99585ad6b 100644 --- a/Server/Components/AICore/AICore.csproj +++ b/Server/Components/AICore/AICore.csproj @@ -121,6 +121,7 @@ + @@ -138,7 +139,6 @@ - diff --git a/Server/Components/AICore/Context/Core/AiContextSummaryFactory.cs b/Server/Components/AICore/Context/Core/AiContextSummaryFactory.cs index ed5f8e4b9..8a387a307 100644 --- a/Server/Components/AICore/Context/Core/AiContextSummaryFactory.cs +++ b/Server/Components/AICore/Context/Core/AiContextSummaryFactory.cs @@ -27,6 +27,18 @@ namespace AICore.Context.Core return new ServiceRecordNavigationCS(entries, customer_entry, costbearer_str, supportconcept_str); } + public static BaseContextSummary CreateServiceRecordDocumentationFunction(IEnumerable serviceRecords, + CompactCustomerDC customer, CompactCostBearerDC costBearer, CompactSupportConceptDC supportConcept, string doku) + { + var entries = ContextEntryMapperFactory.ServiceRecordMapper.MapToNewContexts(serviceRecords); + var customer_entry = ContextEntryMapperFactory.ServiceRecordCustomerMapper.MapToNewContext(customer); + + var costbearer_str = costBearer.OrganisationName; + var supportconcept_str = $"{supportConcept.StartDate?.Date} - {supportConcept.EndDate?.Date}"; + + return new ServiceRecordDocumantationFCS(entries, customer_entry, costbearer_str, supportconcept_str, doku); + } + public static BaseContextSummary CreateCustomerDetailContextSummary(CustomerDC customer) { var entry = ContextEntryMapperFactory.CustomerMapper.MapToNewContext(customer); @@ -96,17 +108,5 @@ namespace AICore.Context.Core return new SupportConceptNavigationCS(entry); } - - public static BaseContextSummary CreateServiceRecordDocumentationFunction(IEnumerable serviceRecords, - CompactCustomerDC customer, CompactCostBearerDC costBearer, CompactSupportConceptDC supportConcept, string doku) - { - var entries = ContextEntryMapperFactory.ServiceRecordMapper.MapToNewContexts(serviceRecords); - var customer_entry = ContextEntryMapperFactory.ServiceRecordCustomerMapper.MapToNewContext(customer); - - var costbearer_str = costBearer.OrganisationName; - var supportconcept_str = $"{supportConcept.StartDate?.Date} - {supportConcept.EndDate?.Date}"; - - return new ServiceRecordDocumantationFCS(entries, customer_entry, costbearer_str, supportconcept_str, doku); - } } } diff --git a/Server/Components/AICore/Prompt/AiSystemPromptFactory.cs b/Server/Components/AICore/Prompt/AiSystemPromptFactory.cs new file mode 100644 index 000000000..afd91dde5 --- /dev/null +++ b/Server/Components/AICore/Prompt/AiSystemPromptFactory.cs @@ -0,0 +1,37 @@ +using AICore.Context.Summary; +using AICore.Prompt.SystemPrompts; +using BS.Shared; +using BS.Shared.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; + +namespace AICore.Prompt +{ + public class AiSystemPromptFactory + { + public static string BuildContext(AiActionType actionType, string prompt_path, BaseContextSummary context, AiDataContextType context_type) + { + switch (actionType) + { + case AiActionType.ServiceRecord: + break; + case AiActionType.ServiceRecordConversation: + var factory = new AiConversationSystemPromptBuilder(prompt_path); + var prompt = factory.CreateSystemPrompt(actionType, context, context_type); + return prompt; + case AiActionType.ServiceRecordDocumentation: + var factory2 = new AiFunctionSystemPromptBuilder(prompt_path); + var prompt2 = factory2.CreateServiceRecordDocumentationSystemPrompt(context); + return prompt2; + default: + break; + } + + throw BeWoNotImplementedException.CreateFromEnum(actionType); + } + } +} diff --git a/Server/Components/AICore/Prompt/SystemPrompts/AiConversationSystemPromptBuilder.cs b/Server/Components/AICore/Prompt/SystemPrompts/AiConversationSystemPromptBuilder.cs index f230bb972..a21fb8682 100644 --- a/Server/Components/AICore/Prompt/SystemPrompts/AiConversationSystemPromptBuilder.cs +++ b/Server/Components/AICore/Prompt/SystemPrompts/AiConversationSystemPromptBuilder.cs @@ -14,7 +14,7 @@ using System.Text.Json; namespace AICore.Prompt.SystemPrompts { - public class AiConversationSystemPromptBuilder : BaseSystemPromptBuilder + internal class AiConversationSystemPromptBuilder : BaseSystemPromptBuilder { public AiConversationSystemPromptBuilder(string prompt_path) : base(prompt_path) { diff --git a/Server/Components/AICore/Prompt/SystemPrompts/AiFunctionSystemPromptBuilder.cs b/Server/Components/AICore/Prompt/SystemPrompts/AiFunctionSystemPromptBuilder.cs index 36fceb03e..6ffb6a3c9 100644 --- a/Server/Components/AICore/Prompt/SystemPrompts/AiFunctionSystemPromptBuilder.cs +++ b/Server/Components/AICore/Prompt/SystemPrompts/AiFunctionSystemPromptBuilder.cs @@ -13,7 +13,7 @@ using System.Threading.Tasks; namespace AICore.Prompt.SystemPrompts { - public class AiFunctionSystemPromptBuilder : BaseSystemPromptBuilder + internal class AiFunctionSystemPromptBuilder : BaseSystemPromptBuilder { public AiFunctionSystemPromptBuilder(string prompt_path) : base(prompt_path) { diff --git a/Server/Components/AICore/Prompt/SystemPrompts/BaseSystemPromptBuilder.cs b/Server/Components/AICore/Prompt/SystemPrompts/BaseSystemPromptBuilder.cs index ccf48c35a..759459afe 100644 --- a/Server/Components/AICore/Prompt/SystemPrompts/BaseSystemPromptBuilder.cs +++ b/Server/Components/AICore/Prompt/SystemPrompts/BaseSystemPromptBuilder.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace AICore.Prompt.SystemPrompts { - public class BaseSystemPromptBuilder + internal class BaseSystemPromptBuilder { public readonly static Dictionary Translations = new Dictionary { diff --git a/Service/Plugins/AiFunctionService.cs b/Service/Plugins/AiFunctionService.cs index 275eb3d69..e303d4248 100644 --- a/Service/Plugins/AiFunctionService.cs +++ b/Service/Plugins/AiFunctionService.cs @@ -24,102 +24,102 @@ using BS.Shared.Exceptions; namespace BeWo.Service.Plugins { - public class AiFunctionService - { - public AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(AiFunctionDocRequest request) - { - // Request valideren - ValidateDocRequest(request); + //public class AiFunctionService + //{ + // //public AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(AiFunctionDocRequest request) + // //{ + // // // Request valideren + // // ValidateDocRequest(request); - // Request auswerten - // DB Daten holen - // DB Daten prüfen - // Anfrage zusammensetzen + // // // Request auswerten + // // // DB Daten holen + // // // DB Daten prüfen + // // // Anfrage zusammensetzen - var prompt_baustein_ref = request.AiPromptbausteinPrompt; - var prompt_baustein_ref_oid = prompt_baustein_ref.Oid; - var prompt_baustein_ref_ver = prompt_baustein_ref.Version; + // // var prompt_baustein_ref = request.AiPromptbausteinPrompt; + // // var prompt_baustein_ref_oid = prompt_baustein_ref.Oid; + // // var prompt_baustein_ref_ver = prompt_baustein_ref.Version; - if (prompt_baustein_ref_oid <= 0) - throw new BeWoNotImplementedException(); + // // if (prompt_baustein_ref_oid <= 0) + // // throw new BeWoNotImplementedException(); - var prompt_baustein = DAOFactory.GenericDAO.LoadByID(prompt_baustein_ref_oid); - ServiceLogic.ConcurrencyCheck(prompt_baustein_ref_ver, prompt_baustein); + // // var prompt_baustein = DAOFactory.GenericDAO.LoadByID(prompt_baustein_ref_oid); + // // ServiceLogic.ConcurrencyCheck(prompt_baustein_ref_ver, prompt_baustein); - var context = AiFunctionContextLoader.LoadServiceRecordDocumentationContext(request, prompt_baustein.SamplesCount); + // // var context = AiFunctionContextLoader.LoadServiceRecordDocumentationContext(request, prompt_baustein.SamplesCount); - var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomFunctionPreSystemPrompt); - var factory = new AiFunctionSystemPromptBuilder(configPath); - var system_prompt = factory.CreateServiceRecordDocumentationSystemPrompt(context); + // // var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomFunctionPreSystemPrompt); + // // var factory = new AiFunctionSystemPromptBuilder(configPath); + // // var system_prompt = factory.CreateServiceRecordDocumentationSystemPrompt(context); - // Anfrage abschicken - var result = getResult(system_prompt, prompt_baustein.Prompt); + // // // Anfrage abschicken + // // var result = getResult(system_prompt, prompt_baustein.Prompt); - // Response auswerten + // // // Response auswerten - // Eigene Response erstellen - var response = new AiFunctionDocResponse(); - response.Result = result; + // // // Eigene Response erstellen + // // var response = new AiFunctionDocResponse(); + // // response.Result = result; - return response; - } + // // return response; + // //} - private string getResult(string system, string user_prompt) - { - var models = ServiceFacade.Ollama2ApiClient.GetAiModelle(); + // //private string getResult(string system, string user_prompt) + // //{ + // // var models = ServiceFacade.Ollama2ApiClient.GetAiModelle(); - var model = models.Data.FirstOrDefault(x => x.ModelName == "gemma3_12b_max_context:latest"); + // // var model = models.Data.FirstOrDefault(x => x.ModelName == "gemma3_12b_max_context:latest"); - if (model == null) - throw new NotImplementedException(); + // // if (model == null) + // // throw new NotImplementedException(); - if (user_prompt == null) - user_prompt = string.Empty; + // // if (user_prompt == null) + // // user_prompt = string.Empty; - var messages = new List() - { - system, user_prompt - }; + // // var messages = new List() + // // { + // // system, user_prompt + // // }; - if(model.Value > -1) - { - if(!AiUtils.CheckUserAllMessageContextSize(messages, model.ModelName, model.Value, out var error)) - { - throw new BeWoNotImplementedException(error); - } - } + // // if(model.Value > -1) + // // { + // // if(!AiUtils.CheckUserAllMessageContextSize(messages, model.ModelName, model.Value, out var error)) + // // { + // // throw new BeWoNotImplementedException(error); + // // } + // // } - var logger = ServiceLogger.GetRequestLogger(out string id); + // // var logger = ServiceLogger.GetRequestLogger(out string id); - var payload = new - { - model = "gemma3_12b_max_context:latest", - messages = new[] - { - new - { - role = "System", - content = system - }, - new - { - role = "User", - content = user_prompt - }, - }, - customerid = UserRightHelper.GetTenant(), - requestid = id - }; + // // var payload = new + // // { + // // model = "gemma3_12b_max_context:latest", + // // messages = new[] + // // { + // // new + // // { + // // role = "System", + // // content = system + // // }, + // // new + // // { + // // role = "User", + // // content = user_prompt + // // }, + // // }, + // // customerid = UserRightHelper.GetTenant(), + // // requestid = id + // // }; - var response = ServiceFacade.Ollama2ApiClient.SendAiMessageRequest(payload, out string message, out string model2, out int? duration, logger); + // // var response = ServiceFacade.Ollama2ApiClient.SendAiMessageRequest(payload, out string message, out string model2, out int? duration, logger); - return message; - } + // // return message; + // //} - private void ValidateDocRequest(AiFunctionDocRequest request) - { - //throw new NotImplementedException(); - } - } + // //private void ValidateDocRequest(AiFunctionDocRequest request) + // //{ + // // //throw new NotImplementedException(); + // //} + //} } diff --git a/Service/Plugins/AiService2.cs b/Service/Plugins/AiService2.cs index 015d56fc9..834f70901 100644 --- a/Service/Plugins/AiService2.cs +++ b/Service/Plugins/AiService2.cs @@ -9,6 +9,8 @@ using System.Security.Cryptography; using System.ServiceModel; using System.Text; using System.Web.Hosting; +using AICore.Context.Summary; +using AICore.Prompt; using AICore.Prompt.SystemPrompts; using BeWo.Data.Access; using BeWo.Data.Entities; @@ -20,6 +22,7 @@ using BeWo.Service.ServiceUtils; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts.Feature.AI; +using BS.Shared.DataContracts.Feature.AI.Functions.ServiceRecords; using BS.Shared.Exceptions; using BS.Shared.Extensions; using DevExpress.Charts.Native; @@ -119,11 +122,9 @@ namespace BeWo.Service.Plugins return dcs; } - public virtual AiConversationDC CreateAiConversation(AiConversationDC conversation, long modell_oid) + public virtual AiConversationDC CreateAiConversation(AiConversationDC conversation, AiViewContextDC view_context) { - var config = GetAiConfig(); - - var conv = createAiConversation(conversation.ActionType, conversation.ContextBeWoObjects, modell_oid, config.Context_Type); + var conv = createAiConversation(conversation.ActionType, view_context); DAOFactory.GenericDAO.Insert(conv); @@ -131,11 +132,11 @@ namespace BeWo.Service.Plugins return conv_dc; } - public virtual AiConversationDC CreateAiConversationWithMessage(AiConversationDC conversation, long modell_oid, string message) + public virtual AiConversationDC CreateAiConversationWithMessage(AiConversationDC conversation, AiViewContextDC view_context, string message) { var config = GetAiConfig(); - var conv = createAiConversation(conversation.ActionType, conversation.ContextBeWoObjects, modell_oid, config.Context_Type); + var conv = createAiConversation(conversation.ActionType, view_context, config); var new_messages = sendNewMessage(conv, message, config); @@ -145,6 +146,22 @@ namespace BeWo.Service.Plugins return conv_dc; } + public AiConversationDC CreateAiConversationWithPromptbaustein(AiConversationDC conversation, AiViewContextDC view_context, long oid) + { + var config = GetAiConfig(); + + var conv = createAiConversation(conversation.ActionType, view_context, config); + + var promptbaustein = DAOFactory.GenericDAO.LoadByID(oid); + + var new_messages = sendNewMessage(conv, promptbaustein.Prompt, config); + + DAOFactory.GenericDAO.Insert(conv); + + var conv_dc = MapperFactory.AiConversation.MapToNewDC(conv); + + return conv_dc; + } public virtual AiConversationDC CloneAiConversation(long conversation_oid, long? last_message_oid) { var parent_conv = DAOFactory.GenericDAO.GetByID(conversation_oid); @@ -218,19 +235,16 @@ namespace BeWo.Service.Plugins return sendNewMessage(conv, message); } - public virtual bool CheckContextSize(AiActionType actionType, Dictionary context, long? modell_oid) + public virtual bool CheckContextSize(AiActionType actionType, AiViewContextDC view_context) { - var modell_oid_value = modell_oid ?? GetAiConfig().SelectedModel.Oid.Value; + var modell_oid_value = GetAiConfig().SelectedModel.Oid.Value; var modell = DAOFactory.GenericDAO.LoadByID(modell_oid_value); if (modell.Value < 0) return true; - if (modell_oid is null) - modell_oid = GetAiConfig().SelectedModel.Oid; - - var conversation = createAiConversation(actionType, context, modell_oid.Value, AiDataContextType.csv2); + var conversation = createAiConversation(actionType, view_context); return AiUtils.CheckSystemMessageContextSize(conversation.Messages.Select(x => x.Message), modell.ModelName, modell.Value, out var error); } @@ -305,21 +319,25 @@ namespace BeWo.Service.Plugins return models; } - protected virtual AiConversation createAiConversation(AiActionType actionType, Dictionary context_reference, long modell_oid, AiDataContextType context_Type) + protected virtual AiConversation createAiConversation(AiActionType actionType, AiViewContextDC view_context, AiConfigDC config = null) { + if (config is null) + config = GetAiConfig(); + + var context_Type = config.Context_Type; + var modell_oid = config.SelectedModel.Oid.Value; + var current_user = UserRightHelper.GetLoggedInUserWithOid(); + var context_loaded = AiContextLoader.LoadContext(actionType, view_context); - var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomPreSystemPrompt); - var factory = new AiConversationSystemPromptBuilder(configPath); - - var context_loaded = AiContextLoader.LoadContext(actionType, context_reference); - var system_prompt = factory.CreateSystemPrompt(actionType, context_loaded, context_Type); + var configPath = getPromptPath(actionType); + var system_prompt = AiSystemPromptFactory.BuildContext(actionType, configPath, context_loaded, context_Type); var conv = new AiConversation(); conv.Created = DateTime.Now; // RETODO Muss noch bzgl Sicherheit geprüft werden. - conv.ContextBeWoObjects = MapperFactory.AiConversation.Parse(context_reference); + conv.ContextBeWoObjects = MapperFactory.AiConversation.Parse(view_context.References); conv.Displayname = "Neue Unterhaltung"; conv.Messages = new List(); conv.Modell = DAOFactory.GenericDAO.LoadByID(modell_oid); @@ -342,7 +360,7 @@ namespace BeWo.Service.Plugins protected virtual AiConversationMessageDC[] sendNewMessage(AiConversation conv, string message, AiConfigDC config = null) { // Hole Config - if (config == null) + if (config is null) config = GetAiConfig(); var configModel = config.SelectedModel; @@ -373,9 +391,9 @@ namespace BeWo.Service.Plugins // Check Token Size if (-1 < configModel.Value) { - if(conv.Messages.Count <= 2) + if (conv.Messages.Count <= 2) { - if(!AiUtils.CheckUserFirstMessageContextSize(conv.Messages.Select(x => x.Message), configModelName, configModel.Value, out var error)) + if (!AiUtils.CheckUserFirstMessageContextSize(conv.Messages.Select(x => x.Message), configModelName, configModel.Value, out var error)) { throw new BeWoNotImplementedException(error); } @@ -429,7 +447,7 @@ namespace BeWo.Service.Plugins var usersetting = DAOFactory.GenericSearchDAO.LoadEntityByProperty(nameof(AiUserSetting.Oid), current_user.Oid); - if(usersetting == null) + if (usersetting == null) { var new_usersetting = new AiUserSettingDC(); @@ -440,5 +458,35 @@ namespace BeWo.Service.Plugins return usersetting; } + + private string getPromptPath(AiActionType actionType) + { + SpecialConfig? specialConfig = null; + + switch (actionType) + { + case AiActionType.ServiceRecord: + break; + case AiActionType.ServiceRecordConversation: + specialConfig = SpecialConfig.CustomPreSystemPrompt; + break; + case AiActionType.ServiceRecordDocumentation: + specialConfig = SpecialConfig.CustomFunctionPreSystemPrompt; + break; + default: + break; + } + + if (specialConfig == null) + throw BeWoNotImplementedException.CreateFromEnum(actionType); + + var path = ConfigReader.GetConfigPath(specialConfig.Value); + + return path; + } + private void CheckPermissions(AiActionType actionType) + { + + } } } \ No newline at end of file diff --git a/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs b/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs index 782d36715..af4d3b855 100644 --- a/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs +++ b/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs @@ -40,13 +40,15 @@ namespace BeWo.Service.ServiceContracts.Enhanced [FaultContract(typeof(BeWoFault))] [OperationContract] - [RequireWcfAuthorization(UserRightType.AiModuleServiceRecordChat)] - AiConversationDC CreateAiConversation(AiConversationDC conversation, long modell_oid); + AiConversationDC CreateAiConversation(AiConversationDC conversation, AiViewContextDC view_context); [FaultContract(typeof(BeWoFault))] [OperationContract] - [RequireWcfAuthorization(UserRightType.AiModuleServiceRecordChat)] - AiConversationDC CreateAiConversationWithMessage(AiConversationDC conversation, long modell_oid, string message); + AiConversationDC CreateAiConversationWithMessage(AiConversationDC conversation, AiViewContextDC view_context, string message); + + [FaultContract(typeof(BeWoFault))] + [OperationContract] + AiConversationDC CreateAiConversationWithPromptbaustein(AiConversationDC conversation, AiViewContextDC view_context, long prompt_oid); [FaultContract(typeof(BeWoFault))] [OperationContract] @@ -71,7 +73,7 @@ namespace BeWo.Service.ServiceContracts.Enhanced [FaultContract(typeof(BeWoFault))] [OperationContract] [RequireWcfAuthorization(UserRightType.AiModuleServiceRecordChat)] - bool CheckContextSize(AiActionType actionType, Dictionary context, long? modell_oid); + bool CheckContextSize(AiActionType actionType, AiViewContextDC view_context); [FaultContract(typeof(BeWoFault))] [OperationContract] @@ -123,11 +125,6 @@ namespace BeWo.Service.ServiceContracts.Enhanced [RequireWcfAuthorization(UserRightType.AiModulePromptbausteineEdit)] void DeleteAiPromptbausteinRoutine(AiPromptbausteinRoutineDC routine); - [FaultContract(typeof(BeWoFault))] - [OperationContract] - [RequireWcfAuthorization(UserRightType.AiModuleServiceRecordDocu)] - AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(AiFunctionDocRequest request); - [FaultContract(typeof(BeWoFault))] [OperationContract] AiUserSettingDC GetUserSetting(); diff --git a/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs b/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs index 669d9aebf..5dc993d23 100644 --- a/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs +++ b/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs @@ -31,10 +31,12 @@ namespace BeWo.Service.ServiceImplementations.Enhanced public IEnumerable GetAiConversations(AiActionType actionType, long? oid) => GetAiService().GetAiConversations(actionType, oid); - public AiConversationDC CreateAiConversation(AiConversationDC conversation, long modell_oid) - => GetAiService().CreateAiConversation(conversation, modell_oid); - public AiConversationDC CreateAiConversationWithMessage(AiConversationDC conversation, long modell_oid, string message) - => GetAiService().CreateAiConversationWithMessage(conversation, modell_oid, message); + public AiConversationDC CreateAiConversation(AiConversationDC conversation, AiViewContextDC view_context) + => GetAiService().CreateAiConversation(conversation, view_context); + public AiConversationDC CreateAiConversationWithMessage(AiConversationDC conversation, AiViewContextDC view_context, string message) + => GetAiService().CreateAiConversationWithMessage(conversation, view_context, message); + public AiConversationDC CreateAiConversationWithPromptbaustein(AiConversationDC conversation, AiViewContextDC view_context, long prompt_oid) + => GetAiService().CreateAiConversationWithPromptbaustein(conversation, view_context, prompt_oid); public AiConversationDC CloneAiConversation(long conversation_oid, long? last_message_oid) => GetAiService().CloneAiConversation(conversation_oid, last_message_oid); @@ -48,8 +50,8 @@ namespace BeWo.Service.ServiceImplementations.Enhanced public AiConversationMessageDC[] SendNewMessage(long conversation, string message) => GetAiService().SendNewMessage(conversation, message); - public bool CheckContextSize(AiActionType actionType, Dictionary context, long? modell_oid) - => GetAiService().CheckContextSize(actionType, context, modell_oid); + public bool CheckContextSize(AiActionType actionType, AiViewContextDC view_context) + => GetAiService().CheckContextSize(actionType, view_context); public AiUserSettingDC GetUserSetting() => GetAiService().GetUserSetting(); @@ -110,18 +112,5 @@ namespace BeWo.Service.ServiceImplementations.Enhanced private AiPromptbausteinRoutineLogic GetRoutineLogic() => new AiPromptbausteinRoutineLogic(); #endregion - - #region Functions - public AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(AiFunctionDocRequest request) - => GetAiFunctionService().ExecuteAiFunctionServiceRecordDocumentation(request); - - private AiFunctionService GetAiFunctionService() - { - return new AiFunctionService(); - - //var plugin = PluginLoader.FindClass(); - //return plugin; - } - #endregion } } diff --git a/Service/ServiceUtils/AiContextLoader.cs b/Service/ServiceUtils/AiContextLoader.cs index b3a12d5a2..b125a6994 100644 --- a/Service/ServiceUtils/AiContextLoader.cs +++ b/Service/ServiceUtils/AiContextLoader.cs @@ -14,21 +14,22 @@ using System.Threading.Tasks; using AICore.Context.Core; using DevExpress.DataAccess.Native.Sql; using BS.Shared.Exceptions; +using BS.Shared.DataContracts.Feature.AI; namespace BeWo.Service.ServiceUtils { public static class AiContextLoader { - public static BaseContextSummary LoadContext(AiActionType actionType, Dictionary context_reference) + public static BaseContextSummary LoadContext(AiActionType actionType, AiViewContextDC view_context) { switch (actionType) { case AiActionType.ServiceRecord: break; case AiActionType.ServiceRecordConversation: + return loadServiceRecordConversationContext(view_context.References); case AiActionType.ServiceRecordDocumentation: - return loadServiceRecordContext(context_reference); - + return loadServiceRecordDocumentationContext(view_context.References, view_context.Secondary as string); default: break; } @@ -94,7 +95,7 @@ namespace BeWo.Service.ServiceUtils throw BeWoNotImplementedException.CreateFromEnum(actionType); } - private static BaseContextSummary loadServiceRecordContext(Dictionary context_reference) + private static BaseContextSummary loadServiceRecordConversationContext(Dictionary context_reference) { var customer = loadCompactCustomerDC(context_reference); var serviceRecords = loadServiceRecordDCs(context_reference); @@ -104,6 +105,16 @@ namespace BeWo.Service.ServiceUtils return AiContextSummaryFactory.CreateServiceRecordNavigationContextSummary(serviceRecords, customer, costBearer, supportConcept); } + private static BaseContextSummary loadServiceRecordDocumentationContext(Dictionary context_reference, string doku_text) + { + var customer = loadCompactCustomerDC(context_reference); + var serviceRecords = loadServiceRecordDCs(context_reference); + var supportConcept = loadSupportConceptDC(context_reference); + var costBearer = supportConcept.CostBearerList.First(x => x.CostBearerOid == context_reference[TableID.CostBearer][0]); + + return AiContextSummaryFactory.CreateServiceRecordDocumentationFunction(serviceRecords, customer, costBearer, supportConcept, doku_text); + } + private static bool oidsContainsOid(long[] oids, long oid) { if (oids == null || oids.Length == 0) diff --git a/Shared/DataContracts/Feature/AI/AiViewContextDC.cs b/Shared/DataContracts/Feature/AI/AiViewContextDC.cs index 8211fadcb..f5a23237e 100644 --- a/Shared/DataContracts/Feature/AI/AiViewContextDC.cs +++ b/Shared/DataContracts/Feature/AI/AiViewContextDC.cs @@ -11,7 +11,7 @@ namespace BS.Shared.DataContracts.Feature.AI public class AiViewContextDC : IDataContract { [DataMember] - public object Primary { get; set; } + public long? Primary { get; set; } [DataMember] public object Secondary { get; set; }