From f4f7dd2bf976d61349cb28877da4175bd3572d6e Mon Sep 17 00:00:00 2001 From: Rene Evertz Date: Thu, 6 Aug 2026 11:30:45 +0200 Subject: [PATCH] ExecuteAiConversationWithRoutineComplete --- .../GeneratedAiEnhancedService.cs | 45 +++++++++++++- Service/Plugins/AiService2.cs | 59 +++++++++++++++++++ .../Enhanced/IAiEnhancedService.cs | 4 ++ .../Enhanced/AiEnhancedServiceImp.cs | 5 +- 4 files changed, 110 insertions(+), 3 deletions(-) diff --git a/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs b/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs index 491630257..730f929fc 100644 --- a/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs +++ b/BeWo/ServiceProxy/GeneratedAiEnhancedService.cs @@ -192,7 +192,43 @@ namespace BeWo.ServiceProxy [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.ValueListEntryType))] [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiRoutineType))] System.Collections.Generic.List ExecuteAiConversationWithRoutine(long conversation, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, long routine, int step); - + + [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAiEnhancedService/ExecuteAiConversationWithRoutineComplete", ReplyAction="http://tempuri.org/IAiEnhancedService/ExecuteAiConversationWithRoutineCompleteResponse")] + [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAiEnhancedService/ExecuteAiConversationWithRoutineCompleteBeW" + + "oFaultFault", 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))] + [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.AiRoutineType))] + System.Collections.Generic.List ExecuteAiConversationWithRoutineComplete(BS.Shared.AiActionType actionType, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, long routine); + [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")] [System.ServiceModel.ServiceKnownTypeAttribute(typeof(BS.Shared.DataContracts.BeWoDataContract))] @@ -373,7 +409,12 @@ namespace BeWo.ServiceProxy { return base.Channel.ExecuteAiConversationWithRoutine(conversation, view_context, routine, step); } - + + public System.Collections.Generic.List ExecuteAiConversationWithRoutineComplete(BS.Shared.AiActionType actionType, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context, long routine) + { + return base.Channel.ExecuteAiConversationWithRoutineComplete(actionType, view_context, routine); + } + public bool CheckContextSize(BS.Shared.AiActionType actionType, BS.Shared.DataContracts.Feature.AI.AiViewContextDC view_context) { return base.Channel.CheckContextSize(actionType, view_context); diff --git a/Service/Plugins/AiService2.cs b/Service/Plugins/AiService2.cs index facb4ecd1..854336321 100644 --- a/Service/Plugins/AiService2.cs +++ b/Service/Plugins/AiService2.cs @@ -259,6 +259,65 @@ namespace BeWo.Service.Plugins return executePrompt(conversation, view_context, prompt.Oid.Value, routine_ent.RoutineType); } + public virtual AiConversationMessageDC[] ExecuteAiConversationWithRoutineComplete(AiActionType actionType, AiViewContextDC view_context, long routine) + { + var ai_config = GetAiConfig(); + + var routine_ent = DAOFactory.GenericDAO.LoadByID(routine); + + // Gesamter Chatverlauf über alle Schritte hinweg (analog zu ExecuteChain im ViewModel). + var history = new List(); + + for (int step = 0; step < routine_ent.Steps.Count; step++) + { + var step_ent = routine_ent.Steps[step]; + var prompt = step_ent.PromptReference; + + var custom_viewContext = new AiViewContextDC + { + References = view_context.References, + Primary = view_context.Primary + }; + + if (step > 0 && routine_ent.RoutineType == AiRoutineType.Chain) + { + custom_viewContext.Secondary = history.Last().Message; + } + else + { + custom_viewContext.Secondary = view_context.Secondary; + } + + var new_conv = createAiConversation(actionType, custom_viewContext, ai_config); + var systemPrompt = addSystemPrompt(new_conv, custom_viewContext, ai_config, prompt); + + if (routine_ent.RoutineType == AiRoutineType.Chain) + { + // Conversation = (S(n), U(n)) => A(n) + } + else if (routine_ent.RoutineType == AiRoutineType.Stack) + { + // Conversation = (S(n), U(0), A(0), U(1), ..., U(n-1), A(n-1), U(n)) => A(n) + var messages_filtered = history.Where(m => m.Role != AiConversationMessageRole.System); + + new_conv.Messages.AddRange(messages_filtered); + } + else + { + throw new ArgumentOutOfRangeException(nameof(routine_ent.RoutineType)); + } + + // Server Request - wirft bei Fehlschlag eine Exception und bricht damit den gesamten Aufruf ab. + sendNewMessage(new_conv, prompt.Prompt, false); + + history.Add(systemPrompt); + history.Add(new_conv.Messages.Last(x => x.Role == AiConversationMessageRole.User)); + history.Add(new_conv.Messages.Last(x => x.Role == AiConversationMessageRole.Assistent)); + } + + return MapperFactory.AiConversationMessage.MapToNewDCs(history).ToArray(); + } + public virtual bool CheckContextSize(AiActionType actionType, AiViewContextDC view_context) { var modell_oid_value = GetAiConfig().SelectedModel.Oid.Value; diff --git a/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs b/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs index 638d625f0..64efb3377 100644 --- a/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs +++ b/Service/ServiceContracts/Enhanced/IAiEnhancedService.cs @@ -72,6 +72,10 @@ namespace BeWo.Service.ServiceContracts.Enhanced [OperationContract] AiConversationMessageDC[] ExecuteAiConversationWithRoutine(long conversation, AiViewContextDC view_context, long routine, int step); + [FaultContract(typeof(BeWoFault))] + [OperationContract] + AiConversationMessageDC[] ExecuteAiConversationWithRoutineComplete(AiActionType actionType, AiViewContextDC view_context, long routine); + [FaultContract(typeof(BeWoFault))] [OperationContract] //[RequireWcfAuthorization(UserRightType.AiModuleServiceRecordChat)] diff --git a/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs b/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs index e792d81f2..b31776a00 100644 --- a/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs +++ b/Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs @@ -52,9 +52,12 @@ namespace BeWo.Service.ServiceImplementations.Enhanced public AiConversationMessageDC[] ExecuteAiConversationWithPrompt(long conversation, AiViewContextDC view_context, long prompt_oid) => GetAiService().ExecuteAiConversationWithPrompt(conversation, view_context, prompt_oid); - public AiConversationMessageDC[] ExecuteAiConversationWithRoutine(long conversation, AiViewContextDC view_context, long routine, int step) + public AiConversationMessageDC[] ExecuteAiConversationWithRoutine(long conversation, AiViewContextDC view_context, long routine, int step) => GetAiService().ExecuteAiConversationWithRoutine(conversation, view_context, routine, step); + public AiConversationMessageDC[] ExecuteAiConversationWithRoutineComplete(AiActionType actionType, AiViewContextDC view_context, long routine) + => GetAiService().ExecuteAiConversationWithRoutineComplete(actionType, view_context, routine); + public bool CheckContextSize(AiActionType actionType, AiViewContextDC view_context) => GetAiService().CheckContextSize(actionType, view_context);