| | |
| | | } |
| | | }, |
| | | 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 } : {}; |
| | |
| | | if (!chatId || this.streaming) return; |
| | | this.currentChatId = chatId; |
| | | this.runTokenUsage = null; |
| | | this.resetting = false; |
| | | this.persistAssistantState(); |
| | | this.switchChat(); |
| | | }, |
| | | switchChat: function() { |
| | |
| | | self.messages = msgs; |
| | | self.pendingText = ''; |
| | | self.resetting = false; |
| | | self.persistAssistantState(); |
| | | self.$nextTick(function() { self.scrollToBottom(true); }); |
| | | }) |
| | | .catch(function() { |
| | |
| | | this.resetting = true; |
| | | this.runTokenUsage = null; |
| | | this.clear(); |
| | | this.persistAssistantState(); |
| | | }, |
| | | deleteChat: function() { |
| | | var self = this; |
| | |
| | | .then(function(r) { return r.json(); }) |
| | | .then(function(ok) { |
| | | if (ok === true) { |
| | | self.clearAssistantState(); |
| | | self.currentChatId = ''; |
| | | self.clear(); |
| | | self.loadChats(false); |
| | |
| | | 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; |
| | |
| | | }; |
| | | this.userInput = ''; |
| | | this.resetting = false; |
| | | this.persistAssistantState(); |
| | | }, |
| | | start: function() { |
| | | if (this.streaming) return; |
| | |
| | | } |
| | | }, |
| | | 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); |
| | | } |
| | | }); |