|
| 1 | +/** |
| 2 | + * Prompt Session Utility for ProcessMaker AI Microservice |
| 3 | + * Provides functionality to manage prompt session IDs and nonces |
| 4 | + */ |
| 5 | + |
| 6 | +export default { |
| 7 | + data() { |
| 8 | + return { |
| 9 | + promptSessionId: localStorage.getItem("promptSessionId") || "", |
| 10 | + currentNonce: "", |
| 11 | + }; |
| 12 | + }, |
| 13 | + |
| 14 | + methods: { |
| 15 | + /** |
| 16 | + * Generate a new nonce and store it in localStorage |
| 17 | + * @returns {string} The generated nonce |
| 18 | + */ |
| 19 | + getNonce() { |
| 20 | + const max = 999999999999999; |
| 21 | + const nonce = Math.floor(Math.random() * max); |
| 22 | + this.currentNonce = nonce.toString(); |
| 23 | + localStorage.setItem("currentNonce", this.currentNonce); |
| 24 | + return this.currentNonce; |
| 25 | + }, |
| 26 | + |
| 27 | + /** |
| 28 | + * Get the current nonce from localStorage |
| 29 | + * @returns {string|null} The current nonce or null if not found |
| 30 | + */ |
| 31 | + getCurrentNonce() { |
| 32 | + return localStorage.getItem("currentNonce"); |
| 33 | + }, |
| 34 | + |
| 35 | + /** |
| 36 | + * Get or create a prompt session ID |
| 37 | + * @returns {Promise<string>} The prompt session ID |
| 38 | + */ |
| 39 | + async getPromptSession() { |
| 40 | + const url = "/package-ai/getPromptSessionHistory"; |
| 41 | + let params = { |
| 42 | + server: window.location.host, |
| 43 | + }; |
| 44 | + |
| 45 | + // Reset session ID if it starts with "ss" (invalid format) |
| 46 | + if (this.promptSessionId && this.promptSessionId.startsWith("ss")) { |
| 47 | + this.promptSessionId = ""; |
| 48 | + } |
| 49 | + |
| 50 | + // Use existing session ID if available |
| 51 | + if (this.promptSessionId && this.promptSessionId !== null && this.promptSessionId !== "") { |
| 52 | + params = { promptSessionId: this.promptSessionId }; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + const response = await ProcessMaker.apiClient.post(url, params); |
| 57 | + this.promptSessionId = response.data.promptSessionId; |
| 58 | + localStorage.setItem("promptSessionId", this.promptSessionId); |
| 59 | + return this.promptSessionId; |
| 60 | + } catch (error) { |
| 61 | + const errorMsg = error.response?.data?.message || error.message; |
| 62 | + |
| 63 | + if (error.response?.status === 404) { |
| 64 | + // Session not found, clear it and try again |
| 65 | + localStorage.removeItem("promptSessionId"); |
| 66 | + this.promptSessionId = ""; |
| 67 | + return this.getPromptSession(); |
| 68 | + } |
| 69 | + // eslint-disable-next-line no-console |
| 70 | + console.error("Error getting prompt session:", errorMsg); |
| 71 | + throw error; |
| 72 | + } |
| 73 | + }, |
| 74 | + |
| 75 | + /** |
| 76 | + * Clear the current prompt session |
| 77 | + */ |
| 78 | + clearPromptSession() { |
| 79 | + this.promptSessionId = ""; |
| 80 | + localStorage.removeItem("promptSessionId"); |
| 81 | + localStorage.removeItem("currentNonce"); |
| 82 | + }, |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if a prompt session exists |
| 86 | + * @returns {boolean} True if session exists |
| 87 | + */ |
| 88 | + hasPromptSession() { |
| 89 | + return this.promptSessionId && this.promptSessionId !== ""; |
| 90 | + }, |
| 91 | + }, |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Standalone utility functions that can be used without Vue component |
| 96 | + */ |
| 97 | +export const promptSessionUtils = { |
| 98 | + /** |
| 99 | + * Get prompt session ID from localStorage |
| 100 | + * @returns {string} The prompt session ID |
| 101 | + */ |
| 102 | + getPromptSessionId() { |
| 103 | + return localStorage.getItem("promptSessionId") || ""; |
| 104 | + }, |
| 105 | + |
| 106 | + /** |
| 107 | + * Set prompt session ID in localStorage |
| 108 | + * @param {string} sessionId - The session ID to store |
| 109 | + */ |
| 110 | + setPromptSessionId(sessionId) { |
| 111 | + localStorage.setItem("promptSessionId", sessionId); |
| 112 | + }, |
| 113 | + |
| 114 | + /** |
| 115 | + * Clear prompt session from localStorage |
| 116 | + */ |
| 117 | + clearPromptSession() { |
| 118 | + localStorage.removeItem("promptSessionId"); |
| 119 | + localStorage.removeItem("currentNonce"); |
| 120 | + }, |
| 121 | + |
| 122 | + /** |
| 123 | + * Generate a new nonce |
| 124 | + * @returns {string} The generated nonce |
| 125 | + */ |
| 126 | + generateNonce() { |
| 127 | + const max = 999999999999999; |
| 128 | + const nonce = Math.floor(Math.random() * max); |
| 129 | + const nonceString = nonce.toString(); |
| 130 | + localStorage.setItem("currentNonce", nonceString); |
| 131 | + return nonceString; |
| 132 | + }, |
| 133 | + |
| 134 | + /** |
| 135 | + * Get current nonce from localStorage |
| 136 | + * @returns {string|null} The current nonce |
| 137 | + */ |
| 138 | + getCurrentNonce() { |
| 139 | + return localStorage.getItem("currentNonce"); |
| 140 | + }, |
| 141 | + |
| 142 | + /** |
| 143 | + * Make API call to get prompt session (standalone version) |
| 144 | + * @returns {Promise<string>} The prompt session ID |
| 145 | + */ |
| 146 | + async getPromptSession() { |
| 147 | + const url = "/package-ai/getPromptSessionHistory"; |
| 148 | + let params = { |
| 149 | + server: window.location.host, |
| 150 | + }; |
| 151 | + |
| 152 | + const currentSessionId = this.getPromptSessionId(); |
| 153 | + |
| 154 | + // Reset session ID if it starts with "ss" (invalid format) |
| 155 | + if (currentSessionId && currentSessionId.startsWith("ss")) { |
| 156 | + this.clearPromptSession(); |
| 157 | + } |
| 158 | + |
| 159 | + // Use existing session ID if available |
| 160 | + if (currentSessionId && currentSessionId !== "") { |
| 161 | + params = { promptSessionId: currentSessionId }; |
| 162 | + } |
| 163 | + |
| 164 | + try { |
| 165 | + const response = await ProcessMaker.apiClient.post(url, params); |
| 166 | + const sessionId = response.data.promptSessionId; |
| 167 | + this.setPromptSessionId(sessionId); |
| 168 | + return sessionId; |
| 169 | + } catch (error) { |
| 170 | + const errorMsg = error.response?.data?.message || error.message; |
| 171 | + |
| 172 | + if (error.response?.status === 404) { |
| 173 | + // Session not found, clear it and try again |
| 174 | + this.clearPromptSession(); |
| 175 | + return this.getPromptSession(); |
| 176 | + } |
| 177 | + // eslint-disable-next-line no-console |
| 178 | + console.error("Error getting prompt session:", errorMsg); |
| 179 | + throw error; |
| 180 | + } |
| 181 | + }, |
| 182 | +}; |
0 commit comments