@Override public String storeConversationMemorySnapshot(ConversationMemorySnapshot snapshot) throws IResourceStore.ResourceStoreException { try { String json = documentBuilder.toString(snapshot); Document document = Document.parse(json); document.remove("conversationId"); if (snapshot.getConversationId() != null) { document.put(OBJECT_ID, new ObjectId(snapshot.getConversationId())); conversationCollection.updateOne(new Document(OBJECT_ID, new ObjectId(snapshot.getConversationId())), new Document("$set", document), new UpdateOptions().upsert(true)); } else { conversationCollection.insertOne(document); } return document.get(OBJECT_ID).toString(); } catch (IOException e) { throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e); } }
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; }