@braze/sanity-plugin-ai-translation 0.2.1 → 0.2.3

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/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ArrayDefinition } from "sanity";
2
2
  import { FieldDefinitionBase } from "sanity";
3
3
  import { Plugin as Plugin_2 } from "sanity";
4
+ import type { SanityDocument } from "sanity";
4
5
  import { WidenInitialValue } from "sanity";
5
6
  import { WidenValidation } from "sanity";
6
7
 
@@ -12,13 +13,215 @@ import { WidenValidation } from "sanity";
12
13
  */
13
14
  export declare const aiTranslationPlugin: Plugin_2<PluginConfig>;
14
15
 
16
+ /**
17
+ * Type-erased version of FieldTransform for use in collections.
18
+ * Uses `any` to allow variance - this is necessary because function parameters
19
+ * are contravariant, and using `unknown` would prevent assigning specific
20
+ * transforms like `FieldTransform<VTTCaption[], TranslatableVTT>` to the array.
21
+ *
22
+ * This type is used internally by the plugin for storing transforms
23
+ * in arrays and registries where the specific types aren't known.
24
+ *
25
+ * @public
26
+ *
27
+ * @example Usage in plugin config
28
+ * ```typescript
29
+ * const config: PluginConfig = {
30
+ * // ... other config
31
+ * fieldTransforms: [
32
+ * vttTransform, // FieldTransform<VTTCaption[], TranslatableVTT>
33
+ * richTextTransform, // FieldTransform<PortableTextBlock[], TranslatableRichText>
34
+ * customTransform, // FieldTransform<CustomType, TranslatableCustom>
35
+ * ] // All coerced to AnyFieldTransform[]
36
+ * }
37
+ * ```
38
+ */
39
+ export declare type AnyFieldTransform = FieldTransform<any, any>;
40
+
41
+ /**
42
+ * Criteria for matching fields that should be transformed.
43
+ * @public
44
+ * @typeParam TOriginal - Type of the original field value
45
+ */
46
+ export declare interface FieldMatcher<TOriginal = unknown> {
47
+ /**
48
+ * Schema type name to match (e.g., 'video')
49
+ */
50
+ schemaType?: string;
51
+ /**
52
+ * Field name to match (e.g., 'captions')
53
+ */
54
+ fieldName?: string;
55
+ /**
56
+ * Full field path pattern to match (e.g., 'captions[].text' or RegExp)
57
+ */
58
+ fieldPath?: string | RegExp;
59
+ /**
60
+ * Optional function to match based on field value.
61
+ * This is called after other matcher criteria pass (or if no other criteria specified).
62
+ * Useful for detecting field types by examining their content (e.g., Portable Text detection).
63
+ * @param value - The field value to examine
64
+ * @param context - Transform context with field path, schema type, and document
65
+ * @returns true if this field should be transformed, false otherwise
66
+ */
67
+ valueMatcher?: (value: TOriginal, context: TransformContext) => boolean;
68
+ }
69
+
70
+ /**
71
+ * Transform definition for custom field handling.
72
+ *
73
+ * Transforms allow you to convert complex field values to simpler formats
74
+ * for translation, then restore the original structure after translation.
75
+ *
76
+ * @public
77
+ * @typeParam TOriginal - Type of the original field value
78
+ * @typeParam TTranslatable - Type of the translatable format (what gets sent to translation)
79
+ *
80
+ * @example Creating a type-safe transform for video captions
81
+ * ```typescript
82
+ * interface VTTCaption {
83
+ * startTime: number;
84
+ * endTime: number;
85
+ * text: string;
86
+ * }
87
+ *
88
+ * interface TranslatableCaption {
89
+ * _type: 'translatable.vtt';
90
+ * vtt: string;
91
+ * }
92
+ *
93
+ * const vttTransform: FieldTransform<VTTCaption[], TranslatableCaption> = {
94
+ * matcher: {
95
+ * schemaType: 'video',
96
+ * fieldName: 'captions',
97
+ * },
98
+ * toTranslatable: (captions) => ({
99
+ * _type: 'translatable.vtt',
100
+ * vtt: captions.map(c => `${c.startTime} --> ${c.endTime}\n${c.text}`).join('\n\n')
101
+ * }),
102
+ * fromTranslatable: (translated, original) => {
103
+ * // Parse VTT and restore original structure with translated text
104
+ * const lines = translated.vtt.split('\n\n');
105
+ * return original.map((caption, i) => ({
106
+ * ...caption,
107
+ * text: lines[i]?.split('\n')[1] || caption.text
108
+ * }));
109
+ * }
110
+ * };
111
+ * ```
112
+ */
113
+ export declare interface FieldTransform<
114
+ TOriginal = unknown,
115
+ TTranslatable = unknown,
116
+ > {
117
+ /**
118
+ * Criteria for matching fields that should be transformed
119
+ */
120
+ matcher: FieldMatcher<TOriginal>;
121
+ /**
122
+ * Transform complex field value to translatable format.
123
+ * This method is called before translation to simplify the field structure.
124
+ *
125
+ * @param value - Original field value (typed as TOriginal)
126
+ * @param context - Transform context with field path, schema type, and document
127
+ * @returns Transformed value for translation (typed as TTranslatable)
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * toTranslatable: (value: CustomField[], context) => {
132
+ * return {
133
+ * _type: 'translatable.custom',
134
+ * text: value.map(item => item.content).join('\n'),
135
+ * _metadata: value.map(item => ({ id: item.id }))
136
+ * };
137
+ * }
138
+ * ```
139
+ */
140
+ toTranslatable: (
141
+ value: TOriginal,
142
+ context: TransformContext,
143
+ ) => TTranslatable;
144
+ /**
145
+ * Transform translated value back to original format.
146
+ * This method is called after translation to restore the original structure
147
+ * while preserving the translated content.
148
+ *
149
+ * @param value - Translated value (typed as TTranslatable)
150
+ * @param originalValue - Original value before translation (typed as TOriginal)
151
+ * @param context - Transform context with field path, schema type, and document
152
+ * @returns Restored value with translated content (must match TOriginal type)
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * fromTranslatable: (translated: TranslatableCustom, original: CustomField[], context) => {
157
+ * const translatedLines = translated.text.split('\n');
158
+ * return original.map((item, index) => ({
159
+ * ...item, // Preserve original structure and metadata
160
+ * content: translatedLines[index] || item.content // Apply translated text
161
+ * }));
162
+ * }
163
+ * ```
164
+ */
165
+ fromTranslatable: (
166
+ value: TTranslatable,
167
+ originalValue: TOriginal,
168
+ context: TransformContext,
169
+ ) => TOriginal;
170
+ }
171
+
172
+ /**
173
+ * Helper type to infer the original type from a FieldTransform.
174
+ * Useful for type checking and creating wrapper functions.
175
+ *
176
+ * @public
177
+ * @typeParam T - The FieldTransform type to extract from
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * const myTransform: FieldTransform<MyType, TranslatableType> = { ... };
182
+ *
183
+ * // Inferred type will be MyType
184
+ * type OriginalType = InferOriginalType<typeof myTransform>;
185
+ *
186
+ * // Use in function signature
187
+ * function processOriginal(value: InferOriginalType<typeof myTransform>) {
188
+ * // value is typed as MyType
189
+ * }
190
+ * ```
191
+ */
192
+ export declare type InferOriginalType<T> =
193
+ T extends FieldTransform<infer O, unknown> ? O : never;
194
+
195
+ /**
196
+ * Helper type to infer the translatable type from a FieldTransform.
197
+ * Useful for type checking intermediate translation formats.
198
+ *
199
+ * @public
200
+ * @typeParam T - The FieldTransform type to extract from
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * const myTransform: FieldTransform<MyType, TranslatableType> = { ... };
205
+ *
206
+ * // Inferred type will be TranslatableType
207
+ * type TransType = InferTranslatableType<typeof myTransform>;
208
+ *
209
+ * // Use in function signature
210
+ * function processTranslatable(value: InferTranslatableType<typeof myTransform>) {
211
+ * // value is typed as TranslatableType
212
+ * }
213
+ * ```
214
+ */
215
+ export declare type InferTranslatableType<T> =
216
+ T extends FieldTransform<unknown, infer R> ? R : never;
217
+
15
218
  /**
16
219
  *
17
220
  * Configuration for the Sanity AI Translation plugin.
18
221
  * @public
19
222
  *
20
223
  */
