Const AlphaA stable callback that triggers the action and yields a Subscribable stream.
This hook provides a stable callback to trigger AI-powered content generation for documents.
Features:
$variables and supports instructionParams (constants, fields, documents, GROQ queries).temperature, async, noWrite, and conditionalPaths.import {useAgentGenerate} from '@sanity/sdk-react'
function GenerateDescription({documentId}: {documentId: string}) {
const generate = useAgentGenerate()
const handleGenerate = () => {
generate({
documentId,
instruction: 'Write a compelling product description based on the title: $title',
instructionParams: {
title: {type: 'field', path: 'title'},
},
targetPaths: ['description'],
}).subscribe({
next: (result) => console.log('Generation progress:', result),
complete: () => console.log('Generation complete'),
error: (err) => console.error('Generation failed:', err),
})
}
return <button onClick={handleGenerate}>Generate Description</button>
}
import {useAgentGenerate} from '@sanity/sdk-react'
function GenerateProductImage({documentId}: {documentId: string}) {
const generate = useAgentGenerate()
const handleGenerateImage = () => {
generate({
documentId,
instruction: 'Generate a product photo for: $productName',
instructionParams: {
productName: {type: 'field', path: 'name'},
},
targetPaths: ['mainImage'],
}).subscribe({
complete: () => console.log('Image generated'),
})
}
return <button onClick={handleGenerateImage}>Generate Image</button>
}
Generates content for a document (or specific fields) via Sanity Agent Actions.