@sanity/assist 5.0.4 → 6.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 (131) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +28 -254
  3. package/dist/index.d.ts +322 -410
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +3181 -2649
  6. package/dist/index.js.map +1 -1
  7. package/package.json +38 -78
  8. package/dist/index.cjs +0 -4239
  9. package/dist/index.cjs.map +0 -1
  10. package/dist/index.d.cts +0 -791
  11. package/sanity.json +0 -8
  12. package/src/_lib/connector/ConnectFromRegion.tsx +0 -25
  13. package/src/_lib/connector/ConnectToRegion.tsx +0 -23
  14. package/src/_lib/connector/ConnectorRegion.tsx +0 -24
  15. package/src/_lib/connector/ConnectorsProvider.tsx +0 -20
  16. package/src/_lib/connector/ConnectorsStore.ts +0 -122
  17. package/src/_lib/connector/ConnectorsStoreContext.ts +0 -5
  18. package/src/_lib/connector/helpers.ts +0 -5
  19. package/src/_lib/connector/index.ts +0 -9
  20. package/src/_lib/connector/mapConnectorToLine.ts +0 -83
  21. package/src/_lib/connector/types.ts +0 -56
  22. package/src/_lib/connector/useConnectorsStore.ts +0 -14
  23. package/src/_lib/connector/useRegionRects.ts +0 -142
  24. package/src/_lib/fixedListenQuery.ts +0 -101
  25. package/src/_lib/form/DocumentForm.tsx +0 -201
  26. package/src/_lib/form/constants.ts +0 -1
  27. package/src/_lib/form/helpers.ts +0 -32
  28. package/src/_lib/form/index.ts +0 -1
  29. package/src/_lib/randomKey.ts +0 -29
  30. package/src/_lib/useListeningQuery.ts +0 -62
  31. package/src/_lib/usePrevious.ts +0 -9
  32. package/src/assistConnectors/AssistConnectorsOverlay.tsx +0 -133
  33. package/src/assistConnectors/ConnectorPath.tsx +0 -63
  34. package/src/assistConnectors/draw/arrowPath.ts +0 -9
  35. package/src/assistConnectors/draw/connectorPath.ts +0 -142
  36. package/src/assistConnectors/index.ts +0 -1
  37. package/src/assistDocument/AssistDocumentContext.tsx +0 -51
  38. package/src/assistDocument/AssistDocumentContextProvider.tsx +0 -17
  39. package/src/assistDocument/AssistDocumentInput.tsx +0 -61
  40. package/src/assistDocument/AssistDocumentLayout.tsx +0 -12
  41. package/src/assistDocument/RequestRunInstructionProvider.tsx +0 -61
  42. package/src/assistDocument/components/AssistDocumentForm.tsx +0 -287
  43. package/src/assistDocument/components/AssistTypeContext.tsx +0 -7
  44. package/src/assistDocument/components/FieldRefPreview.tsx +0 -26
  45. package/src/assistDocument/components/InstructionsArrayField.tsx +0 -8
  46. package/src/assistDocument/components/InstructionsArrayInput.tsx +0 -27
  47. package/src/assistDocument/components/SelectedFieldContext.tsx +0 -10
  48. package/src/assistDocument/components/generic/HiddenFieldTitle.tsx +0 -5
  49. package/src/assistDocument/components/helpers.ts +0 -21
  50. package/src/assistDocument/components/instruction/BackToInstructionsLink.tsx +0 -32
  51. package/src/assistDocument/components/instruction/FieldRefInput.tsx +0 -54
  52. package/src/assistDocument/components/instruction/InstructionInput.tsx +0 -89
  53. package/src/assistDocument/components/instruction/InstructionOutputField.tsx +0 -46
  54. package/src/assistDocument/components/instruction/InstructionOutputInput.tsx +0 -206
  55. package/src/assistDocument/components/instruction/PromptInput.tsx +0 -59
  56. package/src/assistDocument/components/instruction/appearance/IconInput.tsx +0 -46
  57. package/src/assistDocument/components/instruction/appearance/InstructionVisibility.tsx +0 -37
  58. package/src/assistDocument/hooks/useAssistDocumentContextValue.tsx +0 -127
  59. package/src/assistDocument/hooks/useDocumentState.ts +0 -6
  60. package/src/assistDocument/hooks/useInstructionToaster.tsx +0 -75
  61. package/src/assistDocument/hooks/useStudioAssistDocument.ts +0 -99
  62. package/src/assistDocument/index.ts +0 -1
  63. package/src/assistFormComponents/AssistField.tsx +0 -63
  64. package/src/assistFormComponents/AssistFormBlock.tsx +0 -31
  65. package/src/assistFormComponents/AssistInlineFormBlock.tsx +0 -13
  66. package/src/assistFormComponents/AssistItem.tsx +0 -21
  67. package/src/assistFormComponents/validation/listItem.tsx +0 -63
  68. package/src/assistFormComponents/validation/validationList.tsx +0 -90
  69. package/src/assistInspector/AssistInspector.tsx +0 -419
  70. package/src/assistInspector/FieldAutocomplete.tsx +0 -146
  71. package/src/assistInspector/InstructionTaskHistoryButton.tsx +0 -262
  72. package/src/assistInspector/constants.ts +0 -1
  73. package/src/assistInspector/helpers.ts +0 -211
  74. package/src/assistInspector/index.ts +0 -27
  75. package/src/assistLayout/AiAssistanceConfigContext.tsx +0 -32
  76. package/src/assistLayout/AiAssistanceConfigProvider.tsx +0 -98
  77. package/src/assistLayout/AssistLayout.tsx +0 -39
  78. package/src/assistLayout/RunInstructionProvider.tsx +0 -278
  79. package/src/assistLayout/fieldRefCache.tsx +0 -34
  80. package/src/assistTypes.ts +0 -83
  81. package/src/components/AssistFeatureBadge.tsx +0 -9
  82. package/src/components/FadeInContent.tsx +0 -40
  83. package/src/components/HideReferenceChangedBannerInput.tsx +0 -25
  84. package/src/components/ImageContext.tsx +0 -85
  85. package/src/components/SafeValueInput.tsx +0 -74
  86. package/src/components/TimeAgo.tsx +0 -18
  87. package/src/constants.ts +0 -20
  88. package/src/fieldActions/PrivateIcon.tsx +0 -20
  89. package/src/fieldActions/assistFieldActions.tsx +0 -320
  90. package/src/fieldActions/customFieldActions.tsx +0 -333
  91. package/src/fieldActions/generateCaptionActions.tsx +0 -77
  92. package/src/fieldActions/generateImageActions.tsx +0 -58
  93. package/src/fieldActions/useUserInput.ts +0 -107
  94. package/src/globals.d.ts +0 -4
  95. package/src/helpers/assistSupported.ts +0 -49
  96. package/src/helpers/conditionalMembers.test.ts +0 -319
  97. package/src/helpers/conditionalMembers.ts +0 -134
  98. package/src/helpers/ids.test.ts +0 -28
  99. package/src/helpers/ids.ts +0 -23
  100. package/src/helpers/misc.ts +0 -25
  101. package/src/helpers/styleguide.ts +0 -24
  102. package/src/helpers/typeUtils.ts +0 -60
  103. package/src/helpers/useAssistSupported.ts +0 -8
  104. package/src/index.ts +0 -26
  105. package/src/onboarding/FirstAssistedPathProvider.tsx +0 -30
  106. package/src/onboarding/InspectorOnboarding.tsx +0 -47
  107. package/src/onboarding/onboardingStore.ts +0 -32
  108. package/src/plugin.tsx +0 -162
  109. package/src/presence/AiFieldPresence.tsx +0 -28
  110. package/src/presence/AssistAvatar.tsx +0 -96
  111. package/src/presence/AssistDocumentPresence.tsx +0 -50
  112. package/src/presence/useAssistPresence.ts +0 -64
  113. package/src/schemas/assistDocumentSchema.tsx +0 -497
  114. package/src/schemas/contextDocumentSchema.tsx +0 -57
  115. package/src/schemas/index.ts +0 -69
  116. package/src/schemas/serialize/SchemTypeTool.tsx +0 -103
  117. package/src/schemas/serialize/schemaUtils.ts +0 -38
  118. package/src/schemas/serialize/serializeSchema.test.ts +0 -819
  119. package/src/schemas/serialize/serializeSchema.ts +0 -224
  120. package/src/schemas/serializedSchemaTypeSchema.ts +0 -60
  121. package/src/schemas/typeDefExtensions.ts +0 -127
  122. package/src/translate/FieldTranslationProvider.tsx +0 -382
  123. package/src/translate/getLanguageParams.ts +0 -26
  124. package/src/translate/languageStore.ts +0 -18
  125. package/src/translate/paths.test.ts +0 -181
  126. package/src/translate/paths.ts +0 -183
  127. package/src/translate/translateActions.tsx +0 -205
  128. package/src/translate/types.ts +0 -197
  129. package/src/types.ts +0 -220
  130. package/src/useApiClient.ts +0 -338
  131. package/v2-incompatible.js +0 -11
