@mux/ai 0.54.0 → 0.56.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.
@@ -527,7 +527,13 @@ declare const MAX_CUE_TEXT_CHARS = 2000;
527
527
  */
528
528
  declare function stripVttMetadataBlocks(vttContent: string): string;
529
529
  declare function extractTextFromVTT(vttContent: string): string;
530
- declare function vttTimestampToSeconds(timestamp: string): number;
530
+ /**
531
+ * Converts a WebVTT timestamp (`HH:MM:SS.mmm` or `MM:SS.mmm`) to seconds.
532
+ *
533
+ * @param timestamp - The timestamp to convert
534
+ * @returns The timestamp in seconds, or `undefined` if it is malformed
535
+ */
536
+ declare function vttTimestampToSeconds(timestamp: string): number | undefined;
531
537
  /**
532
538
  * Converts seconds to a human-readable timestamp.
533
539
  * Returns M:SS for durations under an hour, H:MM:SS for an hour or more.
@@ -604,4 +610,4 @@ interface TranscriptFetchStepResult {
604
610
  declare function fetchTranscriptDataForUrl(transcriptUrl: string, options?: Pick<TranscriptFetchOptions, "cleanTranscript" | "scope">): Promise<TranscriptFetchStepResult>;
605
611
  declare function fetchTranscriptForAsset(asset: MuxAsset, playbackId: string, options?: TranscriptFetchOptions): Promise<TranscriptResult>;
606
612
 
607
- export { fetchTranscriptDataForUrl as $, type AssetTextTrack as A, type ShotsResult as B, type ChunkEmbedding as C, type DeletedShotsResult as D, type Encrypted as E, type SkippedShotsResult as F, type TranscriptEmptyReason as G, type Hotspot as H, type ImageSubmissionMode as I, type TranscriptFetchOptions as J, type TranscriptFetchStepResult as K, LOW_CONFIDENCE_THRESHOLD as L, type MuxAsset as M, type TranscriptResult as N, type WaitForShotsOptions as O, type PlaybackAsset as P, buildTranscriptUrl as Q, buildVttFromCueBlocks as R, type StoragePutObjectInput as S, type TextChunk as T, type UsageMetadata as U, type VTTChunkingConfig as V, type WorkflowCredentials as W, buildVttFromTranslatedCueBlocks as X, concatenateVttSegments as Y, extractTextFromVTT as Z, extractTimestampedTranscript as _, type StoragePresignGetObjectInput as a, fetchTranscriptForAsset as a0, filterVttByScope as a1, findCaptionTrack as a2, getHotspotsForAsset as a3, getHotspotsForPlaybackId as a4, getHotspotsForVideo as a5, getReadyTextTracks as a6, getReliableLanguageCode as a7, getShotsForAsset as a8, parseVTTCues as a9, replaceCueText as aa, requestShotsForAsset as ab, sanitizeUntrustedText as ac, secondsToTimestamp as ad, splitVttPreambleAndCueBlocks as ae, stripVttMetadataBlocks as af, vttTimestampToSeconds as ag, waitForShotsForAsset as ah, type ChunkingStrategy as b, type EncryptedPayload as c, type MuxAIOptions as d, type PlaybackPolicy as e, type ScopedMuxAIOptions as f, type StorageAdapter as g, type TokenChunkingConfig as h, type TokenUsage as i, type ToneType as j, type VideoEmbeddingsResult as k, type WorkflowCredentialsInput as l, type WorkflowMuxClient as m, type WorkflowScope as n, decryptFromWorkflow as o, encryptForWorkflow as p, type VTTCue as q, type CompletedShotsResult as r, type ErroredShotsResult as s, type HotspotOptions as t, type HotspotResponse as u, MAX_CUE_TEXT_CHARS as v, type ParseVTTCuesOptions as w, type PendingShotsResult as x, type Shot as y, type ShotRequestOptions as z };
613
+ export { fetchTranscriptDataForUrl as $, type AssetTextTrack as A, type ShotsResult as B, type ChunkEmbedding as C, type DeletedShotsResult as D, type Encrypted as E, type SkippedShotsResult as F, type TranscriptEmptyReason as G, type Hotspot as H, type ImageSubmissionMode as I, type TranscriptFetchOptions as J, type TranscriptFetchStepResult as K, LOW_CONFIDENCE_THRESHOLD as L, type MuxAsset as M, type TranscriptResult as N, type WaitForShotsOptions as O, type PlaybackAsset as P, buildTranscriptUrl as Q, buildVttFromCueBlocks as R, type StoragePutObjectInput as S, type TokenUsage as T, type UsageMetadata as U, type VTTChunkingConfig as V, type WorkflowCredentials as W, buildVttFromTranslatedCueBlocks as X, concatenateVttSegments as Y, extractTextFromVTT as Z, extractTimestampedTranscript as _, type StoragePresignGetObjectInput as a, fetchTranscriptForAsset as a0, filterVttByScope as a1, findCaptionTrack as a2, getHotspotsForAsset as a3, getHotspotsForPlaybackId as a4, getHotspotsForVideo as a5, getReadyTextTracks as a6, getReliableLanguageCode as a7, getShotsForAsset as a8, parseVTTCues as a9, replaceCueText as aa, requestShotsForAsset as ab, sanitizeUntrustedText as ac, secondsToTimestamp as ad, splitVttPreambleAndCueBlocks as ae, stripVttMetadataBlocks as af, vttTimestampToSeconds as ag, waitForShotsForAsset as ah, type ChunkingStrategy as b, type EncryptedPayload as c, type MuxAIOptions as d, type PlaybackPolicy as e, type ScopedMuxAIOptions as f, type StorageAdapter as g, type TextChunk as h, type TokenChunkingConfig as i, type ToneType as j, type VideoEmbeddingsResult as k, type WorkflowCredentialsInput as l, type WorkflowMuxClient as m, type WorkflowScope as n, decryptFromWorkflow as o, encryptForWorkflow as p, type VTTCue as q, type CompletedShotsResult as r, type ErroredShotsResult as s, type HotspotOptions as t, type HotspotResponse as u, MAX_CUE_TEXT_CHARS as v, type ParseVTTCuesOptions as w, type PendingShotsResult as x, type Shot as y, type ShotRequestOptions as z };
@@ -1,7 +1,7 @@
1
- export { J as ASK_QUESTIONS_DEFAULT_MAX_ANSWER_OPTION_LENGTH, K as ASK_QUESTIONS_DEFAULT_MAX_FREE_FORM_ANSWER_LENGTH, N as ASK_QUESTIONS_MAX_QUESTIONS_PER_CALL, O as ASK_QUESTIONS_MAX_QUESTION_LENGTH, P as AskQuestionsOptions, Q as AskQuestionsResult, U as AskQuestionsType, V as AudioTranslationOptions, W as AudioTranslationResult, X as AutoCensorProfanityOptions, Y as BurnedInCaptionsAnalysis, Z as BurnedInCaptionsOptions, _ as BurnedInCaptionsPromptOverrides, $ as BurnedInCaptionsPromptSections, a0 as BurnedInCaptionsResult, a1 as CaptionReplacement, a2 as CensorMode, a3 as Chapter, a4 as ChapterSystemPromptSections, a5 as ChaptersOptions, a6 as ChaptersPromptOverrides, a7 as ChaptersPromptSections, a8 as ChaptersResult, a9 as ChaptersType, aa as DEFAULT_TRANSCRIPT_WINDOWING, ab as EditCaptionsOptions, ac as EditCaptionsResult, ad as EmbeddingsOptions, ae as EmbeddingsResult, af as EngagementInsightsOptions, ag as EngagementInsightsResult, ah as GENERATE_TEXT_CALLS_TO_ACTION, ai as GENERATE_TEXT_CHANNELS, aj as GENERATE_TEXT_CHANNEL_CEILINGS, ak as GENERATE_TEXT_FORMATS, al as GENERATE_TEXT_LENGTH_BOUNDS, am as GENERATE_TEXT_MAX_ARTIFACTS, an as GENERATE_TEXT_MAX_AUDIENCE_CHARS, ao as GENERATE_TEXT_MAX_BRAND_TERMS, ap as GENERATE_TEXT_MAX_BRAND_TERMS_TOTAL_CHARS, aq as GENERATE_TEXT_MAX_BRAND_TERM_CHARS, ar as GENERATE_TEXT_MAX_CONCURRENT_GENERATIONS, as as GENERATE_TEXT_MAX_INSTRUCTIONS_CHARS, at as GENERATE_TEXT_MAX_SHOT_FRAMES, au as GENERATE_TEXT_MAX_VARIANTS, av as GENERATE_TEXT_VOICES, aw as GOOGLE_VISION_LIKELIHOOD_TO_SCORE, ax as GenerateTextArtifact, ay as GenerateTextCallToAction, az as GenerateTextChannel, aA as GenerateTextFormat, aB as GenerateTextKeyedItem, aC as GenerateTextLengthLimit, aD as GenerateTextOptions, aE as GenerateTextResult, aF as GenerateTextVariant, aG as GenerateTextVoice, aH as GeneratedTextArtifact, aI as GeneratedTextVariant, aJ as HIVE_SEXUAL_CATEGORIES, aK as HIVE_VIOLENCE_CATEGORIES, aL as HiveModerationOutput, aM as HiveModerationSource, aN as ModerationOptions, aO as ModerationProvider, aP as ModerationResult, aQ as ModerationSurfaceStatus, aR as MomentInsight, aS as OverallInsight, aT as ProfanityDetectionPayload, aU as Question, aV as QuestionAnswer, aW as QuestionAnswerType, aX as RepairAttempt, aY as RepairCall, aZ as RepairOptions, a_ as RepairPlan, a$ as RepairResult, b0 as RepairSpan, b1 as ReplacementRecord, b2 as SpeakerReplacement, b3 as StaticRenditionCleanupOutcome, b4 as SummarizationOptions, b5 as SummarizationPromptOverrides, b6 as SummarizationPromptSections, b7 as SummaryAndTagsResult, b8 as SummaryType, b9 as TextLengthLimit, ba as TextRepairModelOptions, bb as ThumbnailModerationScore, bc as ThumbnailModerationSkipReason, bd as ThumbnailModerationStatus, be as TranscriptModerationScore, bf as TranscriptModerationSkipReason, bg as TranscriptModerationStatus, bh as TranslationChunkingOptions, bi as TranslationOptions, bj as TranslationPayload, bk as TranslationResult, bl as aggregateTokenUsage, bm as applyOverrideLists, bn as applyReplacements, bo as applySpeakerReplacements, bp as applyTextRepair, bq as askQuestions, br as buildReplacementRegex, bs as buildTranscriptWindows, bt as burnedInCaptionsSchema, bu as censorVttContent, bv as chapterSchema, bw as chaptersSchema, bx as createReplacer, by as createTextRepairGenerator, bz as editCaptions, bA as extractTranscriptSegmentsForHotspots, bB as findGenerateTextLengthViolation, bC as generateChapters, bD as generateEmbeddings, bE as generateEngagementInsights, bF as generateText, bG as generateVideoEmbeddings, bH as getModerationScores, bI as getSummaryAndTags, bJ as hasBurnedInCaptions, bK as mapShotsToHotspots, bL as measureGenerateTextLength, bM as normalizeTranslatedVtt, bN as planTextRepair, bO as profanityDetectionSchema, bP as questionAnswerSchema, bQ as repairJsonSchema, bR as repairText, bS as resolveGenerateTextLengthLimit, bT as resolveGenerateTextLengthLimits, bU as resolveGenerateTextOptions, bV as runTextRepairLoop, bW as selectGenerateTextShotFrames, bX as shouldSplitChunkTranslationError, bY as summarySchema, bZ as transformCueText, b_ as translateAudio, b$ as translateCaptions, c0 as translationSchema, c1 as validateNeverTranslateTerms, c2 as verifyNeverTranslateTerms } from '../index-CmTC1w4V.js';
1
+ export { J as ASK_QUESTIONS_DEFAULT_MAX_ANSWER_OPTION_LENGTH, K as ASK_QUESTIONS_DEFAULT_MAX_FREE_FORM_ANSWER_LENGTH, N as ASK_QUESTIONS_MAX_QUESTIONS_PER_CALL, O as ASK_QUESTIONS_MAX_QUESTION_LENGTH, P as AskQuestionsOptions, Q as AskQuestionsResult, U as AskQuestionsType, V as AudioTranslationOptions, W as AudioTranslationResult, X as AutoCensorProfanityOptions, Y as BurnedInCaptionsAnalysis, Z as BurnedInCaptionsOptions, _ as BurnedInCaptionsPromptOverrides, $ as BurnedInCaptionsPromptSections, a0 as BurnedInCaptionsResult, a1 as CaptionReplacement, a2 as CensorMode, a3 as Chapter, a4 as ChapterSystemPromptSections, a5 as ChaptersOptions, a6 as ChaptersPromptOverrides, a7 as ChaptersPromptSections, a8 as ChaptersResult, a9 as ChaptersType, aa as DEFAULT_TRANSCRIPT_WINDOWING, ab as EditCaptionsOptions, ac as EditCaptionsResult, ad as EmbeddingsOptions, ae as EmbeddingsResult, af as EngagementInsightsOptions, ag as EngagementInsightsResult, ah as GENERATE_TEXT_CALLS_TO_ACTION, ai as GENERATE_TEXT_CHANNELS, aj as GENERATE_TEXT_CHANNEL_CEILINGS, ak as GENERATE_TEXT_FORMATS, al as GENERATE_TEXT_LENGTH_BOUNDS, am as GENERATE_TEXT_MAX_ARTIFACTS, an as GENERATE_TEXT_MAX_AUDIENCE_CHARS, ao as GENERATE_TEXT_MAX_BRAND_TERMS, ap as GENERATE_TEXT_MAX_BRAND_TERMS_TOTAL_CHARS, aq as GENERATE_TEXT_MAX_BRAND_TERM_CHARS, ar as GENERATE_TEXT_MAX_CONCURRENT_GENERATIONS, as as GENERATE_TEXT_MAX_INSTRUCTIONS_CHARS, at as GENERATE_TEXT_MAX_SHOT_FRAMES, au as GENERATE_TEXT_MAX_VARIANTS, av as GENERATE_TEXT_VOICES, aw as GOOGLE_VISION_LIKELIHOOD_TO_SCORE, ax as GenerateTextArtifact, ay as GenerateTextCallToAction, az as GenerateTextChannel, aA as GenerateTextFormat, aB as GenerateTextKeyedItem, aC as GenerateTextLengthLimit, aD as GenerateTextOptions, aE as GenerateTextResult, aF as GenerateTextVariant, aG as GenerateTextVoice, aH as GeneratedTextArtifact, aI as GeneratedTextVariant, aJ as HIVE_SEXUAL_CATEGORIES, aK as HIVE_VIOLENCE_CATEGORIES, aL as HiveModerationOutput, aM as HiveModerationSource, aN as ModerationOptions, aO as ModerationProvider, aP as ModerationResult, aQ as ModerationSurfaceStatus, aR as MomentInsight, aS as OverallInsight, aT as ProfanityDetectionPayload, aU as Question, aV as QuestionAnswer, aW as QuestionAnswerType, aX as RepairAttempt, aY as RepairCall, aZ as RepairOptions, a_ as RepairPlan, a$ as RepairResult, b0 as RepairSpan, b1 as ReplacementRecord, b2 as SpeakerReplacement, b3 as StaticRenditionCleanupOutcome, b4 as SummarizationOptions, b5 as SummarizationPromptOverrides, b6 as SummarizationPromptSections, b7 as SummaryAndTagsResult, b8 as SummaryType, b9 as TextLengthLimit, ba as TextRepairModelOptions, bb as ThumbnailModerationScore, bc as ThumbnailModerationSkipReason, bd as ThumbnailModerationStatus, be as TranscriptModerationScore, bf as TranscriptModerationSkipReason, bg as TranscriptModerationStatus, bh as TranslationChunkingOptions, bi as TranslationOptions, bj as TranslationPayload, bk as TranslationResult, bl as aggregateTokenUsage, bm as applyOverrideLists, bn as applyReplacements, bo as applySpeakerReplacements, bp as applyTextRepair, bq as askQuestions, br as buildReplacementRegex, bs as buildTranscriptWindows, bt as burnedInCaptionsSchema, bu as censorVttContent, bv as chapterSchema, bw as chaptersSchema, bx as createReplacer, by as createTextRepairGenerator, bz as editCaptions, bA as extractTranscriptSegmentsForHotspots, bB as findGenerateTextLengthViolation, bC as generateChapters, bD as generateEmbeddings, bE as generateEngagementInsights, bF as generateText, bG as generateVideoEmbeddings, bH as getModerationScores, bI as getSummaryAndTags, bJ as hasBurnedInCaptions, bK as mapShotsToHotspots, bL as measureGenerateTextLength, bM as normalizeTranslatedVtt, bN as planTextRepair, bO as profanityDetectionSchema, bP as questionAnswerSchema, bQ as repairJsonSchema, bR as repairText, bS as resolveGenerateTextLengthLimit, bT as resolveGenerateTextLengthLimits, bU as resolveGenerateTextOptions, bV as runTextRepairLoop, bW as selectGenerateTextShotFrames, bX as shouldSplitChunkTranslationError, bY as summarySchema, bZ as transformCueText, b_ as translateAudio, b$ as translateCaptions, c0 as translationSchema, c1 as validateNeverTranslateTerms, c2 as verifyNeverTranslateTerms } from '../index-Bav9fNtf.js';
2
2
  export { D as DEFAULT_DESCRIPTION_LENGTH, a as DEFAULT_SUMMARY_KEYWORD_LIMIT, b as DEFAULT_TITLE_LENGTH } from '../summarization-BFmhXeAu.js';
3
3
  import 'ai';
4
- import '../transcripts-BCtzC8hZ.js';
4
+ import '../transcripts-Brw74qlV.js';
5
5
  import '@mux/ts';
6
6
  import 'zod';
7
7
  import '@ai-sdk/anthropic';
@@ -5,6 +5,10 @@ import { generateText, jsonSchema, NoObjectGeneratedError as NoObjectGeneratedEr
5
5
  import { APICallError as APICallError2, NoObjectGeneratedError, RetryError } from "ai";
6
6
  import { z as z2 } from "zod";
7
7
 
8
+ // node_modules/@workflow/serde/dist/index.js
9
+ var WORKFLOW_SERIALIZE = /* @__PURE__ */ Symbol.for("workflow-serialize");
10
+ var WORKFLOW_DESERIALIZE = /* @__PURE__ */ Symbol.for("workflow-deserialize");
11
+
8
12
  // src/lib/prompt-fragments.ts
