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.
 
 
 

209 lines
7.0 KiB

// @/store/emotion.js
import { defineStore } from 'pinia';
export const useEmotionStore = defineStore('emotion', {
state: () => ({
history: [] as HistoryItem[], // 历史记录数组
stockList: [] as StockData[], // 当前显示的股票列表
activeStockIndex: 0, // 当前激活的股票索引
maxStocks: 10, // 最大股票数量限制
conversations: [] as ConversationMessage[], // 对话消息数组
maxConversations: 100, // 最大对话数量限制
}),
persist: {
key: 'emotion-store',
storage: sessionStorage,
paths: ['history', 'stockList', 'activeStockIndex', 'conversations']
},
getters: {
// 获取当前激活的股票
activeStock: (state) => {
return state.stockList[state.activeStockIndex] || null;
},
// 获取股票数量
stockCount: (state) => state.stockList.length,
// 是否达到最大股票数量
isMaxStocks: (state) => state.stockList.length >= state.maxStocks,
// 获取对话数量
conversationCount: (state) => state.conversations.length,
// 是否达到最大对话数量
isMaxConversations: (state) => state.conversations.length >= state.maxConversations,
// 获取最近的对话消息
recentConversations: (state) => {
return state.conversations.slice(-20); // 返回最近20条对话
},
},
actions: {
// 添加历史记录
addHistory(item: HistoryItem) {
this.history.unshift(item); // 新记录添加到数组顶部,保持顺序
},
// 清空历史记录
clearHistory() {
this.history = [];
},
// 添加新股票
addStock(stockData: StockData) {
// 检查是否已存在相同的股票
const existingIndex = this.stockList.findIndex(
stock => stock.stockInfo.code === stockData.stockInfo.code &&
stock.stockInfo.market === stockData.stockInfo.market
);
if (existingIndex !== -1) {
// 如果股票已存在,更新数据并切换到该股票
this.stockList[existingIndex] = stockData;
this.activeStockIndex = existingIndex;
} else {
// 如果达到最大数量,移除最旧的股票
if (this.stockList.length >= this.maxStocks) {
this.stockList.shift();
if (this.activeStockIndex > 0) {
this.activeStockIndex--;
}
}
// 添加新股票并设为当前激活
this.stockList.push(stockData);
this.activeStockIndex = this.stockList.length - 1;
}
// 同时添加到历史记录
// this.addHistory({
// queryText: stockData.queryText,
// stockInfo: stockData.stockInfo,
// apiData: stockData.apiData,
// conclusionData: stockData.conclusionData,
// timestamp: stockData.timestamp
// });
},
// 切换股票
switchStock(index: number) {
if (index >= 0 && index < this.stockList.length) {
this.activeStockIndex = index;
}
},
// 移除股票
removeStock(index: number) {
if (index >= 0 && index < this.stockList.length) {
this.stockList.splice(index, 1);
// 调整激活索引
if (this.activeStockIndex >= this.stockList.length) {
this.activeStockIndex = Math.max(0, this.stockList.length - 1);
} else if (this.activeStockIndex > index) {
this.activeStockIndex--;
}
}
},
// 更新股票数据
updateStockData(index: number, apiData: any) {
if (index >= 0 && index < this.stockList.length) {
this.stockList[index].apiData = apiData;
this.stockList[index].timestamp = new Date().toISOString();
}
},
// 清空所有股票
clearAllStocks() {
this.stockList = [];
this.activeStockIndex = 0;
},
// 从历史记录恢复股票
restoreFromHistory(historyItem: HistoryItem) {
const stockData: StockData = {
queryText: historyItem.queryText,
stockInfo: historyItem.stockInfo,
apiData: historyItem.apiData,
conclusionData: historyItem.conclusionData,
timestamp: historyItem.timestamp
};
this.addStock(stockData);
},
// 更新股票的结论数据
updateStockConclusion(index: number, conclusionData: string) {
if (index >= 0 && index < this.stockList.length) {
this.stockList[index].conclusionData = conclusionData;
this.stockList[index].timestamp = new Date().toISOString();
}
},
// 更新当前激活股票的结论数据
updateActiveStockConclusion(conclusionData: string) {
this.updateStockConclusion(this.activeStockIndex, conclusionData);
},
// 添加对话消息
addConversation(message: ConversationMessage) {
// 如果达到最大数量,移除最旧的消息
if (this.conversations.length >= this.maxConversations) {
this.conversations.shift();
}
// 添加新消息到数组末尾
this.conversations.push({
...message,
timestamp: message.timestamp || new Date().toISOString()
});
},
// 批量添加对话消息
addConversations(messages: ConversationMessage[]) {
messages.forEach(message => {
this.addConversation(message);
});
},
// 清空对话记录
clearConversations() {
this.conversations = [];
},
// 获取对话记录
getConversations() {
return this.conversations;
},
// 删除指定索引的对话消息
removeConversation(index: number) {
if (index >= 0 && index < this.conversations.length) {
this.conversations.splice(index, 1);
}
},
// 更新对话消息
updateConversation(index: number, updatedMessage: Partial<ConversationMessage>) {
if (index >= 0 && index < this.conversations.length) {
this.conversations[index] = {
...this.conversations[index],
...updatedMessage,
timestamp: new Date().toISOString()
};
}
},
},
});
// 定义历史记录项的类型
interface HistoryItem {
queryText: string; // 用户输入的查询文本(如股票名称/代码)
stockInfo: {
name: string; // 股票名称
code: string; // 股票代码
market: string; // 市场(如 usa/cn/hk)
};
apiData: any; // 接口返回的原始数据(包含图表数据)
conclusionData?: string; // 第二个工作流接口返回的结论数据
timestamp: string; // 记录时间
}
// 定义股票数据的类型
interface StockData {
queryText: string; // 用户输入的查询文本
stockInfo: {
name: string; // 股票名称
code: string; // 股票代码
market: string; // 市场
};
apiData: any; // API返回的完整数据
conclusionData?: string; // 第二个工作流接口返回的结论数据
timestamp: string; // 数据获取时间
}
// 定义对话消息的类型
interface ConversationMessage {
sender: 'user' | 'ai'; // 消息发送者类型
text: string; // 消息内容
timestamp?: string; // 消息时间戳
id?: string; // 消息唯一标识(可选)
type?: string; // 消息类型(可选,如 'text', 'error', 'system' 等)
}