examples/functions/capture-tone-of-voice/README.md
Content creators often need to understand the tone of voice in their content to ensure consistency and alignment with brand guidelines. Manually analyzing tone can be time-consuming and subjective, especially for longer pieces of content.
This function automatically analyzes the tone of voice of your content using Sanity's Agent Actions (AI) capabilities. When triggered, it examines the content and saves an analysis of the tone to a field in your schema, making it easy for content creators to understand and maintain consistent voice across their content.
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 toneOfVoice field to your post schema:
studio/src/schemaTypes/documents/post.tsfields array:defineField({
name: 'toneOfVoice',
title: 'Tone of Voice',
type: 'text',
readOnly: () => true, // This is optional
description: 'Tone of voice analysis 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 capture-tone-of-voice
Add configuration to your blueprint
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: 'capture-tone-of-voice',
src: './functions/capture-tone-of-voice',
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
Make sure you have a schema deployed
From the studio folder, run:
# In the studio/ folder
npx sanity schema deploy
You can test the capture-tone-of-voice 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 capture-tone-of-voice --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: Be sure to set noWrite: false in your Agent Action Function.
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: 'capture-tone-of-voice',
src: './functions/capture-tone-of-voice',
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:
After deployment, you can verify your function is active by:
toneOfVoice field is populatednpx sanity functions logs log-eventtoneOfVoice function runs, it writes changes to the same document's toneOfVoice 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 loopError: "Deploy Studio permission required"
Error: "Blueprint validation failed"
sanity.blueprint.ts configurationFunction not analyzing tone after deployment
For more details, see the official function deployment documentation.
post document type containing:
content field (for content analysis)toneOfVoice field in your schema (type: text, optionally hidden or readonly)When a document is created or updated, the function automatically:
content field using Sanity's AItoneOfVoice fieldResult: Content creators get automatic tone analysis that helps maintain consistent voice across their content.
instructionParams: {
content: {
type: "field",
path: "DESIRED_FIELD",
},
},
Error: "Cannot find module '@sanity/client'"
npm install in the function directoryError: "Field 'toneOfVoice' not found in schema"
toneOfVoice field to your schema as described in the Implementation sectionError: "Agent Action can't access 'toneOfVoice' field"