@braze/sanity-plugin-ai-translation 0.2.68 → 0.2.71

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
@@ -269,6 +269,26 @@ export declare type InferOriginalType<T> =
269
269
  export declare type InferTranslatableType<T> =
270
270
  T extends FieldTransform<unknown, infer R> ? R : never;
271
271
 
272
+ /**
273
+ * Event emitted when an OpenAI API call is made
274
+ * @public
275
+ */
276
+ declare interface OpenAIApiCallEvent extends TelemetryEventBase {
277
+ name: "openai.api_call";
278
+ data: {
279
+ model: string;
280
+ tokensUsed: {
281
+ prompt: number;
282
+ completion: number;
283
+ reasoning: number;
284
+ total: number;
285
+ cached: number;
286
+ };
287
+ cost: number;
288
+ durationMs: number;
289
+ };
290
+ }
291
+
272
292
  /**
273
293
  *
274
294
  * Configuration for the Sanity AI Translation plugin.
@@ -312,6 +332,19 @@ export declare interface PluginConfig {
312
332
  * ```
313
333
  */
314
334
  excludeFields?: string[];
335
+ /**
336
+ * Optional telemetry configuration for monitoring translation operations.
337
+ * Provides callbacks for lightweight events and detailed error bundles.
338
+ *
339
+ * @example
340
+ * ```typescript
341
+ * telemetry: {
342
+ * onEvent: (event) => datadogRum.addAction(event.name, event.data),
343
+ * onError: (bundle) => datadogLogs.logger.error('Translation failed', bundle)
344
+ * }
345
+ * ```
346
+ */
347
+ telemetry?: TelemetryConfig;
315
348
  }
316
349
 
