@sanity/assist 1.2.16 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +551 -30
- package/dist/index.cjs.mjs +1 -0
- package/dist/index.d.ts +333 -9
- package/dist/index.esm.js +2463 -390
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +2457 -383
- package/dist/index.js.map +1 -1
- package/package.json +12 -11
- package/src/_lib/form/DocumentForm.tsx +2 -1
- package/src/_lib/form/constants.ts +1 -0
- package/src/assistDocument/AssistDocumentInput.tsx +24 -4
- package/src/assistDocument/RequestRunInstructionProvider.tsx +37 -21
- package/src/assistDocument/components/AssistDocumentForm.tsx +65 -21
- package/src/assistDocument/components/instruction/InstructionInput.tsx +5 -4
- package/src/assistDocument/components/instruction/InstructionOutputField.tsx +45 -0
- package/src/assistDocument/components/instruction/InstructionOutputInput.tsx +205 -0
- package/src/assistDocument/hooks/useStudioAssistDocument.ts +5 -32
- package/src/assistFormComponents/AssistField.tsx +11 -5
- package/src/assistFormComponents/AssistFormBlock.tsx +2 -3
- package/src/assistFormComponents/validation/listItem.tsx +2 -2
- package/src/assistInspector/AssistInspector.tsx +6 -0
- package/src/assistInspector/FieldAutocomplete.tsx +1 -0
- package/src/assistInspector/helpers.ts +9 -11
- package/src/assistLayout/AssistLayout.tsx +9 -9
- package/src/components/ImageContext.tsx +30 -13
- package/src/components/SafeValueInput.tsx +4 -1
- package/src/fieldActions/assistFieldActions.tsx +42 -13
- package/src/fieldActions/generateCaptionActions.tsx +17 -6
- package/src/fieldActions/generateImageActions.tsx +57 -0
- package/src/helpers/assistSupported.ts +10 -16
- package/src/helpers/conditionalMembers.test.ts +200 -0
- package/src/helpers/conditionalMembers.ts +127 -0
- package/src/helpers/misc.ts +8 -4
- package/src/helpers/typeUtils.ts +19 -5
- package/src/index.ts +3 -0
- package/src/plugin.tsx +18 -4
- package/src/schemas/assistDocumentSchema.tsx +40 -1
- package/src/schemas/serialize/serializeSchema.test.ts +239 -8
- package/src/schemas/serialize/serializeSchema.ts +77 -10
- package/src/schemas/typeDefExtensions.ts +89 -5
- package/src/translate/FieldTranslationProvider.tsx +360 -0
- package/src/translate/getLanguageParams.ts +26 -0
- package/src/translate/languageStore.ts +18 -0
- package/src/translate/paths.test.ts +133 -0
- package/src/translate/paths.ts +175 -0
- package/src/translate/translateActions.tsx +188 -0
- package/src/translate/types.ts +160 -0
- package/src/types.ts +67 -15
- package/src/useApiClient.ts +134 -2
- package/src/assistLayout/AlphaMigration.tsx +0 -310
- package/src/legacy-types.ts +0 -72
package/README.md
CHANGED
|
@@ -16,8 +16,20 @@
|
|
|
16
16
|
- [Disable for a field](#disable-for-a-field)
|
|
17
17
|
- [Disable for an array type](#disable-for-an-array-type)
|
|
18
18
|
- [Unsupported types](#unsupported-types)
|
|
19
|
+
- [Hidden and readOnly fields](#hidden-and-readonly-fields)
|
|
20
|
+
- [Reference support](#reference-support)
|
|
19
21
|
- [Troubleshooting](#troubleshooting)
|
|
20
22
|
- [Included document types](#included-document-types)
|
|
23
|
+
- [Field and type filters](#field-and-type-filters)
|
|
24
|
+
- [Image description generation](#image-description-generation)
|
|
25
|
+
- [Image generation](#image-generation)
|
|
26
|
+
- [Full document translation](#full-document-translation)
|
|
27
|
+
- [What it solves](#what-ai-assist-full-document-translations-solves)
|
|
28
|
+
- [Configure](#configure-document-translations)
|
|
29
|
+
- [Field level translations](#field-level-translations)
|
|
30
|
+
- [What it solves](#what-ai-assist-field-level-translations-solves)
|
|
31
|
+
- [Configure](#configure-field-translations)
|
|
32
|
+
- [Adding translation actions to fields](#adding-translation-actions-to-fields)
|
|
21
33
|
- [License](#license)
|
|
22
34
|
- [Develop \& test](#develop--test)
|
|
23
35
|
- [Release new version](#release-new-version)
|
|
@@ -25,9 +37,9 @@
|
|
|
25
37
|
|
|
26
38
|
## About Sanity AI Assist
|
|
27
39
|
|
|
28
|
-
Free your team to do more of what they’re great at (and less
|
|
40
|
+
Free your team to do more of what they’re great at (and less busywork) with the AI assistant that works with structured content. Attach reusable AI instructions to fields and documents to supercharge your editorial workflow.
|
|
29
41
|
|
|
30
|
-
You create the instructions; Sanity AI Assist does the rest. [Learn more about writing instructions in the Sanity documentation](https://www.sanity.io/
|
|
42
|
+
You create the instructions; Sanity AI Assist does the rest. [Learn more about writing instructions in the Sanity documentation](https://www.sanity.io/docs/ai-assist?utm_source=github.com&utm_medium=organic_social&utm_campaign=ai-assist&utm_content=).
|
|
31
43
|
|
|
32
44
|
[Read the release announcement here.](https://www.sanity.io/blog/sanity-ai-assist-announcement?utm_source=github.com&utm_medium=organic_social&utm_campaign=ai-assist&utm_content=)
|
|
33
45
|
|
|
@@ -43,7 +55,7 @@ In your Studio project folder, install the following plugin dependency:
|
|
|
43
55
|
npm install @sanity/assist sanity@latest
|
|
44
56
|
```
|
|
45
57
|
|
|
46
|
-
This plugin requires `sanity` version `3.
|
|
58
|
+
This plugin requires `sanity` version `3.26` or greater.
|
|
47
59
|
|
|
48
60
|
## Setup
|
|
49
61
|
|
|
@@ -104,9 +116,8 @@ themselves can edit can be changed by the instruction instance.
|
|
|
104
116
|
|
|
105
117
|
## Schema configuration
|
|
106
118
|
|
|
107
|
-
By default, most
|
|
119
|
+
By default, most string, object, and array field types (including Portable Text!) have AI writing assistance enabled. Your assistant can write to all compatible fields that it detects.
|
|
108
120
|
|
|
109
|
-
The assistant can also create array items, including Portable Text blocks, when the type has been imported to the Studio's schema as a custom type ([learn more about strict schemas](https://www.sanity.io/docs/graphql#33ec7103289a)).
|
|
110
121
|
|
|
111
122
|
### Disable AI Assist for a schema type
|
|
112
123
|
|
|
@@ -117,7 +128,7 @@ defineType({
|
|
|
117
128
|
name: 'policy',
|
|
118
129
|
type: 'document',
|
|
119
130
|
options: {
|
|
120
|
-
|
|
131
|
+
aiAssist: {exclude: true}
|
|
121
132
|
},
|
|
122
133
|
fields: [
|
|
123
134
|
// ...
|
|
@@ -137,7 +148,7 @@ defineType({
|
|
|
137
148
|
name: 'sku',
|
|
138
149
|
type: 'string',
|
|
139
150
|
options: {
|
|
140
|
-
|
|
151
|
+
aiAssist: {exclude: true}
|
|
141
152
|
}
|
|
142
153
|
})
|
|
143
154
|
]
|
|
@@ -156,7 +167,7 @@ defineType({
|
|
|
156
167
|
defineArrayMember({
|
|
157
168
|
type: 'customProduct',
|
|
158
169
|
options: {
|
|
159
|
-
|
|
170
|
+
aiAssist: {exclude: true}
|
|
160
171
|
}
|
|
161
172
|
})
|
|
162
173
|
]
|
|
@@ -167,29 +178,80 @@ defineType({
|
|
|
167
178
|
|
|
168
179
|
The following types are not supported, and behave as excluded types:
|
|
169
180
|
* [Number](https://www.sanity.io/docs/number-type)
|
|
170
|
-
* [Reference](https://www.sanity.io/docs/reference-type)
|
|
171
181
|
* [Slug](https://www.sanity.io/docs/slug-type)
|
|
172
182
|
* [Url](https://www.sanity.io/docs/url-type)
|
|
173
183
|
* [Date](https://www.sanity.io/docs/date-type)
|
|
174
184
|
* [Datetime](https://www.sanity.io/docs/datetime-type)
|
|
175
185
|
* [Image](https://www.sanity.io/docs/image-type) (supported when image has custom fields)
|
|
176
186
|
* [File](https://www.sanity.io/docs/file-type) (never supported, even when file has custom fields)
|
|
177
|
-
|
|
178
|
-
Types and fields with `hidden` or `readonly` with a truthy value (`true` or `function`) are not supported.
|
|
179
|
-
(Hidden and readOnly fields can be referenced in instructions still)
|
|
187
|
+
* [Reference](https://www.sanity.io/docs/reference-type) (supported when configured with embeddingsIndex)
|
|
180
188
|
|
|
181
189
|
Fields with these types will not be changed by the assistant, do not have AI Assist actions, and cannot be referenced in instructions.
|
|
182
190
|
|
|
183
191
|
Objects where all fields are excluded or unsupported and arrays where all member types are excluded or unsupported
|
|
184
192
|
will also be excluded.
|
|
185
193
|
|
|
194
|
+
### Hidden and readOnly fields
|
|
195
|
+
|
|
196
|
+
In AI Assist 2.0 and later, conditionally `hidden` and `readOnly` fields can have instructions.
|
|
197
|
+
These fields can be written to by an instruction, as long as the field is non-hidden and writable when the instruction starts running.
|
|
198
|
+
|
|
199
|
+
Fields with `hidden` or `readOnly` set to literal `true` will be skipped by AI Assist.
|
|
200
|
+
|
|
201
|
+
*Note*: An instruction will not re-evaluate these states during a run.
|
|
202
|
+
I.e., if an instruction makes a change during its execution that triggers another field to change its `hidden` or `readOnly` status,
|
|
203
|
+
the running instruction will still consider these as if in their original state.
|
|
204
|
+
|
|
205
|
+
If it is essential that AI Assist *never* writes to a conditional field,
|
|
206
|
+
it should be marked with `options.aiAssist.exclude: true`.
|
|
207
|
+
|
|
208
|
+
### Reference support
|
|
209
|
+
|
|
210
|
+
#### Create an Embeddings-index
|
|
211
|
+
To enable AI assist for references, first, your project must have an existing [embeddings-index](https://www.sanity.io/docs/embeddings-index-api-overview)
|
|
212
|
+
that includes the document types it should be able to reference.
|
|
213
|
+
|
|
214
|
+
You can manage your indexes directly in the studio using the [Embeddings Index Dashboard plugin](https://github.com/sanity-io/embeddings-index-ui#embeddings-index-api-dashboard-for-sanity-studio).
|
|
215
|
+
|
|
216
|
+
#### Set schema options
|
|
217
|
+
Set `options.aiAssist.embeddingsIndex` for reference fields/types you want to enable reference instructions for.
|
|
218
|
+
Reference fields with this option set can have instructions attached and will be visited when running instructions for object fields and arrays.
|
|
219
|
+
|
|
220
|
+
AI Assist will use the embeddings-index, further filtered by the types allowed by the reference, to look up contextually relevant references.
|
|
221
|
+
For arrays or portable text fields with references, one or more references can be added. Use the instruction text to control this.
|
|
222
|
+
|
|
223
|
+
Example:
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
import {defineArrayMember} from 'sanity'
|
|
227
|
+
|
|
228
|
+
defineField({
|
|
229
|
+
type: 'reference',
|
|
230
|
+
name: 'articleReference',
|
|
231
|
+
title: 'Article reference',
|
|
232
|
+
to: [ { type: 'article'} ],
|
|
233
|
+
options: {
|
|
234
|
+
aiAssist: {
|
|
235
|
+
embeddingsIndex: 'article-index'
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
An example instruction attached to this field could be:
|
|
242
|
+
`Given <Document field: Title> suggest a related article`
|
|
243
|
+
|
|
244
|
+
Running it would use the `article-index` to find a related article based on the current document title.
|
|
245
|
+
|
|
186
246
|
### Troubleshooting
|
|
187
247
|
|
|
188
|
-
There are limits to how much text the
|
|
248
|
+
There are limits to how much text the assistant can handle when processing an instruction. Under the hood, AI Assist will add information about your schema, which adds to what's commonly referred to as “the context window.”
|
|
189
249
|
|
|
190
250
|
If you have a very large schema (that is, many document and field types), it can be necessary to exclude types to limit how much of the context window is used for the schema itself.
|
|
191
251
|
|
|
192
|
-
We recommend excluding any and all types
|
|
252
|
+
We recommend excluding any and all types that aren't likely to benefit from automated workflows. A quick win is typically to exclude array types. It can be a good idea to exclude most non-block types from Portable Text arrays. This will ensure that AI Assist outputs mostly formatted text.
|
|
253
|
+
|
|
254
|
+
It is also possible to exclude fields/types when creating an instruction. See [Field and type filters](#field-and-type-filters) for more.
|
|
193
255
|
|
|
194
256
|
## Included document types
|
|
195
257
|
|
|
@@ -206,20 +268,21 @@ import {contextDocumentTypeName} from '@sanity/assist'
|
|
|
206
268
|
S.documentTypeListItem(contextDocumentTypeName)
|
|
207
269
|
```
|
|
208
270
|
|
|
209
|
-
##
|
|
271
|
+
## Field and type filters
|
|
210
272
|
|
|
211
|
-
|
|
212
|
-
|
|
273
|
+
When creating instructions for documents, object fields, array fields, or portable text fields, you can explicitly control what will be visited by AI Assist.
|
|
274
|
+
By default, the assistant will include all compatible fields and types.
|
|
213
275
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
* Limited capacity: The underlying LLM APIs used by AI Assist are resource constrained
|
|
276
|
+
Opting out fields/types per instruction is done using the respective field/type filter checkboxes under the instruction.
|
|
277
|
+
When using these filters, it is not necessary to tell AI Assist what to include in the instruction text itself.
|
|
217
278
|
|
|
218
|
-
|
|
279
|
+
Note that once the schema targeted by the instruction changes, the following behavior applies:
|
|
280
|
+
* instructions that include all fields or types will automatically also include the new fields or types
|
|
281
|
+
* instructions that have excluded one or more fields or types will NOT include the new fields or types
|
|
219
282
|
|
|
220
|
-
|
|
221
|
-
AI Assist can optionally generate
|
|
222
|
-
by setting the `options.
|
|
283
|
+
## Image description generation
|
|
284
|
+
AI Assist can optionally generate descriptions for images. This has to be enabled on an image-type/field,
|
|
285
|
+
by setting the `options.aiAssist.imageDescriptionField` on the image type, where `imageDescriptionField` is the field name of a
|
|
223
286
|
custom string-field on the image object:
|
|
224
287
|
|
|
225
288
|
```tsx
|
|
@@ -230,21 +293,479 @@ defineField({
|
|
|
230
293
|
fields: [
|
|
231
294
|
defineField({
|
|
232
295
|
type: 'string',
|
|
233
|
-
name: '
|
|
234
|
-
title: '
|
|
296
|
+
name: 'altText',
|
|
297
|
+
title: 'Alternative text',
|
|
235
298
|
}),
|
|
236
299
|
],
|
|
237
300
|
options: {
|
|
238
|
-
|
|
301
|
+
aiAssist: {
|
|
302
|
+
imageDescriptionField: 'altText',
|
|
303
|
+
}
|
|
239
304
|
},
|
|
240
305
|
})
|
|
241
306
|
```
|
|
242
|
-
This will add a
|
|
243
|
-
|
|
307
|
+
This will add a **Generate image description** action to the configured field.
|
|
308
|
+
The **Generate image description** action will automatically run whenever the image changes.
|
|
244
309
|
|
|
245
|
-
`
|
|
310
|
+
`imageDescriptionField` can be a nested field, if the image has an object field, i.e. `imageDescriptionField: 'wrapper.altText'`.
|
|
246
311
|
Fields within array items are not supported.
|
|
247
312
|
|
|
313
|
+
## Image generation
|
|
314
|
+
<img width="600" alt="image" src="https://github.com/sanity-io/assist/assets/835514/c4de6791-f530-4cd1-b0c2-96ef988bc256">
|
|
315
|
+
|
|
316
|
+
AI Assist can generate assets for images configured with a prompt field.
|
|
317
|
+
|
|
318
|
+
An image is generated directly by using the **Generate image from prompt** instruction on the prompt field,
|
|
319
|
+
or indirectly whenever the image prompt field is written to by an AI Assist instruction.
|
|
320
|
+
|
|
321
|
+
### Configure
|
|
322
|
+
To enable image generation for an image field, the image must:
|
|
323
|
+
- set `options.aiAssist.imageInstructionField` to a child-path relative to the image
|
|
324
|
+
- have a `string` or `text` field that corresponds to the `imageInstructionField` path
|
|
325
|
+
|
|
326
|
+
This will add a **Generate image from prompt** instruction to the image prompt field. Running it will generate an image.
|
|
327
|
+
|
|
328
|
+
Additionally, whenever an AI Assist instruction writes to the image prompt field, the image will be re-generated.
|
|
329
|
+
|
|
330
|
+
This could be a document instruction, an instruction for the image field or parent object, or directly on the image prompt field.
|
|
331
|
+
|
|
332
|
+
A common style guide can achieved by adding an instruction to the image prompt field that rewrites its value to include instructions on common style rules.
|
|
333
|
+
Use AI context documents to apply a reusable style guide to the prompt rewriting as needed.
|
|
334
|
+
|
|
335
|
+
#### Example
|
|
336
|
+
|
|
337
|
+
Given the following document schema
|
|
338
|
+
```ts
|
|
339
|
+
defineType({
|
|
340
|
+
type: 'document',
|
|
341
|
+
name: 'article',
|
|
342
|
+
fields: [
|
|
343
|
+
defineField({
|
|
344
|
+
type: 'image',
|
|
345
|
+
name: 'articleImage',
|
|
346
|
+
fields: [
|
|
347
|
+
defineField({
|
|
348
|
+
type: 'text',
|
|
349
|
+
name: 'imagePrompt',
|
|
350
|
+
title: 'Image prompt',
|
|
351
|
+
rows: 2,
|
|
352
|
+
}),
|
|
353
|
+
],
|
|
354
|
+
options: {
|
|
355
|
+
imagePromptField: 'imagePromptField',
|
|
356
|
+
},
|
|
357
|
+
})
|
|
358
|
+
]
|
|
359
|
+
})
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
To directly generate an image based on the value in the prompt field,
|
|
363
|
+
run the "Generate image from prompt" instruction that is automatically added.
|
|
364
|
+
|
|
365
|
+
For better image results or to ensure a consistent style, rewrite the prompt before generating the image:
|
|
366
|
+
|
|
367
|
+
### Example prompt expansion instruction
|
|
368
|
+
<img width="267" alt="image" src="https://github.com/sanity-io/assist/assets/835514/dabc6910-80d3-4a69-940f-49ac5cae9ade">
|
|
369
|
+
|
|
370
|
+
For better image results, use an instruction that expands the prompt to be more detailed.
|
|
371
|
+
|
|
372
|
+
Example instruction text:
|
|
373
|
+
|
|
374
|
+
```
|
|
375
|
+
Rewrite image prompts for image generation according to the following rules:
|
|
376
|
+
- Be Specific: Include detailed descriptions of the scene, objects, colors, and any characters. Instead of saying "a cat in a garden", say "a fluffy gray cat sitting beside pink tulips in a sunny garden".
|
|
377
|
+
- Set the Scene: Describe the environment or background. Mention if it's indoors or outdoors, the time of day, weather conditions, and any specific setting details like a beach, forest, cityscape, etc.
|
|
378
|
+
- Detail Characters: If your image includes people or animals, specify their appearance, clothing, poses, and expressions. For example, "a smiling young woman with short black hair, wearing a red dress, standing under a tree".
|
|
379
|
+
- Color and Style: Mention specific colors and artistic styles you prefer. For instance, "bright, vivid colors with a watercolor effect".
|
|
380
|
+
- Mood and Atmosphere: Describe the mood or atmosphere of the image. Words like 'peaceful', 'dynamic', 'mysterious', or 'joyful' can guide the AI in capturing the right tone.
|
|
381
|
+
- Avoid Ambiguity: Be clear and direct. Avoid using vague or abstract concepts that the AI might struggle to interpret.
|
|
382
|
+
- Follow the Guidelines: Ensure your prompt doesn't include any content against usage policies, such as depictions of real people, copyrighted characters, or sensitive subjects.
|
|
383
|
+
|
|
384
|
+
Keep it 100 words or less.
|
|
385
|
+
|
|
386
|
+
The prompt to rewrite is:
|
|
387
|
+
{Reference to image-prompt-field}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
The rules can be extracted into an AI Context document and reused in other instructions as needed. This approach can also be used to inform a reusable styleguide for image generation.
|
|
391
|
+
## Full document translation
|
|
392
|
+
<img width="250" alt="Translate document action" src="https://github.com/sanity-io/assist/assets/835514/932968ee-1a8c-4389-8822-338188f88b40">
|
|
393
|
+
|
|
394
|
+
AI assist offers full document translations, which is ideal for pairing with [@sanity/document-internationalization](https://github.com/sanity-io/document-internationalization).
|
|
395
|
+
|
|
396
|
+
Translations are done deeply; visiting nested objects, arrays and even Portable text annotations.
|
|
397
|
+
|
|
398
|
+
### What AI Assist full document translations solves
|
|
399
|
+
|
|
400
|
+
Given a document written in one language, AI assist can translate the document in place to a language specified by a language field in the document.
|
|
401
|
+
|
|
402
|
+
When the document translation feature is enabled, AI Assist will go through the document field by field, translating all string and portable text fields into the language specified in the document's language field.
|
|
403
|
+
|
|
404
|
+
This works especially well with [@sanity/document-internationalization](https://github.com/sanity-io/document-internationalization), which uses a strategy of creating copies of the source document for each separate language to be translated into and uses a hidden string field to set the language for each copy.
|
|
405
|
+
|
|
406
|
+
AI Assist allows editors to translate these documents into the desired language immediately.
|
|
407
|
+
|
|
408
|
+
### Configure document translations
|
|
409
|
+
|
|
410
|
+
To enable full document translations, set `translate.document.languageField` to the path of the language field in your documents.
|
|
411
|
+
|
|
412
|
+
All documents with a corresponding language field will get a "Translate document" instruction added to the AI Assist drop-down for the document.
|
|
413
|
+
|
|
414
|
+
To further limit which document types should be enabled for translation instructions, provide an array of document type names to `translate.document.documentTypes`.
|
|
415
|
+
|
|
416
|
+
If the studio is using [@sanity/document-internationalization](https://github.com/sanity-io/document-internationalization), these options should be the same as those used for that plugin.
|
|
417
|
+
|
|
418
|
+
**Example configs**
|
|
419
|
+
|
|
420
|
+
```ts
|
|
421
|
+
// This will add a "Translate document" instruction to all documents with a language field
|
|
422
|
+
assist({
|
|
423
|
+
translate: {
|
|
424
|
+
document: {
|
|
425
|
+
languageField: 'language'
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
})
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
// This will add a "Translate document" instruction only to the 'article' document type
|
|
433
|
+
assist({
|
|
434
|
+
translate: {
|
|
435
|
+
document: {
|
|
436
|
+
languageField: 'language',
|
|
437
|
+
documentTypes: ['article']
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
})
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
**All configuration params**
|
|
444
|
+
```ts
|
|
445
|
+
assist({
|
|
446
|
+
translate: {
|
|
447
|
+
/** Config for document types with a single language field that determines the language for the whole document. */
|
|
448
|
+
document: {
|
|
449
|
+
/**
|
|
450
|
+
* Required config, enable document tranlations.
|
|
451
|
+
*
|
|
452
|
+
* Path to language field in documents. Can be a hidden field.
|
|
453
|
+
* For instance: 'config.language'
|
|
454
|
+
*
|
|
455
|
+
* For projects that use the `@sanity/document-internationalization` plugin,
|
|
456
|
+
* this should be the same as `languageField` config for that plugin (which defaults to 'language')
|
|
457
|
+
*/
|
|
458
|
+
languageField: string,
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* `documentTypes` should be an array of strings where each entry must match a name from your document schemas.
|
|
462
|
+
*
|
|
463
|
+
* this property will add a translate instruction to these document types if defined.
|
|
464
|
+
* If undefined, the instruction will be added to all documents with aiAssistance enabled and a field matching `languageField` config.
|
|
465
|
+
*
|
|
466
|
+
* Documents with translation support will get a "Translate document>" instruction added.
|
|
467
|
+
**/
|
|
468
|
+
documentTypes: string[]
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
})
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
## Field level translations
|
|
475
|
+
<img width="250" alt="Translate fields action" src="https://github.com/sanity-io/assist/assets/835514/99819cd4-578e-43b2-8c70-8e39afff5f09">
|
|
476
|
+
|
|
477
|
+
<img width="250" alt="Translate fields dialog" src="https://github.com/sanity-io/assist/assets/835514/fe3d289c-49b6-46dd-ae2f-cd509a01534a">
|
|
478
|
+
|
|
479
|
+
AI assist offers field-level translations, which is ideal for use in conjunction with [sanity-plugin-internationalized-array](https://github.com/sanity-io/sanity-plugin-internationalized-array?tab=readme-ov-file#sanity-plugin-internationalized-array) and [@sanity/language-filter](https://github.com/sanity-io/language-filter)
|
|
480
|
+
|
|
481
|
+
### What AI Assist field-level translations solves
|
|
482
|
+
|
|
483
|
+
Given a document with field values in different languages, AI assist can transfer and translate from one language to the others.
|
|
484
|
+
|
|
485
|
+
The typical use case would be for documents that use internationalized wrapper types to hold values for multiple languages.
|
|
486
|
+
|
|
487
|
+
AI Assist supports complex values, so language fields that hold nested objects, portable text, or arrays will also be translated.
|
|
488
|
+
|
|
489
|
+
When initiating translations, editors select a language to translate from and which languages to translate to. This means that AI Assist supports partial translations in cases where editors are responsible for only some languages in the document.
|
|
490
|
+
|
|
491
|
+
### Configure field translations
|
|
492
|
+
To enable field-level translations, set `translate.field.documentTypes` to an array with which document types should get field translations, and `translate.field.languages`
|
|
493
|
+
|
|
494
|
+
```ts
|
|
495
|
+
assist({
|
|
496
|
+
translate: {
|
|
497
|
+
field: {
|
|
498
|
+
documentTypes: ['article'],
|
|
499
|
+
languages: [
|
|
500
|
+
{id: 'en', title: 'English'},
|
|
501
|
+
{id: 'de', title: 'German'}
|
|
502
|
+
]
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
})
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
These documents will get a **Translate fields** instruction added to the document AI Assist dropdown.
|
|
509
|
+
|
|
510
|
+
Out of the box, this is sufficient config for document types using the `internationalizedArray*` types provided by [sanity-plugin-internationalized-array](https://github.com/sanity-io/sanity-plugin-internationalized-array?tab=readme-ov-file#sanity-plugin-internationalized-array).
|
|
511
|
+
|
|
512
|
+
It will also work without further config for object types named `locale*`, (e.g. `localeTitle`, `localeDescription`) with one field per language:
|
|
513
|
+
|
|
514
|
+
*Example locale object supported by default*
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
// Object type with name starting with 'locale', and one field per language language
|
|
518
|
+
defineType({
|
|
519
|
+
type: 'object',
|
|
520
|
+
name: 'localeString',
|
|
521
|
+
fields: [
|
|
522
|
+
defineField({
|
|
523
|
+
// these do not have to be string, could be any type
|
|
524
|
+
type: 'string',
|
|
525
|
+
name: 'en',
|
|
526
|
+
title: 'English'
|
|
527
|
+
}),
|
|
528
|
+
defineField({
|
|
529
|
+
type: 'string',
|
|
530
|
+
name: 'de',
|
|
531
|
+
title: 'German'
|
|
532
|
+
})
|
|
533
|
+
]
|
|
534
|
+
})
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
**If your schema is not using either of these structures**, refer to the section on [Custom language fields](#custom-language-fields).
|
|
538
|
+
|
|
539
|
+
### Loading field languages
|
|
540
|
+
Languages must be an array of objects with an id and title.
|
|
541
|
+
|
|
542
|
+
```ts
|
|
543
|
+
assist({
|
|
544
|
+
translate: {
|
|
545
|
+
field: {
|
|
546
|
+
languages: [
|
|
547
|
+
{id: 'en', title: 'English'},
|
|
548
|
+
{id: 'de', title: 'German'}
|
|
549
|
+
]
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
})
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
Or an asynchronous function that returns an array of objects with an id and title.
|
|
556
|
+
|
|
557
|
+
```ts
|
|
558
|
+
assist({
|
|
559
|
+
translate: {
|
|
560
|
+
field: {
|
|
561
|
+
languages: async () => {
|
|
562
|
+
const response = await fetch('https://example.com/languages')
|
|
563
|
+
return response.json()
|
|
564
|
+
}
|
|
565
|
+
},
|
|
566
|
+
},
|
|
567
|
+
})
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
The async function contains a configured Sanity client in the first parameter, allowing you to store language options as documents. Your query should return an array of objects with an id and title.
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
```ts
|
|
574
|
+
assist({
|
|
575
|
+
translate: {
|
|
576
|
+
field: {
|
|
577
|
+
languages: async () => {
|
|
578
|
+
const response = await client.fetch(`*[_type == "language"]{ id, title }`)
|
|
579
|
+
return response
|
|
580
|
+
}
|
|
581
|
+
},
|
|
582
|
+
},
|
|
583
|
+
})
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
Additionally, you can "pick" fields from a document, to pass into the query. For example, if you have a concept of "Markets" where only certain language fields are required in certain markets.
|
|
587
|
+
|
|
588
|
+
In this example, each language document has an array of strings called markets to declare where that language can be used. And the document being authored has a single market field.
|
|
589
|
+
|
|
590
|
+
```ts
|
|
591
|
+
assist({
|
|
592
|
+
translate: {
|
|
593
|
+
field: {
|
|
594
|
+
selectLanguageParams: {
|
|
595
|
+
market: 'market'
|
|
596
|
+
},
|
|
597
|
+
languages: async (client, {market = ``}) => {
|
|
598
|
+
const response = await client.fetch(
|
|
599
|
+
`*[_type == "language" && $market in markets]{ id, title }`,
|
|
600
|
+
{market}
|
|
601
|
+
)
|
|
602
|
+
return response
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
})
|
|
607
|
+
```
|
|
608
|
+
|
|
609
|
+
### Custom language fields
|
|
610
|
+
By providing a function to `translate.field.translationOutputs`, you have complete control over which fields belong to which language.
|
|
611
|
+
|
|
612
|
+
`translationOutputs` is used when an editor uses the **Translate fields** instruction.
|
|
613
|
+
|
|
614
|
+
It determines the relationships between document paths: Given a document path and a language, it should return the approriate sibling paths into which translations are output.
|
|
615
|
+
|
|
616
|
+
`translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following arguments:
|
|
617
|
+
|
|
618
|
+
* `documentMember` - the field or array item for a given path; contains the path and its `schemaType`
|
|
619
|
+
* `enclosingType` - the schema type of the parent holding the member
|
|
620
|
+
* `translateFromLanguageId` - the `languageId` for the language the users want to translate from
|
|
621
|
+
* `translateToLanguageIds` - all `languageId`s the user can translate to
|
|
622
|
+
|
|
623
|
+
The function should return a `TranslationOutput[]` array that contains all the paths where translations from `documentMember` (in the language received in `translateFromLanguageId`) should be output.
|
|
624
|
+
|
|
625
|
+
The function should return `undefined` for all document members that should not be directly translated, or are nested fields under a translated path.
|
|
626
|
+
|
|
627
|
+
#### Default function
|
|
628
|
+
|
|
629
|
+
The default `translationOutputs` is available using `import {defaultTranslationOutputs} from '@sanity/assist`.
|
|
630
|
+
|
|
631
|
+
#### Example
|
|
632
|
+
|
|
633
|
+
Given the following document:
|
|
634
|
+
|
|
635
|
+
```ts
|
|
636
|
+
{
|
|
637
|
+
titles: {
|
|
638
|
+
_type: 'languageObject',
|
|
639
|
+
en: {
|
|
640
|
+
_type: 'titleObject',
|
|
641
|
+
title: 'Some title',
|
|
642
|
+
subtitle: 'Some subtitle'
|
|
643
|
+
},
|
|
644
|
+
de: {
|
|
645
|
+
_type: 'titleObject',
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
When translating from English to German, `translationOutputs` will be
|
|
652
|
+
invoked multiple times.
|
|
653
|
+
|
|
654
|
+
The following parameters will be the same every invocation:
|
|
655
|
+
|
|
656
|
+
* `translateFromLanguageId` will be `'en'`
|
|
657
|
+
* `translateToLanguageIds` will be `['de']`
|
|
658
|
+
|
|
659
|
+
`documentMember` and `enclosingType` will change between each invocation, and take the following values:
|
|
660
|
+
|
|
661
|
+
1. `{path: 'titles', name: 'titles', schemaType: ObjectSchemaType}`, `ObjectSchemaType`
|
|
662
|
+
2. `{path: 'titles.en', name: 'en', schemaType: ObjectSchemaType}`, `ObjectSchemaType`
|
|
663
|
+
3. `{path: 'titles.en.title', name: 'title', schemaType: StringSchemaType}`, `ObjectSchemaType`
|
|
664
|
+
4. `{path: 'titles.en.subtitle', name: 'subtitle', schemaType: StringSchemaType}`, `ObjectSchemaType`
|
|
665
|
+
5. `{path: 'titles.de', name: 'de', schemaType: ObjectSchemaType}`, `ObjectSchemaType`
|
|
666
|
+
|
|
667
|
+
To indicate that you want everything under `title.en` to be translated into `title.de`, `translationOutputs` needs to return `[id: 'de', outputPath: 'titles.de']` when invoked with `documentMember.path: 'titles.en'`.
|
|
668
|
+
|
|
669
|
+
The following function enables this:
|
|
670
|
+
|
|
671
|
+
```ts
|
|
672
|
+
function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds) {
|
|
673
|
+
const parentIsLanguageWrapper = enclosingType.jsonType === 'object' && enclosingType.name.startsWith('language')
|
|
674
|
+
|
|
675
|
+
if (parentIsLanguageWrapper && translateFromLanguageId === member.name) {
|
|
676
|
+
|
|
677
|
+
// [id: 'de', ]
|
|
678
|
+
return translateToLanguageIds.map((translateToId) => ({
|
|
679
|
+
id: translateToId,
|
|
680
|
+
// in this example, member.path is 'titles.en'
|
|
681
|
+
// so this changes titles.en -> titles.de
|
|
682
|
+
outputPath: [...member.path.slice(0, -1), translateToId],
|
|
683
|
+
}))
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// ignore other members
|
|
687
|
+
return undefined
|
|
688
|
+
}
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
### Full field translation configuration example
|
|
692
|
+
|
|
693
|
+
```ts
|
|
694
|
+
assist({
|
|
695
|
+
translate: {
|
|
696
|
+
field: {
|
|
697
|
+
documentTypes: ['article'],
|
|
698
|
+
selectLanguageParams: {market: 'market'},
|
|
699
|
+
apiVersion: '2023-01-01',
|
|
700
|
+
languages: (client, {market}) => {
|
|
701
|
+
return client.fetch(
|
|
702
|
+
`*[_type == "language" && $market in markets]{ id, title }`,
|
|
703
|
+
{market}
|
|
704
|
+
)
|
|
705
|
+
},
|
|
706
|
+
translationOutputs: (member, enclosingType, fromLanguageId, toLanguageIds) => {
|
|
707
|
+
// When the document member is named the same as fromLangagueId
|
|
708
|
+
// and it is a field in a object with a name starting with "language"
|
|
709
|
+
// then we return the paths to all other sibling language fields (and their langauge id)
|
|
710
|
+
// It is ok that the member is an object, then all child fields will be translated
|
|
711
|
+
if (translateFromLanguageId === member.name && enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale')) {
|
|
712
|
+
return translateToLanguageIds.map((translateToId) => ({
|
|
713
|
+
id: translateToId,
|
|
714
|
+
//changes path.to.en -> path.to.de (for instance)
|
|
715
|
+
outputPath: [...member.path.slice(0, -1), translateToId],
|
|
716
|
+
}))
|
|
717
|
+
}
|
|
718
|
+
// all other member paths are skipped
|
|
719
|
+
return undefined
|
|
720
|
+
}
|
|
721
|
+
},
|
|
722
|
+
},
|
|
723
|
+
})
|
|
724
|
+
```
|
|
725
|
+
|
|
726
|
+
## Adding translation actions to fields
|
|
727
|
+
|
|
728
|
+
<img width="250" alt="Translate action on field" src="https://github.com/sanity-io/assist/assets/835514/e6dc0860-90a7-4f7a-b3d2-71893b09862f">
|
|
729
|
+
|
|
730
|
+
<img width="250" alt="Translate fields action on field" src="https://github.com/sanity-io/assist/assets/835514/acc5fa23-2022-4eae-922d-5c83dda7379c">
|
|
731
|
+
|
|
732
|
+
By default, **Translate document** and **Translate fields…** instructions are only added to the top-level document instruction menu.
|
|
733
|
+
|
|
734
|
+
These instructions can also be added to fields by setting
|
|
735
|
+
`options.aiAssist.translateAction: true` for a field or type.
|
|
736
|
+
|
|
737
|
+
This allows editors to translate only parts of the document and can be useful to enable for `internationalizedArrays` or `locale` wrapper object types.
|
|
738
|
+
|
|
739
|
+
For document types configured for full document translations, a **Translate** action will be added. Running it will translate the field to the language set in the language field
|
|
740
|
+
|
|
741
|
+
For document types configured for field translations, a **Translate fields...** action will be added. Running it will open a dialog with language selectors.
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
#### Example
|
|
745
|
+
|
|
746
|
+
```ts
|
|
747
|
+
defineField({
|
|
748
|
+
name: 'subtitle',
|
|
749
|
+
type: 'internationalizedArrayString',
|
|
750
|
+
title: 'Subtitle',
|
|
751
|
+
options: {
|
|
752
|
+
aiAssist: {
|
|
753
|
+
translateAction: true
|
|
754
|
+
}
|
|
755
|
+
},
|
|
756
|
+
})
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
## Caveats
|
|
760
|
+
|
|
761
|
+
Large Language Models (LLMs) are a new technology. Constraints and limitations are still being explored,
|
|
762
|
+
but some common caveats to the field that you may run into using AI Assist are:
|
|
763
|
+
|
|
764
|
+
* Limits to instruction length: Long instructions on deep content structures may exhaust model context
|
|
765
|
+
* Timeouts: To be able to write structured content, we're using the largest language models - long-running results may time out or intermittently fail
|
|
766
|
+
* Limited capacity: The underlying LLM APIs used by AI Assist are resource-constrained
|
|
767
|
+
|
|
768
|
+
|
|
248
769
|
## Third party sub-processors
|
|
249
770
|
|
|
250
771
|
This version of the feature uses OpenAI.com as a third-party sub-processor. Their security posture has been vetted by Sanity's security team, and approved for use.
|