private SimpleConversationMemorySnapshot getSimpleConversationMemorySnapshot( IConversationMemory returnConversationMemory, Boolean returnDetailed, Boolean returnCurrentStepOnly, List<String> returningFields) { return getSimpleConversationMemorySnapshot( convertConversationMemory(returnConversationMemory), returnDetailed, returnCurrentStepOnly, returningFields); }
private IConversationMemory loadConversationMemory(String conversationId) throws ResourceStoreException, ResourceNotFoundException { var conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(conversationId); return convertConversationMemorySnapshot(conversationMemorySnapshot); }
private void addContextToConversationOutput(IWritableConversationStep currentStep, List<IData<Context>> contextData) { if (!contextData.isEmpty()) { var context = ConversationMemoryUtilities.prepareContext(contextData); currentStep.addConversationOutputMap(KEY_CONTEXT, context); } }
private SimpleConversationMemorySnapshot getSimpleConversationMemorySnapshot( ConversationMemorySnapshot conversationMemorySnapshot, Boolean returnDetailed, Boolean returnCurrentStepOnly, List<String> returningFields) { var memorySnapshot = convertSimpleConversationMemory(conversationMemorySnapshot, returnDetailed); if (returnCurrentStepOnly) { if (returningFields.isEmpty() || returningFields.contains("conversationSteps")) { var conversationSteps = memorySnapshot.getConversationSteps(); var conversationStep = conversationSteps.get(conversationSteps.size() - 1); conversationSteps.clear(); conversationSteps.add(conversationStep); } else { memorySnapshot.setConversationSteps(null); } if (returningFields.isEmpty() || returningFields.contains("conversationOutputs")) { var conversationOutputs = memorySnapshot.getConversationOutputs(); var conversationOutput = conversationOutputs.get(conversationOutputs.size() - 1); conversationOutputs.clear(); conversationOutputs.add(conversationOutput); } else { memorySnapshot.setConversationOutputs(null); } } return memorySnapshot; }
public static ConversationMemorySnapshot convertConversationMemory(IConversationMemory conversationMemory) { ConversationMemorySnapshot snapshot = new ConversationMemorySnapshot(); if (conversationMemory.getUserId() != null) { snapshot.setUserId(conversationMemory.getUserId()); } if (conversationMemory.getConversationId() != null) { snapshot.setConversationId(conversationMemory.getConversationId()); } snapshot.setBotId(conversationMemory.getBotId()); snapshot.setBotVersion(conversationMemory.getBotVersion()); snapshot.setConversationState(conversationMemory.getConversationState()); for (IConversationMemory.IConversationStep redoStep : conversationMemory.getRedoCache()) { ConversationMemorySnapshot.ConversationStepSnapshot redoStepSnapshot = iterateConversationStep(redoStep); snapshot.getRedoCache().push(redoStepSnapshot); } for (int i = conversationMemory.getAllSteps().size() - 1; i >= 0; i--) { IConversationMemory.IConversationStep conversationStep = conversationMemory.getAllSteps().get(i); snapshot.getConversationSteps().add(iterateConversationStep(conversationStep)); } snapshot.getConversationOutputs().addAll(conversationMemory.getConversationOutputs()); snapshot.getConversationProperties().putAll(conversationMemory.getConversationProperties()); return snapshot; }
public static IConversationMemory convertConversationMemorySnapshot(ConversationMemorySnapshot snapshot) { ConversationMemory conversationMemory = new ConversationMemory(snapshot.getConversationId(), snapshot.getBotId(), snapshot.getBotVersion(), snapshot.getUserId()); conversationMemory.setConversationState(snapshot.getConversationState()); conversationMemory.getConversationProperties().putAll(snapshot.getConversationProperties()); List<IConversationMemory.IConversationStep> redoSteps = iterateRedoCache(snapshot.getRedoCache()); for (IConversationMemory.IConversationStep redoStep : redoSteps) { conversationMemory.getRedoCache().add(redoStep); } List<ConversationMemorySnapshot.ConversationStepSnapshot> conversationSteps = snapshot.getConversationSteps(); for (int i = 0; i < conversationSteps.size(); i++) { ConversationOutput conversationOutput = snapshot.getConversationOutputs().get(i); ConversationMemorySnapshot.ConversationStepSnapshot conversationStepSnapshot = conversationSteps.get(i); if (i > 0) { conversationMemory.startNextStep(conversationOutput); } for (ConversationMemorySnapshot.PackageRunSnapshot packageRunSnapshot : conversationStepSnapshot.getPackages()) { for (ConversationMemorySnapshot.ResultSnapshot resultSnapshot : packageRunSnapshot.getLifecycleTasks()) { Data data = new Data(resultSnapshot.getKey(), resultSnapshot.getResult(), resultSnapshot.getPossibleResults(), resultSnapshot.getTimestamp(), resultSnapshot.isPublic()); conversationMemory.getCurrentStep().storeData(data); } } } return conversationMemory; }
private String storeConversationMemory(IConversationMemory conversationMemory, Deployment.Environment environment) throws ResourceStoreException { var memorySnapshot = convertConversationMemory(conversationMemory); memorySnapshot.setEnvironment(environment); return conversationMemoryStore.storeConversationMemorySnapshot(memorySnapshot); }
private void mergeConversationMemory(IConversationMemory currentConversationMemory, ConversationMemorySnapshot callbackMemorySnapshot) { if (callbackMemorySnapshot != null && !callbackMemorySnapshot.getConversationSteps().isEmpty()) { var currentStep = currentConversationMemory.getCurrentStep(); var callbackConversationMemory = convertConversationMemorySnapshot(callbackMemorySnapshot); var currentCallbackStep = callbackConversationMemory.getCurrentStep(); Set<String> callbackKeys = currentCallbackStep.getAllKeys(); for (String callbackKey : callbackKeys) { currentStep.storeData(currentCallbackStep.getData(callbackKey)); } } }
@Override public Map<String, Object> convert(IConversationMemory memory) { Map<String, Object> ret = new LinkedHashMap<>(); List<IData<Context>> contextDataList = memory.getCurrentStep().getAllData(KEY_CONTEXT); var contextMap = prepareContext(contextDataList); var memoryMap = convertMemoryItems(memory); var conversationProperties = memory.getConversationProperties(); if (!contextMap.isEmpty()) { ret.put(KEY_CONTEXT, contextMap); ret.putAll(contextMap); } if (!conversationProperties.isEmpty()) { ret.put(KEY_PROPERTIES, conversationProperties.toMap()); } if (!memoryMap.isEmpty()) { ret.put(KEY_MEMORY, convertMemoryItems(memory)); } return ret; }
@Override public void executeTask(IConversationMemory memory) { IData<List<String>> actionData = memory.getCurrentStep().getLatestData(KEY_ACTIONS); if (!executeCallback(actionData)) { return; } var request = new ConversationDataRequest(); request.setConversationMemorySnapshot(convertConversationMemory(memory)); var response = conversationCallback.doExternalCall(callback, request, timeoutInMillis); if (String.valueOf(response.getHttpCode()).startsWith("2")) { //check for success, http code 2xx mergeConversationMemory(memory, response.getConversationMemorySnapshot()); } else { String msg = "ConversationCallback was (%s) but should have been 2xx. Return value as been ignored"; msg = String.format(msg, response.getHttpCode()); log.warn(msg); } }