@sanity/assist 1.2.16 → 2.0.0

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.
Files changed (50) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +551 -30
  3. package/dist/index.cjs.mjs +1 -0
  4. package/dist/index.d.ts +253 -11
  5. package/dist/index.esm.js +2385 -373
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.js +2379 -366
  8. package/dist/index.js.map +1 -1
  9. package/package.json +10 -9
  10. package/src/_lib/form/DocumentForm.tsx +2 -1
  11. package/src/_lib/form/constants.ts +1 -0
  12. package/src/assistDocument/AssistDocumentInput.tsx +24 -4
  13. package/src/assistDocument/RequestRunInstructionProvider.tsx +37 -21
  14. package/src/assistDocument/components/instruction/InstructionInput.tsx +5 -4
  15. package/src/assistDocument/components/instruction/InstructionOutputField.tsx +45 -0
  16. package/src/assistDocument/components/instruction/InstructionOutputInput.tsx +205 -0
  17. package/src/assistDocument/hooks/useStudioAssistDocument.ts +5 -32
  18. package/src/assistFormComponents/AssistField.tsx +11 -5
  19. package/src/assistFormComponents/AssistFormBlock.tsx +2 -3
  20. package/src/assistFormComponents/validation/listItem.tsx +2 -2
  21. package/src/assistInspector/AssistInspector.tsx +6 -0
  22. package/src/assistInspector/FieldAutocomplete.tsx +1 -0
  23. package/src/assistInspector/helpers.ts +9 -11
  24. package/src/assistLayout/AssistLayout.tsx +9 -9
  25. package/src/components/ImageContext.tsx +19 -9
  26. package/src/components/SafeValueInput.tsx +4 -1
  27. package/src/fieldActions/assistFieldActions.tsx +42 -13
  28. package/src/fieldActions/generateCaptionActions.tsx +2 -2
  29. package/src/fieldActions/generateImageActions.tsx +57 -0
  30. package/src/helpers/assistSupported.ts +10 -16
  31. package/src/helpers/conditionalMembers.test.ts +200 -0
  32. package/src/helpers/conditionalMembers.ts +127 -0
  33. package/src/helpers/typeUtils.ts +19 -5
  34. package/src/index.ts +3 -0
  35. package/src/plugin.tsx +14 -5
  36. package/src/schemas/assistDocumentSchema.tsx +40 -1
  37. package/src/schemas/serialize/serializeSchema.test.ts +239 -8
  38. package/src/schemas/serialize/serializeSchema.ts +77 -10
  39. package/src/schemas/typeDefExtensions.ts +89 -5
  40. package/src/translate/FieldTranslationProvider.tsx +360 -0
  41. package/src/translate/getLanguageParams.ts +26 -0
  42. package/src/translate/languageStore.ts +18 -0
  43. package/src/translate/paths.test.ts +133 -0
  44. package/src/translate/paths.ts +175 -0
  45. package/src/translate/translateActions.tsx +188 -0
  46. package/src/translate/types.ts +160 -0
  47. package/src/types.ts +33 -12
  48. package/src/useApiClient.ts +130 -2
  49. package/src/assistLayout/AlphaMigration.tsx +0 -310
  50. package/src/legacy-types.ts +0 -72
@@ -3,4 +3,5 @@ import cjs from './index.js';
3
3
  export const SchemaTypeTool = cjs.SchemaTypeTool;
4
4
  export const assist = cjs.assist;
5
5
  export const contextDocumentTypeName = cjs.contextDocumentTypeName;
6
+ export const defaultLanguageOutputs = cjs.defaultLanguageOutputs;
6
7
 
package/dist/index.d.ts CHANGED
@@ -1,31 +1,197 @@
1
1
  import {JSX as JSX_2} from 'react/jsx-runtime'
2
+ import {Path} from 'sanity'
2
3
  import {Plugin as Plugin_2} from 'sanity'
3
- import {SanityClient} from '@sanity/client'
4
+ import {SanityClient} from 'sanity'
5
+ import {SanityClient as SanityClient_2} from '@sanity/client'
6
+ import {SchemaType} from 'sanity'
4
7
 
5
8
  export declare const assist: Plugin_2<void | AssistPluginConfig>
6
9
 
