Skip to content

Commit 0c5e778

Browse files
author
github-actions
committed
Merge: make transcript payload authoritative
2 parents 4f317ce + 50dc008 commit 0c5e778

File tree

5 files changed

+56
-25
lines changed

5 files changed

+56
-25
lines changed

index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ <h2>Caption Listener Status</h2>
6161
async function check() {
6262
try {
6363
const res = await fetch(url, { cache: 'no-store' });
64-
render(res.ok);
64+
if (!res.ok) { render(false); return; }
65+
const data = await res.json().catch(() => null);
66+
// Prefer an explicit flag; otherwise check generated timestamp recency
67+
if (data && data.active === true) { render(true); return; }
68+
if (data && data.generated) {
69+
const t = Date.parse(data.generated);
70+
if (!Number.isNaN(t) && (Date.now() - t) < 5000) { render(true); return; }
71+
}
72+
render(false);
6573
} catch {
6674
render(false);
6775
}

scripts/run-whisper.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async function main() {
123123

124124
let transcript = '';
125125
const writeOut = debounce(() => {
126-
const payload = { text: transcript.trim(), updatedAt: new Date().toISOString() };
126+
const payload = { active: true, generated: new Date().toISOString(), text: transcript.trim() };
127127
try {
128128
fs.writeFileSync(outFile, JSON.stringify(payload, null, 2));
129129
} catch (e) {
@@ -155,14 +155,25 @@ async function main() {
155155

156156
const shutdown = () => {
157157
try { child.kill('SIGINT'); } catch {}
158-
writeOut();
158+
// write a final 'inactive' payload so UIs know listener stopped
159+
try {
160+
const final = { active: false, generated: new Date().toISOString(), text: transcript.trim() };
161+
fs.writeFileSync(outFile, JSON.stringify(final, null, 2));
162+
} catch (e) {
163+
// ignore
164+
}
159165
setTimeout(() => process.exit(0), 250);
160166
};
161167
process.on('SIGINT', shutdown);
162168
process.on('SIGTERM', shutdown);
163169

164170
child.on('exit', (code) => {
165-
writeOut();
171+
try {
172+
const final = { active: false, generated: new Date().toISOString(), text: transcript.trim() };
173+
fs.writeFileSync(outFile, JSON.stringify(final, null, 2));
174+
} catch (e) {
175+
// ignore
176+
}
166177
console.log(`\n[run-whisper] whisper-stream exited with code ${code}`);
167178
process.exit(code || 0);
168179
});

scripts/whisper-transcript-watch.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ function toJsonPayload(srcPath, content) {
3535
if (ext === '.json') {
3636
try {
3737
const obj = JSON.parse(content);
38-
if (typeof obj === 'string') return { text: obj, updatedAt: new Date().toISOString() };
39-
if (obj && typeof obj.text === 'string') return { text: obj.text, updatedAt: new Date().toISOString() };
40-
return { text: JSON.stringify(obj), updatedAt: new Date().toISOString() };
38+
if (typeof obj === 'string') return { active: true, generated: new Date().toISOString(), text: obj };
39+
if (obj && typeof obj.text === 'string') return { active: true, generated: new Date().toISOString(), text: obj.text };
40+
// If already an object, try to preserve structure but still include active/generation
41+
return { active: true, generated: new Date().toISOString(), text: typeof obj === 'object' ? JSON.stringify(obj) : String(obj) };
4142
} catch {
4243
// fall through to txt
4344
}
4445
}
45-
return { text: content, updatedAt: new Date().toISOString() };
46+
return { active: true, generated: new Date().toISOString(), text: content };
4647
}
4748

4849
function writeOut(dst, payload) {

slides/captions-button.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,24 +281,33 @@
281281
try {
282282
const res = await fetch('whisper-demo/transcript.json', { cache: 'no-store' });
283283
if (res.ok) {
284-
// Check if file was recently modified (within last 10 seconds)
285-
const lastModified = res.headers.get('Last-Modified');
284+
const data = await res.json().catch(() => null);
286285
let isActive = false;
287-
288-
if (lastModified) {
289-
const modifiedDate = new Date(lastModified);
290-
const now = new Date();
291-
const secondsSinceUpdate = (now - modifiedDate) / 1000;
292-
isActive = secondsSinceUpdate < 10; // File updated in last 10 seconds
293-
console.log('[Captions] Fetch successful - Age:', Math.round(secondsSinceUpdate), 's | Active:', isActive);
286+
287+
// Prefer an explicit active flag from the producer
288+
if (data && data.active === true) {
289+
isActive = true;
290+
} else if (data && data.generated) {
291+
const t = Date.parse(data.generated);
292+
if (!Number.isNaN(t)) {
293+
const secondsSince = (Date.now() - t) / 1000;
294+
isActive = secondsSince < 10; // recent within 10s
295+
console.log('[Captions] Data generated age:', Math.round(secondsSince), 's | Active:', isActive);
296+
}
297+
} else {
298+
// Fallback: last-modified header as a hint
299+
const lastModified = res.headers.get('Last-Modified');
300+
if (lastModified) {
301+
const modifiedDate = new Date(lastModified);
302+
const secondsSinceUpdate = (Date.now() - modifiedDate) / 1000;
303+
isActive = secondsSinceUpdate < 10;
304+
console.log('[Captions] Last-Modified age hint:', Math.round(secondsSinceUpdate), 's | Active:', isActive);
305+
}
294306
}
295-
296-
const data = await res.json();
297-
console.log('[Captions] Data received, length:', data.text?.length || 0);
298-
// Button shows active status based on file modification time
307+
308+
console.log('[Captions] Data received, text length:', data?.text?.length || 0);
299309
updateCaptionButton(isActive);
300-
// But always show the latest transcript text (even if slightly old)
301-
updateLiveTranscript(data.text);
310+
updateLiveTranscript(data?.text || '');
302311
} else {
303312
console.log('[Captions] Fetch failed:', res.status);
304313
updateCaptionButton(false);

whisper-demo/transcript.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"transcript": [],
3-
"generated": "placeholder for CI link-checks"
2+
"active": false,
3+
"generated": "2026-02-13T00:00:00Z",
4+
"text": "",
5+
"note": "placeholder for CI link-checks"
46
}

0 commit comments

Comments
 (0)