interstellar_ai/app/backend/ChatHistory.ts

31 lines
777 B
TypeScript
Raw Normal View History

2024-10-07 16:41:31 +02:00
type Message = {
role: string;
content:string
}
type Chat = {
2024-10-01 10:50:19 +02:00
name: string;
2024-10-07 16:41:31 +02:00
messages: Message[];
2024-10-01 10:50:19 +02:00
timestamp: number;
};
2024-10-07 16:41:31 +02:00
export let chatHistory: Chat[] = [];
2024-10-01 10:50:19 +02:00
2024-10-07 16:41:31 +02:00
export function addMessageToHistory(index: number, chat: Chat): void {
if (index >= 0 && index < chatHistory.length) {
chatHistory[index] = chat;
chatHistory.sort((a, b) => b.timestamp - a.timestamp)
}
2024-10-01 10:50:19 +02:00
}
2024-10-07 16:41:31 +02:00
export function removeMessageFromHistory(timestamp: number): void {
2024-10-01 10:50:19 +02:00
const index = chatHistory.findIndex((msg) => msg.timestamp === timestamp);
if (index > -1) {
chatHistory.splice(index, 1);
console.log(`Removed message with timestamp: ${timestamp}`);
} else {
console.log(`Message not found with timestamp: ${timestamp}`);
}
}