@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 +47 -2
- package/dist/index.d.mts +71 -51
- package/dist/index.d.ts +71 -51
- package/dist/index.esm.js +3445 -3434
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +3441 -3430
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3445 -3434
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
- package/src/_lib/fixedListenQuery.ts +1 -1
- package/src/assistInspector/AssistInspector.tsx +1 -1
- package/src/helpers/assistSupported.ts +1 -0
- package/src/helpers/conditionalMembers.test.ts +47 -0
- package/src/helpers/conditionalMembers.ts +7 -3
- package/src/index.ts +2 -5
- package/src/plugin.tsx +19 -10
- package/src/schemas/serialize/schemaUtils.ts +1 -0
- package/src/schemas/serialize/serializeSchema.test.ts +59 -0
- package/src/schemas/serialize/serializeSchema.ts +1 -0
- package/src/translate/FieldTranslationProvider.tsx +5 -2
- package/src/translate/paths.test.ts +41 -0
- package/src/translate/paths.ts +11 -4
- package/src/translate/translateActions.tsx +6 -1
- package/src/translate/types.ts +22 -1
- package/src/types.ts +1 -1
- package/src/useApiClient.ts +8 -4
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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 {
|