@cartanova/qgrid-cli 2.16.0 → 2.17.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.
Files changed (36) hide show
  1. package/bundle/dist/application/qgrid/qgrid.frame.js +63 -25
  2. package/bundle/dist/application/qgrid/qgrid.types.js +11 -3
  3. package/bundle/dist/utils/providers/grok/grok-types.js +8 -2
  4. package/bundle/dist/utils/providers/grok/grok-voice-client.js +4 -4
  5. package/bundle/dist/utils/speech-alignment.js +129 -0
  6. package/bundle/src/application/qgrid/qgrid.frame.test.ts +57 -7
  7. package/bundle/src/application/qgrid/qgrid.frame.ts +67 -30
  8. package/bundle/src/application/qgrid/qgrid.types.ts +17 -2
  9. package/bundle/src/utils/providers/grok/grok-types.ts +15 -10
  10. package/bundle/src/utils/providers/grok/grok-voice-client.ts +4 -4
  11. package/bundle/src/utils/speech-alignment.test.ts +72 -0
  12. package/bundle/src/utils/speech-alignment.ts +178 -0
  13. package/bundle/web-dist/client/assets/TimedPlayer-cuEgYA3Q.js +1 -0
  14. package/bundle/web-dist/client/assets/{index-SGfYBzGT.js → index-Cb14ZYfq.js} +3 -3
  15. package/bundle/web-dist/client/assets/index-Co8uAhSV.css +1 -0
  16. package/bundle/web-dist/client/assets/{logs-DrTz38re.js → logs-CSL1j0RN.js} +1 -1
  17. package/bundle/web-dist/client/assets/{monit-klIY-HEI.js → monit-J69xfniH.js} +1 -1
  18. package/bundle/web-dist/client/assets/{routes-bGN3y2NW.js → routes-CdWdhivH.js} +1 -1
  19. package/bundle/web-dist/client/assets/{settings-BHwEvAZL.js → settings-BGa41_nL.js} +1 -1
  20. package/bundle/web-dist/client/assets/show-CSpPVL8-.js +2 -0
  21. package/bundle/web-dist/client/assets/{tokens-DMWMG9O5.js → tokens-B9FeVPJk.js} +1 -1
  22. package/bundle/web-dist/client/assets/{use-oauth-login-flow-D1v4h9zA.js → use-oauth-login-flow-Tmy4lKYv.js} +1 -1
  23. package/bundle/web-dist/client/assets/{use-update-token-mutation-BVBUlqBN.js → use-update-token-mutation-CGwI6TU8.js} +1 -1
  24. package/bundle/web-dist/client/assets/voice-BNcGUdUd.js +1 -0
  25. package/bundle/web-dist/client/index.html +2 -2
  26. package/bundle/web-dist/server/assets/TimedPlayer-DAwjkP4z.js +54 -0
  27. package/bundle/web-dist/server/assets/{show-D8r2sVK0.js → show-hBbPOUZD.js} +41 -6
  28. package/bundle/web-dist/server/assets/{voice-C2HfwkGp.js → voice-CHoPEIfo.js} +76 -68
  29. package/bundle/web-dist/server/entry-server.generated.js +3 -3
  30. package/package.json +1 -1
  31. package/skills/qgrid/references/ai-sdk-provider-contract.md +1 -1
  32. package/skills/qgrid/references/grok-runtime.md +5 -1
  33. package/skills/qgrid/references/sonamu-api-web-flow.md +2 -2
  34. package/bundle/web-dist/client/assets/index-DyrPRt6O.css +0 -1
  35. package/bundle/web-dist/client/assets/show-BiJkyrfB.js +0 -2
  36. package/bundle/web-dist/client/assets/voice-CwD3asSF.js +0 -1
