@braze/sanity-plugin-ai-translation 0.2.69 → 0.2.72

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,57 @@ 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
+ | TranslationJobFailedEvent
480
+ | OpenAIApiCallEvent;
481
+
482
+ /**
483
+ * Base interface for all telemetry events
484
+ * @public
485
+ */
486
+ declare interface TelemetryEventBase {
487
+ name: string;
488
+ timestamp: string;
489
+ }
490
+
405
491
  /**
406
492
  * Context passed to transform functions.
407
493
  * @public
@@ -461,6 +547,107 @@ declare interface TranslatableWistiaCaptions {
461
547
  }>;
462
548
  }
463
549
 
550
+ /**
551
+ * Event emitted when a translation batch completes
552
+ * @public
553
+ */
554
+ declare interface TranslationBatchCompletedEvent extends TelemetryEventBase {
555
+ name: "translation.batch_completed";
556
+ data: {
557
+ documentId: string;
558
+ documentType: string;
559
+ sourceLanguage: string;
560
+ targetLanguages: string[];
561
+ translationMode: TranslationMode;
562
+ succeeded: number;
563
+ failed: number;
564
+ skipped: number;
565
+ durationMs: number;
566
+ };
567
+ }
568
+
569
+ /**
570
+ * Event emitted when a translation batch starts
571
+ * @public
572
+ */
573
+ declare interface TranslationBatchStartedEvent extends TelemetryEventBase {
574
+ name: "translation.batch_started";
575
+ data: {
576
+ documentId: string;
577
+ documentType: string;
578
+ sourceLanguage: string;
579
+ targetLanguages: string[];
580
+ translationMode: TranslationMode;
581
+ referenceCount: number;
582
+ releaseMode: ReleaseSelectionMode;
583
+ };
584
+ }
585
+
586
+ /**
587
+ * Debug bundle containing full context for troubleshooting translation failures.
588
+ * This is only emitted when a translation fails.
589
+ * @public
590
+ */
591
+ declare interface TranslationErrorBundle {
592
+ /** Document ID being translated */
593
+ documentId: string;
594
+ /** Document schema type */
595
+ documentType: string;
596
+ /** Target language for translation */
597
+ targetLanguage: string;
598
+ /** Source language of the document */
599
+ sourceLanguage: string;
600
+ /** Translation mode used */
601
+ translationMode: TranslationMode;
602
+ /** Error details */
603
+ error: {
604
+ /** Error message */
605
+ message: string;
606
+ /** Error stack trace if available */
607
+ stack?: string;
608
+ /** Categorized error type for filtering/alerting */
609
+ type: TranslationErrorType;
610
+ };
611
+ /**
612
+ * Debug data for troubleshooting.
613
+ * Can be large - consumer decides how to handle (truncate, store locally, etc.)
614
+ */
615
+ debug: {
616
+ /** Full source document */
617
+ originalDocument: SanityDocument;
618
+ /** XML-serialized document sent to OpenAI (if OpenAI mode) */
619
+ serializedDocument?: string;
620
+ /** Raw API response from OpenAI (if OpenAI mode) */
621
+ openaiResponse?: string;
622
+ /** Translation prompts used */
623
+ translationPrompts?: {
624
+ globalPrompt?: string;
625
+ localePrompt?: string;
626
+ protectedPhrases?: string[];
627
+ glossaryTerms?: any[];
628
+ };
629
+ /** Validation errors if structure validation failed */
630
+ validationErrors?: any[];
631
+ };
632
+ /** ISO timestamp when error occurred */
633
+ timestamp: string;
634
+ /** Duration of the translation attempt in milliseconds */
635
+ durationMs: number;
636
+ }
637
+
638
+ /**
639
+ * Error types for categorizing translation failures
640
+ * @public
641
+ */
642
+ declare type TranslationErrorType =
643
+ | "serialization"
644
+ | "deserialization"
645
+ | "openai_api"
646
+ | "validation"
647
+ | "sanity_agent"
648
+ | "network"
649
+ | "unknown";
650
+
464
651
  /**
465
652
  * Schema for translation history
466
653
  *
@@ -474,6 +661,46 @@ export declare const translationHistorySchema: {
474
661
  WidenValidation &
475
662
  WidenInitialValue;
476
663
 
664
+ /**
665
+ * Event emitted when an individual translation job completes successfully
666
+ * @public
667
+ */
668
+ declare interface TranslationJobCompletedEvent extends TelemetryEventBase {
669
+ name: "translation.job_completed";
670
+ data: {
671
+ documentId: string;
672
+ documentType: string;
673
+ sourceLanguage: string;
674
+ targetLanguage: string;
675
+ translationMode: TranslationMode;
676
+ durationMs: number;
677
+ tokensUsed?: number;
678
+ estimatedCost?: number;
679
+ };
680
+ }
681
+
682
+ /**
683
+ * Event emitted when an individual translation job fails.
684
+ * This is emitted alongside the detailed error bundle (via onError) to enable
685
+ * easy error rate calculations by comparing job_completed vs job_failed counts.
686
+ * @public
687
+ */
688
+ declare interface TranslationJobFailedEvent extends TelemetryEventBase {
689
+ name: "translation.job_failed";
690
+ data: {
691
+ documentId: string;
692
+ documentType: string;
693
+ sourceLanguage: string;
694
+ targetLanguage: string;
695
+ translationMode: TranslationMode;
696
+ durationMs: number;
697
+ /** Categorized error type for filtering/grouping */
698
+ errorType: TranslationErrorType;
699
+ /** Error message (kept short for metrics; full details in error bundle) */
700
+ errorMessage: string;
701
+ };
702
+ }
703
+
477
704
  declare type TranslationMode = "sanity-only" | "openai-only";
478
705
 
479
706
  /**