You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.0 KiB

  1. // @/store/emotion.js
  2. import { defineStore } from 'pinia';
  3. export const useEmotionStore = defineStore('emotion', {
  4. state: () => ({
  5. history: [] as HistoryItem[], // 历史记录数组
  6. stockList: [] as StockData[], // 当前显示的股票列表
  7. activeStockIndex: 0, // 当前激活的股票索引
  8. maxStocks: 10, // 最大股票数量限制
  9. conversations: [] as ConversationMessage[], // 对话消息数组
  10. maxConversations: 100, // 最大对话数量限制
  11. }),
  12. persist: {
  13. key: 'emotion-store',
  14. storage: sessionStorage,
  15. paths: ['history', 'stockList', 'activeStockIndex', 'conversations']
  16. },
  17. getters: {
  18. // 获取当前激活的股票
  19. activeStock: (state) => {
  20. return state.stockList[state.activeStockIndex] || null;
  21. },
  22. // 获取股票数量
  23. stockCount: (state) => state.stockList.length,
  24. // 是否达到最大股票数量
  25. isMaxStocks: (state) => state.stockList.length >= state.maxStocks,
  26. // 获取对话数量
  27. conversationCount: (state) => state.conversations.length,
  28. // 是否达到最大对话数量
  29. isMaxConversations: (state) => state.conversations.length >= state.maxConversations,
  30. // 获取最近的对话消息
  31. recentConversations: (state) => {
  32. return state.conversations.slice(-20); // 返回最近20条对话
  33. },
  34. },
  35. actions: {
  36. // 添加历史记录
  37. addHistory(item: HistoryItem) {
  38. this.history.unshift(item); // 新记录添加到数组顶部,保持顺序
  39. },
  40. // 清空历史记录
  41. clearHistory() {
  42. this.history = [];
  43. },
  44. // 添加新股票
  45. addStock(stockData: StockData) {
  46. // 检查是否已存在相同的股票
  47. const existingIndex = this.stockList.findIndex(
  48. stock => stock.stockInfo.code === stockData.stockInfo.code &&
  49. stock.stockInfo.market === stockData.stockInfo.market
  50. );
  51. if (existingIndex !== -1) {
  52. // 如果股票已存在,更新数据并切换到该股票
  53. this.stockList[existingIndex] = stockData;
  54. this.activeStockIndex = existingIndex;
  55. } else {
  56. // 如果达到最大数量,移除最旧的股票
  57. if (this.stockList.length >= this.maxStocks) {
  58. this.stockList.shift();
  59. if (this.activeStockIndex > 0) {
  60. this.activeStockIndex--;
  61. }
  62. }
  63. // 添加新股票并设为当前激活
  64. this.stockList.push(stockData);
  65. this.activeStockIndex = this.stockList.length - 1;
  66. }
  67. // 同时添加到历史记录
  68. // this.addHistory({
  69. // queryText: stockData.queryText,
  70. // stockInfo: stockData.stockInfo,
  71. // apiData: stockData.apiData,
  72. // conclusionData: stockData.conclusionData,
  73. // timestamp: stockData.timestamp
  74. // });
  75. },
  76. // 切换股票
  77. switchStock(index: number) {
  78. if (index >= 0 && index < this.stockList.length) {
  79. this.activeStockIndex = index;
  80. }
  81. },
  82. // 移除股票
  83. removeStock(index: number) {
  84. if (index >= 0 && index < this.stockList.length) {
  85. this.stockList.splice(index, 1);
  86. // 调整激活索引
  87. if (this.activeStockIndex >= this.stockList.length) {
  88. this.activeStockIndex = Math.max(0, this.stockList.length - 1);
  89. } else if (this.activeStockIndex > index) {
  90. this.activeStockIndex--;
  91. }
  92. }
  93. },
  94. // 更新股票数据
  95. updateStockData(index: number, apiData: any) {
  96. if (index >= 0 && index < this.stockList.length) {
  97. this.stockList[index].apiData = apiData;
  98. this.stockList[index].timestamp = new Date().toISOString();
  99. }
  100. },
  101. // 清空所有股票
  102. clearAllStocks() {
  103. this.stockList = [];
  104. this.activeStockIndex = 0;
  105. },
  106. // 从历史记录恢复股票
  107. restoreFromHistory(historyItem: HistoryItem) {
  108. const stockData: StockData = {
  109. queryText: historyItem.queryText,
  110. stockInfo: historyItem.stockInfo,
  111. apiData: historyItem.apiData,
  112. conclusionData: historyItem.conclusionData,
  113. timestamp: historyItem.timestamp
  114. };
  115. this.addStock(stockData);
  116. },
  117. // 更新股票的结论数据
  118. updateStockConclusion(index: number, conclusionData: string) {
  119. if (index >= 0 && index < this.stockList.length) {
  120. this.stockList[index].conclusionData = conclusionData;
  121. this.stockList[index].timestamp = new Date().toISOString();
  122. }
  123. },
  124. // 更新当前激活股票的结论数据
  125. updateActiveStockConclusion(conclusionData: string) {
  126. this.updateStockConclusion(this.activeStockIndex, conclusionData);
  127. },
  128. // 添加对话消息
  129. addConversation(message: ConversationMessage) {
  130. // 如果达到最大数量,移除最旧的消息
  131. if (this.conversations.length >= this.maxConversations) {
  132. this.conversations.shift();
  133. }
  134. // 添加新消息到数组末尾
  135. this.conversations.push({
  136. ...message,
  137. timestamp: message.timestamp || new Date().toISOString()
  138. });
  139. },
  140. // 批量添加对话消息
  141. addConversations(messages: ConversationMessage[]) {
  142. messages.forEach(message => {
  143. this.addConversation(message);
  144. });
  145. },
  146. // 清空对话记录
  147. clearConversations() {
  148. this.conversations = [];
  149. },
  150. // 获取对话记录
  151. getConversations() {
  152. return this.conversations;
  153. },
  154. // 删除指定索引的对话消息
  155. removeConversation(index: number) {
  156. if (index >= 0 && index < this.conversations.length) {
  157. this.conversations.splice(index, 1);
  158. }
  159. },
  160. // 更新对话消息
  161. updateConversation(index: number, updatedMessage: Partial<ConversationMessage>) {
  162. if (index >= 0 && index < this.conversations.length) {
  163. this.conversations[index] = {
  164. ...this.conversations[index],
  165. ...updatedMessage,
  166. timestamp: new Date().toISOString()
  167. };
  168. }
  169. },
  170. },
  171. });
  172. // 定义历史记录项的类型
  173. interface HistoryItem {
  174. queryText: string; // 用户输入的查询文本(如股票名称/代码)
  175. stockInfo: {
  176. name: string; // 股票名称
  177. code: string; // 股票代码
  178. market: string; // 市场(如 usa/cn/hk)
  179. };
  180. apiData: any; // 接口返回的原始数据(包含图表数据)
  181. conclusionData?: string; // 第二个工作流接口返回的结论数据
  182. timestamp: string; // 记录时间
  183. }
  184. // 定义股票数据的类型
  185. interface StockData {
  186. queryText: string; // 用户输入的查询文本
  187. stockInfo: {
  188. name: string; // 股票名称
  189. code: string; // 股票代码
  190. market: string; // 市场
  191. };
  192. apiData: any; // API返回的完整数据
  193. conclusionData?: string; // 第二个工作流接口返回的结论数据
  194. timestamp: string; // 数据获取时间
  195. }
  196. // 定义对话消息的类型
  197. interface ConversationMessage {
  198. sender: 'user' | 'ai'; // 消息发送者类型
  199. text: string; // 消息内容
  200. timestamp?: string; // 消息时间戳
  201. id?: string; // 消息唯一标识(可选)
  202. type?: string; // 消息类型(可选,如 'text', 'error', 'system' 等)
  203. }