mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-13 23:16:55 +00:00
Add user chat history memory store
This commit is contained in:
@@ -11,6 +11,8 @@ const client = new Client({
|
||||
partials: [Partials.Message, Partials.Channel],
|
||||
});
|
||||
|
||||
const chatHistory = new Map();
|
||||
|
||||
async function initializeAgent() {
|
||||
try {
|
||||
const llm = new ChatOpenAI({
|
||||
@@ -68,13 +70,22 @@ client.on(Events.MessageCreate, async (message) => {
|
||||
|
||||
const { agent, config } = await initializeAgent();
|
||||
|
||||
const stream = await agent.stream({ messages: [new HumanMessage(message.content)] }, config);
|
||||
const userId = message.author.id;
|
||||
if (!chatHistory.has(userId)) {
|
||||
chatHistory.set(userId, []);
|
||||
}
|
||||
const userChatHistory = chatHistory.get(userId);
|
||||
userChatHistory.push(new HumanMessage(message.content));
|
||||
|
||||
const stream = await agent.stream({ messages: userChatHistory }, config);
|
||||
|
||||
const replyIfNotEmpty = async (content: string) => content.trim() !== '' && message.reply(content);
|
||||
|
||||
for await (const chunk of stream) {
|
||||
if ('agent' in chunk) {
|
||||
await replyIfNotEmpty(chunk.agent.messages[0].content);
|
||||
const agentMessage = chunk.agent.messages[0].content;
|
||||
await replyIfNotEmpty(agentMessage);
|
||||
userChatHistory.push(new HumanMessage(agentMessage));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user