@aksolab/recall
Version:
A memory management package for AI SDK memory functionality
57 lines (56 loc) • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeText = summarizeText;
exports.summarizeMessages = summarizeMessages;
const ai_1 = require("ai");
const openai_1 = require("@ai-sdk/openai");
function extractMessageContent(content) {
if (typeof content === 'string') {
return content;
}
if (Array.isArray(content)) {
return content
.map(part => {
if (part.type === 'text') {
return part.text;
}
// Ignore tool calls and tool results
return '';
})
.filter(text => text.length > 0)
.join(' ');
}
return '';
}
async function summarizeText(context, openaiApiKey) {
const { text } = await (0, ai_1.generateText)({
model: (0, openai_1.openai)('gpt-4o-mini'),
system: 'You are a professional summarizer. You write clear, concise, and accurate summaries.',
prompt: `Summarize the main themes in these retrieved docs in a clear and concise way: ${context}`,
});
return text;
}
async function summarizeMessages(messages, openaiApiKey, previousSummary) {
// Filter out tool messages and extract actual content from complex message types
const filteredMessages = messages
.filter(msg => msg.role !== "tool")
.map(msg => ({
role: msg.role,
content: extractMessageContent(msg.content)
}))
.filter(msg => msg.content.length > 0); // Remove empty messages
const messagesText = filteredMessages
.map(msg => `${msg.role}: ${msg.content}`)
.join("\n");
const context = previousSummary
? `Previous Summary: ${previousSummary}\n\nNew Messages:\n${messagesText}`
: `Messages:\n${messagesText}`;
const { text } = await (0, ai_1.generateText)({
model: (0, openai_1.openai)('gpt-4o-mini'),
system: 'You are a professional conversation summarizer. ' +
'You write clear, concise, and accurate summaries that capture the key points and context of conversations. ' +
'Focus on the main themes, important details, and any decisions or actions discussed.',
prompt: `Given the following conversation, provide a comprehensive summary that captures the main themes and important points:\n\n${context}`,
});
return text;
}