@broberg/ai-sdk 0.44.0 → 0.45.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.
package/dist/index.d.ts CHANGED
@@ -87,6 +87,61 @@ interface Pronunciation {
87
87
  lang?: string;
88
88
  }
89
89
 
90
+ /** One entry of Azure's `[nnnn].word.json`, verbatim field names. */
91
+ interface AzureWordBoundary {
92
+ Text: string;
93
+ /** Milliseconds into the audio. */
94
+ AudioOffset: number;
95
+ /** Milliseconds. */
96
+ Duration: number;
97
+ }
98
+ interface WordTiming {
99
+ /** The spoken word, as Azure reported it. */
100
+ text: string;
101
+ startMs: number;
102
+ endMs: number;
103
+ /** Character span in the SOURCE text. Several spoken words share one span when they
104
+ * came from one substituted manuscript word. */
105
+ sourceStart: number;
106
+ sourceEnd: number;
107
+ }
108
+ interface AlignedWordTimings {
109
+ /** Every word we could place in the source, in audio order. */
110
+ words: WordTiming[];
111
+ /** Spoken words we could NOT place, in order, verbatim.
112
+ *
113
+ * Named rather than dropped, and never given a guessed offset. "I could not place
114
+ * this" and "it was here" must not be the same answer — the same discipline as
115
+ * `rerank.unscored` (F052). A highlighter that treats `words` as complete must look
116
+ * here. */
117
+ unaligned: string[];
118
+ }
119
+ /** A pronunciation entry, narrowed to what alignment needs. */
120
+ interface AliasEntry {
121
+ word: string;
122
+ alias?: string;
123
+ }
124
+ /**
125
+ * Map Azure's spoken word list onto positions in the SOURCE text.
126
+ *
127
+ * Two rules carry the whole function:
128
+ *
129
+ * **The walk only moves FORWARD.** That is what puts a repeated word on its own
130
+ * occurrence — "AI er AI" must not place the second on the first. Searching from 0 each
131
+ * time passes every test with distinct words and fails silently on real prose.
132
+ *
133
+ * **A substitution is matched as a RUN, not word by word.** Azure speaks SSML, so
134
+ * `<sub alias='broberg punktum a i'>broberg.ai</sub>` puts four words in the list where
135
+ * the manuscript has one. Matching them individually is not merely imprecise, it is
136
+ * wrong in a way that bites every Danish text: MEASURED on cms's own dictionary entry,
137
+ * a per-word map made the alias's "i" swallow the next real Danish word "i" in
138
+ * "broberg.ai i dag". An alias's parts are often ordinary words ("a", "i", "punktum"),
139
+ * so a lookup table cannot be the mechanism — the run has to be consumed positionally.
140
+ */
141
+ declare function alignWordTimings(text: string, words: AzureWordBoundary[], opts?: {
142
+ pronunciations?: AliasEntry[];
143
+ }): AlignedWordTimings;
144
+
90
145
  /** How a call reaches the model. `http` = provider REST API; `subprocess` = local
91
146
  * `claude -p` CLI (Max plan, costUsd 0). */
92
147
  type Transport = "http" | "subprocess";
@@ -492,6 +547,14 @@ interface PodcastResult {
492
547
  /** Episode audio bytes. */
493
548
  audio: Uint8Array;
494
549
  mimeType: string;
550
+ /** F055 — per-word timings, present only when `wordTimings` was asked for AND the
551
+ * route produces them. Its ABSENCE says "not available here"; an empty `words` array
552
+ * would say "the text had no words", which is a different claim.
553
+ *
554
+ * `sourceStart`/`sourceEnd` index the ORIGINAL text you passed, not the SSML we sent
555
+ * — Azure reports only audio time, so the link back to the manuscript is derived
556
+ * here. `unaligned` names any spoken word that could not be placed. */
557
+ wordTimings?: AlignedWordTimings;
495
558
  usage: Usage;
496
559
  }
497
560
 
@@ -512,6 +575,16 @@ interface TtsRequest {
512
575
  * into SSML** — the substitution happens adapter-side AFTER the text is escaped, so
513
576
  * `text` can never inject markup, and `alias`/`ipa` are escaped too. */
514
577
  pronunciations?: Pronunciation[];
578
+ /** F055 — ask for per-word timings so a reader can highlight each word as it is
579
+ * spoken. Azure only. Absent from the result means "this route does not produce
580
+ * them", which is a different answer from an empty list.
581
+ *
582
+ * **This changes the ROUTE, not just the payload.** The real-time endpoint returns
583
+ * audio only; word boundaries exist solely on Azure's asynchronous batch synthesis
584
+ * API, so the call becomes submit → poll → fetch. Microsoft quotes 10–20s for half
585
+ * of all jobs and up to 120s for 95%. Worth it for a pre-generated, cached reading;
586
+ * wrong for anything a user is waiting on. */
587
+ wordTimings?: boolean;
515
588
  spec: TierSpec;
516
589
  }