21
- declare interface PluginConfig {
224
+ export declare interface PluginConfig {
22
225
  supportedLanguages: Array<{
23
226
  id: string;
24
227
  title: string;
@@ -30,12 +233,72 @@ declare interface PluginConfig {
30
233
  maxReferenceDepth?: number;
31
234
  openai?: {
32
235
  apiKey: string;
33
- model?: "gpt-5" | "gpt-4o" | "gpt-4-turbo" | "gpt-4";
34
- reasoningEffort?: "minimal" | "low" | "medium" | "high";
35
- verbosity?: "low" | "medium" | "high";
236
+ model?: "gpt-5.1" | "gpt-5" | "gpt-4o" | "gpt-4-turbo" | "gpt-4";
237
+ reasoningEffort?: "low" | "medium" | "high";
36
238
  enabled?: boolean;
37
239
  defaultMode?: TranslationMode;
38
240
  };
241
+ fieldTransforms?: AnyFieldTransform[];
242
+ }
243
+
244
+ /**
245
+ * Transform for handling Portable Text (rich text) content using XML-like serialization.
246
+ * This leverages the existing serialization utilities to convert complex rich text
247
+ * to a format that's easier for translation models to process.
248
+ *
249
+ * Uses value-based detection to identify Portable Text arrays (checking for _type === 'block').
250
+ * The fieldPath pattern is used as a performance optimization to avoid checking every field,
251
+ * but the valueMatcher is the definitive check.
252
+ *
253
+ * @public
254
+ */
255
+ export declare const richTextTransform: FieldTransform<
256
+ unknown[],
257
+ TranslatableRichText
258
+ >;
259
+
260
+ /**
261
+ * Context passed to transform functions.
262
+ * @public
263
+ */
264
+ export declare interface TransformContext {
265
+ /**
266
+ * Current field path
267
+ */
268
+ fieldPath: string;
269
+ /**
270
+ * Document schema type
271
+ */
272
+ schemaType: string;
273
+ /**
274
+ * Full document being processed
275
+ */
276
+ document: TranslatableDocument;
277
+ }
278
+
279
+ declare interface TranslatableDocument extends SanityDocument {
280
+ language: string;
281
+ }
282
+
283
+ /**
284
+ * Special type to track transformed rich text content.
285
+ * This separates the serialized XML content from regular strings
286
+ * so the main serialization can handle it correctly.
287
+ */
288
+ declare interface TranslatableRichText {
289
+ _type: "translatable.richtext";
290
+ translationText: string;
291
+ _originalValue: unknown[];
292
+ }
293
+
294
+ /**
295
+ * Special type to track transformed VTT captions.
296
+ * This combines metadata separation for token efficiency with XML format for translation.
297
+ */
298
+ declare interface TranslatableVTTCaptions {
299
+ _type: "translatable.vtt.captions";
300
+ translationText: string;
301
+ _originalMetadata: VTTCaptionMetadata[];
39
302
  }
40
303
 
41
304
  /**
@@ -56,4 +319,40 @@ declare type TranslationMode =
56
319
  | "openai-only"
57
320
  | "sanity-with-review";
58
321
 
322
+ /**
323
+ * Represents a VTT caption cue with timing and text content.
324
+ */
325
+ declare interface VTTCaption {
326
+ _key?: string;
327
+ _type?: string;
328
+ text?: string;
329
+ startTime?: number;
330
+ endTime?: number;
331
+ [key: string]: string | number | undefined;
332
+ }
333
+
334
+ /**
335
+ * VTT caption metadata without text content, used for preservation during translation.
336
+ */
337
+ declare interface VTTCaptionMetadata {
338
+ _key?: string;
339
+ _type?: string;
340
+ startTime?: number;
341
+ endTime?: number;
342
+ _originalIndex: number;
343
+ [key: string]: string | number | undefined;
344
+ }
345
+
346
+ /**
347
+ * Transform for handling VTT captions in video documents using a hybrid approach.
348
+ * This stores metadata separately for token efficiency while formatting
349
+ * text content as XML for better translation results.
350
+ *
351
+ * @public
352
+ */
353
+ export declare const vttCaptionTransform: FieldTransform<
354
+ VTTCaption[],
355
+ TranslatableVTTCaptions
356
+ >;
357
+
59
358
  export {};