317
350
  /**
@@ -374,6 +407,8 @@ export declare const PREDEFINED_EXCLUSIONS: {
374
407
  ];
375
408
  };
376
409
 
410
+ declare type ReleaseSelectionMode = "new" | "existing";
411
+
377
412
  /**
378
413
  * Restores previously excluded fields to a translated document
379
414
  * @param translatedDocument - The translated document
@@ -402,6 +437,56 @@ export declare const richTextTransform: FieldTransform<
402
437
  TranslatableRichText
403
438
  >;
404
439
 
440
+ /**
441
+ * Configuration for telemetry/monitoring integration.
442
+ * Both callbacks are optional - configure based on your monitoring needs.
443
+ * @public
444
+ *
445
+ * @example Basic Datadog RUM integration
446
+ * ```typescript
447
+ * telemetry: {
448
+ * onEvent: (event) => datadogRum.addAction(event.name, event.data),
449
+ * onError: (bundle) => datadogRum.addError(new Error(bundle.error.message), {
450
+ * documentId: bundle.documentId,
451
+ * errorType: bundle.error.type,
452
+ * })
453
+ * }
454
+ * ```
455
+ */
456
+ declare interface TelemetryConfig {
457
+ /**
458
+ * Lightweight event callback - called for all translation events.
459
+ * Keep payloads small (IDs, counts, durations, costs).
460
+ * @param event - The telemetry event with name and data
461
+ */
462
+ onEvent?: (event: TelemetryEvent) => void;
463
+ /**
464
+ * Error callback with full debug bundle - only called on failures.
465
+ * Consumer decides how to handle (send to Datadog, store locally, truncate, etc.)
466
+ * @param bundle - Full debug context for troubleshooting
467
+ */
468
+ onError?: (bundle: TranslationErrorBundle) => void;
469
+ }
470
+
471
+ /**
472
+ * Union of all telemetry event types
473
+ * @public
474
+ */
475
+ declare type TelemetryEvent =
476
+ | TranslationBatchStartedEvent
477
+ | TranslationBatchCompletedEvent
478
+ | TranslationJobCompletedEvent
479
+ | OpenAIApiCallEvent;
480
+
481
+ /**
482
+ * Base interface for all telemetry events
483
+ * @public
484
+ */
485
+ declare interface TelemetryEventBase {
486
+ name: string;
487
+ timestamp: string;
488
+ }
489
+
405
490
  /**
406
491
  * Context passed to transform functions.
407
492
  * @public
@@ -461,6 +546,107 @@ declare interface TranslatableWistiaCaptions {
461
546
  }>;
462
547
  }
463
548
 
549
+ /**
550
+ * Event emitted when a translation batch completes
551
+ * @public
552
+ */
553
+ declare interface TranslationBatchCompletedEvent extends TelemetryEventBase {
554
+ name: "translation.batch_completed";
555
+ data: {
556
+ documentId: string;
557
+ documentType: string;
558
+ sourceLanguage: string;
559
+ targetLanguages: string[];
560
+ translationMode: TranslationMode;
561
+ succeeded: number;
562
+ failed: number;
563
+ skipped: number;
564
+ durationMs: number;
565
+ };
566
+ }
567
+
568
+ /**
569
+ * Event emitted when a translation batch starts
570
+ * @public
571
+ */
572
+ declare interface TranslationBatchStartedEvent extends TelemetryEventBase {
573
+ name: "translation.batch_started";
574
+ data: {
575
+ documentId: string;
576
+ documentType: string;
577
+ sourceLanguage: string;
578
+ targetLanguages: string[];
579
+ translationMode: TranslationMode;
580
+ referenceCount: number;
581
+ releaseMode: ReleaseSelectionMode;
582
+ };
583
+ }
584
+
585
+ /**
586
+ * Debug bundle containing full context for troubleshooting translation failures.
587
+ * This is only emitted when a translation fails.
588
+ * @public
589
+ */
590
+ declare interface TranslationErrorBundle {
591
+ /** Document ID being translated */
592
+ documentId: string;
593
+ /** Document schema type */
594
+ documentType: string;
595
+ /** Target language for translation */
596
+ targetLanguage: string;
597
+ /** Source language of the document */
598
+ sourceLanguage: string;
599
+ /** Translation mode used */
600
+ translationMode: TranslationMode;
601
+ /** Error details */
602
+ error: {
603
+ /** Error message */
604
+ message: string;
605
+ /** Error stack trace if available */
606
+ stack?: string;
607
+ /** Categorized error type for filtering/alerting */
608
+ type: TranslationErrorType;
609
+ };
610
+ /**
611
+ * Debug data for troubleshooting.
612
+ * Can be large - consumer decides how to handle (truncate, store locally, etc.)
613
+ */
614
+ debug: {
615
+ /** Full source document */
616
+ originalDocument: SanityDocument;
617
+ /** XML-serialized document sent to OpenAI (if OpenAI mode) */
618
+ serializedDocument?: string;
619
+ /** Raw API response from OpenAI (if OpenAI mode) */
620
+ openaiResponse?: string;
621
+ /** Translation prompts used */
622
+ translationPrompts?: {
623
+ globalPrompt?: string;
624
+ localePrompt?: string;
625
+ protectedPhrases?: string[];
626
+ glossaryTerms?: any[];
627
+ };
628
+ /** Validation errors if structure validation failed */
629
+ validationErrors?: any[];
630
+ };
631
+ /** ISO timestamp when error occurred */
632
+ timestamp: string;
633
+ /** Duration of the translation attempt in milliseconds */
634
+ durationMs: number;
635
+ }
636
+
637
+ /**
638
+ * Error types for categorizing translation failures
639
+ * @public
640
+ */
641
+ declare type TranslationErrorType =
642
+ | "serialization"
643
+ | "deserialization"
644
+ | "openai_api"
645
+ | "validation"
646
+ | "sanity_agent"
647
+ | "network"
648
+ | "unknown";
649
+
464
650
  /**
465
651
  * Schema for translation history
466
652
  *
@@ -474,6 +660,24 @@ export declare const translationHistorySchema: {
474
660
  WidenValidation &
475
661
  WidenInitialValue;
476
662
 
663
+ /**
664
+ * Event emitted when an individual translation job completes successfully
665
+ * @public
666
+ */
667
+ declare interface TranslationJobCompletedEvent extends TelemetryEventBase {
668
+ name: "translation.job_completed";
669
+ data: {
670
+ documentId: string;
671
+ documentType: string;
672
+ sourceLanguage: string;
673
+ targetLanguage: string;
674
+ translationMode: TranslationMode;
675
+ durationMs: number;
676
+ tokensUsed?: number;
677
+ estimatedCost?: number;
678
+ };
679
+ }
680
+
477
681
  declare type TranslationMode = "sanity-only" | "openai-only";
478
682
 
479
683
  /**