View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.chat;
17  
18  import java.util.List;
19  import java.util.Locale;
20  import java.util.Map;
21  
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.Logger;
24  import org.codelibs.fess.Constants;
25  import org.codelibs.fess.app.web.base.FessSearchAction;
26  import org.codelibs.fess.app.web.base.SearchForm;
27  import org.codelibs.fess.app.web.search.SearchAction;
28  import org.codelibs.fess.chat.ChatClient;
29  import org.codelibs.fess.chat.ChatSessionManager;
30  import org.codelibs.fess.entity.SearchRequestParams.SearchRequestType;
31  import org.codelibs.fess.util.ComponentUtil;
32  import org.codelibs.fess.util.RenderDataUtil;
33  import org.lastaflute.web.Execute;
34  import org.lastaflute.web.response.HtmlResponse;
35  
36  import jakarta.annotation.Resource;
37  
38  /**
39   * Action class for the RAG chat page.
40   *
41   * @author FessProject
42   */
43  public class ChatAction extends FessSearchAction {
44  
45      private static final Logger logger = LogManager.getLogger(ChatAction.class);
46  
47      /**
48       * Default constructor.
49       */
50      public ChatAction() {
51          // Default constructor
52      }
53  
54      /** The chat client for handling chat operations. */
55      @Resource
56      protected ChatClient chatClient;
57  
58      /** The session manager for managing chat sessions. */
59      @Resource
60      protected ChatSessionManager chatSessionManager;
61  
62      /**
63       * Displays the chat page.
64       *
65       * @return the HTML response for the chat page, or redirects to search if chat is not available
66       */
67      @Execute
68      public HtmlResponse index() {
69          if (logger.isDebugEnabled()) {
70              logger.debug("Chat page requested. Checking availability...");
71          }
72  
73          if (!chatClient.isAvailable()) {
74              if (logger.isInfoEnabled()) {
75                  logger.info("Redirecting to search page. RAG chat is not available.");
76              }
77              return redirect(SearchAction.class);
78          }
79  
80          if (logger.isDebugEnabled()) {
81              logger.debug("Displaying chat page. chatEnabled=true");
82          }
83  
84          return asHtml(virtualHost(path_Chat_ChatJsp)).useForm(SearchForm.class).renderWith(data -> {
85              RenderDataUtil.register(data, "chatEnabled", true);
86              RenderDataUtil.register(data, "chatPage", true);
87              // Label type items for filter UI
88              final List<Map<String, String>> labelTypeItems = labelTypeHelper.getLabelTypeItemList(SearchRequestType.SEARCH,
89                      request.getLocale() == null ? Locale.ROOT : request.getLocale());
90              RenderDataUtil.register(data, "labelTypeItems", labelTypeItems);
91              RenderDataUtil.register(data, "displayLabelTypeItems", labelTypeItems != null && !labelTypeItems.isEmpty());
92              // Facet query views for filter UI (file type, timestamp, size)
93              RenderDataUtil.register(data, "facetQueryViewList", ComponentUtil.getViewHelper().getFacetQueryViewList());
94          });
95      }
96  
97      /**
98       * Clears the chat session.
99       *
100      * @param form the chat form containing the session ID to clear
101      * @return the HTML response redirecting to the chat page
102      */
103     @Execute
104     public HtmlResponse clear(final ChatForm form) {
105         if (form.sessionId != null) {
106             if (logger.isDebugEnabled()) {
107                 logger.debug("Clearing chat session. sessionId={}", form.sessionId);
108             }
109             chatSessionManager.clearSession(form.sessionId, getUserId());
110         }
111         return redirect(getClass());
112     }
113 
114     /**
115      * Returns the user ID for the current session. If the user is authenticated, returns the username;
116      * otherwise, returns the guest user code.
117      *
118      * @return the user ID
119      */
120     protected String getUserId() {
121         final String username = systemHelper.getUsername();
122         if (!Constants.GUEST_USER.equals(username)) {
123             return username;
124         }
125         return userInfoHelper.getUserCode();
126     }
127 
128 }