configStorage.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import { STORAGE_KEYS, DEFAULT_CONFIG } from '../../constants/playground.constants';
  16. const MESSAGES_STORAGE_KEY = 'playground_messages';
  17. /**
  18. * 保存配置到 localStorage
  19. * @param {Object} config - 要保存的配置对象
  20. */
  21. export const saveConfig = (config) => {
  22. try {
  23. const configToSave = {
  24. ...config,
  25. timestamp: new Date().toISOString(),
  26. };
  27. localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(configToSave));
  28. } catch (error) {
  29. console.error('保存配置失败:', error);
  30. }
  31. };
  32. /**
  33. * 保存消息到 localStorage
  34. * @param {Array} messages - 要保存的消息数组
  35. */
  36. export const saveMessages = (messages) => {
  37. try {
  38. const messagesToSave = {
  39. messages,
  40. timestamp: new Date().toISOString(),
  41. };
  42. localStorage.setItem(STORAGE_KEYS.MESSAGES, JSON.stringify(messagesToSave));
  43. } catch (error) {
  44. console.error('保存消息失败:', error);
  45. }
  46. };
  47. /**
  48. * 从 localStorage 加载配置
  49. * @returns {Object} 配置对象,如果不存在则返回默认配置
  50. */
  51. export const loadConfig = () => {
  52. try {
  53. const savedConfig = localStorage.getItem(STORAGE_KEYS.CONFIG);
  54. if (savedConfig) {
  55. const parsedConfig = JSON.parse(savedConfig);
  56. const mergedConfig = {
  57. inputs: {
  58. ...DEFAULT_CONFIG.inputs,
  59. ...parsedConfig.inputs,
  60. },
  61. parameterEnabled: {
  62. ...DEFAULT_CONFIG.parameterEnabled,
  63. ...parsedConfig.parameterEnabled,
  64. },
  65. showDebugPanel: parsedConfig.showDebugPanel || DEFAULT_CONFIG.showDebugPanel,
  66. customRequestMode: parsedConfig.customRequestMode || DEFAULT_CONFIG.customRequestMode,
  67. customRequestBody: parsedConfig.customRequestBody || DEFAULT_CONFIG.customRequestBody,
  68. };
  69. return mergedConfig;
  70. }
  71. } catch (error) {
  72. console.error('加载配置失败:', error);
  73. }
  74. return DEFAULT_CONFIG;
  75. };
  76. /**
  77. * 从 localStorage 加载消息
  78. * @returns {Array} 消息数组,如果不存在则返回 null
  79. */
  80. export const loadMessages = () => {
  81. try {
  82. const savedMessages = localStorage.getItem(STORAGE_KEYS.MESSAGES);
  83. if (savedMessages) {
  84. const parsedMessages = JSON.parse(savedMessages);
  85. return parsedMessages.messages || null;
  86. }
  87. } catch (error) {
  88. console.error('加载消息失败:', error);
  89. }
  90. return null;
  91. };
  92. /**
  93. * 清除保存的配置
  94. */
  95. export const clearConfig = () => {
  96. try {
  97. localStorage.removeItem(STORAGE_KEYS.CONFIG);
  98. localStorage.removeItem(STORAGE_KEYS.MESSAGES); // 同时清除消息
  99. } catch (error) {
  100. console.error('清除配置失败:', error);
  101. }
  102. };
  103. /**
  104. * 清除保存的消息
  105. */
  106. export const clearMessages = () => {
  107. try {
  108. localStorage.removeItem(STORAGE_KEYS.MESSAGES);
  109. } catch (error) {
  110. console.error('清除消息失败:', error);
  111. }
  112. };
  113. /**
  114. * 检查是否有保存的配置
  115. * @returns {boolean} 是否存在保存的配置
  116. */
  117. export const hasStoredConfig = () => {
  118. try {
  119. return localStorage.getItem(STORAGE_KEYS.CONFIG) !== null;
  120. } catch (error) {
  121. console.error('检查配置失败:', error);
  122. return false;
  123. }
  124. };
  125. /**
  126. * 获取配置的最后保存时间
  127. * @returns {string|null} 最后保存时间的 ISO 字符串
  128. */
  129. export const getConfigTimestamp = () => {
  130. try {
  131. const savedConfig = localStorage.getItem(STORAGE_KEYS.CONFIG);
  132. if (savedConfig) {
  133. const parsedConfig = JSON.parse(savedConfig);
  134. return parsedConfig.timestamp || null;
  135. }
  136. } catch (error) {
  137. console.error('获取配置时间戳失败:', error);
  138. }
  139. return null;
  140. };
  141. /**
  142. * 导出配置为 JSON 文件(包含消息)
  143. * @param {Object} config - 要导出的配置
  144. * @param {Array} messages - 要导出的消息
  145. */
  146. export const exportConfig = (config, messages = null) => {
  147. try {
  148. const configToExport = {
  149. ...config,
  150. messages: messages || loadMessages(), // 包含消息数据
  151. exportTime: new Date().toISOString(),
  152. version: '1.0',
  153. };
  154. const dataStr = JSON.stringify(configToExport, null, 2);
  155. const dataBlob = new Blob([dataStr], { type: 'application/json' });
  156. const link = document.createElement('a');
  157. link.href = URL.createObjectURL(dataBlob);
  158. link.download = `playground-config-${new Date().toISOString().split('T')[0]}.json`;
  159. link.click();
  160. URL.revokeObjectURL(link.href);
  161. } catch (error) {
  162. console.error('导出配置失败:', error);
  163. }
  164. };
  165. /**
  166. * 从文件导入配置(包含消息)
  167. * @param {File} file - 包含配置的 JSON 文件
  168. * @returns {Promise<Object>} 导入的配置对象
  169. */
  170. export const importConfig = (file) => {
  171. return new Promise((resolve, reject) => {
  172. try {
  173. const reader = new FileReader();
  174. reader.onload = (e) => {
  175. try {
  176. const importedConfig = JSON.parse(e.target.result);
  177. if (importedConfig.inputs && importedConfig.parameterEnabled) {
  178. // 如果导入的配置包含消息,也一起导入
  179. if (importedConfig.messages && Array.isArray(importedConfig.messages)) {
  180. saveMessages(importedConfig.messages);
  181. }
  182. resolve(importedConfig);
  183. } else {
  184. reject(new Error('配置文件格式无效'));
  185. }
  186. } catch (parseError) {
  187. reject(new Error('解析配置文件失败: ' + parseError.message));
  188. }
  189. };
  190. reader.onerror = () => reject(new Error('读取文件失败'));
  191. reader.readAsText(file);
  192. } catch (error) {
  193. reject(new Error('导入配置失败: ' + error.message));
  194. }
  195. });
  196. };