517
590
  interface BatchRequestItem {
@@ -1322,6 +1395,7 @@ declare const imageInputSchema: z.ZodObject<{
1322
1395
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "video" | "embedding" | undefined;
1323
1396
  seed?: number | undefined;
1324
1397
  purpose?: string | undefined;
1398
+ outputFormat?: "jpeg" | "png" | "webp" | undefined;
1325
1399
  loras?: {
1326
1400
  path: string;
1327
1401
  scale?: number | undefined;
@@ -1343,7 +1417,6 @@ declare const imageInputSchema: z.ZodObject<{
1343
1417
  finetune?: string | undefined;
1344
1418
  finetuneStrength?: number | undefined;
1345
1419
  referenceImages?: (string | Uint8Array<ArrayBuffer>)[] | undefined;
1346
- outputFormat?: "jpeg" | "png" | "webp" | undefined;
1347
1420
  safetyTolerance?: number | undefined;
1348
1421
  retryOnBlack?: boolean | undefined;
1349
1422
  }, {
@@ -1351,6 +1424,7 @@ declare const imageInputSchema: z.ZodObject<{
1351
1424
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "video" | "embedding" | undefined;
1352
1425
  seed?: number | undefined;
1353
1426
  purpose?: string | undefined;
1427
+ outputFormat?: "jpeg" | "png" | "webp" | undefined;
1354
1428
  loras?: {
1355
1429
  path: string;
1356
1430
  scale?: number | undefined;
@@ -1372,7 +1446,6 @@ declare const imageInputSchema: z.ZodObject<{
1372
1446
  finetune?: string | undefined;
1373
1447
  finetuneStrength?: number | undefined;
1374
1448
  referenceImages?: (string | Uint8Array<ArrayBuffer>)[] | undefined;
1375
- outputFormat?: "jpeg" | "png" | "webp" | undefined;
1376
1449
  safetyTolerance?: number | undefined;
1377
1450
  retryOnBlack?: boolean | undefined;
1378
1451
  }>;
@@ -1873,6 +1946,13 @@ declare const podcastInputSchema: z.ZodObject<{
1873
1946
  voices: Record<string, string>;
1874
1947
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "video" | "embedding" | undefined;
1875
1948
  purpose?: string | undefined;
1949
+ pronunciations?: {
1950
+ word: string;
1951
+ matchInCompounds?: boolean | undefined;
1952
+ alias?: string | undefined;
1953
+ ipa?: string | undefined;
1954
+ lang?: string | undefined;
1955
+ }[] | undefined;
1876
1956
  override?: {
1877
1957
  provider?: string | undefined;
1878
1958
  model?: string | undefined;
@@ -1885,13 +1965,6 @@ declare const podcastInputSchema: z.ZodObject<{
1885
1965
  })[] | undefined;
1886
1966
  labels?: Record<string, string> | undefined;
1887
1967
  format?: string | undefined;
1888
- pronunciations?: {
1889
- word: string;
1890
- matchInCompounds?: boolean | undefined;
1891
- alias?: string | undefined;
1892
- ipa?: string | undefined;
1893
- lang?: string | undefined;
1894
- }[] | undefined;
1895
1968
  }, {
1896
1969
  script: {
1897
1970
  text: string;
@@ -1900,6 +1973,13 @@ declare const podcastInputSchema: z.ZodObject<{
1900
1973
  voices: Record<string, string>;
1901
1974
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "video" | "embedding" | undefined;
1902
1975
  purpose?: string | undefined;
1976
+ pronunciations?: {
1977
+ word: string;
1978
+ matchInCompounds?: boolean | undefined;
1979
+ alias?: string | undefined;
1980
+ ipa?: string | undefined;
1981
+ lang?: string | undefined;
1982
+ }[] | undefined;
1903
1983
  override?: {
1904
1984
  provider?: string | undefined;
1905
1985
  model?: string | undefined;
@@ -1912,13 +1992,6 @@ declare const podcastInputSchema: z.ZodObject<{
1912
1992
  })[] | undefined;
1913
1993
  labels?: Record<string, string> | undefined;
1914
1994
  format?: string | undefined;
1915
- pronunciations?: {
1916
- word: string;
1917
- matchInCompounds?: boolean | undefined;
1918
- alias?: string | undefined;
1919
- ipa?: string | undefined;
1920
- lang?: string | undefined;
1921
- }[] | undefined;
1922
1995
  }>;
1923
1996
  declare const ttsInputSchema: z.ZodObject<{
1924
1997
  tier: z.ZodOptional<z.ZodEnum<["fast", "smart", "powerful", "cheap", "vision", "video", "embedding"]>>;
@@ -1974,6 +2047,8 @@ declare const ttsInputSchema: z.ZodObject<{
1974
2047
  ipa?: string | undefined;
1975
2048
  lang?: string | undefined;
1976
2049
  }>, "many">>;
2050
+ /** F055 — per-word timings (Azure only). Changes the route to batch synthesis. */
2051
+ wordTimings: z.ZodOptional<z.ZodBoolean>;
1977
2052
  /** F037: voice to use if `voice` is one we know the provider has retired. Without
1978
2053
  * it a retired voice throws VoiceUnavailableError rather than reaching the API.
1979
2054
  *
@@ -1993,6 +2068,14 @@ declare const ttsInputSchema: z.ZodObject<{
1993
2068
  voice: string;
1994
2069
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "video" | "embedding" | undefined;
1995
2070
  purpose?: string | undefined;
2071
+ wordTimings?: boolean | undefined;
2072
+ pronunciations?: {
2073
+ word: string;
2074
+ matchInCompounds?: boolean | undefined;
2075
+ alias?: string | undefined;
2076
+ ipa?: string | undefined;
2077
+ lang?: string | undefined;
2078
+ }[] | undefined;
1996
2079
  override?: {
1997
2080
  provider?: string | undefined;
1998
2081
  model?: string | undefined;
@@ -2006,13 +2089,6 @@ declare const ttsInputSchema: z.ZodObject<{
2006
2089
  labels?: Record<string, string> | undefined;
2007
2090
  lang?: string | undefined;
2008
2091
  format?: string | undefined;
2009
- pronunciations?: {
2010
- word: string;
2011
- matchInCompounds?: boolean | undefined;
2012
- alias?: string | undefined;
2013
- ipa?: string | undefined;
2014
- lang?: string | undefined;
2015
- }[] | undefined;
2016
2092
  voiceFallback?: string | undefined;
2017
2093
  rate?: number | undefined;
2018
2094
  }, {
@@ -2020,6 +2096,14 @@ declare const ttsInputSchema: z.ZodObject<{
2020
2096
  voice: string;
2021
2097
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "video" | "embedding" | undefined;
2022
2098
  purpose?: string | undefined;
2099
+ wordTimings?: boolean | undefined;
2100
+ pronunciations?: {
2101
+ word: string;
2102
+ matchInCompounds?: boolean | undefined;
2103
+ alias?: string | undefined;
2104
+ ipa?: string | undefined;
2105
+ lang?: string | undefined;
2106
+ }[] | undefined;
2023
2107
  override?: {
2024
2108
  provider?: string | undefined;
2025
2109
  model?: string | undefined;
@@ -2033,13 +2117,6 @@ declare const ttsInputSchema: z.ZodObject<{
2033
2117
  labels?: Record<string, string> | undefined;
2034
2118
  lang?: string | undefined;
2035
2119
  format?: string | undefined;
2036
- pronunciations?: {
2037
- word: string;
2038
- matchInCompounds?: boolean | undefined;
2039
- alias?: string | undefined;
2040
- ipa?: string | undefined;
2041
- lang?: string | undefined;
2042
- }[] | undefined;
2043
2120
  voiceFallback?: string | undefined;
2044
2121
  rate?: number | undefined;
2045
2122
  }>;
@@ -2313,6 +2390,11 @@ declare function azureAdapter(config?: {
2313
2390
  resource?: string;
2314
2391
  /** Fast-transcription api-version (overrides the GA default). */
2315
2392
  sttApiVersion?: string;
2393
+ /** F055 — how long to wait for a batch synthesis job. Default 180s (Microsoft's own
2394
+ * 95th percentile is 120s). */
2395
+ batchTimeoutMs?: number;
2396
+ /** F055 — poll interval for batch synthesis. Default 3s. */
2397
+ batchPollMs?: number;
2316
2398
  /** phraseList biasing weight (0–2) applied when a call passes `phrases`. Default 1.5. */
2317
2399
  sttBiasingWeight?: number;
2318
2400
  }): ProviderAdapter;
@@ -2432,8 +2514,8 @@ declare const falStubAdapter: ProviderAdapter;
2432
2514
  * wires the live adapters. */
2433
2515
  declare const stubProviders: Record<string, ProviderAdapter>;
2434
2516
 
2435
- declare const VERSION: "0.44.0";
2436
- declare const SDK_TAG: "@broberg/ai-sdk@0.44.0";
2517
+ declare const VERSION: "0.45.0";
2518
+ declare const SDK_TAG: "@broberg/ai-sdk@0.45.0";
2437
2519
 
2438
2520
  /** Built-in defaults. Every entry is overridable via AiConfig.defaults or a
2439
2521
  * per-call override.
@@ -2844,4 +2926,4 @@ interface StreamTransportRequest extends TransportRequest {
2844
2926
  */
2845
2927
  declare function streamTransport(req: StreamTransportRequest): AsyncIterable<string>;
2846
2928
 
2847
- export { AZURE_DANISH_VOICES, AZURE_DANISH_VOICE_LIST, type AiClient, type AiConfig, type AzureVoiceInfo, type BatchJob, type BatchRequestItem, type BatchResultItem, type BflAdapterConfig, type BflCredits, type BudgetConfig, BudgetExceededError, BudgetGuard, type BudgetStore, type CallOptions, type Capability, type ChatInput, type ChatRequest, type ChatResult, type ChatStreamEvent, type CheckVoiceOptions, type ClassifyInput, type ClassifyResult, type ContentPart, type Contracts, type CostQuery, type CostSink, type CostSummary, type CostSummaryQuery, type CostTimeseriesQuery, DEFAULT_TIER_MAP, type DesignInput, type DesignResult, type DialogueRequest, type DialogueTurn, type DiscordSinkConfig, ELEVENLABS_DANISH_VOICES, type EmbeddingInput, type EmbeddingRequest, type EmbeddingResult, type ExtractInput, type ExtractResult, type FalAdapterConfig, type HttpResponse, type ImageInput, type ImageRequest, type ImageResult, type LoraWeight, type Message, type MockupInput, type MockupResult, type ModerationInput, type ModerationItem, type ModerationRequest, type ModerationResult, type OcrInput, type OcrPage, type OcrRequest, type OcrResult, type OpenAICompatibleConfig, type PodcastInput, type PodcastResult, type PricingEntry, type ProviderAdapter, type RefreshOptions, type RefreshResult, type Region, type RerankInput, type RerankResult, type Role, SDK_TAG, type SqliteBudgetStoreConfig, type SqliteSinkConfig, StreamHttpError, type SubprocessResponse, type Tier, type TierSpec, type Tool, type ToolCall, type TrainStyleInput, type TrainStyleRequest, type TrainStyleResult, type TranscribeInput, type TranscribeRequest, type TranscribeResult, type TranslateInput, type TranslateResult, type Transport, type TransportRequest, type TransportResponse, type TtsInput, type TtsRequest, type UpmetricsCostClientConfig, UpmetricsCostError, type UpmetricsCostRow, type UpmetricsCostSummary, type UpmetricsCostTimeseries, type UpmetricsSinkConfig, type Usage, VERSION, type VideoInput, type VisionInput, type VoiceInfo, type VoiceProvider, type VoiceResolveResult, type VoiceStatus, VoiceUnavailableError, aiConfigSchema, anthropicAdapter, anthropicApiAdapter, anthropicSubprocessAdapter, azureAdapter, bflAdapter, bflCredits, chatInputSchema, checkVoice, classifyRegionName, computeCost, createAI, deepinfraAdapter, deeplAdapter, deepseekAdapter, defaultProviders, discordSink, elevenlabsAdapter, embeddingInputSchema, falAdapter, falStubAdapter, freshUsage, fromProviderToolCall, geminiAdapter, getCostSummary, getPrice, httpTransport, imageInputSchema, listAzureDanishVoices, listVoices, makeContracts, makeOpenAICompatibleAdapter, messageSchema, mistralAdapter, mistralStubAdapter, multiSink, noopSink, openaiAdapter, openaiStubAdapter, openrouterAdapter, parseClaudeCliJson, parseJsonLoose, refreshAvailability, regionOfHost, regionOfProvider, requestyAdapter, resetRefreshState, resetRegistry, resolveAzureVoice, resolveTier, resolveVoice, sqliteBudgetStore, sqliteSink, streamTransport, stubProviders, subprocessTransport, tierSpecSchema, toProviderTools, toolSchema, translateInputSchema, upmetricsCostClient, upmetricsSink, usdFromMicro, vertexAdapter, visionInputSchema };
2929
+ export { AZURE_DANISH_VOICES, AZURE_DANISH_VOICE_LIST, type AiClient, type AiConfig, type AlignedWordTimings, type AzureVoiceInfo, type AzureWordBoundary, type BatchJob, type BatchRequestItem, type BatchResultItem, type BflAdapterConfig, type BflCredits, type BudgetConfig, BudgetExceededError, BudgetGuard, type BudgetStore, type CallOptions, type Capability, type ChatInput, type ChatRequest, type ChatResult, type ChatStreamEvent, type CheckVoiceOptions, type ClassifyInput, type ClassifyResult, type ContentPart, type Contracts, type CostQuery, type CostSink, type CostSummary, type CostSummaryQuery, type CostTimeseriesQuery, DEFAULT_TIER_MAP, type DesignInput, type DesignResult, type DialogueRequest, type DialogueTurn, type DiscordSinkConfig, ELEVENLABS_DANISH_VOICES, type EmbeddingInput, type EmbeddingRequest, type EmbeddingResult, type ExtractInput, type ExtractResult, type FalAdapterConfig, type HttpResponse, type ImageInput, type ImageRequest, type ImageResult, type LoraWeight, type Message, type MockupInput, type MockupResult, type ModerationInput, type ModerationItem, type ModerationRequest, type ModerationResult, type OcrInput, type OcrPage, type OcrRequest, type OcrResult, type OpenAICompatibleConfig, type PodcastInput, type PodcastResult, type PricingEntry, type ProviderAdapter, type RefreshOptions, type RefreshResult, type Region, type RerankInput, type RerankResult, type Role, SDK_TAG, type SqliteBudgetStoreConfig, type SqliteSinkConfig, StreamHttpError, type SubprocessResponse, type Tier, type TierSpec, type Tool, type ToolCall, type TrainStyleInput, type TrainStyleRequest, type TrainStyleResult, type TranscribeInput, type TranscribeRequest, type TranscribeResult, type TranslateInput, type TranslateResult, type Transport, type TransportRequest, type TransportResponse, type TtsInput, type TtsRequest, type UpmetricsCostClientConfig, UpmetricsCostError, type UpmetricsCostRow, type UpmetricsCostSummary, type UpmetricsCostTimeseries, type UpmetricsSinkConfig, type Usage, VERSION, type VideoInput, type VisionInput, type VoiceInfo, type VoiceProvider, type VoiceResolveResult, type VoiceStatus, VoiceUnavailableError, type WordTiming, aiConfigSchema, alignWordTimings, anthropicAdapter, anthropicApiAdapter, anthropicSubprocessAdapter, azureAdapter, bflAdapter, bflCredits, chatInputSchema, checkVoice, classifyRegionName, computeCost, createAI, deepinfraAdapter, deeplAdapter, deepseekAdapter, defaultProviders, discordSink, elevenlabsAdapter, embeddingInputSchema, falAdapter, falStubAdapter, freshUsage, fromProviderToolCall, geminiAdapter, getCostSummary, getPrice, httpTransport, imageInputSchema, listAzureDanishVoices, listVoices, makeContracts, makeOpenAICompatibleAdapter, messageSchema, mistralAdapter, mistralStubAdapter, multiSink, noopSink, openaiAdapter, openaiStubAdapter, openrouterAdapter, parseClaudeCliJson, parseJsonLoose, refreshAvailability, regionOfHost, regionOfProvider, requestyAdapter, resetRefreshState, resetRegistry, resolveAzureVoice, resolveTier, resolveVoice, sqliteBudgetStore, sqliteSink, streamTransport, stubProviders, subprocessTransport, tierSpecSchema, toProviderTools, toolSchema, translateInputSchema, upmetricsCostClient, upmetricsSink, usdFromMicro, vertexAdapter, visionInputSchema };
package/dist/index.js CHANGED
@@ -1658,6 +1658,124 @@ function elevenlabsAdapter(config = {}) {
1658
1658
  return { name: "elevenlabs", dialogue, tts, listVoices: listVoices2 };
1659
1659
  }
1660
1660
 
1661
+ // src/providers/word-timings.ts
1662
+ var PUNCTUATION = /* @__PURE__ */ new Set([".", ",", "!", "?", ":", ";", "\u2026", "\u2014", "\u2013", '"', "'", "(", ")"]);
1663
+ var fold = (s) => s.toLowerCase();
1664
+ function alignWordTimings(text, words, opts = {}) {
1665
+ const out = [];
1666
+ const unaligned = [];
1667
+ const haystack = fold(text);
1668
+ const aliases = (opts.pronunciations ?? []).filter((p) => typeof p.alias === "string" && p.alias.trim() !== "").map((p) => ({ word: p.word, parts: p.alias.trim().split(/\s+/).map(fold) })).sort((a, b) => b.parts.length - a.parts.length);
1669
+ const spoken = words.filter((w) => !PUNCTUATION.has(w.Text.trim()));
1670
+ let cursor = 0;
1671
+ let i = 0;
1672
+ while (i < spoken.length) {
1673
+ let consumed = false;
1674
+ for (const a of aliases) {
1675
+ if (i + a.parts.length > spoken.length) continue;
1676
+ const matches = a.parts.every((part, k) => fold(spoken[i + k].Text) === part);
1677
+ if (!matches) continue;
1678
+ const at2 = haystack.indexOf(fold(a.word), cursor);
1679
+ if (at2 === -1) continue;
1680
+ const span = { start: at2, end: at2 + a.word.length };
1681
+ for (let k = 0; k < a.parts.length; k++) {
1682
+ const w2 = spoken[i + k];
1683
+ out.push({
1684
+ text: w2.Text,
1685
+ startMs: w2.AudioOffset,
1686
+ endMs: w2.AudioOffset + w2.Duration,
1687
+ sourceStart: span.start,
1688
+ sourceEnd: span.end
1689
+ });
1690
+ }
1691
+ cursor = span.end;
1692
+ i += a.parts.length;
1693
+ consumed = true;
1694
+ break;
1695
+ }
1696
+ if (consumed) continue;
1697
+ const w = spoken[i];
1698
+ const at = haystack.indexOf(fold(w.Text), cursor);
1699
+ if (at === -1) {
1700
+ unaligned.push(w.Text);
1701
+ } else {
1702
+ out.push({
1703
+ text: w.Text,
1704
+ startMs: w.AudioOffset,
1705
+ endMs: w.AudioOffset + w.Duration,
1706
+ sourceStart: at,
1707
+ sourceEnd: at + w.Text.length
1708
+ });
1709
+ cursor = at + w.Text.length;
1710
+ }
1711
+ i++;
1712
+ }
1713
+ return { words: out, unaligned };
1714
+ }
1715
+
1716
+ // src/providers/zip.ts
1717
+ import { inflateRawSync } from "zlib";
1718
+ var EOCD_SIG = 101010256;
1719
+ var CEN_SIG = 33639248;
1720
+ function readZipEntry(zip, name) {
1721
+ const view = new DataView(zip.buffer, zip.byteOffset, zip.byteLength);
1722
+ let eocd = -1;
1723
+ for (let i = zip.length - 22; i >= 0; i--) {
1724
+ if (view.getUint32(i, true) === EOCD_SIG) {
1725
+ eocd = i;
1726
+ break;
1727
+ }
1728
+ }
1729
+ if (eocd === -1) throw new Error("readZipEntry: not a ZIP archive (no end-of-central-directory record)");
1730
+ const entryCount = view.getUint16(eocd + 10, true);
1731
+ let p = view.getUint32(eocd + 16, true);
1732
+ for (let n = 0; n < entryCount; n++) {
1733
+ if (view.getUint32(p, true) !== CEN_SIG) {
1734
+ throw new Error(`readZipEntry: corrupt central directory at entry ${n}`);
1735
+ }
1736
+ const method = view.getUint16(p + 10, true);
1737
+ const compressedSize = view.getUint32(p + 20, true);
1738
+ const nameLen = view.getUint16(p + 28, true);
1739
+ const extraLen = view.getUint16(p + 30, true);
1740
+ const commentLen = view.getUint16(p + 32, true);
1741
+ const localOffset = view.getUint32(p + 42, true);
1742
+ const entryName = new TextDecoder().decode(zip.subarray(p + 46, p + 46 + nameLen));
1743
+ if (entryName === name) {
1744
+ const lNameLen = view.getUint16(localOffset + 26, true);
1745
+ const lExtraLen = view.getUint16(localOffset + 28, true);
1746
+ const start = localOffset + 30 + lNameLen + lExtraLen;
1747
+ const raw = zip.subarray(start, start + compressedSize);
1748
+ if (method === 0) return new Uint8Array(raw);
1749
+ if (method === 8) return new Uint8Array(inflateRawSync(raw));
1750
+ throw new Error(`readZipEntry: "${name}" uses unsupported compression method ${method}`);
1751
+ }
1752
+ p += 46 + nameLen + extraLen + commentLen;
1753
+ }
1754
+ return void 0;
1755
+ }
1756
+ function listZipEntries(zip) {
1757
+ const view = new DataView(zip.buffer, zip.byteOffset, zip.byteLength);
1758
+ let eocd = -1;
1759
+ for (let i = zip.length - 22; i >= 0; i--) {
1760
+ if (view.getUint32(i, true) === EOCD_SIG) {
1761
+ eocd = i;
1762
+ break;
1763
+ }
1764
+ }
1765
+ if (eocd === -1) throw new Error("listZipEntries: not a ZIP archive (no end-of-central-directory record)");
1766
+ const entryCount = view.getUint16(eocd + 10, true);
1767
+ let p = view.getUint32(eocd + 16, true);
1768
+ const names = [];
1769
+ for (let n = 0; n < entryCount; n++) {
1770
+ const nameLen = view.getUint16(p + 28, true);
1771
+ const extraLen = view.getUint16(p + 30, true);
1772
+ const commentLen = view.getUint16(p + 32, true);
1773
+ names.push(new TextDecoder().decode(zip.subarray(p + 46, p + 46 + nameLen)));
1774
+ p += 46 + nameLen + extraLen + commentLen;
1775
+ }
1776
+ return names;
1777
+ }
1778
+
1661
1779
  // src/providers/azure.ts
1662
1780
  var DEFAULT_STT_API_VERSION = "2025-10-15";
1663
1781
  var DEFAULT_REGION = "westeurope";
@@ -1739,10 +1857,9 @@ function azureAdapter(config = {}) {
1739
1857
  usage.costUsd = chars / 1e3 * (config.pricePer1kChars ?? getMediaPrice("azure", "tts")?.usd ?? 0);
1740
1858
  return usage;
1741
1859
  }
1742
- async function tts(req) {
1860
+ function buildSsml(req) {
1743
1861
  const voice = resolveAzureVoice(req.voiceId);
1744
1862
  const lang = req.lang ?? localeOf(voice);
1745
- const format = req.format ?? DEFAULT_FORMAT;
1746
1863
  assertPronunciations(req.pronunciations, "azure");
1747
1864
  const escaped = applyPronunciations(
1748
1865
  xmlEscape(req.text),
@@ -1752,7 +1869,11 @@ function azureAdapter(config = {}) {
1752
1869
  );
1753
1870
  const effRate = req.rate ?? AZURE_DANISH_VOICE_LIST.find((v) => v.voiceId === voice)?.defaultRate;
1754
1871
  const inner = effRate != null && effRate !== 1 ? `<prosody rate='${effRate}'>${escaped}</prosody>` : escaped;
1755
- const ssml = `<speak version='1.0' xml:lang='${lang}'><voice name='${voice}'>${inner}</voice></speak>`;
1872
+ return `<speak version='1.0' xml:lang='${lang}'><voice name='${voice}'>${inner}</voice></speak>`;
1873
+ }
1874
+ async function tts(req) {
1875
+ if (req.wordTimings) return ttsBatch(req);
1876
+ const format = req.format ?? DEFAULT_FORMAT;
1756
1877
  const res = await fetchImpl(
1757
1878
  `https://${region()}.tts.speech.microsoft.com/cognitiveservices/v1`,
1758
1879
  {
@@ -1762,7 +1883,7 @@ function azureAdapter(config = {}) {
1762
1883
  "Content-Type": "application/ssml+xml",
1763
1884
  "X-Microsoft-OutputFormat": format
1764
1885
  },
1765
- body: ssml
1886
+ body: buildSsml(req)
1766
1887
  }
1767
1888
  );
1768
1889
  if (!res.ok) {
@@ -1772,6 +1893,82 @@ function azureAdapter(config = {}) {
1772
1893
  const audio = new Uint8Array(await res.arrayBuffer());
1773
1894
  return { audio, mimeType: "audio/mpeg", usage: priceFor(req.text.length, req.spec.model) };
1774
1895
  }
1896
+ async function ttsBatch(req) {
1897
+ const host = sttBaseUrl();
1898
+ const api = "api-version=2024-04-01";
1899
+ const id = `wt-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
1900
+ const headers = { "Ocp-Apim-Subscription-Key": key(), "Content-Type": "application/json" };
1901
+ const put = await fetchImpl(`${host}/texttospeech/batchsyntheses/${id}?${api}`, {
1902
+ method: "PUT",
1903
+ headers,
1904
+ body: JSON.stringify({
1905
+ inputKind: "SSML",
1906
+ inputs: [{ content: buildSsml(req) }],
1907
+ properties: {
1908
+ wordBoundaryEnabled: true,
1909
+ // ONE audio file and ONE word list for the whole text. Without it a chunked
1910
+ // input yields per-chunk offsets that somebody has to re-base — the kind of
1911
+ // arithmetic that looks right and is 40 ms wrong by the end.
1912
+ concatenateResult: true,
1913
+ ...req.format ? { outputFormat: req.format } : {}
1914
+ }
1915
+ })
1916
+ });
1917
+ if (!put.ok) {
1918
+ const body = await put.text().catch(() => "");
1919
+ throw new Error(`azure batch synthesis submit ${put.status}: ${body.slice(0, 300)}`);
1920
+ }
1921
+ const deadline = Date.now() + (config.batchTimeoutMs ?? 18e4);
1922
+ let resultUrl = "";
1923
+ for (; ; ) {
1924
+ const get = await fetchImpl(`${host}/texttospeech/batchsyntheses/${id}?${api}`, {
1925
+ headers: { "Ocp-Apim-Subscription-Key": key() }
1926
+ });
1927
+ if (!get.ok) {
1928
+ const body = await get.text().catch(() => "");
1929
+ throw new Error(`azure batch synthesis poll ${get.status}: ${body.slice(0, 300)}`);
1930
+ }
1931
+ const job = await get.json();
1932
+ if (job.status === "Succeeded") {
1933
+ resultUrl = job.outputs?.result ?? "";
1934
+ break;
1935
+ }
1936
+ if (job.status === "Failed") throw new Error(`azure batch synthesis ${id} failed`);
1937
+ if (Date.now() > deadline) {
1938
+ throw new Error(
1939
+ `azure batch synthesis ${id} still "${job.status}" after ${Math.round((config.batchTimeoutMs ?? 18e4) / 1e3)}s`
1940
+ );
1941
+ }
1942
+ await new Promise((r) => setTimeout(r, config.batchPollMs ?? 3e3));
1943
+ }
1944
+ if (!resultUrl) throw new Error(`azure batch synthesis ${id} succeeded without a result URL`);
1945
+ const zipRes = await fetchImpl(resultUrl, { headers: { "Ocp-Apim-Subscription-Key": key() } });
1946
+ if (!zipRes.ok) {
1947
+ throw new Error(`azure batch synthesis results ${zipRes.status}`);
1948
+ }
1949
+ const zip = new Uint8Array(await zipRes.arrayBuffer());
1950
+ const audio = readZipEntry(zip, "0001.wav");
1951
+ if (!audio) {
1952
+ throw new Error(`azure batch synthesis: no 0001.wav in results; archive holds ${listZipEntries(zip).join(", ")}`);
1953
+ }
1954
+ const wordsRaw = readZipEntry(zip, "0001.word.json");
1955
+ if (!wordsRaw) {
1956
+ throw new Error(
1957
+ `azure batch synthesis: wordTimings was requested but the archive has no 0001.word.json; it holds ${listZipEntries(zip).join(", ")}`
1958
+ );
1959
+ }
1960
+ const boundaries = JSON.parse(new TextDecoder().decode(wordsRaw));
1961
+ return {
1962
+ audio,
1963
+ // Batch defaults to riff PCM, not mp3 — saying audio/mpeg here would be a lie the
1964
+ // browser would act on.
1965
+ mimeType: req.format?.includes("mp3") ? "audio/mpeg" : "audio/wav",
1966
+ // Aligned against the ORIGINAL text, with the dictionary, so the offsets index the
1967
+ // manuscript rather than the SSML we sent.
1968
+ wordTimings: alignWordTimings(req.text, boundaries, { pronunciations: req.pronunciations }),
1969
+ usage: priceFor(req.text.length, req.spec.model)
1970
+ };
1971
+ }
1775
1972
  async function transcribe(req) {
1776
1973
  const locale = toAzureLocale(req.language);
1777
1974
  const definition = { locales: [locale] };
@@ -3085,6 +3282,8 @@ var ttsInputSchema = z.object({
3085
3282
  /** F051 — see TtsRequest.pronunciations. The alias/ipa exclusivity is enforced in
3086
3283
  * the adapter, not here: the message must name the provider that cannot do it. */
3087
3284
  pronunciations: z.array(pronunciationSchema).optional(),
3285
+ /** F055 — per-word timings (Azure only). Changes the route to batch synthesis. */
3286
+ wordTimings: z.boolean().optional(),
3088
3287
  /** F037: voice to use if `voice` is one we know the provider has retired. Without
3089
3288
  * it a retired voice throws VoiceUnavailableError rather than reaching the API.
3090
3289
  *
@@ -3126,8 +3325,8 @@ var aiConfigSchema = z.object({
3126
3325
  });
3127
3326
 
3128
3327
  // src/version.ts
3129
- var VERSION = "0.44.0";
3130
- var SDK_TAG = "@broberg/ai-sdk@0.44.0";
3328
+ var VERSION = "0.45.0";
3329
+ var SDK_TAG = "@broberg/ai-sdk@0.45.0";
3131
3330
 
3132
3331
  // src/cost/sinks/upmetrics.ts
3133
3332
  function upmetricsSink(config) {
@@ -3682,6 +3881,7 @@ function createAI(config = {}) {
3682
3881
  format: input.format,
3683
3882
  rate: input.rate,
3684
3883
  pronunciations: input.pronunciations,
3884
+ wordTimings: input.wordTimings,
3685
3885
  spec
3686
3886
  });
3687
3887
  }
@@ -4150,6 +4350,7 @@ export {
4150
4350
  VERSION,
4151
4351
  VoiceUnavailableError,
4152
4352
  aiConfigSchema,
4353
+ alignWordTimings,
4153
4354
  anthropicAdapter,
4154
4355
  anthropicApiAdapter,
4155
4356
  anthropicSubprocessAdapter,