@scalemule/nextjs 0.1.67 → 0.1.68

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/audio.d.mts CHANGED
@@ -202,6 +202,7 @@ declare class NetworkAudioController {
202
202
  constructor(resolve: ResolveNetworkAudio);
203
203
  setResolver(resolve: ResolveNetworkAudio): void;
204
204
  getSnapshot: () => NetworkAudioSnapshot;
205
+ getPosition: () => number;
205
206
  subscribe: (listener: () => void) => () => void;
206
207
  private patch;
207
208
  attach(audio: HTMLAudioElement): void;
@@ -292,6 +293,7 @@ interface NetworkAudioContextValue {
292
293
  notice: string | null;
293
294
  command: (command: NetworkAudioCommand) => void;
294
295
  openNetworkPlayer?: () => void;
296
+ getPosition?: () => number;
295
297
  highlightAvailable: boolean;
296
298
  highlightEnabled: boolean;
297
299
  setHighlightEnabled: (enabled: boolean) => void;
package/dist/audio.d.ts CHANGED
@@ -202,6 +202,7 @@ declare class NetworkAudioController {
202
202
  constructor(resolve: ResolveNetworkAudio);
203
203
  setResolver(resolve: ResolveNetworkAudio): void;
204
204
  getSnapshot: () => NetworkAudioSnapshot;
205
+ getPosition: () => number;
205
206
  subscribe: (listener: () => void) => () => void;
206
207
  private patch;
207
208
  attach(audio: HTMLAudioElement): void;
@@ -292,6 +293,7 @@ interface NetworkAudioContextValue {
292
293
  notice: string | null;
293
294
  command: (command: NetworkAudioCommand) => void;
294
295
  openNetworkPlayer?: () => void;
296
+ getPosition?: () => number;
295
297
  highlightAvailable: boolean;
296
298
  highlightEnabled: boolean;
297
299
  setHighlightEnabled: (enabled: boolean) => void;
package/dist/audio.js CHANGED
@@ -129,6 +129,131 @@ function wordIndexAt(words, timeMs) {
129
129
  }
130
130
  return ans;
131
131
  }
132
+ var UNSTRESSED_FUNCTION_WORDS = /* @__PURE__ */ new Set([
133
+ "a",
134
+ "an",
135
+ "the",
136
+ "of",
137
+ "to",
138
+ "in",
139
+ "on",
140
+ "at",
141
+ "for",
142
+ "and",
143
+ "or",
144
+ "by",
145
+ "as",
146
+ "is",
147
+ "was",
148
+ "were",
149
+ "be",
150
+ "are",
151
+ "with",
152
+ "from",
153
+ "that",
154
+ "this"
155
+ ]);
156
+ var DASH_TOKENS = /* @__PURE__ */ new Set(["\u2014", "\u2013", "--"]);
157
+ function isClauseEnd(sent, i) {
158
+ const n = sent.length;
159
+ if (i === n - 1) return true;
160
+ const stripped = sent[i][0].replace(/["'\u201d\u2019)\]}]+$/u, "");
161
+ if (DASH_TOKENS.has(stripped)) return true;
162
+ if (/[,;:\u2014\u2013]$/u.test(stripped)) {
163
+ if (i + 1 < n && DASH_TOKENS.has(sent[i + 1][0])) return false;
164
+ if (i + 2 < n && DASH_TOKENS.has(sent[i + 2][0])) return false;
165
+ return true;
166
+ }
167
+ return false;
168
+ }
169
+ function rebalanceLegacySentence(sent) {
170
+ const n = sent.length;
171
+ const sStart = sent[0][1];
172
+ const sEnd = sent[n - 1][2];
173
+ const sDur = sEnd - sStart;
174
+ if (n <= 1 || sDur < 600) return sent;
175
+ const clauses = [];
176
+ let startIdx = 0;
177
+ for (let i = 0; i < n; i++) {
178
+ if (isClauseEnd(sent, i)) {
179
+ clauses.push([startIdx, i]);
180
+ startIdx = i + 1;
181
+ }
182
+ }
183
+ const clauseBounds = clauses.map(([i0, i1]) => [sent[i0][1], sent[i1][2]]);
184
+ if (clauses.length >= 2) {
185
+ const [, i1] = clauses[0];
186
+ if (DASH_TOKENS.has(sent[i1][0]) && i1 <= 5) {
187
+ const c0Dur = clauseBounds[0][1] - clauseBounds[0][0];
188
+ const shift = Math.min(450, c0Dur * 0.24);
189
+ clauseBounds[0][1] -= shift;
190
+ clauseBounds[1][0] -= shift;
191
+ }
192
+ }
193
+ const out = [];
194
+ for (let c = 0; c < clauses.length; c++) {
195
+ const [i0, i1] = clauses[c];
196
+ const [cStart, cEnd] = clauseBounds[c];
197
+ const cDur = cEnd - cStart;
198
+ const cLen = i1 - i0 + 1;
199
+ const pauseMs = cLen >= 2 && cDur > 450 ? Math.min(340, cDur * 0.24) : cDur > 300 ? Math.min(180, cDur * 0.18) : 0;
200
+ const speechDur = Math.max(cDur - pauseMs, cDur * 0.72);
201
+ const weights = [];
202
+ for (let idx = i0; idx <= i1; idx++) {
203
+ const [w, st, en] = sent[idx];
204
+ const oldD = Math.max(en - st, 40);
205
+ const stripped = w.replace(/["'\u201d\u2019)\]}]+$/u, "");
206
+ const clean = stripped.replace(/^["'\u201c\u2018([{]+/u, "").replace(/[.,;:!?\u2014\u2013-]+$/u, "").toLowerCase();
207
+ if (DASH_TOKENS.has(w)) {
208
+ weights.push(30);
209
+ } else {
210
+ let weight = Math.pow(oldD, 0.85);
211
+ if (UNSTRESSED_FUNCTION_WORDS.has(clean) && stripped.toLowerCase() === clean) {
212
+ weight *= 0.58;
213
+ }
214
+ weights.push(weight);
215
+ }
216
+ }
217
+ const totW = weights.reduce((a, b) => a + b, 0) || 1;
218
+ let cur = cStart;
219
+ for (let k = 0; k < cLen; k++) {
220
+ const idx = i0 + k;
221
+ const [w, , , si] = sent[idx];
222
+ const wDur = speechDur * (weights[k] / totW);
223
+ const lead = idx > 0 ? Math.min(45, wDur * 0.22) : 0;
224
+ const wSt = Math.max(sStart, cur - lead);
225
+ const wEn = cur + wDur;
226
+ out.push([w, Math.round(wSt), Math.round(Math.max(wSt, wEn)), si]);
227
+ cur = wEn;
228
+ }
229
+ }
230
+ return out;
231
+ }
232
+ function rebalanceSentenceTimings(words) {
233
+ if (words.length <= 1) return words;
234
+ const out = [];
235
+ let i = 0;
236
+ while (i < words.length) {
237
+ let j = i + 1;
238
+ const sIdx = words[i][3];
239
+ while (j < words.length && words[j][3] === sIdx) j++;
240
+ const sent = words.slice(i, j);
241
+ let hasExplicitGaps = false;
242
+ for (let k = 0; k < sent.length - 1; k++) {
243
+ if (sent[k + 1][1] - sent[k][2] >= 25) {
244
+ hasExplicitGaps = true;
245
+ break;
246
+ }
247
+ }
248
+ if (!hasExplicitGaps && j < words.length && words[j][1] - sent[sent.length - 1][2] >= 25) {
249
+ hasExplicitGaps = true;
250
+ }
251
+ const processed = hasExplicitGaps ? sent : rebalanceLegacySentence(sent);
252
+ for (const row of processed) out.push(row);
253
+ i = j;
254
+ }
255
+ return out;
256
+ }
132
257
  function mergeLineBoxes(rects) {
133
258
  const lines = [];
134
259
  const sorted = rects.filter((r) => r.width > 0 && r.height > 0).sort((a, b) => a.top - b.top || a.left - b.left);
@@ -173,7 +298,7 @@ var NarrationHighlighter = class {
173
298
  this.resizeObserver = null;
174
299
  this.onWindowResize = null;
175
300
  this.root = root;
176
- this.words = timings.words;
301
+ this.words = rebalanceSentenceTimings(timings.words);
177
302
  const domWords = collectDomWords(root);
178
303
  const matches = alignWords(
179
304
  this.words.map((w) => normalizeWord(w[0])),
@@ -922,6 +1047,12 @@ var NetworkAudioController = class {
922
1047
  this.resumePosition = null;
923
1048
  this.candidates = [];
924
1049
  this.getSnapshot = () => this.snapshot;
1050
+ this.getPosition = () => {
1051
+ if (this.audio && this.source && this.resumePosition === null && finite(this.audio.currentTime)) {
1052
+ if (this.audio.currentTime > 0 || this.snapshot.position === 0) return this.audio.currentTime;
1053
+ }
1054
+ return this.snapshot.position;
1055
+ };
925
1056
  this.subscribe = (listener) => {
926
1057
  this.listeners.add(listener);
927
1058
  return () => {
@@ -1747,6 +1878,7 @@ function NetworkAudioProvider({ children, resolveAudio, connection, host, checkp
1747
1878
  notice,
1748
1879
  command,
1749
1880
  openNetworkPlayer: connection ? openNetworkPlayer : void 0,
1881
+ getPosition: remoteSnapshot !== null ? void 0 : controller.getPosition,
1750
1882
  highlightAvailable,
1751
1883
  highlightEnabled,
1752
1884
  setHighlightEnabled,
@@ -1812,7 +1944,7 @@ function NetworkMediaSession() {
1812
1944
  var subscribeCapabilities = () => () => {
1813
1945
  };
1814
1946
  function useArticleNarration(track, narration) {
1815
- const { snapshot, highlightEnabled, setHighlightEnabled } = useNetworkAudio();
1947
+ const { snapshot, remote, getPosition, highlightEnabled, setHighlightEnabled } = useNetworkAudio();
1816
1948
  const supported = (0, import_react2.useSyncExternalStore)(subscribeCapabilities, narrationHighlightSupported, () => false);
1817
1949
  const registerNarration = (0, import_react2.useContext)(NarrationRegistration);
1818
1950
  const narrationKey = trackKey(track);
@@ -1822,9 +1954,16 @@ function useArticleNarration(track, narration) {
1822
1954
  }, [available, narrationKey, registerNarration]);
1823
1955
  const active = snapshot.queue[snapshot.index];
1824
1956
  const matching = !!active && trackKey(active) === trackKey(track);
1825
- const clock = (0, import_react2.useRef)({ snapshot, received: 0 });
1957
+ const clock = (0, import_react2.useRef)({ snapshot, received: 0, lastPosition: -1, lastStatus: "" });
1826
1958
  (0, import_react2.useEffect)(() => {
1827
- clock.current = { snapshot, received: performance.now() };
1959
+ const prev = clock.current;
1960
+ const changed = snapshot.position !== prev.lastPosition || snapshot.status !== prev.lastStatus;
1961
+ clock.current = {
1962
+ snapshot,
1963
+ received: changed ? performance.now() : prev.received,
1964
+ lastPosition: snapshot.position,
1965
+ lastStatus: snapshot.status
1966
+ };
1828
1967
  }, [snapshot]);
1829
1968
  (0, import_react2.useEffect)(() => {
1830
1969
  if (!narration || !matching || !highlightEnabled || !supported || snapshot.hidden) return;
@@ -1832,9 +1971,13 @@ function useArticleNarration(track, narration) {
1832
1971
  let highlighter = null;
1833
1972
  let frame = 0;
1834
1973
  const render = () => {
1835
- const { snapshot: current, received } = clock.current;
1836
- const elapsed = current.status === "playing" ? Math.min((performance.now() - received) / 1e3, 0.5) * current.rate : 0;
1837
- highlighter?.update((current.position + elapsed) * 1e3);
1974
+ if (!remote && getPosition) {
1975
+ highlighter?.update(getPosition() * 1e3);
1976
+ } else {
1977
+ const { snapshot: current, received } = clock.current;
1978
+ const elapsed = current.status === "playing" ? Math.min((performance.now() - received) / 1e3, 0.5) * current.rate : 0;
1979
+ highlighter?.update((current.position + elapsed) * 1e3);
1980
+ }
1838
1981
  frame = requestAnimationFrame(render);
1839
1982
  };
1840
1983
  const visibility = () => {
package/dist/audio.mjs CHANGED
@@ -99,6 +99,131 @@ function wordIndexAt(words, timeMs) {
99
99
  }
100
100
  return ans;
101
101
  }
102
+ var UNSTRESSED_FUNCTION_WORDS = /* @__PURE__ */ new Set([
103
+ "a",
104
+ "an",
105
+ "the",
106
+ "of",
107
+ "to",
108
+ "in",
109
+ "on",
110
+ "at",
111
+ "for",
112
+ "and",
113
+ "or",
114
+ "by",
115
+ "as",
116
+ "is",
117
+ "was",
118
+ "were",
119
+ "be",
120
+ "are",
121
+ "with",
122
+ "from",
123
+ "that",
124
+ "this"
125
+ ]);
126
+ var DASH_TOKENS = /* @__PURE__ */ new Set(["\u2014", "\u2013", "--"]);
127
+ function isClauseEnd(sent, i) {
128
+ const n = sent.length;
129
+ if (i === n - 1) return true;
130
+ const stripped = sent[i][0].replace(/["'\u201d\u2019)\]}]+$/u, "");
131
+ if (DASH_TOKENS.has(stripped)) return true;
132
+ if (/[,;:\u2014\u2013]$/u.test(stripped)) {
133
+ if (i + 1 < n && DASH_TOKENS.has(sent[i + 1][0])) return false;
134
+ if (i + 2 < n && DASH_TOKENS.has(sent[i + 2][0])) return false;
135
+ return true;
136
+ }
137
+ return false;
138
+ }
139
+ function rebalanceLegacySentence(sent) {
140
+ const n = sent.length;
141
+ const sStart = sent[0][1];
142
+ const sEnd = sent[n - 1][2];
143
+ const sDur = sEnd - sStart;
144
+ if (n <= 1 || sDur < 600) return sent;
145
+ const clauses = [];
146
+ let startIdx = 0;
147
+ for (let i = 0; i < n; i++) {
148
+ if (isClauseEnd(sent, i)) {
149
+ clauses.push([startIdx, i]);
150
+ startIdx = i + 1;
151
+ }
152
+ }
153
+ const clauseBounds = clauses.map(([i0, i1]) => [sent[i0][1], sent[i1][2]]);
154
+ if (clauses.length >= 2) {
155
+ const [, i1] = clauses[0];
156
+ if (DASH_TOKENS.has(sent[i1][0]) && i1 <= 5) {
157
+ const c0Dur = clauseBounds[0][1] - clauseBounds[0][0];
158
+ const shift = Math.min(450, c0Dur * 0.24);
159
+ clauseBounds[0][1] -= shift;
160
+ clauseBounds[1][0] -= shift;
161
+ }
162
+ }
163
+ const out = [];
164
+ for (let c = 0; c < clauses.length; c++) {
165
+ const [i0, i1] = clauses[c];
166
+ const [cStart, cEnd] = clauseBounds[c];
167
+ const cDur = cEnd - cStart;
168
+ const cLen = i1 - i0 + 1;
169
+ const pauseMs = cLen >= 2 && cDur > 450 ? Math.min(340, cDur * 0.24) : cDur > 300 ? Math.min(180, cDur * 0.18) : 0;
170
+ const speechDur = Math.max(cDur - pauseMs, cDur * 0.72);
171
+ const weights = [];
172
+ for (let idx = i0; idx <= i1; idx++) {
173
+ const [w, st, en] = sent[idx];
174
+ const oldD = Math.max(en - st, 40);
175
+ const stripped = w.replace(/["'\u201d\u2019)\]}]+$/u, "");
176
+ const clean = stripped.replace(/^["'\u201c\u2018([{]+/u, "").replace(/[.,;:!?\u2014\u2013-]+$/u, "").toLowerCase();
177
+ if (DASH_TOKENS.has(w)) {
178
+ weights.push(30);
179
+ } else {
180
+ let weight = Math.pow(oldD, 0.85);
181
+ if (UNSTRESSED_FUNCTION_WORDS.has(clean) && stripped.toLowerCase() === clean) {
182
+ weight *= 0.58;
183
+ }
184
+ weights.push(weight);
185
+ }
186
+ }
187
+ const totW = weights.reduce((a, b) => a + b, 0) || 1;
188
+ let cur = cStart;
189
+ for (let k = 0; k < cLen; k++) {
190
+ const idx = i0 + k;
191
+ const [w, , , si] = sent[idx];
192
+ const wDur = speechDur * (weights[k] / totW);
193
+ const lead = idx > 0 ? Math.min(45, wDur * 0.22) : 0;
194
+ const wSt = Math.max(sStart, cur - lead);
195
+ const wEn = cur + wDur;
196
+ out.push([w, Math.round(wSt), Math.round(Math.max(wSt, wEn)), si]);
197
+ cur = wEn;
198
+ }
199
+ }
200
+ return out;
201
+ }
202
+ function rebalanceSentenceTimings(words) {
203
+ if (words.length <= 1) return words;
204
+ const out = [];
205
+ let i = 0;
206
+ while (i < words.length) {
207
+ let j = i + 1;
208
+ const sIdx = words[i][3];
209
+ while (j < words.length && words[j][3] === sIdx) j++;
210
+ const sent = words.slice(i, j);
211
+ let hasExplicitGaps = false;
212
+ for (let k = 0; k < sent.length - 1; k++) {
213
+ if (sent[k + 1][1] - sent[k][2] >= 25) {
214
+ hasExplicitGaps = true;
215
+ break;
216
+ }
217
+ }
218
+ if (!hasExplicitGaps && j < words.length && words[j][1] - sent[sent.length - 1][2] >= 25) {
219
+ hasExplicitGaps = true;
220
+ }
221
+ const processed = hasExplicitGaps ? sent : rebalanceLegacySentence(sent);
222
+ for (const row of processed) out.push(row);
223
+ i = j;
224
+ }
225
+ return out;
226
+ }
102
227
  function mergeLineBoxes(rects) {
103
228
  const lines = [];
104
229
  const sorted = rects.filter((r) => r.width > 0 && r.height > 0).sort((a, b) => a.top - b.top || a.left - b.left);
@@ -143,7 +268,7 @@ var NarrationHighlighter = class {
143
268
  this.resizeObserver = null;
144
269
  this.onWindowResize = null;
145
270
  this.root = root;
146
- this.words = timings.words;
271
+ this.words = rebalanceSentenceTimings(timings.words);
147
272
  const domWords = collectDomWords(root);
148
273
  const matches = alignWords(
149
274
  this.words.map((w) => normalizeWord(w[0])),
@@ -901,6 +1026,12 @@ var NetworkAudioController = class {
901
1026
  this.resumePosition = null;
902
1027
  this.candidates = [];
903
1028
  this.getSnapshot = () => this.snapshot;
1029
+ this.getPosition = () => {
1030
+ if (this.audio && this.source && this.resumePosition === null && finite(this.audio.currentTime)) {
1031
+ if (this.audio.currentTime > 0 || this.snapshot.position === 0) return this.audio.currentTime;
1032
+ }
1033
+ return this.snapshot.position;
1034
+ };
904
1035
  this.subscribe = (listener) => {
905
1036
  this.listeners.add(listener);
906
1037
  return () => {
@@ -1726,6 +1857,7 @@ function NetworkAudioProvider({ children, resolveAudio, connection, host, checkp
1726
1857
  notice,
1727
1858
  command,
1728
1859
  openNetworkPlayer: connection ? openNetworkPlayer : void 0,
1860
+ getPosition: remoteSnapshot !== null ? void 0 : controller.getPosition,
1729
1861
  highlightAvailable,
1730
1862
  highlightEnabled,
1731
1863
  setHighlightEnabled,
@@ -1791,7 +1923,7 @@ function NetworkMediaSession() {
1791
1923
  var subscribeCapabilities = () => () => {
1792
1924
  };
1793
1925
  function useArticleNarration(track, narration) {
1794
- const { snapshot, highlightEnabled, setHighlightEnabled } = useNetworkAudio();
1926
+ const { snapshot, remote, getPosition, highlightEnabled, setHighlightEnabled } = useNetworkAudio();
1795
1927
  const supported = useSyncExternalStore2(subscribeCapabilities, narrationHighlightSupported, () => false);
1796
1928
  const registerNarration = useContext(NarrationRegistration);
1797
1929
  const narrationKey = trackKey(track);
@@ -1801,9 +1933,16 @@ function useArticleNarration(track, narration) {
1801
1933
  }, [available, narrationKey, registerNarration]);
1802
1934
  const active = snapshot.queue[snapshot.index];
1803
1935
  const matching = !!active && trackKey(active) === trackKey(track);
1804
- const clock = useRef2({ snapshot, received: 0 });
1936
+ const clock = useRef2({ snapshot, received: 0, lastPosition: -1, lastStatus: "" });
1805
1937
  useEffect2(() => {
1806
- clock.current = { snapshot, received: performance.now() };
1938
+ const prev = clock.current;
1939
+ const changed = snapshot.position !== prev.lastPosition || snapshot.status !== prev.lastStatus;
1940
+ clock.current = {
1941
+ snapshot,
1942
+ received: changed ? performance.now() : prev.received,
1943
+ lastPosition: snapshot.position,
1944
+ lastStatus: snapshot.status
1945
+ };
1807
1946
  }, [snapshot]);
1808
1947
  useEffect2(() => {
1809
1948
  if (!narration || !matching || !highlightEnabled || !supported || snapshot.hidden) return;
@@ -1811,9 +1950,13 @@ function useArticleNarration(track, narration) {
1811
1950
  let highlighter = null;
1812
1951
  let frame = 0;
1813
1952
  const render = () => {
1814
- const { snapshot: current, received } = clock.current;
1815
- const elapsed = current.status === "playing" ? Math.min((performance.now() - received) / 1e3, 0.5) * current.rate : 0;
1816
- highlighter?.update((current.position + elapsed) * 1e3);
1953
+ if (!remote && getPosition) {
1954
+ highlighter?.update(getPosition() * 1e3);
1955
+ } else {
1956
+ const { snapshot: current, received } = clock.current;
1957
+ const elapsed = current.status === "playing" ? Math.min((performance.now() - received) / 1e3, 0.5) * current.rate : 0;
1958
+ highlighter?.update((current.position + elapsed) * 1e3);
1959
+ }
1817
1960
  frame = requestAnimationFrame(render);
1818
1961
  };
1819
1962
  const visibility = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/nextjs",
3
- "version": "0.1.67",
3
+ "version": "0.1.68",
4
4
  "description": "ScaleMule SDK for Next.js applications - authentication, storage, and user management",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",