summarizeConversation()
The standalone summarizeConversation() function summarizes a conversation in one shot. It distills the messages you pass in with the same Observer plumbing that powers Observational Memory: without Observational Memory attached to an agent, and without reading from or writing to storage.
Nothing is written back to memory. The summary and extracted values are returned to you (and to each extractor's onExtracted hook), so you decide where they go: for example your own database.
Use this when you already have the messages in hand and want explicit control over what gets summarized. For a stored thread identified by ID, use Memory.summarizeThread(), which loads the messages for you.
Usage exampleUsage example的直接链接
import { summarizeConversation } from '@mastra/memory'
const result = await summarizeConversation({
model: 'openai/gpt-5-mini',
messages,
instructions: 'Summarize this voicemail call for the business owner.',
})
ParametersParameters的直接链接
model:
{ model, maxRetries } entries to fall back to the next model when one fails.messages:
instructions?:
extract?:
onExtracted hook fires with the extracted value.threadId?:
threadId found on messages. Passed to extractor contexts.resourceId?:
resourceId found on messages. Passed to extractor contexts.memory?:
Memory.summarizeThread().mastra?:
Memory.summarizeThread().requestContext?:
abortSignal?:
ReturnsReturns的直接链接
summary:
extracted:
extract extractors, keyed by extractor slug.extractionFailures:
usage:
Extended usage exampleExtended usage example的直接链接
The following example summarizes an in-flight voice session from messages the application already holds, and extracts a structured record:
import { summarizeConversation, Extractor } from '@mastra/memory'
import type { MastraDBMessage } from '@mastra/core/agent'
import { z } from 'zod'
import { sessionRecords } from './db'
const sessionSummary = new Extractor({
name: 'session-summary',
instructions: 'Return a concise summary of the session.',
schema: z.object({
summary: z.string(),
sentiment: z.enum(['positive', 'neutral', 'negative']),
followUps: z.array(z.string()),
}),
metadataKeyPath: false, // don't persist into memory metadata — the hook owns storage
onExtracted: async ({ current, threadId }) => {
await sessionRecords.upsert({ sessionId: threadId, record: current })
},
})
export async function onSessionEnd(messages: MastraDBMessage[]) {
const { summary, extracted, extractionFailures } = await summarizeConversation({
model: 'openai/gpt-5-mini',
messages,
instructions: 'Summarize this support session for the account manager.',
extract: [sessionSummary],
})
if (extractionFailures?.length) {
console.warn('Some extractors failed', extractionFailures)
}
return { summary, extracted }
}