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.

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