@@ -1,183 +0,0 @@
1
- import {extractWithPath} from '@sanity/mutator'
2
- import {
3
- isDocumentSchemaType,
4
- isKeySegment,
5
- type ObjectSchemaType,
6
- type Path,
7
- pathToString,
8
- type SanityDocumentLike,
9
- } from 'sanity'
10
-
11
- import type {DocumentMember, TranslationOutput, TranslationOutputsFunction} from './types'
12
-
13
- export interface FieldLanguageMap {
14
- inputLanguageId: string
15
- inputPath: Path
16
- outputs: TranslationOutput[]
17
- }
18
-
19
- const DEFAULT_MAX_DEPTH = 6
20
- const ABSOLUTE_MAX_DEPTH = 50
21
-
22
- export function getDocumentMembersFlat(
23
- doc: SanityDocumentLike,
24
- schemaType: ObjectSchemaType,
25
- maxDepth = DEFAULT_MAX_DEPTH,
26
- ) {
27
- if (!isDocumentSchemaType(schemaType)) {
28
- console.error(`Schema type is not a document`)
29
- return []
30
- }
31
-
32
- return extractPaths(doc, schemaType, [], Math.min(maxDepth, ABSOLUTE_MAX_DEPTH))
33
- }
34
-
35
- function extractPaths(
36
- doc: SanityDocumentLike,
37
- schemaType: ObjectSchemaType,
38
- path: Path,
39
- maxDepth: number,
40
- ): DocumentMember[] {
41
- if (path.length >= maxDepth) {
42
- return []
43
- }
44
-
45
- return schemaType.fields.reduce<DocumentMember[]>((acc, field) => {
46
- const fieldPath = [...path, field.name]
47
- const fieldSchema = field.type
48
- const {value} = extractWithPath(pathToString(fieldPath), doc)[0] ?? {}
49
- if (!value) {
50
- return acc
51
- }
52
-
53
- const thisFieldWithPath: DocumentMember = {
54
- path: fieldPath,
55
- name: field.name,
56
- schemaType: fieldSchema,
57
- value,
58
- }
59
-
60
- if (fieldSchema.jsonType === 'object') {
61
- const innerFields = extractPaths(doc, fieldSchema, fieldPath, maxDepth)
62
-
63
- return [...acc, thisFieldWithPath, ...innerFields]
64
- } else if (
65
- fieldSchema.jsonType === 'array' &&
66
- fieldSchema.of.length &&
67
- fieldSchema.of.some((item) => 'fields' in item) &&
68
- // no reason to drill into arrays if the item fields will be culled by maxDepth, ie we need 1 extra path headroom
69
- path.length + 1 < maxDepth
70
- ) {
71
- const {value: arrayValue} = extractWithPath(pathToString(fieldPath), doc)[0] ?? {}
72
-
73
- let arrayPaths: DocumentMember[] = []
74
- if ((arrayValue as any)?.length) {
75
- for (const item of arrayValue as any[]) {
76
- const itemPath = [...fieldPath, {_key: item._key}]
77
- let itemSchema = fieldSchema.of.find((t) => t.name === item._type)
78
- if (!item._type) {
79
- itemSchema = fieldSchema.of[0]
80
- console.warn(
81
- 'Array item is missing _type - using the first defined type in the array.of schema',
82
- {
83
- itemPath,
84
- item,
85
- itemSchema,
86
- },
87
- )
88
- }
89
- if (item._key && itemSchema) {
90
- const innerFields = extractPaths(
91
- doc,
92
- itemSchema as ObjectSchemaType,
93
- itemPath,
94
- maxDepth,
95
- )
96
- const arrayMember = {
97
- path: itemPath,
98
- name: item._key,
99
- schemaType: itemSchema,
100
- value: item,
101
- }
102
- arrayPaths = [...arrayPaths, arrayMember, ...innerFields]
103
- }
104
- }
105
- }
106
-
107
- return [...acc, thisFieldWithPath, ...arrayPaths]
108
- }
109
-
110
- return [...acc, thisFieldWithPath]
111
- }, [])
112
- }
113
-
114
- /**
115
- * Default implementation for plugin config `translate.field.translationOutputs`
116
- *
117
- * @see FieldTranslationConfig#translationOutputs
118
- */
119
- export const defaultLanguageOutputs: TranslationOutputsFunction = function (
120
- member,
121
- enclosingType,
122
- translateFromLanguageId,
123
- translateToLanguageIds,
124
- ) {
125
- if (
126
- member.schemaType.jsonType === 'object' &&
127
- member.schemaType.name.startsWith('internationalizedArray')
128
- ) {
129
- const pathEnd = member.path.slice(-1)
130
-
131
- const language = isKeySegment(pathEnd[0]) ? pathEnd[0]._key : null
132
- return language === translateFromLanguageId
133
- ? translateToLanguageIds.map((translateToId) => ({
134
- id: translateToId,
135
- outputPath: [...member.path.slice(0, -1), {_key: translateToId}],
136
- }))
137
- : undefined
138
- }
139
-
140
- if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale')) {
141
- return translateFromLanguageId === member.name
142
- ? translateToLanguageIds.map((translateToId) => ({
143
- id: translateToId,
144
- outputPath: [...member.path.slice(0, -1), translateToId],
145
- }))
146
- : undefined
147
- }
148
-
149
- return undefined
150
- }
151
-
152
- export function getFieldLanguageMap(
153
- documentSchema: ObjectSchemaType,
154
- documentMembers: DocumentMember[],
155
- translateFromLanguageId: string,
156
- outputLanguageIds: string[],
157
- langFn: TranslationOutputsFunction,
158
- ): FieldLanguageMap[] {
159
- const translationMaps: FieldLanguageMap[] = []
160
- for (const member of documentMembers) {
161
- const parentPath = member.path.slice(0, -1)
162
- const enclosingType =
163
- documentMembers.find((m) => pathToString(m.path) === pathToString(parentPath))?.schemaType ??
164
- documentSchema
165
-
166
- const translations = langFn(
167
- member,
168
- enclosingType,
169
- translateFromLanguageId,
170
- outputLanguageIds,
171
- )?.filter((translation) => translation.id !== translateFromLanguageId)
172
-
173
- if (translations) {
174
- translationMaps.push({
175
- inputLanguageId: translateFromLanguageId,
176
- inputPath: member.path,
177
- outputs: translations,
178
- })
179
- }
180
- }
181
-
182
- return translationMaps
183
- }
@@ -1,205 +0,0 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
- import {TranslateIcon} from '@sanity/icons'
3
- import {Box, Spinner} from '@sanity/ui'
4
- import {useMemo, useRef} from 'react'
5
- import {
6
- DocumentFieldAction,
7
- DocumentFieldActionGroup,
8
- DocumentFieldActionItem,
9
- DocumentFieldActionProps,
10
- ObjectSchemaType,
11
- useClient,
12
- } from 'sanity'
13
- import {useDocumentPane} from 'sanity/structure'
14
-
15
- import {useDraftDelayedTask} from '../assistDocument/RequestRunInstructionProvider'
16
- import {useAiAssistanceConfig} from '../assistLayout/AiAssistanceConfigContext'
17
- import {isAssistSupported} from '../helpers/assistSupported'
18
- import {getConditionalMembers} from '../helpers/conditionalMembers'
19
- import {createStyleGuideResolver} from '../helpers/styleguide'
20
- import type {AssistOptions} from '../schemas/typeDefExtensions'
21
- import {API_VERSION_WITH_EXTENDED_TYPES, useApiClient, useTranslate} from '../useApiClient'
22
- import {useFieldTranslation} from './FieldTranslationProvider'
23
-
24
- function node(node: DocumentFieldActionItem | DocumentFieldActionGroup) {
25
- return node
26
- }
27
-
28
- export type TranslateProps = DocumentFieldActionProps & {
29
- documentIsAssistable?: boolean
30
- documentSchemaType?: ObjectSchemaType
31
- }
32
- export const translateActions: DocumentFieldAction = {
33
- name: 'sanity-assist-translate',
34
- useAction(props: TranslateProps) {
35
- const {config, status} = useAiAssistanceConfig()
36
- const apiClient = useApiClient(config?.__customApiClient)
37
- const client = useClient({apiVersion: API_VERSION_WITH_EXTENDED_TYPES})
38
- const {
39
- schemaType: fieldSchemaType,
40
- path,
41
- documentId,
42
- documentSchemaType,
43
- documentIsAssistable,
44
- } = props
45
- const isDocumentLevel = path.length === 0
46
- const readOnly = fieldSchemaType.readOnly === true
47
-
48
- const docTransTypes = config.translate?.document?.documentTypes
49
- const options = fieldSchemaType?.options as AssistOptions | undefined
50
- const addFieldAction = isDocumentLevel || options?.aiAssist?.translateAction
51
-
52
- //All props used here MUST have the same value always, or we break the rules of hooks (conditional hook usage)
53
- const fieldTransEnabled =
54
- addFieldAction &&
55
- documentSchemaType &&
56
- config.translate?.field?.documentTypes?.includes(documentSchemaType.name)
57
- const documentTranslationEnabled =
58
- addFieldAction &&
59
- documentSchemaType &&
60
- ((!docTransTypes && isAssistSupported(fieldSchemaType)) ||
61
- docTransTypes?.includes(documentSchemaType.name))
62
-
63
- // these checks are stable (ie, does not change after mount), so not breaking rules of hooks
64
- if (documentSchemaType && (documentTranslationEnabled || fieldTransEnabled)) {
65
- const {value: documentValue, onChange: documentOnChange, formState} = useDocumentPane()
66
- const docRef = useRef(documentValue)
67
- docRef.current = documentValue
68
- const formStateRef = useRef(formState)
69
- formStateRef.current = formState
70
-
71
- const translationApi = useTranslate(apiClient)
72
- const translate = useDraftDelayedTask({
73
- documentOnChange,
74
- isDocAssistable: documentIsAssistable ?? false,
75
- task: translationApi.translate,
76
- })
77
-
78
- const styleguide = config.translate?.styleguide
79
- const languagePath = config.translate?.document?.languageField
80
-
81
- // if this is true, it is stable, and not breaking rules of hooks
82
- const translateDocumentAction = useMemo(() => {
83
- if (!languagePath || !documentTranslationEnabled) {
84
- return undefined
85
- }
86
- const title = path.length ? `Translate` : `Translate document`
87
- return node({
88
- type: 'action',
89
- icon: translationApi.loading
90
- ? () => (
91
- <Box style={{height: 17}}>
92
- <Spinner style={{transform: 'translateY(6px)'}} />
93
- </Box>
94
- )
95
- : TranslateIcon,
96
- title,
97
- onAction: () => {
98
- if (translationApi.loading || !languagePath || !documentId) {
99
- return
100
- }
101
- translate({
102
- languagePath,
103
- translatePath: path,
104
- styleguide: createStyleGuideResolver(styleguide, {
105
- client,
106
- documentId,
107
- schemaType: documentSchemaType,
108
- }),
109
- documentId: documentId ?? '',
110
- conditionalMembers: formStateRef.current
111
- ? getConditionalMembers(formStateRef.current)
112
- : [],
113
- })
114
- },
115
- renderAsButton: true,
116
- disabled: translationApi.loading || readOnly,
117
- })
118
- }, [
119
- languagePath,
120
- translate,
121
- styleguide,
122
- documentId,
123
- translationApi.loading,
124
- documentTranslationEnabled,
125
- path,
126
- readOnly,
127
- client,
128
- documentSchemaType,
129
- ])
130
- const fieldTranslate = useFieldTranslation()
131
- const openFieldTranslation = useDraftDelayedTask({
132
- documentOnChange,
133
- isDocAssistable: documentIsAssistable ?? false,
134
- task: fieldTranslate.openFieldTranslation,
135
- })
136
-
137
- const maxDepth = config.translate?.field?.maxPathDepth
138
- const translateFieldsAction = useMemo(
139
- () =>
140
- fieldTransEnabled
141
- ? node({
142
- type: 'action',
143
- icon: fieldTranslate.translationLoading
144
- ? () => (
145
- <Box style={{height: 17}}>
146
- <Spinner style={{transform: 'translateY(6px)'}} />
147
- </Box>
148
- )
149
- : TranslateIcon,
150
- title: `Translate fields...`,
151
- onAction: () => {
152
- if (fieldTranslate.translationLoading || !documentId) {
153
- return
154
- }
155
- if (formStateRef.current) {
156
- getConditionalMembers(formStateRef.current)
157
- }
158
- openFieldTranslation({
159
- document: {
160
- ...docRef.current,
161
- _id: documentId,
162
- },
163
- documentSchema: documentSchemaType,
164
- translatePath: path,
165
- conditionalMembers: formStateRef.current
166
- ? getConditionalMembers(formStateRef.current, maxDepth)
167
- : [],
168
- })
169
- },
170
- renderAsButton: true,
171
- disabled: fieldTranslate.translationLoading || readOnly,
172
- })
173
- : undefined,
174
- [
175
- openFieldTranslation,
176
- documentSchemaType,
177
- documentId,
178
- fieldTranslate.translationLoading,
179
- fieldTransEnabled,
180
- path,
181
- readOnly,
182
- maxDepth,
183
- ],
184
- )
185
-
186
- // eslint-disable-next-line react-hooks/rules-of-hooks
187
- return useMemo(() => {
188
- if (!status?.initialized) {
189
- return undefined as unknown as DocumentFieldActionItem
190
- }
191
- return node({
192
- type: 'group',
193
- icon: () => null,
194
- title: 'Translation',
195
- children: [translateDocumentAction, translateFieldsAction].filter(
196
- (c): c is DocumentFieldActionItem => !!c,
197
- ),
198
- expanded: true,
199
- })
200
- }, [translateDocumentAction, translateFieldsAction, status])
201
- }
202
- // works but not supported by types
203
- return undefined as unknown as DocumentFieldActionItem
204
- },
205
- }
@@ -1,197 +0,0 @@
1
- import {ObjectSchemaType, Path, SanityClient, SchemaType} from 'sanity'
2
-
3
- export interface Language {
4
- id: string
5
- title?: string
6
- }
7
-
8
- export interface DocumentMember {
9
- schemaType: SchemaType
10
- path: Path
11
- name: string
12
- value: unknown
13
- }
14
-
15
- export interface TranslationOutput {
16
- /** Language id */
17
- id: string
18
- outputPath: Path
19
- }
20
-
21
- export type TranslationOutputsFunction = (
22
- documentMember: DocumentMember,
23
- enclosingType: SchemaType,
24
- translateFromLanguageId: string,
25
- translateToLanguageIds: string[],
26
- ) => TranslationOutput[] | undefined
27
-
28
- export type LanguageCallback = (
29
- client: SanityClient,
30
- selectedLanguageParams: Record<string, unknown>,
31
- ) => Promise<Language[]>
32
-
33
- export interface FieldTranslationConfig {
34
- /**
35
- * `documentTypes` should be an array of strings where each entry must match a name from your document schemas.
36
- *
37
- * If defined, matching document will get a "Translate fields" instruction added.
38
- **/
39
- documentTypes?: string[]
40
-
41
- /**
42
- *
43
- * Used for display strings in the Studio, and to determine languages for field level translations
44
- *
45
- * If the studio is using the sanity-plugin-internationalized-array plugin, this
46
- * should be set to the same configuration.
47
- */
48
- languages: Language[] | LanguageCallback
49
-
50
- /**
51
- * API version for client passed to LanguageCallback for languages
52
- * https://www.sanity.io/docs/api-versioning
53
- * @defaultValue '2022-11-27'
54
- */
55
- apiVersion?: string
56
-
57
- /**
58
- * Specify fields that should be available in the languages callback:
59
- * ```tsx
60
- * {
61
- * select: {
62
- * markets: 'markets'
63
- * },
64
- * languages: (client, {markets}) =>
65
- * client.fetch('*[_type == "language" && market in $markets]{id,title}', {markets})
66
- * }
67
- * ```
68
- *
69
- * If the studio is using the sanity-plugin-internationalized-array plugin, this
70
- * should be set to the same configuration.
71
- */
72
- selectLanguageParams?: Record<string, string>
73
-
74
- /**
75
- * `translationOutputs` is used when the "Translate fields" instruction is started by a Studio user.
76
- *
77
- * It determines the relationships between document paths: Given a document path and a language, into which
78
- * sibling paths should translations be output.
79
- *
80
- * `translationOutputs` is invoked once per path in the document (limited to a depth of 6), with the following:
81
- *
82
- * * `documentMember` - the field or array item for a given path; contains the path and its schemaType,
83
- * * `enclosingType` - the schema type of parent holding the member
84
- * * `translateFromLanguageId` - the languageId for the language the user want to translate from
85
- * * `translateToLanguageIds` - all languageIds the user can translate to
86
- *
87
- * The function should return a `TranslationOutput[]` array that contains all the paths where translations from
88
- * documentMember language (translateFromLanguageId) should be output.
89
- *
90
- * The function should return `undefined` for all documentMembers that should not be directly translated,
91
- * or are nested fields under a translated path.
92
- *
93
- * ## Default function
94
- *
95
- * The default function for `translationOutputs` is configured to be automatically compatible with sanity-plugin-internationalized-array
96
- * and object types prefixed with "locale".
97
- *
98
- * See <link to source for defaultTranslationOutputs> implementation details.
99
- *
100
- * ## Example
101
- * A document has the following document members:
102
- * * `{path: 'localeObject.en', schemaType: ObjectSchemaType}`
103
- * * `{path: 'localeObject.en.title', schemaType: StringSchemaType}`
104
- * * `{path: 'localeObject.de', schemaType: ObjectSchemaType}`,
105
- * * `{path: 'localeObject.de.title', schemaType: StringSchemaType}`
106
- *
107
- * `translationOutputs` for invoked with `translateFromLanguageId` `en`,
108
- * should only return [{id: 'de', outputPath: 'localeObject.de'}] for the `'localeObject.en'` path,
109
- * and undefined for all the other members.
110
- *
111
- * ### Example implementation
112
- * ```ts
113
- * function translationOutputs(member, enclosingType, translateFromLanguageId, translateToLanguageIds)
114
- * if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale') && translateFromLanguageId === member.name) {
115
- * return translateToLanguageIds.map((translateToId) => ({
116
- * id: translateToId,
117
- * outputPath: [...member.path.slice(0, -1), translateToId],
118
- * }))
119
- * }
120
- * return undefined
121
- * }
122
- * ```
123
- *
124
- * @see #maxPathDepth
125
- **/
126
- translationOutputs?: TranslationOutputsFunction
127
-
128
- /**
129
- * The max depth for document paths AI Assist will translate.
130
- *
131
- * Depth is based on field path segments:
132
- * - `title` has depth 1
133
- * - `array[_key="no"].title` has depth 3
134
- *
135
- * Be careful not to set this too high in studios with recursive document schemas, as it could have
136
- * negative impact on performance.
137
- *
138
- * Default: 6
139
- */
140
- maxPathDepth?: number
141
- }
142
-
143
- export interface DocumentTranslationConfig {
144
- /**
145
- * Path to language field in documents. Can be a hidden field.
146
- * For instance: 'config.language'
147
- *
148
- * For projects that use the `@sanity/document-internationalization` plugin,
149
- * this should be the same as `languageField` config for that plugin.
150
- *
151
- * Default: 'language'
152
- */
153
- languageField: string
154
-
155
- /**
156
- * `documentTypes` should be an array of strings where each entry must match a name from your document schemas.
157
- *
158
- * If defined, this property will add a translate instruction to these document types.
159
- * If undefined, the instruction will be added to all documents with aiAssistance enabled and a field matching `documentLanguageField` config.
160
- *
161
- * Documents with translation support will get a "Translate document>" instruction added.
162
- **/
163
- documentTypes?: string[]
164
- }
165
-
166
- export interface TranslateStyleguideContext {
167
- documentId: string
168
- schemaType: ObjectSchemaType
169
- client: SanityClient
170
- /**
171
- * Only provided for field translations
172
- */
173
- translatePath?: Path
174
- }
175
-
176
- export type TranslateStyleguide =
177
- | string
178
- | ((context: TranslateStyleguideContext) => Promise<string>)
179
-
180
- export interface TranslationConfig {
181
- /**
182
- * Config for document types with fields in multiple languages in the same document.
183
- */
184
- field?: FieldTranslationConfig
185
- /**
186
- * Config for document types with a single language field that determines the language for the whole document.
187
- */
188
- document?: DocumentTranslationConfig
189
- /**
190
- * A "style guide" that can be used to provide guidance on how to translate content.
191
- * Will be passed to the LLM - ergo this is only a guide and the model _may_ not
192
- * always follow it to the letter.
193
- *
194
- * When providing a function, consider caching the results of any async operation; it will invoked every time translate runs
195
- */
196
- styleguide?: TranslateStyleguide
197
- }