9
13
  import dedent from "dedent";
10
14
 
@@ -526,7 +530,7 @@ function rethrowWithTokenUsage(error, collectedUsage) {
526
530
  }
527
531
 
528
532
  // src/lib/mux-ai-error.ts
529
- var MuxAiError = class extends Error {
533
+ var MuxAiError = class _MuxAiError extends Error {
530
534
  constructor(message, opts) {
531
535
  super(message);
532
536
  /** Structural brand for duck-type identification. */
@@ -542,6 +546,32 @@ var MuxAiError = class extends Error {
542
546
  static is(value) {
543
547
  return typeof value === "object" && value !== null && "__robots_error" in value && value.__robots_error === true;
544
548
  }
549
+ static [WORKFLOW_SERIALIZE](instance) {
550
+ const incomplete = instance;
551
+ return {
552
+ message: instance.publicMessage,
553
+ publicType: instance.publicType,
554
+ retryable: instance.retryable,
555
+ stack: instance.stack,
556
+ usage: getErrorTokenUsage(instance),
557
+ finishReason: incomplete.finishReason,
558
+ rawFinishReason: incomplete.rawFinishReason,
559
+ diagnostics: instance.diagnostics
560
+ };
561
+ }
562
+ static [WORKFLOW_DESERIALIZE](value) {
563
+ const error = new _MuxAiError(value.message, { type: value.publicType, retryable: value.retryable });
564
+ Object.assign(error, {
565
+ usage: value.usage,
566
+ diagnostics: value.diagnostics,
567
+ ...value.finishReason !== void 0 && { finishReason: value.finishReason },
568
+ ...value.rawFinishReason !== void 0 && { rawFinishReason: value.rawFinishReason }
569
+ });
570
+ if (value.stack !== void 0) {
571
+ error.stack = value.stack;
572
+ }
573
+ return error;
574
+ }
545
575
  };
546
576
  function extractErrorDetail(error) {
547
577
  if (error instanceof Error && error.message) {
@@ -615,7 +645,8 @@ async function withRetry(fn, {
615
645
  maxRetries = DEFAULT_RETRY_OPTIONS.maxRetries,
616
646
  baseDelay = DEFAULT_RETRY_OPTIONS.baseDelay,
617
647
  maxDelay = DEFAULT_RETRY_OPTIONS.maxDelay,
618
- shouldRetry = defaultShouldRetry
648
+ shouldRetry = defaultShouldRetry,
649
+ onRetry
619
650
  } = {}) {
620
651
  let lastError;
621
652
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
@@ -628,6 +659,7 @@ async function withRetry(fn, {
628
659
  if (isLastAttempt || !shouldRetry(retryError, attempt + 1)) {
629
660
  throw error;
630
661
  }
662
+ onRetry?.(error, attempt + 1);
631
663
  const delay = calculateDelay(attempt + 1, baseDelay, maxDelay);
632
664
  console.warn(
633
665
  `Attempt ${attempt + 1} failed: ${retryError.message}. Retrying in ${Math.round(delay)}ms...`
@@ -721,10 +753,54 @@ async function withContentPolicyErrorHandling(operation) {
721
753
  }
722
754
  }
723
755
  async function withContentPolicyAwareRetry(operation, retryOptions) {
724
- return withRetry(
725
- () => withContentPolicyErrorHandling(operation),
726
- retryOptions
727
- );
756
+ const retriedUsage = [];
757
+ try {
758
+ const response = await withRetry(
759
+ () => withContentPolicyErrorHandling(operation),
760
+ {
761
+ ...retryOptions,
762
+ onRetry: (error, attempt) => {
763
+ const usage = getErrorTokenUsage(error);
764
+ if (usage) {
765
+ retriedUsage.push(usage);
766
+ }
767
+ retryOptions?.onRetry?.(error, attempt);
768
+ }
769
+ }
770
+ );
771
+ return includeRetriedUsage(response, retriedUsage);
772
+ } catch (error) {
773
+ rethrowWithTokenUsage(error, retriedUsage);
774
+ }
775
+ }
776
+ function includeRetriedUsage(response, retriedUsage) {
777
+ if (!retriedUsage.length || typeof response !== "object" || response === null) {
778
+ return response;
779
+ }
780
+ const result = response;
781
+ const merge = (usage) => {
782
+ const combined = aggregateTokenUsage([...retriedUsage, getErrorTokenUsage({ usage }) ?? {}]);
783
+ return {
784
+ ...usage,
785
+ ...combined,
786
+ ...combined.cachedInputTokens !== void 0 || combined.cacheWriteTokens !== void 0 ? {
787
+ inputTokenDetails: {
788
+ ...usage?.inputTokenDetails,
789
+ ...combined.cachedInputTokens !== void 0 && { cacheReadTokens: combined.cachedInputTokens },
790
+ ...combined.cacheWriteTokens !== void 0 && { cacheWriteTokens: combined.cacheWriteTokens }
791
+ }
792
+ } : {},
793
+ ...combined.reasoningTokens !== void 0 ? {
794
+ outputTokenDetails: { ...usage?.outputTokenDetails, reasoningTokens: combined.reasoningTokens }
795
+ } : {}
796
+ };
797
+ };
798
+ const descriptors = Object.getOwnPropertyDescriptors(response);
799
+ descriptors.usage = { value: merge(result.usage), enumerable: true, configurable: true };
800
+ if ("totalUsage" in result) {
801
+ descriptors.totalUsage = { value: merge(result.totalUsage), enumerable: true, configurable: true };
802
+ }
803
+ return Object.create(Object.getPrototypeOf(response), descriptors);
728
804
  }
729
805
  function rethrowContentPolicyError(error) {
730
806
  const block = extractContentPolicyBlock(error);
@@ -860,6 +936,35 @@ function findBlockedCategory(ratings) {
860
936
  return ratings?.find((rating) => rating.blocked === true)?.category ?? void 0;
861
937
  }
862
938
 
939
+ // src/lib/model-call-diagnostics.ts
940
+ import { APICallError as APICallError3 } from "ai";
941
+ function recordModelCallFailure(error, diagnostics, observedUsage = getErrorTokenUsage(error)) {
942
+ if (typeof error !== "object" || error === null) {
943
+ return;
944
+ }
945
+ const failure = error;
946
+ if (failure.diagnostics) {
947
+ return;
948
+ }
949
+ failure.diagnostics = { ...diagnostics };
950
+ console.warn("[@mux/ai] Model call failed.", {
951
+ ...diagnostics,
952
+ finishReason: failure.finishReason,
953
+ statusCode: failure.statusCode,
954
+ errorName: error instanceof Error ? error.name : void 0,
955
+ requestId: APICallError3.isInstance(error) ? error.responseHeaders?.["x-request-id"] ?? error.responseHeaders?.["request-id"] : void 0,
956
+ usage: observedUsage
957
+ });
958
+ }
959
+ async function withModelCallDiagnostics(diagnostics, operation) {
960
+ try {
961
+ return await operation();
962
+ } catch (error) {
963
+ recordModelCallFailure(error, diagnostics);
964
+ throw error;
965
+ }
966
+ }
967
+
863
968
  // src/lib/text-repair-capabilities.ts
864
969
  var OPENAI_REPAIR_CAPABILITIES = {
865
970
  "gpt-5.1": { supportsReasoningNone: true },
@@ -1021,13 +1126,14 @@ function createTextRepairGenerator(options) {
1021
1126
  return async (plan) => {
1022
1127
  let usage;
1023
1128
  const started = Date.now();
1129
+ const maxOutputTokens = options.maxOutputTokens ?? Math.min(32768, Math.max(1024, plan.spans.reduce((total, span) => total + Math.min(...span.budgets.map((limit) => limit.value * (limit.unit === "words" ? 4 : 2))), 0) + 128));
1024
1130
  try {
1025
1131
  const response = await withContentPolicyErrorHandling(() => generateText({
1026
1132
  model: options.model,
1027
1133
  providerOptions: options.providerOptions ?? getTextRepairProviderOptions(options.model),
1028
1134
  maxRetries: 0,
1029
1135
  abortSignal: AbortSignal.timeout(options.timeoutMs ?? 3e4),
1030
- maxOutputTokens: options.maxOutputTokens ?? Math.min(32768, Math.max(1024, plan.spans.reduce((total, span) => total + Math.min(...span.budgets.map((limit) => limit.value * (limit.unit === "words" ? 4 : 2))), 0) + 128)),
1136
+ maxOutputTokens,
1031
1137
  output: Output.object({ schema: jsonSchema(repairJsonSchema(plan)), name: "paragraph_replacements" }),
1032
1138
  system: promptDedent`
1033
1139
  ${NON_DISCLOSURE_CONSTRAINT}
@@ -1055,6 +1161,12 @@ function createTextRepairGenerator(options) {
1055
1161
  return { replacements: output, usage, elapsedMs: Date.now() - started };
1056
1162
  } catch (error) {
1057
1163
  const cause = NoObjectGeneratedError2.isInstance(error) && error.finishReason && error.finishReason !== "stop" ? new IncompleteGenerationError(error.finishReason, void 0, getErrorTokenUsage(error)) : error;
1164
+ recordModelCallFailure(cause, {
1165
+ phase: "length_repair",
1166
+ provider: typeof options.model === "object" ? options.model.provider : "gateway",
1167
+ modelId: typeof options.model === "object" ? options.model.modelId : options.model,
1168
+ maxOutputTokens
1169
+ }, usage ?? getErrorTokenUsage(cause));
1058
1170
  return { usage: usage ?? getErrorTokenUsage(cause), elapsedMs: Date.now() - started, error: cause instanceof Error ? cause.message : String(cause), errorCause: cause };
1059
1171
  }
1060
1172
  };
@@ -2558,14 +2670,13 @@ function extractTextFromVTT(vttContent) {
2558
2670
  textLines.join(" ").replace(/\s+/g, " ").trim()
2559
2671
  );
2560
2672
  }
2673
+ var VTT_TIMESTAMP_RE = /^(?:(\d+):)?(\d{2}):(\d{2})(?:\.(\d{3}))?$/;
2561
2674
  function vttTimestampToSeconds(timestamp) {
2562
- const parts = timestamp.split(":");
2563
- if (parts.length !== 3)
2564
- return 0;
2565
- const hours = Number.parseInt(parts[0], 10) || 0;
2566
- const minutes = Number.parseInt(parts[1], 10) || 0;
2567
- const seconds = Number.parseFloat(parts[2]) || 0;
2568
- return hours * 3600 + minutes * 60 + seconds;
2675
+ const match = VTT_TIMESTAMP_RE.exec(timestamp);
2676
+ if (!match)
2677
+ return void 0;
2678
+ const [, hours = "0", minutes, seconds, millis = "0"] = match;
2679
+ return Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds) + Number(millis) / 1e3;
2569
2680
  }
2570
2681
  function secondsToTimestamp(seconds) {
2571
2682
  const rounded = Math.max(0, Math.floor(seconds));
@@ -2588,6 +2699,8 @@ function extractTimestampedTranscript(vttContent) {
2588
2699
  if (line.includes("-->")) {
2589
2700
  const startTime = line.split(" --> ")[0].trim();
2590
2701
  const timeInSeconds = vttTimestampToSeconds(startTime);
2702
+ if (timeInSeconds === void 0)
2703
+ continue;
2591
2704
  let j = i + 1;
2592
2705
  while (j < lines.length && !lines[j].trim()) {
2593
2706
  j++;
@@ -2616,13 +2729,17 @@ function parseVTTCues(vttContent, options = {}) {
2616
2729
  if (isTimingLine(line)) {
2617
2730
  const [startStr, endStr] = line.split(" --> ").map((s) => s.trim());
2618
2731
  const startTime = vttTimestampToSeconds(startStr);
2619
- const endTime = vttTimestampToSeconds(endStr.split(" ")[0]);
2732
+ const endTime = vttTimestampToSeconds(endStr?.split(/\s+/)[0] ?? "");
2620
2733
  const currentCueIdentifierLine = lines[i - 1]?.trim() ?? "";
2621
2734
  const currentCueIdentifier = isLikelyCueIdentifier({
2622
2735
  line: currentCueIdentifierLine,
2623
2736
  nextLine: line,
2624
2737
  previousCueIdentifier
2625
2738
  }) ? parseNumericCueIdentifier(currentCueIdentifierLine) : null;
2739
+ if (startTime === void 0 || endTime === void 0) {
2740
+ previousCueIdentifier = currentCueIdentifier;
2741
+ continue;
2742
+ }
2626
2743
  const rawTextLines = [];
2627
2744
  let j = i + 1;
2628
2745
  while (j < lines.length && lines[j].trim() && !isTimingLine(lines[j].trim())) {
@@ -4725,7 +4842,7 @@ function transformCueText(rawVtt, transform) {
4725
4842
  const transformed = lines.map((line) => {
4726
4843
  if (line.includes("-->")) {
4727
4844
  const startTimestamp = line.split("-->")[0].trim();
4728
- currentCueStartTime = vttTimestampToSeconds(startTimestamp);
4845
+ currentCueStartTime = vttTimestampToSeconds(startTimestamp) ?? currentCueStartTime;
4729
4846
  inCueText = true;
4730
4847
  return line;
4731
4848
  }
@@ -4757,7 +4874,7 @@ function transformCueTextBlocks(rawVtt, transform) {
4757
4874
  if (line.includes("-->")) {
4758
4875
  flushCueText();
4759
4876
  const startTimestamp = line.split("-->")[0].trim();
4760
- currentCueStartTime = vttTimestampToSeconds(startTimestamp);
4877
+ currentCueStartTime = vttTimestampToSeconds(startTimestamp) ?? currentCueStartTime;
4761
4878
  transformed.push(line);
4762
4879
  cueTextLines = [];
4763
4880
  continue;
@@ -6348,7 +6465,7 @@ async function generateEngagementInsightsInternal(assetId, options, collectedUsa
6348
6465
  }
6349
6466
 
6350
6467
  // src/workflows/generate-text.ts
6351
- import { APICallError as APICallError3, generateText as generateTextWithModel, Output as Output7 } from "ai";
6468
+ import { APICallError as APICallError4, generateText as generateTextWithModel, Output as Output7 } from "ai";
6352
6469
  import { z as z8 } from "zod";
6353
6470
  var GENERATE_TEXT_MAX_VARIANTS = 5;
6354
6471
  var GENERATE_TEXT_MAX_ARTIFACTS = 5;
@@ -6471,7 +6588,7 @@ function resolveGenerateTextOptions(options) {
6471
6588
  }
6472
6589
  return { ...options, variants };
6473
6590
  }
6474
- var LENGTH_TARGET_RATIO = 0.9;
6591
+ var LENGTH_TARGET_RATIO = 0.85;
6475
6592
  function resolveLengthTarget(limit) {
6476
6593
  return Math.max(1, Math.floor(limit.value * LENGTH_TARGET_RATIO));
6477
6594
  }
@@ -6760,122 +6877,124 @@ function readUsage(response) {
6760
6877
  }
6761
6878
  async function extractBriefWithModel(args) {
6762
6879
  "use step";
6763
- const model = await createLanguageModelFromConfig(args.provider, args.modelId, args.credentials);
6764
- const response = await withContentPolicyAwareRetry(() => generateTextWithModel({
6765
- model,
6766
- maxRetries: 0,
6767
- output: Output7.object({
6768
- name: "editorial_brief",
6769
- description: "Source-grounded editorial brief used to write every requested artifact.",
6770
- schema: briefSchema
6771
- }),
6772
- system: args.systemPrompt,
6773
- messages: [
6774
- {
6775
- role: "user",
6776
- content: [
6777
- { type: "text", text: args.userPrompt },
6778
- ...args.imageUrls.map((url) => ({ type: "image", image: url }))
6779
- ]
6780
- }
6781
- ]
6782
- }));
6783
- const output = getGeneratedOutputWithContentPolicyHandling(response);
6784
- if (!output) {
6785
- throw new Error("Editorial brief output missing");
6786
- }
6787
- return {
6788
- brief: clampBriefArrays(briefSchema.parse(output)),
6789
- usage: readUsage(response),
6790
- unexpectedKeys: detectUnexpectedKeysFromRawText(response.text, briefSchema.keyof().options)
6791
- };
6792
- }
6793
- var artifactSchema = z8.object({ content: z8.string() });
6794
- async function generateArtifactWithModel(args) {
6795
- "use step";
6796
- const model = await createLanguageModelFromConfig(args.provider, args.modelId, args.credentials);
6797
- const attempt = async (messages) => {
6880
+ return withModelCallDiagnostics({ phase: "brief", provider: args.provider, modelId: args.modelId }, async () => {
6881
+ const model = await createLanguageModelFromConfig(args.provider, args.modelId, args.credentials);
6798
6882
  const response = await withContentPolicyAwareRetry(() => generateTextWithModel({
6799
6883
  model,
6800
6884
  maxRetries: 0,
6801
6885
  output: Output7.object({
6802
- name: "generated_text",
6803
- description: "One finished artifact written from the editorial brief.",
6804
- schema: artifactSchema
6886
+ name: "editorial_brief",
6887
+ description: "Source-grounded editorial brief used to write every requested artifact.",
6888
+ schema: briefSchema
6805
6889
  }),
6806
6890
  system: args.systemPrompt,
6807
- messages
6891
+ messages: [
6892
+ {
6893
+ role: "user",
6894
+ content: [
6895
+ { type: "text", text: args.userPrompt },
6896
+ ...args.imageUrls.map((url) => ({ type: "image", image: url }))
6897
+ ]
6898
+ }
6899
+ ]
6808
6900
  }));
6809
6901
  const output = getGeneratedOutputWithContentPolicyHandling(response);
6810
6902
  if (!output) {
6811
- throw new Error("Generated text output missing");
6903
+ throw new Error("Editorial brief output missing");
6812
6904
  }
6813
6905
  return {
6814
- content: artifactSchema.parse(output).content.trim(),
6906
+ brief: clampBriefArrays(briefSchema.parse(output)),
6815
6907
  usage: readUsage(response),
6816
- unexpectedKeys: detectUnexpectedKeysFromRawText(response.text, artifactSchema.keyof().options)
6908
+ unexpectedKeys: detectUnexpectedKeysFromRawText(response.text, briefSchema.keyof().options)
6817
6909
  };
6818
- };
6819
- const judge = (content) => content ? findGenerateTextLengthViolation(content, args.limits) : "empty";
6820
- const initialMessages = [{ role: "user", content: args.userPrompt }];
6821
- const first = await attempt(initialMessages);
6822
- const firstViolation = judge(first.content);
6823
- const firstEscaped = hasEscapedLineBreaks(first.content);
6824
- if (!firstViolation && !firstEscaped) {
6825
- return { content: first.content, usages: [first.usage], unexpectedKeys: first.unexpectedKeys };
6826
- }
6827
- let candidate = first;
6828
- const usages = [first.usage];
6829
- const unexpectedKeys = [...first.unexpectedKeys];
6830
- if (firstViolation === "empty" || firstEscaped) {
6831
- const feedback = [
6832
- firstViolation && firstViolation !== "empty" ? describeLengthRevision(firstViolation) : void 0,
6833
- firstEscaped ? ESCAPED_TEXT_REVISION : void 0
6834
- ].filter((line) => Boolean(line)).join("\n");
6835
- const revisionMessages = firstViolation === "empty" ? [{ role: "user", content: `${args.userPrompt}
6910
+ });
6911
+ }
6912
+ var artifactSchema = z8.object({ content: z8.string() });
6913
+ async function generateArtifactWithModel(args) {
6914
+ "use step";
6915
+ const diagnostics = { phase: "draft", provider: args.provider, modelId: args.modelId };
6916
+ return withModelCallDiagnostics(diagnostics, async () => {
6917
+ const model = await createLanguageModelFromConfig(args.provider, args.modelId, args.credentials);
6918
+ const attempt = async (messages) => {
6919
+ const response = await withContentPolicyAwareRetry(() => generateTextWithModel({
6920
+ model,
6921
+ maxRetries: 0,
6922
+ output: Output7.object({
6923
+ name: "generated_text",
6924
+ description: "One finished artifact written from the editorial brief.",
6925
+ schema: artifactSchema
6926
+ }),
6927
+ system: args.systemPrompt,
6928
+ messages
6929
+ }));
6930
+ const output = getGeneratedOutputWithContentPolicyHandling(response);
6931
+ if (!output) {
6932
+ throw new Error("Generated text output missing");
6933
+ }
6934
+ return {
6935
+ content: artifactSchema.parse(output).content.trim(),
6936
+ usage: readUsage(response),
6937
+ unexpectedKeys: detectUnexpectedKeysFromRawText(response.text, artifactSchema.keyof().options)
6938
+ };
6939
+ };
6940
+ const judge = (content) => content ? findGenerateTextLengthViolation(content, args.limits) : "empty";
6941
+ const initialMessages = [{ role: "user", content: args.userPrompt }];
6942
+ const first = await attempt(initialMessages);
6943
+ const firstViolation = judge(first.content);
6944
+ const firstEscaped = hasEscapedLineBreaks(first.content);
6945
+ if (!firstViolation && !firstEscaped) {
6946
+ return { content: first.content, usages: [first.usage], unexpectedKeys: first.unexpectedKeys };
6947
+ }
6948
+ let candidate = first;
6949
+ const usages = [first.usage];
6950
+ const unexpectedKeys = [...first.unexpectedKeys];
6951
+ if (firstViolation === "empty" || firstEscaped) {
6952
+ const feedback = [
6953
+ firstViolation && firstViolation !== "empty" ? describeLengthRevision(firstViolation) : void 0,
6954
+ firstEscaped ? ESCAPED_TEXT_REVISION : void 0
6955
+ ].filter((line) => Boolean(line)).join("\n");
6956
+ const revisionMessages = firstViolation === "empty" ? [{ role: "user", content: `${args.userPrompt}
6836
6957
 
6837
6958
  ${renderSection({ tag: "revision_request", content: "The previous draft was empty. Write the complete artifact." })}` }] : [...initialMessages, { role: "assistant", content: JSON.stringify({ content: first.content }) }, { role: "user", content: renderSection({ tag: "revision_request", content: feedback }) }];
6838
- candidate = await attempt(revisionMessages).catch((error) => rethrowWithTokenUsage(error, usages));
6839
- usages.push(candidate.usage);
6840
- unexpectedKeys.push(...candidate.unexpectedKeys);
6841
- }
6842
- const violation = judge(candidate.content);
6843
- if (!violation || violation === "empty") {
6844
- return { content: candidate.content, usages, unexpectedKeys, violation };
6845
- }
6846
- const repaired = await repairText({
6847
- content: candidate.content,
6848
- limits: args.limits,
6849
- protectedTerms: args.protectedTerms,
6850
- model
6851
- });
6852
- usages.push(...repaired.attempts.flatMap((item) => item.call.usage ? [item.call.usage] : []));
6853
- if (repaired.status !== "valid") {
6854
- const cause = repaired.attempts.at(-1)?.call.errorCause;
6855
- let failure;
6856
- if (isIncompleteGenerationError(cause)) {
6857
- failure = new IncompleteGenerationError(cause.finishReason, cause.rawFinishReason);
6858
- } else if (MuxAiError.is(cause)) {
6859
- failure = new MuxAiError(cause.publicMessage, { type: cause.publicType, retryable: cause.retryable });
6860
- } else {
6861
- const apiError = APICallError3.isInstance(cause) ? cause : void 0;
6862
- console.warn("[@mux/ai] Bounded text repair failed.", {
6863
- provider: args.provider,
6864
- modelId: args.modelId,
6865
- reason: repaired.reason,
6866
- errorName: cause instanceof Error ? cause.name : void 0,
6867
- statusCode: apiError?.statusCode,
6868
- requestId: apiError?.responseHeaders?.["x-request-id"] ?? apiError?.responseHeaders?.["request-id"]
6869
- });
6870
- failure = new MuxAiError("Unable to complete text generation.", {
6871
- type: "processing_error",
6872
- retryable: apiError?.isRetryable ?? true,
6873
- cause
6874
- });
6959
+ diagnostics.phase = "format_repair";
6960
+ candidate = await attempt(revisionMessages).catch((error) => rethrowWithTokenUsage(error, usages));
6961
+ usages.push(candidate.usage);
6962
+ unexpectedKeys.push(...candidate.unexpectedKeys);
6963
+ }
6964
+ const violation = judge(candidate.content);
6965
+ if (!violation || violation === "empty") {
6966
+ return { content: candidate.content, usages, unexpectedKeys, violation };
6967
+ }
6968
+ diagnostics.phase = "length_repair";
6969
+ const repaired = await repairText({
6970
+ content: candidate.content,
6971
+ limits: args.limits,
6972
+ protectedTerms: args.protectedTerms,
6973
+ model
6974
+ });
6975
+ usages.push(...repaired.attempts.flatMap((item) => item.call.usage ? [item.call.usage] : []));
6976
+ if (repaired.status !== "valid") {
6977
+ const cause = repaired.attempts.at(-1)?.call.errorCause;
6978
+ let failure;
6979
+ if (isIncompleteGenerationError(cause)) {
6980
+ failure = new IncompleteGenerationError(cause.finishReason, cause.rawFinishReason);
6981
+ } else if (MuxAiError.is(cause)) {
6982
+ failure = new MuxAiError(cause.publicMessage, { type: cause.publicType, retryable: cause.retryable });
6983
+ } else {
6984
+ const apiError = APICallError4.isInstance(cause) ? cause : void 0;
6985
+ failure = new MuxAiError("Unable to complete text generation.", {
6986
+ type: "processing_error",
6987
+ retryable: apiError?.isRetryable ?? true,
6988
+ cause
6989
+ });
6990
+ }
6991
+ if (typeof cause === "object" && cause !== null && "diagnostics" in cause) {
6992
+ failure.diagnostics = cause.diagnostics;
6993
+ }
6994
+ rethrowWithTokenUsage(failure, usages);
6875
6995
  }
6876
- rethrowWithTokenUsage(failure, usages);
6877
- }
6878
- return { content: repaired.content, usages, unexpectedKeys };
6996
+ return { content: repaired.content, usages, unexpectedKeys };
6997
+ });
6879
6998
  }
6880
6999
  async function generateText7(assetId, options) {
6881
7000
  "use workflow";
@@ -9354,7 +9473,7 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
9354
9473
 
9355
9474
  // src/workflows/translate-captions.ts
9356
9475
  import {
9357
- APICallError as APICallError4,
9476
+ APICallError as APICallError5,
9358
9477
  generateText as generateText9,
9359
9478
  NoObjectGeneratedError as NoObjectGeneratedError3,
9360
9479
  NoOutputGeneratedError as NoOutputGeneratedError2,
@@ -9509,7 +9628,7 @@ function isProviderServiceError(error) {
9509
9628
  if (RetryError2.isInstance(error)) {
9510
9629
  return isProviderServiceError(error.lastError);
9511
9630
  }
9512
- if (APICallError4.isInstance(error)) {
9631
+ if (APICallError5.isInstance(error)) {
9513
9632
  return true;
9514
9633
  }
9515
9634
  if (error instanceof Error && "cause" in error) {