office-dynamixs-ai/dynamixs-ai-app/src/main/java/ai/dynamixs/OrderPlugin.java
2026-02-09 11:14:10 +01:00

175 lines
5.2 KiB
Java

/****************************************************************************
* 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.index.IndexService;
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.
* <p>
* 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`
*
* <p>
* 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.
* <p>
*
* <pre>
{@code
<dynamixs-ai name="INDEX">
<debug>false</debug>
</dynamixs-ai>
}
* </pre>
* <p>
* The tag 'debug' is optional
*
*
* @author rsoika
*
*/
public class OrderPlugin extends org.imixs.ai.rag.workflow.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 IndexService 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<ItemCollection> ragIndexDefinitions = workflowService.evalWorkflowResultXML(event, "dynamixs-ai",
IndexService.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));
loadPromptDefinition(indexDefinition, event);
ragService.indexWorkitem(indexDefinition, workitem);
}
}
} 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<ItemCollection> orderHistory = null;
try {
orderHistory = workflowService.getDocumentService().findStubs(sQuery, MAX_HISTORY_RESULT, 0,
WorkflowKernel.LASTEVENTDATE, true);
} catch (QueryException e) {
e.printStackTrace();
}
if (orderHistory != null) {
List<String> 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
"<PromptDefinition>\n" + //
" <prompt_options>{}</prompt_options>\n" + //
" <prompt><![CDATA[";
template = template + prompt;
template = template + "]]>\n" + //
"</prompt>\n" + //
"</PromptDefinition> ";
return template;
}
}