#
Junjie
3 天以前 1b8a4677f362d234d834120deac4880d7ae89a50
src/main/webapp/views/ai/diagnosis.html
@@ -747,6 +747,38 @@
        }
      },
      methods: {
        stateHost: function() {
          try {
            if (window.top && window.top !== window) {
              return window.top;
            }
          } catch (e) {}
          return window;
        },
        getAssistantState: function() {
          var host = this.stateHost();
          if (!host.__WCS_AI_ASSISTANT_STATE__) {
            host.__WCS_AI_ASSISTANT_STATE__ = {};
          }
          return host.__WCS_AI_ASSISTANT_STATE__;
        },
        restoreAssistantState: function() {
          var state = this.getAssistantState();
          return {
            chatId: state.currentChatId || '',
            resetting: state.resetting === true
          };
        },
        persistAssistantState: function() {
          var state = this.getAssistantState();
          state.currentChatId = this.currentChatId || '';
          state.resetting = this.resetting === true;
        },
        clearAssistantState: function() {
          var state = this.getAssistantState();
          state.currentChatId = '';
          state.resetting = false;
        },
        authHeaders: function() {
          var token = localStorage.getItem('token');
          return token ? { token: token } : {};
@@ -850,6 +882,8 @@
          if (!chatId || this.streaming) return;
          this.currentChatId = chatId;
          this.runTokenUsage = null;
          this.resetting = false;
          this.persistAssistantState();
          this.switchChat();
        },
        switchChat: function() {
@@ -883,6 +917,7 @@
              self.messages = msgs;
              self.pendingText = '';
              self.resetting = false;
              self.persistAssistantState();
              self.$nextTick(function() { self.scrollToBottom(true); });
            })
            .catch(function() {
@@ -896,6 +931,7 @@
          this.resetting = true;
          this.runTokenUsage = null;
          this.clear();
          this.persistAssistantState();
        },
        deleteChat: function() {
          var self = this;
@@ -907,6 +943,7 @@
            .then(function(r) { return r.json(); })
            .then(function(ok) {
              if (ok === true) {
                self.clearAssistantState();
                self.currentChatId = '';
                self.clear();
                self.loadChats(false);
@@ -966,6 +1003,7 @@
          var url = baseUrl + '/ai/diagnose/askStream?prompt=' + encodeURIComponent(message);
          if (this.currentChatId) url += '&chatId=' + encodeURIComponent(this.currentChatId);
          if (this.resetting) url += '&reset=true';
          this.persistAssistantState();
          this.source = new EventSource(url);
          var self = this;
@@ -993,6 +1031,7 @@
          };
          this.userInput = '';
          this.resetting = false;
          this.persistAssistantState();
        },
        start: function() {
          if (this.streaming) return;
@@ -1087,9 +1126,23 @@
        }
      },
      mounted: function() {
        this.loadChats(false);
        var restored = this.restoreAssistantState();
        if (restored.chatId) {
          this.currentChatId = restored.chatId;
          this.resetting = restored.resetting;
          if (this.resetting) {
            this.clear();
          } else {
            this.switchChat();
          }
          this.loadChats(true);
          return;
        }
        this.newChat();
        this.loadChats(true);
      },
      beforeDestroy: function() {
        this.persistAssistantState();
        this.stop(true);
      }
    });