@sanity/assist 3.1.1 → 3.2.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/README.md CHANGED
@@ -31,6 +31,7 @@
31
31
  - [What it solves](#what-ai-assist-field-level-translations-solves)
32
32
  - [Configure](#configure-field-translations)
33
33
  - [Adding translation actions to fields](#adding-translation-actions-to-fields)
34
+ - [Translation style guide](#translation-style-guide)
34
35
  - [License](#license)
35
36
  - [Develop \& test](#develop--test)
36
37
  - [Release new version](#release-new-version)
@@ -113,6 +114,7 @@ Note that instructions run using the permissions of the user invoking it, so onl
113
114
  themselves can edit can be changed by the instruction instance.
114
115
 
115
116
  ### Conditional user access
117
+
116
118
  To limit which users can see the AI Assist actions in the Studio, use a custom-plugin after `assist()`
117
119
  that filters out the inspector and actions, based on user properties:
118
120
 
@@ -141,7 +143,6 @@ export default defineConfig({
141
143
  },
142
144
  },
143
145
  ],
144
-
145
146
  })
146
147
 
147
148
  const ALLOWED_ROLES = ['administrator']
@@ -591,6 +592,28 @@ defineType({
591
592
 
592
593
  **If your schema is not using either of these structures**, refer to the section on [Custom language fields](#custom-language-fields).
593
594
 
595
+ #### Note on document schema depth
596
+ By default, field level translations will translate 6 "path-segments" deep.
597
+
598
+ Depth is based on field path segments like so:
599
+ - `title` has depth 1
600
+ - `array[_key="no"].title` has depth 3
601
+
602
+ If this is not sufficient for your document types, use `maxPathDepth`:
603
+
604
+ ```ts
605
+ assist({
606
+ translate: {
607
+ field: {
608
+ maxPathDepth: 12
609
+ },
610
+ },
611
+ })
612
+ ```
613
+
614
+ Be careful not to set this too high in studios with recursive document schemas, as it could have negative impact on performance.
615
+ maxPathDepth is hard-capped to 50.
616
+
594
617
  ### Loading field languages
595
618
 
596
619
  Languages must be an array of objects with an id and title.
@@ -749,7 +772,7 @@ function translationOutputs(
749
772
  }
750
773
  ```
751
774
 
752
- ### Full field translation configuration example
775
+ [### Full field translation configuration example]()
753
776
 
754
777
  ```ts
755
778
  assist({
@@ -817,6 +840,28 @@ defineField({
817
840
  })
818
841
  ```
819
842
 
843
+ ## Translation style guide
844
+
845
+ In some cases you might want/need the translator to follow a certain style guide - for
846
+ instance you might tell it not to translate certain words, or be more formal or casual.
847
+ To configure this you can pass a `styleguide` property under the translation
848
+ configuration:
849
+
850
+ ```ts
851
+ assist({
852
+ translate: {
853
+ styleguide: `Be extremely formal and precise. Translate as if you are Spock from Star Trek.`,
854
+ },
855
+ })
856
+ ```
857
+
858
+ The style guide is currently limited to 2000 characters, and the translation might get
859
+ slower the longer your style guide is. If the provided string is longer than the limit,
860
+ the plugin will throw upon studio startup.
861
+
862
+ Note that this is currently only available on a global level - it can not be defined
863
+ per-field for now.
864
+
820
865
  ## Caveats
821
866
 
822
867
  Large Language Models (LLMs) are a new technology. Constraints and limitations are still being explored,
package/dist/index.d.mts CHANGED
@@ -5,7 +5,7 @@ import {PortableTextBlock} from '@portabletext/types'
5
5
  import {PortableTextMarkDefinition} from '@portabletext/types'
6
6
  import {PortableTextSpan} from '@portabletext/types'
7
7
  import {SanityClient} from 'sanity'
8
- import {SanityClient as SanityClient_2} from '@sanity/client'
8
+ import type {SanityClient as SanityClient_2} from '@sanity/client'
9
9
  import {SchemaType} from 'sanity'
10
10
 
11
11
  export declare const assist: Plugin_2<void | AssistPluginConfig>
@@ -129,57 +129,71 @@ export declare interface FieldTranslationConfig {
129
129
  */
130
130
  selectLanguageParams?: Record<string, string>
131
131
  /**
132
- * `translationOutputs` is used when the "Translate fields" instruction is started by a Studio user.
133
- *
134
- * It determines the relationships between document paths: Given a document path and a language, into which
135
- * sibling paths should translations be output.
136
-
137
- *
138
- * `translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following:
139
- *
140
- * * `documentMember` - the field or array item for a given path; contains the path and its schemaType,
141
- * * `enclosingType` - the schema type of parent holding the member
142
- * * `translateFromLanguageId` - the languageId for the language the user want to translate from
143
- * * `translateToLanguageIds` - all languageIds the user can translate to
144
- *
145
- * The function should return a `TranslationOutput[]` array that contains all the paths where translations from
146
- * documentMember language (translateFromLanguageId) should be output.
147
- *
148
- * The function should return `undefined` for all documentMembers that should not be directly translated,
149
- * or are nested fields under a translated path.
150
- *
151
- * ## Default function
152
- *
153
- * The default function for `translationOutputs` is configured to be automatically compatible with sanity-plugin-internationalized-array
154
- * and object types prefixed with "locale".
155
- *
156
- * See <link to source for defaultTranslationOutputs> implementation details.
157
- *
158
- * ## Example
159
- * A document has the following document members:
160
- * * `{path: 'localeObject.en', schemaType: ObjectSchemaType}`
161
- * * `{path: 'localeObject.en.title', schemaType: StringSchemaType}`
162
- * * `{path: 'localeObject.de', schemaType: ObjectSchemaType}`,
163
- * * `{path: 'localeObject.de.title', schemaType: StringSchemaType}`
164
- *
165
- * `translationOutputs` for invoked with `translateFromLanguageId` `en`,
166
- * should only return [{id: 'de', outputPath: 'localeObject.de'}] for the `'localeObject.en'` path,
167
- * and undefined for all the other members.
168
- *
169
- * ### Example implementation
170
- * ```ts
171
- * function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds)
172
- * if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale') && translateFromLanguageId === member.name) {
173
- * return translateToLanguageIds.map((translateToId) => ({
174
- * id: translateToId,
175
- * outputPath: [...member.path.slice(0, -1), translateToId],
176
- * }))
177
- * }
178
- * return undefined
179
- * }
180
- * ```
181
- **/
132
+ * `translationOutputs` is used when the "Translate fields" instruction is started by a Studio user.
133
+ *
134
+ * It determines the relationships between document paths: Given a document path and a language, into which
135
+ * sibling paths should translations be output.
136
+ *
137
+ * `translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following:
138
+ *
139
+ * * `documentMember` - the field or array item for a given path; contains the path and its schemaType,
140
+ * * `enclosingType` - the schema type of parent holding the member
141
+ * * `translateFromLanguageId` - the languageId for the language the user want to translate from
142
+ * * `translateToLanguageIds` - all languageIds the user can translate to
143
+ *
144
+ * The function should return a `TranslationOutput[]` array that contains all the paths where translations from
145
+ * documentMember language (translateFromLanguageId) should be output.
146
+ *
147
+ * The function should return `undefined` for all documentMembers that should not be directly translated,
148
+ * or are nested fields under a translated path.
149
+ *
150
+ * ## Default function
151
+ *
152
+ * The default function for `translationOutputs` is configured to be automatically compatible with sanity-plugin-internationalized-array
153
+ * and object types prefixed with "locale".
154
+ *
155
+ * See <link to source for defaultTranslationOutputs> implementation details.
156
+ *
157
+ * ## Example
158
+ * A document has the following document members:
159
+ * * `{path: 'localeObject.en', schemaType: ObjectSchemaType}`
160
+ * * `{path: 'localeObject.en.title', schemaType: StringSchemaType}`
161
+ * * `{path: 'localeObject.de', schemaType: ObjectSchemaType}`,
162
+ * * `{path: 'localeObject.de.title', schemaType: StringSchemaType}`
163
+ *
164
+ * `translationOutputs` for invoked with `translateFromLanguageId` `en`,
165
+ * should only return [{id: 'de', outputPath: 'localeObject.de'}] for the `'localeObject.en'` path,
166
+ * and undefined for all the other members.
167
+ *
168
+ * ### Example implementation
169
+ * ```ts
170
+ * function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds)
171
+ * if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale') && translateFromLanguageId === member.name) {
172
+ * return translateToLanguageIds.map((translateToId) => ({
173
+ * id: translateToId,
174
+ * outputPath: [...member.path.slice(0, -1), translateToId],
175
+ * }))
176
+ * }
177
+ * return undefined
178
+ * }
179
+ * ```
180
+ *
181
+ * @see #maxPathDepth
182
+ **/
182
183
  translationOutputs?: TranslationOutputsFunction
184
+ /**
185
+ * The max depth for document paths AI Assist will translate.
186
+ *
187
+ * Depth is based on field path segments:
188
+ * - `title` has depth 1
189
+ * - `array[_key="no"].title` has depth 3
190
+ *
191
+ * Be careful not to set this too high in studios with recursive document schemas, as it could have
192
+ * negative impact on performance.
193
+ *
194
+ * Default: 6
195
+ */
196
+ maxPathDepth?: number
183
197
  }
184
198
 
185
199
  declare type InlinePromptBlock = PortableTextSpan | FieldRef | UserInputBlock | ContextBlock
@@ -250,6 +264,12 @@ export declare interface TranslationConfig {
250
264
  * Config for document types with a single language field that determines the language for the whole document.
251
265
  */
252
266
  document?: DocumentTranslationConfig
267
+ /**
268
+ * A "style guide" that can be used to provide guidance on how to translate content.
269
+ * Will be passed to the LLM - ergo this is only a guide and the model _may_ not
270
+ * always follow it to the letter.
271
+ */
272
+ styleguide?: string
253
273
  }
254
274
 
255
275
  export declare interface TranslationOutput {
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ import {PortableTextBlock} from '@portabletext/types'
5
5
  import {PortableTextMarkDefinition} from '@portabletext/types'
6
6
  import {PortableTextSpan} from '@portabletext/types'
7
7
  import {SanityClient} from 'sanity'
8
- import {SanityClient as SanityClient_2} from '@sanity/client'
8
+ import type {SanityClient as SanityClient_2} from '@sanity/client'
9
9
  import {SchemaType} from 'sanity'
10
10
 
11
11
  export declare const assist: Plugin_2<void | AssistPluginConfig>
@@ -129,57 +129,71 @@ export declare interface FieldTranslationConfig {
129
129
  */
130
130
  selectLanguageParams?: Record<string, string>
131
131
  /**
132
- * `translationOutputs` is used when the "Translate fields" instruction is started by a Studio user.
133
- *
134
- * It determines the relationships between document paths: Given a document path and a language, into which
135
- * sibling paths should translations be output.
136
-
137
- *
138
- * `translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following:
139
- *
140
- * * `documentMember` - the field or array item for a given path; contains the path and its schemaType,
141
- * * `enclosingType` - the schema type of parent holding the member
142
- * * `translateFromLanguageId` - the languageId for the language the user want to translate from
143
- * * `translateToLanguageIds` - all languageIds the user can translate to
144
- *
145
- * The function should return a `TranslationOutput[]` array that contains all the paths where translations from
146
- * documentMember language (translateFromLanguageId) should be output.
147
- *
148
- * The function should return `undefined` for all documentMembers that should not be directly translated,
149
- * or are nested fields under a translated path.
150
- *
151
- * ## Default function
152
- *
153
- * The default function for `translationOutputs` is configured to be automatically compatible with sanity-plugin-internationalized-array
154
- * and object types prefixed with "locale".
155
- *
156
- * See <link to source for defaultTranslationOutputs> implementation details.
157
- *
158
- * ## Example
159
- * A document has the following document members:
160
- * * `{path: 'localeObject.en', schemaType: ObjectSchemaType}`
161
- * * `{path: 'localeObject.en.title', schemaType: StringSchemaType}`
162
- * * `{path: 'localeObject.de', schemaType: ObjectSchemaType}`,
163
- * * `{path: 'localeObject.de.title', schemaType: StringSchemaType}`
164
- *
165
- * `translationOutputs` for invoked with `translateFromLanguageId` `en`,
166
- * should only return [{id: 'de', outputPath: 'localeObject.de'}] for the `'localeObject.en'` path,
167
- * and undefined for all the other members.
168
- *
169
- * ### Example implementation
170
- * ```ts
171
- * function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds)
172
- * if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale') && translateFromLanguageId === member.name) {
173
- * return translateToLanguageIds.map((translateToId) => ({
174
- * id: translateToId,
175
- * outputPath: [...member.path.slice(0, -1), translateToId],
176
- * }))
177
- * }
178
- * return undefined
179
- * }
180
- * ```
181
- **/
132
+ * `translationOutputs` is used when the "Translate fields" instruction is started by a Studio user.
133
+ *
134
+ * It determines the relationships between document paths: Given a document path and a language, into which
135
+ * sibling paths should translations be output.
136
+ *
137
+ * `translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following:
138
+ *
139
+ * * `documentMember` - the field or array item for a given path; contains the path and its schemaType,
140
+ * * `enclosingType` - the schema type of parent holding the member
141
+ * * `translateFromLanguageId` - the languageId for the language the user want to translate from
142
+ * * `translateToLanguageIds` - all languageIds the user can translate to
143
+ *
144
+ * The function should return a `TranslationOutput[]` array that contains all the paths where translations from
145
+ * documentMember language (translateFromLanguageId) should be output.
146
+ *
147
+ * The function should return `undefined` for all documentMembers that should not be directly translated,
148
+ * or are nested fields under a translated path.
149
+ *
150
+ * ## Default function
151
+ *
152
+ * The default function for `translationOutputs` is configured to be automatically compatible with sanity-plugin-internationalized-array
153
+ * and object types prefixed with "locale".
154
+ *
155
+ * See <link to source for defaultTranslationOutputs> implementation details.
156
+ *
157
+ * ## Example
158
+ * A document has the following document members:
159
+ * * `{path: 'localeObject.en', schemaType: ObjectSchemaType}`
160
+ * * `{path: 'localeObject.en.title', schemaType: StringSchemaType}`
161
+ * * `{path: 'localeObject.de', schemaType: ObjectSchemaType}`,
162
+ * * `{path: 'localeObject.de.title', schemaType: StringSchemaType}`
163
+ *
164
+ * `translationOutputs` for invoked with `translateFromLanguageId` `en`,
165
+ * should only return [{id: 'de', outputPath: 'localeObject.de'}] for the `'localeObject.en'` path,
166
+ * and undefined for all the other members.
167
+ *
168
+ * ### Example implementation
169
+ * ```ts
170
+ * function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds)
171
+ * if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale') && translateFromLanguageId === member.name) {
172
+ * return translateToLanguageIds.map((translateToId) => ({
173
+ * id: translateToId,
174
+ * outputPath: [...member.path.slice(0, -1), translateToId],
175
+ * }))
176
+ * }
177
+ * return undefined
178
+ * }
179
+ * ```
180
+ *
181
+ * @see #maxPathDepth
182
+ **/
182
183
  translationOutputs?: TranslationOutputsFunction
184
+ /**
185
+ * The max depth for document paths AI Assist will translate.
186
+ *
187
+ * Depth is based on field path segments:
188
+ * - `title` has depth 1
189
+ * - `array[_key="no"].title` has depth 3
190
+ *
191
+ * Be careful not to set this too high in studios with recursive document schemas, as it could have
192
+ * negative impact on performance.
193
+ *
194
+ * Default: 6
195
+ */
196
+ maxPathDepth?: number
183
197
  }
184
198
 
185
199
  declare type InlinePromptBlock = PortableTextSpan | FieldRef | UserInputBlock | ContextBlock
@@ -250,6 +264,12 @@ export declare interface TranslationConfig {
250
264
  * Config for document types with a single language field that determines the language for the whole document.
251
265
  */
252
266
  document?: DocumentTranslationConfig
267
+ /**
268
+ * A "style guide" that can be used to provide guidance on how to translate content.
269
+ * Will be passed to the LLM - ergo this is only a guide and the model _may_ not
270
+ * always follow it to the letter.
271
+ */
272
+ styleguide?: string
253
273
  }
254
274
 
255
275
  export declare interface TranslationOutput {