7
10
  export declare interface AssistOptions {
8
- aiWritingAssistance?: {
11
+ aiAssist?: {
9
12
  /** Set to true to disable assistance for this field or type */
10
13
  exclude?: boolean
14
+ /**
15
+ * Set to true to add translation field-action to the field.
16
+ * Only has an effect in document types configured for document or field level translations.
17
+ */
18
+ translateAction?: boolean
11
19
  }
12
20
  }
13
21
 
14
22
  declare interface AssistPluginConfig {
15
- /**
16
- * Set this to false to disable model migration from the alpha version of this plugin
17
- */
18
- alphaMigration?: boolean
23
+ translate?: TranslationConfig
19
24
  /**
20
25
  * @internal
21
26
  */
22
- __customApiClient?: (defaultClient: SanityClient) => SanityClient
27
+ __customApiClient?: (defaultClient: SanityClient_2) => SanityClient_2
23
28
  }
24
29
 
25
30
  export declare const contextDocumentTypeName: 'assist.instruction.context'
26
31
 
32
+ /**
33
+ * Default implementation for plugin config `translate.field.translationOutputs`
34
+ *
35
+ * @see FieldTranslationConfig#translationOutputs
36
+ */
37
+ export declare const defaultLanguageOutputs: TranslationOutputsFunction
38
+
39
+ export declare interface DocumentMember {
40
+ schemaType: SchemaType
41
+ path: Path
42
+ name: string
43
+ value: unknown
44
+ }
45
+
46
+ export declare interface DocumentTranslationConfig {
47
+ /**
48
+ * Path to language field in documents. Can be a hidden field.
49
+ * For instance: 'config.language'
50
+ *
51
+ * For projects that use the `@sanity/document-internationalization` plugin,
52
+ * this should be the same as `languageField` config for that plugin.
53
+ *
54
+ * Default: 'language'
55
+ */
56
+ languageField: string
57
+ /**
58
+ * `documentTypes` should be an array of strings where each entry must match a name from your document schemas.
59
+ *
60
+ * If defined, this property will add a translate instruction to these document types.
61
+ * If undefined, the instruction will be added to all documents with aiAssistance enabled and a field matching `documentLanguageField` config.
62
+ *
63
+ * Documents with translation support will get a "Translate document>" instruction added.
64
+ **/
65
+ documentTypes?: string[]
66
+ }
67
+
68
+ export declare interface FieldTranslationConfig {
69
+ /**
70
+ * `documentTypes` should be an array of strings where each entry must match a name from your document schemas.
71
+ *
72
+ * If defined, matching document will get a "Translate fields" instruction added.
73
+ **/
74
+ documentTypes?: string[]
75
+ /**
76
+ *
77
+ * Used for display strings in the Studio, and to determine languages for field level translations
78
+ *
79
+ * If the studio is using the sanity-plugin-internationalized-array plugin, this
80
+ * should be set to the same configuration.
81
+ */
82
+ languages: Language[] | LanguageCallback
83
+ /**
84
+ * API version for client passed to LanguageCallback for languages
85
+ * https://www.sanity.io/docs/api-versioning
86
+ * @defaultValue '2022-11-27'
87
+ */
88
+ apiVersion?: string
89
+ /**
90
+ * Specify fields that should be available in the languages callback:
91
+ * ```tsx
92
+ * {
93
+ * select: {
94
+ * markets: 'markets'
95
+ * },
96
+ * languages: (client, {markets}) =>
97
+ * client.fetch('*[_type == "language" && market in $markets]{id,title}', {markets})
98
+ * }
99
+ * ```
100
+ *
101
+ * If the studio is using the sanity-plugin-internationalized-array plugin, this
102
+ * should be set to the same configuration.
103
+ */
104
+ selectLanguageParams?: Record<string, string>
105
+ /**
106
+ * `translationOutputs` is used when the "Translate fields" instruction is started by a Studio user.
107
+ *
108
+ * It determines the relationships between document paths: Given a document path and a language, into which
109
+ * sibling paths should translations be output.
110
+
111
+ *
112
+ * `translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following:
113
+ *
114
+ * * `documentMember` - the field or array item for a given path; contains the path and its schemaType,
115
+ * * `enclosingType` - the schema type of parent holding the member
116
+ * * `translateFromLanguageId` - the languageId for the language the user want to translate from
117
+ * * `translateToLanguageIds` - all languageIds the user can translate to
118
+ *
119
+ * The function should return a `TranslationOutput[]` array that contains all the paths where translations from
120
+ * documentMember language (translateFromLanguageId) should be output.
121
+ *
122
+ * The function should return `undefined` for all documentMembers that should not be directly translated,
123
+ * or are nested fields under a translated path.
124
+ *
125
+ * ## Default function
126
+ *
127
+ * The default function for `translationOutputs` is configured to be automatically compatible with sanity-plugin-internationalized-array
128
+ * and object types prefixed with "locale".
129
+ *
130
+ * See <link to source for defaultTranslationOutputs> implementation details.
131
+ *
132
+ * ## Example
133
+ * A document has the following document members:
134
+ * * `{path: 'localeObject.en', schemaType: ObjectSchemaType}`
135
+ * * `{path: 'localeObject.en.title', schemaType: StringSchemaType}`
136
+ * * `{path: 'localeObject.de', schemaType: ObjectSchemaType}`,
137
+ * * `{path: 'localeObject.de.title', schemaType: StringSchemaType}`
138
+ *
139
+ * `translationOutputs` for invoked with `translateFromLanguageId` `en`,
140
+ * should only return [{id: 'de', outputPath: 'localeObject.de'}] for the `'localeObject.en'` path,
141
+ * and undefined for all the other members.
142
+ *
143
+ * ### Example implementation
144
+ * ```ts
145
+ * function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds)
146
+ * if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale') && translateFromLanguageId === member.name) {
147
+ * return translateToLanguageIds.map((translateToId) => ({
148
+ * id: translateToId,
149
+ * outputPath: [...member.path.slice(0, -1), translateToId],
150
+ * }))
151
+ * }
152
+ * return undefined
153
+ * }
154
+ * ```
155
+ **/
156
+ translationOutputs?: TranslationOutputsFunction
157
+ }
158
+
159
+ export declare interface Language {
160
+ id: string
161
+ title?: string
162
+ }
163
+
164
+ export declare type LanguageCallback = (
165
+ client: SanityClient,
166
+ selectedLanguageParams: Record<string, unknown>,
167
+ ) => Promise<Language[]>
168
+
27
169
  export declare function SchemaTypeTool(): JSX_2.Element
28
170
 
171
+ export declare interface TranslationConfig {
172
+ /**
173
+ * Config for document types with fields in multiple languages in the same document.
174
+ */
175
+ field?: FieldTranslationConfig
176
+ /**
177
+ * Config for document types with a single language field that determines the language for the whole document.
178
+ */
179
+ document?: DocumentTranslationConfig
180
+ }
181
+
182
+ export declare interface TranslationOutput {
183
+ /** Language id */
184
+ id: string
185
+ outputPath: Path
186
+ }
187
+
188
+ export declare type TranslationOutputsFunction = (
189
+ documentMember: DocumentMember,
190
+ enclosingType: SchemaType,
191
+ translateFromLanguageId: string,
192
+ translateToLanguageIds: string[],
193
+ ) => TranslationOutput[] | undefined
194
+
29
195
  export {}
30
196
 
31
197
  declare module 'sanity' {
@@ -38,13 +204,89 @@ declare module 'sanity' {
38
204
  interface DocumentOptions extends AssistOptions {}
39
205
  interface FileOptions extends AssistOptions {}
40
206
  interface GeopointOptions extends AssistOptions {}
41
- interface ImageOptions extends AssistOptions {
42
- imagePromptField?: string
43
- captionField?: string
207
+ interface ImageOptions {
208
+ aiAssist?: AssistOptions['aiAssist'] & {
209
+ /**
210
+ * When set, an image will be created whenever the `imageInstructionField` is written to by
211
+ * an AI Assist instruction.
212
+ *
213
+ * The value output by AI Assist will be use as an image prompt for an generative image AI.
214
+ *
215
+ * This means that instructions directly for the field or instructions that visit the field when running,
216
+ * will result in the image being changed.
217
+ *
218
+ * `imageInstructionField` must be a child-path relative to the image field, ie:
219
+ * * field
220
+ * * path.to.field
221
+ *
222
+ * ### Example
223
+ * ```ts
224
+ * defineType({
225
+ * type: 'image',
226
+ * name: 'articleImage',
227
+ * fields: [
228
+ * defineField({
229
+ * type: 'text',
230
+ * name: 'imagePrompt',
231
+ * title: 'Image prompt',
232
+ * rows: 2,
233
+ * }),
234
+ * ],
235
+ * options: {
236
+ * aiAssist: {
237
+ * imageInstructionField: 'imagePrompt',
238
+ * }
239
+ * },
240
+ * })
241
+ * ```
242
+ */
243
+ imageInstructionField?: string
244
+ /**
245
+ * When set, an image description will be automatically created for the image.
246
+ *
247
+ * `imageDescriptionField` must be a child-path relative to the image field, ie:
248
+ * * field
249
+ * * path.to.field
250
+ *
251
+ * Whenever the image asset for the field is changed in the Studio,
252
+ * an image description is generated and set into the `imageDescriptionField`.
253
+ *
254
+ * ### Example
255
+ * ```ts
256
+ * defineType({
257
+ * type: 'image',
258
+ * name: 'articleImage',
259
+ * fields: [
260
+ * defineField({
261
+ * type: 'string',
262
+ * name: 'altText',
263
+ * title: 'Alt text',
264
+ * }),
265
+ * ],
266
+ * options: {
267
+ * aiAssist: {
268
+ * imageDescriptionField: 'altText',
269
+ * }
270
+ * },
271
+ * })
272
+ * ```
273
+ */
274
+ imageDescriptionField?: string
275
+ }
44
276
  }
45
277
  interface NumberOptions extends AssistOptions {}
46
278
  interface ObjectOptions extends AssistOptions {}
47
- interface ReferenceBaseOptions extends AssistOptions {}
279
+ interface ReferenceBaseOptions {
280
+ aiAssist?: {
281
+ /** Set to true to disable assistance for this field or type */
282
+ exclude?: boolean
283
+ /**
284
+ * When set, the reference field will allow instructions to be added to it.
285
+ * Should be the name of the embeddings-index where assist will look for contextually relevant documents
286
+ * */
287
+ embeddingsIndex?: string
288
+ }
289
+ }
48
290
  interface SlugOptions extends AssistOptions {}
49
291
  interface StringOptions extends AssistOptions {}
50
292
  interface TextOptions extends AssistOptions {}