Kevin Green
Developer at Sanity
AI-Powered Content Summary from Long Form Content
defineField({
name: 'autoSummary',
title: 'Auto Summary',
type: 'text',
description: 'Summary will be automatically generated when you publish a post',
}),import {createClient} from '@sanity/client'
import {documentEventHandler} from '@sanity/functions'
export const handler = documentEventHandler(async ({context, event}) => {
const client = createClient({
...context.clientOptions,
apiVersion: 'vX',
useCdn: false,
})
const {data} = event
const {local} = context // local is true when running locally
try {
const result = await client.agent.action.generate({
noWrite: local ? true : false, // if local is true, we don't want to write to the document, just return the result for logging
instructionParams: {
content: {
type: 'field',
path: 'body',
},
},
instruction: `Based on the $content, write a summary no more than 250 words.`,
target: {
path: 'autoSummary',
},
documentId: data._id,
schemaId: '_.schemas.default',
forcePublishedWrite: true,
})
console.log(
local
? 'Generated summary (LOCAL TEST MODE - Content Lake not updated):'
: 'Generated summary:',
result.autoSummary,
)
} catch (error) {
console.error('Error occurred during summary generation:', error)
}
})
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
type: 'sanity.function.document',
src: './functions/auto-summary',
memory: 2,
timeout: 30,
name: 'auto-summary',
event: {
on: ['create', 'update'],
filter:
"_type == 'post' && (delta::changedAny(content) || (delta::operation() == 'create' && defined(content)))",
projection: '{_id}',
},
}),
],
})This Sanity Function solves a common content management challenge: the time-consuming task of summarizing long form content. Content creators could spend 5-10 minutes summarizing a completed article, which can lead reduced productivity in editorial workflows.
Quick Start
View the complete example and source code.
Initialize blueprints if you haven't already: npx sanity blueprints init
Then: npx sanity blueprints add function --example auto-summary
Then deploy: npx sanity blueprints deploy
How It Works
When a content editor publishes a new blog post without a summary, the function automatically:
Key Benefits
Technical Implementation
The function uses Sanity's AI capabilities to analyze the portable text content and generate contextually relevant summary. It's built with:
Perfect For
The function is compatible with any of Sanity's official "clean" templates and can be easily customized to adjust summary length, format, or target different document types.
Developer at Sanity
Automatically track when content was first published with a timestamp that sets once and never overwrites, providing reliable publication history for analytics and editorial workflows.
Go to First Published Timestamp FunctionAI-powered automatic tagging for Sanity blog posts that analyzes content to generate 3 relevant tags, maintaining consistency by reusing existing tags from your content library.
Go to Automatically tag blog postsOn document publish, send a Slack notification so your team is informed
Go to Notify your team via Slack on publishCreate, preview, and send Klaviyo campaigns without ever leaving Sanity Studio"
Go to Klaviyo campaigns without leaving your Studio