/**************************************************************************** * Copyright (c) 2022-2025 Imixs Software Solutions GmbH and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * https://www.eclipse.org/legal/epl-2.0 * * This Source Code may also be made available under the terms of the * GNU General Public License, version 2 or later (GPL-2.0-or-later), * which is available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later ****************************************************************************/ package ai.dynamixs; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import org.imixs.ai.rag.RAGService; import org.imixs.workflow.ItemCollection; import org.imixs.workflow.WorkflowKernel; import org.imixs.workflow.engine.WorkflowService; import org.imixs.workflow.exceptions.AdapterException; import org.imixs.workflow.exceptions.PluginException; import org.imixs.workflow.exceptions.QueryException; import jakarta.inject.Inject; /** * The OrderPlugin is a generic plugin for Order workflows. *

* The plugin collects a list of $uniqueids for the last 5 orders for the given * customer.ref. The list is stored in the item `order.history.ref` * *

* The plugin extends the default imixs-ai RAGIndexPlugin. In the mode INDEX * this plugins generates a default prompt template.This is to simplify the * models. *

* *

  {@code
   
      false  
   
    }
 * 
*

* The tag 'debug' is optional * * * @author rsoika * */ public class OrderPlugin extends org.imixs.ai.rag.RAGIndexPlugin { private static final Logger logger = Logger.getLogger(OrderPlugin.class.getName()); public static final String LINK_PROPERTY = "$workitemref"; public static final int MAX_HISTORY_RESULT = 5; @Inject private WorkflowService workflowService; @Inject private RAGService ragService; @Override public ItemCollection run(ItemCollection workitem, ItemCollection event) throws PluginException { // do not run on Snapshots! if (workitem.getType().startsWith("snapshot-")) { // no op! return workitem; } logger.info("├── running Dynamixs.AI Order Plugin"); // Update History.... updateOrderHistory(workitem); // read optional configuration form the model or imixs.properties.... List ragIndexDefinitions = workflowService.evalWorkflowResultXML(event, "dynamixs-ai", RAGService.RAG_INDEX, workitem, false); try { // INDEX Mode? if (ragIndexDefinitions != null && ragIndexDefinitions.size() > 0) { // do we have a prompt template? ItemCollection indexDefinition = ragIndexDefinitions.get(0); String promptTemplate = indexDefinition.getItemValueString("promptTemplate"); if (promptTemplate.isBlank()) { // generate default template ChronicleTextBuilder chronicleTextBuilder = new ChronicleTextBuilder(workitem); promptTemplate = chronicleTextBuilder.build(); workitem.setItemValue("ai.chronicle.summary", promptTemplate); indexDefinition.setItemValue("promptTemplate", embedTemplate(promptTemplate)); ragService.createIndex(ragIndexDefinitions, workitem, event); } } } catch (AdapterException e) { throw new PluginException(e); } return workitem; } /** * This method updates the item `order.history.ref` with the latest 5 orders for * the current customer * * @param workitem */ private void updateOrderHistory(ItemCollection workitem) { workitem.removeItem("order.history.ref"); String customerRef = workitem.getItemValueString("customer.ref"); if (!customerRef.isEmpty()) { // append customer ref to $workitemref workitem.appendItemValueUnique("$workitemref", customerRef); // collect all history orders.... String sQuery = "("; sQuery = " (type:\"workitem\" OR type:\"workitemarchive\") AND (" + LINK_PROPERTY + ":\"" + customerRef + "\") AND ($workflowgroup:\"" + workitem.getWorkflowGroup() + "\")"; List orderHistory = null; try { orderHistory = workflowService.getDocumentService().findStubs(sQuery, MAX_HISTORY_RESULT, 0, WorkflowKernel.LASTEVENTDATE, true); } catch (QueryException e) { e.printStackTrace(); } if (orderHistory != null) { List orderHistoryRefList = new ArrayList<>(); for (ItemCollection order : orderHistory) { if (!workitem.getUniqueID().equals(order.getUniqueID())) { // skip current id! orderHistoryRefList.add(order.getUniqueID()); } } workitem.setItemValue("order.history.ref", orderHistoryRefList); } } } /** * Embedds a prompt into a imixs prompt template * * * @param prompt * @return */ private String embedTemplate(String prompt) { String template = "\n" + // "\n" + // " {}\n" + // " \n" + // "\n" + // " "; return template; } }