|
| 1 | +import { |
| 2 | + type AgentGenerateOptions, |
| 3 | + type AgentPromptOptions, |
| 4 | + useAgentGenerate, |
| 5 | + useAgentPrompt, |
| 6 | +} from '@sanity/sdk-react' |
| 7 | +import {Box, Button, Card, Code, Label, Stack, Text} from '@sanity/ui' |
| 8 | +import {type JSX, useMemo, useState} from 'react' |
| 9 | + |
| 10 | +import {PageLayout} from '../components/PageLayout' |
| 11 | + |
| 12 | +export function AgentActionsRoute(): JSX.Element { |
| 13 | + const generate = useAgentGenerate() |
| 14 | + const prompt = useAgentPrompt() |
| 15 | + const [text, setText] = useState('Write a short poem about typescript and cats') |
| 16 | + const [promptResult, setPromptResult] = useState<string>('') |
| 17 | + const [generateResult, setGenerateResult] = useState<string>('') |
| 18 | + const [isLoadingPrompt, setIsLoadingPrompt] = useState(false) |
| 19 | + const [isLoadingGenerate, setIsLoadingGenerate] = useState(false) |
| 20 | + const generateOptions = useMemo<AgentGenerateOptions>(() => { |
| 21 | + return { |
| 22 | + // Use the schema collection id (workspace name), not the type name |
| 23 | + schemaId: '_.schemas.default', |
| 24 | + targetDocument: { |
| 25 | + operation: 'create', |
| 26 | + _id: crypto.randomUUID(), |
| 27 | + _type: 'movie', |
| 28 | + }, |
| 29 | + instruction: |
| 30 | + 'Generate a title and overview for a movie about $topic based on a famous movie. Try to not pick the same movie as someone else would pick.', |
| 31 | + instructionParams: {topic: 'Sanity SDK'}, |
| 32 | + target: {include: ['title', 'overview']}, |
| 33 | + noWrite: true, |
| 34 | + } |
| 35 | + }, []) |
| 36 | + |
| 37 | + const promptOptions = useMemo<AgentPromptOptions>( |
| 38 | + () => ({instruction: text, format: 'string'}), |
| 39 | + [text], |
| 40 | + ) |
| 41 | + |
| 42 | + return ( |
| 43 | + <PageLayout title="Agent Actions" subtitle="Prompt and generate using the movie schema"> |
| 44 | + <Stack space={4}> |
| 45 | + <Card padding={4} radius={2} shadow={1} tone="inherit"> |
| 46 | + <Stack space={3}> |
| 47 | + <Label size={1}>Prompt</Label> |
| 48 | + <Text muted size={1}> |
| 49 | + Sends an instruction to the LLM and returns plain text (or JSON if requested). Does |
| 50 | + not reference a schema or write any data. |
| 51 | + </Text> |
| 52 | + <textarea |
| 53 | + value={text} |
| 54 | + onChange={(e) => setText(e.target.value)} |
| 55 | + disabled={isLoadingPrompt} |
| 56 | + style={{ |
| 57 | + width: '100%', |
| 58 | + height: 120, |
| 59 | + border: '1px solid #ccc', |
| 60 | + borderRadius: 4, |
| 61 | + padding: 8, |
| 62 | + }} |
| 63 | + /> |
| 64 | + <Box> |
| 65 | + <Button |
| 66 | + text="Run prompt" |
| 67 | + tone="primary" |
| 68 | + disabled={isLoadingPrompt} |
| 69 | + onClick={() => { |
| 70 | + setIsLoadingPrompt(true) |
| 71 | + prompt(promptOptions) |
| 72 | + .then((value) => setPromptResult(String(value ?? ''))) |
| 73 | + .catch((err) => { |
| 74 | + // eslint-disable-next-line no-console |
| 75 | + console.error(err) |
| 76 | + }) |
| 77 | + .finally(() => setIsLoadingPrompt(false)) |
| 78 | + }} |
| 79 | + /> |
| 80 | + </Box> |
| 81 | + {promptResult && ( |
| 82 | + <Card padding={3} radius={2} tone="transparent"> |
| 83 | + <Text> |
| 84 | + <Code style={{whiteSpace: 'pre-wrap'}}>{promptResult}</Code> |
| 85 | + </Text> |
| 86 | + </Card> |
| 87 | + )} |
| 88 | + </Stack> |
| 89 | + </Card> |
| 90 | + |
| 91 | + <Card padding={4} radius={2} shadow={1} tone="inherit"> |
| 92 | + <Stack space={3}> |
| 93 | + <Label size={1}>Generaten a Sanity document (no write)</Label> |
| 94 | + <Text muted size={1}> |
| 95 | + Generates title and overview for a movie; does not persist changes. |
| 96 | + </Text> |
| 97 | + <Text muted size={1}> |
| 98 | + Schema‑aware content generation targeting the current project/dataset. Use schemaId of |
| 99 | + your document type (e.g. "movie") and target to specify fields. Set noWrite |
| 100 | + to preview without saving. |
| 101 | + </Text> |
| 102 | + <Box> |
| 103 | + <Button |
| 104 | + text="Generate" |
| 105 | + tone="primary" |
| 106 | + disabled={isLoadingGenerate} |
| 107 | + onClick={() => { |
| 108 | + setIsLoadingGenerate(true) |
| 109 | + const sub = generate(generateOptions).subscribe({ |
| 110 | + next: (value) => { |
| 111 | + setGenerateResult(JSON.stringify(value, null, 2)) |
| 112 | + setIsLoadingGenerate(false) |
| 113 | + sub.unsubscribe() |
| 114 | + }, |
| 115 | + error: (err) => { |
| 116 | + // eslint-disable-next-line no-console |
| 117 | + console.error(err) |
| 118 | + setIsLoadingGenerate(false) |
| 119 | + }, |
| 120 | + }) |
| 121 | + }} |
| 122 | + /> |
| 123 | + </Box> |
| 124 | + {generateResult && ( |
| 125 | + <Card padding={3} radius={2} tone="transparent"> |
| 126 | + <Text> |
| 127 | + <Code style={{whiteSpace: 'pre-wrap'}}>{generateResult}</Code> |
| 128 | + </Text> |
| 129 | + </Card> |
| 130 | + )} |
| 131 | + </Stack> |
| 132 | + </Card> |
| 133 | + </Stack> |
| 134 | + </PageLayout> |
| 135 | + ) |
| 136 | +} |
0 commit comments