UNPKG

conversation-engine

Version:

A powerful wrapper around the OpenAI API, providing additional features and making it easier to interact with AI models. Seamlessly chat with your AI assistant, include context strings, and manage conversation history.

51 lines 2.98 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { ModelName } from '../types.js'; import { summarizeChatMessages } from './summarizeChatMessages.js'; // The messageHistoryArray stores the message history across function calls const messageHistoryArray = []; // The previousSummary stores the last summarized message let previousSummary = { role: 'user', content: '' }; /** * Updates the message history with new messages and returns the combined chat history. * The chat history consists of a summary of older messages and the recent messages within the cutoff. * * @param {Message[]|undefined} newMessages - The new messages to add to the history. * @param {number} cutoff - Optional. The number of recent messages to include without summarization. Default is 6. * @param {ModelName} historySummarizationModel - Optional. The AI model to use for history summarization. Default is ModelName.GPT_3_5_TURBO. * @returns {Promise<Message[]>} - A Promise that resolves to the combined history (summary and recent messages). */ export function updateAndGetMessageHistory(newMessages, cutoff = 6, historySummarizationModel = ModelName.GPT_3_5_TURBO) { return __awaiter(this, void 0, void 0, function* () { let isNewMessage = false; // Update the message history with the new message, if provided if (newMessages) { messageHistoryArray.push(...newMessages); isNewMessage = true; } // Separate older messages (to be summarized) and recent messages (within the cutoff) const olderMessages = messageHistoryArray.slice(0, -cutoff); const recentMessages = messageHistoryArray.slice(-cutoff); let summary; let combinedHistory; // Summarize the older messages if there are any if (olderMessages.length > 0 && isNewMessage) { const newestOldMessage = olderMessages[olderMessages.length - 1]; summary = yield summarizeChatMessages([newestOldMessage], previousSummary, historySummarizationModel); previousSummary = summary; // Store the new summary combinedHistory = [summary, ...recentMessages]; } else { combinedHistory = [previousSummary, ...recentMessages]; } return combinedHistory; }); } //# sourceMappingURL=manageMessageHistory.js.map