examples/functions/brand-voice-validator/README.md
Content creators often struggle to align their writing with brand guidelines and style requirements. Manually reviewing content for tone, structure, and style consistency is time-consuming and can be subjective, especially when working with multiple authors or large content teams.
This function automatically analyzes your content against brand style guidelines using Sanity's Agent Actions (AI) capabilities. When triggered, it examines the content and generates specific, actionable suggestions for improvement, storing them directly in a field in your schema for easy review and implementation.
This function is built to be compatible with any of the official "clean" templates. We recommend testing the function out in one of those after you have installed them locally.
If you're using the nextjs-clean template, you'll need to add a suggestedChanges field to your post schema:
studio/src/schemaTypes/documents/post.tsfields array:defineField({
name: 'suggestedChanges',
title: 'Suggested Changes',
type: 'text',
readOnly: () => true, // This is optional
description: 'Content suggestions will be automatically generated when you publish a post',
}),
# From the studio/ folder
npx sanity schema deploy
Important: Run these commands from the root of your project (not inside the studio/ folder).
Initialize the example
Run this if you haven't initialized blueprints:
npx sanity blueprints init
You'll be prompted to select your organization and Sanity studio.
Then run:
npx sanity blueprints add function --example brand-voice-validator
Add configuration to your blueprint
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: 'brand-voice-validator',
src: './functions/brand-voice-validator',
memory: 2,
timeout: 60,
event: {
on: ['create', 'update'],
filter:
"_type == 'post' && (delta::changedAny(content) || (delta::operation() == 'create' && defined(content)))",
projection: '{_id}',
},
}),
],
})
Install dependencies
Install dependencies in the project root:
npm install
And install function dependencies:
Make sure you have a schema deployed
From the studio folder, run:
# In the studio/ folder
npx sanity schema deploy
You can test the brand-voice-validator function locally using the Sanity CLI before deploying it to production.
Test the function with an existing document ID from your dataset:
npx sanity functions test brand-voice-validator --document-id <insert-document-id> --dataset production --with-user-token
Replace <insert-document-id> with an actual document ID from your dataset and production with your dataset name.
Start the development server for interactive testing:
npx sanity functions dev
Once you've tested your function locally and are satisfied with its behavior, you can deploy it to production.
Important: Make sure you have the Deploy Studio permission for your Sanity project before attempting to deploy.
Make sure your sanity.blueprint.ts file is properly configured with your function:
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: 'brand-voice-validator',
src: './functions/brand-voice-validator',
memory: 2,
timeout: 60,
event: {
on: ['create', 'update'],
filter:
"_type == 'post' && defined(content) && (delta::changedAny(content) || delta::operation() == 'create')",
projection: '{_id}',
},
}),
],
})
From your project root, run:
npx sanity blueprints deploy
This command will:
Important: Be sure to set noWrite: false in your Agent Action Function.
After deployment, you can verify your function is active by:
suggestedChanges field is populatedsuggestedChanges function runs, it writes changes to the same document's suggestedChanges field but does not publish the document. Be sure not to add skipforcePublishedWrite: true, to your index.ts's agent.action.generate function, as this will create an infinite loopsuggestedChanges field exists in your schemaError: "Deploy Studio permission required"
Error: "Blueprint validation failed"
sanity.blueprint.ts configurationFunction not generating suggestions after deployment
Error: "AI Assistant feature not enabled"
For more details, see the official function deployment documentation.
content field (rich text/portable text)suggestedChanges field added to your post schema (text type)When you publish a post document and the content field changes, the function automatically:
suggestedChanges field in the documentThis results in content creators having immediate feedback on how to improve their writing to match brand guidelines.
You can customize the brand style guide by modifying the brandsWritingStyleGuide constant in the function code. The current guide focuses on:
To modify the style guide:
brandsWritingStyleGuide variable in index.tsError: "AI Assistant feature not enabled"
Error: "Field 'suggestedChanges' not found"
Error: "Document not found"