private IConversationMemory loadConversationMemory(String conversationId) throws ResourceStoreException, ResourceNotFoundException { var conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(conversationId); return convertConversationMemorySnapshot(conversationMemorySnapshot); }
private void setConversationState(String conversationId, ConversationState conversationState) { conversationMemoryStore.setConversationState(conversationId, conversationState); cacheConversationState(conversationId, conversationState); }
@Override public void deleteConversationLog(String conversationId, Boolean deletePermanently) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceNotFoundException { RuntimeUtilities.checkNotNull(conversationId, "conversationId"); if (deletePermanently) { conversationMemoryStore.deleteConversationMemorySnapshot(conversationId); log.info(String.format("Conversation has been permanently deleted (conversationId=%s)", conversationId)); } // DocumentDescriptorInterceptor will mark the DocumentDescriptor of this resource as deleted, // regardless of whether it has been permanently deleted or not }
private String storeConversationMemory(IConversationMemory conversationMemory, Deployment.Environment environment) throws ResourceStoreException { var memorySnapshot = convertConversationMemory(conversationMemory); memorySnapshot.setEnvironment(environment); return conversationMemoryStore.storeConversationMemorySnapshot(memorySnapshot); }
@Override public Response undeployBot(Deployment.Environment environment, String botId, Integer version) { RuntimeUtilities.checkNotNull(environment, "environment"); RuntimeUtilities.checkNotNull(botId, "botId"); RuntimeUtilities.checkNotNull(version, "version"); try { Long activeConversationCount = conversationMemoryStore.getActiveConversationCount(botId, version); if (activeConversationCount > 0) { String message = "%s active (thus not ENDED) conversation(s) going on with this bot!" + "\nCheck GET /conversationstore/conversations/active/%s?botVersion=%s " + "to see active conversations and end conversations with " + "POST /conversationstore/conversations/end , " + "providing the list you receive with GET"; message = String.format(message, activeConversationCount, botId, version); return Response.status(Response.Status.CONFLICT).entity(message).type(MediaType.TEXT_PLAIN).build(); } undeploy(environment, botId, version); return Response.accepted().build(); } catch (Exception e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } }
@Override public ConversationState getConversationState(Deployment.Environment environment, String conversationId) { RuntimeUtilities.checkNotNull(environment, "environment"); RuntimeUtilities.checkNotNull(conversationId, "conversationId"); ConversationState conversationState = conversationStateCache.get(conversationId); if (conversationState == null) { conversationState = conversationMemoryStore.getConversationState(conversationId); cacheConversationState(conversationId, conversationState); } if (conversationState == null) { String message = "No conversation found! (conversationId=%s)"; message = String.format(message, conversationId); throw new NoLogWebApplicationException(new Throwable(message), Response.Status.NOT_FOUND); } return conversationState; }
@Override public ConversationMemorySnapshot readConversationLog(String conversationId) { RuntimeUtilities.checkNotNull(conversationId, "conversationId"); try { return conversationMemoryStore.loadConversationMemorySnapshot(conversationId); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getMessage(), e); throw new InternalServerErrorException(e); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(String.format("Conversation (%s) could not be found", conversationId)); } }
@Override public Response endActiveConversations(List<ConversationStatus> conversationStatuses) { try { for (ConversationStatus conversationStatus : conversationStatuses) { String conversationId = conversationStatus.getConversationId(); conversationMemoryStore.setConversationState( conversationId, ConversationState.ENDED); ConversationDescriptor conversationDescriptor = conversationDescriptorStore. readDescriptor(conversationId, 0); conversationDescriptor.setConversationState(ConversationState.ENDED); conversationDescriptorStore.setDescriptor(conversationId, 0, conversationDescriptor); log.info(String.format("conversation (%s) has been set to ENDED", conversationId)); } return Response.ok().build(); } catch (IResourceStore.ResourceStoreException | IResourceStore.ResourceNotFoundException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(); } } }
@Override public SimpleConversationMemorySnapshot readConversation(Deployment.Environment environment, String botId, String conversationId, Boolean returnDetailed, Boolean returnCurrentStepOnly, List<String> returningFields) { RuntimeUtilities.checkNotNull(environment, "environment"); RuntimeUtilities.checkNotNull(botId, "botId"); RuntimeUtilities.checkNotNull(conversationId, "conversationId"); try { var conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(conversationId); if (!botId.equals(conversationMemorySnapshot.getBotId())) { String message = "conversationId: '%s' does not belong to bot with conversationId: '%s'. " + "(provided botId='%s', botId in ConversationMemory='%s')"; message = String.format(message, conversationId, botId, botId, conversationMemorySnapshot.getBotId()); throw new IllegalAccessException(message); } return getSimpleConversationMemorySnapshot(conversationMemorySnapshot, returnDetailed, returnCurrentStepOnly, returningFields); } catch (ResourceStoreException | IllegalAccessException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } catch (ResourceNotFoundException e) { throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } }
private ConversationMemorySnapshot runTestCase(String botId, TestCase testCase) throws Exception { IRestBotEngine botEngine = restInterfaceFactory.get(IRestBotEngine.class, apiServerURI); Response ConversationResponse = botEngine.startConversation(Deployment.Environment.test, botId, "testCaseRunner"); URI conversationURI = ConversationResponse.getLocation(); String conversationURIPath = conversationURI.getPath(); String conversationId = conversationURIPath.substring(conversationURIPath.lastIndexOf("/") + 1); ConversationMemorySnapshot expected = testCase.getExpected(); List<ConversationMemorySnapshot.ConversationStepSnapshot> expectedConversationSteps = expected.getConversationSteps(); //we skip the first one, since the initial run has already been done at this point (at startConversation) for (int i = 1; i < expectedConversationSteps.size(); i++) { ConversationMemorySnapshot.ConversationStepSnapshot expectedConversationStep = expectedConversationSteps.get(i); String input = getFirstInput(expectedConversationStep); if (RuntimeUtilities.isNullOrEmpty(input)) { input = " "; } botEngine.say(Deployment.Environment.test, botId, conversationId, true, false, Collections.emptyList(), input, new MockAsyncResponse()); while (botEngine.getConversationState(Deployment.Environment.test, conversationId) == ConversationState.IN_PROGRESS) { Thread.sleep(1000); } } return conversationMemoryStore.loadConversationMemorySnapshot(conversationId); }
@Override public Response createTestCase(String id) { try { ConversationMemorySnapshot conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(id); TestCase testCase = new TestCase(); testCase.setBotId(conversationMemorySnapshot.getBotId()); testCase.setBotVersion(conversationMemorySnapshot.getBotVersion()); testCase.setTestCaseState(TestCaseState.SUCCESS); testCase.setActual(conversationMemorySnapshot); testCase.setExpected(conversationMemorySnapshot); IResourceStore.IResourceId resourceId = testCaseStore.create(testCase); URI createdUri = RestUtilities.createURI(resourceURI, resourceId.getId(), versionQueryParam, 0); return Response.created(createdUri).build(); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e); } catch (IResourceStore.ResourceNotFoundException e) { log.debug(e.getLocalizedMessage(), e); throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } }
RestUtilities.extractResourceId(conversationDescriptor.getResource()); try { memorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(resourceId.getId()); } catch (IResourceStore.ResourceNotFoundException e) { String message = "Resource referenced in descriptor does not exist (anymore) [%s]. ";