isEnabled) { return new RedirectResponse('/app'); } /** @var UserEntity */ $user = $request->getAttribute(UserEntity::class); $id = $request->getAttribute('id'); if (!$id) { return new ViewResponse('/templates/app/chat.twig'); } $data = []; $conversation = null; $assistant = null; // First check if the ID belongs to a document $cmd = new ReadLibraryItemCommand($id); try { /** @var AbstractLibraryItemEntity */ $conversation = $this->dispatcher->dispatch($cmd); if ( !($conversation instanceof ConversationEntity) || !$this->ac->isGranted(Permission::LIBRARY_ITEM_READ, $user, $conversation) ) { return new RedirectResponse('/app/chat'); } $data['conversation'] = new ConversationResource( $conversation, ['messages'] ); $last = $conversation->getLastMessage(); if ($last->getAssistant()) { $data['assistant'] = new AssistantResource($last->getAssistant()); } $data['model'] = $last->getModel()->value; } catch (LibraryItemNotFoundException $th) { // Do nothing } if (!$conversation) { $cmd = new ReadAssistantCommand($id); try { /** @var AssistantEntity $assistant */ $assistant = $this->dispatcher->dispatch($cmd); $data['assistant'] = new AssistantResource($assistant); } catch (AssistantNotFoundException $th) { // Neither conversation nor assistant found return new RedirectResponse('/app/writer'); } } return new ViewResponse( '/templates/app/chat.twig', $data ); } }