@@ -0,0 +1,178 @@
1
+ /** A word an STT model heard, with its time span in seconds. */
2
+ export interface TimedWord {
3
+ text: string;
4
+ start: number;
5
+ end: number;
6
+ }
7
+
8
+ /** Per-character timing in the shape Grok TTS returns (`with_timestamps`). */
9
+ export interface CharTimestamps {
10
+ graphChars: string[];
11
+ graphTimes: Array<[number, number]>;
12
+ }
13
+
14
+ const normalize = (word: string) =>
15
+ word
16
+ .normalize("NFKC")
17
+ .toLowerCase()
18
+ .replace(/[^\p{L}\p{N}']/gu, "");
19
+
20
+ /**
21
+ * Infers per-character timing for a known script from the words an STT model heard in its audio.
22
+ * Script and heard words are paired by an edit-distance alignment, so a mis-heard word (a name
23
+ * spelled differently, a number written out) still takes the time of the word in its place, and
24
+ * extra heard words (a spoken preamble) are skipped. Script words left unpaired get the gap
25
+ * between their neighbours; each word's span is split over its characters by position, and the
26
+ * whitespace between words takes the pause between them. `durationSec` bounds leading and
27
+ * trailing gaps. `matchedWords` counts script words heard exactly (after normalization).
28
+ */
29
+ export function alignScriptToWords(script: string, words: TimedWord[], durationSec: number) {
30
+ const tokens = [...script.matchAll(/\S+/g)].map((m) => ({
31
+ from: m.index,
32
+ to: m.index + m[0].length,
33
+ norm: normalize(m[0]),
34
+ }));
35
+
36
+ const heard = words.flatMap((w) => {
37
+ const norm = normalize(w.text);
38
+
39
+ return norm === "" ? [] : [{ ...w, norm }];
40
+ });
41
+
42
+ const n = tokens.length;
43
+ const m = heard.length;
44
+ const width = m + 1;
45
+ const cost = new Uint32Array((n + 1) * width);
46
+
47
+ for (let i = 0; i <= n; i++) cost[i * width] = i;
48
+
49
+ for (let j = 0; j <= m; j++) cost[j] = j;
50
+
51
+ for (let i = 1; i <= n; i++) {
52
+ for (let j = 1; j <= m; j++) {
53
+ const same = tokens[i - 1]!.norm === heard[j - 1]!.norm;
54
+
55
+ cost[i * width + j] = Math.min(
56
+ cost[(i - 1) * width + (j - 1)]! + (same ? 0 : 1),
57
+ cost[(i - 1) * width + j]! + 1,
58
+ cost[i * width + (j - 1)]! + 1,
59
+ );
60
+ }
61
+ }
62
+
63
+ // Walk back from the corner; prefer pairing over skipping so each script word keeps a time.
64
+ const spans: Array<[number, number] | undefined> = Array.from({ length: n });
65
+ let matchedWords = 0;
66
+
67
+ for (let i = n, j = m; i > 0 || j > 0;) {
68
+ const here = cost[i * width + j]!;
69
+ const same = i > 0 && j > 0 && tokens[i - 1]!.norm === heard[j - 1]!.norm;
70
+
71
+ if (i > 0 && j > 0 && here === cost[(i - 1) * width + (j - 1)]! + (same ? 0 : 1)) {
72
+ spans[i - 1] = [heard[j - 1]!.start, heard[j - 1]!.end];
73
+
74
+ if (same) matchedWords++;
75
+ i--;
76
+ j--;
77
+ } else if (i > 0 && here === cost[(i - 1) * width + j]! + 1) {
78
+ i--; // script word not heard: filled from its neighbours below
79
+ } else {
80
+ j--; // extra heard word (e.g. a spoken preamble): skipped
81
+ }
82
+ }
83
+
84
+ fillGaps(spans, tokens, durationSec);
85
+
86
+ // Each character of a word takes its share of the word's span; characters between words (spaces)
87
+ // take the pause from the previous word's end to the next word's start.
88
+ const graphChars: string[] = [];
89
+ const graphTimes: Array<[number, number]> = [];
90
+ let token = 0;
91
+ let previousEnd = 0;
92
+
93
+ for (let c = 0; c < script.length;) {
94
+ const char = String.fromCodePoint(script.codePointAt(c)!);
95
+
96
+ while (token < n && tokens[token]!.to <= c) token++;
97
+ const current = tokens[token];
98
+
99
+ if (current && current.from <= c) {
100
+ const [start, end] = spans[token]!;
101
+ const length = current.to - current.from;
102
+ const offset = c - current.from;
103
+
104
+ const span: [number, number] = [
105
+ start + ((end - start) * offset) / length,
106
+ start + ((end - start) * (offset + char.length)) / length,
107
+ ];
108
+
109
+ graphTimes.push(span);
110
+ previousEnd = span[1];
111
+ } else {
112
+ const nextStart = current ? spans[token]![0] : durationSec;
113
+ graphTimes.push([previousEnd, Math.max(previousEnd, nextStart)]);
114
+ }
115
+
116
+ graphChars.push(char);
117
+ c += char.length;
118
+ }
119
+
120
+ return { audioTimestamps: { graphChars, graphTimes }, matchedWords, scriptWords: n };
121
+ }
122
+
123
+ /** Gives unpaired script words a slice of the gap between paired neighbours, by length. */
124
+ function fillGaps(
125
+ spans: Array<[number, number] | undefined>,
126
+ tokens: Array<{ from: number; to: number }>,
127
+ durationSec: number,
128
+ ): void {
129
+ for (let k = 0; k < spans.length;) {
130
+ if (spans[k]) {
131
+ k++;
132
+ continue;
133
+ }
134
+
135
+ let last = k;
136
+
137
+ while (last + 1 < spans.length && !spans[last + 1]) last++;
138
+ const gapStart = k > 0 ? spans[k - 1]![1] : 0;
139
+ const gapEnd = last + 1 < spans.length ? spans[last + 1]![0] : durationSec;
140
+ const lengths = tokens.slice(k, last + 1).map((t) => t.to - t.from);
141
+ const total = lengths.reduce((sum, length) => sum + length, 0);
142
+ let cursor = gapStart;
143
+
144
+ lengths.forEach((length, offset) => {
145
+ const width = ((gapEnd - gapStart) * length) / total;
146
+ spans[k + offset] = [cursor, cursor + width];
147
+ cursor += width;
148
+ });
149
+
150
+ k = last + 1;
151
+ }
152
+ }
153
+
154
+ /** Words from per-character timing: each run of non-space characters, spanning its first to last. */
155
+ export function wordsFromCharTimestamps(timestamps: CharTimestamps): TimedWord[] {
156
+ const words: TimedWord[] = [];
157
+ let current: TimedWord | undefined;
158
+
159
+ timestamps.graphChars.forEach((char, i) => {
160
+ const [start, end] = timestamps.graphTimes[i]!;
161
+
162
+ if (/\s/.test(char)) {
163
+ current = undefined;
164
+
165
+ return;
166
+ }
167
+
168
+ if (current) {
169
+ current.text += char;
170
+ current.end = end;
171
+ } else {
172
+ current = { text: char, start, end };
173
+ words.push(current);
174
+ }
175
+ });
176
+
177
+ return words;
178
+ }
@@ -0,0 +1 @@
1
+ import{r as e}from"./rolldown-runtime-XKyaHtl9.js";import{i as t,r as n}from"./vendor-react-8xo7RSum.js";var r=e(t(),1),i=n();function a({src:e,words:t,autoPlay:n,className:a=`h-8 w-full`}){let o=(0,r.useRef)(null),s=(0,r.useRef)(0),[c,l]=(0,r.useState)(-1),u=()=>{l(o.current?.currentTime??-1),s.current=requestAnimationFrame(u)},d=()=>cancelAnimationFrame(s.current);return(0,r.useEffect)(()=>()=>cancelAnimationFrame(s.current),[]),(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(`audio`,{ref:o,controls:!0,autoPlay:n,src:e,className:a,onPlay:()=>{d(),u()},onPause:d,onTimeUpdate:e=>l(e.currentTarget.currentTime),onEnded:()=>{d(),l(-1)}}),t&&t.length>0&&(0,i.jsx)(`p`,{className:`flex flex-wrap gap-x-0.5 gap-y-1 text-[13px] leading-relaxed`,children:t.map((e,t)=>(0,i.jsx)(`button`,{type:`button`,onClick:()=>{let t=o.current;t&&(t.currentTime=e.start,l(e.start),t.play())},title:`${e.start.toFixed(2)}–${e.end.toFixed(2)}s · play from here`,className:`rounded px-1 transition-colors duration-75 ${c>=e.start&&c<e.end?`bg-sienna-500 text-white`:`text-sand-700 hover:bg-sand-100`}`,children:e.text},t))})]})}export{a as t};